blob: 5bf0536092f2286fe1dd9db372b2b0c741849dd6 [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/*
3 * crond -d[#] -c <crondir> -f -b
4 *
5 * run as root, but NOT setuid root
6 *
7 * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +00008 * (version 2.3.2)
Mike Frysingerf284c762006-04-16 20:38:26 +00009 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000010 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020011 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000012 */
13
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000014#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000015#include <syslog.h>
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000016
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000017/* glibc frees previous setenv'ed value when we do next setenv()
18 * of the same variable. uclibc does not do this! */
19#if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020020# define SETENV_LEAKS 0
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000021#else
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020022# define SETENV_LEAKS 1
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000023#endif
24
25
Denis Vlasenkoded5dfe2009-02-03 23:59:41 +000026#define TMPDIR CONFIG_FEATURE_CROND_DIR
27#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000028#ifndef SENDMAIL
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020029# define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000030#endif
31#ifndef SENDMAIL_ARGS
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +020032# define SENDMAIL_ARGS "-ti"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000033#endif
34#ifndef CRONUPDATE
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020035# define CRONUPDATE "cron.update"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000036#endif
37#ifndef MAXLINES
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020038# define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000039#endif
40
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000041
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000042typedef struct CronFile {
Denys Vlasenko45963c82010-07-08 15:37:10 +020043 struct CronFile *cf_next;
44 struct CronLine *cf_lines;
45 char *cf_username;
46 smallint cf_wants_starting; /* bool: one or more jobs ready */
47 smallint cf_has_running; /* bool: one or more jobs running */
48 smallint cf_deleted; /* marked for deletion (but still has running jobs) */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000049} CronFile;
50
51typedef struct CronLine {
Denys Vlasenko45963c82010-07-08 15:37:10 +020052 struct CronLine *cl_next;
53 char *cl_cmd; /* shell command */
54 pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000055#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +020056 int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */
Denys Vlasenko45963c82010-07-08 15:37:10 +020057 char *cl_mailto; /* whom to mail results, may be NULL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000058#endif
59 /* ordered by size, not in natural order. makes code smaller: */
Denys Vlasenko45963c82010-07-08 15:37:10 +020060 char cl_Dow[7]; /* 0-6, beginning sunday */
61 char cl_Mons[12]; /* 0-11 */
62 char cl_Hrs[24]; /* 0-23 */
63 char cl_Days[32]; /* 1-31 */
64 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000065} CronLine;
66
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000067
Denys Vlasenko45963c82010-07-08 15:37:10 +020068#define DAEMON_UID 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000069
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000070
71enum {
72 OPT_l = (1 << 0),
73 OPT_L = (1 << 1),
74 OPT_f = (1 << 2),
75 OPT_b = (1 << 3),
76 OPT_S = (1 << 4),
77 OPT_c = (1 << 5),
Denis Vlasenko35a064b2008-11-06 00:49:59 +000078 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000079};
Denis Vlasenko35a064b2008-11-06 00:49:59 +000080#if ENABLE_FEATURE_CROND_D
Denys Vlasenko45963c82010-07-08 15:37:10 +020081# define DebugOpt (option_mask32 & OPT_d)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000082#else
Denys Vlasenko45963c82010-07-08 15:37:10 +020083# define DebugOpt 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000084#endif
85
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000086
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000087struct globals {
Denys Vlasenko45963c82010-07-08 15:37:10 +020088 unsigned log_level; /* = 8; */
89 time_t crontab_dir_mtime;
90 const char *log_filename;
91 const char *crontab_dir_name; /* = CRONTABS; */
92 CronFile *cron_files;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000093#if SETENV_LEAKS
94 char *env_var_user;
95 char *env_var_home;
96#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +010097} FIX_ALIASING;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000098#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000099#define INIT_G() do { \
Denys Vlasenko45963c82010-07-08 15:37:10 +0200100 G.log_level = 8; \
101 G.crontab_dir_name = CRONTABS; \
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000102} while (0)
103
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000104
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100105/* 0 is the most verbose, default 8 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000106#define LVL5 "\x05"
107#define LVL7 "\x07"
108#define LVL8 "\x08"
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000109#define WARN9 "\x49"
110#define DIE9 "\xc9"
111/* level >= 20 is "error" */
112#define ERR20 "\x14"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000113
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100114static void crondlog(const char *ctl, ...) __attribute__ ((format (printf, 1, 2)));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000115static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000116{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000117 va_list va;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000118 int level = (ctl[0] & 0x1f);
Eric Andersen35e643b2003-07-28 07:40:39 +0000119
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000120 va_start(va, ctl);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200121 if (level >= (int)G.log_level) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000122 /* Debug mode: all to (non-redirected) stderr, */
123 /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200124 if (!DebugOpt && G.log_filename) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000125 /* Otherwise (log to file): we reopen log file at every write: */
Denys Vlasenkobec58882010-10-20 13:21:22 +0200126 int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000127 if (logfd >= 0)
128 xmove_fd(logfd, STDERR_FILENO);
Eric Andersen35e643b2003-07-28 07:40:39 +0000129 }
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100130 /* When we log to syslog, level > 8 is logged at LOG_ERR
131 * syslog level, level <= 8 is logged at LOG_INFO. */
132 if (level > 8) {
133 bb_verror_msg(ctl + 1, va, /* strerr: */ NULL);
134 } else {
135 char *msg = NULL;
136 vasprintf(&msg, ctl + 1, va);
137 bb_info_msg("%s: %s", applet_name, msg);
138 free(msg);
139 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000140 }
141 va_end(va);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000142 if (ctl[0] & 0x80)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000143 exit(20);
Eric Andersen35e643b2003-07-28 07:40:39 +0000144}
145
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000146static const char DowAry[] ALIGN1 =
147 "sun""mon""tue""wed""thu""fri""sat"
148 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
149;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000150
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000151static const char MonAry[] ALIGN1 =
152 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
153 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
154;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000155
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000156static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000157 const char *names, char *ptr)
158/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000159{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000160 char *base = ptr;
161 int n1 = -1;
162 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000163
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000164 // this can't happen due to config_read()
165 /*if (base == NULL)
166 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000167
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000168 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000169 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000170
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000171 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000172 if (*ptr == '*') {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200173 n1 = 0; /* everything will be filled */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000174 n2 = modvalue - 1;
175 skip = 1;
176 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000177 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200178 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000179 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200180 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000181 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200182 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000183 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200184 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000185 skip = 1;
186 } else if (names) {
187 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000188
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000189 for (i = 0; names[i]; i += 3) {
190 /* was using strncmp before... */
191 if (strncasecmp(ptr, &names[i], 3) == 0) {
192 ptr += 3;
193 if (n1 < 0) {
194 n1 = i / 3;
195 } else {
196 n2 = i / 3;
197 }
198 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000199 break;
200 }
201 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000202 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000203
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000204 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000205 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000206 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000207 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000208 if (*ptr == '-' && n2 < 0) {
209 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000210 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000211 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000212
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000213 /*
214 * collapse single-value ranges, handle skipmark, and fill
215 * in the character array appropriately.
216 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000217 if (n2 < 0) {
218 n2 = n1;
219 }
220 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200221 char *endp;
222 skip = strtol(ptr + 1, &endp, 10);
223 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000224 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000225
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000226 /*
227 * fill array, using a failsafe is the easiest way to prevent
228 * an endless loop
229 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000230 {
231 int s0 = 1;
232 int failsafe = 1024;
233
234 --n1;
235 do {
236 n1 = (n1 + 1) % modvalue;
237
238 if (--s0 == 0) {
239 ary[n1 % modvalue] = 1;
240 s0 = skip;
241 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000242 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000243 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000244 }
245 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000246 }
247 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000248 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000249 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000250 ++ptr;
251 n1 = -1;
252 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000253 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000254
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000255 if (*ptr) {
256 err:
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000257 crondlog(WARN9 "user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000258 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000259 }
260
Denys Vlasenko45963c82010-07-08 15:37:10 +0200261 if (DebugOpt && (G.log_level <= 5)) { /* like LVL5 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000262 /* can't use crondlog, it inserts '\n' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000263 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000264 for (i = 0; i < modvalue; ++i)
265 fprintf(stderr, "%d", (unsigned char)ary[i]);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200266 bb_putchar_stderr('\n');
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000267 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000268}
269
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000270static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000271{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000272 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000273 int weekUsed = 0;
274 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000275
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000276 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000277 if (line->cl_Dow[i] == 0) {
278 weekUsed = 1;
279 break;
280 }
281 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000282 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000283 if (line->cl_Days[i] == 0) {
284 daysUsed = 1;
285 break;
286 }
287 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000288 if (weekUsed != daysUsed) {
289 if (weekUsed)
290 memset(line->cl_Days, 0, sizeof(line->cl_Days));
291 else /* daysUsed */
292 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000293 }
294}
295
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200296/*
Denys Vlasenko45963c82010-07-08 15:37:10 +0200297 * delete_cronfile() - delete user database
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200298 *
299 * Note: multiple entries for same user may exist if we were unable to
300 * completely delete a database due to running processes.
301 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200302//FIXME: we will start a new job even if the old job is running
303//if crontab was reloaded: crond thinks that "new" job is different from "old"
304//even if they are in fact completely the same. Example
305//Crontab was:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200306// 0-59 * * * * job1
307// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200308//User edits crontab to:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200309// 0-59 * * * * job1_updated
310// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200311//Bug: crond can now start another long_running_job2 even if old one
312//is still running.
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200313//OTOH most other versions of cron do not wait for job termination anyway,
314//they end up with multiple copies of jobs if they don't terminate soon enough.
Denys Vlasenko45963c82010-07-08 15:37:10 +0200315static void delete_cronfile(const char *userName)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200316{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200317 CronFile **pfile = &G.cron_files;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200318 CronFile *file;
319
320 while ((file = *pfile) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200321 if (strcmp(userName, file->cf_username) == 0) {
322 CronLine **pline = &file->cf_lines;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200323 CronLine *line;
324
Denys Vlasenko45963c82010-07-08 15:37:10 +0200325 file->cf_has_running = 0;
326 file->cf_deleted = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200327
328 while ((line = *pline) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200329 if (line->cl_pid > 0) {
330 file->cf_has_running = 1;
331 pline = &line->cl_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200332 } else {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200333 *pline = line->cl_next;
334 free(line->cl_cmd);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200335 free(line);
336 }
337 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200338 if (file->cf_has_running == 0) {
339 *pfile = file->cf_next;
340 free(file->cf_username);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200341 free(file);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200342 continue;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200343 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200344 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200345 pfile = &file->cf_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200346 }
347}
348
Denys Vlasenko45963c82010-07-08 15:37:10 +0200349static void load_crontab(const char *fileName)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000350{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000351 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000352 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000353 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000354 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000355#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
356 char *mailTo = NULL;
357#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000358
Denys Vlasenko45963c82010-07-08 15:37:10 +0200359 delete_cronfile(fileName);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000360
Denys Vlasenko45963c82010-07-08 15:37:10 +0200361 if (!getpwnam(fileName)) {
362 crondlog(LVL7 "ignoring file '%s' (no such user)", fileName);
363 return;
364 }
365
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000366 parser = config_open(fileName);
367 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000368 return;
369
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000370 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000371
Denys Vlasenko45963c82010-07-08 15:37:10 +0200372 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000373 CronFile *file = xzalloc(sizeof(CronFile));
374 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000375 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000376
Denys Vlasenko45963c82010-07-08 15:37:10 +0200377 file->cf_username = xstrdup(fileName);
378 pline = &file->cf_lines;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000379
Denis Vlasenko084266e2008-07-26 23:08:31 +0000380 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000381 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000382
Denis Vlasenko084266e2008-07-26 23:08:31 +0000383 if (!--maxLines)
384 break;
385 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
386 if (!n)
387 break;
388
Denis Vlasenkodcb3fcb2008-07-19 22:57:00 +0000389 if (DebugOpt)
390 crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000391
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000392 /* check if line is setting MAILTO= */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000393 if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000394#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
395 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000396 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000397#endif /* otherwise just ignore such lines */
398 continue;
399 }
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000400 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000401 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000402 continue;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000403 *pline = line = xzalloc(sizeof(*line));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000404 /* parse date ranges */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200405 ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
406 ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
407 ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
408 ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
409 ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000410 /*
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000411 * fix days and dow - if one is not "*" and the other
412 * is "*", the other is set to 0, and vise-versa
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000413 */
414 FixDayDow(line);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000415#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
416 /* copy mailto (can be NULL) */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200417 line->cl_mailto = xstrdup(mailTo);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000418#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000419 /* copy command */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200420 line->cl_cmd = xstrdup(tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000421 if (DebugOpt) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000422 crondlog(LVL5 " command:%s", tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000423 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200424 pline = &line->cl_next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000425//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 +0000426 }
427 *pline = NULL;
428
Denys Vlasenko45963c82010-07-08 15:37:10 +0200429 file->cf_next = G.cron_files;
430 G.cron_files = file;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000431
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000432 if (maxLines == 0) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000433 crondlog(WARN9 "user %s: too many lines", fileName);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000434 }
435 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000436 config_close(parser);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000437}
438
Denys Vlasenko45963c82010-07-08 15:37:10 +0200439static void process_cron_update_file(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000440{
441 FILE *fi;
442 char buf[256];
443
Denis Vlasenko5415c852008-07-21 23:05:26 +0000444 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000445 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000446 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000447 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000448 /* use first word only */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200449 skip_non_whitespace(buf)[0] = '\0';
450 load_crontab(buf);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000451 }
452 fclose(fi);
453 }
454}
455
Denys Vlasenko45963c82010-07-08 15:37:10 +0200456static void rescan_crontab_dir(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000457{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000458 CronFile *file;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200459
460 /* Delete all files until we only have ones with running jobs (or none) */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000461 again:
Denys Vlasenko45963c82010-07-08 15:37:10 +0200462 for (file = G.cron_files; file; file = file->cf_next) {
463 if (!file->cf_deleted) {
464 delete_cronfile(file->cf_username);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000465 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000466 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000467 }
468
Denys Vlasenko45963c82010-07-08 15:37:10 +0200469 /* Remove cron update file */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000470 unlink(CRONUPDATE);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200471 /* Re-chdir, in case directory was renamed & deleted */
472 if (chdir(G.crontab_dir_name) < 0) {
473 crondlog(DIE9 "chdir(%s)", G.crontab_dir_name);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000474 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200475
476 /* Scan directory and add associated users */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000477 {
478 DIR *dir = opendir(".");
479 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000480
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000481 if (!dir)
Bernhard Reutner-Fischerca228fb2010-02-26 10:09:31 +0100482 crondlog(DIE9 "chdir(%s)", "."); /* exits */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000483 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000484 if (strchr(den->d_name, '.') != NULL) {
485 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000486 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200487 load_crontab(den->d_name);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000488 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000489 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000490 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000491}
492
Denys Vlasenko45963c82010-07-08 15:37:10 +0200493#if SETENV_LEAKS
494/* We set environment *before* vfork (because we want to use vfork),
495 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
496 * Using putenv(), and freeing memory after unsetenv() won't leak */
497static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000498{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200499 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000500
Denys Vlasenko45963c82010-07-08 15:37:10 +0200501 if (var_val) {
502 bb_unsetenv_and_free(var_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000503 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200504 *pvar_val = xasprintf("%s=%s", var, val);
505 putenv(*pvar_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000506}
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200507#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000508
Denys Vlasenko45963c82010-07-08 15:37:10 +0200509static void set_env_vars(struct passwd *pas)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000510{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200511#if SETENV_LEAKS
512 safe_setenv(&G.env_var_user, "USER", pas->pw_name);
513 safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
514 /* if we want to set user's shell instead: */
515 /*safe_setenv(G.env_var_shell, "SHELL", pas->pw_shell);*/
516#else
517 xsetenv("USER", pas->pw_name);
518 xsetenv("HOME", pas->pw_dir);
519#endif
520 /* currently, we use constant one: */
521 /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000522}
523
Denys Vlasenko45963c82010-07-08 15:37:10 +0200524static void change_user(struct passwd *pas)
525{
526 /* careful: we're after vfork! */
527 change_identity(pas); /* - initgroups, setgid, setuid */
528 if (chdir(pas->pw_dir) < 0) {
529 crondlog(WARN9 "chdir(%s)", pas->pw_dir);
530 if (chdir(TMPDIR) < 0) {
531 crondlog(DIE9 "chdir(%s)", TMPDIR); /* exits */
532 }
533 }
534}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000535
536// TODO: sendmail should be _run-time_ option, not compile-time!
Denys Vlasenko45963c82010-07-08 15:37:10 +0200537#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000538
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200539static pid_t
540fork_job(const char *user, int mailFd,
541 const char *prog,
542 const char *shell_cmd /* if NULL, we run sendmail */
543) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000544 struct passwd *pas;
545 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000546
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000547 /* prepare things before vfork */
548 pas = getpwnam(user);
549 if (!pas) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100550 crondlog(WARN9 "can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000551 goto err;
552 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200553 set_env_vars(pas);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000554
555 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000556 if (pid == 0) {
557 /* CHILD */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200558 /* initgroups, setgid, setuid, and chdir to home or TMPDIR */
559 change_user(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000560 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000561 crondlog(LVL5 "child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000562 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000563 if (mailFd >= 0) {
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200564 xmove_fd(mailFd, shell_cmd ? 1 : 0);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000565 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000566 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000567 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000568 bb_setpgrp();
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200569 execlp(prog, prog, (shell_cmd ? "-c" : SENDMAIL_ARGS), shell_cmd, (char *) NULL);
570 crondlog(ERR20 "can't execute '%s' for user %s", prog, user);
571 if (shell_cmd) {
572 fdprintf(1, "Exec failed: %s -c %s\n", prog, shell_cmd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000573 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000574 _exit(EXIT_SUCCESS);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000575 }
576
577 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000578 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000579 crondlog(ERR20 "can't vfork");
580 err:
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200581 pid = 0;
582 } /* else: PARENT, FORK SUCCESS */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000583
Eric Andersen35e643b2003-07-28 07:40:39 +0000584 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000585 * Close the mail file descriptor.. we can't just leave it open in
586 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000587 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000588 if (mailFd >= 0) {
589 close(mailFd);
590 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200591 return pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000592}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000593
Denys Vlasenko45963c82010-07-08 15:37:10 +0200594static void start_one_job(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000595{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000596 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000597 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000598
Denys Vlasenko45963c82010-07-08 15:37:10 +0200599 line->cl_pid = 0;
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200600 line->cl_empty_mail_size = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000601
Denys Vlasenko45963c82010-07-08 15:37:10 +0200602 if (line->cl_mailto) {
603 /* Open mail file (owner is root so nobody can screw with it) */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000604 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid());
605 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000606
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000607 if (mailFd >= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200608 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
609 line->cl_cmd);
610 line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000611 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100612 crondlog(ERR20 "can't create mail file %s for user %s, "
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000613 "discarding output", mailFile, user);
614 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000615 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000616
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200617 line->cl_pid = fork_job(user, mailFd, DEFAULT_SHELL, line->cl_cmd);
618 if (mailFd >= 0) {
619 if (line->cl_pid <= 0) {
620 unlink(mailFile);
621 } else {
622 /* rename mail-file based on pid of process */
623 char *mailFile2 = xasprintf("%s/cron.%s.%d", TMPDIR, user, (int)line->cl_pid);
624 rename(mailFile, mailFile2); // TODO: xrename?
625 free(mailFile2);
626 }
627 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200628}
629
630/*
631 * process_finished_job - called when job terminates and when mail terminates
632 */
633static void process_finished_job(const char *user, CronLine *line)
634{
635 pid_t pid;
636 int mailFd;
637 char mailFile[128];
638 struct stat sbuf;
639
640 pid = line->cl_pid;
641 line->cl_pid = 0;
642 if (pid <= 0) {
643 /* No job */
644 return;
645 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200646 if (line->cl_empty_mail_size <= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200647 /* End of job and no mail file, or end of sendmail job */
648 return;
649 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200650
651 /*
652 * End of primary job - check for mail file.
653 * If size has changed and the file is still valid, we send it.
654 */
655 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, (int)pid);
656 mailFd = open(mailFile, O_RDONLY);
657 unlink(mailFile);
658 if (mailFd < 0) {
659 return;
660 }
661
662 if (fstat(mailFd, &sbuf) < 0
663 || sbuf.st_uid != DAEMON_UID
664 || sbuf.st_nlink != 0
665 || sbuf.st_size == line->cl_empty_mail_size
666 || !S_ISREG(sbuf.st_mode)
667 ) {
668 close(mailFd);
669 return;
670 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200671 line->cl_empty_mail_size = 0;
672 /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */
673 line->cl_pid = fork_job(user, mailFd, SENDMAIL, NULL);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000674}
675
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200676#else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000677
Denys Vlasenko45963c82010-07-08 15:37:10 +0200678static void start_one_job(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000679{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000680 struct passwd *pas;
681 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000682
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000683 pas = getpwnam(user);
684 if (!pas) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100685 crondlog(WARN9 "can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000686 goto err;
687 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000688
Denys Vlasenko45963c82010-07-08 15:37:10 +0200689 /* Prepare things before vfork */
690 set_env_vars(pas);
691
692 /* Fork as the user in question and run program */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000693 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000694 if (pid == 0) {
695 /* CHILD */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200696 /* initgroups, setgid, setuid, and chdir to home or TMPDIR */
697 change_user(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000698 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000699 crondlog(LVL5 "child running %s", DEFAULT_SHELL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000700 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000701 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000702 bb_setpgrp();
Denys Vlasenko45963c82010-07-08 15:37:10 +0200703 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_cmd, (char *) NULL);
704 crondlog(ERR20 "can't execute '%s' for user %s", DEFAULT_SHELL, user);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000705 _exit(EXIT_SUCCESS);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000706 }
707 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000708 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000709 crondlog(ERR20 "can't vfork");
710 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000711 pid = 0;
712 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200713 line->cl_pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000714}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000715
Denys Vlasenko45963c82010-07-08 15:37:10 +0200716#define process_finished_job(user, line) ((line)->cl_pid = 0)
717
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200718#endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
719
Denys Vlasenko45963c82010-07-08 15:37:10 +0200720/*
721 * Determine which jobs need to be run. Under normal conditions, the
722 * period is about a minute (one scan). Worst case it will be one
723 * hour (60 scans).
724 */
725static void flag_starting_jobs(time_t t1, time_t t2)
726{
727 time_t t;
728
729 /* Find jobs > t1 and <= t2 */
730
731 for (t = t1 - t1 % 60; t <= t2; t += 60) {
732 struct tm *ptm;
733 CronFile *file;
734 CronLine *line;
735
736 if (t <= t1)
737 continue;
738
739 ptm = localtime(&t);
740 for (file = G.cron_files; file; file = file->cf_next) {
741 if (DebugOpt)
742 crondlog(LVL5 "file %s:", file->cf_username);
743 if (file->cf_deleted)
744 continue;
745 for (line = file->cf_lines; line; line = line->cl_next) {
746 if (DebugOpt)
747 crondlog(LVL5 " line %s", line->cl_cmd);
748 if (line->cl_Mins[ptm->tm_min]
749 && line->cl_Hrs[ptm->tm_hour]
750 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
751 && line->cl_Mons[ptm->tm_mon]
752 ) {
753 if (DebugOpt) {
754 crondlog(LVL5 " job: %d %s",
755 (int)line->cl_pid, line->cl_cmd);
756 }
757 if (line->cl_pid > 0) {
758 crondlog(LVL8 "user %s: process already running: %s",
759 file->cf_username, line->cl_cmd);
760 } else if (line->cl_pid == 0) {
761 line->cl_pid = -1;
762 file->cf_wants_starting = 1;
763 }
764 }
765 }
766 }
767 }
768}
769
770static void start_jobs(void)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200771{
772 CronFile *file;
773 CronLine *line;
774
Denys Vlasenko45963c82010-07-08 15:37:10 +0200775 for (file = G.cron_files; file; file = file->cf_next) {
776 if (!file->cf_wants_starting)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200777 continue;
778
Denys Vlasenko45963c82010-07-08 15:37:10 +0200779 file->cf_wants_starting = 0;
780 for (line = file->cf_lines; line; line = line->cl_next) {
781 pid_t pid;
782 if (line->cl_pid >= 0)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200783 continue;
784
Denys Vlasenko45963c82010-07-08 15:37:10 +0200785 start_one_job(file->cf_username, line);
786 pid = line->cl_pid;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200787 crondlog(LVL8 "USER %s pid %3d cmd %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200788 file->cf_username, (int)pid, line->cl_cmd);
789 if (pid < 0) {
790 file->cf_wants_starting = 1;
791 }
792 if (pid > 0) {
793 file->cf_has_running = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200794 }
795 }
796 }
797}
798
Denys Vlasenko45963c82010-07-08 15:37:10 +0200799/*
800 * Check for job completion, return number of jobs still running after
801 * all done.
802 */
803static int check_completions(void)
804{
805 CronFile *file;
806 CronLine *line;
807 int num_still_running = 0;
808
809 for (file = G.cron_files; file; file = file->cf_next) {
810 if (!file->cf_has_running)
811 continue;
812
813 file->cf_has_running = 0;
814 for (line = file->cf_lines; line; line = line->cl_next) {
815 int r;
816
817 if (line->cl_pid <= 0)
818 continue;
819
820 r = waitpid(line->cl_pid, NULL, WNOHANG);
821 if (r < 0 || r == line->cl_pid) {
822 process_finished_job(file->cf_username, line);
823 if (line->cl_pid == 0) {
824 /* sendmail was not started for it */
825 continue;
826 }
827 /* else: sendmail was started, job is still running, fall thru */
828 }
829 /* else: r == 0: "process is still running" */
830 file->cf_has_running = 1;
831 }
832//FIXME: if !file->cf_has_running && file->deleted: delete it!
833//otherwise deleted entries will stay forever, right?
834 num_still_running += file->cf_has_running;
835 }
836 return num_still_running;
837}
838
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200839int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
840int crond_main(int argc UNUSED_PARAM, char **argv)
841{
842 time_t t2;
843 int rescan;
844 int sleep_time;
845 unsigned opts;
846
847 INIT_G();
848
849 /* "-b after -f is ignored", and so on for every pair a-b */
850 opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
851 ":l+:d+"; /* -l and -d have numeric param */
852 opts = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"),
Denys Vlasenko45963c82010-07-08 15:37:10 +0200853 &G.log_level, &G.log_filename, &G.crontab_dir_name
854 IF_FEATURE_CROND_D(,&G.log_level));
855 /* both -d N and -l N set the same variable: G.log_level */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200856
857 if (!(opts & OPT_f)) {
858 /* close stdin, stdout, stderr.
859 * close unused descriptors - don't need them. */
860 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
861 }
862
Denys Vlasenko45963c82010-07-08 15:37:10 +0200863 if (!(opts & OPT_d) && G.log_filename == NULL) {
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200864 /* logging to syslog */
865 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
866 logmode = LOGMODE_SYSLOG;
867 }
868
Denys Vlasenko45963c82010-07-08 15:37:10 +0200869 xchdir(G.crontab_dir_name);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200870 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
871 xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200872 crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", G.log_level);
873 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200874 write_pidfile("/var/run/crond.pid");
875
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200876 /* Main loop */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200877 t2 = time(NULL);
878 rescan = 60;
879 sleep_time = 60;
880 for (;;) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200881 struct stat sbuf;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200882 time_t t1;
883 long dt;
884
885 t1 = t2;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200886
887 /* Synchronize to 1 minute, minimum 1 second */
888 sleep(sleep_time - (time(NULL) % sleep_time) + 1);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200889
890 t2 = time(NULL);
891 dt = (long)t2 - (long)t1;
892
893 /*
894 * The file 'cron.update' is checked to determine new cron
895 * jobs. The directory is rescanned once an hour to deal
896 * with any screwups.
897 *
898 * Check for time jump. Disparities over an hour either way
899 * result in resynchronization. A negative disparity
900 * less than an hour causes us to effectively sleep until we
901 * match the original time (i.e. no re-execution of jobs that
902 * have just been run). A positive disparity less than
903 * an hour causes intermediate jobs to be run, but only once
904 * in the worst case.
905 *
906 * When running jobs, the inequality used is greater but not
907 * equal to t1, and less then or equal to t2.
908 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200909 if (stat(G.crontab_dir_name, &sbuf) != 0)
910 sbuf.st_mtime = 0; /* force update (once) if dir was deleted */
911 if (G.crontab_dir_mtime != sbuf.st_mtime) {
912 G.crontab_dir_mtime = sbuf.st_mtime;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200913 rescan = 1;
914 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200915 if (--rescan == 0) {
916 rescan = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200917 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200918 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200919 process_cron_update_file();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200920 if (DebugOpt)
921 crondlog(LVL5 "wakeup dt=%ld", dt);
922 if (dt < -60 * 60 || dt > 60 * 60) {
923 crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60);
924 /* and we do not run any jobs in this case */
925 } else if (dt > 0) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200926 /* Usual case: time advances forward, as expected */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200927 flag_starting_jobs(t1, t2);
928 start_jobs();
929 if (check_completions() > 0) {
930 /* some jobs are still running */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200931 sleep_time = 10;
932 } else {
933 sleep_time = 60;
934 }
935 }
936 /* else: time jumped back, do not run any jobs */
937 } /* for (;;) */
938
939 return 0; /* not reached */
940}