- s/defined(__uClinux__)/BB_NOMMU/
[oweals/busybox.git] / miscutils / crond.c
index 257632f8fd0fc7a4e548329c43f489ab47b4edf8..954d97965ff2c5bd70336ca87e490d04bed9423f 100644 (file)
@@ -1,19 +1,17 @@
+/* vi: set sw=4 ts=4: */
 /*
  * crond -d[#] -c <crondir> -f -b
  *
  * run as root, but NOT setuid root
  *
  * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
- * May be distributed under the GNU General Public License
+ * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002
  *
- * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 to be used in busybox
+ * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
  */
 
 #define VERSION "2.3.2"
 
-#undef FEATURE_DEBUG_OPT
-
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -82,7 +80,7 @@ typedef struct CronLine {
 
 #define DaemonUid 0
 
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
 static short DebugOpt;
 #endif
 
@@ -100,7 +98,7 @@ static int CheckJobs(void);
 
 static void RunJob(const char *user, CronLine * line);
 
-#ifdef CONFIG_FEATURE_CROND_CALL_SENDMAIL
+#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
 static void EndJob(const char *user, CronLine * line);
 #else
 #define EndJob(user, line)  line->cl_Pid = 0
@@ -124,7 +122,7 @@ static void crondlog(const char *ctl, ...)
        fmt = ctl + 1;
        if (level >= LogLevel) {
 
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                if (DebugOpt) {
                        vfprintf(stderr, fmt, va);
                } else
@@ -132,11 +130,11 @@ static void crondlog(const char *ctl, ...)
                if (LogFile == 0) {
                        vsyslog(type, fmt, va);
                } else {
-                       int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 600);
+                       int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
                        if (logfd >= 0) {
                                vdprintf(logfd, fmt, va);
                                close(logfd);
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                        } else {
                                bb_perror_msg("Can't open log file");
 #endif
@@ -154,7 +152,7 @@ int crond_main(int ac, char **av)
        unsigned long opt;
        char *lopt, *Lopt, *copt;
 
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
        char *dopt;
 
        bb_opt_complementally = "f-b:b-f:S-L:L-S:d-l";
@@ -164,11 +162,11 @@ int crond_main(int ac, char **av)
 
        opterr = 0;                     /* disable getopt 'errors' message. */
        opt = bb_getopt_ulflags(ac, av, "l:L:fbSc:"
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                                        "d:"
 #endif
                                                        , &lopt, &Lopt, &copt
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                                        , &dopt
 #endif
                );
@@ -185,7 +183,7 @@ int crond_main(int ac, char **av)
                        CDir = copt;
                }
        }
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
        if (opt & 64) {
                DebugOpt = atoi(dopt);
                LogLevel = 0;
@@ -196,9 +194,7 @@ int crond_main(int ac, char **av)
         * change directory
         */
 
-       if (chdir(CDir) != 0) {
-               bb_perror_msg_and_die("%s", CDir);
-       }
+       bb_xchdir(CDir);
        signal(SIGHUP, SIG_IGN);        /* hmm.. but, if kill -HUP original
                                                                 * version - his died. ;(
                                                                 */
@@ -209,14 +205,12 @@ int crond_main(int ac, char **av)
         */
 
        if (!(opt & 4)) {
-#if defined(__uClinux__)
+#ifdef BB_NOMMU
                /* reexec for vfork() do continue parent */
                vfork_daemon_rexec(1, 0, ac, av, "-f");
-#else                                                  /* uClinux */
-               if (daemon(1, 0) < 0) {
-                       bb_perror_msg_and_die("daemon");
-               }
-#endif                                                 /* uClinux */
+#else
+               bb_xdaemon(1, 0);
+#endif
        }
 
        (void) startlogger();   /* need if syslog mode selected */
@@ -235,7 +229,7 @@ int crond_main(int ac, char **av)
                time_t t1 = time(NULL);
                time_t t2;
                long dt;
-               short rescan = 60;
+               int rescan = 60;
                short sleep_time = 60;
 
                for (;;) {
@@ -266,7 +260,7 @@ int crond_main(int ac, char **av)
                                SynchronizeDir();
                        }
                        CheckUpdates();
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                        if (DebugOpt)
                                crondlog("\005Wakeup dt=%d\n", dt);
 #endif
@@ -286,23 +280,8 @@ int crond_main(int ac, char **av)
                        }
                }
        }
-       /* not reached */
-}
-
-#if defined(FEATURE_DEBUG_OPT) || defined(CONFIG_FEATURE_CROND_CALL_SENDMAIL)
-/*
-    write to temp file..
-*/
-static void fdprintf(int fd, const char *ctl, ...)
-{
-       va_list va;
-
-       va_start(va, ctl);
-       vdprintf(fd, ctl, va);
-       va_end(va);
+       bb_fflush_stdout_and_exit(EXIT_SUCCESS); /* not reached */
 }
