Debian Codegraph @

upstream/mageia-cauldron/man3/sd_event_add_child.3

raw
1 '\" t
2 .TH "SD_EVENT_ADD_CHILD" "3" "" "systemd 258.7" "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")\&. The event loop internally makes use of pidfds to watch child processes, regardless of whether 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 original file descriptor used for allocation\&. This call takes the event source object as the
163 \fIsource\fR
164 parameter and returns the numeric file descriptor\&.
165 .PP
166 \fBsd_event_source_get_child_pidfd_own()\fR
167 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
168 \fBsd_event_add_child()\fR
169 and off if it was allocated via
170 \fBsd_event_add_child_pidfd()\fR\&. The
171 \fBsd_event_source_set_child_pidfd_own()\fR
172 function may be used to change the setting and takes a boolean parameter with the new setting\&.
173 .PP
174 \fBsd_event_source_get_child_process_own()\fR
175 may be used to query whether the process the event source watches shall be killed (with
176 \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
177 \fBsd_event_source_set_child_process_own()\fR
178 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\&.
179 .PP
180 \fBsd_event_source_send_child_signal()\fR
181 may be used to send a UNIX signal to the watched process via
182 \fBpidfd_send_signal\fR(2)\&. The specified parameters match those of the underlying system call, except that the
183 \fIinfo\fR
184 is never modified (and is thus declared constant)\&. Like for the underlying system call, the
185 \fIflags\fR
186 parameter currently must be zero\&.
187 .SH "RETURN VALUE"
188 .PP
189 On success, these functions return 0 or a positive integer\&. On failure, they return a negative errno\-style error code\&.
190 .SS "Errors"
191 .PP
192 Returned errors may indicate the following problems:
193 .PP
194 \fB\-ENOMEM\fR
195 .RS 4
196 Not enough memory to allocate an object\&.
197 .RE
198 .PP
199 \fB\-EINVAL\fR
200 .RS 4
201 An invalid argument has been passed\&. This includes specifying an empty mask in
202 \fIoptions\fR
203 or a mask which contains values different than a combination of
204 \fBWEXITED\fR,
205 \fBWSTOPPED\fR, and
206 \fBWCONTINUED\fR\&.
207 .RE
208 .PP
209 \fB\-EBUSY\fR
210 .RS 4
211 A handler is already installed for this child process, or
212 \fBSIGCHLD\fR
213 is not blocked\&.
214 .RE
215 .PP
216 \fB\-ESTALE\fR
217 .RS 4
218 The event loop is already terminated\&.
219 .RE
220 .PP
221 \fB\-ECHILD\fR
222 .RS 4
223 The event loop has been created in a different process, library or module instance\&.
224 .RE
225 .PP
226 \fB\-EDOM\fR
227 .RS 4
228 The passed event source is not a child process event source\&.
229 .RE
230 .SH "NOTES"
231 .PP
232 Functions described here are available as a shared library, which can be compiled against and linked to with the
233 \fBlibsystemd\fR\ \&\fBpkg-config\fR(1)
234 file\&.
235 .PP
236 The code described here uses
237 \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
238 \fBsetenv\fR(3)
239 from a parallel thread\&. It is recommended to only do calls to
240 \fBsetenv()\fR
241 from an early phase of the program when no other threads have been started\&.
242 .SH "EXAMPLE"
243 .PP
244 \fBExample\ \&1.\ \&Exit loop when the child terminates\fR
245 .sp
246 .if n \{\
247 .RS 4
248 .\}
249 .nf
250 /* SPDX\-License\-Identifier: MIT\-0 */
251
252 #define _GNU_SOURCE 1
253 #include <assert\&.h>
254 #include <stdio\&.h>
255 #include <unistd\&.h>
256 #include <systemd/sd\-event\&.h>
257
258 int main(int argc, char **argv) {
259 pid_t pid = fork();
260 assert(pid >= 0);
261
262 /* SIGCHLD signal must be blocked for sd_event_add_child to work */
263 sigset_t ss;
264 sigemptyset(&ss);
265 sigaddset(&ss, SIGCHLD);
266 sigprocmask(SIG_BLOCK, &ss, NULL);
267
268 if (pid == 0) /* child */
269 sleep(1);
270
271 else { /* parent */
272 sd_event *e = NULL;
273 int r;
274
275 /* Create the default event loop */
276 sd_event_default(&e);
277 assert(e);
278
279 /* We create a floating child event source (attached to \*(Aqe\*(Aq)\&.
280 * The default handler will be called with 666 as userdata, which
281 * will become the exit value of the loop\&. */
282 r = sd_event_add_child(e, NULL, pid, WEXITED, NULL, (void*) 666);
283 assert(r >= 0);
284
285 r = sd_event_loop(e);
286 assert(r == 666);
287
288 sd_event_unref(e);
289 }
290
291 return 0;
292 }
293 .fi
294 .if n \{\
295 .RE
296 .\}
297 .SH "HISTORY"
298 .PP
299 \fBsd_event_add_child()\fR,
300 \fBsd_event_child_handler_t()\fR, and
301 \fBsd_event_source_get_child_pid()\fR
302 were added in version 217\&.
303 .PP
304 \fBsd_event_add_child_pidfd()\fR,
305 \fBsd_event_source_get_child_pidfd()\fR,
306 \fBsd_event_source_get_child_pidfd_own()\fR,
307 \fBsd_event_source_set_child_pidfd_own()\fR,
308 \fBsd_event_source_get_child_process_own()\fR,
309 \fBsd_event_source_set_child_process_own()\fR, and
310 \fBsd_event_source_send_child_signal()\fR
311 were added in version 245\&.
312 .SH "SEE ALSO"
313 .PP
314 \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)