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