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