From d2b3a983e16f15d636619ebd61a3fa08c889b080 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Sun, 18 Nov 2018 21:24:39 +0100 Subject: Initial commit Signed-off-by: David Oberhollenzer --- crontab.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 crontab.c (limited to 'crontab.c') diff --git a/crontab.c b/crontab.c new file mode 100644 index 0000000..2b26ebf --- /dev/null +++ b/crontab.c @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: ISC */ +#include "gcrond.h" + +void cron_tm_to_mask(crontab_t *out, struct tm *t) +{ + memset(out, 0, sizeof(*out)); + out->minute = 1UL << ((unsigned long)t->tm_min); + out->hour = 1 << t->tm_hour; + out->dayofmonth = 1 << (t->tm_mday - 1); + out->month = 1 << t->tm_mon; + out->dayofweek = 1 << t->tm_wday; +} + +bool cron_should_run(const crontab_t *t, const crontab_t *mask) +{ + if ((t->minute & mask->minute) == 0) + return false; + + if ((t->hour & mask->hour) == 0) + return false; + + if ((t->dayofmonth & mask->dayofmonth) == 0) + return false; + + if ((t->month & mask->month) == 0) + return false; + + if ((t->dayofweek & mask->dayofweek) == 0) + return false; + + return true; +} + +void delcron(crontab_t *cron) +{ + if (cron != NULL) { + free(cron->exec); + free(cron); + } +} + +int runjob(crontab_t *tab) +{ + pid_t pid; + + if (tab->exec == NULL) + return 0; + + pid = fork(); + if (pid == -1) { + perror("fork"); + return -1; + } + + if (pid != 0) + return 0; + + execl("/bin/sh", "sh", "-c", tab->exec, (char *) 0); + perror("runnig shell interpreter"); + exit(EXIT_FAILURE); +} -- cgit v1.2.3