2006-04-12 23:39:26 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-10-22 17:54:59 +05:30
|
|
|
/*
|
|
|
|
* run as root, but NOT setuid root
|
|
|
|
*
|
|
|
|
* Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
|
2007-08-18 21:18:00 +05:30
|
|
|
* (version 2.3.2)
|
2006-04-17 02:08:26 +05:30
|
|
|
* Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
|
2002-10-22 17:54:59 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-10-22 17:54:59 +05:30
|
|
|
*/
|
2014-04-30 18:17:28 +05:30
|
|
|
//config:config CROND
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "crond (14 kb)"
|
2014-04-30 18:17:28 +05:30
|
|
|
//config: default y
|
|
|
|
//config: select FEATURE_SYSLOG
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Crond is a background daemon that parses individual crontab
|
|
|
|
//config: files and executes commands on behalf of the users in question.
|
|
|
|
//config: This is a port of dcron from slackware. It uses files of the
|
|
|
|
//config: format /var/spool/cron/crontabs/<username> files, for example:
|
|
|
|
//config: $ cat /var/spool/cron/crontabs/root
|
|
|
|
//config: # Run daily cron jobs at 4:40 every day:
|
|
|
|
//config: 40 4 * * * /etc/cron/daily > /dev/null 2>&1
|
2014-04-30 18:17:28 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CROND_D
|
2017-07-27 18:15:25 +05:30
|
|
|
//config: bool "Support -d (redirect output to stderr)"
|
2014-04-30 18:17:28 +05:30
|
|
|
//config: depends on CROND
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: -d N sets loglevel (0:most verbose) and directs all output to stderr.
|
2014-04-30 18:17:28 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
//config: bool "Report command output via email (using sendmail)"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on CROND
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Command output will be sent to corresponding user via email.
|
2014-04-30 18:17:28 +05:30
|
|
|
//config:
|
2017-07-09 00:23:11 +05:30
|
|
|
//config:config FEATURE_CROND_SPECIAL_TIMES
|
|
|
|
//config: bool "Support special times (@reboot, @daily, etc) in crontabs"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on CROND
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: string meaning
|
|
|
|
//config: ------ -------
|
|
|
|
//config: @reboot Run once, at startup
|
|
|
|
//config: @yearly Run once a year: "0 0 1 1 *"
|
|
|
|
//config: @annually Same as @yearly: "0 0 1 1 *"
|
|
|
|
//config: @monthly Run once a month: "0 0 1 * *"
|
|
|
|
//config: @weekly Run once a week: "0 0 * * 0"
|
|
|
|
//config: @daily Run once a day: "0 0 * * *"
|
|
|
|
//config: @midnight Same as @daily: "0 0 * * *"
|
|
|
|
//config: @hourly Run once an hour: "0 * * * *"
|
2017-07-09 00:23:11 +05:30
|
|
|
//config:
|
2014-04-30 18:17:28 +05:30
|
|
|
//config:config FEATURE_CROND_DIR
|
|
|
|
//config: string "crond spool directory"
|
|
|
|
//config: default "/var/spool/cron"
|
|
|
|
//config: depends on CROND || CRONTAB
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Location of crond spool.
|
2014-04-30 18:17:28 +05:30
|
|
|
|
|
|
|
//applet:IF_CROND(APPLET(crond, BB_DIR_USR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_CROND) += crond.o
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define crond_trivial_usage
|
|
|
|
//usage: "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR"
|
|
|
|
//usage:#define crond_full_usage "\n\n"
|
|
|
|
//usage: " -f Foreground"
|
|
|
|
//usage: "\n -b Background (default)"
|
|
|
|
//usage: "\n -S Log to syslog (default)"
|
2017-01-21 08:16:35 +05:30
|
|
|
//usage: "\n -l N Set log level. Most verbose 0, default 8"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: IF_FEATURE_CROND_D(
|
2014-04-30 18:17:28 +05:30
|
|
|
//usage: "\n -d N Set log level, log to stderr"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage: )
|
2014-04-30 18:17:28 +05:30
|
|
|
//usage: "\n -L FILE Log to FILE"
|
|
|
|
//usage: "\n -c DIR Cron dir. Default:"CONFIG_FEATURE_CROND_DIR"/crontabs"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2007-08-18 21:18:00 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2008-01-27 18:20:12 +05:30
|
|
|
#include <syslog.h>
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2017-07-22 05:55:47 +05:30
|
|
|
#if 0
|
|
|
|
/* If libc tracks and reuses setenv()-allocated memory, ok to set this to 0 */
|
|
|
|
/* Neither glibc nor uclibc do that! */
|
2010-07-08 07:37:15 +05:30
|
|
|
# define SETENV_LEAKS 0
|
2008-03-13 03:40:25 +05:30
|
|
|
#else
|
2010-07-08 07:37:15 +05:30
|
|
|
# define SETENV_LEAKS 1
|
2008-03-13 03:40:25 +05:30
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2014-04-30 18:17:28 +05:30
|
|
|
#define CRON_DIR CONFIG_FEATURE_CROND_DIR
|
2009-02-04 05:29:41 +05:30
|
|
|
#define CRONTABS CONFIG_FEATURE_CROND_DIR "/crontabs"
|
2017-07-09 00:23:11 +05:30
|
|
|
#define CRON_REBOOT CONFIG_PID_FILE_PATH "/crond.reboot"
|
2002-10-22 17:54:59 +05:30
|
|
|
#ifndef SENDMAIL
|
2010-07-08 07:37:15 +05:30
|
|
|
# define SENDMAIL "sendmail"
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
#ifndef SENDMAIL_ARGS
|
2010-07-08 19:42:10 +05:30
|
|
|
# define SENDMAIL_ARGS "-ti"
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
#ifndef CRONUPDATE
|
2010-07-08 07:37:15 +05:30
|
|
|
# define CRONUPDATE "cron.update"
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
#ifndef MAXLINES
|
2010-10-28 22:27:19 +05:30
|
|
|
# define MAXLINES 256 /* max lines in non-root crontabs */
|
2002-10-22 17:54:59 +05:30
|
|
|
#endif
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
typedef struct CronFile {
|
2010-07-08 19:07:10 +05:30
|
|
|
struct CronFile *cf_next;
|
|
|
|
struct CronLine *cf_lines;
|
|
|
|
char *cf_username;
|
|
|
|
smallint cf_wants_starting; /* bool: one or more jobs ready */
|
|
|
|
smallint cf_has_running; /* bool: one or more jobs running */
|
|
|
|
smallint cf_deleted; /* marked for deletion (but still has running jobs) */
|
2002-10-22 17:54:59 +05:30
|
|
|
} CronFile;
|
|
|
|
|
|
|
|
typedef struct CronLine {
|
2010-07-08 19:07:10 +05:30
|
|
|
struct CronLine *cl_next;
|
|
|
|
char *cl_cmd; /* shell command */
|
|
|
|
pid_t cl_pid; /* >0:running, <0:needs to be started in this minute, 0:dormant */
|
2017-07-09 00:23:11 +05:30
|
|
|
#define START_ME_REBOOT -2
|
|
|
|
#define START_ME_NORMAL -1
|
2008-03-13 03:40:25 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
2010-07-08 19:42:10 +05:30
|
|
|
int cl_empty_mail_size; /* size of mail header only, 0 if no mailfile */
|
2010-07-08 19:07:10 +05:30
|
|
|
char *cl_mailto; /* whom to mail results, may be NULL */
|
2008-03-13 03:40:25 +05:30
|
|
|
#endif
|
2014-04-30 21:08:27 +05:30
|
|
|
char *cl_shell;
|
2008-03-13 03:40:25 +05:30
|
|
|
/* ordered by size, not in natural order. makes code smaller: */
|
2010-07-08 19:07:10 +05:30
|
|
|
char cl_Dow[7]; /* 0-6, beginning sunday */
|
|
|
|
char cl_Mons[12]; /* 0-11 */
|
|
|
|
char cl_Hrs[24]; /* 0-23 */
|
|
|
|
char cl_Days[32]; /* 1-31 */
|
|
|
|
char cl_Mins[60]; /* 0-59 */
|
2002-10-22 17:54:59 +05:30
|
|
|
} CronLine;
|
|
|
|
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
#define DAEMON_UID 0
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
|
|
|
|
enum {
|
|
|
|
OPT_l = (1 << 0),
|
|
|
|
OPT_L = (1 << 1),
|
|
|
|
OPT_f = (1 << 2),
|
|
|
|
OPT_b = (1 << 3),
|
|
|
|
OPT_S = (1 << 4),
|
|
|
|
OPT_c = (1 << 5),
|
2008-11-06 06:19:59 +05:30
|
|
|
OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
|
2008-03-13 03:40:25 +05:30
|
|
|
};
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
struct globals {
|
2010-07-08 19:07:10 +05:30
|
|
|
unsigned log_level; /* = 8; */
|
|
|
|
time_t crontab_dir_mtime;
|
|
|
|
const char *log_filename;
|
|
|
|
const char *crontab_dir_name; /* = CRONTABS; */
|
|
|
|
CronFile *cron_files;
|
2017-07-22 05:17:19 +05:30
|
|
|
char *default_shell;
|
2008-03-13 03:40:25 +05:30
|
|
|
#if SETENV_LEAKS
|
|
|
|
char *env_var_user;
|
|
|
|
char *env_var_home;
|
2014-04-30 21:08:27 +05:30
|
|
|
char *env_var_shell;
|
|
|
|
char *env_var_logname;
|
2008-03-13 03:40:25 +05:30
|
|
|
#endif
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2008-03-13 03:40:25 +05:30
|
|
|
#define INIT_G() do { \
|
2016-04-21 21:48:48 +05:30
|
|
|
setup_common_bufsiz(); \
|
2010-07-08 19:07:10 +05:30
|
|
|
G.log_level = 8; \
|
|
|
|
G.crontab_dir_name = CRONTABS; \
|
2008-03-13 03:40:25 +05:30
|
|
|
} while (0)
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
/* Log levels:
|
|
|
|
* 0 is the most verbose, default 8.
|
|
|
|
* For some reason, in fact only 5, 7 and 8 are used.
|
|
|
|
*/
|
|
|
|
static void crondlog(unsigned level, const char *msg, va_list va)
|
|
|
|
{
|
|
|
|
if (level >= G.log_level) {
|
|
|
|
/*
|
|
|
|
* We are called only for info meesages.
|
|
|
|
* Warnings/errors use plain bb_[p]error_msg's, which
|
|
|
|
* need not touch syslog_level
|
|
|
|
* (they are ok with LOG_ERR default).
|
|
|
|
*/
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_vinfo_msg(msg, va);
|
2014-04-30 18:19:52 +05:30
|
|
|
}
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
static void log5(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, msg);
|
|
|
|
crondlog(4, msg, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
static void log7(const char *msg, ...)
|
2003-07-28 13:10:39 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
va_list va;
|
2014-04-30 18:19:52 +05:30
|
|
|
va_start(va, msg);
|
|
|
|
crondlog(7, msg, va);
|
|
|
|
va_end(va);
|
|
|
|
}
|
2003-07-28 13:10:39 +05:30
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
static void log8(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
va_start(va, msg);
|
|
|
|
crondlog(8, msg, va);
|
2004-02-22 10:14:21 +05:30
|
|
|
va_end(va);
|
2003-07-28 13:10:39 +05:30
|
|
|
}
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
|
2007-08-20 00:19:21 +05:30
|
|
|
static const char DowAry[] ALIGN1 =
|
|
|
|
"sun""mon""tue""wed""thu""fri""sat"
|
|
|
|
;
|
|
|
|
|
|
|
|
static const char MonAry[] ALIGN1 =
|
|
|
|
"jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
|
|
|
|
;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
static void ParseField(char *user, char *ary, int modvalue, int off,
|
2007-08-20 00:19:21 +05:30
|
|
|
const char *names, char *ptr)
|
|
|
|
/* 'names' is a pointer to a set of 3-char abbreviations */
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
char *base = ptr;
|
|
|
|
int n1 = -1;
|
|
|
|
int n2 = -1;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
// this can't happen due to config_read()
|
|
|
|
/*if (base == NULL)
|
|
|
|
return;*/
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
while (1) {
|
2004-02-22 10:14:21 +05:30
|
|
|
int skip = 0;
|
|
|
|
|
|
|
|
/* Handle numeric digit or symbol or '*' */
|
|
|
|
if (*ptr == '*') {
|
2010-10-28 22:27:19 +05:30
|
|
|
n1 = 0; /* everything will be filled */
|
2004-02-22 10:14:21 +05:30
|
|
|
n2 = modvalue - 1;
|
|
|
|
skip = 1;
|
|
|
|
++ptr;
|
2008-03-13 03:40:25 +05:30
|
|
|
} else if (isdigit(*ptr)) {
|
2009-09-23 20:47:53 +05:30
|
|
|
char *endp;
|
2004-02-22 10:14:21 +05:30
|
|
|
if (n1 < 0) {
|
2009-09-23 20:47:53 +05:30
|
|
|
n1 = strtol(ptr, &endp, 10) + off;
|
2004-02-22 10:14:21 +05:30
|
|
|
} else {
|
2009-09-23 20:47:53 +05:30
|
|
|
n2 = strtol(ptr, &endp, 10) + off;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2009-09-23 20:47:53 +05:30
|
|
|
ptr = endp; /* gcc likes temp var for &endp */
|
2004-02-22 10:14:21 +05:30
|
|
|
skip = 1;
|
|
|
|
} else if (names) {
|
|
|
|
int i;
|
|
|
|
|
2007-08-20 00:19:21 +05:30
|
|
|
for (i = 0; names[i]; i += 3) {
|
|
|
|
/* was using strncmp before... */
|
|
|
|
if (strncasecmp(ptr, &names[i], 3) == 0) {
|
|
|
|
ptr += 3;
|
|
|
|
if (n1 < 0) {
|
|
|
|
n1 = i / 3;
|
|
|
|
} else {
|
|
|
|
n2 = i / 3;
|
|
|
|
}
|
|
|
|
skip = 1;
|
2004-02-22 10:14:21 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/* handle optional range '-' */
|
|
|
|
if (skip == 0) {
|
2008-07-17 03:42:18 +05:30
|
|
|
goto err;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
if (*ptr == '-' && n2 < 0) {
|
|
|
|
++ptr;
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/*
|
|
|
|
* collapse single-value ranges, handle skipmark, and fill
|
|
|
|
* in the character array appropriately.
|
|
|
|
*/
|
|
|
|
if (n2 < 0) {
|
|
|
|
n2 = n1;
|
|
|
|
}
|
|
|
|
if (*ptr == '/') {
|
2009-09-23 20:47:53 +05:30
|
|
|
char *endp;
|
|
|
|
skip = strtol(ptr + 1, &endp, 10);
|
|
|
|
ptr = endp; /* gcc likes temp var for &endp */
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2004-02-22 10:14:21 +05:30
|
|
|
/*
|
|
|
|
* fill array, using a failsafe is the easiest way to prevent
|
|
|
|
* an endless loop
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
int s0 = 1;
|
|
|
|
int failsafe = 1024;
|
|
|
|
|
|
|
|
--n1;
|
|
|
|
do {
|
|
|
|
n1 = (n1 + 1) % modvalue;
|
|
|
|
|
|
|
|
if (--s0 == 0) {
|
|
|
|
ary[n1 % modvalue] = 1;
|
|
|
|
s0 = skip;
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
if (--failsafe == 0) {
|
2008-07-17 03:42:18 +05:30
|
|
|
goto err;
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
} while (n1 != n2);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
if (*ptr != ',') {
|
|
|
|
break;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2004-02-22 10:14:21 +05:30
|
|
|
++ptr;
|
|
|
|
n1 = -1;
|
|
|
|
n2 = -1;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
if (*ptr) {
|
|
|
|
err:
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg("user %s: parse error at %s", user, base);
|
2008-07-17 03:42:18 +05:30
|
|
|
return;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
/* can't use log5 (it inserts newlines), open-coding it */
|
|
|
|
if (G.log_level <= 5 && logmode != LOGMODE_SYSLOG) {
|
2004-02-22 10:14:21 +05:30
|
|
|
int i;
|
2008-03-13 03:40:25 +05:30
|
|
|
for (i = 0; i < modvalue; ++i)
|
|
|
|
fprintf(stderr, "%d", (unsigned char)ary[i]);
|
2010-06-07 01:23:09 +05:30
|
|
|
bb_putchar_stderr('\n');
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
static void FixDayDow(CronLine *line)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-05-16 03:00:45 +05:30
|
|
|
unsigned i;
|
2006-01-30 19:06:03 +05:30
|
|
|
int weekUsed = 0;
|
|
|
|
int daysUsed = 0;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(line->cl_Dow); ++i) {
|
2004-02-22 10:14:21 +05:30
|
|
|
if (line->cl_Dow[i] == 0) {
|
|
|
|
weekUsed = 1;
|
|
|
|
break;
|
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
for (i = 0; i < ARRAY_SIZE(line->cl_Days); ++i) {
|
2004-02-22 10:14:21 +05:30
|
|
|
if (line->cl_Days[i] == 0) {
|
|
|
|
daysUsed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
if (weekUsed != daysUsed) {
|
|
|
|
if (weekUsed)
|
|
|
|
memset(line->cl_Days, 0, sizeof(line->cl_Days));
|
|
|
|
else /* daysUsed */
|
|
|
|
memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 07:37:15 +05:30
|
|
|
/*
|
2010-07-08 19:07:10 +05:30
|
|
|
* delete_cronfile() - delete user database
|
2010-07-08 07:37:15 +05:30
|
|
|
*
|
|
|
|
* Note: multiple entries for same user may exist if we were unable to
|
|
|
|
* completely delete a database due to running processes.
|
|
|
|
*/
|
2010-07-08 19:07:10 +05:30
|
|
|
//FIXME: we will start a new job even if the old job is running
|
|
|
|
//if crontab was reloaded: crond thinks that "new" job is different from "old"
|
|
|
|
//even if they are in fact completely the same. Example
|
|
|
|
//Crontab was:
|
2010-07-12 02:50:15 +05:30
|
|
|
// 0-59 * * * * job1
|
|
|
|
// 0-59 * * * * long_running_job2
|
2010-07-08 19:07:10 +05:30
|
|
|
//User edits crontab to:
|
2010-07-12 02:50:15 +05:30
|
|
|
// 0-59 * * * * job1_updated
|
|
|
|
// 0-59 * * * * long_running_job2
|
2010-07-08 19:07:10 +05:30
|
|
|
//Bug: crond can now start another long_running_job2 even if old one
|
|
|
|
//is still running.
|
2010-07-12 02:50:15 +05:30
|
|
|
//OTOH most other versions of cron do not wait for job termination anyway,
|
|
|
|
//they end up with multiple copies of jobs if they don't terminate soon enough.
|
2010-07-08 19:07:10 +05:30
|
|
|
static void delete_cronfile(const char *userName)
|
2010-07-08 07:37:15 +05:30
|
|
|
{
|
2010-07-08 19:07:10 +05:30
|
|
|
CronFile **pfile = &G.cron_files;
|
2010-07-08 07:37:15 +05:30
|
|
|
CronFile *file;
|
|
|
|
|
|
|
|
while ((file = *pfile) != NULL) {
|
2010-07-08 19:07:10 +05:30
|
|
|
if (strcmp(userName, file->cf_username) == 0) {
|
|
|
|
CronLine **pline = &file->cf_lines;
|
2010-07-08 07:37:15 +05:30
|
|
|
CronLine *line;
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_has_running = 0;
|
|
|
|
file->cf_deleted = 1;
|
2010-07-08 07:37:15 +05:30
|
|
|
|
|
|
|
while ((line = *pline) != NULL) {
|
2010-07-08 19:07:10 +05:30
|
|
|
if (line->cl_pid > 0) {
|
|
|
|
file->cf_has_running = 1;
|
|
|
|
pline = &line->cl_next;
|
2010-07-08 07:37:15 +05:30
|
|
|
} else {
|
2010-07-08 19:07:10 +05:30
|
|
|
*pline = line->cl_next;
|
|
|
|
free(line->cl_cmd);
|
2010-07-08 07:37:15 +05:30
|
|
|
free(line);
|
|
|
|
}
|
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
if (file->cf_has_running == 0) {
|
|
|
|
*pfile = file->cf_next;
|
|
|
|
free(file->cf_username);
|
2010-07-08 07:37:15 +05:30
|
|
|
free(file);
|
2010-07-08 19:07:10 +05:30
|
|
|
continue;
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
pfile = &file->cf_next;
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
static void load_crontab(const char *fileName)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-07-17 17:29:13 +05:30
|
|
|
struct parser_t *parser;
|
2008-03-13 03:40:25 +05:30
|
|
|
struct stat sbuf;
|
2004-02-22 10:14:21 +05:30
|
|
|
int maxLines;
|
2008-07-17 03:42:18 +05:30
|
|
|
char *tokens[6];
|
2008-04-08 02:32:35 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
char *mailTo = NULL;
|
|
|
|
#endif
|
2014-04-30 21:08:27 +05:30
|
|
|
char *shell = NULL;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
delete_cronfile(fileName);
|
|
|
|
|
|
|
|
if (!getpwnam(fileName)) {
|
2014-04-30 18:19:52 +05:30
|
|
|
log7("ignoring file '%s' (no such user)", fileName);
|
2008-03-13 03:40:25 +05:30
|
|
|
return;
|
2010-07-08 19:07:10 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2008-07-17 17:29:13 +05:30
|
|
|
parser = config_open(fileName);
|
|
|
|
if (!parser)
|
2008-03-13 03:40:25 +05:30
|
|
|
return;
|
|
|
|
|
2008-07-17 03:42:18 +05:30
|
|
|
maxLines = (strcmp(fileName, "root") == 0) ? 65535 : MAXLINES;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
if (fstat(fileno(parser->fp), &sbuf) == 0 && sbuf.st_uid == DAEMON_UID) {
|
2008-03-13 03:40:25 +05:30
|
|
|
CronFile *file = xzalloc(sizeof(CronFile));
|
|
|
|
CronLine **pline;
|
2008-07-17 03:42:18 +05:30
|
|
|
int n;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_username = xstrdup(fileName);
|
|
|
|
pline = &file->cf_lines;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-27 04:38:31 +05:30
|
|
|
while (1) {
|
2008-03-13 03:40:25 +05:30
|
|
|
CronLine *line;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2014-04-30 18:17:28 +05:30
|
|
|
if (!--maxLines) {
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg("user %s: too many lines", fileName);
|
2008-07-27 04:38:31 +05:30
|
|
|
break;
|
2014-04-30 18:17:28 +05:30
|
|
|
}
|
|
|
|
|
2008-07-27 04:38:31 +05:30
|
|
|
n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
|
|
|
|
if (!n)
|
|
|
|
break;
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
log5("user:%s entry:%s", fileName, parser->data);
|
2008-07-17 03:42:18 +05:30
|
|
|
|
2008-04-08 02:32:35 +05:30
|
|
|
/* check if line is setting MAILTO= */
|
2015-03-12 22:18:34 +05:30
|
|
|
if (is_prefixed_with(tokens[0], "MAILTO=")) {
|
2008-04-08 02:32:35 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
free(mailTo);
|
2008-07-17 03:42:18 +05:30
|
|
|
mailTo = (tokens[0][7]) ? xstrdup(&tokens[0][7]) : NULL;
|
2008-04-08 02:32:35 +05:30
|
|
|
#endif /* otherwise just ignore such lines */
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-12 22:18:34 +05:30
|
|
|
if (is_prefixed_with(tokens[0], "SHELL=")) {
|
2014-04-30 21:08:27 +05:30
|
|
|
free(shell);
|
|
|
|
shell = xstrdup(&tokens[0][6]);
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-09 03:30:39 +05:30
|
|
|
//TODO: handle HOME= too? "man crontab" says:
|
|
|
|
//name = value
|
|
|
|
//
|
|
|
|
//where the spaces around the equal-sign (=) are optional, and any subsequent
|
|
|
|
//non-leading spaces in value will be part of the value assigned to name.
|
|
|
|
//The value string may be placed in quotes (single or double, but matching)
|
|
|
|
//to preserve leading or trailing blanks.
|
|
|
|
//
|
|
|
|
//Several environment variables are set up automatically by the cron(8) daemon.
|
|
|
|
//SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd
|
|
|
|
//line of the crontab's owner. HOME and SHELL may be overridden by settings
|
|
|
|
//in the crontab; LOGNAME may not.
|
|
|
|
|
2017-07-09 00:23:11 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
|
|
|
|
if (tokens[0][0] == '@') {
|
|
|
|
/*
|
|
|
|
* "@daily /a/script/to/run PARAM1 PARAM2..."
|
|
|
|
*/
|
|
|
|
typedef struct SpecialEntry {
|
|
|
|
const char *name;
|
|
|
|
const char tokens[8];
|
|
|
|
} SpecialEntry;
|
2020-11-30 17:33:03 +05:30
|
|
|
static const SpecialEntry SpecAry[] ALIGN8 = {
|
2017-07-09 00:23:11 +05:30
|
|
|
/* hour day month weekday */
|
|
|
|
{ "yearly", "0\0" "1\0" "1\0" "*" },
|
|
|
|
{ "annually", "0\0" "1\0" "1\0" "*" },
|
|
|
|
{ "monthly", "0\0" "1\0" "*\0" "*" },
|
|
|
|
{ "weekly", "0\0" "*\0" "*\0" "0" },
|
|
|
|
{ "daily", "0\0" "*\0" "*\0" "*" },
|
|
|
|
{ "midnight", "0\0" "*\0" "*\0" "*" },
|
|
|
|
{ "hourly", "*\0" "*\0" "*\0" "*" },
|
|
|
|
{ "reboot", "" },
|
|
|
|
};
|
|
|
|
const SpecialEntry *e = SpecAry;
|
|
|
|
|
|
|
|
if (n < 2)
|
|
|
|
continue;
|
|
|
|
for (;;) {
|
|
|
|
if (strcmp(e->name, tokens[0] + 1) == 0) {
|
|
|
|
/*
|
|
|
|
* tokens[1] is only the first word of command,
|
2017-07-09 03:38:13 +05:30
|
|
|
* can'r use it.
|
2017-07-09 00:23:11 +05:30
|
|
|
* find the entire command in unmodified string:
|
|
|
|
*/
|
2017-07-09 03:38:13 +05:30
|
|
|
tokens[5] = skip_whitespace(
|
|
|
|
skip_non_whitespace(
|
|
|
|
skip_whitespace(parser->data)));
|
2017-07-09 00:23:11 +05:30
|
|
|
if (e->tokens[0]) {
|
|
|
|
char *et = (char*)e->tokens;
|
|
|
|
/* minute is "0" for all specials */
|
|
|
|
tokens[0] = (char*)"0";
|
|
|
|
tokens[1] = et;
|
|
|
|
tokens[2] = et + 2;
|
|
|
|
tokens[3] = et + 4;
|
|
|
|
tokens[4] = et + 6;
|
|
|
|
}
|
|
|
|
goto got_it;
|
|
|
|
}
|
|
|
|
if (!e->tokens[0])
|
|
|
|
break;
|
|
|
|
e++;
|
|
|
|
}
|
|
|
|
continue; /* bad line (unrecognized '@foo') */
|
|
|
|
}
|
|
|
|
#endif
|
2008-07-17 03:42:18 +05:30
|
|
|
/* check if a minimum of tokens is specified */
|
2008-07-19 14:57:19 +05:30
|
|
|
if (n < 6)
|
2008-07-17 03:42:18 +05:30
|
|
|
continue;
|
2017-07-09 00:23:11 +05:30
|
|
|
IF_FEATURE_CROND_SPECIAL_TIMES(
|
|
|
|
got_it:
|
|
|
|
)
|
2008-07-27 04:38:31 +05:30
|
|
|
*pline = line = xzalloc(sizeof(*line));
|
2017-07-09 00:23:11 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
|
|
|
|
if (tokens[0][0] == '@') { /* "@reboot" line */
|
|
|
|
file->cf_wants_starting = 1;
|
|
|
|
line->cl_pid = START_ME_REBOOT; /* wants to start */
|
|
|
|
/* line->cl_Mins/Hrs/etc stay zero: never match any time */
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* parse date ranges */
|
|
|
|
ParseField(file->cf_username, line->cl_Mins, 60, 0, NULL, tokens[0]);
|
|
|
|
ParseField(file->cf_username, line->cl_Hrs, 24, 0, NULL, tokens[1]);
|
|
|
|
ParseField(file->cf_username, line->cl_Days, 32, 0, NULL, tokens[2]);
|
|
|
|
ParseField(file->cf_username, line->cl_Mons, 12, -1, MonAry, tokens[3]);
|
|
|
|
ParseField(file->cf_username, line->cl_Dow, 7, 0, DowAry, tokens[4]);
|
|
|
|
/*
|
|
|
|
* fix days and dow - if one is not "*" and the other
|
|
|
|
* is "*", the other is set to 0, and vise-versa
|
|
|
|
*/
|
|
|
|
FixDayDow(line);
|
|
|
|
}
|
2008-04-08 02:32:35 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
/* copy mailto (can be NULL) */
|
2010-07-08 19:07:10 +05:30
|
|
|
line->cl_mailto = xstrdup(mailTo);
|
2008-04-08 02:32:35 +05:30
|
|
|
#endif
|
2014-04-30 21:08:27 +05:30
|
|
|
line->cl_shell = xstrdup(shell);
|
2008-03-13 03:40:25 +05:30
|
|
|
/* copy command */
|
2010-07-08 19:07:10 +05:30
|
|
|
line->cl_cmd = xstrdup(tokens[5]);
|
|
|
|
pline = &line->cl_next;
|
2008-07-17 03:42:18 +05:30
|
|
|
//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]);
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
*pline = NULL;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_next = G.cron_files;
|
|
|
|
G.cron_files = file;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-07-17 17:29:13 +05:30
|
|
|
config_close(parser);
|
2014-04-30 21:08:27 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
|
|
|
free(mailTo);
|
|
|
|
#endif
|
|
|
|
free(shell);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
static void process_cron_update_file(void)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
FILE *fi;
|
|
|
|
char buf[256];
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-07-22 04:35:26 +05:30
|
|
|
fi = fopen_for_read(CRONUPDATE);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (fi != NULL) {
|
2008-02-17 19:58:53 +05:30
|
|
|
unlink(CRONUPDATE);
|
2004-02-22 10:14:21 +05:30
|
|
|
while (fgets(buf, sizeof(buf), fi) != NULL) {
|
2008-03-13 03:40:25 +05:30
|
|
|
/* use first word only */
|
2010-07-08 19:07:10 +05:30
|
|
|
skip_non_whitespace(buf)[0] = '\0';
|
|
|
|
load_crontab(buf);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
fclose(fi);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
static void rescan_crontab_dir(void)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
CronFile *file;
|
2010-07-08 19:07:10 +05:30
|
|
|
|
|
|
|
/* Delete all files until we only have ones with running jobs (or none) */
|
2008-03-13 03:40:25 +05:30
|
|
|
again:
|
2010-07-08 19:07:10 +05:30
|
|
|
for (file = G.cron_files; file; file = file->cf_next) {
|
|
|
|
if (!file->cf_deleted) {
|
|
|
|
delete_cronfile(file->cf_username);
|
2008-03-13 03:40:25 +05:30
|
|
|
goto again;
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
/* Remove cron update file */
|
2008-02-17 19:58:53 +05:30
|
|
|
unlink(CRONUPDATE);
|
2010-07-08 19:07:10 +05:30
|
|
|
/* Re-chdir, in case directory was renamed & deleted */
|
2014-04-30 18:19:52 +05:30
|
|
|
xchdir(G.crontab_dir_name);
|
2010-07-08 19:07:10 +05:30
|
|
|
|
|
|
|
/* Scan directory and add associated users */
|
2004-02-22 10:14:21 +05:30
|
|
|
{
|
|
|
|
DIR *dir = opendir(".");
|
|
|
|
struct dirent *den;
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
/* xopendir exists, but "can't open '.'" is not informative */
|
2008-03-13 03:40:25 +05:30
|
|
|
if (!dir)
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg_and_die("can't open '%s'", G.crontab_dir_name);
|
2008-04-08 02:32:35 +05:30
|
|
|
while ((den = readdir(dir)) != NULL) {
|
2008-03-13 03:40:25 +05:30
|
|
|
if (strchr(den->d_name, '.') != NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
load_crontab(den->d_name);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
closedir(dir);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
#if SETENV_LEAKS
|
|
|
|
/* We set environment *before* vfork (because we want to use vfork),
|
|
|
|
* so we cannot use setenv() - repeated calls to setenv() may leak memory!
|
|
|
|
* Using putenv(), and freeing memory after unsetenv() won't leak */
|
|
|
|
static void safe_setenv(char **pvar_val, const char *var, const char *val)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2010-07-08 19:07:10 +05:30
|
|
|
char *var_val = *pvar_val;
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
if (var_val) {
|
|
|
|
bb_unsetenv_and_free(var_val);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
*pvar_val = xasprintf("%s=%s", var, val);
|
|
|
|
putenv(*pvar_val);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
#endif
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2014-04-30 21:08:27 +05:30
|
|
|
static void set_env_vars(struct passwd *pas, const char *shell)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2014-04-30 21:08:27 +05:30
|
|
|
/* POSIX requires crond to set up at least HOME, LOGNAME, PATH, SHELL.
|
|
|
|
* We assume crond inherited suitable PATH.
|
|
|
|
*/
|
2010-07-08 19:07:10 +05:30
|
|
|
#if SETENV_LEAKS
|
2014-04-30 21:08:27 +05:30
|
|
|
safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
|
2010-07-08 19:07:10 +05:30
|
|
|
safe_setenv(&G.env_var_user, "USER", pas->pw_name);
|
|
|
|
safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
|
2014-04-30 21:08:27 +05:30
|
|
|
safe_setenv(&G.env_var_shell, "SHELL", shell);
|
2010-07-08 07:37:15 +05:30
|
|
|
#else
|
2014-04-30 21:08:27 +05:30
|
|
|
xsetenv("LOGNAME", pas->pw_name);
|
2010-07-08 19:07:10 +05:30
|
|
|
xsetenv("USER", pas->pw_name);
|
|
|
|
xsetenv("HOME", pas->pw_dir);
|
2014-04-30 21:08:27 +05:30
|
|
|
xsetenv("SHELL", shell);
|
2010-07-08 07:37:15 +05:30
|
|
|
#endif
|
2010-07-08 19:07:10 +05:30
|
|
|
}
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
static void change_user(struct passwd *pas)
|
2002-10-22 17:54:59 +05:30
|
|
|
{
|
2010-07-08 19:07:10 +05:30
|
|
|
/* careful: we're after vfork! */
|
|
|
|
change_identity(pas); /* - initgroups, setgid, setuid */
|
|
|
|
if (chdir(pas->pw_dir) < 0) {
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg("can't change directory to '%s'", pas->pw_dir);
|
|
|
|
xchdir(CRON_DIR);
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
// TODO: sendmail should be _run-time_ option, not compile-time!
|
2010-07-08 19:07:10 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2010-07-08 19:42:10 +05:30
|
|
|
static pid_t
|
2014-04-30 21:08:27 +05:30
|
|
|
fork_job(const char *user, int mailFd, CronLine *line, bool run_sendmail)
|
|
|
|
{
|
2008-03-13 03:40:25 +05:30
|
|
|
struct passwd *pas;
|
2014-04-30 21:08:27 +05:30
|
|
|
const char *shell, *prog;
|
|
|
|
smallint sv_logmode;
|
2008-03-13 03:40:25 +05:30
|
|
|
pid_t pid;
|
2002-10-22 17:54:59 +05:30
|
|
|
|
2008-03-13 03:40:25 +05:30
|
|
|
/* prepare things before vfork */
|
|
|
|
pas = getpwnam(user);
|
|
|
|
if (!pas) {
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg("can't get uid for %s", user);
|
2008-03-13 03:40:25 +05:30
|
|
|
goto err;
|
|
|
|
}
|
2014-04-30 21:08:27 +05:30
|
|
|
|
2017-07-22 05:17:19 +05:30
|
|
|
shell = line->cl_shell ? line->cl_shell : G.default_shell;
|
2014-04-30 21:08:27 +05:30
|
|
|
prog = run_sendmail ? SENDMAIL : shell;
|
|
|
|
|
|
|
|
set_env_vars(pas, shell);
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
sv_logmode = logmode;
|
2008-03-13 03:40:25 +05:30
|
|
|
pid = vfork();
|
2004-02-22 10:14:21 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
/* CHILD */
|
2014-04-30 18:17:28 +05:30
|
|
|
/* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
|
2010-07-08 19:07:10 +05:30
|
|
|
change_user(pas);
|
2014-04-30 18:19:52 +05:30
|
|
|
log5("child running %s", prog);
|
2004-02-22 10:14:21 +05:30
|
|
|
if (mailFd >= 0) {
|
2014-04-30 21:08:27 +05:30
|
|
|
xmove_fd(mailFd, run_sendmail ? 0 : 1);
|
2008-01-24 07:03:12 +05:30
|
|
|
dup2(1, 2);
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
2008-12-04 19:27:59 +05:30
|
|
|
/* crond 3.0pl1-100 puts tasks in separate process groups */
|
2008-12-03 16:16:12 +05:30
|
|
|
bb_setpgrp();
|
2014-04-30 21:08:27 +05:30
|
|
|
if (!run_sendmail)
|
|
|
|
execlp(prog, prog, "-c", line->cl_cmd, (char *) NULL);
|
|
|
|
else
|
|
|
|
execlp(prog, prog, SENDMAIL_ARGS, (char *) NULL);
|
2014-04-30 18:19:52 +05:30
|
|
|
/*
|
|
|
|
* I want this error message on stderr too,
|
|
|
|
* even if other messages go only to syslog:
|
|
|
|
*/
|
|
|
|
logmode |= LOGMODE_STDIO;
|
|
|
|
bb_error_msg_and_die("can't execute '%s' for user %s", prog, user);
|
2008-01-24 07:03:12 +05:30
|
|
|
}
|
2014-04-30 18:19:52 +05:30
|
|
|
logmode = sv_logmode;
|
2008-01-24 07:03:12 +05:30
|
|
|
|
|
|
|
if (pid < 0) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("vfork");
|
2008-03-13 03:40:25 +05:30
|
|
|
err:
|
2010-07-08 19:42:10 +05:30
|
|
|
pid = 0;
|
|
|
|
} /* else: PARENT, FORK SUCCESS */
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2002-10-22 17:54:59 +05:30
|
|
|
/*
|
2004-02-22 10:14:21 +05:30
|
|
|
* Close the mail file descriptor.. we can't just leave it open in
|
|
|
|
* a structure, closing it later, because we might run out of descriptors
|
2002-10-22 17:54:59 +05:30
|
|
|
*/
|
2004-02-22 10:14:21 +05:30
|
|
|
if (mailFd >= 0) {
|
|
|
|
close(mailFd);
|
|
|
|
}
|
2010-07-08 19:42:10 +05:30
|
|
|
return pid;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
|
|
|
|
2017-07-09 00:23:11 +05:30
|
|
|
static pid_t start_one_job(const char *user, CronLine *line)
|
2003-07-28 13:10:39 +05:30
|
|
|
{
|
2004-02-22 10:14:21 +05:30
|
|
|
char mailFile[128];
|
2008-04-08 02:32:35 +05:30
|
|
|
int mailFd = -1;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
line->cl_pid = 0;
|
2010-07-08 19:42:10 +05:30
|
|
|
line->cl_empty_mail_size = 0;
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
if (line->cl_mailto) {
|
|
|
|
/* Open mail file (owner is root so nobody can screw with it) */
|
2014-04-30 18:17:28 +05:30
|
|
|
snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, getpid());
|
2008-04-08 02:32:35 +05:30
|
|
|
mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2008-04-08 02:32:35 +05:30
|
|
|
if (mailFd >= 0) {
|
2010-07-08 19:07:10 +05:30
|
|
|
fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", line->cl_mailto,
|
|
|
|
line->cl_cmd);
|
|
|
|
line->cl_empty_mail_size = lseek(mailFd, 0, SEEK_CUR);
|
2008-04-08 02:32:35 +05:30
|
|
|
} else {
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg("can't create mail file %s for user %s, "
|
2008-04-08 02:32:35 +05:30
|
|
|
"discarding output", mailFile, user);
|
|
|
|
}
|
2004-02-22 10:14:21 +05:30
|
|
|
}
|
|
|
|
|
2014-04-30 21:08:27 +05:30
|
|
|
line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 0);
|
2010-07-08 19:42:10 +05:30
|
|
|
if (mailFd >= 0) {
|
|
|
|
if (line->cl_pid <= 0) {
|
|
|
|
unlink(mailFile);
|
|
|
|
} else {
|
|
|
|
/* rename mail-file based on pid of process */
|
2014-04-30 18:17:28 +05:30
|
|
|
char *mailFile2 = xasprintf("%s/cron.%s.%d", CRON_DIR, user, (int)line->cl_pid);
|
2010-07-08 19:42:10 +05:30
|
|
|
rename(mailFile, mailFile2); // TODO: xrename?
|
|
|
|
free(mailFile2);
|
|
|
|
}
|
|
|
|
}
|
2017-07-09 00:23:11 +05:30
|
|
|
|
|
|
|
return line->cl_pid;
|
2010-07-08 19:07:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* process_finished_job - called when job terminates and when mail terminates
|
|
|
|
*/
|
|
|
|
static void process_finished_job(const char *user, CronLine *line)
|
|
|
|
{
|
|
|
|
pid_t pid;
|
|
|
|
int mailFd;
|
|
|
|
char mailFile[128];
|
|
|
|
struct stat sbuf;
|
|
|
|
|
|
|
|
pid = line->cl_pid;
|
|
|
|
line->cl_pid = 0;
|
|
|
|
if (pid <= 0) {
|
|
|
|
/* No job */
|
|
|
|
return;
|
|
|
|
}
|
2010-07-08 19:42:10 +05:30
|
|
|
if (line->cl_empty_mail_size <= 0) {
|
2010-07-08 19:07:10 +05:30
|
|
|
/* End of job and no mail file, or end of sendmail job */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* End of primary job - check for mail file.
|
|
|
|
* If size has changed and the file is still valid, we send it.
|
|
|
|
*/
|
2014-04-30 18:17:28 +05:30
|
|
|
snprintf(mailFile, sizeof(mailFile), "%s/cron.%s.%d", CRON_DIR, user, (int)pid);
|
2010-07-08 19:07:10 +05:30
|
|
|
mailFd = open(mailFile, O_RDONLY);
|
|
|
|
unlink(mailFile);
|
|
|
|
if (mailFd < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fstat(mailFd, &sbuf) < 0
|
|
|
|
|| sbuf.st_uid != DAEMON_UID
|
|
|
|
|| sbuf.st_nlink != 0
|
|
|
|
|| sbuf.st_size == line->cl_empty_mail_size
|
|
|
|
|| !S_ISREG(sbuf.st_mode)
|
|
|
|
) {
|
|
|
|
close(mailFd);
|
|
|
|
return;
|
|
|
|
}
|
2010-07-08 19:42:10 +05:30
|
|
|
line->cl_empty_mail_size = 0;
|
|
|
|
/* if (line->cl_mailto) - always true if cl_empty_mail_size was nonzero */
|
2014-04-30 21:08:27 +05:30
|
|
|
line->cl_pid = fork_job(user, mailFd, line, /*sendmail?*/ 1);
|
2003-07-28 13:10:39 +05:30
|
|
|
}
|
|
|
|
|
2010-07-08 07:37:15 +05:30
|
|
|
#else /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2017-07-09 00:23:11 +05:30
|
|
|
static pid_t start_one_job(const char *user, CronLine *line)
|
2004-02-22 10:14:21 +05:30
|
|
|
{
|
2014-04-30 21:08:27 +05:30
|
|
|
const char *shell;
|
2008-03-13 03:40:25 +05:30
|
|
|
struct passwd *pas;
|
|
|
|
pid_t pid;
|
|
|
|
|
|
|
|
pas = getpwnam(user);
|
|
|
|
if (!pas) {
|
2014-04-30 18:19:52 +05:30
|
|
|
bb_error_msg("can't get uid for %s", user);
|
2008-03-13 03:40:25 +05:30
|
|
|
goto err;
|
|
|
|
}
|
2004-02-22 10:14:21 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
/* Prepare things before vfork */
|
2017-07-22 05:17:19 +05:30
|
|
|
shell = line->cl_shell ? line->cl_shell : G.default_shell;
|
2014-04-30 21:08:27 +05:30
|
|
|
set_env_vars(pas, shell);
|
2010-07-08 19:07:10 +05:30
|
|
|
|
|
|
|
/* Fork as the user in question and run program */
|
2008-03-13 03:40:25 +05:30
|
|
|
pid = vfork();
|
2004-02-22 10:14:21 +05:30
|
|
|
if (pid == 0) {
|
|
|
|
/* CHILD */
|
2014-04-30 18:17:28 +05:30
|
|
|
/* initgroups, setgid, setuid, and chdir to home or CRON_DIR */
|
2010-07-08 19:07:10 +05:30
|
|
|
change_user(pas);
|
2014-04-30 21:08:27 +05:30
|
|
|
log5("child running %s", shell);
|
2008-12-04 19:27:59 +05:30
|
|
|
/* crond 3.0pl1-100 puts tasks in separate process groups */
|
2008-12-03 16:16:12 +05:30
|
|
|
bb_setpgrp();
|
2014-04-30 21:08:27 +05:30
|
|
|
execl(shell, shell, "-c", line->cl_cmd, (char *) NULL);
|
|
|
|
bb_error_msg_and_die("can't execute '%s' for user %s", shell, user);
|
2008-03-13 03:40:25 +05:30
|
|
|
}
|
|
|
|
if (pid < 0) {
|
libbb: reduce the overhead of single parameter bb_error_msg() calls
Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by
Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower
overhead call to bb_perror_msg() when only a string was being printed
with no parameters. This saves space for some CPU architectures because
it avoids the overhead of a call to a variadic function. However there
has never been a simple version of bb_error_msg(), and since 2007 many
new calls to bb_perror_msg() have been added that only take a single
parameter and so could have been using bb_simple_perror_message().
This changeset introduces 'simple' versions of bb_info_msg(),
bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and
bb_herror_msg_and_die(), and replaces all calls that only take a
single parameter, or use something like ("%s", arg), with calls to the
corresponding 'simple' version.
Since it is likely that single parameter calls to the variadic functions
may be accidentally reintroduced in the future a new debugging config
option WARN_SIMPLE_MSG has been introduced. This uses some macro magic
which will cause any such calls to generate a warning, but this is
turned off by default to avoid use of the unpleasant macros in normal
circumstances.
This is a large changeset due to the number of calls that have been
replaced. The only files that contain changes other than simple
substitution of function calls are libbb.h, libbb/herror_msg.c,
libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c,
networking/udhcp/common.h and util-linux/mdev.c additonal macros have
been added for logging so that single parameter and multiple parameter
logging variants exist.
The amount of space saved varies considerably by architecture, and was
found to be as follows (for 'defconfig' using GCC 7.4):
Arm: -92 bytes
MIPS: -52 bytes
PPC: -1836 bytes
x86_64: -938 bytes
Note that for the MIPS architecture only an exception had to be made
disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h)
because it made these files larger on MIPS.
Signed-off-by: James Byrne <james.byrne@origamienergy.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
|
|
|
bb_simple_perror_msg("vfork");
|
2008-03-13 03:40:25 +05:30
|
|
|
err:
|
2004-02-22 10:14:21 +05:30
|
|
|
pid = 0;
|
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
line->cl_pid = pid;
|
2017-07-09 00:23:11 +05:30
|
|
|
return pid;
|
2002-10-22 17:54:59 +05:30
|
|
|
}
|
2008-03-13 03:40:25 +05:30
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
#define process_finished_job(user, line) ((line)->cl_pid = 0)
|
|
|
|
|
2010-07-08 07:37:15 +05:30
|
|
|
#endif /* !ENABLE_FEATURE_CROND_CALL_SENDMAIL */
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
/*
|
|
|
|
* Determine which jobs need to be run. Under normal conditions, the
|
|
|
|
* period is about a minute (one scan). Worst case it will be one
|
|
|
|
* hour (60 scans).
|
|
|
|
*/
|
|
|
|
static void flag_starting_jobs(time_t t1, time_t t2)
|
|
|
|
{
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
/* Find jobs > t1 and <= t2 */
|
|
|
|
|
|
|
|
for (t = t1 - t1 % 60; t <= t2; t += 60) {
|
|
|
|
struct tm *ptm;
|
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
|
|
|
|
|
|
|
if (t <= t1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ptm = localtime(&t);
|
|
|
|
for (file = G.cron_files; file; file = file->cf_next) {
|
2014-04-30 18:19:52 +05:30
|
|
|
log5("file %s:", file->cf_username);
|
2010-07-08 19:07:10 +05:30
|
|
|
if (file->cf_deleted)
|
|
|
|
continue;
|
|
|
|
for (line = file->cf_lines; line; line = line->cl_next) {
|
2014-04-30 18:19:52 +05:30
|
|
|
log5(" line %s", line->cl_cmd);
|
2010-07-08 19:07:10 +05:30
|
|
|
if (line->cl_Mins[ptm->tm_min]
|
|
|
|
&& line->cl_Hrs[ptm->tm_hour]
|
|
|
|
&& (line->cl_Days[ptm->tm_mday] || line->cl_Dow[ptm->tm_wday])
|
|
|
|
&& line->cl_Mons[ptm->tm_mon]
|
|
|
|
) {
|
2014-04-30 18:19:52 +05:30
|
|
|
log5(" job: %d %s",
|
2010-07-08 19:07:10 +05:30
|
|
|
(int)line->cl_pid, line->cl_cmd);
|
|
|
|
if (line->cl_pid > 0) {
|
2014-04-30 18:19:52 +05:30
|
|
|
log8("user %s: process already running: %s",
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_username, line->cl_cmd);
|
|
|
|
} else if (line->cl_pid == 0) {
|
2017-07-09 00:23:11 +05:30
|
|
|
line->cl_pid = START_ME_NORMAL;
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_wants_starting = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 00:23:11 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
|
|
|
|
static int touch_reboot_file(void)
|
|
|
|
{
|
|
|
|
int fd = open(CRON_REBOOT, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC, 0000);
|
|
|
|
if (fd >= 0) {
|
|
|
|
close(fd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
/* File (presumably) exists - this is not the first run after reboot */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void start_jobs(int wants_start)
|
2010-07-08 07:37:15 +05:30
|
|
|
{
|
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
for (file = G.cron_files; file; file = file->cf_next) {
|
|
|
|
if (!file->cf_wants_starting)
|
2010-07-08 07:37:15 +05:30
|
|
|
continue;
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_wants_starting = 0;
|
|
|
|
for (line = file->cf_lines; line; line = line->cl_next) {
|
|
|
|
pid_t pid;
|
2017-07-09 00:23:11 +05:30
|
|
|
if (line->cl_pid != wants_start)
|
2010-07-08 07:37:15 +05:30
|
|
|
continue;
|
|
|
|
|
2017-07-09 00:23:11 +05:30
|
|
|
pid = start_one_job(file->cf_username, line);
|
2014-04-30 18:19:52 +05:30
|
|
|
log8("USER %s pid %3d cmd %s",
|
2010-07-08 19:07:10 +05:30
|
|
|
file->cf_username, (int)pid, line->cl_cmd);
|
|
|
|
if (pid < 0) {
|
|
|
|
file->cf_wants_starting = 1;
|
|
|
|
}
|
|
|
|
if (pid > 0) {
|
|
|
|
file->cf_has_running = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for job completion, return number of jobs still running after
|
|
|
|
* all done.
|
|
|
|
*/
|
|
|
|
static int check_completions(void)
|
|
|
|
{
|
|
|
|
CronFile *file;
|
|
|
|
CronLine *line;
|
|
|
|
int num_still_running = 0;
|
|
|
|
|
|
|
|
for (file = G.cron_files; file; file = file->cf_next) {
|
|
|
|
if (!file->cf_has_running)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
file->cf_has_running = 0;
|
|
|
|
for (line = file->cf_lines; line; line = line->cl_next) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (line->cl_pid <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = waitpid(line->cl_pid, NULL, WNOHANG);
|
|
|
|
if (r < 0 || r == line->cl_pid) {
|
|
|
|
process_finished_job(file->cf_username, line);
|
|
|
|
if (line->cl_pid == 0) {
|
|
|
|
/* sendmail was not started for it */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* else: sendmail was started, job is still running, fall thru */
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
/* else: r == 0: "process is still running" */
|
|
|
|
file->cf_has_running = 1;
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
//FIXME: if !file->cf_has_running && file->deleted: delete it!
|
|
|
|
//otherwise deleted entries will stay forever, right?
|
|
|
|
num_still_running += file->cf_has_running;
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
return num_still_running;
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
static void reopen_logfile_to_stderr(void)
|
|
|
|
{
|
|
|
|
if (G.log_filename) {
|
|
|
|
int logfd = open_or_warn(G.log_filename, O_WRONLY | O_CREAT | O_APPEND);
|
|
|
|
if (logfd >= 0)
|
|
|
|
xmove_fd(logfd, STDERR_FILENO);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-08 07:37:15 +05:30
|
|
|
int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
|
|
|
int crond_main(int argc UNUSED_PARAM, char **argv)
|
|
|
|
{
|
|
|
|
time_t t2;
|
2014-04-30 18:17:28 +05:30
|
|
|
unsigned rescan;
|
|
|
|
unsigned sleep_time;
|
2010-07-08 07:37:15 +05:30
|
|
|
unsigned opts;
|
|
|
|
|
|
|
|
INIT_G();
|
|
|
|
|
2017-08-09 01:25:02 +05:30
|
|
|
opts = getopt32(argv, "^"
|
|
|
|
"l:L:fbSc:" IF_FEATURE_CROND_D("d:")
|
|
|
|
"\0"
|
|
|
|
/* "-b after -f is ignored", and so on for every pair a-b */
|
|
|
|
"f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
|
2011-09-07 09:26:09 +05:30
|
|
|
/* -l and -d have numeric param */
|
2017-08-09 01:25:02 +05:30
|
|
|
":l+" IF_FEATURE_CROND_D(":d+")
|
|
|
|
,
|
2010-07-08 19:07:10 +05:30
|
|
|
&G.log_level, &G.log_filename, &G.crontab_dir_name
|
2017-08-09 01:25:02 +05:30
|
|
|
IF_FEATURE_CROND_D(,&G.log_level)
|
|
|
|
);
|
2010-07-08 19:07:10 +05:30
|
|
|
/* both -d N and -l N set the same variable: G.log_level */
|
2010-07-08 07:37:15 +05:30
|
|
|
|
|
|
|
if (!(opts & OPT_f)) {
|
|
|
|
/* close stdin, stdout, stderr.
|
|
|
|
* close unused descriptors - don't need them. */
|
|
|
|
bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
|
|
|
|
}
|
|
|
|
|
2010-07-08 19:07:10 +05:30
|
|
|
if (!(opts & OPT_d) && G.log_filename == NULL) {
|
2010-07-08 07:37:15 +05:30
|
|
|
/* logging to syslog */
|
|
|
|
openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
|
|
|
|
logmode = LOGMODE_SYSLOG;
|
|
|
|
}
|
|
|
|
|
2014-04-30 21:08:27 +05:30
|
|
|
//signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
|
2014-04-30 18:19:52 +05:30
|
|
|
|
2014-04-30 21:08:27 +05:30
|
|
|
reopen_logfile_to_stderr();
|
2010-07-08 19:07:10 +05:30
|
|
|
xchdir(G.crontab_dir_name);
|
2017-07-22 05:17:19 +05:30
|
|
|
/* $SHELL, or current UID's shell, or DEFAULT_SHELL */
|
|
|
|
/* Useful on Android where DEFAULT_SHELL /bin/sh may not exist */
|
|
|
|
G.default_shell = xstrdup(get_shell_name());
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
log8("crond (busybox "BB_VER") started, log level %d", G.log_level);
|
2010-07-08 19:07:10 +05:30
|
|
|
rescan_crontab_dir();
|
2019-03-18 00:17:52 +05:30
|
|
|
write_pidfile_std_path_and_ext("crond");
|
2017-07-09 00:23:11 +05:30
|
|
|
#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
|
|
|
|
if (touch_reboot_file())
|
|
|
|
start_jobs(START_ME_REBOOT); /* start @reboot entries, if any */
|
|
|
|
#endif
|
2010-07-08 07:37:15 +05:30
|
|
|
|
2010-07-08 07:37:54 +05:30
|
|
|
/* Main loop */
|
2010-07-08 07:37:15 +05:30
|
|
|
t2 = time(NULL);
|
|
|
|
rescan = 60;
|
|
|
|
sleep_time = 60;
|
|
|
|
for (;;) {
|
2010-07-08 07:37:54 +05:30
|
|
|
struct stat sbuf;
|
2010-07-08 07:37:15 +05:30
|
|
|
time_t t1;
|
|
|
|
long dt;
|
|
|
|
|
2010-07-08 07:37:54 +05:30
|
|
|
/* Synchronize to 1 minute, minimum 1 second */
|
2014-04-30 18:19:52 +05:30
|
|
|
t1 = t2;
|
|
|
|
sleep(sleep_time - (time(NULL) % sleep_time));
|
2010-07-08 07:37:15 +05:30
|
|
|
t2 = time(NULL);
|
|
|
|
dt = (long)t2 - (long)t1;
|
|
|
|
|
2014-04-30 18:19:52 +05:30
|
|
|
reopen_logfile_to_stderr();
|
|
|
|
|
2010-07-08 07:37:15 +05:30
|
|
|
/*
|
|
|
|
* The file 'cron.update' is checked to determine new cron
|
|
|
|
* jobs. The directory is rescanned once an hour to deal
|
|
|
|
* with any screwups.
|
|
|
|
*
|
|
|
|
* Check for time jump. Disparities over an hour either way
|
|
|
|
* result in resynchronization. A negative disparity
|
|
|
|
* less than an hour causes us to effectively sleep until we
|
|
|
|
* match the original time (i.e. no re-execution of jobs that
|
|
|
|
* have just been run). A positive disparity less than
|
|
|
|
* an hour causes intermediate jobs to be run, but only once
|
|
|
|
* in the worst case.
|
|
|
|
*
|
|
|
|
* When running jobs, the inequality used is greater but not
|
|
|
|
* equal to t1, and less then or equal to t2.
|
|
|
|
*/
|
2010-07-08 19:07:10 +05:30
|
|
|
if (stat(G.crontab_dir_name, &sbuf) != 0)
|
|
|
|
sbuf.st_mtime = 0; /* force update (once) if dir was deleted */
|
|
|
|
if (G.crontab_dir_mtime != sbuf.st_mtime) {
|
|
|
|
G.crontab_dir_mtime = sbuf.st_mtime;
|
2010-07-08 07:37:54 +05:30
|
|
|
rescan = 1;
|
|
|
|
}
|
2010-07-08 07:37:15 +05:30
|
|
|
if (--rescan == 0) {
|
|
|
|
rescan = 60;
|
2010-07-08 19:07:10 +05:30
|
|
|
rescan_crontab_dir();
|
2010-07-08 07:37:15 +05:30
|
|
|
}
|
2010-07-08 19:07:10 +05:30
|
|
|
process_cron_update_file();
|
2014-04-30 18:19:52 +05:30
|
|
|
log5("wakeup dt=%ld", dt);
|
2010-07-08 07:37:15 +05:30
|
|
|
if (dt < -60 * 60 || dt > 60 * 60) {
|
2019-04-12 22:31:51 +05:30
|
|
|
bb_info_msg("time disparity of %ld minutes detected", dt / 60);
|
2010-07-08 07:37:15 +05:30
|
|
|
/* and we do not run any jobs in this case */
|
|
|
|
} else if (dt > 0) {
|
2010-07-08 07:37:54 +05:30
|
|
|
/* Usual case: time advances forward, as expected */
|
2010-07-08 19:07:10 +05:30
|
|
|
flag_starting_jobs(t1, t2);
|
2017-07-09 00:23:11 +05:30
|
|
|
start_jobs(START_ME_NORMAL);
|
2014-04-30 18:17:28 +05:30
|
|
|
sleep_time = 60;
|
2010-07-08 19:07:10 +05:30
|
|
|
if (check_completions() > 0) {
|
|
|
|
/* some jobs are still running */
|
2010-07-08 07:37:15 +05:30
|
|
|
sleep_time = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* else: time jumped back, do not run any jobs */
|
|
|
|
} /* for (;;) */
|
|
|
|
|
|
|
|
return 0; /* not reached */
|
|
|
|
}
|