summaryrefslogtreecommitdiffstats
path: root/usr.sbin/cron/cron.c
blob: f76d95a1de21c4c26df93bc86133ac1218054a8a (plain) (blame)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*	$OpenBSD: cron.c,v 1.76 2017/06/07 23:36:43 millert Exp $	*/

/* Copyright 1988,1990,1993,1994 by Paul Vixie
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1997,2000 by Internet Software Consortium, Inc.
 *
 * Permission to use, copy, modify, and 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 ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC 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 <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/un.h>
#include <sys/wait.h>

#include <bitstring.h>
#include <err.h>
#include <errno.h>
#include <grp.h>
#include <locale.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include "config.h"
#include "pathnames.h"
#include "macros.h"
#include "structs.h"
#include "funcs.h"
#include "globals.h"

enum timejump { negative, small, medium, large };

static	void	usage(void),
		run_reboot_jobs(cron_db *),
		find_jobs(time_t, cron_db *, int, int),
		set_time(int),
		cron_sleep(time_t, sigset_t *),
		sigchld_handler(int),
		sigchld_reaper(void),
		parse_args(int c, char *v[]);

static	int	open_socket(void);

static	volatile sig_atomic_t	got_sigchld;
static	time_t			timeRunning, virtualTime, clockTime;
static	int			cronSock;
static	long			GMToff;
static	cron_db			*database;
static	at_db			*at_database;
static	double			batch_maxload = BATCH_MAXLOAD;
static	int			NoFork;
static	time_t			StartTime;
	gid_t			cron_gid;

static void
usage(void)
{

	fprintf(stderr, "usage: %s [-n] [-l load_avg]\n", __progname);
	exit(EXIT_FAILURE);
}

int
main(int argc, char *argv[])
{
	struct sigaction sact;
	sigset_t blocked, omask;
	struct group *grp;

	setlocale(LC_ALL, "");

	setvbuf(stdout, NULL, _IOLBF, 0);
	setvbuf(stderr, NULL, _IOLBF, 0);

	parse_args(argc, argv);

	bzero((char *)&sact, sizeof sact);
	sigemptyset(&sact.sa_mask);
	sact.sa_flags = SA_RESTART;
	sact.sa_handler = sigchld_handler;
	(void) sigaction(SIGCHLD, &sact, NULL);
	sact.sa_handler = SIG_IGN;
	(void) sigaction(SIGHUP, &sact, NULL);
	(void) sigaction(SIGPIPE, &sact, NULL);

	openlog(__progname, LOG_PID, LOG_CRON);

	if (pledge("stdio rpath wpath cpath fattr getpw unix id dns proc exec",
	    NULL) == -1) {
		warn("pledge");
		syslog(LOG_ERR, "(CRON) PLEDGE (%m)");
		exit(EXIT_FAILURE);
	}

	if ((grp = getgrnam(CRON_GROUP)) == NULL) {
		warnx("can't find cron group %s", CRON_GROUP);
		syslog(LOG_ERR, "(CRON) DEATH (can't find cron group)");
		exit(EXIT_FAILURE);
	}
	cron_gid = grp->gr_gid;

	cronSock = open_socket();

	if (putenv("PATH="_PATH_DEFPATH) < 0) {
		warn("putenv");
		syslog(LOG_ERR, "(CRON) DEATH (%m)");
		exit(EXIT_FAILURE);
	}

	if (NoFork == 0) {
		if (daemon(0, 0) == -1) {
			syslog(LOG_ERR, "(CRON) DEATH (%m)");
			exit(EXIT_FAILURE);
		}
		syslog(LOG_INFO, "(CRON) STARTUP (%s)", CRON_VERSION);
	}

	load_database(&database);
	scan_atjobs(&at_database, NULL);
	set_time(TRUE);
	run_reboot_jobs(database);
	timeRunning = virtualTime = clockTime;

	/*
	 * We block SIGHUP and SIGCHLD while running jobs and receive them
	 * only while sleeping in ppoll().  This ensures no signal is lost.
	 */
	sigemptyset(&blocked);
	sigaddset(&blocked, SIGCHLD);
	sigaddset(&blocked, SIGHUP);
	sigprocmask(SIG_BLOCK, &blocked, &omask);

	/*
	 * Too many clocks, not enough time (Al. Einstein)
	 * These clocks are in minutes since the epoch, adjusted for timezone.
	 * virtualTime: is the time it *would* be if we woke up
	 * promptly and nobody ever changed the clock. It is
	 * monotonically increasing... unless a timejump happens.
	 * At the top of the loop, all jobs for 'virtualTime' have run.
	 * timeRunning: is the time we last awakened.
	 * clockTime: is the time when set_time was last called.
	 */
	while (TRUE) {
		int timeDiff;
		enum timejump wakeupKind;

		/* ... wait for the time (in minutes) to change ... */
		do {
			cron_sleep(timeRunning + 1, &omask);
			set_time(FALSE);
		} while (clockTime == timeRunning);
		timeRunning = clockTime;

		/*
		 * Calculate how the current time differs from our virtual
		 * clock.  Classify the change into one of 4 cases.
		 */
		timeDiff = timeRunning - virtualTime;

		/* shortcut for the most common case */
		if (timeDiff == 1) {
			virtualTime = timeRunning;
			find_jobs(virtualTime, database, TRUE, TRUE);
		} else {
			if (timeDiff > (3*MINUTE_COUNT) ||
			    timeDiff < -(3*MINUTE_COUNT))
				wakeupKind = large;
			else if (timeDiff > 5)
				wakeupKind = medium;
			else if (timeDiff > 0)
				wakeupKind = small;
			else
				wakeupKind = negative;

			switch (wakeupKind) {
			case small:
				/*
				 * case 1: timeDiff is a small positive number
				 * (wokeup late) run jobs for each virtual
				 * minute until caught up.
				 */
				do {
					if (job_runqueue())
						sleep(10);
					virtualTime++;
					find_jobs(virtualTime, database,
					    TRUE, TRUE);
				} while (virtualTime < timeRunning);
				break;

			case medium:
				/*
				 * case 2: timeDiff is a medium-sized positive
				 * number, for example because we went to DST
				 * run wildcard jobs once, then run any
				 * fixed-time jobs that would otherwise be
				 * skipped if we use up our minute (possible,
				 * if there are a lot of jobs to run) go
				 * around the loop again so that wildcard jobs
				 * have a chance to run, and we do our
				 * housekeeping.
				 */
				/* run wildcard jobs for current minute */
				find_jobs(timeRunning, database, TRUE, FALSE);

				/* run fixed-time jobs for each minute missed */
				do {
					if (job_runqueue())
						sleep(10);
					virtualTime++;
					find_jobs(virtualTime, database,
					    FALSE, TRUE);
					set_time(FALSE);
				} while (virtualTime< timeRunning &&
				    clockTime == timeRunning);
				break;

			case negative:
				/*
				 * case 3: timeDiff is a small or medium-sized
				 * negative num, eg. because of DST ending.
				 * Just run the wildcard jobs. The fixed-time
				 * jobs probably have already run, and should
				 * not be repeated.  Virtual time does not
				 * change until we are caught up.
				 */
				find_jobs(timeRunning, database, TRUE, FALSE);
				break;
			default:
				/*
				 * other: time has changed a *lot*,
				 * jump virtual time, and run everything
				 */
				virtualTime = timeRunning;
				find_jobs(timeRunning, database, TRUE, TRUE);
			}
		}

		/* Jobs to be run (if any) are loaded; clear the queue. */
		job_runqueue();

		/* Run any jobs in the at queue. */
		atrun(at_database, batch_maxload,
		    timeRunning * SECONDS_PER_MINUTE - GMToff);

		/* Reload jobs as needed. */
		load_database(&database);
		scan_atjobs(&at_database, NULL);
	}
}

