1/****************************************************************************
2**
3** Copyright (C) 2020 Intel Corporation.
4** Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com
5**
6** Permission is hereby granted, free of charge, to any person obtaining a copy
7** of this software and associated documentation files (the "Software"), to deal
8** in the Software without restriction, including without limitation the rights
9** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10** copies of the Software, and to permit persons to whom the Software is
11** furnished to do so, subject to the following conditions:
12**
13** The above copyright notice and this permission notice shall be included in
14** all copies or substantial portions of the Software.
15**
16** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22** THE SOFTWARE.
23**
24****************************************************************************/
25
26#ifndef _GNU_SOURCE
27# define _GNU_SOURCE
28#endif
29
30#include "forkfd.h"
31
32/* Macros fine-tuning the build: */
33//#define FORKFD_NO_FORKFD 1 /* disable the forkfd() function */
34//#define FORKFD_NO_SPAWNFD 1 /* disable the spawnfd() function */
35//#define FORKFD_DISABLE_FORK_FALLBACK 1 /* disable falling back to fork() from system_forkfd() */
36
37#include <sys/types.h>
38#if defined(__OpenBSD__) || defined(__NetBSD__)
39# include <sys/param.h>
40#endif
41#include <sys/time.h>
42#include <sys/resource.h>
43#include <sys/wait.h>
44#include <assert.h>
45#include <errno.h>
46#include <pthread.h>
47#include <signal.h>
48#include <stdlib.h>
49#include <string.h>
50#include <time.h>
51#include <unistd.h>
52
53#ifdef __linux__
54# define HAVE_WAIT4 1
55# if defined(__BIONIC__) || (defined(__GLIBC__) && (__GLIBC__ << 8) + __GLIBC_MINOR__ >= 0x208 && \
56 (!defined(__UCLIBC__) || ((__UCLIBC_MAJOR__ << 16) + (__UCLIBC_MINOR__ << 8) + __UCLIBC_SUBLEVEL__ > 0x90201)))
57# include <sys/eventfd.h>
58# ifdef EFD_CLOEXEC
59# define HAVE_EVENTFD 1
60# endif
61# endif
62# if defined(__BIONIC__) || (defined(__GLIBC__) && (__GLIBC__ << 8) + __GLIBC_MINOR__ >= 0x209 && \
63 (!defined(__UCLIBC__) || ((__UCLIBC_MAJOR__ << 16) + (__UCLIBC_MINOR__ << 8) + __UCLIBC_SUBLEVEL__ > 0x90201)))
64# define HAVE_PIPE2 1
65# endif
66#endif
67
68#if _POSIX_VERSION-0 >= 200809L || _XOPEN_VERSION-0 >= 500
69# define HAVE_WAITID 1
70#endif
71#if !defined(WEXITED) || !defined(WNOWAIT)
72# undef HAVE_WAITID
73#endif
74
75#if (defined(__FreeBSD__) && defined(__FreeBSD_version) && __FreeBSD_version >= 1000032) || \
76 (defined(__OpenBSD__) && OpenBSD >= 201505) || \
77 (defined(__NetBSD__) && __NetBSD_Version__ >= 600000000)
78# define HAVE_PIPE2 1
79#endif
80#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__FreeBSD_kernel__) || \
81 defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
82# define HAVE_WAIT4 1
83#endif
84
85#if defined(__APPLE__)
86/* Up until OS X 10.7, waitid(P_ALL, ...) will return success, but will not
87 * fill in the details of the dead child. That means waitid is not useful to us.
88 * Therefore, we only enable waitid() support if we're targetting OS X 10.8 or
89 * later.
90 */
91# include <Availability.h>
92# include <AvailabilityMacros.h>
93# if MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
94# define HAVE_BROKEN_WAITID 1
95# endif
96#endif
97
98#include "forkfd_atomic.h"
99
100static int system_has_forkfd(void);
101static int system_forkfd(int flags, pid_t *ppid, int *system);
102static int system_vforkfd(int flags, pid_t *ppid, int (*)(void *), void *, int *system);
103static int system_forkfd_wait(int ffd, struct forkfd_info *info, int ffdwoptions, struct rusage *rusage);
104
105static int disable_fork_fallback(void)
106{
107#ifdef FORKFD_DISABLE_FORK_FALLBACK
108 /* if there's no system forkfd, we have to use the fallback */
109 return system_has_forkfd();
110#else
111 return 0;
112#endif
113}
114
115#define CHILDREN_IN_SMALL_ARRAY 16
116#define CHILDREN_IN_BIG_ARRAY 256
117#define sizeofarray(array) (sizeof(array)/sizeof(array[0]))
118#define EINTR_LOOP(ret, call) \
119 do { \
120 ret = call; \
121 } while (ret == -1 && errno == EINTR)
122
123struct pipe_payload
124{
125 struct forkfd_info info;
126 struct rusage rusage;
127};
128
129typedef struct process_info
130{
131 ffd_atomic_int pid;
132 int deathPipe;
133} ProcessInfo;
134
135struct BigArray;
136typedef struct Header
137{
138 ffd_atomic_pointer(struct BigArray) nextArray;
139 ffd_atomic_int busyCount;
140} Header;
141
142typedef struct BigArray
143{
144 Header header;
145 ProcessInfo entries[CHILDREN_IN_BIG_ARRAY];
146} BigArray;
147
148typedef struct SmallArray
149{
150 Header header;
151 ProcessInfo entries[CHILDREN_IN_SMALL_ARRAY];
152} SmallArray;
153static SmallArray children;
154
155static struct sigaction old_sigaction;
156static pthread_once_t forkfd_initialization = PTHREAD_ONCE_INIT;
157static ffd_atomic_int forkfd_status = FFD_ATOMIC_INIT(0);
158
159#ifdef HAVE_BROKEN_WAITID
160static int waitid_works = 0;
161#else
162static const int waitid_works = 1;
163#endif
164
165static ProcessInfo *tryAllocateInSection(Header *header, ProcessInfo entries[], int maxCount)
166{
167 /* we use ACQUIRE here because the signal handler might have released the PID */
168 int busyCount = ffd_atomic_add_fetch(&header->busyCount, 1, FFD_ATOMIC_ACQUIRE);
169 if (busyCount <= maxCount) {
170 /* there's an available entry in this section, find it and take it */
171 int i;
172 for (i = 0; i < maxCount; ++i) {
173 /* if the PID is 0, it's free; mark it as used by swapping it with -1 */
174 int expected_pid = 0;
175 if (ffd_atomic_compare_exchange(&entries[i].pid, &expected_pid,
176 -1, FFD_ATOMIC_RELAXED, FFD_ATOMIC_RELAXED))
177 return &entries[i];
178 }
179 }
180
181 /* there isn't an available entry, undo our increment */
182 (void)ffd_atomic_add_fetch(&header->busyCount, -1, FFD_ATOMIC_RELAXED);
183 return NULL;
184}
185
186static ProcessInfo *allocateInfo(Header **header)
187{
188 Header *currentHeader = &children.header;
189
190 /* try to find an available entry in the small array first */
191 ProcessInfo *info =
192 tryAllocateInSection(header: currentHeader, entries: children.entries, sizeofarray(children.entries));
193
194 /* go on to the next arrays */
195 while (info == NULL) {
196 BigArray *array = ffd_atomic_load(&currentHeader->nextArray, FFD_ATOMIC_ACQUIRE);
197 if (array == NULL) {
198 /* allocate an array and try to use it */
199 BigArray *allocatedArray = (BigArray *)calloc(nmemb: 1, size: sizeof(BigArray));
200 if (allocatedArray == NULL)
201 return NULL;
202
203 if (ffd_atomic_compare_exchange(&currentHeader->nextArray, &array, allocatedArray,
204 FFD_ATOMIC_RELEASE, FFD_ATOMIC_ACQUIRE)) {
205 /* success */
206 array = allocatedArray;
207 } else {
208 /* failed, the atomic updated 'array' */
209 free(ptr: allocatedArray);
210 }
211 }
212
213 currentHeader = &array->header;
214 info = tryAllocateInSection(header: currentHeader, entries: array->entries, sizeofarray(array->entries));
215 }
216
217 *header = currentHeader;
218 return info;
219}
220
221#ifdef HAVE_WAITID
222static int isChildReady(pid_t pid, siginfo_t *info)
223{
224 info->si_pid = 0;
225 return waitid(idtype: P_PID, id: pid, infop: info, WEXITED | WNOHANG | WNOWAIT) == 0 && info->si_pid == pid;
226}
227#endif
228
229static void convertStatusToForkfdInfo(int status, struct forkfd_info *info)
230{
231 if (WIFEXITED(status)) {
232 info->code = CLD_EXITED;
233 info->status = WEXITSTATUS(status);
234 } else if (WIFSIGNALED(status)) {
235 info->code = CLD_KILLED;
236# ifdef WCOREDUMP
237 if (WCOREDUMP(status))
238 info->code = CLD_DUMPED;
239# endif
240 info->status = WTERMSIG(status);
241 }
242}
243
244#ifdef __GNUC__
245__attribute__((unused))
246#endif
247static int convertForkfdWaitFlagsToWaitFlags(int ffdoptions)
248{
249 int woptions = WEXITED;
250 if (ffdoptions & FFDW_NOWAIT)
251 woptions |= WNOWAIT;
252 if (ffdoptions & FFDW_NOHANG)
253 woptions |= WNOHANG;
254 return woptions;
255}
256
257static int tryReaping(pid_t pid, struct pipe_payload *payload)
258{
259 /* reap the child */
260#if defined(HAVE_WAIT4)
261 int status;
262 if (wait4(pid: pid, stat_loc: &status, WNOHANG, usage: &payload->rusage) <= 0)
263 return 0;
264 convertStatusToForkfdInfo(status, info: &payload->info);
265#else
266# if defined(HAVE_WAITID)
267 if (waitid_works) {
268 /* we have waitid(2), which gets us some payload values on some systems */
269 siginfo_t info;
270 info.si_pid = 0;
271 int ret = waitid(P_PID, pid, &info, WEXITED | WNOHANG) == 0 && info.si_pid == pid;
272 if (!ret)
273 return ret;
274
275 payload->info.code = info.si_code;
276 payload->info.status = info.si_status;
277# ifdef __linux__
278 payload->rusage.ru_utime.tv_sec = info.si_utime / CLOCKS_PER_SEC;
279 payload->rusage.ru_utime.tv_usec = info.si_utime % CLOCKS_PER_SEC;
280 payload->rusage.ru_stime.tv_sec = info.si_stime / CLOCKS_PER_SEC;
281 payload->rusage.ru_stime.tv_usec = info.si_stime % CLOCKS_PER_SEC;
282# endif
283 return 1;
284 }
285# endif // HAVE_WAITID
286 int status;
287 if (waitpid(pid, &status, WNOHANG) <= 0)
288 return 0; // child did not change state
289 convertStatusToForkfdInfo(status, &payload->info);
290#endif // !HAVE_WAIT4
291
292 return 1;
293}
294
295static void freeInfo(Header *header, ProcessInfo *entry)
296{
297 entry->deathPipe = -1;
298 ffd_atomic_store(&entry->pid, 0, FFD_ATOMIC_RELEASE);
299
300 (void)ffd_atomic_add_fetch(&header->busyCount, -1, FFD_ATOMIC_RELEASE);
301 assert(header->busyCount >= 0);
302}
303
304static void notifyAndFreeInfo(Header *header, ProcessInfo *entry,
305 const struct pipe_payload *payload)
306{
307 ssize_t ret;
308 EINTR_LOOP(ret, write(entry->deathPipe, payload, sizeof(*payload)));
309 EINTR_LOOP(ret, close(entry->deathPipe));
310
311 freeInfo(header, entry);
312}
313
314static void reapChildProcesses();
315static void sigchld_handler(int signum, siginfo_t *handler_info, void *handler_context)
316{
317 /*
318 * This is a signal handler, so we need to be careful about which functions
319 * we can call. See the full, official listing in the POSIX.1-2008
320 * specification at:
321 * http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03
322 *
323 * The handler_info and handler_context parameters may not be valid, if
324 * we're a chained handler from another handler that did not use
325 * SA_SIGINFO. Therefore, we must obtain the siginfo ourselves directly by
326 * calling waitid.
327 *
328 * But we pass them anyway. Let's call the chained handler first, while
329 * those two arguments have a chance of being correct.
330 */
331 if (old_sigaction.sa_handler != SIG_IGN && old_sigaction.sa_handler != SIG_DFL) {
332 if (old_sigaction.sa_flags & SA_SIGINFO)
333 old_sigaction.sa_sigaction(signum, handler_info, handler_context);
334 else
335 old_sigaction.sa_handler(signum);
336 }
337
338 if (ffd_atomic_load(&forkfd_status, FFD_ATOMIC_RELAXED) == 1) {
339 int saved_errno = errno;
340 reapChildProcesses();
341 errno = saved_errno;
342 }
343}
344
345static inline void reapChildProcesses()
346{
347 /* is this one of our children? */
348 BigArray *array;
349 siginfo_t info;
350 struct pipe_payload payload;
351 int i;
352
353 memset(s: &info, c: 0, n: sizeof info);
354 memset(s: &payload, c: 0, n: sizeof payload);
355
356#ifdef HAVE_WAITID
357 if (waitid_works) {
358 /* be optimistic: try to see if we can get the child that exited */
359search_next_child:
360 /* waitid returns -1 ECHILD if there are no further children at all;
361 * it returns 0 and sets si_pid to 0 if there are children but they are not ready
362 * to be waited (we're passing WNOHANG). We should not get EINTR because
363 * we're passing WNOHANG and we should definitely not get EINVAL or anything else.
364 * That means we can actually ignore the return code and only inspect si_pid.
365 */
366 info.si_pid = 0;
367 waitid(idtype: P_ALL, id: 0, infop: &info, WNOHANG | WNOWAIT | WEXITED);
368 if (info.si_pid == 0) {
369 /* there are no further un-waited-for children, so we can just exit.
370 */
371 return;
372 }
373
374 for (i = 0; i < (int)sizeofarray(children.entries); ++i) {
375 /* acquire the child first: swap the PID with -1 to indicate it's busy */
376 int pid = info.si_pid;
377 if (ffd_atomic_compare_exchange(&children.entries[i].pid, &pid, -1,
378 FFD_ATOMIC_ACQUIRE, FFD_ATOMIC_RELAXED)) {
379 /* this is our child, send notification and free up this entry */
380 /* ### FIXME: what if tryReaping returns false? */
381 if (tryReaping(pid, payload: &payload))
382 notifyAndFreeInfo(header: &children.header, entry: &children.entries[i], payload: &payload);
383 goto search_next_child;
384 }
385 }
386
387 /* try the arrays */
388 array = ffd_atomic_load(&children.header.nextArray, FFD_ATOMIC_ACQUIRE);
389 while (array != NULL) {
390 for (i = 0; i < (int)sizeofarray(array->entries); ++i) {
391 int pid = info.si_pid;
392 if (ffd_atomic_compare_exchange(&array->entries[i].pid, &pid, -1,
393 FFD_ATOMIC_ACQUIRE, FFD_ATOMIC_RELAXED)) {
394 /* this is our child, send notification and free up this entry */
395 /* ### FIXME: what if tryReaping returns false? */
396 if (tryReaping(pid, payload: &payload))
397 notifyAndFreeInfo(header: &array->header, entry: &array->entries[i], payload: &payload);
398 goto search_next_child;
399 }
400 }
401
402 array = ffd_atomic_load(&array->header.nextArray, FFD_ATOMIC_ACQUIRE);
403 }
404
405 /* if we got here, we couldn't find this child in our list. That means this child
406 * belongs to one of the chained SIGCHLD handlers. However, there might be another
407 * child that exited and does belong to us, so we need to check each one individually.
408 */
409 }
410#endif
411
412 for (i = 0; i < (int)sizeofarray(children.entries); ++i) {
413 int pid = ffd_atomic_load(&children.entries[i].pid, FFD_ATOMIC_ACQUIRE);
414 if (pid <= 0)
415 continue;
416#ifdef HAVE_WAITID
417 if (waitid_works) {
418 /* The child might have been reaped by the block above in another thread,
419 * so first check if it's ready and, if it is, lock it */
420 if (!isChildReady(pid, info: &info) ||
421 !ffd_atomic_compare_exchange(&children.entries[i].pid, &pid, -1,
422 FFD_ATOMIC_RELAXED, FFD_ATOMIC_RELAXED))
423 continue;
424 }
425#endif
426 if (tryReaping(pid, payload: &payload)) {
427 /* this is our child, send notification and free up this entry */
428 notifyAndFreeInfo(header: &children.header, entry: &children.entries[i], payload: &payload);
429 }
430 }
431
432 /* try the arrays */
433 array = ffd_atomic_load(&children.header.nextArray, FFD_ATOMIC_ACQUIRE);
434 while (array != NULL) {
435 for (i = 0; i < (int)sizeofarray(array->entries); ++i) {
436 int pid = ffd_atomic_load(&array->entries[i].pid, FFD_ATOMIC_ACQUIRE);
437 if (pid <= 0)
438 continue;
439#ifdef HAVE_WAITID
440 if (waitid_works) {
441 /* The child might have been reaped by the block above in another thread,
442 * so first check if it's ready and, if it is, lock it */
443 if (!isChildReady(pid, info: &info) ||
444 !ffd_atomic_compare_exchange(&array->entries[i].pid, &pid, -1,
445 FFD_ATOMIC_RELAXED, FFD_ATOMIC_RELAXED))
446 continue;
447 }
448#endif
449 if (tryReaping(pid, payload: &payload)) {
450 /* this is our child, send notification and free up this entry */
451 notifyAndFreeInfo(header: &array->header, entry: &array->entries[i], payload: &payload);
452 }
453 }
454
455 array = ffd_atomic_load(&array->header.nextArray, FFD_ATOMIC_ACQUIRE);
456 }
457}
458
459static void ignore_sigpipe()
460{
461#ifdef O_NOSIGPIPE
462 static ffd_atomic_int done = FFD_ATOMIC_INIT(0);
463 if (ffd_atomic_load(&done, FFD_ATOMIC_RELAXED))
464 return;
465#endif
466
467 struct sigaction action;
468 memset(s: &action, c: 0, n: sizeof action);
469 sigemptyset(set: &action.sa_mask);
470 action.sa_handler = SIG_IGN;
471 action.sa_flags = 0;
472 sigaction(SIGPIPE, act: &action, NULL);
473
474#ifdef O_NOSIGPIPE
475 ffd_atomic_store(&done, 1, FFD_ATOMIC_RELAXED);
476#endif
477}
478
479#if defined(__GNUC__) && (!defined(__FreeBSD__) || __FreeBSD__ < 10)
480__attribute((destructor, unused)) static void cleanup();
481#endif
482
483static void cleanup()
484{
485 BigArray *array;
486 /* This function is not thread-safe!
487 * It must only be called when the process is shutting down.
488 * At shutdown, we expect no one to be calling forkfd(), so we don't
489 * need to be thread-safe with what is done there.
490 *
491 * But SIGCHLD might be delivered to any thread, including this one.
492 * There's no way to prevent that. The correct solution would be to
493 * cooperatively delete. We don't do that.
494 */
495 if (ffd_atomic_load(&forkfd_status, FFD_ATOMIC_RELAXED) == 0)
496 return;
497
498 /* notify the handler that we're no longer in operation */
499 ffd_atomic_store(&forkfd_status, 0, FFD_ATOMIC_RELAXED);
500
501 /* free any arrays we might have */
502 array = ffd_atomic_load(&children.header.nextArray, FFD_ATOMIC_ACQUIRE);
503 while (array != NULL) {
504 BigArray *next = ffd_atomic_load(&array->header.nextArray, FFD_ATOMIC_ACQUIRE);
505 free(ptr: array);
506 array = next;
507 }
508}
509
510static void forkfd_initialize()
511{
512#if defined(HAVE_BROKEN_WAITID)
513 pid_t pid = fork();
514 if (pid == 0) {
515 _exit(0);
516 } else if (pid > 0) {
517 siginfo_t info;
518 waitid(P_ALL, 0, &info, WNOWAIT | WEXITED);
519 waitid_works = (info.si_pid != 0);
520 info.si_pid = 0;
521
522 // now really reap the child
523 waitid(P_PID, pid, &info, WEXITED);
524 waitid_works = waitid_works && (info.si_pid != 0);
525 }
526#endif
527
528 /* install our signal handler */
529 struct sigaction action;
530 memset(s: &action, c: 0, n: sizeof action);
531 sigemptyset(set: &action.sa_mask);
532 action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
533 action.sa_sigaction = sigchld_handler;
534
535 /* ### RACE CONDITION
536 * The sigaction function does a memcpy from an internal buffer
537 * to old_sigaction, which we use in the SIGCHLD handler. If a
538 * SIGCHLD is delivered before or during that memcpy, the handler will
539 * see an inconsistent state.
540 *
541 * There is no solution. pthread_sigmask doesn't work here because the
542 * signal could be delivered to another thread.
543 */
544 sigaction(SIGCHLD, act: &action, oact: &old_sigaction);
545
546#ifndef O_NOSIGPIPE
547 /* disable SIGPIPE too */
548 ignore_sigpipe();
549#endif
550
551#ifdef __GNUC__
552 (void) cleanup; /* suppress unused static function warning */
553#else
554 atexit(cleanup);
555#endif
556
557 ffd_atomic_store(&forkfd_status, 1, FFD_ATOMIC_RELAXED);
558}
559
560static int create_pipe(int filedes[], int flags)
561{
562 int ret = -1;
563#ifdef HAVE_PIPE2
564 /* use pipe2(2) whenever possible, since it can thread-safely create a
565 * cloexec pair of pipes. Without it, we have a race condition setting
566 * FD_CLOEXEC
567 */
568
569# ifdef O_NOSIGPIPE
570 /* try first with O_NOSIGPIPE */
571 ret = pipe2(filedes, O_CLOEXEC | O_NOSIGPIPE);
572 if (ret == -1) {
573 /* O_NOSIGPIPE not supported, ignore SIGPIPE */
574 ignore_sigpipe();
575 }
576# endif
577 if (ret == -1)
578 ret = pipe2(pipedes: filedes, O_CLOEXEC);
579 if (ret == -1)
580 return ret;
581
582 if ((flags & FFD_CLOEXEC) == 0)
583 fcntl(fd: filedes[0], F_SETFD, 0);
584#else
585 ret = pipe(filedes);
586 if (ret == -1)
587 return ret;
588
589 fcntl(filedes[1], F_SETFD, FD_CLOEXEC);
590 if (flags & FFD_CLOEXEC)
591 fcntl(filedes[0], F_SETFD, FD_CLOEXEC);
592#endif
593 if (flags & FFD_NONBLOCK)
594 fcntl(fd: filedes[0], F_SETFL, fcntl(fd: filedes[0], F_GETFL) | O_NONBLOCK);
595 return ret;
596}
597
598#ifndef FORKFD_NO_FORKFD
599static int forkfd_fork_fallback(int flags, pid_t *ppid)
600{
601 Header *header;
602 ProcessInfo *info;
603 pid_t pid;
604 int fd = -1;
605 int death_pipe[2];
606 int sync_pipe[2];
607 int ret;
608#ifdef __linux__
609 int efd;
610#endif
611
612 (void) pthread_once(once_control: &forkfd_initialization, init_routine: forkfd_initialize);
613
614 info = allocateInfo(header: &header);
615 if (info == NULL) {
616 errno = ENOMEM;
617 return -1;
618 }
619
620 /* create the pipes before we fork */
621 if (create_pipe(filedes: death_pipe, flags) == -1)
622 goto err_free; /* failed to create the pipes, pass errno */
623
624#ifdef HAVE_EVENTFD
625 /* try using an eventfd, which consumes less resources */
626 efd = eventfd(count: 0, EFD_CLOEXEC);
627 if (efd == -1)
628#endif
629 {
630 /* try a pipe */
631 if (create_pipe(filedes: sync_pipe, FFD_CLOEXEC) == -1) {
632 /* failed both at eventfd and pipe; fail and pass errno */
633 goto err_close;
634 }
635 }
636
637 /* now fork */
638 pid = fork();
639 if (pid == -1)
640 goto err_close2; /* failed to fork, pass errno */
641 if (ppid)
642 *ppid = pid;
643
644 /*
645 * We need to store the child's PID in the info structure, so
646 * the SIGCHLD handler knows that this child is present and it
647 * knows the writing end of the pipe to pass information on.
648 * However, the child process could exit before we stored the
649 * information (or the handler could run for other children exiting).
650 * We prevent that from happening by blocking the child process in
651 * a read(2) until we're finished storing the information.
652 */
653 if (pid == 0) {
654 /* this is the child process */
655 /* first, wait for the all clear */
656#ifdef HAVE_EVENTFD
657 if (efd != -1) {
658 eventfd_t val64;
659 EINTR_LOOP(ret, eventfd_read(efd, &val64));
660 EINTR_LOOP(ret, close(efd));
661 } else
662#endif
663 {
664 char c;
665 EINTR_LOOP(ret, close(sync_pipe[1]));
666 EINTR_LOOP(ret, read(sync_pipe[0], &c, sizeof c));
667 EINTR_LOOP(ret, close(sync_pipe[0]));
668 }
669
670 /* now close the pipes and return to the caller */
671 EINTR_LOOP(ret, close(death_pipe[0]));
672 EINTR_LOOP(ret, close(death_pipe[1]));
673 fd = FFD_CHILD_PROCESS;
674 } else {
675 /* parent process */
676 info->deathPipe = death_pipe[1];
677 fd = death_pipe[0];
678 ffd_atomic_store(&info->pid, pid, FFD_ATOMIC_RELEASE);
679
680 /* release the child */
681#ifdef HAVE_EVENTFD
682 if (efd != -1) {
683 eventfd_t val64 = 42;
684 EINTR_LOOP(ret, eventfd_write(efd, val64));
685 EINTR_LOOP(ret, close(efd));
686 } else
687#endif
688 {
689 /*
690 * Usually, closing would be enough to make read(2) return and the child process
691 * continue. We need to write here: another thread could be calling forkfd at the
692 * same time, which means auxpipe[1] might be open in another child process.
693 */
694 EINTR_LOOP(ret, close(sync_pipe[0]));
695 EINTR_LOOP(ret, write(sync_pipe[1], "", 1));
696 EINTR_LOOP(ret, close(sync_pipe[1]));
697 }
698 }
699
700 return fd;
701
702err_close2:
703#ifdef HAVE_EVENTFD
704 if (efd != -1) {
705 EINTR_LOOP(ret, close(efd));
706 } else
707#endif
708 {
709 EINTR_LOOP(ret, close(sync_pipe[0]));
710 EINTR_LOOP(ret, close(sync_pipe[1]));
711 }
712err_close:
713 EINTR_LOOP(ret, close(death_pipe[0]));
714 EINTR_LOOP(ret, close(death_pipe[1]));
715err_free:
716 /* free the info pointer */
717 freeInfo(header, entry: info);
718 return -1;
719}
720
721/**
722 * @brief forkfd returns a file descriptor representing a child process
723 * @return a file descriptor, or -1 in case of failure
724 *
725 * forkfd() creates a file descriptor that can be used to be notified of when a
726 * child process exits. This file descriptor can be monitored using select(2),
727 * poll(2) or similar mechanisms.
728 *
729 * The @a flags parameter can contain the following values ORed to change the
730 * behaviour of forkfd():
731 *
732 * @li @c FFD_NONBLOCK Set the O_NONBLOCK file status flag on the new open file
733 * descriptor. Using this flag saves extra calls to fnctl(2) to achieve the same
734 * result.
735 *
736 * @li @c FFD_CLOEXEC Set the close-on-exec (FD_CLOEXEC) flag on the new file
737 * descriptor. You probably want to set this flag, since forkfd() does not work
738 * if the original parent process dies.
739 *
740 * @li @c FFD_USE_FORK Tell forkfd() to actually call fork() instead of a
741 * different system implementation that may be available. On systems where a
742 * different implementation is available, its behavior may differ from that of
743 * fork(), such as not calling the functions registered with pthread_atfork().
744 * If that's necessary, pass this flag.
745 *
746 * The file descriptor returned by forkfd() supports the following operations:
747 *
748 * @li read(2) When the child process exits, then the buffer supplied to
749 * read(2) is used to return information about the status of the child in the
750 * form of one @c siginfo_t structure. The buffer must be at least
751 * sizeof(siginfo_t) bytes. The return value of read(2) is the total number of
752 * bytes read.
753 *
754 * @li poll(2), select(2) (and similar) The file descriptor is readable (the
755 * select(2) readfds argument; the poll(2) POLLIN flag) if the child has exited
756 * or signalled via SIGCHLD.
757 *
758 * @li close(2) When the file descriptor is no longer required it should be closed.
759 */
760int forkfd(int flags, pid_t *ppid)
761{
762 int fd;
763 if (disable_fork_fallback())
764 flags &= ~FFD_USE_FORK;
765
766 if ((flags & FFD_USE_FORK) == 0) {
767 int system_forkfd_works;
768 fd = system_forkfd(flags, ppid, system: &system_forkfd_works);
769 if (system_forkfd_works || disable_fork_fallback())
770 return fd;
771 }
772
773 return forkfd_fork_fallback(flags, ppid);
774}
775
776/**
777 * @brief vforkfd returns a file descriptor representing a child process
778 * @return a file descriptor, or -1 in case of failure
779 *
780 * vforkfd() operates in the same way as forkfd() and the @a flags and @a ppid
781 * arguments are the same. See the forkfd() documentation for details on the
782 * possible values and information on the returned file descriptor.
783 *
784 * This function does not return @c FFD_CHILD_PROCESS. Instead, the function @a
785 * childFn is called in the child process with the @a token parameter as
786 * argument. If that function returns, its return value will be passed to
787 * _exit(2).
788 *
789 * This function differs from forkfd() the same way that vfork() differs from
790 * fork(): the parent process may be suspended while the child is has not yet
791 * called _exit(2) or execve(2). Additionally, on some systems, the child
792 * process may share memory with the parent process the same way an auxiliary
793 * thread would, so extreme care should be employed on what functions the child
794 * process uses before termination.
795 *
796 * The @c FFD_USE_FORK flag retains its behavior as described in the forkfd()
797 * documentation, including that of actually using fork(2) and no other
798 * implementation.
799 *
800 * Currently, only on Linux will this function have any behavior different from
801 * forkfd(). In all other systems, it is equivalent to the following code:
802 *
803 * @code
804 * int ffd = forkfd(flags, &pid);
805 * if (ffd == FFD_CHILD_PROCESS)
806 * _exit(childFn(token));
807 * @endcode
808 */
809int vforkfd(int flags, pid_t *ppid, int (*childFn)(void *), void *token)
810{
811 int fd;
812 if ((flags & FFD_USE_FORK) == 0) {
813 int system_forkfd_works;
814 fd = system_vforkfd(flags, ppid, childFn, token, system: &system_forkfd_works);
815 if (system_forkfd_works || disable_fork_fallback())
816 return fd;
817 }
818
819 fd = forkfd_fork_fallback(flags, ppid);
820 if (fd == FFD_CHILD_PROCESS) {
821 /* child process */
822 _exit(status: childFn(token));
823 }
824 return fd;
825}
826#endif // FORKFD_NO_FORKFD
827
828#if _POSIX_SPAWN > 0 && !defined(FORKFD_NO_SPAWNFD)
829int spawnfd(int flags, pid_t *ppid, const char *path, const posix_spawn_file_actions_t *file_actions,
830 posix_spawnattr_t *attrp, char *const argv[], char *const envp[])
831{
832 Header *header;
833 ProcessInfo *info;
834 struct pipe_payload payload;
835 pid_t pid;
836 int death_pipe[2];
837 int ret = -1;
838 /* we can only do work if we have a way to start the child in stopped mode;
839 * otherwise, we have a major race condition. */
840
841 assert(!system_has_forkfd());
842
843 (void) pthread_once(&forkfd_initialization, forkfd_initialize);
844
845 info = allocateInfo(&header);
846 if (info == NULL) {
847 errno = ENOMEM;
848 goto out;
849 }
850
851 /* create the pipe before we spawn */
852 if (create_pipe(death_pipe, flags) == -1)
853 goto err_free; /* failed to create the pipes, pass errno */
854
855 /* start the process */
856 if (flags & FFD_SPAWN_SEARCH_PATH) {
857 /* use posix_spawnp */
858 if (posix_spawnp(&pid, path, file_actions, attrp, argv, envp) != 0)
859 goto err_close;
860 } else {
861 if (posix_spawn(&pid, path, file_actions, attrp, argv, envp) != 0)
862 goto err_close;
863 }
864
865 if (ppid)
866 *ppid = pid;
867
868 /* Store the child's PID in the info structure.
869 */
870 info->deathPipe = death_pipe[1];
871 ffd_atomic_store(&info->pid, pid, FFD_ATOMIC_RELEASE);
872
873 /* check if the child has already exited */
874 if (tryReaping(pid, &payload))
875 notifyAndFreeInfo(header, info, &payload);
876
877 ret = death_pipe[0];
878 return ret;
879
880err_close:
881 EINTR_LOOP(ret, close(death_pipe[0]));
882 EINTR_LOOP(ret, close(death_pipe[1]));
883
884err_free:
885 /* free the info pointer */
886 freeInfo(header, info);
887
888out:
889 return -1;
890}
891#endif // _POSIX_SPAWN && !FORKFD_NO_SPAWNFD
892
893int forkfd_wait4(int ffd, struct forkfd_info *info, int options, struct rusage *rusage)
894{
895 struct pipe_payload payload;
896 int ret;
897
898 if (system_has_forkfd()) {
899 /* if this is one of our pipes, not a procdesc/pidfd, we'll get an EBADF */
900 ret = system_forkfd_wait(ffd, info, ffdwoptions: options, rusage);
901 if (disable_fork_fallback() || ret != -1 || errno != EBADF)
902 return ret;
903 }
904
905 ret = read(fd: ffd, buf: &payload, nbytes: sizeof(payload));
906 if (ret == -1)
907 return ret; /* pass errno, probably EINTR, EBADF or EWOULDBLOCK */
908
909 assert(ret == sizeof(payload));
910 if (info)
911 *info = payload.info;
912 if (rusage)
913 *rusage = payload.rusage;
914
915 return 0; /* success */
916}
917
918
919int forkfd_close(int ffd)
920{
921 return close(fd: ffd);
922}
923
924#if defined(__FreeBSD__) && __FreeBSD__ >= 9
925# include "forkfd_freebsd.c"
926#elif defined(__linux__)
927# include "forkfd_linux.c"
928#else
929int system_has_forkfd()
930{
931 return 0;
932}
933
934int system_forkfd(int flags, pid_t *ppid, int *system)
935{
936 (void)flags;
937 (void)ppid;
938 *system = 0;
939 return -1;
940}
941
942int system_forkfd_wait(int ffd, struct forkfd_info *info, int options, struct rusage *rusage)
943{
944 (void)ffd;
945 (void)info;
946 (void)options;
947 (void)rusage;
948 return -1;
949}
950#endif
951#ifndef SYSTEM_FORKFD_CAN_VFORK
952int system_vforkfd(int flags, pid_t *ppid, int (*childFn)(void *), void *token, int *system)
953{
954 /* we don't have a way to vfork(), so fake it */
955 int ret = system_forkfd(flags, ppid, system);
956 if (ret == FFD_CHILD_PROCESS) {
957 /* child process */
958 _exit(childFn(token));
959 }
960 return ret;
961}
962#endif
963#undef SYSTEM_FORKFD_CAN_VFORK
964

source code of qtbase/src/3rdparty/forkfd/forkfd.c