1 #ifndef LIB_SIGNALS_H
2 #define LIB_SIGNALS_H
3 
4 #include <signal.h>
5 
6 enum libsig_flags {
7 	/* Signal handler will be called later from IO loop when it's safe to
8 	   do any kind of work */
9 	LIBSIG_FLAG_DELAYED	= 0x01,
10 	/* Restart syscalls instead of having them fail with EINTR */
11 	LIBSIG_FLAG_RESTART	= 0x02,
12 	/* Automatically shift delayed signal handling for this signal
13 	   to a newly started ioloop. */
14 	LIBSIG_FLAG_IOLOOP_AUTOMOVE = 0x04,
15 };
16 #define LIBSIG_FLAGS_SAFE (LIBSIG_FLAG_DELAYED | LIBSIG_FLAG_RESTART)
17 
18 typedef void signal_handler_t(const siginfo_t *si, void *context);
19 
20 /* Number of times a "termination signal" has been received.
21    These signals are SIGINT, SIGQUIT and SIGTERM. Callers can compare this to
22    their saved previous value to see if a syscall returning EINTR should be
23    treated as someone wanting to end the process or just some internal signal
24    that should be ignored, such as SIGCHLD.
25 
26    This is marked as volatile so that compiler won't optimize away its
27    comparisons. It may not work perfectly everywhere, such as when accessing it
28    isn't atomic, so you shouldn't heavily rely on its actual value. */
29 extern volatile unsigned int signal_term_counter;
30 
31 /* Convert si_code to string */
32 const char *lib_signal_code_to_str(int signo, int sicode);
33 
34 /* Detach IOs from all ioloops. This isn't normally necessary, except when
35    forking a process. */
36 void lib_signals_ioloop_detach(void);
37 void lib_signals_ioloop_attach(void);
38 
39 /* Set signal handler for specific signal. */
40 void lib_signals_set_handler(int signo, enum libsig_flags flags,
41 			     signal_handler_t *handler, void *context)
42 	ATTR_NULL(4);
43 /* Ignore given signal. */
44 void lib_signals_ignore(int signo, bool restart_syscalls);
45 /* Clear all signal handlers for a specific signal and set the signal to be
46    ignored. */
47 void lib_signals_clear_handlers_and_ignore(int signo);
48 /* Unset specific signal handler for specific signal. */
49 void lib_signals_unset_handler(int signo,
50 			       signal_handler_t *handler, void *context)
51 	ATTR_NULL(3);
52 
53 /* Indicate whether signals are expected for the indicated delayed handler. When
54    signals are expected, the io for delayed handlers will be allowed to wait
55    alone on the ioloop.  */
56 void lib_signals_set_expected(int signo, bool expected,
57 			      signal_handler_t *handler, void *context);
58 	ATTR_NULL(4);
59 
60 /* Switch ioloop for a specific signal handler created with
61    LIBSIG_FLAG_NO_IOLOOP_AUTOMOVE. */
62 void lib_signals_switch_ioloop(int signo,
63 			       signal_handler_t *handler, void *context);
64 
65 /* Log a syscall error inside a (non-delayed) signal handler where i_error() is
66    unsafe. errno number will be appended to the prefix. */
67 void lib_signals_syscall_error(const char *prefix);
68 
69 void lib_signals_init(void);
70 void lib_signals_deinit(void);
71 
72 #endif
73