postrun revision 8060
10139N/A#!/bin/ksh
10139N/A#
10139N/A# Script for starting a postponed post-installation command in
10139N/A# a Live-Upgrade-safe environment
10139N/A#
16307N/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
17185N/A# (the "License"). You may not use this file except in compliance
10139N/A# with the License.
10139N/A#
17240N/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
13598N/A# and limitations under the License.
10139N/A#
19958N/A# When distributing Covered Code, include this CDDL HEADER in each
18688N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
18688N/A# If applicable, add the following below this CDDL HEADER, with the
10139N/A# fields enclosed by brackets "[]" replaced with your own identifying
20056N/A# information: Portions Copyright [yyyy] [name of copyright owner]
10139N/A#
12856N/A# CDDL HEADER END
10139N/A#
10139N/A#
10139N/A# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
19142N/A# Use is subject to license terms.
16401N/A#
16401N/A
10139N/Aif [ `/usr/xpg4/bin/id -u` != 0 ]; then
19997N/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/Aumask 0133
16307N/A
13075N/Ausage() {
17240N/A echo 'Usage: postrun [options]'
16575N/A echo
16575N/A echo 'Options:'
17529N/A echo ' -u, --uniq'
16401N/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.'
19997N/A echo
18993N/A echo ' -b, --bg'
10139N/A echo ' Run the command in the background and return control'
10139N/A echo ' immediately'
13075N/A echo
16575N/A echo ' -f <file>'
16575N/A echo ' Read the commands from <file> instead of the standard'
17529N/A echo ' input.'
10139N/A echo
10139N/A echo ' -h, -?, --help'
19126N/A echo ' Display this help'
10139N/A exit 1
19126N/A}
10139N/A
19142N/Apostrun_debug() {
10139N/A if [ "x$POSTRUN_DEBUG" = xyes ]; then
10139N/A for msg in "${@}"; do
10139N/A echo '<POSTRUN_DEBUG>' "$msg" 1>&2
10139N/A done
10139N/A fi
10139N/A}
10139N/A
19142N/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
19142N/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
10139N/A pid=`cat $this_lock 2>/dev/null` || continue
13598N/A # is the lock held by this process?
10139N/A test "$pid" == "$$" && return
10139N/A # check if the process is still running or else delete the stale lock
10139N/A ps -g $pid -o 'pid' | egrep -s "$pid\$" 2>&1 \
10139N/A && sleep 1 || rm -f $this_lock
10139N/A done
10139N/A # create a read-only lock file
16745N/A umask 0333
15964N/A # run false so that we enter the while loop
13598N/A false
13598N/A while [ $? != 0 ]; do
15964N/A # write the pid to the lock file
10139N/A # recurse if failed. recursion here helps to avoid an infinite
10139N/A # loop -- ksh will kill the script after 128 attempts
13598N/A echo "$$" > $this_lock || postrun_lock $1
10139N/A # read it back in case another process also wrote its pid there
10139N/A # in the meantime
16745N/A pid=`cat $this_lock 2>/dev/null`
16745N/A # the loop will restart is the file cannot be read
10139N/A done
13598N/A umask 0133
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() {
10139N/A this_lock=$LOCKFILE
10139N/A if [ "x$1" = xlog ]; then
10139N/A this_lock=${LOCKFILE}.log
10139N/A fi
10139N/A if ! rm -f $this_lock; then
10139N/A echo "postrun: error: cannot remove lock file $this_lock"
16307N/A exit 1
10139N/A fi
10139N/A}
16307N/A#LOCK_UNLOCK_FUNCTIONS_END
10139N/A
10139N/A# get the next job id
10139N/Apostrun_get_seq() {
10139N/A postrun_lock
10139N/A seq=`cat $SEQFILE 2>/dev/null`
10139N/A next_seq=$(($seq + 1))
10139N/A echo $next_seq > $SEQFILE
10139N/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=' '
14348N/A postrun_lock
14348N/A for f in *.cmd; do
14348N/A test -f "$f" || continue
14348N/A cmp -s $postrun_command_file $f && {
14348N/A if [ $postrun_is_uniq = yes ]; then
14348N/A uniq_job_nr=`basename $f .cmd`
14348N/A break
14348N/A fi
14348N/A egrep -s '^uniq_command: yes' `basename $f .cmd`.ctrl && {
14348N/A uniq_job_nr=`basename $f .cmd`
16575N/A break
16575N/A }
16575N/A }
16575N/A done
14348N/A if [ "x$uniq_job_nr" != x ]; then
16575N/A postrun_debug "matching spooled uniq job (#${uniq_job_nr}) found"
16575N/A # we found a matching spooled uniq job
14348N/A # all we need to do is update the uniq time and make sure it's
14348N/A # flagged as a uniq job
14348N/A
14348N/A #
14348N/A # FIXME: shouldn't use sed for this, safer to simply append
14348N/A # and process the duplicate entries when reading the file
14348N/A #
14348N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \
14348N/A -e 's/^\(pkginst: .*\)/\1, '$PKGINST'/' \
14348N/A -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \
14348N/A -e 's/^uniq_timeout: .*/uniq_timeout: '"$postrun_uniq_timeout"'/' \
14348N/A $uniq_job_nr.ctrl > $uniq_job_nr.ctrl.new
14348N/A
14348N/A #
14348N/A # FIXME: add the user name to the contol file
14348N/A #
14348N/A
14348N/A mv $uniq_job_nr.ctrl.new $uniq_job_nr.ctrl
14348N/A # run the spooled jobs in postrun_uniq_timeout minutes
14348N/A echo "$MYDIR/postrun -q" | \
14348N/A at now "+${postrun_uniq_timeout}minutes" > /dev/null 2>&1
14348N/A
14348N/A # FIXME: kill the previous at(1) job?
14348N/A
14348N/A else
14348N/A postrun_unlock
14348N/A job_seq=`postrun_get_seq`
14348N/A postrun_debug "spooling command as job #${job_seq}"
14348N/A postrun_lock
14348N/A user_name=${EMAIL:-root}
14348N/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 echo "user: $user_name" >> $ctrl_file
10139N/A fi
10139N/A postrun_unlock
10139N/A}
10139N/A
10522N/Apostrun_run_command() {
10139N/A cmdout=`mktemp /tmp/postrun.out.XXXX`
10139N/A # create a background jobs script that executes the commands
10139N/A # then locks the spool/log file and appends the output to the
10139N/A # log file and finally unlocks
10139N/A cmdfile=`mktemp /tmp/postrun.job.XXXX`
10139N/A cat /dev/null > $cmdfile
10139N/A cat /dev/null > $cmdout
10139N/A echo '#!/bin/ksh' >> $cmdfile
10139N/A # copy the postrun_lock and postrun_unlock commands from
10139N/A # this script to the background job script
10139N/A echo "LOCKFILE=$LOCKFILE" >> $cmdfile
10139N/A sed -e '1,/#LOCK_UNLOCK_FUNCTIONS_START/d' \
10139N/A -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile
10139N/A # save the stdout file description
17967N/A echo 'exec 3<&1' >> $cmdfile
17967N/A echo "exec >> $cmdout 2>&1" >> $cmdfile
17529N/A echo 'PATH=/usr/bin; export PATH' >> $cmdfile
17529N/A echo 'echo Starting postrun job at `date`' >> $cmdfile
17529N/A if [ "x$postrun_submit_time" != x ]; then
17240N/A if [ $postrun_bg_job = yes ]; then
17240N/A echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile
16745N/A else
16745N/A echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile
16745N/A fi
16714N/A echo "echo Job submitted by $postrun_pkginst at $postrun_submit_time" \
16714N/A >> $cmdfile
16714N/A else
16575N/A if [ $postrun_bg_job = yes ]; then
16307N/A echo 'echo This is an immediate background job' >> $cmdfile
16307N/A else
16224N/A echo 'echo This is an immediate foreground job' >> $cmdfile
16224N/A fi
16224N/A echo "echo Job submitted by $postrun_pkginst"\
16224N/A >> $cmdfile
15964N/A fi
15964N/A echo 'echo Running commands:' >> $cmdfile
15964N/A echo "echo '>>>' commands follow:" >> $cmdfile
14348N/A cat $postrun_command_file | \
14348N/A sed -e "s/'/'\"'\"'/g" | \
13075N/A sed -e "s/^/echo '/" \
13075N/A -e "s/\$/'/" >> $cmdfile
12203N/A echo "echo '<<<' commands end" >> $cmdfile
12203N/A echo "echo '>>>' Command output follows:" >> $cmdfile
11314N/A echo "chmod 700 $postrun_command_file" >> $cmdfile
11314N/A echo "$postrun_command_file" >> $cmdfile
10522N/A
10522N/A #
10139N/A # FIXME: send email to $postrun_user if the command failed
10139N/A #
10139N/A
10139N/A echo "echo '<<<' Command completed with exit status \$?" \
10139N/A >> $cmdfile
10139N/A echo 'echo Job finished at `date`' >> $cmdfile
10139N/A echo 'echo --' >> $cmdfile
10139N/A # restore PATH in case the command changed it
10139N/A echo 'PATH=/usr/bin; export PATH' >> $cmdfile
10139N/A # restore stdout
10139N/A echo 'exec 1<&3' >> $cmdfile
10139N/A # close file descriptor 3
10139N/A echo 'exec 3<&-' >> $cmdfile
10139N/A echo 'exec 2>&1' >> $cmdfile
10139N/A # append the messages to the real log file
10139N/A # need to lock the log file to avoid 2 postrun commands
10139N/A # writing at the same time and messing up the log
10139N/A echo 'postrun_lock log' >> $cmdfile
10139N/A echo "cat $cmdout >> $LOGFILE" >> $cmdfile
10139N/A echo 'postrun_unlock log' >> $cmdfile
10139N/A echo "rm -f $cmdout" >> $cmdfile
10139N/A echo "rm -f $cmdfile" >> $cmdfile
10139N/A echo "rm -f $postrun_command_file" >> $cmdfile
10139N/A chmod 700 $cmdfile
10139N/A if [ $postrun_bg_job = yes ]; then
10139N/A $cmdfile &
10139N/A else
10139N/A $cmdfile
10139N/A fi
10139N/A exitval=$?
10139N/A}
10139N/A
10139N/Ausername=${EMAIL:-root}
10139N/A
10139N/Apostrun_defaults() {
10139N/A # default settings
10139N/A postrun_pkginst="$PKGINST"
10139N/A postrun_submit_time=''
10139N/A postrun_uniq_time=''
10139N/A postrun_is_uniq=no
10139N/A postrun_uniq_timeout=5
10139N/A postrun_bg_job=no
10139N/A postrun_command_file=''
10139N/A postrun_user=${username}
10139N/A postrun_job_number='???'
10139N/A postrun_alt_root_okay=no
10139N/A}
10139N/A
10139N/A# usage: is_leap_year yyyy
10139N/Ais_leap_year() {
10139N/A cal 02 $1 | egrep -s 29 && return 0
10139N/A return 1
10139N/A}
10139N/A
10139N/A# get_abstime yy mm dd hh mm ss
10139N/A#
10139N/A# prints the elapsed time in seconds since 1970.01.01.00.00.00
16401N/A#Length of the months:
19252N/A# JA FE MA AP MY JN JL AU SE OC NO DE
19569N/Aset -A MONTH 0 31 28 31 30 31 30 31 31 30 31 30 31
get_abstime() {
# the absolute time since 1970...
t=0
# number of years
t=$(($t + ($1 - 1970) * 31536000))
# add 1 day for each leap year
y=1972
end_y=$1
if [ $2 -lt 2 ]; then
end_y=$(($1 - 1))
fi
while [ $y -le $end_y ]; do
is_leap_year $y && t=$(($t + 86400))
y=$(($y + 4))
done
# number of months
m=1
while [ $m -lt $2 ]; do
t=$(($t + ${MONTH[$m]} * 86400))
m=$(($m + 1))
done
# number of days, hours, minutes and seconds:
echo $(($t + ($3 - 1) * 86400 + $4 * 3600 + $5 * 60 + $6))
}
# get_timediff: prints the difference in seconds between 2 time strings
# the time strings should be of the following format:
# YYYY.MM.DD.HH.MM.SS as printed by date +%Y.%m.%d.%H.%M.%S
#
# Works for dates after 1970.01.01.00.00.00
#
get_timediff() {
year1=$(expr "$1" : "^\([^.]*\)\..*")
month1=$(expr "$1" : "^[^.]*\.\([^.]*\)\..*")
day1=$(expr "$1" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
hour1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
min1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
sec1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
year2=$(expr "$2" : "^\([^.]*\)\..*")
month2=$(expr "$2" : "^[^.]*\.\([^.]*\)\..*")
day2=$(expr "$2" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
hour2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
min2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
sec2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
# calculate seconds since 1970.01.01.00.00.00
t1=`get_abstime $year1 $month1 $day1 $hour1 $min1 $sec1`
t2=`get_abstime $year2 $month2 $day2 $hour2 $min2 $sec2`
# print difference
expr $t1 - $t2
}
postrun_runq() {
cd $SPOOLDIR
IFS=' '
postrun_lock
for job in *.ctrl; do
test -f "$job" || continue
postrun_defaults
while read var val; do
case "$var" in
pkginst: )
postrun_pkginst="$val"
;;
submit_time: )
postrun_submit_time="$val"
;;
uniq_command: )
postrun_is_uniq="$val"
;;
uniq_time: )
postrun_uniq_time="$val"
;;
uniq_timeout: )
postrun_uniq_timeout="$val"
;;
background: )
postrun_bg_job="$val"
;;
user: )
postrun_user="$val"
;;
* )
echo "postrun: WARNING: invalid setting in $job: $var"
;;
esac
done < $job
postrun_command_file=$SPOOLDIR/`basename $job .ctrl`.cmd
postrun_job_number=`basename $job .ctrl`
if [ $postrun_ignore_timeout = no ]; then
# if it's a uniq job, check if it timed out
if [ "x$postrun_is_uniq" = xyes ]; then
# calculate time difference (seconds)
tdiff=$(get_timediff $(date +%Y.%m.%d.%H.%M.%S) \
$postrun_uniq_time)
timeout_sec=$((postrun_uniq_timeout * 60))
if [ $tdiff -ge $timeout_sec ]; then
postrun_run_command
rm -f $job
fi
else
postrun_run_command
rm -f $job
fi
else
# ignore timeout, just run the job
postrun_run_command
rm -f $job
fi
done
postrun_unlock
exit 0
}
postrun_defaults
exitval=0
postrun_ignore_timeout=no
if [ $# = 1 -a "x$1" = 'x-qf' ]; then
# postrun-runq mode (ignore timeout for uniq jobs, since this is
# expected to be run at system boot)
postrun_ignore_timeout=yes
postrun_runq
exit 1
fi
if [ $# = 1 -a "x$1" = 'x-q' ]; then
# postrun-runq mode, to be run from at(1)
postrun_runq
exit 1
fi
# process the command line
while [ $# -gt 0 ]; do
case "$1" in
-h|-\?|--help)
usage
;;
-u|--uniq)
postrun_is_uniq=yes
;;
-b|--bg)
postrun_bg_job=yes
;;
-t|--timeout)
opt="$1"
if [ $# == 0 ]; then
echo "postrun: error: argument expected after $opt"
exit 1
fi
shift
timeout=$1
if ! is_number "$timeout"; then
echo "postrun: error: interger number expected after $opt (found \"$timeout\")"
exit 1
fi
postrun_uniq_timeout=$timeout
;;
-f)
opt="$1"
if [ $# == 0 ]; then
echo "postrun: error: argument expected after $opt"
exit 1
fi
shift
postrun_command_file="$1"
;;
-a)
postrun_alt_root_okay=yes
;;
--)
break
;;
*)
echo "postrun: error: invalid argument: $1"
exit 1
;;
esac
shift
done
if [ "x$postrun_command_file" = x ]; then
# save the standard input in a temporary file
tmp_cmd_file=`mktemp /tmp/postrun.cmd.XXXX`
cat > $tmp_cmd_file
postrun_command_file=$tmp_cmd_file
fi
if [ "$LUBIN" != "" ]; then
#
# Live Upgrade. Unsafe to run the command now.
# Put into spool and defer to next boot.
#
postrun_spool_command "${@}"
elif [ "$PKG_INSTALL_ROOT" != "" -a "$PKG_INSTALL_ROOT" != "/" -a \
"x$postrun_alt_root_okay" != xyes ]; then
#
# Installation to an alternate root directory
# Put command into spool and defer to next boot.
#
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" > /dev/null 2>&1
else
postrun_debug "Executing commands immediately"
postrun_run_command "${@}"
# do not delete the tmp_cmd_file because it's the only copy of the
# commands (since the job is not spooled)
tmp_cmd_file=''
fi
fi
if [ "x$tmp_cmd_file" != x ]; then
rm -f $tmp_cmd_file
fi
exit $exitval