diff options
Diffstat (limited to 'networking/udhcp/common.c')
-rw-r--r-- | networking/udhcp/common.c | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c index 3704ba7..7b2e19c 100644 --- a/networking/udhcp/common.c +++ b/networking/udhcp/common.c @@ -22,40 +22,56 @@ long uptime(void) return info.uptime; } -void udhcp_background(const char *pidfile) + +static const char *saved_pidfile; + +static void pidfile_delete(void) +{ + if (saved_pidfile) + unlink(saved_pidfile); +} + +static int pidfile_acquire(const char *pidfile) { -#ifdef __uClinux__ - bb_error_msg("cannot background in uclinux (yet)"); -#else /* __uClinux__ */ int pid_fd; + if (!pidfile) return -1; - /* hold lock during fork. */ - pid_fd = pidfile_acquire(pidfile); - setsid(); - xdaemon(0, 0); - logmode &= ~LOGMODE_STDIO; - pidfile_write_release(pid_fd); -#endif /* __uClinux__ */ + pid_fd = open(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644); + if (pid_fd < 0) { + bb_perror_msg("cannot open pidfile %s", pidfile); + } else { + /* lockf(pid_fd, F_LOCK, 0); */ + if (!saved_pidfile) + atexit(pidfile_delete); + saved_pidfile = pidfile; + } + + return pid_fd; +} + +static void pidfile_write_release(int pid_fd) +{ + if (pid_fd < 0) return; + + fdprintf(pid_fd, "%d\n", getpid()); + /* lockf(pid_fd, F_UNLCK, 0); */ + close(pid_fd); } -void udhcp_start_log_and_pid(const char *pidfile) + +void udhcp_make_pidfile(const char *pidfile) { int pid_fd; - /* Make sure our syslog fd isn't overwritten */ + /* Make sure fd 0,1,2 are open */ bb_sanitize_stdio(); - /* do some other misc startup stuff while we are here to save bytes */ - pid_fd = pidfile_acquire(pidfile); - pidfile_write_release(pid_fd); - - /* equivelent of doing a fflush after every \n */ + /* Equivalent of doing a fflush after every \n */ setlinebuf(stdout); - if (ENABLE_FEATURE_UDHCP_SYSLOG) { - openlog(applet_name, LOG_PID, LOG_LOCAL0); - logmode |= LOGMODE_SYSLOG; - } + /* Create pidfile */ + pid_fd = pidfile_acquire(pidfile); + pidfile_write_release(pid_fd); bb_info_msg("%s (v%s) started", applet_name, BB_VER); } |