Implement optional syslog logging using ordinary
[oweals/busybox.git] / networking / udhcp / pidfile.c
index 246a64aa1fc2ae4dc917853f86fb0745730effde..148b07b342a42ff67457b9cc5ab1cda4469174bc 100644 (file)
@@ -1,8 +1,9 @@
+/* vi: set sw=4 ts=4: */
 /* pidfile.c
  *
  * Functions to assist in the writing and removing of pidfiles.
  *
- * Russ Dill <Russ.Dill@asu.edu> Soptember 2001
+ * Russ Dill <Russ.Dill@asu.edu> September 2001
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #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 "common.h"
+#include "pidfile.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));
+               bb_perror_msg("Unable to open pidfile %s", pidfile);
        } else {
                lockf(pid_fd, F_LOCK, 0);
+               if (!saved_pidfile)
+                       atexit(pidfile_delete);
+               saved_pidfile = (char *) pidfile;
        }
-       
+
        return pid_fd;
 }
 
@@ -61,9 +72,5 @@ void pidfile_write_release(int pid_fd)
 }
 
 
-void pidfile_delete(char *pidfile)
-{
-       if (pidfile) unlink(pidfile);
-}