Debian Codegraph @

upstream/debian-trixie/man3/sd_event_add_child.3

raw
1 '\" t
2 .TH "SD_EVENT_ADD_CHILD" "3" "" "systemd 257.13" "sd_event_add_child"
3 .\" -----------------------------------------------------------------
4 .\" * Define some portability stuff
5 .\" -----------------------------------------------------------------
6 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7 .\" http://bugs.debian.org/507673
8 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
9 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 .ie \n(.g .ds Aq \(aq
11 .el .ds Aq '
12 .\" -----------------------------------------------------------------
13 .\" * set default formatting
14 .\" -----------------------------------------------------------------
15 .\" disable hyphenation
16 .nh
17 .\" disable justification (adjust text to left margin only)
18 .ad l
19 .\" -----------------------------------------------------------------
20 .\" * MAIN CONTENT STARTS HERE *
21 .\" -----------------------------------------------------------------
22 .SH "NAME"
23 sd_event_add_child, sd_event_add_child_pidfd, sd_event_source_get_child_pid, sd_event_source_get_child_pidfd, sd_event_source_get_child_pidfd_own, sd_event_source_set_child_pidfd_own, sd_event_source_get_child_process_own, sd_event_source_set_child_process_own, sd_event_source_send_child_signal, sd_event_child_handler_t \- Add a child process state change event source to an event loop
24 .SH "SYNOPSIS"
25 .sp
26 .ft B
27 .nf
28 #include <systemd/sd\-event\&.h>
29 .fi
30 .ft
31 .sp
32 .ft B
33 .nf
34 typedef struct sd_event_source sd_event_source;
35 .fi
36 .ft
37 .HP \w'typedef\ int\ (*sd_event_child_handler_t)('u
38 .BI "typedef int (*sd_event_child_handler_t)(sd_event_source\ *" "s" ", const\ siginfo_t\ *" "si" ", void\ *" "userdata" ");"
39 .HP \w'int\ sd_event_add_child('u
40 .BI "int sd_event_add_child(sd_event\ *" "event" ", sd_event_source\ **" "source" ", pid_t\ " "pid" ", int\ " "options" ", sd_event_child_handler_t\ " "handler" ", void\ *" "userdata" ");"
41 .HP \w'int\ sd_event_add_child_pidfd('u
42 .BI "int sd_event_add_child_pidfd(sd_event\ *" "event" ", sd_event_source\ **" "source" ", int\ " "pidfd" ", int\ " "options" ", sd_event_child_handler_t\ " "handler" ", void\ *" "userdata" ");"
43 .HP \w'int\ sd_event_source_get_child_pid('u
44 .BI "int sd_event_source_get_child_pid(sd_event_source\ *" "source" ", pid_t\ *" "ret" ");"
45 .HP \w'int\ sd_event_source_get_child_pidfd('u
46 .BI "int sd_event_source_get_child_pidfd(sd_event_source\ *" "source" ");"
47 .HP \w'int\ sd_event_source_get_child_pidfd_own('u
48 .BI "int sd_event_source_get_child_pidfd_own(sd_event_source\ *" "source" ");"
49 .HP \w'int\ sd_event_source_set_child_pidfd_own('u
50 .BI "int sd_event_source_set_child_pidfd_own(sd_event_source\ *" "source" ", int\ " "own" ");"
51 .HP \w'int\ sd_event_source_get_child_process_own('u
52 .BI "int sd_event_source_get_child_process_own(sd_event_source\ *" "source" ");"
53 .HP \w'int\ sd_event_source_set_child_process_own('u
54 .BI "int sd_event_source_set_child_process_own(sd_event_source\ *" "source" ", int\ " "own" ");"
55 .HP \w'int\ sd_event_source_send_child_signal('u
56 .BI "int sd_event_source_send_child_signal(sd_event_source\ *" "source" ", int\ " "sig" ", const\ siginfo_t\ *" "info" ", unsigned\ " "flags" ");"
57 .SH "DESCRIPTION"
58 .PP
59 \fBsd_event_add_child()\fR
60 adds a new child process state change event source to an event loop\&. The event loop object is specified in the
61 \fIevent\fR
62 parameter, the event source object is returned in the
63 \fIsource\fR
64 parameter\&. The
65 \fIpid\fR
66 parameter specifies the PID of the process to watch, which must be a direct child process of the invoking process\&. The
67 \fIoptions\fR
68 parameter determines which state changes will be watched for\&. It must contain an OR\-ed mask of
69 \fBWEXITED\fR
70 (watch for the child process terminating),
71 \fBWSTOPPED\fR
72 (watch for the child process being stopped by a signal), and
73 \fBWCONTINUED\fR
74 (watch for the child process being resumed by a signal)\&. See
75 \fBwaitid\fR(2)
76 for further information\&.
77 .PP
78 The
79 \fIhandler\fR
80 must be a function to call when the process changes state or
81 \fBNULL\fR\&. The handler function will be passed the
82 \fIuserdata\fR
83 pointer, which may be chosen freely by the caller\&. The handler also receives a pointer to a
84 siginfo_t
85 structure containing information about the child process event\&. The handler may return negative to signal an error (see below), other return values are ignored\&. If
86 \fIhandler\fR
87 is
88 \fBNULL\fR, a default handler that calls
89 \fBsd_event_exit\fR(3)
90 will be used\&.
91 .PP
92 Only a single handler may be installed for a specific child process\&. The handler is enabled for a single event (\fBSD_EVENT_ONESHOT\fR), but this may be changed with
93 \fBsd_event_source_set_enabled\fR(3)\&. If the handler function returns a negative error code, it will either be disabled after the invocation, even if the
94 \fBSD_EVENT_ON\fR
95 mode was requested before, or it will cause the loop to terminate, see
96 \fBsd_event_source_set_exit_on_failure\fR(3)\&.
97 .PP
98 To destroy an event source object use
99 \fBsd_event_source_unref\fR(3), but note that the event source is only removed from the event loop when all references to the event source are dropped\&. To make sure an event source does not fire anymore, even when there\*(Aqs still a reference to it kept, consider setting the event source to
100 \fBSD_EVENT_OFF\fR
101 with
102 \fBsd_event_source_set_enabled\fR(3)\&.
103 .PP
104 The
105 \fBSIGCHLD\fR
106 signal must be blocked in all threads before this function is called (using
107 \fBsigprocmask\fR(2)
108 or
109 \fBpthread_sigmask\fR(3))\&.
110 .PP
111 If the second parameter of
112 \fBsd_event_add_child()\fR
113 is passed as
114 \fBNULL\fR
115 no reference to the event source object is returned\&. In this case, the event source is considered "floating", and will be destroyed implicitly when the event loop itself is destroyed\&.
116 .PP
117 Note that the
118 \fIhandler\fR
119 function is invoked at a time where the child process is not reaped yet (and thus still is exposed as a zombie process by the kernel)\&. However, the child will be reaped automatically after the function returns\&. Child processes for which no child process state change event sources are installed will not be reaped by the event loop implementation\&.
120 .PP
121 If the
122 \fIhandler\fR
123 parameter to
124 \fBsd_event_add_child()\fR
125 is
126 \fBNULL\fR, and the event source fires, this will be considered a request to exit the event loop\&. In this case, the
127 \fIuserdata\fR
128 parameter, cast to an integer, is passed as the exit code parameter to
129 \fBsd_event_exit\fR(3)\&.
130 .PP
131 If both a child process state change event source and a
132 \fBSIGCHLD\fR
133 signal event source is installed in the same event loop, the configured event source priorities decide which event source is dispatched first\&. If the signal handler is processed first, it should leave the child processes for which child process state change event sources are installed unreaped\&.
134 .PP
135 \fBsd_event_add_child_pidfd()\fR
136 is similar to
137 \fBsd_event_add_child()\fR
138 but takes a file descriptor referencing the process ("pidfd") instead of the numeric PID\&. A suitable file descriptor may be acquired via
139 \fBpidfd_open\fR(2)
140 and related calls\&. The passed file descriptor is not closed when the event source is freed again, unless
141 \fBsd_event_source_set_child_pidfd_own()\fR
142 is used to turn this behaviour on\&. Note that regardless which of
143 \fBsd_event_add_child()\fR
144 and
145 \fBsd_event_add_child_pidfd()\fR
146 is used for allocating an event source, the watched process has to be a direct child process of the invoking process\&. Also in both cases
147 \fBSIGCHLD\fR
148 has to be blocked in the invoking process\&.
149 .PP
150 \fBsd_event_source_get_child_pid()\fR
151 retrieves the configured PID of a child process state change event source created previously with
152 \fBsd_event_add_child()\fR\&. It takes the event source object as the
153 \fIsource\fR
154 parameter and a pointer to a
155 \fBpid_t\fR
156 variable to return the process ID in\&.
157 .PP
158 \fBsd_event_source_get_child_pidfd()\fR
159 retrieves the file descriptor referencing the watched process ("pidfd") if this functionality is available\&. On kernels that support the concept the event loop will make use of pidfds to watch child processes, regardless if the individual event sources are allocated via
160 \fBsd_event_add_child()\fR
161 or
162 \fBsd_event_add_child_pidfd()\fR\&. If the latter call was used to allocate the event source, this function returns the file descriptor used for allocation\&. On kernels that do not support the pidfd concept this function will fail with
163 \fBEOPNOTSUPP\fR\&. This call takes the event source object as the
164 \fIsource\fR
165 parameter and returns the numeric file descriptor\&.
166 .PP
167 \fBsd_event_source_get_child_pidfd_own()\fR
168 may be used to query whether the pidfd the event source encapsulates shall be closed when the event source is freed\&. This function returns zero if the pidfd shall be left open, and positive if it shall be closed automatically\&. By default, this setting defaults to on if the event source was allocated via
169 \fBsd_event_add_child()\fR
170 and off if it was allocated via
171 \fBsd_event_add_child_pidfd()\fR\&. The
172 \fBsd_event_source_set_child_pidfd_own()\fR
173 function may be used to change the setting and takes a boolean parameter with the new setting\&.
174 .PP
175 \fBsd_event_source_get_child_process_own()\fR
176 may be used to query whether the process the event source watches shall be killed (with
177 \fBSIGKILL\fR) and reaped when the event source is freed\&. This function returns zero if the process shell be left running, and positive if it shall be killed and reaped automatically\&. By default, this setting defaults to off\&. The
178 \fBsd_event_source_set_child_process_own()\fR
179 function may be used to change the setting and takes a boolean parameter with the new setting\&. Note that currently if the calling process is terminated abnormally the watched process might survive even thought the event source ceases to exist\&. This behaviour might change eventually\&.
180 .PP
181 \fBsd_event_source_send_child_signal()\fR
182 may be used to send a UNIX signal to the watched process\&. If the pidfd concept is supported in the kernel, this is implemented via
183 \fBpidfd_send_signal\fR(2)
184 and otherwise via
185 \fBrt_sigqueueinfo\fR(2)
186 (or via
187 \fBkill\fR(2)
188 in case
189 \fIinfo\fR
190 is
191 \fBNULL\fR)\&. The specified parameters match those of these underlying system calls, except that the
192 \fIinfo\fR
193 is never modified (and is thus declared constant)\&. Like for the underlying system calls, the
194 \fIflags\fR
195 parameter currently must be zero\&.
196 .SH "RETURN VALUE"
197 .PP
198 On success, these functions return 0 or a positive integer\&. On failure, they return a negative errno\-style error code\&.
199 .SS "Errors"
200 .PP
201 Returned errors may indicate the following problems:
202 .PP
203 \fB\-ENOMEM\fR
204 .RS 4
205 Not enough memory to allocate an object\&.
206 .RE
207 .PP
208 \fB\-EINVAL\fR
209 .RS 4
210 An invalid argument has been passed\&. This includes specifying an empty mask in
211 \fIoptions\fR
212 or a mask which contains values different than a combination of
213 \fBWEXITED\fR,
214 \fBWSTOPPED\fR, and
215 \fBWCONTINUED\fR\&.
216 .RE
217 .PP
218 \fB\-EBUSY\fR
219 .RS 4
220 A handler is already installed for this child process, or
221 \fBSIGCHLD\fR
222 is not blocked\&.
223 .RE
224 .PP
225 \fB\-ESTALE\fR
226 .RS 4
227 The event loop is already terminated\&.
228 .RE
229 .PP
230 \fB\-ECHILD\fR
231 .RS 4
232 The event loop has been created in a different process, library or module instance\&.
233 .RE
234 .PP
235 \fB\-EDOM\fR
236 .RS 4
237 The passed event source is not a child process event source\&.
238 .RE
239 .PP
240 \fB\-EOPNOTSUPP\fR
241 .RS 4
242 A pidfd was requested but the kernel does not support this concept\&.
243 .sp
244 Added in version 245\&.
245 .RE
246 .SH "NOTES"
247 .PP
248 Functions described here are available as a shared library, which can be compiled against and linked to with the
249 \fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
250 file\&.
251 .PP
252 The code described here uses
253 \fBgetenv\fR(3), which is declared to be not multi\-thread\-safe\&. This means that the code calling the functions described here must not call
254 \fBsetenv\fR(3)
255 from a parallel thread\&. It is recommended to only do calls to
256 \fBsetenv()\fR
257 from an early phase of the program when no other threads have been started\&.
258 .SH "EXAMPLE"
259 .PP
260 \fBExample\ \&1.\ \&Exit loop when the child terminates\fR
261 .sp
262 .if n \{\
263 .RS 4
264 .\}
265 .nf
266 /* SPDX\-License\-Identifier: MIT\-0 */
267
268 #define _GNU_SOURCE 1
269 #include <assert\&.h>
270 #include <stdio\&.h>
271 #include <unistd\&.h>
272 #include <systemd/sd\-event\&.h>
273
274 int main(int argc, char **argv) {
275 pid_t pid = fork();
276 assert(pid >= 0);
277
278 /* SIGCHLD signal must be blocked for sd_event_add_child to work */
279 sigset_t ss;
280 sigemptyset(&ss);
281 sigaddset(&ss, SIGCHLD);
282 sigprocmask(SIG_BLOCK, &ss, NULL);
283
284 if (pid == 0) /* child */
285 sleep(1);
286
287 else { /* parent */
288 sd_event *e = NULL;
289 int r;
290
291 /* Create the default event loop */
292 sd_event_default(&e);
293 assert(e);
294
295 /* We create a floating child event source (attached to \*(Aqe\*(Aq)\&.
296 * The default handler will be called with 666 as userdata, which
297 * will become the exit value of the loop\&. */
298 r = sd_event_add_child(e, NULL, pid, WEXITED, NULL, (void*) 666);
299 assert(r >= 0);
300
301 r = sd_event_loop(e);
302 assert(r == 666);
303
304 sd_event_unref(e);
305 }
306
307 return 0;
308 }
309 .fi
310 .if n \{\
311 .RE
312 .\}
313 .SH "HISTORY"
314 .PP
315 \fBsd_event_add_child()\fR,
316 \fBsd_event_child_handler_t()\fR, and
317 \fBsd_event_source_get_child_pid()\fR
318 were added in version 217\&.
319 .PP
320 \fBsd_event_add_child_pidfd()\fR,
321 \fBsd_event_source_get_child_pidfd()\fR,
322 \fBsd_event_source_get_child_pidfd_own()\fR,
323 \fBsd_event_source_set_child_pidfd_own()\fR,
324 \fBsd_event_source_get_child_process_own()\fR,
325 \fBsd_event_source_set_child_process_own()\fR, and
326 \fBsd_event_source_send_child_signal()\fR
327 were added in version 245\&.
328 .SH "SEE ALSO"
329 .PP
330 \fBsystemd\fR(1), \fBsd-event\fR(3), \fBsd_event_new\fR(3), \fBsd_event_now\fR(3), \fBsd_event_add_io\fR(3), \fBsd_event_add_time\fR(3), \fBsd_event_add_signal\fR(3), \fBsd_event_add_inotify\fR(3), \fBsd_event_add_defer\fR(3), \fBsd_event_source_set_enabled\fR(3), \fBsd_event_source_set_priority\fR(3), \fBsd_event_source_set_userdata\fR(3), \fBsd_event_source_set_description\fR(3), \fBsd_event_source_set_floating\fR(3), \fBwaitid\fR(2), \fBsigprocmask\fR(2), \fBpthread_sigmask\fR(3), \fBpidfd_open\fR(2), \fBpidfd_send_signal\fR(2), \fBrt_sigqueueinfo\fR(2), \fBkill\fR(2)