s3: Fix the clustering build
[mat/samba.git] / source4 / smbd / pidfile.c
index 4847ddd7b5253d1329cd1316dfa41efa9d0455f7..b7d1c27cd0f0a99fcff0bf0925f42903c970e15e 100644 (file)
@@ -21,7 +21,6 @@
 
 #include "includes.h"
 #include "system/filesys.h"
-#include "param/param.h"
 #include "smbd/pidfile.h"
 
 /**
  * return the pid in a pidfile. return 0 if the process (or pidfile)
  * does not exist 
  */
-pid_t pidfile_pid(const char *name)
+pid_t pidfile_pid(const char *piddir, const char *name)
 {
        int fd;
        char pidstr[20];
        pid_t ret;
        char *pidFile;
 
-       asprintf(&pidFile, "%s/%s.pid", lp_piddir(global_loadparm), name);
+       if (asprintf(&pidFile, "%s/%s.pid", piddir, name) < 0) {
+               return 0;
+       }
 
        fd = open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
 
@@ -57,7 +58,7 @@ pid_t pidfile_pid(const char *name)
 
        ret = (pid_t)atoi(pidstr);
        
-       if (!process_exists(ret)) {
+       if (!process_exists_by_pid(ret)) {
                goto noproc;
        }
 
@@ -80,16 +81,19 @@ pid_t pidfile_pid(const char *name)
 /**
  * create a pid file in the pid directory. open it and leave it locked 
  */
-void pidfile_create(const char *name)
+void pidfile_create(const char *piddir, const char *name)
 {
        int     fd;
        char    buf[20];
        char *pidFile;
        pid_t pid;
 
-       asprintf(&pidFile, "%s/%s.pid", lp_piddir(global_loadparm), name);
+       if (asprintf(&pidFile, "%s/%s.pid", piddir, name) < 0) {
+               DEBUG(0,("ERROR: Out of memory\n"));
+               exit(1);
+       }
 
-       pid = pidfile_pid(name);
+       pid = pidfile_pid(piddir, 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));
@@ -103,6 +107,8 @@ void pidfile_create(const char *name)
                exit(1);
        }
 
+       smb_set_close_on_exec(fd);
+
        if (fcntl_lock(fd,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)));