3 * Functions for debugging and logging as well as some other
4 * simple helper functions.
6 * Russ Dill <Russ.Dill@asu.edu> 2001-2003
7 * Rewrited by Vladimir Oleynik <dzo@simtreas.ru> (C) 2003
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.
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.
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.
31 #include <sys/socket.h>
38 static int daemonized;
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()
46 static inline void sanitize_fds(void)
49 if ((zero = open(_PATH_DEVNULL, O_RDWR, 0)) < 0) return;
50 while (zero < 3) zero = dup(zero);
55 void background(const char *pidfile)
58 LOG(LOG_ERR, "Cannot background in uclinux (yet)");
59 #else /* __uClinux__ */
62 /* hold lock during fork. */
63 if (pidfile) pid_fd = pidfile_acquire(pidfile);
64 if (daemon(0, 0) == -1) {
69 if (pidfile) pidfile_write_release(pid_fd);
70 #endif /* __uClinux__ */
75 void udhcp_logging(int level, const char *fmt, ...)
86 vsyslog(level, fmt, p2);
91 void start_log_and_pid(const char *client_server, const char *pidfile)
95 /* Make sure our syslog fd isn't overwritten */
98 /* 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);
104 /* equivelent of doing a fflush after every \n */
107 openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0);
108 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);
115 static char *syslog_level_msg[] = {
116 [LOG_EMERG] = "EMERGENCY!",
117 [LOG_ALERT] = "ALERT!",
118 [LOG_CRIT] = "critical!",
119 [LOG_WARNING] = "warning",
122 [LOG_DEBUG] = "debug"
126 void udhcp_logging(int level, const char *fmt, ...)
132 printf("%s, ", syslog_level_msg[level]);
140 void start_log_and_pid(const char *client_server, const char *pidfile)
144 /* Make sure our syslog fd isn't overwritten */
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);
151 /* equivelent of doing a fflush after every \n */
154 udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION);