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>
43 #define ksyslog klogctl
44 extern int ksyslog(int type, char *buf, int len);
47 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
49 #include <sys/syslog.h>
51 /* Path for the file where all log messages are written */
52 #define __LOG_FILE "/var/log/messages"
54 /* Path to the unix socket */
55 char lfile[PATH_MAX] = "";
57 static char *logFilePath = __LOG_FILE;
59 /* interval between marks in seconds */
60 static int MarkInterval = 20 * 60;
62 /* localhost's name */
63 static char LocalHostName[32];
65 static const char syslogd_usage[] =
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"
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"
73 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
75 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
77 /* Note: There is also a function called "message()" in init.c */
78 /* Print a message to the log file. */
79 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
80 static void message (char *fmt, ...)
85 if ((fd = device_open (logFilePath,
86 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
88 va_start (arguments, fmt);
89 vdprintf (fd, fmt, arguments);
93 /* Always send console messages to /dev/console so people will see them. */
94 if ((fd = device_open (_PATH_CONSOLE,
95 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
96 va_start (arguments, fmt);
97 vdprintf (fd, fmt, arguments);
101 fprintf (stderr, "Bummer, can't print: ");
102 va_start (arguments, fmt);
103 vfprintf (stderr, fmt, arguments);
110 static void logMessage (int pri, char *msg)
114 static char res[20] = "";
118 for (c_fac = facilitynames;
119 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
120 for (c_pri = prioritynames;
121 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
122 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
123 snprintf(res, sizeof(res), "<%d>", pri);
125 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
128 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
129 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
131 timestamp = ctime(&now) + 4;
132 timestamp[15] = '\0';
135 timestamp[15] = '\0';
139 /* todo: supress duplicates */
141 /* now spew out the message to wherever it is supposed to go */
142 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
145 static void quit_signal(int sig)
147 logMessage(0, "System log daemon exiting.");
152 static void domark(int sig)
154 if (MarkInterval > 0) {
155 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
160 static void doSyslogd (void) __attribute__ ((noreturn));
161 static void doSyslogd (void)
163 struct sockaddr_un sunx;
169 char lfile[PATH_MAX];
171 /* Set up signal handlers. */
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);
179 /* Create the syslog file so realpath() can work. */
180 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
181 if (realpath (_PATH_LOG, lfile) == NULL)
182 fatalError ("Could not resolv path to " _PATH_LOG ": %s", strerror (errno));
186 memset (&sunx, 0, sizeof (sunx));
187 sunx.sun_family = AF_UNIX;
188 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
189 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
190 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s", strerror (errno));
192 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
193 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
194 fatalError ("Could not connect to socket " _PATH_LOG ": %s", strerror (errno));
196 if (chmod (lfile, 0666) < 0)
197 fatalError ("Could not set permission on " _PATH_LOG ": %s", strerror (errno));
200 FD_SET (sock_fd, &fds);
202 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
210 memcpy (&readfds, &fds, sizeof (fds));
212 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
213 if (errno == EINTR) continue; /* alarm may have happened. */
214 fatalError ("select error: %s\n", strerror (errno));
217 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
218 if (FD_ISSET (fd, &readfds)) {
222 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
223 fatalError ("accept error: %s\n", strerror (errno));
229 # define BUFSIZE 1023
230 char buf[ BUFSIZE + 1 ];
233 while ((n_read = read (fd, buf, BUFSIZE )) > 0) {
235 int pri = (LOG_USER | LOG_NOTICE);
236 char line[ BUFSIZE + 1 ];
238 char *p = buf, *q = line;
240 buf[ n_read - 1 ] = '\0';
242 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
244 /* Parse the magic priority number. */
246 while (isdigit (*(++p))) {
247 pri = 10 * pri + (*p - '0');
249 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
250 pri = (LOG_USER | LOG_NOTICE);
251 } else if (c == '\n') {
253 } else if (iscntrl (c) && (c < 0177)) {
263 logMessage (pri, line);
275 static void klogd_signal(int sig)
279 logMessage(0, "Kernel log daemon exiting.");
283 static void doKlogd (void) __attribute__ ((noreturn));
284 static void doKlogd (void)
286 int priority = LOG_INFO;
287 char log_buffer[4096];
290 /* Set up sig handlers */
291 signal(SIGINT, klogd_signal);
292 signal(SIGKILL, klogd_signal);
293 signal(SIGTERM, klogd_signal);
294 signal(SIGHUP, SIG_IGN);
295 logMessage(0, "klogd started: "
296 "BusyBox v" BB_VER " (" BB_BT ")");
301 /* Use kernel syscalls */
302 memset(log_buffer, '\0', sizeof(log_buffer));
303 if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) {
308 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
309 "%d - %s.\n", errno, strerror(errno));
310 logMessage(LOG_SYSLOG | LOG_ERR, message);
314 if (*log_buffer == '<') {
315 switch (*(log_buffer + 1)) {
317 priority = LOG_EMERG;
320 priority = LOG_ALERT;
329 priority = LOG_WARNING;
332 priority = LOG_NOTICE;
339 priority = LOG_DEBUG;
343 logMessage(LOG_KERN | priority, logp);
350 static void daemon_init (char **argv, char *dz, void fn (void))
354 strncpy(argv[0], dz, strlen(argv[0]));
359 extern int syslogd_main(int argc, char **argv)
365 int startKlogd = TRUE;
367 int stopDoingThat = FALSE;
371 while (--argc > 0 && **(++argv1) == '-') {
372 stopDoingThat = FALSE;
373 while (stopDoingThat == FALSE && *(++(*argv1))) {
377 usage(syslogd_usage);
379 MarkInterval = atoi(*(++argv1)) * 60;
391 usage(syslogd_usage);
393 logFilePath = *(++argv1);
394 stopDoingThat = TRUE;
397 usage(syslogd_usage);
402 /* Store away localhost's name before the fork */
403 gethostname(LocalHostName, sizeof(LocalHostName));
404 if ((p = strchr(LocalHostName, '.'))) {
411 /* Start up the klogd process */
412 if (startKlogd == TRUE) {
414 if (klogd_pid == 0) {
415 daemon_init (argv, "klogd", doKlogd);
420 if (doFork == TRUE) {
425 daemon_init (argv, "syslogd", doSyslogd);
436 * c-file-style: "linux"