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 * 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.
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.
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
34 #include <sys/socket.h>
36 #include <sys/types.h>
41 #define ksyslog klogctl
42 extern int ksyslog(int type, char *buf, int len);
45 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
47 #include <sys/syslog.h>
49 /* Path for the file where all log messages are written */
50 #define __LOG_FILE "/var/log/messages"
52 /* Path to the unix socket */
53 char lfile[PATH_MAX] = "";
55 static char *logFilePath = __LOG_FILE;
57 /* interval between marks in seconds */
58 static int MarkInterval = 20 * 60;
60 /* localhost's name */
61 static char LocalHostName[32];
63 static const char syslogd_usage[] =
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"
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"
71 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
73 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
75 /* Note: There is also a function called "message()" in init.c */
76 /* Print a message to the log file. */
77 static void message(char *fmt, ...)
78 __attribute__ ((format (printf, 1, 2)));
79 static void message(char *fmt, ...)
84 if ( (fd = device_open(logFilePath,
85 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
87 va_start(arguments, fmt);
88 vdprintf(fd, fmt, arguments);
92 /* Always send console messages to /dev/console so people will see them. */
93 if ( (fd = device_open(_PATH_CONSOLE,
94 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
95 va_start(arguments, fmt);
96 vdprintf(fd, fmt, arguments);
100 fprintf(stderr, "Bummer, can't print: ");
101 va_start(arguments, fmt);
102 vfprintf(stderr, fmt, arguments);
109 static void logMessage(int pri, char *msg)
113 static char res[20] = "";
117 for (c_fac = facilitynames;
118 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
119 for (c_pri = prioritynames;
120 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
121 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
122 snprintf(res, sizeof(res), "<%d>", pri);
124 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
127 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
128 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
130 timestamp = ctime(&now) + 4;
131 timestamp[15] = '\0';
134 timestamp[15] = '\0';
138 /* todo: supress duplicates */
140 /* now spew out the message to wherever it is supposed to go */
141 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
144 static void quit_signal(int sig)
146 logMessage(0, "System log daemon exiting.");
151 static void domark(int sig)
153 if (MarkInterval > 0) {
154 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
159 static void doSyslogd (void) __attribute__ ((noreturn));
160 static void doSyslogd (void)
162 struct sockaddr_un sunx;
166 char lfile[PATH_MAX];
168 /* Set up sig handlers */
169 signal (SIGINT, quit_signal);
170 signal (SIGTERM, quit_signal);
171 signal (SIGQUIT, quit_signal);
172 signal (SIGHUP, SIG_IGN);
173 signal (SIGALRM, domark);
174 alarm (MarkInterval);
176 /* create the syslog file so realpath() can work */
177 close(open(_PATH_LOG, O_RDWR | O_CREAT, 0644));
178 if (realpath(_PATH_LOG, lfile) == NULL) {
179 fatalError("Could not resolv path to " _PATH_LOG);
184 memset (&sunx, 0, sizeof(sunx));
186 sunx.sun_family = AF_UNIX;
187 strncpy (sunx.sun_path, lfile, sizeof(sunx.sun_path));
188 if ((sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
189 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG);
192 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
193 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) ||
194 (listen (sock_fd, 5))) {
195 fatalError ("Could not connect to socket " _PATH_LOG);
198 if (chmod (lfile, 0666) < 0) {
199 fatalError ("Could not set permission on " _PATH_LOG);
203 FD_SET (sock_fd, &readfds);
205 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
211 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
212 if (errno == EINTR) continue; /* alarm may have happened. */
213 fatalError( "select error: %s\n", strerror(errno));
216 /* Skip stdin, stdout, stderr */
217 for (fd = 3; fd < FD_SETSIZE; fd++) {
218 if (FD_ISSET (fd, &readfds)) {
221 if ((conn = accept(sock_fd, (struct sockaddr *) &sunx,
223 fatalError( "accept error: %s\n", strerror(errno));
225 FD_SET (conn, &readfds);
228 #define BUFSIZE 1024 + 1
235 /* Keep reading stuff till there is nothing else to read */
236 while( (n_read = read (fd, buf, BUFSIZE)) > 0 && errno != EOF) {
237 int pri = (LOG_USER | LOG_NOTICE);
239 memset (line, 0, sizeof(line));
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)) {
264 logMessage(pri, line);
267 FD_CLR (fd, &readfds);
276 static void klogd_signal(int sig)
280 logMessage(0, "Kernel log daemon exiting.");
284 static void doKlogd (void) __attribute__ ((noreturn));
285 static void doKlogd (void)
287 int priority = LOG_INFO;
288 char log_buffer[4096];
291 /* Set up sig handlers */
292 signal(SIGINT, klogd_signal);
293 signal(SIGKILL, klogd_signal);
294 signal(SIGTERM, klogd_signal);
295 signal(SIGHUP, SIG_IGN);
296 logMessage(0, "klogd started: "
297 "BusyBox v" BB_VER " (" BB_BT ")");
302 /* Use kernel syscalls */
303 memset(log_buffer, '\0', sizeof(log_buffer));
304 if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) {
309 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
310 "%d - %s.\n", errno, strerror(errno));
311 logMessage(LOG_SYSLOG | LOG_ERR, message);
315 if (*log_buffer == '<') {
316 switch (*(log_buffer + 1)) {
318 priority = LOG_EMERG;
321 priority = LOG_ALERT;
330 priority = LOG_WARNING;
333 priority = LOG_NOTICE;
340 priority = LOG_DEBUG;
344 logMessage(LOG_KERN | priority, logp);
351 static void daemon_init (char **argv, char *dz, void fn (void)) __attribute__ ((noreturn));
352 static void daemon_init (char **argv, char *dz, void fn (void))
356 strncpy(argv[0], dz, strlen(argv[0]));
361 extern int syslogd_main(int argc, char **argv)
367 int startKlogd = TRUE;
369 int stopDoingThat = FALSE;
373 while (--argc > 0 && **(++argv1) == '-') {
374 stopDoingThat = FALSE;
375 while (stopDoingThat == FALSE && *(++(*argv1))) {
379 usage(syslogd_usage);
381 MarkInterval = atoi(*(++argv1)) * 60;
393 usage(syslogd_usage);
395 logFilePath = *(++argv1);
396 stopDoingThat = TRUE;
399 usage(syslogd_usage);
404 /* Store away localhost's name before the fork */
405 gethostname(LocalHostName, sizeof(LocalHostName));
406 if ((p = strchr(LocalHostName, '.'))) {
413 /* Start up the klogd process */
414 if (startKlogd == TRUE) {
416 if (klogd_pid == 0) {
417 daemon_init (argv, "klogd", doKlogd);
422 if (doFork == TRUE) {
427 daemon_init (argv, "syslogd", doSyslogd);
438 * c-file-style: "linux"