Files
lix/lix/libexec/run-diff-hook.cc
T
eldritch horrors 5351518c75 libstore: add a diff hook helper libexec binary
diff hooks use uid/gid switch functionality that is otherwise only
needed for linux sandbox setup and unsupported by posix_spawn. not
doing these switches inside lix core code may let us move to using
posix_spawn for most process launching in the future, and for diff
hooks the added overhead of a wrapper program really does not hurt
at all. diff hooks are expected to be expensive in terms of output
size, process launch overhead is not likely to even be noticeable.

Change-Id: Ifa4b3eedef237632db3eb88d10e6469acae01f9e
2026-01-22 15:23:38 +00:00

28 lines
675 B
C++

#include "common.hh"
#include <grp.h>
using std::literals::operator""sv;
LIBEXEC_HELPER(3)
int helperMain(const char * name, std::span<char *> args) noexcept
{
const auto uid = args[0];
const auto gid = args[1];
const auto hook = args.subspan(2);
DIE_UNLESS_SYS("chdir", chdir("/"));
if (gid != "-"sv) {
DIE_UNLESS_SYS("setgid", setgid(argToInt<gid_t>("gid", gid)));
/* Drop all other groups if we're setgid. */
DIE_UNLESS_SYS("setgroups", setgroups(0, 0));
}
if (uid != "-"sv) {
DIE_UNLESS_SYS("setuid", setuid(argToInt<uid_t>("uid", uid)));
}
execvp(hook[0], hook.data());
die("exec failed");
}