fatal-signal.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* Emergency actions in case of a fatal signal.
  2. Copyright (C) 2003-2004, 2009-2020 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2003.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #ifndef _FATAL_SIGNAL_H
  15. #define _FATAL_SIGNAL_H
  16. #include <signal.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /* It is often useful to do some cleanup action when a usually fatal signal
  21. terminates the process, like removing a temporary file or killing a
  22. subprocess that may be stuck waiting for a device, pipe or network input.
  23. Such signals are SIGHUP, SIGINT, SIGPIPE, SIGTERM, and possibly others.
  24. The limitation of this facility is that it cannot work for SIGKILL.
  25. Signals with a SIG_IGN handler are considered to be non-fatal. The
  26. functions in this file assume that when a SIG_IGN handler is installed
  27. for a signal, it was installed before any functions in this file were
  28. called and it stays so for the whole lifetime of the process. */
  29. /* Register a cleanup function to be executed when a catchable fatal signal
  30. occurs.
  31. Restrictions for the cleanup function:
  32. - The cleanup function can do all kinds of system calls. It may also
  33. modify (clobber) errno.
  34. - It can also access application dependent memory locations and data
  35. structures provided they are in a consistent state. One way to ensure
  36. this is through block_fatal_signals()/unblock_fatal_signals(), see
  37. below. Another - more tricky - way to ensure this is the careful use
  38. of 'volatile'.
  39. However,
  40. - malloc() and similarly complex facilities are not safe to be called
  41. because they are not guaranteed to be in a consistent state.
  42. - Also, the cleanup function must not block the catchable fatal signals
  43. and leave them blocked upon return.
  44. The cleanup function is executed asynchronously. It is unspecified
  45. whether during its execution the catchable fatal signals are blocked
  46. or not. */
  47. extern void at_fatal_signal (_GL_ASYNC_SAFE void (*function) (int sig));
  48. /* Sometimes it is necessary to block the usually fatal signals while the
  49. data structures being accessed by the cleanup action are being built or
  50. reorganized. This is the case, for example, when a temporary file or
  51. directory is created through mkstemp() or mkdtemp(), because these
  52. functions create the temporary file or directory _before_ returning its
  53. name to the application. */
  54. /* Temporarily delay the catchable fatal signals.
  55. The signals will be blocked (= delayed) until the next call to
  56. unblock_fatal_signals(). If the signals are already blocked, a further
  57. call to block_fatal_signals() has no effect. */
  58. extern void block_fatal_signals (void);
  59. /* Stop delaying the catchable fatal signals. */
  60. extern void unblock_fatal_signals (void);
  61. /* Return the list of signals that block_fatal_signals/unblock_fatal_signals
  62. would block or unblock.
  63. Fills signals[0..count-1] and returns count. */
  64. extern unsigned int get_fatal_signals (int signals[64]);
  65. /* Return the list of signals that block_fatal_signals/unblock_fatal_signals
  66. would block or unblock. */
  67. extern const sigset_t * get_fatal_signal_set (void);
  68. #ifdef __cplusplus
  69. }
  70. #endif
  71. #endif /* _FATAL_SIGNAL_H */