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;
91 /* Note: There is also a function called "message()" in init.c */
92 /* Print a message to the log file. */
93 static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
94 static void message (char *fmt, ...)
100 fl.l_whence = SEEK_SET;
104 if ((fd = device_open (logFilePath,
105 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
108 fcntl (fd, F_SETLKW, &fl);
109 va_start (arguments, fmt);
110 vdprintf (fd, fmt, arguments);
113 fcntl (fd, F_SETLKW, &fl);
116 /* Always send console messages to /dev/console so people will see them. */
117 if ((fd = device_open (_PATH_CONSOLE,
118 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
119 va_start (arguments, fmt);
120 vdprintf (fd, fmt, arguments);
124 fprintf (stderr, "Bummer, can't print: ");
125 va_start (arguments, fmt);
126 vfprintf (stderr, fmt, arguments);
133 static void logMessage (int pri, char *msg)
137 static char res[20] = "";
141 for (c_fac = facilitynames;
142 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
143 for (c_pri = prioritynames;
144 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
145 if (*c_fac->c_name == '\0' || *c_pri->c_name == '\0')
146 snprintf(res, sizeof(res), "<%d>", pri);
148 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
151 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
152 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
154 timestamp = ctime(&now) + 4;
155 timestamp[15] = '\0';
158 timestamp[15] = '\0';
162 /* todo: supress duplicates */
164 /* now spew out the message to wherever it is supposed to go */
165 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
167 #ifdef BB_FEATURE_REMOTE_LOG
168 /* send message to remote logger */
169 if ( -1 != remotefd){
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 fatalError("syslogd: cannot write to remote file handle on"
185 "%s:%d\n",RemoteHost,RemotePort);
192 static void quit_signal(int sig)
194 logMessage(0, "System log daemon exiting.");
199 static void domark(int sig)
201 if (MarkInterval > 0) {
202 logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --");
208 static int serveConnection (int conn)
210 char buf[ BUFSIZE + 1 ];
213 while ((n_read = read (conn, buf, BUFSIZE )) > 0) {
215 int pri = (LOG_USER | LOG_NOTICE);
216 char line[ BUFSIZE + 1 ];
219 char *p = buf, *q = line;
221 buf[ n_read - 1 ] = '\0';
223 while (p && (c = *p) && q < &line[ sizeof (line) - 1 ]) {
225 /* Parse the magic priority number. */
227 while (isdigit (*(++p))) {
228 pri = 10 * pri + (*p - '0');
230 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){
231 pri = (LOG_USER | LOG_NOTICE);
233 } else if (c == '\n') {
235 } else if (iscntrl (c) && (c < 0177)) {
245 logMessage (pri, line);
251 #ifdef BB_FEATURE_REMOTE_LOG
252 static void init_RemoteLog (void){
254 struct sockaddr_in remoteaddr;
255 struct hostent *hostinfo;
256 int len = sizeof(remoteaddr);
258 bzero(&remoteaddr, len);
260 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
263 fatalError("syslogd: cannot create socket\n");
266 hostinfo = (struct hostent *) gethostbyname(RemoteHost);
269 fatalError("syslogd: cannot resolve remote host name [%s]\n", RemoteHost);
272 remoteaddr.sin_family = AF_INET;
273 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
274 remoteaddr.sin_port = htons(RemotePort);
277 Since we are using UDP sockets, connect just sets the default host and port
278 for future operations
280 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){
281 fatalError("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort);
287 static void doSyslogd (void) __attribute__ ((noreturn));
288 static void doSyslogd (void)
290 struct sockaddr_un sunx;
291 socklen_t addrLength;
299 /* Set up signal handlers. */
300 signal (SIGINT, quit_signal);
301 signal (SIGTERM, quit_signal);
302 signal (SIGQUIT, quit_signal);
303 signal (SIGHUP, SIG_IGN);
304 signal (SIGCLD, SIG_IGN);
305 signal (SIGALRM, domark);
306 alarm (MarkInterval);
308 /* Create the syslog file so realpath() can work. */
309 close (open (_PATH_LOG, O_RDWR | O_CREAT, 0644));
310 if (realpath (_PATH_LOG, lfile) == NULL)
311 fatalError ("Could not resolv path to " _PATH_LOG ": %s\n", strerror (errno));
315 memset (&sunx, 0, sizeof (sunx));
316 sunx.sun_family = AF_UNIX;
317 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path));
318 if ((sock_fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0)
319 fatalError ("Couldn't obtain descriptor for socket " _PATH_LOG ": %s\n", strerror (errno));
321 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path);
322 if ((bind (sock_fd, (struct sockaddr *) &sunx, addrLength)) || (listen (sock_fd, 5)))
323 fatalError ("Could not connect to socket " _PATH_LOG ": %s\n", strerror (errno));
325 if (chmod (lfile, 0666) < 0)
326 fatalError ("Could not set permission on " _PATH_LOG ": %s\n", strerror (errno));
329 FD_SET (sock_fd, &fds);
331 #ifdef BB_FEATURE_REMOTE_LOG
332 if (doRemoteLog == TRUE){
337 logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")");
345 memcpy (&readfds, &fds, sizeof (fds));
347 if ((n_ready = select (FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0) {
348 if (errno == EINTR) continue; /* alarm may have happened. */
349 fatalError ("select error: %s\n", strerror (errno));
352 for (fd = 0; (n_ready > 0) && (fd < FD_SETSIZE); fd++) {
353 if (FD_ISSET (fd, &readfds)) {
362 if ((conn = accept (sock_fd, (struct sockaddr *) &sunx, &addrLength)) < 0) {
363 fatalError ("accept error: %s\n", strerror (errno));
369 perror ("syslogd: fork");
375 serveConnection (conn);
386 #ifdef BB_FEATURE_KLOGD
388 static void klogd_signal(int sig)
392 logMessage(0, "Kernel log daemon exiting.");
396 static void doKlogd (void) __attribute__ ((noreturn));
397 static void doKlogd (void)
399 int priority = LOG_INFO;
400 char log_buffer[4096];
403 /* Set up sig handlers */
404 signal(SIGINT, klogd_signal);
405 signal(SIGKILL, klogd_signal);
406 signal(SIGTERM, klogd_signal);
407 signal(SIGHUP, SIG_IGN);
409 #ifdef BB_FEATURE_REMOTE_LOG
410 if (doRemoteLog == TRUE){
415 logMessage(0, "klogd started: "
416 "BusyBox v" BB_VER " (" BB_BT ")");
421 /* Use kernel syscalls */
422 memset(log_buffer, '\0', sizeof(log_buffer));
423 if (klogctl(2, log_buffer, sizeof(log_buffer)) < 0) {
428 snprintf(message, 79, "klogd: Error return from sys_sycall: " \
429 "%d - %s.\n", errno, strerror(errno));
430 logMessage(LOG_SYSLOG | LOG_ERR, message);
434 if (*log_buffer == '<') {
435 switch (*(log_buffer + 1)) {
437 priority = LOG_EMERG;
440 priority = LOG_ALERT;
449 priority = LOG_WARNING;
452 priority = LOG_NOTICE;
459 priority = LOG_DEBUG;
463 logMessage(LOG_KERN | priority, logp);
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)
484 #ifdef BB_FEATURE_KLOGD
485 int startKlogd = TRUE;
487 int stopDoingThat = FALSE;
491 while (--argc > 0 && **(++argv1) == '-') {
492 stopDoingThat = FALSE;
493 while (stopDoingThat == FALSE && *(++(*argv1))) {
497 usage(syslogd_usage);
499 MarkInterval = atoi(*(++argv1)) * 60;
504 #ifdef BB_FEATURE_KLOGD
511 usage(syslogd_usage);
513 logFilePath = *(++argv1);
514 stopDoingThat = TRUE;
516 #ifdef BB_FEATURE_REMOTE_LOG
519 usage(syslogd_usage);
521 RemoteHost = *(++argv1);
522 if ( (p = strchr(RemoteHost, ':'))){
523 RemotePort = atoi(p+1);
527 stopDoingThat = TRUE;
531 usage(syslogd_usage);
537 usage(syslogd_usage);
539 /* Store away localhost's name before the fork */
540 gethostname(LocalHostName, sizeof(LocalHostName));
541 if ((p = strchr(LocalHostName, '.'))) {
547 #ifdef BB_FEATURE_KLOGD
548 /* Start up the klogd process */
549 if (startKlogd == TRUE) {
551 if (klogd_pid == 0) {
552 daemon_init (argv, "klogd", doKlogd);
557 if (doFork == TRUE) {
562 daemon_init (argv, "syslogd", doSyslogd);
573 c-file-style: "linux"