2005-03-25 17:53:12 +00:00
|
|
|
/*
|
2007-02-22 20:00:21 +00:00
|
|
|
* By Per Jessen <per@computer.org> with changes by the ClamAV team
|
2005-03-25 17:53:12 +00:00
|
|
|
*
|
|
|
|
* 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
|
2006-04-09 19:59:28 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
2005-03-25 17:53:12 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2007-02-22 20:00:21 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2005-03-25 17:53:12 +00:00
|
|
|
#include <unistd.h>
|
2007-02-22 20:00:21 +00:00
|
|
|
#endif
|
2005-03-25 17:53:12 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2006-08-30 22:41:21 +00:00
|
|
|
#include "shared/output.h"
|
2008-12-18 19:42:53 +00:00
|
|
|
#include "shared/optparser.h"
|
2007-02-11 00:41:13 +00:00
|
|
|
#include "execute.h"
|
2006-08-30 22:41:21 +00:00
|
|
|
|
|
|
|
#define MAX_CHILDREN 5
|
2005-03-25 17:53:12 +00:00
|
|
|
|
|
|
|
int active_children;
|
|
|
|
|
2012-07-31 17:30:19 -04:00
|
|
|
void
|
|
|
|
execute (const char *type, const char *text, const struct optstruct *opts)
|
2005-03-25 17:53:12 +00:00
|
|
|
{
|
2012-07-31 17:30:19 -04:00
|
|
|
int ret;
|
2008-06-02 13:47:15 +00:00
|
|
|
|
2012-07-31 17:30:19 -04:00
|
|
|
if (!optget (opts, "daemon")->enabled)
|
|
|
|
{
|
|
|
|
if (sscanf (text, "EXIT_%d", &ret) == 1)
|
|
|
|
{
|
|
|
|
logg ("*%s: EXIT_%d\n", type, ret);
|
|
|
|
exit (ret);
|
|
|
|
}
|
|
|
|
if (system (text) == -1)
|
|
|
|
logg ("%s: system(%s) failed\n", type, text);
|
2008-10-27 13:28:48 +00:00
|
|
|
|
2012-07-31 17:30:19 -04:00
|
|
|
return;
|
2008-06-02 13:47:15 +00:00
|
|
|
}
|
|
|
|
|
2009-09-24 16:08:52 +02:00
|
|
|
#ifdef _WIN32
|
2012-07-31 17:30:19 -04:00
|
|
|
if (spawnlp (_P_NOWAIT, text, text, NULL) == -1)
|
|
|
|
{
|
|
|
|
logg ("^%s: couldn't execute \"%s\".\n", type, text);
|
|
|
|
return;
|
2009-10-16 01:50:09 +02:00
|
|
|
}
|
2007-02-22 20:00:21 +00:00
|
|
|
#else
|
2012-07-31 17:30:19 -04:00
|
|
|
if (active_children < MAX_CHILDREN)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
switch (pid = fork ())
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
if (-1 == system (text))
|
|
|
|
{
|
|
|
|
logg ("^%s: couldn't execute \"%s\".\n", type, text);
|
|
|
|
}
|
|
|
|
exit (0);
|
|
|
|
case -1:
|
|
|
|
logg ("^%s::fork() failed, %s.\n", type, strerror (errno));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
active_children++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logg ("^%s: already %d processes active.\n", type, active_children);
|
2009-09-24 19:23:21 +02:00
|
|
|
}
|
2007-02-22 20:00:21 +00:00
|
|
|
#endif
|
2005-03-25 17:53:12 +00:00
|
|
|
}
|