Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 1 | /* common.c |
| 2 | * |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 3 | * Functions for debugging and logging as well as some other |
| 4 | * simple helper functions. |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 5 | * |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 6 | * Russ Dill <Russ.Dill@asu.edu> 2001-2003 |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 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> |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 30 | #include <paths.h> |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 31 | #include <sys/socket.h> |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 32 | #include <stdarg.h> |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 33 | |
| 34 | #include "common.h" |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 35 | #include "pidfile.h" |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 36 | |
| 37 | |
| 38 | static int daemonized; |
| 39 | |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 40 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 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 | |
Russ Dill | 6caa073 | 2003-12-26 23:41:28 +0000 | [diff] [blame] | 62 | /* hold lock during fork. */ |
| 63 | if (pidfile) pid_fd = pidfile_acquire(pidfile); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 64 | if (daemon(0, 0) == -1) { |
| 65 | perror("fork"); |
| 66 | exit(1); |
| 67 | } |
| 68 | daemonized++; |
Russ Dill | 6caa073 | 2003-12-26 23:41:28 +0000 | [diff] [blame] | 69 | if (pidfile) pidfile_write_release(pid_fd); |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 70 | #endif /* __uClinux__ */ |
| 71 | } |
| 72 | |
| 73 | |
| 74 | #ifdef UDHCP_SYSLOG |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 75 | void udhcp_logging(int level, const char *fmt, ...) |
| 76 | { |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 77 | 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 Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 85 | } |
| 86 | vsyslog(level, fmt, p2); |
| 87 | va_end(p); |
| 88 | } |
| 89 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 90 | |
| 91 | void start_log_and_pid(const char *client_server, const char *pidfile) |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 92 | { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 93 | 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 Dill | 6caa073 | 2003-12-26 23:41:28 +0000 | [diff] [blame] | 99 | if (pidfile) { |
| 100 | pid_fd = pidfile_acquire(pidfile); |
| 101 | pidfile_write_release(pid_fd); |
| 102 | } |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 103 | |
| 104 | /* equivelent of doing a fflush after every \n */ |
| 105 | setlinebuf(stdout); |
| 106 | |
| 107 | openlog(client_server, LOG_PID | LOG_CONS, LOG_LOCAL0); |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 108 | udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION); |
| 109 | } |
| 110 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 111 | |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 112 | #else |
| 113 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 114 | |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 115 | static 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 Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 125 | |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 126 | void udhcp_logging(int level, const char *fmt, ...) |
| 127 | { |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 128 | va_list p; |
| 129 | |
| 130 | va_start(p, fmt); |
| 131 | if(!daemonized) { |
| 132 | printf("%s, ", syslog_level_msg[level]); |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 133 | vprintf(fmt, p); |
| 134 | putchar('\n'); |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 135 | } |
| 136 | va_end(p); |
| 137 | } |
| 138 | |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 139 | |
| 140 | void start_log_and_pid(const char *client_server, const char *pidfile) |
Eric Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 141 | { |
Russ Dill | 4e864a3 | 2003-12-18 22:25:38 +0000 | [diff] [blame] | 142 | 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 Andersen | a8da17a | 2003-06-14 02:57:53 +0000 | [diff] [blame] | 154 | udhcp_logging(LOG_INFO, "%s (v%s) started", client_server, VERSION); |
| 155 | } |
| 156 | #endif |
| 157 | |