mirror of
https://github.com/NixOS/nix.git
synced 2025-11-16 07:22:43 +01:00
nix-find-roots: Cleanup
Based on an offline review by @mopleen (thanks!)
This commit is contained in:
parent
c4ca0d45cb
commit
150f3bb5ea
2 changed files with 15 additions and 17 deletions
|
|
@ -7,19 +7,9 @@ namespace nix::roots_tracer {
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
using std::set, std::map, std::string;
|
using std::set, std::map, std::string;
|
||||||
|
|
||||||
class Error : public std::exception {
|
class Error : public std::runtime_error {
|
||||||
private:
|
|
||||||
const string message;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Error(std::string message)
|
using std::runtime_error::runtime_error;
|
||||||
: message(message)
|
|
||||||
{}
|
|
||||||
|
|
||||||
const char* what() const noexcept override
|
|
||||||
{
|
|
||||||
return message.c_str();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void logNone(std::string_view)
|
inline void logNone(std::string_view)
|
||||||
|
|
|
||||||
|
|
@ -80,11 +80,11 @@ std::string escape(std::string original)
|
||||||
{
|
{
|
||||||
map<string, string> replacements = {
|
map<string, string> replacements = {
|
||||||
{"\n", "\\n"},
|
{"\n", "\\n"},
|
||||||
{"\n", "\\t"},
|
{"\t", "\\t"},
|
||||||
};
|
};
|
||||||
for (auto [oldStr, newStr] : replacements) {
|
for (auto [oldStr, newStr] : replacements) {
|
||||||
size_t currentPos = 0;
|
size_t currentPos = 0;
|
||||||
while ((currentPos = original.find(oldStr)) != std::string::npos) {
|
while ((currentPos = original.find(oldStr, currentPos)) != std::string::npos) {
|
||||||
original.replace(currentPos, oldStr.length(), newStr);
|
original.replace(currentPos, oldStr.length(), newStr);
|
||||||
currentPos += newStr.length();
|
currentPos += newStr.length();
|
||||||
}
|
}
|
||||||
|
|
@ -109,19 +109,27 @@ int main(int argc, char * * argv)
|
||||||
auto rawListenFds = std::getenv("LISTEN_FDS");
|
auto rawListenFds = std::getenv("LISTEN_FDS");
|
||||||
if (rawListenFds) {
|
if (rawListenFds) {
|
||||||
auto listenFds = std::string(rawListenFds);
|
auto listenFds = std::string(rawListenFds);
|
||||||
if (std::getenv("LISTEN_PID") != std::to_string(getpid()) || listenFds != "1")
|
auto listenPid = std::getenv("LISTEN_PID");
|
||||||
|
if (listenPid == nullptr || listenPid != std::to_string(getpid()) || listenFds != "1")
|
||||||
throw Error("unexpected systemd environment variables");
|
throw Error("unexpected systemd environment variables");
|
||||||
mySock = SD_LISTEN_FDS_START;
|
mySock = SD_LISTEN_FDS_START;
|
||||||
} else {
|
} else {
|
||||||
mySock = socket(PF_UNIX, SOCK_STREAM, 0);
|
mySock = socket(PF_UNIX, SOCK_STREAM, 0);
|
||||||
if (mySock == 0) {
|
if (mySock == -1) {
|
||||||
throw Error("Cannot create Unix domain socket");
|
throw Error(std::string("Cannot create Unix domain socket, got") +
|
||||||
|
std::strerror(errno));
|
||||||
}
|
}
|
||||||
struct sockaddr_un addr;
|
struct sockaddr_un addr;
|
||||||
addr.sun_family = AF_UNIX;
|
addr.sun_family = AF_UNIX;
|
||||||
|
|
||||||
auto socketDir = opts.socketPath.parent_path();
|
auto socketDir = opts.socketPath.parent_path();
|
||||||
auto socketFilename = opts.socketPath.filename();
|
auto socketFilename = opts.socketPath.filename();
|
||||||
|
if (socketFilename.string().size() > sizeof(addr.sun_path))
|
||||||
|
throw Error(
|
||||||
|
"Socket filename " + socketFilename.string() +
|
||||||
|
" is too long, should be at most " +
|
||||||
|
std::to_string(sizeof(addr.sun_path))
|
||||||
|
);
|
||||||
chdir(socketDir.c_str());
|
chdir(socketDir.c_str());
|
||||||
|
|
||||||
fs::remove(socketFilename);
|
fs::remove(socketFilename);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue