move debugging to safe place (before vfork)
[oweals/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  * Rewrited 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         if (!pidfile) return;
63
64         pid_fd = pidfile_acquire(pidfile); /* hold lock during fork. */
65         if (daemon(0, 0) == -1) {
66                 perror("fork");
67                 exit(1);
68         }
69         daemonized++;
70         pidfile_write_release(pid_fd);
71 #endif /* __uClinux__ */
72 }
73
74
75 #ifdef UDHCP_SYSLOG
76 void udhcp_logging(int level, const char *fmt, ...)
77 {
78         va_list p;
79         va_list p2;
80
81         va_start(p, fmt);
82         __va_copy(p2, p);
83         if(!daemonized) {
84                 vprintf(fmt, p);
85                 putchar('\n');
86         }
87         vsyslog(level, fmt, p2);
88         va_end(p);
89 }
90
91
92 void start_log_and_pid(const char *client_server, const char *pidfile)
93 {
94         int pid_fd;
95
96         /* Make sure our syslog fd isn't overwritten */
97         sanitize_fds();
98
99         /* do some other misc startup stuff while we are here to save bytes */
100         pid_fd = pidfile_acquire(pidfile);
101         pidfile_write_release(pid_fd);
102
103         /* equivelent of doing a fflush after every \n */
104         setlinebuf(stdout);
105
106         openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
107         udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
108 }
109
110
111 #else
112
113
114 static char *syslog_level_msg[] = {
115         [LOG_EMERG]   = "EMERGENCY!",
116         [LOG_ALERT]   = "ALERT!",
117         [LOG_CRIT]    = "critical!",
118         [LOG_WARNING] = "warning",
119         [LOG_ERR]     = "error",
120         [LOG_INFO]    = "info",
121         [LOG_DEBUG]   = "debug"
122 };
123
124
125 void udhcp_logging(int level, const char *fmt, ...)
126 {
127         va_list p;
128
129         va_start(p, fmt);
130         if(!daemonized) {
131                 printf("%s, ", syslog_level_msg[level]);
132                 vprintf(fmt, p);
133                 putchar('\n');
134         }
135         va_end(p);
136 }
137
138
139 void start_log_and_pid(const char *client_server, const char *pidfile)
140 {
141         int pid_fd;
142
143         /* Make sure our syslog fd isn't overwritten */
144         sanitize_fds();
145
146         /* do some other misc startup stuff while we are here to save bytes */
147         pid_fd = pidfile_acquire(pidfile);
148         pidfile_write_release(pid_fd);
149
150         /* equivelent of doing a fflush after every \n */
151         setlinebuf(stdout);
152
153         udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
154 }
155 #endif
156