2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/calls.h"
|
2022-09-08 18:54:56 +00:00
|
|
|
#include "libc/calls/struct/sigset.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-nt.internal.h"
|
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/dce.h"
|
2022-10-03 05:14:33 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2022-03-16 20:33:13 +00:00
|
|
|
#include "libc/nt/process.h"
|
2022-04-20 16:56:53 +00:00
|
|
|
#include "libc/runtime/internal.h"
|
2022-09-08 18:54:56 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2022-09-10 09:56:25 +00:00
|
|
|
#include "libc/thread/tls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
|
|
|
/**
|
2021-07-04 19:21:29 +00:00
|
|
|
* Creates new process.
|
2020-06-15 14:18:57 +00:00
|
|
|
*
|
2022-10-03 05:14:33 +00:00
|
|
|
* @return 0 to child, child pid to parent, or -1 w/ errno
|
|
|
|
* @raise EAGAIN if `RLIMIT_NPROC` was exceeded or system lacked resources
|
|
|
|
* @raise ENOMEM if we require more vespene gas
|
2020-06-15 14:18:57 +00:00
|
|
|
* @asyncsignalsafe
|
|
|
|
*/
|
|
|
|
int fork(void) {
|
2021-02-07 14:11:44 +00:00
|
|
|
axdx_t ad;
|
2022-09-08 18:54:56 +00:00
|
|
|
sigset_t old, all;
|
2022-03-16 20:33:13 +00:00
|
|
|
int ax, dx, parent;
|
2022-09-08 18:54:56 +00:00
|
|
|
sigfillset(&all);
|
|
|
|
sigprocmask(SIG_BLOCK, &all, &old);
|
2020-11-13 09:27:49 +00:00
|
|
|
if (!IsWindows()) {
|
2021-02-07 14:11:44 +00:00
|
|
|
ad = sys_fork();
|
|
|
|
ax = ad.ax;
|
|
|
|
dx = ad.dx;
|
|
|
|
if (IsXnu() && ax != -1) {
|
2022-09-08 18:54:56 +00:00
|
|
|
// eax always returned with childs pid
|
|
|
|
// edx is 0 for parent and 1 for child
|
2021-02-07 14:11:44 +00:00
|
|
|
ax &= dx - 1;
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
} else {
|
2021-02-07 14:11:44 +00:00
|
|
|
ax = sys_fork_nt();
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
if (!ax) {
|
2022-03-16 20:33:13 +00:00
|
|
|
if (!IsWindows()) {
|
|
|
|
dx = sys_getpid().ax;
|
|
|
|
} else {
|
|
|
|
dx = GetCurrentProcessId();
|
|
|
|
}
|
|
|
|
parent = __pid;
|
|
|
|
__pid = dx;
|
2022-07-25 02:40:32 +00:00
|
|
|
if (__tls_enabled) {
|
2022-09-10 09:56:25 +00:00
|
|
|
__get_tls()->tib_tid = IsLinux() ? dx : sys_gettid();
|
2022-07-25 02:40:32 +00:00
|
|
|
}
|
2022-09-08 18:54:56 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &old, 0);
|
2022-10-03 05:14:33 +00:00
|
|
|
STRACE("fork() → 0 (child of %d)", parent);
|
2022-03-16 20:33:13 +00:00
|
|
|
} else {
|
2022-09-08 18:54:56 +00:00
|
|
|
sigprocmask(SIG_SETMASK, &old, 0);
|
2022-10-03 05:14:33 +00:00
|
|
|
STRACE("fork() → %d% m", ax);
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2021-02-07 14:11:44 +00:00
|
|
|
return ax;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|