svn commit: trunk/busybox/sysklogd

vda at busybox.net vda at busybox.net
Tue Dec 9 22:53:31 UTC 2008


Author: vda
Date: 2008-12-09 14:53:31 -0800 (Tue, 09 Dec 2008)
New Revision: 24349

Log:
syslogd: comment out file locking;
 make signal handling syncronous (old was racy)

function                                             old     new   delta
syslogd_main                                         963    1090    +127
quit_signal                                           96       -     -96
log_locally                                          743     595    -148
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/1 up/down: 127/-244)         Total: -117 bytes



Modified:
   trunk/busybox/sysklogd/syslogd.c


Changeset:
Modified: trunk/busybox/sysklogd/syslogd.c
===================================================================
--- trunk/busybox/sysklogd/syslogd.c	2008-12-09 21:23:31 UTC (rev 24348)
+++ trunk/busybox/sysklogd/syslogd.c	2008-12-09 22:53:31 UTC (rev 24349)
@@ -43,6 +43,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,
@@ -152,7 +155,7 @@
 	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_remotelog  ,)	// -R
 	USE_FEATURE_REMOTE_LOG(    OPTBIT_locallog   ,)	// -L
 	USE_FEATURE_IPC_SYSLOG(    OPTBIT_circularlog,)	// -C
 	USE_FEATURE_SYSLOGD_DUP(   OPTBIT_dup        ,)	// -D
@@ -164,7 +167,7 @@
 	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_remotelog   = USE_FEATURE_REMOTE_LOG(    (1 << OPTBIT_remotelog  )) + 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,
@@ -291,7 +294,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
@@ -332,11 +337,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) {
@@ -355,8 +362,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;
 		}
@@ -365,8 +374,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)
@@ -432,6 +443,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);
@@ -476,15 +488,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)
 {
@@ -553,14 +556,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);
@@ -573,7 +573,7 @@
 
 	timestamp_and_log_internal("syslogd started: BusyBox v" BB_VER);
 
-	for (;;) {
+	while (!bb_got_signal) {
 		ssize_t sz;
 
 #if ENABLE_FEATURE_SYSLOGD_DUP
@@ -584,9 +584,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) {
@@ -633,7 +636,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
 }
 
@@ -641,6 +650,7 @@
 int syslogd_main(int argc UNUSED_PARAM, char **argv)
 {
 	char OPTION_DECL;
+	int opts;
 
 	INIT_G();
 #if ENABLE_FEATURE_REMOTE_LOG
@@ -649,20 +659,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
@@ -671,14 +681,14 @@
 #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);
@@ -688,6 +698,9 @@
 }
 
 /* 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




More information about the busybox-cvs mailing list