blob: a0b73c77497018d1abed6c469bac6da8623150ab [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
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage:#define crond_trivial_usage
15//usage: "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR"
16//usage:#define crond_full_usage "\n\n"
17//usage: " -f Foreground"
18//usage: "\n -b Background (default)"
19//usage: "\n -S Log to syslog (default)"
20//usage: "\n -l Set log level. 0 is the most verbose, default 8"
21//usage: IF_FEATURE_CROND_D(
22//usage: "\n -d Set log level, log to stderr"
23//usage: )
24//usage: "\n -L Log to file"
25//usage: "\n -c Working dir"
26
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000027#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000028#include <syslog.h>
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000029
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000030/* glibc frees previous setenv'ed value when we do next setenv()
31 * of the same variable. uclibc does not do this! */
32#if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020033# define SETENV_LEAKS 0
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000034#else
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020035# define SETENV_LEAKS 1
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000036#endif
37
38
Denis Vlasenkoded5dfe2009-02-03 23:59:41 +000039#define TMPDIR CONFIG_FEATURE_CROND_DIR
40#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000041#ifndef SENDMAIL
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020042# define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000043#endif
44#ifndef SENDMAIL_ARGS
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +020045# define SENDMAIL_ARGS "-ti"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000046#endif
47#ifndef CRONUPDATE
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020048# define CRONUPDATE "cron.update"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000049#endif
50#ifndef MAXLINES
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020051# define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000052#endif
53
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000054
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000055typedef struct CronFile {
Denys Vlasenko45963c82010-07-08 15:37:10 +020056 struct CronFile *cf_next;
57 struct CronLine *cf_lines;
58 char *cf_username;
59 smallint cf_wants_starting; /* bool: one or more jobs ready */
60 smallint cf_has_running; /* bool: one or more jobs running */
61 smallint cf_deleted; /* marked for deletion (but still has running jobs) */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000062} CronFile;
63
64typedef struct CronLine {
Denys Vlasenko45963c82010-07-08 15:37:10 +020065 struct CronLine *cl_next;
66 char *cl_cmd; /* shell command */
67 pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000068#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +020069 int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */
Denys Vlasenko45963c82010-07-08 15:37:10 +020070 char *cl_mailto; /* whom to mail results, may be NULL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000071#endif
72 /* ordered by size, not in natural order. makes code smaller: */
Denys Vlasenko45963c82010-07-08 15:37:10 +020073 char cl_Dow[7]; /* 0-6, beginning sunday */
74 char cl_Mons[12]; /* 0-11 */
75 char cl_Hrs[24]; /* 0-23 */
76 char cl_Days[32]; /* 1-31 */
77 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000078} CronLine;
79
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000080
Denys Vlasenko45963c82010-07-08 15:37:10 +020081#define DAEMON_UID 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000082
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000083
84enum {
85 OPT_l = (1 << 0),
86 OPT_L = (1 << 1),
87 OPT_f = (1 << 2),
88 OPT_b = (1 << 3),
89 OPT_S = (1 << 4),
90 OPT_c = (1 << 5),
Denis Vlasenko35a064b2008-11-06 00:49:59 +000091 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000092};
Denis Vlasenko35a064b2008-11-06 00:49:59 +000093#if ENABLE_FEATURE_CROND_D
Denys Vlasenko45963c82010-07-08 15:37:10 +020094# define DebugOpt (option_mask32 & OPT_d)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000095#else
Denys Vlasenko45963c82010-07-08 15:37:10 +020096# define DebugOpt 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000097#endif
98
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000099
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000100struct globals {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200101 unsigned log_level; /* = 8; */
102 time_t crontab_dir_mtime;
103 const char *log_filename;
104 const char *crontab_dir_name; /* = CRONTABS; */
105 CronFile *cron_files;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000106#if SETENV_LEAKS
107 char *env_var_user;
108 char *env_var_home;
109#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100110} FIX_ALIASING;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000111#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000112#define INIT_G() do { \
Denys Vlasenko45963c82010-07-08 15:37:10 +0200113 G.log_level = 8; \
114 G.crontab_dir_name = CRONTABS; \
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000115} while (0)
116
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000117
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100118/* 0 is the most verbose, default 8 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000119#define LVL5 "\x05"
120#define LVL7 "\x07"
121#define LVL8 "\x08"
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000122#define WARN9 "\x49"
123#define DIE9 "\xc9"
124/* level >= 20 is "error" */
125#define ERR20 "\x14"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000126
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100127static void crondlog(const char *ctl, ...) __attribute__ ((format (printf, 1, 2)));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000128static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000129{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000130 va_list va;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000131 int level = (ctl[0] & 0x1f);
Eric Andersen35e643b2003-07-28 07:40:39 +0000132
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000133 va_start(va, ctl);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200134 if (level >= (int)G.log_level) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000135 /* Debug mode: all to (non-redirected) stderr, */
136 /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200137 if (!DebugOpt && G.log_filename) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000138 /* Otherwise (log to file): we reopen log file at every write: */
Denys Vlasenkobec58882010-10-20 13:21:22 +0200139 int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000140 if (logfd >= 0)
141 xmove_fd(logfd, STDERR_FILENO);
Eric Andersen35e643b2003-07-28 07:40:39 +0000142 }
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100143 /* When we log to syslog, level > 8 is logged at LOG_ERR
144 * syslog level, level <= 8 is logged at LOG_INFO. */
145 if (level > 8) {
146 bb_verror_msg(ctl + 1, va, /* strerr: */ NULL);
147 } else {
148 char *msg = NULL;
149 vasprintf(&msg, ctl + 1, va);
150 bb_info_msg("%s: %s", applet_name, msg);
151 free(msg);
152 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000153 }
154 va_end(va);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000155 if (ctl[0] & 0x80)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000156 exit(20);
Eric Andersen35e643b2003-07-28 07:40:39 +0000157}
158
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000159static const char DowAry[] ALIGN1 =
160 "sun""mon""tue""wed""thu""fri""sat"
161 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
162;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000163
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000164static const char MonAry[] ALIGN1 =
165 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
166 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
167;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000168
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000169static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000170 const char *names, char *ptr)
171/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000172{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000173 char *base = ptr;
174 int n1 = -1;
175 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000176
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000177 // this can't happen due to config_read()
178 /*if (base == NULL)
179 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000180
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000181 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000182 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000183
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000184 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000185 if (*ptr == '*') {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200186 n1 = 0; /* everything will be filled */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000187 n2 = modvalue - 1;
188 skip = 1;
189 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000190 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200191 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000192 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200193 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000194 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200195 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000196 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200197 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000198 skip = 1;
199 } else if (names) {
200 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000201
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000202 for (i = 0; names[i]; i += 3) {
203 /* was using strncmp before... */
204 if (strncasecmp(ptr, &names[i], 3) == 0) {
205 ptr += 3;
206 if (n1 < 0) {
207 n1 = i / 3;
208 } else {
209 n2 = i / 3;
210 }
211 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000212 break;
213 }
214 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000215 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000216
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000217 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000218 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000219 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000220 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000221 if (*ptr == '-' && n2 < 0) {
222 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000223 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000224 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000225
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000226 /*
227 * collapse single-value ranges, handle skipmark, and fill
228 * in the character array appropriately.
229 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000230 if (n2 < 0) {
231 n2 = n1;
232 }
233 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200234 char *endp;
235 skip = strtol(ptr + 1, &endp, 10);
236 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000237 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000238
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000239 /*
240 * fill array, using a failsafe is the easiest way to prevent
241 * an endless loop
242 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000243 {
244 int s0 = 1;
245 int failsafe = 1024;
246
247 --n1;
248 do {
249 n1 = (n1 + 1) % modvalue;
250
251 if (--s0 == 0) {
252 ary[n1 % modvalue] = 1;
253 s0 = skip;
254 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000255 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000256 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000257 }
258 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000259 }
260 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000261 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000262 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000263 ++ptr;
264 n1 = -1;
265 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000266 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000267
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000268 if (*ptr) {
269 err:
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000270 crondlog(WARN9 "user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000271 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000272 }
273
Denys Vlasenko45963c82010-07-08 15:37:10 +0200274 if (DebugOpt && (G.log_level <= 5)) { /* like LVL5 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000275 /* can't use crondlog, it inserts '\n' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000276 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000277 for (i = 0; i < modvalue; ++i)
278 fprintf(stderr, "%d", (unsigned char)ary[i]);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200279 bb_putchar_stderr('\n');
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000280 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000281}
282
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000283static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000284{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000285 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000286 int weekUsed = 0;
287 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000288
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000289 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000290 if (line->cl_Dow[i] == 0) {
291 weekUsed = 1;
292 break;
293 }
294 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000295 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000296 if (line->cl_Days[i] == 0) {
297 daysUsed = 1;
298 break;
299 }
300 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000301 if (weekUsed != daysUsed) {
302 if (weekUsed)
303 memset(line->cl_Days, 0, sizeof(line->cl_Days));
304 else /* daysUsed */
305 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000306 }
307}
308
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200309/*
Denys Vlasenko45963c82010-07-08 15:37:10 +0200310 * delete_cronfile() - delete user database
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200311 *
312 * Note: multiple entries for same user may exist if we were unable to
313 * completely delete a database due to running processes.
314 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200315//FIXME: we will start a new job even if the old job is running
316//if crontab was reloaded: crond thinks that "new" job is different from "old"
317//even if they are in fact completely the same. Example
318//Crontab was:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200319// 0-59 * * * * job1
320// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200321//User edits crontab to:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200322// 0-59 * * * * job1_updated
323// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200324//Bug: crond can now start another long_running_job2 even if old one
325//is still running.
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200326//OTOH most other versions of cron do not wait for job termination anyway,
327//they end up with multiple copies of jobs if they don't terminate soon enough.
Denys Vlasenko45963c82010-07-08 15:37:10 +0200328static void delete_cronfile(const char *userName)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200329{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200330 CronFile **pfile = &G.cron_files;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200331 CronFile *file;
332
333 while ((file = *pfile) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200334 if (strcmp(userName, file->cf_username) == 0) {
335 CronLine **pline = &file->cf_lines;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200336 CronLine *line;
337
Denys Vlasenko45963c82010-07-08 15:37:10 +0200338 file->cf_has_running = 0;
339 file->cf_deleted = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200340
341 while ((line = *pline) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200342 if (line->cl_pid > 0) {
343 file->cf_has_running = 1;
344 pline = &line->cl_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200345 } else {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200346 *pline = line->cl_next;
347 free(line->cl_cmd);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200348 free(line);
349 }
350 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200351 if (file->cf_has_running == 0) {
352 *pfile = file->cf_next;
353 free(file->cf_username);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200354 free(file);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200355 continue;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200356 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200357 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200358 pfile = &file->cf_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200359 }
360}
361
Denys Vlasenko45963c82010-07-08 15:37:10 +0200362static void load_crontab(const char *fileName)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000363{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000364 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000365 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000366 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000367 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000368#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
369 char *mailTo = NULL;
370#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000371
Denys Vlasenko45963c82010-07-08 15:37:10 +0200372 delete_cronfile(fileName);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000373
Denys Vlasenko45963c82010-07-08 15:37:10 +0200374 if (!getpwnam(fileName)) {
375 crondlog(LVL7 "ignoring file '%s' (no such user)", fileName);
376 return;
377 }
378
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000379 parser = config_open(fileName);
380 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000381 return;
382
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000383 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000384
Denys Vlasenko45963c82010-07-08 15:37:10 +0200385 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000386 CronFile *file = xzalloc(sizeof(CronFile));
387 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000388 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000389
Denys Vlasenko45963c82010-07-08 15:37:10 +0200390 file->cf_username = xstrdup(fileName);
391 pline = &file->cf_lines;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000392
Denis Vlasenko084266e2008-07-26 23:08:31 +0000393 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000394 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000395
Denis Vlasenko084266e2008-07-26 23:08:31 +0000396 if (!--maxLines)
397 break;
398 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
399 if (!n)
400 break;
401
Denis Vlasenkodcb3fcb2008-07-19 22:57:00 +0000402 if (DebugOpt)
403 crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000404
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000405 /* check if line is setting MAILTO= */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000406 if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000407#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
408 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000409 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000410#endif /* otherwise just ignore such lines */
411 continue;
412 }
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000413 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000414 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000415 continue;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000416 *pline = line = xzalloc(sizeof(*line));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000417 /* parse date ranges */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200418 ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
419 ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
420 ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
421 ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
422 ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000423 /*
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000424 * fix days and dow - if one is not "*" and the other
425 * is "*", the other is set to 0, and vise-versa
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000426 */
427 FixDayDow(line);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000428#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
429 /* copy mailto (can be NULL) */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200430 line->cl_mailto = xstrdup(mailTo);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000431#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000432 /* copy command */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200433 line->cl_cmd = xstrdup(tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000434 if (DebugOpt) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000435 crondlog(LVL5 " command:%s", tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000436 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200437 pline = &line->cl_next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000438//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 +0000439 }
440 *pline = NULL;
441
Denys Vlasenko45963c82010-07-08 15:37:10 +0200442 file->cf_next = G.cron_files;
443 G.cron_files = file;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000444
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000445 if (maxLines == 0) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000446 crondlog(WARN9 "user %s: too many lines", fileName);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000447 }
448 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000449 config_close(parser);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000450}
451
Denys Vlasenko45963c82010-07-08 15:37:10 +0200452static void process_cron_update_file(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000453{
454 FILE *fi;
455 char buf[256];
456
Denis Vlasenko5415c852008-07-21 23:05:26 +0000457 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000458 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000459 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000460 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000461 /* use first word only */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200462 skip_non_whitespace(buf)[0] = '\0';
463 load_crontab(buf);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000464 }
465 fclose(fi);
466 }
467}
468
Denys Vlasenko45963c82010-07-08 15:37:10 +0200469static void rescan_crontab_dir(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000470{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000471 CronFile *file;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200472
473 /* Delete all files until we only have ones with running jobs (or none) */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000474 again:
Denys Vlasenko45963c82010-07-08 15:37:10 +0200475 for (file = G.cron_files; file; file = file->cf_next) {
476 if (!file->cf_deleted) {
477 delete_cronfile(file->cf_username);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000478 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000479 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000480 }
481
Denys Vlasenko45963c82010-07-08 15:37:10 +0200482 /* Remove cron update file */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000483 unlink(CRONUPDATE);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200484 /* Re-chdir, in case directory was renamed & deleted */
485 if (chdir(G.crontab_dir_name) < 0) {
486 crondlog(DIE9 "chdir(%s)", G.crontab_dir_name);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000487 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200488
489 /* Scan directory and add associated users */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000490 {
491 DIR *dir = opendir(".");
492 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000493
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000494 if (!dir)
Bernhard Reutner-Fischerca228fb2010-02-26 10:09:31 +0100495 crondlog(DIE9 "chdir(%s)", "."); /* exits */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000496 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000497 if (strchr(den->d_name, '.') != NULL) {
498 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000499 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200500 load_crontab(den->d_name);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000501 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000502 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000503 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000504}
505
Denys Vlasenko45963c82010-07-08 15:37:10 +0200506#if SETENV_LEAKS
507/* We set environment *before* vfork (because we want to use vfork),
508 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
509 * Using putenv(), and freeing memory after unsetenv() won't leak */
510static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000511{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200512 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000513
Denys Vlasenko45963c82010-07-08 15:37:10 +0200514 if (var_val) {
515 bb_unsetenv_and_free(var_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000516 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200517 *pvar_val = xasprintf("%s=%s", var, val);
518 putenv(*pvar_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000519}
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200520#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000521
Denys Vlasenko45963c82010-07-08 15:37:10 +0200522static void set_env_vars(struct passwd *pas)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000523{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200524#if SETENV_LEAKS
525 safe_setenv(&G.env_var_user, "USER", pas->pw_name);
526 safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
527 /* if we want to set user's shell instead: */
528 /*safe_setenv(G.env_var_shell, "SHELL", pas->pw_shell);*/
529#else
530 xsetenv("USER", pas->pw_name);
531 xsetenv("HOME", pas->pw_dir);
532#endif
533 /* currently, we use constant one: */
534 /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000535}
536
Denys Vlasenko45963c82010-07-08 15:37:10 +0200537static void change_user(struct passwd *pas)
538{
539 /* careful: we're after vfork! */
540 change_identity(pas); /* - initgroups, setgid, setuid */
541 if (chdir(pas->pw_dir) < 0) {
542 crondlog(WARN9 "chdir(%s)", pas->pw_dir);
543 if (chdir(TMPDIR) < 0) {
544 crondlog(DIE9 "chdir(%s)", TMPDIR); /* exits */
545 }
546 }
547}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000548
549// TODO: sendmail should be _run-time_ option, not compile-time!
Denys Vlasenko45963c82010-07-08 15:37:10 +0200550#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000551
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200552static pid_t
553fork_job(const char *user, int mailFd,
554 const char *prog,
555 const char *shell_cmd /* if NULL, we run sendmail */
556) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000557 struct passwd *pas;
558 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000559
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000560 /* prepare things before vfork */
561 pas = getpwnam(user);
562 if (!pas) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100563 crondlog(WARN9 "can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000564 goto err;
565 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200566 set_env_vars(pas);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000567
568 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000569 if (pid == 0) {
570 /* CHILD */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200571 /* initgroups, setgid, setuid, and chdir to home or TMPDIR */
572 change_user(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000573 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000574 crondlog(LVL5 "child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000575 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000576 if (mailFd >= 0) {
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200577 xmove_fd(mailFd, shell_cmd ? 1 : 0);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000578 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000579 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000580 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000581 bb_setpgrp();
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200582 execlp(prog, prog, (shell_cmd ? "-c" : SENDMAIL_ARGS), shell_cmd, (char *) NULL);
583 crondlog(ERR20 "can't execute '%s' for user %s", prog, user);
584 if (shell_cmd) {
585 fdprintf(1, "Exec failed: %s -c %s\n", prog, shell_cmd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000586 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000587 _exit(EXIT_SUCCESS);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000588 }
589
590 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000591 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000592 crondlog(ERR20 "can't vfork");
593 err:
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200594 pid = 0;
595 } /* else: PARENT, FORK SUCCESS */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000596
Eric Andersen35e643b2003-07-28 07:40:39 +0000597 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000598 * Close the mail file descriptor.. we can't just leave it open in
599 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000600 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000601 if (mailFd >= 0) {
602 close(mailFd);
603 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200604 return pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000605}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000606
Denys Vlasenko45963c82010-07-08 15:37:10 +0200607static void start_one_job(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000608{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000609 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000610 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000611
Denys Vlasenko45963c82010-07-08 15:37:10 +0200612 line->cl_pid = 0;
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200613 line->cl_empty_mail_size = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000614
Denys Vlasenko45963c82010-07-08 15:37:10 +0200615 if (line->cl_mailto) {
616 /* Open mail file (owner is root so nobody can screw with it) */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000617 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid());
618 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000619
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000620 if (mailFd >= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200621 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
622 line->cl_cmd);
623 line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000624 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100625 crondlog(ERR20 "can't create mail file %s for user %s, "
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000626 "discarding output", mailFile, user);
627 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000628 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000629
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200630 line->cl_pid = fork_job(user, mailFd, DEFAULT_SHELL, line->cl_cmd);
631 if (mailFd >= 0) {
632 if (line->cl_pid <= 0) {
633 unlink(mailFile);
634 } else {
635 /* rename mail-file based on pid of process */
636 char *mailFile2 = xasprintf("%s/cron.%s.%d", TMPDIR, user, (int)line->cl_pid);
637 rename(mailFile, mailFile2); // TODO: xrename?
638 free(mailFile2);
639 }
640 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200641}
642
643/*
644 * process_finished_job - called when job terminates and when mail terminates
645 */
646static void process_finished_job(const char *user, CronLine *line)
647{
648 pid_t pid;
649 int mailFd;
650 char mailFile[128];
651 struct stat sbuf;
652
653 pid = line->cl_pid;
654 line->cl_pid = 0;
655 if (pid <= 0) {
656 /* No job */
657 return;
658 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200659 if (line->cl_empty_mail_size <= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200660 /* End of job and no mail file, or end of sendmail job */
661 return;
662 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200663
664 /*
665 * End of primary job - check for mail file.
666 * If size has changed and the file is still valid, we send it.
667 */
668 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, (int)pid);
669 mailFd = open(mailFile, O_RDONLY);
670 unlink(mailFile);
671 if (mailFd < 0) {
672 return;
673 }
674
675 if (fstat(mailFd, &sbuf) < 0
676 || sbuf.st_uid != DAEMON_UID
677 || sbuf.st_nlink != 0
678 || sbuf.st_size == line->cl_empty_mail_size
679 || !S_ISREG(sbuf.st_mode)
680 ) {
681 close(mailFd);
682 return;
683 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200684 line->cl_empty_mail_size = 0;
685 /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */
686 line->cl_pid = fork_job(user, mailFd, SENDMAIL, NULL);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000687}
688
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200689#else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000690
Denys Vlasenko45963c82010-07-08 15:37:10 +0200691static void start_one_job(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000692{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000693 struct passwd *pas;
694 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000695
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000696 pas = getpwnam(user);
697 if (!pas) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100698 crondlog(WARN9 "can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000699 goto err;
700 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000701
Denys Vlasenko45963c82010-07-08 15:37:10 +0200702 /* Prepare things before vfork */
703 set_env_vars(pas);
704
705 /* Fork as the user in question and run program */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000706 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000707 if (pid == 0) {
708 /* CHILD */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200709 /* initgroups, setgid, setuid, and chdir to home or TMPDIR */
710 change_user(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000711 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000712 crondlog(LVL5 "child running %s", DEFAULT_SHELL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000713 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000714 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000715 bb_setpgrp();
Denys Vlasenko45963c82010-07-08 15:37:10 +0200716 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_cmd, (char *) NULL);
717 crondlog(ERR20 "can't execute '%s' for user %s", DEFAULT_SHELL, user);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000718 _exit(EXIT_SUCCESS);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000719 }
720 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000721 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000722 crondlog(ERR20 "can't vfork");
723 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000724 pid = 0;
725 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200726 line->cl_pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000727}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000728
Denys Vlasenko45963c82010-07-08 15:37:10 +0200729#define process_finished_job(user, line) ((line)->cl_pid = 0)
730
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200731#endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
732
Denys Vlasenko45963c82010-07-08 15:37:10 +0200733/*
734 * Determine which jobs need to be run. Under normal conditions, the
735 * period is about a minute (one scan). Worst case it will be one
736 * hour (60 scans).
737 */
738static void flag_starting_jobs(time_t t1, time_t t2)
739{
740 time_t t;
741
742 /* Find jobs > t1 and <= t2 */
743
744 for (t = t1 - t1 % 60; t <= t2; t += 60) {
745 struct tm *ptm;
746 CronFile *file;
747 CronLine *line;
748
749 if (t <= t1)
750 continue;
751
752 ptm = localtime(&t);
753 for (file = G.cron_files; file; file = file->cf_next) {
754 if (DebugOpt)
755 crondlog(LVL5 "file %s:", file->cf_username);
756 if (file->cf_deleted)
757 continue;
758 for (line = file->cf_lines; line; line = line->cl_next) {
759 if (DebugOpt)
760 crondlog(LVL5 " line %s", line->cl_cmd);
761 if (line->cl_Mins[ptm->tm_min]
762 && line->cl_Hrs[ptm->tm_hour]
763 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
764 && line->cl_Mons[ptm->tm_mon]
765 ) {
766 if (DebugOpt) {
767 crondlog(LVL5 " job: %d %s",
768 (int)line->cl_pid, line->cl_cmd);
769 }
770 if (line->cl_pid > 0) {
771 crondlog(LVL8 "user %s: process already running: %s",
772 file->cf_username, line->cl_cmd);
773 } else if (line->cl_pid == 0) {
774 line->cl_pid = -1;
775 file->cf_wants_starting = 1;
776 }
777 }
778 }
779 }
780 }
781}
782
783static void start_jobs(void)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200784{
785 CronFile *file;
786 CronLine *line;
787
Denys Vlasenko45963c82010-07-08 15:37:10 +0200788 for (file = G.cron_files; file; file = file->cf_next) {
789 if (!file->cf_wants_starting)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200790 continue;
791
Denys Vlasenko45963c82010-07-08 15:37:10 +0200792 file->cf_wants_starting = 0;
793 for (line = file->cf_lines; line; line = line->cl_next) {
794 pid_t pid;
795 if (line->cl_pid >= 0)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200796 continue;
797
Denys Vlasenko45963c82010-07-08 15:37:10 +0200798 start_one_job(file->cf_username, line);
799 pid = line->cl_pid;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200800 crondlog(LVL8 "USER %s pid %3d cmd %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200801 file->cf_username, (int)pid, line->cl_cmd);
802 if (pid < 0) {
803 file->cf_wants_starting = 1;
804 }
805 if (pid > 0) {
806 file->cf_has_running = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200807 }
808 }
809 }
810}
811
Denys Vlasenko45963c82010-07-08 15:37:10 +0200812/*
813 * Check for job completion, return number of jobs still running after
814 * all done.
815 */
816static int check_completions(void)
817{
818 CronFile *file;
819 CronLine *line;
820 int num_still_running = 0;
821
822 for (file = G.cron_files; file; file = file->cf_next) {
823 if (!file->cf_has_running)
824 continue;
825
826 file->cf_has_running = 0;
827 for (line = file->cf_lines; line; line = line->cl_next) {
828 int r;
829
830 if (line->cl_pid <= 0)
831 continue;
832
833 r = waitpid(line->cl_pid, NULL, WNOHANG);
834 if (r < 0 || r == line->cl_pid) {
835 process_finished_job(file->cf_username, line);
836 if (line->cl_pid == 0) {
837 /* sendmail was not started for it */
838 continue;
839 }
840 /* else: sendmail was started, job is still running, fall thru */
841 }
842 /* else: r == 0: "process is still running" */
843 file->cf_has_running = 1;
844 }
845//FIXME: if !file->cf_has_running && file->deleted: delete it!
846//otherwise deleted entries will stay forever, right?
847 num_still_running += file->cf_has_running;
848 }
849 return num_still_running;
850}
851
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200852int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
853int crond_main(int argc UNUSED_PARAM, char **argv)
854{
855 time_t t2;
856 int rescan;
857 int sleep_time;
858 unsigned opts;
859
860 INIT_G();
861
862 /* "-b after -f is ignored", and so on for every pair a-b */
863 opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
Denys Vlasenko8c84f752011-09-07 05:56:09 +0200864 /* -l and -d have numeric param */
865 ":l+" IF_FEATURE_CROND_D(":d+");
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200866 opts = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"),
Denys Vlasenko45963c82010-07-08 15:37:10 +0200867 &G.log_level, &G.log_filename, &G.crontab_dir_name
868 IF_FEATURE_CROND_D(,&G.log_level));
869 /* both -d N and -l N set the same variable: G.log_level */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200870
871 if (!(opts & OPT_f)) {
872 /* close stdin, stdout, stderr.
873 * close unused descriptors - don't need them. */
874 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
875 }
876
Denys Vlasenko45963c82010-07-08 15:37:10 +0200877 if (!(opts & OPT_d) && G.log_filename == NULL) {
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200878 /* logging to syslog */
879 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
880 logmode = LOGMODE_SYSLOG;
881 }
882
Denys Vlasenko45963c82010-07-08 15:37:10 +0200883 xchdir(G.crontab_dir_name);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200884 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
885 xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200886 crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", G.log_level);
887 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200888 write_pidfile("/var/run/crond.pid");
889
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200890 /* Main loop */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200891 t2 = time(NULL);
892 rescan = 60;
893 sleep_time = 60;
894 for (;;) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200895 struct stat sbuf;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200896 time_t t1;
897 long dt;
898
899 t1 = t2;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200900
901 /* Synchronize to 1 minute, minimum 1 second */
902 sleep(sleep_time - (time(NULL) % sleep_time) + 1);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200903
904 t2 = time(NULL);
905 dt = (long)t2 - (long)t1;
906
907 /*
908 * The file 'cron.update' is checked to determine new cron
909 * jobs. The directory is rescanned once an hour to deal
910 * with any screwups.
911 *
912 * Check for time jump. Disparities over an hour either way
913 * result in resynchronization. A negative disparity
914 * less than an hour causes us to effectively sleep until we
915 * match the original time (i.e. no re-execution of jobs that
916 * have just been run). A positive disparity less than
917 * an hour causes intermediate jobs to be run, but only once
918 * in the worst case.
919 *
920 * When running jobs, the inequality used is greater but not
921 * equal to t1, and less then or equal to t2.
922 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200923 if (stat(G.crontab_dir_name, &sbuf) != 0)
924 sbuf.st_mtime = 0; /* force update (once) if dir was deleted */
925 if (G.crontab_dir_mtime != sbuf.st_mtime) {
926 G.crontab_dir_mtime = sbuf.st_mtime;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200927 rescan = 1;
928 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200929 if (--rescan == 0) {
930 rescan = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200931 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200932 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200933 process_cron_update_file();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200934 if (DebugOpt)
935 crondlog(LVL5 "wakeup dt=%ld", dt);
936 if (dt < -60 * 60 || dt > 60 * 60) {
937 crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60);
938 /* and we do not run any jobs in this case */
939 } else if (dt > 0) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200940 /* Usual case: time advances forward, as expected */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200941 flag_starting_jobs(t1, t2);
942 start_jobs();
943 if (check_completions() > 0) {
944 /* some jobs are still running */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200945 sleep_time = 10;
946 } else {
947 sleep_time = 60;
948 }
949 }
950 /* else: time jumped back, do not run any jobs */
951 } /* for (;;) */
952
953 return 0; /* not reached */
954}