commit 94582629a78e4b213882ab407dfd119a16342c09
parent 981a9bc6275358bb6086d72268d4939062377c45
Author: Ari Malinen <ari.malinen@gmail.com>
Date: Mon, 30 Jun 2014 02:26:39 +0300
better crontab parsing
Diffstat:
M | README | | | 14 | ++++++++------ |
M | crond.c | | | 78 | ++++++++++++++++++++++++++++++++++++++---------------------------------------- |
2 files changed, 46 insertions(+), 46 deletions(-)
diff --git a/README b/README
@@ -3,10 +3,12 @@ scron - simple cron daemon
features
--------
-Schedule tasks.
-Single daemon, config and initscript.
-Log to stdout and syslog.
-No mail support.
+schedule tasks
+single daemon, config and initscript
+log job output: command &>> /var/log/cron.log
+run as different user: su -c 'command' user
+log to stdout and syslog
+no mail support
usage
-----
@@ -18,5 +20,5 @@ usage: crond [options]
example config
--------------
# tm_min, tm_hour, tm_mday, tm_mon, tm_wday, command
-0 5 * * * updatedb # 06:00 every day
-29 4 * * 1-5 emerge --sync # 5:30 every weekday
+0 6 * * * updatedb # 06:00 every day
+30 5 * * 1-5 emerge --sync # 5:30 every weekday
diff --git a/crond.c b/crond.c
@@ -1,16 +1,14 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <ctype.h>
#include <time.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
+#include <linux/limits.h>
#include <syslog.h>
-#define LEN 256
-
-char config[LEN+1] = "/etc/crontab";
+char config[PATH_MAX] = "/etc/crontab";
void arg(int argc, char *argv[]) {
int i;
@@ -21,7 +19,7 @@ void arg(int argc, char *argv[]) {
pid = fork();
if (pid < 0) {
fprintf(stderr, "error: failed to fork daemon\n");
- syslog(LOG_NOTICE, "error: failed to fork daemon");
+ syslog(LOG_WARNING, "error: failed to fork daemon");
} else if (pid == 0) {
setsid();
fclose(stdin);
@@ -33,10 +31,10 @@ void arg(int argc, char *argv[]) {
} else if (!strcmp("-f", argv[i])) {
if (argv[i+1] == NULL || argv[i+1][0] == '-') {
fprintf(stderr, "error: -f needs parameter\n");
- syslog(LOG_NOTICE, "error: -f needs parameter");
+ syslog(LOG_WARNING, "error: -f needs parameter");
exit(EXIT_FAILURE);
}
- strncpy(config, argv[++i], LEN);
+ strncpy(config, argv[++i], PATH_MAX-1);
} else {
fprintf(stderr, "usage: %s [options]\n", argv[0]);
fprintf(stderr, "-h help\n");
@@ -58,34 +56,34 @@ int parsecolumn(char *col, int y, int x) {
if (x >= 0 && x <= 4) {
endptr = "";
+ endptr2 = "";
entry = strtol(col, &endptr, 0);
- if (!strcmp("*", col)) {
- return 0;
- } else if (*endptr == '-') {
+ entry2 = -1;
+ if (*endptr == '-') {
endptr++;
- endptr2 = "";
entry2 = strtol(endptr, &endptr2, 0);
- if (*endptr2 != '\0' || entry2 < 0 || entry2 > 59) {
- fprintf(stderr, "error: %s line %d column %d\n", config, y+1, x+1);
- syslog(LOG_NOTICE, "error: %s line %d column %d", config, y+1, x+1);
- return 1;
- } else if ((x == 0 && entry <= tm->tm_min && entry2 >= tm->tm_min) ||
+ endptr = "";
+ }
+
+ if (!strcmp("*", col)) {
+ return 0;
+ } else 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);
+ } else if (entry2 == -1) {
+ if ((x == 0 && entry == tm->tm_min) ||
+ (x == 1 && entry == tm->tm_hour) ||
+ (x == 2 && entry == tm->tm_mday) ||
+ (x == 3 && entry == tm->tm_mon) ||
+ (x == 4 && entry == tm->tm_wday))
+ return 0;
+ } else {
+ if ((x == 0 && entry <= tm->tm_min && entry2 >= tm->tm_min) ||
(x == 1 && entry <= tm->tm_hour && entry2 >= tm->tm_hour) ||
(x == 2 && entry <= tm->tm_mday && entry2 >= tm->tm_mday) ||
(x == 3 && entry <= tm->tm_mon && entry2 >= tm->tm_mon) ||
- (x == 4 && entry <= tm->tm_wday && entry2 >= tm->tm_wday)) {
+ (x == 4 && entry <= tm->tm_wday && entry2 >= tm->tm_wday))
return 0;
- }
- } else if (*endptr != '\0' || entry < 0 || entry > 59) {
- fprintf(stderr, "error: %s line %d column %d\n", config, y+1, x+1);
- syslog(LOG_NOTICE, "error: %s line %d column %d", config, y+1, x+1);
- return 1;
- } else if ((x == 0 && entry == tm->tm_min) ||
- (x == 1 && entry == tm->tm_hour) ||
- (x == 2 && entry == tm->tm_mday) ||
- (x == 3 && entry == tm->tm_mon) ||
- (x == 4 && entry == tm->tm_wday)) {
- return 0;
}
}
@@ -101,13 +99,13 @@ void runjob(char *cmd) {
pid = fork();
if (pid < 0) {
fprintf(stderr, "error: failed to fork job: %s time: %s", cmd, ctime(&t));
- syslog(LOG_NOTICE, "error: failed to fork job: %s", cmd);
+ 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_NOTICE, "run: %s pid: %d", cmd, (int) getpid());
+ syslog(LOG_INFO, "run: %s pid: %d", cmd, (int) getpid());
execl("/bin/sh", "/bin/sh", "-c", cmd, (char *) NULL);
fprintf(stderr, "error: job failed: %s time: %s\n", cmd, ctime(&t));
- syslog(LOG_NOTICE, "error: job failed: %s", cmd);
+ syslog(LOG_WARNING, "error: job failed: %s", cmd);
exit(EXIT_FAILURE);
}
}
@@ -121,18 +119,18 @@ void checkreturn(void) {
while ((pid = waitpid(-1, &status, WNOHANG | WUNTRACED)) > 0) {
printf("complete: pid %d, return: %d time: %s", (int) pid, status, ctime(&t));
- syslog(LOG_NOTICE, "complete: pid: %d return: %d", (int) pid, status);
+ syslog(LOG_INFO, "complete: pid: %d return: %d", (int) pid, status);
}
}
int main(int argc, char *argv[]) {
int y, x;
- char line[LEN+1];
+ char line[PATH_MAX];
char *col;
time_t t;
FILE *fp;
- openlog(argv[0], LOG_CONS | LOG_PID, LOG_LOCAL1);
+ openlog(argv[0], LOG_CONS | LOG_PID, LOG_DAEMON);
arg(argc, argv);
@@ -142,12 +140,12 @@ int main(int argc, char *argv[]) {
if ((fp = fopen(config, "r")) == NULL) {
fprintf(stderr, "error: cant read %s\n", config);
- syslog(LOG_NOTICE, "error: cant read %s", config);
+ syslog(LOG_WARNING, "error: cant read %s", config);
continue;
}
- for (y = 0; fgets(line, LEN+1, fp); y++) {
- if (line[0] == '#' || line[0] == '\n')
+ for (y = 0; fgets(line, PATH_MAX-1, fp); y++) {
+ if (line[0] == '#' || line[0] == '\n' || line[0] == '\0')
continue;
strtok(line, "\n");
@@ -157,8 +155,8 @@ int main(int argc, char *argv[]) {
continue;
else if (x == 5)
runjob(col);
-
- break;
+ else
+ break;
}
}
@@ -167,5 +165,5 @@ int main(int argc, char *argv[]) {
}
closelog();
- return 0;
+ return EXIT_SUCCESS;
}