~vcs-imports/busybox/trunk

« back to all changes in this revision

Viewing changes to miscutils/crond.c

  • Committer: Denys Vlasenko
  • Author(s): Christian Franke
  • Date: 2023-11-13 10:32:35 UTC
  • Revision ID: git-v1:a63b60bdd6fa26b867c80d44074118babbae7ffd
Cygwin: regenerate defconfig

Signed-off-by: Christian Franke <christian.franke@t-online.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10
10
 */
11
11
//config:config CROND
12
 
//config:       bool "crond (14 kb)"
 
12
//config:       bool "crond (15 kb)"
13
13
//config:       default y
14
14
//config:       select FEATURE_SYSLOG
15
15
//config:       help
63
63
//kbuild:lib-$(CONFIG_CROND) += crond.o
64
64
 
65
65
//usage:#define crond_trivial_usage
66
 
//usage:       "-fbS -l N " IF_FEATURE_CROND_D("-d N ") "-L LOGFILE -c DIR"
 
66
//usage:       "[-fbS] [-l N] " IF_FEATURE_CROND_D("[-d N] ") "[-L LOGFILE] [-c DIR]"
67
67
//usage:#define crond_full_usage "\n\n"
68
68
//usage:       "        -f      Foreground"
69
69
//usage:     "\n        -b      Background (default)"
125
125
        char *cl_mailto;                /* whom to mail results, may be NULL */
126
126
#endif
127
127
        char *cl_shell;
 
128
        char *cl_path;
128
129
        /* ordered by size, not in natural order. makes code smaller: */
129
130
        char cl_Dow[7];                 /* 0-6, beginning sunday */
130
131
        char cl_Mons[12];               /* 0-11 */
421
422
        char *mailTo = NULL;
422
423
#endif
423
424
        char *shell = NULL;
 
425
        char *path = NULL;
424
426
 
425
427
        delete_cronfile(fileName);
426
428
 
470
472
                                shell = xstrdup(&tokens[0][6]);
471
473
                                continue;
472
474
                        }
473
 
//TODO: handle HOME= too? "man crontab" says:
 
475
                        if (is_prefixed_with(tokens[0], "PATH=")) {
 
476
                                free(path);
 
477
                                path = xstrdup(&tokens[0][5]);
 
478
                                continue;
 
479
                        }
 
480
//TODO: handle HOME= too? Better yet, handle arbitrary ENVVARs? "man crontab" says:
474
481
//name = value
475
482
//
476
483
//where the spaces around the equal-sign (=) are optional, and any subsequent
480
487
//
481
488
//Several environment variables are set up automatically by the cron(8) daemon.
482
489
//SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd
483
 
//line of the crontab's owner. HOME and SHELL may be overridden by settings
484
 
//in the crontab; LOGNAME may not.
 
490
//line of the crontab's owner.  HOME, SHELL, and PATH may be overridden by
 
491
//settings in the crontab; LOGNAME may not.
485
492
 
486
493
#if ENABLE_FEATURE_CROND_SPECIAL_TIMES
487
494
                        if (tokens[0][0] == '@') {
492
499
                                        const char *name;
493
500
                                        const char tokens[8];
494
501
                                } SpecialEntry;
495
 
                                static const SpecialEntry SpecAry[] = {
 
502
                                static const SpecialEntry SpecAry[] ALIGN8 = {
496
503
                                        /*              hour  day   month weekday */
497
504
                                        { "yearly",     "0\0" "1\0" "1\0" "*" },
498
505
                                        { "annually",   "0\0" "1\0" "1\0" "*" },
567
574
                        line->cl_mailto = xstrdup(mailTo);
568
575
#endif
569
576
                        line->cl_shell = xstrdup(shell);
 
577
                        line->cl_path = xstrdup(path);
570
578
                        /* copy command */
571
579
                        line->cl_cmd = xstrdup(tokens[5]);
572
580
                        pline = &line->cl_next;
653
661
}
654
662
#endif
655
663
 
656
 
static void set_env_vars(struct passwd *pas, const char *shell)
 
664
static void set_env_vars(struct passwd *pas, const char *shell, const char *path)
657
665
{
658
666
        /* POSIX requires crond to set up at least HOME, LOGNAME, PATH, SHELL.
659
 
         * We assume crond inherited suitable PATH.
660
667
         */
661
668
#if SETENV_LEAKS
662
669
        safe_setenv(&G.env_var_logname, "LOGNAME", pas->pw_name);
663
670
        safe_setenv(&G.env_var_user, "USER", pas->pw_name);
664
671
        safe_setenv(&G.env_var_home, "HOME", pas->pw_dir);
665
672
        safe_setenv(&G.env_var_shell, "SHELL", shell);
 
673
        if (path) safe_setenv(&G.env_var_shell, "PATH", path);
666
674
#else
667
675
        xsetenv("LOGNAME", pas->pw_name);
668
676
        xsetenv("USER", pas->pw_name);
669
677
        xsetenv("HOME", pas->pw_dir);
670
678
        xsetenv("SHELL", shell);
 
679
        if (path) xsetenv("PATH", path);
671
680
#endif
672
681
}
673
682
 
675
684
{
676
685
        /* careful: we're after vfork! */
677
686
        change_identity(pas); /* - initgroups, setgid, setuid */
678
 
        if (chdir(pas->pw_dir) < 0) {
679
 
                bb_error_msg("can't change directory to '%s'", pas->pw_dir);
 
687
        if (chdir_or_warn(pas->pw_dir) != 0) {
680
688
                xchdir(CRON_DIR);
681
689
        }
682
690
}
702
710
        shell = line->cl_shell ? line->cl_shell : G.default_shell;
703
711
        prog = run_sendmail ? SENDMAIL : shell;
704
712
 
705
 
        set_env_vars(pas, shell);
 
713
        set_env_vars(pas, shell, NULL); /* don't use crontab's PATH for sendmail */
706
714
 
707
715
        sv_logmode = logmode;
708
716
        pid = vfork();
846
854
 
847
855
        /* Prepare things before vfork */
848
856
        shell = line->cl_shell ? line->cl_shell : G.default_shell;
849
 
        set_env_vars(pas, shell);
 
857
        set_env_vars(pas, shell, line->cl_path);
850
858
 
851
859
        /* Fork as the user in question and run program */
852
860
        pid = vfork();