我在一个项目中通过 python-prctl 启用了 seccomp。我无法弄清楚如何干净地退出 - 结果总是被杀死。
我看到了一些使用 ctypes 或 ffi 来尝试引用 libc 的示例,但如果我希望它们使用 WIFEXITED ,它们似乎也有同样的问题。
下面的示例代码。结果总是“我们被杀了”。
def main():
pid = os.fork()
if not pid:
prctl.set_seccomp(True)
os.write(0, 'Hi\n')
# os._exit(0)
# _exit(0)
# sys._exit(0)
# return
# ?!@#(*! What do?
endpid, status = os.waitpid(pid, 0)
print 'Child forked as %d and returned with %d' % (endpid, status)
if not os.WIFEXITED(status):
print 'Exitted abnormally'
if os.WIFSIGNALED:
if os.WTERMSIG(status) == signal.SIGKILL:
print 'We were killed to death'
else:
print 'Returned with %d' % (os.WEXITSTATUS(status))
快速更新,因为我忘记了 libc 的东西:
用其中任何一个定义上面的 _exit() 仍然会导致终止。
# FFI Method
ffi = cffi.FFI()
# Use _exit, which avoids atexit(), etc
ffi.cdef('void _exit(int);')
libc = ffi.dlopen(None)
_exit = libc._exit
.... 或者 ....
# ctypes method
libc = cdll.LoadLibrary('libc-2.18.so')
_exit = libc._exit