blob: 8542c664c661cf792188b61ab5dc3cf4d1e57801 [file] [log] [blame]
Eric Andersena8da17a2003-06-14 02:57:53 +00001/* common.c
2 *
Russ Dill4e864a32003-12-18 22:25:38 +00003 * Functions for debugging and logging as well as some other
4 * simple helper functions.
Eric Andersena8da17a2003-06-14 02:57:53 +00005 *
Russ Dill4e864a32003-12-18 22:25:38 +00006 * Russ Dill <Russ.Dill@asu.edu> 2001-2003
Eric Andersena8da17a2003-06-14 02:57:53 +00007 * 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>
Russ Dill4e864a32003-12-18 22:25:38 +000030#include <paths.h>
Eric Andersena8da17a2003-06-14 02:57:53 +000031#include <sys/socket.h>
Russ Dill4e864a32003-12-18 22:25:38 +000032#include <stdarg.h>
Eric Andersena8da17a2003-06-14 02:57:53 +000033
34#include "common.h"
Russ Dill4e864a32003-12-18 22:25:38 +000035#include "pidfile.h"
Eric Andersena8da17a2003-06-14 02:57:53 +000036
37
38static int daemonized;
39
Eric Andersena8da17a2003-06-14 02:57:53 +000040
Russ Dill4e864a32003-12-18 22:25:38 +000041/*
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 */
46static 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
55void 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
Russ Dill6caa0732003-12-26 23:41:28 +000062 /* hold lock during fork. */
63 if (pidfile) pid_fd = pidfile_acquire(pidfile);
Russ Dill4e864a32003-12-18 22:25:38 +000064 if (daemon(0, 0) == -1) {
65 perror("fork");
66 exit(1);
67 }
68 daemonized++;
Russ Dill6caa0732003-12-26 23:41:28 +000069 if (pidfile) pidfile_write_release(pid_fd);
Russ Dill4e864a32003-12-18 22:25:38 +000070#endif /* __uClinux__ */
71}
72
73
74#ifdef UDHCP_SYSLOG
Eric Andersena8da17a2003-06-14 02:57:53 +000075void udhcp_logging(int level, const char *fmt, ...)
76{
Eric Andersena8da17a2003-06-14 02:57:53 +000077 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');
Eric Andersena8da17a2003-06-14 02:57:53 +000085 }
86 vsyslog(level, fmt, p2);
87 va_end(p);
88}
89
Russ Dill4e864a32003-12-18 22:25:38 +000090
91void start_log_and_pid(const char *client_server, const char *pidfile)
Eric Andersena8da17a2003-06-14 02:57:53 +000092{
Russ Dill4e864a32003-12-18 22:25:38 +000093 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 */
Russ Dill6caa0732003-12-26 23:41:28 +000099 if (pidfile) {
100 pid_fd = pidfile_acquire(pidfile);
101 pidfile_write_release(pid_fd);
102 }
Russ Dill4e864a32003-12-18 22:25:38 +0000103
104 /* equivelent of doing a fflush after every \n */
105 setlinebuf(stdout);
106
107 openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
Eric Andersena8da17a2003-06-14 02:57:53 +0000108 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
109}
110
Russ Dill4e864a32003-12-18 22:25:38 +0000111
Eric Andersena8da17a2003-06-14 02:57:53 +0000112#else
113
Russ Dill4e864a32003-12-18 22:25:38 +0000114
Eric Andersena8da17a2003-06-14 02:57:53 +0000115static 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
Russ Dill4e864a32003-12-18 22:25:38 +0000125
Eric Andersena8da17a2003-06-14 02:57:53 +0000126void udhcp_logging(int level, const char *fmt, ...)
127{
Eric Andersena8da17a2003-06-14 02:57:53 +0000128 va_list p;
129
130 va_start(p, fmt);
131 if(!daemonized) {
132 printf("%s, ", syslog_level_msg[level]);
Eric Andersena8da17a2003-06-14 02:57:53 +0000133 vprintf(fmt, p);
134 putchar('\n');
Eric Andersena8da17a2003-06-14 02:57:53 +0000135 }
136 va_end(p);
137}
138
Russ Dill4e864a32003-12-18 22:25:38 +0000139
140void start_log_and_pid(const char *client_server, const char *pidfile)
Eric Andersena8da17a2003-06-14 02:57:53 +0000141{
Russ Dill4e864a32003-12-18 22:25:38 +0000142 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
Eric Andersena8da17a2003-06-14 02:57:53 +0000154 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
155}
156#endif
157