]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_event_add_signal.xml
7f2bb28b779d52143566ffc84053055053232a3f
[thirdparty/systemd.git] / man / sd_event_add_signal.xml
1 <?xml version='1.0'?>
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 -->
5
6 <refentry id="sd_event_add_signal" xmlns:xi="http://www.w3.org/2001/XInclude">
7
8 <refentryinfo>
9 <title>sd_event_add_signal</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>sd_event_add_signal</refentrytitle>
15 <manvolnum>3</manvolnum>
16 </refmeta>
17
18 <refnamediv>
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>
23
24 <refpurpose>Add a UNIX process signal event source to an event
25 loop</refpurpose>
26 </refnamediv>
27
28 <refsynopsisdiv>
29 <funcsynopsis>
30 <funcsynopsisinfo>#include &lt;systemd/sd-event.h&gt;</funcsynopsisinfo>
31
32 <funcsynopsisinfo><token>typedef</token> struct sd_event_source sd_event_source;</funcsynopsisinfo>
33
34 <funcsynopsisinfo><constant>SD_EVENT_SIGNAL_PROCMASK</constant></funcsynopsisinfo>
35
36 <funcprototype>
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>
41 </funcprototype>
42
43 <funcprototype>
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>
50 </funcprototype>
51
52 <funcprototype>
53 <funcdef>int <function>sd_event_source_get_signal</function></funcdef>
54 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
55 </funcprototype>
56 </funcsynopsis>
57 </refsynopsisdiv>
58
59 <refsect1>
60 <title>Description</title>
61
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>
67
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
72 <citerefentry
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
77 used.</para>
78
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
82 <citerefentry
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>
88
89 <para>By default, the event source is enabled permanently (<constant>SD_EVENT_ON</constant>), but this
90 may be changed with
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
94 terminate, see
95 <citerefentry><refentrytitle>sd_event_source_set_exit_on_failure</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
96 </para>
97
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>
106
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>
113
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>
119
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>
124 parameter.</para>
125 </refsect1>
126
127 <refsect1>
128 <title>Return Value</title>
129
130 <para>On success, these functions return 0 or a positive
131 integer. On failure, they return a negative errno-style error
132 code.</para>
133
134 <refsect2>
135 <title>Errors</title>
136
137 <para>Returned errors may indicate the following problems:</para>
138
139 <variablelist>
140 <varlistentry>
141 <term><constant>-ENOMEM</constant></term>
142
143 <listitem><para>Not enough memory to allocate an object.</para></listitem>
144 </varlistentry>
145
146 <varlistentry>
147 <term><constant>-EINVAL</constant></term>
148
149 <listitem><para>An invalid argument has been passed.</para></listitem>
150 </varlistentry>
151
152 <varlistentry>
153 <term><constant>-EBUSY</constant></term>
154
155 <listitem><para>A handler is already installed for this
156 signal or the signal was not blocked previously.</para></listitem>
157 </varlistentry>
158
159 <varlistentry>
160 <term><constant>-ESTALE</constant></term>
161
162 <listitem><para>The event loop is already terminated.</para></listitem>
163 </varlistentry>
164
165 <varlistentry>
166 <term><constant>-ECHILD</constant></term>
167
168 <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem>
169 </varlistentry>
170
171 <varlistentry>
172 <term><constant>-EDOM</constant></term>
173
174 <listitem><para>The passed event source is not a signal event source.</para></listitem>
175 </varlistentry>
176
177 </variablelist>
178 </refsect2>
179 </refsect1>
180
181 <xi:include href="libsystemd-pkgconfig.xml" />
182
183 <refsect1>
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>
188 </refsect1>
189
190 <refsect1>
191 <title>See Also</title>
192
193 <para><simplelist type="inline">
194 <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member>
195 <member><citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
196 <member><citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
197 <member><citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
198 <member><citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
199 <member><citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
200 <member><citerefentry><refentrytitle>sd_event_add_child</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
201 <member><citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
202 <member><citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
203 <member><citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
204 <member><citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
205 <member><citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
206 <member><citerefentry><refentrytitle>sd_event_source_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
207 <member><citerefentry project='man-pages'><refentrytitle>signal</refentrytitle><manvolnum>7</manvolnum></citerefentry></member>
208 <member><citerefentry project='man-pages'><refentrytitle>signalfd</refentrytitle><manvolnum>2</manvolnum></citerefentry></member>
209 <member><citerefentry project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry></member>
210 <member><citerefentry project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry></member>
211 </simplelist></para>
212 </refsect1>
213
214 </refentry>