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 | |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 25 | void udhcp_background(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 26 | { |
| 27 | #ifdef __uClinux__ |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 28 | bb_error_msg("cannot background in uclinux (yet)"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 29 | #else /* __uClinux__ */ |
| 30 | int pid_fd; |
| 31 | |
| 32 | /* hold lock during fork. */ |
| 33 | pid_fd = pidfile_acquire(pidfile); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 34 | setsid(); |
Denis Vlasenko | 27af5a0 | 2006-09-03 12:21:59 +0000 | [diff] [blame] | 35 | xdaemon(0, 0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 36 | logmode &= ~LOGMODE_STDIO; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | pidfile_write_release(pid_fd); |
| 38 | #endif /* __uClinux__ */ |
| 39 | } |
| 40 | |
Denis Vlasenko | 239369b | 2006-09-07 17:05:44 +0000 | [diff] [blame] | 41 | void udhcp_start_log_and_pid(const char *pidfile) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 42 | { |
| 43 | int pid_fd; |
| 44 | |
| 45 | /* Make sure our syslog fd isn't overwritten */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 46 | bb_sanitize_stdio(); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 47 | |
| 48 | /* do some other misc startup stuff while we are here to save bytes */ |
| 49 | pid_fd = pidfile_acquire(pidfile); |
| 50 | pidfile_write_release(pid_fd); |
| 51 | |
| 52 | /* equivelent of doing a fflush after every \n */ |
| 53 | setlinebuf(stdout); |
| 54 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 55 | if (ENABLE_FEATURE_UDHCP_SYSLOG) { |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 56 | openlog(applet_name, LOG_PID, LOG_LOCAL0); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 57 | logmode |= LOGMODE_SYSLOG; |
| 58 | } |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 59 | |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 60 | bb_info_msg("%s (v%s) started", applet_name, BB_VER); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 61 | } |