Larry Doolittle writes:
[platform/upstream/busybox.git] / networking / udhcp / common.c
1 /* common.c
2  *
3  * Functions for debugging and logging as well as some other
4  * simple helper functions.
5  *
6  * Russ Dill <Russ.Dill@asu.edu> 2001-2003
7  * Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <signal.h>
30 #include <paths.h>
31 #include <sys/socket.h>
32 #include <stdarg.h>
33
34 #include "common.h"
35 #include "pidfile.h"
36
37
38 static int daemonized;
39
40
41 /*
42  * This function makes sure our first socket calls
43  * aren't going to fd 1 (printf badness...) and are
44  * not later closed by daemon()
45  */
46 static inline void sanitize_fds(void)
47 {
48         int zero;
49         if ((zero = open(_PATH_DEVNULL, O_RDWR, 0)) < 0) return;
50         while (zero < 3) zero = dup(zero);
51         close(zero);
52 }
53
54
55 void background(const char *pidfile)
56 {
57 #ifdef __uClinux__
58         LOG(LOG_ERR, "Cannot background in uclinux (yet)");
59 #else /* __uClinux__ */
60         int pid_fd;
61
62         /* hold lock during fork. */
63         if (pidfile) pid_fd = pidfile_acquire(pidfile);
64         if (daemon(0, 0) == -1) {
65                 perror("fork");
66                 exit(1);
67         }
68         daemonized++;
69         if (pidfile) pidfile_write_release(pid_fd);
70 #endif /* __uClinux__ */
71 }
72
73
74 #ifdef UDHCP_SYSLOG
75 void udhcp_logging(int level, const char *fmt, ...)
76 {
77         va_list p;
78         va_list p2;
79
80         va_start(p, fmt);
81         __va_copy(p2, p);
82         if(!daemonized) {
83                 vprintf(fmt, p);
84                 putchar('\n');
85         }
86         vsyslog(level, fmt, p2);
87         va_end(p);
88 }
89
90
91 void start_log_and_pid(const char *client_server, const char *pidfile)
92 {
93         int pid_fd;
94
95         /* Make sure our syslog fd isn't overwritten */
96         sanitize_fds();
97
98         /* do some other misc startup stuff while we are here to save bytes */
99         if (pidfile) {
100                 pid_fd = pidfile_acquire(pidfile);
101                 pidfile_write_release(pid_fd);
102         }
103
104         /* equivelent of doing a fflush after every \n */
105         setlinebuf(stdout);
106
107         openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
108         udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
109 }
110
111
112 #else
113
114
115 static char *syslog_level_msg[] = {
116         [LOG_EMERG]   = "EMERGENCY!",
117         [LOG_ALERT]   = "ALERT!",
118         [LOG_CRIT]    = "critical!",
119         [LOG_WARNING] = "warning",
120         [LOG_ERR]     = "error",
121         [LOG_INFO]    = "info",
122         [LOG_DEBUG]   = "debug"
123 };
124
125
126 void udhcp_logging(int level, const char *fmt, ...)
127 {
128         va_list p;
129
130         va_start(p, fmt);
131         if(!daemonized) {
132                 printf("%s, ", syslog_level_msg[level]);
133                 vprintf(fmt, p);
134                 putchar('\n');
135         }
136         va_end(p);
137 }
138
139
140 void start_log_and_pid(const char *client_server, const char *pidfile)
141 {
142         int pid_fd;
143
144         /* Make sure our syslog fd isn't overwritten */
145         sanitize_fds();
146
147         /* do some other misc startup stuff while we are here to save bytes */
148         pid_fd = pidfile_acquire(pidfile);
149         pidfile_write_release(pid_fd);
150
151         /* equivelent of doing a fflush after every \n */
152         setlinebuf(stdout);
153
154         udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
155 }
156 #endif
157