--- trunk/mkinitrd-magellan/busybox/sysklogd/syslogd.c 2010/04/29 20:38:48 983 +++ trunk/mkinitrd-magellan/busybox/sysklogd/syslogd.c 2010/05/30 11:32:42 984 @@ -21,7 +21,6 @@ #include */ -#include #include #include @@ -43,6 +42,9 @@ * (semaphores are down but do_mark routine tries to down them again) */ #undef SYSLOGD_MARK +/* Write locking does not seem to be useful either */ +#undef SYSLOGD_WRLOCK + enum { MAX_READ = 256, DNS_WAIT_SEC = 2 * 60, @@ -63,7 +65,7 @@ /*int markInterval;*/ \ /* level of messages to be logged */ \ int logLevel; \ -USE_FEATURE_ROTATE_LOGFILE( \ +IF_FEATURE_ROTATE_LOGFILE( \ /* max size of file before rotation */ \ unsigned logFileSize; \ /* number of rotated message files */ \ @@ -71,12 +73,12 @@ unsigned curFileSize; \ smallint isRegular; \ ) \ -USE_FEATURE_REMOTE_LOG( \ +IF_FEATURE_REMOTE_LOG( \ /* udp socket for remote logging */ \ int remoteFD; \ len_and_sockaddr* remoteAddr; \ ) \ -USE_FEATURE_IPC_SYSLOG( \ +IF_FEATURE_IPC_SYSLOG( \ int shmid; /* ipc shared memory id */ \ int s_semid; /* ipc semaphore id */ \ int shm_size; \ @@ -150,41 +152,41 @@ OPTBIT_outfile, // -O OPTBIT_loglevel, // -l OPTBIT_small, // -S - USE_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s - USE_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b - USE_FEATURE_REMOTE_LOG( OPTBIT_remote ,) // -R - USE_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L - USE_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C - USE_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D + IF_FEATURE_ROTATE_LOGFILE(OPTBIT_filesize ,) // -s + IF_FEATURE_ROTATE_LOGFILE(OPTBIT_rotatecnt ,) // -b + IF_FEATURE_REMOTE_LOG( OPTBIT_remotelog ,) // -R + IF_FEATURE_REMOTE_LOG( OPTBIT_locallog ,) // -L + IF_FEATURE_IPC_SYSLOG( OPTBIT_circularlog,) // -C + IF_FEATURE_SYSLOGD_DUP( OPTBIT_dup ,) // -D OPT_mark = 1 << OPTBIT_mark , OPT_nofork = 1 << OPTBIT_nofork , OPT_outfile = 1 << OPTBIT_outfile , OPT_loglevel = 1 << OPTBIT_loglevel, OPT_small = 1 << OPTBIT_small , - OPT_filesize = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, - OPT_rotatecnt = USE_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, - OPT_remotelog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_remote )) + 0, - OPT_locallog = USE_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0, - OPT_circularlog = USE_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, - OPT_dup = USE_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0, + OPT_filesize = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_filesize )) + 0, + OPT_rotatecnt = IF_FEATURE_ROTATE_LOGFILE((1 << OPTBIT_rotatecnt )) + 0, + OPT_remotelog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_remotelog )) + 0, + OPT_locallog = IF_FEATURE_REMOTE_LOG( (1 << OPTBIT_locallog )) + 0, + OPT_circularlog = IF_FEATURE_IPC_SYSLOG( (1 << OPTBIT_circularlog)) + 0, + OPT_dup = IF_FEATURE_SYSLOGD_DUP( (1 << OPTBIT_dup )) + 0, }; #define OPTION_STR "m:nO:l:S" \ - USE_FEATURE_ROTATE_LOGFILE("s:" ) \ - USE_FEATURE_ROTATE_LOGFILE("b:" ) \ - USE_FEATURE_REMOTE_LOG( "R:" ) \ - USE_FEATURE_REMOTE_LOG( "L" ) \ - USE_FEATURE_IPC_SYSLOG( "C::") \ - USE_FEATURE_SYSLOGD_DUP( "D" ) + IF_FEATURE_ROTATE_LOGFILE("s:" ) \ + IF_FEATURE_ROTATE_LOGFILE("b:" ) \ + IF_FEATURE_REMOTE_LOG( "R:" ) \ + IF_FEATURE_REMOTE_LOG( "L" ) \ + IF_FEATURE_IPC_SYSLOG( "C::") \ + IF_FEATURE_SYSLOGD_DUP( "D" ) #define OPTION_DECL *opt_m, *opt_l \ - USE_FEATURE_ROTATE_LOGFILE(,*opt_s) \ - USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ - USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) + IF_FEATURE_ROTATE_LOGFILE(,*opt_s) \ + IF_FEATURE_ROTATE_LOGFILE(,*opt_b) \ + IF_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \ - USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ - USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ - USE_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \ - USE_FEATURE_IPC_SYSLOG( ,&opt_C) + IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \ + IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \ + IF_FEATURE_REMOTE_LOG( ,&G.remoteAddrStr) \ + IF_FEATURE_IPC_SYSLOG( ,&opt_C) /* circular buffer variables/structures */ @@ -291,7 +293,9 @@ /* Print a message to the log file. */ static void log_locally(time_t now, char *msg) { +#ifdef SYSLOGD_WRLOCK struct flock fl; +#endif int len = strlen(msg); #if ENABLE_FEATURE_IPC_SYSLOG @@ -338,11 +342,13 @@ #endif } +#ifdef SYSLOGD_WRLOCK fl.l_whence = SEEK_SET; fl.l_start = 0; fl.l_len = 1; fl.l_type = F_WRLCK; fcntl(G.logFD, F_SETLKW, &fl); +#endif #if ENABLE_FEATURE_ROTATE_LOGFILE if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) { @@ -361,8 +367,10 @@ } /* newFile == "f.0" now */ rename(G.logFilePath, newFile); +#ifdef SYSLOGD_WRLOCK fl.l_type = F_UNLCK; fcntl(G.logFD, F_SETLKW, &fl); +#endif close(G.logFD); goto reopen; } @@ -371,8 +379,10 @@ G.curFileSize += #endif full_write(G.logFD, msg, len); +#ifdef SYSLOGD_WRLOCK fl.l_type = F_UNLCK; fcntl(G.logFD, F_SETLKW, &fl); +#endif } static void parse_fac_prio_20(int pri, char *res20) @@ -412,6 +422,8 @@ char *timestamp; time_t now; + /* Jan 18 00:11:22 msg... */ + /* 01234567890123456 */ if (len < 16 || msg[3] != ' ' || msg[6] != ' ' || msg[9] != ':' || msg[12] != ':' || msg[15] != ' ' ) { @@ -438,6 +450,7 @@ static void timestamp_and_log_internal(const char *msg) { + /* -L, or no -R */ if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_locallog)) return; timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)msg, 0); @@ -482,15 +495,6 @@ } } -static void quit_signal(int sig) -{ - timestamp_and_log_internal("syslogd exiting"); - puts("syslogd exiting"); - if (ENABLE_FEATURE_IPC_SYSLOG) - ipcsyslog_cleanup(); - kill_myself_with_sig(sig); -} - #ifdef SYSLOGD_MARK static void do_mark(int sig) { @@ -559,14 +563,11 @@ #define recvbuf (G.recvbuf) #endif - /* Set up signal handlers */ - bb_signals(0 - + (1 << SIGINT) - + (1 << SIGTERM) - + (1 << SIGQUIT) - , quit_signal); + /* Set up signal handlers (so that they interrupt read()) */ + signal_no_SA_RESTART_empty_mask(SIGTERM, record_signo); + signal_no_SA_RESTART_empty_mask(SIGINT, record_signo); + //signal_no_SA_RESTART_empty_mask(SIGQUIT, record_signo); signal(SIGHUP, SIG_IGN); - /* signal(SIGCHLD, SIG_IGN); - why? */ #ifdef SYSLOGD_MARK signal(SIGALRM, do_mark); alarm(G.markInterval); @@ -579,7 +580,7 @@ timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER); - for (;;) { + while (!bb_got_signal) { ssize_t sz; #if ENABLE_FEATURE_SYSLOGD_DUP @@ -590,9 +591,12 @@ recvbuf = G.recvbuf; #endif read_again: - sz = safe_read(sock_fd, recvbuf, MAX_READ - 1); - if (sz < 0) - bb_perror_msg_and_die("read from /dev/log"); + sz = read(sock_fd, recvbuf, MAX_READ - 1); + if (sz < 0) { + if (!bb_got_signal) + bb_perror_msg("read from /dev/log"); + break; + } /* Drop trailing '\n' and NULs (typically there is one NUL) */ while (1) { @@ -639,7 +643,13 @@ recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */ split_escape_and_log(recvbuf, sz); } - } /* for (;;) */ + } /* while (!bb_got_signal) */ + + timestamp_and_log_internal("syslogd exiting"); + puts("syslogd exiting"); + if (ENABLE_FEATURE_IPC_SYSLOG) + ipcsyslog_cleanup(); + kill_myself_with_sig(bb_got_signal); #undef recvbuf } @@ -647,6 +657,7 @@ int syslogd_main(int argc UNUSED_PARAM, char **argv) { char OPTION_DECL; + int opts; INIT_G(); #if ENABLE_FEATURE_REMOTE_LOG @@ -655,20 +666,20 @@ /* do normal option parsing */ opt_complementary = "=0"; /* no non-option params */ - getopt32(argv, OPTION_STR, OPTION_PARAM); + opts = getopt32(argv, OPTION_STR, OPTION_PARAM); #ifdef SYSLOGD_MARK - if (option_mask32 & OPT_mark) // -m + if (opts & OPT_mark) // -m G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; #endif - //if (option_mask32 & OPT_nofork) // -n - //if (option_mask32 & OPT_outfile) // -O - if (option_mask32 & OPT_loglevel) // -l + //if (opts & OPT_nofork) // -n + //if (opts & OPT_outfile) // -O + if (opts & OPT_loglevel) // -l G.logLevel = xatou_range(opt_l, 1, 8); - //if (option_mask32 & OPT_small) // -S + //if (opts & OPT_small) // -S #if ENABLE_FEATURE_ROTATE_LOGFILE - if (option_mask32 & OPT_filesize) // -s + if (opts & OPT_filesize) // -s G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024; - if (option_mask32 & OPT_rotatecnt) // -b + if (opts & OPT_rotatecnt) // -b G.logFileRotate = xatou_range(opt_b, 0, 99); #endif #if ENABLE_FEATURE_IPC_SYSLOG @@ -677,23 +688,26 @@ #endif /* If they have not specified remote logging, then log locally */ - if (ENABLE_FEATURE_REMOTE_LOG && !(option_mask32 & OPT_remotelog)) + if (ENABLE_FEATURE_REMOTE_LOG && !(opts & OPT_remotelog)) // -R option_mask32 |= OPT_locallog; /* Store away localhost's name before the fork */ G.hostname = safe_gethostname(); *strchrnul(G.hostname, '.') = '\0'; - if (!(option_mask32 & OPT_nofork)) { + if (!(opts & OPT_nofork)) { bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv); } - umask(0); + //umask(0); - why?? write_pidfile("/var/run/syslogd.pid"); do_syslogd(); /* return EXIT_SUCCESS; */ } /* Clean up. Needed because we are included from syslogd_and_logger.c */ +#undef DEBUG +#undef SYSLOGD_MARK +#undef SYSLOGD_WRLOCK #undef G #undef GLOBALS #undef INIT_G