Browse code

stop scanning if the client disconnects

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@434 77e5149b-7576-45b1-b177-96237e5ba77b

Trog authored on 2004/03/24 00:28:32
Showing 4 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Mar 23 15:39:09 GMT 2004 (trog)
2
+-----------------------------------
3
+  * clamd: stop scanning if the client disconnects
4
+
1 5
 Tue Mar 23 11:09:30 GMT 2004 (njh)
2 6
 ----------------------------------
3 7
   * libclamav/blob.c:	More restrictive about which characters can be used
... ...
@@ -32,6 +32,7 @@
32 32
 #include <sys/stat.h>
33 33
 #include <errno.h>
34 34
 #include <sys/time.h>
35
+#include <sys/socket.h>
35 36
 #if HAVE_SYS_TYPES_H
36 37
 #include <sys/types.h>
37 38
 #endif
... ...
@@ -332,3 +333,45 @@ int poll_fd(int fd, int timeout_sec)
332 332
 
333 333
     return -1;
334 334
 }
335
+
336
+int is_fd_connected(int fd)
337
+{
338
+#ifdef HAVE_POLL
339
+	struct pollfd poll_data[1];
340
+
341
+    poll_data[0].fd = fd;
342
+    poll_data[0].events = POLLIN;
343
+    poll_data[0].revents = 0;
344
+
345
+    if (poll(poll_data, 1, 0) == -1) {
346
+	return 1;
347
+    }
348
+    if (poll_data[0].revents & POLLHUP) {
349
+	return 0;
350
+    }
351
+    return 1;
352
+
353
+#else
354
+	fd_set rfds;
355
+	struct timeval tv;
356
+	char buff[1];
357
+
358
+    if (fd >= DEFAULT_FD_SETSIZE) {
359
+        return 1;
360
+    }
361
+
362
+    FD_ZERO(&rfds);
363
+    FD_SET(fd, &rfds);
364
+    tv.tv_sec = 0;
365
+    tv.tv_usec = 0;
366
+    if (select(fd+1, &rfds, NULL, NULL, &tv) <= 0) {
367
+	return 1;
368
+    }
369
+    if (FD_ISSET(fd, &rfds)) {
370
+	if (recv(fd, buff, 1, MSG_PEEK) != 1) {
371
+	    return 0;
372
+	}
373
+    }
374
+    return 1;
375
+#endif
376
+}
... ...
@@ -38,6 +38,7 @@ const char *logfile;
38 38
 int logg(const char *str, ...);
39 39
 void logg_close(void);
40 40
 int poll_fd(int fd, int timeout_sec);
41
+int is_fd_connected(int fd);
41 42
 
42 43
 #if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
43 44
 short use_syslog;
... ...
@@ -78,6 +78,11 @@ int dirscan(const char *dirname, const char **virname, unsigned long int *scanne
78 78
 
79 79
     if((dd = opendir(dirname)) != NULL) {
80 80
 	while((dent = readdir(dd))) {
81
+	    if (!is_fd_connected(odesc)) {
82
+		logg("Client disconnected\n");
83
+		closedir(dd);
84
+		return 1;
85
+	    }
81 86
 	    if(dent->d_ino) {
82 87
 		if(strcmp(dent->d_name, ".") && strcmp(dent->d_name, "..")) {
83 88
 		    /* build the full name */