Browse code

Better start up of the logg() inferface

git-svn: trunk@2947

Nigel Horne authored on 2007/03/17 02:41:06
Showing 2 changed files
... ...
@@ -1,3 +1,8 @@
1
+Fri Mar 16 15:47:08 GMT 2007 (njh)
2
+----------------------------------
3
+  * clamav-milter:	Better start up of the logg() inferface, now similar
4
+				to that used in clamd
5
+
1 6
 Mon Mar 14 19:29:00 EET 2007 (edwin)
2 7
 ---------------------------------
3 8
   * libclamav/htmlnorm.c,entconv.c: leave <0x20 characters untouched in
... ...
@@ -20,7 +25,7 @@ Sun Mar 11 17:53:54 GMT 2007 (njh)
20 20
 Sun Mar 11 11:21:00 EET 2007 (edwin)
21 21
 ----------------------------------
22 22
   * libclamav/hashtab.[ch],phish_*,regex_list.[ch]: #include cleanup, Code
23
-  cleanup, remove commented-out code, fix some gcc -W* warnings 
23
+  cleanup, remove commented-out code, fix some gcc -W* warnings
24 24
 
25 25
 Sun Mar 11 00:02:12 GMT 2007 (njh)
26 26
 ----------------------------------
... ...
@@ -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-230207"
36
+#define	CM_VERSION	"devel-230316"
37 37
 
38 38
 #if HAVE_CONFIG_H
39 39
 #include "clamav-config.h"
... ...
@@ -137,8 +137,7 @@ int	allow_severity = LOG_DEBUG;
137 137
 int	deny_severity = LOG_NOTICE;
138 138
 #endif
139 139
 
140
-#ifndef	CL_DEBUG
141
-static	const	char	*logFile;
140
+#ifdef	CL_DEBUG
142 141
 static	char	console[] = "/dev/console";
143 142
 #endif
144 143
 
... ...
@@ -403,7 +402,7 @@ static	int	readTimeout = DEFAULT_TIMEOUT; /*
403 403
 				 * respond, see ReadTimeout in clamd.conf
404 404
 				 */
405 405
 static	long	streamMaxLength = -1;	/* StreamMaxLength from clamd.conf */
406
-static	int	logClean = 0;	/*
406
+static	int	logok = 0;	/*
407 407
 				 * Add clean items to the log file
408 408
 				 */
409 409
 static	char	*signature = N_("-- \nScanned by ClamAv - http://www.clamav.net\n");
... ...
@@ -604,7 +603,7 @@ main(int argc, char **argv)
604 604
 	const struct cfgstruct *cpt;
605 605
 	char version[VERSION_LENGTH + 1];
606 606
 	pthread_t tid;
607
-#ifndef	CL_DEBUG
607
+#ifdef	CL_DEBUG
608 608
 	int consolefd;
609 609
 #endif
610 610
 
... ...
@@ -1088,7 +1087,7 @@ main(int argc, char **argv)
1088 1088
 	/*
1089 1089
 	 * Drop privileges
1090 1090
 	 */
1091
-#ifndef	CL_DEBUG
1091
+#ifdef	CL_DEBUG
1092 1092
 	/* Save the fd for later, open while we can */
1093 1093
 	consolefd = open(console, O_WRONLY);
1094 1094
 #endif
... ...
@@ -1398,7 +1397,7 @@ main(int argc, char **argv)
1398 1398
 		}
1399 1399
 		if(send(sessions[0].sock, "SESSION\n", 7, 0) < 7) {
1400 1400
 			perror("send");
1401
-			logg(_("!Can't create a clamd session"));
1401
+			fputs(_("!Can't create a clamd session"), stderr);
1402 1402
 			return EX_UNAVAILABLE;
1403 1403
 		}
1404 1404
 		sessions[0].status = CMDSOCKET_FREE;
... ...
@@ -1439,7 +1438,7 @@ main(int argc, char **argv)
1439 1439
 			free(hostname);
1440 1440
 		}
1441 1441
 
1442
-		logg("*numServers: %d\n", numServers);
1442
+		cli_dbgmsg("numServers: %d\n", numServers);
1443 1443
 
1444 1444
 		serverIPs = (in_addr_t *)cli_malloc(numServers * sizeof(in_addr_t));
1445 1445
 		activeServers = 0;
