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