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
36 #include <sys/socket.h>
38 #include <sys/types.h>
40 #include <sys/param.h>
44 #define ksyslog klogctl
45 extern int ksyslog(int type, char *buf, int len);
48 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
50 #include <sys/syslog.h>
52 /* Path for the file where all log messages are written */
53 #define __LOG_FILE "/var/log/messages"
55 /* Path to the unix socket */
56 char lfile[BUFSIZ] = "";
58 static char *logFilePath = __LOG_FILE;
60 /* interval between marks in seconds */
61 static int MarkInterval = 20 * 60;
63 /* localhost's name */
64 static char LocalHostName[32];
66 static const char syslogd_usage[] =
67 "syslogd [OPTION]...\n"
68 #ifndef BB_FEATURE_TRIVIAL_HELP
69 "\nLinux system and kernel (provides klogd) logging utility.\n"
70 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
72 "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n"
73 "\t-n\t\tRun as a foreground process\n"
75 "\t-K\t\tDo not start up the klogd process\n"
77 "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)\n"
81 /* Note: There is also a function called "message()" in init.c */
82 /* Print a message to the log file. */
83 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
84 static void message (char *fmt, ...)
90 fl.l_whence = SEEK_SET;
94 if ((fd = device_open (logFilePath,
95 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
98 fcntl (fd, F_SETLKW, &fl);
99 va_start (arguments, fmt);
100 vdprintf (fd, fmt, arguments);
103 fcntl (fd, F_SETLKW, &fl);
106 /* Always send console messages to /dev/console so people will see them. */
107 if ((fd = device_open (_PATH_CONSOLE,
108 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
109 va_start (arguments, fmt);
110 vdprintf (fd, fmt, arguments);
114 fprintf (stderr, "Bummer, can't print: ");
115 va_start (arguments, fmt);
116 vfprintf (stderr, fmt, arguments);
123 static void logMessage (int pri, char *msg)
127 static char res[20] = "";
131 for (c_fac = facilitynames;
132 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
133 for (c_pri = prioritynames;
134 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
135 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
136 snprintf(res, sizeof(res), "<%d>", pri);
138 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
141 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
142 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
144 timestamp = ctime(&now) + 4;
145 timestamp[15] = '\0';
148 timestamp[15] = '\0';
152 /* todo: supress duplicates */
154 /* now spew out the message to wherever it is supposed to go */
155 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
158 static void quit_signal(int sig)
160 logMessage(0, "System log daemon exiting.");
165 static void domark(int sig)
167 if (MarkInterval > 0) {
168 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
174 static void serveConnection (int conn) __attribute__ ((noreturn));
175 static void serveConnection (int conn)
177 char buf[ BUFSIZE + 1 ];
180 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
182 int pri = (LOG_USER | LOG_NOTICE);
183 char line[ BUFSIZE + 1 ];
186 char *p = buf, *q = line;
188 buf[ n_read - 1 ] = '\0';
190 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
192 /* Parse the magic priority number. */
194 while (isdigit (*(++p))) {
195 pri = 10 * pri + (*p - '0');
197 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
198 pri = (LOG_USER | LOG_NOTICE);
199 } else if (c == '\n') {
201 } else if (iscntrl (c) && (c < 0177)) {
211 logMessage (pri, line);
216 static void doSyslogd (void) __attribute__ ((noreturn));
217 static void doSyslogd (void)
219 struct sockaddr_un sunx;
220 socklen_t addrLength;
228 /* Set up signal handlers. */
229 signal (SIGINT, quit_signal);
230 signal (SIGTERM, quit_signal);
231 signal (SIGQUIT, quit_signal);
232 signal (SIGHUP, SIG_IGN);
233 signal (SIGCLD, SIG_IGN);
234 signal (SIGALRM, domark);
235 alarm (MarkInterval);
237 /* Create the syslog file so realpath() can work. */
238 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
239 if (realpath (_PATH_LOG, lfile) == NULL)
240 fatalError ("Could not resolv path to " _PATH_LOG ": %s\n", strerror (errno));
244 memset (&sunx, 0, sizeof (sunx));
245 sunx.sun_family = AF_UNIX;
246 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
247 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
248 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
250 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
251 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
252 fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
254 if (chmod (lfile, 0666) < 0)
255 fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
258 FD_SET (sock_fd, &fds);
260 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
268 memcpy (&readfds, &fds, sizeof (fds));
270 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
271 if (errno == EINTR) continue; /* alarm may have happened. */
272 fatalError ("select error: %s\n", strerror (errno));
275 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
276 if (FD_ISSET (fd, &readfds)) {
285 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
286 fatalError ("accept error: %s\n", strerror (errno));
292 perror ("syslogd: fork");
298 serveConnection (conn);
308 static void klogd_signal(int sig)
312 logMessage(0, "Kernel log daemon exiting.");
316 static void doKlogd (void) __attribute__ ((noreturn));
317 static void doKlogd (void)
319 int priority = LOG_INFO;
320 char log_buffer[4096];
323 /* Set up sig handlers */
324 signal(SIGINT, klogd_signal);
325 signal(SIGKILL, klogd_signal);
326 signal(SIGTERM, klogd_signal);
327 signal(SIGHUP, SIG_IGN);
328 logMessage(0, "klogd started: "
329 "BusyBox v" BB_VER " (" BB_BT ")");
334 /* Use kernel syscalls */
335 memset(log_buffer, '\0', sizeof(log_buffer));
336 if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) {
341 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
342 "%d - %s.\n", errno, strerror(errno));
343 logMessage(LOG_SYSLOG | LOG_ERR, message);
347 if (*log_buffer == '<') {
348 switch (*(log_buffer + 1)) {
350 priority = LOG_EMERG;
353 priority = LOG_ALERT;
362 priority = LOG_WARNING;
365 priority = LOG_NOTICE;
372 priority = LOG_DEBUG;
376 logMessage(LOG_KERN | priority, logp);
383 static void daemon_init (char **argv, char *dz, void fn (void))
387 strncpy(argv[0], dz, strlen(argv[0]));
392 extern int syslogd_main(int argc, char **argv)
398 int startKlogd = TRUE;
400 int stopDoingThat = FALSE;
404 while (--argc > 0 && **(++argv1) == '-') {
405 stopDoingThat = FALSE;
406 while (stopDoingThat == FALSE && *(++(*argv1))) {
410 usage(syslogd_usage);
412 MarkInterval = atoi(*(++argv1)) * 60;
424 usage(syslogd_usage);
426 logFilePath = *(++argv1);
427 stopDoingThat = TRUE;
430 usage(syslogd_usage);
436 usage(syslogd_usage);
438 /* Store away localhost's name before the fork */
439 gethostname(LocalHostName, sizeof(LocalHostName));
440 if ((p = strchr(LocalHostName, '.'))) {
447 /* Start up the klogd process */
448 if (startKlogd == TRUE) {
450 if (klogd_pid == 0) {
451 daemon_init (argv, "klogd", doKlogd);
456 if (doFork == TRUE) {
461 daemon_init (argv, "syslogd", doSyslogd);
472 c-file-style: "linux"