Browse code

use select() instead of poll()

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

Trog authored on 2004/02/23 19:38:01
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Mon Feb 23 10:42:25 GMT 2004 (trog)
2
+-----------------------------------
3
+  * clamd: use select() instead of poll()
4
+
1 5
 Mon Feb 23 10:19:01 GMT 2004 (njh)
2 6
 ----------------------------------
3 7
   * libclamav: mbox: handle spaces before the : in headers e.g.
... ...
@@ -26,6 +26,7 @@
26 26
 #include <unistd.h>
27 27
 #include <sys/stat.h>
28 28
 #include <sys/types.h>
29
+#include <sys/time.h>
29 30
 #include <sys/wait.h>
30 31
 #include <dirent.h>
31 32
 #include <sys/types.h>
... ...
@@ -33,7 +34,10 @@
33 33
 #include <netinet/in.h>
34 34
 #include <errno.h>
35 35
 #include <clamav.h>
36
-#include <sys/poll.h>
36
+
37
+#if HAVE_SYS_SELECT_H
38
+#include <sys/select.h>
39
+#endif
37 40
 
38 41
 #include "cfgfile.h"
39 42
 #include "others.h"
... ...
@@ -186,14 +190,15 @@ int scan(const char *filename, unsigned long int *scanned, const struct cl_node
186 186
 
187 187
 int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt)
