scron

simple cron daemon
git clone git://git.2f30.org/scron
Log | Files | Refs | README | LICENSE

commit a25502f3b5c2e6a10160854af4d60a7f00dcd450
parent 7a9b64f0c926c937e7df016e69171618bc29ea25
Author: Ari Malinen <ari.malinen@gmail.com>
Date:   Wed, 11 Jun 2014 14:15:58 +0300

cleaned code, improved error handling

Diffstat:
MREADME | 2+-
Mdcron.c | 109++++++++++++++++++++++++-------------------------------------------------------
Minit.d/dcron | 4++--
3 files changed, 36 insertions(+), 79 deletions(-)

diff --git a/README b/README @@ -5,7 +5,7 @@ Single daemon, config and initscript. Logs to stdout and syslog. usage: -usage: ./dcron [options] +usage: dcron [options] -h help -d daemon -f <file> config file diff --git a/dcron.c b/dcron.c @@ -9,7 +9,6 @@ #define MAXLEN 100 #define SLEEP 60 -// config file location char config[MAXLEN+1] = "/etc/dcron.conf"; int main(int argc, char *argv[]) { @@ -18,19 +17,16 @@ int main(int argc, char *argv[]) { char line[MAXLEN+1]; char cmd[MAXLEN+1]; char *col; - int min, hour, mday, mon, wday; - int i; + int date[5]; + int i, l; time_t t; struct tm *tm; openlog(argv[0], LOG_CONS | LOG_PID, LOG_LOCAL1); - syslog(LOG_NOTICE, "start uid:%d", getuid()); - // parse commandline arguments for (argv0 = *argv; argc > 0; argc--, argv++) { if (!strcmp("-h", argv[0])) { - fprintf(stderr, "usage: %s [options]\n\n", argv0); - + fprintf(stderr, "usage: %s [options]\n", argv0); fprintf(stderr, "-h help\n"); fprintf(stderr, "-d daemon\n"); fprintf(stderr, "-f <file> config file\n"); @@ -42,10 +38,10 @@ int main(int argc, char *argv[]) { return 1; } } else if (!strcmp("-f", argv[0])) { - if (argv[1][0] == '-') { + if (argv[1][0] == '-' || argv[1][0] == '\0') { fprintf(stderr, "error: -f needs parameter\n"); syslog(LOG_NOTICE, "error: -f needs parameter"); - break; + return 1; } strncpy(config, argv[1], MAXLEN); printf("config: %s\n", config); @@ -54,11 +50,12 @@ int main(int argc, char *argv[]) { } } - // main loop + printf("start uid:%d\n", getuid()); + syslog(LOG_NOTICE, "start uid:%d", getuid()); + while (1) { t = time(NULL); tm = localtime(&t); - min = hour = mday = mon = wday = 0; fp = fopen(config, "r"); if (fp == NULL) { @@ -68,74 +65,34 @@ int main(int argc, char *argv[]) { continue; } - // get next line from config - while (fgets(line, MAXLEN, fp) != NULL) { - // skip comments and empty lines - if (line[1] != '\0' && line[0] != '\043') { - // split line to columns - col = strtok(line,"\t"); - i = 0; - while (col != NULL) { - switch (i) { - case 0: - if (isalnum(col[0])) - min = strtol(col, NULL, 0); - else - min = -1; - break; - case 1: - if (isalnum(col[0])) - hour = strtol(col, NULL, 0); - else - hour = -1; - break; - case 2: - if (isalnum(col[0])) - mday = strtol(col, NULL, 0); - else - mday = -1; - break; - case 3: - if (isalnum(col[0])) - mon = strtol(col, NULL, 0); - else - mon = -1; - break; - case 4: - if (isalnum(col[0])) - wday = strtol(col, NULL, 0); - else - wday = -1; - break; - case 5: - strncpy(cmd, col, MAXLEN); - break; - } - col = strtok(NULL, "\t"); - i++; - } + for (l = 0; fgets(line, MAXLEN, fp) != NULL; l++) { + if (line[1] == '\0' || line[0] == '\043') + continue; - // skip task if any column is unset - if (min == 0 || hour == 0 || mday == 0 || mon == 0 || - wday == 0 || cmd[0] == '\0' || cmd[1] == '\0') { - fprintf(stderr, "error: %s line %d\n", config, i); - syslog(LOG_NOTICE, "error: %s line %d", config, i); - sleep(SLEEP); - continue; + col = strtok(line,"\t"); + for (i = 0; col != NULL; col = strtok(NULL, "\t"), i++) { + if (i == 5) + strncpy(cmd, col, MAXLEN); + else if (isdigit(col[0])) + date[i] = strtol(col, NULL, 0); + else if (ispunct(col[0])) + date[i] = -1; + else { + fprintf(stderr, "error: %s line %d column %d\n", config, l+1, i+1); + syslog(LOG_NOTICE, "error: %s line %d column %d", config, l+1, i+1); } + } - // run task if date matches - if ((min == -1 || min == tm->tm_min) && - (hour == -1 || hour == tm->tm_hour) && - (mday == -1 || mday == tm->tm_mday) && - (mon == -1 || mon == tm->tm_mon) && - (wday == -1 || wday == tm->tm_wday)) { - printf("run: %s", cmd); - syslog(LOG_NOTICE, "run: %s", cmd); - if (system(cmd) != 0) { - fprintf(stderr, "error: job failed\n"); - syslog(LOG_NOTICE, "error: job failed"); - } + if ((date[0] == -1 || date[0] == tm->tm_min) && + (date[1] == -1 || date[1] == tm->tm_hour) && + (date[2] == -1 || date[2] == tm->tm_mday) && + (date[3] == -1 || date[3] == tm->tm_mon) && + (date[4] == -1 || date[4] == tm->tm_wday)) { + printf("run: %s", cmd); + syslog(LOG_NOTICE, "run: %s", cmd); + if (system(cmd) != 0) { + fprintf(stderr, "error: job failed\n"); + syslog(LOG_NOTICE, "error: job failed"); } } } diff --git a/init.d/dcron b/init.d/dcron @@ -1,5 +1,5 @@ #!/sbin/runscript -command=/bin/dcron +command=dcron command_args=-d -name="dcron cron daemon" +name="dcron"