blob: eb327f8555692855d3b02f80c7110514a61fd7c7 [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
12//config: bool "crond"
13//config: default y
14//config: select FEATURE_SYSLOG
15//config: help
16//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
23//config:
24//config:config FEATURE_CROND_D
25//config: bool "Support option -d to redirect output to stderr"
26//config: depends on CROND
27//config: default y
28//config: help
29//config: -d N sets loglevel (0:most verbose) and directs all output to stderr.
30//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
36//config: Command output will be sent to corresponding user via email.
37//config:
38//config:config FEATURE_CROND_DIR
39//config: string "crond spool directory"
40//config: default "/var/spool/cron"
41//config: depends on CROND || CRONTAB
42//config: help
43//config: Location of crond spool.
44
45//applet:IF_CROND(APPLET(crond, BB_DIR_USR_SBIN, BB_SUID_DROP))
46
47//kbuild:lib-$(CONFIG_CROND) += crond.o
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000048
Pere Orga5bc8c002011-04-11 03:29:49 +020049//usage:#define crond_trivial_usage
50//usage: "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR"
51//usage:#define crond_full_usage "\n\n"
52//usage: " -f Foreground"
53//usage: "\n -b Background (default)"
54//usage: "\n -S Log to syslog (default)"
Denys Vlasenko01a1a962014-04-30 14:47:28 +020055//usage: "\n -l N Set log level. Most verbose:0, default:8"
Pere Orga5bc8c002011-04-11 03:29:49 +020056//usage: IF_FEATURE_CROND_D(
Denys Vlasenko01a1a962014-04-30 14:47:28 +020057//usage: "\n -d N Set log level, log to stderr"
Pere Orga5bc8c002011-04-11 03:29:49 +020058//usage: )
Denys Vlasenko01a1a962014-04-30 14:47:28 +020059//usage: "\n -L FILE Log to FILE"
60//usage: "\n -c DIR Cron dir. Default:"CONFIG_FEATURE_CROND_DIR"/crontabs"
Pere Orga5bc8c002011-04-11 03:29:49 +020061
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000062#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000063#include <syslog.h>
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000064
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000065/* glibc frees previous setenv'ed value when we do next setenv()
66 * of the same variable. uclibc does not do this! */
67#if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020068# define SETENV_LEAKS 0
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000069#else
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020070# define SETENV_LEAKS 1
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000071#endif
72
73
Denys Vlasenko01a1a962014-04-30 14:47:28 +020074#define CRON_DIR CONFIG_FEATURE_CROND_DIR
Denis Vlasenkoded5dfe2009-02-03 23:59:41 +000075#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000076#ifndef SENDMAIL
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020077# define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000078#endif
79#ifndef SENDMAIL_ARGS
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +020080# define SENDMAIL_ARGS "-ti"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000081#endif
82#ifndef CRONUPDATE
Denys Vlasenko4a09aef2010-07-08 04:07:15 +020083# define CRONUPDATE "cron.update"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000084#endif
85#ifndef MAXLINES
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +020086# define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000087#endif
88
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000089
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000090typedef struct CronFile {
Denys Vlasenko45963c82010-07-08 15:37:10 +020091 struct CronFile *cf_next;
92 struct CronLine *cf_lines;
93 char *cf_username;
94 smallint cf_wants_starting; /* bool: one or more jobs ready */
95 smallint cf_has_running; /* bool: one or more jobs running */
96 smallint cf_deleted; /* marked for deletion (but still has running jobs) */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000097} CronFile;
98
99typedef struct CronLine {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200100 struct CronLine *cl_next;
101 char *cl_cmd; /* shell command */
102 pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000103#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200104 int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200105 char *cl_mailto; /* whom to mail results, may be NULL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000106#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200107 char *cl_shell;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000108 /* ordered by size, not in natural order. makes code smaller: */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200109 char cl_Dow[7]; /* 0-6, beginning sunday */
110 char cl_Mons[12]; /* 0-11 */
111 char cl_Hrs[24]; /* 0-23 */
112 char cl_Days[32]; /* 1-31 */
113 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000114} CronLine;
115
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000116
Denys Vlasenko45963c82010-07-08 15:37:10 +0200117#define DAEMON_UID 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000118
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000119
120enum {
121 OPT_l = (1 << 0),
122 OPT_L = (1 << 1),
123 OPT_f = (1 << 2),
124 OPT_b = (1 << 3),
125 OPT_S = (1 << 4),
126 OPT_c = (1 << 5),
Denis Vlasenko35a064b2008-11-06 00:49:59 +0000127 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000128};
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000129
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000130struct globals {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200131 unsigned log_level; /* = 8; */
132 time_t crontab_dir_mtime;
133 const char *log_filename;
134 const char *crontab_dir_name; /* = CRONTABS; */
135 CronFile *cron_files;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000136#if SETENV_LEAKS
137 char *env_var_user;
138 char *env_var_home;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200139 char *env_var_shell;
140 char *env_var_logname;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000141#endif
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100142} FIX_ALIASING;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000143#define G (*(struct globals*)&bb_common_bufsiz1)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000144#define INIT_G() do { \
Denys Vlasenko45963c82010-07-08 15:37:10 +0200145 G.log_level = 8; \
146 G.crontab_dir_name = CRONTABS; \
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000147} while (0)
148
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200149/* Log levels:
150 * 0 is the most verbose, default 8.
151 * For some reason, in fact only 5, 7 and 8 are used.
152 */
153static void crondlog(unsigned level, const char *msg, va_list va)
154{
155 if (level >= G.log_level) {
156 /*
157 * We are called only for info meesages.
158 * Warnings/errors use plain bb_[p]error_msg's, which
159 * need not touch syslog_level
160 * (they are ok with LOG_ERR default).
161 */
162 syslog_level = LOG_INFO;
163 bb_verror_msg(msg, va, /* strerr: */ NULL);
164 syslog_level = LOG_ERR;
165 }
166}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000167
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200168static void log5(const char *msg, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000169{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000170 va_list va;
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200171 va_start(va, msg);
172 crondlog(4, msg, va);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000173 va_end(va);
Eric Andersen35e643b2003-07-28 07:40:39 +0000174}
175
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200176static void log7(const char *msg, ...)
177{
178 va_list va;
179 va_start(va, msg);
180 crondlog(7, msg, va);
181 va_end(va);
182}
183
184static void log8(const char *msg, ...)
185{
186 va_list va;
187 va_start(va, msg);
188 crondlog(8, msg, va);
189 va_end(va);
190}
191
192
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000193static const char DowAry[] ALIGN1 =
194 "sun""mon""tue""wed""thu""fri""sat"
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000195;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000196
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000197static const char MonAry[] ALIGN1 =
198 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000199;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000200
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000201static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000202 const char *names, char *ptr)
203/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000204{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000205 char *base = ptr;
206 int n1 = -1;
207 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000208
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000209 // this can't happen due to config_read()
210 /*if (base == NULL)
211 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000212
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000213 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000214 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000215
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000216 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000217 if (*ptr == '*') {
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200218 n1 = 0; /* everything will be filled */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000219 n2 = modvalue - 1;
220 skip = 1;
221 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000222 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200223 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000224 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200225 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000226 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200227 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000228 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200229 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000230 skip = 1;
231 } else if (names) {
232 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000233
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000234 for (i = 0; names[i]; i += 3) {
235 /* was using strncmp before... */
236 if (strncasecmp(ptr, &names[i], 3) == 0) {
237 ptr += 3;
238 if (n1 < 0) {
239 n1 = i / 3;
240 } else {
241 n2 = i / 3;
242 }
243 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000244 break;
245 }
246 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000247 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000248
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000249 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000250 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000251 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000252 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000253 if (*ptr == '-' && n2 < 0) {
254 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000255 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000256 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000257
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000258 /*
259 * collapse single-value ranges, handle skipmark, and fill
260 * in the character array appropriately.
261 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000262 if (n2 < 0) {
263 n2 = n1;
264 }
265 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200266 char *endp;
267 skip = strtol(ptr + 1, &endp, 10);
268 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000269 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000270
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000271 /*
272 * fill array, using a failsafe is the easiest way to prevent
273 * an endless loop
274 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000275 {
276 int s0 = 1;
277 int failsafe = 1024;
278
279 --n1;
280 do {
281 n1 = (n1 + 1) % modvalue;
282
283 if (--s0 == 0) {
284 ary[n1 % modvalue] = 1;
285 s0 = skip;
286 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000287 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000288 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000289 }
290 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000291 }
292 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000293 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000294 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000295 ++ptr;
296 n1 = -1;
297 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000298 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000299
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000300 if (*ptr) {
301 err:
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200302 bb_error_msg("user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000303 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000304 }
305
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200306 /* can't use log5 (it inserts newlines), open-coding it */
307 if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000308 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000309 for (i = 0; i < modvalue; ++i)
310 fprintf(stderr, "%d", (unsigned char)ary[i]);
Denys Vlasenko19ced5c2010-06-06 21:53:09 +0200311 bb_putchar_stderr('\n');
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000312 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000313}
314
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000315static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000316{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000317 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000318 int weekUsed = 0;
319 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000320
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000321 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000322 if (line->cl_Dow[i] == 0) {
323 weekUsed = 1;
324 break;
325 }
326 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000327 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000328 if (line->cl_Days[i] == 0) {
329 daysUsed = 1;
330 break;
331 }
332 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000333 if (weekUsed != daysUsed) {
334 if (weekUsed)
335 memset(line->cl_Days, 0, sizeof(line->cl_Days));
336 else /* daysUsed */
337 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000338 }
339}
340
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200341/*
Denys Vlasenko45963c82010-07-08 15:37:10 +0200342 * delete_cronfile() - delete user database
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200343 *
344 * Note: multiple entries for same user may exist if we were unable to
345 * completely delete a database due to running processes.
346 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200347//FIXME: we will start a new job even if the old job is running
348//if crontab was reloaded: crond thinks that "new" job is different from "old"
349//even if they are in fact completely the same. Example
350//Crontab was:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200351// 0-59 * * * * job1
352// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200353//User edits crontab to:
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200354// 0-59 * * * * job1_updated
355// 0-59 * * * * long_running_job2
Denys Vlasenko45963c82010-07-08 15:37:10 +0200356//Bug: crond can now start another long_running_job2 even if old one
357//is still running.
Denys Vlasenko69d69e22010-07-11 23:20:15 +0200358//OTOH most other versions of cron do not wait for job termination anyway,
359//they end up with multiple copies of jobs if they don't terminate soon enough.
Denys Vlasenko45963c82010-07-08 15:37:10 +0200360static void delete_cronfile(const char *userName)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200361{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200362 CronFile **pfile = &G.cron_files;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200363 CronFile *file;
364
365 while ((file = *pfile) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200366 if (strcmp(userName, file->cf_username) == 0) {
367 CronLine **pline = &file->cf_lines;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200368 CronLine *line;
369
Denys Vlasenko45963c82010-07-08 15:37:10 +0200370 file->cf_has_running = 0;
371 file->cf_deleted = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200372
373 while ((line = *pline) != NULL) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200374 if (line->cl_pid > 0) {
375 file->cf_has_running = 1;
376 pline = &line->cl_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200377 } else {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200378 *pline = line->cl_next;
379 free(line->cl_cmd);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200380 free(line);
381 }
382 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200383 if (file->cf_has_running == 0) {
384 *pfile = file->cf_next;
385 free(file->cf_username);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200386 free(file);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200387 continue;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200388 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200389 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200390 pfile = &file->cf_next;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200391 }
392}
393
Denys Vlasenko45963c82010-07-08 15:37:10 +0200394static void load_crontab(const char *fileName)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000395{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000396 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000397 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000398 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000399 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000400#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
401 char *mailTo = NULL;
402#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200403 char *shell = NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000404
Denys Vlasenko45963c82010-07-08 15:37:10 +0200405 delete_cronfile(fileName);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000406
Denys Vlasenko45963c82010-07-08 15:37:10 +0200407 if (!getpwnam(fileName)) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200408 log7("ignoring file '%s' (no such user)", fileName);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200409 return;
410 }
411
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000412 parser = config_open(fileName);
413 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000414 return;
415
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000416 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000417
Denys Vlasenko45963c82010-07-08 15:37:10 +0200418 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000419 CronFile *file = xzalloc(sizeof(CronFile));
420 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000421 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000422
Denys Vlasenko45963c82010-07-08 15:37:10 +0200423 file->cf_username = xstrdup(fileName);
424 pline = &file->cf_lines;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000425
Denis Vlasenko084266e2008-07-26 23:08:31 +0000426 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000427 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000428
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200429 if (!--maxLines) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200430 bb_error_msg("user %s: too many lines", fileName);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000431 break;
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200432 }
433
Denis Vlasenko084266e2008-07-26 23:08:31 +0000434 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
435 if (!n)
436 break;
437
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200438 log5("user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000439
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000440 /* check if line is setting MAILTO= */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100441 if (is_prefixed_with(tokens[0], "MAILTO=")) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000442#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
443 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000444 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000445#endif /* otherwise just ignore such lines */
446 continue;
447 }
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100448 if (is_prefixed_with(tokens[0], "SHELL=")) {
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200449 free(shell);
450 shell = xstrdup(&tokens[0][6]);
451 continue;
452 }
453//TODO: handle HOME= too? "man crontab" says:
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200454//name = value
455//
456//where the spaces around the equal-sign (=) are optional, and any subsequent
457//non-leading spaces in value will be part of the value assigned to name.
458//The value string may be placed in quotes (single or double, but matching)
459//to preserve leading or trailing blanks.
460//
461//Several environment variables are set up automatically by the cron(8) daemon.
462//SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd
463//line of the crontab's owner. HOME and SHELL may be overridden by settings
464//in the crontab; LOGNAME may not.
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200465
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000466 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000467 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000468 continue;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000469 *pline = line = xzalloc(sizeof(*line));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000470 /* parse date ranges */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200471 ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
472 ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
473 ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
474 ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
475 ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000476 /*
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000477 * fix days and dow - if one is not "*" and the other
478 * is "*", the other is set to 0, and vise-versa
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000479 */
480 FixDayDow(line);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000481#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
482 /* copy mailto (can be NULL) */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200483 line->cl_mailto = xstrdup(mailTo);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000484#endif
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200485 line->cl_shell = xstrdup(shell);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000486 /* copy command */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200487 line->cl_cmd = xstrdup(tokens[5]);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200488 pline = &line->cl_next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000489//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 +0000490 }
491 *pline = NULL;
492
Denys Vlasenko45963c82010-07-08 15:37:10 +0200493 file->cf_next = G.cron_files;
494 G.cron_files = file;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000495 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000496 config_close(parser);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200497#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
498 free(mailTo);
499#endif
500 free(shell);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000501}
502
Denys Vlasenko45963c82010-07-08 15:37:10 +0200503static void process_cron_update_file(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000504{
505 FILE *fi;
506 char buf[256];
507
Denis Vlasenko5415c852008-07-21 23:05:26 +0000508 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000509 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000510 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000511 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000512 /* use first word only */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200513 skip_non_whitespace(buf)[0] = '\0';
514 load_crontab(buf);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000515 }
516 fclose(fi);
517 }
518}
519
Denys Vlasenko45963c82010-07-08 15:37:10 +0200520static void rescan_crontab_dir(void)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000521{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000522 CronFile *file;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200523
524 /* Delete all files until we only have ones with running jobs (or none) */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000525 again:
Denys Vlasenko45963c82010-07-08 15:37:10 +0200526 for (file = G.cron_files; file; file = file->cf_next) {
527 if (!file->cf_deleted) {
528 delete_cronfile(file->cf_username);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000529 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000530 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000531 }
532
Denys Vlasenko45963c82010-07-08 15:37:10 +0200533 /* Remove cron update file */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000534 unlink(CRONUPDATE);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200535 /* Re-chdir, in case directory was renamed & deleted */
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200536 xchdir(G.crontab_dir_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200537
538 /* Scan directory and add associated users */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000539 {
540 DIR *dir = opendir(".");
541 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000542
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200543 /* xopendir exists, but "can't open '.'" is not informative */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000544 if (!dir)
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200545 bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000546 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000547 if (strchr(den->d_name, '.') != NULL) {
548 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000549 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200550 load_crontab(den->d_name);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000551 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000552 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000553 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000554}
555
Denys Vlasenko45963c82010-07-08 15:37:10 +0200556#if SETENV_LEAKS
557/* We set environment *before* vfork (because we want to use vfork),
558 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
559 * Using putenv(), and freeing memory after unsetenv() won't leak */
560static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000561{
Denys Vlasenko45963c82010-07-08 15:37:10 +0200562 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000563
Denys Vlasenko45963c82010-07-08 15:37:10 +0200564 if (var_val) {
565 bb_unsetenv_and_free(var_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000566 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200567 *pvar_val = xasprintf("%s=%s", var, val);
568 putenv(*pvar_val);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000569}
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200570#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000571
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200572static void set_env_vars(struct passwd *pas, const char *shell)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000573{
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200574 /* POSIX requires crond to set up at least HOME, LOGNAME, PATH, SHELL.
575 * We assume crond inherited suitable PATH.
576 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200577#if SETENV_LEAKS
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200578 safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200579 safe_setenv(&G.env_var_user, "USER", pas->pw_name);
580 safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200581 safe_setenv(&G.env_var_shell, "SHELL", shell);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200582#else
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200583 xsetenv("LOGNAME", pas->pw_name);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200584 xsetenv("USER", pas->pw_name);
585 xsetenv("HOME", pas->pw_dir);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200586 xsetenv("SHELL", shell);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200587#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000588}
589
Denys Vlasenko45963c82010-07-08 15:37:10 +0200590static void change_user(struct passwd *pas)
591{
592 /* careful: we're after vfork! */
593 change_identity(pas); /* - initgroups, setgid, setuid */
594 if (chdir(pas->pw_dir) < 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200595 bb_error_msg("can't change directory to '%s'", pas->pw_dir);
596 xchdir(CRON_DIR);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200597 }
598}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000599
600// TODO: sendmail should be _run-time_ option, not compile-time!
Denys Vlasenko45963c82010-07-08 15:37:10 +0200601#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000602
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200603static pid_t
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200604fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
605{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000606 struct passwd *pas;
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200607 const char *shell, *prog;
608 smallint sv_logmode;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000609 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000610
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000611 /* prepare things before vfork */
612 pas = getpwnam(user);
613 if (!pas) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200614 bb_error_msg("can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000615 goto err;
616 }
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200617
618 shell = line->cl_shell ? line->cl_shell : DEFAULT_SHELL;
619 prog = run_sendmail ? SENDMAIL : shell;
620
621 set_env_vars(pas, shell);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000622
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200623 sv_logmode = logmode;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000624 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000625 if (pid == 0) {
626 /* CHILD */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200627 /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200628 change_user(pas);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200629 log5("child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000630 if (mailFd >= 0) {
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200631 xmove_fd(mailFd, run_sendmail ? 0 : 1);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000632 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000633 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000634 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000635 bb_setpgrp();
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200636 if (!run_sendmail)
637 execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
638 else
639 execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200640 /*
641 * I want this error message on stderr too,
642 * even if other messages go only to syslog:
643 */
644 logmode |= LOGMODE_STDIO;
645 bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000646 }
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200647 logmode = sv_logmode;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000648
649 if (pid < 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200650 bb_perror_msg("vfork");
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000651 err:
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200652 pid = 0;
653 } /* else: PARENT, FORK SUCCESS */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000654
Eric Andersen35e643b2003-07-28 07:40:39 +0000655 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000656 * Close the mail file descriptor.. we can't just leave it open in
657 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000658 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000659 if (mailFd >= 0) {
660 close(mailFd);
661 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200662 return pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000663}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000664
Denys Vlasenko45963c82010-07-08 15:37:10 +0200665static void start_one_job(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000666{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000667 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000668 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000669
Denys Vlasenko45963c82010-07-08 15:37:10 +0200670 line->cl_pid = 0;
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200671 line->cl_empty_mail_size = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000672
Denys Vlasenko45963c82010-07-08 15:37:10 +0200673 if (line->cl_mailto) {
674 /* Open mail file (owner is root so nobody can screw with it) */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200675 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid());
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000676 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000677
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000678 if (mailFd >= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200679 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
680 line->cl_cmd);
681 line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000682 } else {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200683 bb_error_msg("can't create mail file %s for user %s, "
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000684 "discarding output", mailFile, user);
685 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000686 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000687
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200688 line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 0);
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200689 if (mailFd >= 0) {
690 if (line->cl_pid <= 0) {
691 unlink(mailFile);
692 } else {
693 /* rename mail-file based on pid of process */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200694 char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid);
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200695 rename(mailFile, mailFile2); // TODO: xrename?
696 free(mailFile2);
697 }
698 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200699}
700
701/*
702 * process_finished_job - called when job terminates and when mail terminates
703 */
704static void process_finished_job(const char *user, CronLine *line)
705{
706 pid_t pid;
707 int mailFd;
708 char mailFile[128];
709 struct stat sbuf;
710
711 pid = line->cl_pid;
712 line->cl_pid = 0;
713 if (pid <= 0) {
714 /* No job */
715 return;
716 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200717 if (line->cl_empty_mail_size <= 0) {
Denys Vlasenko45963c82010-07-08 15:37:10 +0200718 /* End of job and no mail file, or end of sendmail job */
719 return;
720 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200721
722 /*
723 * End of primary job - check for mail file.
724 * If size has changed and the file is still valid, we send it.
725 */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200726 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200727 mailFd = open(mailFile, O_RDONLY);
728 unlink(mailFile);
729 if (mailFd < 0) {
730 return;
731 }
732
733 if (fstat(mailFd, &sbuf) < 0
734 || sbuf.st_uid != DAEMON_UID
735 || sbuf.st_nlink != 0
736 || sbuf.st_size == line->cl_empty_mail_size
737 || !S_ISREG(sbuf.st_mode)
738 ) {
739 close(mailFd);
740 return;
741 }
Denys Vlasenko1f0ab1d2010-07-08 16:12:10 +0200742 line->cl_empty_mail_size = 0;
743 /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200744 line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 1);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000745}
746
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200747#else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000748
Denys Vlasenko45963c82010-07-08 15:37:10 +0200749static void start_one_job(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000750{
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200751 const char *shell;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000752 struct passwd *pas;
753 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000754
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000755 pas = getpwnam(user);
756 if (!pas) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200757 bb_error_msg("can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000758 goto err;
759 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000760
Denys Vlasenko45963c82010-07-08 15:37:10 +0200761 /* Prepare things before vfork */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200762 shell = line->cl_shell ? line->cl_shell : DEFAULT_SHELL;
763 set_env_vars(pas, shell);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200764
765 /* Fork as the user in question and run program */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000766 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000767 if (pid == 0) {
768 /* CHILD */
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200769 /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200770 change_user(pas);
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200771 log5("child running %s", shell);
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000772 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000773 bb_setpgrp();
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200774 execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
775 bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000776 }
777 if (pid < 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200778 bb_perror_msg("vfork");
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000779 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000780 pid = 0;
781 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200782 line->cl_pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000783}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000784
Denys Vlasenko45963c82010-07-08 15:37:10 +0200785#define process_finished_job(user, line) ((line)->cl_pid = 0)
786
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200787#endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
788
Denys Vlasenko45963c82010-07-08 15:37:10 +0200789/*
790 * Determine which jobs need to be run. Under normal conditions, the
791 * period is about a minute (one scan). Worst case it will be one
792 * hour (60 scans).
793 */
794static void flag_starting_jobs(time_t t1, time_t t2)
795{
796 time_t t;
797
798 /* Find jobs > t1 and <= t2 */
799
800 for (t = t1 - t1 % 60; t <= t2; t += 60) {
801 struct tm *ptm;
802 CronFile *file;
803 CronLine *line;
804
805 if (t <= t1)
806 continue;
807
808 ptm = localtime(&t);
809 for (file = G.cron_files; file; file = file->cf_next) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200810 log5("file %s:", file->cf_username);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200811 if (file->cf_deleted)
812 continue;
813 for (line = file->cf_lines; line; line = line->cl_next) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200814 log5(" line %s", line->cl_cmd);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200815 if (line->cl_Mins[ptm->tm_min]
816 && line->cl_Hrs[ptm->tm_hour]
817 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
818 && line->cl_Mons[ptm->tm_mon]
819 ) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200820 log5(" job: %d %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200821 (int)line->cl_pid, line->cl_cmd);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200822 if (line->cl_pid > 0) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200823 log8("user %s: process already running: %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200824 file->cf_username, line->cl_cmd);
825 } else if (line->cl_pid == 0) {
826 line->cl_pid = -1;
827 file->cf_wants_starting = 1;
828 }
829 }
830 }
831 }
832 }
833}
834
835static void start_jobs(void)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200836{
837 CronFile *file;
838 CronLine *line;
839
Denys Vlasenko45963c82010-07-08 15:37:10 +0200840 for (file = G.cron_files; file; file = file->cf_next) {
841 if (!file->cf_wants_starting)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200842 continue;
843
Denys Vlasenko45963c82010-07-08 15:37:10 +0200844 file->cf_wants_starting = 0;
845 for (line = file->cf_lines; line; line = line->cl_next) {
846 pid_t pid;
847 if (line->cl_pid >= 0)
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200848 continue;
849
Denys Vlasenko45963c82010-07-08 15:37:10 +0200850 start_one_job(file->cf_username, line);
851 pid = line->cl_pid;
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200852 log8("USER %s pid %3d cmd %s",
Denys Vlasenko45963c82010-07-08 15:37:10 +0200853 file->cf_username, (int)pid, line->cl_cmd);
854 if (pid < 0) {
855 file->cf_wants_starting = 1;
856 }
857 if (pid > 0) {
858 file->cf_has_running = 1;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200859 }
860 }
861 }
862}
863
Denys Vlasenko45963c82010-07-08 15:37:10 +0200864/*
865 * Check for job completion, return number of jobs still running after
866 * all done.
867 */
868static int check_completions(void)
869{
870 CronFile *file;
871 CronLine *line;
872 int num_still_running = 0;
873
874 for (file = G.cron_files; file; file = file->cf_next) {
875 if (!file->cf_has_running)
876 continue;
877
878 file->cf_has_running = 0;
879 for (line = file->cf_lines; line; line = line->cl_next) {
880 int r;
881
882 if (line->cl_pid <= 0)
883 continue;
884
885 r = waitpid(line->cl_pid, NULL, WNOHANG);
886 if (r < 0 || r == line->cl_pid) {
887 process_finished_job(file->cf_username, line);
888 if (line->cl_pid == 0) {
889 /* sendmail was not started for it */
890 continue;
891 }
892 /* else: sendmail was started, job is still running, fall thru */
893 }
894 /* else: r == 0: "process is still running" */
895 file->cf_has_running = 1;
896 }
897//FIXME: if !file->cf_has_running && file->deleted: delete it!
898//otherwise deleted entries will stay forever, right?
899 num_still_running += file->cf_has_running;
900 }
901 return num_still_running;
902}
903
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200904static void reopen_logfile_to_stderr(void)
905{
906 if (G.log_filename) {
907 int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
908 if (logfd >= 0)
909 xmove_fd(logfd, STDERR_FILENO);
910 }
911}
912
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200913int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
914int crond_main(int argc UNUSED_PARAM, char **argv)
915{
916 time_t t2;
Denys Vlasenko01a1a962014-04-30 14:47:28 +0200917 unsigned rescan;
918 unsigned sleep_time;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200919 unsigned opts;
920
921 INIT_G();
922
923 /* "-b after -f is ignored", and so on for every pair a-b */
924 opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
Denys Vlasenko8c84f752011-09-07 05:56:09 +0200925 /* -l and -d have numeric param */
926 ":l+" IF_FEATURE_CROND_D(":d+");
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200927 opts = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"),
Denys Vlasenko45963c82010-07-08 15:37:10 +0200928 &G.log_level, &G.log_filename, &G.crontab_dir_name
929 IF_FEATURE_CROND_D(,&G.log_level));
930 /* both -d N and -l N set the same variable: G.log_level */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200931
932 if (!(opts & OPT_f)) {
933 /* close stdin, stdout, stderr.
934 * close unused descriptors - don't need them. */
935 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
936 }
937
Denys Vlasenko45963c82010-07-08 15:37:10 +0200938 if (!(opts & OPT_d) && G.log_filename == NULL) {
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200939 /* logging to syslog */
940 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
941 logmode = LOGMODE_SYSLOG;
942 }
943
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200944 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Denys Vlasenko43b8a1c2014-04-30 17:38:27 +0200945
946 reopen_logfile_to_stderr();
947 xchdir(G.crontab_dir_name);
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200948 log8("crond (busybox "BB_VER") started, log level %d", G.log_level);
Denys Vlasenko45963c82010-07-08 15:37:10 +0200949 rescan_crontab_dir();
Anthony G. Basile12677ac2012-12-10 14:49:39 -0500950 write_pidfile(CONFIG_PID_FILE_PATH "/crond.pid");
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200951
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200952 /* Main loop */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200953 t2 = time(NULL);
954 rescan = 60;
955 sleep_time = 60;
956 for (;;) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200957 struct stat sbuf;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200958 time_t t1;
959 long dt;
960
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200961 /* Synchronize to 1 minute, minimum 1 second */
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200962 t1 = t2;
963 sleep(sleep_time - (time(NULL) % sleep_time));
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200964 t2 = time(NULL);
965 dt = (long)t2 - (long)t1;
966
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200967 reopen_logfile_to_stderr();
968
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200969 /*
970 * The file 'cron.update' is checked to determine new cron
971 * jobs. The directory is rescanned once an hour to deal
972 * with any screwups.
973 *
974 * Check for time jump. Disparities over an hour either way
975 * result in resynchronization. A negative disparity
976 * less than an hour causes us to effectively sleep until we
977 * match the original time (i.e. no re-execution of jobs that
978 * have just been run). A positive disparity less than
979 * an hour causes intermediate jobs to be run, but only once
980 * in the worst case.
981 *
982 * When running jobs, the inequality used is greater but not
983 * equal to t1, and less then or equal to t2.
984 */
Denys Vlasenko45963c82010-07-08 15:37:10 +0200985 if (stat(G.crontab_dir_name, &sbuf) != 0)
986 sbuf.st_mtime = 0; /* force update (once) if dir was deleted */
987 if (G.crontab_dir_mtime != sbuf.st_mtime) {
988 G.crontab_dir_mtime = sbuf.st_mtime;
Denys Vlasenkodfc870f2010-07-08 04:07:54 +0200989 rescan = 1;
990 }
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200991 if (--rescan == 0) {
992 rescan = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +0200993 rescan_crontab_dir();
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200994 }
Denys Vlasenko45963c82010-07-08 15:37:10 +0200995 process_cron_update_file();
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200996 log5("wakeup dt=%ld", dt);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200997 if (dt < -60 * 60 || dt > 60 * 60) {
Denys Vlasenkod5929d62014-04-30 14:49:52 +0200998 bb_error_msg("time disparity of %ld minutes detected", dt / 60);
Denys Vlasenko4a09aef2010-07-08 04:07:15 +0200999 /* and we do not run any jobs in this case */
1000 } else if (dt > 0) {
Denys Vlasenkodfc870f2010-07-08 04:07:54 +02001001 /* Usual case: time advances forward, as expected */
Denys Vlasenko45963c82010-07-08 15:37:10 +02001002 flag_starting_jobs(t1, t2);
1003 start_jobs();
Denys Vlasenko01a1a962014-04-30 14:47:28 +02001004 sleep_time = 60;
Denys Vlasenko45963c82010-07-08 15:37:10 +02001005 if (check_completions() > 0) {
1006 /* some jobs are still running */
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001007 sleep_time = 10;
Denys Vlasenko4a09aef2010-07-08 04:07:15 +02001008 }
1009 }
1010 /* else: time jumped back, do not run any jobs */
1011 } /* for (;;) */
1012
1013 return 0; /* not reached */
1014}