Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini syslogd implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 6 | * |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 7 | * Copyright (C) 2000 by Karl M. Hegbloom <karlheg@debian.org> |
| 8 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 9 | * "circular buffer" Copyright (C) 2001 by Gennady Feldman <gfeldman@gena01.com> |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 10 | * |
Glenn L McGrath | 6ed7759 | 2002-12-12 10:54:48 +0000 | [diff] [blame] | 11 | * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001 |
Mark Whitley | 6bff9cc | 2001-03-12 23:41:34 +0000 | [diff] [blame] | 12 | * |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 13 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 14 | */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 15 | |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 18 | #include <ctype.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 19 | #include <errno.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
Glenn L McGrath | 5529b7b | 2004-07-22 04:23:18 +0000 | [diff] [blame] | 21 | #include <getopt.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 22 | #include <netdb.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 23 | #include <paths.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 24 | #include <signal.h> |
| 25 | #include <stdarg.h> |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 26 | #include <stdbool.h> |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 27 | #include <time.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 28 | #include <string.h> |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 30 | #include <sys/socket.h> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <sys/un.h> |
Erik Andersen | 7d6ba57 | 2000-04-19 20:02:50 +0000 | [diff] [blame] | 33 | #include <sys/param.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 34 | |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 35 | #include "busybox.h" |
Eric Andersen | 67e3230 | 2000-06-19 17:48:02 +0000 | [diff] [blame] | 36 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 37 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ |
| 38 | #define SYSLOG_NAMES |
| 39 | #include <sys/syslog.h> |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 40 | #include <sys/uio.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 41 | |
| 42 | /* Path for the file where all log messages are written */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 43 | #define __LOG_FILE "/var/log/messages" |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 44 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 45 | /* Path to the unix socket */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 46 | static char lfile[MAXPATHLEN]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 47 | |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 48 | static const char *logFilePath = __LOG_FILE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 50 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 51 | /* max size of message file before being rotated */ |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 52 | static int logFileSize = 200 * 1024; |
| 53 | |
| 54 | /* number of rotated message files */ |
| 55 | static int logFileRotate = 1; |
| 56 | #endif |
| 57 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 58 | /* interval between marks in seconds */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 59 | static int MarkInterval = 20 * 60; |
| 60 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 61 | /* localhost's name */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 62 | static char LocalHostName[64]; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 63 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 64 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 65 | #include <netinet/in.h> |
| 66 | /* udp socket for logging to remote host */ |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 67 | static int remotefd = -1; |
Eric Andersen | 75813ee | 2004-08-26 23:15:29 +0000 | [diff] [blame] | 68 | static struct sockaddr_in remoteaddr; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 69 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 70 | /* where do we log? */ |
| 71 | static char *RemoteHost; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 72 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 73 | /* what port to log to? */ |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 74 | static int RemotePort = 514; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 75 | |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 76 | /* To remote log or not to remote log, that is the question. */ |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 77 | static int doRemoteLog = FALSE; |
Eric Andersen | 70d09ed | 2000-12-11 16:24:16 +0000 | [diff] [blame] | 78 | static int local_logging = FALSE; |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 81 | /* Make loging output smaller. */ |
| 82 | static bool small = false; |
| 83 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 84 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 85 | #define MAXLINE 1024 /* maximum line length */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 86 | |
| 87 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 88 | /* circular buffer variables/structures */ |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 89 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 90 | |
| 91 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4 |
| 92 | #error Sorry, you must set the syslogd buffer size to at least 4KB. |
| 93 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE |
| 94 | #endif |
| 95 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 96 | #include <sys/ipc.h> |
| 97 | #include <sys/sem.h> |
| 98 | #include <sys/shm.h> |
| 99 | |
| 100 | /* our shared key */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 101 | static const long KEY_ID = 0x414e4547; /*"GENA" */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 102 | |
| 103 | // Semaphore operation structures |
| 104 | static struct shbuf_ds { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 105 | int size; // size of data written |
| 106 | int head; // start of message list |
| 107 | int tail; // end of message list |
| 108 | char data[1]; // data/messages |
| 109 | } *buf = NULL; // shared memory pointer |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 110 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 111 | static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; // set SMwup |
| 112 | static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; // set SMwdn |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 113 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 114 | static int shmid = -1; // ipc shared memory id |
| 115 | static int s_semid = -1; // ipc semaphore id |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 116 | static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 117 | static int circular_logging = FALSE; |
| 118 | |
| 119 | /* |
| 120 | * sem_up - up()'s a semaphore. |
| 121 | */ |
| 122 | static inline void sem_up(int semid) |
| 123 | { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 124 | if (semop(semid, SMwup, 1) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 125 | bb_perror_msg_and_die("semop[SMwup]"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 126 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | /* |
| 130 | * sem_down - down()'s a semaphore |
| 131 | */ |
| 132 | static inline void sem_down(int semid) |
| 133 | { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 134 | if (semop(semid, SMwdn, 3) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | bb_perror_msg_and_die("semop[SMwdn]"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 136 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 139 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 140 | static void ipcsyslog_cleanup(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 141 | { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 142 | printf("Exiting Syslogd!\n"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 143 | if (shmid != -1) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 144 | shmdt(buf); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 145 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 146 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 147 | if (shmid != -1) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 148 | shmctl(shmid, IPC_RMID, NULL); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 149 | } |
| 150 | if (s_semid != -1) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 151 | semctl(s_semid, 0, IPC_RMID, 0); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 152 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 153 | } |
| 154 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 155 | static void ipcsyslog_init(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 156 | { |
| 157 | if (buf == NULL) { |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 158 | if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 159 | bb_perror_msg_and_die("shmget"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 160 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 161 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 162 | if ((buf = shmat(shmid, NULL, 0)) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | bb_perror_msg_and_die("shmat"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 164 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 165 | |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 166 | buf->size = shm_size - sizeof(*buf); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 167 | buf->head = buf->tail = 0; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 168 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 169 | // we'll trust the OS to set initial semval to 0 (let's hope) |
| 170 | if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) { |
| 171 | if (errno == EEXIST) { |
| 172 | if ((s_semid = semget(KEY_ID, 2, 0)) == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | bb_perror_msg_and_die("semget"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 174 | } |
| 175 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 176 | bb_perror_msg_and_die("semget"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } else { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 180 | printf("Buffer already allocated just grab the semaphore?"); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | /* write message to buffer */ |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 185 | static void circ_message(const char *msg) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 186 | { |
| 187 | int l = strlen(msg) + 1; /* count the whole message w/ '\0' included */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 188 | |
| 189 | sem_down(s_semid); |
| 190 | |
| 191 | /* |
| 192 | * Circular Buffer Algorithm: |
| 193 | * -------------------------- |
| 194 | * |
| 195 | * Start-off w/ empty buffer of specific size SHM_SIZ |
| 196 | * Start filling it up w/ messages. I use '\0' as separator to break up messages. |
| 197 | * This is also very handy since we can do printf on message. |
| 198 | * |
| 199 | * Once the buffer is full we need to get rid of the first message in buffer and |
| 200 | * insert the new message. (Note: if the message being added is >1 message then |
| 201 | * we will need to "remove" >1 old message from the buffer). The way this is done |
| 202 | * is the following: |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 203 | * When we reach the end of the buffer we set a mark and start from the beginning. |
| 204 | * Now what about the beginning and end of the buffer? Well we have the "head" |
| 205 | * index/pointer which is the starting point for the messages and we have "tail" |
| 206 | * index/pointer which is the ending point for the messages. When we "display" the |
| 207 | * messages we start from the beginning and continue until we reach "tail". If we |
| 208 | * reach end of buffer, then we just start from the beginning (offset 0). "head" and |
| 209 | * "tail" are actually offsets from the beginning of the buffer. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 210 | * |
| 211 | * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 212 | * a threasafe way of handling shared memory operations. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 213 | */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 214 | if ((buf->tail + l) < buf->size) { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 215 | /* before we append the message we need to check the HEAD so that we won't |
| 216 | overwrite any of the message that we still need and adjust HEAD to point |
| 217 | to the next message! */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 218 | if (buf->tail < buf->head) { |
| 219 | if ((buf->tail + l) >= buf->head) { |
| 220 | /* we need to move the HEAD to point to the next message |
| 221 | * Theoretically we have enough room to add the whole message to the |
| 222 | * buffer, because of the first outer IF statement, so we don't have |
| 223 | * to worry about overflows here! |
| 224 | */ |
| 225 | int k = buf->tail + l - buf->head; /* we need to know how many bytes |
| 226 | we are overwriting to make |
| 227 | enough room */ |
| 228 | char *c = |
| 229 | memchr(buf->data + buf->head + k, '\0', |
| 230 | buf->size - (buf->head + k)); |
| 231 | if (c != NULL) { /* do a sanity check just in case! */ |
| 232 | buf->head = c - buf->data + 1; /* we need to convert pointer to |
| 233 | offset + skip the '\0' since |
| 234 | we need to point to the beginning |
| 235 | of the next message */ |
| 236 | /* Note: HEAD is only used to "retrieve" messages, it's not used |
| 237 | when writing messages into our buffer */ |
| 238 | } else { /* show an error message to know we messed up? */ |
| 239 | printf("Weird! Can't find the terminator token??? \n"); |
| 240 | buf->head = 0; |
| 241 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 242 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 243 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 244 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 245 | /* in other cases no overflows have been done yet, so we don't care! */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 246 | /* we should be ok to append the message now */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 247 | strncpy(buf->data + buf->tail, msg, l); /* append our message */ |
| 248 | buf->tail += l; /* count full message w/ '\0' terminating char */ |
| 249 | } else { |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 250 | /* we need to break up the message and "circle" it around */ |
| 251 | char *c; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 252 | int k = buf->tail + l - buf->size; /* count # of bytes we don't fit */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 253 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 254 | /* We need to move HEAD! This is always the case since we are going |
| 255 | * to "circle" the message. |
| 256 | */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 257 | c = memchr(buf->data + k, '\0', buf->size - k); |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 258 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 259 | if (c != NULL) { /* if we don't have '\0'??? weird!!! */ |
| 260 | /* move head pointer */ |
| 261 | buf->head = c - buf->data + 1; |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 262 | |
| 263 | /* now write the first part of the message */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 264 | strncpy(buf->data + buf->tail, msg, l - k - 1); |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 265 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 266 | /* ALWAYS terminate end of buffer w/ '\0' */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 267 | buf->data[buf->size - 1] = '\0'; |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 268 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 269 | /* now write out the rest of the string to the beginning of the buffer */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 270 | strcpy(buf->data, &msg[l - k - 1]); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 271 | |
| 272 | /* we need to place the TAIL at the end of the message */ |
| 273 | buf->tail = k + 1; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 274 | } else { |
| 275 | printf |
| 276 | ("Weird! Can't find the terminator token from the beginning??? \n"); |
| 277 | buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 278 | } |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 279 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 280 | } |
| 281 | sem_up(s_semid); |
| 282 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 283 | #endif /* CONFIG_FEATURE_IPC_SYSLOG */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 284 | |
Erik Andersen | c053e41 | 2000-03-21 01:31:24 +0000 | [diff] [blame] | 285 | /* Note: There is also a function called "message()" in init.c */ |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 286 | /* Print a message to the log file. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 287 | static void message(char *fmt, ...) __attribute__ ((format(printf, 1, 2))); |
| 288 | static void message(char *fmt, ...) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 289 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 290 | int fd; |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 291 | struct flock fl; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 292 | va_list arguments; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 293 | |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 294 | fl.l_whence = SEEK_SET; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 295 | fl.l_start = 0; |
| 296 | fl.l_len = 1; |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 297 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 298 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 299 | if ((circular_logging == TRUE) && (buf != NULL)) { |
| 300 | char b[1024]; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 301 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 302 | va_start(arguments, fmt); |
| 303 | vsnprintf(b, sizeof(b) - 1, fmt, arguments); |
| 304 | va_end(arguments); |
| 305 | circ_message(b); |
| 306 | |
| 307 | } else |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 308 | #endif |
"Vladimir N. Oleynik" | d3d829a | 2005-10-15 14:24:01 +0000 | [diff] [blame] | 309 | if ((fd = device_open(logFilePath, |
| 310 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 311 | O_NONBLOCK)) >= 0) { |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 312 | fl.l_type = F_WRLCK; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 313 | fcntl(fd, F_SETLKW, &fl); |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 314 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
| 315 | if ( logFileSize > 0 ) { |
| 316 | struct stat statf; |
| 317 | int r = fstat(fd, &statf); |
| 318 | if( !r && (statf.st_mode & S_IFREG) |
| 319 | && (lseek(fd,0,SEEK_END) > logFileSize) ) { |
| 320 | if(logFileRotate > 0) { |
| 321 | int i; |
Eric Andersen | 2c6b418 | 2005-09-12 19:16:11 +0000 | [diff] [blame] | 322 | char oldFile[(strlen(logFilePath)+4)], newFile[(strlen(logFilePath)+4)]; |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 323 | for(i=logFileRotate-1;i>0;i--) { |
| 324 | sprintf(oldFile, "%s.%d", logFilePath, i-1); |
| 325 | sprintf(newFile, "%s.%d", logFilePath, i); |
| 326 | rename(oldFile, newFile); |
| 327 | } |
| 328 | sprintf(newFile, "%s.%d", logFilePath, 0); |
| 329 | fl.l_type = F_UNLCK; |
| 330 | fcntl (fd, F_SETLKW, &fl); |
| 331 | close(fd); |
| 332 | rename(logFilePath, newFile); |
| 333 | fd = device_open (logFilePath, |
| 334 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 335 | O_NONBLOCK); |
| 336 | fl.l_type = F_WRLCK; |
| 337 | fcntl (fd, F_SETLKW, &fl); |
| 338 | } else { |
| 339 | ftruncate( fd, 0 ); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | #endif |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 344 | va_start(arguments, fmt); |
| 345 | vdprintf(fd, fmt, arguments); |
| 346 | va_end(arguments); |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 347 | fl.l_type = F_UNLCK; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 348 | fcntl(fd, F_SETLKW, &fl); |
| 349 | close(fd); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 350 | } else { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 351 | /* Always send console messages to /dev/console so people will see them. */ |
"Vladimir N. Oleynik" | d3d829a | 2005-10-15 14:24:01 +0000 | [diff] [blame] | 352 | if ((fd = device_open(_PATH_CONSOLE, |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 353 | O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { |
| 354 | va_start(arguments, fmt); |
| 355 | vdprintf(fd, fmt, arguments); |
| 356 | va_end(arguments); |
| 357 | close(fd); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 358 | } else { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 359 | fprintf(stderr, "Bummer, can't print: "); |
| 360 | va_start(arguments, fmt); |
| 361 | vfprintf(stderr, fmt, arguments); |
| 362 | fflush(stderr); |
| 363 | va_end(arguments); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 364 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 365 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Eric Andersen | f91b928 | 2004-09-08 10:56:06 +0000 | [diff] [blame] | 368 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
| 369 | static void init_RemoteLog(void) |
| 370 | { |
| 371 | memset(&remoteaddr, 0, sizeof(remoteaddr)); |
| 372 | remotefd = socket(AF_INET, SOCK_DGRAM, 0); |
| 373 | |
| 374 | if (remotefd < 0) { |
| 375 | bb_error_msg("cannot create socket"); |
| 376 | } |
| 377 | |
| 378 | remoteaddr.sin_family = AF_INET; |
| 379 | remoteaddr.sin_addr = *(struct in_addr *) *(xgethostbyname(RemoteHost))->h_addr_list; |
| 380 | remoteaddr.sin_port = htons(RemotePort); |
| 381 | } |
| 382 | #endif |
| 383 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 384 | static void logMessage(int pri, char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 385 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 386 | time_t now; |
| 387 | char *timestamp; |
"Vladimir N. Oleynik" | d3d829a | 2005-10-15 14:24:01 +0000 | [diff] [blame] | 388 | static char res[20]; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 389 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Eric Andersen | f91b928 | 2004-09-08 10:56:06 +0000 | [diff] [blame] | 390 | static char line[MAXLINE + 1]; |
Eric Andersen | 192c35f | 2004-09-02 22:22:17 +0000 | [diff] [blame] | 391 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 392 | CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 393 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 394 | if (pri != 0) { |
| 395 | for (c_fac = facilitynames; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 396 | c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 397 | for (c_pri = prioritynames; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 398 | c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); |
| 399 | if (c_fac->c_name == NULL || c_pri->c_name == NULL) { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 400 | snprintf(res, sizeof(res), "<%d>", pri); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 401 | } else { |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 402 | snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 403 | } |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 404 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 405 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 406 | if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 407 | msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 408 | time(&now); |
| 409 | timestamp = ctime(&now) + 4; |
| 410 | timestamp[15] = '\0'; |
| 411 | } else { |
| 412 | timestamp = msg; |
| 413 | timestamp[15] = '\0'; |
| 414 | msg += 16; |
| 415 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 416 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 417 | /* todo: supress duplicates */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 418 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 419 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 420 | if (doRemoteLog == TRUE) { |
| 421 | /* trying connect the socket */ |
| 422 | if (-1 == remotefd) { |
| 423 | init_RemoteLog(); |
| 424 | } |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 425 | |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 426 | /* if we have a valid socket, send the message */ |
| 427 | if (-1 != remotefd) { |
| 428 | now = 1; |
Paul Fox | 27cbffd | 2005-07-20 18:02:11 +0000 | [diff] [blame] | 429 | snprintf(line, sizeof(line), "<%d>%s", pri, msg); |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 430 | |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 431 | retry: |
| 432 | /* send message to remote logger */ |
| 433 | if(( -1 == sendto(remotefd, line, strlen(line), 0, |
| 434 | (struct sockaddr *) &remoteaddr, |
| 435 | sizeof(remoteaddr))) && (errno == EINTR)) { |
| 436 | /* sleep now seconds and retry (with now * 2) */ |
| 437 | sleep(now); |
| 438 | now *= 2; |
| 439 | goto retry; |
| 440 | } |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 443 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 444 | if (local_logging == TRUE) |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 445 | #endif |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 446 | { |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 447 | /* now spew out the message to wherever it is supposed to go */ |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 448 | if (small) |
| 449 | message("%s %s\n", timestamp, msg); |
| 450 | else |
| 451 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
| 452 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | static void quit_signal(int sig) |
| 456 | { |
Eric Andersen | 238bc40 | 2001-05-07 17:55:05 +0000 | [diff] [blame] | 457 | logMessage(LOG_SYSLOG | LOG_INFO, "System log daemon exiting."); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 458 | unlink(lfile); |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 459 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 460 | ipcsyslog_cleanup(); |
| 461 | #endif |
| 462 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 463 | exit(TRUE); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 466 | static void domark(int sig) |
| 467 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 468 | if (MarkInterval > 0) { |
| 469 | logMessage(LOG_SYSLOG | LOG_INFO, "-- MARK --"); |
| 470 | alarm(MarkInterval); |
| 471 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Eric Andersen | e527207 | 2003-07-22 22:15:21 +0000 | [diff] [blame] | 474 | /* This must be a #define, since when CONFIG_DEBUG and BUFFERS_GO_IN_BSS are |
Eric Andersen | 22ecf04 | 2001-07-02 17:32:40 +0000 | [diff] [blame] | 475 | * enabled, we otherwise get a "storage size isn't constant error. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 476 | static int serveConnection(char *tmpbuf, int n_read) |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 477 | { |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 478 | char *p = tmpbuf; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 479 | |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 480 | while (p < tmpbuf + n_read) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 481 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 482 | int pri = (LOG_USER | LOG_NOTICE); |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 483 | int num_lt = 0; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 484 | char line[MAXLINE + 1]; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 485 | unsigned char c; |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 486 | char *q = line; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 487 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 488 | while ((c = *p) && q < &line[sizeof(line) - 1]) { |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 489 | if (c == '<' && num_lt == 0) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 490 | /* Parse the magic priority number. */ |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 491 | num_lt++; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 492 | pri = 0; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 493 | while (isdigit(*(++p))) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 494 | pri = 10 * pri + (*p - '0'); |
| 495 | } |
Eric Andersen | 46ba568 | 2003-05-23 09:29:57 +0000 | [diff] [blame] | 496 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) { |
Eric Andersen | d4f90ed | 2003-05-23 09:28:01 +0000 | [diff] [blame] | 497 | pri = (LOG_USER | LOG_NOTICE); |
Eric Andersen | 46ba568 | 2003-05-23 09:29:57 +0000 | [diff] [blame] | 498 | } |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 499 | } else if (c == '\n') { |
| 500 | *q++ = ' '; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 501 | } else if (iscntrl(c) && (c < 0177)) { |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 502 | *q++ = '^'; |
| 503 | *q++ = c ^ 0100; |
| 504 | } else { |
| 505 | *q++ = c; |
| 506 | } |
| 507 | p++; |
| 508 | } |
| 509 | *q = '\0'; |
Matt Kraai | b6ec781 | 2001-08-14 17:32:23 +0000 | [diff] [blame] | 510 | p++; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 511 | /* Now log it */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 512 | logMessage(pri, line); |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 513 | } |
Mark Whitley | bff6b18 | 2001-03-27 20:17:58 +0000 | [diff] [blame] | 514 | return n_read; |
Pavel Roskin | da10ec0 | 2000-06-07 21:08:25 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Bernhard Reutner-Fischer | 86f5c99 | 2006-01-22 22:55:11 +0000 | [diff] [blame] | 517 | static void doSyslogd(void) ATTRIBUTE_NORETURN; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 518 | static void doSyslogd(void) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 519 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 520 | struct sockaddr_un sunx; |
Erik Andersen | 1d1d950 | 2000-04-21 01:26:49 +0000 | [diff] [blame] | 521 | socklen_t addrLength; |
| 522 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 523 | int sock_fd; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 524 | fd_set fds; |
| 525 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 526 | /* Set up signal handlers. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 527 | signal(SIGINT, quit_signal); |
| 528 | signal(SIGTERM, quit_signal); |
| 529 | signal(SIGQUIT, quit_signal); |
| 530 | signal(SIGHUP, SIG_IGN); |
| 531 | signal(SIGCHLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 532 | #ifdef SIGCLD |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 533 | signal(SIGCLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 534 | #endif |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 535 | signal(SIGALRM, domark); |
| 536 | alarm(MarkInterval); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 537 | |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 538 | /* Create the syslog file so realpath() can work. */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 539 | if (realpath(_PATH_LOG, lfile) != NULL) { |
| 540 | unlink(lfile); |
| 541 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 542 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 543 | memset(&sunx, 0, sizeof(sunx)); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 544 | sunx.sun_family = AF_UNIX; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 545 | strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path)); |
| 546 | if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 547 | bb_perror_msg_and_die("Couldn't get file descriptor for socket " |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 548 | _PATH_LOG); |
| 549 | } |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 550 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 551 | addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); |
| 552 | if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 553 | bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 554 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 555 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 556 | if (chmod(lfile, 0666) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 557 | bb_perror_msg_and_die("Could not set permission on " _PATH_LOG); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 558 | } |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 559 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 560 | if (circular_logging == TRUE) { |
| 561 | ipcsyslog_init(); |
Eric Andersen | ea90650 | 2001-04-05 20:55:17 +0000 | [diff] [blame] | 562 | } |
| 563 | #endif |
| 564 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 565 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 566 | if (doRemoteLog == TRUE) { |
| 567 | init_RemoteLog(); |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 568 | } |
| 569 | #endif |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 570 | |
"Vladimir N. Oleynik" | dd1ccdd | 2006-02-16 15:40:24 +0000 | [diff] [blame] | 571 | logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " "BusyBox v" BB_VER ); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 572 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 573 | for (;;) { |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 574 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 575 | FD_ZERO(&fds); |
| 576 | FD_SET(sock_fd, &fds); |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 577 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 578 | if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 579 | if (errno == EINTR) { |
| 580 | /* alarm may have happened. */ |
| 581 | continue; |
| 582 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 583 | bb_perror_msg_and_die("select error"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 584 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 585 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 586 | if (FD_ISSET(sock_fd, &fds)) { |
| 587 | int i; |
"Vladimir N. Oleynik" | b32b1db | 2005-10-15 13:49:21 +0000 | [diff] [blame] | 588 | #if MAXLINE > BUFSIZ |
| 589 | # define TMP_BUF_SZ BUFSIZ |
| 590 | #else |
| 591 | # define TMP_BUF_SZ MAXLINE |
| 592 | #endif |
| 593 | #define tmpbuf bb_common_bufsiz1 |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 594 | |
"Vladimir N. Oleynik" | b32b1db | 2005-10-15 13:49:21 +0000 | [diff] [blame] | 595 | if ((i = recv(sock_fd, tmpbuf, TMP_BUF_SZ, 0)) > 0) { |
| 596 | tmpbuf[i] = '\0'; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 597 | serveConnection(tmpbuf, i); |
| 598 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 599 | bb_perror_msg_and_die("UNIX socket error"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 600 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 601 | } /* FD_ISSET() */ |
| 602 | } /* for main loop */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 605 | int syslogd_main(int argc, char **argv) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 606 | { |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 607 | int opt; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 608 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 609 | int doFork = TRUE; |
| 610 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 611 | char *p; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 612 | |
Eric Andersen | 394cf22 | 2000-12-11 16:48:50 +0000 | [diff] [blame] | 613 | /* do normal option parsing */ |
Glenn L McGrath | 5529b7b | 2004-07-22 04:23:18 +0000 | [diff] [blame] | 614 | while ((opt = getopt(argc, argv, "m:nO:s:Sb:R:LC::")) > 0) { |
Eric Andersen | 394cf22 | 2000-12-11 16:48:50 +0000 | [diff] [blame] | 615 | switch (opt) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 616 | case 'm': |
| 617 | MarkInterval = atoi(optarg) * 60; |
| 618 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 619 | case 'n': |
| 620 | doFork = FALSE; |
| 621 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 622 | case 'O': |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 623 | logFilePath = optarg; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 624 | break; |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 625 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
| 626 | case 's': |
| 627 | logFileSize = atoi(optarg) * 1024; |
| 628 | break; |
| 629 | case 'b': |
| 630 | logFileRotate = atoi(optarg); |
| 631 | if( logFileRotate > 99 ) logFileRotate = 99; |
| 632 | break; |
| 633 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 634 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 635 | case 'R': |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 636 | RemoteHost = bb_xstrdup(optarg); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 637 | if ((p = strchr(RemoteHost, ':'))) { |
| 638 | RemotePort = atoi(p + 1); |
| 639 | *p = '\0'; |
| 640 | } |
| 641 | doRemoteLog = TRUE; |
| 642 | break; |
| 643 | case 'L': |
| 644 | local_logging = TRUE; |
| 645 | break; |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 646 | #endif |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 647 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 648 | case 'C': |
Glenn L McGrath | a79220d | 2003-09-26 00:49:05 +0000 | [diff] [blame] | 649 | if (optarg) { |
| 650 | int buf_size = atoi(optarg); |
| 651 | if (buf_size >= 4) { |
Glenn L McGrath | df2c565 | 2004-02-22 12:17:33 +0000 | [diff] [blame] | 652 | shm_size = buf_size * 1024; |
Glenn L McGrath | a79220d | 2003-09-26 00:49:05 +0000 | [diff] [blame] | 653 | } |
| 654 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 655 | circular_logging = TRUE; |
| 656 | break; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 657 | #endif |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 658 | case 'S': |
| 659 | small = true; |
| 660 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 661 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 662 | bb_show_usage(); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 663 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 666 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
Eric Andersen | 4ed1782 | 2000-12-11 19:28:29 +0000 | [diff] [blame] | 667 | /* If they have not specified remote logging, then log locally */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 668 | if (doRemoteLog == FALSE) |
Eric Andersen | 4ed1782 | 2000-12-11 19:28:29 +0000 | [diff] [blame] | 669 | local_logging = TRUE; |
| 670 | #endif |
| 671 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 672 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 673 | /* Store away localhost's name before the fork */ |
| 674 | gethostname(LocalHostName, sizeof(LocalHostName)); |
| 675 | if ((p = strchr(LocalHostName, '.'))) { |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 676 | *p = '\0'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 679 | umask(0); |
| 680 | |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 681 | if (doFork == TRUE) { |
Glenn L McGrath | dc72f3a | 2003-08-29 07:35:08 +0000 | [diff] [blame] | 682 | #if defined(__uClinux__) |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 683 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); |
| 684 | #else /* __uClinux__ */ |
| 685 | if(daemon(0, 1) < 0) |
| 686 | bb_perror_msg_and_die("daemon"); |
| 687 | #endif /* __uClinux__ */ |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 688 | } |
Eric Andersen | e5c24df | 2001-03-29 21:58:33 +0000 | [diff] [blame] | 689 | doSyslogd(); |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 690 | |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 691 | return EXIT_SUCCESS; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 692 | } |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
Erik Andersen | e3ed156 | 2000-04-19 18:52:56 +0000 | [diff] [blame] | 695 | Local Variables |
| 696 | c-file-style: "linux" |
| 697 | c-basic-offset: 4 |
| 698 | tab-width: 4 |
| 699 | End: |
| 700 | */ |