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);
}
}