-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nix-daemon: Use a thread instead of SIGPOLL to catch client disconnects
The thread calls poll() to wait until a HUP (or other error event) happens on the client connection. If so, it sends SIGINT to the main thread, which is then cleaned up normally. This is much nicer than messing around with SIGPOLL.
- Loading branch information
Showing
3 changed files
with
51 additions
and
146 deletions.
There are no files selected for viewing
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
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,43 @@ | ||
#pragma once | ||
|
||
#include <thread> | ||
|
||
#include <poll.h> | ||
#include <sys/types.h> | ||
#include <unistd.h> | ||
#include <signal.h> | ||
|
||
namespace nix { | ||
|
||
|
||
class MonitorFdHup | ||
{ | ||
private: | ||
std::thread thread; | ||
|
||
public: | ||
MonitorFdHup(int fd) | ||
{ | ||
thread = std::thread([&]() { | ||
/* Wait indefinitely until a POLLHUP occurs. */ | ||
struct pollfd fds[1]; | ||
fds[0].fd = fd; | ||
fds[0].events = 0; | ||
if (poll(fds, 1, -1) == -1) { | ||
if (errno != EINTR) abort(); // can't happen | ||
return; // destructor is asking us to exit | ||
} | ||
/* We got POLLHUP, so send an INT signal to the main thread. */ | ||
kill(getpid(), SIGINT); | ||
}); | ||
}; | ||
|
||
~MonitorFdHup() | ||
{ | ||
pthread_kill(thread.native_handle(), SIGINT); | ||
thread.join(); | ||
} | ||
}; | ||
|
||
|
||
} |
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