Browse code

fork before system() call in daemon mode

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

Tomasz Kojm authored on 2005/03/26 02:53:12
Showing 7 changed files
... ...
@@ -1,3 +1,8 @@
1
+Fri Mar 25 18:47:34 CET 2005 (tk)
2
+---------------------------------
3
+  * freshclam: fork before system() call in daemon mode (patch by Per Jessen
4
+	       <per*computer.org>)
5
+
1 6
 Tue Mar 22 22:54:46 CET 2005 (tk)
2 7
 ---------------------------------
3 8
   * libclamav/scanners.c: fix encrypted zip detecion (patch by Dirk Mueller
... ...
@@ -38,3 +38,4 @@
38 38
 
39 39
 #define CL_DEFAULT_CHECKS 12
40 40
 #define CL_DEFAULT_MAXATTEMPTS 3
41
+#define CL_MAX_CHILDREN 5
41 42
new file mode 100644
... ...
@@ -0,0 +1,57 @@
0
+/*
1
+ *  By Per Jessen <per@computer.org>
2
+ *
3
+ *  This program is free software; you can redistribute it and/or modify
4
+ *  it under the terms of the GNU General Public License as published by
5
+ *  the Free Software Foundation; either version 2 of the License, or
6
+ *  (at your option) any later version.
7
+ *
8
+ *  This program is distributed in the hope that it will be useful,
9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ *  GNU General Public License for more details.
12
+ *
13
+ *  You should have received a copy of the GNU General Public License
14
+ *  along with this program; if not, write to the Free Software
15
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
+ */
17
+
18
+#if HAVE_CONFIG_H
19
+#include "clamav-config.h"
20
+#endif
21
+
22
+#include <stdio.h>
23
+#include <stdlib.h>
24
+#include <unistd.h>
25
+#include <string.h>
26
+#include <errno.h>
27
+
28
+#include "defaults.h"
29
+#include "freshclam.h"
30
+#include "output.h"
31
+
32
+int active_children;
33
+
34
+void execute( const char *type, const char *text )
35
+{
36
+	pid_t pid;
37
+
38
+	if ( active_children<CL_MAX_CHILDREN )
39
+	switch( pid=fork() ) {
40
+	case 0:
41
+		if ( -1==system(text) )
42
+		{
43
+		mprintf("@%s: couldn't execute \"%s\".\n", type, text);
44
+		}
45
+		exit(0);
46
+	case -1:
47
+		mprintf("@%s::fork() failed, %s.\n", type, strerror(errno));
48
+		break;
49
+	default:
50
+		active_children++;
51
+	}
52
+	else
53
+	{
54
+		mprintf("@%s: already %d processes active.\n", type, active_children);
55
+	}
56
+}
0 57
new file mode 100644
... ...
@@ -0,0 +1,25 @@
0
+/*
1
+ *  By Per Jessen <per@computer.org>
2
+ *
3
+ *  This program is free software; you can redistribute it and/or modify
4
+ *  it under the terms of the GNU General Public License as published by
5
+ *  the Free Software Foundation; either version 2 of the License, or
6
+ *  (at your option) any later version.
7
+ *
8
+ *  This program is distributed in the hope that it will be useful,
9
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
+ *  GNU General Public License for more details.
12
+ *
13
+ *  You should have received a copy of the GNU General Public License
14
+ *  along with this program; if not, write to the Free Software
15
+ *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
+ */
17
+
18
+
19
+#ifndef __EXECUTE_H
20
+#define __EXECUTE_H
21
+
22
+void execute( const char *type, const char *text );
23
+
24
+#endif
... ...
@@ -29,6 +29,7 @@
29 29
 #include <signal.h>
30 30
 #include <time.h>
31 31
 #include <sys/types.h>
32
+#include <sys/wait.h>
32 33
 #include <sys/stat.h>
33 34
 #include <fcntl.h>
34 35
 #include <pwd.h>
... ...
@@ -45,13 +46,19 @@
45 45
 #include "output.h"
46 46
 #include "target.h"
47 47
 #include "misc.h"
48
+#include "execute.h"
48 49
 
49 50
 static short terminate = 0;
50
-
51
+extern int active_children;
51 52
 
52 53
 static void daemon_sighandler(int sig) {
53 54
 
54 55
     switch(sig) {
56
+	case SIGCHLD:
57
+	    waitpid(-1, NULL, WNOHANG);
58
+	    active_children--;
59
+	    break;
60
+
55 61
 	case SIGALRM:
56 62
 	case SIGUSR1:
57 63
 	    terminate = -1;
... ...
@@ -291,23 +298,28 @@ int freshclam(struct optstruct *opt)
291 291
 	    writepid(pidfile);
292 292
 	}
293 293
 
294
+	active_children = 0;
295
+
294 296
 	logg("freshclam daemon "VERSION" (OS: "TARGET_OS_TYPE", ARCH: "TARGET_ARCH_TYPE", CPU: "TARGET_CPU_TYPE")\n");
295 297
 
296 298
 	sigaction(SIGTERM, &sigact, NULL);
297 299
 	sigaction(SIGHUP, &sigact, NULL);
298 300
 	sigaction(SIGINT, &sigact, NULL);
301
+        sigaction(SIGCHLD, &sigact, NULL);
299 302
 
300 303
 	while(!terminate) {
301 304
 	    ret = download(copt, opt);
302 305
 
306
+            if(ret > 1) {
307
+		    const char *arg = NULL;
303 308
 
304
-	    if(optl(opt, "on-error-execute")) {
305
-		if(ret > 1)
306
-		    system(getargl(opt, "on-error-execute"));
309
+	        if(optl(opt, "on-error-execute"))
310
+		    arg = getargl(opt, "on-error-execute");
311
+		else if((cpt = cfgopt(copt, "OnErrorExecute")))
312
+		    arg = cpt->strarg;
307 313
 
308
-	    } else if((cpt = cfgopt(copt, "OnErrorExecute"))) {
309
-		if(ret > 1)
310
-		    system(cpt->strarg);
314
+		if(arg)
315
+		    execute("OnErrorExecute", arg);
311 316
 	    }
312 317
 
313 318
 	    logg("--------------------------------------\n");
... ...
@@ -1,5 +1,5 @@
1 1
 /*
2
- *  Copyright (C) 2002, 2003 Tomasz Kojm <zolw@konarski.edu.pl>
2
+ *  Copyright (C) 2002 - 2005 Tomasz Kojm <tkojm@clamav.net>
3 3
  *
4 4
  *  This program is free software; you can redistribute it and/or modify
5 5
  *  it under the terms of the GNU General Public License as published by
... ...
@@ -20,6 +20,7 @@
20 20
 #define __FRESHCLAM_H
21 21
 
22 22
 #include "cfgparser.h"
23
+#include "options.h"
23 24
 
24 25
 void help(void);
25 26
 void daemonize(void);
... ...
@@ -58,6 +58,7 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
58 58
 	char ipaddr[16], *dnsreply = NULL, *pt;
59 59
 	struct cfgstruct *cpt;
60 60
 	char *localip = NULL;
61
+	const char *arg = NULL;
61 62
 #ifdef HAVE_RESOLV_H
62 63
 	const char *dnsdbinfo;
63 64
 #endif
... ...
@@ -189,9 +190,16 @@ int downloadmanager(const struct cfgstruct *copt, const struct optstruct *opt, c
189 189
 #endif
190 190
 
191 191
 	if(optl(opt, "on-update-execute"))
192
-	    system(getargl(opt, "on-update-execute"));
192
+	    arg = getargl(opt, "on-update-execute");
193 193
 	else if((cpt = cfgopt(copt, "OnUpdateExecute")))
194
-	    system(cpt->strarg);
194
+	    arg = cpt->strarg;
195
+
196
+	if(arg) {
197
+	    if(optc(opt, 'd'))
198
+		execute( "OnUpdateExecute", pt );
199
+            else
200
+		system(pt);
201
+	}
195 202
 
196 203
 	return 0;
197 204