1 /* vi: set sw=4 ts=4: */
3 * Mini syslogd implementation for busybox
5 * Copyright (C) 1999 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
27 #include <sys/socket.h>
31 #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"
53 static char *logFilePath = __LOG_FILE;
55 /* interval between marks in seconds */
56 static int MarkInterval = 20 * 60;
58 /* localhost's name */
59 static char LocalHostName[32];
61 static const char syslogd_usage[] =
62 "syslogd [OPTION]...\n\n"
63 "Linux system and kernel (provides klogd) logging utility.\n"
64 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
66 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
67 "\t-n\tDo not fork into the background (for when run by init)\n"
69 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
71 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
74 /* Note: There is also a function called "message()" in init.c */
75 /* print a message to the log file */
76 static void message(char *fmt, ...)
83 device_open(logFilePath,
84 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
86 va_start(arguments, fmt);
87 vdprintf(fd, fmt, arguments);
91 /* Always send console messages to /dev/console so people will see them. */
94 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)
162 struct sockaddr_un sunx;
169 /* Set up sig handlers */
170 signal(SIGINT, quit_signal);
171 signal(SIGTERM, quit_signal);
172 signal(SIGQUIT, quit_signal);
173 signal(SIGALRM, domark);
174 signal(SIGHUP, SIG_IGN);
177 /* Remove any preexisting socket/file */
180 memset(&sunx, 0, sizeof(sunx));
181 sunx.sun_family = AF_UNIX; /* Unix domain socket */
182 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
183 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
184 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
188 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
189 if ((bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
191 perror("Could not connect to socket " _PATH_LOG);
196 if (chmod(_PATH_LOG, 0666) < 0) {
197 perror("Could not set permission on " _PATH_LOG);
201 logMessage(0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
204 while ((conn = accept(fd, (struct sockaddr *) &sunx,
205 &addrLength)) >= 0) {
206 while ((readSize = read(conn, buf, sizeof(buf))) > 0) {
209 int pri = (LOG_USER | LOG_NOTICE);
211 memset(line, 0, sizeof(line));
214 while (p && (c = *p) && q < &line[sizeof(line) - 1]) {
216 /* Parse the magic priority number */
218 while (isdigit(*(++p))) {
219 pri = 10 * pri + (*p - '0');
221 if (pri & ~(LOG_FACMASK | LOG_PRIMASK))
222 pri = (LOG_USER | LOG_NOTICE);
223 } else if (c == '\n') {
225 } else if (iscntrl(c) && (c < 0177)) {
236 logMessage(pri, line);
246 static void klogd_signal(int sig)
250 logMessage(0, "Kernel log daemon exiting.");
254 static void doKlogd(void)
256 int priority = LOG_INFO;
257 char log_buffer[4096];
260 /* Set up sig handlers */
261 signal(SIGINT, klogd_signal);
262 signal(SIGKILL, klogd_signal);
263 signal(SIGTERM, klogd_signal);
264 signal(SIGHUP, SIG_IGN);
265 logMessage(0, "klogd started: "
266 "BusyBox v" BB_VER " (" BB_BT ")");
271 /* Use kernel syscalls */
272 memset(log_buffer, '\0', sizeof(log_buffer));
273 if (ksyslog(2, log_buffer, sizeof(log_buffer)) < 0) {
278 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
279 "%d - %s.\n", errno, strerror(errno));
280 logMessage(LOG_SYSLOG | LOG_ERR, message);
284 if (*log_buffer == '<') {
285 switch (*(log_buffer + 1)) {
287 priority = LOG_EMERG;
290 priority = LOG_ALERT;
299 priority = LOG_WARNING;
302 priority = LOG_NOTICE;
309 priority = LOG_DEBUG;
313 logMessage(LOG_KERN | priority, logp);
320 extern int syslogd_main(int argc, char **argv)
326 int startKlogd = TRUE;
328 int stopDoingThat = FALSE;
332 while (--argc > 0 && **(++argv1) == '-') {
333 stopDoingThat = FALSE;
334 while (stopDoingThat == FALSE && *(++(*argv1))) {
338 usage(syslogd_usage);
340 MarkInterval = atoi(*(++argv1)) * 60;
352 usage(syslogd_usage);
354 logFilePath = *(++argv1);
355 stopDoingThat = TRUE;
358 usage(syslogd_usage);
363 /* Store away localhost's name before the fork */
364 gethostname(LocalHostName, sizeof(LocalHostName));
365 if ((p = strchr(LocalHostName, '.'))) {
370 /* Start up the klogd process */
371 if (startKlogd == TRUE) {
373 if (klogd_pid == 0) {
374 strncpy(argv[0], "klogd", strlen(argv[0]));
380 if (doFork == TRUE) {
385 strncpy(argv[0], "syslogd", strlen(argv[0]));