* Refactored some shit around.

This commit is contained in:
Sam Hocevar 2007-01-12 18:01:27 +00:00 committed by sam
parent 3a12894365
commit cf8fcc5d13

View File

@ -44,7 +44,9 @@
#include "md5.h"
#include "timer.h"
static void spawn_child(char **);
static void loop_stdin(void);
static void spawn_children(void);
static void clean_children(void);
static void read_children(void);
@ -75,6 +77,8 @@ static struct child_list
} *child_list;
static int maxforks = 1, child_count = 0, maxcrashes = 1, crashes = 0;
static char **newargv;
static char *protect = NULL, *refuse = NULL;
static int seed = 0;
static int endseed = 1;
static int quiet = 0;
@ -83,6 +87,7 @@ static int md5 = 0;
static int checkexit = 0;
static int maxmem = -1;
static int64_t maxtime = -1;
static int64_t lastlaunch = 0;
#define ZZUF_FD_SET(fd, p_fdset, maxfd) \
if(fd >= 0) \
@ -97,11 +102,10 @@ static int64_t maxtime = -1;
int main(int argc, char *argv[])
{
char **newargv;
char *parser, *include, *exclude, *protect, *refuse;
char *parser, *include, *exclude;
int i, cmdline = 0;
include = exclude = protect = refuse = NULL;
include = exclude = NULL;
#if defined(HAVE_GETOPT_H)
for(;;)
@ -238,12 +242,6 @@ int main(int argc, char *argv[])
/* If asked to read from the standard input */
if(optind >= argc)
{
uint8_t md5sum[16];
struct md5 *ctx = NULL;
if(md5)
ctx = _zz_md5_init();
if(endseed != seed + 1)
{
printf("%s: seed ranges are incompatible with stdin fuzzing\n",
@ -252,6 +250,81 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}
loop_stdin();
return EXIT_SUCCESS;
}
/* If asked to launch programs */
if(cmdline)
{
int dashdash = 0;
for(i = optind + 1; i < argc; i++)
{
if(dashdash)
include = merge_file(include, argv[i]);
else if(!strcmp("--", argv[i]))
dashdash = 1;
else if(argv[i][0] != '-')
include = merge_file(include, argv[i]);
}
}
if(include)
setenv("ZZUF_INCLUDE", include, 1);
if(exclude)
setenv("ZZUF_EXCLUDE", exclude, 1);
if(protect)
setenv("ZZUF_PROTECT", protect, 1);
if(refuse)
setenv("ZZUF_REFUSE", refuse, 1);
/* Allocate memory for children handling */
child_list = malloc(maxforks * sizeof(struct child_list));
for(i = 0; i < maxforks; i++)
child_list[i].status = STATUS_FREE;
child_count = 0;
/* Preload libzzuf.so */
set_environment(argv[0]);
/* Create new argv */
newargv = malloc((argc - optind + 1) * sizeof(char *));
memcpy(newargv, argv + optind, (argc - optind) * sizeof(char *));
newargv[argc - optind] = (char *)NULL;
/* Main loop */
while(child_count || seed < endseed)
{
/* Spawn new children, if necessary */
spawn_children();
/* Cleanup dead or dying children */
clean_children();
/* Read data from children */
read_children();
if(maxcrashes && crashes >= maxcrashes && child_count == 0)
break;
}
/* Clean up */
free(newargv);
free(child_list);
return EXIT_SUCCESS;
}
static void loop_stdin(void)
{
uint8_t md5sum[16];
struct md5 *ctx = NULL;
if(md5)
ctx = _zz_md5_init();
if(protect)
_zz_protect(protect);
if(refuse)
@ -297,71 +370,6 @@ int main(int argc, char *argv[])
_zz_unregister(0);
_zz_fd_fini();
return EXIT_SUCCESS;
}
if(cmdline)
{
int dashdash = 0;
for(i = optind + 1; i < argc; i++)
{
if(dashdash)
include = merge_file(include, argv[i]);
else if(!strcmp("--", argv[i]))
dashdash = 1;
else if(argv[i][0] != '-')
include = merge_file(include, argv[i]);
}
}
if(include)
setenv("ZZUF_INCLUDE", include, 1);
if(exclude)
setenv("ZZUF_EXCLUDE", exclude, 1);
if(protect)
setenv("ZZUF_PROTECT", protect, 1);
if(refuse)
setenv("ZZUF_REFUSE", refuse, 1);
/* Allocate memory for children handling */
child_list = malloc(maxforks * sizeof(struct child_list));
for(i = 0; i < maxforks; i++)
child_list[i].status = STATUS_FREE;
child_count = 0;
/* Preload libzzuf.so */
set_environment(argv[0]);
/* Create new argv */
newargv = malloc((argc - optind + 1) * sizeof(char *));
memcpy(newargv, argv + optind, (argc - optind) * sizeof(char *));
newargv[argc - optind] = (char *)NULL;
/* Main loop */
while(child_count || seed < endseed)
{
/* Spawn one new child, if necessary */
if(child_count < maxforks && seed < endseed &&
(maxcrashes && crashes < maxcrashes))
spawn_child(newargv);
/* Cleanup dead or dying children */
clean_children();
/* Read data from children */
read_children();
if(maxcrashes && crashes >= maxcrashes && child_count == 0)
break;
}
/* Clean up */
free(newargv);
free(child_list);
return EXIT_SUCCESS;
}
static char *merge_file(char *regex, char *file)
@ -408,7 +416,7 @@ static char *merge_regex(char *regex, char *string)
return regex;
}
static void spawn_child(char **argv)
static void spawn_children(void)
{
static int const files[] = { DEBUG_FILENO, STDERR_FILENO, STDOUT_FILENO };
char buf[BUFSIZ];
@ -416,6 +424,15 @@ static void spawn_child(char **argv)
pid_t pid;
int i, j;
if(child_count == maxforks)
return; /* no slot */
if(seed == endseed)
return; /* job finished */
if(maxcrashes && crashes >= maxcrashes)
return; /* all jobs crashed */
/* Find the empty slot */
for(i = 0; i < maxforks; i++)
if(child_list[i].status == STATUS_FREE)
@ -458,16 +475,18 @@ static void spawn_child(char **argv)
setenv("ZZUF_SEED", buf, 1);
/* Run our process */
if(execvp(argv[0], argv))
if(execvp(newargv[0], newargv))
{
perror(argv[0]);
perror(newargv[0]);
exit(EXIT_FAILURE);
}
return;
}
lastlaunch = _zz_time();
/* Were the parent, acknowledge spawn */
child_list[i].date = _zz_time();
child_list[i].date = lastlaunch;
child_list[i].pid = pid;
for(j = 0; j < 3; j++)
{
@ -479,6 +498,7 @@ static void spawn_child(char **argv)
child_list[i].status = STATUS_RUNNING;
if(md5)
child_list[i].ctx = _zz_md5_init();
child_count++;
seed++;
}