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