diff --git a/.gitignore b/.gitignore index f2d6762..58b734c 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,4 @@ watcher lts store ALLOWED_USERS.txt +ltscleanerd diff --git a/.watcherignore b/.watcherignore index d91b37d..a867c31 100644 --- a/.watcherignore +++ b/.watcherignore @@ -1 +1,2 @@ ./lts +./ltscleanerd diff --git a/Makefile b/Makefile index 30e6b0b..ecda64a 100644 --- a/Makefile +++ b/Makefile @@ -10,9 +10,12 @@ endif all: lts watcher -lts: lts.go uuid.go +lts: ltscleanerd lts.go uuid.go go build -ldflags="-X main.LISTEN_ADDR=$(LISTEN_ADDR)" +ltscleanerd: ltscleanerd.c + cc -o ltscleanerd ltscleanerd.c + watcher: watcher.c cc -o $@ $< diff --git a/ltscleanerd.c b/ltscleanerd.c new file mode 100644 index 0000000..494def5 --- /dev/null +++ b/ltscleanerd.c @@ -0,0 +1,75 @@ +//go:build exclude + +#include +#include +#define GEBS_NO_PREFIX +#define GEBS_IMPLEMENTATION +#include "gebs/gebs.h" + +char *prog = nil; +char *file_path = nil; + +void timer_handler(int sig, siginfo_t *si, void *uc) +{ + if (file_path != nil) { + remove1(file_path); + exit(0); + } +} + +void make_timer(char *name, long seconds) +{ + struct sigevent timer_event; + struct itimerspec timer_spec; + struct sigaction action; + int sig_no = SIGRTMIN; + + action.sa_flags = SA_SIGINFO; + action.sa_sigaction = &timer_handler; + sigemptyset(&action.sa_mask); + + if (sigaction(sig_no, &action, nil) == -1) { + LOGE("Could not create timer signal handler\n"); + exit(1); + } + + timer_event.sigev_notify = SIGEV_SIGNAL; + timer_event.sigev_signo = sig_no; + timer_event.sigev_value.sival_ptr = name; + + timer_t timer_id; + if (timer_create(CLOCK_REALTIME, &timer_event, &timer_id) == -1) { + LOGE("Could not create timer\n"); + exit(1); + } + + memset(&timer_spec, 0, sizeof(timer_spec)); + timer_spec.it_value.tv_sec = seconds; + + if (timer_settime(timer_id, 0, &timer_spec, nil) == -1) { + LOGE("Could not set the timeout\n"); + exit(1); + } +} + +int main(int argc, char ** argv) +{ + prog = SHIFT(&argc, &argv); + file_path = SHIFT(&argc, &argv); + char *timeout_string = SHIFT(&argc, &argv); + + if (!exists1(file_path)) { + LOGI("Path %s does not exist\n", file_path); + return 1; + } + + char *endp; + long timeout = strtol(timeout_string, &endp, 10); + + make_timer("cleaner", timeout); + + pause(); + + return 0; +} +