1 /* vi: set sw=4 ts=4: */
3 * Mini syslogd implementation for busybox
5 * Copyright (C) 1999,2000,2001 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
39 #include <sys/socket.h>
40 #include <sys/types.h>
42 #include <sys/param.h>
44 #if ! defined __GLIBC__ && ! defined __UCLIBC__
45 #include <sys/syscall.h>
46 #include <linux/unistd.h>
47 typedef unsigned int socklen_t;
50 # define __NR_klogctl __NR_syslog
51 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
53 #define klogctl syslog
57 # include <sys/klog.h>
62 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
64 #include <sys/syslog.h>
67 /* Path for the file where all log messages are written */
68 #define __LOG_FILE "/var/log/messages"
70 /* Path to the unix socket */
71 char lfile[BUFSIZ] = "";
73 static char *logFilePath = __LOG_FILE;
75 /* interval between marks in seconds */
76 static int MarkInterval = 20 * 60;
78 /* localhost's name */
79 static char LocalHostName[32];
81 #ifdef BB_FEATURE_REMOTE_LOG
82 #include <netinet/in.h>
83 /* udp socket for logging to remote host */
84 static int remotefd = -1;
85 /* where do we log? */
86 static char *RemoteHost;
87 /* what port to log to? */
88 static int RemotePort = 514;
89 /* To remote log or not to remote log, that is the question. */
90 static int doRemoteLog = FALSE;
91 static int local_logging = FALSE;
94 /* Note: There is also a function called "message()" in init.c */
95 /* Print a message to the log file. */
96 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
97 static void message (char *fmt, ...)
103 fl.l_whence = SEEK_SET;
107 if ((fd = device_open (logFilePath,
108 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
111 fcntl (fd, F_SETLKW, &fl);
112 va_start (arguments, fmt);
113 vdprintf (fd, fmt, arguments);
116 fcntl (fd, F_SETLKW, &fl);
119 /* Always send console messages to /dev/console so people will see them. */
120 if ((fd = device_open (_PATH_CONSOLE,
121 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
122 va_start (arguments, fmt);
123 vdprintf (fd, fmt, arguments);
127 fprintf (stderr, "Bummer, can't print: ");
128 va_start (arguments, fmt);
129 vfprintf (stderr, fmt, arguments);
136 static void logMessage (int pri, char *msg)
140 static char res[20] = "";
144 for (c_fac = facilitynames;
145 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
146 for (c_pri = prioritynames;
147 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
148 if (c_fac->c_name == NULL || c_pri->c_name == NULL)
149 snprintf(res, sizeof(res), "<%d>", pri);
151 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
154 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
155 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
157 timestamp = ctime(&now) + 4;
158 timestamp[15] = '\0';
161 timestamp[15] = '\0';
165 /* todo: supress duplicates */
167 #ifdef BB_FEATURE_REMOTE_LOG
168 /* send message to remote logger */
169 if ( -1 != remotefd){
170 static const int IOV_COUNT = 2;
171 struct iovec iov[IOV_COUNT];
172 struct iovec *v = iov;
174 bzero(&res, sizeof(res));
175 snprintf(res, sizeof(res), "<%d>", pri);
177 v->iov_len = strlen(res);
181 v->iov_len = strlen(msg);
183 if ( -1 == writev(remotefd,iov, IOV_COUNT)){
184 error_msg_and_die("syslogd: cannot write to remote file handle on"
185 "%s:%d\n",RemoteHost,RemotePort);
188 if (local_logging == TRUE)
190 /* now spew out the message to wherever it is supposed to go */
191 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
196 static void quit_signal(int sig)
198 logMessage(0, "System log daemon exiting.");
203 static void domark(int sig)
205 if (MarkInterval > 0) {
206 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
211 static const int BUFSIZE = 1023;
212 static int serveConnection (int conn)
214 RESERVE_BB_BUFFER(buf, BUFSIZE + 1);
217 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
219 int pri = (LOG_USER | LOG_NOTICE);
220 char line[ BUFSIZE + 1 ];
223 char *p = buf, *q = line;
225 buf[ n_read - 1 ] = '\0';
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);
237 } else if (c == '\n') {
239 } else if (iscntrl (c) && (c < 0177)) {
249 logMessage (pri, line);
255 #ifdef BB_FEATURE_REMOTE_LOG
256 static void init_RemoteLog (void){
258 struct sockaddr_in remoteaddr;
259 struct hostent *hostinfo;
260 int len = sizeof(remoteaddr);
262 bzero(&remoteaddr, len);
264 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
267 error_msg_and_die("syslogd: cannot create socket\n");
270 hostinfo = (struct hostent *) gethostbyname(RemoteHost);
273 error_msg_and_die("syslogd: cannot resolve remote host name [%s]\n", RemoteHost);
276 remoteaddr.sin_family = AF_INET;
277 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
278 remoteaddr.sin_port = htons(RemotePort);
281 Since we are using UDP sockets, connect just sets the default host and port
282 for future operations
284 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
285 error_msg_and_die("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort);
291 static void doSyslogd (void) __attribute__ ((noreturn));
292 static void doSyslogd (void)
294 struct sockaddr_un sunx;
295 socklen_t addrLength;
301 RESERVE_BB_BUFFER(lfile, BUFSIZ);
303 /* Set up signal handlers. */
304 signal (SIGINT, quit_signal);
305 signal (SIGTERM, quit_signal);
306 signal (SIGQUIT, quit_signal);
307 signal (SIGHUP, SIG_IGN);
308 signal (SIGCHLD, SIG_IGN);
310 signal (SIGCLD, SIG_IGN);
312 signal (SIGALRM, domark);
313 alarm (MarkInterval);
315 /* Create the syslog file so realpath() can work. */
316 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
317 if (realpath (_PATH_LOG, lfile) == NULL)
318 perror_msg_and_die ("Could not resolve path to " _PATH_LOG);
322 memset (&sunx, 0, sizeof (sunx));
323 sunx.sun_family = AF_UNIX;
324 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
325 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
326 perror_msg_and_die ("Couldn't obtain descriptor for socket " _PATH_LOG);
328 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
329 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
330 perror_msg_and_die ("Could not connect to socket " _PATH_LOG);
332 if (chmod (lfile, 0666) < 0)
333 perror_msg_and_die ("Could not set permission on " _PATH_LOG);
336 FD_SET (sock_fd, &fds);
338 #ifdef BB_FEATURE_REMOTE_LOG
339 if (doRemoteLog == TRUE){
344 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
352 memcpy (&readfds, &fds, sizeof (fds));
354 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
355 if (errno == EINTR) continue; /* alarm may have happened. */
356 perror_msg_and_die ("select error");
359 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
360 if (FD_ISSET (fd, &readfds)) {
369 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
370 perror_msg_and_die ("accept error");
376 perror ("syslogd: fork");
382 serveConnection (conn);
393 #ifdef BB_FEATURE_KLOGD
395 static void klogd_signal(int sig)
399 logMessage(0, "Kernel log daemon exiting.");
403 static void doKlogd (void) __attribute__ ((noreturn));
404 static void doKlogd (void)
406 int priority = LOG_INFO;
407 char log_buffer[4096];
411 /* Set up sig handlers */
412 signal(SIGINT, klogd_signal);
413 signal(SIGKILL, klogd_signal);
414 signal(SIGTERM, klogd_signal);
415 signal(SIGHUP, SIG_IGN);
417 #ifdef BB_FEATURE_REMOTE_LOG
418 if (doRemoteLog == TRUE){
423 logMessage(0, "klogd started: "
424 "BusyBox v" BB_VER " (" BB_BT ")");
426 /* "Open the log. Currently a NOP." */
430 /* Use kernel syscalls */
431 memset(log_buffer, '\0', sizeof(log_buffer));
432 n = klogctl(2, log_buffer, sizeof(log_buffer));
438 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
439 "%d - %s.\n", errno, strerror(errno));
440 logMessage(LOG_SYSLOG | LOG_ERR, message);
444 /* klogctl buffer parsing modelled after code in dmesg.c */
445 start=&log_buffer[0];
447 for (i=0; i<n; i++) {
448 if (lastc == '\0' && log_buffer[i] == '<') {
451 while (isdigit(log_buffer[i])) {
452 priority = priority*10+(log_buffer[i]-'0');
455 if (log_buffer[i] == '>') i++;
456 start = &log_buffer[i];
458 if (log_buffer[i] == '\n') {
459 log_buffer[i] = '\0'; /* zero terminate this message */
460 logMessage(LOG_KERN | priority, start);
461 start = &log_buffer[i+1];
464 lastc = log_buffer[i];
472 static void daemon_init (char **argv, char *dz, void fn (void))
476 strncpy(argv[0], dz, strlen(argv[0]));
481 extern int syslogd_main(int argc, char **argv)
483 int opt, pid, klogd_pid;
486 #ifdef BB_FEATURE_KLOGD
487 int startKlogd = TRUE;
491 /* do normal option parsing */
492 while ((opt = getopt(argc, argv, "m:nKO:R:L")) > 0) {
495 MarkInterval = atoi(optarg) * 60;
500 #ifdef BB_FEATURE_KLOGD
506 logFilePath = strdup(optarg);
508 #ifdef BB_FEATURE_REMOTE_LOG
510 RemoteHost = strdup(optarg);
511 if ( (p = strchr(RemoteHost, ':'))){
512 RemotePort = atoi(p+1);
518 local_logging = TRUE;
522 usage(syslogd_usage);
526 #ifdef BB_FEATURE_REMOTE_LOG
527 /* If they have not specified remote logging, then log locally */
528 if (doRemoteLog == FALSE)
529 local_logging = TRUE;
532 /* Store away localhost's name before the fork */
533 gethostname(LocalHostName, sizeof(LocalHostName));
534 if ((p = strchr(LocalHostName, '.'))) {
540 #ifdef BB_FEATURE_KLOGD
541 /* Start up the klogd process */
542 if (startKlogd == TRUE) {
544 if (klogd_pid == 0) {
545 daemon_init (argv, "klogd", doKlogd);
550 if (doFork == TRUE) {
555 daemon_init (argv, "syslogd", doSyslogd);
566 c-file-style: "linux"