Browse code

Fixed a fatal race condition. Re-open log file on SIGHUP.

git-svn: trunk@36

Tomasz Kojm authored on 2003/09/15 03:55:56
Showing 2 changed files
... ...
@@ -50,6 +50,7 @@
50 50
 #define CMD6 "CONTSCAN"
51 51
 #define CMD7 "VERSION"
52 52
 #define CMD8 "STREAM"
53
+#define CMD9 "STREAM2"
53 54
 
54 55
 #ifdef CLAMUKO
55 56
 pthread_t clamukoid;
... ...
@@ -117,7 +118,9 @@ void *threadscanner(void *arg)
117 117
     } else if(!strncmp(buff, CMD8, strlen(CMD8))) { /* STREAM */
118 118
 	scanstream(ths[tharg->sid].desc, NULL, tharg->root, tharg->limits, tharg->options, tharg->copt);
119 119
     }
120
-
120
+    /* else if(!strncmp(buff, CMD9, strlen(CMD9))) {
121
+	scanstream2(ths[tharg->sid].desc, NULL, tharg->root, tharg->limits, tharg->options, tharg->copt);
122
+    }*/
121 123
 
122 124
     THREXIT;
123 125
 }
... ...
@@ -363,7 +366,7 @@ int acceptloop(int socketd, struct cl_node *root, const struct cfgstruct *copt)
363 363
 	struct thrwarg thwarg;
364 364
 	struct cl_limits limits;
365 365
 	pthread_attr_t thattr;
366
-	struct sigaction sigact, sigsegvact;
366
+	struct sigaction sigact;
367 367
 	sigset_t sigset;
368 368
 	mode_t old_umask;
369 369
 
... ...
@@ -496,22 +499,19 @@ int acceptloop(int socketd, struct cl_node *root, const struct cfgstruct *copt)
496 496
     sigdelset(&sigset, SIGINT);
497 497
     sigdelset(&sigset, SIGTERM);
498 498
     sigdelset(&sigset, SIGSEGV);
499
+    sigdelset(&sigset, SIGHUP);
499 500
     sigprocmask(SIG_SETMASK, &sigset, NULL);
500 501
 
501
-    /* Initialize sication struct for valgrind's sake */
502
-    memset(&sigact, 0, sizeof(struct sigaction));
503
-
504 502
     /* SIGINT, SIGTERM, SIGSEGV */
505
-    sigact.sa_handler = sigexit;
506
-    sigsegvact.sa_handler = sigsegv;
503
+    sigact.sa_handler = sighandler;
507 504
     sigemptyset(&sigact.sa_mask);
508
-    sigemptyset(&sigsegvact.sa_mask);
509 505
     sigaddset(&sigact.sa_mask, SIGINT);
510 506
     sigaddset(&sigact.sa_mask, SIGTERM);
511
-    sigaddset(&sigsegvact.sa_mask, SIGSEGV);
507
+    sigaddset(&sigact.sa_mask, SIGHUP);
512 508
     sigaction(SIGINT, &sigact, NULL);
513 509
     sigaction(SIGTERM, &sigact, NULL);
514
-    sigaction(SIGSEGV, &sigsegvact, NULL);
510
+    sigaction(SIGSEGV, &sigact, NULL);
511
+    sigaction(SIGHUP, &sigact, NULL);
515 512
 
516 513
     /* we need to save program's PID, because under Linux each thread
517 514
      * has another PID, it works with other OSes as well
... ...
@@ -577,41 +577,48 @@ int acceptloop(int socketd, struct cl_node *root, const struct cfgstruct *copt)
577 577
 	//pthread_mutex_lock(&ths[i].mutex);
578 578
 	ths[i].desc = acceptd;
579 579
 	ths[i].reload = 0;
580
+	ths[i].active = 1;
580 581
 	pthread_create(&ths[i].id, &thattr, threadscanner, tharg);
581 582
 	ths[i].start = time(NULL);
582
-	ths[i].active = 1;
583 583
     }
584 584
 }
585 585
 
586
-void sigexit(int sig)
586
+void sighandler(int sig)
587 587
 {
588 588
 	time_t currtime;
589
-	int maxwait = CL_DEFAULT_MAXWHILEWAIT * 5;
589
+	int maxwait = CL_DEFAULT_MAXWHILEWAIT * 5, i;
590 590
 
591
-    progexit = 1;
592
-    logg("*Signal %d caught -> exiting.\n", sig);
591
+    switch(sig) {
592
+	case SIGINT:
593
+	case SIGTERM:
594
+	    progexit = 1;
595
+	    logg("*Signal %d caught -> exiting.\n", sig);
593 596
 
594
-    while(progexit != 2 && maxwait--)
595
-	usleep(200000);
597
+	    while(progexit != 2 && maxwait--)
598
+		usleep(200000);
596 599
 
597
-    if(!maxwait && progexit != 2)
598
-	logg("!Critical error: Cannot reach exit level 2.\n");
600
+	    if(!maxwait && progexit != 2)
601
+		logg("!Critical error: Cannot reach exit level 2.\n");
599 602
 
600
-    time(&currtime);
601
-    logg("--- Stopped at %s", ctime(&currtime));
602
-    exit(0);
603
-}
603
+	    time(&currtime);
604
+	    logg("--- Stopped at %s", ctime(&currtime));
605
+	    exit(0);
606
+	    break; /* not reached */
604 607
 
605
-void sigsegv(int sig)
606
-{
607
-	int i;
608
+	case SIGSEGV:
609
+	    logg("Segmentation fault :-( Bye..\n");
608 610
 
609
-    logg("Segmentation fault :-( Bye..\n");
611
+	    for(i = 0; i < threads; i++)
612
+		if(ths[i].active)
613
+		    pthread_kill(ths[i].id, 9);
610 614
 
611
-    for(i = 0; i < threads; i++)
612
-	if(ths[i].active)
613
-	    pthread_kill(ths[i].id, 9);
615
+	    pthread_kill(watcherid, 9);
616
+	    exit(11); /* probably not reached at all */
617
+	    break; /* not reached */
614 618
 
615
-    pthread_kill(watcherid, 9);
616
-    exit(11); /* probably not reached at all */
619
+	case SIGHUP:
620
+	    sighup = 1;
621
+	    logg("SIGHUP catched: log file re-opened.\n");
622
+	    break;
623
+    }
617 624
 }
... ...
@@ -52,7 +52,7 @@ int progpid; /* clamd pid */
52 52
 short int reload, clamuko_reload;
53 53
 
54 54
 int acceptloop(int socketd, struct cl_node *root, const struct cfgstruct *copt);
55
-void sigexit(int sig);
55
+void sighandler(int sig);
56 56
 void daemonize(void);
57 57
 void sigsegv(int sig);
58 58