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, ...) |
| 78 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | int fd; |
| 80 | va_list arguments; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 81 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | if ( |
| 83 | (fd = |
| 84 | device_open(logFilePath, |
| 85 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 86 | O_NONBLOCK)) >= 0) { |
| 87 | va_start(arguments, fmt); |
| 88 | vdprintf(fd, fmt, arguments); |
| 89 | va_end(arguments); |
| 90 | close(fd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 91 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 92 | /* Always send console messages to /dev/console so people will see them. */ |
| 93 | if ( |
| 94 | (fd = |
| 95 | device_open(_PATH_CONSOLE, |
| 96 | O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { |
| 97 | va_start(arguments, fmt); |
| 98 | vdprintf(fd, fmt, arguments); |
| 99 | va_end(arguments); |
| 100 | close(fd); |
| 101 | } else { |
| 102 | fprintf(stderr, "Bummer, can't print: "); |
| 103 | va_start(arguments, fmt); |
| 104 | vfprintf(stderr, fmt, arguments); |
| 105 | fflush(stderr); |
| 106 | va_end(arguments); |
| 107 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 108 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 111 | static void logMessage(int pri, char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 112 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 113 | time_t now; |
| 114 | char *timestamp; |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 115 | static char res[20] = ""; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 116 | CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 117 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 118 | if (pri != 0) { |
| 119 | for (c_fac = facilitynames; |
| 120 | c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); |
| 121 | for (c_pri = prioritynames; |
| 122 | c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); |
| 123 | if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0') |
| 124 | snprintf(res, sizeof(res), "<%d>", pri); |
| 125 | else |
| 126 | snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); |
| 127 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 128 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 129 | if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || |
| 130 | msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') { |
| 131 | time(&now); |
| 132 | timestamp = ctime(&now) + 4; |
| 133 | timestamp[15] = '\0'; |
| 134 | } else { |
| 135 | timestamp = msg; |
| 136 | timestamp[15] = '\0'; |
| 137 | msg += 16; |
| 138 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 139 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 140 | /* todo: supress duplicates */ |
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 | /* now spew out the message to wherever it is supposed to go */ |
| 143 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void quit_signal(int sig) |
| 147 | { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 148 | logMessage(0, "System log daemon exiting."); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 149 | unlink(lfile); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 150 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 153 | static void domark(int sig) |
| 154 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 155 | if (MarkInterval > 0) { |
| 156 | logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --"); |
| 157 | alarm(MarkInterval); |
| 158 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 161 | static void doSyslogd (void) __attribute__ ((noreturn)); |
| 162 | static void doSyslogd (void) |
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 | struct sockaddr_un sunx; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 165 | size_t addrLength; |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 166 | int sock_fd; |
| 167 | fd_set readfds; |
| 168 | char lfile[PATH_MAX]; |
| 169 | int t = readlink(_PATH_LOG, lfile, sizeof(lfile) - 1); /* Resolve symlinks */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 170 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 171 | /* Set up sig handlers */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 172 | signal (SIGINT, quit_signal); |
| 173 | signal (SIGTERM, quit_signal); |
| 174 | signal (SIGQUIT, quit_signal); |
| 175 | signal (SIGHUP, SIG_IGN); |
| 176 | signal (SIGALRM, domark); |
| 177 | alarm (MarkInterval); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 178 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 179 | if (t == -1) |
| 180 | strncpy(lfile, _PATH_LOG, sizeof(lfile)); |
| 181 | else |
| 182 | lfile[t] = '\0'; |
Eric Andersen | 14ec6cf | 1999-12-05 22:17:02 +0000 | [diff] [blame] | 183 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 184 | unlink (lfile); |
| 185 | |
| 186 | memset (&sunx, 0, sizeof(sunx)); |
| 187 | |
| 188 | sunx.sun_family = AF_UNIX; |
| 189 | strncpy (sunx.sun_path, lfile, sizeof(sunx.sun_path)); |
| 190 | if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { |
| 191 | perror ("Couldn't obtain descriptor for socket " _PATH_LOG); |
| 192 | exit (FALSE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 193 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 194 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 195 | addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path); |
| 196 | if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || |
| 197 | (listen (sock_fd, 5))) { |
| 198 | perror ("Could not connect to socket " _PATH_LOG); |
| 199 | exit (FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 202 | if (chmod (lfile, 0666) < 0) { |
| 203 | perror ("Could not set permission on " _PATH_LOG); |
| 204 | exit (FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 207 | FD_ZERO (&readfds); |
| 208 | FD_SET (sock_fd, &readfds); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 209 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 210 | logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); |
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 | for (;;) { |
| 213 | int n_ready; |
| 214 | int fd; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 215 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 216 | if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) { |
| 217 | if (errno == EINTR) continue; /* alarm may have happened. */ |
| 218 | perror ("select"); |
| 219 | exit (FALSE); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 220 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 221 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 222 | /* Skip stdin, stdout, stderr */ |
| 223 | for (fd = 3; fd <= FD_SETSIZE; fd++) { |
| 224 | if (FD_ISSET (fd, &readfds)) { |
| 225 | if (fd == sock_fd) { |
| 226 | int conn; |
| 227 | if ((conn = accept(sock_fd, (struct sockaddr *) &sunx, |
| 228 | &addrLength)) < 0) { |
| 229 | perror ("accept"); |
| 230 | exit (FALSE); /* #### ??? */ |
| 231 | } |
| 232 | FD_SET (conn, &readfds); |
| 233 | } |
| 234 | else { |
| 235 | #define BUFSIZE 1024 + 1 |
| 236 | char buf[BUFSIZE]; |
| 237 | char *q, *p; |
| 238 | int n_read; |
| 239 | |
| 240 | n_read = read (fd, buf, BUFSIZE); |
| 241 | |
| 242 | if (n_read < 0) { |
| 243 | perror ("read error"); |
| 244 | goto close_fd; |
| 245 | } |
| 246 | else if (n_read > 0) { |
| 247 | char line[BUFSIZE]; |
| 248 | unsigned char c; |
| 249 | int pri = (LOG_USER | LOG_NOTICE); |
| 250 | |
| 251 | memset (line, 0, sizeof(line)); |
| 252 | p = buf; |
| 253 | q = line; |
| 254 | while (p && (c = *p) && q < &line[sizeof(line) - 1]) { |
| 255 | if (c == '<') { |
| 256 | /* Parse the magic priority number */ |
| 257 | pri = 0; |
| 258 | while (isdigit(*(++p))) { |
| 259 | pri = 10 * pri + (*p - '0'); |
| 260 | } |
| 261 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) |
| 262 | pri = (LOG_USER | LOG_NOTICE); |
| 263 | } else if (c == '\n') { |
| 264 | *q++ = ' '; |
| 265 | } else if (iscntrl(c) && (c < 0177)) { |
| 266 | *q++ = '^'; |
| 267 | *q++ = c ^ 0100; |
| 268 | } else { |
| 269 | *q++ = c; |
| 270 | } |
| 271 | p++; |
| 272 | } |
| 273 | *q = '\0'; |
| 274 | |
| 275 | /* Now log it */ |
| 276 | logMessage(pri, line); |
| 277 | |
| 278 | close_fd: |
| 279 | close (fd); |
| 280 | FD_CLR (fd, &readfds); |
| 281 | } |
| 282 | else { /* EOF */ |
| 283 | goto close_fd; |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 291 | #ifdef BB_KLOGD |
| 292 | |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 293 | static void klogd_signal(int sig) |
| 294 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 295 | ksyslog(7, NULL, 0); |
| 296 | ksyslog(0, 0, 0); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 297 | logMessage(0, "Kernel log daemon exiting."); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 298 | exit(TRUE); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 301 | static void doKlogd (void) __attribute__ ((noreturn)); |
| 302 | static void doKlogd (void) |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 303 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 304 | int priority = LOG_INFO; |
| 305 | char log_buffer[4096]; |
| 306 | char *logp; |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 307 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 308 | /* Set up sig handlers */ |
| 309 | signal(SIGINT, klogd_signal); |
| 310 | signal(SIGKILL, klogd_signal); |
| 311 | signal(SIGTERM, klogd_signal); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 312 | signal(SIGHUP, SIG_IGN); |
| 313 | logMessage(0, "klogd started: " |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 314 | "BusyBox v" BB_VER " (" BB_BT ")"); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 315 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 316 | ksyslog(1, NULL, 0); |
Eric Andersen | 0ecb54a | 1999-12-05 23:24:55 +0000 | [diff] [blame] | 317 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 318 | while (1) { |
| 319 | /* Use kernel syscalls */ |
| 320 | memset(log_buffer, '\0', sizeof(log_buffer)); |
| 321 | if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) { |
| 322 | char message[80]; |
| 323 | |
| 324 | if (errno == EINTR) |
| 325 | continue; |
| 326 | snprintf(message, 79, "klogd: Error return from sys_sycall: " \ |
| 327 | "%d - %s.\n", errno, strerror(errno)); |
| 328 | logMessage(LOG_SYSLOG | LOG_ERR, message); |
| 329 | exit(1); |
| 330 | } |
| 331 | logp = log_buffer; |
| 332 | if (*log_buffer == '<') { |
| 333 | switch (*(log_buffer + 1)) { |
| 334 | case '0': |
| 335 | priority = LOG_EMERG; |
| 336 | break; |
| 337 | case '1': |
| 338 | priority = LOG_ALERT; |
| 339 | break; |
| 340 | case '2': |
| 341 | priority = LOG_CRIT; |
| 342 | break; |
| 343 | case '3': |
| 344 | priority = LOG_ERR; |
| 345 | break; |
| 346 | case '4': |
| 347 | priority = LOG_WARNING; |
| 348 | break; |
| 349 | case '5': |
| 350 | priority = LOG_NOTICE; |
| 351 | break; |
| 352 | case '6': |
| 353 | priority = LOG_INFO; |
| 354 | break; |
| 355 | case '7': |
| 356 | default: |
| 357 | priority = LOG_DEBUG; |
| 358 | } |
| 359 | logp += 3; |
| 360 | } |
| 361 | logMessage(LOG_KERN | priority, logp); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 362 | } |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 363 | |
| 364 | } |
| 365 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 366 | #endif |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 367 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 368 | static void daemon_init (char **argv, char *dz, void fn (void)) __attribute__ ((noreturn)); |
| 369 | static void daemon_init (char **argv, char *dz, void fn (void)) |
| 370 | { |
| 371 | setsid(); |
| 372 | chdir ("/"); |
| 373 | strncpy(argv[0], dz, strlen(argv[0])); |
| 374 | fn(); |
| 375 | exit(0); |
| 376 | } |
| 377 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 378 | extern int syslogd_main(int argc, char **argv) |
| 379 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 380 | int pid, klogd_pid; |
| 381 | int doFork = TRUE; |
| 382 | |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 383 | #ifdef BB_KLOGD |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 384 | int startKlogd = TRUE; |
Eric Andersen | 2cb5507 | 1999-12-10 08:25:07 +0000 | [diff] [blame] | 385 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | int stopDoingThat = FALSE; |
| 387 | char *p; |
| 388 | char **argv1 = argv; |
| 389 | |
| 390 | while (--argc > 0 && **(++argv1) == '-') { |
| 391 | stopDoingThat = FALSE; |
| 392 | while (stopDoingThat == FALSE && *(++(*argv1))) { |
| 393 | switch (**argv1) { |
| 394 | case 'm': |
| 395 | if (--argc == 0) { |
| 396 | usage(syslogd_usage); |
| 397 | } |
| 398 | MarkInterval = atoi(*(++argv1)) * 60; |
| 399 | break; |
| 400 | case 'n': |
| 401 | doFork = FALSE; |
| 402 | break; |
| 403 | #ifdef BB_KLOGD |
| 404 | case 'K': |
| 405 | startKlogd = FALSE; |
| 406 | break; |
| 407 | #endif |
| 408 | case 'O': |
| 409 | if (--argc == 0) { |
| 410 | usage(syslogd_usage); |
| 411 | } |
| 412 | logFilePath = *(++argv1); |
| 413 | stopDoingThat = TRUE; |
| 414 | break; |
| 415 | default: |
| 416 | usage(syslogd_usage); |
| 417 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 418 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /* Store away localhost's name before the fork */ |
| 422 | gethostname(LocalHostName, sizeof(LocalHostName)); |
| 423 | if ((p = strchr(LocalHostName, '.'))) { |
| 424 | *p++ = '\0'; |
| 425 | } |
| 426 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 427 | umask(0); |
| 428 | |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 429 | #ifdef BB_KLOGD |
| 430 | /* Start up the klogd process */ |
| 431 | if (startKlogd == TRUE) { |
| 432 | klogd_pid = fork(); |
| 433 | if (klogd_pid == 0) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 434 | daemon_init (argv, "klogd", doKlogd); |
Erik Andersen | e272915 | 2000-02-18 21:34:17 +0000 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | #endif |
| 438 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 439 | if (doFork == TRUE) { |
| 440 | pid = fork(); |
| 441 | if (pid < 0) |
| 442 | exit(pid); |
| 443 | else if (pid == 0) { |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 444 | daemon_init (argv, "syslogd", doSyslogd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 445 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 446 | } else { |
| 447 | doSyslogd(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 448 | } |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 449 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 450 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 451 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 452 | |
| 453 | /* |
| 454 | * Local Variables |
| 455 | * c-file-style: "linux" |
| 456 | * c-basic-offset: 4 |
| 457 | * tab-width: 4 |
| 458 | * End: |
| 459 | */ |