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