-
Notifications
You must be signed in to change notification settings - Fork 1
/
cronevent.c
300 lines (244 loc) · 6.91 KB
/
cronevent.c
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* Copyright (c) 2019-2023, Michael Santos <michael.santos@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "runcron.h"
#include <err.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef RESTRICT_PROCESS_capsicum
#include <sys/procdesc.h>
#endif
#include "cronevent.h"
#include "limit_process.h"
#include "restrict_process.h"
#include "waitfor.h"
#include "ccronexpr.h"
static int cronexpr_proc(runcron_t *rp, char *cronentry, unsigned int *seconds,
time_t now);
static int cronexpr(runcron_t *rp, char *cronentry, unsigned int *seconds,
time_t now);
static int fields(const char *s);
static int arg_to_timespec(const char *arg, size_t arglen, char *buf,
size_t buflen);
static const char *alias_to_timespec(const char *name);
static struct runcron_alias {
const char *name;
const char *timespec;
} runcron_aliases[] = {
{"@yearly", "0~59 0~59 0~23 1~28 1~12 *"},
{"@annually", "0~59 0~59 0~23 1~28 1~12 *"},
{"@monthly", "0~59 0~59 0~23 1~28 * *"},
{"@weekly", "0~59 0~59 0~23 * * 1~7"},
{"@daily", "0~59 0~59 0~23 * * *"},
{"@hourly", "0~59 0~59 * * * *"},
{"=yearly", "0 0 0 1 1 *"},
{"=annually", "0 0 0 1 1 *"},
{"=monthly", "0 0 0 1 * *"},
{"=weekly", "0 0 0 * * 0"},
{"=daily", "0 0 0 * * *"},
{"=hourly", "0 0 * * * *"},
{"@midnight", "0 0 0 * * *"},
{"=midnight", "0 0 0 * * *"},
{"@reboot", "@reboot"},
{"=reboot", "@reboot"},
{NULL, NULL},
};
int cronevent(runcron_t *rp, char *cronentry, unsigned int *seconds,
time_t now) {
return (rp->opt & OPT_DISABLE_PROCESS_RESTRICTIONS)
? cronexpr(rp, cronentry, seconds, now)
: cronexpr_proc(rp, cronentry, seconds, now);
}
static int cronexpr_proc(runcron_t *rp, char *cronentry, unsigned int *sec,
time_t now) {
pid_t pid;
int sv[2];
unsigned int seconds;
int status;
int exit_value = 0;
int n;
int fdp = -1;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0)
return -1;
#ifdef RESTRICT_PROCESS_capsicum
pid = pdfork(&fdp, PD_CLOEXEC);
#else
pid = fork();
#endif
switch (pid) {
case -1:
n = errno;
(void)close(sv[0]);
(void)close(sv[1]);
errno = n;
return -1;
case 0:
if (close(sv[0]) < 0)
exit(111);
if (limit_process(rp) < 0)
exit(111);
if (restrict_process() < 0)
exit(111);
exit_value = cronexpr(rp, cronentry, &seconds, now);
if (exit_value < 0)
_exit(128);
while ((n = write(sv[1], &seconds, sizeof(seconds))) == -1 &&
errno == EINTR)
;
if (n != sizeof(seconds))
_exit(111);
_exit(0);
default:
if (close(sv[1]) < 0)
return -1;
if (waitfor(fdp, &status) < 0)
return -1;
if (WIFEXITED(status))
exit_value = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
exit_value = 128 + WTERMSIG(status);
switch (exit_value) {
case 0:
break;
case (128 + SIGXCPU):
(void)fprintf(
stderr,
"error: cron expression parsing exceeded allotted runtime: %s\n",
cronentry);
return -1;
case (128 + SIGSEGV):
(void)fprintf(
stderr,
"error: cron expression parsing exceeded allotted memory usage: %s\n",
cronentry);
return -1;
default:
return -1;
}
while ((n = read(sv[0], &seconds, sizeof(seconds))) == -1 && errno == EINTR)
;
if (n != sizeof(seconds))
return -1;
*sec = seconds;
if (close(sv[0]) < 0)
return -1;
}
return 0;
}
static int cronexpr(runcron_t *rp, char *cronentry, unsigned int *seconds,
time_t now) {
cron_expr expr = {0};
const char *errbuf = NULL;
char buf[255] = {0};
char arg[252] = {0};
char *p;
time_t next;
double diff;
int rv;
rv = snprintf(arg, sizeof(arg), "%s", cronentry);
if (rv < 0 || (unsigned)rv >= sizeof(arg)) {
warnx("error: timespec exceeds maximum length: %zu", sizeof(arg));
return -1;
}
/* replace tabs with spaces */
for (p = arg; *p != '\0'; p++)
if (*p == '\t' || *p == '\n' || *p == '\r')
*p = ' ';
if (arg_to_timespec(arg, sizeof(arg), buf, sizeof(buf)) < 0) {
warnx("error: invalid crontab timespec");
return -1;
}
if (rp->verbose > 1)
(void)fprintf(stderr, "crontab=%s\n", buf);
if (strcmp(buf, "@reboot") == 0) {
*seconds = UINT32_MAX;
return 0;
}
cron_parse_expr(buf, &expr, &errbuf);
if (errbuf) {
warnx("error: invalid crontab timespec: %s", errbuf);
return -1;
}
next = cron_next(&expr, now);
if (next == -1) {
warnx("error: cron_next: %s: %s", cronentry,
errno == 0 ? "invalid timespec" : strerror(errno));
return -1;
}
if (rp->verbose > 0) {
(void)fprintf(stderr, "now[%lld]=%s", (long long)now, ctime(&now));
(void)fprintf(stderr, "next[%lld]=%s", (long long)next, ctime(&next));
}
diff = difftime(next, now);
if (diff < 0) {
warnx("error: difftime: negative duration: %.f seconds", diff);
return -1;
}
*seconds = diff > UINT32_MAX ? UINT32_MAX : (unsigned int)diff;
return 0;
}
static int fields(const char *s) {
int n = 0;
const char *p = s;
int field = 0;
for (; *p != '\0'; p++) {
if (*p != ' ') {
if (!field) {
n++;
field = 1;
}
} else {
field = 0;
}
}
return n;
}
static int arg_to_timespec(const char *arg, size_t arglen, char *buf,
size_t buflen) {
const char *timespec;
int n;
int rv;
n = fields(arg);
switch (n) {
case 1:
timespec = alias_to_timespec(arg);
if (timespec == NULL)
return -1;
rv = snprintf(buf, buflen, "%s", timespec);
break;
case 5:
rv = snprintf(buf, buflen, "0 %s", arg);
break;
case 6:
default:
rv = snprintf(buf, buflen, "%s", arg);
break;
}
return (rv < 0 || (unsigned)rv >= buflen) ? -1 : 0;
}
static const char *alias_to_timespec(const char *name) {
struct runcron_alias *ap;
for (ap = runcron_aliases; ap->name != NULL; ap++) {
if (strcmp(name, ap->name) == 0) {
return ap->timespec;
}
}
return NULL;
}