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 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 23 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 24 | #include "internal.h" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 25 | #include <ctype.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 27 | #include <fcntl.h> |
| 28 | #include <netdb.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 29 | #include <paths.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <stdarg.h> |
| 32 | #include <stdio.h> |
| 33 | #include <sys/klog.h> |
| 34 | #include <sys/socket.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/un.h> |
| 38 | #include <time.h> |
| 39 | #include <unistd.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 40 | |
| 41 | #define ksyslog klogctl |
| 42 | extern int ksyslog(int type, char *buf, int len); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 43 | |
| 44 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 45 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ |
| 46 | #define SYSLOG_NAMES |
| 47 | #include <sys/syslog.h> |
| 48 | |
| 49 | /* Path for the file where all log messages are written */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 50 | #define __LOG_FILE "/var/log/messages" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 51 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 52 | /* Path to the unix socket */ |
| 53 | char lfile[PATH_MAX] = ""; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 54 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 55 | static char *logFilePath = __LOG_FILE; |
| 56 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 57 | /* interval between marks in seconds */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | static int MarkInterval = 20 * 60; |
| 59 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 60 | /* localhost's name */ |
| 61 | static char LocalHostName[32]; |
| 62 | |
| 63 | static const char syslogd_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 64 | "syslogd [OPTION]...\n\n" |
| 65 | "Linux system and kernel (provides klogd) logging utility.\n" |
| 66 | "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n" |
| 67 | "Options:\n" |
| 68 | "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n" |
| 69 | "\t-n\tDo not fork into the background (for when run by init)\n" |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 70 | #ifdef BB_KLOGD |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 71 | "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n" |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 72 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | "\t-O\tSpecify an alternate log file. default=/var/log/messages\n"; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 74 | |
Erik Andersen | c053e41 | 2000-03-21 01:31:24 +0000 | [diff] [blame] | 75 | /* Note: There is also a function called "message()" in init.c */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 76 | /* Print a message to the log file. */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 77 | static void message(char *fmt, ...) |
Erik Andersen | 93d6513 | 2000-04-06 08:06:36 +0000 | [diff] [blame] | 78 | __attribute__ ((format (printf, 1, 2))); |
| 79 | static void message(char *fmt, ...) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 80 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | int fd; |
| 82 | va_list arguments; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 83 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | if ( |
| 85 | (fd = |
| 86 | device_open(logFilePath, |
| 87 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 88 | O_NONBLOCK)) >= 0) { |
| 89 | va_start(arguments, fmt); |
| 90 | vdprintf(fd, fmt, arguments); |
| 91 | va_end(arguments); |
| 92 | close(fd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 93 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 94 | /* Always send console messages to /dev/console so people will see them. */ |
| 95 | if ( |
| 96 | (fd = |
| 97 | device_open(_PATH_CONSOLE, |
| 98 | O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { |
| 99 | va_start(arguments, fmt); |
| 100 | vdprintf(fd, fmt, arguments); |
| 101 | va_end(arguments); |
| 102 | close(fd); |
| 103 | } else { |
| 104 | fprintf(stderr, "Bummer, can't print: "); |
| 105 | va_start(arguments, fmt); |
| 106 | vfprintf(stderr, fmt, arguments); |
| 107 | fflush(stderr); |
| 108 | va_end(arguments); |
| 109 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 110 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | static void logMessage(int pri, char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 114 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 115 | time_t now; |
| 116 | char *timestamp; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 117 | static char res[20] = ""; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 118 | CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 119 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 120 | if (pri != 0) { |
| 121 | for (c_fac = facilitynames; |
| 122 | c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); |
| 123 | for (c_pri = prioritynames; |
| 124 | c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); |
| 125 | if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0') |
| 126 | snprintf(res, sizeof(res), "<%d>", pri); |
| 127 | else |
| 128 | snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); |
| 129 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 130 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || |
| 132 | msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') { |
| 133 | time(&now); |
| 134 | timestamp = ctime(&now) + 4; |
| 135 | timestamp[15] = '\0'; |
| 136 | } else { |
| 137 | timestamp = msg; |
| 138 | timestamp[15] = '\0'; |
| 139 | msg += 16; |
| 140 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 141 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 142 | /* todo: supress duplicates */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 143 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 144 | /* now spew out the message to wherever it is supposed to go */ |
| 145 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | static void quit_signal(int sig) |
| 149 | { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 150 | logMessage(0, "System log daemon exiting."); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 151 | unlink(lfile); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 152 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 155 | static void domark(int sig) |
| 156 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 157 | if (MarkInterval > 0) { |
| 158 | logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --"); |
| 159 | alarm(MarkInterval); |
| 160 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 163 | static void doSyslogd (void) __attribute__ ((noreturn)); |
| 164 | static void doSyslogd (void) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 165 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 166 | struct sockaddr_un sunx; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 167 | size_t addrLength; |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 168 | int sock_fd; |
| 169 | fd_set readfds; |
| 170 | char lfile[PATH_MAX]; |
| 171 | int t = readlink(_PATH_LOG, lfile, sizeof(lfile) - 1); /* Resolve symlinks */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 172 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 173 | /* Set up sig handlers */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 174 | signal (SIGINT, quit_signal); |
| 175 | signal (SIGTERM, quit_signal); |
| 176 | signal (SIGQUIT, quit_signal); |
| 177 | signal (SIGHUP, SIG_IGN); |
| 178 | signal (SIGALRM, domark); |
| 179 | alarm (MarkInterval); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 180 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 181 | if (t == -1) |
| 182 | strncpy(lfile, _PATH_LOG, sizeof(lfile)); |
| 183 | else |
| 184 | lfile[t] = '\0'; |
Eric Andersen | 14ec6cf | 1999-12-05 22:17:02 +0000 | [diff] [blame] | 185 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 186 | unlink (lfile); |
| 187 | |
| 188 | memset (&sunx, 0, sizeof(sunx)); |
| 189 | |
| 190 | sunx.sun_family = AF_UNIX; |
| 191 | strncpy (sunx.sun_path, lfile, sizeof(sunx.sun_path)); |
| 192 | if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 193 | perror ("Couldn't obtain descriptor for socket " _PATH_LOG); |
| 194 | exit (FALSE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 195 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 196 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 197 | addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path); |
| 198 | if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || |
| 199 | (listen (sock_fd, 5))) { |
| 200 | perror ("Could not connect to socket " _PATH_LOG); |
| 201 | exit (FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 204 | if (chmod (lfile, 0666) < 0) { |
| 205 | perror ("Could not set permission on " _PATH_LOG); |
| 206 | exit (FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 209 | FD_ZERO (&readfds); |
| 210 | FD_SET (sock_fd, &readfds); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 211 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 212 | logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 213 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 214 | for (;;) { |
| 215 | int n_ready; |
| 216 | int fd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 217 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 218 | if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) { |
| 219 | if (errno == EINTR) continue; /* alarm may have happened. */ |
| 220 | perror ("select"); |
| 221 | exit (FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 222 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 223 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 224 | /* Skip stdin, stdout, stderr */ |
| 225 | for (fd = 3; fd <= FD_SETSIZE; fd++) { |
| 226 | if (FD_ISSET (fd, &readfds)) { |
| 227 | if (fd == sock_fd) { |
| 228 | int conn; |
| 229 | if ((conn = accept(sock_fd, (struct sockaddr *) &sunx, |
| 230 | &addrLength)) < 0) { |
| 231 | perror ("accept"); |
| 232 | exit (FALSE); /* #### ??? */ |
| 233 | } |
| 234 | FD_SET (conn, &readfds); |
| 235 | } |
| 236 | else { |
| 237 | #define BUFSIZE 1024 + 1 |
| 238 | char buf[BUFSIZE]; |
| 239 | char *q, *p; |
| 240 | int n_read; |
| 241 | |
| 242 | n_read = read (fd, buf, BUFSIZE); |
| 243 | |
| 244 | if (n_read < 0) { |
| 245 | perror ("read error"); |
| 246 | goto close_fd; |
| 247 | } |
| 248 | else if (n_read > 0) { |
| 249 | char line[BUFSIZE]; |
| 250 | unsigned char c; |
| 251 | int pri = (LOG_USER | LOG_NOTICE); |
| 252 | |
| 253 | memset (line, 0, sizeof(line)); |
| 254 | p = buf; |
| 255 | q = line; |
| 256 | while (p && (c = *p) && q < &line[sizeof(line) - 1]) { |
| 257 | if (c == '<') { |
| 258 | /* Parse the magic priority number */ |
| 259 | pri = 0; |
| 260 | while (isdigit(*(++p))) { |
| 261 | pri = 10 * pri + (*p - '0'); |
| 262 | } |
| 263 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) |
| 264 | pri = (LOG_USER | LOG_NOTICE); |
| 265 | } else if (c == '\n') { |
| 266 | *q++ = ' '; |
| 267 | } else if (iscntrl(c) && (c < 0177)) { |
| 268 | *q++ = '^'; |
| 269 | *q++ = c ^ 0100; |
| 270 | } else { |
| 271 | *q++ = c; |
| 272 | } |
| 273 | p++; |
| 274 | } |
| 275 | *q = '\0'; |
| 276 | |
| 277 | /* Now log it */ |
| 278 | logMessage(pri, line); |
| 279 | |
| 280 | close_fd: |
| 281 | close (fd); |
| 282 | FD_CLR (fd, &readfds); |
| 283 | } |
| 284 | else { /* EOF */ |
| 285 | goto close_fd; |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 293 | #ifdef BB_KLOGD |
| 294 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 295 | static void klogd_signal(int sig) |
| 296 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 297 | ksyslog(7, NULL, 0); |
| 298 | ksyslog(0, 0, 0); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 299 | logMessage(0, "Kernel log daemon exiting."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 300 | exit(TRUE); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 303 | static void doKlogd (void) __attribute__ ((noreturn)); |
| 304 | static void doKlogd (void) |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 305 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 306 | int priority = LOG_INFO; |
| 307 | char log_buffer[4096]; |
| 308 | char *logp; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 309 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 310 | /* Set up sig handlers */ |
| 311 | signal(SIGINT, klogd_signal); |
| 312 | signal(SIGKILL, klogd_signal); |
| 313 | signal(SIGTERM, klogd_signal); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 314 | signal(SIGHUP, SIG_IGN); |
| 315 | logMessage(0, "klogd started: " |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | "BusyBox v" BB_VER " (" BB_BT ")"); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 317 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 318 | ksyslog(1, NULL, 0); |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 319 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 320 | while (1) { |
| 321 | /* Use kernel syscalls */ |
| 322 | memset(log_buffer, '\0', sizeof(log_buffer)); |
| 323 | if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) { |
| 324 | char message[80]; |
| 325 | |
| 326 | if (errno == EINTR) |
| 327 | continue; |
| 328 | snprintf(message, 79, "klogd: Error return from sys_sycall: " \ |
| 329 | "%d - %s.\n", errno, strerror(errno)); |
| 330 | logMessage(LOG_SYSLOG | LOG_ERR, message); |
| 331 | exit(1); |
| 332 | } |
| 333 | logp = log_buffer; |
| 334 | if (*log_buffer == '<') { |
| 335 | switch (*(log_buffer + 1)) { |
| 336 | case '0': |
| 337 | priority = LOG_EMERG; |
| 338 | break; |
| 339 | case '1': |
| 340 | priority = LOG_ALERT; |
| 341 | break; |
| 342 | case '2': |
| 343 | priority = LOG_CRIT; |
| 344 | break; |
| 345 | case '3': |
| 346 | priority = LOG_ERR; |
| 347 | break; |
| 348 | case '4': |
| 349 | priority = LOG_WARNING; |
| 350 | break; |
| 351 | case '5': |
| 352 | priority = LOG_NOTICE; |
| 353 | break; |
| 354 | case '6': |
| 355 | priority = LOG_INFO; |
| 356 | break; |
| 357 | case '7': |
| 358 | default: |
| 359 | priority = LOG_DEBUG; |
| 360 | } |
| 361 | logp += 3; |
| 362 | } |
| 363 | logMessage(LOG_KERN | priority, logp); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 364 | } |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 365 | |
| 366 | } |
| 367 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 368 | #endif |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 369 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 370 | static void daemon_init (char **argv, char *dz, void fn (void)) __attribute__ ((noreturn)); |
| 371 | static void daemon_init (char **argv, char *dz, void fn (void)) |
| 372 | { |
| 373 | setsid(); |
| 374 | chdir ("/"); |
| 375 | strncpy(argv[0], dz, strlen(argv[0])); |
| 376 | fn(); |
| 377 | exit(0); |
| 378 | } |
| 379 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 380 | extern int syslogd_main(int argc, char **argv) |
| 381 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 382 | int pid, klogd_pid; |
| 383 | int doFork = TRUE; |
| 384 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 385 | #ifdef BB_KLOGD |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | int startKlogd = TRUE; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 387 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 388 | int stopDoingThat = FALSE; |
| 389 | char *p; |
| 390 | char **argv1 = argv; |
| 391 | |
| 392 | while (--argc > 0 && **(++argv1) == '-') { |
| 393 | stopDoingThat = FALSE; |
| 394 | while (stopDoingThat == FALSE && *(++(*argv1))) { |
| 395 | switch (**argv1) { |
| 396 | case 'm': |
| 397 | if (--argc == 0) { |
| 398 | usage(syslogd_usage); |
| 399 | } |
| 400 | MarkInterval = atoi(*(++argv1)) * 60; |
| 401 | break; |
| 402 | case 'n': |
| 403 | doFork = FALSE; |
| 404 | break; |
| 405 | #ifdef BB_KLOGD |
| 406 | case 'K': |
| 407 | startKlogd = FALSE; |
| 408 | break; |
| 409 | #endif |
| 410 | case 'O': |
| 411 | if (--argc == 0) { |
| 412 | usage(syslogd_usage); |
| 413 | } |
| 414 | logFilePath = *(++argv1); |
| 415 | stopDoingThat = TRUE; |
| 416 | break; |
| 417 | default: |
| 418 | usage(syslogd_usage); |
| 419 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 420 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | /* Store away localhost's name before the fork */ |
| 424 | gethostname(LocalHostName, sizeof(LocalHostName)); |
| 425 | if ((p = strchr(LocalHostName, '.'))) { |
| 426 | *p++ = '\0'; |
| 427 | } |
| 428 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 429 | umask(0); |
| 430 | |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 431 | #ifdef BB_KLOGD |
| 432 | /* Start up the klogd process */ |
| 433 | if (startKlogd == TRUE) { |
| 434 | klogd_pid = fork(); |
| 435 | if (klogd_pid == 0) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 436 | daemon_init (argv, "klogd", doKlogd); |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | #endif |
| 440 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 441 | if (doFork == TRUE) { |
| 442 | pid = fork(); |
| 443 | if (pid < 0) |
| 444 | exit(pid); |
| 445 | else if (pid == 0) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 446 | daemon_init (argv, "syslogd", doSyslogd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 447 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 448 | } else { |
| 449 | doSyslogd(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 450 | } |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 451 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 453 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 454 | |
| 455 | /* |
| 456 | * Local Variables |
| 457 | * c-file-style: "linux" |
| 458 | * c-basic-offset: 4 |
| 459 | * tab-width: 4 |
| 460 | * End: |
| 461 | */ |