aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main
diff options
context:
space:
mode:
authorGravatar Lukacs Berki <lberki@google.com>2016-04-26 11:40:24 +0000
committerGravatar Yun Peng <pcloudy@google.com>2016-04-26 14:39:30 +0000
commitd9da60f49d367e935307f71b8f1aa2c03b7aa20b (patch)
treed430a871b01f4d8209ad981d8e114b522db2b23c /src/main
parent2b0b5cc078e9acc4f9b4908fb6c835119f295ce1 (diff)
Revert server.pid to be a symlink so that old server version can shut down new ones.
Add server.pid.txt that contains the same information in text form. ExecuteDaemon() on Windows will simply not write server.pid . -- MOS_MIGRATED_REVID=120802055
Diffstat (limited to 'src/main')
-rw-r--r--src/main/cpp/blaze.cc19
-rw-r--r--src/main/cpp/blaze_util.cc22
-rw-r--r--src/main/cpp/blaze_util.h4
-rw-r--r--src/main/cpp/blaze_util_platform.h5
-rw-r--r--src/main/java/com/google/devtools/build/lib/server/RPCServer.java4
5 files changed, 40 insertions, 14 deletions
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 42e2cc7efa..2c1eeb33bf 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -558,7 +558,7 @@ static int StartServer() {
GoToWorkspace();
return ExecuteDaemon(exe, jvm_args_vector, globals->jvm_log_file.c_str(),
- server_dir + "/server.pid");
+ server_dir);
}
static bool KillRunningServerIfAny(BlazeServer *server);
@@ -846,7 +846,7 @@ static void WriteFileToStreamOrDie(FILE *stream, const char *file_name) {
// After connecting to the Blaze server, initialize server_pid. Return -1 if
// there was an error.
-static int GetServerPid(const string &pid_file) {
+static int GetServerPid(const string &server_dir) {
// Note: there is no race here on startup since the server creates
// the pid file strictly before it binds the socket.
@@ -857,7 +857,9 @@ static int GetServerPid(const string &pid_file) {
// 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);
+ string pid_file = blaze_util::JoinPath(server_dir, ServerPidFile());
+ string pid_symlink = blaze_util::JoinPath(server_dir, ServerPidSymlink());
+ len = readlink(pid_symlink.c_str(), buf, sizeof(buf) - 1);
if (len < 0) {
int fd = open(pid_file.c_str(), O_RDONLY);
if (fd < 0) {
@@ -892,12 +894,11 @@ static bool ConnectToServer(BlazeServer *server, bool start) {
"server directory '%s' could not be created", server_dir.c_str());
}
- string socket_file = server_dir + "/server.socket";
- string pid_file = server_dir + "/server.pid";
+ string socket_file = blaze_util::JoinPath(server_dir, "server.socket");
globals->server_pid = 0;
if (server->Connect()) {
- globals->server_pid = GetServerPid(pid_file);
+ globals->server_pid = GetServerPid(server_dir);
if (globals->server_pid == -1) {
pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
"can't get server pid from connection");
@@ -911,7 +912,7 @@ static bool ConnectToServer(BlazeServer *server, bool start) {
// server is in a GC pause and therefore cannot respond to ping requests and
// having two server instances running in the same output base is a
// disaster.
- int server_pid = GetServerPid(pid_file);
+ int server_pid = GetServerPid(server_dir);
if (server_pid >= 0) {
killpg(server_pid, SIGKILL);
}
@@ -932,10 +933,10 @@ static bool ConnectToServer(BlazeServer *server, bool start) {
fputc('\n', stderr);
fflush(stderr);
}
- globals->server_pid = GetServerPid(pid_file);
+ globals->server_pid = GetServerPid(server_dir);
if (globals->server_pid == -1) {
pdie(blaze_exit_code::LOCAL_ENVIRONMENTAL_ERROR,
- "can't get server pid from connection");
+ "can't get pid of fresh server from connection");
}
return true;
}
diff --git a/src/main/cpp/blaze_util.cc b/src/main/cpp/blaze_util.cc
index 5421fb7d0f..e1a30cbef1 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -46,6 +46,14 @@ using std::vector;
namespace blaze {
+string ServerPidFile() {
+ return "server.pid.txt";
+}
+
+string ServerPidSymlink() {
+ return "server.pid";
+}
+
string GetUserName() {
const char *user = getenv("USER");
if (user && user[0] != '\0') return user;
@@ -368,7 +376,7 @@ static void Daemonize(const string& daemon_output) {
}
int ExecuteDaemon(const string& exe, const std::vector<string>& args_vector,
- const string& daemon_output, const string& pid_file) {
+ const string& daemon_output, const string& server_dir) {
int fds[2];
if (pipe(fds)) {
pdie(blaze_exit_code::INTERNAL_ERROR, "pipe creation failed");
@@ -384,12 +392,22 @@ int ExecuteDaemon(const string& exe, const std::vector<string>& args_vector,
}
Daemonize(daemon_output);
- if (!WriteFile(ToString(getpid()), pid_file)) {
+ string pid_string = ToString(getpid());
+ string pid_file = blaze_util::JoinPath(server_dir, ServerPidFile());
+ string pid_symlink_file =
+ blaze_util::JoinPath(server_dir, ServerPidSymlink());
+
+ if (!WriteFile(pid_string, pid_file)) {
// The exit code does not matter because we are already in the daemonized
// server. The output of this operation will end up in jvm.out .
pdie(0, "Cannot write PID file");
}
+ UnlinkPath(pid_symlink_file.c_str());
+ if (symlink(pid_string.c_str(), pid_symlink_file.c_str()) < 0) {
+ pdie(0, "Cannot write PID symlink");
+ }
+
ExecuteProgram(exe, args_vector);
pdie(0, "Cannot execute %s", exe.c_str());
}
diff --git a/src/main/cpp/blaze_util.h b/src/main/cpp/blaze_util.h
index 86a8256b11..3e34fa4eab 100644
--- a/src/main/cpp/blaze_util.h
+++ b/src/main/cpp/blaze_util.h
@@ -29,6 +29,10 @@ namespace blaze {
using std::string;
+string ServerPidFile();
+
+string ServerPidSymlink();
+
string GetUserName();
// Returns the given path in absolute form. Does not change paths that are
diff --git a/src/main/cpp/blaze_util_platform.h b/src/main/cpp/blaze_util_platform.h
index 42a2f5db9f..e191711c54 100644
--- a/src/main/cpp/blaze_util_platform.h
+++ b/src/main/cpp/blaze_util_platform.h
@@ -62,9 +62,10 @@ void ExecuteProgram(const string& exe, const std::vector<string>& args_vector);
// Starts a daemon process with its standard output and standard error
// redirected to the file "daemon_output". Returns a file descriptor of a named
// pipe whose other end is held by the daemon and which is closed if the daemon
-// exits.
+// exits. The PID of the daemon just started is written into server_dir, both
+// as a symlink (for legacy reasons) and as a file.
int ExecuteDaemon(const string& exe, const std::vector<string>& args_vector,
- const string& daemon_output, const string& pid_file);
+ const string& daemon_output, const string& server_dir);
// Executes a subprocess and returns its standard output and standard error.
// If this fails, exits with the appropriate error code.
diff --git a/src/main/java/com/google/devtools/build/lib/server/RPCServer.java b/src/main/java/com/google/devtools/build/lib/server/RPCServer.java
index 8e71477b49..ad7c9d1377 100644
--- a/src/main/java/com/google/devtools/build/lib/server/RPCServer.java
+++ b/src/main/java/com/google/devtools/build/lib/server/RPCServer.java
@@ -42,8 +42,10 @@ public abstract class RPCServer {
// server.pid was written in the C++ launcher after fork() but before exec() .
// The client only accesses the pid file after connecting to the socket
// which ensures that it gets the correct pid value.
- Path pidFile = serverDirectory.getRelative("server.pid");
+ Path pidFile = serverDirectory.getRelative("server.pid.txt");
+ Path pidSymlink = serverDirectory.getRelative("server.pid");
RPCServer.deleteAtExit(pidFile, /*deleteParent=*/ false);
+ RPCServer.deleteAtExit(pidSymlink, /*deleteParent=*/ false);
}
/**