Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* common.c |
| 3 | * |
| 4 | * Functions for debugging and logging as well as some other |
| 5 | * simple helper functions. |
| 6 | * |
| 7 | * Russ Dill <Russ.Dill@asu.edu> 2001-2003 |
| 8 | * Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003 |
| 9 | * |
| 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 11 | */ |
| 12 | |
| 13 | #include <fcntl.h> |
| 14 | #include <unistd.h> |
| 15 | #include <errno.h> |
| 16 | #include <string.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <signal.h> |
| 19 | #include <paths.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <stdarg.h> |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 22 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 23 | |
| 24 | #include "common.h" |
| 25 | #include "pidfile.h" |
| 26 | |
| 27 | |
| 28 | static int daemonized; |
| 29 | |
| 30 | long uptime(void) |
| 31 | { |
| 32 | struct sysinfo info; |
| 33 | sysinfo(&info); |
| 34 | return info.uptime; |
| 35 | } |
| 36 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | /* |
| 38 | * This function makes sure our first socket calls |
| 39 | * aren't going to fd 1 (printf badness...) and are |
| 40 | * not later closed by daemon() |
| 41 | */ |
| 42 | static inline void sanitize_fds(void) |
| 43 | { |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 44 | int fd = open(bb_dev_null, O_RDWR, 0); |
| 45 | if (fd < 0) |
Bernhard Reutner-Fischer | 0a8812b | 2006-05-19 13:12:21 +0000 | [diff] [blame] | 46 | return; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 47 | while (fd < 3) |
| 48 | fd = dup(fd); |
| 49 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 53 | void udhcp_background(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 54 | { |
| 55 | #ifdef __uClinux__ |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 56 | bb_error_msg("Cannot background in uclinux (yet)"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | #else /* __uClinux__ */ |
| 58 | int pid_fd; |
| 59 | |
| 60 | /* hold lock during fork. */ |
| 61 | pid_fd = pidfile_acquire(pidfile); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 62 | setsid(); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 63 | xdaemon(0, 0); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | daemonized++; |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 65 | logmode &= ~LOGMODE_STDIO; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 66 | pidfile_write_release(pid_fd); |
| 67 | #endif /* __uClinux__ */ |
| 68 | } |
| 69 | |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 70 | void udhcp_start_log_and_pid(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 71 | { |
| 72 | int pid_fd; |
| 73 | |
| 74 | /* Make sure our syslog fd isn't overwritten */ |
| 75 | sanitize_fds(); |
| 76 | |
| 77 | /* do some other misc startup stuff while we are here to save bytes */ |
| 78 | pid_fd = pidfile_acquire(pidfile); |
| 79 | pidfile_write_release(pid_fd); |
| 80 | |
| 81 | /* equivelent of doing a fflush after every \n */ |
| 82 | setlinebuf(stdout); |
| 83 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 84 | if (ENABLE_FEATURE_UDHCP_SYSLOG) { |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 85 | openlog(bb_applet_name, LOG_PID, LOG_LOCAL0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 86 | logmode |= LOGMODE_SYSLOG; |
| 87 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 89 | bb_info_msg("%s (v%s) started", bb_applet_name, BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 90 | } |