mirror of
https://github.com/NixOS/nix.git
synced 2025-11-09 03:56:01 +01:00
Apply clang-format universally.
* It is tough to contribute to a project that doesn't use a formatter, * It is extra hard to contribute to a project which has configured the formatter, but ignores it for some files * Code formatting makes it harder to hide obscure / weird bugs by accident or on purpose, Let's rip the bandaid off? Note that PRs currently in flight should be able to be merged relatively easily by applying `clang-format` to their tip prior to merge. Co-authored-by: Graham Christensen <graham@grahamc.com>
This commit is contained in:
parent
7b97377ac3
commit
a5264aa46e
565 changed files with 23490 additions and 23405 deletions
|
|
@ -20,51 +20,45 @@
|
|||
#include <unistd.h>
|
||||
|
||||
#ifdef __APPLE__
|
||||
# include <sys/syscall.h>
|
||||
# include <sys/syscall.h>
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
# include <sys/prctl.h>
|
||||
# include <sys/mman.h>
|
||||
# include <sys/prctl.h>
|
||||
# include <sys/mman.h>
|
||||
#endif
|
||||
|
||||
#include "util-config-private.hh"
|
||||
#include "util-unix-config-private.hh"
|
||||
|
||||
|
||||
namespace nix {
|
||||
|
||||
Pid::Pid()
|
||||
{
|
||||
}
|
||||
|
||||
Pid::Pid() {}
|
||||
|
||||
Pid::Pid(pid_t pid)
|
||||
: pid(pid)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Pid::~Pid()
|
||||
{
|
||||
if (pid != -1) kill();
|
||||
if (pid != -1)
|
||||
kill();
|
||||
}
|
||||
|
||||
|
||||
void Pid::operator =(pid_t pid)
|
||||
void Pid::operator=(pid_t pid)
|
||||
{
|
||||
if (this->pid != -1 && this->pid != pid) kill();
|
||||
if (this->pid != -1 && this->pid != pid)
|
||||
kill();
|
||||
this->pid = pid;
|
||||
killSignal = SIGKILL; // reset signal to default
|
||||
}
|
||||
|
||||
|
||||
Pid::operator pid_t()
|
||||
{
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
||||
int Pid::kill()
|
||||
{
|
||||
assert(pid != -1);
|
||||
|
|
@ -87,7 +81,6 @@ int Pid::kill()
|
|||
return wait();
|
||||
}
|
||||
|
||||
|
||||
int Pid::wait()
|
||||
{
|
||||
assert(pid != -1);
|
||||
|
|
@ -104,19 +97,16 @@ int Pid::wait()
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
void Pid::setSeparatePG(bool separatePG)
|
||||
{
|
||||
this->separatePG = separatePG;
|
||||
}
|
||||
|
||||
|
||||
void Pid::setKillSignal(int signal)
|
||||
{
|
||||
this->killSignal = signal;
|
||||
}
|
||||
|
||||
|
||||
pid_t Pid::release()
|
||||
{
|
||||
pid_t p = pid;
|
||||
|
|
@ -124,7 +114,6 @@ pid_t Pid::release()
|
|||
return p;
|
||||
}
|
||||
|
||||
|
||||
void killUser(uid_t uid)
|
||||
{
|
||||
debug("killing all processes running under uid '%1%'", uid);
|
||||
|
|
@ -136,7 +125,6 @@ void killUser(uid_t uid)
|
|||
fork a process, switch to uid, and send a mass kill. */
|
||||
|
||||
Pid pid = startProcess([&] {
|
||||
|
||||
if (setuid(uid) == -1)
|
||||
throw SysError("setting uid");
|
||||
|
||||
|
|
@ -147,11 +135,14 @@ void killUser(uid_t uid)
|
|||
calling process. In the OSX libc, it's set to true,
|
||||
which means "follow POSIX", which we don't want here
|
||||
*/
|
||||
if (syscall(SYS_kill, -1, SIGKILL, false) == 0) break;
|
||||
if (syscall(SYS_kill, -1, SIGKILL, false) == 0)
|
||||
break;
|
||||
#else
|
||||
if (kill(-1, SIGKILL) == 0) break;
|
||||
if (kill(-1, SIGKILL) == 0)
|
||||
break;
|
||||
#endif
|
||||
if (errno == ESRCH || errno == EPERM) break; /* no more processes */
|
||||
if (errno == ESRCH || errno == EPERM)
|
||||
break; /* no more processes */
|
||||
if (errno != EINTR)
|
||||
throw SysError("cannot kill processes for uid '%1%'", uid);
|
||||
}
|
||||
|
|
@ -169,7 +160,6 @@ void killUser(uid_t uid)
|
|||
uid | grep -q $uid'. */
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
using ChildWrapperFunction = std::function<void()>;
|
||||
|
|
@ -177,6 +167,7 @@ using ChildWrapperFunction = std::function<void()>;
|
|||
/* Wrapper around vfork to prevent the child process from clobbering
|
||||
the caller's stack frame in the parent. */
|
||||
static pid_t doFork(bool allowVfork, ChildWrapperFunction & fun) __attribute__((noinline));
|
||||
|
||||
static pid_t doFork(bool allowVfork, ChildWrapperFunction & fun)
|
||||
{
|
||||
#ifdef __linux__
|
||||
|
|
@ -184,22 +175,21 @@ static pid_t doFork(bool allowVfork, ChildWrapperFunction & fun)
|
|||
#else
|
||||
pid_t pid = fork();
|
||||
#endif
|
||||
if (pid != 0) return pid;
|
||||
if (pid != 0)
|
||||
return pid;
|
||||
fun();
|
||||
unreachable();
|
||||
}
|
||||
|
||||
|
||||
#ifdef __linux__
|
||||
static int childEntry(void * arg)
|
||||
{
|
||||
auto & fun = *reinterpret_cast<ChildWrapperFunction*>(arg);
|
||||
auto & fun = *reinterpret_cast<ChildWrapperFunction *>(arg);
|
||||
fun();
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
||||
{
|
||||
auto newLogger = makeSimpleLogger();
|
||||
|
|
@ -222,8 +212,10 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
|||
} catch (std::exception & e) {
|
||||
try {
|
||||
std::cerr << options.errorPrefix << e.what() << "\n";
|
||||
} catch (...) { }
|
||||
} catch (...) { }
|
||||
} catch (...) {
|
||||
}
|
||||
} catch (...) {
|
||||
}
|
||||
if (options.runExitHandlers)
|
||||
exit(1);
|
||||
else
|
||||
|
|
@ -233,34 +225,36 @@ pid_t startProcess(std::function<void()> fun, const ProcessOptions & options)
|
|||
pid_t pid = -1;
|
||||
|
||||
if (options.cloneFlags) {
|
||||
#ifdef __linux__
|
||||
#ifdef __linux__
|
||||
// Not supported, since then we don't know when to free the stack.
|
||||
assert(!(options.cloneFlags & CLONE_VM));
|
||||
|
||||
size_t stackSize = 1 * 1024 * 1024;
|
||||
auto stack = static_cast<char *>(mmap(0, stackSize,
|
||||
PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0));
|
||||
if (stack == MAP_FAILED) throw SysError("allocating stack");
|
||||
auto stack = static_cast<char *>(
|
||||
mmap(0, stackSize, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0));
|
||||
if (stack == MAP_FAILED)
|
||||
throw SysError("allocating stack");
|
||||
|
||||
Finally freeStack([&] { munmap(stack, stackSize); });
|
||||
|
||||
pid = clone(childEntry, stack + stackSize, options.cloneFlags | SIGCHLD, &wrapper);
|
||||
#else
|
||||
#else
|
||||
throw Error("clone flags are only supported on Linux");
|
||||
#endif
|
||||
#endif
|
||||
} else
|
||||
pid = doFork(options.allowVfork, wrapper);
|
||||
|
||||
if (pid == -1) throw SysError("unable to fork");
|
||||
if (pid == -1)
|
||||
throw SysError("unable to fork");
|
||||
|
||||
return pid;
|
||||
}
|
||||
|
||||
|
||||
std::string runProgram(Path program, bool lookupPath, const Strings & args,
|
||||
const std::optional<std::string> & input, bool isInteractive)
|
||||
std::string runProgram(
|
||||
Path program, bool lookupPath, const Strings & args, const std::optional<std::string> & input, bool isInteractive)
|
||||
{
|
||||
auto res = runProgram(RunOptions {.program = program, .lookupPath = lookupPath, .args = args, .input = input, .isInteractive = isInteractive});
|
||||
auto res = runProgram(RunOptions{
|
||||
.program = program, .lookupPath = lookupPath, .args = args, .input = input, .isInteractive = isInteractive});
|
||||
|
||||
if (!statusOk(res.first))
|
||||
throw ExecError(res.first, "program '%1%' %2%", program, statusToString(res.first));
|
||||
|
|
@ -301,8 +295,10 @@ void runProgram2(const RunOptions & options)
|
|||
|
||||
/* Create a pipe. */
|
||||
Pipe out, in;
|
||||
if (options.standardOut) out.create();
|
||||
if (source) in.create();
|
||||
if (options.standardOut)
|
||||
out.create();
|
||||
if (source)
|
||||
in.create();
|
||||
|
||||
ProcessOptions processOptions;
|
||||
// vfork implies that the environment of the main process and the fork will
|
||||
|
|
@ -313,41 +309,43 @@ void runProgram2(const RunOptions & options)
|
|||
auto suspension = logger->suspendIf(options.isInteractive);
|
||||
|
||||
/* Fork. */
|
||||
Pid pid = startProcess([&] {
|
||||
if (options.environment)
|
||||
replaceEnv(*options.environment);
|
||||
if (options.standardOut && dup2(out.writeSide.get(), STDOUT_FILENO) == -1)
|
||||
throw SysError("dupping stdout");
|
||||
if (options.mergeStderrToStdout)
|
||||
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
|
||||
throw SysError("cannot dup stdout into stderr");
|
||||
if (source && dup2(in.readSide.get(), STDIN_FILENO) == -1)
|
||||
throw SysError("dupping stdin");
|
||||
Pid pid = startProcess(
|
||||
[&] {
|
||||
if (options.environment)
|
||||
replaceEnv(*options.environment);
|
||||
if (options.standardOut && dup2(out.writeSide.get(), STDOUT_FILENO) == -1)
|
||||
throw SysError("dupping stdout");
|
||||
if (options.mergeStderrToStdout)
|
||||
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
|
||||
throw SysError("cannot dup stdout into stderr");
|
||||
if (source && dup2(in.readSide.get(), STDIN_FILENO) == -1)
|
||||
throw SysError("dupping stdin");
|
||||
|
||||
if (options.chdir && chdir((*options.chdir).c_str()) == -1)
|
||||
throw SysError("chdir failed");
|
||||
if (options.gid && setgid(*options.gid) == -1)
|
||||
throw SysError("setgid failed");
|
||||
/* Drop all other groups if we're setgid. */
|
||||
if (options.gid && setgroups(0, 0) == -1)
|
||||
throw SysError("setgroups failed");
|
||||
if (options.uid && setuid(*options.uid) == -1)
|
||||
throw SysError("setuid failed");
|
||||
if (options.chdir && chdir((*options.chdir).c_str()) == -1)
|
||||
throw SysError("chdir failed");
|
||||
if (options.gid && setgid(*options.gid) == -1)
|
||||
throw SysError("setgid failed");
|
||||
/* Drop all other groups if we're setgid. */
|
||||
if (options.gid && setgroups(0, 0) == -1)
|
||||
throw SysError("setgroups failed");
|
||||
if (options.uid && setuid(*options.uid) == -1)
|
||||
throw SysError("setuid failed");
|
||||
|
||||
Strings args_(options.args);
|
||||
args_.push_front(options.program);
|
||||
Strings args_(options.args);
|
||||
args_.push_front(options.program);
|
||||
|
||||
restoreProcessContext();
|
||||
restoreProcessContext();
|
||||
|
||||
if (options.lookupPath)
|
||||
execvp(options.program.c_str(), stringsToCharPtrs(args_).data());
|
||||
if (options.lookupPath)
|
||||
execvp(options.program.c_str(), stringsToCharPtrs(args_).data());
|
||||
// This allows you to refer to a program with a pathname relative
|
||||
// to the PATH variable.
|
||||
else
|
||||
execv(options.program.c_str(), stringsToCharPtrs(args_).data());
|
||||
else
|
||||
execv(options.program.c_str(), stringsToCharPtrs(args_).data());
|
||||
|
||||
throw SysError("executing '%1%'", options.program);
|
||||
}, processOptions);
|
||||
throw SysError("executing '%1%'", options.program);
|
||||
},
|
||||
processOptions);
|
||||
|
||||
out.writeSide.close();
|
||||
|
||||
|
|
@ -360,7 +358,6 @@ void runProgram2(const RunOptions & options)
|
|||
writerThread.join();
|
||||
});
|
||||
|
||||
|
||||
if (source) {
|
||||
in.readSide.close();
|
||||
writerThread = std::thread([&] {
|
||||
|
|
@ -390,7 +387,8 @@ void runProgram2(const RunOptions & options)
|
|||
int status = pid.wait();
|
||||
|
||||
/* Wait for the writer thread to finish. */
|
||||
if (source) promise.get_future().get();
|
||||
if (source)
|
||||
promise.get_future().get();
|
||||
|
||||
if (status)
|
||||
throw ExecError(status, "program '%1%' %2%", options.program, statusToString(status));
|
||||
|
|
@ -411,13 +409,12 @@ std::string statusToString(int status)
|
|||
#else
|
||||
return fmt("failed due to signal %1%", sig);
|
||||
#endif
|
||||
}
|
||||
else
|
||||
} else
|
||||
return "died abnormally";
|
||||
} else return "succeeded";
|
||||
} else
|
||||
return "succeeded";
|
||||
}
|
||||
|
||||
|
||||
bool statusOk(int status)
|
||||
{
|
||||
return WIFEXITED(status) && WEXITSTATUS(status) == 0;
|
||||
|
|
@ -428,7 +425,7 @@ int execvpe(const char * file0, const char * const argv[], const char * const en
|
|||
auto file = ExecutablePath::load().findPath(file0);
|
||||
// `const_cast` is safe. See the note in
|
||||
// https://pubs.opengroup.org/onlinepubs/9799919799/functions/exec.html
|
||||
return execve(file.c_str(), const_cast<char *const *>(argv), const_cast<char *const *>(envp));
|
||||
return execve(file.c_str(), const_cast<char * const *>(argv), const_cast<char * const *>(envp));
|
||||
}
|
||||
|
||||
}
|
||||
} // namespace nix
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue