Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Co-Authored-By volth <[email protected]>
  • Loading branch information
Ericson2314 committed Sep 2, 2023
1 parent aee760b commit 3906cdd
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 225 deletions.
File renamed without changes.
73 changes: 73 additions & 0 deletions src/libutil/unix/util-unix.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#pragma once
///@file

#include "util.hh"

namespace nix {

/**
* @return the given user's home directory from /etc/passwd.
*/
Path getHomeOf(uid_t userId);

/**
* Kill all processes running under the specified uid by sending them
* a SIGKILL.
*/
void killUser(uid_t uid);


/**
* Start a thread that handles various signals. Also block those signals
* on the current thread (and thus any threads created by it).
* Saves the signal mask before changing the mask to block those signals.
* See saveSignalMask().
*/
void startSignalHandlerThread();

/**
* Saves the signal mask, which is the signal mask that nix will restore
* before creating child processes.
* See setChildSignalMask() to set an arbitrary signal mask instead of the
* current mask.
*/
void saveSignalMask();

/**
* Sets the signal mask. Like saveSignalMask() but for a signal set that doesn't
* necessarily match the current thread's mask.
* See saveSignalMask() to set the saved mask to the current mask.
*/
void setChildSignalMask(sigset_t *sigs);

struct InterruptCallback
{
virtual ~InterruptCallback() { };
};

/**
* Register a function that gets called on SIGINT (in a non-signal
* context).
*/
std::unique_ptr<InterruptCallback> createInterruptCallback(
std::function<void()> callback);

void triggerInterrupt();

/**
* A RAII class that causes the current thread to receive SIGUSR1 when
* the signal handler thread receives SIGINT. That is, this allows
* SIGINT to be multiplexed to multiple threads.
*/
struct ReceiveInterrupts
{
pthread_t target;
std::unique_ptr<InterruptCallback> callback;

ReceiveInterrupts()
: target(pthread_self())
, callback(createInterruptCallback([&]() { pthread_kill(target, SIGUSR1); }))
{ }
};

}
63 changes: 63 additions & 0 deletions src/libutil/unix/util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "util.hh"
#include "sync.hh"
#include "finally.hh"
#include "serialise.hh"
#include "cgroup.hh"

#include <array>
#include <cctype>
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <future>
#include <iostream>
#include <mutex>
#include <sstream>
#include <thread>

#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/un.h>
#include <unistd.h>

#ifdef __APPLE__
#include <sys/syscall.h>
#include <mach-o/dyld.h>
#endif

#ifdef __linux__
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/mman.h>

#include <cmath>
#endif


extern char * * environ __attribute__((weak));


namespace nix {

void clearEnv()
{
for (auto & name : getEnv())
unsetenv(name.first.c_str());
}

void replaceEnv(const std::map<std::string, std::string> & newEnv)
{
clearEnv();
for (auto & newEnvVar : newEnv)
setenv(newEnvVar.first.c_str(), newEnvVar.second.c_str(), 1);
}

}
Loading

0 comments on commit 3906cdd

Please sign in to comment.