]>
Commit | Line | Data |
---|---|---|
514094f9 | 1 | <?xml version='1.0'?> |
3a54a157 | 2 | <!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN" |
eea10b26 | 3 | "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"> |
db9ecf05 | 4 | <!-- SPDX-License-Identifier: LGPL-2.1-or-later --> |
edf25737 | 5 | |
dc83f27a | 6 | <refentry id="sd_event_add_child" xmlns:xi="http://www.w3.org/2001/XInclude"> |
edf25737 ZJS |
7 | |
8 | <refentryinfo> | |
9 | <title>sd_event_add_child</title> | |
10 | <productname>systemd</productname> | |
edf25737 ZJS |
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> | |
80896433 | 20 | <refname>sd_event_add_child_pidfd</refname> |
edf25737 | 21 | <refname>sd_event_source_get_child_pid</refname> |
80896433 LP |
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> | |
dc83f27a | 28 | <refname>sd_event_child_handler_t</refname> |
edf25737 | 29 | |
dc83f27a | 30 | <refpurpose>Add a child process state change event source to an event loop</refpurpose> |
edf25737 ZJS |
31 | </refnamediv> |
32 | ||
33 | <refsynopsisdiv> | |
34 | <funcsynopsis> | |
dc83f27a LP |
35 | <funcsynopsisinfo>#include <systemd/sd-event.h></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> | |
edf25737 ZJS |
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 | ||
80896433 LP |
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 | ||
edf25737 ZJS |
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 | ||
80896433 LP |
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 | ||
edf25737 ZJS |
107 | </funcsynopsis> |
108 | </refsynopsisdiv> | |
109 | ||
110 | <refsect1> | |
111 | <title>Description</title> | |
112 | ||
80896433 LP |
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 | |
9809a788 ZJS |
117 | process. The <parameter>options</parameter> parameter determines which state changes will be watched for. |
118 | It must contain an OR-ed mask of <constant>WEXITED</constant> (watch for the child process terminating), | |
119 | <constant>WSTOPPED</constant> (watch for the child process being stopped by a signal), and | |
120 | <constant>WCONTINUED</constant> (watch for the child process being resumed by a signal). See | |
121 | <citerefentry project='man-pages'><refentrytitle>waitid</refentrytitle><manvolnum>2</manvolnum></citerefentry> | |
122 | for further information.</para> | |
123 | ||
124 | <para>The <parameter>handler</parameter> must be a function to call when the process changes state or | |
125 | <constant>NULL</constant>. The handler function will be passed the <parameter>userdata</parameter> | |
126 | pointer, which may be chosen freely by the caller. The handler also receives a pointer to a | |
127 | <structname>siginfo_t</structname> structure containing information about the child process event. The | |
128 | handler may return negative to signal an error (see below), other return values are ignored. If | |
129 | <parameter>handler</parameter> is <constant>NULL</constant>, a default handler that calls | |
130 | <citerefentry><refentrytitle>sd_event_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry> will be | |
131 | used.</para> | |
132 | ||
133 | <para>Only a single handler may be installed for a specific child process. The handler is enabled for a | |
134 | single event (<constant>SD_EVENT_ONESHOT</constant>), but this may be changed with | |
edf25737 | 135 | <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>. |
9809a788 ZJS |
136 | If the handler function returns a negative error code, it will either be disabled after the invocation, |
137 | even if the <constant>SD_EVENT_ON</constant> mode was requested before, or it will cause the loop to | |
138 | terminate, see | |
139 | <citerefentry><refentrytitle>sd_event_source_set_exit_on_failure</refentrytitle><manvolnum>3</manvolnum></citerefentry>. | |
edf25737 ZJS |
140 | </para> |
141 | ||
dc83f27a LP |
142 | <para>To destroy an event source object use |
143 | <citerefentry><refentrytitle>sd_event_source_unref</refentrytitle><manvolnum>3</manvolnum></citerefentry>, | |
144 | but note that the event source is only removed from the event loop | |
145 | when all references to the event source are dropped. To make sure | |
146 | an event source does not fire anymore, even when there's still a | |
147 | reference to it kept, consider setting the event source to | |
148 | <constant>SD_EVENT_OFF</constant> with | |
149 | <citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para> | |
150 | ||
b3508072 LP |
151 | <para>The <constant>SIGCHLD</constant> signal must be blocked in all threads before this function is |
152 | called (using <citerefentry | |
153 | project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry> or | |
154 | <citerefentry | |
155 | project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry>).</para> | |
156 | ||
b9350e70 LP |
157 | <para>If the second parameter of <function>sd_event_add_child()</function> is passed as |
158 | <constant>NULL</constant> no reference to the event source object is returned. In this case the event | |
159 | source is considered "floating", and will be destroyed implicitly when the event loop itself is | |
160 | destroyed.</para> | |
dc83f27a LP |
161 | |
162 | <para>Note that the <parameter>handler</parameter> function is | |
163 | invoked at a time where the child process is not reaped yet (and | |
164 | thus still is exposed as a zombie process by the kernel). However, | |
165 | the child will be reaped automatically after the function | |
166 | returns. Child processes for which no child process state change | |
167 | event sources are installed will not be reaped by the event loop | |
168 | implementation.</para> | |
169 | ||
b9350e70 LP |
170 | <para>If the <parameter>handler</parameter> parameter to <function>sd_event_add_child()</function> is |
171 | <constant>NULL</constant>, and the event source fires, this will be considered a request to exit the | |
172 | event loop. In this case, the <parameter>userdata</parameter> parameter, cast to an integer, is passed as | |
173 | the exit code parameter to | |
174 | <citerefentry><refentrytitle>sd_event_exit</refentrytitle><manvolnum>3</manvolnum></citerefentry>.</para> | |
175 | ||
dc83f27a LP |
176 | <para>If both a child process state change event source and a |
177 | <constant>SIGCHLD</constant> signal event source is installed in | |
178 | the same event loop, the configured event source priorities decide | |
179 | which event source is dispatched first. If the signal handler is | |
180 | processed first, it should leave the child processes for which | |
181 | child process state change event sources are installed unreaped.</para> | |
182 | ||
80896433 LP |
183 | <para><function>sd_event_add_child_pidfd()</function> is similar to |
184 | <function>sd_event_add_child()</function> but takes a file descriptor referencing the process ("pidfd") | |
185 | instead of the numeric PID. A suitable file descriptor may be acquired via <citerefentry | |
186 | project='man-pages'><refentrytitle>pidfd_open</refentrytitle><manvolnum>2</manvolnum></citerefentry> and | |
187 | related calls. The passed file descriptor is not closed when the event source is freed again, unless | |
188 | <function>sd_event_source_set_child_pidfd_own()</function> is used to turn this behaviour on. Note that | |
189 | regardless which of <function>sd_event_add_child()</function> and | |
190 | <function>sd_event_add_child_pidfd()</function> is used for allocating an event source, the watched | |
191 | process has to be a direct child process of the invoking process. Also in both cases | |
192 | <constant>SIGCHLD</constant> has to be blocked in the invoking process.</para> | |
193 | ||
edf25737 | 194 | <para><function>sd_event_source_get_child_pid()</function> |
dc83f27a LP |
195 | retrieves the configured PID of a child process state change event |
196 | source created previously with | |
edf25737 ZJS |
197 | <function>sd_event_add_child()</function>. It takes the event |
198 | source object as the <parameter>source</parameter> parameter and a | |
dc83f27a LP |
199 | pointer to a <type>pid_t</type> variable to return the process ID |
200 | in. | |
edf25737 | 201 | </para> |
80896433 LP |
202 | |
203 | <para><function>sd_event_source_get_child_pidfd()</function> retrieves the file descriptor referencing | |
204 | the watched process ("pidfd") if this functionality is available. On kernels that support the concept the | |
205 | event loop will make use of pidfds to watch child processes, regardless if the individual event sources | |
206 | are allocated via <function>sd_event_add_child()</function> or | |
207 | <function>sd_event_add_child_pidfd()</function>. If the latter call was used to allocate the event | |
208 | source, this function returns the file descriptor used for allocation. On kernels that do not support the | |
209 | pidfd concept this function will fail with <constant>EOPNOTSUPP</constant>. This call takes the event | |
210 | source object as the <parameter>source</parameter> parameter and returns the numeric file descriptor. | |
211 | </para> | |
212 | ||
213 | <para><function>sd_event_source_get_child_pidfd_own()</function> may be used to query whether the pidfd | |
214 | the event source encapsulates shall be closed when the event source is freed. This function returns zero | |
215 | if the pidfd shall be left open, and positive if it shall be closed automatically. By default this | |
216 | setting defaults to on if the event source was allocated via <function>sd_event_add_child()</function> | |
217 | and off if it was allocated via <function>sd_event_add_child_pidfd()</function>. The | |
218 | <function>sd_event_source_set_child_pidfd_own()</function> function may be used to change the setting and | |
219 | takes a boolean parameter with the new setting.</para> | |
220 | ||
221 | <para><function>sd_event_source_get_child_process_own()</function> may be used to query whether the | |
222 | process the event source watches shall be killed (with <constant>SIGKILL</constant>) and reaped when the | |
223 | event source is freed. This function returns zero if the process shell be left running, and positive if | |
224 | it shall be killed and reaped automatically. By default this setting defaults to off. The | |
225 | <function>sd_event_source_set_child_process_own()</function> function may be used to change the setting | |
226 | and takes a boolean parameter with the new setting. Note that currently if the calling process is | |
227 | terminated abnormally the watched process might survive even thought the event source ceases to | |
228 | exist. This behaviour might change eventually.</para> | |
229 | ||
230 | <para><function>sd_event_source_send_child_signal()</function> may be used to send a UNIX signal to the | |
231 | watched process. If the pidfd concept is supported in the kernel, this is implemented via <citerefentry | |
232 | project='man-pages'><refentrytitle>pidfd_send_signal</refentrytitle><manvolnum>2</manvolnum></citerefentry> | |
233 | and otherwise via <citerefentry | |
234 | project='man-pages'><refentrytitle>rt_sigqueueinfo</refentrytitle><manvolnum>2</manvolnum></citerefentry> | |
235 | (or via <citerefentry | |
236 | project='man-pages'><refentrytitle>kill</refentrytitle><manvolnum>2</manvolnum></citerefentry> in case | |
237 | <parameter>info</parameter> is <constant>NULL</constant>). The specified parameters match those of these | |
238 | underlying system calls, except that the <parameter>info</parameter> is never modified (and is thus | |
239 | declared constant). Like for the underlying system calls, the <parameter>flags</parameter> parameter | |
240 | currently must be zero.</para> | |
edf25737 ZJS |
241 | </refsect1> |
242 | ||
243 | <refsect1> | |
244 | <title>Return Value</title> | |
245 | ||
246 | <para>On success, these functions return 0 or a positive | |
247 | integer. On failure, they return a negative errno-style error | |
248 | code.</para> | |
edf25737 | 249 | |
b1de39de ZJS |
250 | <refsect2> |
251 | <title>Errors</title> | |
edf25737 | 252 | |
b1de39de | 253 | <para>Returned errors may indicate the following problems:</para> |
edf25737 | 254 | |
b1de39de ZJS |
255 | <variablelist> |
256 | <varlistentry> | |
257 | <term><constant>-ENOMEM</constant></term> | |
edf25737 | 258 | |
b1de39de ZJS |
259 | <listitem><para>Not enough memory to allocate an object.</para></listitem> |
260 | </varlistentry> | |
edf25737 | 261 | |
b1de39de ZJS |
262 | <varlistentry> |
263 | <term><constant>-EINVAL</constant></term> | |
edf25737 | 264 | |
b1de39de ZJS |
265 | <listitem><para>An invalid argument has been passed. This includes |
266 | specifying an empty mask in <parameter>options</parameter> or a mask | |
267 | which contains values different than a combination of | |
268 | <constant>WEXITED</constant>, <constant>WSTOPPED</constant>, and | |
269 | <constant>WCONTINUED</constant>. | |
270 | </para></listitem> | |
edf25737 | 271 | |
b1de39de | 272 | </varlistentry> |
edf25737 | 273 | |
b1de39de ZJS |
274 | <varlistentry> |
275 | <term><constant>-EBUSY</constant></term> | |
edf25737 | 276 | |
b3508072 LP |
277 | <listitem><para>A handler is already installed for this child process, or |
278 | <constant>SIGCHLD</constant> is not blocked.</para></listitem> | |
edf25737 | 279 | |
b1de39de | 280 | </varlistentry> |
edf25737 | 281 | |
b1de39de ZJS |
282 | <varlistentry> |
283 | <term><constant>-ESTALE</constant></term> | |
edf25737 | 284 | |
b1de39de | 285 | <listitem><para>The event loop is already terminated.</para></listitem> |
edf25737 | 286 | |
b1de39de | 287 | </varlistentry> |
edf25737 | 288 | |
b1de39de ZJS |
289 | <varlistentry> |
290 | <term><constant>-ECHILD</constant></term> | |
edf25737 | 291 | |
2eeff0f4 | 292 | <listitem><para>The event loop has been created in a different process, library or module instance.</para></listitem> |
edf25737 | 293 | |
b1de39de | 294 | </varlistentry> |
edf25737 | 295 | |
b1de39de ZJS |
296 | <varlistentry> |
297 | <term><constant>-EDOM</constant></term> | |
edf25737 | 298 | |
b1de39de ZJS |
299 | <listitem><para>The passed event source is not a child process event source.</para></listitem> |
300 | </varlistentry> | |
edf25737 | 301 | |
80896433 LP |
302 | <varlistentry> |
303 | <term><constant>-EOPNOTSUPP</constant></term> | |
304 | ||
ec07c3c8 AK |
305 | <listitem><para>A pidfd was requested but the kernel does not support this concept.</para> |
306 | ||
307 | <xi:include href="version-info.xml" xpointer="v245"/></listitem> | |
80896433 LP |
308 | </varlistentry> |
309 | ||
b1de39de ZJS |
310 | </variablelist> |
311 | </refsect2> | |
edf25737 ZJS |
312 | </refsect1> |
313 | ||
dc83f27a LP |
314 | <xi:include href="libsystemd-pkgconfig.xml" /> |
315 | ||
5ca99dfa ZJS |
316 | <refsect1> |
317 | <title>Example</title> | |
318 | ||
319 | <example> | |
320 | <title>Exit loop when the child terminates</title> | |
321 | ||
322 | <programlisting><xi:include href="event-quick-child.c" parse="text" /></programlisting> | |
323 | </example> | |
324 | </refsect1> | |
325 | ||
69106f47 AK |
326 | <refsect1> |
327 | <title>History</title> | |
00f95506 AK |
328 | <para><function>sd_event_add_child()</function>, |
329 | <function>sd_event_child_handler_t()</function>, and | |
330 | <function>sd_event_source_get_child_pid()</function> were added in version 217.</para> | |
331 | <para><function>sd_event_add_child_pidfd()</function>, | |
332 | <function>sd_event_source_get_child_pidfd()</function>, | |
333 | <function>sd_event_source_get_child_pidfd_own()</function>, | |
334 | <function>sd_event_source_set_child_pidfd_own()</function>, | |
335 | <function>sd_event_source_get_child_process_own()</function>, | |
336 | <function>sd_event_source_set_child_process_own()</function>, and | |
337 | <function>sd_event_source_send_child_signal()</function> were added in version 245.</para> | |
69106f47 AK |
338 | </refsect1> |
339 | ||
edf25737 ZJS |
340 | <refsect1> |
341 | <title>See Also</title> | |
342 | ||
13a69c12 DT |
343 | <para><simplelist type="inline"> |
344 | <member><citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry></member> | |
345 | <member><citerefentry><refentrytitle>sd-event</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
346 | <member><citerefentry><refentrytitle>sd_event_new</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
347 | <member><citerefentry><refentrytitle>sd_event_now</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
348 | <member><citerefentry><refentrytitle>sd_event_add_io</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
349 | <member><citerefentry><refentrytitle>sd_event_add_time</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
350 | <member><citerefentry><refentrytitle>sd_event_add_signal</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
351 | <member><citerefentry><refentrytitle>sd_event_add_inotify</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
352 | <member><citerefentry><refentrytitle>sd_event_add_defer</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
353 | <member><citerefentry><refentrytitle>sd_event_source_set_enabled</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
354 | <member><citerefentry><refentrytitle>sd_event_source_set_priority</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
355 | <member><citerefentry><refentrytitle>sd_event_source_set_userdata</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
356 | <member><citerefentry><refentrytitle>sd_event_source_set_description</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
357 | <member><citerefentry><refentrytitle>sd_event_source_set_floating</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
358 | <member><citerefentry project='man-pages'><refentrytitle>waitid</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> | |
359 | <member><citerefentry project='man-pages'><refentrytitle>sigprocmask</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> | |
360 | <member><citerefentry project='man-pages'><refentrytitle>pthread_sigmask</refentrytitle><manvolnum>3</manvolnum></citerefentry></member> | |
361 | <member><citerefentry project='man-pages'><refentrytitle>pidfd_open</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> | |
362 | <member><citerefentry project='man-pages'><refentrytitle>pidfd_send_signal</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> | |
363 | <member><citerefentry project='man-pages'><refentrytitle>rt_sigqueueinfo</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> | |
364 | <member><citerefentry project='man-pages'><refentrytitle>kill</refentrytitle><manvolnum>2</manvolnum></citerefentry></member> | |
365 | </simplelist></para> | |
edf25737 ZJS |
366 | </refsect1> |
367 | ||
368 | </refentry> |