在Python中启用seccomp之后,如何干净地退出?

2024-10-01 07:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个项目中通过pythonprctl启用了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(),仍然会导致一个kill。在

^{pr2}$

。。。。或者。。。。在

# ctypes method
libc = cdll.LoadLibrary('libc-2.18.so')
_exit = libc._exit

Tags: 项目示例ifosstatuswithexitnot
1条回答
网友
1楼 · 发布于 2024-10-01 07:31:35

有人帮我找到了答案:

In glibc up to version 2.3, the _exit() wrapper function invoked the kernel system
call of the same name.  Since glibc 2.3, the wrapper function invokes
exit_group(2), in order to terminate all of the threads in a process.

由于“出口包装”退出“组”,而“出口”组不在seccomp筛选器中。。。。它会被杀死。python执行的strace显示exit_group调用。在

相关问题 更多 >