2 * Mini syslogd implementation for busybox
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <sys/socket.h>
30 #include <sys/types.h>
40 #define ksyslog klogctl
41 extern int ksyslog(int type, char *buf, int len);
44 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
46 #include <sys/syslog.h>
48 /* Path for the file where all log messages are written */
49 #define __LOG_FILE "/var/log/messages"
52 static char* logFilePath = __LOG_FILE;
53 /* interval between marks in seconds */
54 static int MarkInterval = 20*60;
55 /* localhost's name */
56 static char LocalHostName[32];
58 static const char syslogd_usage[] =
59 "syslogd [OPTION]...\n\n"
60 "Linux system and kernel (provides klogd) logging utility.\n"
61 "Note that this version of syslogd/klogd ignores /etc/syslog.conf.\n\n"
63 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
64 "\t-n\tDo not fork into the background (for when run by init)\n"
66 "\t-K\tDo not start up the klogd process (by default syslogd spawns klogd).\n"
68 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
71 /* try to open up the specified device */
72 static int device_open(char *device, int mode)
76 m = mode | O_NONBLOCK;
78 /* Retry up to 5 times */
79 for (f = 0; f < 5; f++)
80 if ((fd = open(device, m)) >= 0)
84 /* Reset original flags. */
86 fcntl(fd, F_SETFL, mode);
90 /* print a message to the log file */
91 static void message(char *fmt, ...)
96 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
97 va_start(arguments, fmt);
98 vdprintf(fd, fmt, arguments);
102 /* Always send console messages to /dev/console so people will see them. */
103 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
104 va_start(arguments, fmt);
105 vdprintf(fd, fmt, arguments);
109 fprintf(stderr, "Bummer, can't print: ");
110 va_start(arguments, fmt);
111 vfprintf(stderr, fmt, arguments);
118 static void logMessage( int pri, char* msg)
125 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
126 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
127 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
128 snprintf (res, sizeof(res), "<%d>", pri);
130 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
132 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
133 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
136 timestamp = ctime(&now) + 4;
137 timestamp[15] = '\0';
140 timestamp[15] = '\0';
144 /* todo: supress duplicates */
146 /* now spew out the message to wherever it is supposed to go */
147 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
150 static void quit_signal(int sig)
152 logMessage(LOG_SYSLOG|LOG_INFO, "System log daemon exiting.");
157 static void restart_signal(int sig)
159 /* pretend to restart */
160 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
163 static void domark(int sig)
165 if (MarkInterval > 0) {
166 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
167 signal(SIGALRM, domark);
172 static void doSyslogd(void)
174 struct sockaddr_un sunx;
181 /* Remove any preexisting socket/file */
184 /* Set up sig handlers */
185 signal(SIGINT, quit_signal);
186 signal(SIGTERM, quit_signal);
187 signal(SIGQUIT, quit_signal);
188 signal(SIGHUP, restart_signal);
189 signal(SIGALRM, domark);
194 memset(&sunx, 0, sizeof(sunx));
195 sunx.sun_family = AF_UNIX; /* Unix domain socket */
196 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
197 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
198 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
202 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
203 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
204 (fchmod(fd, 0666) < 0) || (listen(fd, 5)) )
206 perror("Could not connect to socket " _PATH_LOG);
211 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
212 "BusyBox v" BB_VER " (" BB_BT ")");
215 while ((conn = accept(fd, (struct sockaddr *) &sunx,
218 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
222 int pri = (LOG_USER|LOG_NOTICE);
224 memset (line, 0, sizeof(line));
227 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
229 /* Parse the magic priority number */
231 while (isdigit(*(++p))) {
232 pri = 10 * pri + (*p - '0');
234 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
235 pri = (LOG_USER|LOG_NOTICE);
236 } else if (c == '\n') {
238 } else if (iscntrl(c)&&(c<0177)) {
249 logMessage( pri, line);
259 static void klogd_signal(int sig)
263 logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon exiting.");
267 static void doKlogd(void)
269 int priority=LOG_INFO;
270 char log_buffer[4096];
273 /* Set up sig handlers */
274 signal(SIGINT, klogd_signal);
275 signal(SIGKILL, klogd_signal);
276 signal(SIGTERM, klogd_signal);
277 signal(SIGHUP, klogd_signal);
278 logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: "
279 "BusyBox v" BB_VER " (" BB_BT ")");
284 /* Use kernel syscalls */
285 memset(log_buffer, '\0', sizeof(log_buffer));
286 if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) {
288 if ( errno == EINTR )
290 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
291 "%d - %s.\n", errno, strerror(errno));
292 logMessage(LOG_SYSLOG|LOG_ERR, message);
296 if ( *log_buffer == '<' )
298 switch ( *(log_buffer+1) )
301 priority = LOG_EMERG;
304 priority = LOG_ALERT;
313 priority = LOG_WARNING;
316 priority = LOG_NOTICE;
323 priority = LOG_DEBUG;
327 logMessage(LOG_KERN|priority, logp);
334 extern int syslogd_main(int argc, char **argv)
339 int startKlogd = TRUE;
344 while (--argc > 0 && **(++argv1) == '-') {
345 while (*(++(*argv1))) {
349 usage(syslogd_usage);
351 MarkInterval = atoi(*(++argv1))*60;
363 usage(syslogd_usage);
365 logFilePath = *(++argv1);
368 usage(syslogd_usage);
373 /* Store away localhost's name before the fork */
374 gethostname(LocalHostName, sizeof(LocalHostName));
375 if ( (p = strchr(LocalHostName, '.')) ) {
379 if (doFork == TRUE) {
383 else if ( pid == 0 ) {
384 strncpy(argv[0], "syslogd",strlen(argv[0]));
392 /* Start up the klogd process */
393 if (startKlogd == TRUE) {
395 if (klogd_pid == 0 ) {
396 strncpy(argv[0], "klogd", strlen(argv[0]));