blob: fa7b3da08359a5898d994e0e44f8271030c0cfec [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
Rob Landleyd921b2e2006-08-03 15:41:12 +000014#include <sys/syslog.h>
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000015#include "libbb.h"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000016
17#ifndef CRONTABS
18#define CRONTABS "/var/spool/cron/crontabs"
19#endif
20#ifndef TMPDIR
21#define TMPDIR "/var/spool/cron"
22#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000023#ifndef SENDMAIL
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +000024#define SENDMAIL "sendmail"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000025#endif
26#ifndef SENDMAIL_ARGS
Eric Andersen35e643b2003-07-28 07:40:39 +000027#define SENDMAIL_ARGS "-ti", "oem"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000028#endif
29#ifndef CRONUPDATE
30#define CRONUPDATE "cron.update"
31#endif
32#ifndef MAXLINES
Glenn L McGrath9079ad02004-02-22 04:44:21 +000033#define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000034#endif
35
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000036typedef struct CronFile {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000037 struct CronFile *cf_Next;
38 struct CronLine *cf_LineBase;
39 char *cf_User; /* username */
40 int cf_Ready; /* bool: one or more jobs ready */
41 int cf_Running; /* bool: one or more jobs running */
42 int cf_Deleted; /* marked for deletion, ignore */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000043} CronFile;
44
45typedef struct CronLine {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000046 struct CronLine *cl_Next;
47 char *cl_Shell; /* shell command */
48 pid_t cl_Pid; /* running pid, 0, or armed (-1) */
49 int cl_MailFlag; /* running pid is for mail */
50 int cl_MailPos; /* 'empty file' size */
51 char cl_Mins[60]; /* 0-59 */
52 char cl_Hrs[24]; /* 0-23 */
53 char cl_Days[32]; /* 1-31 */
54 char cl_Mons[12]; /* 0-11 */
55 char cl_Dow[7]; /* 0-6, beginning sunday */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000056} CronLine;
57
58#define RUN_RANOUT 1
59#define RUN_RUNNING 2
60#define RUN_FAILED 3
61
62#define DaemonUid 0
63
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000064#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko13858992006-10-08 12:49:22 +000065static unsigned DebugOpt;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000066#endif
67
Denis Vlasenko13858992006-10-08 12:49:22 +000068static unsigned LogLevel = 8;
Glenn L McGrath9079ad02004-02-22 04:44:21 +000069static const char *LogFile;
70static const char *CDir = CRONTABS;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000071
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000072static void startlogger(void);
73
74static void CheckUpdates(void);
75static void SynchronizeDir(void);
76static int TestJobs(time_t t1, time_t t2);
77static void RunJobs(void);
78static int CheckJobs(void);
Eric Andersen35e643b2003-07-28 07:40:39 +000079
Glenn L McGrath9079ad02004-02-22 04:44:21 +000080static void RunJob(const char *user, CronLine * line);
81
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000082#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Glenn L McGrath9079ad02004-02-22 04:44:21 +000083static void EndJob(const char *user, CronLine * line);
Eric Andersen35e643b2003-07-28 07:40:39 +000084#else
85#define EndJob(user, line) line->cl_Pid = 0
86#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000087
88static void DeleteFile(const char *userName);
89
90static CronFile *FileBase;
91
92
Glenn L McGrath9079ad02004-02-22 04:44:21 +000093static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +000094{
Glenn L McGrath9079ad02004-02-22 04:44:21 +000095 va_list va;
96 const char *fmt;
97 int level = (int) (ctl[0] & 0xf);
98 int type = level == 20 ?
99 LOG_ERR : ((ctl[0] & 0100) ? LOG_WARNING : LOG_NOTICE);
Eric Andersen35e643b2003-07-28 07:40:39 +0000100
101
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000102 va_start(va, ctl);
103 fmt = ctl + 1;
104 if (level >= LogLevel) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000105
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000106#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000107 if (DebugOpt) {
108 vfprintf(stderr, fmt, va);
109 } else
Eric Andersen35e643b2003-07-28 07:40:39 +0000110#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000111 if (LogFile == 0) {
112 vsyslog(type, fmt, va);
113 } else {
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000114#if !ENABLE_DEBUG_CROND_OPTION
"Vladimir N. Oleynik"d0c41a82005-09-05 15:50:56 +0000115 int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000116#else
117 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
118#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000119 if (logfd >= 0) {
120 vdprintf(logfd, fmt, va);
121 close(logfd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000122 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000123 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000124 }
125 va_end(va);
126 if (ctl[0] & 0200) {
127 exit(20);
128 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000129}
130
Denis Vlasenko06af2162007-02-03 17:28:39 +0000131int crond_main(int ac, char **av);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000132int crond_main(int ac, char **av)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000133{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000134 unsigned opt;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000135 char *lopt, *Lopt, *copt;
Denis Vlasenko5a142022007-03-26 13:20:54 +0000136 USE_DEBUG_CROND_OPTION(char *dopt;)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000137
Denis Vlasenko5a142022007-03-26 13:20:54 +0000138 opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000139 opterr = 0; /* disable getopt 'errors' message. */
Denis Vlasenkod37f2222007-08-19 13:42:08 +0000140 opt = getopt32(av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
Denis Vlasenko5a142022007-03-26 13:20:54 +0000141 &lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt));
142 if (opt & 1) /* -l */
Denis Vlasenko13858992006-10-08 12:49:22 +0000143 LogLevel = xatou(lopt);
Denis Vlasenko5a142022007-03-26 13:20:54 +0000144 if (opt & 2) /* -L */
145 if (*Lopt)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000146 LogFile = Lopt;
Denis Vlasenko5a142022007-03-26 13:20:54 +0000147 if (opt & 32) /* -c */
148 if (*copt)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000149 CDir = copt;
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000150#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko5a142022007-03-26 13:20:54 +0000151 if (opt & 64) { /* -d */
Denis Vlasenko13858992006-10-08 12:49:22 +0000152 DebugOpt = xatou(dopt);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000153 LogLevel = 0;
154 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000155#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000156
Denis Vlasenko5a142022007-03-26 13:20:54 +0000157 /* close stdin and stdout, stderr.
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000158 * close unused descriptors - don't need.
159 * optional detach from controlling terminal
160 */
Denis Vlasenko5a142022007-03-26 13:20:54 +0000161 if (!(opt & 4))
162 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000163
Denis Vlasenko5a142022007-03-26 13:20:54 +0000164 xchdir(CDir);
165 signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000166
Denis Vlasenko5a142022007-03-26 13:20:54 +0000167 startlogger(); /* need if syslog mode selected */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000168
169 /*
170 * main loop - synchronize to 1 second after the minute, minimum sleep
171 * of 1 second.
172 */
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +0000173 crondlog("\011%s " BB_VER " started, log level %d\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000174 applet_name, LogLevel);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000175
176 SynchronizeDir();
177
178 {
179 time_t t1 = time(NULL);
180 time_t t2;
181 long dt;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000182 int rescan = 60;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000183 short sleep_time = 60;
184
Denis Vlasenko10457b92007-03-27 22:01:31 +0000185 write_pidfile("/var/run/crond.pid");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000186 for (;;) {
187 sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
188
189 t2 = time(NULL);
190 dt = t2 - t1;
191
192 /*
193 * The file 'cron.update' is checked to determine new cron
194 * jobs. The directory is rescanned once an hour to deal
195 * with any screwups.
196 *
197 * check for disparity. Disparities over an hour either way
198 * result in resynchronization. A reverse-indexed disparity
199 * less then an hour causes us to effectively sleep until we
200 * match the original time (i.e. no re-execution of jobs that
201 * have just been run). A forward-indexed disparity less then
202 * an hour causes intermediate jobs to be run, but only once
203 * in the worst case.
204 *
205 * when running jobs, the inequality used is greater but not
206 * equal to t1, and less then or equal to t2.
207 */
208
209 if (--rescan == 0) {
210 rescan = 60;
211 SynchronizeDir();
212 }
213 CheckUpdates();
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000214#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000215 if (DebugOpt)
216 crondlog("\005Wakeup dt=%d\n", dt);
217#endif
218 if (dt < -60 * 60 || dt > 60 * 60) {
219 t1 = t2;
220 crondlog("\111time disparity of %d minutes detected\n", dt / 60);
221 } else if (dt > 0) {
222 TestJobs(t1, t2);
223 RunJobs();
224 sleep(5);
225 if (CheckJobs() > 0) {
226 sleep_time = 10;
227 } else {
228 sleep_time = 60;
229 }
230 t1 = t2;
231 }
232 }
233 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000234 return 0; /* not reached */
Glenn L McGrathb89fcd42003-12-23 07:21:33 +0000235}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000236
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000237static int ChangeUser(const char *user)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000238{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000239 struct passwd *pas;
240 const char *err_msg;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000241
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000242 /*
Eric Andersenaff114c2004-04-14 17:51:38 +0000243 * Obtain password entry and change privileges
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000244 */
245 pas = getpwnam(user);
246 if (pas == 0) {
247 crondlog("\011failed to get uid for %s", user);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000248 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000249 }
250 setenv("USER", pas->pw_name, 1);
251 setenv("HOME", pas->pw_dir, 1);
252 setenv("SHELL", DEFAULT_SHELL, 1);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000253
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000254 /*
255 * Change running state to the user in question
256 */
257 err_msg = change_identity_e2str(pas);
258 if (err_msg) {
259 crondlog("\011%s for user %s", err_msg, user);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000260 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000261 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000262 if (chdir(pas->pw_dir) < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000263 crondlog("\011chdir failed: %s: %m", pas->pw_dir);
264 if (chdir(TMPDIR) < 0) {
265 crondlog("\011chdir failed: %s: %m", TMPDIR);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000266 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000267 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000268 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000269 return pas->pw_uid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000270}
271
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000272static void startlogger(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000273{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000274 if (LogFile == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000275 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000276 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000277#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000278 else { /* test logfile */
279 int logfd;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000280
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000281 logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
282 if (logfd >= 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000283 close(logfd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000284 }
285 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000286#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000287}
288
289
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000290static const char DowAry[] ALIGN1 =
291 "sun""mon""tue""wed""thu""fri""sat"
292 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
293;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000294
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000295static const char MonAry[] ALIGN1 =
296 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
297 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
298;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000299
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000300static char *ParseField(char *user, char *ary, int modvalue, int off,
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000301 const char *names, char *ptr)
302/* 'names' is a pointer to a set of 3-char abbreviations */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000303{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000304 char *base = ptr;
305 int n1 = -1;
306 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000307
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000308 if (base == NULL) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000309 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000310 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000311
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000312 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
313 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000314
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000315 /* Handle numeric digit or symbol or '*' */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000316
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;
322 } else if (*ptr >= '0' && *ptr <= '9') {
323 if (n1 < 0) {
324 n1 = strtol(ptr, &ptr, 10) + off;
325 } else {
326 n2 = strtol(ptr, &ptr, 10) + off;
327 }
328 skip = 1;
329 } else if (names) {
330 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000331
Denis Vlasenkof9000c52007-08-19 18:49:21 +0000332 for (i = 0; names[i]; i += 3) {
333 /* was using strncmp before... */
334 if (strncasecmp(ptr, &names[i], 3) == 0) {
335 ptr += 3;
336 if (n1 < 0) {
337 n1 = i / 3;
338 } else {
339 n2 = i / 3;
340 }
341 skip = 1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000342 break;
343 }
344 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000345 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000346
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000347 /* handle optional range '-' */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000348
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000349 if (skip == 0) {
350 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000351 return NULL;
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 */
362
363 if (n2 < 0) {
364 n2 = n1;
365 }
366 if (*ptr == '/') {
367 skip = strtol(ptr + 1, &ptr, 10);
368 }
369 /*
370 * fill array, using a failsafe is the easiest way to prevent
371 * an endless loop
372 */
373
374 {
375 int s0 = 1;
376 int failsafe = 1024;
377
378 --n1;
379 do {
380 n1 = (n1 + 1) % modvalue;
381
382 if (--s0 == 0) {
383 ary[n1 % modvalue] = 1;
384 s0 = skip;
385 }
386 }
387 while (n1 != n2 && --failsafe);
388
389 if (failsafe == 0) {
390 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000391 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000392 }
393 }
394 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000395 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000396 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000397 ++ptr;
398 n1 = -1;
399 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000400 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000401
402 if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
403 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000404 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000405 }
406
407 while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
408 ++ptr;
409 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000410#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000411 if (DebugOpt) {
412 int i;
413
414 for (i = 0; i < modvalue; ++i) {
415 crondlog("\005%d", ary[i]);
416 }
417 crondlog("\005\n");
418 }
419#endif
420
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000421 return ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000422}
423
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000424static void FixDayDow(CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000425{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000426 int i;
427 int weekUsed = 0;
428 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000429
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000430 for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Dow)); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000431 if (line->cl_Dow[i] == 0) {
432 weekUsed = 1;
433 break;
434 }
435 }
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000436 for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Days)); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000437 if (line->cl_Days[i] == 0) {
438 daysUsed = 1;
439 break;
440 }
441 }
442 if (weekUsed && !daysUsed) {
443 memset(line->cl_Days, 0, sizeof(line->cl_Days));
444 }
445 if (daysUsed && !weekUsed) {
446 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
447 }
448}
449
450
451
452static void SynchronizeFile(const char *fileName)
453{
454 int maxEntries = MAXLINES;
455 int maxLines;
456 char buf[1024];
457
458 if (strcmp(fileName, "root") == 0) {
459 maxEntries = 65535;
460 }
461 maxLines = maxEntries * 10;
462
463 if (fileName) {
464 FILE *fi;
465
466 DeleteFile(fileName);
467
468 fi = fopen(fileName, "r");
469 if (fi != NULL) {
470 struct stat sbuf;
471
472 if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000473 CronFile *file = xzalloc(sizeof(CronFile));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000474 CronLine **pline;
475
476 file->cf_User = strdup(fileName);
477 pline = &file->cf_LineBase;
478
479 while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) {
480 CronLine line;
481 char *ptr;
482
Rob Landley828548a2005-09-01 10:23:57 +0000483 trim(buf);
484 if (buf[0] == 0 || buf[0] == '#') {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000485 continue;
486 }
487 if (--maxEntries == 0) {
488 break;
489 }
490 memset(&line, 0, sizeof(line));
491
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000492#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000493 if (DebugOpt) {
494 crondlog("\111User %s Entry %s\n", fileName, buf);
495 }
496#endif
497
498 /* parse date ranges */
499 ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf);
500 ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr);
501 ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr);
502 ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr);
503 ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr);
504
505 /* check failure */
506 if (ptr == NULL) {
507 continue;
508 }
509
510 /*
511 * fix days and dow - if one is not * and the other
512 * is *, the other is set to 0, and vise-versa
513 */
514
515 FixDayDow(&line);
516
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000517 *pline = xzalloc(sizeof(CronLine));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000518 **pline = line;
519
520 /* copy command */
521 (*pline)->cl_Shell = strdup(ptr);
522
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000523#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000524 if (DebugOpt) {
525 crondlog("\111 Command %s\n", ptr);
526 }
527#endif
528
529 pline = &((*pline)->cl_Next);
530 }
531 *pline = NULL;
532
533 file->cf_Next = FileBase;
534 FileBase = file;
535
536 if (maxLines == 0 || maxEntries == 0) {
537 crondlog("\111Maximum number of lines reached for user %s\n", fileName);
538 }
539 }
540 fclose(fi);
541 }
542 }
543}
544
545static void CheckUpdates(void)
546{
547 FILE *fi;
548 char buf[256];
549
550 fi = fopen(CRONUPDATE, "r");
551 if (fi != NULL) {
552 remove(CRONUPDATE);
553 while (fgets(buf, sizeof(buf), fi) != NULL) {
554 SynchronizeFile(strtok(buf, " \t\r\n"));
555 }
556 fclose(fi);
557 }
558}
559
560static void SynchronizeDir(void)
561{
562 /* Attempt to delete the database. */
563
564 for (;;) {
565 CronFile *file;
566
567 for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next);
568 if (file == NULL) {
569 break;
570 }
571 DeleteFile(file->cf_User);
572 }
573
574 /*
575 * Remove cron update file
576 *
577 * Re-chdir, in case directory was renamed & deleted, or otherwise
578 * screwed up.
579 *
580 * scan directory and add associated users
581 */
582
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000583 remove(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000584 if (chdir(CDir) < 0) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000585 crondlog("\311cannot find %s\n", CDir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000586 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000587 {
588 DIR *dir = opendir(".");
589 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000590
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000591 if (dir) {
592 while ((den = readdir(dir))) {
593 if (strchr(den->d_name, '.') != NULL) {
594 continue;
595 }
596 if (getpwnam(den->d_name)) {
597 SynchronizeFile(den->d_name);
598 } else {
599 crondlog("\007ignoring %s\n", den->d_name);
600 }
601 }
602 closedir(dir);
603 } else {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000604 crondlog("\311cannot open current dir!\n");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000605 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000606 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000607}
608
609
610/*
611 * DeleteFile() - delete user database
612 *
613 * Note: multiple entries for same user may exist if we were unable to
614 * completely delete a database due to running processes.
615 */
616
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000617static void DeleteFile(const char *userName)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000618{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000619 CronFile **pfile = &FileBase;
620 CronFile *file;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000621
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000622 while ((file = *pfile) != NULL) {
623 if (strcmp(userName, file->cf_User) == 0) {
624 CronLine **pline = &file->cf_LineBase;
625 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000626
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000627 file->cf_Running = 0;
628 file->cf_Deleted = 1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000629
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000630 while ((line = *pline) != NULL) {
631 if (line->cl_Pid > 0) {
632 file->cf_Running = 1;
633 pline = &line->cl_Next;
634 } else {
635 *pline = line->cl_Next;
636 free(line->cl_Shell);
637 free(line);
638 }
639 }
640 if (file->cf_Running == 0) {
641 *pfile = file->cf_Next;
642 free(file->cf_User);
643 free(file);
644 } else {
645 pfile = &file->cf_Next;
646 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000647 } else {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000648 pfile = &file->cf_Next;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000649 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000650 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000651}
652
653/*
654 * TestJobs()
655 *
656 * determine which jobs need to be run. Under normal conditions, the
657 * period is about a minute (one scan). Worst case it will be one
658 * hour (60 scans).
659 */
660
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000661static int TestJobs(time_t t1, time_t t2)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000662{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000663 int nJobs = 0;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000664 time_t t;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000665
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000666 /* Find jobs > t1 and <= t2 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000667
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000668 for (t = t1 - t1 % 60; t <= t2; t += 60) {
669 if (t > t1) {
670 struct tm *tp = localtime(&t);
671 CronFile *file;
672 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000673
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000674 for (file = FileBase; file; file = file->cf_Next) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000675#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000676 if (DebugOpt)
677 crondlog("\005FILE %s:\n", file->cf_User);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000678#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000679 if (file->cf_Deleted)
680 continue;
681 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000682#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000683 if (DebugOpt)
684 crondlog("\005 LINE %s\n", line->cl_Shell);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000685#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000686 if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
687 (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
688 && line->cl_Mons[tp->tm_mon]) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000689#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000690 if (DebugOpt) {
691 crondlog("\005 JobToDo: %d %s\n",
692 line->cl_Pid, line->cl_Shell);
693 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000694#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000695 if (line->cl_Pid > 0) {
696 crondlog("\010 process already running: %s %s\n",
697 file->cf_User, line->cl_Shell);
698 } else if (line->cl_Pid == 0) {
699 line->cl_Pid = -1;
700 file->cf_Ready = 1;
701 ++nJobs;
702 }
703 }
704 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000705 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000706 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000707 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000708 return nJobs;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000709}
710
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000711static void RunJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000712{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000713 CronFile *file;
714 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000715
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000716 for (file = FileBase; file; file = file->cf_Next) {
717 if (file->cf_Ready) {
718 file->cf_Ready = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000719
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000720 for (line = file->cf_LineBase; line; line = line->cl_Next) {
721 if (line->cl_Pid < 0) {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000722
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000723 RunJob(file->cf_User, line);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000724
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000725 crondlog("\010USER %s pid %3d cmd %s\n",
726 file->cf_User, line->cl_Pid, line->cl_Shell);
727 if (line->cl_Pid < 0) {
728 file->cf_Ready = 1;
729 }
730 else if (line->cl_Pid > 0) {
731 file->cf_Running = 1;
732 }
733 }
734 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000735 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000736 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000737}
738
739/*
740 * CheckJobs() - check for job completion
741 *
742 * Check for job completion, return number of jobs still running after
743 * all done.
744 */
745
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000746static int CheckJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000747{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000748 CronFile *file;
749 CronLine *line;
750 int nStillRunning = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000751
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000752 for (file = FileBase; file; file = file->cf_Next) {
753 if (file->cf_Running) {
754 file->cf_Running = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000755
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000756 for (line = file->cf_LineBase; line; line = line->cl_Next) {
757 if (line->cl_Pid > 0) {
758 int status;
759 int r = wait4(line->cl_Pid, &status, WNOHANG, NULL);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000760
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000761 if (r < 0 || r == line->cl_Pid) {
762 EndJob(file->cf_User, line);
763 if (line->cl_Pid) {
764 file->cf_Running = 1;
765 }
766 } else if (r == 0) {
767 file->cf_Running = 1;
768 }
769 }
770 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000771 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000772 nStillRunning += file->cf_Running;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000773 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000774 return nStillRunning;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000775}
776
777
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000778#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Eric Andersen35e643b2003-07-28 07:40:39 +0000779static void
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000780ForkJob(const char *user, CronLine * line, int mailFd,
781 const char *prog, const char *cmd, const char *arg, const char *mailf)
Eric Andersen35e643b2003-07-28 07:40:39 +0000782{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000783 /* Fork as the user in question and run program */
784 pid_t pid = fork();
Eric Andersen35e643b2003-07-28 07:40:39 +0000785
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000786 line->cl_Pid = pid;
787 if (pid == 0) {
788 /* CHILD */
Eric Andersen35e643b2003-07-28 07:40:39 +0000789
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000790 /* Change running state to the user in question */
Eric Andersen35e643b2003-07-28 07:40:39 +0000791
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000792 if (ChangeUser(user) < 0) {
793 exit(0);
794 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000795#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000796 if (DebugOpt) {
797 crondlog("\005Child Running %s\n", prog);
798 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000799#endif
800
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000801 if (mailFd >= 0) {
802 dup2(mailFd, mailf != NULL);
803 dup2((mailf ? mailFd : 1), 2);
804 close(mailFd);
805 }
806 execl(prog, prog, cmd, arg, NULL);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000807 crondlog("\024cannot exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000808 if (mailf) {
809 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
810 }
811 exit(0);
812 } else if (pid < 0) {
813 /* FORK FAILED */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000814 crondlog("\024cannot fork, user %s\n", user);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000815 line->cl_Pid = 0;
816 if (mailf) {
817 remove(mailf);
818 }
819 } else if (mailf) {
820 /* PARENT, FORK SUCCESS
821 * rename mail-file based on pid of process
822 */
823 char mailFile2[128];
824
825 snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
826 rename(mailf, mailFile2);
Eric Andersen35e643b2003-07-28 07:40:39 +0000827 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000828 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000829 * Close the mail file descriptor.. we can't just leave it open in
830 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000831 */
Eric Andersen35e643b2003-07-28 07:40:39 +0000832
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000833 if (mailFd >= 0) {
834 close(mailFd);
835 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000836}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000837
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000838static void RunJob(const char *user, CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000839{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000840 char mailFile[128];
841 int mailFd;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000842
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000843 line->cl_Pid = 0;
844 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000845
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000846 /* open mail file - owner root so nobody can screw with it. */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000847
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000848 snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, getpid());
849 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000850
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000851 if (mailFd >= 0) {
852 line->cl_MailFlag = 1;
853 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
854 line->cl_Shell);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000855 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000856 } else {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000857 crondlog("\024cannot create mail file user %s file %s, output to /dev/null\n", user, mailFile);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000858 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000859
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000860 ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000861}
862
863/*
864 * EndJob - called when job terminates and when mail terminates
865 */
866
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000867static void EndJob(const char *user, CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000868{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000869 int mailFd;
870 char mailFile[128];
871 struct stat sbuf;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000872
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000873 /* No job */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000874
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000875 if (line->cl_Pid <= 0) {
876 line->cl_Pid = 0;
877 return;
878 }
879
880 /*
881 * End of job and no mail file
882 * End of sendmail job
883 */
884
885 snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, line->cl_Pid);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000886 line->cl_Pid = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000887
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000888 if (line->cl_MailFlag != 1) {
889 return;
890 }
891 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000892
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000893 /*
894 * End of primary job - check for mail file. If size has increased and
895 * the file is still valid, we sendmail it.
896 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000897
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000898 mailFd = open(mailFile, O_RDONLY);
899 remove(mailFile);
900 if (mailFd < 0) {
901 return;
902 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000903
Denis Vlasenkob9c02dd2007-08-18 15:48:00 +0000904 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid
905 || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos
906 || !S_ISREG(sbuf.st_mode)
907 ) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000908 close(mailFd);
909 return;
910 }
911 ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
Eric Andersen35e643b2003-07-28 07:40:39 +0000912}
913#else
Eric Andersenaff114c2004-04-14 17:51:38 +0000914/* crond without sendmail */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000915
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000916static void RunJob(const char *user, CronLine * line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000917{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000918 /* Fork as the user in question and run program */
919 pid_t pid = fork();
Eric Andersen35e643b2003-07-28 07:40:39 +0000920
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000921 if (pid == 0) {
922 /* CHILD */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000923
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000924 /* Change running state to the user in question */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000925
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000926 if (ChangeUser(user) < 0) {
927 exit(0);
928 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000929#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000930 if (DebugOpt) {
931 crondlog("\005Child Running %s\n", DEFAULT_SHELL);
932 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000933#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000934
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000935 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000936 crondlog("\024cannot exec, user %s cmd %s -c %s\n", user,
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000937 DEFAULT_SHELL, line->cl_Shell);
938 exit(0);
939 } else if (pid < 0) {
940 /* FORK FAILED */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000941 crondlog("\024cannot, user %s\n", user);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000942 pid = 0;
943 }
944 line->cl_Pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000945}
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000946#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */