blob: 74bf9168ba82eaa32077ced1b1cb9273d394d8e5 [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
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000125#define LVL5 "\x05"
126#define LVL7 "\x07"
127#define LVL8 "\x08"
128#define LVL9 "\x09"
129#define WARN9 "\x49"
130#define DIE9 "\xc9"
131/* level >= 20 is "error" */
132#define ERR20 "\x14"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000133
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000134static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000135{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000136 va_list va;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000137 int level = (ctl[0] & 0x1f);
Eric Andersen35e643b2003-07-28 07:40:39 +0000138
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000139 va_start(va, ctl);
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000140 if (level >= (int)LogLevel) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000141 /* Debug mode: all to (non-redirected) stderr, */
142 /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
143 if (!DebugOpt && LogFile) {
144 /* Otherwise (log to file): we reopen log file at every write: */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000145 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0666);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000146 if (logfd >= 0)
147 xmove_fd(logfd, STDERR_FILENO);
Eric Andersen35e643b2003-07-28 07:40:39 +0000148 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000149// TODO: ERR -> error, WARN -> warning, LVL -> info
150 bb_verror_msg(ctl + 1, va, /* strerr: */ NULL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000151 }
152 va_end(va);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000153 if (ctl[0] & 0x80)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000154 exit(20);
Eric Andersen35e643b2003-07-28 07:40:39 +0000155}
156
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000157int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000158int crond_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000159{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000160 unsigned opt;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000161
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000162 INIT_G();
163
164 /* "-b after -f is ignored", and so on for every pair a-b */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000165 opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000166 ":l+:d+"; /* -l and -d have numeric param */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000167 opt = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"),
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000168 &LogLevel, &LogFile, &CDir
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000169 IF_FEATURE_CROND_D(,&LogLevel));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000170 /* both -d N and -l N set the same variable: LogLevel */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000171
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000172 if (!(opt & OPT_f)) {
173 /* close stdin, stdout, stderr.
174 * close unused descriptors - don't need them. */
Denis Vlasenkoc52248e2008-03-20 14:04:30 +0000175 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000176 }
177
178 if (!DebugOpt && LogFile == NULL) {
179 /* logging to syslog */
180 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
181 logmode = LOGMODE_SYSLOG;
182 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000183
Denis Vlasenko5a142022007-03-26 13:20:54 +0000184 xchdir(CDir);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000185 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000186 xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000187 crondlog(LVL9 "crond (busybox "BB_VER") started, log level %d", LogLevel);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000188 SynchronizeDir();
189
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000190 /* main loop - synchronize to 1 second after the minute, minimum sleep
191 * of 1 second. */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000192 {
193 time_t t1 = time(NULL);
194 time_t t2;
195 long dt;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000196 int rescan = 60;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000197 int sleep_time = 60;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000198
Denis Vlasenko10457b92007-03-27 22:01:31 +0000199 write_pidfile("/var/run/crond.pid");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000200 for (;;) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000201 sleep((sleep_time + 1) - (time(NULL) % sleep_time));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000202
203 t2 = time(NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000204 dt = (long)t2 - (long)t1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000205
206 /*
207 * The file 'cron.update' is checked to determine new cron
208 * jobs. The directory is rescanned once an hour to deal
209 * with any screwups.
210 *
211 * check for disparity. Disparities over an hour either way
212 * result in resynchronization. A reverse-indexed disparity
213 * less then an hour causes us to effectively sleep until we
214 * match the original time (i.e. no re-execution of jobs that
215 * have just been run). A forward-indexed disparity less then
216 * an hour causes intermediate jobs to be run, but only once
217 * in the worst case.
218 *
219 * when running jobs, the inequality used is greater but not
220 * equal to t1, and less then or equal to t2.
221 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000222 if (--rescan == 0) {
223 rescan = 60;
224 SynchronizeDir();
225 }
226 CheckUpdates();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000227 if (DebugOpt)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000228 crondlog(LVL5 "wakeup dt=%ld", dt);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000229 if (dt < -60 * 60 || dt > 60 * 60) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000230 crondlog(WARN9 "time disparity of %d minutes detected", dt / 60);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000231 } else if (dt > 0) {
232 TestJobs(t1, t2);
233 RunJobs();
234 sleep(5);
235 if (CheckJobs() > 0) {
236 sleep_time = 10;
237 } else {
238 sleep_time = 60;
239 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000240 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000241 t1 = t2;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000242 }
243 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000244 return 0; /* not reached */
Glenn L McGrathb89fcd42003-12-23 07:21:33 +0000245}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000246
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000247#if SETENV_LEAKS
248/* We set environment *before* vfork (because we want to use vfork),
249 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
250 * Using putenv(), and freeing memory after unsetenv() won't leak */
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000251static void safe_setenv(char **pvar_val, const char *var, const char *val)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000252{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000253 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000254
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000255 if (var_val) {
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000256 bb_unsetenv(var_val);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000257 free(var_val);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000258 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000259 *pvar_val = xasprintf("%s=%s", var, val);
260 putenv(*pvar_val);
261}
262#endif
263
264static void SetEnv(struct passwd *pas)
265{
266#if SETENV_LEAKS
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000267 safe_setenv(&env_var_user, "USER", pas->pw_name);
268 safe_setenv(&env_var_home, "HOME", pas->pw_dir);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000269 /* if we want to set user's shell instead: */
Denis Vlasenko76ddc2e2008-12-30 05:05:31 +0000270 /*safe_setenv(env_var_user, "SHELL", pas->pw_shell);*/
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000271#else
Denis Vlasenkob8d1a4c2008-09-20 16:28:59 +0000272 xsetenv("USER", pas->pw_name);
273 xsetenv("HOME", pas->pw_dir);
Eric Andersen35e643b2003-07-28 07:40:39 +0000274#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000275 /* currently, we use constant one: */
276 /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000277}
278
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000279static void ChangeUser(struct passwd *pas)
280{
281 /* careful: we're after vfork! */
282 change_identity(pas); /* - initgroups, setgid, setuid */
283 if (chdir(pas->pw_dir) < 0) {
284 crondlog(LVL9 "can't chdir(%s)", pas->pw_dir);
285 if (chdir(TMPDIR) < 0) {
286 crondlog(DIE9 "can't chdir(%s)", TMPDIR); /* exits */
287 }
288 }
289}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000290
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000291static const char DowAry[] ALIGN1 =
292 "sun""mon""tue""wed""thu""fri""sat"
293 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
294;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000295
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000296static const char MonAry[] ALIGN1 =
297 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
298 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
299;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000300
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000301static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000302 const char *names, char *ptr)
303/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000304{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000305 char *base = ptr;
306 int n1 = -1;
307 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000308
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000309 // this can't happen due to config_read()
310 /*if (base == NULL)
311 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000312
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000313 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000314 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000315
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000316 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000317 if (*ptr == '*') {
318 n1 = 0; /* everything will be filled */
319 n2 = modvalue - 1;
320 skip = 1;
321 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000322 } else if (isdigit(*ptr)) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200323 char *endp;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000324 if (n1 < 0) {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200325 n1 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000326 } else {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200327 n2 = strtol(ptr, &endp, 10) + off;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000328 }
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200329 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000330 skip = 1;
331 } else if (names) {
332 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000333
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000334 for (i = 0; names[i]; i += 3) {
335 /* was using strncmp before... */
336 if (strncasecmp(ptr, &names[i], 3) == 0) {
337 ptr += 3;
338 if (n1 < 0) {
339 n1 = i / 3;
340 } else {
341 n2 = i / 3;
342 }
343 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000344 break;
345 }
346 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000347 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000348
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000349 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000350 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000351 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000352 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000353 if (*ptr == '-' && n2 < 0) {
354 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000355 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000356 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000357
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000358 /*
359 * collapse single-value ranges, handle skipmark, and fill
360 * in the character array appropriately.
361 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000362 if (n2 < 0) {
363 n2 = n1;
364 }
365 if (*ptr == '/') {
Denys Vlasenko1f27ab02009-09-23 17:17:53 +0200366 char *endp;
367 skip = strtol(ptr + 1, &endp, 10);
368 ptr = endp; /* gcc likes temp var for &endp */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000369 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000370
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000371 /*
372 * fill array, using a failsafe is the easiest way to prevent
373 * an endless loop
374 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000375 {
376 int s0 = 1;
377 int failsafe = 1024;
378
379 --n1;
380 do {
381 n1 = (n1 + 1) % modvalue;
382
383 if (--s0 == 0) {
384 ary[n1 % modvalue] = 1;
385 s0 = skip;
386 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000387 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000388 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000389 }
390 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000391
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000392 }
393 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000394 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000395 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000396 ++ptr;
397 n1 = -1;
398 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000399 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000400
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000401 if (*ptr) {
402 err:
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000403 crondlog(WARN9 "user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000404 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000405 }
406
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000407 if (DebugOpt && (LogLevel <= 5)) { /* like LVL5 */
408 /* can't use crondlog, it inserts '\n' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000409 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000410 for (i = 0; i < modvalue; ++i)
411 fprintf(stderr, "%d", (unsigned char)ary[i]);
412 fputc('\n', stderr);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000413 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000414}
415
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000416static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000417{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000418 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000419 int weekUsed = 0;
420 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000421
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000422 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000423 if (line->cl_Dow[i] == 0) {
424 weekUsed = 1;
425 break;
426 }
427 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000428 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000429 if (line->cl_Days[i] == 0) {
430 daysUsed = 1;
431 break;
432 }
433 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000434 if (weekUsed != daysUsed) {
435 if (weekUsed)
436 memset(line->cl_Days, 0, sizeof(line->cl_Days));
437 else /* daysUsed */
438 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000439 }
440}
441
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000442static void SynchronizeFile(const char *fileName)
443{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000444 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000445 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000446 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000447 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000448#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
449 char *mailTo = NULL;
450#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000451
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000452 if (!fileName)
453 return;
454
455 DeleteFile(fileName);
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000456 parser = config_open(fileName);
457 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000458 return;
459
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000460 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000461
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000462 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000463 CronFile *file = xzalloc(sizeof(CronFile));
464 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000465 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000466
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000467 file->cf_User = xstrdup(fileName);
468 pline = &file->cf_LineBase;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000469
Denis Vlasenko084266e2008-07-26 23:08:31 +0000470 while (1) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000471 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000472
Denis Vlasenko084266e2008-07-26 23:08:31 +0000473 if (!--maxLines)
474 break;
475 n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
476 if (!n)
477 break;
478
Denis Vlasenkodcb3fcb2008-07-19 22:57:00 +0000479 if (DebugOpt)
480 crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000481
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000482 /* check if line is setting MAILTO= */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000483 if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000484#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
485 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000486 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000487#endif /* otherwise just ignore such lines */
488 continue;
489 }
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000490 /* check if a minimum of tokens is specified */
Denis Vlasenko2e157dd2008-07-19 09:27:19 +0000491 if (n < 6)
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000492 continue;
Denis Vlasenko084266e2008-07-26 23:08:31 +0000493 *pline = line = xzalloc(sizeof(*line));
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000494 /* parse date ranges */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000495 ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, tokens[0]);
496 ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, tokens[1]);
497 ParseField(file->cf_User, line->cl_Days, 32, 0, NULL, tokens[2]);
498 ParseField(file->cf_User, line->cl_Mons, 12, -1, MonAry, tokens[3]);
499 ParseField(file->cf_User, line->cl_Dow, 7, 0, DowAry, tokens[4]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000500 /*
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000501 * fix days and dow - if one is not "*" and the other
502 * is "*", the other is set to 0, and vise-versa
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000503 */
504 FixDayDow(line);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000505#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
506 /* copy mailto (can be NULL) */
507 line->cl_MailTo = xstrdup(mailTo);
508#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000509 /* copy command */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000510 line->cl_Shell = xstrdup(tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000511 if (DebugOpt) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000512 crondlog(LVL5 " command:%s", tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000513 }
514 pline = &line->cl_Next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000515//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 +0000516 }
517 *pline = NULL;
518
519 file->cf_Next = FileBase;
520 FileBase = file;
521
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000522 if (maxLines == 0) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000523 crondlog(WARN9 "user %s: too many lines", fileName);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000524 }
525 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000526 config_close(parser);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000527}
528
529static void CheckUpdates(void)
530{
531 FILE *fi;
532 char buf[256];
533
Denis Vlasenko5415c852008-07-21 23:05:26 +0000534 fi = fopen_for_read(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000535 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000536 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000537 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000538 /* use first word only */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000539 SynchronizeFile(strtok(buf, " \t\r\n"));
540 }
541 fclose(fi);
542 }
543}
544
545static void SynchronizeDir(void)
546{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000547 CronFile *file;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000548 /* Attempt to delete the database. */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000549 again:
550 for (file = FileBase; file; file = file->cf_Next) {
551 if (!file->cf_Deleted) {
552 DeleteFile(file->cf_User);
553 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000554 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000555 }
556
557 /*
558 * Remove cron update file
559 *
560 * Re-chdir, in case directory was renamed & deleted, or otherwise
561 * screwed up.
562 *
563 * scan directory and add associated users
564 */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000565 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000566 if (chdir(CDir) < 0) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000567 crondlog(DIE9 "can't chdir(%s)", CDir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000568 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000569 {
570 DIR *dir = opendir(".");
571 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000572
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000573 if (!dir)
574 crondlog(DIE9 "can't chdir(%s)", "."); /* exits */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000575 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000576 if (strchr(den->d_name, '.') != NULL) {
577 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000578 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000579 if (getpwnam(den->d_name)) {
580 SynchronizeFile(den->d_name);
581 } else {
582 crondlog(LVL7 "ignoring %s", den->d_name);
583 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000584 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000585 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000586 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000587}
588
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000589/*
590 * DeleteFile() - delete user database
591 *
592 * Note: multiple entries for same user may exist if we were unable to
593 * completely delete a database due to running processes.
594 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000595static void DeleteFile(const char *userName)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000596{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000597 CronFile **pfile = &FileBase;
598 CronFile *file;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000599
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000600 while ((file = *pfile) != NULL) {
601 if (strcmp(userName, file->cf_User) == 0) {
602 CronLine **pline = &file->cf_LineBase;
603 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000604
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000605 file->cf_Running = 0;
606 file->cf_Deleted = 1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000607
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000608 while ((line = *pline) != NULL) {
609 if (line->cl_Pid > 0) {
610 file->cf_Running = 1;
611 pline = &line->cl_Next;
612 } else {
613 *pline = line->cl_Next;
614 free(line->cl_Shell);
615 free(line);
616 }
617 }
618 if (file->cf_Running == 0) {
619 *pfile = file->cf_Next;
620 free(file->cf_User);
621 free(file);
622 } else {
623 pfile = &file->cf_Next;
624 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000625 } else {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000626 pfile = &file->cf_Next;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000627 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000628 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000629}
630
631/*
632 * TestJobs()
633 *
634 * determine which jobs need to be run. Under normal conditions, the
635 * period is about a minute (one scan). Worst case it will be one
636 * hour (60 scans).
637 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000638static int TestJobs(time_t t1, time_t t2)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000639{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000640 int nJobs = 0;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000641 time_t t;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000642
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000643 /* Find jobs > t1 and <= t2 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000644
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000645 for (t = t1 - t1 % 60; t <= t2; t += 60) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000646 struct tm *tp;
647 CronFile *file;
648 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000649
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000650 if (t <= t1)
651 continue;
652
653 tp = localtime(&t);
654 for (file = FileBase; file; file = file->cf_Next) {
655 if (DebugOpt)
656 crondlog(LVL5 "file %s:", file->cf_User);
657 if (file->cf_Deleted)
658 continue;
659 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000660 if (DebugOpt)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000661 crondlog(LVL5 " line %s", line->cl_Shell);
662 if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour]
663 && (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
664 && line->cl_Mons[tp->tm_mon]
665 ) {
666 if (DebugOpt) {
667 crondlog(LVL5 " job: %d %s",
668 (int)line->cl_Pid, line->cl_Shell);
669 }
670 if (line->cl_Pid > 0) {
671 crondlog(LVL8 "user %s: process already running: %s",
672 file->cf_User, line->cl_Shell);
673 } else if (line->cl_Pid == 0) {
674 line->cl_Pid = -1;
675 file->cf_Ready = 1;
676 ++nJobs;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000677 }
678 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000679 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000680 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000681 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000682 return nJobs;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000683}
684
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000685static void RunJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000686{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000687 CronFile *file;
688 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000689
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000690 for (file = FileBase; file; file = file->cf_Next) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000691 if (!file->cf_Ready)
692 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000693
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000694 file->cf_Ready = 0;
695 for (line = file->cf_LineBase; line; line = line->cl_Next) {
696 if (line->cl_Pid >= 0)
697 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000698
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000699 RunJob(file->cf_User, line);
700 crondlog(LVL8 "USER %s pid %3d cmd %s",
701 file->cf_User, (int)line->cl_Pid, line->cl_Shell);
702 if (line->cl_Pid < 0) {
703 file->cf_Ready = 1;
704 } else if (line->cl_Pid > 0) {
705 file->cf_Running = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000706 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000707 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000708 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000709}
710
711/*
712 * CheckJobs() - check for job completion
713 *
714 * Check for job completion, return number of jobs still running after
715 * all done.
716 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000717static int CheckJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000718{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000719 CronFile *file;
720 CronLine *line;
721 int nStillRunning = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000722
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000723 for (file = FileBase; file; file = file->cf_Next) {
724 if (file->cf_Running) {
725 file->cf_Running = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000726
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000727 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000728 int status, r;
729 if (line->cl_Pid <= 0)
730 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000731
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000732 r = waitpid(line->cl_Pid, &status, WNOHANG);
733 if (r < 0 || r == line->cl_Pid) {
734 EndJob(file->cf_User, line);
735 if (line->cl_Pid) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000736 file->cf_Running = 1;
737 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000738 } else if (r == 0) {
739 file->cf_Running = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000740 }
741 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000742 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000743 nStillRunning += file->cf_Running;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000744 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000745 return nStillRunning;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000746}
747
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000748#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000749
750// TODO: sendmail should be _run-time_ option, not compile-time!
751
Eric Andersen35e643b2003-07-28 07:40:39 +0000752static void
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000753ForkJob(const char *user, CronLine *line, int mailFd,
Denis Vlasenko314820e2008-01-24 01:33:12 +0000754 const char *prog, const char *cmd, const char *arg,
755 const char *mail_filename)
Eric Andersen35e643b2003-07-28 07:40:39 +0000756{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000757 struct passwd *pas;
758 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000759
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000760 /* prepare things before vfork */
761 pas = getpwnam(user);
762 if (!pas) {
763 crondlog(LVL9 "can't get uid for %s", user);
764 goto err;
765 }
766 SetEnv(pas);
767
768 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000769 if (pid == 0) {
770 /* CHILD */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000771 /* change running state to the user in question */
772 ChangeUser(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000773 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000774 crondlog(LVL5 "child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000775 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000776 if (mailFd >= 0) {
Denis Vlasenko314820e2008-01-24 01:33:12 +0000777 xmove_fd(mailFd, mail_filename ? 1 : 0);
778 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000779 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000780 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000781 bb_setpgrp();
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000782 execlp(prog, prog, cmd, arg, (char *) NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000783 crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000784 if (mail_filename) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000785 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
786 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000787 _exit(EXIT_SUCCESS);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000788 }
789
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000790 line->cl_Pid = pid;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000791 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000792 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000793 crondlog(ERR20 "can't vfork");
794 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000795 line->cl_Pid = 0;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000796 if (mail_filename) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000797 unlink(mail_filename);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000798 }
Denis Vlasenko314820e2008-01-24 01:33:12 +0000799 } else if (mail_filename) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000800 /* PARENT, FORK SUCCESS
801 * rename mail-file based on pid of process
802 */
803 char mailFile2[128];
804
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000805 snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000806 rename(mail_filename, mailFile2); // TODO: xrename?
Eric Andersen35e643b2003-07-28 07:40:39 +0000807 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000808
Eric Andersen35e643b2003-07-28 07:40:39 +0000809 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000810 * Close the mail file descriptor.. we can't just leave it open in
811 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000812 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000813 if (mailFd >= 0) {
814 close(mailFd);
815 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000816}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000817
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000818static void RunJob(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000819{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000820 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000821 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000822
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000823 line->cl_Pid = 0;
824 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000825
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000826 if (line->cl_MailTo) {
827 /* open mail file - owner root so nobody can screw with it. */
828 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid());
829 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000830
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000831 if (mailFd >= 0) {
832 line->cl_MailFlag = 1;
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000833 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_MailTo,
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000834 line->cl_Shell);
835 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
836 } else {
837 crondlog(ERR20 "cannot create mail file %s for user %s, "
838 "discarding output", mailFile, user);
839 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000840 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000841
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000842 ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000843}
844
845/*
846 * EndJob - called when job terminates and when mail terminates
847 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000848static void EndJob(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000849{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000850 int mailFd;
851 char mailFile[128];
852 struct stat sbuf;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000853
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000854 /* No job */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000855 if (line->cl_Pid <= 0) {
856 line->cl_Pid = 0;
857 return;
858 }
859
860 /*
861 * End of job and no mail file
862 * End of sendmail job
863 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000864 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, line->cl_Pid);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000865 line->cl_Pid = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000866
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000867 if (line->cl_MailFlag == 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000868 return;
869 }
870 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000871
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000872 /*
873 * End of primary job - check for mail file. If size has increased and
874 * the file is still valid, we sendmail it.
875 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000876 mailFd = open(mailFile, O_RDONLY);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000877 unlink(mailFile);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000878 if (mailFd < 0) {
879 return;
880 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000881
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +0000882 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid
883 || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos
884 || !S_ISREG(sbuf.st_mode)
885 ) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000886 close(mailFd);
887 return;
888 }
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000889 if (line->cl_MailTo)
890 ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
Eric Andersen35e643b2003-07-28 07:40:39 +0000891}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000892
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000893#else /* crond without sendmail */
894
895static void RunJob(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000896{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000897 struct passwd *pas;
898 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000899
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000900 /* prepare things before vfork */
901 pas = getpwnam(user);
902 if (!pas) {
903 crondlog(LVL9 "can't get uid for %s", user);
904 goto err;
905 }
906 SetEnv(pas);
907
908 /* fork as the user in question and run program */
909 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000910 if (pid == 0) {
911 /* CHILD */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000912 /* change running state to the user in question */
913 ChangeUser(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000914 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000915 crondlog(LVL5 "child running %s", DEFAULT_SHELL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000916 }
Denis Vlasenko3f70b872008-12-04 13:57:59 +0000917 /* crond 3.0pl1-100 puts tasks in separate process groups */
Denis Vlasenko6ebb2f52008-12-03 10:46:12 +0000918 bb_setpgrp();
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000919 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, (char *) NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000920 crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user,
921 DEFAULT_SHELL, "-c", line->cl_Shell);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000922 _exit(EXIT_SUCCESS);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000923 }
924 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000925 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000926 crondlog(ERR20 "can't vfork");
927 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000928 pid = 0;
929 }
930 line->cl_Pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000931}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000932
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000933#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */