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 = FALSE;
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];
409 /* Set up sig handlers */
410 signal(SIGINT, klogd_signal);
411 signal(SIGKILL, klogd_signal);
412 signal(SIGTERM, klogd_signal);
413 signal(SIGHUP, SIG_IGN);
415 #ifdef BB_FEATURE_REMOTE_LOG
416 if (doRemoteLog == TRUE){
421 logMessage(0, "klogd started: "
422 "BusyBox v" BB_VER " (" BB_BT ")");
424 /* "Open the log. Currently a NOP." */
428 /* Use kernel syscalls */
429 memset(log_buffer, '\0', sizeof(log_buffer));
430 n = klogctl(2, log_buffer, sizeof(log_buffer));
436 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
437 "%d - %s.\n", errno, strerror(errno));
438 logMessage(LOG_SYSLOG | LOG_ERR, message);
442 /* klogctl buffer parsing modelled after code in dmesg.c */
443 start=&log_buffer[0];
445 for (i=0; i<n; i++) {
446 if (lastc == '\0' && log_buffer[i] == '<') {
449 while (isdigit(log_buffer[i])) {
450 priority = priority*10+(log_buffer[i]-'0');
453 if (log_buffer[i] == '>') i++;
454 start = &log_buffer[i];
456 if (log_buffer[i] == '\n') {
457 log_buffer[i] = '\0'; /* zero terminate this message */
458 logMessage(LOG_KERN | priority, start);
459 start = &log_buffer[i+1];
462 lastc = log_buffer[i];
470 static void daemon_init (char **argv, char *dz, void fn (void))
474 strncpy(argv[0], dz, strlen(argv[0]));
479 extern int syslogd_main(int argc, char **argv)
481 int opt, pid, klogd_pid;
484 #ifdef BB_FEATURE_KLOGD
485 int startKlogd = TRUE;
489 /* do normal option parsing */
490 while ((opt = getopt(argc, argv, "m:nKO:R:L")) > 0) {
493 MarkInterval = atoi(optarg) * 60;
498 #ifdef BB_FEATURE_KLOGD
504 logFilePath = strdup(optarg);
506 #ifdef BB_FEATURE_REMOTE_LOG
508 RemoteHost = strdup(optarg);
509 if ( (p = strchr(RemoteHost, ':'))){
510 RemotePort = atoi(p+1);
516 local_logging = TRUE;
520 usage(syslogd_usage);
524 /* Store away localhost's name before the fork */
525 gethostname(LocalHostName, sizeof(LocalHostName));
526 if ((p = strchr(LocalHostName, '.'))) {
532 #ifdef BB_FEATURE_KLOGD
533 /* Start up the klogd process */
534 if (startKlogd == TRUE) {
536 if (klogd_pid == 0) {
537 daemon_init (argv, "klogd", doKlogd);
542 if (doFork == TRUE) {
547 daemon_init (argv, "syslogd", doSyslogd);
558 c-file-style: "linux"