X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=networking%2Fudhcp%2Fpidfile.c;h=d656c7ad0ee2e85a8588d15b404a914450f3db72;hb=a8992635b117723e65078ada845cc88a9cb77aa3;hp=246a64aa1fc2ae4dc917853f86fb0745730effde;hpb=61fb48930f45aa536584b2047f9e703186e8f69f;p=oweals%2Fbusybox.git diff --git a/networking/udhcp/pidfile.c b/networking/udhcp/pidfile.c index 246a64aa1..d656c7ad0 100644 --- a/networking/udhcp/pidfile.c +++ b/networking/udhcp/pidfile.c @@ -23,25 +23,35 @@ #include #include #include -#include -#include #include +#include -#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); -}