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 logging utility.\n\n"
62 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
63 "\t-n\tDo not fork into the background (for when run by init)\n"
64 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
68 /* try to open up the specified device */
69 static int device_open(char *device, int mode)
73 m = mode | O_NONBLOCK;
75 /* Retry up to 5 times */
76 for (f = 0; f < 5; f++)
77 if ((fd = open(device, m)) >= 0)
81 /* Reset original flags. */
83 fcntl(fd, F_SETFL, mode);
87 /* print a message to the log file */
88 static void message(char *fmt, ...)
93 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
94 va_start(arguments, fmt);
95 vdprintf(fd, fmt, arguments);
99 /* Always send console messages to /dev/console so people will see them. */
100 if ((fd = device_open(_PATH_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
101 va_start(arguments, fmt);
102 vdprintf(fd, fmt, arguments);
106 fprintf(stderr, "Bummer, can't print: ");
107 va_start(arguments, fmt);
108 vfprintf(stderr, fmt, arguments);
115 static void logMessage( int pri, char* msg)
122 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
123 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
124 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
125 snprintf (res, sizeof(res), "<%d>", pri);
127 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
129 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
130 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
133 timestamp = ctime(&now) + 4;
134 timestamp[15] = '\0';
137 timestamp[15] = '\0';
141 /* todo: supress duplicates */
143 /* now spew out the message to wherever it is supposed to go */
144 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
147 static void quit_signal(int sig)
149 logMessage(LOG_SYSLOG|LOG_INFO, "System log daemon exiting.");
154 static void restart_signal(int sig)
156 /* pretend to restart */
157 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
160 static void domark(int sig)
162 if (MarkInterval > 0) {
163 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
164 signal(SIGALRM, domark);
169 static void doSyslogd(void)
171 struct sockaddr_un sunx;
178 /* Remove any preexisting socket/file */
181 /* Set up sig handlers */
182 signal(SIGINT, quit_signal);
183 signal(SIGTERM, quit_signal);
184 signal(SIGQUIT, quit_signal);
185 signal(SIGHUP, restart_signal);
186 signal(SIGALRM, domark);
191 memset(&sunx, 0, sizeof(sunx));
192 sunx.sun_family = AF_UNIX; /* Unix domain socket */
193 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
194 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0 ) {
195 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
199 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
200 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
201 (fchmod(fd, 0666) < 0) || (listen(fd, 5)) )
203 perror("Could not connect to socket " _PATH_LOG);
208 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
209 "BusyBox v" BB_VER " (" BB_BT ")");
212 while ((conn = accept(fd, (struct sockaddr *) &sunx,
215 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
219 int pri = (LOG_USER|LOG_NOTICE);
221 memset (line, 0, sizeof(line));
224 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
226 /* Parse the magic priority number */
228 while (isdigit(*(++p))) {
229 pri = 10 * pri + (*p - '0');
231 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
232 pri = (LOG_USER|LOG_NOTICE);
233 } else if (c == '\n') {
235 } else if (iscntrl(c)&&(c<0177)) {
246 logMessage( pri, line);
254 static void klogd_signal(int sig)
256 //ksyslog(7, NULL, 0);
258 logMessage(LOG_SYSLOG|LOG_INFO, "Kernel log daemon exiting.");
264 static void doKlogd(void)
266 int priority=LOG_INFO;
268 char log_buffer[4096];
271 /* Set up sig handlers */
272 signal(SIGINT, klogd_signal);
273 signal(SIGKILL, klogd_signal);
274 signal(SIGTERM, klogd_signal);
275 signal(SIGHUP, klogd_signal);
276 logMessage(LOG_SYSLOG|LOG_INFO, "klogd started: "
277 "BusyBox v" BB_VER " (" BB_BT ")");
280 if ( ((stat(_PATH_KLOG, &sb) < 0) && (errno == ENOENT)) ||
281 ( (kmsg = open(_PATH_KLOG, O_RDONLY)) < 0 ) ) {
283 snprintf(message, 79, "klogd: Cannot open %s, " \
284 "%d - %s.\n", _PATH_KLOG, errno, strerror(errno));
285 logMessage(LOG_SYSLOG|LOG_ERR, message);
289 memset(log_buffer, '\0', sizeof(log_buffer));
290 if ( read(kmsg, log_buffer, sizeof(log_buffer)-1) < 0 ) {
292 if ( errno == EINTR )
294 snprintf(message, 79, "klogd: Cannot read proc file system: %d - %s.\n",
295 errno, strerror(errno));
296 logMessage(LOG_SYSLOG|LOG_ERR, message);
300 if ( ksyslog(2, log_buffer, sizeof(log_buffer)) < 0 ) {
302 if ( errno == EINTR )
304 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
305 "%d - %s.\n", errno, strerror(errno));
306 logMessage(LOG_SYSLOG|LOG_ERR, message);
311 if ( *log_buffer == '<' )
313 switch ( *(log_buffer+1) )
316 priority = LOG_EMERG;
319 priority = LOG_ALERT;
328 priority = LOG_WARNING;
331 priority = LOG_NOTICE;
338 priority = LOG_DEBUG;
342 logMessage(LOG_KERN|priority, logp);
348 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 /* Store away localhost's name before the fork */
380 gethostname(LocalHostName, sizeof(LocalHostName));
381 if ( (p = strchr(LocalHostName, '.')) ) {
385 if (doFork == TRUE) {
389 else if ( pid == 0 ) {
390 strncpy(argv[0], "syslogd",strlen(argv[0]));
397 /* Start up the klogd process */
399 if (klogd_pid == 0 ) {
400 strncpy(argv[0], "klogd", strlen(argv[0]));