scron

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

commit 1bc2d7f284813b0354f9e376de8b8411787e1fbe
parent 83b498423c48e562537d1e0b9a6237866c73f707
Author: sin <sin@2f30.org>
Date:   Wed Jul  2 13:54:36 +0100

General code cleanup

Diffstat:
crond.c | 77++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 46 insertions(+), 31 deletions(-)
diff --git a/crond.c b/crond.c @@ -1,16 +1,21 @@ +#define _GNU_SOURCE + +#include <sys/types.h> +#include <sys/wait.h> + +#include <limits.h> #include <stdlib.h> #include <stdio.h> #include <string.h> +#include <syslog.h> #include <time.h> #include <unistd.h> -#include <sys/wait.h> -#include <sys/types.h> -#include <linux/limits.h> -#include <syslog.h> -char config[PATH_MAX] = "/etc/crontab"; +static char config[PATH_MAX] = "/etc/crontab"; -void arg(int argc, char *argv[]) { +static void +arg(int argc, char *argv[]) +{ int i; pid_t pid; @@ -45,7 +50,9 @@ void arg(int argc, char *argv[]) { } } -int parsecolumn(char *col, int y, int x) { +static int +parsecolumn(char *col, int y, int x) +{ int value, value2; char *endptr, *endptr2; time_t t; @@ -72,17 +79,17 @@ int parsecolumn(char *col, int y, int x) { syslog(LOG_WARNING, "error: %s line %d column %d", config, y+1, x+1); } else if (value2 == -1) { if ((x == 0 && value == tm->tm_min) || - (x == 1 && value == tm->tm_hour) || - (x == 2 && value == tm->tm_mday) || - (x == 3 && value == tm->tm_mon) || - (x == 4 && value == tm->tm_wday)) + (x == 1 && value == tm->tm_hour) || + (x == 2 && value == tm->tm_mday) || + (x == 3 && value == tm->tm_mon) || + (x == 4 && value == tm->tm_wday)) return 0; } else { if ((x == 0 && value <= tm->tm_min && value2 >= tm->tm_min) || - (x == 1 && value <= tm->tm_hour && value2 >= tm->tm_hour) || - (x == 2 && value <= tm->tm_mday && value2 >= tm->tm_mday) || - (x == 3 && value <= tm->tm_mon && value2 >= tm->tm_mon) || - (x == 4 && value <= tm->tm_wday && value2 >= tm->tm_wday)) + (x == 1 && value <= tm->tm_hour && value2 >= tm->tm_hour) || + (x == 2 && value <= tm->tm_mday && value2 >= tm->tm_mday) || + (x == 3 && value <= tm->tm_mon && value2 >= tm->tm_mon) || + (x == 4 && value <= tm->tm_wday && value2 >= tm->tm_wday)) return 0; } } @@ -90,7 +97,9 @@ int parsecolumn(char *col, int y, int x) { return 1; } -void runjob(char *cmd) { +static void +runjob(char *cmd) +{ time_t t; pid_t pid; @@ -101,8 +110,8 @@ void runjob(char *cmd) { fprintf(stderr, "error: failed to fork job: %s time: %s", cmd, ctime(&t)); syslog(LOG_WARNING, "error: failed to fork job: %s", cmd); } else if (pid == 0) { - printf("run: %s pid: %d time: %s", cmd, (int) getpid(), ctime(&t)); - syslog(LOG_INFO, "run: %s pid: %d", cmd, (int) getpid()); + printf("run: %s pid: %d time: %s", cmd, getpid(), ctime(&t)); + syslog(LOG_INFO, "run: %s pid: %d", cmd, getpid()); execl("/bin/sh", "/bin/sh", "-c", cmd, (char *) NULL); fprintf(stderr, "error: job failed: %s time: %s\n", cmd, ctime(&t)); syslog(LOG_WARNING, "error: job failed: %s", cmd); @@ -110,7 +119,9 @@ void runjob(char *cmd) { } } -void checkreturn(void) { +static void +checkreturn(void) +{ int status; time_t t; pid_t pid; @@ -123,7 +134,9 @@ void checkreturn(void) { } } -int main(int argc, char *argv[]) { +int +main(int argc, char *argv[]) +{ int y, x; char line[PATH_MAX]; char *col; @@ -134,23 +147,24 @@ int main(int argc, char *argv[]) { arg(argc, argv); + 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; + } + while (1) { t = time(NULL); sleep(60 - t % 60); - if ((fp = fopen(config, "r")) == NULL) { - fprintf(stderr, "error: cant read %s\n", config); - syslog(LOG_WARNING, "error: cant read %s", config); - continue; - } - - for (y = 0; fgets(line, PATH_MAX-1, fp); y++) { + for (y = 0; fgets(line, sizeof(line), fp); y++) { if (line[0] == '#' || line[0] == '\n' || line[0] == '\0') continue; + if (line[strlen(line) - 1] == '\n') + line[strlen(line) - 1] = '\0'; - strtok(line, "\n"); - - for (x = 0, col = strtok(line,"\t"); col; x++, col = strtok(NULL, "\t")) { + for (x = 0, col = strtok(line, "\t"); col; x++, col = strtok(NULL, "\t")) { if (!parsecolumn(col, y, x)) continue; else if (x == 5) @@ -160,10 +174,11 @@ int main(int argc, char *argv[]) { } } - fclose(fp); + rewind(fp); checkreturn(); } + fclose(fp); closelog(); return EXIT_SUCCESS; }