Dear folks,
I've modified the syslogd in Busybox-Version 1.11 to be able to send to
multiple hosts when remote logging.
All that is to do is to specify "-R" multiple times; current limit is eight
hosts.
Well, I just didn't want to hold this back so I've attached the patch.
Have a nice day :-)
Andreas
_________________________________________________________________
News, entertainment and everything you care about at Live.com. Get it now!
http://www.live.com/getstarted.aspx
diff --exclude CVS -uNr busybox-1.11.1/sysklogd/syslogd.c busybox-1.11.1.modified/sysklogd/syslogd.c
--- busybox-1.11.1/sysklogd/syslogd.c 2008-06-25 14:51:17.000000000 +0200
+++ busybox-1.11.1.modified/sysklogd/syslogd.c 2009-01-21 09:35:04.000000000 +0100
@@ -27,6 +27,10 @@
#if ENABLE_FEATURE_REMOTE_LOG
#include <netinet/in.h>
+
+//specify to how many remote hosts this syslog ought to be able to send messages to
+#define MAX_REMOTE_HOSTS 8
+
#endif
#if ENABLE_FEATURE_IPC_SYSLOG
@@ -73,8 +77,8 @@
) \
USE_FEATURE_REMOTE_LOG( \
/* udp socket for remote logging */ \
- int remoteFD; \
- len_and_sockaddr* remoteAddr; \
+ int remoteFD[ MAX_REMOTE_HOSTS ]; \
+ len_and_sockaddr* remoteAddr[ MAX_REMOTE_HOSTS ]; \
) \
USE_FEATURE_IPC_SYSLOG( \
int shmid; /* ipc shared memory id */ \
@@ -84,6 +88,8 @@
struct sembuf SMwdn[3]; \
)
+
+
struct init_globals {
GLOBALS
};
@@ -92,8 +98,10 @@
GLOBALS
#if ENABLE_FEATURE_REMOTE_LOG
- unsigned last_dns_resolve;
- char *remoteAddrStr;
+ unsigned last_dns_resolve[ MAX_REMOTE_HOSTS ];
+ char *remoteAddrStr[ MAX_REMOTE_HOSTS ];
+ unsigned remoteHostCount; //tells how many remote hosts have been specified
+ llist_t *remoteAddrList;
#endif
#if ENABLE_FEATURE_IPC_SYSLOG
@@ -126,7 +134,7 @@
.logFileRotate = 1,
#endif
#if ENABLE_FEATURE_REMOTE_LOG
- .remoteFD = -1,
+ .remoteFD = {-1},
#endif
#if ENABLE_FEATURE_IPC_SYSLOG
.shmid = -1,
@@ -183,7 +191,7 @@
#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_REMOTE_LOG( , &G.remoteAddrList ) \
USE_FEATURE_IPC_SYSLOG( ,&opt_C)
@@ -524,20 +532,23 @@
}
#if ENABLE_FEATURE_REMOTE_LOG
-static int try_to_resolve_remote(void)
+static int try_to_resolve_remote(int host_index)
{
- if (!G.remoteAddr) {
+ //check that host_index is between the array boundary
+ if( (host_index < 0) || (host_index > (MAX_REMOTE_HOSTS - 1) ) ) return -1;
+
+ if (!G.remoteAddr[host_index]) {
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 - G.last_dns_resolve[host_index]) < DNS_WAIT_SEC)
return -1;
- G.last_dns_resolve = now;
- G.remoteAddr = host2sockaddr(G.remoteAddrStr, 514);
- if (!G.remoteAddr)
+ G.last_dns_resolve[host_index] = now;
+ G.remoteAddr[host_index] = host2sockaddr(G.remoteAddrStr[host_index], 514);
+ if (!G.remoteAddr[host_index])
return -1;
}
- return socket(G.remoteAddr->u.sa.sa_family, SOCK_DGRAM, 0);
+ return socket(G.remoteAddr[host_index]->u.sa.sa_family, SOCK_DGRAM, 0);
}
#endif
@@ -545,6 +556,9 @@
static void do_syslogd(void)
{
int sock_fd;
+#if ENABLE_FEATURE_REMOTE_LOG
+ int host_counter = 0; //used for remote logging
+#endif
#if ENABLE_FEATURE_SYSLOGD_DUP
int last_sz = -1;
char *last_buf;
@@ -612,21 +626,30 @@
#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(host_counter = 0; host_counter < G.remoteHostCount; host_counter++)
+ {
+ if( G.remoteAddrStr[ host_counter ] )
+ {
+ if( -1 == G.remoteFD[ host_counter ])
+ {
+ G.remoteFD[ host_counter] = try_to_resolve_remote( host_counter );
+
+ if( -1 == G.remoteFD[ host_counter ] )
+ 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(G.remoteFD, recvbuf, sz+1, MSG_DONTWAIT,
- &G.remoteAddr->u.sa, G.remoteAddr->len);
- no_luck: ;
+ sendto(G.remoteFD[ host_counter ], recvbuf, sz+1, MSG_DONTWAIT,
+ &(G.remoteAddr[ host_counter ]->u.sa), G.remoteAddr[ host_counter ]->len);
+ }
}
#endif
if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) {
@@ -641,15 +664,51 @@
int syslogd_main(int argc ATTRIBUTE_UNUSED, char **argv)
{
char OPTION_DECL;
+ int i = 0; //general counter
INIT_G();
#if ENABLE_FEATURE_REMOTE_LOG
- G.last_dns_resolve = monotonic_sec() - DNS_WAIT_SEC - 1;
+
+ /* Initialize some variables specific for remote logging */
+ for(i = 0; i < MAX_REMOTE_HOSTS; i++)
+ {
+ G.last_dns_resolve[i] = monotonic_sec() - DNS_WAIT_SEC - 1;
+
+ //NOTE: G.remoteFD = {-1} upon initialization will only init the first element which WILL result in an error later on!
+ G.remoteFD[i] = -1;
+ }
+
+ //Argument list MUST be set to NULL initially
+ G.remoteAddrList = NULL;
#endif
/* do normal option parsing */
- opt_complementary = "=0"; /* no non-option params */
+ opt_complementary = "=0R::"; /* no non-option params but note that -R can occur multiple time now */
getopt32(argv, OPTION_STR, OPTION_PARAM);
+
+#if ENABLE_FEATURE_REMOTE_LOG
+
+ //Extract hostnames from argument list:
+ i = 0; //i will keep track of the number of hosts that have been processed.
+ //If i reaches MAX_REMOTE_HOSTS the remaining arguments in the list will simply be popped to nowhere (thus freeing the list).
+ //This kind of freeing is a workaround because using free_llist() produces an "undefined reference" error which I
+ //don't understand quite yet.
+ while( G.remoteAddrList )
+ {
+ if( i >= MAX_REMOTE_HOSTS )
+ {
+ llist_pop( &G.remoteAddrList );
+ }
+ else
+ {
+ G.remoteAddrStr[ i ] = (char *)llist_pop( &G.remoteAddrList );
+ i++; //counter only needs to be incremented when there are still places left in the hostlist
+ }
+ }
+
+ G.remoteHostCount = i;
+#endif
+
#ifdef SYSLOGD_MARK
if (option_mask32 & OPT_mark) // -m
G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60;
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox