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 | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 16 | /* |
| 17 | * Done in syslogd_and_logger.c: |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 19 | #define SYSLOG_NAMES |
| 20 | #define SYSLOG_NAMES_CONST |
| 21 | #include <syslog.h> |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 22 | */ |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 23 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 24 | #include <sys/un.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 | |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 45 | /* Write locking does not seem to be useful either */ |
| 46 | #undef SYSLOGD_WRLOCK |
| 47 | |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 48 | enum { |
Janne Kiviluoto | c897dfe | 2010-03-31 15:58:58 +0200 | [diff] [blame] | 49 | MAX_READ = CONFIG_FEATURE_SYSLOGD_READ_BUFFER_SIZE, |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 50 | DNS_WAIT_SEC = 2 * 60, |
| 51 | }; |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 52 | |
| 53 | /* Semaphore operation structures */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 54 | struct shbuf_ds { |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 55 | int32_t size; /* size of data - 1 */ |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 56 | int32_t tail; /* end of message list */ |
| 57 | char data[1]; /* data/messages */ |
| 58 | }; |
| 59 | |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 60 | #if ENABLE_FEATURE_REMOTE_LOG |
| 61 | typedef struct { |
| 62 | int remoteFD; |
| 63 | unsigned last_dns_resolve; |
| 64 | len_and_sockaddr *remoteAddr; |
| 65 | const char *remoteHostname; |
| 66 | } remoteHost_t; |
| 67 | #endif |
| 68 | |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 69 | /* Allows us to have smaller initializer. Ugly. */ |
| 70 | #define GLOBALS \ |
| 71 | const char *logFilePath; \ |
| 72 | int logFD; \ |
| 73 | /* interval between marks in seconds */ \ |
| 74 | /*int markInterval;*/ \ |
| 75 | /* level of messages to be logged */ \ |
| 76 | int logLevel; \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 77 | IF_FEATURE_ROTATE_LOGFILE( \ |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 78 | /* max size of file before rotation */ \ |
| 79 | unsigned logFileSize; \ |
| 80 | /* number of rotated message files */ \ |
| 81 | unsigned logFileRotate; \ |
| 82 | unsigned curFileSize; \ |
| 83 | smallint isRegular; \ |
| 84 | ) \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 85 | IF_FEATURE_IPC_SYSLOG( \ |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 86 | int shmid; /* ipc shared memory id */ \ |
| 87 | int s_semid; /* ipc semaphore id */ \ |
| 88 | int shm_size; \ |
| 89 | struct sembuf SMwup[1]; \ |
| 90 | struct sembuf SMwdn[3]; \ |
| 91 | ) |
| 92 | |
| 93 | struct init_globals { |
| 94 | GLOBALS |
| 95 | }; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 96 | |
| 97 | struct globals { |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 98 | GLOBALS |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 99 | |
| 100 | #if ENABLE_FEATURE_REMOTE_LOG |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 101 | llist_t *remoteHosts; |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 102 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 103 | #if ENABLE_FEATURE_IPC_SYSLOG |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 104 | struct shbuf_ds *shbuf; |
| 105 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 106 | time_t last_log_time; |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 107 | /* localhost's name. We print only first 64 chars */ |
| 108 | char *hostname; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 109 | |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 110 | /* We recv into recvbuf... */ |
Denis Vlasenko | be048f2 | 2008-02-26 20:13:52 +0000 | [diff] [blame] | 111 | char recvbuf[MAX_READ * (1 + ENABLE_FEATURE_SYSLOGD_DUP)]; |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 112 | /* ...then copy to parsebuf, escaping control chars */ |
| 113 | /* (can grow x2 max) */ |
| 114 | char parsebuf[MAX_READ*2]; |
| 115 | /* ...then sprintf into printbuf, adding timestamp (15 chars), |
| 116 | * host (64), fac.prio (20) to the message */ |
| 117 | /* (growth by: 15 + 64 + 20 + delims = ~110) */ |
| 118 | char printbuf[MAX_READ*2 + 128]; |
| 119 | }; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 120 | |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 121 | static const struct init_globals init_data = { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 122 | .logFilePath = "/var/log/messages", |
| 123 | .logFD = -1, |
| 124 | #ifdef SYSLOGD_MARK |
| 125 | .markInterval = 20 * 60, |
| 126 | #endif |
| 127 | .logLevel = 8, |
| 128 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
| 129 | .logFileSize = 200 * 1024, |
| 130 | .logFileRotate = 1, |
| 131 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 132 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 133 | .shmid = -1, |
| 134 | .s_semid = -1, |
| 135 | .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size |
| 136 | .SMwup = { {1, -1, IPC_NOWAIT} }, |
| 137 | .SMwdn = { {0, 0}, {1, 0}, {1, +1} }, |
| 138 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | #define G (*ptr_to_globals) |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 142 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 143 | SET_PTR_TO_GLOBALS(memcpy(xzalloc(sizeof(G)), &init_data, sizeof(init_data))); \ |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 144 | } while (0) |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 145 | |
| 146 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 147 | /* Options */ |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 148 | enum { |
| 149 | OPTBIT_mark = 0, // -m |
| 150 | OPTBIT_nofork, // -n |
| 151 | OPTBIT_outfile, // -O |
| 152 | OPTBIT_loglevel, // -l |
| 153 | OPTBIT_small, // -S |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 154 | IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s |
| 155 | IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b |
| 156 | IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R |
| 157 | IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L |
| 158 | IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C |
| 159 | IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 160 | |
| 161 | OPT_mark = 1 << OPTBIT_mark , |
| 162 | OPT_nofork = 1 << OPTBIT_nofork , |
| 163 | OPT_outfile = 1 << OPTBIT_outfile , |
| 164 | OPT_loglevel = 1 << OPTBIT_loglevel, |
| 165 | OPT_small = 1 << OPTBIT_small , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 166 | OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, |
| 167 | OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, |
| 168 | OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0, |
| 169 | OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0, |
| 170 | OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, |
| 171 | OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0, |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 172 | }; |
| 173 | #define OPTION_STR "m:nO:l:S" \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 174 | IF_FEATURE_ROTATE_LOGFILE("s:" ) \ |
| 175 | IF_FEATURE_ROTATE_LOGFILE("b:" ) \ |
| 176 | IF_FEATURE_REMOTE_LOG( "R:" ) \ |
| 177 | IF_FEATURE_REMOTE_LOG( "L" ) \ |
| 178 | IF_FEATURE_IPC_SYSLOG( "C::") \ |
| 179 | IF_FEATURE_SYSLOGD_DUP( "D" ) |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 180 | #define OPTION_DECL *opt_m, *opt_l \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 181 | IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \ |
| 182 | IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \ |
| 183 | IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 184 | #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 185 | IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \ |
| 186 | IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \ |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 187 | IF_FEATURE_REMOTE_LOG( ,&remoteAddrList) \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 188 | IF_FEATURE_IPC_SYSLOG( ,&opt_C) |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 189 | |
Eric Andersen | 871d93c | 2002-09-17 20:06:29 +0000 | [diff] [blame] | 190 | |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 191 | /* circular buffer variables/structures */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 192 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 193 | |
Eric Andersen | d4a5e25 | 2003-12-19 11:32:14 +0000 | [diff] [blame] | 194 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4 |
| 195 | #error Sorry, you must set the syslogd buffer size to at least 4KB. |
| 196 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE |
| 197 | #endif |
| 198 | |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 199 | /* our shared key (syslogd.c and logread.c must be in sync) */ |
| 200 | enum { KEY_ID = 0x414e4547 }; /* "GENA" */ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 201 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 202 | static void ipcsyslog_cleanup(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 203 | { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 204 | if (G.shmid != -1) { |
| 205 | shmdt(G.shbuf); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 206 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 207 | if (G.shmid != -1) { |
| 208 | shmctl(G.shmid, IPC_RMID, NULL); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 209 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 210 | if (G.s_semid != -1) { |
| 211 | semctl(G.s_semid, 0, IPC_RMID, 0); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 212 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 213 | } |
| 214 | |
"Vladimir N. Oleynik" | e4baaa2 | 2005-09-22 12:59:26 +0000 | [diff] [blame] | 215 | static void ipcsyslog_init(void) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 216 | { |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 217 | if (DEBUG) |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 218 | printf("shmget(%x, %d,...)\n", (int)KEY_ID, G.shm_size); |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 219 | |
Denis Vlasenko | a1120a8 | 2007-08-14 10:27:56 +0000 | [diff] [blame] | 220 | G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 0644); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 221 | if (G.shmid == -1) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 222 | bb_perror_msg_and_die("shmget"); |
| 223 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 224 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 225 | G.shbuf = shmat(G.shmid, NULL, 0); |
Denis Vlasenko | 4e9ca75 | 2008-01-07 15:58:02 +0000 | [diff] [blame] | 226 | if (G.shbuf == (void*) -1L) { /* shmat has bizarre error return */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 227 | bb_perror_msg_and_die("shmat"); |
| 228 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 229 | |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 230 | memset(G.shbuf, 0, G.shm_size); |
| 231 | G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data) - 1; |
| 232 | /*G.shbuf->tail = 0;*/ |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 234 | // 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] | 235 | G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023); |
| 236 | if (G.s_semid == -1) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 237 | if (errno == EEXIST) { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 238 | G.s_semid = semget(KEY_ID, 2, 0); |
| 239 | if (G.s_semid != -1) |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 240 | return; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 241 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 242 | bb_perror_msg_and_die("semget"); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 246 | /* Write message to shared mem buffer */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 247 | static void log_to_shmem(const char *msg, int len) |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 248 | { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 249 | int old_tail, new_tail; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 250 | |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 251 | if (semop(G.s_semid, G.SMwdn, 3) == -1) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 252 | bb_perror_msg_and_die("SMwdn"); |
| 253 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 255 | /* Circular Buffer Algorithm: |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 256 | * -------------------------- |
Denis Vlasenko | 150f402 | 2007-01-13 21:06:21 +0000 | [diff] [blame] | 257 | * tail == position where to store next syslog message. |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 258 | * tail's max value is (shbuf->size - 1) |
| 259 | * Last byte of buffer is never used and remains NUL. |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 260 | */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 261 | len++; /* length with NUL included */ |
| 262 | again: |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 263 | old_tail = G.shbuf->tail; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 264 | new_tail = old_tail + len; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 265 | if (new_tail < G.shbuf->size) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 266 | /* store message, set new tail */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 267 | memcpy(G.shbuf->data + old_tail, msg, len); |
| 268 | G.shbuf->tail = new_tail; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 269 | } else { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 270 | /* k == available buffer space ahead of old tail */ |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 271 | int k = G.shbuf->size - old_tail; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 272 | /* copy what fits to the end of buffer, and repeat */ |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 273 | memcpy(G.shbuf->data + old_tail, msg, k); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 274 | msg += k; |
| 275 | len -= k; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 276 | G.shbuf->tail = 0; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 277 | goto again; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 278 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 279 | if (semop(G.s_semid, G.SMwup, 1) == -1) { |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 280 | bb_perror_msg_and_die("SMwup"); |
| 281 | } |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 282 | if (DEBUG) |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 283 | printf("tail:%d\n", G.shbuf->tail); |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 284 | } |
Rob Landley | 028ba28 | 2006-08-28 20:16:42 +0000 | [diff] [blame] | 285 | #else |
| 286 | void ipcsyslog_cleanup(void); |
| 287 | void ipcsyslog_init(void); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 288 | void log_to_shmem(const char *msg); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 289 | #endif /* FEATURE_IPC_SYSLOG */ |
| 290 | |
| 291 | |
Erik Andersen | 983b51b | 2000-04-04 18:14:25 +0000 | [diff] [blame] | 292 | /* Print a message to the log file. */ |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 293 | static void log_locally(time_t now, char *msg) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 294 | { |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 295 | #ifdef SYSLOGD_WRLOCK |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 296 | struct flock fl; |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 297 | #endif |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 298 | int len = strlen(msg); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 299 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 300 | #if ENABLE_FEATURE_IPC_SYSLOG |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 301 | if ((option_mask32 & OPT_circularlog) && G.shbuf) { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 302 | log_to_shmem(msg, len); |
| 303 | return; |
| 304 | } |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 305 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 306 | if (G.logFD >= 0) { |
Denis Vlasenko | d9415d6 | 2009-03-02 14:26:28 +0000 | [diff] [blame] | 307 | /* Reopen log file every second. This allows admin |
| 308 | * to delete the file and not worry about restarting us. |
| 309 | * This costs almost nothing since it happens |
| 310 | * _at most_ once a second. |
| 311 | */ |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 312 | if (!now) |
| 313 | now = time(NULL); |
| 314 | if (G.last_log_time != now) { |
Denis Vlasenko | d9415d6 | 2009-03-02 14:26:28 +0000 | [diff] [blame] | 315 | G.last_log_time = now; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 316 | close(G.logFD); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 317 | goto reopen; |
| 318 | } |
| 319 | } else { |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 320 | reopen: |
Denis Vlasenko | d9415d6 | 2009-03-02 14:26:28 +0000 | [diff] [blame] | 321 | G.logFD = open(G.logFilePath, O_WRONLY | O_CREAT |
| 322 | | O_NOCTTY | O_APPEND | O_NONBLOCK, |
| 323 | 0666); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 324 | if (G.logFD < 0) { |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 325 | /* cannot open logfile? - print to /dev/console then */ |
Denis Vlasenko | 70ab28f | 2007-11-18 05:43:05 +0000 | [diff] [blame] | 326 | int fd = device_open(DEV_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 327 | if (fd < 0) |
| 328 | fd = 2; /* then stderr, dammit */ |
| 329 | full_write(fd, msg, len); |
| 330 | if (fd != 2) |
| 331 | close(fd); |
| 332 | return; |
| 333 | } |
| 334 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | bae7948 | 2007-01-09 23:42:43 +0000 | [diff] [blame] | 335 | { |
Denis Vlasenko | 5f1b149 | 2007-08-12 21:33:06 +0000 | [diff] [blame] | 336 | struct stat statf; |
| 337 | G.isRegular = (fstat(G.logFD, &statf) == 0 && S_ISREG(statf.st_mode)); |
| 338 | /* bug (mostly harmless): can wrap around if file > 4gb */ |
| 339 | G.curFileSize = statf.st_size; |
Denis Vlasenko | bae7948 | 2007-01-09 23:42:43 +0000 | [diff] [blame] | 340 | } |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 341 | #endif |
| 342 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 343 | |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 344 | #ifdef SYSLOGD_WRLOCK |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 345 | fl.l_whence = SEEK_SET; |
| 346 | fl.l_start = 0; |
| 347 | fl.l_len = 1; |
| 348 | fl.l_type = F_WRLCK; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 349 | fcntl(G.logFD, F_SETLKW, &fl); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 350 | #endif |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 351 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 352 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 353 | if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) { |
| 354 | if (G.logFileRotate) { /* always 0..99 */ |
| 355 | int i = strlen(G.logFilePath) + 3 + 1; |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 356 | char oldFile[i]; |
| 357 | char newFile[i]; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 358 | i = G.logFileRotate - 1; |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 359 | /* rename: f.8 -> f.9; f.7 -> f.8; ... */ |
| 360 | while (1) { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 361 | sprintf(newFile, "%s.%d", G.logFilePath, i); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 362 | if (i == 0) break; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 363 | sprintf(oldFile, "%s.%d", G.logFilePath, --i); |
Denis Vlasenko | 69dc325 | 2008-05-24 21:03:53 +0000 | [diff] [blame] | 364 | /* ignore errors - file might be missing */ |
| 365 | rename(oldFile, newFile); |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 366 | } |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 367 | /* newFile == "f.0" now */ |
Denis Vlasenko | 69dc325 | 2008-05-24 21:03:53 +0000 | [diff] [blame] | 368 | rename(G.logFilePath, newFile); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 369 | #ifdef SYSLOGD_WRLOCK |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 370 | fl.l_type = F_UNLCK; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 371 | fcntl(G.logFD, F_SETLKW, &fl); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 372 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 373 | close(G.logFD); |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 374 | goto reopen; |
Eric Andersen | 29c77f7 | 2003-10-09 09:43:18 +0000 | [diff] [blame] | 375 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 376 | ftruncate(G.logFD, 0); |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 377 | } |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 378 | G.curFileSize += |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 379 | #endif |
Denis Vlasenko | 9504e44 | 2008-10-29 01:19:15 +0000 | [diff] [blame] | 380 | full_write(G.logFD, msg, len); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 381 | #ifdef SYSLOGD_WRLOCK |
Denis Vlasenko | b893497 | 2007-01-04 18:02:32 +0000 | [diff] [blame] | 382 | fl.l_type = F_UNLCK; |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 383 | fcntl(G.logFD, F_SETLKW, &fl); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 384 | #endif |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 387 | static void parse_fac_prio_20(int pri, char *res20) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 388 | { |
Denis Vlasenko | 3c8b5ba | 2007-06-04 18:23:59 +0000 | [diff] [blame] | 389 | const CODE *c_pri, *c_fac; |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 390 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 391 | if (pri != 0) { |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 392 | c_fac = facilitynames; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 393 | while (c_fac->c_name) { |
| 394 | if (c_fac->c_val != (LOG_FAC(pri) << 3)) { |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 395 | c_fac++; |
| 396 | continue; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 397 | } |
| 398 | /* facility is found, look for prio */ |
| 399 | c_pri = prioritynames; |
| 400 | while (c_pri->c_name) { |
| 401 | if (c_pri->c_val != LOG_PRI(pri)) { |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 402 | c_pri++; |
| 403 | continue; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 404 | } |
| 405 | snprintf(res20, 20, "%s.%s", |
| 406 | c_fac->c_name, c_pri->c_name); |
| 407 | return; |
| 408 | } |
| 409 | /* prio not found, bail out */ |
| 410 | break; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 411 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 412 | snprintf(res20, 20, "<%d>", pri); |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 413 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 414 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 415 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 416 | /* len parameter is used only for "is there a timestamp?" check. |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 417 | * NB: some callers cheat and supply len==0 when they know |
| 418 | * that there is no timestamp, short-circuiting the test. */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 419 | static void timestamp_and_log(int pri, char *msg, int len) |
| 420 | { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 421 | char *timestamp; |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 422 | time_t now; |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 423 | |
Denis Vlasenko | 574c316 | 2009-04-22 02:53:02 +0000 | [diff] [blame] | 424 | /* Jan 18 00:11:22 msg... */ |
| 425 | /* 01234567890123456 */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 426 | if (len < 16 || msg[3] != ' ' || msg[6] != ' ' |
| 427 | || msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' |
| 428 | ) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 429 | time(&now); |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 430 | timestamp = ctime(&now) + 4; /* skip day of week */ |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 431 | } else { |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 432 | now = 0; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 433 | timestamp = msg; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 434 | msg += 16; |
| 435 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 436 | timestamp[15] = '\0'; |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 437 | |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 438 | if (option_mask32 & OPT_small) |
| 439 | sprintf(G.printbuf, "%s %s\n", timestamp, msg); |
| 440 | else { |
| 441 | char res[20]; |
| 442 | parse_fac_prio_20(pri, res); |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 443 | sprintf(G.printbuf, "%s %.64s %s %s\n", timestamp, G.hostname, res, msg); |
Eric Andersen | bf2b8ae | 2000-12-08 19:52:01 +0000 | [diff] [blame] | 444 | } |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 445 | |
| 446 | /* Log message locally (to file or shared mem) */ |
| 447 | log_locally(now, G.printbuf); |
| 448 | } |
| 449 | |
| 450 | static void timestamp_and_log_internal(const char *msg) |
| 451 | { |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 452 | /* -L, or no -R */ |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 453 | if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog)) |
| 454 | return; |
| 455 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 456 | } |
Glenn L McGrath | 73ebb88 | 2004-09-14 18:12:13 +0000 | [diff] [blame] | 457 | |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 458 | /* tmpbuf[len] is a NUL byte (set by caller), but there can be other, |
| 459 | * embedded NULs. Split messages on each of these NULs, parse prio, |
| 460 | * escape control chars and log each locally. */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 461 | static void split_escape_and_log(char *tmpbuf, int len) |
| 462 | { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 463 | char *p = tmpbuf; |
| 464 | |
| 465 | tmpbuf += len; |
| 466 | while (p < tmpbuf) { |
| 467 | char c; |
Denis Vlasenko | 4f93cde | 2007-03-20 20:03:03 +0000 | [diff] [blame] | 468 | char *q = G.parsebuf; |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 469 | int pri = (LOG_USER | LOG_NOTICE); |
| 470 | |
| 471 | if (*p == '<') { |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 472 | /* Parse the magic priority number */ |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 473 | pri = bb_strtou(p + 1, &p, 10); |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 474 | if (*p == '>') |
| 475 | p++; |
| 476 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 477 | pri = (LOG_USER | LOG_NOTICE); |
Denis Vlasenko | 1decd0e | 2006-09-30 19:17:40 +0000 | [diff] [blame] | 478 | } |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 479 | |
| 480 | while ((c = *p++)) { |
| 481 | if (c == '\n') |
| 482 | c = ' '; |
Denis Vlasenko | 2ee028d | 2007-06-21 13:44:53 +0000 | [diff] [blame] | 483 | if (!(c & ~0x1f) && c != '\t') { |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 484 | *q++ = '^'; |
| 485 | c += '@'; /* ^@, ^A, ^B... */ |
| 486 | } |
| 487 | *q++ = c; |
| 488 | } |
| 489 | *q = '\0'; |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 490 | |
Denis Vlasenko | a0e2a0a | 2007-01-04 21:22:11 +0000 | [diff] [blame] | 491 | /* Now log it */ |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 492 | if (LOG_PRI(pri) < G.logLevel) |
| 493 | timestamp_and_log(pri, G.parsebuf, q - G.parsebuf); |
Eric Andersen | 7f94a5c | 2004-06-22 10:12:59 +0000 | [diff] [blame] | 494 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 497 | #ifdef SYSLOGD_MARK |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 498 | static void do_mark(int sig) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 499 | { |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 500 | if (G.markInterval) { |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 501 | timestamp_and_log_internal("-- MARK --"); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 502 | alarm(G.markInterval); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 503 | } |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 504 | } |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 505 | #endif |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 506 | |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 507 | /* Don't inline: prevent struct sockaddr_un to take up space on stack |
| 508 | * permanently */ |
| 509 | static NOINLINE int create_socket(void) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 510 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 511 | struct sockaddr_un sunx; |
Denis Vlasenko | d7ecd86 | 2007-09-07 13:53:32 +0000 | [diff] [blame] | 512 | int sock_fd; |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 513 | char *dev_log_name; |
Erik Andersen | f13df37 | 2000-04-18 23:51:51 +0000 | [diff] [blame] | 514 | |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 515 | memset(&sunx, 0, sizeof(sunx)); |
| 516 | sunx.sun_family = AF_UNIX; |
| 517 | |
| 518 | /* Unlink old /dev/log or object it points to. */ |
| 519 | /* (if it exists, bind will fail) */ |
| 520 | strcpy(sunx.sun_path, "/dev/log"); |
| 521 | dev_log_name = xmalloc_follow_symlinks("/dev/log"); |
| 522 | if (dev_log_name) { |
| 523 | safe_strncpy(sunx.sun_path, dev_log_name, sizeof(sunx.sun_path)); |
| 524 | free(dev_log_name); |
| 525 | } |
| 526 | unlink(sunx.sun_path); |
| 527 | |
| 528 | sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0); |
| 529 | xbind(sock_fd, (struct sockaddr *) &sunx, sizeof(sunx)); |
| 530 | chmod("/dev/log", 0666); |
| 531 | |
| 532 | return sock_fd; |
| 533 | } |
| 534 | |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 535 | #if ENABLE_FEATURE_REMOTE_LOG |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 536 | static int try_to_resolve_remote(remoteHost_t *rh) |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 537 | { |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 538 | if (!rh->remoteAddr) { |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 539 | unsigned now = monotonic_sec(); |
| 540 | |
| 541 | /* Don't resolve name too often - DNS timeouts can be big */ |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 542 | if ((now - rh->last_dns_resolve) < DNS_WAIT_SEC) |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 543 | return -1; |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 544 | rh->last_dns_resolve = now; |
| 545 | rh->remoteAddr = host2sockaddr(rh->remoteHostname, 514); |
| 546 | if (!rh->remoteAddr) |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 547 | return -1; |
| 548 | } |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 549 | return socket(rh->remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0); |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 550 | } |
| 551 | #endif |
| 552 | |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 553 | static void do_syslogd(void) NORETURN; |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 554 | static void do_syslogd(void) |
| 555 | { |
| 556 | int sock_fd; |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 557 | #if ENABLE_FEATURE_REMOTE_LOG |
| 558 | llist_t *item; |
| 559 | #endif |
Denis Vlasenko | be048f2 | 2008-02-26 20:13:52 +0000 | [diff] [blame] | 560 | #if ENABLE_FEATURE_SYSLOGD_DUP |
| 561 | int last_sz = -1; |
| 562 | char *last_buf; |
| 563 | char *recvbuf = G.recvbuf; |
| 564 | #else |
| 565 | #define recvbuf (G.recvbuf) |
| 566 | #endif |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 567 | |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 568 | /* Set up signal handlers (so that they interrupt read()) */ |
| 569 | signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo); |
| 570 | signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); |
| 571 | //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo); |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 572 | signal(SIGHUP, SIG_IGN); |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 573 | #ifdef SYSLOGD_MARK |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 574 | signal(SIGALRM, do_mark); |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 575 | alarm(G.markInterval); |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 576 | #endif |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 577 | sock_fd = create_socket(); |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 578 | |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 579 | if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask32 & OPT_circularlog)) { |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 580 | ipcsyslog_init(); |
Eric Andersen | ea90650 | 2001-04-05 20:55:17 +0000 | [diff] [blame] | 581 | } |
Eric Andersen | ea90650 | 2001-04-05 20:55:17 +0000 | [diff] [blame] | 582 | |
Denis Vlasenko | 4dada74 | 2008-01-03 12:13:42 +0000 | [diff] [blame] | 583 | timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 584 | |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 585 | while (!bb_got_signal) { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 586 | ssize_t sz; |
Denis Vlasenko | be048f2 | 2008-02-26 20:13:52 +0000 | [diff] [blame] | 587 | |
| 588 | #if ENABLE_FEATURE_SYSLOGD_DUP |
| 589 | last_buf = recvbuf; |
| 590 | if (recvbuf == G.recvbuf) |
| 591 | recvbuf = G.recvbuf + MAX_READ; |
| 592 | else |
| 593 | recvbuf = G.recvbuf; |
| 594 | #endif |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 595 | read_again: |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 596 | sz = read(sock_fd, recvbuf, MAX_READ - 1); |
| 597 | if (sz < 0) { |
| 598 | if (!bb_got_signal) |
| 599 | bb_perror_msg("read from /dev/log"); |
| 600 | break; |
| 601 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 602 | |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 603 | /* Drop trailing '\n' and NULs (typically there is one NUL) */ |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 604 | while (1) { |
| 605 | if (sz == 0) |
| 606 | goto read_again; |
Denis Vlasenko | cb12cb2 | 2007-11-06 11:34:03 +0000 | [diff] [blame] | 607 | /* man 3 syslog says: "A trailing newline is added when needed". |
| 608 | * However, neither glibc nor uclibc do this: |
| 609 | * syslog(prio, "test") sends "test\0" to /dev/log, |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 610 | * syslog(prio, "test\n") sends "test\n\0". |
Denis Vlasenko | cb12cb2 | 2007-11-06 11:34:03 +0000 | [diff] [blame] | 611 | * IOW: newline is passed verbatim! |
| 612 | * I take it to mean that it's syslogd's job |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 613 | * to make those look identical in the log files. */ |
Denis Vlasenko | be048f2 | 2008-02-26 20:13:52 +0000 | [diff] [blame] | 614 | if (recvbuf[sz-1] != '\0' && recvbuf[sz-1] != '\n') |
Denis Vlasenko | 018b155 | 2007-11-06 01:38:46 +0000 | [diff] [blame] | 615 | break; |
| 616 | sz--; |
| 617 | } |
Denis Vlasenko | be048f2 | 2008-02-26 20:13:52 +0000 | [diff] [blame] | 618 | #if ENABLE_FEATURE_SYSLOGD_DUP |
| 619 | if ((option_mask32 & OPT_dup) && (sz == last_sz)) |
| 620 | if (memcmp(last_buf, recvbuf, sz) == 0) |
| 621 | continue; |
| 622 | last_sz = sz; |
| 623 | #endif |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 624 | #if ENABLE_FEATURE_REMOTE_LOG |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 625 | /* Stock syslogd sends it '\n'-terminated |
| 626 | * over network, mimic that */ |
| 627 | recvbuf[sz] = '\n'; |
| 628 | |
Denis Vlasenko | d7ecd86 | 2007-09-07 13:53:32 +0000 | [diff] [blame] | 629 | /* We are not modifying log messages in any way before send */ |
| 630 | /* Remote site cannot trust _us_ anyway and need to do validation again */ |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 631 | for (item = G.remoteHosts; item != NULL; item = item->link) { |
| 632 | remoteHost_t *rh = (remoteHost_t *)item->data; |
| 633 | |
| 634 | if (rh->remoteFD == -1) { |
| 635 | rh->remoteFD = try_to_resolve_remote(rh); |
| 636 | if (rh->remoteFD == -1) |
| 637 | continue; |
Glenn L McGrath | 912d8f4 | 2002-11-10 22:46:45 +0000 | [diff] [blame] | 638 | } |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 639 | /* Send message to remote logger, ignore possible error */ |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 640 | /* TODO: on some errors, close and set G.remoteFD to -1 |
| 641 | * so that DNS resolution and connect is retried? */ |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 642 | sendto(rh->remoteFD, recvbuf, sz+1, MSG_DONTWAIT, |
| 643 | &(rh->remoteAddr->u.sa), rh->remoteAddr->len); |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 644 | } |
Denis Vlasenko | d7ecd86 | 2007-09-07 13:53:32 +0000 | [diff] [blame] | 645 | #endif |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 646 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) { |
Denis Vlasenko | be048f2 | 2008-02-26 20:13:52 +0000 | [diff] [blame] | 647 | recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */ |
| 648 | split_escape_and_log(recvbuf, sz); |
Denis Vlasenko | 75cddd8 | 2008-02-13 09:19:14 +0000 | [diff] [blame] | 649 | } |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 650 | } /* while (!bb_got_signal) */ |
| 651 | |
| 652 | timestamp_and_log_internal("syslogd exiting"); |
| 653 | puts("syslogd exiting"); |
| 654 | if (ENABLE_FEATURE_IPC_SYSLOG) |
| 655 | ipcsyslog_cleanup(); |
| 656 | kill_myself_with_sig(bb_got_signal); |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 657 | #undef recvbuf |
Eric Andersen | b99df0f | 1999-11-24 09:04:33 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 660 | int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 661 | int syslogd_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 662 | { |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 663 | int opts; |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 664 | char OPTION_DECL; |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 665 | #if ENABLE_FEATURE_REMOTE_LOG |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 666 | llist_t *remoteAddrList = NULL; |
Denis Vlasenko | 4f2e8bc | 2008-01-03 12:12:27 +0000 | [diff] [blame] | 667 | #endif |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 668 | |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 669 | INIT_G(); |
| 670 | |
| 671 | /* No non-option params, -R can occur multiple times */ |
| 672 | opt_complementary = "=0" IF_FEATURE_REMOTE_LOG(":R::"); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 673 | opts = getopt32(argv, OPTION_STR, OPTION_PARAM); |
Thomas Geulig | d2f7779 | 2010-02-28 13:01:59 +0100 | [diff] [blame] | 674 | #if ENABLE_FEATURE_REMOTE_LOG |
| 675 | while (remoteAddrList) { |
| 676 | remoteHost_t *rh = xzalloc(sizeof(*rh)); |
| 677 | rh->remoteHostname = llist_pop(&remoteAddrList); |
| 678 | rh->remoteFD = -1; |
| 679 | rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1; |
| 680 | llist_add_to(&G.remoteHosts, rh); |
| 681 | } |
| 682 | #endif |
| 683 | |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 684 | #ifdef SYSLOGD_MARK |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 685 | if (opts & OPT_mark) // -m |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 686 | G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; |
Denis Vlasenko | 4998c81 | 2007-02-14 20:51:46 +0000 | [diff] [blame] | 687 | #endif |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 688 | //if (opts & OPT_nofork) // -n |
| 689 | //if (opts & OPT_outfile) // -O |
| 690 | if (opts & OPT_loglevel) // -l |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 691 | G.logLevel = xatou_range(opt_l, 1, 8); |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 692 | //if (opts & OPT_small) // -S |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 693 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 694 | if (opts & OPT_filesize) // -s |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 695 | G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024; |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 696 | if (opts & OPT_rotatecnt) // -b |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 697 | G.logFileRotate = xatou_range(opt_b, 0, 99); |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 698 | #endif |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 699 | #if ENABLE_FEATURE_IPC_SYSLOG |
Denis Vlasenko | 218f2f4 | 2007-01-24 22:02:01 +0000 | [diff] [blame] | 700 | if (opt_C) // -Cn |
Denis Vlasenko | 5e892ba | 2007-03-15 19:50:46 +0000 | [diff] [blame] | 701 | G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024; |
Denis Vlasenko | 14c1940 | 2006-09-30 19:20:00 +0000 | [diff] [blame] | 702 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 703 | |
Eric Andersen | 4ed1782 | 2000-12-11 19:28:29 +0000 | [diff] [blame] | 704 | /* If they have not specified remote logging, then log locally */ |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 705 | if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 706 | option_mask32 |= OPT_locallog; |
Mark Whitley | 6317c4b | 2001-03-12 22:51:50 +0000 | [diff] [blame] | 707 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 708 | /* Store away localhost's name before the fork */ |
Denis Vlasenko | 6f1713f | 2008-02-25 23:23:58 +0000 | [diff] [blame] | 709 | G.hostname = safe_gethostname(); |
| 710 | *strchrnul(G.hostname, '.') = '\0'; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 711 | |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 712 | if (!(opts & OPT_nofork)) { |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 713 | bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 714 | } |
Denys Vlasenko | a425242 | 2010-01-29 16:44:48 +0100 | [diff] [blame] | 715 | //umask(0); - why?? |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 716 | write_pidfile("/var/run/syslogd.pid"); |
Denis Vlasenko | ceab870 | 2007-01-04 17:57:54 +0000 | [diff] [blame] | 717 | do_syslogd(); |
Denis Vlasenko | 8a820b2 | 2007-01-06 22:08:53 +0000 | [diff] [blame] | 718 | /* return EXIT_SUCCESS; */ |
Eric Andersen | 3843e96 | 1999-11-25 07:30:46 +0000 | [diff] [blame] | 719 | } |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 720 | |
| 721 | /* Clean up. Needed because we are included from syslogd_and_logger.c */ |
Denis Vlasenko | 0d94820 | 2008-12-09 22:53:31 +0000 | [diff] [blame] | 722 | #undef DEBUG |
| 723 | #undef SYSLOGD_MARK |
| 724 | #undef SYSLOGD_WRLOCK |
Denis Vlasenko | bd1aeeb | 2008-06-11 15:43:19 +0000 | [diff] [blame] | 725 | #undef G |
| 726 | #undef GLOBALS |
| 727 | #undef INIT_G |
| 728 | #undef OPTION_STR |
| 729 | #undef OPTION_DECL |
| 730 | #undef OPTION_PARAM |