clone process support in python

后端 未结 2 1469
日久生厌
日久生厌 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:56

    Well, finally I think I got the answer, is just usign ctypes with libc,

    This is a simple proof of concept:

    from ctypes import *
    
    libc = CDLL("libc.so.6")
    
    # Create stack.                                                                                                                                                                            
    stack = c_char_p(" " * 8096)
    
    def f():
        print libc.getpid()
        return 0
    
    # Conver function to c type returning an integer.                                                                                                                                          
    f_c = CFUNCTYPE(c_int)(f)
    
    # We need the top of the stack.                                                                                                                                                            
    stack_top = c_void_p(cast(stack, c_void_p).value + 8096)
    
    # Call clone with the NEWPID Flag                                                                                                                                                          
    libc.clone(f_c, stack_top, 0x20000000)
    

    This has to be run by root.

提交回复
热议问题