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