Compare commits

...

4 Commits

1 changed files with 37 additions and 38 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2017 Albert S. <adhocify@quitesimple.org>
* Copyright (c) 2014-2020 Albert S. <adhocify@quitesimple.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@ -278,9 +278,10 @@ bool run_prog(const char *eventfile, uint32_t eventmask)
script_arguments[i] = eventfile;
}
execv(prog, script_arguments);
perror("execv");
return false;
execvp(prog, script_arguments);
logerror("Exec of %s failed: %s\n", prog, strerror(errno));
int exitcode = (errno == ENOENT) ? 127 : EXIT_FAILURE;
exit(exitcode);
}
if(pid == -1)
{
@ -337,7 +338,6 @@ void check_forkbomb(const char *path_logfile, const char *path_prog)
{
if(lkp->isdir)
{
char *dir_lkpPath = lkp->path;
if( STREQ(dir_lkpPath, dir_log) || STREQ(dir_lkpPath, dir_prog) )
{
@ -406,11 +406,11 @@ void handle_event(struct inotify_event *event)
free(eventfile_abspath);
return;
}
logwrite("Starting execution of child %s\n", prog);
logwrite("Starting execution of command %s\n", prog);
bool r = run_prog(eventfile_abspath, event->mask);
if(!r)
{
logerror("Execution of child %s failed\n", prog);
logerror("Execution of command %s failed\n", prog);
exit(EXIT_FAILURE);
}
@ -577,12 +577,6 @@ void process_options()
mask |= IN_CLOSE_WRITE;
}
if(! file_exists(prog))
{
fprintf(stderr, "File %s does not exist\n", prog);
exit(EXIT_FAILURE);
}
if(path_logfile)
{
path_logfile = xrealpath(path_logfile, NULL);
@ -590,8 +584,12 @@ void process_options()
if(forkbombcheck)
{
char *path_prog = xrealpath(prog, NULL);
check_forkbomb(path_logfile, path_prog);
char *path_prog = realpath(prog, NULL);
if(path_prog != NULL)
{
check_forkbomb(path_logfile, path_prog);
}
free(path_prog);
}
if(daemonize)
@ -627,7 +625,7 @@ void start_monitoring(int ifd)
handle_event(event);
offset+=sizeof(struct inotify_event) + event->len;
}
}
}
}
void child_handler(int signum, siginfo_t *info, void *context)
@ -644,36 +642,37 @@ void child_handler(int signum, siginfo_t *info, void *context)
logerror("waitpid failed when handling child exit\n");
exit(EXIT_FAILURE);
}
if(exit_with_child)
int adhocify_exit_code = 0;
if(WIFEXITED(status))
{
int adhocify_exit_code = 0;
if(WIFEXITED(status))
adhocify_exit_code = WEXITSTATUS(status);
if(adhocify_exit_code == 127)
{
adhocify_exit_code = WEXITSTATUS(status);
if(awaited_child_exit_code > -1)
logwrite("command not found, exiting\n");
exit(adhocify_exit_code);
}
if(exit_with_child && awaited_child_exit_code > -1)
{
bool must_exit = adhocify_exit_code == awaited_child_exit_code;
if(negate_child_exit_code)
{
bool must_exit = adhocify_exit_code == awaited_child_exit_code;
if(negate_child_exit_code)
{
must_exit = !must_exit;
}
if(must_exit)
{
logwrite("child exited with specified exit code, exiting too\n");
exit(adhocify_exit_code);
}
return; //not the exit code we wanted, keep running
must_exit = !must_exit;
}
if(must_exit)
{
logwrite("command exited with specified exit code, exiting too\n");
exit(adhocify_exit_code);
}
}
if(WIFSIGNALED(status))
{
adhocify_exit_code = 128 + WTERMSIG(status); //copy bash's behaviour
}
//TODO: coredump?
}
if(exit_with_child && WIFSIGNALED(status))
{
adhocify_exit_code = 128 + WTERMSIG(status); //copy bash's behaviour
exit(adhocify_exit_code);
}
}
void set_signals()
{
struct sigaction action;