Fix bug 4699: Remove pidfile on clean shutdown
authorVolker Lendecke <vl@samba.org>
Thu, 18 Jun 2009 09:45:57 +0000 (11:45 +0200)
committerJeremy Allison <jra@samba.org>
Thu, 18 Jun 2009 23:17:57 +0000 (16:17 -0700)
source3/include/proto.h
source3/lib/pidfile.c
source3/nmbd/nmbd.c
source3/smbd/server.c
source3/winbindd/winbindd.c

index 598d83aee6065b8596a4433647af603521bf7071..74406fdeac7a5a560c66a54ae17c7295cd20f77a 100644 (file)
@@ -583,6 +583,7 @@ int nt_status_to_pam(NTSTATUS nt_status);
 
 pid_t pidfile_pid(const char *name);
 void pidfile_create(const char *program_name);
+void pidfile_unlink(void);
 
 /* The following definitions come from lib/popt_common.c  */
 
index 3495dae5c284745aa2ff1851d672aaeea87a1467..37b36af27717216d195006640b76003431d0ca45 100644 (file)
@@ -25,6 +25,8 @@
 #define O_NONBLOCK
 #endif
 
+static char *pidFile_name = NULL;
+
 /* return the pid in a pidfile. return 0 if the process (or pidfile)
    does not exist */
 pid_t pidfile_pid(const char *name)
@@ -88,7 +90,6 @@ void pidfile_create(const char *program_name)
        char    buf[20];
        const char    *short_configfile;
        char *name;
-       char *pidFile;
        pid_t pid;
 
        /* Add a suffix to the program name if this is a process with a
@@ -110,27 +111,28 @@ void pidfile_create(const char *program_name)
                }
        }
 
-       if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
+       if (asprintf(&pidFile_name, "%s/%s.pid", lp_piddir(), name) == -1) {
                smb_panic("asprintf failed");
        }
 
        pid = pidfile_pid(name);
        if (pid != 0) {
                DEBUG(0,("ERROR: %s is already running. File %s exists and process id %d is running.\n", 
-                        name, pidFile, (int)pid));
+                        name, pidFile_name, (int)pid));
                exit(1);
        }
 
-       fd = sys_open(pidFile, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL, 0644);
+       fd = sys_open(pidFile_name, O_NONBLOCK | O_CREAT | O_WRONLY | O_EXCL,
+                     0644);
        if (fd == -1) {
-               DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile
+               DEBUG(0,("ERROR: can't open %s: Error was %s\n", pidFile_name,
                         strerror(errno)));
                exit(1);
        }
 
        if (fcntl_lock(fd,SMB_F_SETLK,0,1,F_WRLCK)==False) {
                DEBUG(0,("ERROR: %s : fcntl lock of file %s failed. Error was %s\n",  
-              name, pidFile, strerror(errno)));
+                        name, pidFile_name, strerror(errno)));
                exit(1);
        }
 
@@ -138,10 +140,18 @@ void pidfile_create(const char *program_name)
        slprintf(buf, sizeof(buf) - 1, "%u\n", (unsigned int) sys_getpid());
        if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf)) {
                DEBUG(0,("ERROR: can't write to file %s: %s\n", 
-                        pidFile, strerror(errno)));
+                        pidFile_name, strerror(errno)));
                exit(1);
        }
        /* Leave pid file open & locked for the duration... */
        SAFE_FREE(name);
-       SAFE_FREE(pidFile);
+}
+
+void pidfile_unlink(void)
+{
+       if (pidFile_name == NULL) {
+               return;
+       }
+       unlink(pidFile_name);
+       SAFE_FREE(pidFile_name);
 }
index 903dc36d533d5c61cf0a0e2faeede076abf94ac8..848baeff3dbd9f66c12678703b66c9510a31f62d 100644 (file)
@@ -82,6 +82,8 @@ static void terminate(void)
        /* If there was an async dns child - kill it. */
        kill_async_dns_child();
 
+       pidfile_unlink();
+
        exit(0);
 }
 
index d3ce4b6f2d7b0162e10025df2cc6c281247f0e23..5b474d84b440634a1a69163ce1e01af9e252ed95 100644 (file)
@@ -854,6 +854,9 @@ static void exit_server_common(enum server_exit_reason how,
        } else {    
                DEBUG(3,("Server exit (%s)\n",
                        (reason ? reason : "normal exit")));
+               if (am_parent) {
+                       pidfile_unlink();
+               }
        }
 
        /* if we had any open SMB connections when we exited then we
index 0a73c0ebc6c26c02066e220db49e762a011f60b8..d617fe1f0b5c0fa9d15ed96221a31a93a4d1e774 100644 (file)
@@ -173,6 +173,10 @@ static void terminate(bool is_parent)
        }
 #endif
 
+       if (is_parent) {
+               pidfile_unlink();
+       }
+
        exit(0);
 }