Browse code

partial fix for signal immunity in clamd more to come... (closes bb#320)

git-svn: trunk@2836

aCaB authored on 2007/02/22 20:52:48
Showing 5 changed files
... ...
@@ -1,3 +1,7 @@
1
+Thu Feb 22 10:31:23 CET 2007 (acab)
2
+-----------------------------------
3
+  * clamd: handle signals while polling the sockets in the main loop (bb#320)
4
+
1 5
 Thu Feb 22 09:00:31 GMT 2007 (njh)
2 6
 ----------------------------------
3 7
   * clamav-milter/clamav-milter.c:	Better recovery when a remote clamd
... ...
@@ -151,7 +151,7 @@ void virusaction(const char *filename, const char *virname, const struct cfgstru
151 151
 }
152 152
 #endif /* C_WINDOWS */
153 153
 
154
-int poll_fds(int *fds, int nfds, int timeout_sec)
154
+int poll_fds(int *fds, int nfds, int timeout_sec, int check_signals)
155 155
 {
156 156
 	int retval;
157 157
 	int i;
... ...
@@ -179,7 +179,7 @@ int poll_fds(int *fds, int nfds, int timeout_sec)
179 179
     while (1) {
180 180
     	retval = poll(poll_data, nfds, timeout_sec);
181 181
 	if (retval == -1) {
182
-   	    if (errno == EINTR) {
182
+	    if (errno == EINTR && !check_signals) {
183 183
 		continue;
184 184
 	    }
185 185
 	    if (nfds>1)
... ...
@@ -245,9 +245,9 @@ int poll_fds(int *fds, int nfds, int timeout_sec)
245 245
     return -1;
246 246
 }
247 247
 
248
-int poll_fd(int fd, int timeout_sec)
248
+int poll_fd(int fd, int timeout_sec, int check_signals)
249 249
 {
250
-    return poll_fds(&fd, 1, timeout_sec);
250
+    return poll_fds(&fd, 1, timeout_sec, check_signals);
251 251
 }
252 252
 
253 253
 int is_fd_connected(int fd)
... ...
@@ -373,7 +373,7 @@ int readsock(int sockfd, char *buf, size_t size, unsigned char delim, int timeou
373 373
     time(&starttime);
374 374
     while(1) {
375 375
 	time(&timenow);
376
-	switch(poll_fd(sockfd, (timeout_sec && ((timeout_sec-(timenow-starttime)) > 0)) ? timeout_sec-(timenow-starttime) : 0)) {
376
+	switch(poll_fd(sockfd, (timeout_sec && ((timeout_sec-(timenow-starttime)) > 0)) ? timeout_sec-(timenow-starttime) : 0, 0)) {
377 377
 	    case 0: /* timeout */
378 378
 		return -2;
379 379
 	    case -1:
... ...
@@ -457,7 +457,7 @@ int readsock(int sockfd, char *buf, size_t size, unsigned char delim, int timeou
457 457
 	}
458 458
 	while(1) {
459 459
 	    time(&timenow);
460
-	    switch(poll_fd(sockfd, ((timeout_sec-(timenow-starttime)) > 0) ? timeout_sec-(timenow-starttime) : 0)) {
460
+	    switch(poll_fd(sockfd, ((timeout_sec-(timenow-starttime)) > 0) ? timeout_sec-(timenow-starttime) : 0, 0)) {
461 461
 		case 0: /* timeout */
462 462
 		    return -2;
463 463
 		case -1:
... ...
@@ -27,8 +27,8 @@
27 27
 #include <stdlib.h>
28 28
 #include "shared/cfgparser.h"
29 29
 
30
-int poll_fds(int *fds, int nfds, int timeout_sec);
31
-int poll_fd(int fd, int timeout_sec);
30
+int poll_fds(int *fds, int nfds, int timeout_sec, int check_signals);
31
+int poll_fd(int fd, int timeout_sec, int check_signals);
32 32
 int is_fd_connected(int fd);
33 33
 void virusaction(const char *filename, const char *virname, const struct cfgstruct *copt);
34 34
 int writen(int fd, void *buff, unsigned int count);
... ...
@@ -484,7 +484,7 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_engine *en
484 484
 	}
485 485
     }
486 486
 
487
-    switch(retval = poll_fd(sockfd, timeout)) {
487
+    switch(retval = poll_fd(sockfd, timeout, 0)) {
488 488
 	case 0: /* timeout */
489 489
 	    mdprintf(odesc, "Accept timeout. ERROR\n");
490 490
 	    logg("!ScanStream %d: accept timeout.\n", port);
... ...
@@ -519,7 +519,7 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_engine *en
519 519
 
520 520
     btread = sizeof(buff);
521 521
 
522
-    while((retval = poll_fd(acceptd, timeout)) == 1) {
522
+    while((retval = poll_fd(acceptd, timeout, 0)) == 1) {
523 523
 	bread = recv(acceptd, buff, btread, 0);
524 524
 	if(bread <= 0)
525 525
 	    break;
... ...
@@ -251,7 +251,7 @@ static struct cl_engine *reload_db(struct cl_engine *engine, unsigned int dbopti
251 251
 
252 252
 int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigned int dboptions, const struct cfgstruct *copt)
253 253
 {
254
-	int new_sd, max_threads, i, ret = 0;
254
+	int max_threads, i, ret = 0;
255 255
 	unsigned int options = 0;
256 256
 	threadpool_t *thr_pool;
257 257
 #ifndef	C_WINDOWS
... ...
@@ -520,16 +520,18 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigne
520 520
 	    struct stat st_buf;
521 521
 #endif
522 522
     	int socketd = socketds[0];
523
+	int new_sd = 0;
524
+
523 525
     	if(nsockets > 1) {
524
-    	    int pollret = poll_fds(socketds, nsockets, -1);
526
+	    int pollret = poll_fds(socketds, nsockets, -1, 1);
525 527
     	    if(pollret > 0) {
526 528
     		socketd = socketds[pollret - 1];
527 529
     	    } else {
528
-    		socketd = socketds[0]; /* on a poll error use the first socket */
530
+		new_sd = -1;
529 531
     	    }
530 532
     	}
531 533
 #if !defined(C_WINDOWS) && !defined(C_BEOS)
532
-	if(fstat(socketd, &st_buf) == -1) {
534
+	if(new_sd != -1 && fstat(socketd, &st_buf) == -1) {
533 535
 	    logg("!fstat(): socket descriptor gone\n");
534 536
 	    memmove(socketds, socketds + 1, sizeof(socketds[0]) * nsockets);
535 537
 	    nsockets--;
... ...
@@ -539,7 +541,8 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_engine *engine, unsigne
539 539
 	    }
540 540
 	}
541 541
 #endif
542
-	new_sd = accept(socketd, NULL, NULL);
542
+	if (new_sd != -1)
543
+	    new_sd = accept(socketd, NULL, NULL);
543 544
 	if((new_sd == -1) && (errno != EINTR)) {
544 545
 	    if(progexit) {
545 546
 	    	break;