blob: 7135e4475a0b506223fa19d7dd7a61dbe39993ad [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 *
Mike Frysingerf284c762006-04-16 20:38:26 +000011 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000012 */
13
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000014#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000015#include <syslog.h>
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000016
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000017/* glibc frees previous setenv'ed value when we do next setenv()
18 * of the same variable. uclibc does not do this! */
19#if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */
20#define SETENV_LEAKS 0
21#else
22#define SETENV_LEAKS 1
23#endif
24
25
Denis Vlasenkoded5dfe2009-02-03 23:59:41 +000026#define TMPDIR CONFIG_FEATURE_CROND_DIR
27#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000028#ifndef SENDMAIL
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000029#define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000030#endif
31#ifndef SENDMAIL_ARGS
Vladimir Dronnikovb618dba2009-10-04 01:34:54 +020032#define SENDMAIL_ARGS "-ti", NULL
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000033#endif
34#ifndef CRONUPDATE
35#define CRONUPDATE "cron.update"
36#endif
37#ifndef MAXLINES
Glenn L McGrath9079ad02004-02-22 04:44:21 +000038#define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000039#endif
40
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000041
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000042typedef struct CronFile {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000043 struct CronFile *cf_Next;
44 struct CronLine *cf_LineBase;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000045 char *cf_User; /* username */
46 smallint cf_Ready; /* bool: one or more jobs ready */
47 smallint cf_Running; /* bool: one or more jobs running */
48 smallint cf_Deleted; /* marked for deletion, ignore */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000049} CronFile;
50
51typedef struct CronLine {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000052 struct CronLine *cl_Next;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000053 char *cl_Shell; /* shell command */
54 pid_t cl_Pid; /* running pid, 0, or armed (-1) */
55#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
56 int cl_MailPos; /* 'empty file' size */
57 smallint cl_MailFlag; /* running pid is for mail */
Denis Vlasenko551ffdc2009-04-01 19:48:05 +000058 char *cl_MailTo; /* whom to mail results */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000059#endif
60 /* ordered by size, not in natural order. makes code smaller: */
61 char cl_Dow[7]; /* 0-6, beginning sunday */
62 char cl_Mons[12]; /* 0-11 */
63 char cl_Hrs[24]; /* 0-23 */
64 char cl_Days[32]; /* 1-31 */
65 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000066} CronLine;
67
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000068
69#define DaemonUid 0
70
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000071
72enum {
73 OPT_l = (1 << 0),
74 OPT_L = (1 << 1),
75 OPT_f = (1 << 2),
76 OPT_b = (1 << 3),
77 OPT_S = (1 << 4),
78 OPT_c = (1 << 5),
Denis Vlasenko35a064b2008-11-06 00:49:59 +000079 OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000080};
Denis Vlasenko35a064b2008-11-06 00:49:59 +000081#if ENABLE_FEATURE_CROND_D
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000082#define DebugOpt (option_mask32 & OPT_d)
83#else
84#define DebugOpt 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000085#endif
86
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000087
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000088struct globals {
89 unsigned LogLevel; /* = 8; */
90 const char *LogFile;
91 const char *CDir; /* = CRONTABS; */
92 CronFile *FileBase;
93#if SETENV_LEAKS
94 char *env_var_user;
95 char *env_var_home;
96#endif
97};
98#define G (*(struct globals*)&bb_common_bufsiz1)
99#define LogLevel (G.LogLevel )
100#define LogFile (G.LogFile )
101#define CDir (G.CDir )
102#define FileBase (G.FileBase )
103#define env_var_user (G.env_var_user )
104#define env_var_home (G.env_var_home )
105#define INIT_G() do { \
106 LogLevel = 8; \
107 CDir = CRONTABS; \
108} while (0)
109
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000110
111static void CheckUpdates(void);
112static void SynchronizeDir(void);
113static int TestJobs(time_t t1, time_t t2);
114static void RunJobs(void);
115static int CheckJobs(void);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000116static void RunJob(const char *user, CronLine *line);
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000117#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000118static void EndJob(const char *user, CronLine *line);
Eric Andersen35e643b2003-07-28 07:40:39 +0000119#else
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000120#define EndJob(user, line) ((line)->cl_Pid = 0)
Eric Andersen35e643b2003-07-28 07:40:39 +0000121#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000122static void DeleteFile(const char *userName);
123
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000124
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100125/* 0 is the most verbose, default 8 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000126#define LVL5 "\x05"
127#define LVL7 "\x07"
128#define LVL8 "\x08"
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000129#define WARN9 "\x49"
130#define DIE9 "\xc9"
131/* level >= 20 is "error" */
132#define ERR20 "\x14"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000133
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100134static void crondlog(const char *ctl, ...) __attribute__ ((format (printf, 1, 2)));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000135static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000136{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000137 va_list va;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000138 int level = (ctl[0] & 0x1f);
Eric Andersen35e643b2003-07-28 07:40:39 +0000139
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000140 va_start(va, ctl);
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000141 if (level >= (int)LogLevel) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000142 /* Debug mode: all to (non-redirected) stderr, */
143 /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
144 if (!DebugOpt && LogFile) {
145 /* Otherwise (log to file): we reopen log file at every write: */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000146 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0666);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000147 if (logfd >= 0)
148 xmove_fd(logfd, STDERR_FILENO);
Eric Andersen35e643b2003-07-28 07:40:39 +0000149 }
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100150 /* When we log to syslog, level > 8 is logged at LOG_ERR
151 * syslog level, level <= 8 is logged at LOG_INFO. */
152 if (level > 8) {
153 bb_verror_msg(ctl + 1, va, /* strerr: */ NULL);
154 } else {
155 char *msg = NULL;
156 vasprintf(&msg, ctl + 1, va);
157 bb_info_msg("%s: %s", applet_name, msg);
158 free(msg);
159 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000160 }
161 va_end(va);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000162 if (ctl[0] & 0x80)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000163 exit(20);
Eric Andersen35e643b2003-07-28 07:40:39 +0000164}
165
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000166int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000167int crond_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000168{
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100169 unsigned opts;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000170
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000171 INIT_G();
172
173 /* "-b after -f is ignored", and so on for every pair a-b */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000174 opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000175 ":l+:d+"; /* -l and -d have numeric param */
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100176 opts = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"),
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000177 &LogLevel, &LogFile, &CDir
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000178 IF_FEATURE_CROND_D(,&LogLevel));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000179 /* both -d N and -l N set the same variable: LogLevel */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000180
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100181 if (!(opts & OPT_f)) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000182 /* close stdin, stdout, stderr.
183 * close unused descriptors - don't need them. */
Denis Vlasenkoc52248e2008-03-20 14:04:30 +0000184 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000185 }
186
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100187 if (!(opts & OPT_d) && LogFile == NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000188 /* logging to syslog */
189 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
190 logmode = LOGMODE_SYSLOG;
191 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000192
Denis Vlasenko5a142022007-03-26 13:20:54 +0000193 xchdir(CDir);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000194 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000195 xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100196 crondlog(LVL8 "crond (busybox "BB_VER") started, log level %d", LogLevel);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000197 SynchronizeDir();
198
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000199 /* main loop - synchronize to 1 second after the minute, minimum sleep
200 * of 1 second. */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000201 {
202 time_t t1 = time(NULL);
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000203 int rescan = 60;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000204 int sleep_time = 60;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000205
Denis Vlasenko10457b92007-03-27 22:01:31 +0000206 write_pidfile("/var/run/crond.pid");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000207 for (;;) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100208 time_t t2;
209 long dt;
210
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000211 sleep((sleep_time + 1) - (time(NULL) % sleep_time));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000212
213 t2 = time(NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000214 dt = (long)t2 - (long)t1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000215
216 /*
217 * The file 'cron.update' is checked to determine new cron
218 * jobs. The directory is rescanned once an hour to deal
219 * with any screwups.
220 *
221 * check for disparity. Disparities over an hour either way
222 * result in resynchronization. A reverse-indexed disparity
223 * less then an hour causes us to effectively sleep until we
224 * match the original time (i.e. no re-execution of jobs that
225 * have just been run). A forward-indexed disparity less then
226 * an hour causes intermediate jobs to be run, but only once
227 * in the worst case.
228 *
229 * when running jobs, the inequality used is greater but not
230 * equal to t1, and less then or equal to t2.
231 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000232 if (--rescan == 0) {
233 rescan = 60;
234 SynchronizeDir();
235 }
236 CheckUpdates();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000237 if (DebugOpt)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000238 crondlog(LVL5 "wakeup dt=%ld", dt);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000239 if (dt < -60 * 60 || dt > 60 * 60) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100240 crondlog(WARN9 "time disparity of %ld minutes detected", dt / 60);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000241 } else if (dt > 0) {
242 TestJobs(t1, t2);
243 RunJobs();
244 sleep(5);
245 if (CheckJobs() > 0) {
246 sleep_time = 10;
247 } else {
248 sleep_time = 60;
249 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000250 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000251 t1 = t2;
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100252 } /* for (;;) */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000253 }
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100254
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000255 return 0; /* not reached */
Glenn L McGrathb89fcd42003-12-23 07:21:33 +0000256}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000257
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000258#if SETENV_LEAKS
259/* We set environment *before* vfork (because we want to use vfork),
260 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
261 * Using putenv(), and freeing memory after unsetenv() won't leak */
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000262static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000263{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000264 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000265
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000266 if (var_val) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000267 bb_unsetenv(var_val);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000268 free(var_val);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000269 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000270 *pvar_val = xasprintf("%s=%s", var, val);
271 putenv(*pvar_val);
272}
273#endif
274
275static void SetEnv(struct passwd *pas)
276{
277#if SETENV_LEAKS
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000278 safe_setenv(&env_var_user, "USER", pas->pw_name);
279 safe_setenv(&env_var_home, "HOME", pas->pw_dir);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000280 /* if we want to set user's shell instead: */
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000281 /*safe_setenv(env_var_user, "SHELL", pas->pw_shell);*/
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000282#else
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000283 xsetenv("USER", pas->pw_name);
284 xsetenv("HOME", pas->pw_dir);
Eric Andersen35e643b2003-07-28 07:40:39 +0000285#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000286 /* currently, we use constant one: */
287 /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000288}
289
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000290static void ChangeUser(struct passwd *pas)
291{
292 /* careful: we're after vfork! */
293 change_identity(pas); /* - initgroups, setgid, setuid */
294 if (chdir(pas->pw_dir) < 0) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100295 crondlog(WARN9 "can't chdir(%s)", pas->pw_dir);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000296 if (chdir(TMPDIR) < 0) {
297 crondlog(DIE9 "can't chdir(%s)", TMPDIR); /* exits */
298 }
299 }
300}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000301
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000302static const char DowAry[] ALIGN1 =
303 "sun""mon""tue""wed""thu""fri""sat"
304 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
305;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000306
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000307static const char MonAry[] ALIGN1 =
308 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
309 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
310;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000311
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000312static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000313 const char *names, char *ptr)
314/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000315{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000316 char *base = ptr;
317 int n1 = -1;
318 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000319
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000320 // this can't happen due to config_read()
321 /*if (base == NULL)
322 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000323
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000324 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000325 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000326
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000327 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000328 if (*ptr == '*') {
329 n1 = 0; /* everything will be filled */
330 n2 = modvalue - 1;
331 skip = 1;
332 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000333 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200334 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000335 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200336 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000337 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200338 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000339 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200340 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000341 skip = 1;
342 } else if (names) {
343 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000344
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000345 for (i = 0; names[i]; i += 3) {
346 /* was using strncmp before... */
347 if (strncasecmp(ptr, &names[i], 3) == 0) {
348 ptr += 3;
349 if (n1 < 0) {
350 n1 = i / 3;
351 } else {
352 n2 = i / 3;
353 }
354 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000355 break;
356 }
357 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000358 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000359
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000360 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000361 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000362 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000363 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000364 if (*ptr == '-' && n2 < 0) {
365 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000366 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000367 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000368
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000369 /*
370 * collapse single-value ranges, handle skipmark, and fill
371 * in the character array appropriately.
372 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000373 if (n2 < 0) {
374 n2 = n1;
375 }
376 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200377 char *endp;
378 skip = strtol(ptr + 1, &endp, 10);
379 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000380 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000381
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000382 /*
383 * fill array, using a failsafe is the easiest way to prevent
384 * an endless loop
385 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000386 {
387 int s0 = 1;
388 int failsafe = 1024;
389
390 --n1;
391 do {
392 n1 = (n1 + 1) % modvalue;
393
394 if (--s0 == 0) {
395 ary[n1 % modvalue] = 1;
396 s0 = skip;
397 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000398 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000399 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000400 }
401 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000402
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000403 }
404 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000405 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000406 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000407 ++ptr;
408 n1 = -1;
409 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000410 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000411
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000412 if (*ptr) {
413 err:
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000414 crondlog(WARN9 "user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000415 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000416 }
417
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000418 if (DebugOpt && (LogLevel <= 5)) { /* like LVL5 */
419 /* can't use crondlog, it inserts '\n' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000420 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000421 for (i = 0; i < modvalue; ++i)
422 fprintf(stderr, "%d", (unsigned char)ary[i]);
423 fputc('\n', stderr);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000424 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000425}
426
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000427static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000428{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000429 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000430 int weekUsed = 0;
431 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000432
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000433 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000434 if (line->cl_Dow[i] == 0) {
435 weekUsed = 1;
436 break;
437 }
438 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000439 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000440 if (line->cl_Days[i] == 0) {
441 daysUsed = 1;
442 break;
443 }
444 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000445 if (weekUsed != daysUsed) {
446 if (weekUsed)
447 memset(line->cl_Days, 0, sizeof(line->cl_Days));
448 else /* daysUsed */
449 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000450 }
451}
452
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000453static void SynchronizeFile(const char *fileName)
454{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000455 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000456 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000457 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000458 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000459#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
460 char *mailTo = NULL;
461#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000462
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000463 if (!fileName)
464 return;
465
466 DeleteFile(fileName);
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000467 parser = config_open(fileName);
468 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000469 return;
470
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000471 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000472
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000473 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000474 CronFile *file = xzalloc(sizeof(CronFile));
475 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000476 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000477
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000478 file->cf_User = xstrdup(fileName);
479 pline = &file->cf_LineBase;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000480
Denis Vlasenko084266e2008-07-26 23:08:31 +0000481 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000482 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000483
Denis Vlasenko084266e2008-07-26 23:08:31 +0000484 if (!--maxLines)
485 break;
486 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
487 if (!n)
488 break;
489
Denis Vlasenkodcb3fcb2008-07-19 22:57:00 +0000490 if (DebugOpt)
491 crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000492
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000493 /* check if line is setting MAILTO= */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000494 if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000495#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
496 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000497 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000498#endif /* otherwise just ignore such lines */
499 continue;
500 }
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000501 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000502 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000503 continue;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000504 *pline = line = xzalloc(sizeof(*line));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000505 /* parse date ranges */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000506 ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, tokens[0]);
507 ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, tokens[1]);
508 ParseField(file->cf_User, line->cl_Days, 32, 0, NULL, tokens[2]);
509 ParseField(file->cf_User, line->cl_Mons, 12, -1, MonAry, tokens[3]);
510 ParseField(file->cf_User, line->cl_Dow, 7, 0, DowAry, tokens[4]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000511 /*
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000512 * fix days and dow - if one is not "*" and the other
513 * is "*", the other is set to 0, and vise-versa
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000514 */
515 FixDayDow(line);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000516#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
517 /* copy mailto (can be NULL) */
518 line->cl_MailTo = xstrdup(mailTo);
519#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000520 /* copy command */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000521 line->cl_Shell = xstrdup(tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000522 if (DebugOpt) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000523 crondlog(LVL5 " command:%s", tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000524 }
525 pline = &line->cl_Next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000526//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 +0000527 }
528 *pline = NULL;
529
530 file->cf_Next = FileBase;
531 FileBase = file;
532
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000533 if (maxLines == 0) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000534 crondlog(WARN9 "user %s: too many lines", fileName);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000535 }
536 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000537 config_close(parser);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000538}
539
540static void CheckUpdates(void)
541{
542 FILE *fi;
543 char buf[256];
544
Denis Vlasenko5415c852008-07-21 23:05:26 +0000545 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000546 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000547 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000548 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000549 /* use first word only */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000550 SynchronizeFile(strtok(buf, " \t\r\n"));
551 }
552 fclose(fi);
553 }
554}
555
556static void SynchronizeDir(void)
557{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000558 CronFile *file;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000559 /* Attempt to delete the database. */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000560 again:
561 for (file = FileBase; file; file = file->cf_Next) {
562 if (!file->cf_Deleted) {
563 DeleteFile(file->cf_User);
564 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000565 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000566 }
567
568 /*
569 * Remove cron update file
570 *
571 * Re-chdir, in case directory was renamed & deleted, or otherwise
572 * screwed up.
573 *
574 * scan directory and add associated users
575 */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000576 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000577 if (chdir(CDir) < 0) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000578 crondlog(DIE9 "can't chdir(%s)", CDir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000579 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000580 {
581 DIR *dir = opendir(".");
582 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000583
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000584 if (!dir)
585 crondlog(DIE9 "can't chdir(%s)", "."); /* exits */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000586 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000587 if (strchr(den->d_name, '.') != NULL) {
588 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000589 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000590 if (getpwnam(den->d_name)) {
591 SynchronizeFile(den->d_name);
592 } else {
593 crondlog(LVL7 "ignoring %s", den->d_name);
594 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000595 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000596 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000597 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000598}
599
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000600/*
601 * DeleteFile() - delete user database
602 *
603 * Note: multiple entries for same user may exist if we were unable to
604 * completely delete a database due to running processes.
605 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000606static void DeleteFile(const char *userName)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000607{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000608 CronFile **pfile = &FileBase;
609 CronFile *file;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000610
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000611 while ((file = *pfile) != NULL) {
612 if (strcmp(userName, file->cf_User) == 0) {
613 CronLine **pline = &file->cf_LineBase;
614 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000615
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000616 file->cf_Running = 0;
617 file->cf_Deleted = 1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000618
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000619 while ((line = *pline) != NULL) {
620 if (line->cl_Pid > 0) {
621 file->cf_Running = 1;
622 pline = &line->cl_Next;
623 } else {
624 *pline = line->cl_Next;
625 free(line->cl_Shell);
626 free(line);
627 }
628 }
629 if (file->cf_Running == 0) {
630 *pfile = file->cf_Next;
631 free(file->cf_User);
632 free(file);
633 } else {
634 pfile = &file->cf_Next;
635 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000636 } else {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000637 pfile = &file->cf_Next;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000638 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000639 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000640}
641
642/*
643 * TestJobs()
644 *
645 * determine which jobs need to be run. Under normal conditions, the
646 * period is about a minute (one scan). Worst case it will be one
647 * hour (60 scans).
648 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000649static int TestJobs(time_t t1, time_t t2)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000650{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000651 int nJobs = 0;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000652 time_t t;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000653
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000654 /* Find jobs > t1 and <= t2 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000655
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000656 for (t = t1 - t1 % 60; t <= t2; t += 60) {
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100657 struct tm *ptm;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000658 CronFile *file;
659 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000660
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000661 if (t <= t1)
662 continue;
663
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100664 ptm = localtime(&t);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000665 for (file = FileBase; file; file = file->cf_Next) {
666 if (DebugOpt)
667 crondlog(LVL5 "file %s:", file->cf_User);
668 if (file->cf_Deleted)
669 continue;
670 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000671 if (DebugOpt)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000672 crondlog(LVL5 " line %s", line->cl_Shell);
Denys Vlasenkodc698bb2010-01-09 19:10:49 +0100673 if (line->cl_Mins[ptm->tm_min] && line->cl_Hrs[ptm->tm_hour]
674 && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
675 && line->cl_Mons[ptm->tm_mon]
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000676 ) {
677 if (DebugOpt) {
678 crondlog(LVL5 " job: %d %s",
679 (int)line->cl_Pid, line->cl_Shell);
680 }
681 if (line->cl_Pid > 0) {
682 crondlog(LVL8 "user %s: process already running: %s",
683 file->cf_User, line->cl_Shell);
684 } else if (line->cl_Pid == 0) {
685 line->cl_Pid = -1;
686 file->cf_Ready = 1;
687 ++nJobs;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000688 }
689 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000690 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000691 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000692 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000693 return nJobs;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000694}
695
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000696static void RunJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000697{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000698 CronFile *file;
699 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000700
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000701 for (file = FileBase; file; file = file->cf_Next) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000702 if (!file->cf_Ready)
703 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000704
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000705 file->cf_Ready = 0;
706 for (line = file->cf_LineBase; line; line = line->cl_Next) {
707 if (line->cl_Pid >= 0)
708 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000709
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000710 RunJob(file->cf_User, line);
711 crondlog(LVL8 "USER %s pid %3d cmd %s",
712 file->cf_User, (int)line->cl_Pid, line->cl_Shell);
713 if (line->cl_Pid < 0) {
714 file->cf_Ready = 1;
715 } else if (line->cl_Pid > 0) {
716 file->cf_Running = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000717 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000718 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000719 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000720}
721
722/*
723 * CheckJobs() - check for job completion
724 *
725 * Check for job completion, return number of jobs still running after
726 * all done.
727 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000728static int CheckJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000729{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000730 CronFile *file;
731 CronLine *line;
732 int nStillRunning = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000733
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000734 for (file = FileBase; file; file = file->cf_Next) {
735 if (file->cf_Running) {
736 file->cf_Running = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000737
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000738 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000739 int status, r;
740 if (line->cl_Pid <= 0)
741 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000742
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000743 r = waitpid(line->cl_Pid, &status, WNOHANG);
744 if (r < 0 || r == line->cl_Pid) {
745 EndJob(file->cf_User, line);
746 if (line->cl_Pid) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000747 file->cf_Running = 1;
748 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000749 } else if (r == 0) {
750 file->cf_Running = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000751 }
752 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000753 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000754 nStillRunning += file->cf_Running;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000755 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000756 return nStillRunning;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000757}
758
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000759#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000760
761// TODO: sendmail should be _run-time_ option, not compile-time!
762
Eric Andersen35e643b2003-07-28 07:40:39 +0000763static void
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000764ForkJob(const char *user, CronLine *line, int mailFd,
Denis Vlasenko314820e2008-01-24 01:33:12 +0000765 const char *prog, const char *cmd, const char *arg,
766 const char *mail_filename)
Eric Andersen35e643b2003-07-28 07:40:39 +0000767{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000768 struct passwd *pas;
769 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000770
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000771 /* prepare things before vfork */
772 pas = getpwnam(user);
773 if (!pas) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100774 crondlog(WARN9 "can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000775 goto err;
776 }
777 SetEnv(pas);
778
779 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000780 if (pid == 0) {
781 /* CHILD */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000782 /* change running state to the user in question */
783 ChangeUser(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000784 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000785 crondlog(LVL5 "child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000786 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000787 if (mailFd >= 0) {
Denis Vlasenko314820e2008-01-24 01:33:12 +0000788 xmove_fd(mailFd, mail_filename ? 1 : 0);
789 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000790 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000791 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000792 bb_setpgrp();
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000793 execlp(prog, prog, cmd, arg, (char *) NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000794 crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000795 if (mail_filename) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000796 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
797 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000798 _exit(EXIT_SUCCESS);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000799 }
800
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000801 line->cl_Pid = pid;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000802 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000803 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000804 crondlog(ERR20 "can't vfork");
805 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000806 line->cl_Pid = 0;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000807 if (mail_filename) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000808 unlink(mail_filename);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000809 }
Denis Vlasenko314820e2008-01-24 01:33:12 +0000810 } else if (mail_filename) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000811 /* PARENT, FORK SUCCESS
812 * rename mail-file based on pid of process
813 */
814 char mailFile2[128];
815
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000816 snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000817 rename(mail_filename, mailFile2); // TODO: xrename?
Eric Andersen35e643b2003-07-28 07:40:39 +0000818 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000819
Eric Andersen35e643b2003-07-28 07:40:39 +0000820 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000821 * Close the mail file descriptor.. we can't just leave it open in
822 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000823 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000824 if (mailFd >= 0) {
825 close(mailFd);
826 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000827}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000828
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000829static void RunJob(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000830{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000831 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000832 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000833
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000834 line->cl_Pid = 0;
835 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000836
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000837 if (line->cl_MailTo) {
838 /* open mail file - owner root so nobody can screw with it. */
839 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid());
840 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000841
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000842 if (mailFd >= 0) {
843 line->cl_MailFlag = 1;
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000844 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_MailTo,
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000845 line->cl_Shell);
846 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
847 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100848 crondlog(ERR20 "can't create mail file %s for user %s, "
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000849 "discarding output", mailFile, user);
850 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000851 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000852
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000853 ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000854}
855
856/*
857 * EndJob - called when job terminates and when mail terminates
858 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000859static void EndJob(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000860{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000861 int mailFd;
862 char mailFile[128];
863 struct stat sbuf;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000864
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000865 /* No job */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000866 if (line->cl_Pid <= 0) {
867 line->cl_Pid = 0;
868 return;
869 }
870
871 /*
872 * End of job and no mail file
873 * End of sendmail job
874 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000875 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, line->cl_Pid);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000876 line->cl_Pid = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000877
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000878 if (line->cl_MailFlag == 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000879 return;
880 }
881 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000882
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000883 /*
884 * End of primary job - check for mail file. If size has increased and
885 * the file is still valid, we sendmail it.
886 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000887 mailFd = open(mailFile, O_RDONLY);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000888 unlink(mailFile);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000889 if (mailFd < 0) {
890 return;
891 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000892
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +0000893 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid
894 || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos
895 || !S_ISREG(sbuf.st_mode)
896 ) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000897 close(mailFd);
898 return;
899 }
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000900 if (line->cl_MailTo)
901 ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
Eric Andersen35e643b2003-07-28 07:40:39 +0000902}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000903
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000904#else /* crond without sendmail */
905
906static void RunJob(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000907{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000908 struct passwd *pas;
909 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000910
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000911 /* prepare things before vfork */
912 pas = getpwnam(user);
913 if (!pas) {
Denys Vlasenko78fcec42009-12-13 17:42:49 +0100914 crondlog(WARN9 "can't get uid for %s", user);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000915 goto err;
916 }
917 SetEnv(pas);
918
919 /* fork as the user in question and run program */
920 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000921 if (pid == 0) {
922 /* CHILD */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000923 /* change running state to the user in question */
924 ChangeUser(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000925 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000926 crondlog(LVL5 "child running %s", DEFAULT_SHELL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000927 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000928 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000929 bb_setpgrp();
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000930 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, (char *) NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000931 crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user,
932 DEFAULT_SHELL, "-c", line->cl_Shell);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000933 _exit(EXIT_SUCCESS);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000934 }
935 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000936 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000937 crondlog(ERR20 "can't vfork");
938 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000939 pid = 0;
940 }
941 line->cl_Pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000942}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000943
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000944#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */