scron

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

commit 38aa3e88d941ca8741e2c5bab05590587a697a5f
parent 32016bfd2e1fc81bae6609b7f808d9d940802ca0
Author: sin <sin@2f30.org>
Date:   Wed,  2 Jul 2014 15:06:50 +0100

Add arg.h and re-organize code

Diffstat:
Aarg.h | 63+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrond.c | 83+++++++++++++++++++++++++++++++++++++------------------------------------------
2 files changed, 102 insertions(+), 44 deletions(-)

diff --git a/arg.h b/arg.h @@ -0,0 +1,63 @@ +/* + * Copy me if you can. + * by 20h + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][1]\ + && argv[0][0] == '-';\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + for (brk_ = 0, argv[0]++, argv_ = argv;\ + argv[0][0] && !brk_;\ + argv[0]++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][0];\ + switch (argc_) + +/* Handles obsolete -NUM syntax */ +#define ARGNUM case '0':\ + case '1':\ + case '2':\ + case '3':\ + case '4':\ + case '5':\ + case '6':\ + case '7':\ + case '8':\ + case '9' + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define ARGNUMF(base) (brk_ = 1, estrtol(argv[0], (base))) + +#define EARGF(x) ((argv[0][1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][1] != '\0')?\ + (&argv[0][1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/crond.c b/crond.c @@ -12,40 +12,11 @@ #include <time.h> #include <unistd.h> -static char config[PATH_MAX] = "/etc/crontab"; +#include "arg.h" -static void -arg(int argc, char *argv[]) -{ - int i; - pid_t pid; - - for (i = 1; i < argc; i++) { - if (strcmp("-d", argv[i]) == 0) { - pid = fork(); - if (pid < 0) - fprintf(stderr, "error: failed to fork daemon\n"); - else if (pid == 0) - daemon(0, 0); - else - exit(EXIT_SUCCESS); - } else if (strcmp("-f", argv[i]) == 0) { - if (argv[i + 1] == NULL || argv[i + 1][0] == '-') { - fprintf(stderr, "error: -f needs parameter\n"); - syslog(LOG_WARNING, "error: -f needs parameter"); - exit(EXIT_FAILURE); - } - strncpy(config, argv[++i], sizeof(config)); - config[sizeof(config) - 1] = '\0'; - } else { - fprintf(stderr, "usage: %s [options]\n", argv[0]); - fprintf(stderr, "-h help\n"); - fprintf(stderr, "-d daemon\n"); - fprintf(stderr, "-f <file> config file\n"); - exit(EXIT_FAILURE); - } - } -} +char *argv0; +static char *config = "/etc/crontab"; +static int dflag; static int validfield(int x, int value) @@ -72,14 +43,11 @@ parsecolumn(char *col, int y, int x) struct tm *tm; if (x < 0 || x > 4) - return 1; + return -1; if (strcmp("*", col) == 0) return 0; - t = time(NULL); - tm = localtime(&t); - /* parse element */ endptr = ""; endptr2 = ""; @@ -94,7 +62,7 @@ parsecolumn(char *col, int y, int x) if (*endptr != '\0' || *endptr2 != '\0') { fprintf(stderr, "error: %s line %d column %d\n", config, y+1, x+1); syslog(LOG_WARNING, "error: %s line %d column %d", config, y+1, x+1); - return 1; + return -1; } /* check if element is valid */ @@ -106,6 +74,9 @@ parsecolumn(char *col, int y, int x) config, y+1, x+1); } + t = time(NULL); + tm = localtime(&t); + /* check if we have a match */ if (value2 == -1) { if ((x == 0 && value == tm->tm_min) || @@ -123,7 +94,7 @@ parsecolumn(char *col, int y, int x) return 0; } - return 1; + return -1; } static void @@ -169,26 +140,49 @@ waitjob(void) } } +static void +usage(void) +{ + fprintf(stderr, "usage: %s [-d] [-f file] [options]\n", argv0); + fprintf(stderr, " -d daemonize\n"); + fprintf(stderr, " -f config file\n"); + exit(EXIT_FAILURE); +} + int main(int argc, char *argv[]) { - int y, x; char line[PATH_MAX]; char *col; + int y, x; time_t t; FILE *fp; - openlog(argv[0], LOG_CONS | LOG_PID, LOG_DAEMON); + ARGBEGIN { + case 'd': + dflag = 1; + break; + case 'f': + config = EARGF(usage()); + break; + default: + usage(); + } ARGEND; + + if (argc > 0) + usage(); - arg(argc, argv); + openlog(argv[0], LOG_CONS | LOG_PID, LOG_DAEMON); if ((fp = fopen(config, "r")) == NULL) { fprintf(stderr, "error: cant read %s\n", config); - syslog(LOG_WARNING, "error: cant read %s", config); closelog(); return EXIT_FAILURE; } + if (dflag == 1) + daemon(0, 0); + while (1) { t = time(NULL); sleep(60 - t % 60); @@ -200,7 +194,7 @@ main(int argc, char *argv[]) line[strlen(line) - 1] = '\0'; for (x = 0, col = strtok(line, "\t"); col; x++, col = strtok(NULL, "\t")) { - if (!parsecolumn(col, y, x)) + if (parsecolumn(col, y, x) == 0) continue; if (x == 5) runjob(col); @@ -214,5 +208,6 @@ main(int argc, char *argv[]) fclose(fp); closelog(); + return EXIT_SUCCESS; }