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