* run as root, but NOT setuid root
*
* Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
+ * (version 2.3.2)
* Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
*
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
*/
-#define VERSION "2.3.2"
-
-#include "busybox.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <errno.h>
-#include <time.h>
-#include <dirent.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <syslog.h>
-#include <signal.h>
-#include <getopt.h>
-#include <sys/ioctl.h>
-#include <sys/wait.h>
-#include <sys/stat.h>
-#include <sys/resource.h>
-
-#define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
+#include <sys/syslog.h>
+#include "libbb.h"
#ifndef CRONTABS
#define CRONTABS "/var/spool/cron/crontabs"
#define TMPDIR "/var/spool/cron"
#endif
#ifndef SENDMAIL
-#define SENDMAIL "/usr/sbin/sendmail"
+#define SENDMAIL "sendmail"
#endif
#ifndef SENDMAIL_ARGS
#define SENDMAIL_ARGS "-ti", "oem"
#define DaemonUid 0
#if ENABLE_DEBUG_CROND_OPTION
-static short DebugOpt;
+static unsigned DebugOpt;
#endif
-static short LogLevel = 8;
+static unsigned LogLevel = 8;
static const char *LogFile;
static const char *CDir = CRONTABS;
if (LogFile == 0) {
vsyslog(type, fmt, va);
} else {
+#if !ENABLE_DEBUG_CROND_OPTION
int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
+#else
+ int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
+#endif
if (logfd >= 0) {
vdprintf(logfd, fmt, va);
close(logfd);
-#if ENABLE_DEBUG_CROND_OPTION
- } else {
- bb_perror_msg("Can't open log file");
-#endif
}
}
}
}
}
+int crond_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int crond_main(int ac, char **av)
{
- unsigned long opt;
+ unsigned opt;
char *lopt, *Lopt, *copt;
+ USE_DEBUG_CROND_OPTION(char *dopt;)
-#if ENABLE_DEBUG_CROND_OPTION
- char *dopt;
-
- bb_opt_complementally = "f-b:b-f:S-L:L-S:d-l";
-#else
- bb_opt_complementally = "f-b:b-f:S-L:L-S";
-#endif
-
+ opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l");
opterr = 0; /* disable getopt 'errors' message. */
- opt = bb_getopt_ulflags(ac, av, "l:L:fbSc:"
-#if ENABLE_DEBUG_CROND_OPTION
- "d:"
-#endif
- , &lopt, &Lopt, &copt
-#if ENABLE_DEBUG_CROND_OPTION
- , &dopt
-#endif
- );
- if (opt & 1) {
- LogLevel = atoi(lopt);
- }
- if (opt & 2) {
- if (*Lopt != 0) {
+ opt = getopt32(av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
+ &lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt));
+ if (opt & 1) /* -l */
+ LogLevel = xatou(lopt);
+ if (opt & 2) /* -L */
+ if (*Lopt)
LogFile = Lopt;
- }
- }
- if (opt & 32) {
- if (*copt != 0) {
+ if (opt & 32) /* -c */
+ if (*copt)
CDir = copt;
- }
- }
#if ENABLE_DEBUG_CROND_OPTION
- if (opt & 64) {
- DebugOpt = atoi(dopt);
+ if (opt & 64) { /* -d */
+ DebugOpt = xatou(dopt);
LogLevel = 0;
}
#endif
- /*
- * change directory
- */
-
- bb_xchdir(CDir);
- signal(SIGHUP, SIG_IGN); /* hmm.. but, if kill -HUP original
- * version - his died. ;(
- */
- /*
- * close stdin and stdout, stderr.
+ /* close stdin and stdout, stderr.
* close unused descriptors - don't need.
* optional detach from controlling terminal
*/
+ if (!(opt & 4))
+ bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av);
- if (!(opt & 4)) {
-#ifdef BB_NOMMU
- /* reexec for vfork() do continue parent */
- vfork_daemon_rexec(1, 0, ac, av, "-f");
-#else
- bb_xdaemon(1, 0);
-#endif
- }
+ xchdir(CDir);
+ signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
- (void) startlogger(); /* need if syslog mode selected */
+ startlogger(); /* need if syslog mode selected */
/*
* main loop - synchronize to 1 second after the minute, minimum sleep
* of 1 second.
*/
-
- crondlog("\011%s " VERSION " dillon, started, log level %d\n",
- bb_applet_name, LogLevel);
+ crondlog("\011%s " BB_VER " started, log level %d\n",
+ applet_name, LogLevel);
SynchronizeDir();
int rescan = 60;
short sleep_time = 60;
+ write_pidfile("/var/run/crond.pid");
for (;;) {
sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
}
}
}
- bb_fflush_stdout_and_exit(EXIT_SUCCESS); /* not reached */
+ return 0; /* not reached */
}
static int ChangeUser(const char *user)
pas = getpwnam(user);
if (pas == 0) {
crondlog("\011failed to get uid for %s", user);
- return (-1);
+ return -1;
}
setenv("USER", pas->pw_name, 1);
setenv("HOME", pas->pw_dir, 1);
err_msg = change_identity_e2str(pas);
if (err_msg) {
crondlog("\011%s for user %s", err_msg, user);
- return (-1);
+ return -1;
}
if (chdir(pas->pw_dir) < 0) {
crondlog("\011chdir failed: %s: %m", pas->pw_dir);
if (chdir(TMPDIR) < 0) {
crondlog("\011chdir failed: %s: %m", TMPDIR);
- return (-1);
+ return -1;
}
}
- return (pas->pw_uid);
+ return pas->pw_uid;
}
static void startlogger(void)
{
if (LogFile == 0) {
- openlog(bb_applet_name, LOG_CONS | LOG_PID, LOG_CRON);
+ openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON);
}
#if ENABLE_DEBUG_CROND_OPTION
else { /* test logfile */
int logfd;
- if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600)) >= 0) {
+ logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
+ if (logfd >= 0) {
close(logfd);
- } else {
- bb_perror_msg("Failed to open log file '%s' reason", LogFile);
}
}
#endif
}
-static const char *const DowAry[] = {
- "sun",
- "mon",
- "tue",
- "wed",
- "thu",
- "fri",
- "sat",
-
- "Sun",
- "Mon",
- "Tue",
- "Wed",
- "Thu",
- "Fri",
- "Sat",
- NULL
-};
-
-static const char *const MonAry[] = {
- "jan",
- "feb",
- "mar",
- "apr",
- "may",
- "jun",
- "jul",
- "aug",
- "sep",
- "oct",
- "nov",
- "dec",
-
- "Jan",
- "Feb",
- "Mar",
- "Apr",
- "May",
- "Jun",
- "Jul",
- "Aug",
- "Sep",
- "Oct",
- "Nov",
- "Dec",
- NULL
-};
+static const char DowAry[] ALIGN1 =
+ "sun""mon""tue""wed""thu""fri""sat"
+ /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */
+;
+
+static const char MonAry[] ALIGN1 =
+ "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec"
+ /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */
+;
static char *ParseField(char *user, char *ary, int modvalue, int off,
- const char *const *names, char *ptr)
+ const char *names, char *ptr)
+/* 'names' is a pointer to a set of 3-char abbreviations */
{
char *base = ptr;
int n1 = -1;
int n2 = -1;
if (base == NULL) {
- return (NULL);
+ return NULL;
}
while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
} else if (names) {
int i;
- for (i = 0; names[i]; ++i) {
- if (strncmp(ptr, names[i], strlen(names[i])) == 0) {
+ 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;
break;
}
}
- if (names[i]) {
- ptr += strlen(names[i]);
- if (n1 < 0) {
- n1 = i;
- } else {
- n2 = i;
- }
- skip = 1;
- }
}
/* handle optional range '-' */
if (skip == 0) {
crondlog("\111failed user %s parsing %s\n", user, base);
- return (NULL);
+ return NULL;
}
if (*ptr == '-' && n2 < 0) {
++ptr;
if (failsafe == 0) {
crondlog("\111failed user %s parsing %s\n", user, base);
- return (NULL);
+ return NULL;
}
}
if (*ptr != ',') {
if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
crondlog("\111failed user %s parsing %s\n", user, base);
- return (NULL);
+ return NULL;
}
while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
}
#endif
- return (ptr);
+ return ptr;
}
static void FixDayDow(CronLine * line)
int weekUsed = 0;
int daysUsed = 0;
- for (i = 0; i < (int)(arysize(line->cl_Dow)); ++i) {
+ for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Dow)); ++i) {
if (line->cl_Dow[i] == 0) {
weekUsed = 1;
break;
}
}
- for (i = 0; i < (int)(arysize(line->cl_Days)); ++i) {
+ for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Days)); ++i) {
if (line->cl_Days[i] == 0) {
daysUsed = 1;
break;
struct stat sbuf;
if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
- CronFile *file = calloc(1, sizeof(CronFile));
+ CronFile *file = xzalloc(sizeof(CronFile));
CronLine **pline;
file->cf_User = strdup(fileName);
FixDayDow(&line);
- *pline = calloc(1, sizeof(CronLine));
+ *pline = xzalloc(sizeof(CronLine));
**pline = line;
/* copy command */
remove(CRONUPDATE);
if (chdir(CDir) < 0) {
- crondlog("\311unable to find %s\n", CDir);
+ crondlog("\311cannot find %s\n", CDir);
}
{
DIR *dir = opendir(".");
}
closedir(dir);
} else {
- crondlog("\311Unable to open current dir!\n");
+ crondlog("\311cannot open current dir!\n");
}
}
}
}
}
}
- return (nJobs);
+ return nJobs;
}
static void RunJobs(void)
}
nStillRunning += file->cf_Running;
}
- return (nStillRunning);
+ return nStillRunning;
}
close(mailFd);
}
execl(prog, prog, cmd, arg, NULL);
- crondlog("\024unable to exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
+ crondlog("\024cannot exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
if (mailf) {
fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
}
exit(0);
} else if (pid < 0) {
/* FORK FAILED */
- crondlog("\024couldn't fork, user %s\n", user);
+ crondlog("\024cannot fork, user %s\n", user);
line->cl_Pid = 0;
if (mailf) {
remove(mailf);
line->cl_MailFlag = 1;
fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
line->cl_Shell);
- line->cl_MailPos = lseek(mailFd, 0, 1);
+ line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR);
} else {
- crondlog("\024unable to create mail file user %s file %s, output to /dev/null\n", user, mailFile);
+ crondlog("\024cannot create mail file user %s file %s, output to /dev/null\n", user, mailFile);
}
ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
return;
}
- if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid || sbuf.st_nlink != 0 ||
- sbuf.st_size == line->cl_MailPos || !S_ISREG(sbuf.st_mode)) {
+ if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid
+ || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos
+ || !S_ISREG(sbuf.st_mode)
+ ) {
close(mailFd);
return;
}
#endif
execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
- crondlog("\024unable to exec, user %s cmd %s -c %s\n", user,
+ crondlog("\024cannot exec, user %s cmd %s -c %s\n", user,
DEFAULT_SHELL, line->cl_Shell);
exit(0);
} else if (pid < 0) {
/* FORK FAILED */
- crondlog("\024couldn't fork, user %s\n", user);
+ crondlog("\024cannot, user %s\n", user);
pid = 0;
}
line->cl_Pid = pid;