def forward_all_signals(target_pid, process_name): def forwarding_signal_handler(signum): logging.debug("Forwarding signal %d to process %s.", signum, process_name) try: os.kill(forwarding_signal_handler.target_pid, signum) except OSError as e: logging.debug("Could not forward signal %d to process %s: %s", signum, process_name, e) # Somehow we get a Python SystemError sometimes if we access target_pid directly from inside function. forwarding_signal_handler.target_pid = target_pid for signum in _FORWARDABLE_SIGNALS: # Need to directly access libc function, # the state of the signal module is incorrect due to the clone() # (it may think we are in a different thread than the main thread). libc.signal(signum, forwarding_signal_handler)
def forward_all_signals_async(target_pid, process_name): """Install all signal handler that forwards all signals to the given process.""" def forwarding_signal_handler(signum): _forward_signal(signum, process_name, forwarding_signal_handler.target_pid) # Somehow we get a Python SystemError sometimes if we access target_pid directly from inside function. forwarding_signal_handler.target_pid = target_pid for signum in _FORWARDABLE_SIGNALS: # Need to directly access libc function, # the state of the signal module is incorrect due to the clone() # (it may think we are in a different thread than the main thread). libc.signal(signum, forwarding_signal_handler) # Reactivate delivery of signals such that our handler gets called. reset_signal_handling()