Compare commits

...

4 Melakukan

1 mengubah file dengan 37 tambahan dan 38 penghapusan

Melihat 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 * 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,9 +278,10 @@ bool run_prog(const char *eventfile, uint32_t eventmask)
script_arguments[i] = eventfile; script_arguments[i] = eventfile;
} }
execv(prog, script_arguments); execvp(prog, script_arguments);
perror("execv"); logerror("Exec of %s failed: %s\n", prog, strerror(errno));
return false; int exitcode = (errno == ENOENT) ? 127 : EXIT_FAILURE;
exit(exitcode);
} }
if(pid == -1) if(pid == -1)
{ {
@ -337,7 +338,6 @@ 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 child %s\n", prog); logwrite("Starting execution of command %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 child %s failed\n", prog); logerror("Execution of command %s failed\n", prog);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -577,12 +577,6 @@ 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);
@ -590,9 +584,13 @@ void process_options()
if(forkbombcheck) if(forkbombcheck)
{ {
char *path_prog = xrealpath(prog, NULL); char *path_prog = realpath(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)
{ {
@ -644,13 +642,17 @@ 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(awaited_child_exit_code > -1) if(adhocify_exit_code == 127)
{
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)
@ -659,21 +661,18 @@ void child_handler(int signum, siginfo_t *info, void *context)
} }
if(must_exit) if(must_exit)
{ {
logwrite("child exited with specified exit code, exiting too\n"); logwrite("command 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(WIFSIGNALED(status)) if(exit_with_child && 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;