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
25 #include <sys/socket.h>
29 #include <sys/types.h>
39 #define ksyslog klogctl
40 extern int ksyslog(int type, char *buf, int len);
43 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
45 #include <sys/syslog.h>
47 /* Path for the file where all log messages are written */
48 #define __LOG_FILE "/var/log/messages"
51 static char* logFilePath = __LOG_FILE;
52 /* interval between marks in seconds */
53 static int MarkInterval = 20*60;
54 /* localhost's name */
55 static char LocalHostName[32];
57 static const char syslogd_usage[] =
58 "syslogd [OPTION]...\n\n"
59 "Linux system logging utility.\n\n"
61 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
62 "\t-n\tDo not fork into the background (for when run by init)\n"
63 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
67 /* try to open up the specified device */
68 static int device_open(char *device, int mode)
72 m = mode | O_NONBLOCK;
74 /* Retry up to 5 times */
75 for (f = 0; f < 5; f++)
76 if ((fd = open(device, m)) >= 0)
80 /* Reset original flags. */
82 fcntl(fd, F_SETFL, mode);
86 /* print a message to the log file */
87 static void message(char *fmt, ...)
92 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
93 va_start(arguments, fmt);
94 vdprintf(fd, fmt, arguments);
98 /* Always send console messages to /dev/console so people will see them. */
99 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
100 va_start(arguments, fmt);
101 vdprintf(fd, fmt, arguments);
105 fprintf(stderr, "Bummer, can't print: ");
106 va_start(arguments, fmt);
107 vfprintf(stderr, fmt, arguments);
114 static void logMessage( int pri, char* msg)
121 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
122 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
123 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
124 snprintf (res, sizeof(res), "<%d>", pri);
126 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] != ' ')
132 timestamp = ctime(&now) + 4;
133 timestamp[15] = '\0';
136 timestamp[15] = '\0';
140 /* todo: supress duplicates */
142 /* now spew out the message to wherever it is supposed to go */
143 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
146 static void quit_signal(int sig)
148 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd exiting");
152 static void restart_signal(int sig)
154 /* pretend to restart */
155 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
158 static void domark(int sig)
160 if (MarkInterval > 0) {
161 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
162 signal(SIGALRM, domark);
167 static void doSyslogd(void)
169 struct sockaddr_un sunx;
176 /* Remove any preexisting socket/file */
179 /* Set up sig handlers */
180 signal(SIGINT, quit_signal);
181 signal(SIGTERM, quit_signal);
182 signal(SIGQUIT, quit_signal);
183 signal(SIGHUP, restart_signal);
184 signal(SIGALRM, domark);
187 memset(&sunx, 0, sizeof(sunx));
188 sunx.sun_family = AF_UNIX; /* Unix domain socket */
189 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
190 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
191 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
195 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
196 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
197 (chmod(_PATH_LOG, 0666) < 0) ||
200 perror("Could not connect to socket " _PATH_LOG);
205 /* Get localhost's name */
206 gethostname(LocalHostName, sizeof(LocalHostName));
207 if ( (p = strchr(LocalHostName, '.')) ) {
211 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
212 "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
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);
257 static void klogd_signal(int sig)
259 //ksyslog(7, NULL, 0);
261 logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon terminating.");
266 static void doKlogd(void)
268 int priority=LOG_INFO;
270 char log_buffer[4096];
272 /* Set up sig handlers */
273 signal(SIGINT, klogd_signal);
274 signal(SIGKILL, klogd_signal);
275 signal(SIGTERM, klogd_signal);
276 signal(SIGHUP, klogd_signal);
277 logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: "
278 "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
280 //ksyslog(1, NULL, 0);
281 if ( ((stat(_PATH_KLOG, &sb) < 0) && (errno == ENOENT)) ||
282 ( (kmsg = open(_PATH_KLOG, O_RDONLY)) < 0 ) ) {
284 snprintf(message, 79, "klogd: Cannot open %s, " \
285 "%d - %s.\n", _PATH_KLOG, errno, strerror(errno));
286 logMessage(LOG_SYSLOG|LOG_ERR, message);
290 memset(log_buffer, '\0', sizeof(log_buffer));
291 if ( read(kmsg, log_buffer, sizeof(log_buffer)-1) < 0 ) {
293 if ( errno == EINTR )
295 snprintf(message, 79, "klogd: Cannot read proc file system: %d - %s.\n",
296 errno, strerror(errno));
297 logMessage(LOG_SYSLOG|LOG_ERR, message);
301 if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) {
303 if ( errno == EINTR )
305 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
306 "%d - %s.\n", errno, strerror(errno));
307 logMessage(LOG_SYSLOG|LOG_ERR, message);
311 fprintf(stderr, "the kernel says '%s'\n", log_buffer);
312 if ( *log_buffer == '<' )
314 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, log_buffer);
349 extern int syslogd_main(int argc, char **argv)
355 while (--argc > 0 && **(++argv1) == '-') {
356 while (*(++(*argv1))) {
360 usage(syslogd_usage);
362 MarkInterval = atoi(*(++argv1))*60;
369 usage(syslogd_usage);
371 logFilePath = *(++argv1);
374 usage(syslogd_usage);
379 if (doFork == TRUE) {
383 else if ( pid == 0 ) {
384 strncpy(argv[0], "syslogd",strlen(argv[0]));
391 /* Start klogd process */
393 if (klogd_pid == 0 ) {
394 strncpy(argv[0], "klogd", strlen(argv[0]));