aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/cpp
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2016-04-25 07:22:11 +0000
committerGravatar Yun Peng <pcloudy@google.com>2016-04-25 09:10:35 +0000
commitea4c42efd6376834d86fc27d4d640eac884de4ea (patch)
tree791470b06d33eb1a62c67c63f92940df14f7de92 /src/main/cpp
parentf6893d8773bb01772f4fbaeef07dcc1533d89cc8 (diff)
Make the PID be the contents of the PID file instead of a symlink.
I wonder why it was implemented like this in the first place. Unsurprisingly, it doesn't work on Windows. -- MOS_MIGRATED_REVID=120682316
Diffstat (limited to 'src/main/cpp')
-rw-r--r--src/main/cpp/blaze.cc34
1 files changed, 26 insertions, 8 deletions
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 3187f1d883..321706a7fb 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -855,16 +855,34 @@ static void WriteFileToStreamOrDie(FILE *stream, const char *file_name) {
static int GetServerPid(const string &pid_file) {
// Note: there is no race here on startup since the server creates
// the pid file strictly before it binds the socket.
- char buf[16];
- auto len = readlink(pid_file.c_str(), buf, sizeof(buf) - 1);
- if (len > 0) {
- buf[len] = '\0';
- return atoi(buf);
- } else {
+
+ char buf[33];
+
+ // The server writes a file, but we need to handle old servers that still
+ // write a symlink.
+ // TODO(lberki): Remove the readlink() call when there is no chance of an old
+ // server lingering around. Probably safe after 2016.06.01.
+ int len;
+ len = readlink(pid_file.c_str(), buf, sizeof(buf) - 1);
+ if (len < 0) {
+ int fd = open(pid_file.c_str(), O_RDONLY);
+ if (fd < 0) {
+ return -1;
+ }
+ len = read(fd, buf, 32);
+ close(fd);
+ if (len < 0) {
+ return -1;
+ }
+ }
+
+ int result;
+ buf[len] = 0;
+ if (!blaze_util::safe_strto32(string(buf), &result)) {
return -1;
- pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "can't get server pid from connection");
}
+
+ return result;
}
// Connects to the Blaze server, returning the socket, or -1 if no