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