blob: 5653f680480722578c66ffe6ac471ce02780463d [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)
Mike Frysingerf284c762006-04-16 20:38:26 +00008 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
Eric Andersenf6f7bfb2002-10-22 12:24:59 +00009 *
Mike Frysingerf284c762006-04-16 20:38:26 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000011 */
12
13#define VERSION "2.3.2"
14
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Rob Landleyd921b2e2006-08-03 15:41:12 +000016#include <sys/syslog.h>
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000017
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000018
19#ifndef CRONTABS
20#define CRONTABS "/var/spool/cron/crontabs"
21#endif
22#ifndef TMPDIR
23#define TMPDIR "/var/spool/cron"
24#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000025#ifndef SENDMAIL
26#define SENDMAIL "/usr/sbin/sendmail"
27#endif
28#ifndef SENDMAIL_ARGS
Eric Andersen35e643b2003-07-28 07:40:39 +000029#define SENDMAIL_ARGS "-ti", "oem"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000030#endif
31#ifndef CRONUPDATE
32#define CRONUPDATE "cron.update"
33#endif
34#ifndef MAXLINES
Glenn L McGrath9079ad02004-02-22 04:44:21 +000035#define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000036#endif
37
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000038typedef struct CronFile {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000039 struct CronFile *cf_Next;
40 struct CronLine *cf_LineBase;
41 char *cf_User; /* username */
42 int cf_Ready; /* bool: one or more jobs ready */
43 int cf_Running; /* bool: one or more jobs running */
44 int cf_Deleted; /* marked for deletion, ignore */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000045} CronFile;
46
47typedef struct CronLine {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000048 struct CronLine *cl_Next;
49 char *cl_Shell; /* shell command */
50 pid_t cl_Pid; /* running pid, 0, or armed (-1) */
51 int cl_MailFlag; /* running pid is for mail */
52 int cl_MailPos; /* 'empty file' size */
53 char cl_Mins[60]; /* 0-59 */
54 char cl_Hrs[24]; /* 0-23 */
55 char cl_Days[32]; /* 1-31 */
56 char cl_Mons[12]; /* 0-11 */
57 char cl_Dow[7]; /* 0-6, beginning sunday */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000058} CronLine;
59
60#define RUN_RANOUT 1
61#define RUN_RUNNING 2
62#define RUN_FAILED 3
63
64#define DaemonUid 0
65
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000066#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko13858992006-10-08 12:49:22 +000067static unsigned DebugOpt;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000068#endif
69
Denis Vlasenko13858992006-10-08 12:49:22 +000070static unsigned LogLevel = 8;
Glenn L McGrath9079ad02004-02-22 04:44:21 +000071static const char *LogFile;
72static const char *CDir = CRONTABS;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000073
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000074static void startlogger(void);
75
76static void CheckUpdates(void);
77static void SynchronizeDir(void);
78static int TestJobs(time_t t1, time_t t2);
79static void RunJobs(void);
80static int CheckJobs(void);
Eric Andersen35e643b2003-07-28 07:40:39 +000081
Glenn L McGrath9079ad02004-02-22 04:44:21 +000082static void RunJob(const char *user, CronLine * line);
83
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000084#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Glenn L McGrath9079ad02004-02-22 04:44:21 +000085static void EndJob(const char *user, CronLine * line);
Eric Andersen35e643b2003-07-28 07:40:39 +000086#else
87#define EndJob(user, line) line->cl_Pid = 0
88#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000089
90static void DeleteFile(const char *userName);
91
92static CronFile *FileBase;
93
94
Glenn L McGrath9079ad02004-02-22 04:44:21 +000095static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +000096{
Glenn L McGrath9079ad02004-02-22 04:44:21 +000097 va_list va;
98 const char *fmt;
99 int level = (int) (ctl[0] & 0xf);
100 int type = level == 20 ?
101 LOG_ERR : ((ctl[0] & 0100) ? LOG_WARNING : LOG_NOTICE);
Eric Andersen35e643b2003-07-28 07:40:39 +0000102
103
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000104 va_start(va, ctl);
105 fmt = ctl + 1;
106 if (level >= LogLevel) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000107
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000108#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000109 if (DebugOpt) {
110 vfprintf(stderr, fmt, va);
111 } else
Eric Andersen35e643b2003-07-28 07:40:39 +0000112#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000113 if (LogFile == 0) {
114 vsyslog(type, fmt, va);
115 } else {
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000116#if !ENABLE_DEBUG_CROND_OPTION
"Vladimir N. Oleynik"d0c41a82005-09-05 15:50:56 +0000117 int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000118#else
119 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
120#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000121 if (logfd >= 0) {
122 vdprintf(logfd, fmt, va);
123 close(logfd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000124 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000125 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000126 }
127 va_end(va);
128 if (ctl[0] & 0200) {
129 exit(20);
130 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000131}
132
Denis Vlasenko06af2162007-02-03 17:28:39 +0000133int crond_main(int ac, char **av);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000134int crond_main(int ac, char **av)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000135{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000136 unsigned opt;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000137 char *lopt, *Lopt, *copt;
Denis Vlasenko5a142022007-03-26 13:20:54 +0000138 USE_DEBUG_CROND_OPTION(char *dopt;)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000139
Denis Vlasenko5a142022007-03-26 13:20:54 +0000140 opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000141 opterr = 0; /* disable getopt 'errors' message. */
Denis Vlasenko5a142022007-03-26 13:20:54 +0000142 opt = getopt32(ac, av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
143 &lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt));
144 if (opt & 1) /* -l */
Denis Vlasenko13858992006-10-08 12:49:22 +0000145 LogLevel = xatou(lopt);
Denis Vlasenko5a142022007-03-26 13:20:54 +0000146 if (opt & 2) /* -L */
147 if (*Lopt)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000148 LogFile = Lopt;
Denis Vlasenko5a142022007-03-26 13:20:54 +0000149 if (opt & 32) /* -c */
150 if (*copt)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000151 CDir = copt;
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000152#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko5a142022007-03-26 13:20:54 +0000153 if (opt & 64) { /* -d */
Denis Vlasenko13858992006-10-08 12:49:22 +0000154 DebugOpt = xatou(dopt);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000155 LogLevel = 0;
156 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000157#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000158
Denis Vlasenko5a142022007-03-26 13:20:54 +0000159 /* close stdin and stdout, stderr.
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000160 * close unused descriptors - don't need.
161 * optional detach from controlling terminal
162 */
Denis Vlasenko5a142022007-03-26 13:20:54 +0000163 if (!(opt & 4))
164 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000165
Denis Vlasenko5a142022007-03-26 13:20:54 +0000166 xchdir(CDir);
167 signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000168
Denis Vlasenko5a142022007-03-26 13:20:54 +0000169 startlogger(); /* need if syslog mode selected */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000170
171 /*
172 * main loop - synchronize to 1 second after the minute, minimum sleep
173 * of 1 second.
174 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000175 crondlog("\011%s " VERSION " dillon, started, log level %d\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000176 applet_name, LogLevel);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000177
178 SynchronizeDir();
179
180 {
181 time_t t1 = time(NULL);
182 time_t t2;
183 long dt;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000184 int rescan = 60;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000185 short sleep_time = 60;
186
Denis Vlasenko10457b92007-03-27 22:01:31 +0000187 write_pidfile("/var/run/crond.pid");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000188 for (;;) {
189 sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
190
191 t2 = time(NULL);
192 dt = t2 - t1;
193
194 /*
195 * The file 'cron.update' is checked to determine new cron
196 * jobs. The directory is rescanned once an hour to deal
197 * with any screwups.
198 *
199 * check for disparity. Disparities over an hour either way
200 * result in resynchronization. A reverse-indexed disparity
201 * less then an hour causes us to effectively sleep until we
202 * match the original time (i.e. no re-execution of jobs that
203 * have just been run). A forward-indexed disparity less then
204 * an hour causes intermediate jobs to be run, but only once
205 * in the worst case.
206 *
207 * when running jobs, the inequality used is greater but not
208 * equal to t1, and less then or equal to t2.
209 */
210
211 if (--rescan == 0) {
212 rescan = 60;
213 SynchronizeDir();
214 }
215 CheckUpdates();
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000216#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000217 if (DebugOpt)
218 crondlog("\005Wakeup dt=%d\n", dt);
219#endif
220 if (dt < -60 * 60 || dt > 60 * 60) {
221 t1 = t2;
222 crondlog("\111time disparity of %d minutes detected\n", dt / 60);
223 } else if (dt > 0) {
224 TestJobs(t1, t2);
225 RunJobs();
226 sleep(5);
227 if (CheckJobs() > 0) {
228 sleep_time = 10;
229 } else {
230 sleep_time = 60;
231 }
232 t1 = t2;
233 }
234 }
235 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000236 return 0; /* not reached */
Glenn L McGrathb89fcd42003-12-23 07:21:33 +0000237}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000238
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000239static int ChangeUser(const char *user)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000240{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000241 struct passwd *pas;
242 const char *err_msg;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000243
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000244 /*
Eric Andersenaff114c2004-04-14 17:51:38 +0000245 * Obtain password entry and change privileges
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000246 */
247 pas = getpwnam(user);
248 if (pas == 0) {
249 crondlog("\011failed to get uid for %s", user);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000250 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000251 }
252 setenv("USER", pas->pw_name, 1);
253 setenv("HOME", pas->pw_dir, 1);
254 setenv("SHELL", DEFAULT_SHELL, 1);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000255
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000256 /*
257 * Change running state to the user in question
258 */
259 err_msg = change_identity_e2str(pas);
260 if (err_msg) {
261 crondlog("\011%s for user %s", err_msg, user);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000262 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000263 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000264 if (chdir(pas->pw_dir) < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000265 crondlog("\011chdir failed: %s: %m", pas->pw_dir);
266 if (chdir(TMPDIR) < 0) {
267 crondlog("\011chdir failed: %s: %m", TMPDIR);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000268 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000269 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000270 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000271 return pas->pw_uid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000272}
273
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000274static void startlogger(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000275{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000276 if (LogFile == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000277 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000278 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000279#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000280 else { /* test logfile */
281 int logfd;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000282
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000283 logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
284 if (logfd >= 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000285 close(logfd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000286 }
287 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000288#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000289}
290
291
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000292static const char *const DowAry[] = {
293 "sun",
294 "mon",
295 "tue",
296 "wed",
297 "thu",
298 "fri",
299 "sat",
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000300
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000301 "Sun",
302 "Mon",
303 "Tue",
304 "Wed",
305 "Thu",
306 "Fri",
307 "Sat",
308 NULL
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000309};
310
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000311static const char *const MonAry[] = {
312 "jan",
313 "feb",
314 "mar",
315 "apr",
316 "may",
317 "jun",
318 "jul",
319 "aug",
320 "sep",
321 "oct",
322 "nov",
323 "dec",
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000324
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000325 "Jan",
326 "Feb",
327 "Mar",
328 "Apr",
329 "May",
330 "Jun",
331 "Jul",
332 "Aug",
333 "Sep",
334 "Oct",
335 "Nov",
336 "Dec",
337 NULL
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000338};
339
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000340static char *ParseField(char *user, char *ary, int modvalue, int off,
341 const char *const *names, char *ptr)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000342{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000343 char *base = ptr;
344 int n1 = -1;
345 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000346
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000347 if (base == NULL) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000348 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000349 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000350
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000351 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
352 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000353
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000354 /* Handle numeric digit or symbol or '*' */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000355
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000356 if (*ptr == '*') {
357 n1 = 0; /* everything will be filled */
358 n2 = modvalue - 1;
359 skip = 1;
360 ++ptr;
361 } else if (*ptr >= '0' && *ptr <= '9') {
362 if (n1 < 0) {
363 n1 = strtol(ptr, &ptr, 10) + off;
364 } else {
365 n2 = strtol(ptr, &ptr, 10) + off;
366 }
367 skip = 1;
368 } else if (names) {
369 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000370
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000371 for (i = 0; names[i]; ++i) {
372 if (strncmp(ptr, names[i], strlen(names[i])) == 0) {
373 break;
374 }
375 }
376 if (names[i]) {
377 ptr += strlen(names[i]);
378 if (n1 < 0) {
379 n1 = i;
380 } else {
381 n2 = i;
382 }
383 skip = 1;
384 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000385 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000386
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000387 /* handle optional range '-' */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000388
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000389 if (skip == 0) {
390 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000391 return NULL;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000392 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000393 if (*ptr == '-' && n2 < 0) {
394 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000395 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000396 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000397
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000398 /*
399 * collapse single-value ranges, handle skipmark, and fill
400 * in the character array appropriately.
401 */
402
403 if (n2 < 0) {
404 n2 = n1;
405 }
406 if (*ptr == '/') {
407 skip = strtol(ptr + 1, &ptr, 10);
408 }
409 /*
410 * fill array, using a failsafe is the easiest way to prevent
411 * an endless loop
412 */
413
414 {
415 int s0 = 1;
416 int failsafe = 1024;
417
418 --n1;
419 do {
420 n1 = (n1 + 1) % modvalue;
421
422 if (--s0 == 0) {
423 ary[n1 % modvalue] = 1;
424 s0 = skip;
425 }
426 }
427 while (n1 != n2 && --failsafe);
428
429 if (failsafe == 0) {
430 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000431 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000432 }
433 }
434 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000435 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000436 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000437 ++ptr;
438 n1 = -1;
439 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000440 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000441
442 if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
443 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000444 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000445 }
446
447 while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
448 ++ptr;
449 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000450#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000451 if (DebugOpt) {
452 int i;
453
454 for (i = 0; i < modvalue; ++i) {
455 crondlog("\005%d", ary[i]);
456 }
457 crondlog("\005\n");
458 }
459#endif
460
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000461 return ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000462}
463
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000464static void FixDayDow(CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000465{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000466 int i;
467 int weekUsed = 0;
468 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000469
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000470 for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Dow)); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000471 if (line->cl_Dow[i] == 0) {
472 weekUsed = 1;
473 break;
474 }
475 }
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000476 for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Days)); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000477 if (line->cl_Days[i] == 0) {
478 daysUsed = 1;
479 break;
480 }
481 }
482 if (weekUsed && !daysUsed) {
483 memset(line->cl_Days, 0, sizeof(line->cl_Days));
484 }
485 if (daysUsed && !weekUsed) {
486 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
487 }
488}
489
490
491
492static void SynchronizeFile(const char *fileName)
493{
494 int maxEntries = MAXLINES;
495 int maxLines;
496 char buf[1024];
497
498 if (strcmp(fileName, "root") == 0) {
499 maxEntries = 65535;
500 }
501 maxLines = maxEntries * 10;
502
503 if (fileName) {
504 FILE *fi;
505
506 DeleteFile(fileName);
507
508 fi = fopen(fileName, "r");
509 if (fi != NULL) {
510 struct stat sbuf;
511
512 if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000513 CronFile *file = xzalloc(sizeof(CronFile));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000514 CronLine **pline;
515
516 file->cf_User = strdup(fileName);
517 pline = &file->cf_LineBase;
518
519 while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) {
520 CronLine line;
521 char *ptr;
522
Rob Landley828548a2005-09-01 10:23:57 +0000523 trim(buf);
524 if (buf[0] == 0 || buf[0] == '#') {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000525 continue;
526 }
527 if (--maxEntries == 0) {
528 break;
529 }
530 memset(&line, 0, sizeof(line));
531
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000532#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000533 if (DebugOpt) {
534 crondlog("\111User %s Entry %s\n", fileName, buf);
535 }
536#endif
537
538 /* parse date ranges */
539 ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf);
540 ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr);
541 ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr);
542 ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr);
543 ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr);
544
545 /* check failure */
546 if (ptr == NULL) {
547 continue;
548 }
549
550 /*
551 * fix days and dow - if one is not * and the other
552 * is *, the other is set to 0, and vise-versa
553 */
554
555 FixDayDow(&line);
556
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000557 *pline = xzalloc(sizeof(CronLine));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000558 **pline = line;
559
560 /* copy command */
561 (*pline)->cl_Shell = strdup(ptr);
562
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000563#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000564 if (DebugOpt) {
565 crondlog("\111 Command %s\n", ptr);
566 }
567#endif
568
569 pline = &((*pline)->cl_Next);
570 }
571 *pline = NULL;
572
573 file->cf_Next = FileBase;
574 FileBase = file;
575
576 if (maxLines == 0 || maxEntries == 0) {
577 crondlog("\111Maximum number of lines reached for user %s\n", fileName);
578 }
579 }
580 fclose(fi);
581 }
582 }
583}
584
585static void CheckUpdates(void)
586{
587 FILE *fi;
588 char buf[256];
589
590 fi = fopen(CRONUPDATE, "r");
591 if (fi != NULL) {
592 remove(CRONUPDATE);
593 while (fgets(buf, sizeof(buf), fi) != NULL) {
594 SynchronizeFile(strtok(buf, " \t\r\n"));
595 }
596 fclose(fi);
597 }
598}
599
600static void SynchronizeDir(void)
601{
602 /* Attempt to delete the database. */
603
604 for (;;) {
605 CronFile *file;
606
607 for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next);
608 if (file == NULL) {
609 break;
610 }
611 DeleteFile(file->cf_User);
612 }
613
614 /*
615 * Remove cron update file
616 *
617 * Re-chdir, in case directory was renamed & deleted, or otherwise
618 * screwed up.
619 *
620 * scan directory and add associated users
621 */
622
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000623 remove(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000624 if (chdir(CDir) < 0) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000625 crondlog("\311cannot find %s\n", CDir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000626 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000627 {
628 DIR *dir = opendir(".");
629 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000630
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000631 if (dir) {
632 while ((den = readdir(dir))) {
633 if (strchr(den->d_name, '.') != NULL) {
634 continue;
635 }
636 if (getpwnam(den->d_name)) {
637 SynchronizeFile(den->d_name);
638 } else {
639 crondlog("\007ignoring %s\n", den->d_name);
640 }
641 }
642 closedir(dir);
643 } else {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000644 crondlog("\311cannot open current dir!\n");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000645 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000646 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000647}
648
649
650/*
651 * DeleteFile() - delete user database
652 *
653 * Note: multiple entries for same user may exist if we were unable to
654 * completely delete a database due to running processes.
655 */
656
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000657static void DeleteFile(const char *userName)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000658{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000659 CronFile **pfile = &FileBase;
660 CronFile *file;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000661
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000662 while ((file = *pfile) != NULL) {
663 if (strcmp(userName, file->cf_User) == 0) {
664 CronLine **pline = &file->cf_LineBase;
665 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000666
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000667 file->cf_Running = 0;
668 file->cf_Deleted = 1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000669
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000670 while ((line = *pline) != NULL) {
671 if (line->cl_Pid > 0) {
672 file->cf_Running = 1;
673 pline = &line->cl_Next;
674 } else {
675 *pline = line->cl_Next;
676 free(line->cl_Shell);
677 free(line);
678 }
679 }
680 if (file->cf_Running == 0) {
681 *pfile = file->cf_Next;
682 free(file->cf_User);
683 free(file);
684 } else {
685 pfile = &file->cf_Next;
686 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000687 } else {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000688 pfile = &file->cf_Next;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000689 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000690 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000691}
692
693/*
694 * TestJobs()
695 *
696 * determine which jobs need to be run. Under normal conditions, the
697 * period is about a minute (one scan). Worst case it will be one
698 * hour (60 scans).
699 */
700
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000701static int TestJobs(time_t t1, time_t t2)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000702{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000703 int nJobs = 0;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000704 time_t t;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000705
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000706 /* Find jobs > t1 and <= t2 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000707
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000708 for (t = t1 - t1 % 60; t <= t2; t += 60) {
709 if (t > t1) {
710 struct tm *tp = localtime(&t);
711 CronFile *file;
712 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000713
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000714 for (file = FileBase; file; file = file->cf_Next) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000715#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000716 if (DebugOpt)
717 crondlog("\005FILE %s:\n", file->cf_User);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000718#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000719 if (file->cf_Deleted)
720 continue;
721 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000722#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000723 if (DebugOpt)
724 crondlog("\005 LINE %s\n", line->cl_Shell);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000725#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000726 if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
727 (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
728 && line->cl_Mons[tp->tm_mon]) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000729#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000730 if (DebugOpt) {
731 crondlog("\005 JobToDo: %d %s\n",
732 line->cl_Pid, line->cl_Shell);
733 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000734#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000735 if (line->cl_Pid > 0) {
736 crondlog("\010 process already running: %s %s\n",
737 file->cf_User, line->cl_Shell);
738 } else if (line->cl_Pid == 0) {
739 line->cl_Pid = -1;
740 file->cf_Ready = 1;
741 ++nJobs;
742 }
743 }
744 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000745 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000746 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000747 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000748 return nJobs;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000749}
750
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000751static void RunJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000752{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000753 CronFile *file;
754 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000755
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000756 for (file = FileBase; file; file = file->cf_Next) {
757 if (file->cf_Ready) {
758 file->cf_Ready = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000759
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000760 for (line = file->cf_LineBase; line; line = line->cl_Next) {
761 if (line->cl_Pid < 0) {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000762
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000763 RunJob(file->cf_User, line);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000764
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000765 crondlog("\010USER %s pid %3d cmd %s\n",
766 file->cf_User, line->cl_Pid, line->cl_Shell);
767 if (line->cl_Pid < 0) {
768 file->cf_Ready = 1;
769 }
770 else if (line->cl_Pid > 0) {
771 file->cf_Running = 1;
772 }
773 }
774 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000775 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000776 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000777}
778
779/*
780 * CheckJobs() - check for job completion
781 *
782 * Check for job completion, return number of jobs still running after
783 * all done.
784 */
785
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000786static int CheckJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000787{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000788 CronFile *file;
789 CronLine *line;
790 int nStillRunning = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000791
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000792 for (file = FileBase; file; file = file->cf_Next) {
793 if (file->cf_Running) {
794 file->cf_Running = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000795
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000796 for (line = file->cf_LineBase; line; line = line->cl_Next) {
797 if (line->cl_Pid > 0) {
798 int status;
799 int r = wait4(line->cl_Pid, &status, WNOHANG, NULL);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000800
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000801 if (r < 0 || r == line->cl_Pid) {
802 EndJob(file->cf_User, line);
803 if (line->cl_Pid) {
804 file->cf_Running = 1;
805 }
806 } else if (r == 0) {
807 file->cf_Running = 1;
808 }
809 }
810 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000811 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000812 nStillRunning += file->cf_Running;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000813 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000814 return nStillRunning;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000815}
816
817
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000818#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Eric Andersen35e643b2003-07-28 07:40:39 +0000819static void
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000820ForkJob(const char *user, CronLine * line, int mailFd,
821 const char *prog, const char *cmd, const char *arg, const char *mailf)
Eric Andersen35e643b2003-07-28 07:40:39 +0000822{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000823 /* Fork as the user in question and run program */
824 pid_t pid = fork();
Eric Andersen35e643b2003-07-28 07:40:39 +0000825
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000826 line->cl_Pid = pid;
827 if (pid == 0) {
828 /* CHILD */
Eric Andersen35e643b2003-07-28 07:40:39 +0000829
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000830 /* Change running state to the user in question */
Eric Andersen35e643b2003-07-28 07:40:39 +0000831
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000832 if (ChangeUser(user) < 0) {
833 exit(0);
834 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000835#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000836 if (DebugOpt) {
837 crondlog("\005Child Running %s\n", prog);
838 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000839#endif
840
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000841 if (mailFd >= 0) {
842 dup2(mailFd, mailf != NULL);
843 dup2((mailf ? mailFd : 1), 2);
844 close(mailFd);
845 }
846 execl(prog, prog, cmd, arg, NULL);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000847 crondlog("\024cannot exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000848 if (mailf) {
849 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
850 }
851 exit(0);
852 } else if (pid < 0) {
853 /* FORK FAILED */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000854 crondlog("\024cannot fork, user %s\n", user);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000855 line->cl_Pid = 0;
856 if (mailf) {
857 remove(mailf);
858 }
859 } else if (mailf) {
860 /* PARENT, FORK SUCCESS
861 * rename mail-file based on pid of process
862 */
863 char mailFile2[128];
864
865 snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
866 rename(mailf, mailFile2);
Eric Andersen35e643b2003-07-28 07:40:39 +0000867 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000868 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000869 * Close the mail file descriptor.. we can't just leave it open in
870 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000871 */
Eric Andersen35e643b2003-07-28 07:40:39 +0000872
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000873 if (mailFd >= 0) {
874 close(mailFd);
875 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000876}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000877
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000878static void RunJob(const char *user, CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000879{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000880 char mailFile[128];
881 int mailFd;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000882
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000883 line->cl_Pid = 0;
884 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000885
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000886 /* open mail file - owner root so nobody can screw with it. */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000887
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000888 snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, getpid());
889 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000890
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000891 if (mailFd >= 0) {
892 line->cl_MailFlag = 1;
893 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
894 line->cl_Shell);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000895 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000896 } else {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000897 crondlog("\024cannot create mail file user %s file %s, output to /dev/null\n", user, mailFile);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000898 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000899
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000900 ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000901}
902
903/*
904 * EndJob - called when job terminates and when mail terminates
905 */
906
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000907static void EndJob(const char *user, CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000908{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000909 int mailFd;
910 char mailFile[128];
911 struct stat sbuf;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000912
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000913 /* No job */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000914
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000915 if (line->cl_Pid <= 0) {
916 line->cl_Pid = 0;
917 return;
918 }
919
920 /*
921 * End of job and no mail file
922 * End of sendmail job
923 */
924
925 snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, line->cl_Pid);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000926 line->cl_Pid = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000927
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000928 if (line->cl_MailFlag != 1) {
929 return;
930 }
931 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000932
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000933 /*
934 * End of primary job - check for mail file. If size has increased and
935 * the file is still valid, we sendmail it.
936 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000937
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000938 mailFd = open(mailFile, O_RDONLY);
939 remove(mailFile);
940 if (mailFd < 0) {
941 return;
942 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000943
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000944 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid || sbuf.st_nlink != 0 ||
945 sbuf.st_size == line->cl_MailPos || !S_ISREG(sbuf.st_mode)) {
946 close(mailFd);
947 return;
948 }
949 ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
Eric Andersen35e643b2003-07-28 07:40:39 +0000950}
951#else
Eric Andersenaff114c2004-04-14 17:51:38 +0000952/* crond without sendmail */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000953
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000954static void RunJob(const char *user, CronLine * line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000955{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000956 /* Fork as the user in question and run program */
957 pid_t pid = fork();
Eric Andersen35e643b2003-07-28 07:40:39 +0000958
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000959 if (pid == 0) {
960 /* CHILD */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000961
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000962 /* Change running state to the user in question */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000963
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000964 if (ChangeUser(user) < 0) {
965 exit(0);
966 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000967#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000968 if (DebugOpt) {
969 crondlog("\005Child Running %s\n", DEFAULT_SHELL);
970 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000971#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000972
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000973 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000974 crondlog("\024cannot exec, user %s cmd %s -c %s\n", user,
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000975 DEFAULT_SHELL, line->cl_Shell);
976 exit(0);
977 } else if (pid < 0) {
978 /* FORK FAILED */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000979 crondlog("\024cannot, user %s\n", user);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000980 pid = 0;
981 }
982 line->cl_Pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000983}
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000984#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */