2006-04-12 23:39:26 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-10-22 17:54:59 +05:30
|
|
|
/*
|
|
|
|
* 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 21:18:00 +05:30
|
|
|
* (version 2.3.2)
|
2006-04-17 02:08:26 +05:30
|
|
|
* Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
|
2002-10-22 17:54:59 +05:30
|
|
|
*
|
2006-04-17 02:08:26 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2002-10-22 17:54:59 +05:30
|
|
|
*/
|
|
|
|
|
2007-08-18 21:18:00 +05:30
|
|
|
#include "libbb.h"
|
2008-01-27 18:20:12 +05:30
|
|
|
#include <syslog.h>
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
/* glibc frees previous setenv'ed value when we do next setenv()
|
|
|
|
* of the same variable. uclibc does not do this! */
|
|
|
|
#if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */
|
|
|
|
#define SETENV_LEAKS 0
|
|
|
|
#else
|
|
|
|
#define SETENV_LEAKS 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
#ifndef CRONTABS
|
|
|
|
#define CRONTABS "/var/spool/cron/crontabs"
|
|
|
|
#endif
|
|
|
|
#ifndef TMPDIR
|
|
|
|
#define TMPDIR "/var/spool/cron"
|
|
|
|
#endif
|
|
|
|
#ifndef SENDMAIL
|
2007-08-18 21:18:00 +05:30
|
|
|
#define SENDMAIL "sendmail"
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
#ifndef SENDMAIL_ARGS
|
2008-06-29 03:16:41 +05:30
|
|
|
#define SENDMAIL_ARGS "-ti", "oem"
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
#ifndef CRONUPDATE
|
|
|
|
#define CRONUPDATE "cron.update"
|
|
|
|
#endif
|
|
|
|
#ifndef MAXLINES
|
2004-02-22 10:14:21 +05:30
|
|
|
#define MAXLINES 256 /* max lines in non-root crontabs */
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
typedef struct CronFile {
|
2004-02-22 10:14:21 +05:30
|
|
|
struct CronFile *cf_Next;
|
|
|
|
struct CronLine *cf_LineBase;
|
2008-03-13 03:40:25 +05:30
|
|
|
char *cf_User; /* username */
|
|
|
|
smallint cf_Ready; /* bool: one or more jobs ready */
|
|
|
|
smallint cf_Running; /* bool: one or more jobs running */
|
|
|
|
smallint cf_Deleted; /* marked for deletion, ignore */
|
2002-10-22 17:54:59 +05:30
|
|
|
} CronFile;
|
|
|
|
|
|
|
|
typedef struct CronLine {
|
2004-02-22 10:14:21 +05:30
|
|
|
struct CronLine *cl_Next;
|
2008-03-13 03:40:25 +05:30
|
|
|
char *cl_Shell; /* shell command */
|
|
|
|
pid_t cl_Pid; /* running pid, 0, or armed (-1) */
|
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
int cl_MailPos; /* 'empty file' size */
|
|
|
|
smallint cl_MailFlag; /* running pid is for mail */
|
2008-04-08 02:32:35 +05:30
|
|
|
char *cl_MailTo; /* whom to mail results */
|
2008-03-13 03:40:25 +05:30
|
|
|
#endif
|
|
|
|
/* ordered by size, not in natural order. makes code smaller: */
|
|
|
|
char cl_Dow[7]; /* 0-6, beginning sunday */
|
|
|
|
char cl_Mons[12]; /* 0-11 */
|
|
|
|
char cl_Hrs[24]; /* 0-23 */
|
|
|
|
char cl_Days[32]; /* 1-31 */
|
|
|
|
char cl_Mins[60]; /* 0-59 */
|
2002-10-22 17:54:59 +05:30
|
|
|
} CronLine;
|
|
|
|
|
|
|
|
|
|
|
|
#define DaemonUid 0
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_l = (1 << 0),
|
|
|
|
OPT_L = (1 << 1),
|
|
|
|
OPT_f = (1 << 2),
|
|
|
|
OPT_b = (1 << 3),
|
|
|
|
OPT_S = (1 << 4),
|
|
|
|
OPT_c = (1 << 5),
|
|
|
|
OPT_d = (1 << 6) * ENABLE_DEBUG_CROND_OPTION,
|
|
|
|
};
|
2006-05-20 19:44:05 +05:30
|
|
|
#if ENABLE_DEBUG_CROND_OPTION
|
2008-03-13 03:40:25 +05:30
|
|
|
#define DebugOpt (option_mask32 & OPT_d)
|
|
|
|
#else
|
|
|
|
#define DebugOpt 0
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
struct globals {
|
|
|
|
unsigned LogLevel; /* = 8; */
|
|
|
|
const char *LogFile;
|
|
|
|
const char *CDir; /* = CRONTABS; */
|
|
|
|
CronFile *FileBase;
|
|
|
|
#if SETENV_LEAKS
|
|
|
|
char *env_var_user;
|
|
|
|
char *env_var_home;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
|
|
|
#define LogLevel (G.LogLevel )
|
|
|
|
#define LogFile (G.LogFile )
|
|
|
|
#define CDir (G.CDir )
|
|
|
|
#define FileBase (G.FileBase )
|
|
|
|
#define env_var_user (G.env_var_user )
|
|
|
|
#define env_var_home (G.env_var_home )
|
|
|
|
#define INIT_G() do { \
|
|
|
|
LogLevel = 8; \
|
|
|
|
CDir = CRONTABS; \
|
|
|
|
} while (0)
|
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
|
|
|
|
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);
|
2008-03-13 03:40:25 +05:30
|
|
|
static void RunJob(const char *user, CronLine *line);
|
2006-05-20 19:44:05 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
2008-03-13 03:40:25 +05:30
|
|
|
static void EndJob(const char *user, CronLine *line);
|
2003-07-28 13:10:39 +05:30
|
|
|
#else
|
2008-03-13 03:40:25 +05:30
|
|
|
#define EndJob(user, line) ((line)->cl_Pid = 0)
|
2003-07-28 13:10:39 +05:30
|
|
|
#endif
|
2002-10-22 17:54:59 +05:30
|
|
|
static void DeleteFile(const char *userName);
|
|
|
|
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
#define LVL5 "\x05"
|
|
|
|
#define LVL7 "\x07"
|
|
|
|
#define LVL8 "\x08"
|
|
|
|
#define LVL9 "\x09"
|
|
|
|
#define WARN9 "\x49"
|
|
|
|
#define DIE9 "\xc9"
|
|
|
|
/* level >= 20 is "error" */
|
|
|
|
#define ERR20 "\x14"
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
static void crondlog(const char *ctl, ...)
|
2003-07-28 13:10:39 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
va_list va;
|
2008-03-13 03:40:25 +05:30
|
|
|
int level = (ctl[0] & 0x1f);
|
2003-07-28 13:10:39 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
va_start(va, ctl);
|
2008-05-13 07:57:31 +05:30
|
|
|
if (level >= (int)LogLevel) {
|
2008-03-13 03:40:25 +05:30
|
|
|
/* Debug mode: all to (non-redirected) stderr, */
|
|
|
|
/* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
|
|
|
|
if (!DebugOpt && LogFile) {
|
|
|
|
/* Otherwise (log to file): we reopen log file at every write: */
|
2007-04-12 04:50:53 +05:30
|
|
|
int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
|
2008-03-13 03:40:25 +05:30
|
|
|
if (logfd >= 0)
|
|
|
|
xmove_fd(logfd, STDERR_FILENO);
|
2003-07-28 13:10:39 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
// TODO: ERR -> error, WARN -> warning, LVL -> info
|
|
|
|
bb_verror_msg(ctl + 1, va, /* strerr: */ NULL);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
va_end(va);
|
2008-03-13 03:40:25 +05:30
|
|
|
if (ctl[0] & 0x80)
|
2004-02-22 10:14:21 +05:30
|
|
|
exit(20);
|
2003-07-28 13:10:39 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int crond_main(int argc UNUSED_PARAM, char **argv)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2006-10-04 02:30:06 +05:30
|
|
|
unsigned opt;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
INIT_G();
|
|
|
|
|
|
|
|
/* "-b after -f is ignored", and so on for every pair a-b */
|
|
|
|
opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l")
|
|
|
|
":l+:d+"; /* -l and -d have numeric param */
|
2008-03-17 14:59:43 +05:30
|
|
|
opt = getopt32(argv, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
|
2008-03-13 03:40:25 +05:30
|
|
|
&LogLevel, &LogFile, &CDir
|
|
|
|
USE_DEBUG_CROND_OPTION(,&LogLevel));
|
|
|
|
/* both -d N and -l N set the same variable: LogLevel */
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (!(opt & OPT_f)) {
|
|
|
|
/* close stdin, stdout, stderr.
|
|
|
|
* close unused descriptors - don't need them. */
|
2008-03-20 19:34:30 +05:30
|
|
|
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (!DebugOpt && LogFile == NULL) {
|
|
|
|
/* logging to syslog */
|
|
|
|
openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
|
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
xchdir(CDir);
|
|
|
|
//signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
|
2008-09-20 21:58:59 +05:30
|
|
|
xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(LVL9 "crond (busybox "BB_VER") started, log level %d", LogLevel);
|
2004-02-22 10:14:21 +05:30
|
|
|
SynchronizeDir();
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
/* main loop - synchronize to 1 second after the minute, minimum sleep
|
|
|
|
* of 1 second. */
|
2004-02-22 10:14:21 +05:30
|
|
|
{
|
|
|
|
time_t t1 = time(NULL);
|
|
|
|
time_t t2;
|
|
|
|
long dt;
|
2006-01-30 19:06:03 +05:30
|
|
|
int rescan = 60;
|
2008-03-13 03:40:25 +05:30
|
|
|
int sleep_time = 60;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2007-03-28 03:31:31 +05:30
|
|
|
write_pidfile("/var/run/crond.pid");
|
2004-02-22 10:14:21 +05:30
|
|
|
for (;;) {
|
2008-03-13 03:40:25 +05:30
|
|
|
sleep((sleep_time + 1) - (time(NULL) % sleep_time));
|
2004-02-22 10:14:21 +05:30
|
|
|
|
|
|
|
t2 = time(NULL);
|
2008-03-13 03:40:25 +05:30
|
|
|
dt = (long)t2 - (long)t1;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* 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();
|
|
|
|
if (DebugOpt)
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(LVL5 "wakeup dt=%ld", dt);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (dt < -60 * 60 || dt > 60 * 60) {
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(WARN9 "time disparity of %d minutes detected", dt / 60);
|
2004-02-22 10:14:21 +05:30
|
|
|
} else if (dt > 0) {
|
|
|
|
TestJobs(t1, t2);
|
|
|
|
RunJobs();
|
|
|
|
sleep(5);
|
|
|
|
if (CheckJobs() > 0) {
|
|
|
|
sleep_time = 10;
|
|
|
|
} else {
|
|
|
|
sleep_time = 60;
|
|
|
|
}
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
t1 = t2;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2006-10-27 04:51:47 +05:30
|
|
|
return 0; /* not reached */
|
2003-12-23 12:51:33 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
#if SETENV_LEAKS
|
|
|
|
/* We set environment *before* vfork (because we want to use vfork),
|
|
|
|
* so we cannot use setenv() - repeated calls to setenv() may leak memory!
|
|
|
|
* Using putenv(), and freeing memory after unsetenv() won't leak */
|
|
|
|
static void safe_setenv4(char **pvar_val, const char *var, const char *val /*, int len*/)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
const int len = 4; /* both var names are 4 char long */
|
|
|
|
char *var_val = *pvar_val;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (var_val) {
|
|
|
|
var_val[len] = '\0'; /* nuke '=' */
|
|
|
|
unsetenv(var_val);
|
|
|
|
free(var_val);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
*pvar_val = xasprintf("%s=%s", var, val);
|
|
|
|
putenv(*pvar_val);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void SetEnv(struct passwd *pas)
|
|
|
|
{
|
|
|
|
#if SETENV_LEAKS
|
|
|
|
safe_setenv4(&env_var_user, "USER", pas->pw_name);
|
|
|
|
safe_setenv4(&env_var_home, "HOME", pas->pw_dir);
|
|
|
|
/* if we want to set user's shell instead: */
|
|
|
|
/*safe_setenv(env_var_user, "SHELL", pas->pw_shell, 5);*/
|
|
|
|
#else
|
2008-09-20 21:58:59 +05:30
|
|
|
xsetenv("USER", pas->pw_name);
|
|
|
|
xsetenv("HOME", pas->pw_dir);
|
2008-03-13 03:40:25 +05:30
|
|
|
#endif
|
|
|
|
/* currently, we use constant one: */
|
|
|
|
/*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
static void ChangeUser(struct passwd *pas)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
/* careful: we're after vfork! */
|
|
|
|
change_identity(pas); /* - initgroups, setgid, setuid */
|
|
|
|
if (chdir(pas->pw_dir) < 0) {
|
|
|
|
crondlog(LVL9 "can't chdir(%s)", pas->pw_dir);
|
|
|
|
if (chdir(TMPDIR) < 0) {
|
|
|
|
crondlog(DIE9 "can't chdir(%s)", TMPDIR); /* exits */
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2007-08-20 00:19:21 +05:30
|
|
|
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 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
static void ParseField(char *user, char *ary, int modvalue, int off,
|
2007-08-20 00:19:21 +05:30
|
|
|
const char *names, char *ptr)
|
|
|
|
/* 'names' is a pointer to a set of 3-char abbreviations */
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
char *base = ptr;
|
|
|
|
int n1 = -1;
|
|
|
|
int n2 = -1;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
// this can't happen due to config_read()
|
|
|
|
/*if (base == NULL)
|
|
|
|
return;*/
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
while (1) {
|
2004-02-22 10:14:21 +05:30
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
/* Handle numeric digit or symbol or '*' */
|
|
|
|
if (*ptr == '*') {
|
|
|
|
n1 = 0; /* everything will be filled */
|
|
|
|
n2 = modvalue - 1;
|
|
|
|
skip = 1;
|
|
|
|
++ptr;
|
2008-03-13 03:40:25 +05:30
|
|
|
} else if (isdigit(*ptr)) {
|
2004-02-22 10:14:21 +05:30
|
|
|
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-20 00:19:21 +05:30
|
|
|
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 10:14:21 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/* handle optional range '-' */
|
|
|
|
if (skip == 0) {
|
2008-07-17 03:42:18 +05:30
|
|
|
goto err;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
if (*ptr == '-' && n2 < 0) {
|
|
|
|
++ptr;
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/*
|
|
|
|
* collapse single-value ranges, handle skipmark, and fill
|
|
|
|
* in the character array appropriately.
|
|
|
|
*/
|
|
|
|
if (n2 < 0) {
|
|
|
|
n2 = n1;
|
|
|
|
}
|
|
|
|
if (*ptr == '/') {
|
|
|
|
skip = strtol(ptr + 1, &ptr, 10);
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
if (--failsafe == 0) {
|
2008-07-17 03:42:18 +05:30
|
|
|
goto err;
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
} while (n1 != n2);
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
if (*ptr != ',') {
|
|
|
|
break;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2004-02-22 10:14:21 +05:30
|
|
|
++ptr;
|
|
|
|
n1 = -1;
|
|
|
|
n2 = -1;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
if (*ptr) {
|
|
|
|
err:
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(WARN9 "user %s: parse error at %s", user, base);
|
2008-07-17 03:42:18 +05:30
|
|
|
return;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (DebugOpt && (LogLevel <= 5)) { /* like LVL5 */
|
|
|
|
/* can't use crondlog, it inserts '\n' */
|
2004-02-22 10:14:21 +05:30
|
|
|
int i;
|
2008-03-13 03:40:25 +05:30
|
|
|
for (i = 0; i < modvalue; ++i)
|
|
|
|
fprintf(stderr, "%d", (unsigned char)ary[i]);
|
|
|
|
fputc('\n', stderr);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
static void FixDayDow(CronLine *line)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-05-16 03:00:45 +05:30
|
|
|
unsigned i;
|
2006-01-30 19:06:03 +05:30
|
|
|
int weekUsed = 0;
|
|
|
|
int daysUsed = 0;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
|
2004-02-22 10:14:21 +05:30
|
|
|
if (line->cl_Dow[i] == 0) {
|
|
|
|
weekUsed = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
|
2004-02-22 10:14:21 +05:30
|
|
|
if (line->cl_Days[i] == 0) {
|
|
|
|
daysUsed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
if (weekUsed != daysUsed) {
|
|
|
|
if (weekUsed)
|
|
|
|
memset(line->cl_Days, 0, sizeof(line->cl_Days));
|
|
|
|
else /* daysUsed */
|
|
|
|
memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
static void SynchronizeFile(const char *fileName)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-07-17 17:29:13 +05:30
|
|
|
struct parser_t *parser;
|
2008-03-13 03:40:25 +05:30
|
|
|
struct stat sbuf;
|
2004-02-22 10:14:21 +05:30
|
|
|
int maxLines;
|
2008-07-17 03:42:18 +05:30
|
|
|
char *tokens[6];
|
2008-04-08 02:32:35 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
char *mailTo = NULL;
|
|
|
|
#endif
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (!fileName)
|
|
|
|
return;
|
|
|
|
|
|
|
|
DeleteFile(fileName);
|
2008-07-17 17:29:13 +05:30
|
|
|
parser = config_open(fileName);
|
|
|
|
if (!parser)
|
2008-03-13 03:40:25 +05:30
|
|
|
return;
|
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 17:29:13 +05:30
|
|
|
if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
|
2008-03-13 03:40:25 +05:30
|
|
|
CronFile *file = xzalloc(sizeof(CronFile));
|
|
|
|
CronLine **pline;
|
2008-07-17 03:42:18 +05:30
|
|
|
int n;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
file->cf_User = xstrdup(fileName);
|
|
|
|
pline = &file->cf_LineBase;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-27 04:38:31 +05:30
|
|
|
while (1) {
|
2008-03-13 03:40:25 +05:30
|
|
|
CronLine *line;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-27 04:38:31 +05:30
|
|
|
if (!--maxLines)
|
|
|
|
break;
|
|
|
|
n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
|
|
|
|
if (!n)
|
|
|
|
break;
|
|
|
|
|
2008-07-20 04:27:00 +05:30
|
|
|
if (DebugOpt)
|
|
|
|
crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
|
2008-07-17 03:42:18 +05:30
|
|
|
|
2008-04-08 02:32:35 +05:30
|
|
|
/* check if line is setting MAILTO= */
|
2008-07-17 03:42:18 +05:30
|
|
|
if (0 == strncmp(tokens[0], "MAILTO=", 7)) {
|
2008-04-08 02:32:35 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
free(mailTo);
|
2008-07-17 03:42:18 +05:30
|
|
|
mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
|
2008-04-08 02:32:35 +05:30
|
|
|
#endif /* otherwise just ignore such lines */
|
|
|
|
continue;
|
|
|
|
}
|
2008-07-17 03:42:18 +05:30
|
|
|
/* check if a minimum of tokens is specified */
|
2008-07-19 14:57:19 +05:30
|
|
|
if (n < 6)
|
2008-07-17 03:42:18 +05:30
|
|
|
continue;
|
2008-07-27 04:38:31 +05:30
|
|
|
*pline = line = xzalloc(sizeof(*line));
|
2008-03-13 03:40:25 +05:30
|
|
|
/* parse date ranges */
|
2008-07-17 03:42:18 +05:30
|
|
|
ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, tokens[0]);
|
|
|
|
ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, tokens[1]);
|
|
|
|
ParseField(file->cf_User, line->cl_Days, 32, 0, NULL, tokens[2]);
|
|
|
|
ParseField(file->cf_User, line->cl_Mons, 12, -1, MonAry, tokens[3]);
|
|
|
|
ParseField(file->cf_User, line->cl_Dow, 7, 0, DowAry, tokens[4]);
|
2008-03-13 03:40:25 +05:30
|
|
|
/*
|
2008-04-08 02:32:35 +05:30
|
|
|
* fix days and dow - if one is not "*" and the other
|
|
|
|
* is "*", the other is set to 0, and vise-versa
|
2008-03-13 03:40:25 +05:30
|
|
|
*/
|
|
|
|
FixDayDow(line);
|
2008-04-08 02:32:35 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
/* copy mailto (can be NULL) */
|
|
|
|
line->cl_MailTo = xstrdup(mailTo);
|
|
|
|
#endif
|
2008-03-13 03:40:25 +05:30
|
|
|
/* copy command */
|
2008-07-17 03:42:18 +05:30
|
|
|
line->cl_Shell = xstrdup(tokens[5]);
|
2008-03-13 03:40:25 +05:30
|
|
|
if (DebugOpt) {
|
2008-07-17 03:42:18 +05:30
|
|
|
crondlog(LVL5 " command:%s", tokens[5]);
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
pline = &line->cl_Next;
|
2008-07-17 03:42:18 +05:30
|
|
|
//bb_error_msg("M[%s]F[%s][%s][%s][%s][%s][%s]", mailTo, tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5]);
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
*pline = NULL;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
file->cf_Next = FileBase;
|
|
|
|
FileBase = file;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
if (maxLines == 0) {
|
2008-04-08 02:32:35 +05:30
|
|
|
crondlog(WARN9 "user %s: too many lines", fileName);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-07-17 17:29:13 +05:30
|
|
|
config_close(parser);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
static void CheckUpdates(void)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
FILE *fi;
|
|
|
|
char buf[256];
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-22 04:35:26 +05:30
|
|
|
fi = fopen_for_read(CRONUPDATE);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (fi != NULL) {
|
2008-02-17 19:58:53 +05:30
|
|
|
unlink(CRONUPDATE);
|
2004-02-22 10:14:21 +05:30
|
|
|
while (fgets(buf, sizeof(buf), fi) != NULL) {
|
2008-03-13 03:40:25 +05:30
|
|
|
/* use first word only */
|
2004-02-22 10:14:21 +05:30
|
|
|
SynchronizeFile(strtok(buf, " \t\r\n"));
|
|
|
|
}
|
|
|
|
fclose(fi);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
static void SynchronizeDir(void)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
CronFile *file;
|
2004-02-22 10:14:21 +05:30
|
|
|
/* Attempt to delete the database. */
|
2008-03-13 03:40:25 +05:30
|
|
|
again:
|
|
|
|
for (file = FileBase; file; file = file->cf_Next) {
|
|
|
|
if (!file->cf_Deleted) {
|
|
|
|
DeleteFile(file->cf_User);
|
|
|
|
goto again;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove cron update file
|
|
|
|
*
|
|
|
|
* Re-chdir, in case directory was renamed & deleted, or otherwise
|
|
|
|
* screwed up.
|
|
|
|
*
|
|
|
|
* scan directory and add associated users
|
|
|
|
*/
|
2008-02-17 19:58:53 +05:30
|
|
|
unlink(CRONUPDATE);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (chdir(CDir) < 0) {
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(DIE9 "can't chdir(%s)", CDir);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
{
|
|
|
|
DIR *dir = opendir(".");
|
|
|
|
struct dirent *den;
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (!dir)
|
|
|
|
crondlog(DIE9 "can't chdir(%s)", "."); /* exits */
|
2008-04-08 02:32:35 +05:30
|
|
|
while ((den = readdir(dir)) != NULL) {
|
2008-03-13 03:40:25 +05:30
|
|
|
if (strchr(den->d_name, '.') != NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (getpwnam(den->d_name)) {
|
|
|
|
SynchronizeFile(den->d_name);
|
|
|
|
} else {
|
|
|
|
crondlog(LVL7 "ignoring %s", den->d_name);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
closedir(dir);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 10:14:21 +05:30
|
|
|
static void DeleteFile(const char *userName)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
CronFile **pfile = &FileBase;
|
|
|
|
CronFile *file;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
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 17:54:59 +05:30
|
|
|
} else {
|
2004-02-22 10:14:21 +05:30
|
|
|
pfile = &file->cf_Next;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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 10:14:21 +05:30
|
|
|
static int TestJobs(time_t t1, time_t t2)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2006-01-30 19:06:03 +05:30
|
|
|
int nJobs = 0;
|
2004-02-22 10:14:21 +05:30
|
|
|
time_t t;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/* Find jobs > t1 and <= t2 */
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
for (t = t1 - t1 % 60; t <= t2; t += 60) {
|
2008-03-13 03:40:25 +05:30
|
|
|
struct tm *tp;
|
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (t <= t1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
tp = localtime(&t);
|
|
|
|
for (file = FileBase; file; file = file->cf_Next) {
|
|
|
|
if (DebugOpt)
|
|
|
|
crondlog(LVL5 "file %s:", file->cf_User);
|
|
|
|
if (file->cf_Deleted)
|
|
|
|
continue;
|
|
|
|
for (line = file->cf_LineBase; line; line = line->cl_Next) {
|
2004-02-22 10:14:21 +05:30
|
|
|
if (DebugOpt)
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(LVL5 " line %s", line->cl_Shell);
|
|
|
|
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]
|
|
|
|
) {
|
|
|
|
if (DebugOpt) {
|
|
|
|
crondlog(LVL5 " job: %d %s",
|
|
|
|
(int)line->cl_Pid, line->cl_Shell);
|
|
|
|
}
|
|
|
|
if (line->cl_Pid > 0) {
|
|
|
|
crondlog(LVL8 "user %s: process already running: %s",
|
|
|
|
file->cf_User, line->cl_Shell);
|
|
|
|
} else if (line->cl_Pid == 0) {
|
|
|
|
line->cl_Pid = -1;
|
|
|
|
file->cf_Ready = 1;
|
|
|
|
++nJobs;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-10-27 04:51:47 +05:30
|
|
|
return nJobs;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
static void RunJobs(void)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
|
|
|
|
|
|
|
for (file = FileBase; file; file = file->cf_Next) {
|
2008-03-13 03:40:25 +05:30
|
|
|
if (!file->cf_Ready)
|
|
|
|
continue;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
file->cf_Ready = 0;
|
|
|
|
for (line = file->cf_LineBase; line; line = line->cl_Next) {
|
|
|
|
if (line->cl_Pid >= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
RunJob(file->cf_User, line);
|
|
|
|
crondlog(LVL8 "USER %s pid %3d cmd %s",
|
|
|
|
file->cf_User, (int)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;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CheckJobs() - check for job completion
|
|
|
|
*
|
|
|
|
* Check for job completion, return number of jobs still running after
|
|
|
|
* all done.
|
|
|
|
*/
|
2004-02-22 10:14:21 +05:30
|
|
|
static int CheckJobs(void)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
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) {
|
2008-03-13 03:40:25 +05:30
|
|
|
int status, r;
|
|
|
|
if (line->cl_Pid <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = waitpid(line->cl_Pid, &status, WNOHANG);
|
|
|
|
if (r < 0 || r == line->cl_Pid) {
|
|
|
|
EndJob(file->cf_User, line);
|
|
|
|
if (line->cl_Pid) {
|
2004-02-22 10:14:21 +05:30
|
|
|
file->cf_Running = 1;
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
} else if (r == 0) {
|
|
|
|
file->cf_Running = 1;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2004-02-22 10:14:21 +05:30
|
|
|
nStillRunning += file->cf_Running;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2006-10-27 04:51:47 +05:30
|
|
|
return nStillRunning;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2006-05-20 19:44:05 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
2008-03-13 03:40:25 +05:30
|
|
|
|
|
|
|
// TODO: sendmail should be _run-time_ option, not compile-time!
|
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
static void
|
2008-03-13 03:40:25 +05:30
|
|
|
ForkJob(const char *user, CronLine *line, int mailFd,
|
2008-01-24 07:03:12 +05:30
|
|
|
const char *prog, const char *cmd, const char *arg,
|
|
|
|
const char *mail_filename)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
struct passwd *pas;
|
|
|
|
pid_t pid;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
/* prepare things before vfork */
|
|
|
|
pas = getpwnam(user);
|
|
|
|
if (!pas) {
|
|
|
|
crondlog(LVL9 "can't get uid for %s", user);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
SetEnv(pas);
|
|
|
|
|
|
|
|
pid = vfork();
|
2004-02-22 10:14:21 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
/* CHILD */
|
2008-03-13 03:40:25 +05:30
|
|
|
/* change running state to the user in question */
|
|
|
|
ChangeUser(pas);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (DebugOpt) {
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(LVL5 "child running %s", prog);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
if (mailFd >= 0) {
|
2008-01-24 07:03:12 +05:30
|
|
|
xmove_fd(mailFd, mail_filename ? 1 : 0);
|
|
|
|
dup2(1, 2);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2008-06-30 21:18:34 +05:30
|
|
|
execlp(prog, prog, cmd, arg, NULL);
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg);
|
2008-01-24 07:03:12 +05:30
|
|
|
if (mail_filename) {
|
2004-02-22 10:14:21 +05:30
|
|
|
fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
|
|
|
|
}
|
2008-05-19 14:59:47 +05:30
|
|
|
_exit(EXIT_SUCCESS);
|
2008-01-24 07:03:12 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
line->cl_Pid = pid;
|
2008-01-24 07:03:12 +05:30
|
|
|
if (pid < 0) {
|
2004-02-22 10:14:21 +05:30
|
|
|
/* FORK FAILED */
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(ERR20 "can't vfork");
|
|
|
|
err:
|
2004-02-22 10:14:21 +05:30
|
|
|
line->cl_Pid = 0;
|
2008-01-24 07:03:12 +05:30
|
|
|
if (mail_filename) {
|
2008-02-17 19:58:53 +05:30
|
|
|
unlink(mail_filename);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2008-01-24 07:03:12 +05:30
|
|
|
} else if (mail_filename) {
|
2004-02-22 10:14:21 +05:30
|
|
|
/* PARENT, FORK SUCCESS
|
|
|
|
* rename mail-file based on pid of process
|
|
|
|
*/
|
|
|
|
char mailFile2[128];
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid);
|
2008-02-17 19:58:53 +05:30
|
|
|
rename(mail_filename, mailFile2); // TODO: xrename?
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
/*
|
2004-02-22 10:14:21 +05:30
|
|
|
* 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 17:54:59 +05:30
|
|
|
*/
|
2004-02-22 10:14:21 +05:30
|
|
|
if (mailFd >= 0) {
|
|
|
|
close(mailFd);
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
static void RunJob(const char *user, CronLine *line)
|
2003-07-28 13:10:39 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
char mailFile[128];
|
2008-04-08 02:32:35 +05:30
|
|
|
int mailFd = -1;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
|
|
|
line->cl_Pid = 0;
|
|
|
|
line->cl_MailFlag = 0;
|
|
|
|
|
2008-04-08 02:32:35 +05:30
|
|
|
if (line->cl_MailTo) {
|
|
|
|
/* open mail file - owner root so nobody can screw with it. */
|
|
|
|
snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid());
|
|
|
|
mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-04-08 02:32:35 +05:30
|
|
|
if (mailFd >= 0) {
|
|
|
|
line->cl_MailFlag = 1;
|
2008-06-30 19:00:21 +05:30
|
|
|
fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_MailTo,
|
2008-04-08 02:32:35 +05:30
|
|
|
line->cl_Shell);
|
|
|
|
line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
|
|
|
|
} else {
|
|
|
|
crondlog(ERR20 "cannot create mail file %s for user %s, "
|
|
|
|
"discarding output", mailFile, user);
|
|
|
|
}
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
|
2003-07-28 13:10:39 +05:30
|
|
|
}
|
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
/*
|
|
|
|
* EndJob - called when job terminates and when mail terminates
|
|
|
|
*/
|
2008-03-13 03:40:25 +05:30
|
|
|
static void EndJob(const char *user, CronLine *line)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
int mailFd;
|
|
|
|
char mailFile[128];
|
|
|
|
struct stat sbuf;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/* No job */
|
|
|
|
if (line->cl_Pid <= 0) {
|
|
|
|
line->cl_Pid = 0;
|
|
|
|
return;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
|
|
|
/*
|
2004-02-22 10:14:21 +05:30
|
|
|
* End of job and no mail file
|
|
|
|
* End of sendmail job
|
2002-10-22 17:54:59 +05:30
|
|
|
*/
|
2008-03-13 03:40:25 +05:30
|
|
|
snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, line->cl_Pid);
|
2004-02-22 10:14:21 +05:30
|
|
|
line->cl_Pid = 0;
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
if (line->cl_MailFlag == 0) {
|
2004-02-22 10:14:21 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
line->cl_MailFlag = 0;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
|
|
|
/*
|
2004-02-22 10:14:21 +05:30
|
|
|
* End of primary job - check for mail file. If size has increased and
|
|
|
|
* the file is still valid, we sendmail it.
|
2002-10-22 17:54:59 +05:30
|
|
|
*/
|
2004-02-22 10:14:21 +05:30
|
|
|
mailFd = open(mailFile, O_RDONLY);
|
2008-02-17 19:58:53 +05:30
|
|
|
unlink(mailFile);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (mailFd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2003-07-28 13:10:39 +05:30
|
|
|
|
2007-08-18 21:18:00 +05:30
|
|
|
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 10:14:21 +05:30
|
|
|
close(mailFd);
|
|
|
|
return;
|
|
|
|
}
|
2008-04-08 02:32:35 +05:30
|
|
|
if (line->cl_MailTo)
|
|
|
|
ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
#else /* crond without sendmail */
|
|
|
|
|
|
|
|
static void RunJob(const char *user, CronLine *line)
|
2004-02-22 10:14:21 +05:30
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
struct passwd *pas;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
/* prepare things before vfork */
|
|
|
|
pas = getpwnam(user);
|
|
|
|
if (!pas) {
|
|
|
|
crondlog(LVL9 "can't get uid for %s", user);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
SetEnv(pas);
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
/* fork as the user in question and run program */
|
|
|
|
pid = vfork();
|
2004-02-22 10:14:21 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
/* CHILD */
|
2008-03-13 03:40:25 +05:30
|
|
|
/* change running state to the user in question */
|
|
|
|
ChangeUser(pas);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (DebugOpt) {
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(LVL5 "child running %s", DEFAULT_SHELL);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user,
|
|
|
|
DEFAULT_SHELL, "-c", line->cl_Shell);
|
2008-05-19 14:59:47 +05:30
|
|
|
_exit(EXIT_SUCCESS);
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
if (pid < 0) {
|
2004-02-22 10:14:21 +05:30
|
|
|
/* FORK FAILED */
|
2008-03-13 03:40:25 +05:30
|
|
|
crondlog(ERR20 "can't vfork");
|
|
|
|
err:
|
2004-02-22 10:14:21 +05:30
|
|
|
pid = 0;
|
|
|
|
}
|
|
|
|
line->cl_Pid = pid;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2006-05-20 19:44:05 +05:30
|
|
|
#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */
|