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>
40 #include <sys/types.h>
42 #include <sys/param.h>
44 #if ! defined __GLIBC__ && ! defined __UCLIBC__
46 typedef unsigned int socklen_t;
49 # define __NR_klogctl __NR_syslog
50 static inline _syscall3(int, klogctl, int, type, char *, b, int, len);
52 #define klogctl syslog
56 # include <sys/klog.h>
61 /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
63 #include <sys/syslog.h>
66 /* Path for the file where all log messages are written */
67 #define __LOG_FILE "/var/log/messages"
69 /* Path to the unix socket */
70 char lfile[BUFSIZ] = "";
72 static char *logFilePath = __LOG_FILE;
74 /* interval between marks in seconds */
75 static int MarkInterval = 20 * 60;
77 /* localhost's name */
78 static char LocalHostName[32];
80 #ifdef BB_FEATURE_REMOTE_LOG
81 #include <netinet/in.h>
82 /* udp socket for logging to remote host */
83 static int remotefd = -1;
84 /* where do we log? */
85 static char *RemoteHost;
86 /* what port to log to? */
87 static int RemotePort = 514;
88 /* To remote log or not to remote log, that is the question. */
89 static int doRemoteLog = 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 /* now spew out the message to wherever it is supposed to go */
166 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
168 #ifdef BB_FEATURE_REMOTE_LOG
169 /* send message to remote logger */
170 if ( -1 != remotefd){
172 struct iovec iov[IOV_COUNT];
173 struct iovec *v = iov;
175 bzero(&res, sizeof(res));
176 snprintf(res, sizeof(res), "<%d>", pri);
178 v->iov_len = strlen(res);
182 v->iov_len = strlen(msg);
184 if ( -1 == writev(remotefd,iov, IOV_COUNT)){
185 fatalError("syslogd: cannot write to remote file handle on"
186 "%s:%d\n",RemoteHost,RemotePort);
193 static void quit_signal(int sig)
195 logMessage(0, "System log daemon exiting.");
200 static void domark(int sig)
202 if (MarkInterval > 0) {
203 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
209 static int serveConnection (int conn)
211 char buf[ BUFSIZE + 1 ];
214 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
216 int pri = (LOG_USER | LOG_NOTICE);
217 char line[ BUFSIZE + 1 ];
220 char *p = buf, *q = line;
222 buf[ n_read - 1 ] = '\0';
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);
234 } else if (c == '\n') {
236 } else if (iscntrl (c) && (c < 0177)) {
246 logMessage (pri, line);
252 #ifdef BB_FEATURE_REMOTE_LOG
253 static void init_RemoteLog (void){
255 struct sockaddr_in remoteaddr;
256 struct hostent *hostinfo;
257 int len = sizeof(remoteaddr);
259 bzero(&remoteaddr, len);
261 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
264 fatalError("syslogd: cannot create socket\n");
267 hostinfo = (struct hostent *) gethostbyname(RemoteHost);
270 fatalError("syslogd: cannot resolve remote host name [%s]\n", RemoteHost);
273 remoteaddr.sin_family = AF_INET;
274 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
275 remoteaddr.sin_port = htons(RemotePort);
278 Since we are using UDP sockets, connect just sets the default host and port
279 for future operations
281 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
282 fatalError("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort);
288 static void doSyslogd (void) __attribute__ ((noreturn));
289 static void doSyslogd (void)
291 struct sockaddr_un sunx;
292 socklen_t addrLength;
300 /* Set up signal handlers. */
301 signal (SIGINT, quit_signal);
302 signal (SIGTERM, quit_signal);
303 signal (SIGQUIT, quit_signal);
304 signal (SIGHUP, SIG_IGN);
305 signal (SIGCLD, SIG_IGN);
306 signal (SIGALRM, domark);
307 alarm (MarkInterval);
309 /* Create the syslog file so realpath() can work. */
310 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
311 if (realpath (_PATH_LOG, lfile) == NULL)
312 fatalError ("Could not resolv path to " _PATH_LOG ": %s\n", strerror (errno));
316 memset (&sunx, 0, sizeof (sunx));
317 sunx.sun_family = AF_UNIX;
318 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
319 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
320 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
322 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
323 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
324 fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
326 if (chmod (lfile, 0666) < 0)
327 fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
330 FD_SET (sock_fd, &fds);
332 #ifdef BB_FEATURE_REMOTE_LOG
333 if (doRemoteLog == TRUE){
338 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
346 memcpy (&readfds, &fds, sizeof (fds));
348 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
349 if (errno == EINTR) continue; /* alarm may have happened. */
350 fatalError ("select error: %s\n", strerror (errno));
353 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
354 if (FD_ISSET (fd, &readfds)) {
363 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
364 fatalError ("accept error: %s\n", strerror (errno));
370 perror ("syslogd: fork");
376 serveConnection (conn);
384 #ifdef BB_FEATURE_KLOGD
386 static void klogd_signal(int sig)
390 logMessage(0, "Kernel log daemon exiting.");
394 static void doKlogd (void) __attribute__ ((noreturn));
395 static void doKlogd (void)
397 int priority = LOG_INFO;
398 char log_buffer[4096];
401 /* Set up sig handlers */
402 signal(SIGINT, klogd_signal);
403 signal(SIGKILL, klogd_signal);
404 signal(SIGTERM, klogd_signal);
405 signal(SIGHUP, SIG_IGN);
407 #ifdef BB_FEATURE_REMOTE_LOG
408 if (doRemoteLog == TRUE){
413 logMessage(0, "klogd started: "
414 "BusyBox v" BB_VER " (" BB_BT ")");
419 /* Use kernel syscalls */
420 memset(log_buffer, '\0', sizeof(log_buffer));
421 if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
426 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
427 "%d - %s.\n", errno, strerror(errno));
428 logMessage(LOG_SYSLOG | LOG_ERR, message);
432 if (*log_buffer == '<') {
433 switch (*(log_buffer + 1)) {
435 priority = LOG_EMERG;
438 priority = LOG_ALERT;
447 priority = LOG_WARNING;
450 priority = LOG_NOTICE;
457 priority = LOG_DEBUG;
461 logMessage(LOG_KERN | priority, logp);
468 static void daemon_init (char **argv, char *dz, void fn (void))
472 strncpy(argv[0], dz, strlen(argv[0]));
477 extern int syslogd_main(int argc, char **argv)
482 #ifdef BB_FEATURE_KLOGD
483 int startKlogd = TRUE;
485 int stopDoingThat = FALSE;
489 while (--argc > 0 && **(++argv1) == '-') {
490 stopDoingThat = FALSE;
491 while (stopDoingThat == FALSE && *(++(*argv1))) {
495 usage(syslogd_usage);
497 MarkInterval = atoi(*(++argv1)) * 60;
502 #ifdef BB_FEATURE_KLOGD
509 usage(syslogd_usage);
511 logFilePath = *(++argv1);
512 stopDoingThat = TRUE;
514 #ifdef BB_FEATURE_REMOTE_LOG
517 usage(syslogd_usage);
519 RemoteHost = *(++argv1);
520 if ( (p = strchr(RemoteHost, ':'))){
521 RemotePort = atoi(p+1);
525 stopDoingThat = TRUE;
529 usage(syslogd_usage);
535 usage(syslogd_usage);
537 /* Store away localhost's name before the fork */
538 gethostname(LocalHostName, sizeof(LocalHostName));
539 if ((p = strchr(LocalHostName, '.'))) {
545 #ifdef BB_FEATURE_KLOGD
546 /* Start up the klogd process */
547 if (startKlogd == TRUE) {
549 if (klogd_pid == 0) {
550 daemon_init (argv, "klogd", doKlogd);
555 if (doFork == TRUE) {
560 daemon_init (argv, "syslogd", doSyslogd);
571 c-file-style: "linux"