Sync cron with latest scron

This commit is contained in:
sin 2015-11-06 10:58:27 +00:00
parent b553349251
commit a1c04e932b

283
cron.c
View File

@ -1,11 +1,14 @@
/* See LICENSE file for copyright and license details. */ /* See LICENSE file for copyright and license details. */
#include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <errno.h> #include <errno.h>
#include <limits.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <time.h> #include <time.h>
@ -15,11 +18,16 @@
#include "util.h" #include "util.h"
struct field { struct field {
/* [low, high] */ enum {
long low; ERROR,
long high; WILDCARD,
/* for every `div' units */ NUMBER,
long div; RANGE,
REPEAT,
LIST
} type;
long *val;
int len;
}; };
struct ctabentry { struct ctabentry {
@ -112,7 +120,7 @@ runjob(char *cmd)
loginfo("run: %s pid: %d at %s", loginfo("run: %s pid: %d at %s",
cmd, getpid(), ctime(&t)); cmd, getpid(), ctime(&t));
execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL); execl("/bin/sh", "/bin/sh", "-c", cmd, (char *)NULL);
logwarn("error: failed to execute job: %s time: %s", logerr("error: failed to execute job: %s time: %s",
cmd, ctime(&t)); cmd, ctime(&t));
_exit(1); _exit(1);
default: default:
@ -194,25 +202,39 @@ matchentry(struct ctabentry *cte, struct tm *tm)
{ .f = &cte->wday, .tm = tm->tm_wday, .len = 7 }, { .f = &cte->wday, .tm = tm->tm_wday, .len = 7 },
}; };
size_t i; size_t i;
int j;
for (i = 0; i < LEN(matchtbl); i++) { for (i = 0; i < LEN(matchtbl); i++) {
if (matchtbl[i].f->high == -1) { switch (matchtbl[i].f->type) {
if (matchtbl[i].f->low == -1) { case WILDCARD:
continue;
} else if (matchtbl[i].f->div > 0) {
if (matchtbl[i].tm > 0) {
if (matchtbl[i].tm % matchtbl[i].f->div == 0)
continue;
} else {
if (matchtbl[i].len % matchtbl[i].f->div == 0)
continue;
}
} else if (matchtbl[i].f->low == matchtbl[i].tm) {
continue;
}
} else if (matchtbl[i].f->low <= matchtbl[i].tm &&
matchtbl[i].f->high >= matchtbl[i].tm) {
continue; continue;
case NUMBER:
if (matchtbl[i].f->val[0] == matchtbl[i].tm)
continue;
break;
case RANGE:
if (matchtbl[i].f->val[0] <= matchtbl[i].tm)
if (matchtbl[i].f->val[1] >= matchtbl[i].tm)
continue;
break;
case REPEAT:
if (matchtbl[i].tm > 0) {
if (matchtbl[i].tm % matchtbl[i].f->val[0] == 0)
continue;
} else {
if (matchtbl[i].len % matchtbl[i].f->val[0] == 0)
continue;
}
break;
case LIST:
for (j = 0; j < matchtbl[i].f->len; j++)
if (matchtbl[i].f->val[j] == matchtbl[i].tm)
break;
if (j < matchtbl[i].f->len)
continue;
break;
default:
break;
} }
break; break;
} }
@ -224,58 +246,119 @@ matchentry(struct ctabentry *cte, struct tm *tm)
static int static int
parsefield(const char *field, long low, long high, struct field *f) parsefield(const char *field, long low, long high, struct field *f)
{ {
long min, max, div; int i;
char *e1, *e2; char *e1, *e2;
const char *p;
if (strcmp(field, "*") == 0) { p = field;
f->low = -1; while (isdigit(*p))
f->high = -1; p++;
return 0;
}
div = -1; f->type = ERROR;
max = -1;
min = strtol(field, &e1, 10);
switch (e1[0]) { switch (*p) {
case '-':
e1++;
errno = 0;
max = strtol(e1, &e2, 10);
if (e2[0] != '\0' || errno != 0)
return -1;
break;
case '*': case '*':
e1++; if (strcmp(field, "*") == 0) {
if (e1[0] != '/') f->val = NULL;
return -1; f->len = 0;
e1++; f->type = WILDCARD;
errno = 0; } else if (strncmp(field, "*/", 2) == 0) {
div = strtol(e1, &e2, 10); f->val = emalloc(sizeof(*f->val));
if (e2[0] != '\0' || errno != 0) f->len = 1;
return -1;
errno = 0;
f->val[0] = strtol(field + 2, &e1, 10);
if (e1[0] != '\0' || errno != 0 || f->val[0] == 0)
break;
f->type = REPEAT;
}
break; break;
case '\0': case '\0':
f->val = emalloc(sizeof(*f->val));
f->len = 1;
errno = 0;
f->val[0] = strtol(field, &e1, 10);
if (e1[0] != '\0' || errno != 0)
break;
f->type = NUMBER;
break;
case '-':
f->val = emalloc(2 * sizeof(*f->val));
f->len = 2;
errno = 0;
f->val[0] = strtol(field, &e1, 10);
if (e1[0] != '-' || errno != 0)
break;
errno = 0;
f->val[1] = strtol(e1 + 1, &e2, 10);
if (e2[0] != '\0' || errno != 0)
break;
f->type = RANGE;
break;
case ',':
for (i = 1; isdigit(*p) || *p == ','; p++)
if (*p == ',')
i++;
f->val = emalloc(i * sizeof(*f->val));
f->len = i;
errno = 0;
f->val[0] = strtol(field, &e1, 10);
if (f->val[0] < low || f->val[0] > high)
break;
for (i = 1; *e1 == ',' && errno == 0; i++) {
errno = 0;
f->val[i] = strtol(e1 + 1, &e2, 10);
e1 = e2;
}
if (e1[0] != '\0' || errno != 0)
break;
f->type = LIST;
break; break;
default: default:
return -1; return -1;
} }
if (min < low || min > high) for (i = 0; i < f->len; i++)
return -1; if (f->val[i] < low || f->val[i] > high)
if (max != -1) f->type = ERROR;
if (max < low || max > high)
return -1; if (f->type == ERROR) {
if (div != -1) free(f->val);
if (div < low || div > high) return -1;
return -1; }
f->low = min;
f->high = max;
f->div = div;
return 0; return 0;
} }
static void
freecte(struct ctabentry *cte, int nfields)
{
switch (nfields) {
case 6:
free(cte->cmd);
case 5:
free(cte->wday.val);
case 4:
free(cte->mon.val);
case 3:
free(cte->mday.val);
case 2:
free(cte->hour.val);
case 1:
free(cte->min.val);
}
free(cte);
}
static void static void
unloadentries(void) unloadentries(void)
{ {
@ -284,8 +367,7 @@ unloadentries(void)
for (cte = TAILQ_FIRST(&ctabhead); cte; cte = tmp) { for (cte = TAILQ_FIRST(&ctabhead); cte; cte = tmp) {
tmp = TAILQ_NEXT(cte, entry); tmp = TAILQ_NEXT(cte, entry);
TAILQ_REMOVE(&ctabhead, cte, entry); TAILQ_REMOVE(&ctabhead, cte, entry);
free(cte->cmd); freecte(cte, 6);
free(cte);
} }
} }
@ -298,69 +380,62 @@ loadentries(void)
int r = 0, y; int r = 0, y;
size_t size = 0; size_t size = 0;
ssize_t len; ssize_t len;
struct fieldlimits {
char *name;
long min;
long max;
struct field *f;
} flim[] = {
{ "min", 0, 59, NULL },
{ "hour", 0, 23, NULL },
{ "mday", 1, 31, NULL },
{ "mon", 1, 12, NULL },
{ "wday", 0, 6, NULL }
};
size_t x;
if ((fp = fopen(config, "r")) == NULL) { if ((fp = fopen(config, "r")) == NULL) {
logerr("error: can't open %s: %s\n", config, strerror(errno)); logerr("error: can't open %s: %s\n", config, strerror(errno));
return -1; return -1;
} }
for (y = 0; (len = getline(&line, &size, fp)) > 0; y++) { for (y = 0; (len = getline(&line, &size, fp)) != -1; y++) {
p = line; p = line;
if (line[0] == '#' || line[0] == '\n' || line[0] == '\0') if (line[0] == '#' || line[0] == '\n' || line[0] == '\0')
continue; continue;
cte = emalloc(sizeof(*cte)); cte = emalloc(sizeof(*cte));
flim[0].f = &cte->min;
flim[1].f = &cte->hour;
flim[2].f = &cte->mday;
flim[3].f = &cte->mon;
flim[4].f = &cte->wday;
col = strsep(&p, "\t"); for (x = 0; x < LEN(flim); x++) {
if (!col || parsefield(col, 0, 59, &cte->min) < 0) { do
logerr("error: failed to parse `min' field on line %d\n", col = strsep(&p, "\t\n ");
y + 1); while (col && col[0] == '\0');
free(cte);
r = -1; if (!col || parsefield(col, flim[x].min, flim[x].max, flim[x].f) < 0) {
break; logerr("error: failed to parse `%s' field on line %d\n",
flim[x].name, y + 1);
freecte(cte, x);
r = -1;
break;
}
} }
col = strsep(&p, "\t"); if (r == -1)
if (!col || parsefield(col, 0, 23, &cte->hour) < 0) {
logerr("error: failed to parse `hour' field on line %d\n",
y + 1);
free(cte);
r = -1;
break; break;
}
col = strsep(&p, "\t");
if (!col || parsefield(col, 1, 31, &cte->mday) < 0) {
logerr("error: failed to parse `mday' field on line %d\n",
y + 1);
free(cte);
r = -1;
break;
}
col = strsep(&p, "\t");
if (!col || parsefield(col, 1, 12, &cte->mon) < 0) {
logerr("error: failed to parse `mon' field on line %d\n",
y + 1);
free(cte);
r = -1;
break;
}
col = strsep(&p, "\t");
if (!col || parsefield(col, 0, 6, &cte->wday) < 0) {
logerr("error: failed to parse `wday' field on line %d\n",
y + 1);
free(cte);
r = -1;
break;
}
col = strsep(&p, "\n"); col = strsep(&p, "\n");
if (!col) { if (col)
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); y + 1);
free(cte); freecte(cte, 5);
r = -1; r = -1;
break; break;
} }