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