signals.py 845 B

1234567891011121314151617181920212223
  1. import os
  2. import signal
  3. def _handle_signal(signal_number, frame): # pragma: no cover
  4. '''
  5. Send the signal to all processes in borgmatic's process group, which includes child processes.
  6. '''
  7. # Prevent infinite signal handler recursion. If the parent frame is this very same handler
  8. # function, we know we're recursing.
  9. if frame.f_back.f_code.co_name == _handle_signal.__name__:
  10. return
  11. os.killpg(os.getpgrp(), signal_number)
  12. def configure_signals(): # pragma: no cover
  13. '''
  14. Configure borgmatic's signal handlers to pass relevant signals through to any child processes
  15. like Borg. Note that SIGINT gets passed through even without these changes.
  16. '''
  17. for signal_number in (signal.SIGHUP, signal.SIGTERM, signal.SIGUSR1, signal.SIGUSR2):
  18. signal.signal(signal_number, _handle_signal)