summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTrond Norbye <trond.norbye@gmail.com>2010-11-12 09:59:06 -0800
committerTrond Norbye <trond.norbye@gmail.com>2010-11-12 10:33:02 -0800
commitaf48b1005639d3cb1dea4e08dd9d2e33a2289a95 (patch)
treed3b66234ee15b8b2db99aa12eb6d6bfbda2a1af6
parentefad616d1cd8bf8eb5d6e7573a1952cacb3073ee (diff)
downloadmemcached-af48b1005639d3cb1dea4e08dd9d2e33a2289a95.tar.gz
Issue 154: pid file out of sync (created before socket binding)
-rw-r--r--memcached.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/memcached.c b/memcached.c
index ec20fd4..149edd1 100644
--- a/memcached.c
+++ b/memcached.c
@@ -4197,20 +4197,29 @@ static void usage_license(void) {
return;
}
-static void save_pid(const pid_t pid, const char *pid_file) {
+static void save_pid(const char *pid_file) {
FILE *fp;
- if (pid_file == NULL)
- return;
+ if (access(pid_file, F_OK) == 0) {
+ if ((fp = fopen(pid_file, "r")) != NULL) {
+ char buffer[1024];
+ if (fgets(buffer, sizeof(buffer), fp) != NULL) {
+ unsigned int pid;
+ if (safe_strtoul(buffer, &pid) && kill((pid_t)pid, 0) == 0) {
+ fprintf(stderr, "WARNING: The pid file contained the following (running) pid: %u\n", pid);
+ }
+ }
+ fclose(fp);
+ }
+ }
if ((fp = fopen(pid_file, "w")) == NULL) {
vperror("Could not open the pid file %s for writing", pid_file);
return;
}
- fprintf(fp,"%ld\n", (long)pid);
+ fprintf(fp,"%ld\n", (long)getpid());
if (fclose(fp) == -1) {
vperror("Could not close the pid file %s", pid_file);
- return;
}
}
@@ -4664,15 +4673,11 @@ int main (int argc, char **argv) {
}
/* start up worker threads if MT mode */
thread_init(settings.num_threads, main_base);
- /* save the PID in if we're a daemon, do this after thread_init due to
- a file descriptor handling bug somewhere in libevent */
if (start_assoc_maintenance_thread() == -1) {
exit(EXIT_FAILURE);
}
- if (do_daemonize)
- save_pid(getpid(), pid_file);
/* initialise clock event */
clock_handler(0, 0, 0);
@@ -4734,6 +4739,10 @@ int main (int argc, char **argv) {
}
}
+ if (pid_file != NULL) {
+ save_pid(pid_file);
+ }
+
/* Drop privileges no longer needed */
drop_privileges();