Header cleanup on two more networking files (move libbb.h to the top and
[oweals/busybox.git] / miscutils / crond.c
index 4d479bc0d3ba00197dbf0dae73292e7245720279..3f60468de48e251aff73226c152ec586de1d3732 100644 (file)
@@ -5,16 +5,14 @@
  * 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 "busybox.h"
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -32,8 +30,6 @@
 #include <sys/stat.h>
 #include <sys/resource.h>
 
-#include "busybox.h"
-
 #define arysize(ary)    (sizeof(ary)/sizeof((ary)[0]))
 
 #ifndef CRONTABS
@@ -83,7 +79,7 @@ typedef struct CronLine {
 
 #define DaemonUid 0
 
-#ifdef FEATURE_DEBUG_OPT
+#if ENABLE_DEBUG_CROND_OPTION
 static short DebugOpt;
 #endif
 
@@ -101,7 +97,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
@@ -125,7 +121,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
@@ -137,7 +133,7 @@ static void crondlog(const char *ctl, ...)
                        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
@@ -155,7 +151,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";
@@ -165,11 +161,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
                );
@@ -186,7 +182,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;
@@ -208,12 +204,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 */
+#else
                bb_xdaemon(1, 0);
-#endif                                                 /* uClinux */
+#endif
        }
 
        (void) startlogger();   /* need if syslog mode selected */
@@ -263,7 +259,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
@@ -283,24 +279,9 @@ int crond_main(int ac, char **av)
                        }
                }
        }
-       /* not reached */
+       bb_fflush_stdout_and_exit(EXIT_SUCCESS); /* 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);
-}
-#endif
-
-
 static int ChangeUser(const char *user)
 {
        struct passwd *pas;
@@ -341,7 +322,7 @@ 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;
 
@@ -513,7 +494,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;
 
@@ -595,7 +576,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);
                                        }
@@ -626,7 +607,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);
                                        }
@@ -778,21 +759,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);
@@ -881,7 +862,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)
@@ -898,7 +879,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);
                }
@@ -1030,7 +1011,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);
                }
@@ -1047,4 +1028,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 */