diff options
author | Lukacs Berki <lberki@google.com> | 2016-05-02 09:31:37 +0000 |
---|---|---|
committer | Damien Martin-Guillerez <dmarting@google.com> | 2016-05-02 11:51:43 +0000 |
commit | 1977d92e17e6bb640cc5947ff76826928c7d7419 (patch) | |
tree | d1cb6a574e25c9abf6722c9c2817898d60ee2c84 /src/main/cpp/blaze_util_posix.cc | |
parent | 7093b97ec3f332016822ed154e81bd86bc7ca645 (diff) |
Various cleanups and refactorings in the client:
- Made the control flow much simpler and more understandable
- Added some documentation about the interplay of the client and the server
- Abstracted out POSIX mechanisms from blaze.cc so that they can be implemented properly on Windows
- Added assertions that the methods on BlazeServer are called when they should be
Polish for #930.
--
MOS_MIGRATED_REVID=121256601
Diffstat (limited to 'src/main/cpp/blaze_util_posix.cc')
-rw-r--r-- | src/main/cpp/blaze_util_posix.cc | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/src/main/cpp/blaze_util_posix.cc b/src/main/cpp/blaze_util_posix.cc index 11a74febb7..5c28bd372b 100644 --- a/src/main/cpp/blaze_util_posix.cc +++ b/src/main/cpp/blaze_util_posix.cc @@ -99,8 +99,36 @@ static void Daemonize(const string& daemon_output) { dup(STDOUT_FILENO); // stderr (2>&1) } -int ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, - const string& daemon_output, const string& server_dir) { +class PipeBlazeServerStartup : public BlazeServerStartup { + public: + PipeBlazeServerStartup(int pipe_fd); + virtual ~PipeBlazeServerStartup(); + bool IsStillAlive() override; + + private: + int pipe_fd; +}; + +PipeBlazeServerStartup::PipeBlazeServerStartup(int pipe_fd) { + this->pipe_fd = pipe_fd; + if (fcntl(pipe_fd, F_SETFL, O_NONBLOCK | fcntl(pipe_fd, F_GETFL))) { + pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR, + "Failed: fcntl to enable O_NONBLOCK on pipe"); + } +} + +PipeBlazeServerStartup::~PipeBlazeServerStartup() { + close(pipe_fd); +} + +bool PipeBlazeServerStartup::IsStillAlive() { + char c; + return read(this->pipe_fd, &c, 1) == -1 && errno == EAGAIN; +} + +void ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, + const string& daemon_output, const string& server_dir, + BlazeServerStartup** server_startup) { int fds[2]; if (pipe(fds)) { pdie(blaze_exit_code::INTERNAL_ERROR, "pipe creation failed"); @@ -110,7 +138,8 @@ int ExecuteDaemon(const string& exe, const std::vector<string>& args_vector, pdie(blaze_exit_code::INTERNAL_ERROR, "fork() failed"); } else if (child > 0) { // we're the parent close(fds[1]); // parent keeps only the reading side - return fds[0]; + *server_startup = new PipeBlazeServerStartup(fds[0]); + return; } else { close(fds[0]); // child keeps only the writing side } @@ -180,4 +209,10 @@ bool CompareAbsolutePaths(const string& a, const string& b) { return a == b; } +void KillServerProcess(int pid, const string& output_base) { + // TODO(lberki): This might accidentally kill an unrelated process if the + // server died and the PID got reused. + killpg(pid, SIGKILL); +} + } // namespace blaze. |