-#endif
-
 
 static int ChangeUser(const char *user)
 {
@@ -344,11 +323,11 @@ static void startlogger(void)
        if (LogFile == 0) {
                openlog(bb_applet_name, LOG_CONS | LOG_PID, LOG_CRON);
        }
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
        else {                          /* test logfile */
                int logfd;
 
-               if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 600)) >= 0) {
+               if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600)) >= 0) {
                        close(logfd);
                } else {
                        bb_perror_msg("Failed to open log file '%s' reason", LogFile);
@@ -516,7 +495,7 @@ static char *ParseField(char *user, char *ary, int modvalue, int off,
        while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
                ++ptr;
        }
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
        if (DebugOpt) {
                int i;
 
@@ -532,17 +511,17 @@ static char *ParseField(char *user, char *ary, int modvalue, int off,
 
 static void FixDayDow(CronLine * line)
 {
-       short i;
-       short weekUsed = 0;
-       short daysUsed = 0;
+       int i;
+       int weekUsed = 0;
+       int daysUsed = 0;
 
-       for (i = 0; i < arysize(line->cl_Dow); ++i) {
+       for (i = 0; i < (int)(arysize(line->cl_Dow)); ++i) {
                if (line->cl_Dow[i] == 0) {
                        weekUsed = 1;
                        break;
                }
        }
-       for (i = 0; i < arysize(line->cl_Days); ++i) {
+       for (i = 0; i < (int)(arysize(line->cl_Days)); ++i) {
                if (line->cl_Days[i] == 0) {
                        daysUsed = 1;
                        break;
@@ -598,7 +577,7 @@ static void SynchronizeFile(const char *fileName)
                                        }
                                        memset(&line, 0, sizeof(line));
 
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                        if (DebugOpt) {
                                                crondlog("\111User %s Entry %s\n", fileName, buf);
                                        }
@@ -629,7 +608,7 @@ static void SynchronizeFile(const char *fileName)
                                        /* copy command */
                                        (*pline)->cl_Shell = strdup(ptr);
 
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                        if (DebugOpt) {
                                                crondlog("\111    Command %s\n", ptr);
                                        }
@@ -769,7 +748,7 @@ static void DeleteFile(const char *userName)
 
 static int TestJobs(time_t t1, time_t t2)
 {
-       short nJobs = 0;
+       int nJobs = 0;
        time_t t;
 
        /* Find jobs > t1 and <= t2 */
@@ -781,21 +760,21 @@ static int TestJobs(time_t t1, time_t t2)
                        CronLine *line;
 
                        for (file = FileBase; file; file = file->cf_Next) {
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                if (DebugOpt)
                                        crondlog("\005FILE %s:\n", file->cf_User);
 #endif
                                if (file->cf_Deleted)
                                        continue;
                                for (line = file->cf_LineBase; line; line = line->cl_Next) {
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                        if (DebugOpt)
                                                crondlog("\005    LINE %s\n", line->cl_Shell);
 #endif
                                        if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
                                                (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
                                                && line->cl_Mons[tp->tm_mon]) {
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                                                if (DebugOpt) {
                                                        crondlog("\005    JobToDo: %d %s\n",
                                                                line->cl_Pid, line->cl_Shell);
@@ -884,7 +863,7 @@ static int CheckJobs(void)
 }
 
 
-#ifdef CONFIG_FEATURE_CROND_CALL_SENDMAIL
+#if ENABLE_FEATURE_CROND_CALL_SENDMAIL
 static void
 ForkJob(const char *user, CronLine * line, int mailFd,
                const char *prog, const char *cmd, const char *arg, const char *mailf)
@@ -901,7 +880,7 @@ ForkJob(const char *user, CronLine * line, int mailFd,
                if (ChangeUser(user) < 0) {
                        exit(0);
                }
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                if (DebugOpt) {
                        crondlog("\005Child Running %s\n", prog);
                }
@@ -1033,7 +1012,7 @@ static void RunJob(const char *user, CronLine * line)
                if (ChangeUser(user) < 0) {
                        exit(0);
                }
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
                if (DebugOpt) {
                        crondlog("\005Child Running %s\n", DEFAULT_SHELL);
                }
@@ -1050,4 +1029,4 @@ static void RunJob(const char *user, CronLine * line)
        }
        line->cl_Pid = pid;
 }
-#endif                                                 /* CONFIG_FEATURE_CROND_CALL_SENDMAIL */
+#endif /* ENABLE_FEATURE_CROND_CALL_SENDMAIL */