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 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 13 | #include <syslog.h> |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 14 | |
| 15 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | |
| 17 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 18 | long uptime(void) |
| 19 | { |
| 20 | struct sysinfo info; |
| 21 | sysinfo(&info); |
| 22 | return info.uptime; |
| 23 | } |
| 24 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 25 | /* |
| 26 | * This function makes sure our first socket calls |
| 27 | * aren't going to fd 1 (printf badness...) and are |
| 28 | * not later closed by daemon() |
| 29 | */ |
| 30 | static inline void sanitize_fds(void) |
| 31 | { |
Denis Vlasenko | 61126ab | 2006-11-18 22:03:26 +0000 | [diff] [blame] | 32 | int fd = xopen(bb_dev_null, O_RDWR); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 33 | while (fd < 3) |
| 34 | fd = dup(fd); |
| 35 | close(fd); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 39 | void udhcp_background(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 40 | { |
| 41 | #ifdef __uClinux__ |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 42 | bb_error_msg("cannot background in uclinux (yet)"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 43 | #else /* __uClinux__ */ |
| 44 | int pid_fd; |
| 45 | |
| 46 | /* hold lock during fork. */ |
| 47 | pid_fd = pidfile_acquire(pidfile); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 48 | setsid(); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 49 | xdaemon(0, 0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 50 | logmode &= ~LOGMODE_STDIO; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 51 | pidfile_write_release(pid_fd); |
| 52 | #endif /* __uClinux__ */ |
| 53 | } |
| 54 | |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 55 | void udhcp_start_log_and_pid(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 56 | { |
| 57 | int pid_fd; |
| 58 | |
| 59 | /* Make sure our syslog fd isn't overwritten */ |
| 60 | sanitize_fds(); |
| 61 | |
| 62 | /* do some other misc startup stuff while we are here to save bytes */ |
| 63 | pid_fd = pidfile_acquire(pidfile); |
| 64 | pidfile_write_release(pid_fd); |
| 65 | |
| 66 | /* equivelent of doing a fflush after every \n */ |
| 67 | setlinebuf(stdout); |
| 68 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 69 | if (ENABLE_FEATURE_UDHCP_SYSLOG) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 70 | openlog(applet_name, LOG_PID, LOG_LOCAL0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 71 | logmode |= LOGMODE_SYSLOG; |
| 72 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 74 | bb_info_msg("%s (v%s) started", applet_name, BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 75 | } |