Browse code

wrap select()/poll()

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

Trog authored on 2004/03/08 18:55:06
Showing 5 changed files
... ...
@@ -1,3 +1,7 @@
1
+Mon Mar  8 10:01:01 GMT 2004 (trog)
2
+-----------------------------------
3
+  * clamd: wrap select()/poll()
4
+
1 5
 Mon Mar  8 01:24:37 CET 2004 (tk)
2 6
 ---------------------------------
3 7
   * applied comments cleanup (C89) patch (global) from Jesper Juhl
... ...
@@ -30,6 +30,20 @@
30 30
 #include <fcntl.h>
31 31
 #include <time.h>
32 32
 #include <sys/stat.h>
33
+#include <errno.h>
34
+
35
+#undef HAVE_POLL
36
+
37
+#if HAVE_POLL
38
+#if HAVE_POLL_H
39
+#include <poll.h>
40
+#else /* HAVE_POLL_H */
41
+#undef HAVE_POLL
42
+#if HAVE_SYS_SELECT_H
43
+#include <sys/select.h>
44
+#endif /* HAVE_SYS_SELECT_H */
45
+#endif /* HAVE_POLL_H */
46
+#endif /* HAVE_POLL */
33 47
 
34 48
 #if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
35 49
 #include <syslog.h>
... ...
@@ -267,3 +281,52 @@ void virusaction(const char *filename, const char *virname, const struct cfgstru
267 267
 
268 268
     free(cmd);
269 269
 }
270
+
271
+int poll_fd(int fd, int timeout_sec)
272
+{
273
+	int retval;
274
+#ifdef HAVE_POLL
275
+	struct pollfd poll_data[1];
276
+
277
+    poll_data[0].fd = fd;
278
+    poll_data[0].events = POLLIN;
279
+    poll_data[0].revents = 0;
280
+
281
+    while (1) {
282
+    	retval = poll(poll_data, 1, timeout_sec*1000);
283
+	if (retval == -1) {
284
+   	    if (errno == EINTR) {
285
+		continue;
286
+	    }
287
+	    return -1;
288
+	}
289
+	return retval;
290
+    }
291
+
292
+#else
293
+	fd_set rfds;
294
+	struct timeval tv;
295
+
296
+    if (fd >= DEFAULT_FD_SETSIZE) {
297
+	return -1;
298
+    }
299
+
300
+    while (1) {
301
+	FD_ZERO(&rfds);
302
+	FD_SET(fd, &rfds);
303
+	tv.tv_sec = timeout_sec;
304
+	tv.tv_usec = 0;
305
+
306
+	retval = select(fd+1, &rfds, NULL, NULL, &tv);
307
+	if (retval == -1) {
308
+	    if (errno == EINTR) {
309
+		continue;
310
+	    }
311
+	    return -1;
312
+	}
313
+	return retval;
314
+    }
315
+#endif
316
+
317
+    return -1;
318
+}
... ...
@@ -33,6 +33,7 @@ int logsize;
33 33
 const char *logfile;
34 34
 int logg(const char *str, ...);
35 35
 void logg_close(void);
36
+int poll_fd(int fd, int timeout_sec);
36 37
 
37 38
 #if defined(CLAMD_USE_SYSLOG) && !defined(C_AIX)
38 39
 short use_syslog;
... ...
@@ -35,10 +35,6 @@
35 35
 #include <errno.h>
36 36
 #include <clamav.h>
37 37
 
38
-#if HAVE_SYS_SELECT_H
39
-#include <sys/select.h>
40
-#endif
41
-
42 38
 #include "cfgfile.h"
43 39
 #include "others.h"
44 40
 #include "scanner.h"
... ...
@@ -197,8 +193,6 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
197 197
 	struct sockaddr_in server;
198 198
 	struct cfgstruct *cpt;
199 199
 	FILE *tmp = NULL;
200
-	fd_set rfds;
201
-	struct timeval tv;
202 200
 
203 201
     while(!bound && portscan--) {
204 202
 	if((port = cl_rndnum(60000)) < 1024)
... ...
@@ -228,27 +222,16 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
228 228
 	mdprintf(odesc, "PORT %d\n", port);
229 229
     }
230 230
 
