Bernhard Reutner-Fischer | 2c99851 | 2006-04-12 18:09:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 2 | /* |
| 3 | * crond -d[#] -c <crondir> -f -b |
| 4 | * |
| 5 | * run as root, but NOT setuid root |
| 6 | * |
| 7 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) |
Denis Vlasenko | b9c02dd | 2007-08-18 15:48:00 +0000 | [diff] [blame] | 8 | * (version 2.3.2) |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 9 | * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 10 | * |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 11 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Denis Vlasenko | b9c02dd | 2007-08-18 15:48:00 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 15 | #include <syslog.h> |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 17 | /* glibc frees previous setenv'ed value when we do next setenv() |
| 18 | * of the same variable. uclibc does not do this! */ |
| 19 | #if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */ |
| 20 | #define SETENV_LEAKS 0 |
| 21 | #else |
| 22 | #define SETENV_LEAKS 1 |
| 23 | #endif |
| 24 | |
| 25 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 26 | #ifndef CRONTABS |
| 27 | #define CRONTABS "/var/spool/cron/crontabs" |
| 28 | #endif |
| 29 | #ifndef TMPDIR |
| 30 | #define TMPDIR "/var/spool/cron" |
| 31 | #endif |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 32 | #ifndef SENDMAIL |
Denis Vlasenko | b9c02dd | 2007-08-18 15:48:00 +0000 | [diff] [blame] | 33 | #define SENDMAIL "sendmail" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 34 | #endif |
| 35 | #ifndef SENDMAIL_ARGS |
Denis Vlasenko | 6ea75e2 | 2008-06-28 21:46:41 +0000 | [diff] [blame] | 36 | #define SENDMAIL_ARGS "-ti", "oem" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 37 | #endif |
| 38 | #ifndef CRONUPDATE |
| 39 | #define CRONUPDATE "cron.update" |
| 40 | #endif |
| 41 | #ifndef MAXLINES |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 42 | #define MAXLINES 256 /* max lines in non-root crontabs */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 45 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 46 | typedef struct CronFile { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 47 | struct CronFile *cf_Next; |
| 48 | struct CronLine *cf_LineBase; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 49 | char *cf_User; /* username */ |
| 50 | smallint cf_Ready; /* bool: one or more jobs ready */ |
| 51 | smallint cf_Running; /* bool: one or more jobs running */ |
| 52 | smallint cf_Deleted; /* marked for deletion, ignore */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 53 | } CronFile; |
| 54 | |
| 55 | typedef struct CronLine { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 56 | struct CronLine *cl_Next; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 57 | char *cl_Shell; /* shell command */ |
| 58 | pid_t cl_Pid; /* running pid, 0, or armed (-1) */ |
| 59 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 60 | int cl_MailPos; /* 'empty file' size */ |
| 61 | smallint cl_MailFlag; /* running pid is for mail */ |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 62 | char *cl_MailTo; /* whom to mail results */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 63 | #endif |
| 64 | /* ordered by size, not in natural order. makes code smaller: */ |
| 65 | char cl_Dow[7]; /* 0-6, beginning sunday */ |
| 66 | char cl_Mons[12]; /* 0-11 */ |
| 67 | char cl_Hrs[24]; /* 0-23 */ |
| 68 | char cl_Days[32]; /* 1-31 */ |
| 69 | char cl_Mins[60]; /* 0-59 */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 70 | } CronLine; |
| 71 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 72 | |
| 73 | #define DaemonUid 0 |
| 74 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 75 | |
| 76 | enum { |
| 77 | OPT_l = (1 << 0), |
| 78 | OPT_L = (1 << 1), |
| 79 | OPT_f = (1 << 2), |
| 80 | OPT_b = (1 << 3), |
| 81 | OPT_S = (1 << 4), |
| 82 | OPT_c = (1 << 5), |
| 83 | OPT_d = (1 << 6) * ENABLE_DEBUG_CROND_OPTION, |
| 84 | }; |
Bernhard Reutner-Fischer | ef21629 | 2006-05-20 14:14:05 +0000 | [diff] [blame] | 85 | #if ENABLE_DEBUG_CROND_OPTION |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 86 | #define DebugOpt (option_mask32 & OPT_d) |
| 87 | #else |
| 88 | #define DebugOpt 0 |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 89 | #endif |
| 90 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 91 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 92 | struct globals { |
| 93 | unsigned LogLevel; /* = 8; */ |
| 94 | const char *LogFile; |
| 95 | const char *CDir; /* = CRONTABS; */ |
| 96 | CronFile *FileBase; |
| 97 | #if SETENV_LEAKS |
| 98 | char *env_var_user; |
| 99 | char *env_var_home; |
| 100 | #endif |
| 101 | }; |
| 102 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 103 | #define LogLevel (G.LogLevel ) |
| 104 | #define LogFile (G.LogFile ) |
| 105 | #define CDir (G.CDir ) |
| 106 | #define FileBase (G.FileBase ) |
| 107 | #define env_var_user (G.env_var_user ) |
| 108 | #define env_var_home (G.env_var_home ) |
| 109 | #define INIT_G() do { \ |
| 110 | LogLevel = 8; \ |
| 111 | CDir = CRONTABS; \ |
| 112 | } while (0) |
| 113 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 114 | |
| 115 | static void CheckUpdates(void); |
| 116 | static void SynchronizeDir(void); |
| 117 | static int TestJobs(time_t t1, time_t t2); |
| 118 | static void RunJobs(void); |
| 119 | static int CheckJobs(void); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 120 | static void RunJob(const char *user, CronLine *line); |
Bernhard Reutner-Fischer | ef21629 | 2006-05-20 14:14:05 +0000 | [diff] [blame] | 121 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 122 | static void EndJob(const char *user, CronLine *line); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 123 | #else |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 124 | #define EndJob(user, line) ((line)->cl_Pid = 0) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 125 | #endif |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 126 | static void DeleteFile(const char *userName); |
| 127 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 128 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 129 | #define LVL5 "\x05" |
| 130 | #define LVL7 "\x07" |
| 131 | #define LVL8 "\x08" |
| 132 | #define LVL9 "\x09" |
| 133 | #define WARN9 "\x49" |
| 134 | #define DIE9 "\xc9" |
| 135 | /* level >= 20 is "error" */ |
| 136 | #define ERR20 "\x14" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 137 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 138 | static void crondlog(const char *ctl, ...) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 139 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 140 | va_list va; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 141 | int level = (ctl[0] & 0x1f); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 142 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 143 | va_start(va, ctl); |
Denis Vlasenko | 77ad97f | 2008-05-13 02:27:31 +0000 | [diff] [blame] | 144 | if (level >= (int)LogLevel) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 145 | /* Debug mode: all to (non-redirected) stderr, */ |
| 146 | /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */ |
| 147 | if (!DebugOpt && LogFile) { |
| 148 | /* Otherwise (log to file): we reopen log file at every write: */ |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 149 | int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 150 | if (logfd >= 0) |
| 151 | xmove_fd(logfd, STDERR_FILENO); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 152 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 153 | // TODO: ERR -> error, WARN -> warning, LVL -> info |
| 154 | bb_verror_msg(ctl + 1, va, /* strerr: */ NULL); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 155 | } |
| 156 | va_end(va); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 157 | if (ctl[0] & 0x80) |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 158 | exit(20); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 161 | int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 162 | int crond_main(int argc ATTRIBUTE_UNUSED, char **argv) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 163 | { |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 164 | unsigned opt; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 165 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 166 | INIT_G(); |
| 167 | |
| 168 | /* "-b after -f is ignored", and so on for every pair a-b */ |
| 169 | opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l") |
| 170 | ":l+:d+"; /* -l and -d have numeric param */ |
Denis Vlasenko | b44c790 | 2008-03-17 09:29:43 +0000 | [diff] [blame] | 171 | opt = getopt32(argv, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"), |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 172 | &LogLevel, &LogFile, &CDir |
| 173 | USE_DEBUG_CROND_OPTION(,&LogLevel)); |
| 174 | /* both -d N and -l N set the same variable: LogLevel */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 176 | if (!(opt & OPT_f)) { |
| 177 | /* close stdin, stdout, stderr. |
| 178 | * close unused descriptors - don't need them. */ |
Denis Vlasenko | c52248e | 2008-03-20 14:04:30 +0000 | [diff] [blame] | 179 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | if (!DebugOpt && LogFile == NULL) { |
| 183 | /* logging to syslog */ |
| 184 | openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON); |
| 185 | logmode = LOGMODE_SYSLOG; |
| 186 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 187 | |
Denis Vlasenko | 5a14202 | 2007-03-26 13:20:54 +0000 | [diff] [blame] | 188 | xchdir(CDir); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 189 | //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */ |
| 190 | setenv("SHELL", DEFAULT_SHELL, 1); /* once, for all future children */ |
| 191 | crondlog(LVL9 "crond (busybox "BB_VER") started, log level %d", LogLevel); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 192 | SynchronizeDir(); |
| 193 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 194 | /* main loop - synchronize to 1 second after the minute, minimum sleep |
| 195 | * of 1 second. */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 196 | { |
| 197 | time_t t1 = time(NULL); |
| 198 | time_t t2; |
| 199 | long dt; |
"Vladimir N. Oleynik" | cd5c15d | 2006-01-30 13:36:03 +0000 | [diff] [blame] | 200 | int rescan = 60; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 201 | int sleep_time = 60; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 202 | |
Denis Vlasenko | 10457b9 | 2007-03-27 22:01:31 +0000 | [diff] [blame] | 203 | write_pidfile("/var/run/crond.pid"); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 204 | for (;;) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 205 | sleep((sleep_time + 1) - (time(NULL) % sleep_time)); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 206 | |
| 207 | t2 = time(NULL); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 208 | dt = (long)t2 - (long)t1; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | * The file 'cron.update' is checked to determine new cron |
| 212 | * jobs. The directory is rescanned once an hour to deal |
| 213 | * with any screwups. |
| 214 | * |
| 215 | * check for disparity. Disparities over an hour either way |
| 216 | * result in resynchronization. A reverse-indexed disparity |
| 217 | * less then an hour causes us to effectively sleep until we |
| 218 | * match the original time (i.e. no re-execution of jobs that |
| 219 | * have just been run). A forward-indexed disparity less then |
| 220 | * an hour causes intermediate jobs to be run, but only once |
| 221 | * in the worst case. |
| 222 | * |
| 223 | * when running jobs, the inequality used is greater but not |
| 224 | * equal to t1, and less then or equal to t2. |
| 225 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 226 | if (--rescan == 0) { |
| 227 | rescan = 60; |
| 228 | SynchronizeDir(); |
| 229 | } |
| 230 | CheckUpdates(); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 231 | if (DebugOpt) |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 232 | crondlog(LVL5 "wakeup dt=%ld", dt); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 233 | if (dt < -60 * 60 || dt > 60 * 60) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 234 | crondlog(WARN9 "time disparity of %d minutes detected", dt / 60); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 235 | } else if (dt > 0) { |
| 236 | TestJobs(t1, t2); |
| 237 | RunJobs(); |
| 238 | sleep(5); |
| 239 | if (CheckJobs() > 0) { |
| 240 | sleep_time = 10; |
| 241 | } else { |
| 242 | sleep_time = 60; |
| 243 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 244 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 245 | t1 = t2; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 248 | return 0; /* not reached */ |
Glenn L McGrath | b89fcd4 | 2003-12-23 07:21:33 +0000 | [diff] [blame] | 249 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 250 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 251 | #if SETENV_LEAKS |
| 252 | /* We set environment *before* vfork (because we want to use vfork), |
| 253 | * so we cannot use setenv() - repeated calls to setenv() may leak memory! |
| 254 | * Using putenv(), and freeing memory after unsetenv() won't leak */ |
| 255 | static void safe_setenv4(char **pvar_val, const char *var, const char *val /*, int len*/) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 256 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 257 | const int len = 4; /* both var names are 4 char long */ |
| 258 | char *var_val = *pvar_val; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 259 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 260 | if (var_val) { |
| 261 | var_val[len] = '\0'; /* nuke '=' */ |
| 262 | unsetenv(var_val); |
| 263 | free(var_val); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 264 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 265 | *pvar_val = xasprintf("%s=%s", var, val); |
| 266 | putenv(*pvar_val); |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | static void SetEnv(struct passwd *pas) |
| 271 | { |
| 272 | #if SETENV_LEAKS |
| 273 | safe_setenv4(&env_var_user, "USER", pas->pw_name); |
| 274 | safe_setenv4(&env_var_home, "HOME", pas->pw_dir); |
| 275 | /* if we want to set user's shell instead: */ |
| 276 | /*safe_setenv(env_var_user, "SHELL", pas->pw_shell, 5);*/ |
| 277 | #else |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 278 | setenv("USER", pas->pw_name, 1); |
| 279 | setenv("HOME", pas->pw_dir, 1); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 280 | #endif |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 281 | /* currently, we use constant one: */ |
| 282 | /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 285 | static void ChangeUser(struct passwd *pas) |
| 286 | { |
| 287 | /* careful: we're after vfork! */ |
| 288 | change_identity(pas); /* - initgroups, setgid, setuid */ |
| 289 | if (chdir(pas->pw_dir) < 0) { |
| 290 | crondlog(LVL9 "can't chdir(%s)", pas->pw_dir); |
| 291 | if (chdir(TMPDIR) < 0) { |
| 292 | crondlog(DIE9 "can't chdir(%s)", TMPDIR); /* exits */ |
| 293 | } |
| 294 | } |
| 295 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 296 | |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 297 | static const char DowAry[] ALIGN1 = |
| 298 | "sun""mon""tue""wed""thu""fri""sat" |
| 299 | /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */ |
| 300 | ; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 301 | |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 302 | static const char MonAry[] ALIGN1 = |
| 303 | "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" |
| 304 | /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */ |
| 305 | ; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 306 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 307 | static char *ParseField(char *user, char *ary, int modvalue, int off, |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 308 | const char *names, char *ptr) |
| 309 | /* 'names' is a pointer to a set of 3-char abbreviations */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 310 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 311 | char *base = ptr; |
| 312 | int n1 = -1; |
| 313 | int n2 = -1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 314 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 315 | if (base == NULL) { |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 316 | return NULL; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 317 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 318 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 319 | while (!isspace(*ptr)) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 320 | int skip = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 321 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 322 | /* Handle numeric digit or symbol or '*' */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 323 | if (*ptr == '*') { |
| 324 | n1 = 0; /* everything will be filled */ |
| 325 | n2 = modvalue - 1; |
| 326 | skip = 1; |
| 327 | ++ptr; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 328 | } else if (isdigit(*ptr)) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 329 | if (n1 < 0) { |
| 330 | n1 = strtol(ptr, &ptr, 10) + off; |
| 331 | } else { |
| 332 | n2 = strtol(ptr, &ptr, 10) + off; |
| 333 | } |
| 334 | skip = 1; |
| 335 | } else if (names) { |
| 336 | int i; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 337 | |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 338 | for (i = 0; names[i]; i += 3) { |
| 339 | /* was using strncmp before... */ |
| 340 | if (strncasecmp(ptr, &names[i], 3) == 0) { |
| 341 | ptr += 3; |
| 342 | if (n1 < 0) { |
| 343 | n1 = i / 3; |
| 344 | } else { |
| 345 | n2 = i / 3; |
| 346 | } |
| 347 | skip = 1; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 348 | break; |
| 349 | } |
| 350 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 351 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 352 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 353 | /* handle optional range '-' */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 354 | if (skip == 0) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 355 | crondlog(WARN9 "user %s: parse error at %s", user, base); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 356 | return NULL; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 357 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 358 | if (*ptr == '-' && n2 < 0) { |
| 359 | ++ptr; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 360 | continue; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 361 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 362 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 363 | /* |
| 364 | * collapse single-value ranges, handle skipmark, and fill |
| 365 | * in the character array appropriately. |
| 366 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 367 | if (n2 < 0) { |
| 368 | n2 = n1; |
| 369 | } |
| 370 | if (*ptr == '/') { |
| 371 | skip = strtol(ptr + 1, &ptr, 10); |
| 372 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 373 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 374 | /* |
| 375 | * fill array, using a failsafe is the easiest way to prevent |
| 376 | * an endless loop |
| 377 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 378 | { |
| 379 | int s0 = 1; |
| 380 | int failsafe = 1024; |
| 381 | |
| 382 | --n1; |
| 383 | do { |
| 384 | n1 = (n1 + 1) % modvalue; |
| 385 | |
| 386 | if (--s0 == 0) { |
| 387 | ary[n1 % modvalue] = 1; |
| 388 | s0 = skip; |
| 389 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 390 | if (--failsafe == 0) { |
| 391 | crondlog(WARN9 "user %s: parse error at %s", user, base); |
| 392 | return NULL; |
| 393 | } |
| 394 | } while (n1 != n2); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 395 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 396 | } |
| 397 | if (*ptr != ',') { |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 398 | break; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 399 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 400 | ++ptr; |
| 401 | n1 = -1; |
| 402 | n2 = -1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 403 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 404 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 405 | if (!isspace(*ptr)) { |
| 406 | crondlog(WARN9 "user %s: parse error at %s", user, base); |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 407 | return NULL; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 410 | if (DebugOpt && (LogLevel <= 5)) { /* like LVL5 */ |
| 411 | /* can't use crondlog, it inserts '\n' */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 412 | int i; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 413 | for (i = 0; i < modvalue; ++i) |
| 414 | fprintf(stderr, "%d", (unsigned char)ary[i]); |
| 415 | fputc('\n', stderr); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 416 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 417 | return skip_whitespace(ptr); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 420 | static void FixDayDow(CronLine *line) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 421 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 422 | unsigned i; |
"Vladimir N. Oleynik" | cd5c15d | 2006-01-30 13:36:03 +0000 | [diff] [blame] | 423 | int weekUsed = 0; |
| 424 | int daysUsed = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 425 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 426 | for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 427 | if (line->cl_Dow[i] == 0) { |
| 428 | weekUsed = 1; |
| 429 | break; |
| 430 | } |
| 431 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 432 | for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 433 | if (line->cl_Days[i] == 0) { |
| 434 | daysUsed = 1; |
| 435 | break; |
| 436 | } |
| 437 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 438 | if (weekUsed != daysUsed) { |
| 439 | if (weekUsed) |
| 440 | memset(line->cl_Days, 0, sizeof(line->cl_Days)); |
| 441 | else /* daysUsed */ |
| 442 | memset(line->cl_Dow, 0, sizeof(line->cl_Dow)); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 446 | static void SynchronizeFile(const char *fileName) |
| 447 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 448 | FILE *fi; |
| 449 | struct stat sbuf; |
| 450 | int maxEntries; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 451 | int maxLines; |
| 452 | char buf[1024]; |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 453 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 454 | char *mailTo = NULL; |
| 455 | #endif |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 456 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 457 | if (!fileName) |
| 458 | return; |
| 459 | |
| 460 | DeleteFile(fileName); |
| 461 | fi = fopen(fileName, "r"); |
| 462 | if (!fi) |
| 463 | return; |
| 464 | |
| 465 | maxEntries = MAXLINES; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 466 | if (strcmp(fileName, "root") == 0) { |
| 467 | maxEntries = 65535; |
| 468 | } |
| 469 | maxLines = maxEntries * 10; |
| 470 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 471 | if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) { |
| 472 | CronFile *file = xzalloc(sizeof(CronFile)); |
| 473 | CronLine **pline; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 474 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 475 | file->cf_User = xstrdup(fileName); |
| 476 | pline = &file->cf_LineBase; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 477 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 478 | while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) { |
| 479 | CronLine *line; |
| 480 | char *ptr; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 481 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 482 | trim(buf); |
| 483 | if (buf[0] == '\0' || buf[0] == '#') { |
| 484 | continue; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 485 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 486 | if (--maxEntries == 0) { |
| 487 | break; |
| 488 | } |
| 489 | if (DebugOpt) { |
| 490 | crondlog(LVL5 "user:%s entry:%s", fileName, buf); |
| 491 | } |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 492 | /* check if line is setting MAILTO= */ |
| 493 | if (0 == strncmp("MAILTO=", buf, 7)) { |
| 494 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 495 | free(mailTo); |
| 496 | mailTo = (buf[7]) ? xstrdup(buf+7) : NULL; |
| 497 | #endif /* otherwise just ignore such lines */ |
| 498 | continue; |
| 499 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 500 | *pline = line = xzalloc(sizeof(CronLine)); |
| 501 | /* parse date ranges */ |
| 502 | ptr = ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, buf); |
| 503 | ptr = ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, ptr); |
| 504 | ptr = ParseField(file->cf_User, line->cl_Days, 32, 0, NULL, ptr); |
| 505 | ptr = ParseField(file->cf_User, line->cl_Mons, 12, -1, MonAry, ptr); |
| 506 | ptr = ParseField(file->cf_User, line->cl_Dow, 7, 0, DowAry, ptr); |
| 507 | /* check failure */ |
| 508 | if (ptr == NULL) { |
| 509 | free(line); |
| 510 | continue; |
| 511 | } |
| 512 | /* |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 513 | * fix days and dow - if one is not "*" and the other |
| 514 | * is "*", the other is set to 0, and vise-versa |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 515 | */ |
| 516 | FixDayDow(line); |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 517 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 518 | /* copy mailto (can be NULL) */ |
| 519 | line->cl_MailTo = xstrdup(mailTo); |
| 520 | #endif |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 521 | /* copy command */ |
| 522 | line->cl_Shell = xstrdup(ptr); |
| 523 | if (DebugOpt) { |
| 524 | crondlog(LVL5 " command:%s", ptr); |
| 525 | } |
| 526 | pline = &line->cl_Next; |
| 527 | } |
| 528 | *pline = NULL; |
| 529 | |
| 530 | file->cf_Next = FileBase; |
| 531 | FileBase = file; |
| 532 | |
| 533 | if (maxLines == 0 || maxEntries == 0) { |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 534 | crondlog(WARN9 "user %s: too many lines", fileName); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 535 | } |
| 536 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 537 | fclose(fi); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | static void CheckUpdates(void) |
| 541 | { |
| 542 | FILE *fi; |
| 543 | char buf[256]; |
| 544 | |
| 545 | fi = fopen(CRONUPDATE, "r"); |
| 546 | if (fi != NULL) { |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 547 | unlink(CRONUPDATE); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 548 | while (fgets(buf, sizeof(buf), fi) != NULL) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 549 | /* use first word only */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 550 | SynchronizeFile(strtok(buf, " \t\r\n")); |
| 551 | } |
| 552 | fclose(fi); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | static void SynchronizeDir(void) |
| 557 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 558 | CronFile *file; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 559 | /* Attempt to delete the database. */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 560 | again: |
| 561 | for (file = FileBase; file; file = file->cf_Next) { |
| 562 | if (!file->cf_Deleted) { |
| 563 | DeleteFile(file->cf_User); |
| 564 | goto again; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 565 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | * Remove cron update file |
| 570 | * |
| 571 | * Re-chdir, in case directory was renamed & deleted, or otherwise |
| 572 | * screwed up. |
| 573 | * |
| 574 | * scan directory and add associated users |
| 575 | */ |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 576 | unlink(CRONUPDATE); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 577 | if (chdir(CDir) < 0) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 578 | crondlog(DIE9 "can't chdir(%s)", CDir); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 579 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 580 | { |
| 581 | DIR *dir = opendir("."); |
| 582 | struct dirent *den; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 583 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 584 | if (!dir) |
| 585 | crondlog(DIE9 "can't chdir(%s)", "."); /* exits */ |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 586 | while ((den = readdir(dir)) != NULL) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 587 | if (strchr(den->d_name, '.') != NULL) { |
| 588 | continue; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 589 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 590 | if (getpwnam(den->d_name)) { |
| 591 | SynchronizeFile(den->d_name); |
| 592 | } else { |
| 593 | crondlog(LVL7 "ignoring %s", den->d_name); |
| 594 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 595 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 596 | closedir(dir); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 597 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 600 | /* |
| 601 | * DeleteFile() - delete user database |
| 602 | * |
| 603 | * Note: multiple entries for same user may exist if we were unable to |
| 604 | * completely delete a database due to running processes. |
| 605 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 606 | static void DeleteFile(const char *userName) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 607 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 608 | CronFile **pfile = &FileBase; |
| 609 | CronFile *file; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 610 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 611 | while ((file = *pfile) != NULL) { |
| 612 | if (strcmp(userName, file->cf_User) == 0) { |
| 613 | CronLine **pline = &file->cf_LineBase; |
| 614 | CronLine *line; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 615 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 616 | file->cf_Running = 0; |
| 617 | file->cf_Deleted = 1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 618 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 619 | while ((line = *pline) != NULL) { |
| 620 | if (line->cl_Pid > 0) { |
| 621 | file->cf_Running = 1; |
| 622 | pline = &line->cl_Next; |
| 623 | } else { |
| 624 | *pline = line->cl_Next; |
| 625 | free(line->cl_Shell); |
| 626 | free(line); |
| 627 | } |
| 628 | } |
| 629 | if (file->cf_Running == 0) { |
| 630 | *pfile = file->cf_Next; |
| 631 | free(file->cf_User); |
| 632 | free(file); |
| 633 | } else { |
| 634 | pfile = &file->cf_Next; |
| 635 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 636 | } else { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 637 | pfile = &file->cf_Next; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 638 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 639 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 640 | } |
| 641 | |
| 642 | /* |
| 643 | * TestJobs() |
| 644 | * |
| 645 | * determine which jobs need to be run. Under normal conditions, the |
| 646 | * period is about a minute (one scan). Worst case it will be one |
| 647 | * hour (60 scans). |
| 648 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 649 | static int TestJobs(time_t t1, time_t t2) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 650 | { |
"Vladimir N. Oleynik" | cd5c15d | 2006-01-30 13:36:03 +0000 | [diff] [blame] | 651 | int nJobs = 0; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 652 | time_t t; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 653 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 654 | /* Find jobs > t1 and <= t2 */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 655 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 656 | for (t = t1 - t1 % 60; t <= t2; t += 60) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 657 | struct tm *tp; |
| 658 | CronFile *file; |
| 659 | CronLine *line; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 660 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 661 | if (t <= t1) |
| 662 | continue; |
| 663 | |
| 664 | tp = localtime(&t); |
| 665 | for (file = FileBase; file; file = file->cf_Next) { |
| 666 | if (DebugOpt) |
| 667 | crondlog(LVL5 "file %s:", file->cf_User); |
| 668 | if (file->cf_Deleted) |
| 669 | continue; |
| 670 | for (line = file->cf_LineBase; line; line = line->cl_Next) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 671 | if (DebugOpt) |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 672 | crondlog(LVL5 " line %s", line->cl_Shell); |
| 673 | if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] |
| 674 | && (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday]) |
| 675 | && line->cl_Mons[tp->tm_mon] |
| 676 | ) { |
| 677 | if (DebugOpt) { |
| 678 | crondlog(LVL5 " job: %d %s", |
| 679 | (int)line->cl_Pid, line->cl_Shell); |
| 680 | } |
| 681 | if (line->cl_Pid > 0) { |
| 682 | crondlog(LVL8 "user %s: process already running: %s", |
| 683 | file->cf_User, line->cl_Shell); |
| 684 | } else if (line->cl_Pid == 0) { |
| 685 | line->cl_Pid = -1; |
| 686 | file->cf_Ready = 1; |
| 687 | ++nJobs; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 688 | } |
| 689 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 690 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 691 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 692 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 693 | return nJobs; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 696 | static void RunJobs(void) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 697 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 698 | CronFile *file; |
| 699 | CronLine *line; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 700 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 701 | for (file = FileBase; file; file = file->cf_Next) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 702 | if (!file->cf_Ready) |
| 703 | continue; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 704 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 705 | file->cf_Ready = 0; |
| 706 | for (line = file->cf_LineBase; line; line = line->cl_Next) { |
| 707 | if (line->cl_Pid >= 0) |
| 708 | continue; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 709 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 710 | RunJob(file->cf_User, line); |
| 711 | crondlog(LVL8 "USER %s pid %3d cmd %s", |
| 712 | file->cf_User, (int)line->cl_Pid, line->cl_Shell); |
| 713 | if (line->cl_Pid < 0) { |
| 714 | file->cf_Ready = 1; |
| 715 | } else if (line->cl_Pid > 0) { |
| 716 | file->cf_Running = 1; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 717 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 718 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 719 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
| 723 | * CheckJobs() - check for job completion |
| 724 | * |
| 725 | * Check for job completion, return number of jobs still running after |
| 726 | * all done. |
| 727 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 728 | static int CheckJobs(void) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 729 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 730 | CronFile *file; |
| 731 | CronLine *line; |
| 732 | int nStillRunning = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 733 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 734 | for (file = FileBase; file; file = file->cf_Next) { |
| 735 | if (file->cf_Running) { |
| 736 | file->cf_Running = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 737 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 738 | for (line = file->cf_LineBase; line; line = line->cl_Next) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 739 | int status, r; |
| 740 | if (line->cl_Pid <= 0) |
| 741 | continue; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 742 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 743 | r = waitpid(line->cl_Pid, &status, WNOHANG); |
| 744 | if (r < 0 || r == line->cl_Pid) { |
| 745 | EndJob(file->cf_User, line); |
| 746 | if (line->cl_Pid) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 747 | file->cf_Running = 1; |
| 748 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 749 | } else if (r == 0) { |
| 750 | file->cf_Running = 1; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 751 | } |
| 752 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 753 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 754 | nStillRunning += file->cf_Running; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 755 | } |
Denis Vlasenko | f0ed376 | 2006-10-26 23:21:47 +0000 | [diff] [blame] | 756 | return nStillRunning; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Bernhard Reutner-Fischer | ef21629 | 2006-05-20 14:14:05 +0000 | [diff] [blame] | 759 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 760 | |
| 761 | // TODO: sendmail should be _run-time_ option, not compile-time! |
| 762 | |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 763 | static void |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 764 | ForkJob(const char *user, CronLine *line, int mailFd, |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 765 | const char *prog, const char *cmd, const char *arg, |
| 766 | const char *mail_filename) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 767 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 768 | struct passwd *pas; |
| 769 | pid_t pid; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 770 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 771 | /* prepare things before vfork */ |
| 772 | pas = getpwnam(user); |
| 773 | if (!pas) { |
| 774 | crondlog(LVL9 "can't get uid for %s", user); |
| 775 | goto err; |
| 776 | } |
| 777 | SetEnv(pas); |
| 778 | |
| 779 | pid = vfork(); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 780 | if (pid == 0) { |
| 781 | /* CHILD */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 782 | /* change running state to the user in question */ |
| 783 | ChangeUser(pas); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 784 | if (DebugOpt) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 785 | crondlog(LVL5 "child running %s", prog); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 786 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 787 | if (mailFd >= 0) { |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 788 | xmove_fd(mailFd, mail_filename ? 1 : 0); |
| 789 | dup2(1, 2); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 790 | } |
| 791 | execl(prog, prog, cmd, arg, NULL); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 792 | crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg); |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 793 | if (mail_filename) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 794 | fdprintf(1, "Exec failed: %s -c %s\n", prog, arg); |
| 795 | } |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 796 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 799 | line->cl_Pid = pid; |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 800 | if (pid < 0) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 801 | /* FORK FAILED */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 802 | crondlog(ERR20 "can't vfork"); |
| 803 | err: |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 804 | line->cl_Pid = 0; |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 805 | if (mail_filename) { |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 806 | unlink(mail_filename); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 807 | } |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 808 | } else if (mail_filename) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 809 | /* PARENT, FORK SUCCESS |
| 810 | * rename mail-file based on pid of process |
| 811 | */ |
| 812 | char mailFile2[128]; |
| 813 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 814 | snprintf(mailFile2, sizeof(mailFile2), "%s/cron.%s.%d", TMPDIR, user, pid); |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 815 | rename(mail_filename, mailFile2); // TODO: xrename? |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 816 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 817 | |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 818 | /* |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 819 | * Close the mail file descriptor.. we can't just leave it open in |
| 820 | * a structure, closing it later, because we might run out of descriptors |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 821 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 822 | if (mailFd >= 0) { |
| 823 | close(mailFd); |
| 824 | } |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 825 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 826 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 827 | static void RunJob(const char *user, CronLine *line) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 828 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 829 | char mailFile[128]; |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 830 | int mailFd = -1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 831 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 832 | line->cl_Pid = 0; |
| 833 | line->cl_MailFlag = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 834 | |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 835 | if (line->cl_MailTo) { |
| 836 | /* open mail file - owner root so nobody can screw with it. */ |
| 837 | snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, getpid()); |
| 838 | mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 839 | |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 840 | if (mailFd >= 0) { |
| 841 | line->cl_MailFlag = 1; |
| 842 | fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user, |
| 843 | line->cl_Shell); |
| 844 | line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR); |
| 845 | } else { |
| 846 | crondlog(ERR20 "cannot create mail file %s for user %s, " |
| 847 | "discarding output", mailFile, user); |
| 848 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 849 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 850 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 851 | ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* |
| 855 | * EndJob - called when job terminates and when mail terminates |
| 856 | */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 857 | static void EndJob(const char *user, CronLine *line) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 858 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 859 | int mailFd; |
| 860 | char mailFile[128]; |
| 861 | struct stat sbuf; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 862 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 863 | /* No job */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 864 | if (line->cl_Pid <= 0) { |
| 865 | line->cl_Pid = 0; |
| 866 | return; |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * End of job and no mail file |
| 871 | * End of sendmail job |
| 872 | */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 873 | snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", TMPDIR, user, line->cl_Pid); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 874 | line->cl_Pid = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 875 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 876 | if (line->cl_MailFlag == 0) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 877 | return; |
| 878 | } |
| 879 | line->cl_MailFlag = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 880 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 881 | /* |
| 882 | * End of primary job - check for mail file. If size has increased and |
| 883 | * the file is still valid, we sendmail it. |
| 884 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 885 | mailFd = open(mailFile, O_RDONLY); |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 886 | unlink(mailFile); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 887 | if (mailFd < 0) { |
| 888 | return; |
| 889 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 890 | |
Denis Vlasenko | b9c02dd | 2007-08-18 15:48:00 +0000 | [diff] [blame] | 891 | if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid |
| 892 | || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos |
| 893 | || !S_ISREG(sbuf.st_mode) |
| 894 | ) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 895 | close(mailFd); |
| 896 | return; |
| 897 | } |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 898 | if (line->cl_MailTo) |
| 899 | ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 900 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 901 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 902 | #else /* crond without sendmail */ |
| 903 | |
| 904 | static void RunJob(const char *user, CronLine *line) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 905 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 906 | struct passwd *pas; |
| 907 | pid_t pid; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 908 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 909 | /* prepare things before vfork */ |
| 910 | pas = getpwnam(user); |
| 911 | if (!pas) { |
| 912 | crondlog(LVL9 "can't get uid for %s", user); |
| 913 | goto err; |
| 914 | } |
| 915 | SetEnv(pas); |
| 916 | |
| 917 | /* fork as the user in question and run program */ |
| 918 | pid = vfork(); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 919 | if (pid == 0) { |
| 920 | /* CHILD */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 921 | /* change running state to the user in question */ |
| 922 | ChangeUser(pas); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 923 | if (DebugOpt) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 924 | crondlog(LVL5 "child running %s", DEFAULT_SHELL); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 925 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 926 | execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 927 | crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, |
| 928 | DEFAULT_SHELL, "-c", line->cl_Shell); |
Bernhard Reutner-Fischer | 636a1f8 | 2008-05-19 09:29:47 +0000 | [diff] [blame] | 929 | _exit(EXIT_SUCCESS); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 930 | } |
| 931 | if (pid < 0) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 932 | /* FORK FAILED */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 933 | crondlog(ERR20 "can't vfork"); |
| 934 | err: |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 935 | pid = 0; |
| 936 | } |
| 937 | line->cl_Pid = pid; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 938 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 939 | |
Bernhard Reutner-Fischer | ef21629 | 2006-05-20 14:14:05 +0000 | [diff] [blame] | 940 | #endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */ |