Browse code

w32 patch

git-svn: trunk@2316

Tomasz Kojm authored on 2006/09/27 23:06:28
Showing 2 changed files
... ...
@@ -1,3 +1,7 @@
1
+Wed Sep 27 16:05:31 CEST 2006 (tk)
2
+----------------------------------
3
+  * freshclam/freshclam.c: apply w32 patch from NJH
4
+
1 5
 Wed Sep 27 12:48:06 CEST 2006 (tk)
2 6
 ----------------------------------
3 7
   * shared/misc.c: apply w32 patch from NJH
... ...
@@ -16,7 +16,9 @@
16 16
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17 17
  *  MA 02110-1301, USA.
18 18
  */
19
-
19
+#ifdef	_MSC_VER
20
+#include <winsock.h>
21
+#endif
20 22
 
21 23
 #if HAVE_CONFIG_H
22 24
 #include "clamav-config.h"
... ...
@@ -24,17 +26,23 @@
24 24
 
25 25
 #include <stdio.h>
26 26
 #include <stdlib.h>
27
+#ifdef	HAVE_UNISTD_H
27 28
 #include <unistd.h>
29
+#endif
28 30
 #include <string.h>
29 31
 #include <errno.h>
30 32
 #include <signal.h>
31 33
 #include <time.h>
32 34
 #include <sys/types.h>
35
+#ifndef	C_WINDOWS
33 36
 #include <sys/wait.h>
37
+#endif
34 38
 #include <sys/stat.h>
35 39
 #include <fcntl.h>
40
+#ifndef	C_WINDOWS
36 41
 #include <pwd.h>
37 42
 #include <grp.h>
43
+#endif
38 44
 
39 45
 #if defined(USE_SYSLOG) && !defined(C_AIX)
40 46
 #include <syslog.h>
... ...
@@ -58,19 +66,29 @@ static short foreground = 1;
58 58
 static void daemon_sighandler(int sig) {
59 59
 
60 60
     switch(sig) {
61
+#ifdef	SIGCHLD
61 62
 	case SIGCHLD:
62 63
 	    waitpid(-1, NULL, WNOHANG);
63 64
 	    active_children--;
64 65
 	    break;
66
+#endif
65 67
 
68
+#ifdef	SIGALRM
66 69
 	case SIGALRM:
70
+		terminate = -1;
71
+	    break;
72
+#endif
73
+#ifdef	SIGUSR1
67 74
 	case SIGUSR1:
68
-	    terminate = -1;
75
+		terminate = -1;
69 76
 	    break;
77
+#endif
70 78
 
79
+#ifdef	SIGHUP
71 80
 	case SIGHUP:
72 81
 	    terminate = -2;
73 82
 	    break;
83
+#endif
74 84
 
75 85
 	default:
76 86
 	    terminate = 1;
... ...
@@ -84,7 +102,6 @@ static void writepid(char *pidfile)
84 84
 {
85 85
 	FILE *fd;
86 86
 	int old_umask;
87
-
88 87
     old_umask = umask(0006);
89 88
     if((fd = fopen(pidfile, "w")) == NULL) {
90 89
 	logg("!Can't save PID to file %s: %s\n", pidfile, strerror(errno));
... ...
@@ -177,8 +194,11 @@ int main(int argc, char **argv)
177 177
 	char *newdir, *cfgfile;
178 178
 	char *pidfile = NULL;
179 179
 	struct cfgstruct *copt, *cpt;
180
-	struct sigaction sigact, oldact;
181
-#if !defined(C_CYGWIN)  && !defined(C_OS2)
180
+#ifndef	C_WINDOWS
181
+	struct sigaction sigact;
182
+	struct sigaction oldact;
183
+#endif
184
+#if !defined(C_CYGWIN)  && !defined(C_OS2) && !defined(C_WINDOWS)
182 185
 	char *unpuser;
183 186
 	struct passwd *user;
184 187
 #endif
... ...
@@ -245,6 +265,13 @@ int main(int argc, char **argv)
245 245
 	return 56;
246 246
     }
247 247
 
248
+#ifdef C_WINDOWS
249
+    if(!pthread_win32_process_attach_np()) {
250
+	mprintf("!Can't start the win32 pthreads layer\n");
251
+	return 63;
252
+    }
253
+#endif
254
+
248 255
     if(opt_check(opt, "http-proxy") || opt_check(opt, "proxy-user"))
249 256
 	logg("WARNING: Proxy settings are now only configurable in the config file.\n");
250 257
 
... ...
@@ -254,7 +281,8 @@ int main(int argc, char **argv)
254 254
 	    opt_free(opt);
255 255
 	    return 56;
256 256
 	}
257
-#ifndef C_CYGWIN
257
+
258
+#if !defined(C_CYGWIN) && !defined(C_WINDOWS)
258 259
 	if(statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IWOTH | S_IXOTH)) {
259 260
 	    logg("^Insecure permissions (for HTTPProxyPassword): %s must have no more than 0700 permissions.\n", cfgfile);
260 261
 	    opt_free(opt);
... ...
@@ -263,7 +291,7 @@ int main(int argc, char **argv)
263 263
 #endif
264 264
     }
265 265
 
266
-#if !defined(C_CYGWIN)  && !defined(C_OS2)
266
+#if !defined(C_CYGWIN)  && !defined(C_OS2) && !defined(C_WINDOWS)
267 267
     /* freshclam shouldn't work with root privileges */
268 268
     if(opt_check(opt, "user"))
269 269
 	unpuser = opt_arg(opt, "user");
... ...
@@ -375,12 +403,25 @@ int main(int argc, char **argv)
375 375
     } else
