diff options
author | Mike Frysinger <vapier@gentoo.org> | 2006-05-08 03:20:50 +0000 |
---|---|---|
committer | Mike Frysinger <vapier@gentoo.org> | 2006-05-08 03:20:50 +0000 |
commit | 7031f62d9b750568b5e98bdb8c59c3c1a72e073d (patch) | |
tree | b8d037a539281e7f7592e3045fa59e445495f603 /networking/udhcp/common.c | |
parent | 15fe2e11d7886d04450cabc8b40f0d396b6b6d85 (diff) | |
download | busybox-w32-7031f62d9b750568b5e98bdb8c59c3c1a72e073d.tar.gz busybox-w32-7031f62d9b750568b5e98bdb8c59c3c1a72e073d.tar.bz2 busybox-w32-7031f62d9b750568b5e98bdb8c59c3c1a72e073d.zip |
add back in udhcp support
Diffstat (limited to 'networking/udhcp/common.c')
-rw-r--r-- | networking/udhcp/common.c | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/networking/udhcp/common.c b/networking/udhcp/common.c new file mode 100644 index 000000000..f36009a1c --- /dev/null +++ b/networking/udhcp/common.c | |||
@@ -0,0 +1,135 @@ | |||
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> | ||
22 | |||
23 | #include "common.h" | ||
24 | #include "pidfile.h" | ||
25 | |||
26 | |||
27 | static int daemonized; | ||
28 | |||
29 | long uptime(void) | ||
30 | { | ||
31 | struct sysinfo info; | ||
32 | sysinfo(&info); | ||
33 | return info.uptime; | ||
34 | } | ||
35 | |||
36 | |||
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 | { | ||
44 | int zero; | ||
45 | if ((zero = open(_PATH_DEVNULL, O_RDWR, 0)) < 0) return; | ||
46 | while (zero < 3) zero = dup(zero); | ||
47 | close(zero); | ||
48 | } | ||
49 | |||
50 | |||
51 | void background(const char *pidfile) | ||
52 | { | ||
53 | #ifdef __uClinux__ | ||
54 | LOG(LOG_ERR, "Cannot background in uclinux (yet)"); | ||
55 | #else /* __uClinux__ */ | ||
56 | int pid_fd; | ||
57 | |||
58 | /* hold lock during fork. */ | ||
59 | pid_fd = pidfile_acquire(pidfile); | ||
60 | if (daemon(0, 0) == -1) { /* bb_xdaemon? */ | ||
61 | perror("fork"); | ||
62 | exit(1); | ||
63 | } | ||
64 | daemonized++; | ||
65 | pidfile_write_release(pid_fd); | ||
66 | #endif /* __uClinux__ */ | ||
67 | } | ||
68 | |||
69 | |||
70 | #ifdef UDHCP_SYSLOG | ||
71 | |||
72 | void udhcp_logging(int level, const char *fmt, ...) | ||
73 | { | ||
74 | va_list p; | ||
75 | va_list p2; | ||
76 | |||
77 | va_start(p, fmt); | ||
78 | __va_copy(p2, p); | ||
79 | if(!daemonized) { | ||
80 | vprintf(fmt, p); | ||
81 | putchar('\n'); | ||
82 | } | ||
83 | vsyslog(level, fmt, p2); | ||
84 | va_end(p); | ||
85 | } | ||
86 | |||
87 | #else | ||
88 | |||
89 | |||
90 | static char *syslog_level_msg[] = { | ||
91 | [LOG_EMERG] = "EMERGENCY!", | ||
92 | [LOG_ALERT] = "ALERT!", | ||
93 | [LOG_CRIT] = "critical!", | ||
94 | [LOG_WARNING] = "warning", | ||
95 | [LOG_ERR] = "error", | ||
96 | [LOG_INFO] = "info", | ||
97 | [LOG_DEBUG] = "debug" | ||
98 | }; | ||
99 | |||
100 | |||
101 | void udhcp_logging(int level, const char *fmt, ...) | ||
102 | { | ||
103 | va_list p; | ||
104 | |||
105 | va_start(p, fmt); | ||
106 | if(!daemonized) { | ||
107 | printf("%s, ", syslog_level_msg[level]); | ||
108 | vprintf(fmt, p); | ||
109 | putchar('\n'); | ||
110 | } | ||
111 | va_end(p); | ||
112 | } | ||
113 | #endif | ||
114 | |||
115 | |||
116 | void start_log_and_pid(const char *client_server, const char *pidfile) | ||
117 | { | ||
118 | int pid_fd; | ||
119 | |||
120 | /* Make sure our syslog fd isn't overwritten */ | ||
121 | sanitize_fds(); | ||
122 | |||
123 | /* do some other misc startup stuff while we are here to save bytes */ | ||
124 | pid_fd = pidfile_acquire(pidfile); | ||
125 | pidfile_write_release(pid_fd); | ||
126 | |||
127 | /* equivelent of doing a fflush after every \n */ | ||
128 | setlinebuf(stdout); | ||
129 | |||
130 | #ifdef UDHCP_SYSLOG | ||
131 | openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0); | ||
132 | #endif | ||
133 | |||
134 | udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION); | ||
135 | } | ||