Compare commits

..

No commits in common. "d62d2956cd9bd894744664a768229933b668af08" and "200b6f6c33d577e2996b9f45f87a56b90163f2ed" have entirely different histories.

View File

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