Browse code

apply w32 patches from NJH

git-svn: trunk@2245

Tomasz Kojm authored on 2006/09/13 05:55:09
Showing 10 changed files
... ...
@@ -1,3 +1,7 @@
1
+Tue Sep 12 22:52:14 CEST 2006 (tk)
2
+----------------------------------
3
+  * clamd: apply w32 patches from NJH
4
+
1 5
 Tue Sep 12 21:59:17 CEST 2006 (acab)
2 6
 ------------------------------------
3 7
   * libclamav: Merge of the related part of the phishing module from
... ...
@@ -21,17 +21,25 @@
21 21
 #include "clamav-config.h"
22 22
 #endif
23 23
 
24
+#ifdef	_MSC_VER
25
+#include <winsock.h>
26
+#endif
27
+
24 28
 #include <stdio.h>
25 29
 #include <stdlib.h>
26 30
 #include <string.h>
31
+#ifdef HAVE_UNISTD_H
27 32
 #include <unistd.h>
28 33
 #include <sys/time.h>
34
+#endif
29 35
 #include <sys/types.h>
30 36
 #include <sys/stat.h>
31 37
 #include <fcntl.h>
32 38
 #include <time.h>
39
+#ifndef	C_WINDOWS
33 40
 #include <pwd.h>
34 41
 #include <grp.h>
42
+#endif
35 43
 
36 44
 #if defined(USE_SYSLOG) && !defined(C_AIX)
37 45
 #include <syslog.h>
... ...
@@ -100,6 +108,12 @@ int main(int argc, char **argv)
100 100
 	    {0, 0, 0, 0}
101 101
     	};
102 102
 
103
+#ifdef C_WINDOWS
104
+    if(!pthread_win32_process_attach_np()) {
105
+	mprintf("!Can't start the win32 pthreads layer\n");
106
+        return 1;
107
+    }
108
+#endif
103 109
 
104 110
     opt = opt_parse(argc, argv, short_options, long_options, NULL);
