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 | /* |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 3 | * run as root, but NOT setuid root |
| 4 | * |
| 5 | * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) |
Denis Vlasenko | b9c02dd | 2007-08-18 15:48:00 +0000 | [diff] [blame] | 6 | * (version 2.3.2) |
Mike Frysinger | f284c76 | 2006-04-16 20:38:26 +0000 | [diff] [blame] | 7 | * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 8 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 9 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 10 | */ |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 11 | //config:config CROND |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame^] | 12 | //config: bool "crond (13 kb)" |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 13 | //config: default y |
| 14 | //config: select FEATURE_SYSLOG |
| 15 | //config: help |
| 16 | //config: Crond is a background daemon that parses individual crontab |
| 17 | //config: files and executes commands on behalf of the users in question. |
| 18 | //config: This is a port of dcron from slackware. It uses files of the |
| 19 | //config: format /var/spool/cron/crontabs/<username> files, for example: |
| 20 | //config: $ cat /var/spool/cron/crontabs/root |
| 21 | //config: # Run daily cron jobs at 4:40 every day: |
| 22 | //config: 40 4 * * * /etc/cron/daily > /dev/null 2>&1 |
| 23 | //config: |
| 24 | //config:config FEATURE_CROND_D |
| 25 | //config: bool "Support option -d to redirect output to stderr" |
| 26 | //config: depends on CROND |
| 27 | //config: default y |
| 28 | //config: help |
| 29 | //config: -d N sets loglevel (0:most verbose) and directs all output to stderr. |
| 30 | //config: |
| 31 | //config:config FEATURE_CROND_CALL_SENDMAIL |
| 32 | //config: bool "Report command output via email (using sendmail)" |
| 33 | //config: default y |
| 34 | //config: depends on CROND |
| 35 | //config: help |
| 36 | //config: Command output will be sent to corresponding user via email. |
| 37 | //config: |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 38 | //config:config FEATURE_CROND_SPECIAL_TIMES |
| 39 | //config: bool "Support special times (@reboot, @daily, etc) in crontabs" |
| 40 | //config: default y |
| 41 | //config: depends on CROND |
| 42 | //config: help |
| 43 | //config: string meaning |
| 44 | //config: ------ ------- |
| 45 | //config: @reboot Run once, at startup |
| 46 | //config: @yearly Run once a year: "0 0 1 1 *" |
| 47 | //config: @annually Same as @yearly: "0 0 1 1 *" |
| 48 | //config: @monthly Run once a month: "0 0 1 * *" |
| 49 | //config: @weekly Run once a week: "0 0 * * 0" |
| 50 | //config: @daily Run once a day: "0 0 * * *" |
| 51 | //config: @midnight Same as @daily: "0 0 * * *" |
| 52 | //config: @hourly Run once an hour: "0 * * * *" |
| 53 | //config: |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 54 | //config:config FEATURE_CROND_DIR |
| 55 | //config: string "crond spool directory" |
| 56 | //config: default "/var/spool/cron" |
| 57 | //config: depends on CROND || CRONTAB |
| 58 | //config: help |
| 59 | //config: Location of crond spool. |
| 60 | |
| 61 | //applet:IF_CROND(APPLET(crond, BB_DIR_USR_SBIN, BB_SUID_DROP)) |
| 62 | |
| 63 | //kbuild:lib-$(CONFIG_CROND) += crond.o |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 64 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 65 | //usage:#define crond_trivial_usage |
| 66 | //usage: "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR" |
| 67 | //usage:#define crond_full_usage "\n\n" |
| 68 | //usage: " -f Foreground" |
| 69 | //usage: "\n -b Background (default)" |
| 70 | //usage: "\n -S Log to syslog (default)" |
Denys Vlasenko | f3d705f | 2017-01-21 03:46:35 +0100 | [diff] [blame] | 71 | //usage: "\n -l N Set log level. Most verbose 0, default 8" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 72 | //usage: IF_FEATURE_CROND_D( |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 73 | //usage: "\n -d N Set log level, log to stderr" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 74 | //usage: ) |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 75 | //usage: "\n -L FILE Log to FILE" |
| 76 | //usage: "\n -c DIR Cron dir. Default:"CONFIG_FEATURE_CROND_DIR"/crontabs" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 77 | |
Denis Vlasenko | b9c02dd | 2007-08-18 15:48:00 +0000 | [diff] [blame] | 78 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 79 | #include "common_bufsiz.h" |
Bernhard Reutner-Fischer | f470196 | 2008-01-27 12:50:12 +0000 | [diff] [blame] | 80 | #include <syslog.h> |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 81 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 82 | /* glibc frees previous setenv'ed value when we do next setenv() |
| 83 | * of the same variable. uclibc does not do this! */ |
| 84 | #if (defined(__GLIBC__) && !defined(__UCLIBC__)) /* || OTHER_SAFE_LIBC... */ |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 85 | # define SETENV_LEAKS 0 |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 86 | #else |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 87 | # define SETENV_LEAKS 1 |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 88 | #endif |
| 89 | |
| 90 | |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 91 | #define CRON_DIR CONFIG_FEATURE_CROND_DIR |
Denis Vlasenko | ded5dfe | 2009-02-03 23:59:41 +0000 | [diff] [blame] | 92 | #define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs" |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 93 | #define CRON_REBOOT CONFIG_PID_FILE_PATH "/crond.reboot" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 94 | #ifndef SENDMAIL |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 95 | # define SENDMAIL "sendmail" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 96 | #endif |
| 97 | #ifndef SENDMAIL_ARGS |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 98 | # define SENDMAIL_ARGS "-ti" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 99 | #endif |
| 100 | #ifndef CRONUPDATE |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 101 | # define CRONUPDATE "cron.update" |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 102 | #endif |
| 103 | #ifndef MAXLINES |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 104 | # define MAXLINES 256 /* max lines in non-root crontabs */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 105 | #endif |
| 106 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 107 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 108 | typedef struct CronFile { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 109 | struct CronFile *cf_next; |
| 110 | struct CronLine *cf_lines; |
| 111 | char *cf_username; |
| 112 | smallint cf_wants_starting; /* bool: one or more jobs ready */ |
| 113 | smallint cf_has_running; /* bool: one or more jobs running */ |
| 114 | smallint cf_deleted; /* marked for deletion (but still has running jobs) */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 115 | } CronFile; |
| 116 | |
| 117 | typedef struct CronLine { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 118 | struct CronLine *cl_next; |
| 119 | char *cl_cmd; /* shell command */ |
| 120 | pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */ |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 121 | #define START_ME_REBOOT -2 |
| 122 | #define START_ME_NORMAL -1 |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 123 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 124 | 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] | 125 | char *cl_mailto; /* whom to mail results, may be NULL */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 126 | #endif |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 127 | char *cl_shell; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 128 | /* ordered by size, not in natural order. makes code smaller: */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 129 | char cl_Dow[7]; /* 0-6, beginning sunday */ |
| 130 | char cl_Mons[12]; /* 0-11 */ |
| 131 | char cl_Hrs[24]; /* 0-23 */ |
| 132 | char cl_Days[32]; /* 1-31 */ |
| 133 | char cl_Mins[60]; /* 0-59 */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 134 | } CronLine; |
| 135 | |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 136 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 137 | #define DAEMON_UID 0 |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 138 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 139 | |
| 140 | enum { |
| 141 | OPT_l = (1 << 0), |
| 142 | OPT_L = (1 << 1), |
| 143 | OPT_f = (1 << 2), |
| 144 | OPT_b = (1 << 3), |
| 145 | OPT_S = (1 << 4), |
| 146 | OPT_c = (1 << 5), |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 147 | OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D, |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 148 | }; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 149 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 150 | struct globals { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 151 | unsigned log_level; /* = 8; */ |
| 152 | time_t crontab_dir_mtime; |
| 153 | const char *log_filename; |
| 154 | const char *crontab_dir_name; /* = CRONTABS; */ |
| 155 | CronFile *cron_files; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 156 | #if SETENV_LEAKS |
| 157 | char *env_var_user; |
| 158 | char *env_var_home; |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 159 | char *env_var_shell; |
| 160 | char *env_var_logname; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 161 | #endif |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 162 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 163 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 164 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 165 | setup_common_bufsiz(); \ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 166 | G.log_level = 8; \ |
| 167 | G.crontab_dir_name = CRONTABS; \ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 168 | } while (0) |
| 169 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 170 | /* Log levels: |
| 171 | * 0 is the most verbose, default 8. |
| 172 | * For some reason, in fact only 5, 7 and 8 are used. |
| 173 | */ |
| 174 | static void crondlog(unsigned level, const char *msg, va_list va) |
| 175 | { |
| 176 | if (level >= G.log_level) { |
| 177 | /* |
| 178 | * We are called only for info meesages. |
| 179 | * Warnings/errors use plain bb_[p]error_msg's, which |
| 180 | * need not touch syslog_level |
| 181 | * (they are ok with LOG_ERR default). |
| 182 | */ |
| 183 | syslog_level = LOG_INFO; |
| 184 | bb_verror_msg(msg, va, /* strerr: */ NULL); |
| 185 | syslog_level = LOG_ERR; |
| 186 | } |
| 187 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 188 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 189 | static void log5(const char *msg, ...) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 190 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 191 | va_list va; |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 192 | va_start(va, msg); |
| 193 | crondlog(4, msg, va); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 194 | va_end(va); |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 197 | static void log7(const char *msg, ...) |
| 198 | { |
| 199 | va_list va; |
| 200 | va_start(va, msg); |
| 201 | crondlog(7, msg, va); |
| 202 | va_end(va); |
| 203 | } |
| 204 | |
| 205 | static void log8(const char *msg, ...) |
| 206 | { |
| 207 | va_list va; |
| 208 | va_start(va, msg); |
| 209 | crondlog(8, msg, va); |
| 210 | va_end(va); |
| 211 | } |
| 212 | |
| 213 | |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 214 | static const char DowAry[] ALIGN1 = |
| 215 | "sun""mon""tue""wed""thu""fri""sat" |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 216 | ; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 217 | |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 218 | static const char MonAry[] ALIGN1 = |
| 219 | "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 220 | ; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 221 | |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 222 | static void ParseField(char *user, char *ary, int modvalue, int off, |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 223 | const char *names, char *ptr) |
| 224 | /* 'names' is a pointer to a set of 3-char abbreviations */ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 225 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 226 | char *base = ptr; |
| 227 | int n1 = -1; |
| 228 | int n2 = -1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 229 | |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 230 | // this can't happen due to config_read() |
| 231 | /*if (base == NULL) |
| 232 | return;*/ |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 233 | |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 234 | while (1) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 235 | int skip = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 236 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 237 | /* Handle numeric digit or symbol or '*' */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 238 | if (*ptr == '*') { |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 239 | n1 = 0; /* everything will be filled */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 240 | n2 = modvalue - 1; |
| 241 | skip = 1; |
| 242 | ++ptr; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 243 | } else if (isdigit(*ptr)) { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 244 | char *endp; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 245 | if (n1 < 0) { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 246 | n1 = strtol(ptr, &endp, 10) + off; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 247 | } else { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 248 | n2 = strtol(ptr, &endp, 10) + off; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 249 | } |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 250 | ptr = endp; /* gcc likes temp var for &endp */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 251 | skip = 1; |
| 252 | } else if (names) { |
| 253 | int i; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | f9000c5 | 2007-08-19 18:49:21 +0000 | [diff] [blame] | 255 | for (i = 0; names[i]; i += 3) { |
| 256 | /* was using strncmp before... */ |
| 257 | if (strncasecmp(ptr, &names[i], 3) == 0) { |
| 258 | ptr += 3; |
| 259 | if (n1 < 0) { |
| 260 | n1 = i / 3; |
| 261 | } else { |
| 262 | n2 = i / 3; |
| 263 | } |
| 264 | skip = 1; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 268 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 269 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 270 | /* handle optional range '-' */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 271 | if (skip == 0) { |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 272 | goto err; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 273 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 274 | if (*ptr == '-' && n2 < 0) { |
| 275 | ++ptr; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 276 | continue; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 277 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 278 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 279 | /* |
| 280 | * collapse single-value ranges, handle skipmark, and fill |
| 281 | * in the character array appropriately. |
| 282 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 283 | if (n2 < 0) { |
| 284 | n2 = n1; |
| 285 | } |
| 286 | if (*ptr == '/') { |
Denys Vlasenko | 1f27ab0 | 2009-09-23 17:17:53 +0200 | [diff] [blame] | 287 | char *endp; |
| 288 | skip = strtol(ptr + 1, &endp, 10); |
| 289 | ptr = endp; /* gcc likes temp var for &endp */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 290 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 291 | |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 292 | /* |
| 293 | * fill array, using a failsafe is the easiest way to prevent |
| 294 | * an endless loop |
| 295 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 296 | { |
| 297 | int s0 = 1; |
| 298 | int failsafe = 1024; |
| 299 | |
| 300 | --n1; |
| 301 | do { |
| 302 | n1 = (n1 + 1) % modvalue; |
| 303 | |
| 304 | if (--s0 == 0) { |
| 305 | ary[n1 % modvalue] = 1; |
| 306 | s0 = skip; |
| 307 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 308 | if (--failsafe == 0) { |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 309 | goto err; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 310 | } |
| 311 | } while (n1 != n2); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 312 | } |
| 313 | if (*ptr != ',') { |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 314 | break; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 315 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 316 | ++ptr; |
| 317 | n1 = -1; |
| 318 | n2 = -1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 319 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 320 | |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 321 | if (*ptr) { |
| 322 | err: |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 323 | bb_error_msg("user %s: parse error at %s", user, base); |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 324 | return; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 327 | /* can't use log5 (it inserts newlines), open-coding it */ |
| 328 | if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 329 | int i; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 330 | for (i = 0; i < modvalue; ++i) |
| 331 | fprintf(stderr, "%d", (unsigned char)ary[i]); |
Denys Vlasenko | 19ced5c | 2010-06-06 21:53:09 +0200 | [diff] [blame] | 332 | bb_putchar_stderr('\n'); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 333 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 336 | static void FixDayDow(CronLine *line) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 337 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 338 | unsigned i; |
"Vladimir N. Oleynik" | cd5c15d | 2006-01-30 13:36:03 +0000 | [diff] [blame] | 339 | int weekUsed = 0; |
| 340 | int daysUsed = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 341 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 342 | for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 343 | if (line->cl_Dow[i] == 0) { |
| 344 | weekUsed = 1; |
| 345 | break; |
| 346 | } |
| 347 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 348 | for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 349 | if (line->cl_Days[i] == 0) { |
| 350 | daysUsed = 1; |
| 351 | break; |
| 352 | } |
| 353 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 354 | if (weekUsed != daysUsed) { |
| 355 | if (weekUsed) |
| 356 | memset(line->cl_Days, 0, sizeof(line->cl_Days)); |
| 357 | else /* daysUsed */ |
| 358 | memset(line->cl_Dow, 0, sizeof(line->cl_Dow)); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 362 | /* |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 363 | * delete_cronfile() - delete user database |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 364 | * |
| 365 | * Note: multiple entries for same user may exist if we were unable to |
| 366 | * completely delete a database due to running processes. |
| 367 | */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 368 | //FIXME: we will start a new job even if the old job is running |
| 369 | //if crontab was reloaded: crond thinks that "new" job is different from "old" |
| 370 | //even if they are in fact completely the same. Example |
| 371 | //Crontab was: |
Denys Vlasenko | 69d69e2 | 2010-07-11 23:20:15 +0200 | [diff] [blame] | 372 | // 0-59 * * * * job1 |
| 373 | // 0-59 * * * * long_running_job2 |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 374 | //User edits crontab to: |
Denys Vlasenko | 69d69e2 | 2010-07-11 23:20:15 +0200 | [diff] [blame] | 375 | // 0-59 * * * * job1_updated |
| 376 | // 0-59 * * * * long_running_job2 |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 377 | //Bug: crond can now start another long_running_job2 even if old one |
| 378 | //is still running. |
Denys Vlasenko | 69d69e2 | 2010-07-11 23:20:15 +0200 | [diff] [blame] | 379 | //OTOH most other versions of cron do not wait for job termination anyway, |
| 380 | //they end up with multiple copies of jobs if they don't terminate soon enough. |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 381 | static void delete_cronfile(const char *userName) |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 382 | { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 383 | CronFile **pfile = &G.cron_files; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 384 | CronFile *file; |
| 385 | |
| 386 | while ((file = *pfile) != NULL) { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 387 | if (strcmp(userName, file->cf_username) == 0) { |
| 388 | CronLine **pline = &file->cf_lines; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 389 | CronLine *line; |
| 390 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 391 | file->cf_has_running = 0; |
| 392 | file->cf_deleted = 1; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 393 | |
| 394 | while ((line = *pline) != NULL) { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 395 | if (line->cl_pid > 0) { |
| 396 | file->cf_has_running = 1; |
| 397 | pline = &line->cl_next; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 398 | } else { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 399 | *pline = line->cl_next; |
| 400 | free(line->cl_cmd); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 401 | free(line); |
| 402 | } |
| 403 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 404 | if (file->cf_has_running == 0) { |
| 405 | *pfile = file->cf_next; |
| 406 | free(file->cf_username); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 407 | free(file); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 408 | continue; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 409 | } |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 410 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 411 | pfile = &file->cf_next; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 415 | static void load_crontab(const char *fileName) |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 416 | { |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 417 | struct parser_t *parser; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 418 | struct stat sbuf; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 419 | int maxLines; |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 420 | char *tokens[6]; |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 421 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 422 | char *mailTo = NULL; |
| 423 | #endif |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 424 | char *shell = NULL; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 425 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 426 | delete_cronfile(fileName); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 427 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 428 | if (!getpwnam(fileName)) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 429 | log7("ignoring file '%s' (no such user)", fileName); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 430 | return; |
| 431 | } |
| 432 | |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 433 | parser = config_open(fileName); |
| 434 | if (!parser) |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 435 | return; |
| 436 | |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 437 | maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 438 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 439 | if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 440 | CronFile *file = xzalloc(sizeof(CronFile)); |
| 441 | CronLine **pline; |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 442 | int n; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 443 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 444 | file->cf_username = xstrdup(fileName); |
| 445 | pline = &file->cf_lines; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 446 | |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 447 | while (1) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 448 | CronLine *line; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 449 | |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 450 | if (!--maxLines) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 451 | bb_error_msg("user %s: too many lines", fileName); |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 452 | break; |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 453 | } |
| 454 | |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 455 | n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY); |
| 456 | if (!n) |
| 457 | break; |
| 458 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 459 | log5("user:%s entry:%s", fileName, parser->data); |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 460 | |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 461 | /* check if line is setting MAILTO= */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 462 | if (is_prefixed_with(tokens[0], "MAILTO=")) { |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 463 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 464 | free(mailTo); |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 465 | mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL; |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 466 | #endif /* otherwise just ignore such lines */ |
| 467 | continue; |
| 468 | } |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 469 | if (is_prefixed_with(tokens[0], "SHELL=")) { |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 470 | free(shell); |
| 471 | shell = xstrdup(&tokens[0][6]); |
| 472 | continue; |
| 473 | } |
Denys Vlasenko | 0b3b65f | 2017-07-09 00:00:39 +0200 | [diff] [blame] | 474 | //TODO: handle HOME= too? "man crontab" says: |
| 475 | //name = value |
| 476 | // |
| 477 | //where the spaces around the equal-sign (=) are optional, and any subsequent |
| 478 | //non-leading spaces in value will be part of the value assigned to name. |
| 479 | //The value string may be placed in quotes (single or double, but matching) |
| 480 | //to preserve leading or trailing blanks. |
| 481 | // |
| 482 | //Several environment variables are set up automatically by the cron(8) daemon. |
| 483 | //SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd |
| 484 | //line of the crontab's owner. HOME and SHELL may be overridden by settings |
| 485 | //in the crontab; LOGNAME may not. |
| 486 | |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 487 | #if ENABLE_FEATURE_CROND_SPECIAL_TIMES |
| 488 | if (tokens[0][0] == '@') { |
| 489 | /* |
| 490 | * "@daily /a/script/to/run PARAM1 PARAM2..." |
| 491 | */ |
| 492 | typedef struct SpecialEntry { |
| 493 | const char *name; |
| 494 | const char tokens[8]; |
| 495 | } SpecialEntry; |
| 496 | static const SpecialEntry SpecAry[] = { |
| 497 | /* hour day month weekday */ |
| 498 | { "yearly", "0\0" "1\0" "1\0" "*" }, |
| 499 | { "annually", "0\0" "1\0" "1\0" "*" }, |
| 500 | { "monthly", "0\0" "1\0" "*\0" "*" }, |
| 501 | { "weekly", "0\0" "*\0" "*\0" "0" }, |
| 502 | { "daily", "0\0" "*\0" "*\0" "*" }, |
| 503 | { "midnight", "0\0" "*\0" "*\0" "*" }, |
| 504 | { "hourly", "*\0" "*\0" "*\0" "*" }, |
| 505 | { "reboot", "" }, |
| 506 | }; |
| 507 | const SpecialEntry *e = SpecAry; |
| 508 | |
| 509 | if (n < 2) |
| 510 | continue; |
| 511 | for (;;) { |
| 512 | if (strcmp(e->name, tokens[0] + 1) == 0) { |
| 513 | /* |
| 514 | * tokens[1] is only the first word of command, |
Denys Vlasenko | d18b200 | 2017-07-09 00:08:13 +0200 | [diff] [blame] | 515 | * can'r use it. |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 516 | * find the entire command in unmodified string: |
| 517 | */ |
Denys Vlasenko | d18b200 | 2017-07-09 00:08:13 +0200 | [diff] [blame] | 518 | tokens[5] = skip_whitespace( |
| 519 | skip_non_whitespace( |
| 520 | skip_whitespace(parser->data))); |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 521 | if (e->tokens[0]) { |
| 522 | char *et = (char*)e->tokens; |
| 523 | /* minute is "0" for all specials */ |
| 524 | tokens[0] = (char*)"0"; |
| 525 | tokens[1] = et; |
| 526 | tokens[2] = et + 2; |
| 527 | tokens[3] = et + 4; |
| 528 | tokens[4] = et + 6; |
| 529 | } |
| 530 | goto got_it; |
| 531 | } |
| 532 | if (!e->tokens[0]) |
| 533 | break; |
| 534 | e++; |
| 535 | } |
| 536 | continue; /* bad line (unrecognized '@foo') */ |
| 537 | } |
| 538 | #endif |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 539 | /* check if a minimum of tokens is specified */ |
Denis Vlasenko | 2e157dd | 2008-07-19 09:27:19 +0000 | [diff] [blame] | 540 | if (n < 6) |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 541 | continue; |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 542 | IF_FEATURE_CROND_SPECIAL_TIMES( |
| 543 | got_it: |
| 544 | ) |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 545 | *pline = line = xzalloc(sizeof(*line)); |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 546 | #if ENABLE_FEATURE_CROND_SPECIAL_TIMES |
| 547 | if (tokens[0][0] == '@') { /* "@reboot" line */ |
| 548 | file->cf_wants_starting = 1; |
| 549 | line->cl_pid = START_ME_REBOOT; /* wants to start */ |
| 550 | /* line->cl_Mins/Hrs/etc stay zero: never match any time */ |
| 551 | } else |
| 552 | #endif |
| 553 | { |
| 554 | /* parse date ranges */ |
| 555 | ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]); |
| 556 | ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]); |
| 557 | ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]); |
| 558 | ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]); |
| 559 | ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]); |
| 560 | /* |
| 561 | * fix days and dow - if one is not "*" and the other |
| 562 | * is "*", the other is set to 0, and vise-versa |
| 563 | */ |
| 564 | FixDayDow(line); |
| 565 | } |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 566 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 567 | /* copy mailto (can be NULL) */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 568 | line->cl_mailto = xstrdup(mailTo); |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 569 | #endif |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 570 | line->cl_shell = xstrdup(shell); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 571 | /* copy command */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 572 | line->cl_cmd = xstrdup(tokens[5]); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 573 | pline = &line->cl_next; |
Denis Vlasenko | c01340f | 2008-07-16 22:12:18 +0000 | [diff] [blame] | 574 | //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] | 575 | } |
| 576 | *pline = NULL; |
| 577 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 578 | file->cf_next = G.cron_files; |
| 579 | G.cron_files = file; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 580 | } |
Bernhard Reutner-Fischer | 6792128 | 2008-07-17 11:59:13 +0000 | [diff] [blame] | 581 | config_close(parser); |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 582 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
| 583 | free(mailTo); |
| 584 | #endif |
| 585 | free(shell); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 588 | static void process_cron_update_file(void) |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 589 | { |
| 590 | FILE *fi; |
| 591 | char buf[256]; |
| 592 | |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 593 | fi = fopen_for_read(CRONUPDATE); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 594 | if (fi != NULL) { |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 595 | unlink(CRONUPDATE); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 596 | while (fgets(buf, sizeof(buf), fi) != NULL) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 597 | /* use first word only */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 598 | skip_non_whitespace(buf)[0] = '\0'; |
| 599 | load_crontab(buf); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 600 | } |
| 601 | fclose(fi); |
| 602 | } |
| 603 | } |
| 604 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 605 | static void rescan_crontab_dir(void) |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 606 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 607 | CronFile *file; |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 608 | |
| 609 | /* 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] | 610 | again: |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 611 | for (file = G.cron_files; file; file = file->cf_next) { |
| 612 | if (!file->cf_deleted) { |
| 613 | delete_cronfile(file->cf_username); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 614 | goto again; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 615 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 618 | /* Remove cron update file */ |
Denis Vlasenko | cb448fe | 2008-02-17 14:28:53 +0000 | [diff] [blame] | 619 | unlink(CRONUPDATE); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 620 | /* Re-chdir, in case directory was renamed & deleted */ |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 621 | xchdir(G.crontab_dir_name); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 622 | |
| 623 | /* Scan directory and add associated users */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 624 | { |
| 625 | DIR *dir = opendir("."); |
| 626 | struct dirent *den; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 627 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 628 | /* xopendir exists, but "can't open '.'" is not informative */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 629 | if (!dir) |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 630 | bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name); |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 631 | while ((den = readdir(dir)) != NULL) { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 632 | if (strchr(den->d_name, '.') != NULL) { |
| 633 | continue; |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 634 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 635 | load_crontab(den->d_name); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 636 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 637 | closedir(dir); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 638 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 641 | #if SETENV_LEAKS |
| 642 | /* We set environment *before* vfork (because we want to use vfork), |
| 643 | * so we cannot use setenv() - repeated calls to setenv() may leak memory! |
| 644 | * Using putenv(), and freeing memory after unsetenv() won't leak */ |
| 645 | 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] | 646 | { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 647 | char *var_val = *pvar_val; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 648 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 649 | if (var_val) { |
| 650 | bb_unsetenv_and_free(var_val); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 651 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 652 | *pvar_val = xasprintf("%s=%s", var, val); |
| 653 | putenv(*pvar_val); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 654 | } |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 655 | #endif |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 656 | |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 657 | static void set_env_vars(struct passwd *pas, const char *shell) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 658 | { |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 659 | /* POSIX requires crond to set up at least HOME, LOGNAME, PATH, SHELL. |
| 660 | * We assume crond inherited suitable PATH. |
| 661 | */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 662 | #if SETENV_LEAKS |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 663 | safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 664 | safe_setenv(&G.env_var_user, "USER", pas->pw_name); |
| 665 | safe_setenv(&G.env_var_home, "HOME", pas->pw_dir); |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 666 | safe_setenv(&G.env_var_shell, "SHELL", shell); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 667 | #else |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 668 | xsetenv("LOGNAME", pas->pw_name); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 669 | xsetenv("USER", pas->pw_name); |
| 670 | xsetenv("HOME", pas->pw_dir); |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 671 | xsetenv("SHELL", shell); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 672 | #endif |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 675 | static void change_user(struct passwd *pas) |
| 676 | { |
| 677 | /* careful: we're after vfork! */ |
| 678 | change_identity(pas); /* - initgroups, setgid, setuid */ |
| 679 | if (chdir(pas->pw_dir) < 0) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 680 | bb_error_msg("can't change directory to '%s'", pas->pw_dir); |
| 681 | xchdir(CRON_DIR); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 682 | } |
| 683 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 684 | |
| 685 | // TODO: sendmail should be _run-time_ option, not compile-time! |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 686 | #if ENABLE_FEATURE_CROND_CALL_SENDMAIL |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 687 | |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 688 | static pid_t |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 689 | fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail) |
| 690 | { |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 691 | struct passwd *pas; |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 692 | const char *shell, *prog; |
| 693 | smallint sv_logmode; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 694 | pid_t pid; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 695 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 696 | /* prepare things before vfork */ |
| 697 | pas = getpwnam(user); |
| 698 | if (!pas) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 699 | bb_error_msg("can't get uid for %s", user); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 700 | goto err; |
| 701 | } |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 702 | |
| 703 | shell = line->cl_shell ? line->cl_shell : DEFAULT_SHELL; |
| 704 | prog = run_sendmail ? SENDMAIL : shell; |
| 705 | |
| 706 | set_env_vars(pas, shell); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 707 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 708 | sv_logmode = logmode; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 709 | pid = vfork(); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 710 | if (pid == 0) { |
| 711 | /* CHILD */ |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 712 | /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 713 | change_user(pas); |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 714 | log5("child running %s", prog); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 715 | if (mailFd >= 0) { |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 716 | xmove_fd(mailFd, run_sendmail ? 0 : 1); |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 717 | dup2(1, 2); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 718 | } |
Denis Vlasenko | 3f70b87 | 2008-12-04 13:57:59 +0000 | [diff] [blame] | 719 | /* crond 3.0pl1-100 puts tasks in separate process groups */ |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 720 | bb_setpgrp(); |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 721 | if (!run_sendmail) |
| 722 | execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL); |
| 723 | else |
| 724 | execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL); |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 725 | /* |
| 726 | * I want this error message on stderr too, |
| 727 | * even if other messages go only to syslog: |
| 728 | */ |
| 729 | logmode |= LOGMODE_STDIO; |
| 730 | bb_error_msg_and_die("can't execute '%s' for user %s", prog, user); |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 731 | } |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 732 | logmode = sv_logmode; |
Denis Vlasenko | 314820e | 2008-01-24 01:33:12 +0000 | [diff] [blame] | 733 | |
| 734 | if (pid < 0) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 735 | bb_perror_msg("vfork"); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 736 | err: |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 737 | pid = 0; |
| 738 | } /* else: PARENT, FORK SUCCESS */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 739 | |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 740 | /* |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 741 | * Close the mail file descriptor.. we can't just leave it open in |
| 742 | * a structure, closing it later, because we might run out of descriptors |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 743 | */ |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 744 | if (mailFd >= 0) { |
| 745 | close(mailFd); |
| 746 | } |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 747 | return pid; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 748 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 749 | |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 750 | static pid_t start_one_job(const char *user, CronLine *line) |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 751 | { |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 752 | char mailFile[128]; |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 753 | int mailFd = -1; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 754 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 755 | line->cl_pid = 0; |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 756 | line->cl_empty_mail_size = 0; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 757 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 758 | if (line->cl_mailto) { |
| 759 | /* Open mail file (owner is root so nobody can screw with it) */ |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 760 | snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid()); |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 761 | 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] | 762 | |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 763 | if (mailFd >= 0) { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 764 | fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto, |
| 765 | line->cl_cmd); |
| 766 | line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR); |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 767 | } else { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 768 | bb_error_msg("can't create mail file %s for user %s, " |
Denis Vlasenko | 6fa1ba3 | 2008-04-07 21:02:35 +0000 | [diff] [blame] | 769 | "discarding output", mailFile, user); |
| 770 | } |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 771 | } |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 772 | |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 773 | line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 0); |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 774 | if (mailFd >= 0) { |
| 775 | if (line->cl_pid <= 0) { |
| 776 | unlink(mailFile); |
| 777 | } else { |
| 778 | /* rename mail-file based on pid of process */ |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 779 | char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid); |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 780 | rename(mailFile, mailFile2); // TODO: xrename? |
| 781 | free(mailFile2); |
| 782 | } |
| 783 | } |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 784 | |
| 785 | return line->cl_pid; |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
| 789 | * process_finished_job - called when job terminates and when mail terminates |
| 790 | */ |
| 791 | static void process_finished_job(const char *user, CronLine *line) |
| 792 | { |
| 793 | pid_t pid; |
| 794 | int mailFd; |
| 795 | char mailFile[128]; |
| 796 | struct stat sbuf; |
| 797 | |
| 798 | pid = line->cl_pid; |
| 799 | line->cl_pid = 0; |
| 800 | if (pid <= 0) { |
| 801 | /* No job */ |
| 802 | return; |
| 803 | } |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 804 | if (line->cl_empty_mail_size <= 0) { |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 805 | /* End of job and no mail file, or end of sendmail job */ |
| 806 | return; |
| 807 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 808 | |
| 809 | /* |
| 810 | * End of primary job - check for mail file. |
| 811 | * If size has changed and the file is still valid, we send it. |
| 812 | */ |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 813 | snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 814 | mailFd = open(mailFile, O_RDONLY); |
| 815 | unlink(mailFile); |
| 816 | if (mailFd < 0) { |
| 817 | return; |
| 818 | } |
| 819 | |
| 820 | if (fstat(mailFd, &sbuf) < 0 |
| 821 | || sbuf.st_uid != DAEMON_UID |
| 822 | || sbuf.st_nlink != 0 |
| 823 | || sbuf.st_size == line->cl_empty_mail_size |
| 824 | || !S_ISREG(sbuf.st_mode) |
| 825 | ) { |
| 826 | close(mailFd); |
| 827 | return; |
| 828 | } |
Denys Vlasenko | 1f0ab1d | 2010-07-08 16:12:10 +0200 | [diff] [blame] | 829 | line->cl_empty_mail_size = 0; |
| 830 | /* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */ |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 831 | line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 1); |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 832 | } |
| 833 | |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 834 | #else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 835 | |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 836 | static pid_t start_one_job(const char *user, CronLine *line) |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 837 | { |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 838 | const char *shell; |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 839 | struct passwd *pas; |
| 840 | pid_t pid; |
Eric Andersen | 35e643b | 2003-07-28 07:40:39 +0000 | [diff] [blame] | 841 | |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 842 | pas = getpwnam(user); |
| 843 | if (!pas) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 844 | bb_error_msg("can't get uid for %s", user); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 845 | goto err; |
| 846 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 847 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 848 | /* Prepare things before vfork */ |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 849 | shell = line->cl_shell ? line->cl_shell : DEFAULT_SHELL; |
| 850 | set_env_vars(pas, shell); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 851 | |
| 852 | /* Fork as the user in question and run program */ |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 853 | pid = vfork(); |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 854 | if (pid == 0) { |
| 855 | /* CHILD */ |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 856 | /* initgroups, setgid, setuid, and chdir to home or CRON_DIR */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 857 | change_user(pas); |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 858 | log5("child running %s", shell); |
Denis Vlasenko | 3f70b87 | 2008-12-04 13:57:59 +0000 | [diff] [blame] | 859 | /* crond 3.0pl1-100 puts tasks in separate process groups */ |
Denis Vlasenko | 6ebb2f5 | 2008-12-03 10:46:12 +0000 | [diff] [blame] | 860 | bb_setpgrp(); |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 861 | execl(shell, shell, "-c", line->cl_cmd, (char *) NULL); |
| 862 | bb_error_msg_and_die("can't execute '%s' for user %s", shell, user); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 863 | } |
| 864 | if (pid < 0) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 865 | bb_perror_msg("vfork"); |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 866 | err: |
Glenn L McGrath | 9079ad0 | 2004-02-22 04:44:21 +0000 | [diff] [blame] | 867 | pid = 0; |
| 868 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 869 | line->cl_pid = pid; |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 870 | return pid; |
Eric Andersen | f6f7bfb | 2002-10-22 12:24:59 +0000 | [diff] [blame] | 871 | } |
Denis Vlasenko | 4e6c812 | 2008-03-12 22:10:25 +0000 | [diff] [blame] | 872 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 873 | #define process_finished_job(user, line) ((line)->cl_pid = 0) |
| 874 | |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 875 | #endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */ |
| 876 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 877 | /* |
| 878 | * Determine which jobs need to be run. Under normal conditions, the |
| 879 | * period is about a minute (one scan). Worst case it will be one |
| 880 | * hour (60 scans). |
| 881 | */ |
| 882 | static void flag_starting_jobs(time_t t1, time_t t2) |
| 883 | { |
| 884 | time_t t; |
| 885 | |
| 886 | /* Find jobs > t1 and <= t2 */ |
| 887 | |
| 888 | for (t = t1 - t1 % 60; t <= t2; t += 60) { |
| 889 | struct tm *ptm; |
| 890 | CronFile *file; |
| 891 | CronLine *line; |
| 892 | |
| 893 | if (t <= t1) |
| 894 | continue; |
| 895 | |
| 896 | ptm = localtime(&t); |
| 897 | for (file = G.cron_files; file; file = file->cf_next) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 898 | log5("file %s:", file->cf_username); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 899 | if (file->cf_deleted) |
| 900 | continue; |
| 901 | for (line = file->cf_lines; line; line = line->cl_next) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 902 | log5(" line %s", line->cl_cmd); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 903 | if (line->cl_Mins[ptm->tm_min] |
| 904 | && line->cl_Hrs[ptm->tm_hour] |
| 905 | && (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday]) |
| 906 | && line->cl_Mons[ptm->tm_mon] |
| 907 | ) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 908 | log5(" job: %d %s", |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 909 | (int)line->cl_pid, line->cl_cmd); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 910 | if (line->cl_pid > 0) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 911 | log8("user %s: process already running: %s", |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 912 | file->cf_username, line->cl_cmd); |
| 913 | } else if (line->cl_pid == 0) { |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 914 | line->cl_pid = START_ME_NORMAL; |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 915 | file->cf_wants_starting = 1; |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | } |
| 922 | |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 923 | #if ENABLE_FEATURE_CROND_SPECIAL_TIMES |
| 924 | static int touch_reboot_file(void) |
| 925 | { |
| 926 | int fd = open(CRON_REBOOT, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0000); |
| 927 | if (fd >= 0) { |
| 928 | close(fd); |
| 929 | return 1; |
| 930 | } |
| 931 | /* File (presumably) exists - this is not the first run after reboot */ |
| 932 | return 0; |
| 933 | } |
| 934 | #endif |
| 935 | |
| 936 | static void start_jobs(int wants_start) |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 937 | { |
| 938 | CronFile *file; |
| 939 | CronLine *line; |
| 940 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 941 | for (file = G.cron_files; file; file = file->cf_next) { |
| 942 | if (!file->cf_wants_starting) |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 943 | continue; |
| 944 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 945 | file->cf_wants_starting = 0; |
| 946 | for (line = file->cf_lines; line; line = line->cl_next) { |
| 947 | pid_t pid; |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 948 | if (line->cl_pid != wants_start) |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 949 | continue; |
| 950 | |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 951 | pid = start_one_job(file->cf_username, line); |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 952 | log8("USER %s pid %3d cmd %s", |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 953 | file->cf_username, (int)pid, line->cl_cmd); |
| 954 | if (pid < 0) { |
| 955 | file->cf_wants_starting = 1; |
| 956 | } |
| 957 | if (pid > 0) { |
| 958 | file->cf_has_running = 1; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 964 | /* |
| 965 | * Check for job completion, return number of jobs still running after |
| 966 | * all done. |
| 967 | */ |
| 968 | static int check_completions(void) |
| 969 | { |
| 970 | CronFile *file; |
| 971 | CronLine *line; |
| 972 | int num_still_running = 0; |
| 973 | |
| 974 | for (file = G.cron_files; file; file = file->cf_next) { |
| 975 | if (!file->cf_has_running) |
| 976 | continue; |
| 977 | |
| 978 | file->cf_has_running = 0; |
| 979 | for (line = file->cf_lines; line; line = line->cl_next) { |
| 980 | int r; |
| 981 | |
| 982 | if (line->cl_pid <= 0) |
| 983 | continue; |
| 984 | |
| 985 | r = waitpid(line->cl_pid, NULL, WNOHANG); |
| 986 | if (r < 0 || r == line->cl_pid) { |
| 987 | process_finished_job(file->cf_username, line); |
| 988 | if (line->cl_pid == 0) { |
| 989 | /* sendmail was not started for it */ |
| 990 | continue; |
| 991 | } |
| 992 | /* else: sendmail was started, job is still running, fall thru */ |
| 993 | } |
| 994 | /* else: r == 0: "process is still running" */ |
| 995 | file->cf_has_running = 1; |
| 996 | } |
| 997 | //FIXME: if !file->cf_has_running && file->deleted: delete it! |
| 998 | //otherwise deleted entries will stay forever, right? |
| 999 | num_still_running += file->cf_has_running; |
| 1000 | } |
| 1001 | return num_still_running; |
| 1002 | } |
| 1003 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 1004 | static void reopen_logfile_to_stderr(void) |
| 1005 | { |
| 1006 | if (G.log_filename) { |
| 1007 | int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND); |
| 1008 | if (logfd >= 0) |
| 1009 | xmove_fd(logfd, STDERR_FILENO); |
| 1010 | } |
| 1011 | } |
| 1012 | |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1013 | int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 1014 | int crond_main(int argc UNUSED_PARAM, char **argv) |
| 1015 | { |
| 1016 | time_t t2; |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 1017 | unsigned rescan; |
| 1018 | unsigned sleep_time; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1019 | unsigned opts; |
| 1020 | |
| 1021 | INIT_G(); |
| 1022 | |
| 1023 | /* "-b after -f is ignored", and so on for every pair a-b */ |
| 1024 | opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l") |
Denys Vlasenko | 8c84f75 | 2011-09-07 05:56:09 +0200 | [diff] [blame] | 1025 | /* -l and -d have numeric param */ |
| 1026 | ":l+" IF_FEATURE_CROND_D(":d+"); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1027 | opts = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"), |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1028 | &G.log_level, &G.log_filename, &G.crontab_dir_name |
| 1029 | IF_FEATURE_CROND_D(,&G.log_level)); |
| 1030 | /* 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] | 1031 | |
| 1032 | if (!(opts & OPT_f)) { |
| 1033 | /* close stdin, stdout, stderr. |
| 1034 | * close unused descriptors - don't need them. */ |
| 1035 | bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv); |
| 1036 | } |
| 1037 | |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1038 | if (!(opts & OPT_d) && G.log_filename == NULL) { |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1039 | /* logging to syslog */ |
| 1040 | openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON); |
| 1041 | logmode = LOGMODE_SYSLOG; |
| 1042 | } |
| 1043 | |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1044 | //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */ |
Denys Vlasenko | 43b8a1c | 2014-04-30 17:38:27 +0200 | [diff] [blame] | 1045 | |
| 1046 | reopen_logfile_to_stderr(); |
| 1047 | xchdir(G.crontab_dir_name); |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 1048 | log8("crond (busybox "BB_VER") started, log level %d", G.log_level); |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1049 | rescan_crontab_dir(); |
Anthony G. Basile | 12677ac | 2012-12-10 14:49:39 -0500 | [diff] [blame] | 1050 | write_pidfile(CONFIG_PID_FILE_PATH "/crond.pid"); |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 1051 | #if ENABLE_FEATURE_CROND_SPECIAL_TIMES |
| 1052 | if (touch_reboot_file()) |
| 1053 | start_jobs(START_ME_REBOOT); /* start @reboot entries, if any */ |
| 1054 | #endif |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1055 | |
Denys Vlasenko | dfc870f | 2010-07-08 04:07:54 +0200 | [diff] [blame] | 1056 | /* Main loop */ |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1057 | t2 = time(NULL); |
| 1058 | rescan = 60; |
| 1059 | sleep_time = 60; |
| 1060 | for (;;) { |
Denys Vlasenko | dfc870f | 2010-07-08 04:07:54 +0200 | [diff] [blame] | 1061 | struct stat sbuf; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1062 | time_t t1; |
| 1063 | long dt; |
| 1064 | |
Denys Vlasenko | dfc870f | 2010-07-08 04:07:54 +0200 | [diff] [blame] | 1065 | /* Synchronize to 1 minute, minimum 1 second */ |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 1066 | t1 = t2; |
| 1067 | sleep(sleep_time - (time(NULL) % sleep_time)); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1068 | t2 = time(NULL); |
| 1069 | dt = (long)t2 - (long)t1; |
| 1070 | |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 1071 | reopen_logfile_to_stderr(); |
| 1072 | |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1073 | /* |
| 1074 | * The file 'cron.update' is checked to determine new cron |
| 1075 | * jobs. The directory is rescanned once an hour to deal |
| 1076 | * with any screwups. |
| 1077 | * |
| 1078 | * Check for time jump. Disparities over an hour either way |
| 1079 | * result in resynchronization. A negative disparity |
| 1080 | * less than an hour causes us to effectively sleep until we |
| 1081 | * match the original time (i.e. no re-execution of jobs that |
| 1082 | * have just been run). A positive disparity less than |
| 1083 | * an hour causes intermediate jobs to be run, but only once |
| 1084 | * in the worst case. |
| 1085 | * |
| 1086 | * When running jobs, the inequality used is greater but not |
| 1087 | * equal to t1, and less then or equal to t2. |
| 1088 | */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1089 | if (stat(G.crontab_dir_name, &sbuf) != 0) |
| 1090 | sbuf.st_mtime = 0; /* force update (once) if dir was deleted */ |
| 1091 | if (G.crontab_dir_mtime != sbuf.st_mtime) { |
| 1092 | G.crontab_dir_mtime = sbuf.st_mtime; |
Denys Vlasenko | dfc870f | 2010-07-08 04:07:54 +0200 | [diff] [blame] | 1093 | rescan = 1; |
| 1094 | } |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1095 | if (--rescan == 0) { |
| 1096 | rescan = 60; |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1097 | rescan_crontab_dir(); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1098 | } |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1099 | process_cron_update_file(); |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 1100 | log5("wakeup dt=%ld", dt); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1101 | if (dt < -60 * 60 || dt > 60 * 60) { |
Denys Vlasenko | d5929d6 | 2014-04-30 14:49:52 +0200 | [diff] [blame] | 1102 | bb_error_msg("time disparity of %ld minutes detected", dt / 60); |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1103 | /* and we do not run any jobs in this case */ |
| 1104 | } else if (dt > 0) { |
Denys Vlasenko | dfc870f | 2010-07-08 04:07:54 +0200 | [diff] [blame] | 1105 | /* Usual case: time advances forward, as expected */ |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1106 | flag_starting_jobs(t1, t2); |
Denys Vlasenko | 75fbea3 | 2017-07-08 20:53:11 +0200 | [diff] [blame] | 1107 | start_jobs(START_ME_NORMAL); |
Denys Vlasenko | 01a1a96 | 2014-04-30 14:47:28 +0200 | [diff] [blame] | 1108 | sleep_time = 60; |
Denys Vlasenko | 45963c8 | 2010-07-08 15:37:10 +0200 | [diff] [blame] | 1109 | if (check_completions() > 0) { |
| 1110 | /* some jobs are still running */ |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1111 | sleep_time = 10; |
Denys Vlasenko | 4a09aef | 2010-07-08 04:07:15 +0200 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | /* else: time jumped back, do not run any jobs */ |
| 1115 | } /* for (;;) */ |
| 1116 | |
| 1117 | return 0; /* not reached */ |
| 1118 | } |