231
-    while (1) {
232
-	FD_ZERO(&rfds);
233
-	FD_SET(sockfd, &rfds);
234
-	tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
235
-	tv.tv_usec = 0;	
236
-
237
-	retval = select(sockfd+1, &rfds, NULL, NULL, &tv);
238
-	switch (retval) {
239
-	case 0: /* timeout */
240
-	    mdprintf(sockfd, "ERROR\n");
241
-	    logg("!ScanStream: accept timeout.\n");
242
-	    return -1;
243
-	case -1:
244
-	    if (errno == EINTR) {
245
-		continue;
246
-	    }
247
-	    mdprintf(sockfd, "ERROR\n");
248
-	    logg("!ScanStream: select failed.\n");
249
-	    return -1;
250
-	}
251
-	break;
231
+    retval = poll_fd(sockfd, CL_DEFAULT_SCANTIMEOUT);
232
+    switch (retval) {
233
+    case 0: /* timeout */
234
+	mdprintf(sockfd, "ERROR\n");
235
+	logg("!ScanStream: accept timeout.\n");
236
+	return -1;
237
+    case -1:
238
+	mdprintf(sockfd, "ERROR\n");
239
+	logg("!ScanStream: accept poll failed.\n");
240
+	return -1;
252 241
     }
253 242
 
254 243
     if((acceptd = accept(sockfd, NULL, NULL)) == -1) {
... ...
@@ -261,11 +244,6 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
261 261
 
262 262
     logg("*Accepted connection on port %d, fd %d\n", port, acceptd);
263 263
 
264
-    FD_ZERO(&rfds);
265
-    FD_SET(acceptd, &rfds);
266
-    tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
267
-    tv.tv_usec = 0;
268
-    
269 264
     /* StreamSaveToDisk is enforced, to ensure timeoute */
270 265
     /*if(cfgopt(copt, "StreamSaveToDisk")) {	*/
271 266
 	if((tmp = tmpfile()) == NULL) {
... ...
@@ -281,12 +259,7 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
281 281
 	if((cpt = cfgopt(copt, "StreamMaxLength")))
282 282
 	    maxsize = cpt->numarg;
283 283
 
284
-	while((retval = select(acceptd+1, &rfds, NULL, NULL, &tv)) == 1) {
285
-	    FD_ZERO(&rfds);
286
-	    FD_SET(acceptd, &rfds);
287
-	    tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
288
-	    tv.tv_usec = 0;
289
-	    
284
+	while((retval = poll_fd(acceptd, CL_DEFAULT_SCANTIMEOUT)) == 1) {
290 285
 	    bread = read(acceptd, buff, sizeof(buff));
291 286
 	    if (bread <= 0) {
292 287
 		break;
... ...
@@ -32,10 +32,6 @@
32 32
 #include <signal.h>
33 33
 #include <errno.h>
34 34
 
35
-#if HAVE_SYS_SELECT_H
36
-#include <sys/select.h>
37
-#endif
38
-
39 35
 #include "cfgfile.h"
40 36
 #include "others.h"
41 37
 #include "defaults.h"
... ...
@@ -62,30 +58,17 @@ int command(int desc, const struct cl_node *root, const struct cl_limits *limits
62 62
 {
63 63
 	char buff[1025];
64 64
 	int bread, opt, ret, retval;
65
-	fd_set rfds;
66
-	struct timeval tv;
67
-
68
-    while (1) {
69
-	FD_ZERO(&rfds);
70
-	FD_SET(desc, &rfds);
71
-	tv.tv_sec = CL_DEFAULT_SCANTIMEOUT;
72
-	tv.tv_usec = 0;
73
-
74
-	retval = select(desc+1, &rfds, NULL, NULL, &tv);
75
-	switch (retval) {
76
-	case 0: /* timeout */
77
-	    mdprintf(desc, "ERROR\n");
78
-	    logg("!Command: command timeout.\n");
79
-	    return -1;
80
-	case -1:
81
-	    if (errno == EINTR) {
82
-		continue;
83
-	    }
84
-	    mdprintf(desc, "ERROR\n");
85
-	    logg("!Command: select failed.\n");
86
-	    return -1;
87
-	}
88
-	break;
65
+
66
+    retval = poll_fd(desc, CL_DEFAULT_SCANTIMEOUT);
67
+    switch (retval) {
68
+    case 0: /* timeout */
69
+	mdprintf(desc, "ERROR\n");
70
+	logg("!Command: command timeout.\n");
71
+	return -1;
72
+    case -1:
73
+	mdprintf(desc, "ERROR\n");
74
+	logg("!Command: poll_fd failed.\n");
75
+	return -1;
89 76
     }
90 77
 
91 78
     if((bread = read(desc, buff, 1024)) == -1) {