Browse code

- freshclam -d signal handling, logfile rotation

git-svn-id: file:///var/lib/svn/clamav-devel/trunk/clamav-devel@310 77e5149b-7576-45b1-b177-96237e5ba77b

Thomas Lamy authored on 2004/02/19 20:14:22
Showing 2 changed files
... ...
@@ -27,6 +27,7 @@
27 27
 #include <stdlib.h>
28 28
 #include <unistd.h>
29 29
 #include <string.h>
30
+#include <errno.h>
30 31
 #include <signal.h>
31 32
 #include <sys/types.h>
32 33
 #include <sys/stat.h>
... ...
@@ -43,11 +44,57 @@
43 43
 
44 44
 #define TIMEOUT 1200
45 45
 
46
+static short terminate = 0;
47
+
48
+
49
+static void daemon_sighandler(int sig) {
50
+	char *action = NULL;
51
+
52
+    switch(sig) {
53
+	case SIGALRM:
54
+	case SIGUSR1:
55
+	    action = "wake up";
56
+	    break;
57
+
58
+	case SIGHUP:
59
+	    action = "re-opening log file";
60
+	    break;
61
+
62
+	default:
63
+	    action = "terminating";
64
+	    terminate = 1;
65
+	    break;
66
+    }
67
+    logg("Received signal %d, %s\n", sig, action);
68
+    if (sig == SIGHUP) {
69
+	logg(NULL);	/* forces log file re-opening */
70
+    }
71
+    return;
72
+}
73
+
74
+
75
+static void writepid(char *pidfile) {
76
+	FILE *fd;
77
+	int old_umask;
78
+    old_umask = umask(0006);
79
+    if((fd = fopen(pidfile, "w")) == NULL) {
80
+	logg("!Can't save PID to file %s: %s\n", pidfile, strerror(errno));
81
+    } else {
82
+	fprintf(fd, "%d", getpid());
83
+	fclose(fd);
84
+    }
85
+    umask(old_umask);
86
+}
87
+
88
+
46 89
 int freshclam(struct optstruct *opt)
47 90
 {
48 91
 	int ret;
49 92
 	char *newdir, *cfgfile;
93
+	char *pidfile = NULL;
50 94
 	struct cfgstruct *copt, *cpt;
95
+	struct sigaction sigact;
96
+	struct sigaction oldact;
51 97
 #ifndef C_CYGWIN
52 98
 	char *unpuser;
53 99
 	struct passwd *user;
... ...
@@ -162,6 +209,8 @@ int freshclam(struct optstruct *opt)
162 162
     if(optc(opt, 'd')) {
163 163
 	    int bigsleep, checks;
164 164
 
165
+	memset(&sigact, 0, sizeof(struct sigaction));
166
+	sigact.sa_handler = daemon_sighandler;
165 167
 
166 168
 	if(optc(opt, 'c')) {
167 169
 	    checks = atoi(getargc(opt, 'c'));
... ...
@@ -178,8 +227,20 @@ int freshclam(struct optstruct *opt)
178 178
 
179 179
 	bigsleep = 24 * 3600 / checks;
180 180
 	daemonize();
181
+	if (optl(opt, "pid")) {
182
+	    pidfile = getargl(opt, "pid");
183
+	} else if (cpt = cfgopt(copt, "PidFile")) {
184
+	    pidfile = cpt->strarg;
185
+	}
186
+	if (pidfile) {
187
+	    writepid(pidfile);
188
+	}
189
+	logg("freshclam daemon started (pid=%d)\n", getpid());
181 190
 
182
-	while(1) {
191
+	sigaction(SIGTERM, &sigact, NULL);
192
+	sigaction(SIGHUP, &sigact, NULL);
193
+	sigaction(SIGINT, &sigact, NULL);
194
+	while(!terminate) {
183 195
 	    ret = download(copt, opt);
184 196
 
185 197
 
... ...
@@ -193,7 +254,12 @@ int freshclam(struct optstruct *opt)
193 193
 	    }
194 194
 
195 195
 	    logg("\n--------------------------------------\n");
196
-	    sleep(bigsleep);
196
+	    sigaction(SIGALRM, &sigact, &oldact);
197
+	    sigaction(SIGUSR1, &sigact, &oldact);
198
+	    alarm(bigsleep);
199
+	    pause();
200
+	    sigaction(SIGALRM, &oldact, NULL);
201
+	    sigaction(SIGUSR1, &oldact, NULL);
197 202
 	}
198 203
 
199 204
     } else
... ...
@@ -207,6 +273,9 @@ int freshclam(struct optstruct *opt)
207 207
 	if(ret > 1)
208 208
 	    system(cpt->strarg);
209 209
     }
210
+    if (pidfile) {
211
+        unlink(pidfile);
212
+    }
210 213
 
211 214
     return(ret);
212 215
 }
... ...
@@ -298,10 +367,11 @@ void help(void)
298 298
     mprintf("    --stdout                             write to stdout instead of stderr\n");
299 299
     mprintf("                                         (this help is always written to stdout)\n");
300 300
     mprintf("\n");
301
-    mprintf("    --config-file=FILE                   Read configuration from FILE.\n");
301
+    mprintf("    --config-file=FILE                   read configuration from FILE.\n");
302 302
     mprintf("    --log=FILE           -l FILE         log into FILE\n");
303 303
     mprintf("    --log-verbose                        log additional information\n");
304 304
     mprintf("    --daemon             -d              run in daemon mode\n");
305
+    mprintf("    --pid                -p FILE         save daemon's pid in FILE\n");
305 306
     mprintf("    --user=USER          -u USER         run as USER\n");
306 307
     mprintf("    --checks=#n          -c #n           number of checks per day, 1 <= n <= 50\n");
307 308
     mprintf("    --datadir=DIRECTORY                  download new databases into DIRECTORY\n");
... ...
@@ -34,7 +34,7 @@ int main(int argc, char **argv)
34 34
 	int ret, opt_index, i, len;
35 35
 	struct optstruct *opt;
36 36
 
37
-	const char *getopt_parameters = "hvdVl:c:u:";
37
+	const char *getopt_parameters = "hvdp:Vl:c:u:";
38 38
 
39 39
 	static struct option long_options[] = {
40 40
 	    {"help", 0, 0, 'h'},
... ...
@@ -47,6 +47,7 @@ int main(int argc, char **argv)
47 47
 	    {"log-verbose", 0, 0, 0},
48 48
 	    {"stdout", 0, 0, 0},
49 49
 	    {"daemon", 0, 0, 'd'},
50
+	    {"pid", 1, 0, 'p'},
50 51
 	    {"user", 1, 0, 'u'}, /* not used */
51 52
 	    {"config-file", 1, 0, 0},
52 53
 	    {"checks", 1, 0, 'c'},