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