1SD_EVENT_ADD_SIGNAL(3) sd_event_add_signal SD_EVENT_ADD_SIGNAL(3)
2
3
4
6 sd_event_add_signal, sd_event_source_get_signal,
7 sd_event_signal_handler_t, SD_EVENT_SIGNAL_PROCMASK - Add a UNIX
8 process signal event source to an event loop
9
11 #include <systemd/sd-event.h>
12
13 typedef struct sd_event_source sd_event_source;
14
15 SD_EVENT_SIGNAL_PROCMASK
16
17 typedef int (*sd_event_signal_handler_t)(sd_event_source *s,
18 const struct signalfd_siginfo *si,
19 void *userdata);
20
21 int sd_event_add_signal(sd_event *event, sd_event_source **source,
22 int signal, sd_event_signal_handler_t handler,
23 void *userdata);
24
25 int sd_event_source_get_signal(sd_event_source *source);
26
28 sd_event_add_signal() adds a new UNIX process signal event source to an
29 event loop. The event loop object is specified in the event parameter,
30 and the event source object is returned in the source parameter. The
31 signal parameter specifies the numeric signal to be handled (see
32 signal(7)).
33
34 The handler parameter is a function to call when the signal is received
35 or NULL. The handler function will be passed the userdata pointer,
36 which may be chosen freely by the caller. The handler also receives a
37 pointer to a signalfd_siginfo structure containing information about
38 the received signal. See signalfd(2) for further information. The
39 handler may return negative to signal an error (see below), other
40 return values are ignored. If handler is NULL, a default handler that
41 calls sd_event_exit(3) will be used.
42
43 Only a single handler may be installed for a specific signal. The
44 signal must be blocked in all threads before this function is called
45 (using sigprocmask(2) or pthread_sigmask(3)). For convenience, if the
46 special flag SD_EVENT_SIGNAL_PROCMASK is ORed into the specified signal
47 the signal will be automatically masked as necessary, for the calling
48 thread. Note that this only works reliably if the signal is already
49 masked in all other threads of the process, or if there are no other
50 threads at the moment of invocation.
51
52 By default, the event source is enabled permanently (SD_EVENT_ON), but
53 this may be changed with sd_event_source_set_enabled(3). If the handler
54 function returns a negative error code, it will either be disabled
55 after the invocation, even if the SD_EVENT_ON mode was requested
56 before, or it will cause the loop to terminate, see
57 sd_event_source_set_exit_on_failure(3).
58
59 To destroy an event source object use sd_event_source_unref(3), but
60 note that the event source is only removed from the event loop when all
61 references to the event source are dropped. To make sure an event
62 source does not fire anymore, even if it is still referenced, disable
63 the event source using sd_event_source_set_enabled(3) with
64 SD_EVENT_OFF.
65
66 If the second parameter of sd_event_add_signal() is NULL no reference
67 to the event source object is returned. In this case the event source
68 is considered "floating", and will be destroyed implicitly when the
69 event loop itself is destroyed.
70
71 If the handler parameter to sd_event_add_signal() is NULL, and the
72 event source fires, this will be considered a request to exit the event
73 loop. In this case, the userdata parameter, cast to an integer, is
74 passed as the exit code parameter to sd_event_exit(3).
75
76 sd_event_source_get_signal() returns the configured signal number of an
77 event source created previously with sd_event_add_signal(). It takes
78 the event source object as the source parameter.
79
81 On success, these functions return 0 or a positive integer. On failure,
82 they return a negative errno-style error code.
83
84 Errors
85 Returned errors may indicate the following problems:
86
87 -ENOMEM
88 Not enough memory to allocate an object.
89
90 -EINVAL
91 An invalid argument has been passed.
92
93 -EBUSY
94 A handler is already installed for this signal or the signal was
95 not blocked previously.
96
97 -ESTALE
98 The event loop is already terminated.
99
100 -ECHILD
101 The event loop has been created in a different process, library or
102 module instance.
103
104 -EDOM
105 The passed event source is not a signal event source.
106
108 Functions described here are available as a shared library, which can
109 be compiled against and linked to with the libsystemd pkg-config(1)
110 file.
111
112 The code described here uses getenv(3), which is declared to be not
113 multi-thread-safe. This means that the code calling the functions
114 described here must not call setenv(3) from a parallel thread. It is
115 recommended to only do calls to setenv() from an early phase of the
116 program when no other threads have been started.
117
119 systemd(1), sd-event(3), sd_event_new(3), sd_event_now(3),
120 sd_event_add_io(3), sd_event_add_time(3), sd_event_add_child(3),
121 sd_event_add_inotify(3), sd_event_add_defer(3),
122 sd_event_source_set_enabled(3), sd_event_source_set_description(3),
123 sd_event_source_set_userdata(3), sd_event_source_set_floating(3),
124 signal(7), signalfd(2), sigprocmask(2), pthread_sigmask(3)
125
126
127
128systemd 254 SD_EVENT_ADD_SIGNAL(3)