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 * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <sys/socket.h>
39 #include <sys/types.h>
41 #include <sys/param.h>
43 #if ! defined __GLIBC__ && ! defined __UCLIBC__
45 typedef unsigned int socklen_t;
48 # define __NR_klogctl __NR_syslog
49 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
51 #define klogctl syslog
55 # include <sys/klog.h>
60 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
62 #include <sys/syslog.h>
65 /* Path for the file where all log messages are written */
66 #define __LOG_FILE "/var/log/messages"
68 /* Path to the unix socket */
69 char lfile[BUFSIZ] = "";
71 static char *logFilePath = __LOG_FILE;
73 /* interval between marks in seconds */
74 static int MarkInterval = 20 * 60;
76 /* localhost's name */
77 static char LocalHostName[32];
79 #ifdef BB_FEATURE_REMOTE_LOG
80 #include <netinet/in.h>
81 /* udp socket for logging to remote host */
82 static int remotefd = -1;
83 /* where do we log? */
84 static char *RemoteHost;
85 /* what port to log to? */
86 static int RemotePort = 514;
87 /* To remote log or not to remote log, that is the question. */
88 static int doRemoteLog = FALSE;
89 static int local_logging = TRUE;
92 /* Note: There is also a function called "message()" in init.c */
93 /* Print a message to the log file. */
94 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
95 static void message (char *fmt, ...)
101 fl.l_whence = SEEK_SET;
105 if ((fd = device_open (logFilePath,
106 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
109 fcntl (fd, F_SETLKW, &fl);
110 va_start (arguments, fmt);
111 vdprintf (fd, fmt, arguments);
114 fcntl (fd, F_SETLKW, &fl);
117 /* Always send console messages to /dev/console so people will see them. */
118 if ((fd = device_open (_PATH_CONSOLE,
119 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
120 va_start (arguments, fmt);
121 vdprintf (fd, fmt, arguments);
125 fprintf (stderr, "Bummer, can't print: ");
126 va_start (arguments, fmt);
127 vfprintf (stderr, fmt, arguments);
134 static void logMessage (int pri, char *msg)
138 static char res[20] = "";
142 for (c_fac = facilitynames;
143 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
144 for (c_pri = prioritynames;
145 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
146 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
147 snprintf(res, sizeof(res), "<%d>", pri);
149 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
152 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
153 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
155 timestamp = ctime(&now) + 4;
156 timestamp[15] = '\0';
159 timestamp[15] = '\0';
163 /* todo: supress duplicates */
165 #ifdef BB_FEATURE_REMOTE_LOG
166 /* send message to remote logger */
167 if ( -1 != remotefd){
169 struct iovec iov[IOV_COUNT];
170 struct iovec *v = iov;
172 bzero(&res, sizeof(res));
173 snprintf(res, sizeof(res), "<%d>", pri);
175 v->iov_len = strlen(res);
179 v->iov_len = strlen(msg);
181 if ( -1 == writev(remotefd,iov, IOV_COUNT)){
182 error_msg_and_die("syslogd: cannot write to remote file handle on"
183 "%s:%d\n",RemoteHost,RemotePort);
186 if (local_logging == TRUE)
188 /* now spew out the message to wherever it is supposed to go */
189 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
194 static void quit_signal(int sig)
196 logMessage(0, "System log daemon exiting.");
201 static void domark(int sig)
203 if (MarkInterval > 0) {
204 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
210 static int serveConnection (int conn)
212 char buf[ BUFSIZE + 1 ];
215 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
217 int pri = (LOG_USER | LOG_NOTICE);
218 char line[ BUFSIZE + 1 ];
221 char *p = buf, *q = line;
223 buf[ n_read - 1 ] = '\0';
225 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
227 /* Parse the magic priority number. */
229 while (isdigit (*(++p))) {
230 pri = 10 * pri + (*p - '0');
232 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){
233 pri = (LOG_USER | LOG_NOTICE);
235 } else if (c == '\n') {
237 } else if (iscntrl (c) && (c < 0177)) {
247 logMessage (pri, line);
253 #ifdef BB_FEATURE_REMOTE_LOG
254 static void init_RemoteLog (void){
256 struct sockaddr_in remoteaddr;
257 struct hostent *hostinfo;
258 int len = sizeof(remoteaddr);
260 bzero(&remoteaddr, len);
262 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
265 error_msg_and_die("syslogd: cannot create socket\n");
268 hostinfo = (struct hostent *) gethostbyname(RemoteHost);
271 error_msg_and_die("syslogd: cannot resolve remote host name [%s]\n", RemoteHost);
274 remoteaddr.sin_family = AF_INET;
275 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
276 remoteaddr.sin_port = htons(RemotePort);
279 Since we are using UDP sockets, connect just sets the default host and port
280 for future operations
282 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
283 error_msg_and_die("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort);
289 static void doSyslogd (void) __attribute__ ((noreturn));
290 static void doSyslogd (void)
292 struct sockaddr_un sunx;
293 socklen_t addrLength;
301 /* Set up signal handlers. */
302 signal (SIGINT, quit_signal);
303 signal (SIGTERM, quit_signal);
304 signal (SIGQUIT, quit_signal);
305 signal (SIGHUP, SIG_IGN);
306 signal (SIGCHLD, SIG_IGN);
308 signal (SIGCLD, SIG_IGN);
310 signal (SIGALRM, domark);
311 alarm (MarkInterval);
313 /* Create the syslog file so realpath() can work. */
314 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
315 if (realpath (_PATH_LOG, lfile) == NULL)
316 error_msg_and_die ("Could not resolve path to " _PATH_LOG ": %s\n", strerror (errno));
320 memset (&sunx, 0, sizeof (sunx));
321 sunx.sun_family = AF_UNIX;
322 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
323 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
324 error_msg_and_die ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
326 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
327 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
328 error_msg_and_die ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
330 if (chmod (lfile, 0666) < 0)
331 error_msg_and_die ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
334 FD_SET (sock_fd, &fds);
336 #ifdef BB_FEATURE_REMOTE_LOG
337 if (doRemoteLog == TRUE){
342 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
350 memcpy (&readfds, &fds, sizeof (fds));
352 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
353 if (errno == EINTR) continue; /* alarm may have happened. */
354 error_msg_and_die ("select error: %s\n", strerror (errno));
357 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
358 if (FD_ISSET (fd, &readfds)) {
367 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
368 error_msg_and_die ("accept error: %s\n", strerror (errno));
374 perror ("syslogd: fork");
380 serveConnection (conn);
391 #ifdef BB_FEATURE_KLOGD
393 static void klogd_signal(int sig)
397 logMessage(0, "Kernel log daemon exiting.");
401 static void doKlogd (void) __attribute__ ((noreturn));
402 static void doKlogd (void)
404 int priority = LOG_INFO;
405 char log_buffer[4096];
408 /* Set up sig handlers */
409 signal(SIGINT, klogd_signal);
410 signal(SIGKILL, klogd_signal);
411 signal(SIGTERM, klogd_signal);
412 signal(SIGHUP, SIG_IGN);
414 #ifdef BB_FEATURE_REMOTE_LOG
415 if (doRemoteLog == TRUE){
420 logMessage(0, "klogd started: "
421 "BusyBox v" BB_VER " (" BB_BT ")");
426 /* Use kernel syscalls */
427 memset(log_buffer, '\0', sizeof(log_buffer));
428 if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
433 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
434 "%d - %s.\n", errno, strerror(errno));
435 logMessage(LOG_SYSLOG | LOG_ERR, message);
439 if (*log_buffer == '<') {
440 switch (*(log_buffer + 1)) {
442 priority = LOG_EMERG;
445 priority = LOG_ALERT;
454 priority = LOG_WARNING;
457 priority = LOG_NOTICE;
464 priority = LOG_DEBUG;
468 logMessage(LOG_KERN | priority, logp);
475 static void daemon_init (char **argv, char *dz, void fn (void))
479 strncpy(argv[0], dz, strlen(argv[0]));
484 extern int syslogd_main(int argc, char **argv)
489 #ifdef BB_FEATURE_KLOGD
490 int startKlogd = TRUE;
492 int stopDoingThat = FALSE;
496 while (--argc > 0 && **(++argv1) == '-') {
497 stopDoingThat = FALSE;
498 while (stopDoingThat == FALSE && *(++(*argv1))) {
502 usage(syslogd_usage);
504 MarkInterval = atoi(*(++argv1)) * 60;
509 #ifdef BB_FEATURE_KLOGD
516 usage(syslogd_usage);
518 logFilePath = *(++argv1);
519 stopDoingThat = TRUE;
521 #ifdef BB_FEATURE_REMOTE_LOG
524 usage(syslogd_usage);
526 RemoteHost = *(++argv1);
527 if ( (p = strchr(RemoteHost, ':'))){
528 RemotePort = atoi(p+1);
532 stopDoingThat = TRUE;
535 local_logging = FALSE;
539 usage(syslogd_usage);
545 usage(syslogd_usage);
547 /* Store away localhost's name before the fork */
548 gethostname(LocalHostName, sizeof(LocalHostName));
549 if ((p = strchr(LocalHostName, '.'))) {
555 #ifdef BB_FEATURE_KLOGD
556 /* Start up the klogd process */
557 if (startKlogd == TRUE) {
559 if (klogd_pid == 0) {
560 daemon_init (argv, "klogd", doKlogd);
565 if (doFork == TRUE) {
570 daemon_init (argv, "syslogd", doSyslogd);
581 c-file-style: "linux"