Compare commits
4 Commits
200b6f6c33
...
d62d2956cd
Author | SHA1 | Date | |
---|---|---|---|
d62d2956cd | |||
da1f55e340 | |||
875c176cc1 | |||
2e77170115 |
75
adhocify.c
75
adhocify.c
@ -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,8 +584,12 @@ void process_options()
|
|||||||
|
|
||||||
if(forkbombcheck)
|
if(forkbombcheck)
|
||||||
{
|
{
|
||||||
char *path_prog = xrealpath(prog, NULL);
|
char *path_prog = realpath(prog, NULL);
|
||||||
check_forkbomb(path_logfile, path_prog);
|
if(path_prog != NULL)
|
||||||
|
{
|
||||||
|
check_forkbomb(path_logfile, path_prog);
|
||||||
|
}
|
||||||
|
free(path_prog);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(daemonize)
|
if(daemonize)
|
||||||
@ -627,7 +625,7 @@ void start_monitoring(int ifd)
|
|||||||
handle_event(event);
|
handle_event(event);
|
||||||
offset+=sizeof(struct inotify_event) + event->len;
|
offset+=sizeof(struct inotify_event) + event->len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void child_handler(int signum, siginfo_t *info, void *context)
|
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");
|
logerror("waitpid failed when handling child exit\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if(exit_with_child)
|
|
||||||
|
int adhocify_exit_code = 0;
|
||||||
|
if(WIFEXITED(status))
|
||||||
{
|
{
|
||||||
int adhocify_exit_code = 0;
|
adhocify_exit_code = WEXITSTATUS(status);
|
||||||
if(WIFEXITED(status))
|
if(adhocify_exit_code == 127)
|
||||||
{
|
{
|
||||||
adhocify_exit_code = WEXITSTATUS(status);
|
logwrite("command not found, exiting\n");
|
||||||
if(awaited_child_exit_code > -1)
|
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;
|
must_exit = !must_exit;
|
||||||
if(negate_child_exit_code)
|
}
|
||||||
{
|
if(must_exit)
|
||||||
must_exit = !must_exit;
|
{
|
||||||
}
|
logwrite("command exited with specified exit code, exiting too\n");
|
||||||
if(must_exit)
|
exit(adhocify_exit_code);
|
||||||
{
|
|
||||||
logwrite("child exited with specified exit code, exiting too\n");
|
|
||||||
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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user