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