CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

70 lines
2.1 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2020 by The CAT-SOOP Developers <catsoop-dev@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. """
  17. Tools for controlling processes
  18. """
  19. import ctypes
  20. import signal
  21. _nodoc = {"i"}
  22. # libc lives in different places on different OS
  23. libc = None
  24. """
  25. The `ctypes.CDLL` object representing libc (which lives in different places on
  26. different platforms). Used in `catsoop.process.set_pdeathsig`.
  27. """
  28. for i in ("libc.so.6", "libc.dylib", "cygwin1.dll", "msys-2.0.dll"):
  29. try:
  30. libc = ctypes.CDLL(i)
  31. except:
  32. pass
  33. if libc is not None:
  34. break
  35. assert libc is not None
  36. def set_pdeathsig(sig=signal.SIGTERM):
  37. """
  38. Create a function that can be used to set the signal that the calling
  39. process receives when its parent process dies (not supported on Mac OSX).
  40. This is used when starting new processes to try to make sure they die if
  41. CAT-SOOP is killed.
  42. **Optional Parameters:**
  43. * `sig` (default `signal.SIGTERM`): the signal to be sent to this process
  44. when its parent dies
  45. **Returns:** a function of no arguments that, when called, sets the parent
  46. process death signal of the calling process to the value given by `sig`.
  47. On Mac OSX, instead returns a function of no arguments that does nothing
  48. when called.
  49. """
  50. if hasattr(libc, "prctl"):
  51. def callable():
  52. return libc.prctl(1, sig)
  53. else:
  54. # on mac osx, there is no such thing as prctl.
  55. def callable():
  56. pass
  57. return callable