Browse code

Added Signal Handling

git-svn: trunk@3364

Nigel Horne authored on 2007/11/08 06:36:10
Showing 2 changed files
... ...
@@ -1,3 +1,9 @@
1
+Wed Nov  7 20:37:27 GMT 2007 (njh)
2
+----------------------------------
3
+  * clamav-milter:	Added signal handling: SIGUSR2 to reload database
4
+				(when not external mode); SIGHUP to reopen
5
+				the logfile (to aid logrotate)
6
+
1 7
 Wed Nov  7 13:06:54 GMT 2007 (njh)
2 8
 ----------------------------------
3 9
   * libclamav/blob.c:	VBS.Redlof-A was not being detected under Windows
... ...
@@ -33,7 +33,7 @@
33 33
  */
34 34
 static	char	const	rcsid[] = "$Id: clamav-milter.c,v 1.312 2007/02/12 22:24:21 njh Exp $";
35 35
 
36
-#define	CM_VERSION	"devel-20081024"
36
+#define	CM_VERSION	"devel-20071107"
37 37
 
38 38
 #if HAVE_CONFIG_H
39 39
 #include "clamav-config.h"
... ...
@@ -516,6 +516,7 @@ static	pthread_cond_t	watchdog_cond = PTHREAD_COND_INITIALIZER;
516 516
 static	const	char	*postmaster = "postmaster";
517 517
 static	const	char	*from = "MAILER-DAEMON";
518 518
 static	int	quitting;
519
+static	int	reload;	/* reload database when SIGUSR2 is received */
519 520
 static	const	char	*report;	/* Report Phishing to this address */
520 521
 static	const	char	*report_fps;	/* Report Phish FPs to this address */
521 522
 
... ...
@@ -552,6 +553,8 @@ static	pthread_mutex_t	blacklist_mutex = PTHREAD_MUTEX_INITIALIZER;
552 552
 #endif
553 553
 
554 554
 static	void	sigsegv(int sig);
555
+static	void	sighup(int sig);
556
+static	void	sigusr2(int sig);
555 557
 
556 558
 #ifdef HAVE_BACKTRACE
557 559
 #include <execinfo.h>
... ...
@@ -1790,8 +1793,6 @@ main(int argc, char **argv)
1790 1790
 		close(0);
1791 1791
 		open("/dev/null", O_RDONLY);
1792 1792
 
1793
-		close(1);
1794
-
1795 1793
 		/* initialize logger */
1796 1794
 		logg_lock = cfgopt(copt, "LogFileUnlock")->enabled;
1797 1795
 		logg_time = cfgopt(copt, "LogTime")->enabled;
... ...
@@ -1800,7 +1801,7 @@ main(int argc, char **argv)
1800 1800
 		logg_verbose = mprintf_verbose = cfgopt(copt, "LogVerbose")->enabled;
1801 1801
 
1802 1802
 		if(cfgopt(copt, "Debug")->enabled) /* enable debug messages in libclamav */
1803
-		cl_debug();
1803
+			cl_debug();
1804 1804
 
1805 1805
 		if((cpt = cfgopt(copt, "LogFile"))->enabled) {
1806 1806
 			time_t currtime;
... ...
@@ -1814,6 +1815,7 @@ main(int argc, char **argv)
1814 1814
 				return 1;
1815 1815
 			}
1816 1816
 			time(&currtime);
1817
+			close(1);
1817 1818
 			if(logg("#ClamAV-milter started at %s", ctime(&currtime))) {
1818 1819
 				fprintf(stderr, "ERROR: Problem with internal logger. Please check the permissions on the %s file.\n", logg_file);
1819 1820
 				logg_close();
... ...
@@ -1822,6 +1824,7 @@ main(int argc, char **argv)
1822 1822
 			}
