clone process support in python

后端 未结 2 1509
日久生厌
日久生厌 2021-02-13 22:21

Is there any support of the syscall clone(2) (not os.fork) in Python? I want to play with Linux namespaces under Python but it seems that there is not a l

2条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 22:47

    If you want to have "fork() but with new namespace" semantics, you can directly call the SYS_clone syscall instead. Note that if you do so, the os.getpid() method will return the wrong process ID in the child because glibc caches the process ID and doesn't know about the SYS_clone invocation to invalidate its cache.

    Assuming x86_64 (NR_clone == 56, NR_getpid == 39), you can call libc.syscall(56, signal.SIGCHLD|0x000200000, 0 0 0) to "fork", and then libc.syscall(39) to get the current PID of the "forked" child process.

提交回复
热议问题