2014-04-11 19:46:09 +02:00
|
|
|
/*The author disclaims copyright to this source code: Public domain.
|
|
|
|
* Do whatever you want with it.
|
|
|
|
* Contact: adhocify at quitesimple period org
|
|
|
|
*/
|
2013-04-21 22:36:59 +02:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/inotify.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <libgen.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
2013-06-12 22:38:52 +02:00
|
|
|
#include <fnmatch.h>
|
2013-09-21 20:45:10 +02:00
|
|
|
#include <getopt.h>
|
2014-04-11 19:46:09 +02:00
|
|
|
#include <linux/limits.h>
|
|
|
|
#define BUF_SIZE (sizeof(struct inotify_event) + NAME_MAX + 1) * 1024
|
2013-06-12 22:38:52 +02:00
|
|
|
#define STREQ(s1,s2) ( strcmp(s1,s2) == 0 )
|
2013-04-21 22:36:59 +02:00
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
struct watchlistentry
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
|
|
|
int ifd;
|
|
|
|
char *path;
|
|
|
|
bool isdir;
|
2013-08-29 14:41:55 +02:00
|
|
|
struct watchlistentry *next;
|
2013-04-21 22:36:59 +02:00
|
|
|
};
|
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
struct watchlistentry *watchlist_head = NULL;
|
|
|
|
struct watchlistentry **watchlist = &watchlist_head;
|
2013-06-12 22:38:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct ignorelist
|
|
|
|
{
|
|
|
|
char *ignore;
|
|
|
|
struct ignorelist *next;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ignorelist *ignorelist_head = NULL;
|
|
|
|
struct ignorelist **ignorelist_current = &ignorelist_head;
|
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
|
2014-04-11 19:46:09 +02:00
|
|
|
/* Write once globals. Set from process_arguments*/
|
2013-12-16 21:13:07 +01:00
|
|
|
bool silent = false;
|
|
|
|
bool noappend = false;
|
2014-04-11 19:46:09 +02:00
|
|
|
bool fromstdin = false;
|
|
|
|
bool forkbombcheck = true;
|
|
|
|
bool daemonize = false;
|
2013-12-16 21:13:07 +01:00
|
|
|
uint32_t mask = 0;
|
|
|
|
char *prog = NULL;
|
|
|
|
char *path_logfile = NULL;
|
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
|
|
|
|
void *xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
void *m = malloc(size);
|
|
|
|
if(m == NULL)
|
|
|
|
{
|
|
|
|
perror("malloc");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrdup(const char *s)
|
|
|
|
{
|
|
|
|
char *tmp = strdup(s);
|
|
|
|
if(tmp == NULL)
|
|
|
|
{
|
|
|
|
perror("strdup");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *xrealpath(const char *path, char *resolved_path)
|
|
|
|
{
|
|
|
|
char *tmp = realpath(path, resolved_path);
|
|
|
|
if(tmp == NULL)
|
|
|
|
{
|
|
|
|
perror("realpath");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char *ndirname(const char *path)
|
|
|
|
{
|
2013-08-29 14:41:55 +02:00
|
|
|
if(path == NULL)
|
|
|
|
return xstrdup(".");
|
2013-04-21 22:36:59 +02:00
|
|
|
char *c = strdupa(path);
|
|
|
|
return xstrdup(dirname(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
char *find_ifd_path(int ifd)
|
|
|
|
{
|
2013-08-29 14:41:55 +02:00
|
|
|
for(struct watchlistentry *lkp = watchlist_head; lkp != NULL; lkp = lkp->next)
|
2013-06-12 22:38:52 +02:00
|
|
|
if(lkp->ifd == ifd)
|
|
|
|
return lkp->path;
|
2013-04-21 22:36:59 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
bool is_ignored(const char *filename)
|
2013-06-12 22:38:52 +02:00
|
|
|
{
|
|
|
|
for(struct ignorelist *l = ignorelist_head; l != NULL; l = l->next)
|
2013-08-29 14:41:55 +02:00
|
|
|
if(fnmatch(l->ignore, filename, 0) == 0)
|
2013-06-12 22:38:52 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
bool path_is_directory(const char *path)
|
|
|
|
{
|
|
|
|
struct stat sb;
|
|
|
|
int r = stat(path, &sb);
|
|
|
|
if(r == -1)
|
|
|
|
{
|
|
|
|
perror("stat");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return S_ISDIR(sb.st_mode);
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
static inline bool file_exists(const char *path)
|
2013-08-29 14:41:55 +02:00
|
|
|
{
|
2013-12-16 21:13:07 +01:00
|
|
|
return access(path, F_OK) == 0;
|
2013-08-29 14:41:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-12 22:38:52 +02:00
|
|
|
void add_ignore_list(const char *str)
|
|
|
|
{
|
|
|
|
*ignorelist_current = xmalloc(sizeof(struct ignorelist));
|
|
|
|
(*ignorelist_current)->ignore = xstrdup(str);
|
|
|
|
|
|
|
|
ignorelist_current = &(*ignorelist_current)->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
|
|
|
|
void logwrite(const char *format, ...)
|
|
|
|
{
|
2014-03-05 18:22:12 +01:00
|
|
|
if(silent)
|
|
|
|
return;
|
2013-04-21 22:36:59 +02:00
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
2013-06-12 22:38:52 +02:00
|
|
|
vfprintf(stdout, format, args);
|
|
|
|
fflush(stdout);
|
2013-04-21 22:36:59 +02:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void logerror(const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
char *prefix = "Error: ";
|
|
|
|
char *tmp = alloca(strlen(format) + strlen(prefix) + 1);
|
|
|
|
strcpy(tmp, prefix);
|
|
|
|
strcat(tmp, format);
|
|
|
|
vfprintf(stderr, tmp, args);
|
|
|
|
fflush(stderr);
|
|
|
|
va_end(args);
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
}
|
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
void watchqueue_addpath(const char *pathname)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
2013-08-29 14:41:55 +02:00
|
|
|
*watchlist = xmalloc(sizeof(struct watchlistentry));
|
|
|
|
struct watchlistentry *e = *watchlist;
|
2013-04-21 22:36:59 +02:00
|
|
|
char *path = xrealpath(pathname, NULL);
|
2013-08-29 14:41:55 +02:00
|
|
|
e->ifd = 0;
|
|
|
|
e->path = path;
|
|
|
|
e->isdir = path_is_directory(pathname);
|
|
|
|
e->next = NULL;
|
|
|
|
watchlist= &e->next;
|
2013-04-21 22:36:59 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 19:46:09 +02:00
|
|
|
void create_watches(int fd, uint32_t mask)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
2013-08-29 14:41:55 +02:00
|
|
|
for(struct watchlistentry *lkp = watchlist_head; lkp != NULL; lkp = lkp->next)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
|
|
|
int ret = inotify_add_watch(fd, lkp->path, mask);
|
|
|
|
if(ret == -1)
|
|
|
|
{
|
|
|
|
perror("inotify_add_watch");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
lkp->ifd = ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool redirect_stdout(const char *outfile)
|
|
|
|
{
|
|
|
|
int fd = open(outfile, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
|
|
|
|
if(fd == -1)
|
|
|
|
{
|
|
|
|
perror("open");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dup2(fd, 1) == -1 || dup2(fd, 2) == -1)
|
|
|
|
{
|
|
|
|
perror("dup2");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
|
|
|
|
bool run_prog(const char *eventfile, uint32_t eventmask)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if(pid == 0)
|
|
|
|
{
|
2013-12-16 21:13:07 +01:00
|
|
|
if(path_logfile)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
2013-12-16 21:13:07 +01:00
|
|
|
if(! redirect_stdout(path_logfile))
|
2013-04-21 22:36:59 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
const char *argv0 = memrchr(prog, '/', strlen(prog));
|
|
|
|
argv0 = ( argv0 == NULL ) ? prog : argv0+1;
|
2013-04-21 22:36:59 +02:00
|
|
|
|
2013-09-30 20:10:22 +02:00
|
|
|
char envvar[30];
|
2014-10-10 15:09:49 +02:00
|
|
|
snprintf(envvar, sizeof(envvar), "adhocifyevent=%"PRIu32, eventmask);
|
2013-04-21 22:36:59 +02:00
|
|
|
putenv(envvar);
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
execl(prog, argv0, (! noappend) ? eventfile : NULL, NULL);
|
2013-04-21 22:36:59 +02:00
|
|
|
perror("execlp");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(pid == -1)
|
2013-06-12 22:38:52 +02:00
|
|
|
{
|
2013-04-21 22:36:59 +02:00
|
|
|
perror("fork");
|
2013-06-12 22:38:52 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-21 22:36:59 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:22:12 +01:00
|
|
|
uint32_t nameToMask(const char *name)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
2013-06-12 22:38:52 +02:00
|
|
|
if(STREQ(name, "IN_CLOSE_WRITE"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_CLOSE_WRITE;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_OPEN"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_OPEN;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_MODIFY"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_MODIFY;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_DELETE"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_DELETE;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_ATTRIB"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_ATTRIB;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_CLOSE_NOWRITE"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_CLOSE_NOWRITE;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_MOVED_FROM"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_MOVED_FROM;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_MOVED_TO"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_MOVED_TO;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_CREATE"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_CREATE;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_DELETE_SELF"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_DELETE_SELF;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_MOVE_SELF"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_DELETE_SELF;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_ALL_EVENTS"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_ALL_EVENTS;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_CLOSE"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_CLOSE;
|
2013-06-12 22:38:52 +02:00
|
|
|
else if(STREQ(name, "IN_MOVE"))
|
2013-04-21 22:36:59 +02:00
|
|
|
return IN_MOVE;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
void check_forkbomb(const char *path_logfile, const char *path_prog)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
2013-08-29 14:41:55 +02:00
|
|
|
char *dir_log = ndirname(path_logfile);
|
|
|
|
char *dir_prog = ndirname(path_prog);
|
2013-04-21 22:36:59 +02:00
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
struct watchlistentry *lkp = watchlist_head;
|
2013-04-21 22:36:59 +02:00
|
|
|
while(lkp)
|
|
|
|
{
|
|
|
|
if(lkp->isdir)
|
|
|
|
{
|
|
|
|
|
|
|
|
char *dir_lkpPath = lkp->path;
|
2013-08-29 14:41:55 +02:00
|
|
|
if( STREQ(dir_lkpPath, dir_log) || STREQ(dir_lkpPath, dir_prog) )
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
2013-08-29 14:41:55 +02:00
|
|
|
logerror("Don't place your logfiles or prog in a directory you are watching for events. Pass -b to bypass this check.\n");
|
2013-04-21 22:36:59 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lkp = lkp->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dir_log);
|
|
|
|
free(dir_prog);
|
|
|
|
}
|
|
|
|
|
2013-06-12 22:38:52 +02:00
|
|
|
void queue_watches_from_stdin()
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
|
|
|
char *line = NULL;
|
2013-09-30 19:45:48 +02:00
|
|
|
size_t n = 0;
|
2013-04-21 22:36:59 +02:00
|
|
|
ssize_t r;
|
|
|
|
while((r = getline(&line, &n, stdin)) != -1)
|
|
|
|
{
|
2013-09-21 20:45:10 +02:00
|
|
|
if(line[r-1] == '\n')
|
|
|
|
line[r-1] = 0;
|
2013-08-29 14:41:55 +02:00
|
|
|
watchqueue_addpath(line);
|
2013-04-21 22:36:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char *get_eventfile_abspath(struct inotify_event *event)
|
|
|
|
{
|
|
|
|
char *wdpath = find_ifd_path(event->wd);
|
|
|
|
if(wdpath == NULL)
|
|
|
|
return NULL;
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
size_t nameLen = strlen(event->name);
|
|
|
|
char *abspath = xmalloc((strlen(wdpath) + nameLen + 2) * sizeof(char));
|
|
|
|
strcpy(abspath, wdpath);
|
|
|
|
if(nameLen > 0)
|
|
|
|
{
|
|
|
|
strcat(abspath, "/");
|
|
|
|
strcat(abspath, event->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return abspath;
|
|
|
|
}
|
|
|
|
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
void handle_event(struct inotify_event *event)
|
2013-06-12 22:38:52 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2013-06-12 22:55:30 +02:00
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
if(is_ignored(eventfile_abspath))
|
2014-03-05 18:22:12 +01:00
|
|
|
{
|
|
|
|
free(eventfile_abspath);
|
2013-08-29 14:41:55 +02:00
|
|
|
return;
|
2014-03-05 18:22:12 +01:00
|
|
|
}
|
2013-06-12 22:38:52 +02:00
|
|
|
logwrite("Starting execution of child %s\n", prog);
|
2013-12-16 21:13:07 +01:00
|
|
|
bool r = run_prog(eventfile_abspath, event->mask);
|
2013-06-12 22:38:52 +02:00
|
|
|
if(!r)
|
|
|
|
{
|
|
|
|
logerror("Execution of child %s failed\n", prog);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
|
|
|
|
|
|
|
free (eventfile_abspath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
static inline char *get_cwd()
|
2013-08-29 14:41:55 +02:00
|
|
|
{
|
|
|
|
return getcwd(NULL,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_usage()
|
|
|
|
{
|
2013-12-16 21:13:07 +01:00
|
|
|
printf("adhocify [OPTIONS]script\n");
|
2013-08-29 14:41:55 +02:00
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
printf("--daemon, -d\t\t\tdaemonize\n");
|
|
|
|
printf("--path, -w\t\t\tpath -- adds the specified path to the watchlist\n");
|
|
|
|
printf("--logfile, -o\t\t\tlogfile -- output goes here\n");
|
|
|
|
printf("--mask, -m\t\t\tmaskval -- inotify mask value. Can be specified multiple times, will be ORed.\n");
|
|
|
|
printf("--no-env, -a\t\t\tif specified, the inotify event which occured won't be passed to the script as an envvar.\n");
|
|
|
|
printf("--silent, -q\t\t\tsilent\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("--ignore, -i\t\t\tpattern -- Ignore events on files for which the pattern matches\n");
|
2013-08-29 14:41:55 +02:00
|
|
|
}
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-09-21 20:45:10 +02:00
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
|
|
|
{ "daemon", no_argument, 0, 'd' },
|
|
|
|
{ "logfile", required_argument, 0, 'o' },
|
|
|
|
{ "mask", required_argument, 0, 'm' },
|
|
|
|
{ "path", required_argument, 0, 'w' },
|
|
|
|
{ "no-env", no_argument, 0, 'a' },
|
|
|
|
{ "stdin", no_argument, 0, 's' },
|
|
|
|
{ "no-forkbomb-check", no_argument, 0, 'b' },
|
|
|
|
{ "ignore", required_argument, 0, 'i' },
|
|
|
|
{ "silent", no_argument, 0, 'q' },
|
|
|
|
{ "help", no_argument, 0, 'h' }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-04-11 19:46:09 +02:00
|
|
|
void parse_options(int argc, char **argv)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
|
|
|
char *watchpath = NULL;
|
2014-03-05 18:22:12 +01:00
|
|
|
int option;
|
2013-09-21 20:45:10 +02:00
|
|
|
int option_index;
|
2014-04-11 19:46:09 +02:00
|
|
|
uint32_t optmask = 0;
|
2013-09-21 20:45:10 +02:00
|
|
|
while((option = getopt_long(argc, argv, "absdo:w:m:l:i:", long_options, &option_index)) != -1)
|
2013-04-21 22:36:59 +02:00
|
|
|
{
|
|
|
|
switch(option)
|
|
|
|
{
|
|
|
|
case 'd':
|
2014-04-11 19:46:09 +02:00
|
|
|
daemonize = true;
|
2013-04-21 22:36:59 +02:00
|
|
|
break;
|
|
|
|
case 'o':
|
2013-08-29 14:41:55 +02:00
|
|
|
path_logfile = optarg;
|
2013-04-21 22:36:59 +02:00
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
optmask = nameToMask(optarg);
|
|
|
|
if(optmask == 0) {
|
|
|
|
logerror("Not supported inotify event: %s\n", optmask);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
mask |= optmask;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
watchpath = optarg;
|
2013-08-29 14:41:55 +02:00
|
|
|
watchqueue_addpath(watchpath);
|
2013-04-21 22:36:59 +02:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
noappend=true;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
fromstdin=true;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
forkbombcheck=false;
|
|
|
|
break;
|
2013-06-12 22:38:52 +02:00
|
|
|
case 'i':
|
|
|
|
add_ignore_list(optarg);
|
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
silent=true;
|
|
|
|
break;
|
2013-08-29 14:41:55 +02:00
|
|
|
case 'h':
|
|
|
|
print_usage();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
break;
|
2013-04-21 22:36:59 +02:00
|
|
|
}
|
|
|
|
}
|
2013-08-29 14:41:55 +02:00
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
if(optind >= argc)
|
|
|
|
{
|
|
|
|
logerror("missing prog/script path\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2013-12-16 21:13:07 +01:00
|
|
|
prog = argv[optind];
|
2014-04-11 19:46:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void process_options()
|
|
|
|
{
|
|
|
|
if(fromstdin)
|
|
|
|
queue_watches_from_stdin();
|
|
|
|
|
|
|
|
if(daemonize)
|
2013-12-16 21:13:07 +01:00
|
|
|
{
|
2014-04-11 19:46:09 +02:00
|
|
|
if(daemon(0,0) == -1)
|
|
|
|
{
|
|
|
|
perror("daemon");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(watchlist_head == NULL)
|
|
|
|
watchqueue_addpath(get_cwd());
|
|
|
|
|
|
|
|
if(mask == 0)
|
|
|
|
mask |= IN_CLOSE_WRITE;
|
|
|
|
|
|
|
|
if(! prog || ! file_exists(prog))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "File %s does not exist", prog);
|
|
|
|
exit(EXIT_FAILURE);
|
2013-12-16 21:13:07 +01:00
|
|
|
}
|
|
|
|
|
2013-08-29 14:41:55 +02:00
|
|
|
if(path_logfile)
|
|
|
|
path_logfile = xrealpath(path_logfile, NULL);
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2013-04-21 22:36:59 +02:00
|
|
|
|
|
|
|
if(forkbombcheck)
|
|
|
|
{
|
|
|
|
char *path_prog = xrealpath(prog, NULL);
|
2013-08-29 14:41:55 +02:00
|
|
|
check_forkbomb(path_logfile, path_prog);
|
2013-04-21 22:36:59 +02:00
|
|
|
}
|
2014-04-11 19:46:09 +02:00
|
|
|
}
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2014-04-11 19:46:09 +02:00
|
|
|
void start_monitoring(int ifd)
|
|
|
|
{
|
2013-04-21 22:36:59 +02:00
|
|
|
while(1)
|
|
|
|
{
|
2014-04-11 19:46:09 +02:00
|
|
|
int len;
|
|
|
|
int offset =0;
|
|
|
|
char buf[BUF_SIZE];
|
|
|
|
len = read(ifd, buf, BUF_SIZE);
|
|
|
|
if(len == -1)
|
|
|
|
{
|
|
|
|
if(errno == EINTR)
|
|
|
|
continue;
|
|
|
|
perror("read");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2013-06-12 22:38:52 +02:00
|
|
|
|
2014-04-11 19:46:09 +02:00
|
|
|
while(offset < len)
|
|
|
|
{
|
2013-04-21 22:36:59 +02:00
|
|
|
|
2014-04-11 19:46:09 +02:00
|
|
|
struct inotify_event *event = (struct inotify_event *)&buf[offset];
|
|
|
|
handle_event(event);
|
|
|
|
offset+=sizeof(struct inotify_event) + event->len;
|
|
|
|
}
|
2013-04-21 22:36:59 +02:00
|
|
|
}
|
|
|
|
}
|
2014-04-11 19:46:09 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
if(argc < 2)
|
|
|
|
{
|
|
|
|
print_usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
signal(SIGCHLD, SIG_IGN);
|
|
|
|
|
|
|
|
parse_options(argc, argv);
|
|
|
|
process_options();
|
|
|
|
|
|
|
|
int ifd = inotify_init();
|
|
|
|
if(ifd == -1)
|
|
|
|
{
|
|
|
|
perror("inotify_init");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
create_watches(ifd, mask);
|
|
|
|
start_monitoring(ifd);
|
|
|
|
}
|