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