blob: a490d417fb9ca477fd6a4dd62b9656b7c17f7ac2 [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#define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
19
20#ifndef CRONTABS
21#define CRONTABS "/var/spool/cron/crontabs"
22#endif
23#ifndef TMPDIR
24#define TMPDIR "/var/spool/cron"
25#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000026#ifndef SENDMAIL
27#define SENDMAIL "/usr/sbin/sendmail"
28#endif
29#ifndef SENDMAIL_ARGS
Eric Andersen35e643b2003-07-28 07:40:39 +000030#define SENDMAIL_ARGS "-ti", "oem"
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000031#endif
32#ifndef CRONUPDATE
33#define CRONUPDATE "cron.update"
34#endif
35#ifndef MAXLINES
Glenn L McGrath9079ad02004-02-22 04:44:21 +000036#define MAXLINES 256 /* max lines in non-root crontabs */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000037#endif
38
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000039typedef struct CronFile {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000040 struct CronFile *cf_Next;
41 struct CronLine *cf_LineBase;
42 char *cf_User; /* username */
43 int cf_Ready; /* bool: one or more jobs ready */
44 int cf_Running; /* bool: one or more jobs running */
45 int cf_Deleted; /* marked for deletion, ignore */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000046} CronFile;
47
48typedef struct CronLine {
Glenn L McGrath9079ad02004-02-22 04:44:21 +000049 struct CronLine *cl_Next;
50 char *cl_Shell; /* shell command */
51 pid_t cl_Pid; /* running pid, 0, or armed (-1) */
52 int cl_MailFlag; /* running pid is for mail */
53 int cl_MailPos; /* 'empty file' size */
54 char cl_Mins[60]; /* 0-59 */
55 char cl_Hrs[24]; /* 0-23 */
56 char cl_Days[32]; /* 1-31 */
57 char cl_Mons[12]; /* 0-11 */
58 char cl_Dow[7]; /* 0-6, beginning sunday */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000059} CronLine;
60
61#define RUN_RANOUT 1
62#define RUN_RUNNING 2
63#define RUN_FAILED 3
64
65#define DaemonUid 0
66
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000067#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko13858992006-10-08 12:49:22 +000068static unsigned DebugOpt;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000069#endif
70
Denis Vlasenko13858992006-10-08 12:49:22 +000071static unsigned LogLevel = 8;
Glenn L McGrath9079ad02004-02-22 04:44:21 +000072static const char *LogFile;
73static const char *CDir = CRONTABS;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000074
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000075static void startlogger(void);
76
77static void CheckUpdates(void);
78static void SynchronizeDir(void);
79static int TestJobs(time_t t1, time_t t2);
80static void RunJobs(void);
81static int CheckJobs(void);
Eric Andersen35e643b2003-07-28 07:40:39 +000082
Glenn L McGrath9079ad02004-02-22 04:44:21 +000083static void RunJob(const char *user, CronLine * line);
84
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +000085#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Glenn L McGrath9079ad02004-02-22 04:44:21 +000086static void EndJob(const char *user, CronLine * line);
Eric Andersen35e643b2003-07-28 07:40:39 +000087#else
88#define EndJob(user, line) line->cl_Pid = 0
89#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +000090
91static void DeleteFile(const char *userName);
92
93static CronFile *FileBase;
94
95
Glenn L McGrath9079ad02004-02-22 04:44:21 +000096static void crondlog(const char *ctl, ...)
Eric Andersen35e643b2003-07-28 07:40:39 +000097{
Glenn L McGrath9079ad02004-02-22 04:44:21 +000098 va_list va;
99 const char *fmt;
100 int level = (int) (ctl[0] & 0xf);
101 int type = level == 20 ?
102 LOG_ERR : ((ctl[0] & 0100) ? LOG_WARNING : LOG_NOTICE);
Eric Andersen35e643b2003-07-28 07:40:39 +0000103
104
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000105 va_start(va, ctl);
106 fmt = ctl + 1;
107 if (level >= LogLevel) {
Eric Andersen35e643b2003-07-28 07:40:39 +0000108
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000109#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000110 if (DebugOpt) {
111 vfprintf(stderr, fmt, va);
112 } else
Eric Andersen35e643b2003-07-28 07:40:39 +0000113#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000114 if (LogFile == 0) {
115 vsyslog(type, fmt, va);
116 } else {
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000117#if !ENABLE_DEBUG_CROND_OPTION
"Vladimir N. Oleynik"d0c41a82005-09-05 15:50:56 +0000118 int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000119#else
120 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
121#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000122 if (logfd >= 0) {
123 vdprintf(logfd, fmt, va);
124 close(logfd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000125 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000126 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000127 }
128 va_end(va);
129 if (ctl[0] & 0200) {
130 exit(20);
131 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000132}
133
Denis Vlasenko06af2162007-02-03 17:28:39 +0000134int crond_main(int ac, char **av);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000135int crond_main(int ac, char **av)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000136{
Denis Vlasenko67b23e62006-10-03 21:00:06 +0000137 unsigned opt;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000138 char *lopt, *Lopt, *copt;
Denis Vlasenko5a142022007-03-26 13:20:54 +0000139 USE_DEBUG_CROND_OPTION(char *dopt;)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000140
Denis Vlasenko5a142022007-03-26 13:20:54 +0000141 opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000142 opterr = 0; /* disable getopt 'errors' message. */
Denis Vlasenko5a142022007-03-26 13:20:54 +0000143 opt = getopt32(ac, av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
144 &lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt));
145 if (opt & 1) /* -l */
Denis Vlasenko13858992006-10-08 12:49:22 +0000146 LogLevel = xatou(lopt);
Denis Vlasenko5a142022007-03-26 13:20:54 +0000147 if (opt & 2) /* -L */
148 if (*Lopt)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000149 LogFile = Lopt;
Denis Vlasenko5a142022007-03-26 13:20:54 +0000150 if (opt & 32) /* -c */
151 if (*copt)
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000152 CDir = copt;
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000153#if ENABLE_DEBUG_CROND_OPTION
Denis Vlasenko5a142022007-03-26 13:20:54 +0000154 if (opt & 64) { /* -d */
Denis Vlasenko13858992006-10-08 12:49:22 +0000155 DebugOpt = xatou(dopt);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000156 LogLevel = 0;
157 }
Eric Andersen8876fb22003-06-20 09:01:58 +0000158#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000159
Denis Vlasenko5a142022007-03-26 13:20:54 +0000160 /* close stdin and stdout, stderr.
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000161 * close unused descriptors - don't need.
162 * optional detach from controlling terminal
163 */
Denis Vlasenko5a142022007-03-26 13:20:54 +0000164 if (!(opt & 4))
165 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000166
Denis Vlasenko5a142022007-03-26 13:20:54 +0000167 xchdir(CDir);
168 signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000169
Denis Vlasenko5a142022007-03-26 13:20:54 +0000170 startlogger(); /* need if syslog mode selected */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000171
172 /*
173 * main loop - synchronize to 1 second after the minute, minimum sleep
174 * of 1 second.
175 */
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000176 crondlog("\011%s " VERSION " dillon, started, log level %d\n",
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000177 applet_name, LogLevel);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000178
179 SynchronizeDir();
180
181 {
182 time_t t1 = time(NULL);
183 time_t t2;
184 long dt;
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000185 int rescan = 60;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000186 short sleep_time = 60;
187
Denis Vlasenko10457b92007-03-27 22:01:31 +0000188 write_pidfile("/var/run/crond.pid");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000189 for (;;) {
190 sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
191
192 t2 = time(NULL);
193 dt = t2 - t1;
194
195 /*
196 * The file 'cron.update' is checked to determine new cron
197 * jobs. The directory is rescanned once an hour to deal
198 * with any screwups.
199 *
200 * check for disparity. Disparities over an hour either way
201 * result in resynchronization. A reverse-indexed disparity
202 * less then an hour causes us to effectively sleep until we
203 * match the original time (i.e. no re-execution of jobs that
204 * have just been run). A forward-indexed disparity less then
205 * an hour causes intermediate jobs to be run, but only once
206 * in the worst case.
207 *
208 * when running jobs, the inequality used is greater but not
209 * equal to t1, and less then or equal to t2.
210 */
211
212 if (--rescan == 0) {
213 rescan = 60;
214 SynchronizeDir();
215 }
216 CheckUpdates();
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000217#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000218 if (DebugOpt)
219 crondlog("\005Wakeup dt=%d\n", dt);
220#endif
221 if (dt < -60 * 60 || dt > 60 * 60) {
222 t1 = t2;
223 crondlog("\111time disparity of %d minutes detected\n", dt / 60);
224 } else if (dt > 0) {
225 TestJobs(t1, t2);
226 RunJobs();
227 sleep(5);
228 if (CheckJobs() > 0) {
229 sleep_time = 10;
230 } else {
231 sleep_time = 60;
232 }
233 t1 = t2;
234 }
235 }
236 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000237 return 0; /* not reached */
Glenn L McGrathb89fcd42003-12-23 07:21:33 +0000238}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000239
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000240static int ChangeUser(const char *user)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000241{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000242 struct passwd *pas;
243 const char *err_msg;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000244
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000245 /*
Eric Andersenaff114c2004-04-14 17:51:38 +0000246 * Obtain password entry and change privileges
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000247 */
248 pas = getpwnam(user);
249 if (pas == 0) {
250 crondlog("\011failed to get uid for %s", user);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000251 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000252 }
253 setenv("USER", pas->pw_name, 1);
254 setenv("HOME", pas->pw_dir, 1);
255 setenv("SHELL", DEFAULT_SHELL, 1);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000256
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000257 /*
258 * Change running state to the user in question
259 */
260 err_msg = change_identity_e2str(pas);
261 if (err_msg) {
262 crondlog("\011%s for user %s", err_msg, user);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000263 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000264 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000265 if (chdir(pas->pw_dir) < 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000266 crondlog("\011chdir failed: %s: %m", pas->pw_dir);
267 if (chdir(TMPDIR) < 0) {
268 crondlog("\011chdir failed: %s: %m", TMPDIR);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000269 return -1;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000270 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000271 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000272 return pas->pw_uid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000273}
274
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000275static void startlogger(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000276{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000277 if (LogFile == 0) {
Denis Vlasenko8f8f2682006-10-03 21:00:43 +0000278 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000279 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000280#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000281 else { /* test logfile */
282 int logfd;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000283
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000284 logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
285 if (logfd >= 0) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000286 close(logfd);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000287 }
288 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000289#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000290}
291
292
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000293static const char *const DowAry[] = {
294 "sun",
295 "mon",
296 "tue",
297 "wed",
298 "thu",
299 "fri",
300 "sat",
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000301
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000302 "Sun",
303 "Mon",
304 "Tue",
305 "Wed",
306 "Thu",
307 "Fri",
308 "Sat",
309 NULL
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000310};
311
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000312static const char *const MonAry[] = {
313 "jan",
314 "feb",
315 "mar",
316 "apr",
317 "may",
318 "jun",
319 "jul",
320 "aug",
321 "sep",
322 "oct",
323 "nov",
324 "dec",
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000325
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000326 "Jan",
327 "Feb",
328 "Mar",
329 "Apr",
330 "May",
331 "Jun",
332 "Jul",
333 "Aug",
334 "Sep",
335 "Oct",
336 "Nov",
337 "Dec",
338 NULL
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000339};
340
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000341static char *ParseField(char *user, char *ary, int modvalue, int off,
342 const char *const *names, char *ptr)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000343{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000344 char *base = ptr;
345 int n1 = -1;
346 int n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000347
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000348 if (base == NULL) {
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000349 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000350 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000351
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000352 while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
353 int skip = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000354
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000355 /* Handle numeric digit or symbol or '*' */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000356
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000357 if (*ptr == '*') {
358 n1 = 0; /* everything will be filled */
359 n2 = modvalue - 1;
360 skip = 1;
361 ++ptr;
362 } else if (*ptr >= '0' && *ptr <= '9') {
363 if (n1 < 0) {
364 n1 = strtol(ptr, &ptr, 10) + off;
365 } else {
366 n2 = strtol(ptr, &ptr, 10) + off;
367 }
368 skip = 1;
369 } else if (names) {
370 int i;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000371
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000372 for (i = 0; names[i]; ++i) {
373 if (strncmp(ptr, names[i], strlen(names[i])) == 0) {
374 break;
375 }
376 }
377 if (names[i]) {
378 ptr += strlen(names[i]);
379 if (n1 < 0) {
380 n1 = i;
381 } else {
382 n2 = i;
383 }
384 skip = 1;
385 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000386 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000387
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000388 /* handle optional range '-' */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000389
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000390 if (skip == 0) {
391 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000392 return NULL;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000393 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000394 if (*ptr == '-' && n2 < 0) {
395 ++ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000396 continue;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000397 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000398
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000399 /*
400 * collapse single-value ranges, handle skipmark, and fill
401 * in the character array appropriately.
402 */
403
404 if (n2 < 0) {
405 n2 = n1;
406 }
407 if (*ptr == '/') {
408 skip = strtol(ptr + 1, &ptr, 10);
409 }
410 /*
411 * fill array, using a failsafe is the easiest way to prevent
412 * an endless loop
413 */
414
415 {
416 int s0 = 1;
417 int failsafe = 1024;
418
419 --n1;
420 do {
421 n1 = (n1 + 1) % modvalue;
422
423 if (--s0 == 0) {
424 ary[n1 % modvalue] = 1;
425 s0 = skip;
426 }
427 }
428 while (n1 != n2 && --failsafe);
429
430 if (failsafe == 0) {
431 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000432 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000433 }
434 }
435 if (*ptr != ',') {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000436 break;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000437 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000438 ++ptr;
439 n1 = -1;
440 n2 = -1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000441 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000442
443 if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
444 crondlog("\111failed user %s parsing %s\n", user, base);
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000445 return NULL;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000446 }
447
448 while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
449 ++ptr;
450 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000451#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000452 if (DebugOpt) {
453 int i;
454
455 for (i = 0; i < modvalue; ++i) {
456 crondlog("\005%d", ary[i]);
457 }
458 crondlog("\005\n");
459 }
460#endif
461
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000462 return ptr;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000463}
464
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000465static void FixDayDow(CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000466{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000467 int i;
468 int weekUsed = 0;
469 int daysUsed = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000470
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000471 for (i = 0; i < (int)(arysize(line->cl_Dow)); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000472 if (line->cl_Dow[i] == 0) {
473 weekUsed = 1;
474 break;
475 }
476 }
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000477 for (i = 0; i < (int)(arysize(line->cl_Days)); ++i) {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000478 if (line->cl_Days[i] == 0) {
479 daysUsed = 1;
480 break;
481 }
482 }
483 if (weekUsed && !daysUsed) {
484 memset(line->cl_Days, 0, sizeof(line->cl_Days));
485 }
486 if (daysUsed && !weekUsed) {
487 memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
488 }
489}
490
491
492
493static void SynchronizeFile(const char *fileName)
494{
495 int maxEntries = MAXLINES;
496 int maxLines;
497 char buf[1024];
498
499 if (strcmp(fileName, "root") == 0) {
500 maxEntries = 65535;
501 }
502 maxLines = maxEntries * 10;
503
504 if (fileName) {
505 FILE *fi;
506
507 DeleteFile(fileName);
508
509 fi = fopen(fileName, "r");
510 if (fi != NULL) {
511 struct stat sbuf;
512
513 if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000514 CronFile *file = xzalloc(sizeof(CronFile));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000515 CronLine **pline;
516
517 file->cf_User = strdup(fileName);
518 pline = &file->cf_LineBase;
519
520 while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) {
521 CronLine line;
522 char *ptr;
523
Rob Landley828548a2005-09-01 10:23:57 +0000524 trim(buf);
525 if (buf[0] == 0 || buf[0] == '#') {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000526 continue;
527 }
528 if (--maxEntries == 0) {
529 break;
530 }
531 memset(&line, 0, sizeof(line));
532
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000533#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000534 if (DebugOpt) {
535 crondlog("\111User %s Entry %s\n", fileName, buf);
536 }
537#endif
538
539 /* parse date ranges */
540 ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf);
541 ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr);
542 ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr);
543 ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr);
544 ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr);
545
546 /* check failure */
547 if (ptr == NULL) {
548 continue;
549 }
550
551 /*
552 * fix days and dow - if one is not * and the other
553 * is *, the other is set to 0, and vise-versa
554 */
555
556 FixDayDow(&line);
557
Denis Vlasenko9b1381f2007-01-03 02:56:00 +0000558 *pline = xzalloc(sizeof(CronLine));
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000559 **pline = line;
560
561 /* copy command */
562 (*pline)->cl_Shell = strdup(ptr);
563
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000564#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000565 if (DebugOpt) {
566 crondlog("\111 Command %s\n", ptr);
567 }
568#endif
569
570 pline = &((*pline)->cl_Next);
571 }
572 *pline = NULL;
573
574 file->cf_Next = FileBase;
575 FileBase = file;
576
577 if (maxLines == 0 || maxEntries == 0) {
578 crondlog("\111Maximum number of lines reached for user %s\n", fileName);
579 }
580 }
581 fclose(fi);
582 }
583 }
584}
585
586static void CheckUpdates(void)
587{
588 FILE *fi;
589 char buf[256];
590
591 fi = fopen(CRONUPDATE, "r");
592 if (fi != NULL) {
593 remove(CRONUPDATE);
594 while (fgets(buf, sizeof(buf), fi) != NULL) {
595 SynchronizeFile(strtok(buf, " \t\r\n"));
596 }
597 fclose(fi);
598 }
599}
600
601static void SynchronizeDir(void)
602{
603 /* Attempt to delete the database. */
604
605 for (;;) {
606 CronFile *file;
607
608 for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next);
609 if (file == NULL) {
610 break;
611 }
612 DeleteFile(file->cf_User);
613 }
614
615 /*
616 * Remove cron update file
617 *
618 * Re-chdir, in case directory was renamed & deleted, or otherwise
619 * screwed up.
620 *
621 * scan directory and add associated users
622 */
623
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000624 remove(CRONUPDATE);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000625 if (chdir(CDir) < 0) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000626 crondlog("\311cannot find %s\n", CDir);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000627 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000628 {
629 DIR *dir = opendir(".");
630 struct dirent *den;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000631
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000632 if (dir) {
633 while ((den = readdir(dir))) {
634 if (strchr(den->d_name, '.') != NULL) {
635 continue;
636 }
637 if (getpwnam(den->d_name)) {
638 SynchronizeFile(den->d_name);
639 } else {
640 crondlog("\007ignoring %s\n", den->d_name);
641 }
642 }
643 closedir(dir);
644 } else {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000645 crondlog("\311cannot open current dir!\n");
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000646 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000647 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000648}
649
650
651/*
652 * DeleteFile() - delete user database
653 *
654 * Note: multiple entries for same user may exist if we were unable to
655 * completely delete a database due to running processes.
656 */
657
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000658static void DeleteFile(const char *userName)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000659{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000660 CronFile **pfile = &FileBase;
661 CronFile *file;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000662
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000663 while ((file = *pfile) != NULL) {
664 if (strcmp(userName, file->cf_User) == 0) {
665 CronLine **pline = &file->cf_LineBase;
666 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000667
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000668 file->cf_Running = 0;
669 file->cf_Deleted = 1;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000670
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000671 while ((line = *pline) != NULL) {
672 if (line->cl_Pid > 0) {
673 file->cf_Running = 1;
674 pline = &line->cl_Next;
675 } else {
676 *pline = line->cl_Next;
677 free(line->cl_Shell);
678 free(line);
679 }
680 }
681 if (file->cf_Running == 0) {
682 *pfile = file->cf_Next;
683 free(file->cf_User);
684 free(file);
685 } else {
686 pfile = &file->cf_Next;
687 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000688 } else {
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000689 pfile = &file->cf_Next;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000690 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000691 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000692}
693
694/*
695 * TestJobs()
696 *
697 * determine which jobs need to be run. Under normal conditions, the
698 * period is about a minute (one scan). Worst case it will be one
699 * hour (60 scans).
700 */
701
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000702static int TestJobs(time_t t1, time_t t2)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000703{
"Vladimir N. Oleynik"cd5c15d2006-01-30 13:36:03 +0000704 int nJobs = 0;
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000705 time_t t;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000706
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000707 /* Find jobs > t1 and <= t2 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000708
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000709 for (t = t1 - t1 % 60; t <= t2; t += 60) {
710 if (t > t1) {
711 struct tm *tp = localtime(&t);
712 CronFile *file;
713 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000714
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000715 for (file = FileBase; file; file = file->cf_Next) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000716#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000717 if (DebugOpt)
718 crondlog("\005FILE %s:\n", file->cf_User);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000719#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000720 if (file->cf_Deleted)
721 continue;
722 for (line = file->cf_LineBase; line; line = line->cl_Next) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000723#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000724 if (DebugOpt)
725 crondlog("\005 LINE %s\n", line->cl_Shell);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000726#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000727 if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
728 (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
729 && line->cl_Mons[tp->tm_mon]) {
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000730#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000731 if (DebugOpt) {
732 crondlog("\005 JobToDo: %d %s\n",
733 line->cl_Pid, line->cl_Shell);
734 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000735#endif
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000736 if (line->cl_Pid > 0) {
737 crondlog("\010 process already running: %s %s\n",
738 file->cf_User, line->cl_Shell);
739 } else if (line->cl_Pid == 0) {
740 line->cl_Pid = -1;
741 file->cf_Ready = 1;
742 ++nJobs;
743 }
744 }
745 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000746 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000747 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000748 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000749 return nJobs;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000750}
751
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000752static void RunJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000753{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000754 CronFile *file;
755 CronLine *line;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000756
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000757 for (file = FileBase; file; file = file->cf_Next) {
758 if (file->cf_Ready) {
759 file->cf_Ready = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000760
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000761 for (line = file->cf_LineBase; line; line = line->cl_Next) {
762 if (line->cl_Pid < 0) {
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000763
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000764 RunJob(file->cf_User, line);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000765
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000766 crondlog("\010USER %s pid %3d cmd %s\n",
767 file->cf_User, line->cl_Pid, line->cl_Shell);
768 if (line->cl_Pid < 0) {
769 file->cf_Ready = 1;
770 }
771 else if (line->cl_Pid > 0) {
772 file->cf_Running = 1;
773 }
774 }
775 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000776 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000777 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000778}
779
780/*
781 * CheckJobs() - check for job completion
782 *
783 * Check for job completion, return number of jobs still running after
784 * all done.
785 */
786
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000787static int CheckJobs(void)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000788{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000789 CronFile *file;
790 CronLine *line;
791 int nStillRunning = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000792
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000793 for (file = FileBase; file; file = file->cf_Next) {
794 if (file->cf_Running) {
795 file->cf_Running = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000796
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000797 for (line = file->cf_LineBase; line; line = line->cl_Next) {
798 if (line->cl_Pid > 0) {
799 int status;
800 int r = wait4(line->cl_Pid, &status, WNOHANG, NULL);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000801
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000802 if (r < 0 || r == line->cl_Pid) {
803 EndJob(file->cf_User, line);
804 if (line->cl_Pid) {
805 file->cf_Running = 1;
806 }
807 } else if (r == 0) {
808 file->cf_Running = 1;
809 }
810 }
811 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000812 }
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000813 nStillRunning += file->cf_Running;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000814 }
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000815 return nStillRunning;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000816}
817
818
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000819#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
Eric Andersen35e643b2003-07-28 07:40:39 +0000820static void
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000821ForkJob(const char *user, CronLine * line, int mailFd,
822 const char *prog, const char *cmd, const char *arg, const char *mailf)
Eric Andersen35e643b2003-07-28 07:40:39 +0000823{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000824 /* Fork as the user in question and run program */
825 pid_t pid = fork();
Eric Andersen35e643b2003-07-28 07:40:39 +0000826
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000827 line->cl_Pid = pid;
828 if (pid == 0) {
829 /* CHILD */
Eric Andersen35e643b2003-07-28 07:40:39 +0000830
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000831 /* Change running state to the user in question */
Eric Andersen35e643b2003-07-28 07:40:39 +0000832
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000833 if (ChangeUser(user) < 0) {
834 exit(0);
835 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000836#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000837 if (DebugOpt) {
838 crondlog("\005Child Running %s\n", prog);
839 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000840#endif
841
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000842 if (mailFd >= 0) {
843 dup2(mailFd, mailf != NULL);
844 dup2((mailf ? mailFd : 1), 2);
845 close(mailFd);
846 }
847 execl(prog, prog, cmd, arg, NULL);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000848 crondlog("\024cannot exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000849 if (mailf) {
850 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
851 }
852 exit(0);
853 } else if (pid < 0) {
854 /* FORK FAILED */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000855 crondlog("\024cannot fork, user %s\n", user);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000856 line->cl_Pid = 0;
857 if (mailf) {
858 remove(mailf);
859 }
860 } else if (mailf) {
861 /* PARENT, FORK SUCCESS
862 * rename mail-file based on pid of process
863 */
864 char mailFile2[128];
865
866 snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
867 rename(mailf, mailFile2);
Eric Andersen35e643b2003-07-28 07:40:39 +0000868 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000869 /*
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000870 * Close the mail file descriptor.. we can't just leave it open in
871 * a structure, closing it later, because we might run out of descriptors
Eric Andersen35e643b2003-07-28 07:40:39 +0000872 */
Eric Andersen35e643b2003-07-28 07:40:39 +0000873
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000874 if (mailFd >= 0) {
875 close(mailFd);
876 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000877}
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000878
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000879static void RunJob(const char *user, CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000880{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000881 char mailFile[128];
882 int mailFd;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000883
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000884 line->cl_Pid = 0;
885 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000886
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000887 /* open mail file - owner root so nobody can screw with it. */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000888
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000889 snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, getpid());
890 mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000891
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000892 if (mailFd >= 0) {
893 line->cl_MailFlag = 1;
894 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
895 line->cl_Shell);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000896 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000897 } else {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000898 crondlog("\024cannot create mail file user %s file %s, output to /dev/null\n", user, mailFile);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000899 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000900
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000901 ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000902}
903
904/*
905 * EndJob - called when job terminates and when mail terminates
906 */
907
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000908static void EndJob(const char *user, CronLine * line)
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000909{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000910 int mailFd;
911 char mailFile[128];
912 struct stat sbuf;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000913
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000914 /* No job */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000915
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000916 if (line->cl_Pid <= 0) {
917 line->cl_Pid = 0;
918 return;
919 }
920
921 /*
922 * End of job and no mail file
923 * End of sendmail job
924 */
925
926 snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, line->cl_Pid);
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000927 line->cl_Pid = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000928
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000929 if (line->cl_MailFlag != 1) {
930 return;
931 }
932 line->cl_MailFlag = 0;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000933
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000934 /*
935 * End of primary job - check for mail file. If size has increased and
936 * the file is still valid, we sendmail it.
937 */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000938
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000939 mailFd = open(mailFile, O_RDONLY);
940 remove(mailFile);
941 if (mailFd < 0) {
942 return;
943 }
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000944
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000945 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid || sbuf.st_nlink != 0 ||
946 sbuf.st_size == line->cl_MailPos || !S_ISREG(sbuf.st_mode)) {
947 close(mailFd);
948 return;
949 }
950 ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
Eric Andersen35e643b2003-07-28 07:40:39 +0000951}
952#else
Eric Andersenaff114c2004-04-14 17:51:38 +0000953/* crond without sendmail */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000954
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000955static void RunJob(const char *user, CronLine * line)
Eric Andersen35e643b2003-07-28 07:40:39 +0000956{
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000957 /* Fork as the user in question and run program */
958 pid_t pid = fork();
Eric Andersen35e643b2003-07-28 07:40:39 +0000959
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000960 if (pid == 0) {
961 /* CHILD */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000962
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000963 /* Change running state to the user in question */
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000964
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000965 if (ChangeUser(user) < 0) {
966 exit(0);
967 }
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000968#if ENABLE_DEBUG_CROND_OPTION
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000969 if (DebugOpt) {
970 crondlog("\005Child Running %s\n", DEFAULT_SHELL);
971 }
Eric Andersen35e643b2003-07-28 07:40:39 +0000972#endif
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000973
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000974 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000975 crondlog("\024cannot exec, user %s cmd %s -c %s\n", user,
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000976 DEFAULT_SHELL, line->cl_Shell);
977 exit(0);
978 } else if (pid < 0) {
979 /* FORK FAILED */
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000980 crondlog("\024cannot, user %s\n", user);
Glenn L McGrath9079ad02004-02-22 04:44:21 +0000981 pid = 0;
982 }
983 line->cl_Pid = pid;
Eric Andersenf6f7bfb2002-10-22 12:24:59 +0000984}
Bernhard Reutner-Fischeref216292006-05-20 14:14:05 +0000985#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */