commit b55d770c556ed9216e544d1fc03bd7e73bd6b932
parent 9b691a623cfdcd32b9a90b917171611133126fa0
Author: Ari Malinen <ari.malinen@gmail.com>
Date: Sun, 15 Jun 2014 02:38:22 +0300
improved error handling
Diffstat:
2 files changed, 17 insertions(+), 11 deletions(-)
diff --git a/dcron.c b/dcron.c
@@ -9,7 +9,7 @@
#define LEN 80
int main(int argc, char *argv[]) {
- char *col, line[LEN+1], config[LEN+1] = "/etc/dcron.conf";
+ char *ep, *col, line[LEN+1], config[LEN+1] = "/etc/dcron.conf";
int i, j;
time_t t;
struct tm *tm;
@@ -21,9 +21,9 @@ int main(int argc, char *argv[]) {
if (!strcmp("-d", argv[i])) {
if (fork() == 0) {
setsid();
+ fclose(stdin);
fclose(stdout);
fclose(stderr);
- fclose(stdin);
} else {
return 0;
}
@@ -56,31 +56,38 @@ int main(int argc, char *argv[]) {
}
for (i = 0; fgets(line, LEN+1, fp); i++) {
- if (line[0] == '#' || line[1] == '\0')
+ if (line[0] == '#' || line[0] == '\n')
continue;
for (j = 0, col = strtok(line,"\t"); col; j++, col = strtok(NULL, "\t")) {
- if ((j == 0 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_min)) ||
- (j == 1 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_hour)) ||
- (j == 2 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_mday)) ||
- (j == 3 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_mon)) ||
- (j == 4 && (ispunct(col[0]) || strtol(col, NULL, 0) == tm->tm_wday))) {
+ ep = "";
+
+ if ((j == 0 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_min) && *ep == '\0') ||
+ (j == 1 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_hour) && *ep == '\0') ||
+ (j == 2 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_mday) && *ep == '\0') ||
+ (j == 3 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_mon) && *ep == '\0') ||
+ (j == 4 && (col[0] == '*' || strtol(col, &ep, 0) == tm->tm_wday) && *ep == '\0')) {
continue;
} else if (j == 5) {
printf("run: %s", col);
syslog(LOG_NOTICE, "run: %s", col);
if (fork() == 0) {
- execl("/bin/sh", "/bin/sh", "-c", col, (char *)NULL);
+ execl("/bin/sh", "/bin/sh", "-c", col, (char *) NULL);
+ fprintf(stderr, "error: job failed: %s", col);
+ syslog(LOG_NOTICE, "error: job failed: %s", col);
}
- } else if (!isdigit(col[0])) {
+ } else if (!isdigit(col[0]) || *col < 0 || *col > 59 || *ep != '\0') {
fprintf(stderr, "error: %s line %d column %d\n", config, i+1, j+1);
syslog(LOG_NOTICE, "error: %s line %d column %d", config, i+1, j+1);
}
+
break;
}
}
+
fclose(fp);
}
+
closelog();
return 0;
}
diff --git a/dcron.conf b/dcron.conf
@@ -1,3 +1,2 @@
# tm_min, tm_hour, tm_mday, tm_mon, tm_wday, command
0 5 * * * updatedb
-9 5 * * * mandb --quiet