]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
msgr: remove pid file on SIGINT, SIGTERM
authorSage Weil <sage@newdream.net>
Thu, 29 Apr 2010 19:50:34 +0000 (12:50 -0700)
committerSage Weil <sage@newdream.net>
Thu, 29 Apr 2010 21:29:23 +0000 (14:29 -0700)
src/msg/SimpleMessenger.cc

index 170b294d53fc3ca7db231c3f2dd1110cddfd70d9..c6998d9ad22af4c3a06fd5474312d61e5430b2e7 100644 (file)
@@ -2088,39 +2088,42 @@ int SimpleMessenger::bind(int64_t force_nonce)
   return accepter.bind(force_nonce);
 }
 
-static void write_pid_file(int pid)
+static void remove_pid_file(int signal = 0)
 {
   if (!g_conf.pid_file)
     return;
 
-  int fd = ::open(g_conf.pid_file, O_CREAT|O_TRUNC|O_WRONLY, 0644);
+  // only remove it if it has OUR pid in it!
+  int fd = ::open(g_conf.pid_file, O_RDONLY);
   if (fd >= 0) {
     char buf[20];
-    int len = snprintf(buf, sizeof(buf), "%d\n", pid);
-    ::write(fd, buf, len);
+    ::read(fd, buf, 20);
     ::close(fd);
+    int a = atoi(buf);
+
+    if (a == getpid())
+      ::unlink(g_conf.pid_file);
+    else if (!signal)
+      generic_dout(0) << "strange, pid file " << g_conf.pid_file 
+             << " has " << a << ", not expected " << getpid()
+             << dendl;
   }
 }
 
-static void remove_pid_file()
+static void write_pid_file(int pid)
 {
   if (!g_conf.pid_file)
     return;
 
-  // only remove it if it has OUR pid in it!
-  int fd = ::open(g_conf.pid_file, O_RDONLY);
+  int fd = ::open(g_conf.pid_file, O_CREAT|O_TRUNC|O_WRONLY, 0644);
   if (fd >= 0) {
     char buf[20];
-    ::read(fd, buf, 20);
+    int len = snprintf(buf, sizeof(buf), "%d\n", pid);
+    ::write(fd, buf, len);
     ::close(fd);
-    int a = atoi(buf);
 
-    if (a == getpid())
-      ::unlink(g_conf.pid_file);
-    else
-      generic_dout(0) << "strange, pid file " << g_conf.pid_file 
-             << " has " << a << ", not expected " << getpid()
-             << dendl;
+    signal(SIGTERM, remove_pid_file);
+    signal(SIGINT, remove_pid_file);
   }
 }