blob: 3704ba71a97617ece4a505d5c54f661723452c80 [file] [log] [blame]
Mike Frysinger7031f622006-05-08 03:20:50 +00001/* 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 Vlasenko3538b9a2006-09-06 18:36:50 +000013#include <syslog.h>
Mike Frysinger7031f622006-05-08 03:20:50 +000014
15#include "common.h"
Mike Frysinger7031f622006-05-08 03:20:50 +000016
17
Mike Frysinger7031f622006-05-08 03:20:50 +000018long uptime(void)
19{
20 struct sysinfo info;
21 sysinfo(&info);
22 return info.uptime;
23}
24
Rob Landley3f785612006-05-28 01:06:36 +000025void udhcp_background(const char *pidfile)
Mike Frysinger7031f622006-05-08 03:20:50 +000026{
27#ifdef __uClinux__
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +000028 bb_error_msg("cannot background in uclinux (yet)");
Mike Frysinger7031f622006-05-08 03:20:50 +000029#else /* __uClinux__ */
30 int pid_fd;
31
32 /* hold lock during fork. */
33 pid_fd = pidfile_acquire(pidfile);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000034 setsid();
Denis Vlasenko27af5a02006-09-03 12:21:59 +000035 xdaemon(0, 0);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000036 logmode &= ~LOGMODE_STDIO;
Mike Frysinger7031f622006-05-08 03:20:50 +000037 pidfile_write_release(pid_fd);
38#endif /* __uClinux__ */
39}
40
Denis Vlasenko239369b2006-09-07 17:05:44 +000041void udhcp_start_log_and_pid(const char *pidfile)
Mike Frysinger7031f622006-05-08 03:20:50 +000042{
43 int pid_fd;
44
45 /* Make sure our syslog fd isn't overwritten */
Denis Vlasenkoe06bed32007-01-27 22:21:12 +000046 bb_sanitize_stdio();
Mike Frysinger7031f622006-05-08 03:20:50 +000047
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 Vlasenko3538b9a2006-09-06 18:36:50 +000055 if (ENABLE_FEATURE_UDHCP_SYSLOG) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000056 openlog(applet_name, LOG_PID, LOG_LOCAL0);
Denis Vlasenko3538b9a2006-09-06 18:36:50 +000057 logmode |= LOGMODE_SYSLOG;
58 }
Mike Frysinger7031f622006-05-08 03:20:50 +000059
Denis Vlasenko8f8f2682006-10-03 21:00:43 +000060 bb_info_msg("%s (v%s) started", applet_name, BB_VER);
Mike Frysinger7031f622006-05-08 03:20:50 +000061}