简体   繁体   中英

Python signal handler not work in child process after linux clone syscall

need some help, experts.

I found a topic in stackoverflow.com, clone process support in python . So like it suggest, I call a function to create child process in new linux namespace.

def ns_clone(func):
    stack = ctypes.c_char_p((' ' * STACK_SIZE).encode("utf_8"))
    stack_top = ctypes.cast(stack, ctypes.c_void_p).value + STACK_SIZE
    pid = libc.clone(ctypes.CFUNCTYPE(None)(func), ctypes.c_void_p(stack_top),
                     CLONE_NEWNS | CLONE_NEWUTS  | CLONE_NEWUTS | signal.SIGCHLD) 
    if pid < 0:
        libc.perror("clone")

It the clone is successful. And the new namespace also works fine. But I meet some problems: In child process, I want to fork more jailed processes to serve. So I try to set the handler for SIGALRM and SIGCLD (I want to handle request timeout and perception grandson process terminated). But it never work. No error raised, but seems this python child process does not care what I set.

signal.signal(signal.SIGALRM, _timeout_callback)
signal.signal(signal.SIGCLD, _child_term_callback)
signal.alarm(15)

I have investigate it for several days. But I'm really a newbie in C language and OS level programming. Only clue I found is that if I use normal os.fork, this problem not exist. Another is in python document, it says the python signal handler is not original OS level handler, and the python handler always run in main thread of one process. But I'm not sure there're some relations.

Is it possible I can set some signal handlers in python process after linux clone syscall? Thanks for help !

My environment: Ubuntu 18.04 Desktop/CPython 3.7

Finally, I use python own "os.fork" to create sub process and use "unshare" to enter namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM