Last year Andreas Bergmann sent a patch to configure multiple remote
logging hosts in the syslog daemon. Vladimir Dronnikow complained about
the (artificial) limit of 8 remote hosts and proposed some changes.
Andreas abandoned the patch, then.

I need this feature too, so I came back to this patch. The limit of 8
hosts is now gone and I made some other optimizations:

function                                             old     new   delta
syslogd_main                                        1113    1220    +107
.rodata                                           126306  126304      -2
init_data                                             72      64      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 107/-10)            Total: 97 bytes
   text    data     bss     dec     hex filename
 678068    2077    9040  689185   a8421 busybox_old
 678173    2077    9040  689290   a848a busybox_unstripped

The second patch (on top of the first) makes this addition configurable
through the FEATURE_MULTIPLE_REMOTE_HOSTS option.
This adds only 5 bytes when the config option is off, but it makes the code
quite ugly (IMHO).

function                                             old     new   delta
syslogd_main                                        1113    1134     +21
init_data                                             72      64      -8
.rodata                                           126306  126298      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 21/-16)              Total: 5 bytes
   text    data     bss     dec     hex filename
 678068    2077    9040  689185   a8421 busybox_old
 678081    2077    9040  689198   a842e busybox_unstripped

If this feature is accepted, I would propose to use only the first
patch. More logging hosts are always better :-)

Thomas

--- busybox-1.16.0/sysklogd/syslogd.c	2010-01-25 01:59:38.000000000 +0100
+++ busybox.new2/sysklogd/syslogd.c	2010-02-23 15:20:44.996887904 +0100
@@ -57,6 +57,15 @@
 	char data[1];   /* data/messages */
 };
 
+#if ENABLE_FEATURE_REMOTE_LOG
+typedef struct {
+	int remoteFD;
+	len_and_sockaddr* remoteAddr;
+	unsigned last_dns_resolve;
+	char *remoteAddrStr;
+} remoteHost_t;
+#endif
+
 /* Allows us to have smaller initializer. Ugly. */
 #define GLOBALS \
 	const char *logFilePath;                \
@@ -73,11 +82,6 @@
 	unsigned curFileSize;                   \
 	smallint isRegular;                     \
 ) \
-IF_FEATURE_REMOTE_LOG( \
-	/* udp socket for remote logging */     \
-	int remoteFD;                           \
-	len_and_sockaddr* remoteAddr;           \
-) \
 IF_FEATURE_IPC_SYSLOG( \
 	int shmid; /* ipc shared memory id */   \
 	int s_semid; /* ipc semaphore id */     \
@@ -94,8 +98,7 @@
 	GLOBALS
 
 #if ENABLE_FEATURE_REMOTE_LOG
-	unsigned last_dns_resolve;
-	char *remoteAddrStr;
+	llist_t *remoteHosts;
 #endif
 
 #if ENABLE_FEATURE_IPC_SYSLOG
@@ -127,9 +130,6 @@
 	.logFileSize = 200 * 1024,
 	.logFileRotate = 1,
 #endif
-#if ENABLE_FEATURE_REMOTE_LOG
-	.remoteFD = -1,
-#endif
 #if ENABLE_FEATURE_IPC_SYSLOG
 	.shmid = -1,
 	.s_semid = -1,
@@ -185,7 +185,7 @@
 #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \
 	IF_FEATURE_ROTATE_LOGFILE(,&opt_s) \
 	IF_FEATURE_ROTATE_LOGFILE(,&opt_b) \
-	IF_FEATURE_REMOTE_LOG(    ,&G.remoteAddrStr) \
+	IF_FEATURE_REMOTE_LOG(	  ,&remoteAddrList) \
 	IF_FEATURE_IPC_SYSLOG(    ,&opt_C)
 
 
@@ -534,20 +534,22 @@
 }
 
 #if ENABLE_FEATURE_REMOTE_LOG
-static int try_to_resolve_remote(void)
+static int try_to_resolve_remote(remoteHost_t *rh)
 {
-	if (!G.remoteAddr) {
+	if (rh == NULL) return -1;
+
+	if (!rh->remoteAddr) {
 		unsigned now = monotonic_sec();
 
 		/* Don't resolve name too often - DNS timeouts can be big */
-		if ((now - G.last_dns_resolve) < DNS_WAIT_SEC)
+		if ((now - rh->last_dns_resolve) < DNS_WAIT_SEC)
 			return -1;
-		G.last_dns_resolve = now;
-		G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
-		if (!G.remoteAddr)
+		rh->last_dns_resolve = now;
+		rh->remoteAddr = host2sockaddr(rh->remoteAddrStr, 514);
+		if (!rh->remoteAddr)
 			return -1;
 	}
-	return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
+	return socket(rh->remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
 }
 #endif
 
@@ -555,6 +557,9 @@
 static void do_syslogd(void)
 {
 	int sock_fd;
+#if ENABLE_FEATURE_REMOTE_LOG
+	llist_t *item;
+#endif
 #if ENABLE_FEATURE_SYSLOGD_DUP
 	int last_sz = -1;
 	char *last_buf;
@@ -622,21 +627,27 @@
 #if ENABLE_FEATURE_REMOTE_LOG
 		/* We are not modifying log messages in any way before send */
 		/* Remote site cannot trust _us_ anyway and need to do validation again */
-		if (G.remoteAddrStr) {
-			if (-1 == G.remoteFD) {
-				G.remoteFD = try_to_resolve_remote();
-				if (-1 == G.remoteFD)
-					goto no_luck;
+        for (item = G.remoteHosts; item != NULL; item = item->link) {
+            remoteHost_t *rh = (remoteHost_t *)item->data;
+
+			if (rh->remoteAddrStr) {
+				if (rh->remoteFD == -1) {
+					rh->remoteFD = try_to_resolve_remote(rh);
+
+					if (rh->remoteFD == -1)
+						continue;
+				}
+
+                /* Stock syslogd sends it '\n'-terminated
+                 * over network, mimic that */
+                recvbuf[sz] = '\n';
+
+                /* send message to remote logger, ignore possible error */
+                /* TODO: on some errors, close and set G.remoteFD to -1
+                 * so that DNS resolution and connect is retried? */
+                sendto(rh->remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
+                        &(rh->remoteAddr->u.sa), rh->remoteAddr->len);
 			}
-			/* Stock syslogd sends it '\n'-terminated
-			 * over network, mimic that */
-			recvbuf[sz] = '\n';
-			/* send message to remote logger, ignore possible error */
-			/* TODO: on some errors, close and set G.remoteFD to -1
-			 * so that DNS resolution and connect is retried? */
-			sendto(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
-				    &G.remoteAddr->u.sa, G.remoteAddr->len);
- no_luck: ;
 		}
 #endif
 		if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
@@ -653,20 +664,47 @@
 #undef recvbuf
 }
 
+#if ENABLE_FEATURE_REMOTE_LOG
+static void add_remoteHost(char *s)
+{
+	remoteHost_t *rh;
+
+	rh = xzalloc(sizeof(*rh));
+	rh->remoteAddrStr = s;
+	rh->remoteFD = -1;
+	rh->last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
+
+	llist_add_to(&G.remoteHosts, rh);
+}
+#endif
+
 int syslogd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int syslogd_main(int argc UNUSED_PARAM, char **argv)
 {
 	char OPTION_DECL;
 	int opts;
+#if ENABLE_FEATURE_REMOTE_LOG
+	llist_t *remoteAddrList;
+#endif
 
 	INIT_G();
 #if ENABLE_FEATURE_REMOTE_LOG
-	G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
+	remoteAddrList = NULL;
 #endif
 
 	/* do normal option parsing */
+#if ENABLE_FEATURE_REMOTE_LOG
+	opt_complementary = "=0R::"; /* no non-option params but note that -R can occur multiple time now */
+#else
 	opt_complementary = "=0"; /* no non-option params */
+#endif
 	opts = getopt32(argv, OPTION_STR, OPTION_PARAM);
+
+#if ENABLE_FEATURE_REMOTE_LOG
+	while (remoteAddrList)
+        add_remoteHost(llist_pop(&remoteAddrList));
+#endif
+
 #ifdef SYSLOGD_MARK
 	if (opts & OPT_mark) // -m
 		G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
--- busybox-1.16.0/sysklogd/syslogd.c	2010-02-23 16:30:15.144534833 +0100
+++ busybox.new2/sysklogd/syslogd.c	2010-02-23 16:30:37.732799789 +0100
@@ -98,7 +98,11 @@
 	GLOBALS

 #if ENABLE_FEATURE_REMOTE_LOG
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 	llist_t *remoteHosts;
+#else
+    remoteHost_t remoteHost;
+#endif
 #endif

 #if ENABLE_FEATURE_IPC_SYSLOG
@@ -558,7 +562,11 @@
 {
 	int sock_fd;
 #if ENABLE_FEATURE_REMOTE_LOG
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 	llist_t *item;
+#else
+    remoteHost_t *rh;
+#endif
 #endif
 #if ENABLE_FEATURE_SYSLOGD_DUP
 	int last_sz = -1;
@@ -627,15 +635,23 @@
 #if ENABLE_FEATURE_REMOTE_LOG
 		/* We are not modifying log messages in any way before send */
 		/* Remote site cannot trust _us_ anyway and need to do validation again */
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
         for (item = G.remoteHosts; item != NULL; item = item->link) {
             remoteHost_t *rh = (remoteHost_t *)item->data;
-
+#else
+            rh = &G.remoteHost;
+#endif
 			if (rh->remoteAddrStr) {
 				if (rh->remoteFD == -1) {
 					rh->remoteFD = try_to_resolve_remote(rh);

+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 					if (rh->remoteFD == -1)
 						continue;
+#else
+					if (rh->remoteFD == -1)
+                        goto no_luck;
+#endif
 				}

                 /* Stock syslogd sends it '\n'-terminated
@@ -648,7 +664,11 @@
                 sendto(rh->remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
                         &(rh->remoteAddr->u.sa), rh->remoteAddr->len);
 			}
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 		}
+#else
+no_luck:
+#endif
 #endif
 		if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
 			recvbuf[sz] = '\0'; /* ensure it *is* NUL terminated */
@@ -664,7 +684,7 @@
 #undef recvbuf
 }

-#if ENABLE_FEATURE_REMOTE_LOG
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 static void add_remoteHost(char *s)
 {
 	remoteHost_t *rh;
@@ -684,16 +704,24 @@
 	char OPTION_DECL;
 	int opts;
 #if ENABLE_FEATURE_REMOTE_LOG
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 	llist_t *remoteAddrList;
+#else
+    char *remoteAddrList;
+#endif
 #endif

 	INIT_G();
 #if ENABLE_FEATURE_REMOTE_LOG
 	remoteAddrList = NULL;
+#if !ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
+	G.remoteHost.remoteFD = -1;
+	G.remoteHost.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
+#endif
 #endif

 	/* do normal option parsing */
-#if ENABLE_FEATURE_REMOTE_LOG
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 	opt_complementary = "=0R::"; /* no non-option params but note that -R can occur multiple time now */
 #else
 	opt_complementary = "=0"; /* no non-option params */
@@ -701,8 +729,12 @@
 	opts = getopt32(argv, OPTION_STR, OPTION_PARAM);

 #if ENABLE_FEATURE_REMOTE_LOG
+#if ENABLE_FEATURE_MULTIPLE_REMOTE_HOSTS
 	while (remoteAddrList)
         add_remoteHost(llist_pop(&remoteAddrList));
+#else
+    G.remoteHost.remoteAddrStr = remoteAddrList;
+#endif
 #endif

 #ifdef SYSLOGD_MARK
--- busybox-1.16.0/sysklogd/Config.in	2010-01-25 01:59:38.000000000 +0100
+++ busybox.new2/sysklogd/Config.in	2010-02-23 17:26:54.440918301 +0100
@@ -42,6 +42,14 @@
 	  measure to prevent system logs from being tampered with
 	  by an intruder.

+config FEATURE_MULTIPLE_REMOTE_HOSTS
+	bool "Support for multiple remote hosts"
+	default n
+	depends on FEATURE_REMOTE_LOG
+	help
+	  This feature makes it possible to configure an arbitrary
+	  number of remote hosts (multiple us of -R option).
+
 config FEATURE_SYSLOGD_DUP
 	bool "Support -D (drop dups) option"
 	default n
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to