]> git.ipfire.org Git - thirdparty/systemd.git/blob - man/sd_event_add_child.xml
man: fix link markup
[thirdparty/systemd.git] / man / sd_event_add_child.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+ -->
5
6 <refentry id="sd_event_add_child" xmlns:xi="http://www.w3.org/2001/XInclude">
7
8 <refentryinfo>
9 <title>sd_event_add_child</title>
10 <productname>systemd</productname>
11 </refentryinfo>
12
13 <refmeta>
14 <refentrytitle>sd_event_add_child</refentrytitle>
15 <manvolnum>3</manvolnum>
16 </refmeta>
17
18 <refnamediv>
19 <refname>sd_event_add_child</refname>
20 <refname>sd_event_add_child_pidfd</refname>
21 <refname>sd_event_source_get_child_pid</refname>
22 <refname>sd_event_source_get_child_pidfd</refname>
23 <refname>sd_event_source_get_child_pidfd_own</refname>
24 <refname>sd_event_source_set_child_pidfd_own</refname>
25 <refname>sd_event_source_get_child_process_own</refname>
26 <refname>sd_event_source_set_child_process_own</refname>
27 <refname>sd_event_source_send_child_signal</refname>
28 <refname>sd_event_child_handler_t</refname>
29
30 <refpurpose>Add a child process state change event source to an event loop</refpurpose>
31 </refnamediv>
32
33 <refsynopsisdiv>
34 <funcsynopsis>
35 <funcsynopsisinfo>#include &lt;systemd/sd-event.h&gt;</funcsynopsisinfo>
36
37 <funcsynopsisinfo><token>typedef</token> struct sd_event_source sd_event_source;</funcsynopsisinfo>
38
39 <funcprototype>
40 <funcdef>typedef int (*<function>sd_event_child_handler_t</function>)</funcdef>
41 <paramdef>sd_event_source *<parameter>s</parameter></paramdef>
42 <paramdef>const siginfo_t *<parameter>si</parameter></paramdef>
43 <paramdef>void *<parameter>userdata</parameter></paramdef>
44 </funcprototype>
45
46 <funcprototype>
47 <funcdef>int <function>sd_event_add_child</function></funcdef>
48 <paramdef>sd_event *<parameter>event</parameter></paramdef>
49 <paramdef>sd_event_source **<parameter>source</parameter></paramdef>
50 <paramdef>pid_t <parameter>pid</parameter></paramdef>
51 <paramdef>int <parameter>options</parameter></paramdef>
52 <paramdef>sd_event_child_handler_t <parameter>handler</parameter></paramdef>
53 <paramdef>void *<parameter>userdata</parameter></paramdef>
54 </funcprototype>
55
56 <funcprototype>
57 <funcdef>int <function>sd_event_add_child_pidfd</function></funcdef>
58 <paramdef>sd_event *<parameter>event</parameter></paramdef>
59 <paramdef>sd_event_source **<parameter>source</parameter></paramdef>
60 <paramdef>int <parameter>pidfd</parameter></paramdef>
61 <paramdef>int <parameter>options</parameter></paramdef>
62 <paramdef>sd_event_child_handler_t <parameter>handler</parameter></paramdef>
63 <paramdef>void *<parameter>userdata</parameter></paramdef>
64 </funcprototype>
65
66 <funcprototype>
67 <funcdef>int <function>sd_event_source_get_child_pid</function></funcdef>
68 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
69 <paramdef>pid_t *<parameter>pid</parameter></paramdef>
70 </funcprototype>
71
72 <funcprototype>
73 <funcdef>int <function>sd_event_source_get_child_pidfd</function></funcdef>
74 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
75 </funcprototype>
76
77 <funcprototype>
78 <funcdef>int <function>sd_event_source_get_child_pidfd_own</function></funcdef>
79 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
80 </funcprototype>
81
82 <funcprototype>
83 <funcdef>int <function>sd_event_source_set_child_pidfd_own</function></funcdef>
84 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
85 <paramdef>int <parameter>own</parameter></paramdef>
86 </funcprototype>
87
88 <funcprototype>
89 <funcdef>int <function>sd_event_source_get_child_process_own</function></funcdef>
90 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
91 </funcprototype>
92
93 <funcprototype>
94 <funcdef>int <function>sd_event_source_set_child_process_own</function></funcdef>
95 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
96 <paramdef>int <parameter>own</parameter></paramdef>
97 </funcprototype>
98
99 <funcprototype>
100 <funcdef>int <function>sd_event_source_send_child_signal</function></funcdef>
101 <paramdef>sd_event_source *<parameter>source</parameter></paramdef>
102 <paramdef>int <parameter>sig</parameter></paramdef>
103 <paramdef>const siginfo_t *<parameter>info</parameter></paramdef>
104 <paramdef>unsigned <parameter>flags</parameter></paramdef>
105 </funcprototype>
106
107 </funcsynopsis>
108 </refsynopsisdiv>
109
110 <refsect1>
111 <title>Description</title>
112
113 <para><function>sd_event_add_child()</function> adds a new child process state change event source to an
114 event loop. The event loop object is specified in the <parameter>event</parameter> parameter, the event
115 source object is returned in the <parameter>source</parameter> parameter. The <parameter>pid</parameter>
116 parameter specifies the PID of the process to watch, which must be a direct child process of the invoking
117 process. The <parameter>handler</parameter> must reference a function to call when the process changes
118 state. The handler function will be passed the <parameter>userdata</parameter> pointer, which may be
119 chosen freely by the caller. The handler also receives a pointer to a <structname>siginfo_t</structname>
120 structure containing information about the child process event. The <parameter>options</parameter>
121 parameter determines which state changes will be watched for. It must contain an OR-ed mask of
122 <constant>WEXITED</constant> (watch for the child process terminating), <constant>WSTOPPED</constant>
123 (watch for the child process being stopped by a signal), and <constant>WCONTINUED</constant> (watch for
124 the child process being resumed by a signal). See <citerefentry
125 project='man-pages'><refentrytitle>waitid</refentrytitle><manvolnum>2</manvolnum></citerefentry> for
126 further information.</para>
127
128 <para>Only a single handler may be installed for a specific
129 child process. The handler is enabled for a single event
130 (<constant>SD_EVENT_ONESHOT</constant>), but this may be changed
131 with
132 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
133 If the handler function returns a negative error code, it will be
134 disabled after the invocation, even if the
135 <constant>SD_EVENT_ON</constant> mode was requested before.
136 </para>
137
138 <para>To destroy an event source object use
139 <citerefentry><refentrytitle>sd_event_source_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
140 but note that the event source is only removed from the event loop
141 when all references to the event source are dropped. To make sure
142 an event source does not fire anymore, even when there's still a
143 reference to it kept, consider setting the event source to
144 <constant>SD_EVENT_OFF</constant> with
145 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para>
146
147 <para>The <constant>SIGCHLD</constant> signal must be blocked in all threads before this function is
148 called (using <citerefentry
149 project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry> or
150 <citerefentry
151 project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry>).</para>
152
153 <para>If the second parameter of
154 <function>sd_event_add_child()</function> is passed as NULL no
155 reference to the event source object is returned. In this case the
156 event source is considered "floating", and will be destroyed
157 implicitly when the event loop itself is destroyed.</para>
158
159 <para>Note that the <parameter>handler</parameter> function is
160 invoked at a time where the child process is not reaped yet (and
161 thus still is exposed as a zombie process by the kernel). However,
162 the child will be reaped automatically after the function
163 returns. Child processes for which no child process state change
164 event sources are installed will not be reaped by the event loop
165 implementation.</para>
166
167 <para>If both a child process state change event source and a
168 <constant>SIGCHLD</constant> signal event source is installed in
169 the same event loop, the configured event source priorities decide
170 which event source is dispatched first. If the signal handler is
171 processed first, it should leave the child processes for which
172 child process state change event sources are installed unreaped.</para>
173
174 <para><function>sd_event_add_child_pidfd()</function> is similar to
175 <function>sd_event_add_child()</function> but takes a file descriptor referencing the process ("pidfd")
176 instead of the numeric PID. A suitable file descriptor may be acquired via <citerefentry
177 project='man-pages'><refentrytitle>pidfd_open</refentrytitle><manvolnum>2</manvolnum></citerefentry> and
178 related calls. The passed file descriptor is not closed when the event source is freed again, unless
179 <function>sd_event_source_set_child_pidfd_own()</function> is used to turn this behaviour on. Note that
180 regardless which of <function>sd_event_add_child()</function> and
181 <function>sd_event_add_child_pidfd()</function> is used for allocating an event source, the watched
182 process has to be a direct child process of the invoking process. Also in both cases
183 <constant>SIGCHLD</constant> has to be blocked in the invoking process.</para>
184
185 <para><function>sd_event_source_get_child_pid()</function>
186 retrieves the configured PID of a child process state change event
187 source created previously with
188 <function>sd_event_add_child()</function>. It takes the event
189 source object as the <parameter>source</parameter> parameter and a
190 pointer to a <type>pid_t</type> variable to return the process ID
191 in.
192 </para>
193
194 <para><function>sd_event_source_get_child_pidfd()</function> retrieves the file descriptor referencing
195 the watched process ("pidfd") if this functionality is available. On kernels that support the concept the
196 event loop will make use of pidfds to watch child processes, regardless if the individual event sources
197 are allocated via <function>sd_event_add_child()</function> or
198 <function>sd_event_add_child_pidfd()</function>. If the latter call was used to allocate the event
199 source, this function returns the file descriptor used for allocation. On kernels that do not support the
200 pidfd concept this function will fail with <constant>EOPNOTSUPP</constant>. This call takes the event
201 source object as the <parameter>source</parameter> parameter and returns the numeric file descriptor.
202 </para>
203
204 <para><function>sd_event_source_get_child_pidfd_own()</function> may be used to query whether the pidfd
205 the event source encapsulates shall be closed when the event source is freed. This function returns zero
206 if the pidfd shall be left open, and positive if it shall be closed automatically. By default this
207 setting defaults to on if the event source was allocated via <function>sd_event_add_child()</function>
208 and off if it was allocated via <function>sd_event_add_child_pidfd()</function>. The
209 <function>sd_event_source_set_child_pidfd_own()</function> function may be used to change the setting and
210 takes a boolean parameter with the new setting.</para>
211
212 <para><function>sd_event_source_get_child_process_own()</function> may be used to query whether the
213 process the event source watches shall be killed (with <constant>SIGKILL</constant>) and reaped when the
214 event source is freed. This function returns zero if the process shell be left running, and positive if
215 it shall be killed and reaped automatically. By default this setting defaults to off. The
216 <function>sd_event_source_set_child_process_own()</function> function may be used to change the setting
217 and takes a boolean parameter with the new setting. Note that currently if the calling process is
218 terminated abnormally the watched process might survive even thought the event source ceases to
219 exist. This behaviour might change eventually.</para>
220
221 <para><function>sd_event_source_send_child_signal()</function> may be used to send a UNIX signal to the
222 watched process. If the pidfd concept is supported in the kernel, this is implemented via <citerefentry
223 project='man-pages'><refentrytitle>pidfd_send_signal</refentrytitle><manvolnum>2</manvolnum></citerefentry>
224 and otherwise via <citerefentry
225 project='man-pages'><refentrytitle>rt_sigqueueinfo</refentrytitle><manvolnum>2</manvolnum></citerefentry>
226 (or via <citerefentry
227 project='man-pages'><refentrytitle>kill</refentrytitle><manvolnum>2</manvolnum></citerefentry> in case
228 <parameter>info</parameter> is <constant>NULL</constant>). The specified parameters match those of these
229 underlying system calls, except that the <parameter>info</parameter> is never modified (and is thus
230 declared constant). Like for the underlying system calls, the <parameter>flags</parameter> parameter
231 currently must be zero.</para>
232 </refsect1>
233
234 <refsect1>
235 <title>Return Value</title>
236
237 <para>On success, these functions return 0 or a positive
238 integer. On failure, they return a negative errno-style error
239 code.</para>
240
241 <refsect2>
242 <title>Errors</title>
243
244 <para>Returned errors may indicate the following problems:</para>
245
246 <variablelist>
247 <varlistentry>
248 <term><constant>-ENOMEM</constant></term>
249
250 <listitem><para>Not enough memory to allocate an object.</para></listitem>
251 </varlistentry>
252
253 <varlistentry>
254 <term><constant>-EINVAL</constant></term>
255
256 <listitem><para>An invalid argument has been passed. This includes
257 specifying an empty mask in <parameter>options</parameter> or a mask
258 which contains values different than a combination of
259 <constant>WEXITED</constant>, <constant>WSTOPPED</constant>, and
260 <constant>WCONTINUED</constant>.
261 </para></listitem>
262
263 </varlistentry>
264
265 <varlistentry>
266 <term><constant>-EBUSY</constant></term>
267
268 <listitem><para>A handler is already installed for this child process, or
269 <constant>SIGCHLD</constant> is not blocked.</para></listitem>
270
271 </varlistentry>
272
273 <varlistentry>
274 <term><constant>-ESTALE</constant></term>
275
276 <listitem><para>The event loop is already terminated.</para></listitem>
277
278 </varlistentry>
279
280 <varlistentry>
281 <term><constant>-ECHILD</constant></term>
282
283 <listitem><para>The event loop has been created in a different process.</para></listitem>
284
285 </varlistentry>
286
287 <varlistentry>
288 <term><constant>-EDOM</constant></term>
289
290 <listitem><para>The passed event source is not a child process event source.</para></listitem>
291 </varlistentry>
292
293 <varlistentry>
294 <term><constant>-EOPNOTSUPP</constant></term>
295
296 <listitem><para>A pidfd was requested but the kernel does not support this concept.</para></listitem>
297 </varlistentry>
298
299 </variablelist>
300 </refsect2>
301 </refsect1>
302
303 <xi:include href="libsystemd-pkgconfig.xml" />
304
305 <refsect1>
306 <title>See Also</title>
307
308 <para>
309 <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
310 <citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
311 <citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
312 <citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
313 <citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
314 <citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
315 <citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
316 <citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
317 <citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
318 <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
319 <citerefentry><refentrytitle>sd_event_source_set_priority</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
320 <citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
321 <citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
322 <citerefentry><refentrytitle>sd_event_source_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
323 <citerefentry project='man-pages'><refentrytitle>waitid</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
324 <citerefentry project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
325 <citerefentry project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
326 <citerefentry project='man-pages'><refentrytitle>pidfd_open</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
327 <citerefentry project='man-pages'><refentrytitle>pidfd_send_signal</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
328 <citerefentry project='man-pages'><refentrytitle>rt_sigqueueinfo</refentrytitle><manvolnum>2</manvolnum></citerefentry>,
329 <citerefentry project='man-pages'><refentrytitle>kill</refentrytitle><manvolnum>2</manvolnum></citerefentry>
330 </para>
331 </refsect1>
332
333 </refentry>