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> |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 18 | #include <sys/un.h> |
Eric Andersen | b186d98 | 1999-12-03 09:19:54 +0000 | [diff] [blame] | 19 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 20 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h: */ |
| 21 | /* prioritynames[] and facilitynames[]. uclibc pulls those in _rwdata_! :( */ |
| 22 | |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 23 | #define SYSLOG_NAMES |
| 24 | #include <sys/syslog.h> |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 25 | #include <sys/uio.h> |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 27 | #if ENABLE_FEATURE_REMOTE_LOG |
| 28 | #include <netinet/in.h> |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 29 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 31 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 32 | #include <sys/ipc.h> |
| 33 | #include <sys/sem.h> |
| 34 | #include <sys/shm.h> |
| 35 | #endif |
Denis Vlasenko | 1decd0e | 2006-09-30 19:17:40 +0000 | [diff] [blame] | 36 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 37 | |
| 38 | #define DEBUG 0 |
| 39 | |
| 40 | // Semaphore operation structures |
| 41 | struct shbuf_ds { |
| 42 | int32_t size; // size of data written |
| 43 | int32_t head; // start of message list |
| 44 | int32_t tail; // end of message list |
| 45 | char data[1]; // data/messages |
| 46 | }; // shared memory pointer |
| 47 | |
| 48 | struct globals { |
| 49 | |
| 50 | const char *logFilePath; |
| 51 | int logFD; |
| 52 | |
| 53 | /* This is not very useful, is bloat, and broken: |
| 54 | * can deadlock if alarmed to make MARK while writing to IPC buffer |
| 55 | * (semaphores are down but do_mark routine tries to down them again) */ |
| 56 | #ifdef SYSLOGD_MARK |
| 57 | /* interval between marks in seconds */ |
| 58 | int markInterval; |
| 59 | #endif |
| 60 | |
| 61 | /* level of messages to be locally logged */ |
| 62 | int logLevel; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 64 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 65 | /* max size of message file before being rotated */ |
| 66 | unsigned logFileSize; |
| 67 | /* number of rotated message files */ |
| 68 | unsigned logFileRotate; |
| 69 | unsigned curFileSize; |
| 70 | smallint isRegular; |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 73 | #if ENABLE_FEATURE_REMOTE_LOG |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 74 | /* udp socket for logging to remote host */ |
| 75 | int remoteFD; |
| 76 | len_and_sockaddr* remoteAddr; |
Eric Andersen | ced2cef | 2000-07-20 23:41:24 +0000 | [diff] [blame] | 77 | #endif |
| 78 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 79 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 80 | int shmid; // ipc shared memory id |
| 81 | int s_semid; // ipc semaphore id |
| 82 | int shm_size; |
| 83 | struct sembuf SMwup[1]; |
| 84 | struct sembuf SMwdn[3]; |
| 85 | struct shbuf_ds *shbuf; |
| 86 | #endif |
| 87 | |
| 88 | time_t last_log_time; |
| 89 | /* localhost's name */ |
| 90 | char localHostName[64]; |
| 91 | |
| 92 | }; /* struct globals */ |
| 93 | |
| 94 | static const struct globals init_globals = { |
| 95 | .logFilePath = "/var/log/messages", |
| 96 | .logFD = -1, |
| 97 | #ifdef SYSLOGD_MARK |
| 98 | .markInterval = 20 * 60, |
| 99 | #endif |
| 100 | .logLevel = 8, |
| 101 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
| 102 | .logFileSize = 200 * 1024, |
| 103 | .logFileRotate = 1, |
| 104 | #endif |
| 105 | #if ENABLE_FEATURE_REMOTE_LOG |
| 106 | .remoteFD = -1, |
| 107 | #endif |
| 108 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 109 | .shmid = -1, |
| 110 | .s_semid = -1, |
| 111 | .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size |
| 112 | .SMwup = { {1, -1, IPC_NOWAIT} }, |
| 113 | .SMwdn = { {0, 0}, {1, 0}, {1, +1} }, |
| 114 | #endif |
| 115 | // FIXME: hidden tail with lotsa zeroes is here.... |
| 116 | }; |
| 117 | |
| 118 | #define G (*ptr_to_globals) |
| 119 | |
| 120 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 121 | /* We are using bb_common_bufsiz1 for buffering: */ |
| 122 | enum { MAX_READ = (BUFSIZ/6) & ~0xf }; |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 123 | /* We recv into RECVBUF... (size: MAX_READ ~== BUFSIZ/6) */ |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 124 | #define RECVBUF bb_common_bufsiz1 |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 125 | /* ...then copy to PARSEBUF, escaping control chars */ |
| 126 | /* (can grow x2 max ~== BUFSIZ/3) */ |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 127 | #define PARSEBUF (bb_common_bufsiz1 + MAX_READ) |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 128 | /* ...then sprintf into PRINTBUF, adding timestamp (15 chars), |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 129 | * host (64), fac.prio (20) to the message */ |
| 130 | /* (growth by: 15 + 64 + 20 + delims = ~110) */ |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 131 | #define PRINTBUF (bb_common_bufsiz1 + 3*MAX_READ) |
| 132 | /* totals: BUFSIZ == BUFSIZ/6 + BUFSIZ/3 + (BUFSIZ/3+BUFSIZ/6) |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 133 | * -- we have BUFSIZ/6 extra at the ent of PRINTBUF |
| 134 | * which covers needed ~110 extra bytes (and much more) */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 135 | |
| 136 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 137 | /* Options */ |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 138 | enum { |
| 139 | OPTBIT_mark = 0, // -m |
| 140 | OPTBIT_nofork, // -n |
| 141 | OPTBIT_outfile, // -O |
| 142 | OPTBIT_loglevel, // -l |
| 143 | OPTBIT_small, // -S |
| 144 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s |
| 145 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b |
| 146 | USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R |
| 147 | USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L |
| 148 | USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C |
| 149 | |
| 150 | OPT_mark = 1 << OPTBIT_mark , |
| 151 | OPT_nofork = 1 << OPTBIT_nofork , |
| 152 | OPT_outfile = 1 << OPTBIT_outfile , |
| 153 | OPT_loglevel = 1 << OPTBIT_loglevel, |
| 154 | OPT_small = 1 << OPTBIT_small , |
| 155 | OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, |
| 156 | OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, |
| 157 | OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0, |
| 158 | OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0, |
| 159 | OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, |
| 160 | }; |
| 161 | #define OPTION_STR "m:nO:l:S" \ |
| 162 | USE_FEATURE_ROTATE_LOGFILE("s:" ) \ |
| 163 | USE_FEATURE_ROTATE_LOGFILE("b:" ) \ |
| 164 | USE_FEATURE_REMOTE_LOG( "R:" ) \ |
| 165 | USE_FEATURE_REMOTE_LOG( "L" ) \ |
| 166 | USE_FEATURE_IPC_SYSLOG( "C::") |
| 167 | #define OPTION_DECL *opt_m, *opt_l \ |
| 168 | USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \ |
| 169 | USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ |
| 170 | USE_FEATURE_REMOTE_LOG( ,*opt_R) \ |
| 171 | USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 172 | #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \ |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 173 | USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ |
| 174 | USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ |
| 175 | USE_FEATURE_REMOTE_LOG( ,&opt_R) \ |
| 176 | USE_FEATURE_IPC_SYSLOG( ,&opt_C) |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 177 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 178 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 179 | /* circular buffer variables/structures */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 180 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 181 | |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 182 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4 |
| 183 | #error Sorry, you must set the syslogd buffer size to at least 4KB. |
| 184 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE |
| 185 | #endif |
| 186 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 187 | /* our shared key */ |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 188 | #define KEY_ID ((long)0x414e4547) /* "GENA" */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 189 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 190 | static void ipcsyslog_cleanup(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 191 | { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 192 | if (G.shmid != -1) { |
| 193 | shmdt(G.shbuf); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 194 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 195 | if (G.shmid != -1) { |
| 196 | shmctl(G.shmid, IPC_RMID, NULL); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 197 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 198 | if (G.s_semid != -1) { |
| 199 | semctl(G.s_semid, 0, IPC_RMID, 0); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 200 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 201 | } |
| 202 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 203 | static void ipcsyslog_init(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 204 | { |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 205 | if (DEBUG) |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 206 | printf("shmget(%lx, %d,...)\n", KEY_ID, G.shm_size); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 207 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 208 | G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 1023); |
| 209 | if (G.shmid == -1) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 210 | bb_perror_msg_and_die("shmget"); |
| 211 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 212 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 213 | G.shbuf = shmat(G.shmid, NULL, 0); |
| 214 | if (!G.shbuf) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 215 | bb_perror_msg_and_die("shmat"); |
| 216 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 217 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 218 | G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data); |
| 219 | G.shbuf->head = G.shbuf->tail = 0; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 220 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 221 | // we'll trust the OS to set initial semval to 0 (let's hope) |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 222 | G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023); |
| 223 | if (G.s_semid == -1) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 224 | if (errno == EEXIST) { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 225 | G.s_semid = semget(KEY_ID, 2, 0); |
| 226 | if (G.s_semid != -1) |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 227 | return; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 228 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 229 | bb_perror_msg_and_die("semget"); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 233 | /* Write message to shared mem buffer */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 234 | static void log_to_shmem(const char *msg, int len) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 235 | { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 236 | int old_tail, new_tail; |
| 237 | char *c; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 238 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 239 | if (semop(G.s_semid, G.SMwdn, 3) == -1) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 240 | bb_perror_msg_and_die("SMwdn"); |
| 241 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 243 | /* Circular Buffer Algorithm: |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 244 | * -------------------------- |
Denis Vlasenko | 150f402 | 2007-01-13 21:06:21 +0000 | [diff] [blame] | 245 | * tail == position where to store next syslog message. |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 246 | * head == position of next message to retrieve ("print"). |
| 247 | * if head == tail, there is no "unprinted" messages left. |
| 248 | * head is typically advanced by separate "reader" program, |
| 249 | * but if there isn't one, we have to do it ourself. |
| 250 | * messages are NUL-separated. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 251 | */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 252 | len++; /* length with NUL included */ |
| 253 | again: |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 254 | old_tail = G.shbuf->tail; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 255 | new_tail = old_tail + len; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 256 | if (new_tail < G.shbuf->size) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 257 | /* No need to move head if shbuf->head <= old_tail, |
| 258 | * else... */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 259 | if (old_tail < G.shbuf->head && G.shbuf->head <= new_tail) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 260 | /* ...need to move head forward */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 261 | c = memchr(G.shbuf->data + new_tail, '\0', |
| 262 | G.shbuf->size - new_tail); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 263 | if (!c) /* no NUL ahead of us, wrap around */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 264 | c = memchr(G.shbuf->data, '\0', old_tail); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 265 | if (!c) { /* still nothing? point to this msg... */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 266 | G.shbuf->head = old_tail; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 267 | } else { |
| 268 | /* convert pointer to offset + skip NUL */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 269 | G.shbuf->head = c - G.shbuf->data + 1; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 270 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 271 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 272 | /* store message, set new tail */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 273 | memcpy(G.shbuf->data + old_tail, msg, len); |
| 274 | G.shbuf->tail = new_tail; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 275 | } else { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 276 | /* we need to break up the message and wrap it around */ |
| 277 | /* k == available buffer space ahead of old tail */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 278 | int k = G.shbuf->size - old_tail - 1; |
| 279 | if (G.shbuf->head > old_tail) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 280 | /* we are going to overwrite head, need to |
| 281 | * move it out of the way */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 282 | c = memchr(G.shbuf->data, '\0', old_tail); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 283 | if (!c) { /* nothing? point to this msg... */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 284 | G.shbuf->head = old_tail; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 285 | } else { /* convert pointer to offset + skip NUL */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 286 | G.shbuf->head = c - G.shbuf->data + 1; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 287 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 288 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 289 | /* copy what fits to the end of buffer, and repeat */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 290 | memcpy(G.shbuf->data + old_tail, msg, k); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 291 | msg += k; |
| 292 | len -= k; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 293 | G.shbuf->tail = 0; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 294 | goto again; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 295 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 296 | if (semop(G.s_semid, G.SMwup, 1) == -1) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 297 | bb_perror_msg_and_die("SMwup"); |
| 298 | } |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 299 | if (DEBUG) |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 300 | printf("head:%d tail:%d\n", G.shbuf->head, G.shbuf->tail); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 301 | } |
Rob Landley | 028ba28 | 2006-08-28 20:16:42 +0000 | [diff] [blame] | 302 | #else |
| 303 | void ipcsyslog_cleanup(void); |
| 304 | void ipcsyslog_init(void); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 305 | void log_to_shmem(const char *msg); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 306 | #endif /* FEATURE_IPC_SYSLOG */ |
| 307 | |
| 308 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 309 | /* Print a message to the log file. */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 310 | static void log_locally(char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 311 | { |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 312 | struct flock fl; |
| 313 | int len = strlen(msg); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 314 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 315 | #if ENABLE_FEATURE_IPC_SYSLOG |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 316 | if ((option_mask32 & OPT_circularlog) && G.shbuf) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 317 | log_to_shmem(msg, len); |
| 318 | return; |
| 319 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 320 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 321 | if (G.logFD >= 0) { |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 322 | time_t cur; |
| 323 | time(&cur); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 324 | if (G.last_log_time != cur) { |
| 325 | G.last_log_time = cur; /* reopen log file every second */ |
| 326 | close(G.logFD); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 327 | goto reopen; |
| 328 | } |
| 329 | } else { |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 330 | reopen: |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 331 | G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT |
Denis Vlasenko | bd8f43d | 2006-09-08 17:31:55 +0000 | [diff] [blame] | 332 | | O_NOCTTY | O_APPEND | O_NONBLOCK); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 333 | if (G.logFD < 0) { |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 334 | /* cannot open logfile? - print to /dev/console then */ |
| 335 | int fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); |
| 336 | if (fd < 0) |
| 337 | fd = 2; /* then stderr, dammit */ |
| 338 | full_write(fd, msg, len); |
| 339 | if (fd != 2) |
| 340 | close(fd); |
| 341 | return; |
| 342 | } |
| 343 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | bae7948 | 2007-01-09 23:42:43 +0000 | [diff] [blame] | 344 | { |
| 345 | struct stat statf; |
| 346 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 347 | G.isRegular = (fstat(G.logFD, &statf) == 0 && (statf.st_mode & S_IFREG)); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 348 | /* bug (mostly harmless): can wrap around if file > 4gb */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 349 | G.curFileSize = statf.st_size; |
Denis Vlasenko | bae7948 | 2007-01-09 23:42:43 +0000 | [diff] [blame] | 350 | } |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 351 | #endif |
| 352 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 353 | |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 354 | fl.l_whence = SEEK_SET; |
| 355 | fl.l_start = 0; |
| 356 | fl.l_len = 1; |
| 357 | fl.l_type = F_WRLCK; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 358 | fcntl(G.logFD, F_SETLKW, &fl); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 359 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 360 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 361 | if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) { |
| 362 | if (G.logFileRotate) { /* always 0..99 */ |
| 363 | int i = strlen(G.logFilePath) + 3 + 1; |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 364 | char oldFile[i]; |
| 365 | char newFile[i]; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 366 | i = G.logFileRotate - 1; |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 367 | /* rename: f.8 -> f.9; f.7 -> f.8; ... */ |
| 368 | while (1) { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 369 | sprintf(newFile, "%s.%d", G.logFilePath, i); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 370 | if (i == 0) break; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 371 | sprintf(oldFile, "%s.%d", G.logFilePath, --i); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 372 | rename(oldFile, newFile); |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 373 | } |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 374 | /* newFile == "f.0" now */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 375 | rename(G.logFilePath, newFile); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 376 | fl.l_type = F_UNLCK; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 377 | fcntl(G.logFD, F_SETLKW, &fl); |
| 378 | close(G.logFD); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 379 | goto reopen; |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 380 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 381 | ftruncate(G.logFD, 0); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 382 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 383 | G.curFileSize += |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 384 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 385 | full_write(G.logFD, msg, len); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 386 | fl.l_type = F_UNLCK; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 387 | fcntl(G.logFD, F_SETLKW, &fl); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 388 | } |
| 389 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 390 | static void parse_fac_prio_20(int pri, char *res20) |
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 | CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 393 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 394 | if (pri != 0) { |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 395 | c_fac = facilitynames; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 396 | while (c_fac->c_name) { |
| 397 | if (c_fac->c_val != (LOG_FAC(pri) << 3)) { |
| 398 | c_fac++; continue; |
| 399 | } |
| 400 | /* facility is found, look for prio */ |
| 401 | c_pri = prioritynames; |
| 402 | while (c_pri->c_name) { |
| 403 | if (c_pri->c_val != LOG_PRI(pri)) { |
| 404 | c_pri++; continue; |
| 405 | } |
| 406 | snprintf(res20, 20, "%s.%s", |
| 407 | c_fac->c_name, c_pri->c_name); |
| 408 | return; |
| 409 | } |
| 410 | /* prio not found, bail out */ |
| 411 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 412 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 413 | snprintf(res20, 20, "<%d>", pri); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 414 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 415 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 416 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 417 | /* len parameter is used only for "is there a timestamp?" check. |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 418 | * NB: some callers cheat and supply 0 when they know |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 419 | * that there is no timestamp, short-cutting the test. */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 420 | static void timestamp_and_log(int pri, char *msg, int len) |
| 421 | { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 422 | char *timestamp; |
| 423 | |
| 424 | if (len < 16 || msg[3] != ' ' || msg[6] != ' ' |
| 425 | || msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' |
| 426 | ) { |
Denis Vlasenko | bae7948 | 2007-01-09 23:42:43 +0000 | [diff] [blame] | 427 | time_t now; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 428 | time(&now); |
| 429 | timestamp = ctime(&now) + 4; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 430 | } else { |
| 431 | timestamp = msg; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 432 | msg += 16; |
| 433 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 434 | timestamp[15] = '\0'; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 435 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 436 | /* Log message locally (to file or shared mem) */ |
| 437 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 438 | if (LOG_PRI(pri) < G.logLevel) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 439 | if (option_mask32 & OPT_small) |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 440 | sprintf(PRINTBUF, "%s %s\n", timestamp, msg); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 441 | else { |
| 442 | char res[20]; |
| 443 | parse_fac_prio_20(pri, res); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 444 | sprintf(PRINTBUF, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 445 | } |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 446 | log_locally(PRINTBUF); |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 447 | } |
| 448 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 449 | } |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 450 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 451 | static void split_escape_and_log(char *tmpbuf, int len) |
| 452 | { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 453 | char *p = tmpbuf; |
| 454 | |
| 455 | tmpbuf += len; |
| 456 | while (p < tmpbuf) { |
| 457 | char c; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 458 | char *q = PARSEBUF; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 459 | int pri = (LOG_USER | LOG_NOTICE); |
| 460 | |
| 461 | if (*p == '<') { |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 462 | /* Parse the magic priority number */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 463 | pri = bb_strtou(p + 1, &p, 10); |
| 464 | if (*p == '>') p++; |
| 465 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) { |
| 466 | pri = (LOG_USER | LOG_NOTICE); |
| 467 | } |
Denis Vlasenko | 1decd0e | 2006-09-30 19:17:40 +0000 | [diff] [blame] | 468 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 469 | |
| 470 | while ((c = *p++)) { |
| 471 | if (c == '\n') |
| 472 | c = ' '; |
| 473 | if (!(c & ~0x1f)) { |
| 474 | *q++ = '^'; |
| 475 | c += '@'; /* ^@, ^A, ^B... */ |
| 476 | } |
| 477 | *q++ = c; |
| 478 | } |
| 479 | *q = '\0'; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 480 | /* Now log it */ |
| 481 | timestamp_and_log(pri, PARSEBUF, q - PARSEBUF); |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 482 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void quit_signal(int sig) |
| 486 | { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 487 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"syslogd exiting", 0); |
Denis Vlasenko | 1c962f2 | 2007-01-09 23:44:57 +0000 | [diff] [blame] | 488 | puts("syslogd exiting"); |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 489 | if (ENABLE_FEATURE_IPC_SYSLOG) |
| 490 | ipcsyslog_cleanup(); |
Denis Vlasenko | 02be0f5 | 2006-09-30 19:21:24 +0000 | [diff] [blame] | 491 | exit(1); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 494 | #ifdef SYSLOGD_MARK |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 495 | static void do_mark(int sig) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 496 | { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 497 | if (G.markInterval) { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 498 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 499 | alarm(G.markInterval); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 500 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 501 | } |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 502 | #endif |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 503 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 504 | static void do_syslogd(void) ATTRIBUTE_NORETURN; |
| 505 | static void do_syslogd(void) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 506 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 507 | struct sockaddr_un sunx; |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 508 | int sock_fd; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 509 | fd_set fds; |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 510 | char *dev_log_name; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 511 | |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 512 | /* Set up signal handlers */ |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 513 | signal(SIGINT, quit_signal); |
| 514 | signal(SIGTERM, quit_signal); |
| 515 | signal(SIGQUIT, quit_signal); |
| 516 | signal(SIGHUP, SIG_IGN); |
| 517 | signal(SIGCHLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 518 | #ifdef SIGCLD |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 519 | signal(SIGCLD, SIG_IGN); |
Pavel Roskin | d39d120 | 2000-09-13 14:14:29 +0000 | [diff] [blame] | 520 | #endif |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 521 | #ifdef SYSLOGD_MARK |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 522 | signal(SIGALRM, do_mark); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 523 | alarm(G.markInterval); |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 524 | #endif |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 525 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 526 | memset(&sunx, 0, sizeof(sunx)); |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 527 | sunx.sun_family = AF_UNIX; |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 528 | strcpy(sunx.sun_path, "/dev/log"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 529 | |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 530 | /* Unlink old /dev/log or object it points to. */ |
| 531 | /* (if it exists, bind will fail) */ |
| 532 | logmode = LOGMODE_NONE; |
| 533 | dev_log_name = xmalloc_readlink_or_warn("/dev/log"); |
| 534 | logmode = LOGMODE_STDIO; |
| 535 | if (dev_log_name) { |
| 536 | int fd = xopen(".", O_NONBLOCK); |
| 537 | xchdir("/dev"); |
| 538 | /* we do not check whether this is a link also */ |
| 539 | unlink(dev_log_name); |
| 540 | fchdir(fd); |
| 541 | close(fd); |
| 542 | safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path)); |
| 543 | free(dev_log_name); |
| 544 | } else { |
| 545 | unlink("/dev/log"); |
| 546 | } |
| 547 | |
| 548 | sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0); |
| 549 | xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx)); |
| 550 | |
| 551 | if (chmod("/dev/log", 0666) < 0) { |
| 552 | bb_perror_msg_and_die("cannot set permission on /dev/log"); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 553 | } |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 554 | if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 555 | ipcsyslog_init(); |
Eric Andersen | ea90650 | 2001-04-05 20:55:17 +0000 | [diff] [blame] | 556 | } |
Eric Andersen | ea90650 | 2001-04-05 20:55:17 +0000 | [diff] [blame] | 557 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 558 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, |
| 559 | (char*)"syslogd started: BusyBox v" BB_VER, 0); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 560 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 561 | for (;;) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 562 | FD_ZERO(&fds); |
| 563 | FD_SET(sock_fd, &fds); |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 564 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 565 | if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) { |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 566 | if (errno == EINTR) { |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 567 | /* alarm may have happened */ |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 568 | continue; |
| 569 | } |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 570 | bb_perror_msg_and_die("select"); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 571 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 572 | |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 573 | if (FD_ISSET(sock_fd, &fds)) { |
| 574 | int i; |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 575 | i = recv(sock_fd, RECVBUF, MAX_READ - 1, 0); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 576 | if (i <= 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 577 | bb_perror_msg_and_die("UNIX socket error"); |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 578 | /* TODO: maybe suppress duplicates? */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 579 | #if ENABLE_FEATURE_REMOTE_LOG |
| 580 | /* We are not modifying log messages in any way before send */ |
| 581 | /* Remote site cannot trust _us_ anyway and need to do validation again */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 582 | if (G.remoteAddr) { |
| 583 | if (-1 == G.remoteFD) { |
| 584 | G.remoteFD = socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 585 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 586 | if (-1 != G.remoteFD) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 587 | /* send message to remote logger, ignore possible error */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 588 | sendto(G.remoteFD, RECVBUF, i, MSG_DONTWAIT, |
| 589 | &G.remoteAddr->sa, G.remoteAddr->len); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 590 | } |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 591 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 592 | #endif |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 593 | RECVBUF[i] = '\0'; |
| 594 | split_escape_and_log(RECVBUF, i); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 595 | } /* FD_ISSET() */ |
| 596 | } /* for */ |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 599 | int syslogd_main(int argc, char **argv); |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 600 | int syslogd_main(int argc, char **argv) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 601 | { |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 602 | char OPTION_DECL; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 603 | char *p; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 604 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 605 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); |
| 606 | memcpy(ptr_to_globals, &init_globals, sizeof(init_globals)); |
| 607 | |
Eric Andersen | 394cf22 | 2000-12-11 16:48:50 +0000 | [diff] [blame] | 608 | /* do normal option parsing */ |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 609 | opt_complementary = "=0"; /* no non-option params */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 610 | getopt32(argc, argv, OPTION_STR, OPTION_PARAM); |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 611 | #ifdef SYSLOGD_MARK |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 612 | if (option_mask32 & OPT_mark) // -m |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 613 | G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 614 | #endif |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 615 | //if (option_mask32 & OPT_nofork) // -n |
| 616 | //if (option_mask32 & OPT_outfile) // -O |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 617 | if (option_mask32 & OPT_loglevel) // -l |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 618 | G.logLevel = xatou_range(opt_l, 1, 8); |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 619 | //if (option_mask32 & OPT_small) // -S |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 620 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 621 | if (option_mask32 & OPT_filesize) // -s |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 622 | G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 623 | if (option_mask32 & OPT_rotatecnt) // -b |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 624 | G.logFileRotate = xatou_range(opt_b, 0, 99); |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 625 | #endif |
| 626 | #if ENABLE_FEATURE_REMOTE_LOG |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 627 | if (option_mask32 & OPT_remotelog) { // -R |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 628 | G.remoteAddr = xhost2sockaddr(opt_R, 514); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 629 | } |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 630 | //if (option_mask32 & OPT_locallog) // -L |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 631 | #endif |
| 632 | #if ENABLE_FEATURE_IPC_SYSLOG |
Denis Vlasenko | 218f2f4 | 2007-01-24 22:02:01 +0000 | [diff] [blame] | 633 | if (opt_C) // -Cn |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 634 | G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024; |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 635 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 636 | |
Eric Andersen | 4ed1782 | 2000-12-11 19:28:29 +0000 | [diff] [blame] | 637 | /* If they have not specified remote logging, then log locally */ |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 638 | if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog)) |
| 639 | option_mask32 |= OPT_locallog; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 640 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 641 | /* Store away localhost's name before the fork */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 642 | gethostname(G.localHostName, sizeof(G.localHostName)); |
| 643 | p = strchr(G.localHostName, '.'); |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 644 | if (p) { |
Glenn L McGrath | fe538ba | 2003-09-10 23:35:45 +0000 | [diff] [blame] | 645 | *p = '\0'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 648 | if (!(option_mask32 & OPT_nofork)) { |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 649 | #ifdef BB_NOMMU |
Russ Dill | a1fece2 | 2003-12-15 21:57:44 +0000 | [diff] [blame] | 650 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 651 | #else |
Denis Vlasenko | 218f2f4 | 2007-01-24 22:02:01 +0000 | [diff] [blame] | 652 | bb_daemonize(); |
Bernhard Reutner-Fischer | c418d48 | 2006-05-31 10:19:51 +0000 | [diff] [blame] | 653 | #endif |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 654 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 655 | umask(0); |
| 656 | do_syslogd(); |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 657 | /* return EXIT_SUCCESS; */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 658 | } |