Several improvements ; WIP ignore option

This commit is contained in:
Albert S. 2013-06-12 22:38:52 +02:00
parent f32f9ea00d
commit 771327286e

View File

@ -16,8 +16,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdarg.h> #include <stdarg.h>
#include <errno.h> #include <errno.h>
#include <fnmatch.h>
#define BUF_SIZE (sizeof(struct inotify_event) * 1024) + 255 #define BUF_SIZE (sizeof(struct inotify_event) * 1024) + 255
#define STREQ(s1,s2) ( strcmp(s1,s2) == 0 )
struct ifdLookup struct ifdLookup
{ {
@ -27,10 +28,23 @@ struct ifdLookup
struct ifdLookup *next; struct ifdLookup *next;
}; };
struct ifdLookup *lkp_first = NULL; struct ifdLookup *lkp_head = NULL;
struct ifdLookup **lookup = &lkp_first; 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 *xmalloc(size_t size)
{ {
void *m = malloc(size); void *m = malloc(size);
@ -74,20 +88,39 @@ char *ndirname(const char *path)
char *find_ifd_path(int ifd) char *find_ifd_path(int ifd)
{ {
for(struct ifdLookup *lkp = lkp_first; lkp != NULL; lkp = lkp->next) for(struct ifdLookup *lkp = lkp_head; lkp != NULL; lkp = lkp->next)
if(lkp->ifd == ifd) return lkp->path; if(lkp->ifd == ifd)
return lkp->path;
return NULL; 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, ...) void logwrite(const char *format, ...)
{ {
if(silent) return;
va_list args; va_list args;
va_start(args, format); va_start(args, format);
vfprintf(stderr, format, args); vfprintf(stdout, format, args);
fflush(stderr); fflush(stdout);
va_end(args); va_end(args);
} }
@ -102,7 +135,7 @@ void logerror(const char *format, ...)
vfprintf(stderr, tmp, args); vfprintf(stderr, tmp, args);
fflush(stderr); fflush(stderr);
va_end(args); va_end(args);
} }
void queue_watch(char *pathname) void queue_watch(char *pathname)
@ -116,8 +149,9 @@ void queue_watch(char *pathname)
} }
*lookup = xmalloc(sizeof(struct ifdLookup)); *lookup = xmalloc(sizeof(struct ifdLookup));
struct ifdLookup *lkp = *lookup; struct ifdLookup *lkp = *lookup;
lkp->ifd = 0;
char *path = xrealpath(pathname, NULL); char *path = xrealpath(pathname, NULL);
lkp->ifd = 0;
lkp->path = path; lkp->path = path;
lkp->isdir = S_ISDIR(sb.st_mode); lkp->isdir = S_ISDIR(sb.st_mode);
lkp->next = NULL; lkp->next = NULL;
@ -127,7 +161,7 @@ void queue_watch(char *pathname)
void start_watches(int fd, uint32_t mask) void start_watches(int fd, uint32_t mask)
{ {
struct ifdLookup *lkp = lkp_first; struct ifdLookup *lkp = lkp_head;
for(; lkp != NULL; lkp = lkp->next) for(; lkp != NULL; lkp = lkp->next)
{ {
int ret = inotify_add_watch(fd, lkp->path, mask); 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)) if(! redirect_stdout(outfile))
return false; return false;
} }
char *argv0 = memrchr(path, '/', strlen(path)); char *argv0 = memrchr(path, '/', strlen(path));
if(argv0==NULL) argv0 = ( argv0 == NULL ) ? path : argv0+1;
argv0=path;
else
argv0+=1;
envvar = xmalloc(19 * sizeof(char)); envvar = xmalloc(19 * sizeof(char));
sprintf(envvar, "adhocifyevent=%"PRIu32, mask); sprintf(envvar, "adhocifyevent=%"PRIu32, mask);
putenv(envvar); putenv(envvar);
execl(path, argv0, (! noappend) ? eventfile : NULL, NULL); execl(path, argv0, (! noappend) ? eventfile : NULL, NULL);
perror("execlp"); perror("execlp");
return false; return false;
} }
if(pid == -1) if(pid == -1)
{
perror("fork"); perror("fork");
return false;
}
return true; return true;
@ -206,33 +240,33 @@ bool run( char *path, char *eventfile, const char *outfile, uint32_t mask, bool
uint32_t nameToMask(char *name) uint32_t nameToMask(char *name)
{ {
if(!strcmp(name, "IN_CLOSE_WRITE")) if(STREQ(name, "IN_CLOSE_WRITE"))
return IN_CLOSE_WRITE; return IN_CLOSE_WRITE;
else if(!strcmp(name, "IN_OPEN")) else if(STREQ(name, "IN_OPEN"))
return IN_OPEN; return IN_OPEN;
else if(!strcmp(name, "IN_MODIFY")) else if(STREQ(name, "IN_MODIFY"))
return IN_MODIFY; return IN_MODIFY;
else if(!strcmp(name, "IN_DELETE")) else if(STREQ(name, "IN_DELETE"))
return IN_DELETE; return IN_DELETE;
else if(!strcmp(name, "IN_ATTRIB")) else if(STREQ(name, "IN_ATTRIB"))
return IN_ATTRIB; return IN_ATTRIB;
else if(!strcmp(name, "IN_CLOSE_NOWRITE")) else if(STREQ(name, "IN_CLOSE_NOWRITE"))
return 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; return IN_MOVED_FROM;
else if(!strcmp(name, "IN_MOVED_TO")) else if(STREQ(name, "IN_MOVED_TO"))
return IN_MOVED_TO; return IN_MOVED_TO;
else if(!strcmp(name, "IN_CREATE")) else if(STREQ(name, "IN_CREATE"))
return IN_CREATE; return IN_CREATE;
else if(!strcmp(name, "IN_DELETE_SELF")) else if(STREQ(name, "IN_DELETE_SELF"))
return 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; return IN_DELETE_SELF;
else if(!strcmp(name, "IN_ALL_EVENTS")) else if(STREQ(name, "IN_ALL_EVENTS"))
return IN_ALL_EVENTS; return IN_ALL_EVENTS;
else if(!strcmp(name, "IN_CLOSE")) else if(STREQ(name, "IN_CLOSE"))
return IN_CLOSE; return IN_CLOSE;
else if(!strcmp(name, "IN_MOVE")) else if(STREQ(name, "IN_MOVE"))
return IN_MOVE; return IN_MOVE;
else else
return 0; return 0;
@ -244,15 +278,15 @@ void check_forkbomb(char *dir_log, char *dir_prog)
dir_log = ndirname(dir_log); dir_log = ndirname(dir_log);
dir_prog = ndirname(dir_prog); dir_prog = ndirname(dir_prog);
struct ifdLookup *lkp = lkp_first; struct ifdLookup *lkp = lkp_head;
while(lkp) while(lkp)
{ {
if(lkp->isdir) if(lkp->isdir)
{ {
char *dir_lkpPath = lkp->path; char *dir_lkpPath = lkp->path;
if( !strcmp(dir_lkpPath, dir_log) if( STREQ(dir_lkpPath, dir_log)
|| !strcmp(dir_lkpPath, dir_prog)) || STREQ(dir_lkpPath, dir_prog))
{ {
logerror("Don't place your logfiles or prog in a directory you are watching for events\n"); logerror("Don't place your logfiles or prog in a directory you are watching for events\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -266,7 +300,7 @@ void check_forkbomb(char *dir_log, char *dir_prog)
free(dir_prog); free(dir_prog);
} }
void watches_from_stdin() void queue_watches_from_stdin()
{ {
char *line = NULL; char *line = NULL;
size_t n = 0; size_t n = 0;
@ -283,7 +317,7 @@ char *get_eventfile_abspath(struct inotify_event *event)
char *wdpath = find_ifd_path(event->wd); char *wdpath = find_ifd_path(event->wd);
if(wdpath == NULL) if(wdpath == NULL)
return NULL; return NULL;
size_t nameLen = strlen(event->name); size_t nameLen = strlen(event->name);
char *abspath = xmalloc((strlen(wdpath) + nameLen + 2) * sizeof(char)); char *abspath = xmalloc((strlen(wdpath) + nameLen + 2) * sizeof(char));
strcpy(abspath, wdpath); strcpy(abspath, wdpath);
@ -296,6 +330,34 @@ char *get_eventfile_abspath(struct inotify_event *event)
return abspath; 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) int main(int argc, char **argv)
{ {
uint32_t mask = 0; uint32_t mask = 0;
@ -307,7 +369,7 @@ int main(int argc, char **argv)
int ifd; int ifd;
char *logfile = NULL; char *logfile = NULL;
char *watchpath = NULL; char *watchpath = NULL;
if(argc < 2) if(argc < 2)
{ {
logwrite("Insert usage text here\n"); logwrite("Insert usage text here\n");
@ -351,10 +413,16 @@ int main(int argc, char **argv)
case 'b': case 'b':
forkbombcheck=false; forkbombcheck=false;
break; 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); watchpath = getcwd(NULL,0);
if(watchpath == NULL) if(watchpath == NULL)
@ -364,10 +432,10 @@ int main(int argc, char **argv)
} }
queue_watch(watchpath); queue_watch(watchpath);
} }
if(fromstdin) if(fromstdin)
watches_from_stdin(); queue_watches_from_stdin();
if(mask == 0) mask |= IN_CLOSE_WRITE; if(mask == 0) mask |= IN_CLOSE_WRITE;
if(optind >= argc) if(optind >= argc)
{ {
@ -379,17 +447,17 @@ int main(int argc, char **argv)
if(logfile) if(logfile)
logfile = xrealpath(logfile, NULL); logfile = xrealpath(logfile, NULL);
if(forkbombcheck) if(forkbombcheck)
{ {
char *path_prog = xrealpath(prog, NULL); char *path_prog = xrealpath(prog, NULL);
check_forkbomb(logfile, path_prog); check_forkbomb(logfile, path_prog);
} }
ifd = inotify_init(); ifd = inotify_init();
start_watches(ifd, mask); start_watches(ifd, mask);
while(1) while(1)
{ {
int len; int len;
@ -402,29 +470,13 @@ int main(int argc, char **argv)
perror("read"); perror("read");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while(i < len) while(i < len)
{ {
struct inotify_event *event = (struct inotify_event *)&buf[i]; 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; i+=sizeof(struct inotify_event) + event->len;