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