aboutsummaryrefslogtreecommitdiff
path: root/crontab.c
blob: 2b26ebf457686743d5e3b49cf63ed5abf27efcac (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
/* SPDX-License-Identifier: ISC */
#include "gcrond.h"

void cron_tm_to_mask(crontab_t *out, struct tm *t)
{
	memset(out, 0, sizeof(*out));
	out->minute     = 1UL << ((unsigned long)t->tm_min);
	out->hour       = 1 << t->tm_hour;
	out->dayofmonth = 1 << (t->tm_mday - 1);
	out->month      = 1 << t->tm_mon;
	out->dayofweek  = 1 << t->tm_wday;
}

bool cron_should_run(const crontab_t *t, const crontab_t *mask)
{
	if ((t->minute & mask->minute) == 0)
		return false;

	if ((t->hour & mask->hour) == 0)
		return false;

	if ((t->dayofmonth & mask->dayofmonth) == 0)
		return false;

	if ((t->month & mask->month) == 0)
		return false;

	if ((t->dayofweek & mask->dayofweek) == 0)
		return false;

	return true;
}

void delcron(crontab_t *cron)
{
	if (cron != NULL) {
		free(cron->exec);
		free(cron);
	}
}

int runjob(crontab_t *tab)
{
	pid_t pid;

	if (tab->exec == NULL)
		return 0;

	pid = fork();
	if (pid == -1) {
		perror("fork");
		return -1;
	}

	if (pid != 0)
		return 0;

	execl("/bin/sh", "sh", "-c", tab->exec, (char *) 0);
	perror("runnig shell interpreter");
	exit(EXIT_FAILURE);
}