Debian Codegraph @

upstream/fedora-44/man3/sd_event_add_child.3

raw
1 '\" t
2 .TH "SD_EVENT_ADD_CHILD" "3" "" "systemd 259.5" "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),
73 \fBWCONTINUED\fR
74 (watch for the child process being resumed by a signal) and
75 \fBWNOWAIT\fR
76 (Do not reap the child process after it exits)\&. See
77 \fBwaitid\fR(2)
78 for further information\&.
79 .PP
80 The
81 \fIhandler\fR
82 must be a function to call when the process changes state or
83 \fBNULL\fR\&. The handler function will be passed the
84 \fIuserdata\fR
85 pointer, which may be chosen freely by the caller\&. The handler also receives a pointer to a
86 siginfo_t
87 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
88 \fIhandler\fR
89 is
90 \fBNULL\fR, a default handler that calls
91 \fBsd_event_exit\fR(3)
92 will be used\&.
93 .PP
94 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
95 \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
96 \fBSD_EVENT_ON\fR
97 mode was requested before, or it will cause the loop to terminate, see
98 \fBsd_event_source_set_exit_on_failure\fR(3)\&.
99 .PP
100 To destroy an event source object use
101 \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
102 \fBSD_EVENT_OFF\fR
103 with
104 \fBsd_event_source_set_enabled\fR(3)\&.
105 .PP
106 The
107 \fBSIGCHLD\fR
108 signal must be blocked in all threads before this function is called (using
109 \fBsigprocmask\fR(2)
110 or
111 \fBpthread_sigmask\fR(3))\&.
112 .PP
113 If the second parameter of
114 \fBsd_event_add_child()\fR
115 is passed as
116 \fBNULL\fR
117 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\&.
118 .PP
119 Note that the
120 \fIhandler\fR
121 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\&.
122 .PP
123 If the
124 \fIhandler\fR
125 parameter to
126 \fBsd_event_add_child()\fR
127 is
128 \fBNULL\fR, and the event source fires, this will be considered a request to exit the event loop\&. In this case, the
129 \fIuserdata\fR
130 parameter, cast to an integer, is passed as the exit code parameter to
131 \fBsd_event_exit\fR(3)\&.
132 .PP
133 If both a child process state change event source and a
134 \fBSIGCHLD\fR
135 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\&.
136 .PP
137 \fBsd_event_add_child_pidfd()\fR
138 is similar to
139 \fBsd_event_add_child()\fR
140 but takes a file descriptor referencing the process ("pidfd") instead of the numeric PID\&. A suitable file descriptor may be acquired via
141 \fBpidfd_open\fR(2)
142 and related calls\&. The passed file descriptor is not closed when the event source is freed again, unless
143 \fBsd_event_source_set_child_pidfd_own()\fR
144 is used to turn this behaviour on\&. Note that regardless which of
145 \fBsd_event_add_child()\fR
146 and
147 \fBsd_event_add_child_pidfd()\fR
148 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
149 \fBSIGCHLD\fR
150 has to be blocked in the invoking process\&.
151 .PP
152 \fBsd_event_source_get_child_pid()\fR
153 retrieves the configured PID of a child process state change event source created previously with
154 \fBsd_event_add_child()\fR\&. It takes the event source object as the
155 \fIsource\fR
156 parameter and a pointer to a
157 \fBpid_t\fR
158 variable to return the process ID in\&.
159 .PP
160 \fBsd_event_source_get_child_pidfd()\fR
161 retrieves the file descriptor referencing the watched process ("pidfd")\&. The event loop internally makes use of pidfds to watch child processes, regardless of whether the individual event sources are allocated via
162 \fBsd_event_add_child()\fR
163 or
164 \fBsd_event_add_child_pidfd()\fR\&. If the latter call was used to allocate the event source, this function returns the original file descriptor used for allocation\&. This call takes the event source object as the
165 \fIsource\fR
166 parameter and returns the numeric file descriptor\&.
167 .PP
168 \fBsd_event_source_get_child_pidfd_own()\fR
169 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
170 \fBsd_event_add_child()\fR
171 and off if it was allocated via
172 \fBsd_event_add_child_pidfd()\fR\&. The
173 \fBsd_event_source_set_child_pidfd_own()\fR
174 function may be used to change the setting and takes a boolean parameter with the new setting\&.
175 .PP
176 \fBsd_event_source_get_child_process_own()\fR
177 may be used to query whether the process the event source watches shall be killed (with
178 \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
179 \fBsd_event_source_set_child_process_own()\fR
180 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\&.
181 .PP
182 \fBsd_event_source_send_child_signal()\fR
183 may be used to send a UNIX signal to the watched process via
184 \fBpidfd_send_signal\fR(2)\&. The specified parameters match those of the underlying system call, except that the
185 \fIinfo\fR
186 is never modified (and is thus declared constant)\&. Like for the underlying system call, the
187 \fIflags\fR
188 parameter currently must be zero\&.
189 .SH "RETURN VALUE"
190 .PP
191 On success, these functions return 0 or a positive integer\&. On failure, they return a negative errno\-style error code\&.
192 .SS "Errors"
193 .PP
194 Returned errors may indicate the following problems:
195 .PP
196 \fB\-ENOMEM\fR
197 .RS 4
198 Not enough memory to allocate an object\&.
199 .RE
200 .PP
201 \fB\-EINVAL\fR
202 .RS 4
203 An invalid argument has been passed\&. This includes specifying an empty mask in
204 \fIoptions\fR
205 or a mask which contains values different than a combination of
206 \fBWEXITED\fR,
207 \fBWSTOPPED\fR, and
208 \fBWCONTINUED\fR\&.
209 .RE
210 .PP
211 \fB\-EBUSY\fR
212 .RS 4
213 A handler is already installed for this child process, or
214 \fBSIGCHLD\fR
215 is not blocked\&.
216 .RE
217 .PP
218 \fB\-ESTALE\fR
219 .RS 4
220 The event loop is already terminated\&.
221 .RE
222 .PP
223 \fB\-ECHILD\fR
224 .RS 4
225 The event loop has been created in a different process, library or module instance\&.
226 .RE
227 .PP
228 \fB\-EDOM\fR
229 .RS 4
230 The passed event source is not a child process event source\&.
231 .RE
232 .SH "NOTES"
233 .PP
234 Functions described here are available as a shared library, which can be compiled against and linked to with the
235 \fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
236 file\&.
237 .PP
238 The code described here uses
239 \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
240 \fBsetenv\fR(3)
241 from a parallel thread\&. It is recommended to only do calls to
242 \fBsetenv()\fR
243 from an early phase of the program when no other threads have been started\&.
244 .SH "EXAMPLE"
245 .PP
246 \fBExample\ \&1.\ \&Exit loop when the child terminates\fR
247 .sp
248 .if n \{\
249 .RS 4
250 .\}
251 .nf
252 /* SPDX\-License\-Identifier: MIT\-0 */
253
254 #define _GNU_SOURCE 1
255 #include <assert\&.h>
256 #include <stdio\&.h>
257 #include <unistd\&.h>
258 #include <systemd/sd\-event\&.h>
259
260 int main(int argc, char **argv) {
261 pid_t pid = fork();
262 assert(pid >= 0);
263
264 /* SIGCHLD signal must be blocked for sd_event_add_child to work */
265 sigset_t ss;
266 sigemptyset(&ss);
267 sigaddset(&ss, SIGCHLD);
268 sigprocmask(SIG_BLOCK, &ss, NULL);
269
270 if (pid == 0) /* child */
271 sleep(1);
272
273 else { /* parent */
274 sd_event *e = NULL;
275 int r;
276
277 /* Create the default event loop */
278 sd_event_default(&e);
279 assert(e);
280
281 /* We create a floating child event source (attached to \*(Aqe\*(Aq)\&.
282 * The default handler will be called with 666 as userdata, which
283 * will become the exit value of the loop\&. */
284 r = sd_event_add_child(e, NULL, pid, WEXITED, NULL, (void*) 666);
285 assert(r >= 0);
286
287 r = sd_event_loop(e);
288 assert(r == 666);
289
290 sd_event_unref(e);
291 }
292
293 return 0;
294 }
295 .fi
296 .if n \{\
297 .RE
298 .\}
299 .SH "HISTORY"
300 .PP
301 \fBsd_event_add_child()\fR,
302 \fBsd_event_child_handler_t()\fR, and
303 \fBsd_event_source_get_child_pid()\fR
304 were added in version 217\&.
305 .PP
306 \fBsd_event_add_child_pidfd()\fR,
307 \fBsd_event_source_get_child_pidfd()\fR,
308 \fBsd_event_source_get_child_pidfd_own()\fR,
309 \fBsd_event_source_set_child_pidfd_own()\fR,
310 \fBsd_event_source_get_child_process_own()\fR,
311 \fBsd_event_source_set_child_process_own()\fR, and
312 \fBsd_event_source_send_child_signal()\fR
313 were added in version 245\&.
314 .SH "SEE ALSO"
315 .PP
316 \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)