httpd: add link to docs
[oweals/busybox.git] / networking / udhcp / common.c
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 <syslog.h>
14
15 #include "common.h"
16
17
18 long uptime(void)
19 {
20         struct sysinfo info;
21         sysinfo(&info);
22         return info.uptime;
23 }
24
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 {
32         int fd = xopen(bb_dev_null, O_RDWR);
33         while (fd < 3)
34                 fd = dup(fd);
35         close(fd);
36 }
37
38
39 void udhcp_background(const char *pidfile)
40 {
41 #ifdef __uClinux__
42         bb_error_msg("cannot background in uclinux (yet)");
43 #else /* __uClinux__ */
44         int pid_fd;
45
46         /* hold lock during fork. */
47         pid_fd = pidfile_acquire(pidfile);
48         setsid();
49         xdaemon(0, 0);
50         logmode &= ~LOGMODE_STDIO;
51         pidfile_write_release(pid_fd);
52 #endif /* __uClinux__ */
53 }
54
55 void udhcp_start_log_and_pid(const char *pidfile)
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
69         if (ENABLE_FEATURE_UDHCP_SYSLOG) {
70                 openlog(applet_name, LOG_PID, LOG_LOCAL0);
71                 logmode |= LOGMODE_SYSLOG;
72         }
73
74         bb_info_msg("%s (v%s) started", applet_name, BB_VER);
75 }