move debugging to safe place (before vfork)
[oweals/busybox.git] / networking / udhcp / pidfile.c
index 246a64aa1fc2ae4dc917853f86fb0745730effde..d656c7ad0ee2e85a8588d15b404a914450f3db72 100644 (file)
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
-#include <errno.h>
-#include <string.h>
 #include <stdio.h>
+#include <stdlib.h>
 
-#include "debug.h"
+#include "pidfile.h"
+#include "common.h"
 
-int pidfile_acquire(char *pidfile)
+static char *saved_pidfile;
+
+static void pidfile_delete(void)
+{
+       if (saved_pidfile) unlink(saved_pidfile);
+}
+
+
+int pidfile_acquire(const char *pidfile)
 {
        int pid_fd;
-       if (pidfile == NULL) return -1;
+       if (!pidfile) return -1;
 
        pid_fd = open(pidfile, O_CREAT | O_WRONLY, 0644);
        if (pid_fd < 0) {
-               LOG(LOG_ERR, "Unable to open pidfile %s: %s\n",
-                   pidfile, strerror(errno));
+               LOG(LOG_ERR, "Unable to open pidfile %s: %m\n", pidfile);
        } else {
                lockf(pid_fd, F_LOCK, 0);
+               if (!saved_pidfile)
+                       atexit(pidfile_delete);
+               saved_pidfile = (char *) pidfile;
        }
-       
+
        return pid_fd;
 }
 
@@ -61,9 +71,5 @@ void pidfile_write_release(int pid_fd)
 }
 
 
-void pidfile_delete(char *pidfile)
-{
-       if (pidfile) unlink(pidfile);
-}