Fix a compile problem
[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  * 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 long uptime(void)
41 {
42         struct sysinfo info;
43         sysinfo(&info);
44         printf("uptime %ld\n", (long)info.uptime);
45         return info.uptime;
46 }
47
48
49 /*
50  * This function makes sure our first socket calls
51  * aren't going to fd 1 (printf badness...) and are
52  * not later closed by daemon()
53  */
54 static inline void sanitize_fds(void)
55 {
56         int zero;
57         if ((zero = open(_PATH_DEVNULL, O_RDWR, 0)) < 0) return;
58         while (zero < 3) zero = dup(zero);
59         close(zero);
60 }
61
62
63 void background(const char *pidfile)
64 {
65 #ifdef __uClinux__
66         LOG(LOG_ERR, "Cannot background in uclinux (yet)");
67 #else /* __uClinux__ */
68         int pid_fd;
69
70         /* hold lock during fork. */
71         pid_fd = pidfile_acquire(pidfile);
72         if (daemon(0, 0) == -1) {
73                 perror("fork");
74                 exit(1);
75         }
76         daemonized++;
77         pidfile_write_release(pid_fd);
78 #endif /* __uClinux__ */
79 }
80
81
82 #ifdef UDHCP_SYSLOG
83 void udhcp_logging(int level, const char *fmt, ...)
84 {
85         va_list p;
86         va_list p2;
87
88         va_start(p, fmt);
89         __va_copy(p2, p);
90         if(!daemonized) {
91                 vprintf(fmt, p);
92                 putchar('\n');
93         }
94         vsyslog(level, fmt, p2);
95         va_end(p);
96 }
97
98
99 void start_log_and_pid(const char *client_server, const char *pidfile)
100 {
101         int pid_fd;
102
103         /* Make sure our syslog fd isn't overwritten */
104         sanitize_fds();
105
106         /* do some other misc startup stuff while we are here to save bytes */
107         pid_fd = pidfile_acquire(pidfile);
108         pidfile_write_release(pid_fd);
109
110         /* equivelent of doing a fflush after every \n */
111         setlinebuf(stdout);
112
113         openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
114         udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
115 }
116
117
118 #else
119
120
121 static char *syslog_level_msg[] = {
122         [LOG_EMERG]   = "EMERGENCY!",
123         [LOG_ALERT]   = "ALERT!",
124         [LOG_CRIT]    = "critical!",
125         [LOG_WARNING] = "warning",
126         [LOG_ERR]     = "error",
127         [LOG_INFO]    = "info",
128         [LOG_DEBUG]   = "debug"
129 };
130
131
132 void udhcp_logging(int level, const char *fmt, ...)
133 {
134         va_list p;
135
136         va_start(p, fmt);
137         if(!daemonized) {
138                 printf("%s, ", syslog_level_msg[level]);
139                 vprintf(fmt, p);
140                 putchar('\n');
141         }
142         va_end(p);
143 }
144
145
146 void start_log_and_pid(const char *client_server, const char *pidfile)
147 {
148         int pid_fd;
149
150         /* Make sure our syslog fd isn't overwritten */
151         sanitize_fds();
152
153         /* do some other misc startup stuff while we are here to save bytes */
154         pid_fd = pidfile_acquire(pidfile);
155         pidfile_write_release(pid_fd);
156
157         /* equivelent of doing a fflush after every \n */
158         setlinebuf(stdout);
159
160         udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
161 }
162 #endif
163