r23595: One pstring a day...
authorVolker Lendecke <vlendec@samba.org>
Sun, 24 Jun 2007 13:40:58 +0000 (13:40 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:23:34 +0000 (12:23 -0500)
source/lib/pidfile.c

index 89ab6d799b7f332bfe3c2c18848a5ae4ed3b6f6f..9dc4f2f1869c21a1b8ac58f7d0d45890c98d3caa 100644 (file)
@@ -34,12 +34,15 @@ pid_t pidfile_pid(const char *name)
        char pidstr[20];
        pid_t pid;
        unsigned int ret;
-       pstring pidFile;
+       char * pidFile;
 
-       slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name);
+       if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
+               return 0;
+       }
 
        fd = sys_open(pidFile, O_NONBLOCK | O_RDONLY, 0644);
        if (fd == -1) {
+               SAFE_FREE(pidFile);
                return 0;
        }
 
@@ -68,12 +71,14 @@ pid_t pidfile_pid(const char *name)
                goto noproc;
        }
 
+       SAFE_FREE(pidFile);
        close(fd);
        return (pid_t)ret;
 
  noproc:
        close(fd);
        unlink(pidFile);
+       SAFE_FREE(pidFile);
        return 0;
 }
 
@@ -83,14 +88,14 @@ void pidfile_create(const char *program_name)
        int     fd;
        char    buf[20];
        char    *short_configfile;
-       pstring name;
-       pstring pidFile;
+       char *name;
+       char *pidFile;
        pid_t pid;
 
        /* Add a suffix to the program name if this is a process with a
         * none default configuration file name. */
        if (strcmp( CONFIGFILE, dyn_CONFIGFILE) == 0) {
-               strncpy( name, program_name, sizeof( name)-1);
+               name = SMB_STRDUP(program_name);
        } else {
                short_configfile = strrchr( dyn_CONFIGFILE, '/');
                if (short_configfile == NULL) {
@@ -100,10 +105,15 @@ void pidfile_create(const char *program_name)
                        /* full/relative path provided */
                        short_configfile++;
                }
-               slprintf( name, sizeof( name)-1, "%s-%s", program_name, short_configfile+1);
+               if (asprintf(&name, "%s-%s", program_name,
+                            short_configfile+1) == -1) {
+                       smb_panic("asprintf failed");
+               }
        }
 
-       slprintf(pidFile, sizeof(pidFile)-1, "%s/%s.pid", lp_piddir(), name);
+       if (asprintf(&pidFile, "%s/%s.pid", lp_piddir(), name) == -1) {
+               smb_panic("asprintf failed");
+       }
 
        pid = pidfile_pid(name);
        if (pid != 0) {
@@ -133,4 +143,6 @@ void pidfile_create(const char *program_name)
                exit(1);
        }
        /* Leave pid file open & locked for the duration... */
+       SAFE_FREE(name);
+       SAFE_FREE(pidFile);
 }