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+ -->
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>
23 <refpurpose>Add a UNIX process signal event source to an event
29 <funcsynopsisinfo>#include
<systemd/sd-event.h
></funcsynopsisinfo>
31 <funcsynopsisinfo><token>typedef
</token> struct sd_event_source sd_event_source;
</funcsynopsisinfo>
34 <funcdef>typedef int (*
<function>sd_event_signal_handler_t
</function>)
</funcdef>
35 <paramdef>sd_event_source *
<parameter>s
</parameter></paramdef>
36 <paramdef>const struct signalfd_siginfo *
<parameter>si
</parameter></paramdef>
37 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
41 <funcdef>int
<function>sd_event_add_signal
</function></funcdef>
42 <paramdef>sd_event *
<parameter>event
</parameter></paramdef>
43 <paramdef>sd_event_source **
<parameter>source
</parameter></paramdef>
44 <paramdef>int
<parameter>signal
</parameter></paramdef>
45 <paramdef>sd_event_signal_handler_t
<parameter>handler
</parameter></paramdef>
46 <paramdef>void *
<parameter>userdata
</parameter></paramdef>
50 <funcdef>int
<function>sd_event_source_get_signal
</function></funcdef>
51 <paramdef>sd_event_source *
<parameter>source
</parameter></paramdef>
58 <title>Description
</title>
60 <para><function>sd_event_add_signal()
</function> adds a new UNIX
61 process signal event source to an event loop. The event loop
62 object is specified in the
<parameter>event
</parameter> parameter,
63 and the event source object is returned in the
64 <parameter>source
</parameter> parameter. The
65 <parameter>signal
</parameter> parameter specifies the numeric
66 signal to be handled (see
<citerefentry
67 project='man-pages'
><refentrytitle>signal
</refentrytitle><manvolnum>7</manvolnum></citerefentry>).
68 The
<parameter>handler
</parameter> parameter must reference a
69 function to call when the signal is received or be
70 <constant>NULL
</constant>. The handler function will be passed
71 the
<parameter>userdata
</parameter> pointer, which may be chosen
72 freely by the caller. The handler also receives a pointer to a
73 <structname>signalfd_siginfo
</structname> structure containing
74 information about the received signal. See
<citerefentry
75 project='man-pages'
><refentrytitle>signalfd
</refentrytitle><manvolnum>2</manvolnum></citerefentry>
76 for further information.
</para>
78 <para>Only a single handler may be installed for a specific
79 signal. The signal will be unblocked by this call, and must be
80 blocked before this function is called in all threads (using
82 project='man-pages'
><refentrytitle>sigprocmask
</refentrytitle><manvolnum>2</manvolnum></citerefentry>). If
83 the handler is not specified (
<parameter>handler
</parameter> is
84 <constant>NULL
</constant>), a default handler which causes the
85 program to exit cleanly will be used.
</para>
87 <para>By default, the event source is enabled permanently
88 (
<constant>SD_EVENT_ON
</constant>), but this may be changed with
89 <citerefentry><refentrytitle>sd_event_source_set_enabled
</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
90 If the handler function returns a negative error code, it will be
91 disabled after the invocation, even if the
92 <constant>SD_EVENT_ON
</constant> mode was requested before.
95 <para>To destroy an event source object use
96 <citerefentry><refentrytitle>sd_event_source_unref
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
97 but note that the event source is only removed from the event loop
98 when all references to the event source are dropped. To make sure
99 an event source does not fire anymore, even if it is still referenced,
100 disable the event source using
101 <citerefentry><refentrytitle>sd_event_source_set_enabled
</refentrytitle><manvolnum>3</manvolnum></citerefentry>
102 with
<constant>SD_EVENT_OFF
</constant>.
</para>
104 <para>If the second parameter of
105 <function>sd_event_add_signal()
</function> is
106 <constant>NULL
</constant> no reference to the event source object
107 is returned. In this case the event source is considered
108 "floating", and will be destroyed implicitly when the event loop
109 itself is destroyed.
</para>
111 <para><function>sd_event_source_get_signal()
</function> returns
112 the configured signal number of an event source created previously
113 with
<function>sd_event_add_signal()
</function>. It takes the
114 event source object as the
<parameter>source
</parameter>
119 <title>Return Value
</title>
121 <para>On success, these functions return
0 or a positive
122 integer. On failure, they return a negative errno-style error
126 <title>Errors
</title>
128 <para>Returned errors may indicate the following problems:
</para>
132 <term><constant>-ENOMEM
</constant></term>
134 <listitem><para>Not enough memory to allocate an object.
</para></listitem>
138 <term><constant>-EINVAL
</constant></term>
140 <listitem><para>An invalid argument has been passed.
</para></listitem>
144 <term><constant>-EBUSY
</constant></term>
146 <listitem><para>A handler is already installed for this
147 signal or the signal was not blocked previously.
</para></listitem>
151 <term><constant>-ESTALE
</constant></term>
153 <listitem><para>The event loop is already terminated.
</para></listitem>
157 <term><constant>-ECHILD
</constant></term>
159 <listitem><para>The event loop has been created in a different process.
</para></listitem>
163 <term><constant>-EDOM
</constant></term>
165 <listitem><para>The passed event source is not a signal event source.
</para></listitem>
172 <xi:include href=
"libsystemd-pkgconfig.xml" />
175 <title>See Also
</title>
178 <citerefentry><refentrytitle>systemd
</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
179 <citerefentry><refentrytitle>sd-event
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
180 <citerefentry><refentrytitle>sd_event_new
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
181 <citerefentry><refentrytitle>sd_event_now
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
182 <citerefentry><refentrytitle>sd_event_add_io
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
183 <citerefentry><refentrytitle>sd_event_add_time
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
184 <citerefentry><refentrytitle>sd_event_add_child
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
185 <citerefentry><refentrytitle>sd_event_add_inotify
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
186 <citerefentry><refentrytitle>sd_event_add_defer
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
187 <citerefentry><refentrytitle>sd_event_source_set_enabled
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
188 <citerefentry><refentrytitle>sd_event_source_set_description
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
189 <citerefentry><refentrytitle>sd_event_source_set_userdata
</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
190 <citerefentry project='man-pages'
><refentrytitle>signal
</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
191 <citerefentry project='man-pages'
><refentrytitle>signalfd
</refentrytitle><manvolnum>2</manvolnum></citerefentry>