blob: d8423cf4f814b2b31da685c49dacc68a5733a35d [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
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000026#ifndef CRONTABS
27#define CRONTABS "/var/spool/cron/crontabs"
28#endif
29#ifndef TMPDIR
30#define TMPDIR "/var/spool/cron"
31#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000032#ifndef SENDMAIL
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000033#define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000034#endif
35#ifndef SENDMAIL_ARGS
Denis Vlasenko6ea75e22008-06-28 21:46:41 +000036#define SENDMAIL_ARGS "-ti", "oem"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000037#endif
38#ifndef CRONUPDATE
39#define CRONUPDATE "cron.update"
40#endif
41#ifndef MAXLINES
Glenn L McGrath9079ad02004-02-22 04:44:21 +000042#define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000043#endif
44
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000045
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000046typedef struct CronFile {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000047 struct CronFile *cf_Next;
48 struct CronLine *cf_LineBase;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000049 char *cf_User; /* username */
50 smallint cf_Ready; /* bool: one or more jobs ready */
51 smallint cf_Running; /* bool: one or more jobs running */
52 smallint cf_Deleted; /* marked for deletion, ignore */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000053} CronFile;
54
55typedef struct CronLine {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000056 struct CronLine *cl_Next;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000057 char *cl_Shell; /* shell command */
58 pid_t cl_Pid; /* running pid, 0, or armed (-1) */
59#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
60 int cl_MailPos; /* 'empty file' size */
61 smallint cl_MailFlag; /* running pid is for mail */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +000062 char *cl_MailTo; /* whom to mail results */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000063#endif
64 /* ordered by size, not in natural order. makes code smaller: */
65 char cl_Dow[7]; /* 0-6, beginning sunday */
66 char cl_Mons[12]; /* 0-11 */
67 char cl_Hrs[24]; /* 0-23 */
68 char cl_Days[32]; /* 1-31 */
69 char cl_Mins[60]; /* 0-59 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000070} CronLine;
71
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000072
73#define DaemonUid 0
74
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000075
76enum {
77 OPT_l = (1 << 0),
78 OPT_L = (1 << 1),
79 OPT_f = (1 << 2),
80 OPT_b = (1 << 3),
81 OPT_S = (1 << 4),
82 OPT_c = (1 << 5),
83 OPT_d = (1 << 6) * ENABLE_DEBUG_CROND_OPTION,
84};
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000085#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000086#define DebugOpt (option_mask32 & OPT_d)
87#else
88#define DebugOpt 0
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000089#endif
90
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000091
Denis Vlasenko4e6c8122008-03-12 22:10:25 +000092struct globals {
93 unsigned LogLevel; /* = 8; */
94 const char *LogFile;
95 const char *CDir; /* = CRONTABS; */
96 CronFile *FileBase;
97#if SETENV_LEAKS
98 char *env_var_user;
99 char *env_var_home;
100#endif
101};
102#define G (*(struct globals*)&bb_common_bufsiz1)
103#define LogLevel (G.LogLevel )
104#define LogFile (G.LogFile )
105#define CDir (G.CDir )
106#define FileBase (G.FileBase )
107#define env_var_user (G.env_var_user )
108#define env_var_home (G.env_var_home )
109#define INIT_G() do { \
110 LogLevel = 8; \
111 CDir = CRONTABS; \
112} while (0)
113
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000114
115static void CheckUpdates(void);
116static void SynchronizeDir(void);
117static int TestJobs(time_t t1, time_t t2);
118static void RunJobs(void);
119static int CheckJobs(void);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000120static void RunJob(const char *user, CronLine *line);
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000121#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000122static void EndJob(const char *user, CronLine *line);
Eric Andersen35e643b2003-07-28 07:40:39 +0000123#else
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000124#define EndJob(user, line) ((line)->cl_Pid = 0)
Eric Andersen35e643b2003-07-28 07:40:39 +0000125#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000126static void DeleteFile(const char *userName);
127
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000128
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000129#define LVL5 "\x05"
130#define LVL7 "\x07"
131#define LVL8 "\x08"
132#define LVL9 "\x09"
133#define WARN9 "\x49"
134#define DIE9 "\xc9"
135/* level >= 20 is "error" */
136#define ERR20 "\x14"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000137
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000138static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +0000139{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000140 va_list va;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000141 int level = (ctl[0] & 0x1f);
Eric Andersen35e643b2003-07-28 07:40:39 +0000142
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000143 va_start(va, ctl);
Denis Vlasenko77ad97f2008-05-13 02:27:31 +0000144 if (level >= (int)LogLevel) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000145 /* Debug mode: all to (non-redirected) stderr, */
146 /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
147 if (!DebugOpt && LogFile) {
148 /* Otherwise (log to file): we reopen log file at every write: */
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000149 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000150 if (logfd >= 0)
151 xmove_fd(logfd, STDERR_FILENO);
Eric Andersen35e643b2003-07-28 07:40:39 +0000152 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000153// TODO: ERR -> error, WARN -> warning, LVL -> info
154 bb_verror_msg(ctl + 1, va, /* strerr: */ NULL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000155 }
156 va_end(va);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000157 if (ctl[0] & 0x80)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000158 exit(20);
Eric Andersen35e643b2003-07-28 07:40:39 +0000159}
160
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000161int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000162int crond_main(int argc UNUSED_PARAM, char **argv)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000163{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000164 unsigned opt;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000165
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000166 INIT_G();
167
168 /* "-b after -f is ignored", and so on for every pair a-b */
169 opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l")
170 ":l+:d+"; /* -l and -d have numeric param */
Denis Vlasenkob44c7902008-03-17 09:29:43 +0000171 opt = getopt32(argv, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000172 &LogLevel, &LogFile, &CDir
173 USE_DEBUG_CROND_OPTION(,&LogLevel));
174 /* both -d N and -l N set the same variable: LogLevel */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000175
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000176 if (!(opt & OPT_f)) {
177 /* close stdin, stdout, stderr.
178 * close unused descriptors - don't need them. */
Denis Vlasenkoc52248e2008-03-20 14:04:30 +0000179 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000180 }
181
182 if (!DebugOpt && LogFile == NULL) {
183 /* logging to syslog */
184 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
185 logmode = LOGMODE_SYSLOG;
186 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000187
Denis Vlasenko5a142022007-03-26 13:20:54 +0000188 xchdir(CDir);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000189 //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
190 setenv("SHELL", DEFAULT_SHELL, 1); /* once, for all future children */
191 crondlog(LVL9 "crond (busybox "BB_VER") started, log level %d", LogLevel);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000192 SynchronizeDir();
193
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000194 /* main loop - synchronize to 1 second after the minute, minimum sleep
195 * of 1 second. */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000196 {
197 time_t t1 = time(NULL);
198 time_t t2;
199 long dt;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000200 int rescan = 60;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000201 int sleep_time = 60;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000202
Denis Vlasenko10457b92007-03-27 22:01:31 +0000203 write_pidfile("/var/run/crond.pid");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000204 for (;;) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000205 sleep((sleep_time + 1) - (time(NULL) % sleep_time));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000206
207 t2 = time(NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000208 dt = (long)t2 - (long)t1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000209
210 /*
211 * The file 'cron.update' is checked to determine new cron
212 * jobs. The directory is rescanned once an hour to deal
213 * with any screwups.
214 *
215 * check for disparity. Disparities over an hour either way
216 * result in resynchronization. A reverse-indexed disparity
217 * less then an hour causes us to effectively sleep until we
218 * match the original time (i.e. no re-execution of jobs that
219 * have just been run). A forward-indexed disparity less then
220 * an hour causes intermediate jobs to be run, but only once
221 * in the worst case.
222 *
223 * when running jobs, the inequality used is greater but not
224 * equal to t1, and less then or equal to t2.
225 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000226 if (--rescan == 0) {
227 rescan = 60;
228 SynchronizeDir();
229 }
230 CheckUpdates();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000231 if (DebugOpt)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000232 crondlog(LVL5 "wakeup dt=%ld", dt);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000233 if (dt < -60 * 60 || dt > 60 * 60) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000234 crondlog(WARN9 "time disparity of %d minutes detected", dt / 60);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000235 } else if (dt > 0) {
236 TestJobs(t1, t2);
237 RunJobs();
238 sleep(5);
239 if (CheckJobs() > 0) {
240 sleep_time = 10;
241 } else {
242 sleep_time = 60;
243 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000244 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000245 t1 = t2;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000246 }
247 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000248 return 0; /* not reached */
Glenn L McGrathb89fcd42003-12-23 07:21:33 +0000249}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000250
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000251#if SETENV_LEAKS
252/* We set environment *before* vfork (because we want to use vfork),
253 * so we cannot use setenv() - repeated calls to setenv() may leak memory!
254 * Using putenv(), and freeing memory after unsetenv() won't leak */
255static void safe_setenv4(char **pvar_val, const char *var, const char *val /*, int len*/)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000256{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000257 const int len = 4; /* both var names are 4 char long */
258 char *var_val = *pvar_val;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000259
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000260 if (var_val) {
261 var_val[len] = '\0'; /* nuke '=' */
262 unsetenv(var_val);
263 free(var_val);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000264 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000265 *pvar_val = xasprintf("%s=%s", var, val);
266 putenv(*pvar_val);
267}
268#endif
269
270static void SetEnv(struct passwd *pas)
271{
272#if SETENV_LEAKS
273 safe_setenv4(&env_var_user, "USER", pas->pw_name);
274 safe_setenv4(&env_var_home, "HOME", pas->pw_dir);
275 /* if we want to set user's shell instead: */
276 /*safe_setenv(env_var_user, "SHELL", pas->pw_shell, 5);*/
277#else
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000278 setenv("USER", pas->pw_name, 1);
279 setenv("HOME", pas->pw_dir, 1);
Eric Andersen35e643b2003-07-28 07:40:39 +0000280#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000281 /* currently, we use constant one: */
282 /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000283}
284
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000285static void ChangeUser(struct passwd *pas)
286{
287 /* careful: we're after vfork! */
288 change_identity(pas); /* - initgroups, setgid, setuid */
289 if (chdir(pas->pw_dir) < 0) {
290 crondlog(LVL9 "can't chdir(%s)", pas->pw_dir);
291 if (chdir(TMPDIR) < 0) {
292 crondlog(DIE9 "can't chdir(%s)", TMPDIR); /* exits */
293 }
294 }
295}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000296
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000297static const char DowAry[] ALIGN1 =
298 "sun""mon""tue""wed""thu""fri""sat"
299 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
300;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000301
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000302static const char MonAry[] ALIGN1 =
303 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
304 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
305;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000306
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000307static void ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000308 const char *names, char *ptr)
309/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000310{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000311 char *base = ptr;
312 int n1 = -1;
313 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000314
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000315 // this can't happen due to config_read()
316 /*if (base == NULL)
317 return;*/
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000318
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000319 while (1) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000320 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000321
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000322 /* Handle numeric digit or symbol or '*' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000323 if (*ptr == '*') {
324 n1 = 0; /* everything will be filled */
325 n2 = modvalue - 1;
326 skip = 1;
327 ++ptr;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000328 } else if (isdigit(*ptr)) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000329 if (n1 < 0) {
330 n1 = strtol(ptr, &ptr, 10) + off;
331 } else {
332 n2 = strtol(ptr, &ptr, 10) + off;
333 }
334 skip = 1;
335 } else if (names) {
336 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000337
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000338 for (i = 0; names[i]; i += 3) {
339 /* was using strncmp before... */
340 if (strncasecmp(ptr, &names[i], 3) == 0) {
341 ptr += 3;
342 if (n1 < 0) {
343 n1 = i / 3;
344 } else {
345 n2 = i / 3;
346 }
347 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000348 break;
349 }
350 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000351 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000352
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000353 /* handle optional range '-' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000354 if (skip == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000355 goto err;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000356 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000357 if (*ptr == '-' && n2 < 0) {
358 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000359 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000360 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000361
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000362 /*
363 * collapse single-value ranges, handle skipmark, and fill
364 * in the character array appropriately.
365 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000366 if (n2 < 0) {
367 n2 = n1;
368 }
369 if (*ptr == '/') {
370 skip = strtol(ptr + 1, &ptr, 10);
371 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000372
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000373 /*
374 * fill array, using a failsafe is the easiest way to prevent
375 * an endless loop
376 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000377 {
378 int s0 = 1;
379 int failsafe = 1024;
380
381 --n1;
382 do {
383 n1 = (n1 + 1) % modvalue;
384
385 if (--s0 == 0) {
386 ary[n1 % modvalue] = 1;
387 s0 = skip;
388 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000389 if (--failsafe == 0) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000390 goto err;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000391 }
392 } while (n1 != n2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000393
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000394 }
395 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000396 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000397 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000398 ++ptr;
399 n1 = -1;
400 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000401 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000402
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000403 if (*ptr) {
404 err:
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000405 crondlog(WARN9 "user %s: parse error at %s", user, base);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000406 return;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000407 }
408
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000409 if (DebugOpt && (LogLevel <= 5)) { /* like LVL5 */
410 /* can't use crondlog, it inserts '\n' */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000411 int i;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000412 for (i = 0; i < modvalue; ++i)
413 fprintf(stderr, "%d", (unsigned char)ary[i]);
414 fputc('\n', stderr);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000415 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000416}
417
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000418static void FixDayDow(CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000419{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000420 unsigned i;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000421 int weekUsed = 0;
422 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000423
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000424 for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000425 if (line->cl_Dow[i] == 0) {
426 weekUsed = 1;
427 break;
428 }
429 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000430 for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000431 if (line->cl_Days[i] == 0) {
432 daysUsed = 1;
433 break;
434 }
435 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000436 if (weekUsed != daysUsed) {
437 if (weekUsed)
438 memset(line->cl_Days, 0, sizeof(line->cl_Days));
439 else /* daysUsed */
440 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000441 }
442}
443
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000444static void SynchronizeFile(const char *fileName)
445{
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000446 struct parser_t *parser;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000447 struct stat sbuf;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000448 int maxLines;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000449 char *tokens[6];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000450#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
451 char *mailTo = NULL;
452#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000453
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000454 if (!fileName)
455 return;
456
457 DeleteFile(fileName);
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000458 parser = config_open(fileName);
459 if (!parser)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000460 return;
461
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000462 maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000463
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000464 if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000465 CronFile *file = xzalloc(sizeof(CronFile));
466 CronLine **pline;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000467 int n;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000468
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000469 file->cf_User = xstrdup(fileName);
470 pline = &file->cf_LineBase;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000471
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000472 while (--maxLines && (n=config_read(parser, tokens, 6, 0, " \t", '#')) >= 0) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000473 CronLine *line;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000474
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000475 if (DebugOpt) {
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000476 crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000477 }
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000478
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000479 /* check if line is setting MAILTO= */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000480 if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000481#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
482 free(mailTo);
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000483 mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000484#endif /* otherwise just ignore such lines */
485 continue;
486 }
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000487 /* check if a minimum of tokens is specified */
488 if (n < 5)
489 continue;
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000490 *pline = line = xzalloc(sizeof(CronLine));
491 /* parse date ranges */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000492 ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, tokens[0]);
493 ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, tokens[1]);
494 ParseField(file->cf_User, line->cl_Days, 32, 0, NULL, tokens[2]);
495 ParseField(file->cf_User, line->cl_Mons, 12, -1, MonAry, tokens[3]);
496 ParseField(file->cf_User, line->cl_Dow, 7, 0, DowAry, tokens[4]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000497 /*
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000498 * fix days and dow - if one is not "*" and the other
499 * is "*", the other is set to 0, and vise-versa
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000500 */
501 FixDayDow(line);
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000502#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
503 /* copy mailto (can be NULL) */
504 line->cl_MailTo = xstrdup(mailTo);
505#endif
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000506 /* copy command */
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000507 line->cl_Shell = xstrdup(tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000508 if (DebugOpt) {
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000509 crondlog(LVL5 " command:%s", tokens[5]);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000510 }
511 pline = &line->cl_Next;
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000512//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 +0000513 }
514 *pline = NULL;
515
516 file->cf_Next = FileBase;
517 FileBase = file;
518
Denis Vlasenkoc01340f2008-07-16 22:12:18 +0000519 if (maxLines == 0) {
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000520 crondlog(WARN9 "user %s: too many lines", fileName);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000521 }
522 }
Bernhard Reutner-Fischer67921282008-07-17 11:59:13 +0000523 config_close(parser);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000524}
525
526static void CheckUpdates(void)
527{
528 FILE *fi;
529 char buf[256];
530
531 fi = fopen(CRONUPDATE, "r");
532 if (fi != NULL) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000533 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000534 while (fgets(buf, sizeof(buf), fi) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000535 /* use first word only */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000536 SynchronizeFile(strtok(buf, " \t\r\n"));
537 }
538 fclose(fi);
539 }
540}
541
542static void SynchronizeDir(void)
543{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000544 CronFile *file;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000545 /* Attempt to delete the database. */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000546 again:
547 for (file = FileBase; file; file = file->cf_Next) {
548 if (!file->cf_Deleted) {
549 DeleteFile(file->cf_User);
550 goto again;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000551 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000552 }
553
554 /*
555 * Remove cron update file
556 *
557 * Re-chdir, in case directory was renamed & deleted, or otherwise
558 * screwed up.
559 *
560 * scan directory and add associated users
561 */
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000562 unlink(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000563 if (chdir(CDir) < 0) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000564 crondlog(DIE9 "can't chdir(%s)", CDir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000565 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000566 {
567 DIR *dir = opendir(".");
568 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000569
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000570 if (!dir)
571 crondlog(DIE9 "can't chdir(%s)", "."); /* exits */
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000572 while ((den = readdir(dir)) != NULL) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000573 if (strchr(den->d_name, '.') != NULL) {
574 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000575 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000576 if (getpwnam(den->d_name)) {
577 SynchronizeFile(den->d_name);
578 } else {
579 crondlog(LVL7 "ignoring %s", den->d_name);
580 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000581 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000582 closedir(dir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000583 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000584}
585
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000586/*
587 * DeleteFile() - delete user database
588 *
589 * Note: multiple entries for same user may exist if we were unable to
590 * completely delete a database due to running processes.
591 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000592static void DeleteFile(const char *userName)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000593{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000594 CronFile **pfile = &FileBase;
595 CronFile *file;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000596
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000597 while ((file = *pfile) != NULL) {
598 if (strcmp(userName, file->cf_User) == 0) {
599 CronLine **pline = &file->cf_LineBase;
600 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000601
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000602 file->cf_Running = 0;
603 file->cf_Deleted = 1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000604
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000605 while ((line = *pline) != NULL) {
606 if (line->cl_Pid > 0) {
607 file->cf_Running = 1;
608 pline = &line->cl_Next;
609 } else {
610 *pline = line->cl_Next;
611 free(line->cl_Shell);
612 free(line);
613 }
614 }
615 if (file->cf_Running == 0) {
616 *pfile = file->cf_Next;
617 free(file->cf_User);
618 free(file);
619 } else {
620 pfile = &file->cf_Next;
621 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000622 } else {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000623 pfile = &file->cf_Next;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000624 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000625 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000626}
627
628/*
629 * TestJobs()
630 *
631 * determine which jobs need to be run. Under normal conditions, the
632 * period is about a minute (one scan). Worst case it will be one
633 * hour (60 scans).
634 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000635static int TestJobs(time_t t1, time_t t2)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000636{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000637 int nJobs = 0;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000638 time_t t;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000639
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000640 /* Find jobs > t1 and <= t2 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000641
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000642 for (t = t1 - t1 % 60; t <= t2; t += 60) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000643 struct tm *tp;
644 CronFile *file;
645 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000646
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000647 if (t <= t1)
648 continue;
649
650 tp = localtime(&t);
651 for (file = FileBase; file; file = file->cf_Next) {
652 if (DebugOpt)
653 crondlog(LVL5 "file %s:", file->cf_User);
654 if (file->cf_Deleted)
655 continue;
656 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000657 if (DebugOpt)
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000658 crondlog(LVL5 " line %s", line->cl_Shell);
659 if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour]
660 && (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
661 && line->cl_Mons[tp->tm_mon]
662 ) {
663 if (DebugOpt) {
664 crondlog(LVL5 " job: %d %s",
665 (int)line->cl_Pid, line->cl_Shell);
666 }
667 if (line->cl_Pid > 0) {
668 crondlog(LVL8 "user %s: process already running: %s",
669 file->cf_User, line->cl_Shell);
670 } else if (line->cl_Pid == 0) {
671 line->cl_Pid = -1;
672 file->cf_Ready = 1;
673 ++nJobs;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000674 }
675 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000676 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000677 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000678 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000679 return nJobs;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000680}
681
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000682static void RunJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000683{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000684 CronFile *file;
685 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000686
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000687 for (file = FileBase; file; file = file->cf_Next) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000688 if (!file->cf_Ready)
689 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000690
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000691 file->cf_Ready = 0;
692 for (line = file->cf_LineBase; line; line = line->cl_Next) {
693 if (line->cl_Pid >= 0)
694 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000695
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000696 RunJob(file->cf_User, line);
697 crondlog(LVL8 "USER %s pid %3d cmd %s",
698 file->cf_User, (int)line->cl_Pid, line->cl_Shell);
699 if (line->cl_Pid < 0) {
700 file->cf_Ready = 1;
701 } else if (line->cl_Pid > 0) {
702 file->cf_Running = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000703 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000704 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000705 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000706}
707
708/*
709 * CheckJobs() - check for job completion
710 *
711 * Check for job completion, return number of jobs still running after
712 * all done.
713 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000714static int CheckJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000715{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000716 CronFile *file;
717 CronLine *line;
718 int nStillRunning = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000719
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000720 for (file = FileBase; file; file = file->cf_Next) {
721 if (file->cf_Running) {
722 file->cf_Running = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000723
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000724 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000725 int status, r;
726 if (line->cl_Pid <= 0)
727 continue;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000728
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000729 r = waitpid(line->cl_Pid, &status, WNOHANG);
730 if (r < 0 || r == line->cl_Pid) {
731 EndJob(file->cf_User, line);
732 if (line->cl_Pid) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000733 file->cf_Running = 1;
734 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000735 } else if (r == 0) {
736 file->cf_Running = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000737 }
738 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000739 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000740 nStillRunning += file->cf_Running;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000741 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000742 return nStillRunning;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000743}
744
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000745#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000746
747// TODO: sendmail should be _run-time_ option, not compile-time!
748
Eric Andersen35e643b2003-07-28 07:40:39 +0000749static void
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000750ForkJob(const char *user, CronLine *line, int mailFd,
Denis Vlasenko314820e2008-01-24 01:33:12 +0000751 const char *prog, const char *cmd, const char *arg,
752 const char *mail_filename)
Eric Andersen35e643b2003-07-28 07:40:39 +0000753{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000754 struct passwd *pas;
755 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000756
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000757 /* prepare things before vfork */
758 pas = getpwnam(user);
759 if (!pas) {
760 crondlog(LVL9 "can't get uid for %s", user);
761 goto err;
762 }
763 SetEnv(pas);
764
765 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000766 if (pid == 0) {
767 /* CHILD */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000768 /* change running state to the user in question */
769 ChangeUser(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000770 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000771 crondlog(LVL5 "child running %s", prog);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000772 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000773 if (mailFd >= 0) {
Denis Vlasenko314820e2008-01-24 01:33:12 +0000774 xmove_fd(mailFd, mail_filename ? 1 : 0);
775 dup2(1, 2);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000776 }
Denis Vlasenko0a656922008-06-30 15:48:34 +0000777 execlp(prog, prog, cmd, arg, NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000778 crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000779 if (mail_filename) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000780 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
781 }
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000782 _exit(EXIT_SUCCESS);
Denis Vlasenko314820e2008-01-24 01:33:12 +0000783 }
784
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000785 line->cl_Pid = pid;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000786 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000787 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000788 crondlog(ERR20 "can't vfork");
789 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000790 line->cl_Pid = 0;
Denis Vlasenko314820e2008-01-24 01:33:12 +0000791 if (mail_filename) {
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000792 unlink(mail_filename);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000793 }
Denis Vlasenko314820e2008-01-24 01:33:12 +0000794 } else if (mail_filename) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000795 /* PARENT, FORK SUCCESS
796 * rename mail-file based on pid of process
797 */
798 char mailFile2[128];
799
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000800 snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000801 rename(mail_filename, mailFile2); // TODO: xrename?
Eric Andersen35e643b2003-07-28 07:40:39 +0000802 }
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000803
Eric Andersen35e643b2003-07-28 07:40:39 +0000804 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000805 * Close the mail file descriptor.. we can't just leave it open in
806 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000807 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000808 if (mailFd >= 0) {
809 close(mailFd);
810 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000811}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000812
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000813static void RunJob(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000814{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000815 char mailFile[128];
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000816 int mailFd = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000817
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000818 line->cl_Pid = 0;
819 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000820
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000821 if (line->cl_MailTo) {
822 /* open mail file - owner root so nobody can screw with it. */
823 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid());
824 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000825
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000826 if (mailFd >= 0) {
827 line->cl_MailFlag = 1;
Denis Vlasenkoc94d3562008-06-30 13:30:21 +0000828 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_MailTo,
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000829 line->cl_Shell);
830 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
831 } else {
832 crondlog(ERR20 "cannot create mail file %s for user %s, "
833 "discarding output", mailFile, user);
834 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000835 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000836
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000837 ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000838}
839
840/*
841 * EndJob - called when job terminates and when mail terminates
842 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000843static void EndJob(const char *user, CronLine *line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000844{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000845 int mailFd;
846 char mailFile[128];
847 struct stat sbuf;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000848
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000849 /* No job */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000850 if (line->cl_Pid <= 0) {
851 line->cl_Pid = 0;
852 return;
853 }
854
855 /*
856 * End of job and no mail file
857 * End of sendmail job
858 */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000859 snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, line->cl_Pid);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000860 line->cl_Pid = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000861
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000862 if (line->cl_MailFlag == 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000863 return;
864 }
865 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000866
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000867 /*
868 * End of primary job - check for mail file. If size has increased and
869 * the file is still valid, we sendmail it.
870 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000871 mailFd = open(mailFile, O_RDONLY);
Denis Vlasenkocb448fe2008-02-17 14:28:53 +0000872 unlink(mailFile);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000873 if (mailFd < 0) {
874 return;
875 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000876
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +0000877 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid
878 || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos
879 || !S_ISREG(sbuf.st_mode)
880 ) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000881 close(mailFd);
882 return;
883 }
Denis Vlasenko6fa1ba32008-04-07 21:02:35 +0000884 if (line->cl_MailTo)
885 ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
Eric Andersen35e643b2003-07-28 07:40:39 +0000886}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000887
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000888#else /* crond without sendmail */
889
890static void RunJob(const char *user, CronLine *line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000891{
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000892 struct passwd *pas;
893 pid_t pid;
Eric Andersen35e643b2003-07-28 07:40:39 +0000894
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000895 /* prepare things before vfork */
896 pas = getpwnam(user);
897 if (!pas) {
898 crondlog(LVL9 "can't get uid for %s", user);
899 goto err;
900 }
901 SetEnv(pas);
902
903 /* fork as the user in question and run program */
904 pid = vfork();
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000905 if (pid == 0) {
906 /* CHILD */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000907 /* change running state to the user in question */
908 ChangeUser(pas);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000909 if (DebugOpt) {
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000910 crondlog(LVL5 "child running %s", DEFAULT_SHELL);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000911 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000912 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000913 crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user,
914 DEFAULT_SHELL, "-c", line->cl_Shell);
Bernhard Reutner-Fischer636a1f82008-05-19 09:29:47 +0000915 _exit(EXIT_SUCCESS);
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000916 }
917 if (pid < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000918 /* FORK FAILED */
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000919 crondlog(ERR20 "can't vfork");
920 err:
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000921 pid = 0;
922 }
923 line->cl_Pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000924}
Denis Vlasenko4e6c8122008-03-12 22:10:25 +0000925
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000926#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */