summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/forkfd/forkfd_linux.c
blob: 4dacc1919d49a5ccdc0e19ba823cc9b10baf2eea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/****************************************************************************
**
** Copyright (C) 2020 Intel Corporation.
**
** Permission is hereby granted, free of charge, to any person obtaining a copy
** of this software and associated documentation files (the "Software"), to deal
** in the Software without restriction, including without limitation the rights
** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the Software is
** furnished to do so, subject to the following conditions:
**
** The above copyright notice and this permission notice shall be included in
** all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
** THE SOFTWARE.
**
****************************************************************************/

#ifndef _GNU_SOURCE
#  define _GNU_SOURCE
#endif

#include "forkfd.h"

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#include "forkfd_atomic.h"

#ifndef CLONE_PIDFD
#  define CLONE_PIDFD   0x00001000
#endif
#ifndef P_PIDFD
#  define P_PIDFD       3
#endif

#define SYSTEM_FORKFD_CAN_VFORK

// in forkfd.c
static int convertForkfdWaitFlagsToWaitFlags(int ffdoptions);
static void convertStatusToForkfdInfo(int status, struct forkfd_info *info);

static ffd_atomic_int system_forkfd_state = FFD_ATOMIC_INIT(0);

static int sys_waitid(int which, int pid_or_pidfd, siginfo_t *infop, int options,
                      struct rusage *ru)
{
    /* use the waitid raw system call, which has an extra parameter that glibc
     * doesn't offer to us */
    return syscall(__NR_waitid, which, pid_or_pidfd, infop, options, ru);
}

static int sys_clone(unsigned long cloneflags, int *ptid)
{
    void *child_stack = NULL;
    int *ctid = NULL;
    unsigned long newtls = 0;
#if defined(__NR_clone2)
    size_t stack_size = 0;
    return syscall(__NR_clone2, cloneflags, child_stack, stack_size, ptid, ctid, newtls);
#elif defined(__cris__) || defined(__s390__)
    /* a.k.a., CONFIG_CLONE_BACKWARDS2 architectures */
    return syscall(__NR_clone, child_stack, cloneflags, ptid, newtls, ctid);
#elif defined(__microblaze__)
    /* a.k.a., CONFIG_CLONE_BACKWARDS3 architectures */
    size_t stack_size = 0;
    return syscall(__NR_clone, cloneflags, child_stack, stack_size, ptid, newtls, ctid);
#elif defined(__arc__) || defined(__arm__) || defined(__aarch64__) || defined(__mips__) || \
    defined(__nds32__) || defined(__hppa__) || defined(__powerpc__) || defined(__i386__) || \
    defined(__x86_64__) || defined(__xtensa__) || defined(__alpha__) || defined(__riscv) || \
    defined(__loongarch__)
    /* ctid and newtls are inverted on CONFIG_CLONE_BACKWARDS architectures,
     * but since both values are 0, there's no harm. */
    return syscall(__NR_clone, cloneflags, child_stack, ptid, ctid, newtls);
#else
    (void) child_stack;
    (void) ctid;
    (void) newtls;
    errno = ENOSYS;
    return -1;
#endif
}

static int detect_clone_pidfd_support()
{
    /*
     * Detect support for CLONE_PIDFD and P_PIDFD. Support was added in steps:
     * - Linux 5.2 added CLONE_PIDFD support in clone(2) system call
     * - Linux 5.2 added pidfd_send_signal(2)
     * - Linux 5.3 added support for poll(2) on pidfds
     * - Linux 5.3 added clone3(2)
     * - Linux 5.4 added P_PIDFD support in waitid(2)
     *
     * We need CLONE_PIDFD and the poll(2) support. We could emulate the
     * P_PIDFD support by reading the PID from /proc/self/fdinfo/n, which works
     * in Linux 5.2, but without poll(2), we can't guarantee the functionality
     * anyway.
     *
     * So we detect by trying to waitid(2) on a positive file descriptor that
     * is definitely closed (INT_MAX). If P_PIDFD is supported, waitid(2) will
     * return EBADF. If it isn't supported, it returns EINVAL (as it would for
     * a negative file descriptor). This will succeed on Linux 5.4.
     *
     * We could have instead detected by the existence of the clone3(2) system
     * call, but for that we would have needed to wait for __NR_clone3 to show
     * up on the libcs. We choose to go via the waitid(2) route, which requires
     * platform-independent constants only. It would have simplified the
     * sys_clone() mess above...
     */

    sys_waitid(P_PIDFD, INT_MAX, NULL, WEXITED|WNOHANG, NULL);
    return errno == EBADF ? 1 : -1;
}

int system_has_forkfd()
{
    return ffd_atomic_load(&system_forkfd_state, FFD_ATOMIC_RELAXED) > 0;
}

static int system_forkfd_availability(void)
{
    int state = ffd_atomic_load(&system_forkfd_state, FFD_ATOMIC_RELAXED);
    if (state == 0) {
        state = detect_clone_pidfd_support();
        ffd_atomic_store(&system_forkfd_state, state, FFD_ATOMIC_RELAXED);
    }
    return state;
}

static int system_forkfd_pidfd_set_flags(int pidfd, int flags)
{
    if ((flags & FFD_CLOEXEC) == 0) {
        /* pidfd defaults to O_CLOEXEC */
        fcntl(pidfd, F_SETFD, 0);
    }
    if (flags & FFD_NONBLOCK)
        fcntl(pidfd, F_SETFL, fcntl(pidfd, F_GETFL) | O_NONBLOCK);
    return pidfd;
}

int system_vforkfd(int flags, pid_t *ppid, int (*childFn)(void *), void *token, int *system)
{
    __attribute__((aligned(64))) char childStack[SIGSTKSZ];
    pid_t pid;
    int pidfd;
    unsigned long cloneflags = CLONE_PIDFD | CLONE_VFORK | CLONE_VM | SIGCHLD;

    int state = system_forkfd_availability();
    if (state < 0) {
        *system = 0;
        return state;
    }
    *system = 1;

    pid = clone(childFn, childStack + sizeof(childStack), cloneflags, token, &pidfd, NULL, NULL);
    if (pid < 0)
        return pid;
    if (ppid)
        *ppid = pid;
    return system_forkfd_pidfd_set_flags(pidfd, flags);
}

int system_forkfd(int flags, pid_t *ppid, int *system)
{
    pid_t pid;
    int pidfd;

    int state = system_forkfd_availability();
    if (state < 0) {
        *system = 0;
        return state;
    }

    *system = 1;
    unsigned long cloneflags = CLONE_PIDFD | SIGCHLD;
    pid = sys_clone(cloneflags, &pidfd);
    if (pid < 0)
        return pid;
    if (ppid)
        *ppid = pid;

    if (pid == 0) {
        /* Child process */
        return FFD_CHILD_PROCESS;
    }

    /* parent process */
    return system_forkfd_pidfd_set_flags(pidfd, flags);
}

int system_forkfd_wait(int ffd, struct forkfd_info *info, int ffdoptions, struct rusage *rusage)
{
    siginfo_t si;
    int ret;
    int options = convertForkfdWaitFlagsToWaitFlags(ffdoptions);

    if ((options & WNOHANG) == 0) {
        /* check if the file descriptor is non-blocking */
        ret = fcntl(ffd, F_GETFL);
        if (ret == -1)
            return ret;
        if (ret & O_NONBLOCK)
            options |= WNOHANG;
    }

    si.si_status = si.si_code = 0;
    ret = sys_waitid(P_PIDFD, ffd, &si, options, rusage);
    if (info) {
        info->code = si.si_code;
        info->status = si.si_status;
    }
    return ret;
}