105 111
     if(!opt) {
... ...
@@ -147,7 +161,7 @@ int main(int argc, char **argv)
147 147
     umask(0);
148 148
 
149 149
     /* drop privileges */
150
-#ifndef C_OS2
150
+#if (!defined(C_OS2)) && (!defined(C_WINDOWS))
151 151
     if(geteuid() == 0 && (cpt = cfgopt(copt, "User"))->enabled) {
152 152
 	if((user = getpwnam(cpt->strarg)) == NULL) {
153 153
 	    fprintf(stderr, "ERROR: Can't get information about user %s.\n", cpt->strarg);
... ...
@@ -189,6 +203,7 @@ int main(int argc, char **argv)
189 189
 	    return 1;
190 190
 	}
191 191
 
192
+
192 193
 	if(setuid(user->pw_uid)) {
193 194
 	    fprintf(stderr, "ERROR: setuid(%d) failed.\n", (int) user->pw_uid);
194 195
 	    logg("!setuid(%d) failed.\n", (int) user->pw_uid);
... ...
@@ -330,6 +345,16 @@ int main(int argc, char **argv)
330 330
     }
331 331
 
332 332
     if(tcpsock) {
333
+#ifdef C_WINDOWS
334
+	    WSADATA wsaData;
335
+
336
+	if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
337
+	    logg("!Error at WSAStartup(): %d\n", WSAGetLastError());
338
+	    logg_close();
339
+	    freecfg(copt);
340
+	    return 1;
341
+	}
342
+#endif
333 343
 	lsockets[nlsockets] = tcpserver(copt);
334 344
 	if(lsockets[nlsockets] == -1) {
335 345
 	    logg_close();
... ...
@@ -353,6 +378,18 @@ int main(int argc, char **argv)
353 353
 
354 354
     ret = acceptloop_th(lsockets, nlsockets, root, copt);
355 355
 
356
+#ifdef C_WINDOWS
357
+    if(tcpsock)
358
+	WSACleanup();
359
+
360
+    if(!pthread_win32_process_detach_np()) {
361
+	logg("!Can't stop the win32 pthreads layer\n");
362
+	logg_close();
363
+	freecfg(copt);
364
+	return 1;
365
+    }
366
+#endif
367
+
356 368
     logg_close();
357 369
     freecfg(copt);
358 370
 
... ...
@@ -38,6 +38,15 @@
38 38
 #include "server.h"
39 39
 #include "output.h"
40 40
 
41
+#ifdef        C_WINDOWS
42
+int localserver(const struct cfgstruct *copt, struct cl_node *root)
43
+{
44
+    logg("!Localserver is not supported on this platform");
45
+    return -1;
46
+}
47
+
48
+#else
49
+
41 50
 int localserver(const struct cfgstruct *copt)
42 51
 {
43 52
 	struct sockaddr_un server;
... ...
@@ -103,3 +112,4 @@ int localserver(const struct cfgstruct *copt)
103 103
 
104 104
     return sockfd;
105 105
 }
106
+#endif /* C_WINDOWS */
... ...
@@ -21,24 +21,34 @@
21 21
 #include "clamav-config.h"
22 22
 #endif
23 23
 
24
+#ifdef	_MSC_VER
25
+#include <winsock.h>
26
+#endif
27
+
24 28
 #include <stdio.h>
25 29
 #include <stdarg.h>
26 30
 #include <stdlib.h>
27 31
 #include <string.h>
32
+#ifdef	HAVE_UNISTD_H
28 33
 #include <unistd.h>
34
+#endif
29 35
 #include <fcntl.h>
30 36
 #include <time.h>
31 37
 #include <sys/stat.h>
32 38
 #include <errno.h>
39
+#ifndef	C_WINDOWS
33 40
 #include <sys/time.h>
34 41
 #include <sys/wait.h>
42
+#endif
35 43
 
36 44
 #if HAVE_SYS_PARAM_H
37 45
 #include <sys/param.h>
38 46
 #endif
39 47
 
48
+#ifndef	C_WINDOWS
40 49
 #include <sys/socket.h>
41 50
 #include <sys/ioctl.h>
51
+#endif
42 52
 
43 53
 #if HAVE_SYS_TYPES_H
44 54
 #include <sys/types.h>
... ...
@@ -77,6 +87,15 @@
77 77
 #define ENV_FILE  "CLAM_VIRUSEVENT_FILENAME"
78 78
 #define ENV_VIRUS "CLAM_VIRUSEVENT_VIRUSNAME"
79 79
 
80
+#ifdef	C_WINDOWS
81
+void virusaction(const char *filename, const char *virname, const struct cfgstruct *copt)
82
+{
83
+    if(cfgopt(copt, "VirusEvent")->enabled)
84
+	logg("^VirusEvent is not supported on this platform");	/* Yet */
85
+}
86
+
87
+#else
88
+
80 89
 void virusaction(const char *filename, const char *virname, const struct cfgstruct *copt)
81 90
 {
82 91
 	pid_t pid;
... ...
@@ -130,6 +149,7 @@ void virusaction(const char *filename, const char *virname, const struct cfgstru
130 130
 	logg("!VirusAction: fork failed.\n");
131 131
     }
132 132
 }
133
+#endif /* C_WINDOWS */
133 134
 
134 135
 int poll_fds(int *fds, int nfds, int timeout_sec)
135 136
 {
... ...
@@ -186,9 +206,11 @@ int poll_fds(int *fds, int nfds, int timeout_sec)
186 186
 	int maxfd = 0;
187 187
 
188 188
     for (i=0; i<nfds; i++) {
189
+#ifndef	C_WINDOWS
189 190
 	if (fds[i] >= DEFAULT_FD_SETSIZE) {
190 191
 	    return -1;
191 192
 	}
193
+#endif
192 194
 	if (fds[i] > maxfd)
193 195
 	    maxfd = fds[i];
194 196
     }
... ...
@@ -262,9 +284,11 @@ int is_fd_connected(int fd)
262 262
 	struct timeval tv;
263 263
 	char buff[1];
264 264
 
265
+#ifndef	C_WINDOWS
265 266
     if (fd >= DEFAULT_FD_SETSIZE) {
266 267
         return 1;
267 268
     }
269
+#endif
268 270
 
269 271
     FD_ZERO(&rfds);
270 272
     FD_SET(fd, &rfds);
... ...
@@ -309,6 +333,16 @@ int writen(int fd, void *buff, unsigned int count)
309 309
     return count;
310 310
 }
311 311
 
312
+#ifdef	C_WINDOWS
313
+/*
314
+ * The code is non-portable, please send patches to NJH
315
+ */
316
+int
317
+readsock(int sockfd, char *buf, size_t size, unsigned char delim, int timeout_sec, int force_delim, int read_command)
318
+{
319
+	return recv(sockfd, buf, size, 0);
320
+}
321
+#else
312 322
 /* FD Support Submitted by Richard Lyons <frob-clamav*webcentral.com.au> */
313 323
 /*
314 324
    This procedure does timed clamd command and delimited input processing.  
... ...
@@ -456,3 +490,4 @@ int readsock(int sockfd, char *buf, size_t size, unsigned char delim, int timeou
456 456
     }
457 457
     return n;
458 458
 }
459
+#endif
... ...
@@ -21,13 +21,20 @@
21 21
 #include "clamav-config.h"
22 22
 #endif
23 23
 
24
+#ifdef	_MSC_VER
25
+#include <winsock.h>
26
+#endif
27
+
24 28
 #include <stdio.h>
25 29
 #include <stdlib.h>
26 30
 #include <string.h>
31
+#ifdef	HAVE_UNISTD_H
27 32
 #include <unistd.h>
33
+#endif
28 34
 #include <errno.h>
29 35
 #include <sys/stat.h>
30 36
 #include <sys/types.h>
37
+#ifndef	C_WINDOWS
31 38
 #include <sys/time.h>
32 39
 #include <sys/wait.h>
33 40
 #include <sys/param.h>
... ...
@@ -36,6 +43,7 @@
36 36
 #include <netinet/in.h>
37 37
 #include <arpa/inet.h>
38 38
 #include <netdb.h>
39
+#endif
39 40
 #include <pthread.h>
40 41
 
41 42
 #if defined(HAVE_READDIR_R_3) || defined(HAVE_READDIR_R_2)
... ...
@@ -59,6 +67,10 @@
59 59
 dev_t procdev; /* /proc device */
60 60
 #endif
61 61
 
62
+#ifndef	C_WINDOWS
63
+#define	closesocket(s)	close(s)
64
+#endif
65
+
62 66
 /* Maximum filenames under various systems - njh */
63 67
 #ifndef	NAME_MAX	/* e.g. Linux */
64 68
 # ifdef	MAXNAMELEN	/* e.g. Solaris */
... ...
@@ -125,7 +137,7 @@ int dirscan(const char *dirname, const char **virname, unsigned long int *scanne
125 125
 		closedir(dd);
126 126
 		return 1;
127 127
 	    }
128
-#ifndef C_INTERIX
128
+#if	(!defined(C_INTERIX)) && (!defined(C_WINDOWS)) && (!defined(C_CYGWIN))
129 129
 	    if(dent->d_ino)
130 130
 #endif
131 131
 	    {
... ...
@@ -215,10 +227,12 @@ int scan(const char *filename, unsigned long int *scanned, const struct cl_node
215 215
     }
216 216
 
217 217
     switch(sb.st_mode & S_IFMT) {
218
+#ifdef	S_IFLNK
218 219
 	case S_IFLNK:
219 220
 	    if(!cfgopt(copt, "FollowFileSymlinks")->enabled)
220 221
 		break;
221 222
 	    /* else go to the next case */
223
+#endif
222 224
 	case S_IFREG: 
223 225
 	    if(sb.st_size == 0) { /* empty file */
224 226
 		mdprintf(odesc, "%s: Empty file\n", filename);
... ...
@@ -347,7 +361,7 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
347 347
 	    continue;
348 348
 
349 349
 	if(bind(sockfd, (struct sockaddr *) &server, sizeof(struct sockaddr_in)) == -1)
350
-	    close(sockfd);
350
+	    closesocket(sockfd);
351 351
 	else
352 352
 	    bound = 1;
353 353
     }
... ...
@@ -406,15 +420,15 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
406 406
     btread = sizeof(buff);
407 407
 
408 408
     while((retval = poll_fd(acceptd, timeout)) == 1) {
409
-	bread = read(acceptd, buff, btread);
409
+	bread = recv(acceptd, buff, btread, 0);
410 410
 	if(bread <= 0)
411 411
 	    break;
412 412
 	size += bread;
413 413
 
414 414
 	if(writen(tmpd, buff, bread) != bread) {
415 415
 	    shutdown(sockfd, 2);
416
-	    close(sockfd);
417
-	    close(acceptd);
416
+	    closesocket(sockfd);
417
+	    closesocket(acceptd);
418 418
 	    mdprintf(odesc, "Temporary file -> write ERROR\n");
419 419
 	    logg("!ScanStream %d: Can't write to temporary file.\n", port);
420 420
 	    close(tmpd);
... ...
@@ -456,8 +470,8 @@ int scanstream(int odesc, unsigned long int *scanned, const struct cl_node *root
456 456
 	unlink(tmpname);
457 457
     free(tmpname);
458 458
 
459
-    close(acceptd);
460
-    close(sockfd);
459
+    closesocket(acceptd);
460
+    closesocket(sockfd);
461 461
 
462 462
     if(ret == CL_VIRUS) {
463 463
 	mdprintf(odesc, "stream: %s FOUND\n", virname);
... ...
@@ -22,6 +22,10 @@
22 22
 #include "clamav-config.h"
23 23
 #endif
24 24
 
25
+#ifdef	_MSC_VER
26
+#include <winsock.h>
27
+#endif
28
+
25 29
 #include <pthread.h>
26 30
 #include <errno.h>
27 31
 #include <signal.h>
... ...
@@ -29,8 +33,12 @@
29 29
 #include <string.h>
30 30
 #include <time.h>
31 31
 #include <sys/types.h>
32
+#ifndef	C_WINDOWS
32 33
 #include <sys/socket.h>
34
+#endif
35
+#ifdef	HAVE_UNISTD_H
33 36
 #include <unistd.h>
37
+#endif
34 38
 
35 39
 #include "libclamav/clamav.h"
36 40
 
... ...
@@ -44,9 +52,17 @@
44 44
 #include "others.h"
45 45
 #include "shared.h"
46 46
 
47
+#ifndef	C_WINDOWS
48
+#define	closesocket(s)	close(s)
49
+#endif
50
+
47 51
 #define BUFFSIZE 1024
52
+#ifndef	FALSE
48 53
 #define FALSE (0)
54
+#endif
55
+#ifndef	TRUE
49 56
 #define TRUE (1)
57
+#endif
50 58
 
51 59
 int progexit = 0;
52 60
 pthread_mutex_t exit_mutex;
... ...
@@ -69,13 +85,17 @@ typedef struct client_conn_tag {
69 69
 void scanner_thread(void *arg)
70 70
 {
71 71
 	client_conn_t *conn = (client_conn_t *) arg;
72
+#ifndef	C_WINDOWS
72 73
 	sigset_t sigset;
74
+#endif
73 75
 	int ret, timeout, i, session=FALSE;
74 76
 
75 77
 
78
+#ifndef	C_WINDOWS
76 79
     /* ignore all signals */
77 80
     sigfillset(&sigset);
78 81
     pthread_sigmask(SIG_SETMASK, &sigset, NULL);
82
+#endif
79 83
 
80 84
     timeout = cfgopt(conn->copt, "ReadTimeout")->numarg;
81 85
     if(!timeout)
... ...
@@ -93,7 +113,7 @@ void scanner_thread(void *arg)
93 93
 		progexit = 1;
94 94
 		for(i = 0; i < conn->nsockets; i++) {
95 95
 		    shutdown(conn->socketds[i], 2);
96
-		    close(conn->socketds[i]);
96
+		    closesocket(conn->socketds[i]);
97 97
 		}
98 98
 		pthread_mutex_unlock(&exit_mutex);
99 99
 		break;
... ...
@@ -126,7 +146,7 @@ void scanner_thread(void *arg)
126 126
 	}
127 127
     } while (session);
128 128
 
129
-    close(conn->sd);
129
+    closesocket(conn->sd);
130 130
     cl_free(conn->root);
131 131
     free(conn);
132 132
     return;
... ...
@@ -145,13 +165,17 @@ void sighandler_th(int sig)
145 145
 	    _exit(11); /* probably not reached at all */
146 146
 	    break; /* not reached */
147 147
 
148
+#ifdef	SIGHUP
148 149
 	case SIGHUP:
149 150
 	    sighup = 1;
150 151
 	    break;
152
+#endif
151 153
 
154
+#ifdef	SIGUSR2
152 155
 	case SIGUSR2:
153 156
 	    reload = 1;
154 157
 	    break;
158
+#endif
155 159
 
156 160
 	default:
157 161
 	    break; /* Take no action on other signals - e.g. SIGPIPE */
... ...
@@ -229,11 +253,15 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, const struc
229 229
 	int new_sd, max_threads, i;
230 230
 	unsigned int options = 0;
231 231
 	threadpool_t *thr_pool;
232
+#ifndef	C_WINDOWS
232 233
 	struct sigaction sigact;
234
+#endif
233 235
 	mode_t old_umask;
234 236
 	struct cl_limits limits;
235 237
 	pthread_attr_t thattr;
238
+#ifndef	C_WINDOWS
236 239
 	sigset_t sigset;
240
+#endif
237 241
 	client_conn_t *client_conn;
238 242
 	struct cfgstruct *cpt;
239 243
 #ifdef HAVE_STRERROR_R
... ...
@@ -253,7 +281,10 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, const struc
253 253
 	pthread_attr_t clamuko_attr;
254 254
 	struct thrarg *tharg = NULL; /* shut up gcc */
255 255
 #endif
256
+
257
+#ifndef	C_WINDOWS
256 258
 	memset(&sigact, 0, sizeof(struct sigaction));
259
+#endif
257 260
 
258 261
     /* save the PID */
259 262
     mainpid = getpid();
... ...
@@ -402,6 +433,7 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, const struc
402 402
 	logg("Clamuko is not available.\n");
403 403
 #endif
404 404
 
405
+#ifndef	C_WINDOWS
405 406
     /* set up signal handling */
406 407
     sigfillset(&sigset);
407 408
     sigdelset(&sigset, SIGINT);
... ...
@@ -430,6 +462,7 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, const struc
430 430
 	sigaddset(&sigact.sa_mask, SIGHUP);
431 431
 	sigaction(SIGSEGV, &sigact, NULL);
432 432
     }
433
+#endif
433 434
 
434 435
 #if defined(C_BIGSTACK) || defined(C_BSD)
435 436
     /*
... ...
@@ -565,7 +598,7 @@ int acceptloop_th(int *socketds, int nsockets, struct cl_node *root, const struc
565 565
 	shutdown(socketds[i], 2);
566 566
     logg("*Closing the main socket%s.\n", (nsockets > 1) ? "s" : "");
567 567
     for (i = 0; i < nsockets; i++)
568
-	close(socketds[i]);
568
+	closesocket(socketds[i]);
569 569
 #ifndef C_OS2
570 570
     if((cpt = cfgopt(copt, "LocalSocket"))->enabled) {
571 571
 	if(unlink(cpt->strarg) == -1)
... ...
@@ -21,18 +21,27 @@
21 21
 #include "clamav-config.h"
22 22
 #endif
23 23
 
24
+#ifdef	_MSC_VER
25
+#include <winsock.h>
26
+#endif
27
+
24 28
 #include <stdio.h>
25 29
 #include <stdlib.h>
26 30
 #include <string.h>
31
+#ifdef	HAVE_UNISTD_H
27 32
 #include <unistd.h>
33
+#endif
28 34
 #include <sys/types.h>
35
+#ifndef	C_WINDOWS
29 36
 #include <dirent.h>
30 37
 #include <sys/socket.h>
31 38
 #include <sys/time.h>
39
+#endif
32 40
 #include <pthread.h>
33 41
 #include <time.h>
34 42
 #include <signal.h>
35 43
 #include <errno.h>
44
+#include <stddef.h>
36 45
 
37 46
 #include "libclamav/clamav.h"
38 47
 #include "libclamav/str.h"
... ...
@@ -65,13 +74,17 @@ void multiscanfile(void *arg)
65 65
 {
66 66
 	struct multi_tag *tag = (struct multi_tag *) arg;
67 67
 	const char *virname;
68
+#ifndef	C_WINDOWS
68 69
         sigset_t sigset;
70
+#endif
69 71
 	int ret;
70 72
 
71 73
 
74
+#ifndef	C_WINDOWS
72 75
     /* ignore all signals */
73 76
     sigfillset(&sigset);
74 77
     pthread_sigmask(SIG_SETMASK, &sigset, NULL);
78
+#endif
75 79
 
76 80
     ret = cl_scanfile(tag->fname, &virname, NULL, tag->root, tag->limits, tag->options);
77 81
 
... ...
@@ -136,7 +149,7 @@ static int multiscan(const char *dirname, const struct cl_node *root, const stru
136 136
 		return -1;
137 137
 	    }
138 138
 
139
-#ifndef C_INTERIX
139
+#if	(!defined(C_INTERIX)) && (!defined(C_WINDOWS)) && (!defined(C_CYGWIN))
140 140
 	    if(dent->d_ino)
141 141
 #endif
142 142
 	    {
... ...
@@ -189,8 +202,10 @@ static int multiscan(const char *dirname, const struct cl_node *root, const stru
189 189
 					return -1;
190 190
 				    }
191 191
 
192
+#ifndef C_WINDOWS
192 193
 				    while(!multi_pool->thr_idle) /* non-critical */
193 194
 					usleep(200);
195
+#endif
194 196
 				}
195 197
 			    }
196 198
 			}
... ...
@@ -21,14 +21,22 @@
21 21
 #include "clamav-config.h"
22 22
 #endif
23 23
 
24
+#ifdef	_MSC_VER
25
+#include <winsock.h>
26
+#endif
27
+
24 28
 #include <stdio.h>
25 29
 #include <string.h>
26 30
 #include <sys/types.h>
31
+#ifndef	C_WINDOWS
27 32
 #include <sys/socket.h>
28 33
 #include <netinet/in.h>
29 34
 #include <arpa/inet.h>
35
+#endif
30 36
 #include <errno.h>
37
+#ifndef	C_WINDOWS
31 38
 #include <netdb.h>
39
+#endif
32 40
 
33 41
 #include "libclamav/clamav.h"
34 42
 
... ...
@@ -17,6 +17,10 @@
17 17
  *  MA 02110-1301, USA.
18 18
  */
19 19
 
20
+#if HAVE_CONFIG_H
21
+#include "clamav-config.h"
22
+#endif
23
+
20 24
 #include <stdio.h>
21 25
 #include <pthread.h>
22 26
 #include <time.h>
... ...
@@ -21,7 +21,10 @@
21 21
 #define __THRMGR_H__
22 22
 
23 23
 #include <pthread.h>
24
+
25
+#ifndef C_WINDOWS
24 26
 #include <sys/time.h>
27
+#endif
25 28
 
26 29
 typedef struct work_item_tag {
27 30
 	struct work_item_tag *next;