Browse code

fix close of wrong fd (bb#233)

git-svn: trunk@2632

Tomasz Kojm authored on 2007/01/16 22:10:40
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Jan 16 14:09:12 CET 2007 (tk)
2
+---------------------------------
3
+  * libclamav/loaddb.c: fix close of wrong fd (bb#233), patch from Edwin
4
+
1 5
 Tue Jan 16 01:22:42 CET 2007 (tk)
2 6
 ---------------------------------
3 7
   * libclamav/entconv.c: fix memory corruption in experimental code (bb#227)
... ...
@@ -513,6 +513,7 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
513 513
     time(&start_time);
514 514
 
515 515
     for(;;) {				
516
+	struct stat st_buf;
516 517
     	int socketd = socketds[0];
517 518
     	if(nsockets > 1) {
518 519
     	    int pollret = poll_fds(socketds, nsockets, -1);
... ...
@@ -522,6 +523,15 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
522 522
     		socketd = socketds[0]; /* on a poll error use the first socket */
523 523
     	    }
524 524
     	}    
525
+	if(fstat(socketd, &st_buf) == -1) {
526
+	    logg("!fstat(): socket descriptor gone\n");
527
+	    memmove(socketds, socketds + 1, sizeof(socketds[0]) * nsockets);
528
+	    nsockets--;
529
+	    if(!nsockets) {
530
+		logg("!Main socket gone: fatal\n");
531
+		break;
532
+	    }
533
+	}
525 534
 	new_sd = accept(socketd, NULL, NULL);
526 535
 	if((new_sd == -1) && (errno != EINTR)) {
527 536
 	    if(progexit) {
... ...
@@ -529,7 +539,8 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, unsigned in
529 529
 	    }
530 530
 	    /* very bad - need to exit or restart */
531 531
 #ifdef HAVE_STRERROR_R
532
-	    logg("!accept() failed: %s\n", strerror_r(errno, buff, BUFFSIZE));
532
+	    strerror_r(errno, buff, BUFFSIZE);
533
+	    logg("!accept() failed: %s\n", buff);
533 534
 #else
534 535
 	    logg("!accept() failed\n");
535 536
 #endif
... ...
@@ -134,6 +134,7 @@ int cli_unlockdb(const char *dbdirpath)
134 134
 	pthread_mutex_unlock(&lock_mutex);
135 135
 #ifndef C_WINDOWS
136 136
 	close(lock->lock_fd);
137
+	lock->lock_fd=-1;
137 138
 	unlink(lock->lock_file);
138 139
 #endif
139 140
 	return CL_ELOCKDB;
... ...
@@ -141,6 +142,7 @@ int cli_unlockdb(const char *dbdirpath)
141 141
     lock->lock_type = -1;
142 142
 #ifndef C_WINDOWS
143 143
     close(lock->lock_fd);
144
+    lock->lock_fd=-1;
144 145
     unlink(lock->lock_file);
145 146
 #endif
146 147
     pthread_mutex_unlock(&lock_mutex);
... ...
@@ -229,6 +231,7 @@ static int cli_lockdb(const char *dbdirpath, int wait, int writelock)
229 229
 #ifndef C_WINDOWS
230 230
 	if(errno != EACCES && errno != EAGAIN) {
231 231
 	    close(lock->lock_fd);
232
+	    lock->lock_fd=-1;
232 233
 	    unlink(lock->lock_file);
233 234
 	    return CL_EIO;
234 235
 	}