static void
run_reboot_jobs(cron_db *db)
{
	user *u;
	entry *e;

	TAILQ_FOREACH(u, &db->users, entries) {
		SLIST_FOREACH(e, &u->crontab, entries) {
			if (e->flags & WHEN_REBOOT)
				job_add(e, u);
		}
	}
	(void) job_runqueue();
}

static void
find_jobs(time_t vtime, cron_db *db, int doWild, int doNonWild)
{
	time_t virtualSecond  = vtime * SECONDS_PER_MINUTE;
	struct tm *tm = gmtime(&virtualSecond);
	int minute, hour, dom, month, dow;
	user *u;
	entry *e;

	/* make 0-based values out of these so we can use them as indices
	 */
	minute = tm->tm_min -FIRST_MINUTE;
	hour = tm->tm_hour -FIRST_HOUR;
	dom = tm->tm_mday -FIRST_DOM;
	month = tm->tm_mon +1 /* 0..11 -> 1..12 */ -FIRST_MONTH;
	dow = tm->tm_wday -FIRST_DOW;

	/* the dom/dow situation is odd.  '* * 1,15 * Sun' will run on the
	 * first and fifteenth AND every Sunday;  '* * * * Sun' will run *only*
	 * on Sundays;  '* * 1,15 * *' will run *only* the 1st and 15th.  this
	 * is why we keep 'e->dow_star' and 'e->dom_star'.  yes, it's bizarre.
	 * like many bizarre things, it's the standard.
	 */
	TAILQ_FOREACH(u, &db->users, entries) {
		SLIST_FOREACH(e, &u->crontab, entries) {
			if (bit_test(e->minute, minute) &&
			    bit_test(e->hour, hour) &&
			    bit_test(e->month, month) &&
			    ( ((e->flags & DOM_STAR) || (e->flags & DOW_STAR))
			      ? (bit_test(e->dow,dow) && bit_test(e->dom,dom))
			      : (bit_test(e->dow,dow) || bit_test(e->dom,dom))
			    )
			   ) {
				if ((doNonWild &&
				    !(e->flags & (MIN_STAR|HR_STAR))) ||
				    (doWild && (e->flags & (MIN_STAR|HR_STAR))))
					job_add(e, u);
			}
		}
	}
}

