Files
lix/lix/libexec/kill-user.cc
T
skye 84347f94da libexec/kill-user: suppress syscall deprecation warning on darwin
The existing function is used with good reason as explained by its
comment. This suppresses the warning it creates.

Change-Id: I58b0de3f5854266fa77b6a06b7821a636a6a6964
2026-04-22 08:37:17 -04:00

64 lines
1.8 KiB
C++

#include "common.hh"
#include <charconv>
#include <cstring>
#include <format>
#include <signal.h>
#include <unistd.h>
#if __APPLE__
#include <sys/syscall.h>
#endif
LIBEXEC_HELPER(1)
int helperMain(const char * name, std::span<char *> args) noexcept
{
std::string_view uidArg = args[0];
uid_t uid;
if (auto res = std::from_chars(uidArg.begin(), uidArg.end(), uid);
res.ptr != uidArg.end() || res.ec != std::errc())
{
die("invalid uid argument");
}
/* The system call kill(-1, sig) sends the signal `sig' to all
users to which the current process can send signals. So we
switch to that uid and send a mass kill once we've done so. */
if (setuid(uid) == -1) {
die(std::format("setuid(): {}", strerror(errno)));
}
while (true) {
#ifdef __APPLE__
/* OSX's kill syscall takes a third parameter that, among
other things, determines if kill(-1, signo) affects the
calling process. In the OSX libc, it's set to true,
which means "follow POSIX", which we don't want here */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
if (syscall(SYS_kill, -1, SIGKILL, false) == 0) {
break;
}
#pragma clang diagnostic pop
#else
if (kill(-1, SIGKILL) == 0) {
break;
}
#endif
if (errno == ESRCH || errno == EPERM) {
break; /* no more processes */
}
if (errno != EINTR) {
die(std::format("cannot kill processes for uid {}: {}", uid, strerror(errno)));
}
}
/* !!! We should really do some check to make sure that there are
no processes left running under `uid', but there is no portable
way to do so (I think). The most reliable way may be `ps -eo
uid | grep -q $uid'. */
return 0;
}