commit: 77e04b2ca68e2e7f43b842a62dcd21158c616c7d
parent: 495ef1b140c06f5f6ef435c717dce24db05e9d97
author: Chris Noxz <chris@noxz.tech>
date: Thu, 19 Sep 2019 17:16:32 +0200
Add support for initial commands using '@'
2 files changed, 47 insertions(+), 2 deletions(-)
diff --git a/Makefile b/Makefile
@@ -1,6 +1,6 @@
CFLAGS += -std=c99 -Wall -Wextra -pedantic -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE
PREFIX = /usr/local
-MANPREFIX = $(PREFIX)/man
+MANPREFIX = $(PREFIX)/share/man
BIN = crond
MAN = scron.1
diff --git a/crond.c b/crond.c
@@ -34,6 +34,11 @@ struct field {
int len;
};
+struct initentry {
+ char *cmd;
+ TAILQ_ENTRY(initentry) entry;
+};
+
struct ctabentry {
struct field min;
struct field hour;
@@ -54,6 +59,7 @@ char *argv0;
static sig_atomic_t chldreap;
static sig_atomic_t reload;
static sig_atomic_t quit;
+static TAILQ_HEAD(, initentry) inithead = TAILQ_HEAD_INITIALIZER(inithead);
static TAILQ_HEAD(, ctabentry) ctabhead = TAILQ_HEAD_INITIALIZER(ctabhead);
static TAILQ_HEAD(, jobentry) jobhead = TAILQ_HEAD_INITIALIZER(jobhead);
static char *config = "/etc/crontab";
@@ -373,6 +379,14 @@ parsefield(const char *field, long low, long high, struct field *f)
return 0;
}
+static void
+freeie(struct initentry *ie, int freecmd)
+{
+ if (freecmd)
+ free(ie->cmd);
+ free(ie);
+}
+
static void
freecte(struct ctabentry *cte, int nfields)
{
@@ -408,6 +422,7 @@ unloadentries(void)
static int
loadentries(void)
{
+ struct initentry *ie;
struct ctabentry *cte;
FILE *fp;
char *line = NULL, *p, *col;
@@ -438,6 +453,25 @@ loadentries(void)
if (line[0] == '#' || line[0] == '\n' || line[0] == '\0')
continue;
+ if (line[0] == '@') {
+ p++;
+ ie = emalloc(sizeof(*ie));
+ col = strsep(&p, "\n");
+ if (col)
+ while (col[0] == '\t' || col[0] == ' ')
+ col++;
+ if (!col || col[0] == '\0') {
+ logerr("error: missing 'cmd' field on line %d\n",
+ y + 1);
+ freeie(ie, 0);
+ r = -1;
+ break;
+ }
+ ie->cmd = estrdup(col);
+ TAILQ_INSERT_TAIL(&inithead, ie, entry);
+ continue;
+ }
+
cte = emalloc(sizeof(*cte));
flim[0].f = &cte->min;
flim[1].f = &cte->hour;
@@ -467,7 +501,7 @@ loadentries(void)
while (col[0] == '\t' || col[0] == ' ')
col++;
if (!col || col[0] == '\0') {
- logerr("error: missing `cmd' field on line %d\n",
+ logerr("error: missing 'cmd' field on line %d\n",
y + 1);
freecte(cte, 5);
r = -1;
@@ -525,6 +559,7 @@ int
main(int argc, char *argv[])
{
FILE *fp;
+ struct initentry *ie, *tmp;
struct ctabentry *cte;
time_t t;
struct tm *tm;
@@ -565,6 +600,16 @@ main(int argc, char *argv[])
loadentries();
+ TAILQ_FOREACH(ie, &inithead, entry) {
+ runjob(ie->cmd);
+ }
+
+ for (ie = TAILQ_FIRST(&inithead); ie; ie = tmp) {
+ tmp = TAILQ_NEXT(ie, entry);
+ TAILQ_REMOVE(&inithead, ie, entry);
+ freeie(ie, 1);
+ }
+
while (1) {
t = time(NULL);
sleep(60 - t % 60);