我目前正在开发一个在线 C/C++/汇编编译器,我偶然发现了一个名为 libsandbox 的好软件。这使我能够运行在线编写的代码,对其进行编译并拦截系统调用(如果有)。
首先,我对 Linux 环境还是个新手。我已经下载了 tar.gz,解压它,配置它并进行安装。运行时没有任何错误,但现在我运行起来很困难。我应该如何在这个沙箱中运行 C/C++ 程序?我必须向其提供 .c/.cpp 文件吗?编译后的可执行文件?
这可能是一个非常愚蠢的问题。我在互联网上搜索了如何执行此操作,并阅读了包含的自述文件,但他们没有给我任何线索。
提前致谢!
沙箱仅适用于 Linux。您实际上必须首先使用库函数创建沙箱,然后告诉沙箱运行您的程序。
这个 python 示例展示了如何从 python 中执行此操作。 “#targeted program”行显示您将在何处指定实际应用程序的名称。
def main(args):
# sandbox configuration
cookbook = {
'args': args[1:], # targeted program
'stdin': sys.stdin, # input to targeted program
'stdout': sys.stdout, # output from targeted program
'stderr': sys.stderr, # error from targeted program
'quota': dict(wallclock = 30000,# 30 sec
cpu = 2000, # 2 sec
memory = 8388608, # 8 MB
disk = 1048576)} # 1 MB
# create a sandbox instance and execute till end
msb = MiniSandbox(**cookbook)
msb.run()
# verbose statistics
sys.stderr.write("result: %(result)s\ncpu: %(cpu)dms\nmem: %(mem)dkB\n" % \
msb.probe())
return os.EX_OK
我建议访问 libsandbox 下载页面并获取完整的 example2.py 文件,然后使用 python 脚本运行沙箱。这比编写 C++ 或 C 程序来为您完成这件事更容易。
So...
编写您的 C 或 C++ 程序。不要将其链接到 LIBSANDBOX。
确保你已经安装了 python。
从 libsandbox 页面运行示例 python 脚本。
python 脚本将为您加载 libsandbox。然后它将运行您在沙箱中构建的程序。
Simple.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)