188 188
 {
189
-	int ret, portscan = CL_DEFAULT_MAXPORTSCAN, sockfd, port, acceptd, tmpd, bread, count;
189
+	int ret, portscan = CL_DEFAULT_MAXPORTSCAN, sockfd, port, acceptd, tmpd, bread, retval;
190 190
 	long int size = 0, maxsize = 0;
191 191
 	short bound = 0;
192 192
 	char *virname, buff[32768];
193 193
 	struct sockaddr_in server;
194 194
 	struct cfgstruct *cpt;
195 195
 	FILE *tmp = NULL;
196
-	struct pollfd poll_data[1];
196
+	fd_set rfds;
197
+	struct timeval tv;
197 198
 
198 199
     while(!bound && portscan--) {
199 200
 	if((port = cl_rndnum(60000)) < 1024)
... ...
@@ -223,23 +228,27 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
223 223
 	mdprintf(odesc, "PORT %d\n", port);
224 224
     }
225 225
 
226
-    poll_data[0].fd = sockfd;
227
-    poll_data[0].events = POLLIN;
228
-    poll_data[0].revents = 0;
229
-
230 226
     while (1) {
231
-	count = poll(poll_data, 1, CL_DEFAULT_SCANTIMEOUT*1000); /* wait for timeout */
232
-	if (count != 1) {
233
-		if ((count == -1) && (errno == EINTR)) {
234
-			continue;
235
-		}
236
-		close(sockfd);
237
-		mdprintf(odesc, "ERROR\n");
238
-		logg("!ScanStream: accept timeout.\n");
239
-		return -1;
240
-	} else {
241
-		break;
227
+	FD_ZERO(&rfds);
228
+	FD_SET(sockfd, &rfds);
229
+	tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
230
+	tv.tv_usec = 0;	
231
+
232
+	retval = select(sockfd+1, &rfds, NULL, NULL, &tv);
233
+	switch (retval) {
234
+	case 0: /* timeout */
235
+	    mdprintf(sockfd, "ERROR\n");
236
+	    logg("!ScanStream: accept timeout.\n");
237
+	    return -1;
238
+	case -1:
239
+	    if (errno == EINTR) {
240
+		continue;
241
+	    }
242
+	    mdprintf(sockfd, "ERROR\n");
243
+	    logg("!ScanStream: select failed.\n");
244
+	    return -1;
242 245
 	}
246
+	break;
243 247
     }
244 248
 
245 249
     if((acceptd = accept(sockfd, NULL, NULL)) == -1) {
... ...
@@ -252,10 +261,11 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
252 252
 
253 253
     logg("*Accepted connection on port %d, fd %d\n", port, acceptd);
254 254
 
255
-    poll_data[0].fd = acceptd;
256
-    poll_data[0].events = POLLIN;
257
-    poll_data[0].revents = 0;
258
-
255
+    FD_ZERO(&rfds);
256
+    FD_SET(acceptd, &rfds);
257
+    tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
258
+    tv.tv_usec = 0;
259
+    
259 260
     /* StreamSaveToDisk is enforced, to ensure timeoute */
260 261
     /*if(cfgopt(copt, "StreamSaveToDisk")) {	*/
261 262
 	if((tmp = tmpfile()) == NULL) {
... ...
@@ -271,7 +281,12 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
271 271
 	if((cpt = cfgopt(copt, "StreamMaxLength")))
272 272
 	    maxsize = cpt->numarg;
273 273
 
274
-	while((count = poll(poll_data, 1, CL_DEFAULT_SCANTIMEOUT*1000)) == 1) {
274
+	while((retval = select(acceptd+1, &rfds, NULL, NULL, &tv)) == 1) {
275
+	    FD_ZERO(&rfds);
276
+	    FD_SET(acceptd, &rfds);
277
+	    tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
278
+	    tv.tv_usec = 0;
279
+	    
275 280
 	    bread = read(acceptd, buff, sizeof(buff));
276 281
 	    if (bread <= 0) {
277 282
 		break;
... ...
@@ -26,12 +26,16 @@
26 26
 #include <unistd.h>
27 27
 #include <sys/types.h>
28 28
 #include <sys/socket.h>
29
+#include <sys/time.h>
29 30
 #include <pthread.h>
30 31
 #include <time.h>
31 32
 #include <signal.h>
32
-#include <poll.h>
33 33
 #include <errno.h>
34 34
 
35
+#if HAVE_SYS_SELECT_H
36
+#include <sys/select.h>
37
+#endif
38
+
35 39
 #include "cfgfile.h"
36 40
 #include "others.h"
37 41
 #include "defaults.h"
... ...
@@ -57,25 +61,31 @@
57 57
 int command(int desc, const struct cl_node *root, const struct cl_limits *limits, int options, const struct cfgstruct *copt)
58 58
 {
59 59
 	char buff[1025];
60
-	int bread, opt, ret, count;
61
-	struct pollfd poll_data[1];
60
+	int bread, opt, ret, retval;
61
+	fd_set rfds;
62
+	struct timeval tv;
62 63
 
63
-    poll_data[0].fd = desc;
64
-    poll_data[0].events = POLLIN;
65
-    poll_data[0].revents = 0;
66
-                                                                                                                                                                                
67 64
     while (1) {
68
-        count = poll(poll_data, 1, CL_DEFAULT_SCANTIMEOUT*1000); /* wait for timeout */
69
-        if (count != 1) {
70
-                if ((count == -1) && (errno == EINTR)) {
71
-                        continue;
72
-                }
73
-                mdprintf(desc, "ERROR\n");
74
-                logg("!ScanStream: command timeout.\n");
75
-                return -1;
76
-        } else {
77
-                break;
78
-        }
65
+	FD_ZERO(&rfds);
66
+	FD_SET(desc, &rfds);
67
+	tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
68
+	tv.tv_usec = 0;
69
+
70
+	retval = select(desc+1, &rfds, NULL, NULL, &tv);
71
+	switch (retval) {
72
+	case 0: /* timeout */
73
+	    mdprintf(desc, "ERROR\n");
74
+	    logg("!Command: command timeout.\n");
75
+	    return -1;
76
+	case -1:
77
+	    if (errno == EINTR) {
78
+		continue;
79
+	    }
80
+	    mdprintf(desc, "ERROR\n");
81
+	    logg("!Command: select failed.\n");
82
+	    return -1;
83
+	}
84
+	break;
79 85
     }
80 86
 
81 87
     if((bread = read(desc, buff, 1024)) == -1) {