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