1823 1823
 		} else {
1824 1824
 #ifdef	CL_DEBUG
1825
+			close(1);
1825 1826
 			logg_file = console;
1826 1827
 			if(consolefd < 0) {
1827 1828
 				perror(console);
... ...
@@ -2096,6 +2099,9 @@ main(int argc, char **argv)
2096 2096
 #endif
2097 2097
 
2098 2098
 	(void)signal(SIGSEGV, sigsegv);
2099
+	(void)signal(SIGHUP, sighup);
2100
+	if(!external)
2101
+		(void)signal(SIGUSR2, sigusr2);
2099 2102
 
2100 2103
 	return smfi_main();
2101 2104
 }
... ...
@@ -3435,6 +3441,16 @@ clamfi_eom(SMFICTX *ctx)
3435 3435
 #else
3436 3436
 			char *hostname = cli_strtok(serverHostNames, privdata->serverNumber, ":");
3437 3437
 #endif
3438
+			if(privdata->subject)
3439
+				logg(_("clamfi_eom: read nothing from clamd on %s, from %s (%s)"),
3440
+					hostname, privdata->from, privdata->subject);
3441
+			else
3442
+				logg(_("clamfi_eom: read nothing from clamd on %s, from %s"),
3443
+					hostname, privdata->from);
3444
+
3445
+#ifndef	MAXHOSTNAMELEN
3446
+			free(hostname);
3447
+#endif
3438 3448
 			/*
3439 3449
 			 * TODO: if more than one host has been specified, try
3440 3450
 			 * another one - setting cl_error to SMFIS_TEMPFAIL
... ...
@@ -3442,8 +3458,6 @@ clamfi_eom(SMFICTX *ctx)
3442 3442
 			 */
3443 3443
 			clamfi_cleanup(ctx);
3444 3444
 
3445
-			logg(_("clamfi_eom: read nothing from clamd on %s\n"), hostname);
3446
-
3447 3445
 #ifdef	SESSION
3448 3446
 			pthread_mutex_lock(&sstatus_mutex);
3449 3447
 			session->status = CMDSOCKET_DOWN;
... ...
@@ -5393,6 +5407,7 @@ watchdog(void *a)
5393 5393
 				perror("pthread_cond_timedwait");
5394 5394
 		}
5395 5395
 		pthread_mutex_unlock(&watchdog_mutex);
5396
+
5396 5397
 		cli_dbgmsg("watchdog wakes\n");
5397 5398
 
5398 5399
 		if(check_and_reload_database() != 0) {
... ...
@@ -5519,6 +5534,7 @@ watchdog(void *a)
5519 5519
 		ts.tv_sec = tp.tv_sec + freshclam_monitor;
5520 5520
 		ts.tv_nsec = tp.tv_usec * 1000;
5521 5521
 		cli_dbgmsg("watchdog sleeps\n");
5522
+
5522 5523
 		pthread_mutex_lock(&watchdog_mutex);
5523 5524
 		/*
5524 5525
 		 * Sometimes this returns EPIPE which isn't listed as a
... ...
@@ -5572,7 +5588,13 @@ check_and_reload_database(void)
5572 5572
 	if(external)
5573 5573
 		return 0;
5574 5574
 
5575
-	switch(rc = cl_statchkdir(&dbstat)) {
5575
+	if(reload) {
5576
+		rc = 1;
5577
+		reload = 0;
5578
+	} else
5579
+		rc = cl_statchkdir(&dbstat);
5580
+
5581
+	switch(rc) {
5576 5582
 		case 1:
5577 5583
 			logg("^Database has changed, loading updated database\n");
5578 5584
 			cl_statfree(&dbstat);
... ...
@@ -5831,9 +5853,32 @@ sigsegv(int sig)
5831 5831
 
5832 5832
 	logg("!Segmentation fault :-( Bye..\n");
5833 5833
 
5834
+	quitting++;
5834 5835
 	smfi_stop();
5835 5836
 }
5836 5837
 
5838
+static void
5839
+sighup(int sig)
5840
+{
5841
+	const struct cfgstruct *cpt;
5842
+
5843
+	signal(SIGHUP, sighup);
5844
+
5845
+	logg("SIGHUP caught: re-opening log file\n");
5846
+	logg_close();
5847
+	if(!logg_file && (cpt = cfgopt(copt, "LogFile"))->enabled)
5848
+		logg_file = cpt->strarg;
5849
+}
5850
+
5851
+static void
5852
+sigusr2(int sig)
5853
+{
5854
+	signal(SIGUSR2, sighup);
5855
+
5856
+	logg("^SIGUSR2 caught: scheduling database reload\n");
5857
+	reload++;
5858
+}
5859
+
5837 5860
 #ifdef HAVE_BACKTRACE
5838 5861
 static void
5839 5862
 print_trace(void)