blob: bf2ac44174ec38008768958c9d4409c8f7c0a790 [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 Andersenaff114c2004-04-14 17:51:38 +00007 * Rewritten by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
Eric Andersena8da17a2003-06-14 02:57:53 +00008 *
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
Russ Dill309c7b72004-05-19 08:29:05 +000040long uptime(void)
41{
42 struct sysinfo info;
43 sysinfo(&info);
Russ Dill309c7b72004-05-19 08:29:05 +000044 return info.uptime;
45}
46
Eric Andersena8da17a2003-06-14 02:57:53 +000047
Russ Dill4e864a32003-12-18 22:25:38 +000048/*
49 * This function makes sure our first socket calls
50 * aren't going to fd 1 (printf badness...) and are
51 * not later closed by daemon()
52 */
53static inline void sanitize_fds(void)
54{
55 int zero;
56 if ((zero = open(_PATH_DEVNULL, O_RDWR, 0)) < 0) return;
57 while (zero < 3) zero = dup(zero);
58 close(zero);
59}
60
61
62void background(const char *pidfile)
63{
64#ifdef __uClinux__
Eric Andersenc7bda1c2004-03-15 08:29:22 +000065 LOG(LOG_ERR, "Cannot background in uclinux (yet)");
Russ Dill4e864a32003-12-18 22:25:38 +000066#else /* __uClinux__ */
67 int pid_fd;
68
Russ Dill6caa0732003-12-26 23:41:28 +000069 /* hold lock during fork. */
Russ Dill91e006c2004-05-19 07:46:23 +000070 pid_fd = pidfile_acquire(pidfile);
Russ Dill4e864a32003-12-18 22:25:38 +000071 if (daemon(0, 0) == -1) {
72 perror("fork");
73 exit(1);
74 }
75 daemonized++;
Russ Dill91e006c2004-05-19 07:46:23 +000076 pidfile_write_release(pid_fd);
Russ Dill4e864a32003-12-18 22:25:38 +000077#endif /* __uClinux__ */
78}
79
80
81#ifdef UDHCP_SYSLOG
Eric Andersena8da17a2003-06-14 02:57:53 +000082void udhcp_logging(int level, const char *fmt, ...)
83{
Eric Andersena8da17a2003-06-14 02:57:53 +000084 va_list p;
85 va_list p2;
86
87 va_start(p, fmt);
88 __va_copy(p2, p);
89 if(!daemonized) {
90 vprintf(fmt, p);
91 putchar('\n');
Eric Andersena8da17a2003-06-14 02:57:53 +000092 }
93 vsyslog(level, fmt, p2);
94 va_end(p);
95}
96
Russ Dill4e864a32003-12-18 22:25:38 +000097
98void start_log_and_pid(const char *client_server, const char *pidfile)
Eric Andersena8da17a2003-06-14 02:57:53 +000099{
Russ Dill4e864a32003-12-18 22:25:38 +0000100 int pid_fd;
101
102 /* Make sure our syslog fd isn't overwritten */
103 sanitize_fds();
104
105 /* do some other misc startup stuff while we are here to save bytes */
Russ Dill91e006c2004-05-19 07:46:23 +0000106 pid_fd = pidfile_acquire(pidfile);
107 pidfile_write_release(pid_fd);
Russ Dill4e864a32003-12-18 22:25:38 +0000108
109 /* equivelent of doing a fflush after every \n */
110 setlinebuf(stdout);
111
112 openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
Eric Andersena8da17a2003-06-14 02:57:53 +0000113 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
114}
115
Russ Dill4e864a32003-12-18 22:25:38 +0000116
Eric Andersena8da17a2003-06-14 02:57:53 +0000117#else
118
Russ Dill4e864a32003-12-18 22:25:38 +0000119
Eric Andersena8da17a2003-06-14 02:57:53 +0000120static char *syslog_level_msg[] = {
121 [LOG_EMERG] = "EMERGENCY!",
122 [LOG_ALERT] = "ALERT!",
123 [LOG_CRIT] = "critical!",
124 [LOG_WARNING] = "warning",
125 [LOG_ERR] = "error",
126 [LOG_INFO] = "info",
127 [LOG_DEBUG] = "debug"
128};
129
Russ Dill4e864a32003-12-18 22:25:38 +0000130
Eric Andersena8da17a2003-06-14 02:57:53 +0000131void udhcp_logging(int level, const char *fmt, ...)
132{
Eric Andersena8da17a2003-06-14 02:57:53 +0000133 va_list p;
134
135 va_start(p, fmt);
136 if(!daemonized) {
137 printf("%s, ", syslog_level_msg[level]);
Eric Andersena8da17a2003-06-14 02:57:53 +0000138 vprintf(fmt, p);
139 putchar('\n');
Eric Andersena8da17a2003-06-14 02:57:53 +0000140 }
141 va_end(p);
142}
143
Russ Dill4e864a32003-12-18 22:25:38 +0000144
145void start_log_and_pid(const char *client_server, const char *pidfile)
Eric Andersena8da17a2003-06-14 02:57:53 +0000146{
Russ Dill4e864a32003-12-18 22:25:38 +0000147 int pid_fd;
148
149 /* Make sure our syslog fd isn't overwritten */
150 sanitize_fds();
151
152 /* do some other misc startup stuff while we are here to save bytes */
153 pid_fd = pidfile_acquire(pidfile);
154 pidfile_write_release(pid_fd);
155
156 /* equivelent of doing a fflush after every \n */
157 setlinebuf(stdout);
158
Eric Andersena8da17a2003-06-14 02:57:53 +0000159 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
160}
161#endif
162