postrun revision 7125
10139N/A#!/bin/ksh
10139N/A#
12219N/A# Script for starting a postponed post-installation command in
10139N/A# a Live-Upgrade-safe environment
10139N/A#
10139N/A# CDDL HEADER START
10139N/A#
10139N/A# The contents of this file are subject to the terms of the
10139N/A# Common Development and Distribution License, Version 1.0 only
10139N/A# (the "License"). You may not use this file except in compliance
10139N/A# with the License.
14185N/A#
10139N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10139N/A# or http://www.opensolaris.org/os/licensing.
10139N/A# See the License for the specific language governing permissions
10139N/A# and limitations under the License.
13299N/A#
10794N/A# When distributing Covered Code, include this CDDL HEADER in each
10794N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
12773N/A# If applicable, add the following below this CDDL HEADER, with the
12773N/A# fields enclosed by brackets "[]" replaced with your own identifying
12773N/A# information: Portions Copyright [yyyy] [name of copyright owner]
10794N/A#
10139N/A# CDDL HEADER END
10815N/A#
10139N/A#
10139N/A# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
10139N/A# Use is subject to license terms.
10139N/A#
10139N/A
10139N/Aif [ `/usr/xpg4/bin/id -u` != 0 ]; then
10139N/A echo "postrun: error: run this script as root"
10139N/A exit 1
10139N/Afi
10139N/A
10139N/Aexport PATH=/usr/bin
10139N/AMYDIR=$(cd $(dirname $0); pwd)
10139N/ASPOOLDIR="$MYDIR/../../var/spool/postrun"
10139N/ALOCKFILE="$SPOOLDIR/.lock"
10139N/ALOGFILE="$MYDIR/../../var/log/postrun.log"
10139N/ASEQFILE="$SPOOLDIR/.seq"
10139N/A
10139N/Ausage() {
10139N/A echo 'Usage: postrun [options]'
10139N/A echo
10139N/A echo 'Options:'
10139N/A echo ' -u, --uniq'
10139N/A echo ' If the same command is requested multiple times, the command'
10139N/A echo ' is only run once. If it is safe to execute the command'
10139N/A echo ' immediately, it will be delayed by 5 minutes, or as set'
10139N/A echo ' using the --timeout option'
10139N/A echo
10139N/A echo ' -t <n>, --timeout <n>'
10139N/A echo ' Delay the execution of uniq commands by <n> minutes.'
10139N/A echo
10139N/A echo ' -b, --bg'
10139N/A echo ' Run the command in the background and return control'
10139N/A echo ' immediately'
10139N/A echo
10139N/A echo ' -f <file>'
10139N/A echo ' Read the commands from <file> instead of the standard'
10139N/A echo ' input.'
10139N/A echo
10139N/A echo ' -h, -?, --help'
10139N/A echo ' Display this help'
10139N/A exit 1
10139N/A}
10139N/A
10139N/A#LOCK_UNLOCK_FUNCTIONS_START
10139N/A# lock the postrun spool or log file
10139N/A# if $1 is 'log' then lock the log file, otherwise log the spool
10139N/Apostrun_lock() {
10139N/A this_lock=$LOCKFILE
10139N/A if [ "x$1" = xlog ]; then
10139N/A this_lock=${LOCKFILE}.log
10139N/A fi
10139N/A # lock file exists (contains the pid of the process that locked it
10139N/A while test -f $this_lock; do
10139N/A # get the pid that holds the lock
10794N/A pid=`cat $this_lock 2>/dev/null` || continue
10794N/A # already locked by this process
12754N/A test "$pid" == "$$" && return
10794N/A # check if the process is still running or else delete the lock
11087N/A ps -g $pid -o 'pid' | egrep -s "^ *$pid *" 2>&1 \
13621N/A && sleep 1 || rm -f $this_lock
10139N/A done
10139N/A # run false so that we enter the while loop
10139N/A false
10139N/A while [ $? != 0 ]; do
10139N/A # write the pid to the lock file
10139N/A if ! echo "$$" > $this_lock; then
10139N/A echo "postrun: error: cannot create lock file $this_lock"
10139N/A exit 1
10139N/A fi
10139N/A # read it back in case another process also wrote it's pid there
10139N/A # in the meantime
10139N/A pid=`cat $this_lock 2>/dev/null`
10139N/A # the loop will restart is the file cannot be read
10139N/A done
10139N/A # check if this process holds the lock or else try the whole thing again
10139N/A test "$pid" == "$$" || postrun_lock $1
10139N/A}
10139N/A
10139N/A# release the lock
10139N/A# unlock the log file if $1 == 'log', unlock the spool otherwise
10139N/Apostrun_unlock() {
11338N/A this_lock=$LOCKFILE
11338N/A if [ "x$1" = xlog ]; then
11338N/A this_lock=${LOCKFILE}.log
11338N/A fi
11338N/A if ! rm -f $this_lock; then
10139N/A echo "postrun: error: cannot remove lock file $this_lock"
10139N/A exit 1
10139N/A fi
10139N/A}
10139N/A#LOCK_UNLOCK_FUNCTIONS_END
10139N/A
10139N/A# get the next job id
10139N/Apostrun_get_seq() {
12773N/A postrun_lock
12773N/A seq=`cat $SEQFILE 2>/dev/null`
12773N/A next_seq=$(($seq + 1))
12773N/A echo $next_seq > $SEQFILE
12773N/A postrun_unlock
10139N/A echo $next_seq
10139N/A}
10139N/A
10139N/Ais_number() {
10139N/A echo "$1" | egrep -vs '^[0-9]+$' && return 1
10139N/A echo "$1" | egrep -s '^[0-9]+$' || return 1
10139N/A return 0
10139N/A}
10139N/A
10139N/Apostrun_spool_command() {
10139N/A cd $SPOOLDIR
10139N/A # check if there's already a spooled job for the same command
10139N/A uniq_job_nr=
10139N/A IFS=' '
10139N/A postrun_lock
10139N/A for f in *.cmd; do
10139N/A test -f "$f" || continue
11338N/A cmp -s $postrun_command_file $f && {
11338N/A if [ $postrun_is_uniq = yes ]; then
10139N/A uniq_job_nr=`basename $f .cmd`
10139N/A break
10139N/A fi
10139N/A egrep -s '^uniq_command: yes' `basename $f .cmd`.ctrl && {
10139N/A uniq_job_nr=`basename $f .cmd`
10139N/A break
10139N/A }
10139N/A }
10139N/A done
10139N/A if [ "x$uniq_job_nr" != x ]; then
10139N/A # we found a matching spooled uniq job
10139N/A # all we need to do is update the uniq time and make sure it's
10139N/A # flagged as a uniq job
10139N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \
10139N/A -e 's/^\(pkginst: .*\)/\1, '$PKGINST'/' \
10139N/A -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \
10139N/A -e 's/^uniq_timeout: .*/uniq_timeout: '"$postrun_uniq_timeout"'/' \
10139N/A $uniq_job_nr.ctrl > $uniq_job_nr.ctrl.new
10139N/A mv $uniq_job_nr.ctrl.new $uniq_job_nr.ctrl
10139N/A # run the spooled jobs in postrun_uniq_timeout minutes
10139N/A echo "$MYDIR/postrun -q" | \
10139N/A at now "+${postrun_uniq_timeout}minutes"
10139N/A else
10139N/A postrun_unlock
10139N/A job_seq=`postrun_get_seq`
10139N/A postrun_lock
10139N/A ctrl_file="$SPOOLDIR/$job_seq.ctrl"
10139N/A cmd_file="$SPOOLDIR/$job_seq.cmd"
10139N/A cat $postrun_command_file > $cmd_file
10139N/A cat /dev/null > $ctrl_file
10139N/A echo "pkginst: $PKGINST" >> $ctrl_file
10139N/A echo "submit_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
10139N/A echo "uniq_command: $postrun_is_uniq" >> $ctrl_file
10139N/A echo "uniq_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
10139N/A echo "uniq_timeout: $postrun_uniq_timeout" >> $ctrl_file
10139N/A echo "background: $postrun_bg_job" >> $ctrl_file
10139N/A fi
10139N/A postrun_unlock
10139N/A}
10139N/A
10139N/Apostrun_run_command() {
14185N/A cmdout=`mktemp /tmp/postrun.out.XXXX`
14185N/A # create a background jobs script that executes the commands
14123N/A # then locks the spool/log file and appends the output to the
14123N/A # log file and finally unlocks
14123N/A cmdfile=`mktemp /tmp/postrun.job.XXXX`
14038N/A cat /dev/null > $cmdfile
14038N/A cat /dev/null > $cmdout
13925N/A echo '#!/bin/ksh' >> $cmdfile
13925N/A # copy the postrun_lock and postrun_unlock commands from
13925N/A # this script to the background job script
13761N/A echo "LOCKFILE=$LOCKFILE" >> $cmdfile
13767N/A sed -e '1,/#LOCK_UNLOCK_FUNCTIONS_START/d' \
13767N/A -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile
13767N/A # save the stdout file description
13767N/A echo 'exec 3<&1' >> $cmdfile
13767N/A echo "exec >> $cmdout 2>&1" >> $cmdfile
13761N/A echo 'PATH=/usr/bin; export PATH' >> $cmdfile
13754N/A echo 'echo Starting postrun job at `date`' >> $cmdfile
13754N/A if [ "x$postrun_submit_time" != x ]; then
13692N/A if [ $postrun_bg_job = yes ]; then
13692N/A echo 'echo This is a spooled background job' >> $cmdfile
13639N/A else
13639N/A echo 'echo This is a spooled foreground job' >> $cmdfile
13639N/A fi
13639N/A echo "echo Job submitted by $postrun_pkginst at $postrun_submit_time" \
13639N/A >> $cmdfile
13621N/A else
13621N/A if [ $postrun_bg_job = yes ]; then
13621N/A echo 'echo This is an immediate background job' >> $cmdfile
13499N/A else
13499N/A echo 'echo This is an immediate foreground job' >> $cmdfile
13498N/A fi
13498N/A echo "echo Job submitted by $postrun_pkginst"\
13416N/A >> $cmdfile
13416N/A fi
13313N/A echo 'echo Running commands:' >> $cmdfile
13313N/A echo "echo '>>>' commands follow:" >> $cmdfile
13299N/A cat $postrun_command_file | \
13299N/A sed -e "s/'/'\"'\"'/g" | \
13274N/A sed -e "s/^/echo '/" \
13274N/A -e "s/\$/'/" >> $cmdfile
13261N/A echo "echo '<<<' commands end" >> $cmdfile
13261N/A echo "echo '>>>' Command output follows:" >> $cmdfile
13103N/A echo "chmod +x $postrun_command_file" >> $cmdfile
13103N/A echo "$postrun_command_file" >> $cmdfile
12988N/A echo "echo '<<<' Command completed with exit status \$?" \
12988N/A >> $cmdfile
12933N/A echo 'echo Job finished at `date`' >> $cmdfile
12933N/A echo 'echo --' >> $cmdfile
12933N/A # restore PATH in case the command changed it
12571N/A echo 'PATH=/usr/bin; export PATH' >> $cmdfile
12571N/A # restore stdout
12466N/A echo 'exec 1<&3' >> $cmdfile
12466N/A # close file descriptor 3
12413N/A echo 'exec 3<&-' >> $cmdfile
12413N/A echo 'exec 2>&1' >> $cmdfile
12413N/A # append the messages to the real log file
12401N/A # need to lock the log file to avoid 2 postrun commands
12401N/A # writing at the same time and messing up the log
12387N/A echo 'postrun_lock log' >> $cmdfile
12387N/A echo "cat $cmdout >> $LOGFILE" >> $cmdfile
12370N/A echo 'postrun_unlock log' >> $cmdfile
12370N/A echo "rm -f $cmdout" >> $cmdfile
12287N/A echo "rm -f $cmdfile" >> $cmdfile
12287N/A chmod +x $cmdfile
12227N/A if [ $postrun_bg_job = yes ]; then
12227N/A $cmdfile &
12219N/A else
12219N/A $cmdfile
12071N/A fi
12071N/A exitval=$?
12022N/A}
12022N/A
11922N/Apostrun_defaults() {
11922N/A # default settings
11819N/A postrun_pkginst="$PKGINST"
11821N/A postrun_submit_time=""
11416N/A postrun_uniq_time=""
11418N/A postrun_is_uniq=no
11338N/A postrun_uniq_timeout=5
11338N/A postrun_bg_job=no
11253N/A postrun_command_file=""
11253N/A}
11172N/A
11172N/A# usage: is_leap_year yyyy
11118N/Ais_leap_year() {
11118N/A cal 02 $1 | egrep -s 29 && return 0
11087N/A return 1
11087N/A}
11087N/A
11073N/A# get_abstime yy mm dd hh mm ss
11067N/A#
11067N/A# prints the elapsed time in seconds since 1970.01.01.00.00.00
11060N/A#Length of the months:
11060N/A# JA FE MA AP MY JN JL AU SE OC NO DE
11025N/Aset -A MONTH 0 31 28 31 30 31 30 31 31 30 31 30 31
11025N/Aget_abstime() {
11025N/A # the absolute time since 1970...
10986N/A t=0
10988N/A
10980N/A # number of years
10980N/A t=$(($t + ($1 - 1970) * 31536000))
10917N/A
10917N/A # add 1 day for each leap year
10815N/A y=1972
10815N/A end_y=$1
10794N/A if [ $2 -lt 2 ]; then
10794N/A end_y=$(($1 - 1))
10794N/A fi
10742N/A while [ $y -le $end_y ]; do
10742N/A is_leap_year $y && t=$(($t + 86400))
10640N/A y=$(($y + 4))
10640N/A done
10613N/A
10613N/A # number of months
10473N/A m=1
10474N/A while [ $m -lt $2 ]; do
10406N/A t=$(($t + ${MONTH[$m]} * 86400))
10406N/A m=$(($m + 1))
10406N/A done
10283N/A
10283N/A # number of days, hours, minutes and seconds:
10139N/A echo $(($t + ($3 - 1) * 86400 + $4 * 3600 + $5 * 60 + $6))
10139N/A}
10139N/A
10139N/A# get_timediff: prints the difference in seconds between 2 time strings
10139N/A# the time strings should be of the following format:
10139N/A# YYYY.MM.DD.HH.MM.SS as printed by date +%Y.%m.%d.%H.%M.%S
10139N/A#
10139N/A# Works for dates after 1970.01.01.00.00.00
10139N/A#
10139N/Aget_timediff() {
10139N/A year1=$(expr "$1" : "^\([^.]*\)\..*")
10139N/A month1=$(expr "$1" : "^[^.]*\.\([^.]*\)\..*")
10139N/A day1=$(expr "$1" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
10139N/A hour1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
10139N/A min1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
10139N/A sec1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
10139N/A
10139N/A year2=$(expr "$2" : "^\([^.]*\)\..*")
10139N/A month2=$(expr "$2" : "^[^.]*\.\([^.]*\)\..*")
10139N/A day2=$(expr "$2" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
10139N/A hour2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
10139N/A min2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
10139N/A sec2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
10139N/A
10139N/A # calculate seconds since 1970.01.01.00.00.00
10139N/A t1=`get_abstime $year1 $month1 $day1 $hour1 $min1 $sec1`
10139N/A t2=`get_abstime $year2 $month2 $day2 $hour2 $min2 $sec2`
10139N/A
10139N/A # print difference
10139N/A expr $t1 - $t2
10139N/A}
10139N/A
10139N/Apostrun_runq() {
10139N/A cd $SPOOLDIR
10139N/A IFS=' '
10139N/A postrun_lock
10139N/A for job in *.ctrl; do
10139N/A test -f "$job" || continue
10139N/A postrun_defaults
10139N/A while read var val; do
10139N/A case "$var" in
10139N/A pkginst: )
10139N/A postrun_pkginst="$val"
10139N/A ;;
10139N/A submit_time: )
10139N/A postrun_submit_time="$val"
10139N/A ;;
10139N/A uniq_command: )
10139N/A postrun_is_uniq="$val"
10139N/A ;;
10139N/A uniq_time: )
10139N/A postrun_uniq_time="$val"
10139N/A ;;
10139N/A uniq_timeout: )
10139N/A postrun_uniq_timeout="$val"
10139N/A ;;
10139N/A background: )
10139N/A postrun_bg_job="$val"
10139N/A ;;
10139N/A * )
10139N/A echo "postrun: WARNING: invalid setting in $job: $var"
10139N/A ;;
10139N/A esac
10139N/A done < $job
10139N/A postrun_command_file=$SPOOLDIR/`basename $job .ctrl`.cmd
10139N/A if [ $postrun_ignore_timeout = no ]; then
10139N/A # if it's a uniq job, check if it timed out
10139N/A if [ "x$postrun_is_uniq" = xyes ]; then
10139N/A # calculate time difference (seconds)
10139N/A tdiff=$(get_timediff $(date +%Y.%m.%d.%H.%M.%S) \
10139N/A $postrun_uniq_time)
10139N/A timeout_sec=$((postrun_uniq_timeout * 60))
10139N/A if [ $tdiff -ge $timeout_sec ]; then
10139N/A postrun_run_command
10139N/A rm -f $postrun_command_file $job
10139N/A fi
10139N/A else
10139N/A postrun_run_command
10139N/A rm -f $postrun_command_file $job
10139N/A fi
10139N/A else
10139N/A # ignore timeout, just run the job
10139N/A postrun_run_command
10139N/A rm -f $postrun_command_file $job
10139N/A fi
10139N/A done
10139N/A postrun_unlock
10139N/A exit 0
10139N/A}
10139N/A
10139N/Apostrun_defaults
10139N/Aexitval=0
10139N/A
10139N/Apostrun_ignore_timeout=no
10139N/Aif [ $# = 1 -a "x$1" = 'x-qf' ]; then
10139N/A # postrun-runq mode (ignore timeout for uniq jobs, since this is
10139N/A # expected to be run at system boot)
10139N/A postrun_ignore_timeout=yes
10139N/A postrun_runq
10139N/A exit 1
10139N/Afi
10139N/A
10139N/Aif [ $# = 1 -a "x$1" = 'x-q' ]; then
10139N/A # postrun-runq mode, to be run from at(1)
10139N/A postrun_runq
10139N/A exit 1
10139N/Afi
10139N/A
10139N/A# process the command line
10139N/Awhile [ $# -gt 0 ]; do
10139N/A case "$1" in
10139N/A -h|-\?|--help)
10139N/A usage
10139N/A ;;
10139N/A -u|--uniq)
10139N/A postrun_is_uniq=yes
10139N/A ;;
10139N/A -b|--bg)
10139N/A postrun_bg_job=yes
10139N/A ;;
10139N/A -t|--timeout)
10139N/A opt="$1"
10139N/A if [ $# == 0 ]; then
10139N/A echo "postrun: error: argument expected after $opt"
10139N/A exit 1
10139N/A fi
10139N/A shift
10139N/A timeout=$1
10139N/A if ! is_number "$timeout"; then
10139N/A echo "postrun: error: interger number expected after $opt (found \"$timeout\")"
10139N/A exit 1
10139N/A fi
10139N/A postrun_uniq_timeout=$timeout
10139N/A ;;
10139N/A -f)
10139N/A opt="$1"
10139N/A if [ $# == 0 ]; then
10139N/A echo "postrun: error: argument expected after $opt"
10139N/A exit 1
10139N/A fi
10139N/A shift
10139N/A postrun_command_file="$1"
10139N/A ;;
10139N/A --)
10139N/A break
10139N/A ;;
10139N/A *)
10139N/A echo "postrun: error: invalid argument: $1"
10139N/A exit 1
10139N/A ;;
10139N/A esac
10139N/A shift
10139N/Adone
10139N/A
10139N/Aif [ "x$postrun_command_file" = x ]; then
10139N/A # save the standard input in a temporary file
10139N/A tmp_cmd_file=`mktemp /tmp/postrun.cmd.XXXX`
10139N/A cat > $tmp_cmd_file
10139N/A postrun_command_file=$tmp_cmd_file
10139N/Afi
10139N/A
10139N/Aif [ "$LUBIN" != "" ]; then
10139N/A #
10139N/A # Live Upgrade. Unsafe to run the command now.
10139N/A # Put into spool and defer to next boot.
10139N/A #
10139N/A postrun_spool_command "${@}"
10139N/Aelif [ "$PKG_INSTALL_ROOT" != "" -a "$PKG_INSTALL_ROOT" != "/" ]; then
10139N/A #
10139N/A # Installation to an alternate root directory
10139N/A # Put command into spool and defer to next boot.
10139N/A #
postrun_spool_command "${@}"
else
#
# Local package install. Everything's shiny happy,
# safe to run the command right now
#
if [ $postrun_is_uniq = yes ]; then
# don't run the command yet in case the same command is requested
# within the next postrun_uniq_timeout minutes
postrun_spool_command "${@}"
# run the spooled jobs in postrun_uniq_timeout minutes
echo "$MYDIR/postrun -q" | \
at now "+${postrun_uniq_timeout}minutes"
else
postrun_run_command "${@}"
fi
fi
if [ "x$tmp_cmd_file" != x ]; then
rm -f $tmp_cmd_file
fi
exit $exitval