aboutsummaryrefslogtreecommitdiff
path: root/gcrond.c
blob: 0e28277b2a74dc626b1d343b794e1eccc23e1e54 (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
/* SPDX-License-Identifier: ISC */
#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 timeout_minutes(int minutes)
{
	time_t now = time(NULL);
	struct tm t;

	localtime_r(&now, &t);
	return minutes * 60 + 30 - t.tm_sec;
}

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 timeout_minutes(minutes ? minutes : 1);
}

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)
{
	pid_t pid;

	switch (signo) {
	case SIGINT:
	case SIGTERM:
		run = 0;
		break;
	case SIGHUP:
		rescan = 1;
		break;
	case SIGCHLD:
		while ((pid = waitpid(-1, NULL, WNOHANG)) != -1)
			;
		break;
	}
}

int main(void)
{
	struct timespec stime;
	struct sigaction act;
	int timeout;

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

	while (run) {
		if (rescan == 1) {
			cleanup_config();
			read_config();
			timeout = timeout_minutes(1);
			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;
			}
		}
	}

	return EXIT_SUCCESS;
}