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