more: stop using bss
[oweals/busybox.git] / miscutils / crond.c
index aace3ee18b55f99156012162ee872aeaaca26f10..a490d417fb9ca477fd6a4dd62b9656b7c17f7ac2 100644 (file)
@@ -12,7 +12,7 @@
 
 #define VERSION "2.3.2"
 
-#include "busybox.h"
+#include "libbb.h"
 #include <sys/syslog.h>
 
 #define arysize(ary)    (sizeof(ary)/sizeof((ary)[0]))
@@ -114,14 +114,14 @@ static void crondlog(const char *ctl, ...)
                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
                        }
                }
        }
@@ -131,79 +131,48 @@ static void crondlog(const char *ctl, ...)
        }
 }
 
+int crond_main(int ac, char **av);
 int crond_main(int ac, char **av)
 {
        unsigned opt;
        char *lopt, *Lopt, *copt;
+       USE_DEBUG_CROND_OPTION(char *dopt;)
 
-#if ENABLE_DEBUG_CROND_OPTION
-       char *dopt;
-
-       opt_complementary = "f-b:b-f:S-L:L-S:d-l";
-#else
-       opt_complementary = "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 = getopt32(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) {
+       opt = getopt32(ac, 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) {
-               if (*Lopt != 0) {
+       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) {
+       if (opt & 64) { /* -d */
                DebugOpt = xatou(dopt);
                LogLevel = 0;
        }
 #endif
 
-       /*
-        * change directory
-        */
-
-       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
-               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",
                         applet_name, LogLevel);
 
@@ -216,6 +185,7 @@ int crond_main(int ac, char **av)
                int rescan = 60;
                short sleep_time = 60;
 
+               write_pidfile("/var/run/crond.pid");
                for (;;) {
                        sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
 
@@ -311,10 +281,9 @@ static void startlogger(void)
        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': ", LogFile);
                }
        }
 #endif