2 <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
4 <!-- SPDX-License-Identifier: LGPL-2.1-or-later -->
6 <refentry id="sd_event_add_signal" xmlns:xi="http://www.w3.org/2001/XInclude">
9 <title>sd_event_add_signal</title>
10 <productname>systemd</productname>
14 <refentrytitle>sd_event_add_signal</refentrytitle>
15 <manvolnum>3</manvolnum>
19 <refname>sd_event_add_signal</refname>
20 <refname>sd_event_source_get_signal</refname>
21 <refname>sd_event_signal_handler_t</refname>
22 <refname>SD_EVENT_SIGNAL_PROCMASK</refname>
24 <refpurpose>Add a UNIX process signal event source to an event
30 <funcsynopsisinfo>#include <systemd/sd-event.h></funcsynopsisinfo>
32 <funcsynopsisinfo><token>typedef</token> struct sd_event_source sd_event_source;</funcsynopsisinfo>
34 <funcsynopsisinfo><constant>SD_EVENT_SIGNAL_PROCMASK</constant></funcsynopsisinfo>
37 <funcdef>typedef int (*<function>sd_event_signal_handler_t</function>)</funcdef>
38 <paramdef>sd_event_source *<parameter>s</parameter></paramdef>
39 <paramdef>const struct signalfd_siginfo *<parameter>si</parameter></paramdef>
40 <paramdef>void *<parameter>userdata</parameter></paramdef>
44 <funcdef>int <function>sd_event_add_signal</function></funcdef>
45 <paramdef>sd_event *<parameter>event</parameter></paramdef>
46 <paramdef>sd_event_source **<parameter>source</parameter></paramdef>
47 <paramdef>int <parameter>signal</parameter></paramdef>
48 <paramdef>sd_event_signal_handler_t <parameter>handler</parameter></paramdef>
49 <paramdef>void *<parameter>userdata</parameter></paramdef>
53 <funcdef>int <function>sd_event_source_get_signal</function></funcdef>
54 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
60 <title>Description</title>
62 <para><function>sd_event_add_signal()</function> adds a new UNIX process signal event source to an event
63 loop. The event loop object is specified in the <parameter>event</parameter> parameter, and the event
64 source object is returned in the <parameter>source</parameter> parameter. The
65 <parameter>signal</parameter> parameter specifies the numeric signal to be handled (see <citerefentry
66 project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry>).</para>
68 <para>The <parameter>handler</parameter> parameter is a function to call when the signal is received or
69 <constant>NULL</constant>. The handler function will be passed the <parameter>userdata</parameter>
70 pointer, which may be chosen freely by the caller. The handler also receives a pointer to a
71 <structname>signalfd_siginfo</structname> structure containing information about the received signal. See
73 project='man-pages'><refentrytitle>signalfd</refentrytitle><manvolnum>2</manvolnum></citerefentry> for
74 further information. The handler may return negative to signal an error (see below), other return values
75 are ignored. If <parameter>handler</parameter> is <constant>NULL</constant>, a default handler that calls
76 <citerefentry><refentrytitle>sd_event_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry> will be
79 <para>Only a single handler may be installed for a specific signal. The signal must be blocked in all
80 threads before this function is called (using <citerefentry
81 project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry> or
83 project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry>). For
84 convenience, if the special flag <constant>SD_EVENT_SIGNAL_PROCMASK</constant> is ORed into the specified
85 signal the signal will be automatically masked as necessary, for the calling thread. Note that this only
86 works reliably if the signal is already masked in all other threads of the process, or if there are no
87 other threads at the moment of invocation.</para>
89 <para>By default, the event source is enabled permanently (<constant>SD_EVENT_ON</constant>), but this
91 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
92 If the handler function returns a negative error code, it will either be disabled after the invocation,
93 even if the <constant>SD_EVENT_ON</constant> mode was requested before, or it will cause the loop to
95 <citerefentry><refentrytitle>sd_event_source_set_exit_on_failure</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
98 <para>To destroy an event source object use
99 <citerefentry><refentrytitle>sd_event_source_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
100 but note that the event source is only removed from the event loop
101 when all references to the event source are dropped. To make sure
102 an event source does not fire anymore, even if it is still referenced,
103 disable the event source using
104 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>
105 with <constant>SD_EVENT_OFF</constant>.</para>
107 <para>If the second parameter of
108 <function>sd_event_add_signal()</function> is
109 <constant>NULL</constant> no reference to the event source object
110 is returned. In this case the event source is considered
111 "floating", and will be destroyed implicitly when the event loop
112 itself is destroyed.</para>
114 <para>If the <parameter>handler</parameter> parameter to <function>sd_event_add_signal()</function> is
115 <constant>NULL</constant>, and the event source fires, this will be considered a request to exit the
116 event loop. In this case, the <parameter>userdata</parameter> parameter, cast to an integer, is passed as
117 the exit code parameter to
118 <citerefentry><refentrytitle>sd_event_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
120 <para><function>sd_event_source_get_signal()</function> returns
121 the configured signal number of an event source created previously
122 with <function>sd_event_add_signal()</function>. It takes the
123 event source object as the <parameter>source</parameter>
128 <title>Return Value</title>
130 <para>On success, these functions return 0 or a positive
131 integer. On failure, they return a negative errno-style error
135 <title>Errors</title>
137 <para>Returned errors may indicate the following problems:</para>
141 <term><constant>-ENOMEM</constant></term>
143 <listitem><para>Not enough memory to allocate an object.</para></listitem>
147 <term><constant>-EINVAL</constant></term>
149 <listitem><para>An invalid argument has been passed.</para></listitem>
153 <term><constant>-EBUSY</constant></term>
155 <listitem><para>A handler is already installed for this
156 signal or the signal was not blocked previously.</para></listitem>
160 <term><constant>-ESTALE</constant></term>
162 <listitem><para>The event loop is already terminated.</para></listitem>
166 <term><constant>-ECHILD</constant></term>
168 <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
172 <term><constant>-EDOM</constant></term>
174 <listitem><para>The passed event source is not a signal event source.</para></listitem>
181 <xi:include href="libsystemd-pkgconfig.xml" />
184 <title>History</title>
185 <para><function>sd_event_add_signal()</function>,
186 <function>sd_event_signal_handler_t()</function>, and
187 <function>sd_event_source_get_signal()</function> were added in version 217.</para>
191 <title>See Also</title>
194 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
195 <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
196 <citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
197 <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
198 <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
199 <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
200 <citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
201 <citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
202 <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
203 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
204 <citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
205 <citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
206 <citerefentry><refentrytitle>sd_event_source_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
207 <citerefentry project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
208 <citerefentry project='man-pages'><refentrytitle>signalfd</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
209 <citerefentry project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
210 <citerefentry project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry>