Write pid file so we can tell when netd bounces.
We clear the file early so people know it's died and rewrite after all the
setup and flushing of data is done so people know when it's safe to
repopulate the data.
bug:18069270
Change-Id: I954cf43ff02f1d352015f128ef88b659e6d0f95a
diff --git a/server/main.cpp b/server/main.cpp
index 6af1e4e..5e189cc 100644
--- a/server/main.cpp
+++ b/server/main.cpp
@@ -37,6 +37,12 @@
#include "FwmarkServer.h"
static void blockSigpipe();
+static void remove_pid_file();
+static bool write_pid_file();
+
+const char* const PID_FILE_PATH = "/data/misc/net/netd_pid";
+const int PID_FILE_FLAGS = O_CREAT | O_TRUNC | O_WRONLY | O_NOFOLLOW | O_CLOEXEC;
+const mode_t PID_FILE_MODE = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; // mode 0644, rw-r--r--
int main() {
@@ -47,6 +53,7 @@
FwmarkServer* fwmarkServer;
ALOGI("Netd 1.0 starting");
+ remove_pid_file();
blockSigpipe();
@@ -92,15 +99,52 @@
exit(1);
}
- // Eventually we'll become the monitoring thread
+ bool wrote_pid = write_pid_file();
+
while(1) {
- sleep(1000);
+ sleep(30); // 30 sec
+ if (!wrote_pid) {
+ wrote_pid = write_pid_file();
+ }
}
ALOGI("Netd exiting");
+ remove_pid_file();
exit(0);
}
+static bool write_pid_file() {
+ char pid_buf[20]; // current pid_max is 32768, so plenty of room
+ snprintf(pid_buf, sizeof(pid_buf), "%ld\n", (long)getpid());
+
+ int fd = open(PID_FILE_PATH, PID_FILE_FLAGS, PID_FILE_MODE);
+ if (fd == -1) {
+ ALOGE("Unable to create pid file (%s)", strerror(errno));
+ return false;
+ }
+
+ // File creation is affected by umask, so make sure the right mode bits are set.
+ if (fchmod(fd, PID_FILE_MODE) == -1) {
+ ALOGE("failed to set mode 0%o on %s (%s)", PID_FILE_MODE, PID_FILE_PATH, strerror(errno));
+ close(fd);
+ remove_pid_file();
+ return false;
+ }
+
+ if (write(fd, pid_buf, strlen(pid_buf)) != (ssize_t)strlen(pid_buf)) {
+ ALOGE("Unable to write to pid file (%s)", strerror(errno));
+ close(fd);
+ remove_pid_file();
+ return false;
+ }
+ close(fd);
+ return true;
+}
+
+static void remove_pid_file() {
+ unlink(PID_FILE_PATH);
+}
+
static void blockSigpipe()
{
sigset_t mask;