... ...
@@ -1537,8 +1536,7 @@ main(int argc, char **argv)
1537 1537
 		if(activeServers == 0) {
1538 1538
 			cli_errmsg(_("Check your entry for TCPSocket in %s\n"),
1539 1539
 				cfgfile);
1540
-			logg(_("!Can't find any clamd server"));
1541
-			logg_close();
1540
+			fputs(_("!Can't find any clamd server"), stderr);
1542 1541
 			return EX_CONFIG;
1543 1542
 		}
1544 1543
 #endif
... ...
@@ -1638,6 +1636,7 @@ main(int argc, char **argv)
1638 1638
 	if(cfgopt(copt, "Foreground")->enabled)
1639 1639
 		logg_foreground = 1;
1640 1640
 	else {
1641
+		logg_foreground = 0;
1641 1642
 #ifdef	CL_DEBUG
1642 1643
 		printf(_("When debugging it is recommended that you use Foreground mode in %s\n"), cfgfile);
1643 1644
 		puts(_("\tso that you can see all of the messages"));
... ...
@@ -1655,48 +1654,73 @@ main(int argc, char **argv)
1655 1655
 		close(0);
1656 1656
 		open("/dev/null", O_RDONLY);
1657 1657
 
1658
-#ifndef	CL_DEBUG
1659 1658
 		close(1);
1660 1659
 
1661
-		if((cpt = cfgopt(copt, "LogFile")) && cpt->enabled) {
1662
-			logFile = cpt->strarg;
1660
+		/* initialize logger */
1661
+		logg_lock = cfgopt(copt, "LogFileUnlock")->enabled;
1662
+		logg_time = cfgopt(copt, "LogTime")->enabled;
1663
+		logok = cfgopt(copt, "LogClean")->enabled;
1664
+		logg_size = cfgopt(copt, "LogFileMaxSize")->numarg;
1665
+		logg_verbose = mprintf_verbose = cfgopt(copt, "LogVerbose")->enabled;
1663 1666
 
1664
-#if	defined(MSDOS) || defined(C_CYGWIN) || defined(WIN32)
1665
-			if((strlen(logFile) < 2) || ((logFile[0] != '/') && (logFile[0] != '\\') && (logFile[1] != ':'))) {
1666
-#else
1667
-			if((strlen(logFile) < 2) || (logFile[0] != '/')) {
1668
-#endif
1669
-				fprintf(stderr, "%s: LogFile requires full path\n", argv[0]);
1670
-				return EX_CONFIG;
1667
+		if(cfgopt(copt, "Debug")->enabled) /* enable debug messages in libclamav */
1668
+		cl_debug();
1669
+
1670
+		if((cpt = cfgopt(copt, "LogFile"))->enabled) {
1671
+			time_t currtime;
1672
+
1673
+			logg_file = cpt->strarg;
1674
+			if((strlen(logg_file) < 2) ||
1675
+			   ((logg_file[0] != '/') && (logg_file[0] != '\\') && (logg_file[1] != ':'))) {
1676
+				fprintf(stderr, "ERROR: LogFile requires full path.\n");
1677
+				logg_close();
1678
+				freecfg(copt);
1679
+				return 1;
1671 1680
 			}
1672
-			if(open(logFile, O_WRONLY|O_APPEND) < 0) {
1673
-				if(errno == ENOENT) {
1674
-					/*
1675
-					 * There is low risk race condition here
1676
-					 */
1677
-					if(open(logFile, O_WRONLY|O_CREAT, 0644) < 0) {
1678
-						perror(logFile);
1679
-						return EX_CANTCREAT;
1680
-					}
1681
-				} else {
1682
-					perror(logFile);
1683
-					return EX_CANTCREAT;
1684
-				}
1681
+			time(&currtime);
1682
+			if(logg("#ClamAV-milter started at %s", ctime(&currtime))) {
1683
+				fprintf(stderr, "ERROR: Problem with internal logger. Please check the permissions on the %s file.\n", logg_file);
1684
+				logg_close();
1685
+				freecfg(copt);
1686
+				return 1;
1685 1687
 			}
1686 1688
 		} else {
1687
-			logFile = console;
1689
+#ifdef	CL_DEBUG
1690
+			logg_file = console;
1688 1691
 			if(consolefd < 0) {
1689 1692
 				perror(console);
1690 1693
 				return EX_OSFILE;
1691 1694
 			}
1692 1695
 			dup(consolefd);
1696
+#else
1697
+			logg_file = NULL;
1698
+#endif
1693 1699
 		}
1700
+
1701
+#if defined(USE_SYSLOG) && !defined(C_AIX)
1702
+		if(cfgopt(copt, "LogSyslog")->enabled) {
1703
+			int fac = LOG_LOCAL6;
1704
+
1705
+			cpt = cfgopt(copt, "LogFacility");
1706
+			if((fac = logg_facility(cpt->strarg)) == -1) {
1707
+				logg("!LogFacility: %s: No such facility.\n", cpt->strarg);
1708
+				logg_close();
1709
+				freecfg(copt);
1710
+				return 1;
1711
+			}
1712
+
1713
+			openlog("clamav-milter", LOG_PID, fac);
1714
+			logg_syslog = 1;
1715
+		}
1716
+#endif
1717
+
1694 1718
 		close(2);
1695 1719
 		dup(1);
1720
+
1721
+#ifdef	CL_DEBUG
1696 1722
 		if(consolefd >= 0)
1697 1723
 			close(consolefd);
1698
-
1699
-#endif	/*!CL_DEBUG*/
1724
+#endif
1700 1725
 
1701 1726
 #ifdef HAVE_SETPGRP
1702 1727
 #ifdef SETPGRP_VOID
... ...
@@ -1711,12 +1735,6 @@ main(int argc, char **argv)
1711 1711
 #endif
1712 1712
 	}
1713 1713
 
1714
-	logg_lock = cfgopt(copt, "LogFileUnlock")->enabled;
1715
-	logg_time = cfgopt(copt, "LogTime")->enabled;
1716
-	logClean = cfgopt(copt, "LogClean")->enabled;
1717
-	logg_size = cfgopt(copt, "LogFileMaxSize")->numarg;
1718
-	logg_verbose = mprintf_verbose = cfgopt(copt, "LogVerbose")->enabled;
1719
-
1720 1714
 	if(cfgopt(copt, "Debug")->enabled)
1721 1715
 		/*
1722 1716
 		 * enable debug messages in libclamav, --debug also does this
... ...
@@ -3039,7 +3057,7 @@ clamfi_eom(SMFICTX *ctx)
3039 3039
 		}
3040 3040
 		switch(cl_scanfile(privdata->filename, &virname, NULL, privdata->root, &limits, options)) {
3041 3041
 			case CL_CLEAN:
3042
-				if(logClean)
3042
+				if(logok)
3043 3043
 					logg("#%s: OK", privdata->filename);
3044 3044
 				strcpy(mess, "OK");
3045 3045
 				break;
... ...
@@ -3596,7 +3614,7 @@ clamfi_eom(SMFICTX *ctx)
3596 3596
 			smfi_addheader(ctx, "X-Virus-Status", _("Clean"));
3597 3597
 
3598 3598
 		/* Include the sendmail queue ID in the log */
3599
-		if(logClean)
3599
+		if(logok)
3600 3600
 			logg(_("%s: clean message from %s\n"),
3601 3601
 				sendmailId,
3602 3602
 				(privdata->from) ? privdata->from : _("an unknown sender"));
... ...
@@ -4432,7 +4450,7 @@ checkClamd(void)
4432 4432
 	pid = atoi(buf);
4433 4433
 	if((kill(pid, 0) < 0) && (errno == ESRCH)) {
4434 4434
 		perror("clamd");
4435
-		logg(_("!Clamd (pid %d) seems to have died"), pid);
4435
+		logg(_("!Clamd (pid %d) seems to have died"), (int)pid);
4436 4436
 	}
4437 4437
 }
4438 4438
 
... ...
@@ -5321,14 +5339,14 @@ loadDatabase(void)
5321 5321
 #ifdef	SESSION
5322 5322
 	pthread_mutex_lock(&version_mutex);
5323 5323
 #endif
5324
-	logg( _("Loaded %s"), clamav_version);
5324
+	logg( _("Loaded %s\n"), clamav_version);
5325 5325
 #ifdef	SESSION
5326 5326
 	pthread_mutex_unlock(&version_mutex);
5327 5327
 #endif
5328
-	logg(_("ClamAV: Protecting against %u viruses"), signatures);
5328
+	logg(_("ClamAV: Protecting against %u viruses\n"), signatures);
5329 5329
 	if(oldroot) {
5330 5330
 		cl_free(oldroot);
5331
-		logg("#Database correctly reloaded (%u viruses)", signatures);
5331
+		logg("#Database correctly reloaded (%u viruses)\n", signatures);
5332 5332
 	} else
5333 5333
 		cli_dbgmsg("Database loaded\n");
5334 5334