ps: conditionally enable -T on non-DESKTOP build too
[oweals/busybox.git] / miscutils / crond.c
index b3a06a376a76c29e48e712d2460e262a25994aff..f0b64fe3aaeb9a298c987c6d7385730443626e79 100644 (file)
 #endif
 
 
-#ifndef CRONTABS
-#define CRONTABS        "/var/spool/cron/crontabs"
-#endif
-#ifndef TMPDIR
-#define TMPDIR          "/var/spool/cron"
-#endif
+#define TMPDIR          CONFIG_FEATURE_CROND_DIR
+#define CRONTABS        CONFIG_FEATURE_CROND_DIR "/crontabs"
 #ifndef SENDMAIL
 #define SENDMAIL        "sendmail"
 #endif
@@ -59,7 +55,7 @@ typedef struct CronLine {
 #if ENABLE_FEATURE_CROND_CALL_SENDMAIL
        int cl_MailPos;         /* 'empty file' size                    */
        smallint cl_MailFlag;   /* running pid is for mail              */
-       char *cl_MailTo;        /* whom to mail results                 */
+       char *cl_MailTo;        /* whom to mail results                 */
 #endif
        /* ordered by size, not in natural order. makes code smaller: */
        char cl_Dow[7];         /* 0-6, beginning sunday                */
@@ -80,9 +76,9 @@ enum {
        OPT_b = (1 << 3),
        OPT_S = (1 << 4),
        OPT_c = (1 << 5),
-       OPT_d = (1 << 6) * ENABLE_DEBUG_CROND_OPTION,
+       OPT_d = (1 << 6) * ENABLE_FEATURE_CROND_D,
 };
-#if ENABLE_DEBUG_CROND_OPTION
+#if ENABLE_FEATURE_CROND_D
 #define DebugOpt (option_mask32 & OPT_d)
 #else
 #define DebugOpt 0
@@ -146,7 +142,7 @@ static void crondlog(const char *ctl, ...)
                /* Syslog mode: all to syslog (logmode = LOGMODE_SYSLOG), */
                if (!DebugOpt && LogFile) {
                        /* Otherwise (log to file): we reopen log file at every write: */
-                       int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600);
+                       int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0666);
                        if (logfd >= 0)
                                xmove_fd(logfd, STDERR_FILENO);
                }
@@ -166,11 +162,11 @@ int crond_main(int argc UNUSED_PARAM, char **argv)
        INIT_G();
 
        /* "-b after -f is ignored", and so on for every pair a-b */
-       opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l")
+       opt_complementary = "f-b:b-f:S-L:L-S" IF_FEATURE_CROND_D(":d-l")
                        ":l+:d+"; /* -l and -d have numeric param */
-       opt = getopt32(argv, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"),
+       opt = getopt32(argv, "l:L:fbSc:" IF_FEATURE_CROND_D("d:"),
                        &LogLevel, &LogFile, &CDir
-                       USE_DEBUG_CROND_OPTION(,&LogLevel));
+                       IF_FEATURE_CROND_D(,&LogLevel));
        /* both -d N and -l N set the same variable: LogLevel */
 
        if (!(opt & OPT_f)) {
@@ -187,7 +183,7 @@ int crond_main(int argc UNUSED_PARAM, char **argv)
 
        xchdir(CDir);
        //signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */
-       setenv("SHELL", DEFAULT_SHELL, 1); /* once, for all future children */
+       xsetenv("SHELL", DEFAULT_SHELL); /* once, for all future children */
        crondlog(LVL9 "crond (busybox "BB_VER") started, log level %d", LogLevel);
        SynchronizeDir();
 
@@ -252,14 +248,12 @@ int crond_main(int argc UNUSED_PARAM, char **argv)
 /* 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_setenv4(char **pvar_val, const char *var, const char *val /*, int len*/)
