Commitok összehasonlítása
11 Commit-ok
0a32cc92e8
...
master
Szerző | SHA1 | Dátum | |
---|---|---|---|
9a5e96f058 | |||
b9d287b28c | |||
8688897a23 | |||
4425b31804 | |||
25fe05e702 | |||
cfe2182886 | |||
350c4affe5 | |||
88063430ce | |||
bf920a533d | |||
5236c8d947 | |||
250e2ae24f |
16
README.md
16
README.md
@ -83,13 +83,13 @@ File: /tmp/test Event: IN_CLOSE,IN_CLOSE_WRITE
|
||||
File: /tmp/test Event: IN_MODIFY
|
||||
|
||||
```
|
||||
A second shell ran
|
||||
In this example, another shell ran
|
||||
```
|
||||
chmod 600 /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
|
||||
@ -103,8 +103,16 @@ Install
|
||||
## Debian / Ubuntu
|
||||
Latest release can be installed using apt
|
||||
```
|
||||
curl -s https://repo.quitesimple.org/repo.quitesimple.org.asc | sudo apt-key add -
|
||||
echo "deb https://repo.quitesimple.org/debian/ default main" | sudo tee /etc/apt/sources.list.d/quitesimple.list
|
||||
# First, obtain key. Here we just assume it's trustworhty.
|
||||
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 install adhocify
|
||||
```
|
||||
|
176
adhocify.c
176
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
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
@ -78,6 +78,8 @@ char *path_logfile = NULL;
|
||||
char **script_arguments = NULL; // options to be passed to script we are calling
|
||||
size_t n_script_arguments = 0;
|
||||
|
||||
volatile sig_atomic_t handle_child_exits = 0;
|
||||
|
||||
void *xmalloc(size_t size)
|
||||
{
|
||||
void *m = malloc(size);
|
||||
@ -167,7 +169,7 @@ void add_to_ignore_list(const char *str)
|
||||
{
|
||||
*ignorelist_current = xmalloc(sizeof(struct ignorelist));
|
||||
(*ignorelist_current)->ignore = xstrdup(str);
|
||||
|
||||
(*ignorelist_current)->next = NULL;
|
||||
ignorelist_current = &(*ignorelist_current)->next;
|
||||
}
|
||||
|
||||
@ -244,71 +246,72 @@ bool redirect_stdout(const char *outfile)
|
||||
|
||||
const char *mask_to_names(int mask)
|
||||
{
|
||||
static char ret[1024];
|
||||
size_t n = sizeof(ret) - 1;
|
||||
char ret[1024] = {0};
|
||||
FILE *f = fmemopen(ret, sizeof(ret), "w");
|
||||
if(f == NULL)
|
||||
{
|
||||
logerror("fmemopen() failed\n");
|
||||
return NULL;
|
||||
}
|
||||
if(mask & IN_ATTRIB)
|
||||
{
|
||||
strncat(ret, "IN_ATTRIB,", n);
|
||||
fputs("IN_ATTRIB,", f);
|
||||
}
|
||||
if(mask & IN_OPEN)
|
||||
{
|
||||
strncat(ret, "IN_OPEN,", n);
|
||||
fputs("IN_OPEN,", f);
|
||||
}
|
||||
if(mask & IN_CLOSE)
|
||||
{
|
||||
strncat(ret, "IN_CLOSE,", n);
|
||||
fputs("IN_CLOSE,", f);
|
||||
}
|
||||
if(mask & IN_CLOSE_NOWRITE)
|
||||
{
|
||||
strncat(ret, "IN_CLOSE,", n);
|
||||
fputs("IN_CLOSE_NOWRITE,", f);
|
||||
}
|
||||
if(mask & IN_CLOSE_WRITE)
|
||||
{
|
||||
strncat(ret, "IN_CLOSE_WRITE,", n);
|
||||
fputs("IN_CLOSE_WRITE,", f);
|
||||
}
|
||||
if(mask & IN_CREATE)
|
||||
{
|
||||
strncat(ret, "IN_CREATE,", n);
|
||||
fputs("IN_CREATE,", f);
|
||||
}
|
||||
if(mask & IN_DELETE)
|
||||
{
|
||||
strncat(ret, "IN_DELETE,", n);
|
||||
fputs("IN_DELETE,", f);
|
||||
}
|
||||
if(mask & IN_DELETE_SELF)
|
||||
{
|
||||
strncat(ret, "IN_DELETE_SELF,", n);
|
||||
fputs("IN_DELETE_SELF,", f);
|
||||
}
|
||||
if(mask & IN_MODIFY)
|
||||
{
|
||||
strncat(ret, "IN_MODIFY,", n);
|
||||
fputs("IN_MODIFY,", f);
|
||||
}
|
||||
if(mask & IN_MOVE)
|
||||
{
|
||||
strncat(ret, "IN_MOVE,", n);
|
||||
fputs("IN_MOVE,", f);
|
||||
}
|
||||
if(mask & IN_MOVE_SELF)
|
||||
{
|
||||
strncat(ret, "IN_MOVE_SELF,", n);
|
||||
fputs("IN_MOVE_SELF,", f);
|
||||
}
|
||||
if(mask & IN_MOVED_FROM)
|
||||
{
|
||||
strncat(ret, "IN_MOVED_FROM,", n);
|
||||
fputs("IN_MOVED_FROM,", f);
|
||||
}
|
||||
if(mask & IN_MOVED_TO)
|
||||
{
|
||||
strncat(ret, "IN_MOVED_TO,", n);
|
||||
fputs("IN_MOVED_TO,", f);
|
||||
}
|
||||
|
||||
for(int i = n; i >= 0; --i)
|
||||
long pos = ftell(f);
|
||||
fclose(f);
|
||||
if(pos > 0)
|
||||
{
|
||||
if(ret[i] == ',')
|
||||
{
|
||||
ret[i] = 0;
|
||||
break;
|
||||
ret[pos-1] = '\0';
|
||||
}
|
||||
}
|
||||
ret[1023] = 0;
|
||||
return ret;
|
||||
return xstrdup(ret);
|
||||
}
|
||||
|
||||
bool run_prog(const char *eventfile, uint32_t eventmask)
|
||||
@ -327,8 +330,13 @@ bool run_prog(const char *eventfile, uint32_t eventmask)
|
||||
if(!noenv)
|
||||
{
|
||||
char envvar[30];
|
||||
snprintf(envvar, sizeof(envvar), "ADHOCIFYEVENT=%" PRIu32, eventmask);
|
||||
putenv(envvar);
|
||||
snprintf(envvar, sizeof(envvar), "%" PRIu32, eventmask);
|
||||
int ret = setenv("ADHOCIFYEVENT", envvar, 1);
|
||||
if(ret != 0)
|
||||
{
|
||||
perror("setenv");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
for(unsigned int i = 0; i < n_script_arguments; i++)
|
||||
@ -342,7 +350,13 @@ bool run_prog(const char *eventfile, uint32_t eventmask)
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -432,6 +446,7 @@ void queue_watches_from_stdin()
|
||||
line[r - 1] = 0;
|
||||
watchqueue_add_path(line);
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
|
||||
char *get_eventfile_abspath(struct inotify_event *event)
|
||||
@ -505,11 +520,11 @@ void print_usage()
|
||||
"environment variable\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");
|
||||
"in a seperate line.\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("--exit-with-child, -e Exit when the commands exits. You can also specify a return code (e. g. -e=1 to "
|
||||
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");
|
||||
@ -525,7 +540,8 @@ static struct option long_options[] = {{"daemon", no_argument, 0, 'd'},
|
||||
{"ignore", required_argument, 0, 'i'},
|
||||
{"silent", no_argument, 0, 'q'},
|
||||
{"help", no_argument, 0, 'h'},
|
||||
{"exit-with-child", optional_argument, 0, 'e'}};
|
||||
{"exit-with-child", optional_argument, 0, 'e'},
|
||||
{0,0,0,0}};
|
||||
|
||||
// fills global n_script_arguments and script_arguments var
|
||||
void fill_script_arguments(size_t n_args, char *args[])
|
||||
@ -536,13 +552,13 @@ void fill_script_arguments(size_t n_args, char *args[])
|
||||
|
||||
const char *argv0 = memrchr(prog, '/', strlen(prog));
|
||||
argv0 = (argv0 == NULL) ? prog : argv0 + 1;
|
||||
arguments[0] = argv0;
|
||||
arguments[0] = xstrdup(argv0);
|
||||
|
||||
const int begin_offset = 1;
|
||||
for(unsigned int i = 0; i < n_args; i++)
|
||||
{
|
||||
char *argument = args[i];
|
||||
arguments[i + begin_offset] = strdup(argument);
|
||||
arguments[i + begin_offset] = xstrdup(argument);
|
||||
}
|
||||
arguments[n_args + begin_offset] = NULL;
|
||||
|
||||
@ -555,7 +571,7 @@ void parse_options(int argc, char **argv)
|
||||
int option;
|
||||
int option_index;
|
||||
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)
|
||||
{
|
||||
@ -569,7 +585,7 @@ void parse_options(int argc, char **argv)
|
||||
optmask = name_to_mask(optarg);
|
||||
if(optmask == 0)
|
||||
{
|
||||
logerror("Not supported inotify event: %s\n", optarg);
|
||||
logerror("Unsupported inotify event: %s\n", optarg);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
mask |= optmask;
|
||||
@ -673,47 +689,22 @@ void process_options()
|
||||
}
|
||||
}
|
||||
|
||||
void start_monitoring(int ifd)
|
||||
|
||||
void wait_for_children()
|
||||
{
|
||||
while(1)
|
||||
{
|
||||
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)
|
||||
int status;
|
||||
pid_t p = waitpid(-1, &status, WNOHANG);
|
||||
if(p == 0 || (p == -1 && errno == ECHILD)) // No more children to reap
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int status;
|
||||
pid_t p = waitpid(-1, &status, WNOHANG);
|
||||
if(p == -1)
|
||||
{
|
||||
logerror("waitpid failed when handling child exit\n");
|
||||
logerror("waitpid() failed when handling child exit\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
int adhocify_exit_code = 0;
|
||||
if(WIFEXITED(status))
|
||||
{
|
||||
@ -723,9 +714,9 @@ void child_handler(int signum, siginfo_t *info, void *context)
|
||||
logwrite("command not found, exiting\n");
|
||||
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)
|
||||
{
|
||||
must_exit = !must_exit;
|
||||
@ -742,13 +733,54 @@ void child_handler(int signum, siginfo_t *info, void *context)
|
||||
adhocify_exit_code = 128 + WTERMSIG(status); // copy bash's behaviour
|
||||
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()
|
||||
{
|
||||
struct sigaction action;
|
||||
struct sigaction action = {0};
|
||||
action.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
|
||||
action.sa_sigaction = &child_handler;
|
||||
sigemptyset(&action.sa_mask);
|
||||
if(sigaction(SIGCHLD, &action, NULL) == -1)
|
||||
{
|
||||
logerror("Error when setting up the signal handler\n");
|
||||
@ -770,10 +802,10 @@ int main(int argc, char **argv)
|
||||
parse_options(argc, argv);
|
||||
process_options();
|
||||
|
||||
int ifd = inotify_init();
|
||||
int ifd = inotify_init1(O_CLOEXEC);
|
||||
if(ifd == -1)
|
||||
{
|
||||
perror("inotify_init");
|
||||
perror("inotify_init1");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
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,5 +1,5 @@
|
||||
#!/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".
|
||||
#adhocify -d -m IN_CLOSE_WRITE -m IN_MOVED_TO -w /home/user/Downloads -w /home/user/other_dir /path/to/move_downloads.sh
|
||||
|
||||
@ -12,11 +12,12 @@ today=$(date +%Y%m%d)
|
||||
TARGET_DIR="/target/dir/path"
|
||||
TODAY_DIR="$TARGET_DIR"/$today
|
||||
if [ ! -d "$TODAY_DIR" ] ; then
|
||||
mkdir "$TODAY_DIR"
|
||||
rm -f "$TARGET_DIR"/today
|
||||
ln -s "$TODAY_DIR" "$TARGET_DIR"/today
|
||||
mkdir "$TODAY_DIR"
|
||||
rm -f "$TARGET_DIR"/today
|
||||
ln -s "$TODAY_DIR" "$TARGET_DIR"/today
|
||||
fi
|
||||
#You can also filter/grep the filename here and move certain patterns to other designated locations...
|
||||
mv "$INCOMING" "$TARGET_DIR"/$today/
|
||||
# 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.
|
||||
# Alternatively, a sufficiently long enough "sleep" followed by "mv" might work
|
||||
ln "$INCOMING" "$TARGET_DIR"/$today/
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user