summaryrefslogtreecommitdiff
path: root/crond/main.c
blob: d7cc33e8f8f117bb5d91bf382a743732fae57f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * Copyright (C) 2018 - David Oberhollenzer
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "gcrond.h"

static crontab_t *jobs;
static sig_atomic_t run = 1;
static sig_atomic_t rescan = 1;

static void read_config(void)
{
	if (cronscan(GCRONDIR, &jobs)) {
		fputs("Error reading configuration. Continuing anyway.\n",
		      stderr);
	}
}

static void cleanup_config(void)
{
	crontab_t *t;

	while (jobs != NULL) {
		t = jobs;
		jobs = jobs->next;
		delcron(t);
	}
}

static int calc_timeout(void)
{
	time_t now = time(NULL), future;
	struct tm tmstruct;
	crontab_t mask, *t;
	int minutes;

	for (minutes = 0; minutes < 120; ++minutes) {
		future = now + minutes * 60;

		localtime_r(&future, &tmstruct);
		cron_tm_to_mask(&mask, &tmstruct);

		for (t = jobs; t != NULL; t = t->next) {
			if (cron_should_run(t, &mask))
				goto out;
		}
	}
out:
	return minutes ? minutes * 60 : 60;
}

static void runjobs(void)
{
	time_t now = time(NULL);
	struct tm tmstruct;
	crontab_t mask, *t;

	localtime_r(&now, &tmstruct);
	cron_tm_to_mask(&mask, &tmstruct);

	for (t = jobs; t != NULL; t = t->next) {
		if (cron_should_run(t, &mask))
			runjob(t);
	}
}

static void sighandler(int signo)
{
	switch (signo) {
	case SIGINT:
	case SIGTERM:
		run = 0;
		break;
	case SIGHUP:
		rescan = 1;
		break;
	}
}

int main(void)
{
	struct timespec stime;
	struct sigaction act;
	crontab_t *t;
	int timeout;
	pid_t pid;

	memset(&act, 0, sizeof(act));
	act.sa_handler = sighandler;
	sigaction(SIGINT, &act, NULL);
	sigaction(SIGTERM, &act, NULL);
	sigaction(SIGHUP, &act, NULL);

	while (run) {
		if (rescan == 1) {
			cleanup_config();
			read_config();
			timeout = 60;
			rescan = 0;
		} else {
			runjobs();
			timeout = calc_timeout();
		}

		stime.tv_sec = timeout;
		stime.tv_nsec = 0;

		while (nanosleep(&stime, &stime) != 0 && run && !rescan) {
			if (errno != EINTR) {
				perror("nanosleep");
				break;
			}
		}

		while ((pid = waitpid(-1, NULL, WNOHANG)) != -1) {
			for (t = jobs; t != NULL; t = t->next) {
				if (t->pid == pid) {
					t->pid = -1;
					break;
				}
			}
		}
	}

	return EXIT_SUCCESS;
}