openpkg/rc

Fri, 03 Aug 2012 15:54:42 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 03 Aug 2012 15:54:42 +0200
changeset 458
153370aaf13f
parent 427
71503088f51b
child 469
65414bdd541c
permissions
-rw-r--r--

Import package vendor original specs for necessary manipulations.

     1 #!@l_prefix@/lib/openpkg/bash --noprofile
     2 ##
     3 ##  rc -- OpenPKG Run-Command Processor
     4 ##  Copyright (c) 2000-2012 OpenPKG GmbH <http://openpkg.com/>
     5 ##
     6 ##  This software is property of the OpenPKG GmbH, DE MUC HRB 160208.
     7 ##  All rights reserved. Licenses which grant limited permission to use,
     8 ##  copy, modify and distribute this software are available from the
     9 ##  OpenPKG GmbH.
    10 ##
    11 ##  THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
    12 ##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    13 ##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    14 ##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
    15 ##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    16 ##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    17 ##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
    18 ##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    19 ##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
    20 ##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
    21 ##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    22 ##  SUCH DAMAGE.
    23 ##
    25 ##
    26 ##  configuration
    27 ##
    29 #   program name, version and date
    30 progname="rc"
    31 progvers="1.2.1"
    32 progdate="2007-12-27"
    34 #   path to OpenPKG instance
    35 prefix="@l_prefix@"
    37 #   path to GNU bash and GNU shtool
    38 bash="$prefix/lib/openpkg/bash"
    39 shtool="$prefix/lib/openpkg/shtool"
    41 #   path to rc.d, rc.conf and rc.func
    42 rcdir="$prefix/etc/rc.d"
    43 rcconf="$prefix/etc/rc.conf"
    44 rcfunc="$prefix/etc/rc.func"
    46 #   helper variables
    47 NL="
    48 "
    50 ##
    51 ##  temporary file handling
    52 ##
    54 #   establish secure temporary directory
    55 i=0
    56 while [ $i -lt 10 ]; do
    57    tmpdir="/tmp/rc-`date '+%Y%m%d%H%M%S'`-$$"
    58    (umask 022; mkdir $tmpdir >/dev/null 2>&1) && break
    59    i=$(($i + 1))
    60    sleep 1
    61 done
    62 if [ $i -eq 10 ]; then
    63     echo "openpkg:rc:ERROR: unable to establish secure temporary directory" 1>&2
    64     exit 1
    65 fi
    66 declare -r tmpdir
    67 cleanup () {
    68     if [ ".$tmpdir" != . ]; then
    69         if [ -d $tmpdir ]; then
    70             rm -rf $tmpdir >/dev/null 2>&1 || true
    71         fi
    72     fi
    73 }
    74 trap "cleanup; trap - EXIT INT ABRT QUIT TERM" EXIT INT ABRT QUIT TERM
    76 #   determine reasonable temporary files
    77 tmpfile="$tmpdir/rc.tmp"
    78 outfile="$tmpdir/rc.out"
    79 errfile="$tmpdir/rc.err"
    80 allfile="$tmpdir/rc.all"
    81 deffile="$tmpdir/rc.def"
    83 #   initialize files
    84 cp /dev/null $deffile
    86 ##
    87 ##  command line option parsing
    88 ##
    90 #   default parameters
    91 silent=0
    92 verbose=0
    93 debug=0
    94 help=0
    95 keep=0
    96 print=0
    97 eval=0
    98 config=0
    99 query=0
   101 #   iterate over argument line
   102 while [ $# -gt 0 ]; do
   103     case "$1" in
   104         -s|--silent  ) silent=1      ;;
   105         -v|--verbose ) verbose=1     ;;
   106         -d|--debug   ) debug=1       ;;
   107         -h|--help    ) help="Usage"  ;;
   108         -k|--keep    ) keep=1        ;;
   109         -p|--print   ) print=1       ;;
   110         -e|--eval    ) eval=1        ;;
   111         -c|--config  ) config=1      ;;
   112         -q|--query   ) query=1       ;;
   113         -D|--define  ) echo "@$2" | \
   114                        sed -e "s;';\\\\';g" | \
   115                        sed -e "s;^@\\([a-z][a-zA-Z0-9_]*\\)=\\(.*\\)\$;\1='\2';" \
   116                            -e "s;^@.*;;" >>$deffile; shift ;;
   117         -*           ) help="Invalid option \`$1'"; break ;;
   118         *            ) break ;;
   119     esac
   120     shift
   121 done
   123 #   display error or usage message
   124 if [ ".$help" != .0 ]; then
   125     if [ ".$help" != ".Usage" ]; then
   126         echo "$progname:ERROR: $help" 1>&2
   127     fi
   128     echo "Usage: $progname [-s|--silent] [-v|--verbose] [-d|--debug] [-k|--keep] [-h|--help]" 1>&2
   129     echo "       [-p|--print] [-e|--eval] [-c|--config] [-q|--query] [-D|--define <name>=<value>]" 1>&2
   130     echo "       <package> <command> [<command> ...]"  1>&2
   131     if [ ".$help" != ".Usage" ]; then
   132         exit 1
   133     else
   134         exit 0
   135     fi
   136 fi
   138 #   determine a reasonable default silent/verbose situation in case
   139 #   nothing was explicitly specified or a conflicting situation was
   140 #   specified. Else is silent either disabled by default or was
   141 #   explicitly enabled.
   142 if [ $silent -eq $verbose ]; then
   143     if [ -t 2 ]; then
   144         #   stdout connected to a terminal device, so no need to be silent
   145         silent=0
   146     else
   147         #   stdout NOT connected to a terminal device, so be silent
   148         silent=1
   149     fi
   150 fi
   152 #   extend run-time environment with local OpenPKG tools (shtool, rpmtool, etc)
   153 PATH_ORIG="$PATH"
   154 PATH="$prefix/lib/openpkg/fallback:$PATH"
   155 PATH="$prefix/bin:$PATH"
   156 PATH="$prefix/sbin:$PATH"
   157 PATH="$prefix/lib/openpkg:$PATH"
   158 PATH="$prefix/lib/openpkg/override:$PATH"
   160 #   handle --query option
   161 if [ ".$query" = .1 ]; then
   162     #   suck in all %config sections of all scripts
   163     #   (rc.openpkg is special: has to be first and requires pre-inclusion of rc.conf)
   164     touch $tmpfile
   165     sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   166     echo ". $rcconf" >>$tmpfile
   167     echo ". $deffile" >>$tmpfile
   168     scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   169     for s_name in $scripts; do
   170         sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   171     done
   172     . $tmpfile
   174     #   apply override values to get effective values
   175     . $rcconf
   176     . $deffile
   178     #   display variable value
   179     for var in $*; do
   180         eval "echo \${$var}"
   181     done
   183     #   stop processing immediately
   184     exit 0
   185 fi
   187 #   handle --config option
   188 if [ ".$config" = .1 ]; then
   189     #   suck in all %config sections of all scripts
   190     #   (rc.openpkg is special: has to be first and requires pre-inclusion of rc.conf)
   191     touch $tmpfile
   192     sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   193     echo ". $rcconf" >>$tmpfile
   194     echo ". $deffile" >>$tmpfile
   195     scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   196     for s_name in $scripts; do
   197         sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   198     done
   199     . $tmpfile
   201     #   remember default values
   202     vars=""
   203     OIFS="$IFS"; IFS="$NL"
   204     for assign in `egrep '[ 	]*[a-zA-Z_][a-zA-Z_0-9]*=' $tmpfile | sort`; do
   205         var=`echo "$assign" | sed -e 's;^[ 	]*\([a-zA-Z_][a-zA-Z_0-9]*\)=.*;\1;'`
   206         vars="$vars $var"
   207         eval "${var}_def=\"\$$var\""
   208     done
   209     IFS="$OIFS"
   211     #   apply override values to get effective values
   212     . $rcconf
   213     . $deffile
   215     #   determine how to print in bold mode in case output
   216     #   is connected to a terminal device
   217     if [ -t 1 ]; then
   218         begin_bold=`$shtool echo -e '%B'`
   219         end_bold=`$shtool echo -e '%b'`
   220     else
   221         begin_bold=""
   222         end_bold=""
   223     fi
   225     #   iterate over all variables and display name, default and effective value
   226     echo "${begin_bold}Configuration Variable   Effective Value              Default Value${end_bold}"
   227     echo "------------------------ ------------------------- -- -------------------------"
   228     for var in . $vars; do
   229         test ".$var" = .. && continue
   230         eval "val=\"\$$var\""
   231         eval "def=\"\$${var}_def\""
   232         tag="!="
   233         begin="$begin_bold"
   234         end="$end_bold"
   235         if [ ".$val" = ".$def" ]; then
   236             tag="=="
   237             begin=""
   238             end=""
   239         fi
   240         printf "%s%-24s %-25s %s %-25s%s\n" "$begin" "$var" "\"$val\"" "$tag" "\"$def\"" "$end"
   241     done
   243     #   stop processing immediately
   244     exit 0
   245 fi
   247 #   determine script(s) to use and make sure they exist
   248 if [ $# -lt 1 ]; then
   249     echo "openpkg:rc:ERROR: no package and command(s) specified" 1>&2
   250     exit 1
   251 fi
   252 if [ $# -lt 2 ]; then
   253     echo "openpkg:rc:ERROR: no command(s) specified for package" 1>&2
   254     exit 1
   255 fi
   256 scripts="${1/*rc./}"
   257 shift
   258 isall=0
   259 if [ ".$scripts" = ".cron" ]; then
   260     #   "cron" is an "all" with a random delay (to ensure that multiple
   261     #   OpenPKG instances do not all startup at exactly the same time)
   262     #   plus a mutex lock to ensure that multiple OpenPKG cron tasks of
   263     #   the same OpenPKG instance do not stumble over each other).
   265     #   determine delay range and timeout
   266     case "${1-quarterly}" in
   267         monthly     ) delay=1800; timeout=28800 ;; # 30m /  8h
   268         weekly      ) delay=1800; timeout=14400 ;; # 30m /  4h
   269         daily       ) delay=900;  timeout=7200  ;; # 15m /  2h
   270         hourly      ) delay=600;  timeout=3600  ;; #  5m /  1h
   271         quarterly|* ) delay=30;   timeout=900   ;; # 30s / 15m
   272     esac
   274     #   apply random run-time delay
   275     #   (hint: $RANDOM is a random value 0..32767)
   276     sleep $(( ($RANDOM * $delay) / 32767 ))
   278     #   wrap ourself for mutual exclusion run-time
   279     #   and then perform the "all" command
   280     cleanup
   281     exec $prefix/lib/openpkg/mutex \
   282         -t $timeout $prefix/RPM/TMP/openpkg-rc-cron.mutex \
   283         sh -c "exec $0 all $*" || exit $?
   284 fi
   285 if [ ".$scripts" = ".all" ]; then
   286     isall=1
   287     . $rcconf
   288     . $deffile
   289     case "$openpkg_rc_all" in
   290         [Nn][Oo] | [Ff][Aa][Ll][Ss][Ee] | [Oo][Ff][Ff] | 0 ) exit 0 ;;
   291     esac
   292     scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;"`
   294     #   the "all" target is usually called from system startup scripts,
   295     #   and we really want to ensure that no potentially corrupt
   296     #   (because of an unclean shutdown of the system) RPM DB journal
   297     #   files are staying around and preventing the startup of the
   298     #   OpenPKG instance. When called manually it also doesn't hurt to
   299     #   cleanup.
   300     rm -f $prefix/RPM/DB/__db.* >/dev/null 2>&1 || true
   301 else
   302     if [ ! -f "$rcdir/rc.$scripts" ]; then
   303         echo "openpkg:rc:ERROR: package \"$scripts\" not found" 1>&2
   304         exit 1
   305     fi
   306 fi
   308 #   determine current run-time user
   309 user=`(id -un) 2>/dev/null ||\
   310       (id | sed -e 's;^[^(]*(\([^)]*\)).*;\1;') 2>/dev/null ||\
   311       (whoami) 2>/dev/null ||\
   312       (who am i | cut "-d " -f1) 2>/dev/null ||\
   313       echo ${LOGNAME:-${USER}}`
   314 if [ ".$user" = . ]; then
   315     echo "openpkg:rc:ERROR: unable to determine current username" 1>&2
   316     exit 1
   317 fi
   319 #   just call OpenPKG RPM to let it once perform the run-time integrity checks
   320 $prefix/bin/openpkg rpm -q openpkg >/dev/null || exit $?
   322 #   iterate over the specified commands
   323 rv=0
   324 cmds="$*"
   325 for cmd in $cmds; do
   326     #   create "all outputs" file for execution operation (i.e. not --print and --eval)
   327     if [ ".$print" = .0 -a ".$eval" = .0 ]; then
   328         rm -f $allfile
   329         touch $allfile
   330     fi
   332     #   find scripts which contain the command and determine
   333     #   their individual user/priority settings
   334     list=''
   335     for s_name in $scripts; do
   336         enable=yes
   338         #   check for upgraded package with unresolved configuration file conflicts
   339         if [ -d "$prefix/etc/$s_name" -a ".$eval" != .1 ]; then
   340             if [ ".`(find $prefix/etc/$s_name -type f -print; find $prefix/etc/$s_name -type l -print) 2>/dev/null | egrep -v '.*/\.(snap|snapshot)/.*' | egrep '.*\.rpm(new|orig|save)$'`" != . ]; then
   341                 case "$cmd" in
   342                     start|restart ) type="ERROR"   ;;
   343                     * )             type="WARNING" ;;
   344                 esac
   345                 echo "openpkg:rc:${type}: package \"$s_name\" has unresolved configuration file conflicts" 1>&2
   346                 echo "openpkg:rc:${type}: indicated by \"*.rpm(new|orig|save)\" files in or below the" 1>&2
   347                 echo "openpkg:rc:${type}: directory \"$prefix/etc/$s_name\". Please resolve first!" 1>&2
   348                 if [ ".$type" = .ERROR ]; then
   349                     continue
   350                 fi
   351             fi
   352         fi
   354         #   check whether command exists in script at all
   355         cmdline=`grep "^%$cmd" $rcdir/rc.$s_name | sed -e "s;^%$cmd[^ 	].*;;"`
   356         if [ ".$cmdline" != . ]; then
   357             #   parse local command options
   358             cmdopts=`echo "$cmdline" | sed -e "s;^%$cmd *;;"`
   359             s_user=$user
   360             s_prio=500
   361             s_output=no
   362             set -- $cmdopts
   363             prev=''
   364             for opt
   365             do
   366                 if [ ".$prev" != . ]; then
   367                     opt="$prev$opt"
   368                     prev=''
   369                 fi
   370                 case $opt in
   371                     -*=*       ) arg=${opt/-*=/} ;;
   372                     -[a-zA-Z]* ) arg=${opt/-[a-zA-Z0-9]/} ;;
   373                               *) arg='' ;;
   374                 esac
   375                 case $opt in
   376                     -u|-p         ) prev=$opt    ;;
   377                     -e|--enable   ) enable=yes   ;;
   378                     -d|--disable  ) enable=no    ;;
   379                     -o|--output   ) s_output=yes ;;
   380                     -u*|--user=*  ) s_user=$arg  ;;
   381                     -p*|--prio=*  ) s_prio=$arg  ;;
   382                     *             ) echo "openpkg:rc:WARNING: invalid local option \"$opt\" in \"$rcdir/rc.$s_name:%$cmd\""; break ;;
   383                 esac
   384                 shift
   385             done
   387             #   sanity check: is operation supported by current environment?
   388             if [ ".$s_user" != ".$user" -a ".$user" != ".root" -a ".$print" = .0 ]; then
   389                 echo "openpkg:rc:ERROR: $s_name:%$cmd: require root privileges to run as user \"$s_user\"" 1>&2
   390                 exit 1
   391             fi
   393             #   skip this script if script is disabled
   394             if [ ".$enable" != .yes ]; then
   395                 continue
   396             fi
   398             #   accumulate the determined information
   399             list="$list,$s_prio:$s_name:$s_user:$s_output"
   400         else
   401             #   command not found in script
   402             if [ ".$isall" = .0 ]; then
   403                 echo "openpkg:rc:ERROR: $s_name: command \"$cmd\" not found" 1>&2
   404                 exit 1
   405             fi
   406         fi
   407     done
   409     #   if operating on all scripts, complain if a non-standard command
   410     #   was used and it was not found in any(!) script at all. The
   411     #   standard commands are accepted to perform no operation if no
   412     #   packages are currently installed which provide such commands.
   413     if [ ".$list" = . -a ".$isall" = .1 ]; then
   414         case "$cmd" in
   415             start|stop|monthly|weekly|daily|hourly|quarterly )
   416                 ;;
   417             * )
   418                 echo "openpkg:rc:ERROR: command \"$cmd\" not found in any script" 1>&2
   419                 rv=1
   420                 break
   421                 ;;
   422         esac
   423     fi
   425     #   generate global (loop invariant) header for script in case of
   426     #   --print and --eval (for the execution approach we cannot do
   427     #   this, because there a new script is generated from scratch for
   428     #   each package.
   429     if [ ".$print" = .1 -o ".$eval" = .1 ]; then
   430         rm -f $tmpfile
   431         touch $tmpfile
   433         #   generate: optionally enable shell debugging
   434         if [ ".$debug" = .1 ]; then
   435             echo "set -x" >>$tmpfile
   436         fi
   438         #   generate: inclusion of the run-command helper functions
   439         echo ". $rcfunc" >>$tmpfile
   441         #   generate: all %config sections of all(!) scripts. We cannot
   442         #   just include those which have the current command in it
   443         #   because by design all command scripts see the %config
   444         #   section of all(!) scripts. Because of $openpkg_rc_def the
   445         #   variable, we place the %config section of "openpkg" to the front.
   446         #   And we have to extra pre-include the rc.conf to allow
   447         #   rc.conf to override the default of $openpkg_rc_def, too.
   448         sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   449         echo ". $rcconf" >>$tmpfile
   450         echo ". $deffile" >>$tmpfile
   451         l_scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   452         for l_name in $l_scripts; do
   453             sed <$rcdir/rc.$l_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   454         done
   456         #   generate: inclusion of the application of override variables
   457         echo ". $rcconf" >>$tmpfile
   458         echo ". $deffile" >>$tmpfile
   460         #   for --eval redirect stderr and stdout (but remember stdout)
   461         #   (let stderr pass unfiltered in case of debug mode)
   462         if [ ".$eval" = .1 ]; then
   463             if [ ".$debug" = .1 ]; then
   464                echo "exec 3<&1- 1>/dev/null" >>$tmpfile
   465             else
   466                echo "exec 3<&1- 1>/dev/null 2>/dev/null" >>$tmpfile
   467             fi
   468         fi
   469     fi
   471     #   iterate over all packages (in priority order!) where the command
   472     #   was found in order to execute, print, or evaluate their scripts
   473     verbose_pos=0
   474     for entry in `echo $list | tr ',' '\012' | sort -n`; do
   475         [ ".$entry" = . ] && continue
   477         #   re-determine the script name, script and whether to print output
   478         eval `echo $entry | sed -e 's%^[0-9]*:\(.*\):\(.*\):\(.*\)$%s_name="\1"; s_user="\2"; s_output="\3";%'`
   480         #   display verbose progress message parts
   481         if [ ".$print" = .0 -a ".$eval" = .0 -a ".$silent" = .0 ]; then
   482             #   line break if we already have output more than 70
   483             #   characters (notice that usually already more characters
   484             #   where printed, because of the name of the last script)
   485             if [ $verbose_pos -gt 70 ]; then
   486                 verbose_pos=0
   487                 echo "" 1>&2
   488             fi
   490             #   display verbose message parts: prefix (on first), separator and package
   491             if [ $verbose_pos -eq 0 ]; then
   492                 output=$(printf "OpenPKG: %s: " "$cmd")
   493                 echo -n "$output" 1>&2
   494                 verbose_pos=$(($verbose_pos + ${#output}))
   495                 output_prefix=""
   496             else
   497                 output_prefix=", "
   498             fi
   499             output=$(printf "%s%s" "$output_prefix" "$s_name")
   500             echo -n "$output" 1>&2
   501             verbose_pos=$(($verbose_pos + ${#output}))
   502         fi
   504         #   now operate on the particular script
   505         if [ ".$print" = .1 ]; then
   506             #   special case: under --print we just add the %common and
   507             #   command scripts to the generated output script and do
   508             #   not execute anything at this point.
   509             sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%common/d" -e '/^%.*/,$d'
   510             sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%$cmd/d" -e '/^%.*/,$d'
   511         elif [ ".$eval" = .1 ]; then
   512             #   special case: under --eval we just add the %common and
   513             #   command scripts to the generated output script and do
   514             #   not execute anything at this point. Additionally, we
   515             #   emulate a real sub-shell environment.
   516             sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%common/d" -e '/^%.*/,$d'
   517             echo "while [ 1 ]; do" >>$tmpfile
   518             sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%$cmd/d" -e '/^%.*/,$d' \
   519                 -e 's/^exit[^;]*/break 99/'   -e 's/\([^a-zA-Z0-9_]\)exit[^;]*/\1break 99/g' \
   520                 -e 's/^return[^;]*/break 99/' -e 's/\([^a-zA-Z0-9_]\)return[^;]*/\1break 99/g'
   521             echo "break" >>$tmpfile
   522             echo "done" >>$tmpfile
   523         else
   524             #   the regular case of executing the command script directly
   526             #   prepare temporary files
   527             rm -f $tmpfile $outfile $errfile
   528             (umask 077; touch $tmpfile $outfile $errfile)
   530             #   generate: optionally enable shell debugging
   531             if [ ".$debug" = .1 ]; then
   532                 echo "set -x" >>$tmpfile
   533             fi
   535             #   generate: inclusion of the run-command helper functions
   536             echo ". $rcfunc" >>$tmpfile
   538             #   generate: all %config sections of all(!) scripts. We cannot
   539             #   just include those which have the current command in it
   540             #   because by design all command scripts see the %config
   541             #   section of all(!) scripts. Because of $openpkg_rc_def the
   542             #   variable, we place the %config section of "openpkg" to the front.
   543             #   And we have to extra pre-include the rc.conf to allow
   544             #   rc.conf to override the default of $openpkg_rc_def, too.
   545             sed <$rcdir/rc.openpkg >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   546             echo ". $rcconf" >>$tmpfile
   547             echo ". $deffile" >>$tmpfile
   548             l_scripts=`/bin/ls $rcdir/rc.* | sed -e "s;^$rcdir/rc\.;;" | egrep -v '^openpkg$'`
   549             for l_name in $l_scripts; do
   550                 sed <$rcdir/rc.$l_name >>$tmpfile -e "1,/^%config/d" -e '/^%.*/,$d'
   551             done
   553             #   generate: inclusion of the application of override variables
   554             echo ". $rcconf" >>$tmpfile
   555             echo ". $deffile" >>$tmpfile
   557             #   generate: %common section and particular command section
   558             sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%common/d" -e '/^%.*/,$d'
   559             sed <$rcdir/rc.$s_name >>$tmpfile -e "1,/^%$cmd/d" -e '/^%.*/,$d'
   561             #   execute the generated script with GNU Bash
   562             if [ ".$user" != ".$s_user" ]; then
   563                 #   execute as different user
   564                 if [ ".$verbose" = .1 ]; then
   565                     echo "openpkg:rc:NOTICE: $prefix:$s_name:%$cmd: executing as user $s_user"
   566                 fi
   567                 if [ ".$user" = ".@l_susr@" -a ".@l_susr@" = ".root" ]; then
   568                     chown $s_user $tmpfile
   569                     if [ ".$debug" = .1 ]; then
   570                         su - $s_user -c "PATH=\"$PATH\"; $bash $tmpfile" >$outfile
   571                         rc=$?
   572                     else
   573                         su - $s_user -c "PATH=\"$PATH\"; $bash $tmpfile" >$outfile 2>$errfile
   574                         rc=$?
   575                     fi
   576                 elif [ ".@l_susr@" != ".root" ]; then
   577                     #   do not complain if we would not have any chance
   578                     #   at all to switch the user because we are running
   579                     #   in a non-privileged instance. Else we would just
   580                     #   trash the mailbox of the user receiving the
   581                     #   output of periodic run-commands.
   582                     rc=0
   583                 else
   584                     echo "openpkg:rc:WARNING: $prefix:$s_name:%$cmd: require root privileges to run as user \"$s_user\"" 1>&2
   585                     rc=1
   586                 fi
   587             else
   588                 #   execute as current user
   589                 if [ ".$verbose" = .1 ]; then
   590                     echo "openpkg:rc:NOTICE: $prefix:$s_name:%$cmd: executing as user $user"
   591                 fi
   592                 if [ ".$debug" = .1 ]; then
   593                     $bash $tmpfile >$outfile
   594                     rc=$?
   595                 else
   596                     $bash $tmpfile >$outfile 2>$errfile
   597                     rc=$?
   598                 fi
   599             fi
   600             if [ $rc -ne 0 ]; then
   601                 if [ ".$silent" = .0 ]; then
   602                     #   indicate failure of execution on verbose message line
   603                     echo ":FAILED" 1>&2
   604                     verbose_pos=0
   605                 fi
   606                 #   give details of execution failure
   607                 ( echo "openpkg:rc:WARNING: $prefix:$s_name:%$cmd: failed with return code $rc"
   608                   if [ ".`cat $outfile $errfile`" != . ]; then
   609                       echo "openpkg:rc:NOTICE: output from stdout/stderr is following:"
   610                       echo "+----------------------------------------------------------------------"
   611                       cat $outfile $errfile | sed -e 's;^;| ;'
   612                       echo "+----------------------------------------------------------------------"
   613                   else
   614                       echo "openpkg:rc:NOTICE: no output occurred on stdout/stderr"
   615                   fi
   616                 ) 1>&2
   618                 #   enforce global return value
   619                 rv=1
   620             else
   621                 #   give details of execution success in case verbose operation is requested
   622                 if [ ".$verbose" = .1 ]; then
   623                     ( echo "openpkg:rc:NOTICE: $prefix:$s_name:%$cmd: succeeded with return code $rc"
   624                       if [ ".`cat $outfile $errfile`" != . ]; then
   625                           echo "openpkg:rc:NOTICE: output from stdout/stderr is following:"
   626                           echo "+----------------------------------------------------------------------"
   627                           cat $outfile $errfile | sed -e 's;^;| ;'
   628                           echo "+----------------------------------------------------------------------"
   629                       else
   630                           echo "openpkg:rc:NOTICE: no output occurred on stdout/stderr"
   631                       fi
   632                     ) 1>&2
   633                 fi
   634                 if [ ".$s_output" = .yes ]; then
   635                     #   accumulate script output for later display
   636                     cat $outfile >>$allfile
   637                 fi
   638             fi
   639         fi
   640     done
   642     #   post-processing for each command
   643     if [ ".$print" = .1 ]; then
   644         #   for --print just print the resulting script to stdout
   645         cat $tmpfile
   646     elif [ ".$eval" = .1 ]; then
   647         #   finish generation of temporary script by restoring stdout
   648         #   and printing the exported environment variables into a format
   649         #   suitable for evaluation by the callers shell.
   650         echo "exec 1<&3-" >>$tmpfile
   651         echo "unset PWD SHLVL" >>$tmpfile
   652         echo "env |\\" >>$tmpfile
   653         echo "egrep '^[A-Z_][A-Z0-9_]*=.' |\\" >>$tmpfile
   654         echo "sed -e '/^_=/d' -e 's/\\\\/\\\\\\\\/g' -e 's/\"/\\\\\"/g' \\" >>$tmpfile
   655         case $SHELL in
   656             csh|*/csh|tcsh|*/tcsh )
   657                 echo "-e 's/^\\([^=]*\\)=\\(.*\\)\$/setenv \\1 \"\\2\"/'" >>$tmpfile
   658                 ;;
   659             * )
   660                 echo "-e 's/^\\([^=]*\\)=\\(.*\\)\$/\\1=\"\\2\"; export \\1/'" >>$tmpfile
   661                 ;;
   662         esac
   664         #   prepare temporary files
   665         rm -f $outfile $errfile
   666         touch $outfile $errfile
   668         #   now replace temporary script with its output
   669         #   by executing it and capturing its output
   670         #   (let stderr pass unfiltered in case of debug mode)
   671         if [ ".$debug" = .1 ]; then
   672             env -i \
   673                 HOME="$HOME" \
   674                 USER="$USER" \
   675                 LOGNAME="$LOGNAME" \
   676                 TERM="$TERM" \
   677                 PATH="$PATH_ORIG" \
   678                 MANPATH="$MANPATH" \
   679                 INFOPATH="$INFOPATH" \
   680                 LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
   681                 $bash --norc --noprofile --posix \
   682                 $tmpfile >$outfile
   683         else
   684             env -i \
   685                 HOME="$HOME" \
   686                 USER="$USER" \
   687                 LOGNAME="$LOGNAME" \
   688                 TERM="$TERM" \
   689                 PATH="$PATH_ORIG" \
   690                 MANPATH="$MANPATH" \
   691                 INFOPATH="$INFOPATH" \
   692                 LD_LIBRARY_PATH="$LD_LIBRARY_PATH" \
   693                 $bash --norc --noprofile --posix \
   694                 $tmpfile >$outfile 2>/dev/null
   695         fi
   696         cp $outfile $tmpfile
   698         #   for --eval we cannot just print the resulting script because
   699         #   not all Bourne-Shell implementations like to "eval" large
   700         #   multi-line outputs. Hence we output a little one-liner which
   701         #   "sources" the script instead and cleans up.
   702         case $SHELL in
   703             csh|*/csh|tcsh|*/tcsh )
   704                 echo "source $tmpfile; rm -rf $tmpdir 2>/dev/null || true"
   705                 ;;
   706             * )
   707                 echo ". $tmpfile; rm -rf $tmpdir 2>/dev/null || true"
   708                 ;;
   709         esac
   710     else
   711         #   for the execution situation just make sure we
   712         #   terminate the verbose message output.
   713         if [ ".$silent" = .0 -a $verbose_pos -gt 0 ]; then
   714             echo "." 1>&2
   715         fi
   717         #   additionally, if a script wants its output to be displayed,
   718         #   now do it. In case there was no such request, do not bother
   719         #   -- we then just output an empty file and so can avoid an
   720         #   extra test surrounding the next command.
   721         cat $allfile
   722     fi
   723 done
   725 #   cleanup temporary files except the result script in
   726 #   case of --eval (which is then removed by the caller).
   727 #   keep those files for debugging purposes if requested.
   728 if [ ".$keep" = .0 ]; then
   729     rm -f $outfile $errfile $allfile >/dev/null 2>&1 || true
   730     if [ ".$eval" = .0 ]; then
   731         rm -f $tmpfile >/dev/null 2>&1 || true
   732         rm -rf $tmpdir >/dev/null 2>&1 || true
   733     fi
   734 fi
   736 #   now clean the exit trap and exit with the global return value
   737 #   indicating to the caller whether all scripts were executed
   738 #   successfully or at least one failed.
   739 trap - EXIT
   740 exit $rv

mercurial