Browse code

Fix handling of only one server being up

git-svn: trunk@3118

Nigel Horne authored on 2007/06/27 18:11:22
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+Wed Jun 27 09:11:29 BST 2007 (njh)
2
+---------------------------------
3
+  * clamav-milter:	If there's only one clamd server up, ensure that it
4
+				is used even if it's not the first listed
5
+
1 6
 Tue Jun 26 10:19:00 CEST 2007 (edwin)
2 7
 ----------------------------------
3 8
   * libclamav/regex_list.c:	fix false substring matches, related to bug
... ...
@@ -33,7 +33,7 @@
33 33
  */
34 34
 static	char	const	rcsid[] = "$Id: clamav-milter.c,v 1.312 2007/02/12 22:24:21 njh Exp $";
35 35
 
36
-#define	CM_VERSION	"devel-070626"
36
+#define	CM_VERSION	"devel-070627"
37 37
 
38 38
 #if HAVE_CONFIG_H
39 39
 #include "clamav-config.h"
... ...
@@ -289,7 +289,7 @@ static	int		createSession(unsigned int s);
289 289
 #else
290 290
 static	int		pingServer(int serverNumber);
291 291
 static	void		*try_server(void *var);
292
-static	int		active_servers(void);
292
+static	int		active_servers(int *active);
293 293
 struct	try_server_struct {
294 294
 	int	sock;
295 295
 	int	rc;
... ...
@@ -2305,7 +2305,7 @@ static int
2305 2305
 findServer(void)
2306 2306
 {
2307 2307
 	struct sockaddr_in *servers, *server;
2308
-	int maxsock, i, j;
2308
+	int maxsock, i, j, active;
2309 2309
 	int retval;
2310 2310
 	pthread_t *tids;
2311 2311
 	struct try_server_struct *socks;
... ...
@@ -2317,8 +2317,8 @@ findServer(void)
2317 2317
 	if(numServers == 1)
2318 2318
 		return 0;
2319 2319
 
2320
-	if(active_servers() <= 1)
2321
-		return 0;
2320
+	if(active_servers(&active) <= 1)
2321
+		return active;
2322 2322
 
2323 2323
 	servers = (struct sockaddr_in *)cli_calloc(numServers, sizeof(struct sockaddr_in));
2324 2324
 	if(servers == NULL)
... ...
@@ -2456,17 +2456,19 @@ findServer(void)
2456 2456
 /*
2457 2457
  * How many servers are up at the moment? If a server is marked as down,
2458 2458
  *	don't keep on flooding it with requests to see if it's now back up
2459
+ * If only one server is active, let the caller know
2459 2460
  */
2460 2461
 static int
2461
-active_servers(void)
2462
+active_servers(int *active)
2462 2463
 {
2463 2464
 	int server, count;
2464 2465
 	time_t now = (time_t)0;
2465 2466
 
2466 2467
 	for(count = server = 0; server < numServers; server++)
2467
-		if(last_failed_pings[server] == (time_t)0)
2468
+		if(last_failed_pings[server] == (time_t)0) {
2469
+			*active = server;
2468 2470
 			count++;
2469
-		else {
2471
+		} else {
2470 2472
 			if(now == (time_t)0)
2471 2473
 				time(&now);
2472 2474
 			if(now - last_failed_pings[server] >= RETRY_SECS)
... ...
@@ -2474,6 +2476,8 @@ active_servers(void)
2474 2474
 				last_failed_pings[server] = (time_t)0;
2475 2475
 		}
2476 2476
 
2477
+	if(count != 1)
2478
+		*active = 0;
2477 2479
 	return count;
2478 2480
 }
2479 2481
 
... ...
@@ -4383,9 +4387,17 @@ connect2clamd(struct privdata *privdata)
4383 4383
 				return 0;
4384 4384
 			}
4385 4385
 			if(connect(privdata->cmdSocket, (struct sockaddr *)&server, sizeof(struct sockaddr_in)) < 0) {
4386
-				perror("connect");
4386
+				char *hostname = cli_strtok(serverHostNames, freeServer, ":");
4387
+
4388
+				perror(hostname ? hostname : "connect");
4389
+				close(privdata->cmdSocket);
4390
+				privdata->cmdSocket = -1;
4391
+				if(hostname)
4392
+					free(hostname);
4393
+				time(&last_failed_pings[freeServer]);
4387 4394
 				return 0;
4388 4395
 			}
4396
+			last_failed_pings[freeServer] = (time_t)0;
4389 4397
 #endif
4390 4398
 			privdata->serverNumber = freeServer;
4391 4399
 		}