diff options
| author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-30 19:20:00 +0000 |
|---|---|---|
| committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-09-30 19:20:00 +0000 |
| commit | 14c1940255a190143ef003a2a73bcba29e4c3d56 (patch) | |
| tree | c53646ec260564f546562c980d912f78618544f7 | |
| parent | 1decd0e5292181284e403cd9ecf7abf39a5d7363 (diff) | |
| download | busybox-w32-14c1940255a190143ef003a2a73bcba29e4c3d56.tar.gz busybox-w32-14c1940255a190143ef003a2a73bcba29e4c3d56.tar.bz2 busybox-w32-14c1940255a190143ef003a2a73bcba29e4c3d56.zip | |
syslogd: getopt_ulflags'ification (save ~50 bytes)
| -rw-r--r-- | sysklogd/syslogd.c | 299 |
1 files changed, 156 insertions, 143 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index a257e740e..a28dde6b3 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
| @@ -23,13 +23,11 @@ | |||
| 23 | #include <sys/syslog.h> | 23 | #include <sys/syslog.h> |
| 24 | #include <sys/uio.h> | 24 | #include <sys/uio.h> |
| 25 | 25 | ||
| 26 | /* Path for the file where all log messages are written */ | ||
| 27 | #define __LOG_FILE "/var/log/messages" | ||
| 28 | |||
| 29 | /* Path to the unix socket */ | 26 | /* Path to the unix socket */ |
| 30 | static char lfile[MAXPATHLEN]; | 27 | static char lfile[MAXPATHLEN]; |
| 31 | 28 | ||
| 32 | static const char *logFilePath = __LOG_FILE; | 29 | /* Path for the file where all log messages are written */ |
| 30 | static const char *logFilePath = "/var/log/messages"; | ||
| 33 | 31 | ||
| 34 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE | 32 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE |
| 35 | /* max size of message file before being rotated */ | 33 | /* max size of message file before being rotated */ |
| @@ -63,15 +61,50 @@ static int RemotePort = 514; | |||
| 63 | #endif | 61 | #endif |
| 64 | 62 | ||
| 65 | /* options */ | 63 | /* options */ |
| 66 | static unsigned opts; | 64 | static unsigned option_mask; |
| 67 | #define SYSLOG_OPT_small (1) | 65 | /* Correct regardless of combination of CONFIG_xxx */ |
| 68 | #define SYSLOG_OPT_remotelog (2) | 66 | enum { |
| 69 | #define SYSLOG_OPT_locallog (4) | 67 | OPTBIT_mark = 0, // -m |
| 70 | #define SYSLOG_OPT_circularlog (8) | 68 | OPTBIT_nofork, // -n |
| 69 | OPTBIT_outfile, // -O | ||
| 70 | OPTBIT_loglevel, // -l | ||
| 71 | OPTBIT_small, // -S | ||
| 72 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s | ||
| 73 | USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b | ||
| 74 | USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R | ||
| 75 | USE_FEATURE_REMOTE_LOG( OPTBIT_localtoo ,) // -L | ||
| 76 | USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C | ||
| 77 | |||
| 78 | OPT_mark = 1 << OPTBIT_mark , | ||
| 79 | OPT_nofork = 1 << OPTBIT_nofork , | ||
| 80 | OPT_outfile = 1 << OPTBIT_outfile , | ||
| 81 | OPT_loglevel = 1 << OPTBIT_loglevel, | ||
| 82 | OPT_small = 1 << OPTBIT_small , | ||
| 83 | OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, | ||
| 84 | OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, | ||
| 85 | OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0, | ||
| 86 | OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_localtoo )) + 0, | ||
| 87 | OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, | ||
| 88 | }; | ||
| 89 | #define OPTION_STR "m:nO:l:S" \ | ||
| 90 | USE_FEATURE_ROTATE_LOGFILE("s:" ) \ | ||
| 91 | USE_FEATURE_ROTATE_LOGFILE("b:" ) \ | ||
| 92 | USE_FEATURE_REMOTE_LOG( "R:" ) \ | ||
| 93 | USE_FEATURE_REMOTE_LOG( "L" ) \ | ||
| 94 | USE_FEATURE_IPC_SYSLOG( "C::") | ||
| 95 | #define OPTION_DECL *opt_m, *opt_l \ | ||
| 96 | USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \ | ||
| 97 | USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ | ||
| 98 | USE_FEATURE_REMOTE_LOG( ,*opt_R) \ | ||
| 99 | USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) | ||
| 100 | #define OPTION_PARAM &opt_m, &logFilePath, &opt_l \ | ||
| 101 | USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ | ||
| 102 | USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ | ||
| 103 | USE_FEATURE_REMOTE_LOG( ,&opt_R) \ | ||
| 104 | USE_FEATURE_IPC_SYSLOG( ,&opt_C) | ||
| 71 | 105 | ||
| 72 | #define MAXLINE 1024 /* maximum line length */ | 106 | #define MAXLINE 1024 /* maximum line length */ |
| 73 | 107 | ||
| 74 | |||
| 75 | /* circular buffer variables/structures */ | 108 | /* circular buffer variables/structures */ |
| 76 | #ifdef CONFIG_FEATURE_IPC_SYSLOG | 109 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
| 77 | 110 | ||
| @@ -85,28 +118,28 @@ static unsigned opts; | |||
| 85 | #include <sys/shm.h> | 118 | #include <sys/shm.h> |
| 86 | 119 | ||
| 87 | /* our shared key */ | 120 | /* our shared key */ |
| 88 | static const long KEY_ID = 0x414e4547; /*"GENA" */ | 121 | #define KEY_ID ((long)0x414e4547) /* "GENA" */ |
| 89 | 122 | ||
| 90 | // Semaphore operation structures | 123 | // Semaphore operation structures |
| 91 | static struct shbuf_ds { | 124 | static struct shbuf_ds { |
| 92 | int size; // size of data written | 125 | int size; // size of data written |
| 93 | int head; // start of message list | 126 | int head; // start of message list |
| 94 | int tail; // end of message list | 127 | int tail; // end of message list |
| 95 | char data[1]; // data/messages | 128 | char data[1]; // data/messages |
| 96 | } *buf = NULL; // shared memory pointer | 129 | } *shbuf = NULL; // shared memory pointer |
| 97 | 130 | ||
| 98 | static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; // set SMwup | 131 | static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; // set SMwup |
| 99 | static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; // set SMwdn | 132 | static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; // set SMwdn |
| 100 | 133 | ||
| 101 | static int shmid = -1; // ipc shared memory id | 134 | static int shmid = -1; // ipc shared memory id |
| 102 | static int s_semid = -1; // ipc semaphore id | 135 | static int s_semid = -1; // ipc semaphore id |
| 103 | static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size | 136 | static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size |
| 104 | 137 | ||
| 105 | static void ipcsyslog_cleanup(void) | 138 | static void ipcsyslog_cleanup(void) |
| 106 | { | 139 | { |
| 107 | printf("Exiting Syslogd!\n"); | 140 | puts("Exiting syslogd!"); |
| 108 | if (shmid != -1) { | 141 | if (shmid != -1) { |
| 109 | shmdt(buf); | 142 | shmdt(shbuf); |
| 110 | } | 143 | } |
| 111 | 144 | ||
| 112 | if (shmid != -1) { | 145 | if (shmid != -1) { |
| @@ -119,22 +152,26 @@ static void ipcsyslog_cleanup(void) | |||
| 119 | 152 | ||
| 120 | static void ipcsyslog_init(void) | 153 | static void ipcsyslog_init(void) |
| 121 | { | 154 | { |
| 122 | if (buf == NULL) { | 155 | if (shbuf == NULL) { |
| 123 | if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) { | 156 | shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023); |
| 157 | if (shmid == -1) { | ||
| 124 | bb_perror_msg_and_die("shmget"); | 158 | bb_perror_msg_and_die("shmget"); |
| 125 | } | 159 | } |
| 126 | 160 | ||
| 127 | if ((buf = shmat(shmid, NULL, 0)) == NULL) { | 161 | shbuf = shmat(shmid, NULL, 0); |
| 162 | if (!shbuf) { | ||
| 128 | bb_perror_msg_and_die("shmat"); | 163 | bb_perror_msg_and_die("shmat"); |
| 129 | } | 164 | } |
| 130 | 165 | ||
| 131 | buf->size = shm_size - sizeof(*buf); | 166 | shbuf->size = shm_size - sizeof(*shbuf); |
| 132 | buf->head = buf->tail = 0; | 167 | shbuf->head = shbuf->tail = 0; |
| 133 | 168 | ||
| 134 | // we'll trust the OS to set initial semval to 0 (let's hope) | 169 | // we'll trust the OS to set initial semval to 0 (let's hope) |
| 135 | if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) { | 170 | s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023); |
| 171 | if (s_semid == -1) { | ||
| 136 | if (errno == EEXIST) { | 172 | if (errno == EEXIST) { |
| 137 | if ((s_semid = semget(KEY_ID, 2, 0)) == -1) { | 173 | s_semid = semget(KEY_ID, 2, 0); |
| 174 | if (s_semid == -1) { | ||
| 138 | bb_perror_msg_and_die("semget"); | 175 | bb_perror_msg_and_die("semget"); |
| 139 | } | 176 | } |
| 140 | } else { | 177 | } else { |
| @@ -179,69 +216,66 @@ static void circ_message(const char *msg) | |||
| 179 | * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide | 216 | * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide |
| 180 | * a threadsafe way of handling shared memory operations. | 217 | * a threadsafe way of handling shared memory operations. |
| 181 | */ | 218 | */ |
| 182 | if ((buf->tail + l) < buf->size) { | 219 | if ((shbuf->tail + l) < shbuf->size) { |
| 183 | /* before we append the message we need to check the HEAD so that we won't | 220 | /* before we append the message we need to check the HEAD so that we won't |
| 184 | overwrite any of the message that we still need and adjust HEAD to point | 221 | overwrite any of the message that we still need and adjust HEAD to point |
| 185 | to the next message! */ | 222 | to the next message! */ |
| 186 | if (buf->tail < buf->head) { | 223 | if (shbuf->tail < shbuf->head) { |
| 187 | if ((buf->tail + l) >= buf->head) { | 224 | if ((shbuf->tail + l) >= shbuf->head) { |
| 188 | /* we need to move the HEAD to point to the next message | 225 | /* we need to move the HEAD to point to the next message |
| 189 | * Theoretically we have enough room to add the whole message to the | 226 | * Theoretically we have enough room to add the whole message to the |
| 190 | * buffer, because of the first outer IF statement, so we don't have | 227 | * buffer, because of the first outer IF statement, so we don't have |
| 191 | * to worry about overflows here! | 228 | * to worry about overflows here! |
| 192 | */ | 229 | */ |
| 193 | int k = buf->tail + l - buf->head; /* we need to know how many bytes | 230 | /* we need to know how many bytes we are overwriting to make enough room */ |
| 194 | we are overwriting to make | 231 | int k = shbuf->tail + l - shbuf->head; |
| 195 | enough room */ | ||
| 196 | char *c = | 232 | char *c = |
| 197 | memchr(buf->data + buf->head + k, '\0', | 233 | memchr(shbuf->data + shbuf->head + k, '\0', |
| 198 | buf->size - (buf->head + k)); | 234 | shbuf->size - (shbuf->head + k)); |
| 199 | if (c != NULL) { /* do a sanity check just in case! */ | 235 | if (c != NULL) { /* do a sanity check just in case! */ |
| 200 | buf->head = c - buf->data + 1; /* we need to convert pointer to | 236 | /* we need to convert pointer to offset + skip the '\0' |
| 201 | offset + skip the '\0' since | 237 | since we need to point to the beginning of the next message */ |
| 202 | we need to point to the beginning | 238 | shbuf->head = c - shbuf->data + 1; |
| 203 | of the next message */ | ||
| 204 | /* Note: HEAD is only used to "retrieve" messages, it's not used | 239 | /* Note: HEAD is only used to "retrieve" messages, it's not used |
| 205 | when writing messages into our buffer */ | 240 | when writing messages into our buffer */ |
| 206 | } else { /* show an error message to know we messed up? */ | 241 | } else { /* show an error message to know we messed up? */ |
| 207 | printf(fail_msg,""); | 242 | printf(fail_msg,""); |
| 208 | buf->head = 0; | 243 | shbuf->head = 0; |
| 209 | } | 244 | } |
| 210 | } | 245 | } |
| 211 | } | 246 | } |
| 212 | 247 | ||
| 213 | /* in other cases no overflows have been done yet, so we don't care! */ | 248 | /* in other cases no overflows have been done yet, so we don't care! */ |
| 214 | /* we should be ok to append the message now */ | 249 | /* we should be ok to append the message now */ |
| 215 | strncpy(buf->data + buf->tail, msg, l); /* append our message */ | 250 | strncpy(shbuf->data + shbuf->tail, msg, l); /* append our message */ |
| 216 | buf->tail += l; /* count full message w/ '\0' terminating char */ | 251 | shbuf->tail += l; /* count full message w/ '\0' terminating char */ |
| 217 | } else { | 252 | } else { |
| 218 | /* we need to break up the message and "circle" it around */ | 253 | /* we need to break up the message and "circle" it around */ |
| 219 | char *c; | 254 | char *c; |
| 220 | int k = buf->tail + l - buf->size; /* count # of bytes we don't fit */ | 255 | int k = shbuf->tail + l - shbuf->size; /* count # of bytes we don't fit */ |
| 221 | 256 | ||
| 222 | /* We need to move HEAD! This is always the case since we are going | 257 | /* We need to move HEAD! This is always the case since we are going |
| 223 | * to "circle" the message. | 258 | * to "circle" the message. */ |
| 224 | */ | 259 | c = memchr(shbuf->data + k, '\0', shbuf->size - k); |
| 225 | c = memchr(buf->data + k, '\0', buf->size - k); | ||
| 226 | 260 | ||
| 227 | if (c != NULL) { /* if we don't have '\0'??? weird!!! */ | 261 | if (c != NULL) { /* if we don't have '\0'??? weird!!! */ |
| 228 | /* move head pointer */ | 262 | /* move head pointer */ |
| 229 | buf->head = c - buf->data + 1; | 263 | shbuf->head = c - shbuf->data + 1; |
| 230 | 264 | ||
| 231 | /* now write the first part of the message */ | 265 | /* now write the first part of the message */ |
| 232 | strncpy(buf->data + buf->tail, msg, l - k - 1); | 266 | strncpy(shbuf->data + shbuf->tail, msg, l - k - 1); |
| 233 | 267 | ||
| 234 | /* ALWAYS terminate end of buffer w/ '\0' */ | 268 | /* ALWAYS terminate end of buffer w/ '\0' */ |
| 235 | buf->data[buf->size - 1] = '\0'; | 269 | shbuf->data[shbuf->size - 1] = '\0'; |
| 236 | 270 | ||
| 237 | /* now write out the rest of the string to the beginning of the buffer */ | 271 | /* now write out the rest of the string to the beginning of the buffer */ |
| 238 | strcpy(buf->data, &msg[l - k - 1]); | 272 | strcpy(shbuf->data, &msg[l - k - 1]); |
| 239 | 273 | ||
| 240 | /* we need to place the TAIL at the end of the message */ | 274 | /* we need to place the TAIL at the end of the message */ |
| 241 | buf->tail = k + 1; | 275 | shbuf->tail = k + 1; |
| 242 | } else { | 276 | } else { |
| 243 | printf(fail_msg, " from the beginning"); | 277 | printf(fail_msg, " from the beginning"); |
| 244 | buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */ | 278 | shbuf->head = shbuf->tail = 0; /* reset buffer, since it's probably corrupted */ |
| 245 | } | 279 | } |
| 246 | 280 | ||
| 247 | } | 281 | } |
| @@ -270,7 +304,7 @@ static void message(char *fmt, ...) | |||
| 270 | fl.l_len = 1; | 304 | fl.l_len = 1; |
| 271 | 305 | ||
| 272 | #ifdef CONFIG_FEATURE_IPC_SYSLOG | 306 | #ifdef CONFIG_FEATURE_IPC_SYSLOG |
| 273 | if ((opts & SYSLOG_OPT_circularlog) && (buf != NULL)) { | 307 | if ((option_mask & OPT_circularlog) && shbuf) { |
| 274 | char b[1024]; | 308 | char b[1024]; |
| 275 | 309 | ||
| 276 | va_start(arguments, fmt); | 310 | va_start(arguments, fmt); |
| @@ -290,29 +324,29 @@ static void message(char *fmt, ...) | |||
| 290 | if (ENABLE_FEATURE_ROTATE_LOGFILE && logFileSize > 0 ) { | 324 | if (ENABLE_FEATURE_ROTATE_LOGFILE && logFileSize > 0 ) { |
| 291 | struct stat statf; | 325 | struct stat statf; |
| 292 | int r = fstat(fd, &statf); | 326 | int r = fstat(fd, &statf); |
| 293 | if( !r && (statf.st_mode & S_IFREG) | 327 | if (!r && (statf.st_mode & S_IFREG) |
| 294 | && (lseek(fd,0,SEEK_END) > logFileSize) ) { | 328 | && (lseek(fd,0,SEEK_END) > logFileSize)) { |
| 295 | if(logFileRotate > 0) { | 329 | if (logFileRotate > 0) { |
| 296 | int i; | 330 | int i = strlen(logFilePath) + 4; |
| 297 | char oldFile[(strlen(logFilePath)+4)]; | 331 | char oldFile[i]; |
| 298 | char newFile[(strlen(logFilePath)+4)]; | 332 | char newFile[i]; |
| 299 | for(i=logFileRotate-1;i>0;i--) { | 333 | for (i=logFileRotate-1; i>0; i--) { |
| 300 | sprintf(oldFile, "%s.%d", logFilePath, i-1); | 334 | sprintf(oldFile, "%s.%d", logFilePath, i-1); |
| 301 | sprintf(newFile, "%s.%d", logFilePath, i); | 335 | sprintf(newFile, "%s.%d", logFilePath, i); |
| 302 | rename(oldFile, newFile); | 336 | rename(oldFile, newFile); |
| 303 | } | 337 | } |
| 304 | sprintf(newFile, "%s.%d", logFilePath, 0); | 338 | sprintf(newFile, "%s.%d", logFilePath, 0); |
| 305 | fl.l_type = F_UNLCK; | 339 | fl.l_type = F_UNLCK; |
| 306 | fcntl (fd, F_SETLKW, &fl); | 340 | fcntl(fd, F_SETLKW, &fl); |
| 307 | close(fd); | 341 | close(fd); |
| 308 | rename(logFilePath, newFile); | 342 | rename(logFilePath, newFile); |
| 309 | fd = device_open (logFilePath, | 343 | fd = device_open(logFilePath, |
| 310 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | | 344 | O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | |
| 311 | O_NONBLOCK); | 345 | O_NONBLOCK); |
| 312 | fl.l_type = F_WRLCK; | 346 | fl.l_type = F_WRLCK; |
| 313 | fcntl (fd, F_SETLKW, &fl); | 347 | fcntl(fd, F_SETLKW, &fl); |
| 314 | } else { | 348 | } else { |
| 315 | ftruncate( fd, 0 ); | 349 | ftruncate(fd, 0); |
| 316 | } | 350 | } |
| 317 | } | 351 | } |
| 318 | } | 352 | } |
| @@ -362,10 +396,12 @@ static void logMessage(int pri, char *msg) | |||
| 362 | CODE *c_pri, *c_fac; | 396 | CODE *c_pri, *c_fac; |
| 363 | 397 | ||
| 364 | if (pri != 0) { | 398 | if (pri != 0) { |
| 365 | for (c_fac = facilitynames; | 399 | c_fac = facilitynames; |
| 366 | c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); | 400 | while (c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3)) |
| 367 | for (c_pri = prioritynames; | 401 | c_fac++; |
| 368 | c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); | 402 | c_pri = prioritynames; |
| 403 | while (c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri))) | ||
| 404 | c_pri++; | ||
| 369 | if (c_fac->c_name == NULL || c_pri->c_name == NULL) { | 405 | if (c_fac->c_name == NULL || c_pri->c_name == NULL) { |
| 370 | snprintf(res, sizeof(res), "<%d>", pri); | 406 | snprintf(res, sizeof(res), "<%d>", pri); |
| 371 | } else { | 407 | } else { |
| @@ -387,7 +423,7 @@ static void logMessage(int pri, char *msg) | |||
| 387 | /* todo: supress duplicates */ | 423 | /* todo: supress duplicates */ |
| 388 | 424 | ||
| 389 | #ifdef CONFIG_FEATURE_REMOTE_LOG | 425 | #ifdef CONFIG_FEATURE_REMOTE_LOG |
| 390 | if (opts & SYSLOG_OPT_remotelog) { | 426 | if (option_mask & OPT_remotelog) { |
| 391 | char line[MAXLINE + 1]; | 427 | char line[MAXLINE + 1]; |
| 392 | /* trying connect the socket */ | 428 | /* trying connect the socket */ |
| 393 | if (-1 == remotefd) { | 429 | if (-1 == remotefd) { |
| @@ -401,7 +437,7 @@ static void logMessage(int pri, char *msg) | |||
| 401 | 437 | ||
| 402 | retry: | 438 | retry: |
| 403 | /* send message to remote logger */ | 439 | /* send message to remote logger */ |
| 404 | if(( -1 == sendto(remotefd, line, strlen(line), 0, | 440 | if ((-1 == sendto(remotefd, line, strlen(line), 0, |
| 405 | (struct sockaddr *) &remoteaddr, | 441 | (struct sockaddr *) &remoteaddr, |
| 406 | sizeof(remoteaddr))) && (errno == EINTR)) { | 442 | sizeof(remoteaddr))) && (errno == EINTR)) { |
| 407 | /* sleep now seconds and retry (with now * 2) */ | 443 | /* sleep now seconds and retry (with now * 2) */ |
| @@ -412,12 +448,12 @@ retry: | |||
| 412 | } | 448 | } |
| 413 | } | 449 | } |
| 414 | 450 | ||
| 415 | if (opts & SYSLOG_OPT_locallog) | 451 | if (option_mask & OPT_locallog) |
| 416 | #endif | 452 | #endif |
| 417 | { | 453 | { |
| 418 | /* now spew out the message to wherever it is supposed to go */ | 454 | /* now spew out the message to wherever it is supposed to go */ |
| 419 | if (pri == 0 || LOG_PRI(pri) < logLevel) { | 455 | if (pri == 0 || LOG_PRI(pri) < logLevel) { |
| 420 | if (opts & SYSLOG_OPT_small) | 456 | if (option_mask & OPT_small) |
| 421 | message("%s %s\n", timestamp, msg); | 457 | message("%s %s\n", timestamp, msg); |
| 422 | else | 458 | else |
| 423 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); | 459 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
| @@ -518,24 +554,23 @@ static void doSyslogd(void) | |||
| 518 | sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0); | 554 | sock_fd = xsocket(AF_UNIX, SOCK_DGRAM, 0); |
| 519 | addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); | 555 | addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path); |
| 520 | if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { | 556 | if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) { |
| 521 | bb_perror_msg_and_die("Could not connect to socket " _PATH_LOG); | 557 | bb_perror_msg_and_die("cannot connect to socket %s", lfile); |
| 522 | } | 558 | } |
| 523 | 559 | ||
| 524 | if (chmod(lfile, 0666) < 0) { | 560 | if (chmod(lfile, 0666) < 0) { |
| 525 | bb_perror_msg_and_die("Could not set permission on " _PATH_LOG); | 561 | bb_perror_msg_and_die("cannot set permission on %s", lfile); |
| 526 | } | 562 | } |
| 527 | if (ENABLE_FEATURE_IPC_SYSLOG && opts & SYSLOG_OPT_circularlog) { | 563 | if (ENABLE_FEATURE_IPC_SYSLOG && (option_mask & OPT_circularlog)) { |
| 528 | ipcsyslog_init(); | 564 | ipcsyslog_init(); |
| 529 | } | 565 | } |
| 530 | 566 | ||
| 531 | if (ENABLE_FEATURE_REMOTE_LOG && opts & SYSLOG_OPT_remotelog) { | 567 | if (ENABLE_FEATURE_REMOTE_LOG && (option_mask & OPT_remotelog)) { |
| 532 | init_RemoteLog(); | 568 | init_RemoteLog(); |
| 533 | } | 569 | } |
| 534 | 570 | ||
| 535 | logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " "BusyBox v" BB_VER ); | 571 | logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " "BusyBox v" BB_VER ); |
| 536 | 572 | ||
| 537 | for (;;) { | 573 | for (;;) { |
| 538 | |||
| 539 | FD_ZERO(&fds); | 574 | FD_ZERO(&fds); |
| 540 | FD_SET(sock_fd, &fds); | 575 | FD_SET(sock_fd, &fds); |
| 541 | 576 | ||
| @@ -544,7 +579,7 @@ static void doSyslogd(void) | |||
| 544 | /* alarm may have happened. */ | 579 | /* alarm may have happened. */ |
| 545 | continue; | 580 | continue; |
| 546 | } | 581 | } |
| 547 | bb_perror_msg_and_die("select error"); | 582 | bb_perror_msg_and_die("select"); |
| 548 | } | 583 | } |
| 549 | 584 | ||
| 550 | if (FD_ISSET(sock_fd, &fds)) { | 585 | if (FD_ISSET(sock_fd, &fds)) { |
| @@ -566,88 +601,66 @@ static void doSyslogd(void) | |||
| 566 | } /* for main loop */ | 601 | } /* for main loop */ |
| 567 | } | 602 | } |
| 568 | 603 | ||
| 604 | |||
| 569 | int syslogd_main(int argc, char **argv) | 605 | int syslogd_main(int argc, char **argv) |
| 570 | { | 606 | { |
| 571 | int opt; | 607 | char OPTION_DECL; |
| 572 | |||
| 573 | int doFork = TRUE; | ||
| 574 | |||
| 575 | char *p; | 608 | char *p; |
| 576 | 609 | ||
| 577 | /* do normal option parsing */ | 610 | /* do normal option parsing */ |
| 578 | while ((opt = getopt(argc, argv, "m:nO:s:Sb:R:LC::")) > 0) { | 611 | option_mask = bb_getopt_ulflags(argc, argv, OPTION_STR, OPTION_PARAM); |
| 579 | switch (opt) { | 612 | if (option_mask & OPT_mark) MarkInterval = atoi(opt_m) * 60; // -m |
| 580 | case 'm': | 613 | //if (option_mask & OPT_nofork) // -n |
| 581 | MarkInterval = atoi(optarg) * 60; | 614 | //if (option_mask & OPT_outfile) // -O |
| 582 | break; | 615 | if (option_mask & OPT_loglevel) { // -l |
| 583 | case 'n': | 616 | logLevel = atoi(opt_l); |
| 584 | doFork = FALSE; | 617 | /* Valid levels are between 1 and 8 */ |
| 585 | break; | 618 | if (logLevel < 1 || logLevel > 8) |
| 586 | case 'O': | 619 | bb_show_usage(); |
| 587 | logFilePath = optarg; | 620 | } |
| 588 | break; | 621 | //if (option_mask & OPT_small) // -S |
| 589 | case 'l': | 622 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
| 590 | logLevel = atoi(optarg); | 623 | if (option_mask & OPT_filesize) logFileSize = atoi(opt_s) * 1024; // -s |
| 591 | /* Valid levels are between 1 and 8 */ | 624 | if (option_mask & OPT_rotatecnt) { // -b |
| 592 | if (logLevel < 1 || logLevel > 8) { | 625 | logFileRotate = atoi(opt_b); |
| 593 | bb_show_usage(); | 626 | if (logFileRotate > 99) logFileRotate = 99; |
| 594 | } | 627 | } |
| 595 | break; | ||
| 596 | #ifdef CONFIG_FEATURE_ROTATE_LOGFILE | ||
| 597 | case 's': | ||
| 598 | logFileSize = atoi(optarg) * 1024; | ||
| 599 | break; | ||
| 600 | case 'b': | ||
| 601 | logFileRotate = atoi(optarg); | ||
| 602 | if( logFileRotate > 99 ) logFileRotate = 99; | ||
| 603 | break; | ||
| 604 | #endif | ||
| 605 | #ifdef CONFIG_FEATURE_REMOTE_LOG | ||
| 606 | case 'R': | ||
| 607 | RemoteHost = xstrdup(optarg); | ||
| 608 | if ((p = strchr(RemoteHost, ':'))) { | ||
| 609 | RemotePort = atoi(p + 1); | ||
| 610 | *p = '\0'; | ||
| 611 | } | ||
| 612 | opts |= SYSLOG_OPT_remotelog; | ||
| 613 | break; | ||
| 614 | case 'L': | ||
| 615 | opts |= SYSLOG_OPT_locallog; | ||
| 616 | break; | ||
| 617 | #endif | 628 | #endif |
| 618 | #ifdef CONFIG_FEATURE_IPC_SYSLOG | 629 | #if ENABLE_FEATURE_REMOTE_LOG |
| 619 | case 'C': | 630 | if (option_mask & OPT_remotelog) { // -R |
| 620 | if (optarg) { | 631 | RemoteHost = xstrdup(opt_R); |
| 621 | int buf_size = atoi(optarg); | 632 | p = strchr(RemoteHost, ':'); |
| 622 | if (buf_size >= 4) { | 633 | if (p) { |
| 623 | shm_size = buf_size * 1024; | 634 | RemotePort = atoi(p + 1); |
| 624 | } | 635 | *p = '\0'; |
| 625 | } | 636 | } |
| 626 | opts |= SYSLOG_OPT_circularlog; | 637 | } |
| 627 | break; | 638 | //if (option_mask & OPT_locallog) // -L |
| 628 | #endif | 639 | #endif |
| 629 | case 'S': | 640 | #if ENABLE_FEATURE_IPC_SYSLOG |
| 630 | opts |= SYSLOG_OPT_small; | 641 | if (option_mask & OPT_circularlog) { // -C |
| 631 | break; | 642 | if (opt_C) { |
| 632 | default: | 643 | int buf_size = atoi(opt_C); |
| 633 | bb_show_usage(); | 644 | if (buf_size >= 4) |
| 645 | shm_size = buf_size * 1024; | ||
| 634 | } | 646 | } |
| 635 | } | 647 | } |
| 648 | #endif | ||
| 636 | 649 | ||
| 637 | /* If they have not specified remote logging, then log locally */ | 650 | /* If they have not specified remote logging, then log locally */ |
| 638 | if (ENABLE_FEATURE_REMOTE_LOG && !(opts & SYSLOG_OPT_remotelog)) | 651 | if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask & OPT_remotelog)) |
| 639 | opts |= SYSLOG_OPT_locallog; | 652 | option_mask |= OPT_locallog; |
| 640 | |||
| 641 | 653 | ||
| 642 | /* Store away localhost's name before the fork */ | 654 | /* Store away localhost's name before the fork */ |
| 643 | gethostname(LocalHostName, sizeof(LocalHostName)); | 655 | gethostname(LocalHostName, sizeof(LocalHostName)); |
| 644 | if ((p = strchr(LocalHostName, '.'))) { | 656 | p = strchr(LocalHostName, '.'); |
| 657 | if (p) { | ||
| 645 | *p = '\0'; | 658 | *p = '\0'; |
| 646 | } | 659 | } |
| 647 | 660 | ||
| 648 | umask(0); | 661 | umask(0); |
| 649 | 662 | ||
| 650 | if (doFork == TRUE) { | 663 | if (!(option_mask & OPT_nofork)) { |
| 651 | #ifdef BB_NOMMU | 664 | #ifdef BB_NOMMU |
| 652 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); | 665 | vfork_daemon_rexec(0, 1, argc, argv, "-n"); |
| 653 | #else | 666 | #else |
