1 /* vi: set sw=4 ts=4: */
3 * Mini syslogd implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
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.
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.
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
38 #include <sys/socket.h>
40 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <linux/unistd.h>
45 #if __GNU_LIBRARY__ < 5
47 typedef unsigned int socklen_t;
50 # define __NR_klogctl __NR_syslog
51 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
53 #define klogctl syslog
57 # include <sys/klog.h>
62 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
64 #include <sys/syslog.h>
66 /* Path for the file where all log messages are written */
67 #define __LOG_FILE "/var/log/messages"
69 /* Path to the unix socket */
70 char lfile[BUFSIZ] = "";
72 static char *logFilePath = __LOG_FILE;
74 /* interval between marks in seconds */
75 static int MarkInterval = 20 * 60;
77 /* localhost's name */
78 static char LocalHostName[32];
80 static const char syslogd_usage[] =
81 "syslogd [OPTION]...\n"
82 #ifndef BB_FEATURE_TRIVIAL_HELP
83 "\nLinux system and kernel (provides klogd) logging utility.\n"
84 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
86 "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n"
87 "\t-n\t\tRun as a foreground process\n"
89 "\t-K\t\tDo not start up the klogd process\n"
91 "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)\n"
95 /* Note: There is also a function called "message()" in init.c */
96 /* Print a message to the log file. */
97 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
98 static void message (char *fmt, ...)
104 fl.l_whence = SEEK_SET;
108 if ((fd = device_open (logFilePath,
109 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
112 fcntl (fd, F_SETLKW, &fl);
113 va_start (arguments, fmt);
114 vdprintf (fd, fmt, arguments);
117 fcntl (fd, F_SETLKW, &fl);
120 /* Always send console messages to /dev/console so people will see them. */
121 if ((fd = device_open (_PATH_CONSOLE,
122 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
123 va_start (arguments, fmt);
124 vdprintf (fd, fmt, arguments);
128 fprintf (stderr, "Bummer, can't print: ");
129 va_start (arguments, fmt);
130 vfprintf (stderr, fmt, arguments);
137 static void logMessage (int pri, char *msg)
141 static char res[20] = "";
145 for (c_fac = facilitynames;
146 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
147 for (c_pri = prioritynames;
148 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
149 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
150 snprintf(res, sizeof(res), "<%d>", pri);
152 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
155 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
156 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
158 timestamp = ctime(&now) + 4;
159 timestamp[15] = '\0';
162 timestamp[15] = '\0';
166 /* todo: supress duplicates */
168 /* now spew out the message to wherever it is supposed to go */
169 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
172 static void quit_signal(int sig)
174 logMessage(0, "System log daemon exiting.");
179 static void domark(int sig)
181 if (MarkInterval > 0) {
182 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
188 static int serveConnection (int conn)
190 char buf[ BUFSIZE + 1 ];
193 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
195 int pri = (LOG_USER | LOG_NOTICE);
196 char line[ BUFSIZE + 1 ];
199 char *p = buf, *q = line;
201 buf[ n_read - 1 ] = '\0';
203 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
205 /* Parse the magic priority number. */
207 while (isdigit (*(++p))) {
208 pri = 10 * pri + (*p - '0');
210 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
211 pri = (LOG_USER | LOG_NOTICE);
212 } else if (c == '\n') {
214 } else if (iscntrl (c) && (c < 0177)) {
224 logMessage (pri, line);
229 static void doSyslogd (void) __attribute__ ((noreturn));
230 static void doSyslogd (void)
232 struct sockaddr_un sunx;
233 socklen_t addrLength;
241 /* Set up signal handlers. */
242 signal (SIGINT, quit_signal);
243 signal (SIGTERM, quit_signal);
244 signal (SIGQUIT, quit_signal);
245 signal (SIGHUP, SIG_IGN);
246 signal (SIGCLD, SIG_IGN);
247 signal (SIGALRM, domark);
248 alarm (MarkInterval);
250 /* Create the syslog file so realpath() can work. */
251 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
252 if (realpath (_PATH_LOG, lfile) == NULL)
253 fatalError ("Could not resolv path to " _PATH_LOG ": %s\n", strerror (errno));
257 memset (&sunx, 0, sizeof (sunx));
258 sunx.sun_family = AF_UNIX;
259 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
260 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
261 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
263 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
264 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
265 fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
267 if (chmod (lfile, 0666) < 0)
268 fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
271 FD_SET (sock_fd, &fds);
273 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
281 memcpy (&readfds, &fds, sizeof (fds));
283 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
284 if (errno == EINTR) continue; /* alarm may have happened. */
285 fatalError ("select error: %s\n", strerror (errno));
288 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
289 if (FD_ISSET (fd, &readfds)) {
298 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
299 fatalError ("accept error: %s\n", strerror (errno));
305 perror ("syslogd: fork");
311 serveConnection (conn);
321 static void klogd_signal(int sig)
325 logMessage(0, "Kernel log daemon exiting.");
329 static void doKlogd (void) __attribute__ ((noreturn));
330 static void doKlogd (void)
332 int priority = LOG_INFO;
333 char log_buffer[4096];
336 /* Set up sig handlers */
337 signal(SIGINT, klogd_signal);
338 signal(SIGKILL, klogd_signal);
339 signal(SIGTERM, klogd_signal);
340 signal(SIGHUP, SIG_IGN);
341 logMessage(0, "klogd started: "
342 "BusyBox v" BB_VER " (" BB_BT ")");
347 /* Use kernel syscalls */
348 memset(log_buffer, '\0', sizeof(log_buffer));
349 if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
354 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
355 "%d - %s.\n", errno, strerror(errno));
356 logMessage(LOG_SYSLOG | LOG_ERR, message);
360 if (*log_buffer == '<') {
361 switch (*(log_buffer + 1)) {
363 priority = LOG_EMERG;
366 priority = LOG_ALERT;
375 priority = LOG_WARNING;
378 priority = LOG_NOTICE;
385 priority = LOG_DEBUG;
389 logMessage(LOG_KERN | priority, logp);
396 static void daemon_init (char **argv, char *dz, void fn (void))
400 strncpy(argv[0], dz, strlen(argv[0]));
405 extern int syslogd_main(int argc, char **argv)
411 int startKlogd = TRUE;
413 int stopDoingThat = FALSE;
417 while (--argc > 0 && **(++argv1) == '-') {
418 stopDoingThat = FALSE;
419 while (stopDoingThat == FALSE && *(++(*argv1))) {
423 usage(syslogd_usage);
425 MarkInterval = atoi(*(++argv1)) * 60;
437 usage(syslogd_usage);
439 logFilePath = *(++argv1);
440 stopDoingThat = TRUE;
443 usage(syslogd_usage);
449 usage(syslogd_usage);
451 /* Store away localhost's name before the fork */
452 gethostname(LocalHostName, sizeof(LocalHostName));
453 if ((p = strchr(LocalHostName, '.'))) {
460 /* Start up the klogd process */
461 if (startKlogd == TRUE) {
463 if (klogd_pid == 0) {
464 daemon_init (argv, "klogd", doKlogd);
469 if (doFork == TRUE) {
474 daemon_init (argv, "syslogd", doSyslogd);
485 c-file-style: "linux"