128112Spst#!/bin/sh -
228112Spst#
350479Speter# $FreeBSD: stable/10/usr.sbin/periodic/periodic.sh 321260 2017-07-20 00:33:12Z ngie $
428112Spst#
528112Spst# Run nightly periodic scripts
628112Spst#
7302600Sasomers# usage: periodic { daily | weekly | monthly | security } - run standard scripts
828112Spst#        periodic /absolute/path/to/directory  - run periodic scripts in dir
928112Spst#
1028112Spst
1128143Spstusage () {
1228112Spst    echo "usage: $0 <directory of files to execute>" 1>&2
13302600Sasomers    echo "or     $0 { daily | weekly | monthly | security }"    1>&2
1428112Spst    exit 1
1528143Spst}
1628143Spst
17231568Sbrooksoutput_pipe()
18231568Sbrooks{
19231568Sbrooks    # Where's our output going ?
20231568Sbrooks    eval output=\$${1##*/}_output
21231568Sbrooks    case "$output" in
22231568Sbrooks    /*) pipe="cat >>$output";;
23231568Sbrooks    "") pipe=cat;;
24255178Sjlh    *)  pipe="mail -E -s '$host ${2}${2:+ }${1##*/} run output' $output";;
25231568Sbrooks    esac
26231568Sbrooks    eval $pipe
27231568Sbrooks}
28231568Sbrooks
2928143Spstif [ $# -lt 1 ] ; then
3028143Spst    usage
3128112Spstfi
3228112Spst
33321260Sngie# If possible, check the global system configuration file,
3444008Sjkh# to see if there are additional dirs to check
3561981Sbrianif [ -r /etc/defaults/periodic.conf ]; then
3661981Sbrian    . /etc/defaults/periodic.conf
3761981Sbrian    source_periodic_confs
3828143Spstfi
3928143Spst
4042239Sbillfhost=`hostname`
4158767Scpiazzaexport host
42231568Sbrooks
43231568Sbrooks# If we were called normally, then create a lock file for each argument
44231568Sbrooks# in turn and reinvoke ourselves with the LOCKED argument.  This prevents
45231568Sbrooks# very long running jobs from being overlapped by another run as this is
46321260Sngie# will lead the system running progressivly slower and more and more jobs
47231568Sbrooks# are run at once.
48231568Sbrooksif [ $1 != "LOCKED" ]; then
49231568Sbrooks    ret=0
50231568Sbrooks    for arg; do
51231568Sbrooks        lockfile=/var/run/periodic.${arg##*/}.lock
52231568Sbrooks        lockf -t 0 "${lockfile}" /bin/sh $0 LOCKED "$arg"
53231568Sbrooks        case $? in
54231568Sbrooks        0) ;;
55231568Sbrooks        73) #EX_CANTCREATE
56255178Sjlh            echo "can't create ${lockfile}" | \
57255178Sjlh                output_pipe $arg "$PERIODIC"
58231568Sbrooks            ret=1
59231568Sbrooks            ;;
60231568Sbrooks        75) #EX_TEMPFAIL
61231568Sbrooks            echo "$host ${arg##*/} prior run still in progress" | \
62255178Sjlh                output_pipe $arg "$PERIODIC"
63231568Sbrooks            ret=1
64231568Sbrooks            ;;
65231568Sbrooks        *)
66231568Sbrooks            ret=1
67231568Sbrooks            ;;
68231568Sbrooks        esac
69231568Sbrooks    done
70231568Sbrooks    exit $ret
71231568Sbrooksfi
72231568Sbrooks
73231568Sbrooksif [ $# -ne 2 ]; then
74231568Sbrooks    usage
75231568Sbrooksfi
76231568Sbrooksshift
77231568Sbrooksarg=$1
78231568Sbrooks
7968226Skristmp_output=`mktemp ${TMPDIR:-/tmp}/periodic.XXXXXXXXXX`
80255178Sjlhcontext="$PERIODIC"
81254829Sjlhexport PERIODIC="$arg${PERIODIC:+ }${PERIODIC}"
8228112Spst
8328143Spst# Execute each executable file in the directory list.  If the x bit is not
8428112Spst# set, assume the user didn't really want us to muck with it (it's a
8528112Spst# README file or has been disabled).
8628112Spst
87231568Sbrookssuccess=YES info=YES badconfig=NO empty_output=YES	# Defaults when ${run}_* aren't YES/NO
88231568Sbrooksfor var in success info badconfig empty_output; do
89231568Sbrooks    case $(eval echo "\$${arg##*/}_show_$var") in
90231568Sbrooks    [Yy][Ee][Ss]) eval $var=YES;;
91231568Sbrooks    [Nn][Oo])     eval $var=NO;;
9265843Sbrian    esac
93231568Sbrooksdone
9465843Sbrian
95231568Sbrookscase $arg in
96231568Sbrooks/*) if [ -d "$arg" ]; then
97231568Sbrooks        dirlist="$arg"
98231568Sbrooks    else
99321260Sngie        echo "$0: $arg not found" >&2
100231568Sbrooks        continue
101231568Sbrooks    fi
102231568Sbrooks    ;;
103231568Sbrooks*)  dirlist=
104231568Sbrooks    for top in /etc/periodic ${local_periodic}; do
105231568Sbrooks        [ -d $top/$arg ] && dirlist="$dirlist $top/$arg"
10665843Sbrian    done
107231568Sbrooks    ;;
108231568Sbrooksesac
10965843Sbrian
110231568Sbrooks{
111231568Sbrooks    empty=TRUE
112231568Sbrooks    processed=0
113231568Sbrooks    for dir in $dirlist; do
114231568Sbrooks        for file in $dir/*; do
115231568Sbrooks            if [ -x $file -a ! -d $file ]; then
116231568Sbrooks                output=TRUE
117231568Sbrooks                processed=$(($processed + 1))
118231568Sbrooks                $file </dev/null >$tmp_output 2>&1
119231568Sbrooks                rc=$?
120231568Sbrooks                if [ -s $tmp_output ]; then
121231568Sbrooks                    case $rc in
122231568Sbrooks                    0)  [ $success = NO ] && output=FALSE;;
123231568Sbrooks                    1)  [ $info = NO ] && output=FALSE;;
124231568Sbrooks                    2)  [ $badconfig = NO ] && output=FALSE;;
125231568Sbrooks                    esac
126231568Sbrooks                    [ $output = TRUE ] && { cat $tmp_output; empty=FALSE; }
12765956Sbrian                fi
128231568Sbrooks                cp /dev/null $tmp_output
129231568Sbrooks            fi
13065956Sbrian        done
131231568Sbrooks    done
132231568Sbrooks    if [ $empty = TRUE ]; then
133231568Sbrooks        if [ $empty_output = TRUE ]; then
134170990Sdwmalone            [ $processed = 1 ] && plural= || plural=s
135170990Sdwmalone            echo "No output from the $processed file$plural processed"
13665956Sbrian        fi
137231568Sbrooks    else
138231568Sbrooks        echo ""
139231568Sbrooks        echo "-- End of $arg output --"
140231568Sbrooks    fi
141255178Sjlh} | output_pipe $arg "$context"
142231568Sbrooks
14369188Skrisrm -f $tmp_output
144