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