freshclam/execute.c
5f0d9945
 /*
17ad1c5b
  *  By Per Jessen <per@computer.org> with changes by the ClamAV team
5f0d9945
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
48b7b4a7
  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  *  MA 02110-1301, USA.
5f0d9945
  */
 
 #if HAVE_CONFIG_H
 #include "clamav-config.h"
 #endif
 
 #include <stdio.h>
 #include <stdlib.h>
17ad1c5b
 #ifdef	HAVE_UNISTD_H
5f0d9945
 #include <unistd.h>
17ad1c5b
 #endif
5f0d9945
 #include <string.h>
 #include <errno.h>
17ad1c5b
 #ifdef	C_WINDOWS
 #include <process.h>
 #endif
5f0d9945
 
a889f40e
 #include "shared/output.h"
3f7802c9
 #include "shared/optparser.h"
fc83da82
 #include "execute.h"
a889f40e
 
 #define MAX_CHILDREN 5
5f0d9945
 
 int active_children;
 
3f7802c9
 void execute( const char *type, const char *text, const struct optstruct *opts )
5f0d9945
 {
770fb166
 	int ret;
 #ifndef C_WINDOWS
 	pid_t pid;
 #endif
 
3f7802c9
     if(!optget(opts, "daemon")->enabled) {
770fb166
 	if(sscanf(text, "EXIT_%d", &ret) == 1) {
 	    logg("*%s: EXIT_%d\n", type, ret);
 	    exit(ret);
 	}
cafd3412
 	if(system(text) == -1)
770fb166
 	    logg("%s: system(%s) failed\n", type, text);
cafd3412
 
 	return;
770fb166
     }
 
17ad1c5b
 #ifdef        C_WINDOWS
 	if(active_children < MAX_CHILDREN) {
 		if(spawnlp(P_DETACH, text, text, NULL) == -1) {
 			logg("^%s: couldn't execute \"%s\".\n", type, text);
 			return;
 		}
 		active_children++;	/* FIXME: this is never reduced */
 	} else
 		logg("^%s: already %d processes active.\n", type, active_children);
 #else
a889f40e
 	if ( active_children<MAX_CHILDREN )
5f0d9945
 	switch( pid=fork() ) {
 	case 0:
 		if ( -1==system(text) )
 		{
0ae41a2d
 		logg("^%s: couldn't execute \"%s\".\n", type, text);
5f0d9945
 		}
 		exit(0);
 	case -1:
0ae41a2d
 		logg("^%s::fork() failed, %s.\n", type, strerror(errno));
5f0d9945
 		break;
 	default:
 		active_children++;
 	}
 	else
 	{
0ae41a2d
 		logg("^%s: already %d processes active.\n", type, active_children);
5f0d9945
 	}
17ad1c5b
 #endif
5f0d9945
 }