Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini syslogd implementation for busybox |
| 4 | * |
Erik Andersen | 61677fe | 2000-04-13 01:18:56 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999,2000 by Lineo, inc. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 7 | * |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 8 | * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org> |
| 9 | * |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 23 | * |
| 24 | */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 25 | |
Eric Andersen | 3570a34 | 2000-09-25 21:45:58 +0000 | [diff] [blame] | 26 | #include "busybox.h" |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 29 | #include <ctype.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 30 | #include <errno.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 31 | #include <fcntl.h> |
| 32 | #include <netdb.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 33 | #include <paths.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 34 | #include <signal.h> |
| 35 | #include <stdarg.h> |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 36 | #include <time.h> |
| 37 | #include <unistd.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 38 | #include <sys/socket.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 39 | #include <sys/types.h> |
| 40 | #include <sys/un.h> |
Erik Andersen | 7d6ba57 | 2000-04-19 20:02:50 +0000 | [diff] [blame] | 41 | #include <sys/param.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 42 | |
Eric Andersen | 999bf72 | 2000-07-09 06:59:58 +0000 | [diff] [blame] | 43 | #if ! defined __GLIBC__ && ! defined __UCLIBC__ |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 44 | |
Eric Andersen | a15cd0b | 2000-06-19 18:14:20 +0000 | [diff] [blame] | 45 | typedef unsigned int socklen_t; |
| 46 | |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 47 | #ifndef __alpha__ |
| 48 | # define __NR_klogctl __NR_syslog |
| 49 | static inline _syscall3(int, klogctl, int, type, char *, b, int, len); |
| 50 | #else /* __alpha__ */ |
| 51 | #define klogctl syslog |
| 52 | #endif |
| 53 | |
| 54 | #else |
| 55 | # include <sys/klog.h> |
| 56 | #endif |
| 57 | |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 58 | |
| 59 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 60 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ |
| 61 | #define SYSLOG_NAMES |
| 62 | #include <sys/syslog.h> |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 63 | #include <sys/uio.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 64 | |
| 65 | /* Path for the file where all log messages are written */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 66 | #define __LOG_FILE "/var/log/messages" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 67 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 68 | /* Path to the unix socket */ |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 69 | char lfile[BUFSIZ] = ""; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 70 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | static char *logFilePath = __LOG_FILE; |
| 72 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 73 | /* interval between marks in seconds */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | static int MarkInterval = 20 * 60; |
| 75 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 76 | /* localhost's name */ |
| 77 | static char LocalHostName[32]; |
| 78 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 79 | #ifdef BB_FEATURE_REMOTE_LOG |
| 80 | #include <netinet/in.h> |
| 81 | /* udp socket for logging to remote host */ |
| 82 | static int remotefd = -1; |
| 83 | /* where do we log? */ |
| 84 | static char *RemoteHost; |
| 85 | /* what port to log to? */ |
| 86 | static int RemotePort = 514; |
| 87 | /* To remote log or not to remote log, that is the question. */ |
| 88 | static int doRemoteLog = FALSE; |
| 89 | #endif |
| 90 | |
Erik Andersen | c053e41 | 2000-03-21 01:31:24 +0000 | [diff] [blame] | 91 | /* Note: There is also a function called "message()" in init.c */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 92 | /* Print a message to the log file. */ |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 93 | static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2))); |
| 94 | static void message (char *fmt, ...) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 95 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | int fd; |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 97 | struct flock fl; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 98 | va_list arguments; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 99 | |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 100 | fl.l_whence = SEEK_SET; |
| 101 | fl.l_start = 0; |
| 102 | fl.l_len = 1; |
| 103 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 104 | if ((fd = device_open (logFilePath, |
| 105 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 106 | O_NONBLOCK)) >= 0) { |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 107 | fl.l_type = F_WRLCK; |
| 108 | fcntl (fd, F_SETLKW, &fl); |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 109 | va_start (arguments, fmt); |
| 110 | vdprintf (fd, fmt, arguments); |
| 111 | va_end (arguments); |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 112 | fl.l_type = F_UNLCK; |
| 113 | fcntl (fd, F_SETLKW, &fl); |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 114 | close (fd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 115 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | /* Always send console messages to /dev/console so people will see them. */ |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 117 | if ((fd = device_open (_PATH_CONSOLE, |
| 118 | O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { |
| 119 | va_start (arguments, fmt); |
| 120 | vdprintf (fd, fmt, arguments); |
| 121 | va_end (arguments); |
| 122 | close (fd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 123 | } else { |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 124 | fprintf (stderr, "Bummer, can't print: "); |
| 125 | va_start (arguments, fmt); |
| 126 | vfprintf (stderr, fmt, arguments); |
| 127 | fflush (stderr); |
| 128 | va_end (arguments); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 130 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 133 | static void logMessage (int pri, char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 134 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 135 | time_t now; |
| 136 | char *timestamp; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 137 | static char res[20] = ""; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 138 | CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 139 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 140 | if (pri != 0) { |
| 141 | for (c_fac = facilitynames; |
| 142 | c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); |
| 143 | for (c_pri = prioritynames; |
| 144 | c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); |
| 145 | if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0') |
| 146 | snprintf(res, sizeof(res), "<%d>", pri); |
| 147 | else |
| 148 | snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); |
| 149 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 150 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 151 | if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || |
| 152 | msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') { |
| 153 | time(&now); |
| 154 | timestamp = ctime(&now) + 4; |
| 155 | timestamp[15] = '\0'; |
| 156 | } else { |
| 157 | timestamp = msg; |
| 158 | timestamp[15] = '\0'; |
| 159 | msg += 16; |
| 160 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 161 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 162 | /* todo: supress duplicates */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 163 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 164 | /* now spew out the message to wherever it is supposed to go */ |
| 165 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 166 | |
| 167 | #ifdef BB_FEATURE_REMOTE_LOG |
| 168 | /* send message to remote logger */ |
| 169 | if ( -1 != remotefd){ |
| 170 | #define IOV_COUNT 2 |
| 171 | struct iovec iov[IOV_COUNT]; |
| 172 | struct iovec *v = iov; |
| 173 | |
| 174 | bzero(&res, sizeof(res)); |
| 175 | snprintf(res, sizeof(res), "<%d>", pri); |
| 176 | v->iov_base = res ; |
| 177 | v->iov_len = strlen(res); |
| 178 | v++; |
| 179 | |
| 180 | v->iov_base = msg; |
| 181 | v->iov_len = strlen(msg); |
| 182 | |
| 183 | if ( -1 == writev(remotefd,iov, IOV_COUNT)){ |
| 184 | fatalError("syslogd: cannot write to remote file handle on" |
| 185 | "%s:%d\n",RemoteHost,RemotePort); |
| 186 | } |
| 187 | } |
| 188 | #endif |
| 189 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static void quit_signal(int sig) |
| 193 | { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 194 | logMessage(0, "System log daemon exiting."); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 195 | unlink(lfile); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 196 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 199 | static void domark(int sig) |
| 200 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 201 | if (MarkInterval > 0) { |
| 202 | logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --"); |
| 203 | alarm(MarkInterval); |
| 204 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 207 | #define BUFSIZE 1023 |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 208 | static int serveConnection (int conn) |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 209 | { |
| 210 | char buf[ BUFSIZE + 1 ]; |
| 211 | int n_read; |
| 212 | |
| 213 | while ((n_read = read (conn, buf, BUFSIZE )) > 0) { |
| 214 | |
| 215 | int pri = (LOG_USER | LOG_NOTICE); |
| 216 | char line[ BUFSIZE + 1 ]; |
| 217 | unsigned char c; |
| 218 | |
| 219 | char *p = buf, *q = line; |
| 220 | |
| 221 | buf[ n_read - 1 ] = '\0'; |
| 222 | |
| 223 | while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) { |
| 224 | if (c == '<') { |
| 225 | /* Parse the magic priority number. */ |
| 226 | pri = 0; |
| 227 | while (isdigit (*(++p))) { |
| 228 | pri = 10 * pri + (*p - '0'); |
| 229 | } |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 230 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){ |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 231 | pri = (LOG_USER | LOG_NOTICE); |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 232 | } |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 233 | } else if (c == '\n') { |
| 234 | *q++ = ' '; |
| 235 | } else if (iscntrl (c) && (c < 0177)) { |
| 236 | *q++ = '^'; |
| 237 | *q++ = c ^ 0100; |
| 238 | } else { |
| 239 | *q++ = c; |
| 240 | } |
| 241 | p++; |
| 242 | } |
| 243 | *q = '\0'; |
| 244 | /* Now log it */ |
| 245 | logMessage (pri, line); |
| 246 | } |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 247 | return (0); |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 250 | |
| 251 | #ifdef BB_FEATURE_REMOTE_LOG |
| 252 | static void init_RemoteLog (void){ |
| 253 | |
| 254 | struct sockaddr_in remoteaddr; |
| 255 | struct hostent *hostinfo; |
| 256 | int len = sizeof(remoteaddr); |
| 257 | |
| 258 | bzero(&remoteaddr, len); |
| 259 | |
| 260 | remotefd = socket(AF_INET, SOCK_DGRAM, 0); |
| 261 | |
| 262 | if (remotefd < 0) { |
| 263 | fatalError("syslogd: cannot create socket\n"); |
| 264 | } |
| 265 | |
| 266 | hostinfo = (struct hostent *) gethostbyname(RemoteHost); |
| 267 | |
| 268 | if (!hostinfo) { |
| 269 | fatalError("syslogd: cannot resolve remote host name [%s]\n", RemoteHost); |
| 270 | } |
| 271 | |
| 272 | remoteaddr.sin_family = AF_INET; |
| 273 | remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; |
| 274 | remoteaddr.sin_port = htons(RemotePort); |
| 275 | |
| 276 | /* |
| 277 | Since we are using UDP sockets, connect just sets the default host and port |
| 278 | for future operations |
| 279 | */ |
| 280 | if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){ |
| 281 | fatalError("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort); |
| 282 | } |
| 283 | |
| 284 | } |
| 285 | #endif |
| 286 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 287 | static void doSyslogd (void) __attribute__ ((noreturn)); |
| 288 | static void doSyslogd (void) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 289 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 290 | struct sockaddr_un sunx; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 291 | socklen_t addrLength; |
| 292 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 293 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 294 | int sock_fd; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 295 | fd_set fds; |
| 296 | |
Erik Andersen | 4f3f757 | 2000-04-28 00:18:56 +0000 | [diff] [blame] | 297 | char lfile[BUFSIZ]; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 298 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 299 | /* Set up signal handlers. */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 300 | signal (SIGINT, quit_signal); |
| 301 | signal (SIGTERM, quit_signal); |
| 302 | signal (SIGQUIT, quit_signal); |
| 303 | signal (SIGHUP, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 304 | signal (SIGCHLD, SIG_IGN); |
| 305 | #ifdef SIGCLD |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 306 | signal (SIGCLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 307 | #endif |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 308 | signal (SIGALRM, domark); |
| 309 | alarm (MarkInterval); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 310 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 311 | /* Create the syslog file so realpath() can work. */ |
| 312 | close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644)); |
| 313 | if (realpath (_PATH_LOG, lfile) == NULL) |
Pavel Roskin | 23ad7f7 | 2000-09-12 22:05:17 +0000 | [diff] [blame] | 314 | fatalError ("Could not resolve path to " _PATH_LOG ": %s\n", strerror (errno)); |
Eric Andersen | 14ec6cf | 1999-12-05 22:17:02 +0000 | [diff] [blame] | 315 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 316 | unlink (lfile); |
| 317 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 318 | memset (&sunx, 0, sizeof (sunx)); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 319 | sunx.sun_family = AF_UNIX; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 320 | strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path)); |
| 321 | if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 322 | fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno)); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 323 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 324 | addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path); |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 325 | if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5))) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 326 | fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 327 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 328 | if (chmod (lfile, 0666) < 0) |
Eric Andersen | 86ab8a3 | 2000-06-02 03:21:42 +0000 | [diff] [blame] | 329 | fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 330 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 331 | FD_ZERO (&fds); |
| 332 | FD_SET (sock_fd, &fds); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 333 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 334 | #ifdef BB_FEATURE_REMOTE_LOG |
| 335 | if (doRemoteLog == TRUE){ |
| 336 | init_RemoteLog(); |
| 337 | } |
| 338 | #endif |
| 339 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 340 | logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 341 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 342 | for (;;) { |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 343 | |
| 344 | fd_set readfds; |
| 345 | int n_ready; |
| 346 | int fd; |
| 347 | |
| 348 | memcpy (&readfds, &fds, sizeof (fds)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 349 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 350 | if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) { |
| 351 | if (errno == EINTR) continue; /* alarm may have happened. */ |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 352 | fatalError ("select error: %s\n", strerror (errno)); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 353 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 354 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 355 | for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 356 | if (FD_ISSET (fd, &readfds)) { |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 357 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 358 | --n_ready; |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 359 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 360 | if (fd == sock_fd) { |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 361 | |
| 362 | int conn; |
| 363 | pid_t pid; |
| 364 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 365 | if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) { |
| 366 | fatalError ("accept error: %s\n", strerror (errno)); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 367 | } |
Erik Andersen | a6c7522 | 2000-04-18 00:00:52 +0000 | [diff] [blame] | 368 | |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 369 | pid = fork(); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 370 | |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 371 | if (pid < 0) { |
| 372 | perror ("syslogd: fork"); |
| 373 | close (conn); |
| 374 | continue; |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 375 | } |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 376 | |
Eric Andersen | b2fc5a0 | 2000-08-20 06:11:32 +0000 | [diff] [blame] | 377 | if (pid == 0) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 378 | serveConnection (conn); |
Eric Andersen | b2fc5a0 | 2000-08-20 06:11:32 +0000 | [diff] [blame] | 379 | close (conn); |
| 380 | exit( TRUE); |
| 381 | } |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 382 | close (conn); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 383 | } |
| 384 | } |
| 385 | } |
| 386 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 389 | #ifdef BB_FEATURE_KLOGD |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 390 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 391 | static void klogd_signal(int sig) |
| 392 | { |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 393 | klogctl(7, NULL, 0); |
| 394 | klogctl(0, 0, 0); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 395 | logMessage(0, "Kernel log daemon exiting."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 396 | exit(TRUE); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 399 | static void doKlogd (void) __attribute__ ((noreturn)); |
| 400 | static void doKlogd (void) |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 401 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 402 | int priority = LOG_INFO; |
| 403 | char log_buffer[4096]; |
| 404 | char *logp; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 405 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 406 | /* Set up sig handlers */ |
| 407 | signal(SIGINT, klogd_signal); |
| 408 | signal(SIGKILL, klogd_signal); |
| 409 | signal(SIGTERM, klogd_signal); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 410 | signal(SIGHUP, SIG_IGN); |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 411 | |
| 412 | #ifdef BB_FEATURE_REMOTE_LOG |
| 413 | if (doRemoteLog == TRUE){ |
| 414 | init_RemoteLog(); |
| 415 | } |
| 416 | #endif |
| 417 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 418 | logMessage(0, "klogd started: " |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 419 | "BusyBox v" BB_VER " (" BB_BT ")"); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 420 | |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 421 | klogctl(1, NULL, 0); |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 422 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 423 | while (1) { |
| 424 | /* Use kernel syscalls */ |
| 425 | memset(log_buffer, '\0', sizeof(log_buffer)); |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 426 | if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 427 | char message[80]; |
| 428 | |
| 429 | if (errno == EINTR) |
| 430 | continue; |
| 431 | snprintf(message, 79, "klogd: Error return from sys_sycall: " \ |
| 432 | "%d - %s.\n", errno, strerror(errno)); |
| 433 | logMessage(LOG_SYSLOG | LOG_ERR, message); |
| 434 | exit(1); |
| 435 | } |
| 436 | logp = log_buffer; |
| 437 | if (*log_buffer == '<') { |
| 438 | switch (*(log_buffer + 1)) { |
| 439 | case '0': |
| 440 | priority = LOG_EMERG; |
| 441 | break; |
| 442 | case '1': |
| 443 | priority = LOG_ALERT; |
| 444 | break; |
| 445 | case '2': |
| 446 | priority = LOG_CRIT; |
| 447 | break; |
| 448 | case '3': |
| 449 | priority = LOG_ERR; |
| 450 | break; |
| 451 | case '4': |
| 452 | priority = LOG_WARNING; |
| 453 | break; |
| 454 | case '5': |
| 455 | priority = LOG_NOTICE; |
| 456 | break; |
| 457 | case '6': |
| 458 | priority = LOG_INFO; |
| 459 | break; |
| 460 | case '7': |
| 461 | default: |
| 462 | priority = LOG_DEBUG; |
| 463 | } |
| 464 | logp += 3; |
| 465 | } |
| 466 | logMessage(LOG_KERN | priority, logp); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 467 | } |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 468 | |
| 469 | } |
| 470 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 471 | #endif |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 472 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 473 | static void daemon_init (char **argv, char *dz, void fn (void)) |
| 474 | { |
| 475 | setsid(); |
| 476 | chdir ("/"); |
| 477 | strncpy(argv[0], dz, strlen(argv[0])); |
| 478 | fn(); |
| 479 | exit(0); |
| 480 | } |
| 481 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 482 | extern int syslogd_main(int argc, char **argv) |
| 483 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 484 | int pid, klogd_pid; |
| 485 | int doFork = TRUE; |
| 486 | |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 487 | #ifdef BB_FEATURE_KLOGD |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 488 | int startKlogd = TRUE; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 489 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 490 | int stopDoingThat = FALSE; |
| 491 | char *p; |
| 492 | char **argv1 = argv; |
| 493 | |
| 494 | while (--argc > 0 && **(++argv1) == '-') { |
| 495 | stopDoingThat = FALSE; |
| 496 | while (stopDoingThat == FALSE && *(++(*argv1))) { |
| 497 | switch (**argv1) { |
| 498 | case 'm': |
| 499 | if (--argc == 0) { |
| 500 | usage(syslogd_usage); |
| 501 | } |
| 502 | MarkInterval = atoi(*(++argv1)) * 60; |
| 503 | break; |
| 504 | case 'n': |
| 505 | doFork = FALSE; |
| 506 | break; |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 507 | #ifdef BB_FEATURE_KLOGD |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 508 | case 'K': |
| 509 | startKlogd = FALSE; |
| 510 | break; |
| 511 | #endif |
| 512 | case 'O': |
| 513 | if (--argc == 0) { |
| 514 | usage(syslogd_usage); |
| 515 | } |
| 516 | logFilePath = *(++argv1); |
| 517 | stopDoingThat = TRUE; |
| 518 | break; |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 519 | #ifdef BB_FEATURE_REMOTE_LOG |
| 520 | case 'R': |
| 521 | if (--argc == 0) { |
| 522 | usage(syslogd_usage); |
| 523 | } |
| 524 | RemoteHost = *(++argv1); |
| 525 | if ( (p = strchr(RemoteHost, ':'))){ |
| 526 | RemotePort = atoi(p+1); |
| 527 | *p = '\0'; |
| 528 | } |
| 529 | doRemoteLog = TRUE; |
| 530 | stopDoingThat = TRUE; |
| 531 | break; |
| 532 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 533 | default: |
| 534 | usage(syslogd_usage); |
| 535 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 536 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 539 | if (argc > 0) |
| 540 | usage(syslogd_usage); |
| 541 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 542 | /* Store away localhost's name before the fork */ |
| 543 | gethostname(LocalHostName, sizeof(LocalHostName)); |
| 544 | if ((p = strchr(LocalHostName, '.'))) { |
| 545 | *p++ = '\0'; |
| 546 | } |
| 547 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 548 | umask(0); |
| 549 | |
Eric Andersen | 03f4c27 | 2000-07-06 23:10:29 +0000 | [diff] [blame] | 550 | #ifdef BB_FEATURE_KLOGD |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 551 | /* Start up the klogd process */ |
| 552 | if (startKlogd == TRUE) { |
| 553 | klogd_pid = fork(); |
| 554 | if (klogd_pid == 0) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 555 | daemon_init (argv, "klogd", doKlogd); |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | #endif |
| 559 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 560 | if (doFork == TRUE) { |
| 561 | pid = fork(); |
| 562 | if (pid < 0) |
| 563 | exit(pid); |
| 564 | else if (pid == 0) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 565 | daemon_init (argv, "syslogd", doSyslogd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 566 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 567 | } else { |
| 568 | doSyslogd(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 569 | } |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 570 | |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 571 | return(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 572 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 573 | |
| 574 | /* |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 575 | Local Variables |
| 576 | c-file-style: "linux" |
| 577 | c-basic-offset: 4 |
| 578 | tab-width: 4 |
| 579 | End: |
| 580 | */ |