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│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
2021-01-28 03:34:02 +00:00
|
|
|
│ Copyright 2021 Justine Alexandra Roberts Tunney │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-03-25 14:11:44 +00:00
|
|
|
#include "libc/calls/calls.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/dce.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/nt/console.h"
|
|
|
|
#include "libc/nt/enum/ctrlevent.h"
|
2021-08-16 22:26:31 +00:00
|
|
|
#include "libc/nt/enum/processaccess.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/nt/enum/th32cs.h"
|
2022-03-23 02:51:27 +00:00
|
|
|
#include "libc/nt/errors.h"
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/nt/process.h"
|
2021-08-16 22:26:31 +00:00
|
|
|
#include "libc/nt/runtime.h"
|
2022-05-19 23:57:49 +00:00
|
|
|
#include "libc/nt/struct/processentry32.h"
|
2023-03-29 05:28:09 +00:00
|
|
|
#include "libc/sysv/consts/sig.h"
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
|
2023-03-29 05:28:09 +00:00
|
|
|
static inline int GetConsoleCtrlEvent(int sig) {
|
|
|
|
switch (sig) {
|
|
|
|
case SIGINT:
|
|
|
|
return kNtCtrlCEvent;
|
|
|
|
case SIGQUIT:
|
|
|
|
return kNtCtrlBreakEvent;
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
textwindows int sys_kill_nt(int pid, int sig) {
|
2022-04-12 12:20:17 +00:00
|
|
|
bool32 ok;
|
2022-05-19 23:57:49 +00:00
|
|
|
int64_t h;
|
2022-03-23 02:51:27 +00:00
|
|
|
int event, ntpid;
|
2022-04-12 12:20:17 +00:00
|
|
|
|
|
|
|
// is killing everything except init really worth supporting?
|
|
|
|
if (pid == -1) return einval();
|
|
|
|
|
|
|
|
// XXX: NT doesn't really have process groups. For instance the
|
|
|
|
// CreateProcess() flag for starting a process group actually
|
|
|
|
// just does an "ignore ctrl-c" internally.
|
|
|
|
pid = ABS(pid);
|
|
|
|
|
|
|
|
// If we're targeting current process group then just call raise().
|
|
|
|
if (!pid || pid == getpid()) {
|
2023-03-29 05:28:09 +00:00
|
|
|
if (!sig) return 0; // ability check passes
|
2022-04-12 12:20:17 +00:00
|
|
|
return raise(sig);
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateConsoleCtrlEvent() will always signal groups and there's
|
|
|
|
// nothing we can do about it, unless we have a GUI GetMessage loop
|
|
|
|
// and alternatively create a centralized signal daemon like cygwin
|
|
|
|
if ((event = GetConsoleCtrlEvent(sig)) != -1) {
|
|
|
|
// we're killing with SIGINT or SIGQUIT which are the only two
|
|
|
|
// signals we can really use, since TerminateProcess() makes
|
|
|
|
// everything else effectively a SIGKILL ;_;
|
|
|
|
if (__isfdkind(pid, kFdProcess)) {
|
|
|
|
ntpid = GetProcessId(g_fds.p[pid].handle);
|
|
|
|
} else if (!__isfdopen(pid)) {
|
|
|
|
ntpid = pid; // XXX: suboptimal
|
|
|
|
} else {
|
|
|
|
return esrch();
|
|
|
|
}
|
|
|
|
if (GenerateConsoleCtrlEvent(event, ntpid)) {
|
|
|
|
return 0;
|
2021-01-28 03:34:02 +00:00
|
|
|
} else {
|
2022-04-12 12:20:17 +00:00
|
|
|
return -1;
|
2021-01-28 03:34:02 +00:00
|
|
|
}
|
2022-04-12 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-19 23:57:49 +00:00
|
|
|
// is this a cosmo pid that was returned by fork?
|
2022-04-12 12:20:17 +00:00
|
|
|
if (__isfdkind(pid, kFdProcess)) {
|
2023-03-29 05:28:09 +00:00
|
|
|
if (!sig) return 0; // ability check passes
|
2022-05-19 23:57:49 +00:00
|
|
|
// since windows can't execve we need to kill the grandchildren
|
|
|
|
// TODO(jart): should we just kill the whole tree too? there's
|
|
|
|
// no obvious way to tell if it's the execve shell
|
|
|
|
struct NtProcessEntry32 pe = {.dwSize = sizeof(struct NtProcessEntry32)};
|
|
|
|
ntpid = GetProcessId(g_fds.p[pid].handle);
|
2023-07-30 15:55:01 +00:00
|
|
|
int64_t hSnap = CreateToolhelp32Snapshot(kNtTh32csSnapprocess, 0);
|
2022-05-19 23:57:49 +00:00
|
|
|
if (Process32First(hSnap, &pe)) {
|
|
|
|
do {
|
|
|
|
if (pe.th32ParentProcessID == ntpid) {
|
|
|
|
if ((h = OpenProcess(kNtProcessTerminate, false, pe.th32ProcessID))) {
|
2023-07-30 21:26:21 +00:00
|
|
|
TerminateProcess(h, sig);
|
2022-05-19 23:57:49 +00:00
|
|
|
CloseHandle(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (Process32Next(hSnap, &pe));
|
|
|
|
}
|
2023-07-30 15:55:01 +00:00
|
|
|
CloseHandle(hSnap);
|
2023-07-30 21:26:21 +00:00
|
|
|
ok = TerminateProcess(g_fds.p[pid].handle, sig);
|
2022-04-12 12:20:17 +00:00
|
|
|
if (!ok && GetLastError() == kNtErrorAccessDenied) ok = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: Is this a raw new technology pid? Because that's messy.
|
2022-05-19 23:57:49 +00:00
|
|
|
if ((h = OpenProcess(kNtProcessTerminate, false, pid))) {
|
2023-03-29 05:28:09 +00:00
|
|
|
if (sig) {
|
2023-07-30 21:26:21 +00:00
|
|
|
ok = TerminateProcess(h, sig);
|
2023-03-29 05:28:09 +00:00
|
|
|
if (!ok && GetLastError() == kNtErrorAccessDenied) {
|
|
|
|
ok = true; // cargo culting other codebases here
|
|
|
|
}
|
2022-04-12 12:20:17 +00:00
|
|
|
}
|
2022-05-19 23:57:49 +00:00
|
|
|
CloseHandle(h);
|
2022-04-12 12:20:17 +00:00
|
|
|
return 0;
|
2021-08-16 22:26:31 +00:00
|
|
|
} else {
|
2022-04-12 12:20:17 +00:00
|
|
|
return -1;
|
2021-01-28 03:34:02 +00:00
|
|
|
}
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|