Compare commits
	
		
			16 Commits
		
	
	
		
			v1.1
			...
			9a5e96f058
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9a5e96f058 | |||
| b9d287b28c | |||
| 8688897a23 | |||
| 4425b31804 | |||
| 25fe05e702 | |||
| cfe2182886 | |||
| 350c4affe5 | |||
| 88063430ce | |||
|   | bf920a533d | ||
|   | 5236c8d947 | ||
| 250e2ae24f | |||
|   | 0a32cc92e8 | ||
| 75b7db7d36 | |||
| cf14367aa8 | |||
| 9925f12675 | |||
| 19e34c1904 | 
							
								
								
									
										19
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								README.md
									
									
									
									
									
								
							| @@ -73,7 +73,6 @@ You can also get a string of the inotify events. This is particularly useful if | |||||||
| ``` | ``` | ||||||
| echo "test" > /tmp/test | echo "test" > /tmp/test | ||||||
| adhocify -m IN_ALL_EVENTS -w /tmp/test echo File: "%eventfilepath%" Event: "%eventmaskstr%" | adhocify -m IN_ALL_EVENTS -w /tmp/test echo File: "%eventfilepath%" Event: "%eventmaskstr%" | ||||||
| #second shell commands |  | ||||||
| Starting execution of command echo | Starting execution of command echo | ||||||
| File: /tmp/test Event: IN_ATTRIB | File: /tmp/test Event: IN_ATTRIB | ||||||
| Starting execution of command echo | Starting execution of command echo | ||||||
| @@ -84,19 +83,19 @@ File: /tmp/test Event: IN_CLOSE,IN_CLOSE_WRITE | |||||||
| File: /tmp/test Event: IN_MODIFY | File: /tmp/test Event: IN_MODIFY | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| A second shell ran | In this example, another shell ran | ||||||
| ``` | ``` | ||||||
| chmod 600 /tmp/test | chmod 600 /tmp/test | ||||||
| echo "test" >> /tmp/test | echo "test" >> /tmp/test | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| Passing ```-q``` would also keep adhocify silent, surpressing those "Starting execution..." messages. | Passing ```-q``` keeps adhocify silent, surpressing those "Starting execution..." messages. | ||||||
|  |  | ||||||
|  |  | ||||||
| Other tools | Other tools | ||||||
| =========== | =========== | ||||||
| If adhocify does not suit your needs, take a look at: | If adhocify does not suit your needs, take a look at: | ||||||
|   * inotify-tools: https://github.com/rvoicilas/inotify-tools/wiki |   * inotify-tools: https://github.com/inotify-tools/inotify-tools/wiki | ||||||
|  |  | ||||||
| Install | Install | ||||||
| ======= | ======= | ||||||
| @@ -104,8 +103,16 @@ Install | |||||||
| ## Debian / Ubuntu | ## Debian / Ubuntu | ||||||
| Latest release can be installed using apt | Latest release can be installed using apt | ||||||
| ``` | ``` | ||||||
| curl -s https://repo.quitesimple.org/repo.quitesimple.org.asc | sudo apt-key add - | # First, obtain key. Here we just assume it's trustworhty. | ||||||
| echo "deb https://repo.quitesimple.org/debian/ default main" | sudo tee /etc/apt/sources.list.d/quitesimple.list | wget -O- https://repo.quitesimple.org/repo.quitesimple.org.asc  | gpg --dearmor > repo.quitesimple.org-keyring.gpg | ||||||
|  | cat repo.quitesimple.org-keyring.gpg | sudo tee -a /usr/share/keyrings/repo.quitesimple.org.gpg > /dev/null | ||||||
|  |  | ||||||
|  | #For Debian | ||||||
|  | echo "deb [arch=amd64 signed-by=/usr/share/keyrings/repo.quitesimple.org.gpg] https://repo.quitesimple.org/debian/ default main" | sudo tee /etc/apt/sources.list.d/quitesimple.list | ||||||
|  |  | ||||||
|  | #For Ubuntu >=21.10, prefer these sources | ||||||
|  | #echo "deb [arch=amd64 signed-by=/usr/share/keyrings/repo.quitesimple.org.gpg] https://repo.quitesimple.org/debian/ $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/quitesimple.list | ||||||
|  |  | ||||||
| sudo apt-get update | sudo apt-get update | ||||||
| sudo apt-get install adhocify | sudo apt-get install adhocify | ||||||
| ``` | ``` | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								TODO
									
									
									
									
									
								
							
							
						
						
									
										4
									
								
								TODO
									
									
									
									
									
								
							| @@ -1,4 +0,0 @@ | |||||||
