Several changes:
-use access() to check for existince of file -check in main() whether prog exists, not just before running it -helptext: tabs -Variables which values are specified by argv-options moved to global scope. They are write-once and don't need to be passed around all the time.
This commit is contained in:
		
							
								
								
									
										83
									
								
								adhocify.c
									
									
									
									
									
								
							
							
						
						
									
										83
									
								
								adhocify.c
									
									
									
									
									
								
							@@ -1,4 +1,5 @@
 | 
				
			|||||||
//The author disclaims copyright to this source code. 
 | 
					//The author disclaims copyright to this source code. 
 | 
				
			||||||
 | 
					// adhocify at quitesimple period org
 | 
				
			||||||
#define  _GNU_SOURCE
 | 
					#define  _GNU_SOURCE
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <stdio.h>
 | 
					#include <stdio.h>
 | 
				
			||||||
@@ -44,7 +45,13 @@ struct ignorelist *ignorelist_head = NULL;
 | 
				
			|||||||
struct ignorelist **ignorelist_current = &ignorelist_head;
 | 
					struct ignorelist **ignorelist_current = &ignorelist_head;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool silent;
 | 
					/* Write once globals */
 | 
				
			||||||
 | 
					bool silent = false;
 | 
				
			||||||
 | 
					bool noappend = false;
 | 
				
			||||||
 | 
					uint32_t mask = 0;
 | 
				
			||||||
 | 
					char *prog = NULL;
 | 
				
			||||||
 | 
					char *path_logfile = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void *xmalloc(size_t size)
 | 
					void *xmalloc(size_t size)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -116,10 +123,9 @@ bool path_is_directory(const char *path)
 | 
				
			|||||||
	return S_ISDIR(sb.st_mode);
 | 
						return S_ISDIR(sb.st_mode);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline bool path_exists(const char *path)
 | 
					static inline bool file_exists(const char *path)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	struct stat sb;
 | 
						return access(path, F_OK) == 0; 
 | 
				
			||||||
	return stat(path, &sb) != -1; 
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -202,36 +208,26 @@ bool redirect_stdout(const char *outfile)
 | 
				
			|||||||
	return true;
 | 
						return true;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//path: file to execute
 | 
					 | 
				
			||||||
//eventfile: path to the file the event occured on 
 | 
					 | 
				
			||||||
//outfile: to log output of the program to be executed
 | 
					 | 
				
			||||||
//mask: occured adhocify event
 | 
					 | 
				
			||||||
//noappend: supply the adhocify event as an environment variable to the program to be executed
 | 
					 | 
				
			||||||
