-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
201 additions
and
225 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); })) | ||
{ } | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.