/*
 * Set StartTime and clockTime to the current time.
 * These are used for computing what time it really is right now.
 * Note that clockTime is a unix wallclock time converted to minutes.
 */
static void
set_time(int initialize)
{
	struct tm tm;
	static int isdst;

	StartTime = time(NULL);

	/* We adjust the time to GMT so we can catch DST changes. */
	tm = *localtime(&StartTime);
	if (initialize || tm.tm_isdst != isdst) {
		isdst = tm.tm_isdst;
		GMToff = get_gmtoff(&StartTime, &tm);
	}
	clockTime = (StartTime + GMToff) / (time_t)SECONDS_PER_MINUTE;
}

/*
 * Try to just hit the next minute.
 */
static void
cron_sleep(time_t target, sigset_t *mask)
{
	int fd, nfds;
	unsigned char poke;
	struct timespec t1, t2, timeout;
	struct sockaddr_un s_un;
	socklen_t sunlen;
	static struct pollfd pfd[1];

	clock_gettime(CLOCK_REALTIME, &t1);
	t1.tv_sec += GMToff;
	timeout.tv_sec = (target * SECONDS_PER_MINUTE - t1.tv_sec) + 1;
	timeout.tv_nsec = 0;

	pfd[0].fd = cronSock;
	pfd[0].events = POLLIN;

	while (timespecisset(&timeout) && timeout.tv_sec < 65) {
		poke = RELOAD_CRON | RELOAD_AT;

		/* Sleep until we time out, get a poke, or get a signal. */
		nfds = ppoll(pfd, 1, &timeout, mask);
		if (nfds == 0)
			break;		/* timer expired */
		if (nfds == -1 && errno != EINTR)
			break;		/* an error occurred */
		if (nfds > 0) {
			sunlen = sizeof(s_un);
			fd = accept4(cronSock, (struct sockaddr *)&s_un,
			    &sunlen, SOCK_NONBLOCK);
			if (fd >= 0) {
				(void) read(fd, &poke, 1);
				close(fd);
				if (poke & RELOAD_CRON) {
					timespecclear(&database->mtime);
					load_database(&database);
				}
				if (poke & RELOAD_AT) {
					/*
					 * We run any pending at jobs right
					 * away so that "at now" really runs
					 * jobs immediately.
					 */
					clock_gettime(CLOCK_REALTIME, &t2);
					timespecclear(&at_database->mtime);
					if (scan_atjobs(&at_database, &t2))
						atrun(at_database,
						    batch_maxload, t2.tv_sec);
				}
			}
		} else {
			/* Interrupted by a signal. */
			if (got_sigchld) {
				got_sigchld = 0;
				sigchld_reaper();
			}
		}

		/* Adjust tv and continue where we left off.  */
		clock_gettime(CLOCK_REALTIME, &t2);
		t2.tv_sec += GMToff;
		timespecsub(&t2, &t1, &t1);
		timespecsub(&timeout, &t1, &timeout);
		memcpy(&t1, &t2, sizeof(t1));
		if (timeout.tv_sec < 0)
			timeout.tv_sec = 0;
		if (timeout.tv_nsec < 0)
			timeout.tv_nsec = 0;
	}
}

