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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-08-26 04:35:58 +00:00
|
|
|
#include "libc/assert.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/calls.h"
|
|
|
|
#include "libc/calls/internal.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#include "libc/calls/sig.internal.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/state.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/rusage.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
#include "libc/fmt/conv.h"
|
2022-10-03 05:14:33 +00:00
|
|
|
#include "libc/intrin/strace.internal.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/accounting.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/enum/accessmask.h"
|
|
|
|
#include "libc/nt/enum/processaccess.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/enum/status.h"
|
2023-07-30 15:55:01 +00:00
|
|
|
#include "libc/nt/enum/th32cs.h"
|
2020-11-13 09:27:49 +00:00
|
|
|
#include "libc/nt/enum/wait.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/process.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/filetime.h"
|
2023-07-30 15:55:01 +00:00
|
|
|
#include "libc/nt/struct/processentry32.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/struct/processmemorycounters.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/synchronization.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2022-08-13 20:11:56 +00:00
|
|
|
#include "libc/stdio/lcg.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2022-04-12 12:20:17 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/consts/w.h"
|
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2023-05-09 08:56:56 +00:00
|
|
|
#ifdef __x86_64__
|
|
|
|
|
2023-07-30 15:55:01 +00:00
|
|
|
static textwindows void AddProcessStats(int64_t h, struct rusage *ru) {
|
|
|
|
struct NtProcessMemoryCountersEx memcount = {
|
|
|
|
.cb = sizeof(struct NtProcessMemoryCountersEx)};
|
|
|
|
if (GetProcessMemoryInfo(h, &memcount, sizeof(memcount))) {
|
|
|
|
ru->ru_maxrss = MAX(ru->ru_maxrss, memcount.PeakWorkingSetSize / 1024);
|
|
|
|
ru->ru_majflt += memcount.PageFaultCount;
|
|
|
|
} else {
|
|
|
|
STRACE("%s failed %u", "GetProcessMemoryInfo", GetLastError());
|
|
|
|
}
|
|
|
|
struct NtFileTime createfiletime, exitfiletime;
|
|
|
|
struct NtFileTime kernelfiletime, userfiletime;
|
|
|
|
if (GetProcessTimes(h, &createfiletime, &exitfiletime, &kernelfiletime,
|
|
|
|
&userfiletime)) {
|
|
|
|
ru->ru_utime = timeval_add(
|
|
|
|
ru->ru_utime, WindowsDurationToTimeVal(ReadFileTime(userfiletime)));
|
|
|
|
ru->ru_stime = timeval_add(
|
|
|
|
ru->ru_stime, WindowsDurationToTimeVal(ReadFileTime(kernelfiletime)));
|
|
|
|
} else {
|
|
|
|
STRACE("%s failed %u", "GetProcessTimes", GetLastError());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static textwindows int sys_wait4_nt_impl(int *pid, int *opt_out_wstatus,
|
2022-04-12 12:20:17 +00:00
|
|
|
int options,
|
|
|
|
struct rusage *opt_out_rusage) {
|
2022-03-23 02:51:27 +00:00
|
|
|
int64_t handle;
|
2022-11-04 08:04:43 +00:00
|
|
|
int rc, pids[64];
|
2020-11-13 09:27:49 +00:00
|
|
|
int64_t handles[64];
|
2020-06-15 14:18:57 +00:00
|
|
|
uint32_t dwExitCode;
|
2023-07-30 15:55:01 +00:00
|
|
|
uint32_t i, j, count;
|
|
|
|
if (*pid != -1 && *pid != 0) {
|
|
|
|
if (*pid < 0) {
|
|
|
|
// XXX: this is sloppy
|
|
|
|
*pid = -*pid;
|
2022-03-23 04:04:08 +00:00
|
|
|
}
|
2023-07-30 15:55:01 +00:00
|
|
|
if (!__isfdkind(*pid, kFdProcess)) {
|
|
|
|
// XXX: this is sloppy (see fork-nt.c)
|
|
|
|
if (!__isfdopen(*pid) &&
|
2022-03-23 02:51:27 +00:00
|
|
|
(handle = OpenProcess(kNtSynchronize | kNtProcessQueryInformation,
|
2023-07-30 15:55:01 +00:00
|
|
|
true, *pid))) {
|
|
|
|
if ((*pid = __reservefd_unlocked(-1)) != -1) {
|
|
|
|
g_fds.p[*pid].kind = kFdProcess;
|
|
|
|
g_fds.p[*pid].handle = handle;
|
|
|
|
g_fds.p[*pid].flags = O_CLOEXEC;
|
2022-03-23 02:51:27 +00:00
|
|
|
} else {
|
|
|
|
CloseHandle(handle);
|
|
|
|
return echild();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return echild();
|
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2023-07-30 15:55:01 +00:00
|
|
|
handles[0] = g_fds.p[*pid].handle;
|
|
|
|
pids[0] = *pid;
|
2020-11-13 09:27:49 +00:00
|
|
|
count = 1;
|
|
|
|
} else {
|
2022-03-25 14:11:44 +00:00
|
|
|
count = __sample_pids(pids, handles, false);
|
2022-05-17 11:14:28 +00:00
|
|
|
if (!count) {
|
|
|
|
return echild();
|
|
|
|
}
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2023-07-30 15:55:01 +00:00
|
|
|
dwExitCode = kNtStillActive;
|
|
|
|
if (options & WNOHANG) {
|
|
|
|
i = WaitForMultipleObjects(count, handles, false, 0);
|
|
|
|
if (i == kNtWaitTimeout) {
|
|
|
|
return 0;
|
2020-11-13 09:27:49 +00:00
|
|
|
}
|
2023-07-30 15:55:01 +00:00
|
|
|
} else {
|
|
|
|
i = WaitForMultipleObjects(count, handles, false,
|
|
|
|
__SIG_POLLING_INTERVAL_MS);
|
|
|
|
if (i == kNtWaitTimeout) {
|
|
|
|
return -2;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2023-07-30 15:55:01 +00:00
|
|
|
if (i == kNtWaitFailed) {
|
|
|
|
STRACE("%s failed %u", "WaitForMultipleObjects", GetLastError());
|
|
|
|
return __winerr();
|
|
|
|
}
|
|
|
|
if (!GetExitCodeProcess(handles[i], &dwExitCode)) {
|
|
|
|
STRACE("%s failed %u", "GetExitCodeProcess", GetLastError());
|
|
|
|
return __winerr();
|
|
|
|
}
|
|
|
|
if (dwExitCode == kNtStillActive) {
|
|
|
|
return -2;
|
|
|
|
}
|
2023-07-30 21:26:21 +00:00
|
|
|
if (dwExitCode == 0xc9af3d51u) {
|
|
|
|
dwExitCode = kNtStillActive;
|
|
|
|
}
|
|
|
|
if (opt_out_wstatus) {
|
|
|
|
*opt_out_wstatus = dwExitCode;
|
2023-07-30 15:55:01 +00:00
|
|
|
}
|
|
|
|
if (opt_out_rusage) {
|
|
|
|
bzero(opt_out_rusage, sizeof(*opt_out_rusage));
|
|
|
|
AddProcessStats(handles[i], opt_out_rusage);
|
|
|
|
}
|
|
|
|
CloseHandle(handles[i]);
|
|
|
|
__releasefd(pids[i]);
|
|
|
|
return pids[i];
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-04-12 12:20:17 +00:00
|
|
|
|
|
|
|
textwindows int sys_wait4_nt(int pid, int *opt_out_wstatus, int options,
|
|
|
|
struct rusage *opt_out_rusage) {
|
|
|
|
int rc;
|
2022-10-03 05:14:33 +00:00
|
|
|
sigset_t oldmask, mask = {0};
|
2022-04-12 12:20:17 +00:00
|
|
|
sigaddset(&mask, SIGCHLD);
|
|
|
|
__sig_mask(SIG_BLOCK, &mask, &oldmask);
|
2023-07-30 15:55:01 +00:00
|
|
|
do {
|
2023-08-19 13:41:06 +00:00
|
|
|
rc = _check_interrupts(kSigOpRestartable | kSigOpNochld);
|
2023-07-30 15:55:01 +00:00
|
|
|
if (rc == -1) break;
|
|
|
|
__fds_lock();
|
|
|
|
rc = sys_wait4_nt_impl(&pid, opt_out_wstatus, options, opt_out_rusage);
|
|
|
|
__fds_unlock();
|
|
|
|
} while (rc == -2);
|
2022-04-12 12:20:17 +00:00
|
|
|
__sig_mask(SIG_SETMASK, &oldmask, 0);
|
|
|
|
return rc;
|
|
|
|
}
|
2023-05-09 08:56:56 +00:00
|
|
|
|
|
|
|
#endif /* __x86_64__ */
|