Added support for user specified script arguments
This commit is contained in:
parent
781b702d7d
commit
9c29c163c1
62
adhocify.c
62
adhocify.c
@ -24,7 +24,7 @@
|
|||||||
#include <linux/limits.h>
|
#include <linux/limits.h>
|
||||||
#define BUF_SIZE (sizeof(struct inotify_event) + NAME_MAX + 1) * 1024
|
#define BUF_SIZE (sizeof(struct inotify_event) + NAME_MAX + 1) * 1024
|
||||||
#define STREQ(s1,s2) ( strcmp(s1,s2) == 0 )
|
#define STREQ(s1,s2) ( strcmp(s1,s2) == 0 )
|
||||||
|
#define SCRIPT_PLACE_SPECIFIER "{}" //TODO: think of a better name...
|
||||||
struct watchlistentry
|
struct watchlistentry
|
||||||
{
|
{
|
||||||
int ifd;
|
int ifd;
|
||||||
@ -50,14 +50,15 @@ struct ignorelist **ignorelist_current = &ignorelist_head;
|
|||||||
|
|
||||||
/* Write once globals. Set from process_arguments*/
|
/* Write once globals. Set from process_arguments*/
|
||||||
bool silent = false;
|
bool silent = false;
|
||||||
bool noappend = false;
|
bool noenv = false;
|
||||||
bool fromstdin = false;
|
bool fromstdin = false;
|
||||||
bool forkbombcheck = true;
|
bool forkbombcheck = true;
|
||||||
bool daemonize = false;
|
bool daemonize = false;
|
||||||
uint32_t mask = 0;
|
uint32_t mask = 0;
|
||||||
char *prog = NULL;
|
char *prog = NULL;
|
||||||
char *path_logfile = NULL;
|
char *path_logfile = NULL;
|
||||||
|
char **script_arguments = NULL; //options to be passed to script we are calling
|
||||||
|
size_t n_script_arguments = 0;
|
||||||
|
|
||||||
void *xmalloc(size_t size)
|
void *xmalloc(size_t size)
|
||||||
{
|
{
|
||||||
@ -227,14 +228,36 @@ bool run_prog(const char *eventfile, uint32_t eventmask)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if(! noenv)
|
||||||
|
{
|
||||||
|
char envvar[30];
|
||||||
|
snprintf(envvar, sizeof(envvar), "adhocifyevent=%"PRIu32, eventmask);
|
||||||
|
putenv(envvar);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *arguments[n_script_arguments + 2]; //2 = argv0 and terminating NULL
|
||||||
|
|
||||||
const char *argv0 = memrchr(prog, '/', strlen(prog));
|
const char *argv0 = memrchr(prog, '/', strlen(prog));
|
||||||
argv0 = ( argv0 == NULL ) ? prog : argv0+1;
|
argv0 = ( argv0 == NULL ) ? prog : argv0+1;
|
||||||
|
arguments[0] = argv0;
|
||||||
char envvar[30];
|
|
||||||
snprintf(envvar, sizeof(envvar), "adhocifyevent=%"PRIu32, eventmask);
|
const int begin_offset = 1;
|
||||||
putenv(envvar);
|
for(int i = 0; i < n_script_arguments; i++)
|
||||||
|
{
|
||||||
execl(prog, argv0, (! noappend) ? eventfile : NULL, NULL);
|
char *argument = script_arguments[i];
|
||||||
|
if(STREQ(argument, SCRIPT_PLACE_SPECIFIER))
|
||||||
|
arguments[i+begin_offset] = eventfile;
|
||||||
|
else
|
||||||
|
arguments[i+begin_offset] = argument;
|
||||||
|
}
|
||||||
|
|
||||||
|
arguments[n_script_arguments+begin_offset] = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
execv(prog, arguments);
|
||||||
|
|
||||||
|
|
||||||
perror("execlp");
|
perror("execlp");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -379,7 +402,7 @@ static inline char *get_cwd()
|
|||||||
|
|
||||||
void print_usage()
|
void print_usage()
|
||||||
{
|
{
|
||||||
printf("adhocify [OPTIONS]script\n");
|
printf("adhocify [OPTIONS] script [arguments for script]\n");
|
||||||
|
|
||||||
printf("--daemon, -d\t\t\tdaemonize\n");
|
printf("--daemon, -d\t\t\tdaemonize\n");
|
||||||
printf("--path, -w\t\t\tpath -- adds the specified path to the watchlist\n");
|
printf("--path, -w\t\t\tpath -- adds the specified path to the watchlist\n");
|
||||||
@ -390,6 +413,7 @@ void print_usage()
|
|||||||
printf("--stdin, -s\t\t\tRead the paths which must be added to the watchlist from stdin. Each path in a seperate line\n");
|
printf("--stdin, -s\t\t\tRead the paths which must be added to the watchlist from stdin. Each path in a seperate line\n");
|
||||||
printf("--no-forkbomb-check, -b\t\tDisable fork bomb detection\n");
|
printf("--no-forkbomb-check, -b\t\tDisable fork bomb detection\n");
|
||||||
printf("--ignore, -i\t\t\tpattern -- Ignore events on files for which the pattern matches\n");
|
printf("--ignore, -i\t\t\tpattern -- Ignore events on files for which the pattern matches\n");
|
||||||
|
printf("\n\nIf your script should know the file the event occured on, use {} when you specify the script arguments (like xargs)\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct option long_options[] =
|
static struct option long_options[] =
|
||||||
@ -436,7 +460,7 @@ void parse_options(int argc, char **argv)
|
|||||||
watchqueue_addpath(watchpath);
|
watchqueue_addpath(watchpath);
|
||||||
break;
|
break;
|
||||||
case 'a':
|
case 'a':
|
||||||
noappend=true;
|
noenv=true;
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
fromstdin=true;
|
fromstdin=true;
|
||||||
@ -457,13 +481,19 @@ void parse_options(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(optind >= argc)
|
if(optind == argc)
|
||||||
{
|
{
|
||||||
|
print_usage();
|
||||||
logerror("missing prog/script path\n");
|
logerror("missing prog/script path\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
prog = argv[optind];
|
prog = argv[optind++];
|
||||||
|
if(optind < argc)
|
||||||
|
{
|
||||||
|
script_arguments = &argv[optind];
|
||||||
|
n_script_arguments = argc - optind;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void process_options()
|
void process_options()
|
||||||
@ -486,9 +516,9 @@ void process_options()
|
|||||||
if(mask == 0)
|
if(mask == 0)
|
||||||
mask |= IN_CLOSE_WRITE;
|
mask |= IN_CLOSE_WRITE;
|
||||||
|
|
||||||
if(! prog || ! file_exists(prog))
|
if(! file_exists(prog))
|
||||||
{
|
{
|
||||||
fprintf(stderr, "File %s does not exist", prog);
|
fprintf(stderr, "File %s does not exist\n", prog);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user