+static void safe_setenv(char **pvar_val, const char *var, const char *val)
 {
-       const int len = 4; /* both var names are 4 char long */
        char *var_val = *pvar_val;
 
        if (var_val) {
-               var_val[len] = '\0'; /* nuke '=' */
-               unsetenv(var_val);
+               bb_unsetenv(var_val);
                free(var_val);
        }
        *pvar_val = xasprintf("%s=%s", var, val);
@@ -270,13 +264,13 @@ static void safe_setenv4(char **pvar_val, const char *var, const char *val /*, i
 static void SetEnv(struct passwd *pas)
 {
 #if SETENV_LEAKS
-       safe_setenv4(&env_var_user, "USER", pas->pw_name);
-       safe_setenv4(&env_var_home, "HOME", pas->pw_dir);
+       safe_setenv(&env_var_user, "USER", pas->pw_name);
+       safe_setenv(&env_var_home, "HOME", pas->pw_dir);
        /* if we want to set user's shell instead: */
-       /*safe_setenv(env_var_user, "SHELL", pas->pw_shell, 5);*/
+       /*safe_setenv(env_var_user, "SHELL", pas->pw_shell);*/
 #else
-       setenv("USER", pas->pw_name, 1);
-       setenv("HOME", pas->pw_dir, 1);
+       xsetenv("USER", pas->pw_name);
+       xsetenv("HOME", pas->pw_dir);
 #endif
        /* currently, we use constant one: */
        /*setenv("SHELL", DEFAULT_SHELL, 1); - done earlier */
@@ -469,11 +463,15 @@ static void SynchronizeFile(const char *fileName)
                file->cf_User = xstrdup(fileName);
                pline = &file->cf_LineBase;
 
-               while (--maxLines
-                && (n = config_read(parser, tokens, 6, 1, "# \t", PARSE_LAST_IS_GREEDY|PARSE_KEEP_COPY))
-               ) {
+               while (1) {
                        CronLine *line;
 
+                       if (!--maxLines)
+                               break;
+                       n = config_read(parser, tokens, 6, 1, "# \t", PARSE_NORMAL | PARSE_KEEP_COPY);
+                       if (!n)
+                               break;
+
                        if (DebugOpt)
                                crondlog(LVL5 "user:%s entry:%s", fileName, parser->data);
 
@@ -488,7 +486,7 @@ static void SynchronizeFile(const char *fileName)
                        /* check if a minimum of tokens is specified */
                        if (n < 6)
                                continue;
-                       *pline = line = xzalloc(sizeof(CronLine));
+                       *pline = line = xzalloc(sizeof(*line));
                        /* parse date ranges */
                        ParseField(file->cf_User, line->cl_Mins, 60, 0, NULL, tokens[0]);
                        ParseField(file->cf_User, line->cl_Hrs, 24, 0, NULL, tokens[1]);
@@ -775,7 +773,9 @@ ForkJob(const char *user, CronLine *line, int mailFd,
                        xmove_fd(mailFd, mail_filename ? 1 : 0);
                        dup2(1, 2);
                }
-               execlp(prog, prog, cmd, arg, NULL);
+               /* crond 3.0pl1-100 puts tasks in separate process groups */
+               bb_setpgrp();
+               execlp(prog, prog, cmd, arg, (char *) NULL);
                crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user, prog, cmd, arg);
                if (mail_filename) {
                        fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
@@ -910,7 +910,9 @@ static void RunJob(const char *user, CronLine *line)
                if (DebugOpt) {
                        crondlog(LVL5 "child running %s", DEFAULT_SHELL);
                }
-               execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
+               /* crond 3.0pl1-100 puts tasks in separate process groups */
+               bb_setpgrp();
+               execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, (char *) NULL);
                crondlog(ERR20 "can't exec, user %s cmd %s %s %s", user,
                                 DEFAULT_SHELL, "-c", line->cl_Shell);
                _exit(EXIT_SUCCESS);