summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/forkfd/forkfd_linux.c
blob: ea587b7ed5a641ca4fc87ecbd4e0cde04ec3f00f (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/****************************************************************************
**
** Copyright (C) 2019 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

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__)
    /* 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;

#if 0
    /* Detection methods not used: */
#ifdef __NR_pidfd_send_signal
    /*
     * pidfd_send_signal was added at the same time as CLONE_PIDFD, so if this
     * system call exists, so does CLONE_PIDFD. We make a system call with a
     * file descriptor of -1: if it's supported, we get EBADF; otherwise, the
     * typical ENOSYS.
     */
    syscall(__NR_pidfd_send_signal, -1, 0, NULL, 0);
    return errno == EBADF ? 1 : -1;
#else
    /*
     * detect kernel CLONE_PIDFD support directly: CLONE_PIDFD |
     * CLONE_PARENT_SETTID causes EINVAL on kernel >= 5.2, but on older
     * kernels, that combination is ignored. Therefore, if we EINVAL, we know
     * that CLONE_PIDFD is supported.
     *
     * To avoid creating a process unnecessarily, we add CLONE_NEWUTS, which is
     * a privileged operation. Therefore, if we're not root, we'll get EPERM.
     * If we're root, we need to exit the child process and wait for it on the
     * parent.
     */
    pid_t pid = sys_clone(CLONE_PIDFD | CLONE_PARENT_SETTID | CLONE_NEWUTS, NULL);
    if (pid == -1 && errno == EINVAL)
        return 1;
    if (pid == 0)
        _exit(0);               /* Child */
    if (pid > 0)
        sys_waitid(P_PID, pid, NULL, WEXITED | __WALL, NULL);   /* Parent */

    return -1;
#endif
#endif  // 0
}

#if 0
/* To be used if waitid's P_PIDFD support gets bumped to 5.4 */
static pid_t pidfd_to_pid(int pidfd)
{
    static const char pidtext[] = "Pid:\t";
    int fdinfo;
    ssize_t ret;
    char buf[256];
    char *text, *endpid = NULL;

    snprintf(buf, sizeof(buf), "/proc/self/fdinfo/%d", pidfd);
    fdinfo = open(buf, O_RDONLY | O_CLOEXEC);
    if (fdinfo < 0)
        return fdinfo;

    ret = read(fdinfo, buf, sizeof(buf) - 1);
    close(fdinfo);
    if (ret < 1)
        return ret;

    buf[ret] = '\0';
    text = (char *) memmem(buf, ret, pidtext, sizeof(pidtext) - 1);
    if (text == NULL)
        return -1;

    text += sizeof(pidtext) - 1;
    ret = strtol(text, &endpid, 10);
    if (ret < 0 || (endpid && *endpid != '\n'))
        return -1;
    return ret;
}
#endif

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

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

    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);
    }
    if (state < 0) {
        *system = 0;
        return state;
    }

    *system = 1;
    pid = sys_clone(CLONE_PIDFD, &pidfd);
    if (ppid)
        *ppid = pid;

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

    /* parent process */
    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_forkfd_wait(int ffd, struct forkfd_info *info, struct rusage *rusage)
{
    siginfo_t si;
    int options = WEXITED | __WALL;
    int ret = fcntl(ffd, F_GETFL);
    if (ret == -1)
        return ret;
    if (ret & O_NONBLOCK)
        options |= WNOHANG;

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