376 376
 	logg("*Current working dir is %s\n", newdir);
377 377
 
378
+#ifdef	C_WINDOWS
379
+    {
380
+	    WSADATA wsaData;
381
+
382
+	if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) {
383
+	    logg("!Error at WSAStartup(): %d\n", WSAGetLastError());
384
+	    return 1;
385
+	}
386
+    }
387
+#endif
388
+
378 389
     if(opt_check(opt, "daemon")) {
379 390
 	    int bigsleep, checks;
391
+#ifndef	C_WINDOWS
380 392
 	    time_t now, wakeup;
381 393
 
382 394
 	memset(&sigact, 0, sizeof(struct sigaction));
383 395
 	sigact.sa_handler = daemon_sighandler;
396
+#endif
384 397
 
385 398
 	if(opt_check(opt, "checks"))
386 399
 	    checks = atoi(opt_arg(opt, "checks"));
... ...
@@ -422,10 +463,15 @@ int main(int argc, char **argv)
422 422
 
423 423
 	logg("#freshclam daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
424 424
 
425
+#ifdef	C_WINDOWS
426
+	signal(SIGINT, daemon_sighandler);
427
+	terminate = 0;
428
+#else
425 429
 	sigaction(SIGTERM, &sigact, NULL);
426 430
 	sigaction(SIGHUP, &sigact, NULL);
427 431
 	sigaction(SIGINT, &sigact, NULL);
428 432
         sigaction(SIGCHLD, &sigact, NULL);
433
+#endif
429 434
 
430 435
 	while(!terminate) {
431 436
 	    ret = download(copt, opt);
... ...
@@ -443,8 +489,16 @@ int main(int argc, char **argv)
443 443
 	    }
444 444
 
445 445
 	    logg("#--------------------------------------\n");
446
+#ifdef	SIGALRM
446 447
 	    sigaction(SIGALRM, &sigact, &oldact);
448
+#endif
449
+#ifdef	SIGUSR1
447 450
 	    sigaction(SIGUSR1, &sigact, &oldact);
451
+#endif
452
+
453
+#ifdef	C_WINDOWS
454
+	    sleep(bigsleep);
455
+#else   
448 456
 	    time(&wakeup);
449 457
 	    wakeup += bigsleep;
450 458
 	    alarm(bigsleep);
... ...
@@ -461,9 +515,14 @@ int main(int argc, char **argv)
461 461
 		terminate = 0;
462 462
 		logg_close();
463 463
 	    }
464
+#endif
464 465
 
466
+#ifdef	SIGALRM
465 467
 	    sigaction(SIGALRM, &oldact, NULL);
468
+#endif
469
+#ifdef	SIGUSR1
466 470
 	    sigaction(SIGUSR1, &oldact, NULL);
471
+#endif	    
467 472
 	}
468 473
 
469 474
     } else
... ...
@@ -482,5 +541,15 @@ int main(int argc, char **argv)
482 482
     }
483 483
 
484 484
     opt_free(opt);
485
+
486
+#ifdef C_WINDOWS
487
+    WSACleanup();
488
+
489
+    if(!pthread_win32_process_detach_np()) {
490
+	mprintf("!Can't stop the win32 pthreads layer\n");
491
+	return 63;
492
+    }
493
+#endif
494
+
485 495
     return(ret);
486 496
 }