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
diff --git a/src/main/cpp/blaze.cc b/src/main/cpp/blaze.cc
index 42e2cc7..2c1eeb3 100644
--- a/src/main/cpp/blaze.cc
+++ b/src/main/cpp/blaze.cc
@@ -558,7 +558,7 @@
   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 @@
 
 // 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 @@
   // 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 @@
          "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 @@
     // 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 @@
           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 5421fb7..e1a30cb 100644
--- a/src/main/cpp/blaze_util.cc
+++ b/src/main/cpp/blaze_util.cc
@@ -46,6 +46,14 @@
 
 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 @@
 }
 
 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 @@
   }
 
   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 86a8256..3e34fa4 100644
--- a/src/main/cpp/blaze_util.h
+++ b/src/main/cpp/blaze_util.h
@@ -29,6 +29,10 @@
 
 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 42a2f5d..e191711 100644
--- a/src/main/cpp/blaze_util_platform.h
+++ b/src/main/cpp/blaze_util_platform.h
@@ -62,9 +62,10 @@
 // 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 8e71477..ad7c9d1 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 @@
     // 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);
   }
 
   /**