mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-27 04:50:28 +00:00
Put more thought into i/o polyfills
wait4() is now solid enough to run `make -j100` on Windows. You can now use MSG_DONTWAIT on Windows. There was a handle leak in accept() that's been fixed. Our WIN32 overlapped i/o code has been simplified. Priority class now inherits into subprocesses, so the verynice command will work and the signal mask will now be inherited by execve() and posix_spawn()
This commit is contained in:
parent
736fdb757a
commit
e961385e55
52 changed files with 679 additions and 487 deletions
|
@ -22,6 +22,7 @@
|
|||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/syscall-nt.internal.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/enum/processaccess.h"
|
||||
|
@ -46,7 +47,7 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|||
char *const envp[]) {
|
||||
|
||||
// execve() needs to be @asyncsignalsafe
|
||||
sigset_t m = __sig_block();
|
||||
sigset_t sigmask = __sig_block();
|
||||
_pthread_lock();
|
||||
|
||||
// new process should be a child of our parent
|
||||
|
@ -55,10 +56,14 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|||
if (!(hParentProcess = OpenProcess(
|
||||
kNtProcessDupHandle | kNtProcessCreateProcess, false, ppid))) {
|
||||
_pthread_unlock();
|
||||
__sig_unblock(m);
|
||||
__sig_unblock(sigmask);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// inherit signal mask
|
||||
char maskvar[6 + 21];
|
||||
FormatUint64(stpcpy(maskvar, "_MASK="), sigmask);
|
||||
|
||||
// define stdio handles for the spawned subprocess
|
||||
struct NtStartupInfo si = {
|
||||
.cb = sizeof(struct NtStartupInfo),
|
||||
|
@ -80,21 +85,21 @@ textwindows int sys_execve_nt(const char *program, char *const argv[],
|
|||
&lpExplicitHandles, &dwExplicitHandleCount))) {
|
||||
CloseHandle(hParentProcess);
|
||||
_pthread_unlock();
|
||||
__sig_unblock(m);
|
||||
__sig_unblock(sigmask);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// launch the process
|
||||
struct NtProcessInformation pi;
|
||||
int rc = ntspawn(AT_FDCWD, program, argv, envp, (char *[]){fdspec, 0}, 0, 0,
|
||||
hParentProcess, lpExplicitHandles, dwExplicitHandleCount,
|
||||
&si, &pi);
|
||||
int rc = ntspawn(AT_FDCWD, program, argv, envp,
|
||||
(char *[]){fdspec, maskvar, 0}, 0, 0, hParentProcess,
|
||||
lpExplicitHandles, dwExplicitHandleCount, &si, &pi);
|
||||
__undescribe_fds(hParentProcess, lpExplicitHandles, dwExplicitHandleCount);
|
||||
if (rc == -1) {
|
||||
free(fdspec);
|
||||
CloseHandle(hParentProcess);
|
||||
_pthread_unlock();
|
||||
__sig_unblock(m);
|
||||
__sig_unblock(sigmask);
|
||||
if (GetLastError() == kNtErrorSharingViolation) {
|
||||
return etxtbsy();
|
||||
} else {
|
||||
|
|
|
@ -401,7 +401,7 @@ textwindows int sys_fork_nt(uint32_t dwCreationFlags) {
|
|||
}
|
||||
}
|
||||
if (rc == -1) {
|
||||
__proc_free(proc);
|
||||
dll_make_first(&__proc.free, &proc->elem);
|
||||
}
|
||||
ftrace_enabled(+1);
|
||||
strace_enabled(+1);
|
||||
|
|
|
@ -248,9 +248,9 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
struct SpawnFds fds = {0};
|
||||
int64_t dirhand = AT_FDCWD;
|
||||
int64_t *lpExplicitHandles = 0;
|
||||
sigset_t sigmask = __sig_block();
|
||||
uint32_t dwExplicitHandleCount = 0;
|
||||
int64_t hCreatorProcess = GetCurrentProcess();
|
||||
sigset_t m = __sig_block();
|
||||
|
||||
// reserve process tracking object
|
||||
__proc_lock();
|
||||
|
@ -266,11 +266,11 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
free(fdspec);
|
||||
if (proc) {
|
||||
__proc_lock();
|
||||
__proc_free(proc);
|
||||
dll_make_first(&__proc.free, &proc->elem);
|
||||
__proc_unlock();
|
||||
}
|
||||
spawnfds_destroy(&fds);
|
||||
__sig_unblock(m);
|
||||
__sig_unblock(sigmask);
|
||||
errno = e;
|
||||
return err;
|
||||
}
|
||||
|
@ -360,13 +360,17 @@ static textwindows errno_t posix_spawn_nt_impl(
|
|||
}
|
||||
}
|
||||
|
||||
// inherit signal mask
|
||||
char maskvar[6 + 21];
|
||||
FormatUint64(stpcpy(maskvar, "_MASK="), sigmask);
|
||||
|
||||
// launch process
|
||||
int rc = -1;
|
||||
struct NtProcessInformation procinfo;
|
||||
if (!envp) envp = environ;
|
||||
if ((fdspec = __describe_fds(fds.p, fds.n, &startinfo, hCreatorProcess,
|
||||
&lpExplicitHandles, &dwExplicitHandleCount))) {
|
||||
rc = ntspawn(dirhand, path, argv, envp, (char *[]){fdspec, 0},
|
||||
rc = ntspawn(dirhand, path, argv, envp, (char *[]){fdspec, maskvar, 0},
|
||||
dwCreationFlags, lpCurrentDirectory, 0, lpExplicitHandles,
|
||||
dwExplicitHandleCount, &startinfo, &procinfo);
|
||||
}
|
||||
|
|
|
@ -32,15 +32,20 @@
|
|||
* - `POSIX_SPAWN_SETSCHEDPARAM`
|
||||
* - `POSIX_SPAWN_SETSCHEDULER`
|
||||
* - `POSIX_SPAWN_SETSID`
|
||||
* - `POSIX_SPAWN_SETRLIMIT`
|
||||
* @return 0 on success, or errno on error
|
||||
* @raise EINVAL if `flags` has invalid bits
|
||||
*/
|
||||
int posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags) {
|
||||
if (flags &
|
||||
~(POSIX_SPAWN_USEVFORK | POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP |
|
||||
POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK |
|
||||
POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
|
||||
POSIX_SPAWN_SETSID | POSIX_SPAWN_SETRLIMIT)) {
|
||||
if (flags & ~(POSIX_SPAWN_USEVFORK | //
|
||||
POSIX_SPAWN_RESETIDS | //
|
||||
POSIX_SPAWN_SETPGROUP | //
|
||||
POSIX_SPAWN_SETSIGDEF | //
|
||||
POSIX_SPAWN_SETSIGMASK | //
|
||||
POSIX_SPAWN_SETSCHEDPARAM | //
|
||||
POSIX_SPAWN_SETSCHEDULER | //
|
||||
POSIX_SPAWN_SETSID | //
|
||||
POSIX_SPAWN_SETRLIMIT)) {
|
||||
return EINVAL;
|
||||
}
|
||||
(*attr)->flags = flags;
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/proc/posix_spawn.h"
|
||||
#include "libc/proc/posix_spawn.internal.h"
|
||||
|
||||
/**
|
||||
|
@ -25,14 +24,14 @@
|
|||
* Setting `pgroup` to zero will ensure newly created processes are
|
||||
* placed within their own brand new process group.
|
||||
*
|
||||
* This setter also sets the `POSIX_SPAWN_SETPGROUP` flag.
|
||||
* You also need to pass `POSIX_SPAWN_SETPGROUP` to
|
||||
* posix_spawnattr_setflags() for it to take effect.
|
||||
*
|
||||
* @param attr was initialized by posix_spawnattr_init()
|
||||
* @param pgroup is the process group id, or 0 for self
|
||||
* @return 0 on success, or errno on error
|
||||
*/
|
||||
int posix_spawnattr_setpgroup(posix_spawnattr_t *attr, int pgroup) {
|
||||
(*attr)->flags |= POSIX_SPAWN_SETPGROUP;
|
||||
(*attr)->pgroup = pgroup;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -26,7 +26,8 @@
|
|||
/**
|
||||
* Sets resource limit on spawned process.
|
||||
*
|
||||
* Using this setter automatically sets `POSIX_SPAWN_SETRLIMIT`.
|
||||
* You also need to pass `POSIX_SPAWN_SETRLIMIT` to
|
||||
* posix_spawnattr_setflags() for it to take effect.
|
||||
*
|
||||
* @return 0 on success, or errno on error
|
||||
* @raise EINVAL if resource is invalid
|
||||
|
@ -34,7 +35,6 @@
|
|||
int posix_spawnattr_setrlimit(posix_spawnattr_t *attr, int resource,
|
||||
const struct rlimit *rlim) {
|
||||
if (0 <= resource && resource < MIN(RLIM_NLIMITS, ARRAYLEN((*attr)->rlim))) {
|
||||
(*attr)->flags |= POSIX_SPAWN_SETRLIMIT;
|
||||
(*attr)->rlimset |= 1u << resource;
|
||||
(*attr)->rlim[resource] = *rlim;
|
||||
return 0;
|
||||
|
|
|
@ -16,14 +16,13 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/sched_param.h"
|
||||
#include "libc/proc/posix_spawn.h"
|
||||
#include "libc/proc/posix_spawn.internal.h"
|
||||
|
||||
/**
|
||||
* Specifies scheduler parameter override for spawned process.
|
||||
*
|
||||
* Using this setter automatically sets `POSIX_SPAWN_SETSCHEDPARAM`.
|
||||
* You also need to pass `POSIX_SPAWN_SETSCHEDPARAM` to
|
||||
* posix_spawnattr_setflags() for it to take effect.
|
||||
*
|
||||
* @param attr was initialized by posix_spawnattr_init()
|
||||
* @param schedparam receives the result
|
||||
|
@ -31,7 +30,6 @@
|
|||
*/
|
||||
int posix_spawnattr_setschedparam(posix_spawnattr_t *attr,
|
||||
const struct sched_param *schedparam) {
|
||||
(*attr)->flags |= POSIX_SPAWN_SETSCHEDPARAM;
|
||||
(*attr)->schedparam = *schedparam;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,19 +16,18 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/proc/posix_spawn.h"
|
||||
#include "libc/proc/posix_spawn.internal.h"
|
||||
|
||||
/**
|
||||
* Specifies scheduler policy override for spawned process.
|
||||
*
|
||||
* Using this setter automatically sets `POSIX_SPAWN_SETSCHEDULER`.
|
||||
* You also need to pass `POSIX_SPAWN_SETSCHEDULER` to
|
||||
* posix_spawnattr_setflags() for it to take effect.
|
||||
*
|
||||
* @param attr was initialized by posix_spawnattr_init()
|
||||
* @return 0 on success, or errno on error
|
||||
*/
|
||||
int posix_spawnattr_setschedpolicy(posix_spawnattr_t *attr, int schedpolicy) {
|
||||
(*attr)->flags |= POSIX_SPAWN_SETSCHEDULER;
|
||||
(*attr)->schedpolicy = schedpolicy;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,8 +16,6 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/proc/posix_spawn.h"
|
||||
#include "libc/proc/posix_spawn.internal.h"
|
||||
|
||||
/**
|
||||
|
@ -32,13 +30,13 @@
|
|||
* set the signals to `SIG_IGN` earlier (since posix_spawn() will not
|
||||
* issue O(128) system calls just to be totally pedantic about that).
|
||||
*
|
||||
* Using this setter automatically sets `POSIX_SPAWN_SETSIGDEF`.
|
||||
* You also need to pass `POSIX_SPAWN_SETSIGDEF` to
|
||||
* posix_spawnattr_setflags() for it to take effect.
|
||||
*
|
||||
* @return 0 on success, or errno on error
|
||||
*/
|
||||
int posix_spawnattr_setsigdefault(posix_spawnattr_t *attr,
|
||||
const sigset_t *sigdefault) {
|
||||
(*attr)->flags |= POSIX_SPAWN_SETSIGDEF;
|
||||
(*attr)->sigdefault = *sigdefault;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,20 +16,18 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/proc/posix_spawn.h"
|
||||
#include "libc/proc/posix_spawn.internal.h"
|
||||
|
||||
/**
|
||||
* Specifies signal mask for sigprocmask() in child process.
|
||||
*
|
||||
* This setter also sets the `POSIX_SPAWN_SETSIGMASK` flag.
|
||||
* You also need to pass `POSIX_SPAWN_SETSIGMASK` to
|
||||
* posix_spawnattr_setflags() for it to take effect.
|
||||
*
|
||||
* @return 0 on success, or errno on error
|
||||
*/
|
||||
int posix_spawnattr_setsigmask(posix_spawnattr_t *attr,
|
||||
const sigset_t *sigmask) {
|
||||
(*attr)->flags |= POSIX_SPAWN_SETSIGMASK;
|
||||
(*attr)->sigmask = *sigmask;
|
||||
return 0;
|
||||
}
|
||||
|
|
204
libc/proc/proc.c
204
libc/proc/proc.c
|
@ -16,7 +16,6 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
|
@ -37,6 +36,7 @@
|
|||
#include "libc/nt/enum/processcreationflags.h"
|
||||
#include "libc/nt/enum/status.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/events.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
|
@ -59,95 +59,167 @@
|
|||
|
||||
struct Procs __proc;
|
||||
|
||||
static textwindows void GetProcessStats(int64_t h, struct rusage *ru) {
|
||||
static textwindows void __proc_stats(int64_t h, struct rusage *ru) {
|
||||
bzero(ru, sizeof(*ru));
|
||||
struct NtProcessMemoryCountersEx memcount = {sizeof(memcount)};
|
||||
unassert(GetProcessMemoryInfo(h, &memcount, sizeof(memcount)));
|
||||
GetProcessMemoryInfo(h, &memcount, sizeof(memcount));
|
||||
ru->ru_maxrss = memcount.PeakWorkingSetSize / 1024;
|
||||
ru->ru_majflt = memcount.PageFaultCount;
|
||||
struct NtFileTime createtime, exittime;
|
||||
struct NtFileTime kerneltime, usertime;
|
||||
unassert(GetProcessTimes(h, &createtime, &exittime, &kerneltime, &usertime));
|
||||
GetProcessTimes(h, &createtime, &exittime, &kerneltime, &usertime);
|
||||
ru->ru_utime = WindowsDurationToTimeVal(ReadFileTime(usertime));
|
||||
ru->ru_stime = WindowsDurationToTimeVal(ReadFileTime(kerneltime));
|
||||
struct NtIoCounters iocount;
|
||||
unassert(GetProcessIoCounters(h, &iocount));
|
||||
GetProcessIoCounters(h, &iocount);
|
||||
ru->ru_inblock = iocount.ReadOperationCount;
|
||||
ru->ru_oublock = iocount.WriteOperationCount;
|
||||
}
|
||||
|
||||
// performs accounting on exited process
|
||||
// multiple threads can wait on a process
|
||||
// it's important that only one calls this
|
||||
textwindows int __proc_harvest(struct Proc *pr, bool iswait4) {
|
||||
int sic = 0;
|
||||
uint32_t status;
|
||||
struct rusage ru;
|
||||
GetExitCodeProcess(pr->handle, &status);
|
||||
if (status == kNtStillActive) return 0;
|
||||
__proc_stats(pr->handle, &ru);
|
||||
rusage_add(&pr->ru, &ru);
|
||||
rusage_add(&__proc.ruchlds, &ru);
|
||||
if ((status & 0xFF000000u) == 0x23000000u) {
|
||||
// handle child execve()
|
||||
CloseHandle(pr->handle);
|
||||
pr->handle = status & 0x00FFFFFF;
|
||||
} else {
|
||||
// handle child _Exit()
|
||||
if (status == 0xc9af3d51u) {
|
||||
status = kNtStillActive;
|
||||
}
|
||||
pr->wstatus = status;
|
||||
if (!iswait4 && !pr->waiters && !__proc.waiters &&
|
||||
(__sighandrvas[SIGCHLD] == (uintptr_t)SIG_IGN ||
|
||||
(__sighandflags[SIGCHLD] & SA_NOCLDWAIT))) {
|
||||
// perform automatic zombie reaping
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
CloseHandle(pr->handle);
|
||||
} else {
|
||||
// transitions process to zombie state
|
||||
// wait4 is responsible for reaping it
|
||||
pr->status = PROC_ZOMBIE;
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.zombies, &pr->elem);
|
||||
SetEvent(__proc.haszombies);
|
||||
if (!pr->waiters && !__proc.waiters) {
|
||||
if (WIFSIGNALED(status)) {
|
||||
sic = CLD_KILLED;
|
||||
} else {
|
||||
sic = CLD_EXITED;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return sic;
|
||||
}
|
||||
|
||||
static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
||||
__bootstrap_tls(&__proc.tls, __builtin_frame_address(0));
|
||||
struct CosmoTib tls;
|
||||
__bootstrap_tls(&tls, __builtin_frame_address(0));
|
||||
for (;;) {
|
||||
|
||||
// assemble a group of processes to wait on. if more than 64
|
||||
// children exist, then we'll use a small timeout and select
|
||||
// processes with a shifting window via a double linked list
|
||||
struct rusage ru;
|
||||
// if fewer than 64 processes exist then we'll also wait for
|
||||
// process birth notifications, and wait on them immediately
|
||||
int64_t handles[64];
|
||||
int sic, dosignal = 0;
|
||||
struct Proc *pr, *objects[64];
|
||||
struct Proc *objects[64];
|
||||
uint32_t millis, i, n = 0;
|
||||
struct Dll *e, *e2, *samples = 0;
|
||||
uint32_t millis, status, i, n = 1;
|
||||
__proc_lock();
|
||||
handles[0] = __proc.onstart;
|
||||
for (e = dll_first(__proc.list); e && n < 64; ++n, e = e2) {
|
||||
pr = PROC_CONTAINER(e);
|
||||
for (e = dll_first(__proc.list); e && n < 64; e = e2) {
|
||||
struct Proc *pr = PROC_CONTAINER(e);
|
||||
e2 = dll_next(__proc.list, e);
|
||||
// cycle process to end of list
|
||||
dll_remove(&__proc.list, e);
|
||||
dll_make_last(&samples, e);
|
||||
handles[n] = pr->handle;
|
||||
objects[n] = pr;
|
||||
// don't bother waiting if it's already awaited
|
||||
if (!pr->waiters) {
|
||||
handles[n] = pr->handle;
|
||||
objects[n] = pr;
|
||||
++pr->waiters;
|
||||
++n;
|
||||
}
|
||||
}
|
||||
dll_make_last(&__proc.list, samples);
|
||||
__proc_unlock();
|
||||
|
||||
// wait for win32 to report any status change
|
||||
millis = n == 64 ? 20 : -1u;
|
||||
unassert((i = WaitForMultipleObjects(n, handles, false, millis)) != -1u);
|
||||
i &= ~kNtWaitAbandoned;
|
||||
if (!i || i == kNtWaitTimeout) continue;
|
||||
GetExitCodeProcess(handles[i], &status);
|
||||
if (status == kNtStillActive) continue;
|
||||
GetProcessStats(handles[i], &ru);
|
||||
|
||||
// update data structures and notify folks
|
||||
__proc_lock();
|
||||
pr = objects[i];
|
||||
rusage_add(&pr->ru, &ru);
|
||||
rusage_add(&__proc.ruchlds, &ru);
|
||||
if ((status & 0xFF000000u) == 0x23000000u) {
|
||||
// handle child execve()
|
||||
CloseHandle(pr->handle);
|
||||
pr->handle = status & 0x00FFFFFF;
|
||||
// wait for something to happen
|
||||
if (n == 64) {
|
||||
millis = 5;
|
||||
} else {
|
||||
// handle child _exit()
|
||||
CloseHandle(pr->handle);
|
||||
if (status == 0xc9af3d51u) {
|
||||
status = kNtStillActive;
|
||||
}
|
||||
pr->wstatus = status;
|
||||
if ((__sighandrvas[SIGCHLD] == (uintptr_t)SIG_IGN ||
|
||||
(__sighandflags[SIGCHLD] & SA_NOCLDWAIT)) &&
|
||||
(!pr->waiters && !__proc.waiters)) {
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
} else {
|
||||
pr->iszombie = 1;
|
||||
dll_remove(&__proc.list, &pr->elem);
|
||||
dll_make_first(&__proc.zombies, &pr->elem);
|
||||
if (pr->waiters) {
|
||||
nsync_cv_broadcast(&pr->onexit);
|
||||
} else if (__proc.waiters) {
|
||||
nsync_cv_signal(&__proc.onexit);
|
||||
} else {
|
||||
dosignal = 1;
|
||||
sic = WIFSIGNALED(status) ? CLD_KILLED : CLD_EXITED;
|
||||
}
|
||||
millis = -1u;
|
||||
handles[n++] = __proc.onbirth;
|
||||
}
|
||||
i = WaitForMultipleObjects(n, handles, false, millis);
|
||||
if (i == -1u) {
|
||||
STRACE("proc wait panic %d", GetLastError());
|
||||
_Exit(157);
|
||||
}
|
||||
if (i & kNtWaitAbandoned) {
|
||||
i &= ~kNtWaitAbandoned;
|
||||
STRACE("proc %u handle %ld abandoned", i, handles[i]);
|
||||
}
|
||||
__proc_lock();
|
||||
|
||||
// release our waiter status
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (handles[j] == __proc.onbirth) continue;
|
||||
if (j == i) continue;
|
||||
if (!--objects[j]->waiters && objects[j]->status == PROC_UNDEAD) {
|
||||
__proc_free(objects[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// check if we need to churn due to >64 processes
|
||||
if (i == kNtWaitTimeout) {
|
||||
__proc_unlock();
|
||||
continue;
|
||||
}
|
||||
|
||||
// churn on new process birth
|
||||
if (handles[i] == __proc.onbirth) {
|
||||
__proc_unlock();
|
||||
continue;
|
||||
}
|
||||
|
||||
// handle process status change
|
||||
int sic = 0;
|
||||
--objects[i]->waiters;
|
||||
switch (objects[i]->status) {
|
||||
case PROC_ALIVE:
|
||||
sic = __proc_harvest(objects[i], false);
|
||||
break;
|
||||
case PROC_ZOMBIE:
|
||||
break;
|
||||
case PROC_UNDEAD:
|
||||
if (!objects[i]->waiters) {
|
||||
__proc_free(objects[i]);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
__proc_unlock();
|
||||
if (dosignal) {
|
||||
|
||||
// don't raise SIGCHLD if
|
||||
// 1. wait4() is being used
|
||||
// 2. SIGCHLD has SIG_IGN handler
|
||||
// 3. SIGCHLD has SA_NOCLDWAIT flag
|
||||
if (sic) {
|
||||
__sig_generate(SIGCHLD, sic);
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +230,8 @@ static textwindows dontinstrument uint32_t __proc_worker(void *arg) {
|
|||
* Lazy initializes process tracker data structures and worker.
|
||||
*/
|
||||
static textwindows void __proc_setup(void) {
|
||||
__proc.onstart = CreateSemaphore(0, 0, 1, 0);
|
||||
__proc.onbirth = CreateEvent(0, 0, 0, 0); // auto reset
|
||||
__proc.haszombies = CreateEvent(0, 1, 0, 0); // manual reset
|
||||
__proc.thread = CreateThread(0, 65536, __proc_worker, 0,
|
||||
kNtStackSizeParamIsAReservation, 0);
|
||||
}
|
||||
|
@ -191,7 +264,7 @@ textwindows void __proc_wipe(void) {
|
|||
* The returned memory is not tracked by any list. It must be filled in
|
||||
* with system process information and then added back to the system by
|
||||
* calling __proc_add(). If process creation fails, then it needs to be
|
||||
* released using __proc_free().
|
||||
* added back to the __proc.free list by caller.
|
||||
*/
|
||||
textwindows struct Proc *__proc_new(void) {
|
||||
struct Dll *e;
|
||||
|
@ -230,16 +303,13 @@ IGNORE_LEAKS(__proc_new)
|
|||
*/
|
||||
textwindows void __proc_add(struct Proc *proc) {
|
||||
dll_make_first(&__proc.list, &proc->elem);
|
||||
ReleaseSemaphore(__proc.onstart, 1, 0);
|
||||
SetEvent(__proc.onbirth);
|
||||
}
|
||||
|
||||
/**
|
||||
* Frees process allocation.
|
||||
*
|
||||
* Process must not be currently tracked in the active or zombies list.
|
||||
*/
|
||||
textwindows void __proc_free(struct Proc *proc) {
|
||||
dll_make_first(&__proc.free, &proc->elem);
|
||||
textwindows void __proc_free(struct Proc *pr) {
|
||||
dll_remove(&__proc.undead, &pr->elem);
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
CloseHandle(pr->handle);
|
||||
}
|
||||
|
||||
// returns owned handle of direct child process
|
||||
|
|
|
@ -2,25 +2,27 @@
|
|||
#define COSMOPOLITAN_LIBC_PROC_H_
|
||||
#include "libc/atomic.h"
|
||||
#include "libc/calls/struct/rusage.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/dll.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#include "third_party/nsync/cv.h"
|
||||
#include "third_party/nsync/mu.h"
|
||||
#if !(__ASSEMBLER__ + __LINKER__ + 0)
|
||||
COSMOPOLITAN_C_START_
|
||||
|
||||
#define PROC_ALIVE 0
|
||||
#define PROC_ZOMBIE 1
|
||||
#define PROC_UNDEAD 2
|
||||
|
||||
#define PROC_CONTAINER(e) DLL_CONTAINER(struct Proc, elem, e)
|
||||
|
||||
struct Proc {
|
||||
int pid;
|
||||
int status;
|
||||
int waiters;
|
||||
bool iszombie;
|
||||
bool wasforked;
|
||||
uint32_t wstatus;
|
||||
int64_t handle;
|
||||
struct Dll elem;
|
||||
nsync_cv onexit;
|
||||
struct rusage ru;
|
||||
};
|
||||
|
||||
|
@ -28,15 +30,15 @@ struct Procs {
|
|||
int waiters;
|
||||
atomic_uint once;
|
||||
nsync_mu lock;
|
||||
nsync_cv onexit;
|
||||
intptr_t thread;
|
||||
intptr_t onstart;
|
||||
intptr_t onbirth;
|
||||
intptr_t haszombies;
|
||||
struct Dll *list;
|
||||
struct Dll *free;
|
||||
struct Dll *undead;
|
||||
struct Dll *zombies;
|
||||
struct Proc pool[8];
|
||||
unsigned allocated;
|
||||
struct CosmoTib tls;
|
||||
struct rusage ruchlds;
|
||||
};
|
||||
|
||||
|
@ -50,6 +52,7 @@ int64_t __proc_search(int);
|
|||
struct Proc *__proc_new(void);
|
||||
void __proc_add(struct Proc *);
|
||||
void __proc_free(struct Proc *);
|
||||
int __proc_harvest(struct Proc *, bool);
|
||||
int sys_wait4_nt(int, int *, int, struct rusage *);
|
||||
|
||||
COSMOPOLITAN_C_END_
|
||||
|
|
|
@ -35,8 +35,7 @@ LIBC_PROC_A_DIRECTDEPS = \
|
|||
LIBC_STR \
|
||||
LIBC_SYSV \
|
||||
LIBC_SYSV_CALLS \
|
||||
THIRD_PARTY_NSYNC \
|
||||
THIRD_PARTY_NSYNC_MEM
|
||||
THIRD_PARTY_NSYNC
|
||||
|
||||
LIBC_PROC_A_DEPS := \
|
||||
$(call uniq,$(foreach x,$(LIBC_PROC_A_DIRECTDEPS),$($(x))))
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/struct/sched_param.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/sysv/consts/ioprio.h"
|
||||
#include "libc/sysv/consts/prio.h"
|
||||
#include "libc/sysv/consts/sched.h"
|
||||
|
@ -31,7 +32,7 @@
|
|||
*/
|
||||
int verynice(void) {
|
||||
int e = errno;
|
||||
setpriority(PRIO_PROCESS, 0, 10);
|
||||
setpriority(PRIO_PROCESS, 0, NZERO);
|
||||
sys_ioprio_set(IOPRIO_WHO_PROCESS, 0,
|
||||
IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0));
|
||||
struct sched_param param = {sched_get_priority_min(SCHED_IDLE)};
|
||||
|
|
|
@ -16,139 +16,191 @@
|
|||
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
||||
│ PERFORMANCE OF THIS SOFTWARE. │
|
||||
╚─────────────────────────────────────────────────────────────────────────────*/
|
||||
#include "libc/assert.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/calls/internal.h"
|
||||
#include "libc/calls/sig.internal.h"
|
||||
#include "libc/calls/struct/rusage.h"
|
||||
#include "libc/calls/struct/sigset.h"
|
||||
#include "libc/calls/struct/sigset.internal.h"
|
||||
#include "libc/calls/struct/timespec.h"
|
||||
#include "libc/cosmo.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/wintime.internal.h"
|
||||
#include "libc/intrin/atomic.h"
|
||||
#include "libc/intrin/dll.h"
|
||||
#include "libc/intrin/strace.internal.h"
|
||||
#include "libc/macros.internal.h"
|
||||
#include "libc/nt/accounting.h"
|
||||
#include "libc/nt/process.h"
|
||||
#include "libc/nt/enum/wait.h"
|
||||
#include "libc/nt/events.h"
|
||||
#include "libc/nt/runtime.h"
|
||||
#include "libc/nt/struct/filetime.h"
|
||||
#include "libc/nt/struct/processmemorycounters.h"
|
||||
#include "libc/nt/synchronization.h"
|
||||
#include "libc/proc/proc.internal.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/sicode.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
#include "libc/sysv/consts/w.h"
|
||||
#include "libc/sysv/errfuns.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/thread/tls.h"
|
||||
#ifdef __x86_64__
|
||||
|
||||
static textwindows struct timespec GetNextDeadline(struct timespec deadline) {
|
||||
if (timespec_iszero(deadline)) deadline = timespec_real();
|
||||
struct timespec delay = timespec_frommillis(__SIG_PROC_INTERVAL_MS);
|
||||
return timespec_add(deadline, delay);
|
||||
}
|
||||
|
||||
static textwindows int ReapZombie(struct Proc *pr, int *wstatus,
|
||||
struct rusage *opt_out_rusage) {
|
||||
static textwindows int __proc_reap(struct Proc *pr, int *wstatus,
|
||||
struct rusage *opt_out_rusage) {
|
||||
if (wstatus) {
|
||||
*wstatus = pr->wstatus;
|
||||
}
|
||||
if (opt_out_rusage) {
|
||||
*opt_out_rusage = pr->ru;
|
||||
}
|
||||
if (!pr->waiters) {
|
||||
dll_remove(&__proc.zombies, &pr->elem);
|
||||
dll_remove(&__proc.zombies, &pr->elem);
|
||||
if (dll_is_empty(__proc.zombies)) {
|
||||
ResetEvent(__proc.haszombies);
|
||||
}
|
||||
if (pr->waiters) {
|
||||
pr->status = PROC_UNDEAD;
|
||||
dll_make_first(&__proc.undead, &pr->elem);
|
||||
} else {
|
||||
dll_make_first(&__proc.free, &pr->elem);
|
||||
CloseHandle(pr->handle);
|
||||
}
|
||||
return pr->pid;
|
||||
}
|
||||
|
||||
static textwindows int CheckZombies(int pid, int *wstatus,
|
||||
static textwindows int __proc_check(int pid, int *wstatus,
|
||||
struct rusage *opt_out_rusage) {
|
||||
struct Dll *e;
|
||||
for (e = dll_first(__proc.zombies); e; e = dll_next(__proc.zombies, e)) {
|
||||
struct Proc *pr = PROC_CONTAINER(e);
|
||||
if (pid == -1 && pr->waiters) {
|
||||
continue; // this zombie has been claimed
|
||||
}
|
||||
if (pid == -1 || pid == pr->pid) {
|
||||
return ReapZombie(pr, wstatus, opt_out_rusage);
|
||||
return __proc_reap(pr, wstatus, opt_out_rusage);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static textwindows void UnwindWaiterCount(void *arg) {
|
||||
int *waiters = arg;
|
||||
--*waiters;
|
||||
}
|
||||
static textwindows int __proc_wait(int pid, int *wstatus, int options,
|
||||
struct rusage *rusage, sigset_t waitmask) {
|
||||
for (;;) {
|
||||
|
||||
static textwindows int WaitForProcess(int pid, int *wstatus, int options,
|
||||
struct rusage *rusage,
|
||||
uint64_t waitmask) {
|
||||
uint64_t m;
|
||||
int rc, *wv;
|
||||
nsync_cv *cv;
|
||||
struct Dll *e;
|
||||
struct Proc *pr;
|
||||
struct timespec deadline = timespec_zero;
|
||||
|
||||
// check list of processes that've already exited
|
||||
if ((rc = CheckZombies(pid, wstatus, rusage))) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
// find the mark
|
||||
pr = 0;
|
||||
if (pid == -1) {
|
||||
if (dll_is_empty(__proc.list)) {
|
||||
return echild();
|
||||
// check for signals and cancelation
|
||||
int sig, handler_was_called;
|
||||
if (_check_cancel() == -1) {
|
||||
return -1;
|
||||
}
|
||||
cv = &__proc.onexit;
|
||||
wv = &__proc.waiters;
|
||||
} else {
|
||||
for (e = dll_first(__proc.list); e; e = dll_next(__proc.list, e)) {
|
||||
if (pid == PROC_CONTAINER(e)->pid) {
|
||||
pr = PROC_CONTAINER(e);
|
||||
if ((sig = __sig_get(waitmask))) {
|
||||
handler_was_called = __sig_relay(sig, SI_KERNEL, waitmask);
|
||||
if (_check_cancel() == -1) {
|
||||
return -1; // ECANCELED because SIGTHR was just handled
|
||||
}
|
||||
if (handler_was_called & SIG_HANDLED_NO_RESTART) {
|
||||
return eintr(); // a non-SA_RESTART handler was called
|
||||
}
|
||||
}
|
||||
if (pr) {
|
||||
unassert(!pr->iszombie);
|
||||
cv = &pr->onexit;
|
||||
wv = &pr->waiters;
|
||||
} else {
|
||||
|
||||
// check for zombie to harvest
|
||||
__proc_lock();
|
||||
CheckForZombies:
|
||||
int rc = __proc_check(pid, wstatus, rusage);
|
||||
if (rc || (options & WNOHANG)) {
|
||||
__proc_unlock();
|
||||
return rc;
|
||||
}
|
||||
|
||||
// there's no zombies left
|
||||
// check if there's any living processes
|
||||
if (dll_is_empty(__proc.list)) {
|
||||
__proc_unlock();
|
||||
return echild();
|
||||
}
|
||||
}
|
||||
|
||||
// wait for status change
|
||||
if (options & WNOHANG) return 0;
|
||||
WaitMore:
|
||||
deadline = GetNextDeadline(deadline);
|
||||
SpuriousWakeup:
|
||||
++*wv;
|
||||
pthread_cleanup_push(UnwindWaiterCount, wv);
|
||||
m = __sig_begin(waitmask);
|
||||
if ((rc = _check_signal(true)) != -1) {
|
||||
rc = nsync_cv_wait_with_deadline(cv, &__proc.lock, deadline, 0);
|
||||
// get appropriate wait object
|
||||
// register ourself as waiting
|
||||
struct Proc *pr = 0;
|
||||
uintptr_t hWaitObject;
|
||||
if (pid == -1) {
|
||||
// wait for any status change
|
||||
hWaitObject = __proc.haszombies;
|
||||
++__proc.waiters;
|
||||
} else {
|
||||
// wait on specific child
|
||||
for (struct Dll *e = dll_first(__proc.list); e;
|
||||
e = dll_next(__proc.list, e)) {
|
||||
pr = PROC_CONTAINER(e);
|
||||
if (pid == pr->pid) break;
|
||||
}
|
||||
if (pr) {
|
||||
// by making the waiter count non-zero, the proc daemon stops
|
||||
// being obligated to monitor this process. this means we may
|
||||
// need to assume responsibility later on for zombifying this
|
||||
++pr->waiters;
|
||||
hWaitObject = pr->handle;
|
||||
} else {
|
||||
__proc_unlock();
|
||||
return echild();
|
||||
}
|
||||
}
|
||||
__proc_unlock();
|
||||
|
||||
// perform blocking operation
|
||||
uint32_t wi;
|
||||
uintptr_t sem;
|
||||
struct PosixThread *pt = _pthread_self();
|
||||
pt->pt_blkmask = waitmask;
|
||||
pt->pt_semaphore = sem = CreateSemaphore(0, 0, 1, 0);
|
||||
atomic_store_explicit(&pt->pt_blocker, PT_BLOCKER_SEM,
|
||||
memory_order_release);
|
||||
wi = WaitForMultipleObjects(2, (intptr_t[2]){hWaitObject, sem}, 0, -1u);
|
||||
atomic_store_explicit(&pt->pt_blocker, 0, memory_order_release);
|
||||
CloseHandle(sem);
|
||||
|
||||
// log warning if handle unexpectedly closed
|
||||
if (wi & kNtWaitAbandoned) {
|
||||
wi &= ~kNtWaitAbandoned;
|
||||
STRACE("wait4 abandoned %u", wi);
|
||||
}
|
||||
|
||||
// check for wait() style wakeup
|
||||
__proc_lock();
|
||||
if (!wi && !pr) {
|
||||
--__proc.waiters;
|
||||
goto CheckForZombies;
|
||||
}
|
||||
|
||||
// check if killed or win32 error
|
||||
if (wi) {
|
||||
if (pr && --pr->waiters && pr->status == PROC_UNDEAD) {
|
||||
__proc_free(pr);
|
||||
}
|
||||
__proc_unlock();
|
||||
if (wi == 1) {
|
||||
// __sig_cancel() woke our semaphore
|
||||
continue;
|
||||
} else {
|
||||
// neither posix or win32 define i/o error conditions for
|
||||
// generic wait. failure should only be due to api misuse
|
||||
return einval();
|
||||
}
|
||||
}
|
||||
|
||||
// handle process exit notification
|
||||
--pr->waiters;
|
||||
if (pr->status == PROC_ALIVE) {
|
||||
__proc_harvest(pr, true);
|
||||
}
|
||||
switch (pr->status) {
|
||||
case PROC_ALIVE:
|
||||
// exit caused by execve() reparenting
|
||||
__proc_unlock();
|
||||
break;
|
||||
case PROC_ZOMBIE:
|
||||
// exit happened and we're the first to know
|
||||
rc = __proc_reap(pr, wstatus, rusage);
|
||||
__proc_unlock();
|
||||
return rc;
|
||||
case PROC_UNDEAD:
|
||||
// exit happened but another thread waited first
|
||||
if (!pr->waiters) {
|
||||
__proc_free(pr);
|
||||
}
|
||||
__proc_unlock();
|
||||
return echild();
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
}
|
||||
__sig_finish(m);
|
||||
pthread_cleanup_pop(true);
|
||||
if (rc == -1) return -1;
|
||||
if (rc == ETIMEDOUT) goto WaitMore;
|
||||
if (rc == ECANCELED) return ecanceled();
|
||||
if (pr && pr->iszombie) return ReapZombie(pr, wstatus, rusage);
|
||||
unassert(!rc); // i have to follow my dreams however crazy they seem
|
||||
if (!pr && (rc = CheckZombies(pid, wstatus, rusage))) return rc;
|
||||
goto SpuriousWakeup;
|
||||
}
|
||||
|
||||
textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
||||
struct rusage *opt_out_rusage) {
|
||||
int rc;
|
||||
uint64_t m;
|
||||
// no support for WCONTINUED and WUNTRACED yet
|
||||
if (options & ~WNOHANG) return einval();
|
||||
// XXX: NT doesn't really have process groups. For instance the
|
||||
|
@ -156,12 +208,9 @@ textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
|||
// just does an "ignore ctrl-c" internally.
|
||||
if (pid == 0) pid = -1;
|
||||
if (pid < -1) pid = -pid;
|
||||
m = __sig_block();
|
||||
__proc_lock();
|
||||
pthread_cleanup_push((void *)__proc_unlock, 0);
|
||||
rc = WaitForProcess(pid, opt_out_wstatus, options, opt_out_rusage,
|
||||
m | 1ull << (SIGCHLD - 1));
|
||||
pthread_cleanup_pop(true);
|
||||
sigset_t m = __sig_block();
|
||||
int rc = __proc_wait(pid, opt_out_wstatus, options, opt_out_rusage,
|
||||
m | 1ull << (SIGCHLD - 1));
|
||||
__sig_unblock(m);
|
||||
return rc;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue