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