/* int open_socket(void)
 *	opens a UNIX domain socket that crontab uses to poke cron.
 *	If the socket is already in use, return an error.
 */
static int
open_socket(void)
{
	int		   sock, rc;
	mode_t		   omask;
	struct sockaddr_un s_un;

	sock = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
	if (sock == -1) {
		warn("socket");
		syslog(LOG_ERR, "(CRON) DEATH (can't create socket)");
		exit(EXIT_FAILURE);
	}
	bzero(&s_un, sizeof(s_un));
	if (strlcpy(s_un.sun_path, _PATH_CRON_SOCK, sizeof(s_un.sun_path))
	    >= sizeof(s_un.sun_path)) {
		warnc(ENAMETOOLONG, _PATH_CRON_SOCK);
		syslog(LOG_ERR, "(CRON) DEATH (socket path too long)");
		exit(EXIT_FAILURE);
	}
	s_un.sun_family = AF_UNIX;

	if (connect(sock, (struct sockaddr *)&s_un, sizeof(s_un)) == 0) {
		warnx("already running");
		syslog(LOG_ERR, "(CRON) DEATH (already running)");
		exit(EXIT_FAILURE);
	}
	if (errno != ENOENT)
		unlink(s_un.sun_path);

	omask = umask(007);
	rc = bind(sock, (struct sockaddr *)&s_un, sizeof(s_un));
	umask(omask);
	if (rc != 0) {
		warn("bind");
		syslog(LOG_ERR, "(CRON) DEATH (can't bind socket)");
		exit(EXIT_FAILURE);
	}
	if (listen(sock, SOMAXCONN)) {
		warn("listen");
		syslog(LOG_ERR, "(CRON) DEATH (can't listen on socket)");
		exit(EXIT_FAILURE);
	}

	/* pledge won't let us change files to a foreign group. */
	if (setegid(cron_gid) == 0) {
		chown(s_un.sun_path, -1, cron_gid);
		(void)setegid(getgid());
	}
	chmod(s_un.sun_path, 0660);

	return(sock);
}

static void
sigchld_handler(int x)
{
	got_sigchld = 1;
}

static void
sigchld_reaper(void)
{
	int waiter;
	pid_t pid;

	do {
		pid = waitpid(-1, &waiter, WNOHANG);
		switch (pid) {
		case -1:
			if (errno == EINTR)
				continue;
			break;
		case 0:
			break;
		default:
			break;
		}
	} while (pid > 0);
}

static void
parse_args(int argc, char *argv[])
{
	int argch;
	char *ep;

	while (-1 != (argch = getopt(argc, argv, "l:n"))) {
		switch (argch) {
		case 'l':
			errno = 0;
			batch_maxload = strtod(optarg, &ep);
			if (*ep != '\0' || ep == optarg || errno == ERANGE ||
			    batch_maxload < 0) {
				warnx("illegal load average: %s", optarg);
				usage();
			}
			break;
		case 'n':
			NoFork = 1;
			break;
		default:
			usage();
		}
	}
}