bool run(const char *path, const char *eventfile, const char *outfile, uint32_t mask, bool noappend)
 | 
					 | 
				
			||||||
{
 | 
					 | 
				
			||||||
	if(! path_exists(path))
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		perror("path_exists");
 | 
					 | 
				
			||||||
		return false;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool run_prog(const char *eventfile,  uint32_t eventmask)
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
	pid_t pid = fork();
 | 
						pid_t pid = fork();
 | 
				
			||||||
	if(pid == 0)
 | 
						if(pid == 0)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if(outfile)
 | 
							if(path_logfile)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			if(! redirect_stdout(outfile)) 
 | 
								if(! redirect_stdout(path_logfile)) 
 | 
				
			||||||
				return false;	
 | 
									return false;	
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		const char *argv0 = memrchr(path, '/', strlen(path));
 | 
							const char *argv0 = memrchr(prog, '/', strlen(prog));
 | 
				
			||||||
		argv0 = ( argv0 == NULL ) ? path : argv0+1;
 | 
							argv0 = ( argv0 == NULL ) ? prog : argv0+1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		char envvar[30];
 | 
							char envvar[30];
 | 
				
			||||||
		snprintf(envvar, 30, "adhocifyevent=%"PRIu32, mask); 
 | 
							snprintf(envvar, 30, "adhocifyevent=%"PRIu32, eventmask); 
 | 
				
			||||||
		putenv(envvar);
 | 
							putenv(envvar);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		execl(path, argv0, (! noappend) ? eventfile : NULL, NULL);
 | 
							execl(prog, argv0, (! noappend) ? eventfile : NULL, NULL);
 | 
				
			||||||
		perror("execlp");
 | 
							perror("execlp");
 | 
				
			||||||
		return false;
 | 
							return false;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -338,7 +334,7 @@ char *get_eventfile_abspath(struct inotify_event *event)
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void handle_event(struct inotify_event *event, uint32_t mask, const char *prog, const char *logfile,  bool noappend)
 | 
					void handle_event(struct inotify_event *event)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	if(event->mask & mask) 
 | 
						if(event->mask & mask) 
 | 
				
			||||||
	{	
 | 
						{	
 | 
				
			||||||
@@ -353,7 +349,7 @@ void handle_event(struct inotify_event *event, uint32_t mask, const char *prog,
 | 
				
			|||||||
			return;
 | 
								return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		logwrite("Starting execution of child %s\n", prog);
 | 
							logwrite("Starting execution of child %s\n", prog);
 | 
				
			||||||
		bool r = run(prog, eventfile_abspath, logfile, event->mask, noappend);
 | 
							bool r = run_prog(eventfile_abspath, event->mask);
 | 
				
			||||||
		if(!r) 
 | 
							if(!r) 
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			logerror("Execution of child %s failed\n", prog);
 | 
								logerror("Execution of child %s failed\n", prog);
 | 
				
			||||||
@@ -367,24 +363,24 @@ void handle_event(struct inotify_event *event, uint32_t mask, const char *prog,
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static inline char *cur_wkdir()
 | 
					static inline char *get_cwd()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	return getcwd(NULL,0); 
 | 
						return getcwd(NULL,0); 
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void print_usage()
 | 
					void print_usage()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	printf("adhocify [OPTIONS] script\n");
 | 
						printf("adhocify [OPTIONS]script\n");
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	printf("--daemon, -d		daemonize\n");
 | 
						printf("--daemon, -d\t\t\tdaemonize\n");
 | 
				
			||||||
	printf("--path, -w 		path -- adds the specified path to the watchlist\n");
 | 
						printf("--path, -w\t\t\tpath -- adds the specified path to the watchlist\n");
 | 
				
			||||||
	printf("--logfile, -o		logfile -- output goes here\n");
 | 
						printf("--logfile, -o\t\t\tlogfile -- output goes here\n");
 | 
				
			||||||
	printf("--mask, -m		maskval -- inotify mask value. Can be specified multiple times, will be ORed.\n");
 | 
						printf("--mask, -m\t\t\tmaskval -- inotify mask value. Can be specified multiple times, will be ORed.\n");
 | 
				
			||||||
	printf("--no-env, -a		if specified, the inotify event which occured won't be passed to the script as an envvar.\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		silent\n");
 | 
						printf("--silent, -q\t\t\tsilent\n");
 | 
				
			||||||
	printf("--stdin, -s		Read 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		Disable fork bomb detection\n");
 | 
						printf("--no-forkbomb-check, -b\t\tDisable fork bomb detection\n");
 | 
				
			||||||
	printf("--ignore, -i 		pattern -- 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");  
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static struct option long_options[] = 
 | 
					static struct option long_options[] = 
 | 
				
			||||||
@@ -405,14 +401,11 @@ static struct option long_options[] =
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
int main(int argc, char **argv)
 | 
					int main(int argc, char **argv)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	uint32_t mask = 0;
 | 
					 | 
				
			||||||
	uint32_t optmask = 0; 
 | 
						uint32_t optmask = 0; 
 | 
				
			||||||
	bool noappend=false;
 | 
					 | 
				
			||||||
	bool fromstdin=false;
 | 
						bool fromstdin=false;
 | 
				
			||||||
	bool forkbombcheck=true;
 | 
						bool forkbombcheck=true;
 | 
				
			||||||
	int option;
 | 
						int option;
 | 
				
			||||||
	int ifd;
 | 
						int ifd;
 | 
				
			||||||
	char *path_logfile = NULL;
 | 
					 | 
				
			||||||
	char *watchpath = NULL;
 | 
						char *watchpath = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if(argc < 2) 
 | 
						if(argc < 2) 
 | 
				
			||||||
@@ -476,7 +469,7 @@ int main(int argc, char **argv)
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	if(watchlist_head == NULL)
 | 
						if(watchlist_head == NULL)
 | 
				
			||||||
	{ 
 | 
						{ 
 | 
				
			||||||
		watchpath = cur_wkdir();
 | 
							watchpath = get_cwd();
 | 
				
			||||||
		watchqueue_addpath(watchpath);
 | 
							watchqueue_addpath(watchpath);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -489,7 +482,12 @@ int main(int argc, char **argv)
 | 
				
			|||||||
		exit(EXIT_FAILURE); 
 | 
							exit(EXIT_FAILURE); 
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	char *prog = argv[optind];
 | 
						prog = argv[optind];
 | 
				
			||||||
 | 
						if(! file_exists(prog))
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							perror("file_exists");
 | 
				
			||||||
 | 
							return false;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	if(path_logfile)
 | 
						if(path_logfile)
 | 
				
			||||||
		path_logfile = xrealpath(path_logfile, NULL);
 | 
							path_logfile = xrealpath(path_logfile, NULL);
 | 
				
			||||||
@@ -522,10 +520,7 @@ int main(int argc, char **argv)
 | 
				
			|||||||
		{
 | 
							{
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			struct inotify_event *event = (struct inotify_event *)&buf[offset];
 | 
								struct inotify_event *event = (struct inotify_event *)&buf[offset];
 | 
				
			||||||
 | 
								handle_event(event);
 | 
				
			||||||
			handle_event(event, mask, prog, path_logfile, noappend);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
			offset+=sizeof(struct inotify_event) + event->len;
 | 
								offset+=sizeof(struct inotify_event) + event->len;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}              
 | 
						}              
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user