blob: 2e8ca8b680580e48812667c75b12af1bb482f28d [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
66//usage: "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR"
67//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;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000128 /* ordered by size, not in natural order. makes code smaller: */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200129 char cl_Dow[7]; /* 0-6, beginning sunday */
130 char cl_Mons[12]; /* 0-11 */
131 char cl_Hrs[24]; /* 0-23 */
132 char cl_Days[32]; /* 1-31 */
133 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000134} CronLine;
135
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000136
Denys Vlasenko45963c82010-07-08 15:37:10 +0200137#define DAEMON_UID 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000138
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000139
140enum {
141 OPT_l = (1 << 0),
142 OPT_L = (1 << 1),
143 OPT_f = (1 << 2),
144 OPT_b = (1 << 3),
145 OPT_S = (1 << 4),
146 OPT_c = (1 << 5),
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000147 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000148};
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000149
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000150struct globals {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200151 unsigned log_level; /* = 8; */
152 time_t crontab_dir_mtime;
153 const char *log_filename;
154 const char *crontab_dir_name; /* = CRONTABS; */
155 CronFile *cron_files;
Denys Vlasenkoc9e78432017-07-22 01:47:19 +0200156 char *default_shell;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000157#if SETENV_LEAKS
158 char *env_var_user;
159 char *env_var_home;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200160 char *env_var_shell;
161 char *env_var_logname;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000162#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100163} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200164#define G (*(struct globals*)bb_common_bufsiz1)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000165#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200166 setup_common_bufsiz(); \
Denys Vlasenko45963c82010-07-08 15:37:10 +0200167 G.log_level = 8; \
168 G.crontab_dir_name = CRONTABS; \
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000169} while (0)
170
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200171/* Log levels:
172 * 0 is the most verbose, default 8.
173 * For some reason, in fact only 5, 7 and 8 are used.
174 */
175static void crondlog(unsigned level, const char *msg, va_list va)
176{
177 if (level >= G.log_level) {
178 /*
179 * We are called only for info meesages.
180 * Warnings/errors use plain bb_[p]error_msg's, which
181 * need not touch syslog_level
182 * (they are ok with LOG_ERR default).
183 */
James Byrne253c4e72019-04-12 17:01:51 +0000184 bb_vinfo_msg(msg, va);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200185 }
186}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000187
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200188static void log5(const char *msg, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000189{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000190 va_list va;
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200191 va_start(va, msg);
192 crondlog(4, msg, va);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000193 va_end(va);
Eric Andersen35e643b2003-07-28 07:40:39 +0000194}
195
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200196static void log7(const char *msg, ...)
197{
198 va_list va;
199 va_start(va, msg);
200 crondlog(7, msg, va);
201 va_end(va);
202}
203
204static void log8(const char *msg, ...)
205{
206 va_list va;
207 va_start(va, msg);
208 crondlog(8, msg, va);
209 va_end(va);
210}
211
212
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000213static const char DowAry[] ALIGN1 =
214 "sun""mon""tue""wed""thu""fri""sat"
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000215;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000216
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000217static const char MonAry[] ALIGN1 =
218 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000219;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000220
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000221static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000222 const char *names, char *ptr)
223/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000224{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000225 char *base = ptr;
226 int n1 = -1;
227 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000228
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000229 // this can't happen due to config_read()
230 /*if (base == NULL)
231 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000232
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000233 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000234 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000235
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000236 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000237 if (*ptr == '*') {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200238 n1 = 0; /* everything will be filled */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000239 n2 = modvalue - 1;
240 skip = 1;
241 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000242 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200243 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000244 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200245 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000246 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200247 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000248 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200249 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000250 skip = 1;
251 } else if (names) {
252 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000253
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000254 for (i = 0; names[i]; i += 3) {
255 /* was using strncmp before... */
256 if (strncasecmp(ptr, &names[i], 3) == 0) {
257 ptr += 3;
258 if (n1 < 0) {
259 n1 = i / 3;
260 } else {
261 n2 = i / 3;
262 }
263 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000264 break;
265 }
266 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000267 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000268
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000269 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000270 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000271 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000272 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000273 if (*ptr == '-' && n2 < 0) {
274 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000275 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000276 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000277
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000278 /*
279 * collapse single-value ranges, handle skipmark, and fill
280 * in the character array appropriately.
281 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000282 if (n2 < 0) {
283 n2 = n1;
284 }
285 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200286 char *endp;
287 skip = strtol(ptr + 1, &endp, 10);
288 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000289 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000290
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000291 /*
292 * fill array, using a failsafe is the easiest way to prevent
293 * an endless loop
294 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000295 {
296 int s0 = 1;
297 int failsafe = 1024;
298
299 --n1;
300 do {
301 n1 = (n1 + 1) % modvalue;
302
303 if (--s0 == 0) {
304 ary[n1 % modvalue] = 1;
305 s0 = skip;
306 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000307 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000308 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000309 }
310 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000311 }
312 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000313 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000314 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000315 ++ptr;
316 n1 = -1;
317 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000318 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000319
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000320 if (*ptr) {
321 err:
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200322 bb_error_msg("user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000323 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000324 }
325
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200326 /* can't use log5 (it inserts newlines), open-coding it */
327 if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000328 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000329 for (i = 0; i < modvalue; ++i)
330 fprintf(stderr, "%d", (unsigned char)ary[i]);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200331 bb_putchar_stderr('\n');
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000332 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000333}
334
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000335static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000336{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000337 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000338 int weekUsed = 0;
339 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000340
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000341 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000342 if (line->cl_Dow[i] == 0) {
343 weekUsed = 1;
344 break;
345 }
346 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000347 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000348 if (line->cl_Days[i] == 0) {
349 daysUsed = 1;
350 break;
351 }
352 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000353 if (weekUsed != daysUsed) {
354 if (weekUsed)
355 memset(line->cl_Days, 0, sizeof(line->cl_Days));
356 else /* daysUsed */
357 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000358 }
359}
360
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200361/*
Denys Vlasenko45963c82010-07-08 15:37:10 +0200362 * delete_cronfile() - delete user database
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200363 *
364 * Note: multiple entries for same user may exist if we were unable to
365 * completely delete a database due to running processes.
366 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200367//FIXME: we will start a new job even if the old job is running
368//if crontab was reloaded: crond thinks that "new" job is different from "old"
369//even if they are in fact completely the same. Example
370//Crontab was:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200371// 0-59 * * * * job1
372// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200373//User edits crontab to:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200374// 0-59 * * * * job1_updated
375// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200376//Bug: crond can now start another long_running_job2 even if old one
377//is still running.
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200378//OTOH most other versions of cron do not wait for job termination anyway,
379//they end up with multiple copies of jobs if they don't terminate soon enough.
Denys Vlasenko45963c82010-07-08 15:37:10 +0200380static void delete_cronfile(const char *userName)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200381{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200382 CronFile **pfile = &G.cron_files;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200383 CronFile *file;
384
385 while ((file = *pfile) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200386 if (strcmp(userName, file->cf_username) == 0) {
387 CronLine **pline = &file->cf_lines;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200388 CronLine *line;
389
Denys Vlasenko45963c82010-07-08 15:37:10 +0200390 file->cf_has_running = 0;
391 file->cf_deleted = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200392
393 while ((line = *pline) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200394 if (line->cl_pid > 0) {
395 file->cf_has_running = 1;
396 pline = &line->cl_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200397 } else {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200398 *pline = line->cl_next;
399 free(line->cl_cmd);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200400 free(line);
401 }
402 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200403 if (file->cf_has_running == 0) {
404 *pfile = file->cf_next;
405 free(file->cf_username);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200406 free(file);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200407 continue;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200408 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200409 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200410 pfile = &file->cf_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200411 }
412}
413
Denys Vlasenko45963c82010-07-08 15:37:10 +0200414static void load_crontab(const char *fileName)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000415{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000416 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000417 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000418 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000419 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000420#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
421 char *mailTo = NULL;
422#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200423 char *shell = NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000424
Denys Vlasenko45963c82010-07-08 15:37:10 +0200425 delete_cronfile(fileName);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000426
Denys Vlasenko45963c82010-07-08 15:37:10 +0200427 if (!getpwnam(fileName)) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200428 log7("ignoring file '%s' (no such user)", fileName);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200429 return;
430 }
431
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000432 parser = config_open(fileName);
433 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000434 return;
435
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000436 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000437
Denys Vlasenko45963c82010-07-08 15:37:10 +0200438 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000439 CronFile *file = xzalloc(sizeof(CronFile));
440 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000441 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000442
Denys Vlasenko45963c82010-07-08 15:37:10 +0200443 file->cf_username = xstrdup(fileName);
444 pline = &file->cf_lines;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000445
Denis Vlasenko084266e2008-07-26 23:08:31 +0000446 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000447 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000448
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200449 if (!--maxLines) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200450 bb_error_msg("user %s: too many lines", fileName);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000451 break;
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200452 }
453
Denis Vlasenko084266e2008-07-26 23:08:31 +0000454 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
455 if (!n)
456 break;
457
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200458 log5("user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000459
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000460 /* check if line is setting MAILTO= */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100461 if (is_prefixed_with(tokens[0], "MAILTO=")) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000462#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
463 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000464 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000465#endif /* otherwise just ignore such lines */
466 continue;
467 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100468 if (is_prefixed_with(tokens[0], "SHELL=")) {
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200469 free(shell);
470 shell = xstrdup(&tokens[0][6]);
471 continue;
472 }
Denys Vlasenko0b3b65f2017-07-09 00:00:39 +0200473//TODO: handle HOME= too? "man crontab" says:
474//name = value
475//
476//where the spaces around the equal-sign (=) are optional, and any subsequent
477//non-leading spaces in value will be part of the value assigned to name.
478//The value string may be placed in quotes (single or double, but matching)
479//to preserve leading or trailing blanks.
480//
481//Several environment variables are set up automatically by the cron(8) daemon.
482//SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd
483//line of the crontab's owner. HOME and SHELL may be overridden by settings
484//in the crontab; LOGNAME may not.
485
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200486#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
487 if (tokens[0][0] == '@') {
488 /*
489 * "@daily /a/script/to/run PARAM1 PARAM2..."
490 */
491 typedef struct SpecialEntry {
492 const char *name;
493 const char tokens[8];
494 } SpecialEntry;
495 static const SpecialEntry SpecAry[] = {
496 /* hour day month weekday */
497 { "yearly", "0\0" "1\0" "1\0" "*" },
498 { "annually", "0\0" "1\0" "1\0" "*" },
499 { "monthly", "0\0" "1\0" "*\0" "*" },
500 { "weekly", "0\0" "*\0" "*\0" "0" },
501 { "daily", "0\0" "*\0" "*\0" "*" },
502 { "midnight", "0\0" "*\0" "*\0" "*" },
503 { "hourly", "*\0" "*\0" "*\0" "*" },
504 { "reboot", "" },
505 };
506 const SpecialEntry *e = SpecAry;
507
508 if (n < 2)
509 continue;
510 for (;;) {
511 if (strcmp(e->name, tokens[0] + 1) == 0) {
512 /*
513 * tokens[1] is only the first word of command,
Denys Vlasenkod18b2002017-07-09 00:08:13 +0200514 * can'r use it.
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200515 * find the entire command in unmodified string:
516 */
Denys Vlasenkod18b2002017-07-09 00:08:13 +0200517 tokens[5] = skip_whitespace(
518 skip_non_whitespace(
519 skip_whitespace(parser->data)));
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200520 if (e->tokens[0]) {
521 char *et = (char*)e->tokens;
522 /* minute is "0" for all specials */
523 tokens[0] = (char*)"0";
524 tokens[1] = et;
525 tokens[2] = et + 2;
526 tokens[3] = et + 4;
527 tokens[4] = et + 6;
528 }
529 goto got_it;
530 }
531 if (!e->tokens[0])
532 break;
533 e++;
534 }
535 continue; /* bad line (unrecognized '@foo') */
536 }
537#endif
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000538 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000539 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000540 continue;
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200541 IF_FEATURE_CROND_SPECIAL_TIMES(
542 got_it:
543 )
Denis Vlasenko084266e2008-07-26 23:08:31 +0000544 *pline = line = xzalloc(sizeof(*line));
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200545#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
546 if (tokens[0][0] == '@') { /* "@reboot" line */
547 file->cf_wants_starting = 1;
548 line->cl_pid = START_ME_REBOOT; /* wants to start */
549 /* line->cl_Mins/Hrs/etc stay zero: never match any time */
550 } else
551#endif
552 {
553 /* parse date ranges */
554 ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
555 ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
556 ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
557 ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
558 ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
559 /*
560 * fix days and dow - if one is not "*" and the other
561 * is "*", the other is set to 0, and vise-versa
562 */
563 FixDayDow(line);
564 }
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000565#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
566 /* copy mailto (can be NULL) */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200567 line->cl_mailto = xstrdup(mailTo);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000568#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200569 line->cl_shell = xstrdup(shell);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000570 /* copy command */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200571 line->cl_cmd = xstrdup(tokens[5]);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200572 pline = &line->cl_next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000573//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 +0000574 }
575 *pline = NULL;
576
Denys Vlasenko45963c82010-07-08 15:37:10 +0200577 file->cf_next = G.cron_files;
578 G.cron_files = file;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000579 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000580 config_close(parser);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200581#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
582 free(mailTo);
583#endif
584 free(shell);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000585}
586
Denys Vlasenko45963c82010-07-08 15:37:10 +0200587static void process_cron_update_file(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000588{
589 FILE *fi;
590 char buf[256];
591
Denis Vlasenko5415c852008-07-21 23:05:26 +0000592 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000593 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000594 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000595 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000596 /* use first word only */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200597 skip_non_whitespace(buf)[0] = '\0';
598 load_crontab(buf);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000599 }
600 fclose(fi);
601 }
602}
603
Denys Vlasenko45963c82010-07-08 15:37:10 +0200604static void rescan_crontab_dir(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000605{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000606 CronFile *file;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200607
608 /* Delete all files until we only have ones with running jobs (or none) */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000609 again:
Denys Vlasenko45963c82010-07-08 15:37:10 +0200610 for (file = G.cron_files; file; file = file->cf_next) {
611 if (!file->cf_deleted) {
612 delete_cronfile(file->cf_username);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000613 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000614 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000615 }
616
Denys Vlasenko45963c82010-07-08 15:37:10 +0200617 /* Remove cron update file */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000618 unlink(CRONUPDATE);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200619 /* Re-chdir, in case directory was renamed & deleted */
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200620 xchdir(G.crontab_dir_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200621
622 /* Scan directory and add associated users */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000623 {
624 DIR *dir = opendir(".");
625 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000626
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200627 /* xopendir exists, but "can't open '.'" is not informative */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000628 if (!dir)
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200629 bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000630 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000631 if (strchr(den->d_name, '.') != NULL) {
632 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000633 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200634 load_crontab(den->d_name);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000635 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000636 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000637 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000638}
639
Denys Vlasenko45963c82010-07-08 15:37:10 +0200640#if SETENV_LEAKS
641/* We set environment *before* vfork (because we want to use vfork),
642 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
643 * Using putenv(), and freeing memory after unsetenv() won't leak */
644static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000645{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200646 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000647
Denys Vlasenko45963c82010-07-08 15:37:10 +0200648 if (var_val) {
649 bb_unsetenv_and_free(var_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000650 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200651 *pvar_val = xasprintf("%s=%s", var, val);
652 putenv(*pvar_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000653}
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200654#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000655
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200656static void set_env_vars(struct passwd *pas, const char *shell)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000657{
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200658 /* POSIX requires crond to set up at least HOME, LOGNAME, PATH, SHELL.
659 * We assume crond inherited suitable PATH.
660 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200661#if SETENV_LEAKS
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200662 safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200663 safe_setenv(&G.env_var_user, "USER", pas->pw_name);
664 safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200665 safe_setenv(&G.env_var_shell, "SHELL", shell);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200666#else
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200667 xsetenv("LOGNAME", pas->pw_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200668 xsetenv("USER", pas->pw_name);
669 xsetenv("HOME", pas->pw_dir);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200670 xsetenv("SHELL", shell);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200671#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000672}
673
Denys Vlasenko45963c82010-07-08 15:37:10 +0200674static void change_user(struct passwd *pas)
675{
676 /* careful: we're after vfork! */
677 change_identity(pas); /* - initgroups, setgid, setuid */
678 if (chdir(pas->pw_dir) < 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200679 bb_error_msg("can't change directory to '%s'", pas->pw_dir);
680 xchdir(CRON_DIR);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200681 }
682}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000683
684// TODO: sendmail should be _run-time_ option, not compile-time!
Denys Vlasenko45963c82010-07-08 15:37:10 +0200685#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000686
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200687static pid_t
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200688fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
689{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000690 struct passwd *pas;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200691 const char *shell, *prog;
692 smallint sv_logmode;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000693 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000694
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000695 /* prepare things before vfork */
696 pas = getpwnam(user);
697 if (!pas) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200698 bb_error_msg("can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000699 goto err;
700 }
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200701
Denys Vlasenkoc9e78432017-07-22 01:47:19 +0200702 shell = line->cl_shell ? line->cl_shell : G.default_shell;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200703 prog = run_sendmail ? SENDMAIL : shell;
704
705 set_env_vars(pas, shell);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000706
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200707 sv_logmode = logmode;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000708 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000709 if (pid == 0) {
710 /* CHILD */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200711 /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200712 change_user(pas);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200713 log5("child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000714 if (mailFd >= 0) {
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200715 xmove_fd(mailFd, run_sendmail ? 0 : 1);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000716 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000717 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000718 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000719 bb_setpgrp();
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200720 if (!run_sendmail)
721 execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
722 else
723 execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200724 /*
725 * I want this error message on stderr too,
726 * even if other messages go only to syslog:
727 */
728 logmode |= LOGMODE_STDIO;
729 bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000730 }
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200731 logmode = sv_logmode;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000732
733 if (pid < 0) {
James Byrne69374872019-07-02 11:35:03 +0200734 bb_simple_perror_msg("vfork");
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000735 err:
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200736 pid = 0;
737 } /* else: PARENT, FORK SUCCESS */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000738
Eric Andersen35e643b2003-07-28 07:40:39 +0000739 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000740 * Close the mail file descriptor.. we can't just leave it open in
741 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000742 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000743 if (mailFd >= 0) {
744 close(mailFd);
745 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200746 return pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000747}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000748
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200749static pid_t start_one_job(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000750{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000751 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000752 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000753
Denys Vlasenko45963c82010-07-08 15:37:10 +0200754 line->cl_pid = 0;
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200755 line->cl_empty_mail_size = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000756
Denys Vlasenko45963c82010-07-08 15:37:10 +0200757 if (line->cl_mailto) {
758 /* Open mail file (owner is root so nobody can screw with it) */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200759 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid());
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000760 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000761
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000762 if (mailFd >= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200763 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
764 line->cl_cmd);
765 line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000766 } else {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200767 bb_error_msg("can't create mail file %s for user %s, "
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000768 "discarding output", mailFile, user);
769 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000770 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000771
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200772 line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 0);
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200773 if (mailFd >= 0) {
774 if (line->cl_pid <= 0) {
775 unlink(mailFile);
776 } else {
777 /* rename mail-file based on pid of process */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200778 char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid);
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200779 rename(mailFile, mailFile2); // TODO: xrename?
780 free(mailFile2);
781 }
782 }
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200783
784 return line->cl_pid;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200785}
786
787/*
788 * process_finished_job - called when job terminates and when mail terminates
789 */
790static void process_finished_job(const char *user, CronLine *line)
791{
792 pid_t pid;
793 int mailFd;
794 char mailFile[128];
795 struct stat sbuf;
796
797 pid = line->cl_pid;
798 line->cl_pid = 0;
799 if (pid <= 0) {
800 /* No job */
801 return;
802 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200803 if (line->cl_empty_mail_size <= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200804 /* End of job and no mail file, or end of sendmail job */
805 return;
806 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200807
808 /*
809 * End of primary job - check for mail file.
810 * If size has changed and the file is still valid, we send it.
811 */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200812 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200813 mailFd = open(mailFile, O_RDONLY);
814 unlink(mailFile);
815 if (mailFd < 0) {
816 return;
817 }
818
819 if (fstat(mailFd, &sbuf) < 0
820 || sbuf.st_uid != DAEMON_UID
821 || sbuf.st_nlink != 0
822 || sbuf.st_size == line->cl_empty_mail_size
823 || !S_ISREG(sbuf.st_mode)
824 ) {
825 close(mailFd);
826 return;
827 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200828 line->cl_empty_mail_size = 0;
829 /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200830 line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 1);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000831}
832
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200833#else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000834
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200835static pid_t start_one_job(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000836{
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200837 const char *shell;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000838 struct passwd *pas;
839 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000840
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000841 pas = getpwnam(user);
842 if (!pas) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200843 bb_error_msg("can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000844 goto err;
845 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000846
Denys Vlasenko45963c82010-07-08 15:37:10 +0200847 /* Prepare things before vfork */
Denys Vlasenkoc9e78432017-07-22 01:47:19 +0200848 shell = line->cl_shell ? line->cl_shell : G.default_shell;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200849 set_env_vars(pas, shell);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200850
851 /* Fork as the user in question and run program */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000852 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000853 if (pid == 0) {
854 /* CHILD */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200855 /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200856 change_user(pas);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200857 log5("child running %s", shell);
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000858 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000859 bb_setpgrp();
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200860 execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
861 bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000862 }
863 if (pid < 0) {
James Byrne69374872019-07-02 11:35:03 +0200864 bb_simple_perror_msg("vfork");
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000865 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000866 pid = 0;
867 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200868 line->cl_pid = pid;
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200869 return pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000870}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000871
Denys Vlasenko45963c82010-07-08 15:37:10 +0200872#define process_finished_job(user, line) ((line)->cl_pid = 0)
873
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200874#endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
875
Denys Vlasenko45963c82010-07-08 15:37:10 +0200876/*
877 * Determine which jobs need to be run. Under normal conditions, the
878 * period is about a minute (one scan). Worst case it will be one
879 * hour (60 scans).
880 */
881static void flag_starting_jobs(time_t t1, time_t t2)
882{
883 time_t t;
884
885 /* Find jobs > t1 and <= t2 */
886
887 for (t = t1 - t1 % 60; t <= t2; t += 60) {
888 struct tm *ptm;
889 CronFile *file;
890 CronLine *line;
891
892 if (t <= t1)
893 continue;
894
895 ptm = localtime(&t);
896 for (file = G.cron_files; file; file = file->cf_next) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200897 log5("file %s:", file->cf_username);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200898 if (file->cf_deleted)
899 continue;
900 for (line = file->cf_lines; line; line = line->cl_next) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200901 log5(" line %s", line->cl_cmd);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200902 if (line->cl_Mins[ptm->tm_min]
903 && line->cl_Hrs[ptm->tm_hour]
904 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
905 && line->cl_Mons[ptm->tm_mon]
906 ) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200907 log5(" job: %d %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200908 (int)line->cl_pid, line->cl_cmd);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200909 if (line->cl_pid > 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200910 log8("user %s: process already running: %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200911 file->cf_username, line->cl_cmd);
912 } else if (line->cl_pid == 0) {
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200913 line->cl_pid = START_ME_NORMAL;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200914 file->cf_wants_starting = 1;
915 }
916 }
917 }
918 }
919 }
920}
921
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200922#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
923static int touch_reboot_file(void)
924{
925 int fd = open(CRON_REBOOT, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0000);
926 if (fd >= 0) {
927 close(fd);
928 return 1;
929 }
930 /* File (presumably) exists - this is not the first run after reboot */
931 return 0;
932}
933#endif
934
935static void start_jobs(int wants_start)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200936{
937 CronFile *file;
938 CronLine *line;
939
Denys Vlasenko45963c82010-07-08 15:37:10 +0200940 for (file = G.cron_files; file; file = file->cf_next) {
941 if (!file->cf_wants_starting)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200942 continue;
943
Denys Vlasenko45963c82010-07-08 15:37:10 +0200944 file->cf_wants_starting = 0;
945 for (line = file->cf_lines; line; line = line->cl_next) {
946 pid_t pid;
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200947 if (line->cl_pid != wants_start)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200948 continue;
949
Denys Vlasenko75fbea32017-07-08 20:53:11 +0200950 pid = start_one_job(file->cf_username, line);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200951 log8("USER %s pid %3d cmd %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200952 file->cf_username, (int)pid, line->cl_cmd);
953 if (pid < 0) {
954 file->cf_wants_starting = 1;
955 }
956 if (pid > 0) {
957 file->cf_has_running = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200958 }
959 }
960 }
961}
962
Denys Vlasenko45963c82010-07-08 15:37:10 +0200963/*
964 * Check for job completion, return number of jobs still running after
965 * all done.
966 */
967static int check_completions(void)
968{
969 CronFile *file;
970 CronLine *line;
971 int num_still_running = 0;
972
973 for (file = G.cron_files; file; file = file->cf_next) {
974 if (!file->cf_has_running)
975 continue;
976
977 file->cf_has_running = 0;
978 for (line = file->cf_lines; line; line = line->cl_next) {
979 int r;
980
981 if (line->cl_pid <= 0)
982 continue;
983
984 r = waitpid(line->cl_pid, NULL, WNOHANG);
985 if (r < 0 || r == line->cl_pid) {
986 process_finished_job(file->cf_username, line);
987 if (line->cl_pid == 0) {
988 /* sendmail was not started for it */
989 continue;
990 }
991 /* else: sendmail was started, job is still running, fall thru */
992 }
993 /* else: r == 0: "process is still running" */
994 file->cf_has_running = 1;
995 }
996//FIXME: if !file->cf_has_running && file->deleted: delete it!
997//otherwise deleted entries will stay forever, right?
998 num_still_running += file->cf_has_running;
999 }
1000 return num_still_running;
1001}
1002
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001003static void reopen_logfile_to_stderr(void)
1004{
1005 if (G.log_filename) {
1006 int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
1007 if (logfd >= 0)
1008 xmove_fd(logfd, STDERR_FILENO);
1009 }
1010}
1011
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001012int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1013int crond_main(int argc UNUSED_PARAM, char **argv)
1014{
1015 time_t t2;
Denys Vlasenko01a1a962014-04-30 14:47:28 +02001016 unsigned rescan;
1017 unsigned sleep_time;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001018 unsigned opts;
1019
1020 INIT_G();
1021
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001022 opts = getopt32(argv, "^"
1023 "l:L:fbSc:" IF_FEATURE_CROND_D("d:")
1024 "\0"
1025 /* "-b after -f is ignored", and so on for every pair a-b */
1026 "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
Denys Vlasenko8c84f752011-09-07 05:56:09 +02001027 /* -l and -d have numeric param */
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001028 ":l+" IF_FEATURE_CROND_D(":d+")
1029 ,
Denys Vlasenko45963c82010-07-08 15:37:10 +02001030 &G.log_level, &G.log_filename, &G.crontab_dir_name
Denys Vlasenko22542ec2017-08-08 21:55:02 +02001031 IF_FEATURE_CROND_D(,&G.log_level)
1032 );
Denys Vlasenko45963c82010-07-08 15:37:10 +02001033 /* both -d N and -l N set the same variable: G.log_level */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001034
1035 if (!(opts & OPT_f)) {
1036 /* close stdin, stdout, stderr.
1037 * close unused descriptors - don't need them. */
1038 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
1039 }
1040
Denys Vlasenko45963c82010-07-08 15:37:10 +02001041 if (!(opts & OPT_d) && G.log_filename == NULL) {
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001042 /* logging to syslog */
1043 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
1044 logmode = LOGMODE_SYSLOG;
1045 }
1046
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001047 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +02001048
1049 reopen_logfile_to_stderr();
1050 xchdir(G.crontab_dir_name);
Denys Vlasenkoc9e78432017-07-22 01:47:19 +02001051 /* $SHELL, or current UID's shell, or DEFAULT_SHELL */
1052 /* Useful on Android where DEFAULT_SHELL /bin/sh may not exist */
1053 G.default_shell = xstrdup(get_shell_name());
1054
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001055 log8("crond (busybox "BB_VER") started, log level %d", G.log_level);
Denys Vlasenko45963c82010-07-08 15:37:10 +02001056 rescan_crontab_dir();
Denys Vlasenko50596532019-03-17 19:47:52 +01001057 write_pidfile_std_path_and_ext("crond");
Denys Vlasenko75fbea32017-07-08 20:53:11 +02001058#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
1059 if (touch_reboot_file())
1060 start_jobs(START_ME_REBOOT); /* start @reboot entries, if any */
1061#endif
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001062
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001063 /* Main loop */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001064 t2 = time(NULL);
1065 rescan = 60;
1066 sleep_time = 60;
1067 for (;;) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001068 struct stat sbuf;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001069 time_t t1;
1070 long dt;
1071
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001072 /* Synchronize to 1 minute, minimum 1 second */
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001073 t1 = t2;
1074 sleep(sleep_time - (time(NULL) % sleep_time));
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001075 t2 = time(NULL);
1076 dt = (long)t2 - (long)t1;
1077
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001078 reopen_logfile_to_stderr();
1079
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001080 /*
1081 * The file 'cron.update' is checked to determine new cron
1082 * jobs. The directory is rescanned once an hour to deal
1083 * with any screwups.
1084 *
1085 * Check for time jump. Disparities over an hour either way
1086 * result in resynchronization. A negative disparity
1087 * less than an hour causes us to effectively sleep until we
1088 * match the original time (i.e. no re-execution of jobs that
1089 * have just been run). A positive disparity less than
1090 * an hour causes intermediate jobs to be run, but only once
1091 * in the worst case.
1092 *
1093 * When running jobs, the inequality used is greater but not
1094 * equal to t1, and less then or equal to t2.
1095 */
Denys Vlasenko45963c82010-07-08 15:37:10 +02001096 if (stat(G.crontab_dir_name, &sbuf) != 0)
1097 sbuf.st_mtime = 0; /* force update (once) if dir was deleted */
1098 if (G.crontab_dir_mtime != sbuf.st_mtime) {
1099 G.crontab_dir_mtime = sbuf.st_mtime;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001100 rescan = 1;
1101 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001102 if (--rescan == 0) {
1103 rescan = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +02001104 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001105 }
Denys Vlasenko45963c82010-07-08 15:37:10 +02001106 process_cron_update_file();
Denys Vlasenkod5929d62014-04-30 14:49:52 +02001107 log5("wakeup dt=%ld", dt);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001108 if (dt < -60 * 60 || dt > 60 * 60) {
James Byrne253c4e72019-04-12 17:01:51 +00001109 bb_info_msg("time disparity of %ld minutes detected", dt / 60);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001110 /* and we do not run any jobs in this case */
1111 } else if (dt > 0) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001112 /* Usual case: time advances forward, as expected */
Denys Vlasenko45963c82010-07-08 15:37:10 +02001113 flag_starting_jobs(t1, t2);
Denys Vlasenko75fbea32017-07-08 20:53:11 +02001114 start_jobs(START_ME_NORMAL);
Denys Vlasenko01a1a962014-04-30 14:47:28 +02001115 sleep_time = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +02001116 if (check_completions() > 0) {
1117 /* some jobs are still running */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001118 sleep_time = 10;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001119 }
1120 }
1121 /* else: time jumped back, do not run any jobs */
1122 } /* for (;;) */
1123
1124 return 0; /* not reached */
1125}