summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/forkfd/forkfd_freebsd.c
blob: ba18d835915a3e0c38331edfe6644820c03d0e98 (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
/****************************************************************************
**
** 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.
**
****************************************************************************/

#include "forkfd.h"

#include <sys/types.h>
#include <sys/procdesc.h>

#include "forkfd_atomic.h"

#undef SYSTEM_FORKFD_CAN_VFORK

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

#if __FreeBSD__ >= 10
/* On FreeBSD 10, PROCDESC was enabled by default. On v11, it's not an option
 * anymore and can't be disabled. */
static ffd_atomic_int system_forkfd_state = FFD_ATOMIC_INIT(1);
#else
static ffd_atomic_int system_forkfd_state = FFD_ATOMIC_INIT(0);
#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)
{
    int ret;
    pid_t pid;

    int state = ffd_atomic_load(&system_forkfd_state, FFD_ATOMIC_RELAXED);
    *system = 0;
    if (state < 0)
        return -1;

    pid = pdfork(&ret, PD_DAEMON);
#  if __FreeBSD__ == 9
    if (state == 0 && pid != 0) {
        /* Parent process: remember whether PROCDESC was compiled into the kernel */
        state = (pid == -1 && errno == ENOSYS) ? -1 : 1;
        ffd_atomic_store(&system_forkfd_state, state, FFD_ATOMIC_RELAXED);
    }
    if (state < 0)
        return -1;
#  endif
    *system = 1;
    if (__builtin_expect(pid == -1, 0))
        return -1;

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

    /* parent process */
    if (flags & FFD_CLOEXEC)
        fcntl(ret, F_SETFD, FD_CLOEXEC);
    if (flags & FFD_NONBLOCK)
        fcntl(ret, F_SETFL, fcntl(ret, F_GETFL) | O_NONBLOCK);
    if (ppid)
        *ppid = pid;
    return ret;
}

int system_forkfd_wait(int ffd, struct forkfd_info *info, int ffdoptions, struct rusage *rusage)
{
    pid_t pid;
    int status;
    int options = convertForkfdWaitFlagsToWaitFlags(ffdoptions);

    int ret = pdgetpid(ffd, &pid);
    if (ret == -1)
        return ret;

    if ((options & WNOHANG) == 0) {
        /* check if the file descriptor is non-blocking */
        ret = fcntl(ffd, F_GETFL);
        if (ret == -1)
            return ret;
        options |= (ret & O_NONBLOCK) ? WNOHANG : 0;
    }
    ret = wait4(pid, &status, options, rusage);
    if (ret != -1 && info)
        convertStatusToForkfdInfo(status, info);
    return ret == -1 ? -1 : 0;
}