2006-04-12 18:09:26 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-10-22 12:24:59 +00:00
|
|
|
/*
|
|
|
|
* crond -d[#] -c <crondir> -f -b
|
|
|
|
*
|
|
|
|
* run as root, but NOT setuid root
|
|
|
|
*
|
|
|
|
* Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
|
2007-08-18 15:48:00 +00:00
|
|
|
* (version 2.3.2)
|
2006-04-16 20:38:26 +00:00
|
|
|
* Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
|
2002-10-22 12:24:59 +00:00
|
|
|
*
|
2006-04-16 20:38:26 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2002-10-22 12:24:59 +00:00
|
|
|
*/
|
|
|
|
|
2006-08-03 15:41:12 +00:00
|
|
|
#include <sys/syslog.h>
|
2007-08-18 15:48:00 +00:00
|
|
|
#include "libbb.h"
|
2002-10-22 12:24:59 +00:00
|
|
|
|
|
|
|
#ifndef CRONTABS
|
|
|
|
#define CRONTABS "/var/spool/cron/crontabs"
|
|
|
|
#endif
|
|
|
|
#ifndef TMPDIR
|
|
|
|
#define TMPDIR "/var/spool/cron"
|
|
|
|
#endif
|
|
|
|
#ifndef SENDMAIL
|
2007-08-18 15:48:00 +00:00
|
|
|
#define SENDMAIL "sendmail"
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
#ifndef SENDMAIL_ARGS
|
2003-07-28 07:40:39 +00:00
|
|
|
#define SENDMAIL_ARGS "-ti", "oem"
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
#ifndef CRONUPDATE
|
|
|
|
#define CRONUPDATE "cron.update"
|
|
|
|
#endif
|
|
|
|
#ifndef MAXLINES
|
2004-02-22 04:44:21 +00:00
|
|
|
#define MAXLINES 256 /* max lines in non-root crontabs */
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct CronFile {
|
2004-02-22 04:44:21 +00:00
|
|
|
struct CronFile *cf_Next;
|
|
|
|
struct CronLine *cf_LineBase;
|
|
|
|
char *cf_User; /* username */
|
|
|
|
int cf_Ready; /* bool: one or more jobs ready */
|
|
|
|
int cf_Running; /* bool: one or more jobs running */
|
|
|
|
int cf_Deleted; /* marked for deletion, ignore */
|
2002-10-22 12:24:59 +00:00
|
|
|
} CronFile;
|
|
|
|
|
|
|
|
typedef struct CronLine {
|
2004-02-22 04:44:21 +00:00
|
|
|
struct CronLine *cl_Next;
|
|
|
|
char *cl_Shell; /* shell command */
|
|
|
|
pid_t cl_Pid; /* running pid, 0, or armed (-1) */
|
|
|
|
int cl_MailFlag; /* running pid is for mail */
|
|
|
|
int cl_MailPos; /* 'empty file' size */
|
|
|
|
char cl_Mins[60]; /* 0-59 */
|
|
|
|
char cl_Hrs[24]; /* 0-23 */
|
|
|
|
char cl_Days[32]; /* 1-31 */
|
|
|
|
char cl_Mons[12]; /* 0-11 */
|
|
|
|
char cl_Dow[7]; /* 0-6, beginning sunday */
|
2002-10-22 12:24:59 +00:00
|
|
|
} CronLine;
|
|
|
|
|
|
|
|
#define RUN_RANOUT 1
|
|
|
|
#define RUN_RUNNING 2
|
|
|
|
#define RUN_FAILED 3
|
|
|
|
|
|
|
|
#define DaemonUid 0
|
|
|
|
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2006-10-08 12:49:22 +00:00
|
|
|
static unsigned DebugOpt;
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-08 12:49:22 +00:00
|
|
|
static unsigned LogLevel = 8;
|
2004-02-22 04:44:21 +00:00
|
|
|
static const char *LogFile;
|
|
|
|
static const char *CDir = CRONTABS;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
|
|
|
static void startlogger(void);
|
|
|
|
|
|
|
|
static void CheckUpdates(void);
|
|
|
|
static void SynchronizeDir(void);
|
|
|
|
static int TestJobs(time_t t1, time_t t2);
|
|
|
|
static void RunJobs(void);
|
|
|
|
static int CheckJobs(void);
|
2003-07-28 07:40:39 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void RunJob(const char *user, CronLine * line);
|
|
|
|
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
2004-02-22 04:44:21 +00:00
|
|
|
static void EndJob(const char *user, CronLine * line);
|
2003-07-28 07:40:39 +00:00
|
|
|
#else
|
|
|
|
#define EndJob(user, line) line->cl_Pid = 0
|
|
|
|
#endif
|
2002-10-22 12:24:59 +00:00
|
|
|
|
|
|
|
static void DeleteFile(const char *userName);
|
|
|
|
|
|
|
|
static CronFile *FileBase;
|
|
|
|
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void crondlog(const char *ctl, ...)
|
2003-07-28 07:40:39 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
va_list va;
|
|
|
|
const char *fmt;
|
|
|
|
int level = (int) (ctl[0] & 0xf);
|
|
|
|
int type = level == 20 ?
|
|
|
|
LOG_ERR : ((ctl[0] & 0100) ? LOG_WARNING : LOG_NOTICE);
|
2003-07-28 07:40:39 +00:00
|
|
|
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
va_start(va, ctl);
|
|
|
|
fmt = ctl + 1;
|
|
|
|
if (level >= LogLevel) {
|
2003-07-28 07:40:39 +00:00
|
|
|
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
vfprintf(stderr, fmt, va);
|
|
|
|
} else
|
2003-07-28 07:40:39 +00:00
|
|
|
#endif
|
2004-02-22 04:44:21 +00:00
|
|
|
if (LogFile == 0) {
|
|
|
|
vsyslog(type, fmt, va);
|
|
|
|
} else {
|
2007-04-11 23:20:53 +00:00
|
|
|
#if !ENABLE_DEBUG_CROND_OPTION
|
2005-09-05 15:50:56 +00:00
|
|
|
int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
2007-04-11 23:20:53 +00:00
|
|
|
#else
|
|
|
|
int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
|
|
|
#endif
|
2004-02-22 04:44:21 +00:00
|
|
|
if (logfd >= 0) {
|
|
|
|
vdprintf(logfd, fmt, va);
|
|
|
|
close(logfd);
|
|
|
|
}
|
2003-07-28 07:40:39 +00:00
|
|
|
}
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
|
|
|
va_end(va);
|
|
|
|
if (ctl[0] & 0200) {
|
|
|
|
exit(20);
|
|
|
|
}
|
2003-07-28 07:40:39 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2004-02-22 04:44:21 +00:00
|
|
|
int crond_main(int ac, char **av)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2006-10-03 21:00:06 +00:00
|
|
|
unsigned opt;
|
2004-02-22 04:44:21 +00:00
|
|
|
char *lopt, *Lopt, *copt;
|
2007-03-26 13:20:54 +00:00
|
|
|
USE_DEBUG_CROND_OPTION(char *dopt;)
|
2004-02-22 04:44:21 +00:00
|
|
|
|
2007-03-26 13:20:54 +00:00
|
|
|
opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l");
|
2004-02-22 04:44:21 +00:00
|
|
|
opterr = 0; /* disable getopt 'errors' message. */
|
2007-08-19 13:42:08 +00:00
|
|
|
opt = getopt32(av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
|
2007-03-26 13:20:54 +00:00
|
|
|
&lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt));
|
|
|
|
if (opt & 1) /* -l */
|
2006-10-08 12:49:22 +00:00
|
|
|
LogLevel = xatou(lopt);
|
2007-03-26 13:20:54 +00:00
|
|
|
if (opt & 2) /* -L */
|
|
|
|
if (*Lopt)
|
2004-02-22 04:44:21 +00:00
|
|
|
LogFile = Lopt;
|
2007-03-26 13:20:54 +00:00
|
|
|
if (opt & 32) /* -c */
|
|
|
|
if (*copt)
|
2004-02-22 04:44:21 +00:00
|
|
|
CDir = copt;
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2007-03-26 13:20:54 +00:00
|
|
|
if (opt & 64) { /* -d */
|
2006-10-08 12:49:22 +00:00
|
|
|
DebugOpt = xatou(dopt);
|
2004-02-22 04:44:21 +00:00
|
|
|
LogLevel = 0;
|
|
|
|
}
|
2003-06-20 09:01:58 +00:00
|
|
|
#endif
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2007-03-26 13:20:54 +00:00
|
|
|
/* close stdin and stdout, stderr.
|
2004-02-22 04:44:21 +00:00
|
|
|
* close unused descriptors - don't need.
|
|
|
|
* optional detach from controlling terminal
|
|
|
|
*/
|
2007-03-26 13:20:54 +00:00
|
|
|
if (!(opt & 4))
|
|
|
|
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2007-03-26 13:20:54 +00:00
|
|
|
xchdir(CDir);
|
|
|
|
signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2007-03-26 13:20:54 +00:00
|
|
|
startlogger(); /* need if syslog mode selected */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/*
|
|
|
|
* main loop - synchronize to 1 second after the minute, minimum sleep
|
|
|
|
* of 1 second.
|
|
|
|
*/
|
2007-08-18 15:48:00 +00:00
|
|
|
crondlog("\011%s " BB_VER " started, log level %d\n",
|
2006-10-03 21:00:43 +00:00
|
|
|
applet_name, LogLevel);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
SynchronizeDir();
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
{
|
|
|
|
time_t t1 = time(NULL);
|
|
|
|
time_t t2;
|
|
|
|
long dt;
|
2006-01-30 13:36:03 +00:00
|
|
|
int rescan = 60;
|
2004-02-22 04:44:21 +00:00
|
|
|
short sleep_time = 60;
|
|
|
|
|
2007-03-27 22:01:31 +00:00
|
|
|
write_pidfile("/var/run/crond.pid");
|
2004-02-22 04:44:21 +00:00
|
|
|
for (;;) {
|
|
|
|
sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
|
|
|
|
|
|
|
|
t2 = time(NULL);
|
|
|
|
dt = t2 - t1;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The file 'cron.update' is checked to determine new cron
|
|
|
|
* jobs. The directory is rescanned once an hour to deal
|
|
|
|
* with any screwups.
|
|
|
|
*
|
|
|
|
* check for disparity. Disparities over an hour either way
|
|
|
|
* result in resynchronization. A reverse-indexed disparity
|
|
|
|
* less then an hour causes us to effectively sleep until we
|
|
|
|
* match the original time (i.e. no re-execution of jobs that
|
|
|
|
* have just been run). A forward-indexed disparity less then
|
|
|
|
* an hour causes intermediate jobs to be run, but only once
|
|
|
|
* in the worst case.
|
|
|
|
*
|
|
|
|
* when running jobs, the inequality used is greater but not
|
|
|
|
* equal to t1, and less then or equal to t2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (--rescan == 0) {
|
|
|
|
rescan = 60;
|
|
|
|
SynchronizeDir();
|
|
|
|
}
|
|
|
|
CheckUpdates();
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt)
|
|
|
|
crondlog("\005Wakeup dt=%d\n", dt);
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
2004-02-22 04:44:21 +00:00
|
|
|
if (dt < -60 * 60 || dt > 60 * 60) {
|
|
|
|
t1 = t2;
|
|
|
|
crondlog("\111time disparity of %d minutes detected\n", dt / 60);
|
|
|
|
} else if (dt > 0) {
|
|
|
|
TestJobs(t1, t2);
|
|
|
|
RunJobs();
|
|
|
|
sleep(5);
|
|
|
|
if (CheckJobs() > 0) {
|
|
|
|
sleep_time = 10;
|
|
|
|
} else {
|
|
|
|
sleep_time = 60;
|
|
|
|
}
|
|
|
|
t1 = t2;
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
return 0; /* not reached */
|
2003-12-23 07:21:33 +00:00
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static int ChangeUser(const char *user)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
struct passwd *pas;
|
|
|
|
const char *err_msg;
|
|
|
|
|
|
|
|
/*
|
2004-04-14 17:51:38 +00:00
|
|
|
* Obtain password entry and change privileges
|
2004-02-22 04:44:21 +00:00
|
|
|
*/
|
|
|
|
pas = getpwnam(user);
|
|
|
|
if (pas == 0) {
|
|
|
|
crondlog("\011failed to get uid for %s", user);
|
2006-10-26 23:21:47 +00:00
|
|
|
return -1;
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
|
|
|
setenv("USER", pas->pw_name, 1);
|
|
|
|
setenv("HOME", pas->pw_dir, 1);
|
|
|
|
setenv("SHELL", DEFAULT_SHELL, 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change running state to the user in question
|
|
|
|
*/
|
|
|
|
err_msg = change_identity_e2str(pas);
|
|
|
|
if (err_msg) {
|
|
|
|
crondlog("\011%s for user %s", err_msg, user);
|
2006-10-26 23:21:47 +00:00
|
|
|
return -1;
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
if (chdir(pas->pw_dir) < 0) {
|
2004-02-22 04:44:21 +00:00
|
|
|
crondlog("\011chdir failed: %s: %m", pas->pw_dir);
|
|
|
|
if (chdir(TMPDIR) < 0) {
|
|
|
|
crondlog("\011chdir failed: %s: %m", TMPDIR);
|
2006-10-26 23:21:47 +00:00
|
|
|
return -1;
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
return pas->pw_uid;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void startlogger(void)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
if (LogFile == 0) {
|
2006-10-03 21:00:43 +00:00
|
|
|
openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
else { /* test logfile */
|
|
|
|
int logfd;
|
|
|
|
|
2007-04-11 23:20:53 +00:00
|
|
|
logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
|
|
|
if (logfd >= 0) {
|
2004-02-22 04:44:21 +00:00
|
|
|
close(logfd);
|
|
|
|
}
|
|
|
|
}
|
2003-07-28 07:40:39 +00:00
|
|
|
#endif
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-19 18:49:21 +00:00
|
|
|
static const char DowAry[] ALIGN1 =
|
|
|
|
"sun""mon""tue""wed""thu""fri""sat"
|
|
|
|
/* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
|
|
|
|
;
|
|
|
|
|
|
|
|
static const char MonAry[] ALIGN1 =
|
|
|
|
"jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
|
|
|
|
/* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
|
|
|
|
;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static char *ParseField(char *user, char *ary, int modvalue, int off,
|
2007-08-19 18:49:21 +00:00
|
|
|
const char *names, char *ptr)
|
|
|
|
/* 'names' is a pointer to a set of 3-char abbreviations */
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
char *base = ptr;
|
|
|
|
int n1 = -1;
|
|
|
|
int n2 = -1;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (base == NULL) {
|
2006-10-26 23:21:47 +00:00
|
|
|
return NULL;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
|
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
/* Handle numeric digit or symbol or '*' */
|
|
|
|
|
|
|
|
if (*ptr == '*') {
|
|
|
|
n1 = 0; /* everything will be filled */
|
|
|
|
n2 = modvalue - 1;
|
|
|
|
skip = 1;
|
|
|
|
++ptr;
|
|
|
|
} else if (*ptr >= '0' && *ptr <= '9') {
|
|
|
|
if (n1 < 0) {
|
|
|
|
n1 = strtol(ptr, &ptr, 10) + off;
|
|
|
|
} else {
|
|
|
|
n2 = strtol(ptr, &ptr, 10) + off;
|
|
|
|
}
|
|
|
|
skip = 1;
|
|
|
|
} else if (names) {
|
|
|
|
int i;
|
|
|
|
|
2007-08-19 18:49:21 +00:00
|
|
|
for (i = 0; names[i]; i += 3) {
|
|
|
|
/* was using strncmp before... */
|
|
|
|
if (strncasecmp(ptr, &names[i], 3) == 0) {
|
|
|
|
ptr += 3;
|
|
|
|
if (n1 < 0) {
|
|
|
|
n1 = i / 3;
|
|
|
|
} else {
|
|
|
|
n2 = i / 3;
|
|
|
|
}
|
|
|
|
skip = 1;
|
2004-02-22 04:44:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* handle optional range '-' */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (skip == 0) {
|
|
|
|
crondlog("\111failed user %s parsing %s\n", user, base);
|
2006-10-26 23:21:47 +00:00
|
|
|
return NULL;
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
|
|
|
if (*ptr == '-' && n2 < 0) {
|
|
|
|
++ptr;
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/*
|
|
|
|
* collapse single-value ranges, handle skipmark, and fill
|
|
|
|
* in the character array appropriately.
|
|
|
|
*/
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (n2 < 0) {
|
|
|
|
n2 = n1;
|
|
|
|
}
|
|
|
|
if (*ptr == '/') {
|
|
|
|
skip = strtol(ptr + 1, &ptr, 10);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* fill array, using a failsafe is the easiest way to prevent
|
|
|
|
* an endless loop
|
|
|
|
*/
|
|
|
|
|
|
|
|
{
|
|
|
|
int s0 = 1;
|
|
|
|
int failsafe = 1024;
|
|
|
|
|
|
|
|
--n1;
|
|
|
|
do {
|
|
|
|
n1 = (n1 + 1) % modvalue;
|
|
|
|
|
|
|
|
if (--s0 == 0) {
|
|
|
|
ary[n1 % modvalue] = 1;
|
|
|
|
s0 = skip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (n1 != n2 && --failsafe);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (failsafe == 0) {
|
|
|
|
crondlog("\111failed user %s parsing %s\n", user, base);
|
2006-10-26 23:21:47 +00:00
|
|
|
return NULL;
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*ptr != ',') {
|
|
|
|
break;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2004-02-22 04:44:21 +00:00
|
|
|
++ptr;
|
|
|
|
n1 = -1;
|
|
|
|
n2 = -1;
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
|
2003-07-28 09:37:56 +00:00
|
|
|
crondlog("\111failed user %s parsing %s\n", user, base);
|
2006-10-26 23:21:47 +00:00
|
|
|
return NULL;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
|
|
|
|
++ptr;
|
|
|
|
}
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
int i;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
for (i = 0; i < modvalue; ++i) {
|
|
|
|
crondlog("\005%d", ary[i]);
|
|
|
|
}
|
|
|
|
crondlog("\005\n");
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-26 23:21:47 +00:00
|
|
|
return ptr;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void FixDayDow(CronLine * line)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2006-01-30 13:36:03 +00:00
|
|
|
int i;
|
|
|
|
int weekUsed = 0;
|
|
|
|
int daysUsed = 0;
|
2004-02-22 04:44:21 +00:00
|
|
|
|
2007-06-25 10:55:35 +00:00
|
|
|
for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Dow)); ++i) {
|
2004-02-22 04:44:21 +00:00
|
|
|
if (line->cl_Dow[i] == 0) {
|
|
|
|
weekUsed = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2007-06-25 10:55:35 +00:00
|
|
|
for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Days)); ++i) {
|
2004-02-22 04:44:21 +00:00
|
|
|
if (line->cl_Days[i] == 0) {
|
|
|
|
daysUsed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (weekUsed && !daysUsed) {
|
|
|
|
memset(line->cl_Days, 0, sizeof(line->cl_Days));
|
|
|
|
}
|
|
|
|
if (daysUsed && !weekUsed) {
|
|
|
|
memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void SynchronizeFile(const char *fileName)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
int maxEntries = MAXLINES;
|
|
|
|
int maxLines;
|
|
|
|
char buf[1024];
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (strcmp(fileName, "root") == 0) {
|
|
|
|
maxEntries = 65535;
|
|
|
|
}
|
|
|
|
maxLines = maxEntries * 10;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (fileName) {
|
|
|
|
FILE *fi;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
DeleteFile(fileName);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
fi = fopen(fileName, "r");
|
|
|
|
if (fi != NULL) {
|
|
|
|
struct stat sbuf;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
|
2007-01-03 02:56:00 +00:00
|
|
|
CronFile *file = xzalloc(sizeof(CronFile));
|
2004-02-22 04:44:21 +00:00
|
|
|
CronLine **pline;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
file->cf_User = strdup(fileName);
|
|
|
|
pline = &file->cf_LineBase;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) {
|
|
|
|
CronLine line;
|
|
|
|
char *ptr;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2005-09-01 10:23:57 +00:00
|
|
|
trim(buf);
|
|
|
|
if (buf[0] == 0 || buf[0] == '#') {
|
2004-02-22 04:44:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (--maxEntries == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
memset(&line, 0, sizeof(line));
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
crondlog("\111User %s Entry %s\n", fileName, buf);
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* parse date ranges */
|
|
|
|
ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf);
|
|
|
|
ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr);
|
|
|
|
ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr);
|
|
|
|
ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr);
|
|
|
|
ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* check failure */
|
|
|
|
if (ptr == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/*
|
|
|
|
* fix days and dow - if one is not * and the other
|
|
|
|
* is *, the other is set to 0, and vise-versa
|
|
|
|
*/
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
FixDayDow(&line);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2007-01-03 02:56:00 +00:00
|
|
|
*pline = xzalloc(sizeof(CronLine));
|
2004-02-22 04:44:21 +00:00
|
|
|
**pline = line;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* copy command */
|
|
|
|
(*pline)->cl_Shell = strdup(ptr);
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
crondlog("\111 Command %s\n", ptr);
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
pline = &((*pline)->cl_Next);
|
|
|
|
}
|
|
|
|
*pline = NULL;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
file->cf_Next = FileBase;
|
|
|
|
FileBase = file;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (maxLines == 0 || maxEntries == 0) {
|
|
|
|
crondlog("\111Maximum number of lines reached for user %s\n", fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fi);
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void CheckUpdates(void)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
FILE *fi;
|
|
|
|
char buf[256];
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
fi = fopen(CRONUPDATE, "r");
|
|
|
|
if (fi != NULL) {
|
|
|
|
remove(CRONUPDATE);
|
|
|
|
while (fgets(buf, sizeof(buf), fi) != NULL) {
|
|
|
|
SynchronizeFile(strtok(buf, " \t\r\n"));
|
|
|
|
}
|
|
|
|
fclose(fi);
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void SynchronizeDir(void)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
/* Attempt to delete the database. */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
for (;;) {
|
|
|
|
CronFile *file;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next);
|
|
|
|
if (file == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DeleteFile(file->cf_User);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove cron update file
|
|
|
|
*
|
|
|
|
* Re-chdir, in case directory was renamed & deleted, or otherwise
|
|
|
|
* screwed up.
|
|
|
|
*
|
|
|
|
* scan directory and add associated users
|
|
|
|
*/
|
|
|
|
|
|
|
|
remove(CRONUPDATE);
|
|
|
|
if (chdir(CDir) < 0) {
|
2006-11-18 22:04:09 +00:00
|
|
|
crondlog("\311cannot find %s\n", CDir);
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
DIR *dir = opendir(".");
|
|
|
|
struct dirent *den;
|
|
|
|
|
|
|
|
if (dir) {
|
|
|
|
while ((den = readdir(dir))) {
|
|
|
|
if (strchr(den->d_name, '.') != NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (getpwnam(den->d_name)) {
|
|
|
|
SynchronizeFile(den->d_name);
|
|
|
|
} else {
|
|
|
|
crondlog("\007ignoring %s\n", den->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
} else {
|
2006-11-18 22:04:09 +00:00
|
|
|
crondlog("\311cannot open current dir!\n");
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DeleteFile() - delete user database
|
|
|
|
*
|
|
|
|
* Note: multiple entries for same user may exist if we were unable to
|
|
|
|
* completely delete a database due to running processes.
|
|
|
|
*/
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void DeleteFile(const char *userName)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
CronFile **pfile = &FileBase;
|
|
|
|
CronFile *file;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
while ((file = *pfile) != NULL) {
|
|
|
|
if (strcmp(userName, file->cf_User) == 0) {
|
|
|
|
CronLine **pline = &file->cf_LineBase;
|
|
|
|
CronLine *line;
|
|
|
|
|
|
|
|
file->cf_Running = 0;
|
|
|
|
file->cf_Deleted = 1;
|
|
|
|
|
|
|
|
while ((line = *pline) != NULL) {
|
|
|
|
if (line->cl_Pid > 0) {
|
|
|
|
file->cf_Running = 1;
|
|
|
|
pline = &line->cl_Next;
|
|
|
|
} else {
|
|
|
|
*pline = line->cl_Next;
|
|
|
|
free(line->cl_Shell);
|
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file->cf_Running == 0) {
|
|
|
|
*pfile = file->cf_Next;
|
|
|
|
free(file->cf_User);
|
|
|
|
free(file);
|
|
|
|
} else {
|
|
|
|
pfile = &file->cf_Next;
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
} else {
|
2004-02-22 04:44:21 +00:00
|
|
|
pfile = &file->cf_Next;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TestJobs()
|
|
|
|
*
|
|
|
|
* determine which jobs need to be run. Under normal conditions, the
|
|
|
|
* period is about a minute (one scan). Worst case it will be one
|
|
|
|
* hour (60 scans).
|
|
|
|
*/
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static int TestJobs(time_t t1, time_t t2)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2006-01-30 13:36:03 +00:00
|
|
|
int nJobs = 0;
|
2004-02-22 04:44:21 +00:00
|
|
|
time_t t;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* Find jobs > t1 and <= t2 */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
for (t = t1 - t1 % 60; t <= t2; t += 60) {
|
|
|
|
if (t > t1) {
|
|
|
|
struct tm *tp = localtime(&t);
|
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
for (file = FileBase; file; file = file->cf_Next) {
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt)
|
|
|
|
crondlog("\005FILE %s:\n", file->cf_User);
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
2004-02-22 04:44:21 +00:00
|
|
|
if (file->cf_Deleted)
|
|
|
|
continue;
|
|
|
|
for (line = file->cf_LineBase; line; line = line->cl_Next) {
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt)
|
|
|
|
crondlog("\005 LINE %s\n", line->cl_Shell);
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
2004-02-22 04:44:21 +00:00
|
|
|
if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
|
|
|
|
(line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
|
|
|
|
&& line->cl_Mons[tp->tm_mon]) {
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
crondlog("\005 JobToDo: %d %s\n",
|
|
|
|
line->cl_Pid, line->cl_Shell);
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
2004-02-22 04:44:21 +00:00
|
|
|
if (line->cl_Pid > 0) {
|
|
|
|
crondlog("\010 process already running: %s %s\n",
|
|
|
|
file->cf_User, line->cl_Shell);
|
|
|
|
} else if (line->cl_Pid == 0) {
|
|
|
|
line->cl_Pid = -1;
|
|
|
|
file->cf_Ready = 1;
|
|
|
|
++nJobs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
return nJobs;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void RunJobs(void)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
|
|
|
|
|
|
|
for (file = FileBase; file; file = file->cf_Next) {
|
|
|
|
if (file->cf_Ready) {
|
|
|
|
file->cf_Ready = 0;
|
|
|
|
|
|
|
|
for (line = file->cf_LineBase; line; line = line->cl_Next) {
|
|
|
|
if (line->cl_Pid < 0) {
|
|
|
|
|
|
|
|
RunJob(file->cf_User, line);
|
|
|
|
|
|
|
|
crondlog("\010USER %s pid %3d cmd %s\n",
|
|
|
|
file->cf_User, line->cl_Pid, line->cl_Shell);
|
|
|
|
if (line->cl_Pid < 0) {
|
|
|
|
file->cf_Ready = 1;
|
|
|
|
}
|
|
|
|
else if (line->cl_Pid > 0) {
|
|
|
|
file->cf_Running = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CheckJobs() - check for job completion
|
|
|
|
*
|
|
|
|
* Check for job completion, return number of jobs still running after
|
|
|
|
* all done.
|
|
|
|
*/
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static int CheckJobs(void)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
|
|
|
int nStillRunning = 0;
|
|
|
|
|
|
|
|
for (file = FileBase; file; file = file->cf_Next) {
|
|
|
|
if (file->cf_Running) {
|
|
|
|
file->cf_Running = 0;
|
|
|
|
|
|
|
|
for (line = file->cf_LineBase; line; line = line->cl_Next) {
|
|
|
|
if (line->cl_Pid > 0) {
|
|
|
|
int status;
|
|
|
|
int r = wait4(line->cl_Pid, &status, WNOHANG, NULL);
|
|
|
|
|
|
|
|
if (r < 0 || r == line->cl_Pid) {
|
|
|
|
EndJob(file->cf_User, line);
|
|
|
|
if (line->cl_Pid) {
|
|
|
|
file->cf_Running = 1;
|
|
|
|
}
|
|
|
|
} else if (r == 0) {
|
|
|
|
file->cf_Running = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2004-02-22 04:44:21 +00:00
|
|
|
nStillRunning += file->cf_Running;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2006-10-26 23:21:47 +00:00
|
|
|
return nStillRunning;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
2002-10-22 12:24:59 +00:00
|
|
|
static void
|
2004-02-22 04:44:21 +00:00
|
|
|
ForkJob(const char *user, CronLine * line, int mailFd,
|
|
|
|
const char *prog, const char *cmd, const char *arg, const char *mailf)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
/* Fork as the user in question and run program */
|
|
|
|
pid_t pid = fork();
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
line->cl_Pid = pid;
|
|
|
|
if (pid == 0) {
|
|
|
|
/* CHILD */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* Change running state to the user in question */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (ChangeUser(user) < 0) {
|
|
|
|
exit(0);
|
|
|
|
}
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
crondlog("\005Child Running %s\n", prog);
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
#endif
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (mailFd >= 0) {
|
|
|
|
dup2(mailFd, mailf != NULL);
|
|
|
|
dup2((mailf ? mailFd : 1), 2);
|
|
|
|
close(mailFd);
|
|
|
|
}
|
|
|
|
execl(prog, prog, cmd, arg, NULL);
|
2006-11-18 22:04:09 +00:00
|
|
|
crondlog("\024cannot exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
|
2004-02-22 04:44:21 +00:00
|
|
|
if (mailf) {
|
|
|
|
fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
} else if (pid < 0) {
|
|
|
|
/* FORK FAILED */
|
2006-10-20 13:28:22 +00:00
|
|
|
crondlog("\024cannot fork, user %s\n", user);
|
2004-02-22 04:44:21 +00:00
|
|
|
line->cl_Pid = 0;
|
|
|
|
if (mailf) {
|
|
|
|
remove(mailf);
|
|
|
|
}
|
|
|
|
} else if (mailf) {
|
|
|
|
/* PARENT, FORK SUCCESS
|
|
|
|
* rename mail-file based on pid of process
|
|
|
|
*/
|
|
|
|
char mailFile2[128];
|
|
|
|
|
|
|
|
snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
|
|
|
|
rename(mailf, mailFile2);
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
/*
|
2004-02-22 04:44:21 +00:00
|
|
|
* Close the mail file descriptor.. we can't just leave it open in
|
|
|
|
* a structure, closing it later, because we might run out of descriptors
|
2002-10-22 12:24:59 +00:00
|
|
|
*/
|
2004-02-22 04:44:21 +00:00
|
|
|
|
|
|
|
if (mailFd >= 0) {
|
|
|
|
close(mailFd);
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void RunJob(const char *user, CronLine * line)
|
2003-07-28 07:40:39 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
char mailFile[128];
|
|
|
|
int mailFd;
|
|
|
|
|
|
|
|
line->cl_Pid = 0;
|
|
|
|
line->cl_MailFlag = 0;
|
|
|
|
|
|
|
|
/* open mail file - owner root so nobody can screw with it. */
|
|
|
|
|
|
|
|
snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, getpid());
|
|
|
|
mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
|
|
|
|
|
|
|
|
if (mailFd >= 0) {
|
|
|
|
line->cl_MailFlag = 1;
|
|
|
|
fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
|
|
|
|
line->cl_Shell);
|
2006-10-14 02:23:43 +00:00
|
|
|
line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
|
2004-02-22 04:44:21 +00:00
|
|
|
} else {
|
2006-11-18 22:04:09 +00:00
|
|
|
crondlog("\024cannot create mail file user %s file %s, output to /dev/null\n", user, mailFile);
|
2004-02-22 04:44:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
|
2003-07-28 07:40:39 +00:00
|
|
|
}
|
|
|
|
|
2002-10-22 12:24:59 +00:00
|
|
|
/*
|
|
|
|
* EndJob - called when job terminates and when mail terminates
|
|
|
|
*/
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
static void EndJob(const char *user, CronLine * line)
|
2002-10-22 12:24:59 +00:00
|
|
|
{
|
2004-02-22 04:44:21 +00:00
|
|
|
int mailFd;
|
|
|
|
char mailFile[128];
|
|
|
|
struct stat sbuf;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
/* No job */
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
if (line->cl_Pid <= 0) {
|
|
|
|
line->cl_Pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
2002-10-22 12:24:59 +00:00
|
|
|
|
|
|
|
/*
|
2004-02-22 04:44:21 +00:00
|
|
|
* End of job and no mail file
|
|
|
|
* End of sendmail job
|
2002-10-22 12:24:59 +00:00
|
|
|
*/
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, line->cl_Pid);
|
|
|
|
line->cl_Pid = 0;
|
|
|
|
|
|
|
|
if (line->cl_MailFlag != 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
line->cl_MailFlag = 0;
|
2002-10-22 12:24:59 +00:00
|
|
|
|
|
|
|
/*
|
2004-02-22 04:44:21 +00:00
|
|
|
* End of primary job - check for mail file. If size has increased and
|
|
|
|
* the file is still valid, we sendmail it.
|
2002-10-22 12:24:59 +00:00
|
|
|
*/
|
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
mailFd = open(mailFile, O_RDONLY);
|
|
|
|
remove(mailFile);
|
|
|
|
if (mailFd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2003-07-28 07:40:39 +00:00
|
|
|
|
2007-08-18 15:48:00 +00:00
|
|
|
if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid
|
|
|
|
|| sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos
|
|
|
|
|| !S_ISREG(sbuf.st_mode)
|
|
|
|
) {
|
2004-02-22 04:44:21 +00:00
|
|
|
close(mailFd);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
|
|
|
|
}
|
|
|
|
#else
|
2004-04-14 17:51:38 +00:00
|
|
|
/* crond without sendmail */
|
2004-02-22 04:44:21 +00:00
|
|
|
|
|
|
|
static void RunJob(const char *user, CronLine * line)
|
|
|
|
{
|
|
|
|
/* Fork as the user in question and run program */
|
|
|
|
pid_t pid = fork();
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
/* CHILD */
|
|
|
|
|
|
|
|
/* Change running state to the user in question */
|
|
|
|
|
|
|
|
if (ChangeUser(user) < 0) {
|
|
|
|
exit(0);
|
|
|
|
}
|
2006-05-20 14:14:05 +00:00
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2004-02-22 04:44:21 +00:00
|
|
|
if (DebugOpt) {
|
|
|
|
crondlog("\005Child Running %s\n", DEFAULT_SHELL);
|
|
|
|
}
|
2003-07-28 07:40:39 +00:00
|
|
|
#endif
|
2002-10-22 12:24:59 +00:00
|
|
|
|
2004-02-22 04:44:21 +00:00
|
|
|
execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
|
2006-11-18 22:04:09 +00:00
|
|
|
crondlog("\024cannot exec, user %s cmd %s -c %s\n", user,
|
2004-02-22 04:44:21 +00:00
|
|
|
DEFAULT_SHELL, line->cl_Shell);
|
|
|
|
exit(0);
|
|
|
|
} else if (pid < 0) {
|
|
|
|
/* FORK FAILED */
|
2006-10-20 13:28:22 +00:00
|
|
|
crondlog("\024cannot, user %s\n", user);
|
2004-02-22 04:44:21 +00:00
|
|
|
pid = 0;
|
|
|
|
}
|
|
|
|
line->cl_Pid = pid;
|
2002-10-22 12:24:59 +00:00
|
|
|
}
|
2006-05-20 14:14:05 +00:00
|
|
|
#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */
|