diff options
author | Eelco Dolstra <e.dolstra@tudelft.nl> | 2012-03-05 19:19:29 +0100 |
---|---|---|
committer | Eelco Dolstra <e.dolstra@tudelft.nl> | 2012-03-05 19:19:29 +0100 |
commit | 1d487dc6a682468ae00402b3720ff412b6bfb6fc (patch) | |
tree | 94311d8c9b57893255bf613be84fb1cc8b924f55 | |
parent | 2b4964f31979b5227a7a51b646d2e4bbb5ef6579 (diff) | |
download | guix-1d487dc6a682468ae00402b3720ff412b6bfb6fc.tar.gz |
nix-worker: put the pid of the caller in argv[1]
This is useful for debugging.
-rw-r--r-- | src/libmain/shared.cc | 4 | ||||
-rw-r--r-- | src/libmain/shared.hh | 2 | ||||
-rw-r--r-- | src/nix-worker/nix-worker.cc | 21 |
3 files changed, 25 insertions, 2 deletions
diff --git a/src/libmain/shared.cc b/src/libmain/shared.cc index d3b73f8fd5..99ac198ca3 100644 --- a/src/libmain/shared.cc +++ b/src/libmain/shared.cc @@ -287,7 +287,7 @@ static void * oomHandler(size_t requested) int exitCode = 0; - +char * * argvSaved = 0; } @@ -298,6 +298,8 @@ int main(int argc, char * * argv) { using namespace nix; + argvSaved = argv; + /* If we're setuid, then we need to take some security precautions right away. */ if (argc == 0) abort(); diff --git a/src/libmain/shared.hh b/src/libmain/shared.hh index 1d6a6ec573..43725e07fd 100644 --- a/src/libmain/shared.hh +++ b/src/libmain/shared.hh @@ -56,6 +56,8 @@ struct RemoveTempRoots /* Exit code of the program. */ extern int exitCode; +extern char * * argvSaved; + } diff --git a/src/nix-worker/nix-worker.cc b/src/nix-worker/nix-worker.cc index eaa6866673..7ca54dee99 100644 --- a/src/nix-worker/nix-worker.cc +++ b/src/nix-worker/nix-worker.cc @@ -753,8 +753,21 @@ static void daemonLoop() throw SysError("accepting connection"); } - printMsg(lvlInfo, format("accepted connection %1%") % remote); + /* Get the identity of the caller, if possible. */ + uid_t clientUid = -1; + pid_t clientPid = -1; + +#if defined(SO_PEERCRED) + ucred cred; + socklen_t credLen = sizeof(cred); + if (getsockopt(remote, SOL_SOCKET, SO_PEERCRED, &cred, &credLen) != -1) { + clientPid = cred.pid; + clientUid = cred.uid; + } +#endif + printMsg(lvlInfo, format("accepted connection from pid %1%, uid %2%") % clientPid % clientUid); + /* Fork a child to handle the connection. */ pid_t child; child = fork(); @@ -774,6 +787,12 @@ static void daemonLoop() /* Restore normal handling of SIGCHLD. */ setSigChldAction(false); + /* For debugging, stuff the pid into argv[1]. */ + if (clientPid != -1 && argvSaved[1]) { + string processName = int2String(clientPid); + strncpy(argvSaved[1], processName.c_str(), strlen(argvSaved[1])); + } + /* Since the daemon can be long-running, the settings may have changed. So force a reload. */ reloadSettings(); |