blob: bd43c6b68689d2aa023b18ac5e1abbbd45234fbf [file] [log] [blame]
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +00002/*
Eric Andersenf6f7bfb2002-10-22 12:24:59 +00003 * run as root, but NOT setuid root
4 *
5 * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +00006 * (version 2.3.2)
Mike Frysingerf284c762006-04-16 20:38:26 +00007 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
Eric Andersenf6f7bfb2002-10-22 12:24:59 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000010 */
Denys Vlasenko01a1a962014-04-30 14:47:28 +020011//config:config CROND
Denys Vlasenkob097a842018-12-28 03:20:17 +010012//config: bool "crond (14 kb)"
Denys Vlasenko01a1a962014-04-30 14:47:28 +020013//config: default y
14//config: select FEATURE_SYSLOG
15//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020016//config: Crond is a background daemon that parses individual crontab
17//config: files and executes commands on behalf of the users in question.
18//config: This is a port of dcron from slackware. It uses files of the
19//config: format /var/spool/cron/crontabs/<username> files, for example:
20//config: $ cat /var/spool/cron/crontabs/root
21//config: # Run daily cron jobs at 4:40 every day:
22//config: 40 4 * * * /etc/cron/daily > /dev/null 2>&1
Denys Vlasenko01a1a962014-04-30 14:47:28 +020023//config:
24//config:config FEATURE_CROND_D
Denys Vlasenko5b3cbe32017-07-27 14:45:25 +020025//config: bool "Support -d (redirect output to stderr)"
Denys Vlasenko01a1a962014-04-30 14:47:28 +020026//config: depends on CROND
27//config: default y
28//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020029//config: -d N sets loglevel (0:most verbose) and directs all output to stderr.
Denys Vlasenko01a1a962014-04-30 14:47:28 +020030//config:
31//config:config FEATURE_CROND_CALL_SENDMAIL
32//config: bool "Report command output via email (using sendmail)"
33//config: default y
34//config: depends on CROND
35//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020036//config: Command output will be sent to corresponding user via email.
Denys Vlasenko01a1a962014-04-30 14:47:28 +020037//config:
Denys Vlasenko75fbea32017-07-08 20:53:11 +020038//config:config FEATURE_CROND_SPECIAL_TIMES
39//config: bool "Support special times (@reboot, @daily, etc) in crontabs"
40//config: default y
41//config: depends on CROND
42//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020043//config: string meaning
44//config: ------ -------
45//config: @reboot Run once, at startup
46//config: @yearly Run once a year: "0 0 1 1 *"
47//config: @annually Same as @yearly: "0 0 1 1 *"
48//config: @monthly Run once a month: "0 0 1 * *"
49//config: @weekly Run once a week: "0 0 * * 0"
50//config: @daily Run once a day: "0 0 * * *"
51//config: @midnight Same as @daily: "0 0 * * *"
52//config: @hourly Run once an hour: "0 * * * *"
Denys Vlasenko75fbea32017-07-08 20:53:11 +020053//config:
Denys Vlasenko01a1a962014-04-30 14:47:28 +020054//config:config FEATURE_CROND_DIR
55//config: string "crond spool directory"
56//config: default "/var/spool/cron"
57//config: depends on CROND || CRONTAB
58//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020059//config: Location of crond spool.
Denys Vlasenko01a1a962014-04-30 14:47:28 +020060
61//applet:IF_CROND(APPLET(crond, BB_DIR_USR_SBIN, BB_SUID_DROP))
62
63//kbuild:lib-$(CONFIG_CROND) += crond.o
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000064
Pere Orga5bc8c002011-04-11 03:29:49 +020065//usage:#define crond_trivial_usage
Denys Vlasenko11f1a252020-12-13 19:04:19 +010066//usage: "[-fbS] [-l N] " IF_FEATURE_CROND_D("[-d N] ") "[-L LOGFILE] [-c DIR]"
Pere Orga5bc8c002011-04-11 03:29:49 +020067//usage:#define crond_full_usage "\n\n"
68//usage: " -f Foreground"
69//usage: "\n -b Background (default)"
70//usage: "\n -S Log to syslog (default)"
Denys Vlasenkof3d705f2017-01-21 03:46:35 +010071//usage: "\n -l N Set log level. Most verbose 0, default 8"
Pere Orga5bc8c002011-04-11 03:29:49 +020072//usage: IF_FEATURE_CROND_D(
Denys Vlasenko01a1a962014-04-30 14:47:28 +020073//usage: "\n -d N Set log level, log to stderr"
Pere Orga5bc8c002011-04-11 03:29:49 +020074//usage: )
Denys Vlasenko01a1a962014-04-30 14:47:28 +020075//usage: "\n -L FILE Log to FILE"
76//usage: "\n -c DIR Cron dir. Default:"CONFIG_FEATURE_CROND_DIR"/crontabs"
Pere Orga5bc8c002011-04-11 03:29:49 +020077
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000078#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020079#include "common_bufsiz.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000080#include <syslog.h>
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000081
Denys Vlasenkobbf17bb2017-07-22 02:25:47 +020082#if 0
83/* If libc tracks and reuses setenv()-allocated memory, ok to set this to 0 */
84/* Neither glibc nor uclibc do that! */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020085# define SETENV_LEAKS 0
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000086#else
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020087# define SETENV_LEAKS 1
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000088#endif
89
90
Denys Vlasenko01a1a962014-04-30 14:47:28 +020091#define CRON_DIR CONFIG_FEATURE_CROND_DIR
Denis Vlasenkoded5dfe2009-02-03 23:59:41 +000092#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
Denys Vlasenko75fbea32017-07-08 20:53:11 +020093#define CRON_REBOOT CONFIG_PID_FILE_PATH "/crond.reboot"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000094#ifndef SENDMAIL
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020095# define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000096#endif
97#ifndef SENDMAIL_ARGS
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +020098# define SENDMAIL_ARGS "-ti"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000099#endif
100#ifndef CRONUPDATE
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200101# define CRONUPDATE "cron.update"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000102#endif
103#ifndef MAXLINES
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200104# define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000105#endif
106
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000107
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000108typedef struct CronFile {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200109 struct CronFile *cf_next;
110 struct CronLine *cf_lines;
111 char *cf_username;
112 smallint cf_wants_starting; /* bool: one or more jobs ready */
113 smallint cf_has_running; /* bool: one or more jobs running */
114 smallint cf_deleted; /* marked for deletion (but still has running jobs) */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000115} CronFile;
116
117typedef struct CronLine {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200118 struct CronLine *cl_next;
119 char *cl_cmd; /* shell command */
120 pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200121#define START_ME_REBOOT -2
122#define START_ME_NORMAL -1
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000123#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200124 int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200125 char *cl_mailto; /* whom to mail results, may be NULL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000126#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200127 char *cl_shell;
Paul Fox52a7bf62022-03-07 11:35:28 -0500128 char *cl_path;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000129 /* ordered by size, not in natural order. makes code smaller: */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200130 char cl_Dow[7]; /* 0-6, beginning sunday */
131 char cl_Mons[12]; /* 0-11 */
132 char cl_Hrs[24]; /* 0-23 */
133 char cl_Days[32]; /* 1-31 */
134 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000135} CronLine;
136
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000137
Denys Vlasenko45963c82010-07-08 15:37:10 +0200138#define DAEMON_UID 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000139
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000140
141enum {
142 OPT_l = (1 << 0),
143 OPT_L = (1 << 1),
144 OPT_f = (1 << 2),
145 OPT_b = (1 << 3),
146 OPT_S = (1 << 4),
147 OPT_c = (1 << 5),
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000148 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000149};
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000150
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000151struct globals {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200152 unsigned log_level; /* = 8; */
153 time_t crontab_dir_mtime;
154 const char *log_filename;
155 const char *crontab_dir_name; /* = CRONTABS; */
156 CronFile *cron_files;
Denys Vlasenkoc9e78432017-07-22 01:47:19 +0200157 char *default_shell;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000158#if SETENV_LEAKS
159 char *env_var_user;
160 char *env_var_home;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200161 char *env_var_shell;
162 char *env_var_logname;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000163#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100164} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200165#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000166#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200167 setup_common_bufsiz(); \
Denys Vlasenko45963c82010-07-08 15:37:10 +0200168 G.log_level = 8; \
169 G.crontab_dir_name = CRONTABS; \
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000170} while (0)
171
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200172/* Log levels:
173 * 0 is the most verbose, default 8.
174 * For some reason, in fact only 5, 7 and 8 are used.
175 */
176static void crondlog(unsigned level, const char *msg, va_list va)
177{
178 if (level >= G.log_level) {
179 /*
180 * We are called only for info meesages.
181 * Warnings/errors use plain bb_[p]error_msg's, which
182 * need not touch syslog_level
183 * (they are ok with LOG_ERR default).
184 */
James Byrne253c4e72019-04-12 17:01:51 +0000185 bb_vinfo_msg(msg, va);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200186 }
187}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000188
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200189static void log5(const char *msg, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000190{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000191 va_list va;
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200192 va_start(va, msg);
193 crondlog(4, msg, va);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000194 va_end(va);
Eric Andersen35e643b2003-07-28 07:40:39 +0000195}
196
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200197static void log7(const char *msg, ...)
198{
199 va_list va;
200 va_start(va, msg);
201 crondlog(7, msg, va);
202 va_end(va);
203}
204
205static void log8(const char *msg, ...)
206{
207 va_list va;
208 va_start(va, msg);
209 crondlog(8, msg, va);
210 va_end(va);
211}
212
213
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000214static const char DowAry[] ALIGN1 =
215 "sun""mon""tue""wed""thu""fri""sat"
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000216;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000217
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000218static const char MonAry[] ALIGN1 =
219 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000220;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000221
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000222static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000223 const char *names, char *ptr)
224/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000225{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000226 char *base = ptr;
227 int n1 = -1;
228 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000229
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000230 // this can't happen due to config_read()
231 /*if (base == NULL)
232 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000233
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000234 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000235 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000236
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000237 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000238 if (*ptr == '*') {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200239 n1 = 0; /* everything will be filled */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000240 n2 = modvalue - 1;
241 skip = 1;
242 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000243 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200244 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000245 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200246 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000247 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200248 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000249 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200250 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000251 skip = 1;
252 } else if (names) {
253 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000254
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000255 for (i = 0; names[i]; i += 3) {
256 /* was using strncmp before... */
257 if (strncasecmp(ptr, &names[i], 3) == 0) {
258 ptr += 3;
259 if (n1 < 0) {
260 n1 = i / 3;
261 } else {
262 n2 = i / 3;
263 }
264 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000265 break;
266 }
267 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000268 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000269
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000270 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000271 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000272 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000273 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000274 if (*ptr == '-' && n2 < 0) {
275 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000276 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000277 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000278
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000279 /*
280 * collapse single-value ranges, handle skipmark, and fill
281 * in the character array appropriately.
282 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000283 if (n2 < 0) {
284 n2 = n1;
285 }
286 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200287 char *endp;
288 skip = strtol(ptr + 1, &endp, 10);
289 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000290 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000291
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000292 /*
293 * fill array, using a failsafe is the easiest way to prevent
294 * an endless loop
295 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000296 {
297 int s0 = 1;
298 int failsafe = 1024;
299
300 --n1;
301 do {
302 n1 = (n1 + 1) % modvalue;
303
304 if (--s0 == 0) {
305 ary[n1 % modvalue] = 1;
306 s0 = skip;
307 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000308 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000309 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000310 }
311 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000312 }
313 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000314 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000315 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000316 ++ptr;
317 n1 = -1;
318 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000319 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000320
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000321 if (*ptr) {
322 err:
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200323 bb_error_msg("user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000324 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000325 }
326
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200327 /* can't use log5 (it inserts newlines), open-coding it */
328 if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000329 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000330 for (i = 0; i < modvalue; ++i)
331 fprintf(stderr, "%d", (unsigned char)ary[i]);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200332 bb_putchar_stderr('\n');
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000333 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000334}
335
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000336static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000337{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000338 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000339 int weekUsed = 0;
340 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000341
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000342 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000343 if (line->cl_Dow[i] == 0) {
344 weekUsed = 1;
345 break;
346 }
347 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000348 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000349 if (line->cl_Days[i] == 0) {
350 daysUsed = 1;
351 break;
352 }
353 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000354 if (weekUsed != daysUsed) {
355 if (weekUsed)
356 memset(line->cl_Days, 0, sizeof(line->cl_Days));
357 else /* daysUsed */
358 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000359 }
360}
361
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200362/*
Denys Vlasenko45963c82010-07-08 15:37:10 +0200363 * delete_cronfile() - delete user database
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200364 *
365 * Note: multiple entries for same user may exist if we were unable to
366 * completely delete a database due to running processes.
367 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200368//FIXME: we will start a new job even if the old job is running
369//if crontab was reloaded: crond thinks that "new" job is different from "old"
370//even if they are in fact completely the same. Example
371//Crontab was:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200372// 0-59 * * * * job1
373// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200374//User edits crontab to:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200375// 0-59 * * * * job1_updated
376// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200377//Bug: crond can now start another long_running_job2 even if old one
378//is still running.
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200379//OTOH most other versions of cron do not wait for job termination anyway,
380//they end up with multiple copies of jobs if they don't terminate soon enough.
Denys Vlasenko45963c82010-07-08 15:37:10 +0200381static void delete_cronfile(const char *userName)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200382{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200383 CronFile **pfile = &G.cron_files;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200384 CronFile *file;
385
386 while ((file = *pfile) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200387 if (strcmp(userName, file->cf_username) == 0) {
388 CronLine **pline = &file->cf_lines;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200389 CronLine *line;
390
Denys Vlasenko45963c82010-07-08 15:37:10 +0200391 file->cf_has_running = 0;
392 file->cf_deleted = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200393
394 while ((line = *pline) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200395 if (line->cl_pid > 0) {
396 file->cf_has_running = 1;
397 pline = &line->cl_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200398 } else {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200399 *pline = line->cl_next;
400 free(line->cl_cmd);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200401 free(line);
402 }
403 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200404 if (file->cf_has_running == 0) {
405 *pfile = file->cf_next;
406 free(file->cf_username);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200407 free(file);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200408 continue;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200409 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200410 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200411 pfile = &file->cf_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200412 }
413}
414
Denys Vlasenko45963c82010-07-08 15:37:10 +0200415static void load_crontab(const char *fileName)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000416{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000417 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000418 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000419 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000420 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000421#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
422 char *mailTo = NULL;
423#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200424 char *shell = NULL;
Paul Fox52a7bf62022-03-07 11:35:28 -0500425 char *path = NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000426
Denys Vlasenko45963c82010-07-08 15:37:10 +0200427 delete_cronfile(fileName);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000428
Denys Vlasenko45963c82010-07-08 15:37:10 +0200429 if (!getpwnam(fileName)) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200430 log7("ignoring file '%s' (no such user)", fileName);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200431 return;
432 }
433
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000434 parser = config_open(fileName);
435 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000436 return;
437
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000438 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000439
Denys Vlasenko45963c82010-07-08 15:37:10 +0200440 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000441 CronFile *file = xzalloc(sizeof(CronFile));
442 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000443 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000444
Denys Vlasenko45963c82010-07-08 15:37:10 +0200445 file->cf_username = xstrdup(fileName);
446 pline = &file->cf_lines;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000447
Denis Vlasenko084266e2008-07-26 23:08:31 +0000448 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000449 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000450
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200451 if (!--maxLines) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200452 bb_error_msg("user %s: too many lines", fileName);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000453 break;
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200454 }
455
Denis Vlasenko084266e2008-07-26 23:08:31 +0000456 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
457 if (!n)
458 break;
459
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200460 log5("user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000461
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000462 /* check if line is setting MAILTO= */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100463 if (is_prefixed_with(tokens[0], "MAILTO=")) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000464#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
465 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000466 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000467#endif /* otherwise just ignore such lines */
468 continue;
469 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100470 if (is_prefixed_with(tokens[0], "SHELL=")) {
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200471 free(shell);
472 shell = xstrdup(&tokens[0][6]);
473 continue;
474 }
Paul Fox52a7bf62022-03-07 11:35:28 -0500475 if (is_prefixed_with(tokens[0], "PATH=")) {
476 free(path);
477 path = xstrdup(&tokens[0][5]);
478 continue;
479 }
480//TODO: handle HOME= too? Better yet, handle arbitrary ENVVARs? "man crontab" says:
Denys Vlasenko0b3b65f2017-07-09 00:00:39 +0200481//name = value
482//
483//where the spaces around the equal-sign (=) are optional, and any subsequent
484//non-leading spaces in value will be part of the value assigned to name.
485//The value string may be placed in quotes (single or double, but matching)
486//to preserve leading or trailing blanks.
487//
488//Several environment variables are set up automatically by the cron(8) daemon.
489//SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd
Paul Fox52a7bf62022-03-07 11:35:28 -0500490//line of the crontab's owner. HOME, SHELL, and PATH may be overridden by
491//settings in the crontab; LOGNAME may not.
Denys Vlasenko0b3b65f2017-07-09 00:00:39 +0200492
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200493#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
494 if (tokens[0][0] == '@') {
495 /*
496 * "@daily /a/script/to/run PARAM1 PARAM2..."
497 */
498 typedef struct SpecialEntry {
499 const char *name;
500 const char tokens[8];
501 } SpecialEntry;
Denys Vlasenko965b7952020-11-30 13:03:03 +0100502 static const SpecialEntry SpecAry[] ALIGN8 = {
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200503 /* hour day month weekday */
504 { "yearly", "0\0" "1\0" "1\0" "*" },
505 { "annually", "0\0" "1\0" "1\0" "*" },
506 { "monthly", "0\0" "1\0" "*\0" "*" },
507 { "weekly", "0\0" "*\0" "*\0" "0" },
508 { "daily", "0\0" "*\0" "*\0" "*" },
509 { "midnight", "0\0" "*\0" "*\0" "*" },
510 { "hourly", "*\0" "*\0" "*\0" "*" },
511 { "reboot", "" },
512 };
513 const SpecialEntry *e = SpecAry;
514
515 if (n < 2)
516 continue;
517 for (;;) {
518 if (strcmp(e->name, tokens[0] + 1) == 0) {
519 /*
520 * tokens[1] is only the first word of command,
Denys Vlasenkod18b2002017-07-09 00:08:13 +0200521 * can'r use it.
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200522 * find the entire command in unmodified string:
523 */
Denys Vlasenkod18b2002017-07-09 00:08:13 +0200524 tokens[5] = skip_whitespace(
525 skip_non_whitespace(
526 skip_whitespace(parser->data)));
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200527 if (e->tokens[0]) {
528 char *et = (char*)e->tokens;
529 /* minute is "0" for all specials */
530 tokens[0] = (char*)"0";
531 tokens[1] = et;
532 tokens[2] = et + 2;
533 tokens[3] = et + 4;
534 tokens[4] = et + 6;
535 }
536 goto got_it;
537 }
538 if (!e->tokens[0])
539 break;
540 e++;
541 }
542 continue; /* bad line (unrecognized '@foo') */
543 }
544#endif
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000545 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000546 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000547 continue;
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200548 IF_FEATURE_CROND_SPECIAL_TIMES(
549 got_it:
550 )
Denis Vlasenko084266e2008-07-26 23:08:31 +0000551 *pline = line = xzalloc(sizeof(*line));
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200552#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
553 if (tokens[0][0] == '@') { /* "@reboot" line */
554 file->cf_wants_starting = 1;
555 line->cl_pid = START_ME_REBOOT; /* wants to start */
556 /* line->cl_Mins/Hrs/etc stay zero: never match any time */
557 } else
558#endif
559 {
560 /* parse date ranges */
561 ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
562 ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
563 ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
564 ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
565 ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
566 /*
567 * fix days and dow - if one is not "*" and the other
568 * is "*", the other is set to 0, and vise-versa
569 */
570 FixDayDow(line);
571 }
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000572#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
573 /* copy mailto (can be NULL) */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200574 line->cl_mailto = xstrdup(mailTo);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000575#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200576 line->cl_shell = xstrdup(shell);
Paul Fox52a7bf62022-03-07 11:35:28 -0500577 line->cl_path = xstrdup(path);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000578 /* copy command */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200579 line->cl_cmd = xstrdup(tokens[5]);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200580 pline = &line->cl_next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000581//bb_error_msg("M[%s]F[%s][%s][%s][%s][%s][%s]", mailTo, tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000582 }
583 *pline = NULL;
584
Denys Vlasenko45963c82010-07-08 15:37:10 +0200585 file->cf_next = G.cron_files;
586 G.cron_files = file;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000587 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000588 config_close(parser);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200589#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
590 free(mailTo);
591#endif
592 free(shell);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000593}
594
Denys Vlasenko45963c82010-07-08 15:37:10 +0200595static void process_cron_update_file(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000596{
597 FILE *fi;
598 char buf[256];
599
Denis Vlasenko5415c852008-07-21 23:05:26 +0000600 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000601 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000602 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000603 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000604 /* use first word only */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200605 skip_non_whitespace(buf)[0] = '\0';
606 load_crontab(buf);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000607 }
608 fclose(fi);
609 }
610}
611
Denys Vlasenko45963c82010-07-08 15:37:10 +0200612static void rescan_crontab_dir(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000613{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000614 CronFile *file;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200615
616 /* Delete all files until we only have ones with running jobs (or none) */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000617 again:
Denys Vlasenko45963c82010-07-08 15:37:10 +0200618 for (file = G.cron_files; file; file = file->cf_next) {
619 if (!file->cf_deleted) {
620 delete_cronfile(file->cf_username);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000621 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000622 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000623 }
624
Denys Vlasenko45963c82010-07-08 15:37:10 +0200625 /* Remove cron update file */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000626 unlink(CRONUPDATE);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200627 /* Re-chdir, in case directory was renamed & deleted */
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200628 xchdir(G.crontab_dir_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200629
630 /* Scan directory and add associated users */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000631 {
632 DIR *dir = opendir(".");
633 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000634
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200635 /* xopendir exists, but "can't open '.'" is not informative */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000636 if (!dir)
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200637 bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000638 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000639 if (strchr(den->d_name, '.') != NULL) {
640 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000641 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200642 load_crontab(den->d_name);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000643 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000644 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000645 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000646}
647
Denys Vlasenko45963c82010-07-08 15:37:10 +0200648#if SETENV_LEAKS
649/* We set environment *before* vfork (because we want to use vfork),
650 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
651 * Using putenv(), and freeing memory after unsetenv() won't leak */
652static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000653{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200654 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000655
Denys Vlasenko45963c82010-07-08 15:37:10 +0200656 if (var_val) {
657 bb_unsetenv_and_free(var_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000658 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200659 *pvar_val = xasprintf("%s=%s", var, val);
660 putenv(*pvar_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000661}
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200662#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000663
Paul Fox52a7bf62022-03-07 11:35:28 -0500664static void set_env_vars(struct passwd *pas, const char *shell, const char *path)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000665{
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200666 /* POSIX requires crond to set up at least HOME, LOGNAME, PATH, SHELL.
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200667 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200668#if SETENV_LEAKS
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200669 safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200670 safe_setenv(&G.env_var_user, "USER", pas->pw_name);
671 safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200672 safe_setenv(&G.env_var_shell, "SHELL", shell);
Paul Fox52a7bf62022-03-07 11:35:28 -0500673 if (path) safe_setenv(&G.env_var_shell, "PATH", path);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200674#else
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200675 xsetenv("LOGNAME", pas->pw_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200676 xsetenv("USER", pas->pw_name);
677 xsetenv("HOME", pas->pw_dir);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200678 xsetenv("SHELL", shell);
Paul Fox52a7bf62022-03-07 11:35:28 -0500679 if (path) xsetenv("PATH", path);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200680#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000681}
682
Denys Vlasenko45963c82010-07-08 15:37:10 +0200683static void change_user(struct passwd *pas)
684{
685 /* careful: we're after vfork! */
686 change_identity(pas); /* - initgroups, setgid, setuid */
Denys Vlasenkoc2788f82022-01-13 12:56:10 +0100687 if (chdir_or_warn(pas->pw_dir) != 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200688 xchdir(CRON_DIR);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200689 }
690}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000691
692// TODO: sendmail should be _run-time_ option, not compile-time!
Denys Vlasenko45963c82010-07-08 15:37:10 +0200693#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000694
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200695static pid_t
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200696fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
697{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000698 struct passwd *pas;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200699 const char *shell, *prog;
700 smallint sv_logmode;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000701 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000702
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000703 /* prepare things before vfork */
704 pas = getpwnam(user);
705 if (!pas) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200706 bb_error_msg("can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000707 goto err;
708 }
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200709
Denys Vlasenkoc9e78432017-07-22 01:47:19 +0200710 shell = line->cl_shell ? line->cl_shell : G.default_shell;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200711 prog = run_sendmail ? SENDMAIL : shell;
712
Paul Fox52a7bf62022-03-07 11:35:28 -0500713 set_env_vars(pas, shell, NULL); /* don't use crontab's PATH for sendmail */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000714
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200715 sv_logmode = logmode;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000716 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000717 if (pid == 0) {
718 /* CHILD */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200719 /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200720 change_user(pas);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200721 log5("child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000722 if (mailFd >= 0) {
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200723 xmove_fd(mailFd, run_sendmail ? 0 : 1);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000724 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000725 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000726 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000727 bb_setpgrp();
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200728 if (!run_sendmail)
729 execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
730 else
731 execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200732 /*
733 * I want this error message on stderr too,
734 * even if other messages go only to syslog:
735 */
736 logmode |= LOGMODE_STDIO;
737 bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000738 }
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200739 logmode = sv_logmode;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000740
741 if (pid < 0) {
James Byrne69374872019-07-02 11:35:03 +0200742 bb_simple_perror_msg("vfork");
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000743 err:
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200744 pid = 0;
745 } /* else: PARENT, FORK SUCCESS */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000746
Eric Andersen35e643b2003-07-28 07:40:39 +0000747 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000748 * Close the mail file descriptor.. we can't just leave it open in
749 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000750 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000751 if (mailFd >= 0) {
752 close(mailFd);
753 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200754 return pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000755}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000756
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200757static pid_t start_one_job(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000758{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000759 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000760 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000761
Denys Vlasenko45963c82010-07-08 15:37:10 +0200762 line->cl_pid = 0;
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200763 line->cl_empty_mail_size = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000764
Denys Vlasenko45963c82010-07-08 15:37:10 +0200765 if (line->cl_mailto) {
766 /* Open mail file (owner is root so nobody can screw with it) */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200767 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid());
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000768 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000769
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000770 if (mailFd >= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200771 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
772 line->cl_cmd);
773 line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000774 } else {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200775 bb_error_msg("can't create mail file %s for user %s, "
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000776 "discarding output", mailFile, user);
777 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000778 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000779
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200780 line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 0);
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200781 if (mailFd >= 0) {
782 if (line->cl_pid <= 0) {
783 unlink(mailFile);
784 } else {
785 /* rename mail-file based on pid of process */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200786 char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid);
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200787 rename(mailFile, mailFile2); // TODO: xrename?
788 free(mailFile2);
789 }
790 }
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200791
792 return line->cl_pid;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200793}
794
795/*
796 * process_finished_job - called when job terminates and when mail terminates
797 */
798static void process_finished_job(const char *user, CronLine *line)
799{
800 pid_t pid;
801 int mailFd;
802 char mailFile[128];
803 struct stat sbuf;
804
805 pid = line->cl_pid;
806 line->cl_pid = 0;
807 if (pid <= 0) {
808 /* No job */
809 return;
810 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200811 if (line->cl_empty_mail_size <= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200812 /* End of job and no mail file, or end of sendmail job */
813 return;
814 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200815
816 /*
817 * End of primary job - check for mail file.
818 * If size has changed and the file is still valid, we send it.
819 */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200820 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200821 mailFd = open(mailFile, O_RDONLY);
822 unlink(mailFile);
823 if (mailFd < 0) {
824 return;
825 }
826
827 if (fstat(mailFd, &sbuf) < 0
828 || sbuf.st_uid != DAEMON_UID
829 || sbuf.st_nlink != 0
830 || sbuf.st_size == line->cl_empty_mail_size
831 || !S_ISREG(sbuf.st_mode)
832 ) {
833 close(mailFd);
834 return;
835 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200836 line->cl_empty_mail_size = 0;
837 /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200838 line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 1);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000839}
840
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200841#else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000842
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200843static pid_t start_one_job(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000844{
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200845 const char *shell;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000846 struct passwd *pas;
847 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000848
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000849 pas = getpwnam(user);
850 if (!pas) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200851 bb_error_msg("can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000852 goto err;
853 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000854
Denys Vlasenko45963c82010-07-08 15:37:10 +0200855 /* Prepare things before vfork */
Denys Vlasenkoc9e78432017-07-22 01:47:19 +0200856 shell = line->cl_shell ? line->cl_shell : G.default_shell;
Paul Fox52a7bf62022-03-07 11:35:28 -0500857 set_env_vars(pas, shell, line->cl_path);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200858
859 /* Fork as the user in question and run program */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000860 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000861 if (pid == 0) {
862 /* CHILD */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200863 /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200864 change_user(pas);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200865 log5("child running %s", shell);
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000866 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000867 bb_setpgrp();
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200868 execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
869 bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000870 }
871 if (pid < 0) {
James Byrne69374872019-07-02 11:35:03 +0200872 bb_simple_perror_msg("vfork");
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000873 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000874 pid = 0;
875 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200876 line->cl_pid = pid;
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200877 return pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000878}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000879
Denys Vlasenko45963c82010-07-08 15:37:10 +0200880#define process_finished_job(user, line) ((line)->cl_pid = 0)
881
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200882#endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
883
Denys Vlasenko45963c82010-07-08 15:37:10 +0200884/*
885 * Determine which jobs need to be run. Under normal conditions, the
886 * period is about a minute (one scan). Worst case it will be one
887 * hour (60 scans).
888 */
889static void flag_starting_jobs(time_t t1, time_t t2)
890{
891 time_t t;
892
893 /* Find jobs > t1 and <= t2 */
894
895 for (t = t1 - t1 % 60; t <= t2; t += 60) {
896 struct tm *ptm;
897 CronFile *file;
898 CronLine *line;
899
900 if (t <= t1)
901 continue;
902
903 ptm = localtime(&t);
904 for (file = G.cron_files; file; file = file->cf_next) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200905 log5("file %s:", file->cf_username);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200906 if (file->cf_deleted)
907 continue;
908 for (line = file->cf_lines; line; line = line->cl_next) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200909 log5(" line %s", line->cl_cmd);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200910 if (line->cl_Mins[ptm->tm_min]
911 && line->cl_Hrs[ptm->tm_hour]
912 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
913 && line->cl_Mons[ptm->tm_mon]
914 ) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200915 log5(" job: %d %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200916 (int)line->cl_pid, line->cl_cmd);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200917 if (line->cl_pid > 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200918 log8("user %s: process already running: %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200919 file->cf_username, line->cl_cmd);
920 } else if (line->cl_pid == 0) {
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200921 line->cl_pid = START_ME_NORMAL;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200922 file->cf_wants_starting = 1;
923 }
924 }
925 }
926 }
927 }
928}
929
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200930#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
931static int touch_reboot_file(void)
932{
933 int fd = open(CRON_REBOOT, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0000);
934 if (fd >= 0) {
935 close(fd);
936 return 1;
937 }
938 /* File (presumably) exists - this is not the first run after reboot */
939 return 0;
940}
941#endif
942
943static void start_jobs(int wants_start)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200944{
945 CronFile *file;
946 CronLine *line;
947
Denys Vlasenko45963c82010-07-08 15:37:10 +0200948 for (file = G.cron_files; file; file = file->cf_next) {
949 if (!file->cf_wants_starting)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200950 continue;
951
Denys Vlasenko45963c82010-07-08 15:37:10 +0200952 file->cf_wants_starting = 0;
953 for (line = file->cf_lines; line; line = line->cl_next) {
954 pid_t pid;
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200955 if (line->cl_pid != wants_start)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200956 continue;
957
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200958 pid = start_one_job(file->cf_username, line);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200959 log8("USER %s pid %3d cmd %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200960 file->cf_username, (int)pid, line->cl_cmd);
961 if (pid < 0) {
962 file->cf_wants_starting = 1;
963 }
964 if (pid > 0) {
965 file->cf_has_running = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200966 }
967 }
968 }
969}
970
Denys Vlasenko45963c82010-07-08 15:37:10 +0200971/*
972 * Check for job completion, return number of jobs still running after
973 * all done.
974 */
975static int check_completions(void)
976{
977 CronFile *file;
978 CronLine *line;
979 int num_still_running = 0;
980
981 for (file = G.cron_files; file; file = file->cf_next) {
982 if (!file->cf_has_running)
983 continue;
984
985 file->cf_has_running = 0;
986 for (line = file->cf_lines; line; line = line->cl_next) {
987 int r;
988
989 if (line->cl_pid <= 0)
990 continue;
991
992 r = waitpid(line->cl_pid, NULL, WNOHANG);
993 if (r < 0 || r == line->cl_pid) {
994 process_finished_job(file->cf_username, line);
995 if (line->cl_pid == 0) {
996 /* sendmail was not started for it */
997 continue;
998 }
999 /* else: sendmail was started, job is still running, fall thru */
1000 }
1001 /* else: r == 0: "process is still running" */
1002 file->cf_has_running = 1;
1003 }
1004//FIXME: if !file->cf_has_running && file->deleted: delete it!
1005//otherwise deleted entries will stay forever, right?
1006 num_still_running += file->cf_has_running;
1007 }
1008 return num_still_running;
1009}
1010
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001011static void reopen_logfile_to_stderr(void)
1012{
1013 if (G.log_filename) {
1014 int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
1015 if (logfd >= 0)
1016 xmove_fd(logfd, STDERR_FILENO);
1017 }
1018}
1019
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001020int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1021int crond_main(int argc UNUSED_PARAM, char **argv)
1022{
1023 time_t t2;
Denys Vlasenko01a1a962014-04-30 14:47:28 +02001024 unsigned rescan;
1025 unsigned sleep_time;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001026 unsigned opts;
1027
1028 INIT_G();
1029
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001030 opts = getopt32(argv, "^"
1031 "l:L:fbSc:" IF_FEATURE_CROND_D("d:")
1032 "\0"
1033 /* "-b after -f is ignored", and so on for every pair a-b */
1034 "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
Denys Vlasenko8c84f752011-09-07 05:56:09 +02001035 /* -l and -d have numeric param */
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001036 ":l+" IF_FEATURE_CROND_D(":d+")
1037 ,
Denys Vlasenko45963c82010-07-08 15:37:10 +02001038 &G.log_level, &G.log_filename, &G.crontab_dir_name
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001039 IF_FEATURE_CROND_D(,&G.log_level)
1040 );
Denys Vlasenko45963c82010-07-08 15:37:10 +02001041 /* both -d N and -l N set the same variable: G.log_level */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001042
1043 if (!(opts & OPT_f)) {
1044 /* close stdin, stdout, stderr.
1045 * close unused descriptors - don't need them. */
1046 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
1047 }
1048
Denys Vlasenko45963c82010-07-08 15:37:10 +02001049 if (!(opts & OPT_d) && G.log_filename == NULL) {
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001050 /* logging to syslog */
1051 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
1052 logmode = LOGMODE_SYSLOG;
1053 }
1054
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001055 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +02001056
1057 reopen_logfile_to_stderr();
1058 xchdir(G.crontab_dir_name);
Denys Vlasenkoc9e78432017-07-22 01:47:19 +02001059 /* $SHELL, or current UID's shell, or DEFAULT_SHELL */
1060 /* Useful on Android where DEFAULT_SHELL /bin/sh may not exist */
1061 G.default_shell = xstrdup(get_shell_name());
1062
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001063 log8("crond (busybox "BB_VER") started, log level %d", G.log_level);
Denys Vlasenko45963c82010-07-08 15:37:10 +02001064 rescan_crontab_dir();
Denys Vlasenko50596532019-03-17 19:47:52 +01001065 write_pidfile_std_path_and_ext("crond");
Denys Vlasenko75fbea32017-07-08 20:53:11 +02001066#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
1067 if (touch_reboot_file())
1068 start_jobs(START_ME_REBOOT); /* start @reboot entries, if any */
1069#endif
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001070
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001071 /* Main loop */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001072 t2 = time(NULL);
1073 rescan = 60;
1074 sleep_time = 60;
1075 for (;;) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001076 struct stat sbuf;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001077 time_t t1;
1078 long dt;
1079
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001080 /* Synchronize to 1 minute, minimum 1 second */
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001081 t1 = t2;
1082 sleep(sleep_time - (time(NULL) % sleep_time));
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001083 t2 = time(NULL);
1084 dt = (long)t2 - (long)t1;
1085
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001086 reopen_logfile_to_stderr();
1087
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001088 /*
1089 * The file 'cron.update' is checked to determine new cron
1090 * jobs. The directory is rescanned once an hour to deal
1091 * with any screwups.
1092 *
1093 * Check for time jump. Disparities over an hour either way
1094 * result in resynchronization. A negative disparity
1095 * less than an hour causes us to effectively sleep until we
1096 * match the original time (i.e. no re-execution of jobs that
1097 * have just been run). A positive disparity less than
1098 * an hour causes intermediate jobs to be run, but only once
1099 * in the worst case.
1100 *
1101 * When running jobs, the inequality used is greater but not
1102 * equal to t1, and less then or equal to t2.
1103 */
Denys Vlasenko45963c82010-07-08 15:37:10 +02001104 if (stat(G.crontab_dir_name, &sbuf) != 0)
1105 sbuf.st_mtime = 0; /* force update (once) if dir was deleted */
1106 if (G.crontab_dir_mtime != sbuf.st_mtime) {
1107 G.crontab_dir_mtime = sbuf.st_mtime;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001108 rescan = 1;
1109 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001110 if (--rescan == 0) {
1111 rescan = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +02001112 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001113 }
Denys Vlasenko45963c82010-07-08 15:37:10 +02001114 process_cron_update_file();
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001115 log5("wakeup dt=%ld", dt);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001116 if (dt < -60 * 60 || dt > 60 * 60) {
James Byrne253c4e72019-04-12 17:01:51 +00001117 bb_info_msg("time disparity of %ld minutes detected", dt / 60);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001118 /* and we do not run any jobs in this case */
1119 } else if (dt > 0) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001120 /* Usual case: time advances forward, as expected */
Denys Vlasenko45963c82010-07-08 15:37:10 +02001121 flag_starting_jobs(t1, t2);
Denys Vlasenko75fbea32017-07-08 20:53:11 +02001122 start_jobs(START_ME_NORMAL);
Denys Vlasenko01a1a962014-04-30 14:47:28 +02001123 sleep_time = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +02001124 if (check_completions() > 0) {
1125 /* some jobs are still running */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001126 sleep_time = 10;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001127 }
1128 }
1129 /* else: time jumped back, do not run any jobs */
1130 } /* for (;;) */
1131
1132 return 0; /* not reached */
1133}