Several improvements ; WIP ignore option
This commit is contained in:
父節點
f32f9ea00d
當前提交
771327286e
174
adhocify.c
174
adhocify.c
@ -16,8 +16,9 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdarg.h>
|
||||
#include <errno.h>
|
||||
#include <fnmatch.h>
|
||||
#define BUF_SIZE (sizeof(struct inotify_event) * 1024) + 255
|
||||
|
||||
#define STREQ(s1,s2) ( strcmp(s1,s2) == 0 )
|
||||
|
||||
struct ifdLookup
|
||||
{
|
||||
@ -27,10 +28,23 @@ struct ifdLookup
|
||||
struct ifdLookup *next;
|
||||
};
|
||||
|
||||
struct ifdLookup *lkp_first = NULL;
|
||||
struct ifdLookup **lookup = &lkp_first;
|
||||
struct ifdLookup *lkp_head = NULL;
|
||||
struct ifdLookup **lookup = &lkp_head;
|
||||
|
||||
|
||||
|
||||
struct ignorelist
|
||||
{
|
||||
char *ignore;
|
||||
struct ignorelist *next;
|
||||
};
|
||||
|
||||
struct ignorelist *ignorelist_head = NULL;
|
||||
struct ignorelist **ignorelist_current = &ignorelist_head;
|
||||
|
||||
|
||||
bool silent;
|
||||
|
||||
void *xmalloc(size_t size)
|
||||
{
|
||||
void *m = malloc(size);
|
||||
@ -74,20 +88,39 @@ char *ndirname(const char *path)
|
||||
|
||||
char *find_ifd_path(int ifd)
|
||||
{
|
||||
for(struct ifdLookup *lkp = lkp_first; lkp != NULL; lkp = lkp->next)
|
||||
if(lkp->ifd == ifd) return lkp->path;
|
||||
for(struct ifdLookup *lkp = lkp_head; lkp != NULL; lkp = lkp->next)
|
||||
if(lkp->ifd == ifd)
|
||||
return lkp->path;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool is_ignored(const char *str)
|
||||
{
|
||||
for(struct ignorelist *l = ignorelist_head; l != NULL; l = l->next)
|
||||
if(fnmatch(str, l->ignore, 0) == 0)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void add_ignore_list(const char *str)
|
||||
{
|
||||
*ignorelist_current = xmalloc(sizeof(struct ignorelist));
|
||||
(*ignorelist_current)->ignore = xstrdup(str);
|
||||
|
||||
ignorelist_current = &(*ignorelist_current)->next;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void logwrite(const char *format, ...)
|
||||
{
|
||||
if(silent) return;
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vfprintf(stderr, format, args);
|
||||
fflush(stderr);
|
||||
vfprintf(stdout, format, args);
|
||||
fflush(stdout);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
@ -102,7 +135,7 @@ void logerror(const char *format, ...)
|
||||
vfprintf(stderr, tmp, args);
|
||||
fflush(stderr);
|
||||
va_end(args);
|
||||
|
||||
|
||||
}
|
||||
|
||||
void queue_watch(char *pathname)
|
||||
@ -116,8 +149,9 @@ void queue_watch(char *pathname)
|
||||
}
|
||||
*lookup = xmalloc(sizeof(struct ifdLookup));
|
||||
struct ifdLookup *lkp = *lookup;
|
||||
lkp->ifd = 0;
|
||||
char *path = xrealpath(pathname, NULL);
|
||||
|
||||
lkp->ifd = 0;
|
||||
lkp->path = path;
|
||||
lkp->isdir = S_ISDIR(sb.st_mode);
|
||||
lkp->next = NULL;
|
||||
@ -127,7 +161,7 @@ void queue_watch(char *pathname)
|
||||
void start_watches(int fd, uint32_t mask)
|
||||
{
|
||||
|
||||
struct ifdLookup *lkp = lkp_first;
|
||||
struct ifdLookup *lkp = lkp_head;
|
||||
for(; lkp != NULL; lkp = lkp->next)
|
||||
{
|
||||
int ret = inotify_add_watch(fd, lkp->path, mask);
|
||||
@ -182,23 +216,23 @@ bool run( char *path, char *eventfile, const char *outfile, uint32_t mask, bool
|
||||
if(! redirect_stdout(outfile))
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
char *argv0 = memrchr(path, '/', strlen(path));
|
||||
if(argv0==NULL)
|
||||
argv0=path;
|
||||
else
|
||||
argv0+=1;
|
||||
argv0 = ( argv0 == NULL ) ? path : argv0+1;
|
||||
|
||||
envvar = xmalloc(19 * sizeof(char));
|
||||
sprintf(envvar, "adhocifyevent=%"PRIu32, mask);
|
||||
putenv(envvar);
|
||||
|
||||
|
||||
execl(path, argv0, (! noappend) ? eventfile : NULL, NULL);
|
||||
perror("execlp");
|
||||
return false;
|
||||
}
|
||||
if(pid == -1)
|
||||
{
|
||||
perror("fork");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
@ -206,33 +240,33 @@ bool run( char *path, char *eventfile, const char *outfile, uint32_t mask, bool
|
||||
|
||||
uint32_t nameToMask(char *name)
|
||||
{
|
||||
if(!strcmp(name, "IN_CLOSE_WRITE"))
|
||||
if(STREQ(name, "IN_CLOSE_WRITE"))
|
||||
return IN_CLOSE_WRITE;
|
||||
else if(!strcmp(name, "IN_OPEN"))
|
||||
else if(STREQ(name, "IN_OPEN"))
|
||||
return IN_OPEN;
|
||||
else if(!strcmp(name, "IN_MODIFY"))
|
||||
else if(STREQ(name, "IN_MODIFY"))
|
||||
return IN_MODIFY;
|
||||
else if(!strcmp(name, "IN_DELETE"))
|
||||
else if(STREQ(name, "IN_DELETE"))
|
||||
return IN_DELETE;
|
||||
else if(!strcmp(name, "IN_ATTRIB"))
|
||||
else if(STREQ(name, "IN_ATTRIB"))
|
||||
return IN_ATTRIB;
|
||||
else if(!strcmp(name, "IN_CLOSE_NOWRITE"))
|
||||
else if(STREQ(name, "IN_CLOSE_NOWRITE"))
|
||||
return IN_CLOSE_NOWRITE;
|
||||
else if(!strcmp(name, "IN_MOVED_FROM"))
|
||||
else if(STREQ(name, "IN_MOVED_FROM"))
|
||||
return IN_MOVED_FROM;
|
||||
else if(!strcmp(name, "IN_MOVED_TO"))
|
||||
else if(STREQ(name, "IN_MOVED_TO"))
|
||||
return IN_MOVED_TO;
|
||||
else if(!strcmp(name, "IN_CREATE"))
|
||||
else if(STREQ(name, "IN_CREATE"))
|
||||
return IN_CREATE;
|
||||
else if(!strcmp(name, "IN_DELETE_SELF"))
|
||||
else if(STREQ(name, "IN_DELETE_SELF"))
|
||||
return IN_DELETE_SELF;
|
||||
else if(!strcmp(name, "IN_MOVE_SELF"))
|
||||
else if(STREQ(name, "IN_MOVE_SELF"))
|
||||
return IN_DELETE_SELF;
|
||||
else if(!strcmp(name, "IN_ALL_EVENTS"))
|
||||
else if(STREQ(name, "IN_ALL_EVENTS"))
|
||||
return IN_ALL_EVENTS;
|
||||
else if(!strcmp(name, "IN_CLOSE"))
|
||||
else if(STREQ(name, "IN_CLOSE"))
|
||||
return IN_CLOSE;
|
||||
else if(!strcmp(name, "IN_MOVE"))
|
||||
else if(STREQ(name, "IN_MOVE"))
|
||||
return IN_MOVE;
|
||||
else
|
||||
return 0;
|
||||
@ -244,15 +278,15 @@ void check_forkbomb(char *dir_log, char *dir_prog)
|
||||
dir_log = ndirname(dir_log);
|
||||
dir_prog = ndirname(dir_prog);
|
||||
|
||||
struct ifdLookup *lkp = lkp_first;
|
||||
struct ifdLookup *lkp = lkp_head;
|
||||
while(lkp)
|
||||
{
|
||||
if(lkp->isdir)
|
||||
{
|
||||
|
||||
char *dir_lkpPath = lkp->path;
|
||||
if( !strcmp(dir_lkpPath, dir_log)
|
||||
|| !strcmp(dir_lkpPath, dir_prog))
|
||||
if( STREQ(dir_lkpPath, dir_log)
|
||||
|| STREQ(dir_lkpPath, dir_prog))
|
||||
{
|
||||
logerror("Don't place your logfiles or prog in a directory you are watching for events\n");
|
||||
exit(EXIT_FAILURE);
|
||||
@ -266,7 +300,7 @@ void check_forkbomb(char *dir_log, char *dir_prog)
|
||||
free(dir_prog);
|
||||
}
|
||||
|
||||
void watches_from_stdin()
|
||||
void queue_watches_from_stdin()
|
||||
{
|
||||
char *line = NULL;
|
||||
size_t n = 0;
|
||||
@ -283,7 +317,7 @@ char *get_eventfile_abspath(struct inotify_event *event)
|
||||
char *wdpath = find_ifd_path(event->wd);
|
||||
if(wdpath == NULL)
|
||||
return NULL;
|
||||
|
||||
|
||||
size_t nameLen = strlen(event->name);
|
||||
char *abspath = xmalloc((strlen(wdpath) + nameLen + 2) * sizeof(char));
|
||||
strcpy(abspath, wdpath);
|
||||
@ -296,6 +330,34 @@ char *get_eventfile_abspath(struct inotify_event *event)
|
||||
return abspath;
|
||||
}
|
||||
|
||||
|
||||
void handle_event(struct inotify_event *event, uint32_t mask, char *prog, const char *logfile, bool noappend)
|
||||
{
|
||||
if(event->mask & mask)
|
||||
{
|
||||
char *eventfile_abspath = get_eventfile_abspath(event);
|
||||
if(eventfile_abspath == NULL)
|
||||
{
|
||||
logerror("Could not get absoulte path for event. Watch descriptor %i\n", event->wd);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
logwrite("Starting execution of child %s\n", prog);
|
||||
bool r = run(prog, eventfile_abspath, logfile, event->mask, noappend);
|
||||
if(!r)
|
||||
{
|
||||
logerror("Execution of child %s failed\n", prog);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
|
||||
free (eventfile_abspath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
uint32_t mask = 0;
|
||||
@ -307,7 +369,7 @@ int main(int argc, char **argv)
|
||||
int ifd;
|
||||
char *logfile = NULL;
|
||||
char *watchpath = NULL;
|
||||
|
||||
|
||||
if(argc < 2)
|
||||
{
|
||||
logwrite("Insert usage text here\n");
|
||||
@ -351,10 +413,16 @@ int main(int argc, char **argv)
|
||||
case 'b':
|
||||
forkbombcheck=false;
|
||||
break;
|
||||
case 'i':
|
||||
add_ignore_list(optarg);
|
||||
break;
|
||||
case 'q':
|
||||
silent=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(lkp_first == NULL)
|
||||
if(lkp_head == NULL)
|
||||
{
|
||||
watchpath = getcwd(NULL,0);
|
||||
if(watchpath == NULL)
|
||||
@ -364,10 +432,10 @@ int main(int argc, char **argv)
|
||||
}
|
||||
queue_watch(watchpath);
|
||||
}
|
||||
|
||||
|
||||
if(fromstdin)
|
||||
watches_from_stdin();
|
||||
|
||||
queue_watches_from_stdin();
|
||||
|
||||
if(mask == 0) mask |= IN_CLOSE_WRITE;
|
||||
if(optind >= argc)
|
||||
{
|
||||
@ -379,17 +447,17 @@ int main(int argc, char **argv)
|
||||
|
||||
if(logfile)
|
||||
logfile = xrealpath(logfile, NULL);
|
||||
|
||||
|
||||
|
||||
if(forkbombcheck)
|
||||
{
|
||||
char *path_prog = xrealpath(prog, NULL);
|
||||
check_forkbomb(logfile, path_prog);
|
||||
}
|
||||
|
||||
|
||||
ifd = inotify_init();
|
||||
start_watches(ifd, mask);
|
||||
|
||||
|
||||
while(1)
|
||||
{
|
||||
int len;
|
||||
@ -402,29 +470,13 @@ int main(int argc, char **argv)
|
||||
perror("read");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
|
||||
while(i < len)
|
||||
{
|
||||
|
||||
struct inotify_event *event = (struct inotify_event *)&buf[i];
|
||||
if(event->mask & mask)
|
||||
{
|
||||
char *eventfile_abspath = get_eventfile_abspath(event);
|
||||
if(eventfile_abspath == NULL)
|
||||
{
|
||||
logerror("Could not get absoulte path for event. Watch descriptor %i\n", event->wd);
|
||||
break;
|
||||
}
|
||||
|
||||
logwrite("Starting execution of child %s\n", prog);
|
||||
int r = run(prog, eventfile_abspath, logfile, event->mask, noappend);
|
||||
if(!r)
|
||||
logerror("Execution of child %s failed\n", prog);
|
||||
fflush(stdout);
|
||||
fflush(stderr);
|
||||
free (eventfile_abspath);
|
||||
}
|
||||
|
||||
handle_event(event, mask, prog, logfile, noappend);
|
||||
|
||||
|
||||
i+=sizeof(struct inotify_event) + event->len;
|
||||
|
載入中…
新增問題並參考
Block a user