| -e: if multiple children are running and we exit, they become orphaned. |  | ||||||
| Deal with IN_Q_OVERFLOW etc.? |  | ||||||
| General rename handling, e. g. refresh mappings after renames etc. |  | ||||||
| Deal with IN_IGNORE |  | ||||||
							
								
								
									
										282
									
								
								adhocify.c
									
									
									
									
									
								
							
							
						
						
									
										282
									
								
								adhocify.c
									
									
									
									
									
								
							| @@ -1,5 +1,5 @@ | |||||||
| /* | /* | ||||||
|  * Copyright (c) 2014-2020 Albert S. <adhocify@quitesimple.org> |  * Copyright (c) 2014-2024 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 | ||||||
| @@ -36,10 +36,9 @@ | |||||||
| #include <ftw.h> | #include <ftw.h> | ||||||
| #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 "{}" // same as EVENTFILE_PLACEHOLDER for backwards compatibility | ||||||
| #define SCRIPT_PLACE_SPECIFIER "{}" //same as EVENTFILE_PLACEHOLDER for backwards compatibility  |  | ||||||
| #define EVENTFILE_PLACEHOLDER "%eventfilepath%" | #define EVENTFILE_PLACEHOLDER "%eventfilepath%" | ||||||
| #define EVENTSTR_PLACEHOLDER "%eventmaskstr%" | #define EVENTSTR_PLACEHOLDER "%eventmaskstr%" | ||||||
|  |  | ||||||
| @@ -54,8 +53,6 @@ struct watchlistentry | |||||||
| struct watchlistentry *watchlist_head = NULL; | struct watchlistentry *watchlist_head = NULL; | ||||||
| struct watchlistentry **watchlist = &watchlist_head; | struct watchlistentry **watchlist = &watchlist_head; | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| struct ignorelist | struct ignorelist | ||||||
| { | { | ||||||
| 	char *ignore; | 	char *ignore; | ||||||
| @@ -65,7 +62,6 @@ struct ignorelist | |||||||
| struct ignorelist *ignorelist_head = NULL; | struct ignorelist *ignorelist_head = NULL; | ||||||
| struct ignorelist **ignorelist_current = &ignorelist_head; | 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 noenv = false; | bool noenv = false; | ||||||
| @@ -79,9 +75,11 @@ bool negate_child_exit_code = 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  | char **script_arguments = NULL; // options to be passed to script we are calling | ||||||
| size_t n_script_arguments = 0; | size_t n_script_arguments = 0; | ||||||
|  |  | ||||||
|  | volatile sig_atomic_t handle_child_exits = 0; | ||||||
|  |  | ||||||
| void *xmalloc(size_t size) | void *xmalloc(size_t size) | ||||||
| { | { | ||||||
| 	void *m = malloc(size); | 	void *m = malloc(size); | ||||||
| @@ -116,7 +114,6 @@ char *xrealpath(const char *path, char *resolved_path) | |||||||
| 	return tmp; | 	return tmp; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| char *ndirname(const char *path) | char *ndirname(const char *path) | ||||||
| { | { | ||||||
| 	if(path == NULL) | 	if(path == NULL) | ||||||
| @@ -168,17 +165,14 @@ static inline bool file_exists(const char *path) | |||||||
| 	return access(path, F_OK) == 0; | 	return access(path, F_OK) == 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| void add_to_ignore_list(const char *str) | void add_to_ignore_list(const char *str) | ||||||
| { | { | ||||||
| 	*ignorelist_current = xmalloc(sizeof(struct ignorelist)); | 	*ignorelist_current = xmalloc(sizeof(struct ignorelist)); | ||||||
| 	(*ignorelist_current)->ignore = xstrdup(str); | 	(*ignorelist_current)->ignore = xstrdup(str); | ||||||
|  | 	(*ignorelist_current)->next = NULL; | ||||||
| 	ignorelist_current = &(*ignorelist_current)->next; | 	ignorelist_current = &(*ignorelist_current)->next; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| void logwrite(const char *format, ...) | void logwrite(const char *format, ...) | ||||||
| { | { | ||||||
| 	if(silent) | 	if(silent) | ||||||
| @@ -203,7 +197,6 @@ void logerror(const char *format, ...) | |||||||
| 	vfprintf(stderr, tmp, args); | 	vfprintf(stderr, tmp, args); | ||||||
| 	fflush(stderr); | 	fflush(stderr); | ||||||
| 	va_end(args); | 	va_end(args); | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void watchqueue_add_path(const char *pathname) | void watchqueue_add_path(const char *pathname) | ||||||
| @@ -215,11 +208,9 @@ void watchqueue_add_path(const char *pathname) | |||||||
| 	e->path = path; | 	e->path = path; | ||||||
| 	e->isdir = path_is_directory(pathname); | 	e->isdir = path_is_directory(pathname); | ||||||
| 	e->next = NULL; | 	e->next = NULL; | ||||||
| 	watchlist= &e->next; | 	watchlist = &e->next; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| void create_watches(int fd, uint32_t mask) | void create_watches(int fd, uint32_t mask) | ||||||
| { | { | ||||||
| 	for(struct watchlistentry *lkp = watchlist_head; lkp != NULL; lkp = lkp->next) | 	for(struct watchlistentry *lkp = watchlist_head; lkp != NULL; lkp = lkp->next) | ||||||
| @@ -255,71 +246,72 @@ bool redirect_stdout(const char *outfile) | |||||||
|  |  | ||||||
| const char *mask_to_names(int mask) | const char *mask_to_names(int mask) | ||||||
| { | { | ||||||
| 	static char ret[1024]; | 	char ret[1024] = {0}; | ||||||
| 	size_t n = sizeof(ret) - 1; | 	FILE *f = fmemopen(ret, sizeof(ret), "w"); | ||||||
|  | 	if(f == NULL) | ||||||
|  | 	{ | ||||||
|  | 		logerror("fmemopen() failed\n"); | ||||||
|  | 		return NULL; | ||||||
|  | 	} | ||||||
| 	if(mask & IN_ATTRIB) | 	if(mask & IN_ATTRIB) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_ATTRIB,",n); | 		fputs("IN_ATTRIB,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_OPEN) | 	if(mask & IN_OPEN) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_OPEN,", n); | 		fputs("IN_OPEN,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_CLOSE) | 	if(mask & IN_CLOSE) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_CLOSE,", n); | 		fputs("IN_CLOSE,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_CLOSE_NOWRITE) | 	if(mask & IN_CLOSE_NOWRITE) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_CLOSE,", n); | 		fputs("IN_CLOSE_NOWRITE,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_CLOSE_WRITE) | 	if(mask & IN_CLOSE_WRITE) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_CLOSE_WRITE,", n); | 		fputs("IN_CLOSE_WRITE,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_CREATE) | 	if(mask & IN_CREATE) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_CREATE,", n); | 		fputs("IN_CREATE,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_DELETE) | 	if(mask & IN_DELETE) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_DELETE,", n); | 		fputs("IN_DELETE,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_DELETE_SELF) | 	if(mask & IN_DELETE_SELF) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_DELETE_SELF,", n); | 		fputs("IN_DELETE_SELF,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_MODIFY) | 	if(mask & IN_MODIFY) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_MODIFY,", n); | 		fputs("IN_MODIFY,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_MOVE) | 	if(mask & IN_MOVE) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_MOVE,", n); | 		fputs("IN_MOVE,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_MOVE_SELF) | 	if(mask & IN_MOVE_SELF) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_MOVE_SELF,", n); | 		fputs("IN_MOVE_SELF,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_MOVED_FROM) | 	if(mask & IN_MOVED_FROM) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_MOVED_FROM,", n); | 		fputs("IN_MOVED_FROM,", f); | ||||||
| 	} | 	} | ||||||
| 	if(mask & IN_MOVED_TO) | 	if(mask & IN_MOVED_TO) | ||||||
| 	{ | 	{ | ||||||
| 		strncat(ret, "IN_MOVED_TO,", n); | 		fputs("IN_MOVED_TO,", f); | ||||||
| 	} | 	} | ||||||
|  | 	long pos = ftell(f); | ||||||
| 	for(int i = n; i >= 0; --i) | 	fclose(f); | ||||||
|  | 	if(pos > 0) | ||||||
| 	{ | 	{ | ||||||
| 		if(ret[i] == ',') | 			ret[pos-1] = '\0'; | ||||||
| 		{ |  | ||||||
| 			ret[i] = 0; |  | ||||||
| 			break; |  | ||||||
| 	} | 	} | ||||||
| 	} | 	return xstrdup(ret); | ||||||
| 	ret[1023] = 0; |  | ||||||
| 	return ret; |  | ||||||
| } | } | ||||||
|  |  | ||||||
| bool run_prog(const char *eventfile, uint32_t eventmask) | bool run_prog(const char *eventfile, uint32_t eventmask) | ||||||
| @@ -329,17 +321,22 @@ bool run_prog(const char *eventfile,  uint32_t eventmask) | |||||||
| 	{ | 	{ | ||||||
| 		if(path_logfile) | 		if(path_logfile) | ||||||
| 		{ | 		{ | ||||||
| 			if(! redirect_stdout(path_logfile)) | 			if(!redirect_stdout(path_logfile)) | ||||||
| 			{ | 			{ | ||||||
| 				return false; | 				return false; | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		if(! noenv) | 		if(!noenv) | ||||||
| 		{ | 		{ | ||||||
| 			char envvar[30]; | 			char envvar[30]; | ||||||
| 			snprintf(envvar, sizeof(envvar), "ADHOCIFYEVENT=%"PRIu32, eventmask); | 			snprintf(envvar, sizeof(envvar), "%" PRIu32, eventmask); | ||||||
| 			putenv(envvar); | 			int ret = setenv("ADHOCIFYEVENT", envvar, 1); | ||||||
|  | 			if(ret != 0) | ||||||
|  | 			{ | ||||||
|  | 				perror("setenv"); | ||||||
|  | 				exit(EXIT_FAILURE); | ||||||
|  | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		for(unsigned int i = 0; i < n_script_arguments; i++) | 		for(unsigned int i = 0; i < n_script_arguments; i++) | ||||||
| @@ -353,7 +350,13 @@ bool run_prog(const char *eventfile,  uint32_t eventmask) | |||||||
| 				} | 				} | ||||||
| 				if(STREQ(argument, EVENTSTR_PLACEHOLDER)) | 				if(STREQ(argument, EVENTSTR_PLACEHOLDER)) | ||||||
| 				{ | 				{ | ||||||
| 					script_arguments[i] = mask_to_names(eventmask); | 					const char *names = mask_to_names(eventmask); | ||||||
|  | 					if(names == NULL) | ||||||
|  | 					{ | ||||||
|  | 						logerror("Failed to convert mask to strings"); | ||||||
|  | 						exit(EXIT_FAILURE); | ||||||
|  | 					} | ||||||
|  | 					script_arguments[i] = names; | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| @@ -370,7 +373,6 @@ bool run_prog(const char *eventfile,  uint32_t eventmask) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	return true; | 	return true; | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| uint32_t name_to_mask(const char *name) | uint32_t name_to_mask(const char *name) | ||||||
| @@ -418,9 +420,10 @@ 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)) | ||||||
| 			{ | 			{ | ||||||
| 				logerror("Don't place your logfiles or command in a directory you are watching for events. Pass -b to bypass this check.\n"); | 				logerror("Don't place your logfiles or command in a directory you are watching for events. Pass -b to " | ||||||
|  | 						 "bypass this check.\n"); | ||||||
| 				exit(EXIT_FAILURE); | 				exit(EXIT_FAILURE); | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
| @@ -439,10 +442,11 @@ void queue_watches_from_stdin() | |||||||
| 	ssize_t r; | 	ssize_t r; | ||||||
| 	while((r = getline(&line, &n, stdin)) != -1) | 	while((r = getline(&line, &n, stdin)) != -1) | ||||||
| 	{ | 	{ | ||||||
| 		if(line[r-1] == '\n')  | 		if(line[r - 1] == '\n') | ||||||
| 			line[r-1] = 0; | 			line[r - 1] = 0; | ||||||
| 		watchqueue_add_path(line); | 		watchqueue_add_path(line); | ||||||
| 	} | 	} | ||||||
|  | 	free(line); | ||||||
| } | } | ||||||
|  |  | ||||||
| char *get_eventfile_abspath(struct inotify_event *event) | char *get_eventfile_abspath(struct inotify_event *event) | ||||||
| @@ -454,7 +458,7 @@ char *get_eventfile_abspath(struct inotify_event *event) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	char *result = NULL; | 	char *result = NULL; | ||||||
| 	if((event->len) > 0 ) | 	if((event->len) > 0) | ||||||
| 	{ | 	{ | ||||||
| 		if(asprintf(&result, "%s/%s", wdpath, event->name) == -1) | 		if(asprintf(&result, "%s/%s", wdpath, event->name) == -1) | ||||||
| 		{ | 		{ | ||||||
| @@ -468,7 +472,6 @@ char *get_eventfile_abspath(struct inotify_event *event) | |||||||
| 	return result; | 	return result; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| void handle_event(struct inotify_event *event) | void handle_event(struct inotify_event *event) | ||||||
| { | { | ||||||
| 	if(event->mask & mask) | 	if(event->mask & mask) | ||||||
| @@ -496,13 +499,13 @@ void handle_event(struct inotify_event *event) | |||||||
| 		fflush(stdout); | 		fflush(stdout); | ||||||
| 		fflush(stderr); | 		fflush(stderr); | ||||||
|  |  | ||||||
| 		free (eventfile_abspath); | 		free(eventfile_abspath); | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| static inline char *get_cwd() | static inline char *get_cwd() | ||||||
| { | { | ||||||
| 	return getcwd(NULL,0);  | 	return getcwd(NULL, 0); | ||||||
| } | } | ||||||
|  |  | ||||||
| void print_usage() | void print_usage() | ||||||
| @@ -511,60 +514,64 @@ void print_usage() | |||||||
| 	printf("--daemon, -d            run as a daemon\n"); | 	printf("--daemon, -d            run as a daemon\n"); | ||||||
| 	printf("--path, -w              adds the specified path to the watchlist\n"); | 	printf("--path, -w              adds the specified path to the watchlist\n"); | ||||||
| 	printf("--logfile, -o           path to write output of adhocify and stdout/stderr of launched commands to\n"); | 	printf("--logfile, -o           path to write output of adhocify and stdout/stderr of launched commands to\n"); | ||||||
| 	printf("--mask, -m              inotify event to watch for (see inotify(7)). Can be specified multiple times to watch for several events\n"); | 	printf("--mask, -m              inotify event to watch for (see inotify(7)). Can be specified multiple times to " | ||||||
| 	printf("--no-env, -a            if specified, the inotify event which occured won't be passed to the command as an environment variable\n"); | 		   "watch for several events\n"); | ||||||
|  | 	printf("--no-env, -a            if specified, the inotify event which occured won't be passed to the command as an " | ||||||
|  | 		   "environment variable\n"); | ||||||
| 	printf("--silent, -q            surpress any output created by adhocify itself\n"); | 	printf("--silent, -q            surpress any output created by adhocify itself\n"); | ||||||
| 	printf("--stdin, -s             Read the paths which must be added to the watchlist from stdin. Each path must be in a seperate line\n"); | 	printf("--stdin, -s             Read the paths which must be added to the watchlist from stdin. Each path must be " | ||||||
|  | 		   "in a seperate line.\n"); | ||||||
| 	printf("--no-forkbomb-check, -b Disable fork bomb detection\n"); | 	printf("--no-forkbomb-check, -b Disable fork bomb detection\n"); | ||||||
| 	printf("--ignore, -i            Shell wildcard pattern (see glob(7)) to ignore events on files for which the pattern matches\n"); | 	printf("--ignore, -i            Shell wildcard pattern (see glob(7)) to ignore events on files for which the " | ||||||
| 	printf("--exit-with-child, -e   Exit when the commands exits. You can also specify a return code (e. g. -e=1 to exit only on errors)\n");	printf("\nIf your command should know the file the event occured on, use the {} placeholder when you specify the arguments (like xargs)\n"); | 		   "pattern matches\n"); | ||||||
|  | 	printf("--exit-with-child, -e   Exit with the command. You can also specify a return code and negations (e. g. -e'!0' to " | ||||||
|  | 		   "exit only on errors)\n"); | ||||||
|  | 	printf("\nIf your command should know the file the event occured on, use the {} placeholder when you specify the " | ||||||
|  | 		   "arguments (like xargs)\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| static struct option long_options[] =  | static struct option long_options[] = {{"daemon", no_argument, 0, 'd'}, | ||||||
| { | 									   {"logfile", required_argument, 0, 'o'}, | ||||||
|   { "daemon", no_argument, 0, 'd' }, | 									   {"mask", required_argument, 0, 'm'}, | ||||||
|   { "logfile", required_argument, 0, 'o' }, | 									   {"path", required_argument, 0, 'w'}, | ||||||
|   { "mask", required_argument, 0, 'm' }, | 									   {"no-env", no_argument, 0, 'a'}, | ||||||
|   { "path", required_argument, 0, 'w' }, | 									   {"stdin", no_argument, 0, 's'}, | ||||||
|   { "no-env", no_argument, 0, 'a' }, | 									   {"no-forkbomb-check", no_argument, 0, 'b'}, | ||||||
|   { "stdin", no_argument, 0, 's' }, | 									   {"ignore", required_argument, 0, 'i'}, | ||||||
|   { "no-forkbomb-check", no_argument, 0, 'b' }, | 									   {"silent", no_argument, 0, 'q'}, | ||||||
|   { "ignore", required_argument, 0, 'i' },   | 									   {"help", no_argument, 0, 'h'}, | ||||||
|   { "silent", no_argument, 0, 'q' }, | 									   {"exit-with-child", optional_argument, 0, 'e'}, | ||||||
|   { "help", no_argument, 0, 'h' }, | 									   {0,0,0,0}}; | ||||||
|   { "exit-with-child", optional_argument, 0, 'e' } |  | ||||||
| }; |  | ||||||
|  |  | ||||||
| //fills global n_script_arguments and script_arguments var | // fills global n_script_arguments and script_arguments var | ||||||
| void fill_script_arguments(size_t n_args, char *args[]) | void fill_script_arguments(size_t n_args, char *args[]) | ||||||
| { | { | ||||||
| 		n_script_arguments = n_args + 2; //2 = argv0 and terminating NULL | 	n_script_arguments = n_args + 2; // 2 = argv0 and terminating NULL | ||||||
|  |  | ||||||
| 		char **arguments = xmalloc( n_script_arguments * sizeof(char *) );  | 	char **arguments = xmalloc(n_script_arguments * sizeof(char *)); | ||||||
|  |  | ||||||
| 	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; | 	arguments[0] = xstrdup(argv0); | ||||||
|  |  | ||||||
| 	const int begin_offset = 1; | 	const int begin_offset = 1; | ||||||
| 	for(unsigned int i = 0; i < n_args; i++) | 	for(unsigned int i = 0; i < n_args; i++) | ||||||
| 	{ | 	{ | ||||||
| 		char *argument = args[i]; | 		char *argument = args[i]; | ||||||
| 			arguments[i+begin_offset] = strdup(argument);  | 		arguments[i + begin_offset] = xstrdup(argument); | ||||||
| 	} | 	} | ||||||
| 		arguments[n_args+begin_offset] = NULL; | 	arguments[n_args + begin_offset] = NULL; | ||||||
|  |  | ||||||
| 	script_arguments = arguments; | 	script_arguments = arguments; | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| void parse_options(int argc, char **argv) | void parse_options(int argc, char **argv) | ||||||
| { | { | ||||||
| 	char *watchpath = NULL; | 	char *watchpath = NULL; | ||||||
| 	int option; | 	int option; | ||||||
| 	int option_index; | 	int option_index; | ||||||
| 	uint32_t optmask = 0; | 	uint32_t optmask = 0; | ||||||
| 	while((option = getopt_long(argc, argv, "absdo:w:m:l:i:e::", long_options, &option_index)) != -1) | 	while((option = getopt_long(argc, argv, "absdo:w:m:i:e::", long_options, &option_index)) != -1) | ||||||
| 	{ | 	{ | ||||||
| 		switch(option) | 		switch(option) | ||||||
| 		{ | 		{ | ||||||
| @@ -576,8 +583,9 @@ void parse_options(int argc, char **argv) | |||||||
| 			break; | 			break; | ||||||
| 		case 'm': | 		case 'm': | ||||||
| 			optmask = name_to_mask(optarg); | 			optmask = name_to_mask(optarg); | ||||||
| 				if(optmask == 0) {  | 			if(optmask == 0) | ||||||
| 					logerror("Not supported inotify event: %s\n", optarg); | 			{ | ||||||
|  | 				logerror("Unsupported inotify event: %s\n", optarg); | ||||||
| 				exit(EXIT_FAILURE); | 				exit(EXIT_FAILURE); | ||||||
| 			} | 			} | ||||||
| 			mask |= optmask; | 			mask |= optmask; | ||||||
| @@ -587,19 +595,19 @@ void parse_options(int argc, char **argv) | |||||||
| 			watchqueue_add_path(watchpath); | 			watchqueue_add_path(watchpath); | ||||||
| 			break; | 			break; | ||||||
| 		case 'a': | 		case 'a': | ||||||
| 				noenv=true; | 			noenv = true; | ||||||
| 			break; | 			break; | ||||||
| 		case 's': | 		case 's': | ||||||
| 				fromstdin=true; | 			fromstdin = true; | ||||||
| 			break; | 			break; | ||||||
| 		case 'b': | 		case 'b': | ||||||
| 				forkbombcheck=false; | 			forkbombcheck = false; | ||||||
| 			break; | 			break; | ||||||
| 		case 'i': | 		case 'i': | ||||||
| 			add_to_ignore_list(optarg); | 			add_to_ignore_list(optarg); | ||||||
| 			break; | 			break; | ||||||
| 		case 'q': | 		case 'q': | ||||||
| 				silent=true; | 			silent = true; | ||||||
| 			break; | 			break; | ||||||
| 		case 'h': | 		case 'h': | ||||||
| 			print_usage(); | 			print_usage(); | ||||||
| @@ -673,7 +681,7 @@ void process_options() | |||||||
|  |  | ||||||
| 	if(daemonize) | 	if(daemonize) | ||||||
| 	{ | 	{ | ||||||
| 		if(daemon(0,0) == -1) | 		if(daemon(0, 0) == -1) | ||||||
| 		{ | 		{ | ||||||
| 			perror("daemon"); | 			perror("daemon"); | ||||||
| 			exit(EXIT_FAILURE); | 			exit(EXIT_FAILURE); | ||||||
| @@ -681,47 +689,22 @@ void process_options() | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
| void start_monitoring(int ifd) |  | ||||||
|  | void wait_for_children() | ||||||
| { | { | ||||||
| 	while(1) | 	while(1) | ||||||
| 	{ | 	{ | ||||||
| 			int len; | 		int status; | ||||||
| 			int offset =0; | 		pid_t p = waitpid(-1, &status, WNOHANG); | ||||||
| 			char buf[BUF_SIZE]; | 		if(p == 0 || (p == -1 && errno == ECHILD)) // No more children to reap | ||||||
| 			len = read(ifd, buf, BUF_SIZE); |  | ||||||
| 			if(len == -1)  |  | ||||||
| 			{ |  | ||||||
| 				if(errno == EINTR)  |  | ||||||
| 					continue; |  | ||||||
| 				perror("read"); |  | ||||||
| 				exit(EXIT_FAILURE); |  | ||||||
| 			} |  | ||||||
|  |  | ||||||
| 			while(offset < len) |  | ||||||
| 			{ |  | ||||||
|  |  | ||||||
| 				struct inotify_event *event = (struct inotify_event *)&buf[offset]; |  | ||||||
| 				handle_event(event); |  | ||||||
| 				offset+=sizeof(struct inotify_event) + event->len; |  | ||||||
| 			} |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
|  |  | ||||||
| void child_handler(int signum, siginfo_t *info, void *context) |  | ||||||
| { |  | ||||||
| 	if(signum != SIGCHLD) |  | ||||||
| 		{ | 		{ | ||||||
| 			return; | 			return; | ||||||
| 		} | 		} | ||||||
| 	 |  | ||||||
| 	int status; |  | ||||||
| 	pid_t p = waitpid(-1, &status, WNOHANG); |  | ||||||
| 		if(p == -1) | 		if(p == -1) | ||||||
| 		{ | 		{ | ||||||
| 		logerror("waitpid failed when handling child exit\n"); | 			logerror("waitpid() failed when handling child exit\n"); | ||||||
| 			exit(EXIT_FAILURE); | 			exit(EXIT_FAILURE); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		int adhocify_exit_code = 0; | 		int adhocify_exit_code = 0; | ||||||
| 		if(WIFEXITED(status)) | 		if(WIFEXITED(status)) | ||||||
| 		{ | 		{ | ||||||
| @@ -731,9 +714,9 @@ void child_handler(int signum, siginfo_t *info, void *context) | |||||||
| 				logwrite("command not found, exiting\n"); | 				logwrite("command not found, exiting\n"); | ||||||
| 				exit(adhocify_exit_code); | 				exit(adhocify_exit_code); | ||||||
| 			} | 			} | ||||||
| 		if(exit_with_child && awaited_child_exit_code > -1) | 			if(exit_with_child) | ||||||
| 			{ | 			{ | ||||||
| 			bool must_exit = adhocify_exit_code == awaited_child_exit_code; | 				bool must_exit = adhocify_exit_code == awaited_child_exit_code || awaited_child_exit_code == -1; | ||||||
| 				if(negate_child_exit_code) | 				if(negate_child_exit_code) | ||||||
| 				{ | 				{ | ||||||
| 					must_exit = !must_exit; | 					must_exit = !must_exit; | ||||||
| @@ -747,16 +730,57 @@ void child_handler(int signum, siginfo_t *info, void *context) | |||||||
| 		} | 		} | ||||||
| 		if(exit_with_child && 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 | ||||||
| 			exit(adhocify_exit_code); | 			exit(adhocify_exit_code); | ||||||
| 		} | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  | void start_monitoring(int ifd) | ||||||
|  | { | ||||||
|  | 	while(1) | ||||||
|  | 	{ | ||||||
|  | 		if(handle_child_exits) | ||||||
|  | 		{ | ||||||
|  | 			handle_child_exits = 0; | ||||||
|  | 			wait_for_children(); | ||||||
|  | 		} | ||||||
|  | 		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); | ||||||
|  | 		} | ||||||
|  | 		while(offset < len) | ||||||
|  | 		{ | ||||||
|  | 			struct inotify_event *event = (struct inotify_event *)&buf[offset]; | ||||||
|  | 			handle_event(event); | ||||||
|  | 			offset += sizeof(struct inotify_event) + event->len; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | void child_handler(int signum, siginfo_t *info, void *context) | ||||||
|  | { | ||||||
|  | 	if(signum != SIGCHLD) | ||||||
|  | 	{ | ||||||
|  | 		return; | ||||||
|  | 	} | ||||||
|  | 	handle_child_exits = 1; | ||||||
| } | } | ||||||
|  |  | ||||||
| void set_signals() | void set_signals() | ||||||
| { | { | ||||||
| 	struct sigaction action; | 	struct sigaction action = {0}; | ||||||
| 	action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; | 	action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO; | ||||||
| 	action.sa_sigaction = &child_handler; | 	action.sa_sigaction = &child_handler; | ||||||
|  | 	sigemptyset(&action.sa_mask); | ||||||
| 	if(sigaction(SIGCHLD, &action, NULL) == -1) | 	if(sigaction(SIGCHLD, &action, NULL) == -1) | ||||||
| 	{ | 	{ | ||||||
| 		logerror("Error when setting up the signal handler\n"); | 		logerror("Error when setting up the signal handler\n"); | ||||||
| @@ -764,7 +788,6 @@ void set_signals() | |||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
| int main(int argc, char **argv) | int main(int argc, char **argv) | ||||||
| { | { | ||||||
| 	if(argc < 2) | 	if(argc < 2) | ||||||
| @@ -773,17 +796,16 @@ int main(int argc, char **argv) | |||||||
| 		exit(EXIT_FAILURE); | 		exit(EXIT_FAILURE); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	//signal(SIGCHLD, SIG_IGN); | 	// signal(SIGCHLD, SIG_IGN); | ||||||
| 	set_signals(); | 	set_signals(); | ||||||
|  |  | ||||||
| 	 |  | ||||||
| 	parse_options(argc, argv); | 	parse_options(argc, argv); | ||||||
| 	process_options(); | 	process_options(); | ||||||
|  |  | ||||||
| 	int ifd = inotify_init(); | 	int ifd = inotify_init1(O_CLOEXEC); | ||||||
| 	if(ifd == -1) | 	if(ifd == -1) | ||||||
| 	{ | 	{ | ||||||
| 		perror("inotify_init"); | 		perror("inotify_init1"); | ||||||
| 		exit(EXIT_FAILURE); | 		exit(EXIT_FAILURE); | ||||||
| 	} | 	} | ||||||
| 	create_watches(ifd, mask); | 	create_watches(ifd, mask); | ||||||
|   | |||||||
| @@ -1,19 +0,0 @@ | |||||||
| #!/bin/sh |  | ||||||
| #example: encrypt files once they get written to a directory and remove them |  | ||||||
| #launch with: adhocify -w /path/encryptin /path/to/this/script.sh {} |  | ||||||
| #This is a simple example and has security flaws: |  | ||||||
| #-no secure delete (better to use e. g. ramfs or tmpfs...) |  | ||||||
| #-then still not necessarily secure against people who can dump the content of the memory |  | ||||||
| set -e |  | ||||||
| DESTINATION="/tmp/store" |  | ||||||
| if [ -z "$1" ] ; then |  | ||||||
| echo "Need path to encrypt" >&2 |  | ||||||
| exit 1 |  | ||||||
| fi |  | ||||||
| sleep 2 #some clients may want to set permissions and so on after writing |  | ||||||
| FILEPATH="$1" |  | ||||||
| gpg -e -r mail@example.com -o $DESTINATION/$(basename $FILEPATH) $FILEPATH  |  | ||||||
| rm $FILEPATH |  | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| @@ -1,9 +1,10 @@ | |||||||
| #!/bin/bash | #!/bin/bash | ||||||
| #moves all incoming files (e. g. downloads) to another directory.  | #Hardlinks all incoming files (e. g. downloads) to another directory. | ||||||
| #There, they will be put into subdirectories which are named after the current date (YYYYMMDD) to get some minimal automatic "organization".   | #There, they will be put into subdirectories which are named after the current date (YYYYMMDD) to get some minimal automatic "organization".   | ||||||
| #adhocify -d -m IN_CLOSE_WRITE -m IN_MOVED_TO -w /home/user/Downloads -w /home/user/other_dir /path/to/move_downloads.sh | #adhocify -d -m IN_CLOSE_WRITE -m IN_MOVED_TO -w /home/user/Downloads -w /home/user/other_dir /path/to/move_downloads.sh | ||||||
|  |  | ||||||
| INCOMING="$1" | INCOMING="$1" | ||||||
|  | stat -c"%s" "$INCOMING" | grep -q "^0$" && exit | ||||||
| #ignore partial downloads (.part in firefox, .crdownload in chrome) | #ignore partial downloads (.part in firefox, .crdownload in chrome) | ||||||
| echo "$INCOMING" | grep -q .part$ && exit  | echo "$INCOMING" | grep -q .part$ && exit  | ||||||
| echo "$INCOMING" | grep -q .crdownload$ && exit | echo "$INCOMING" | grep -q .crdownload$ && exit | ||||||
| @@ -11,11 +12,12 @@ today=$(date +%Y%m%d) | |||||||
| TARGET_DIR="/target/dir/path" | TARGET_DIR="/target/dir/path" | ||||||
| TODAY_DIR="$TARGET_DIR"/$today | TODAY_DIR="$TARGET_DIR"/$today | ||||||
| if [ ! -d "$TODAY_DIR" ] ; then | if [ ! -d "$TODAY_DIR" ] ; then | ||||||
| mkdir "$TODAY_DIR" | 	mkdir "$TODAY_DIR" | ||||||
| rm -f "$TARGET_DIR"/today | 	rm -f "$TARGET_DIR"/today | ||||||
| ln -s "$TODAY_DIR" "$TARGET_DIR"/today | 	ln -s "$TODAY_DIR" "$TARGET_DIR"/today | ||||||
| fi | fi | ||||||
| #You can also filter/grep the filename here and move certain patterns to other designated locations... | # Nowadays, some browsers don't like it when files are moved away immediately and might report download failure. Use hardlinks so they don't complain. | ||||||
| mv "$INCOMING" "$TARGET_DIR"/$today/ | # Alternatively, a sufficiently long enough "sleep" followed by "mv" might work | ||||||
|  | ln "$INCOMING" "$TARGET_DIR"/$today/ | ||||||
|  |  | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user