7082N/A#!/bin/ksh
7082N/A#
7082N/A# Script for starting a postponed post-installation command in
7082N/A# a Live-Upgrade-safe environment
7082N/A#
7082N/A# CDDL HEADER START
7082N/A#
7082N/A# The contents of this file are subject to the terms of the
7082N/A# Common Development and Distribution License, Version 1.0 only
7082N/A# (the "License"). You may not use this file except in compliance
7082N/A# with the License.
7082N/A#
7082N/A# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7082N/A# or http://www.opensolaris.org/os/licensing.
7082N/A# See the License for the specific language governing permissions
7082N/A# and limitations under the License.
7082N/A#
7082N/A# When distributing Covered Code, include this CDDL HEADER in each
7082N/A# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7082N/A# If applicable, add the following below this CDDL HEADER, with the
7082N/A# fields enclosed by brackets "[]" replaced with your own identifying
7082N/A# information: Portions Copyright [yyyy] [name of copyright owner]
7082N/A#
7082N/A# CDDL HEADER END
7082N/A#
7082N/A#
7082N/A# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
7082N/A# Use is subject to license terms.
7082N/A#
7082N/A
7082N/Aexport PATH=/usr/bin
8105N/ALC_ALL=C
8105N/Aexport LC_ALL
7082N/AMYDIR=$(cd $(dirname $0); pwd)
8392N/ASPOOLDIR="$MYDIR/../../spool/postrun"
9009N/ALOCKFILE="/var/run/postrun.lock"
8392N/ALOGFILE="$MYDIR/../../log/postrun.log"
7082N/ASEQFILE="$SPOOLDIR/.seq"
7082N/A
8105N/Aid | grep " euid=" && \
8105N/A myuid=`id | sed -e 's/.* euid=\([0-9][0-9]*\).*$/\1/'` || \
8105N/A myuid=`id | sed -e 's/uid=\([0-9][0-9]*\).*/\1/'`
8105N/A
8105N/Aif [ "$myuid" != 0 ]; then
8105N/A echo "postrun: error: run this script as root"
8105N/A exit 1
8105N/Afi
8105N/A
8060N/Aumask 0133
8060N/A
7082N/Ausage() {
7082N/A echo 'Usage: postrun [options]'
7082N/A echo
7082N/A echo 'Options:'
7082N/A echo ' -u, --uniq'
7082N/A echo ' If the same command is requested multiple times, the command'
7082N/A echo ' is only run once. If it is safe to execute the command'
7082N/A echo ' immediately, it will be delayed by 5 minutes, or as set'
7082N/A echo ' using the --timeout option'
7082N/A echo
7082N/A echo ' -t <n>, --timeout <n>'
7082N/A echo ' Delay the execution of uniq commands by <n> minutes.'
7082N/A echo
7082N/A echo ' -b, --bg'
7082N/A echo ' Run the command in the background and return control'
7082N/A echo ' immediately'
7082N/A echo
7082N/A echo ' -f <file>'
7082N/A echo ' Read the commands from <file> instead of the standard'
7082N/A echo ' input.'
7082N/A echo
8283N/A echo ' -c <class>'
8283N/A echo ' Assign this job to class <class>. Useful for querying'
8283N/A echo ' jobs with postrun-query'
8283N/A echo
8283N/A echo ' -i'
8283N/A echo ' Ignore this job if it cannot be executed immediately.'
8283N/A echo
7082N/A echo ' -h, -?, --help'
7082N/A echo ' Display this help'
7082N/A exit 1
7082N/A}
7082N/A
8392N/A# Solaris 9 doesn't have mktemp, need a substitute in order to be
8392N/A# Live Upgrade compliant
8409N/A# Note: only creating tmp files is implemented, directories are not
8392N/Apostrun_mktemp () {
8392N/A test -x /usr/bin/mktemp && {
8392N/A /usr/bin/mktemp $1
8392N/A return
8392N/A }
8392N/A tempname="$1.$$.`date +%H%M%S`"
8409N/A while [ -f $tempname ]; do
8409N/A tempname="$tempname.`date +%S`"
8409N/A done
8409N/A touch $tempname && chmod 700 $tempname || exit 1
8392N/A echo $tempname
8392N/A}
8392N/A
7082N/A#LOCK_UNLOCK_FUNCTIONS_START
9009N/Apostrun_debug() {
9009N/A if [ "x$POSTRUN_DEBUG" = xyes ]; then
9009N/A for msg in "${@}"; do
9009N/A echo '<POSTRUN_DEBUG>' "[$$]" "$msg" 1>&2
9009N/A done
9009N/A fi
9009N/A}
9009N/A
8105N/Ais_number() {
8105N/A echo "$1" | egrep -vs '^[0-9]+$' && return 1
8105N/A echo "$1" | egrep -s '^[0-9]+$' || return 1
8105N/A return 0
8105N/A}
8105N/A
7082N/A# lock the postrun spool or log file
7082N/A# if $1 is 'log' then lock the log file, otherwise log the spool
7082N/Apostrun_lock() {
9009N/A typeset this_lock=$LOCKFILE
7082N/A if [ "x$1" = xlog ]; then
7082N/A this_lock=${LOCKFILE}.log
7082N/A fi
9009N/A postrun_debug "Taking lock on $this_lock"
9009N/A
9009N/A while true ; do
9009N/A # Try to take the lock
9009N/A ln -s $$ $this_lock 2>/dev/null
9009N/A if [ $? = 0 ] ; then
9009N/A postrun_debug "Lock on $this_lock taken"
9009N/A return
9009N/A fi
8105N/A
9009N/A # Read who has the lock. If this process has the lock return
9009N/A typeset pid=$(ls -ld $this_lock \
9009N/A | nawk -F' -> ' '$1 ~ /^l/ && NF == 2 { print $NF }')
9009N/A if [ "$pid" = $$ ] ; then
9009N/A postrun_debug "Lock on $this_lock already held by this pid ($$)"
9009N/A return
9009N/A fi
9009N/A
9009N/A # check to be sure the process that holds the lock is still running
9009N/A # if so, wait for it to be freed. If the lock is stale, remove it
9009N/A # so that the next iteration of the loop can take the lock.
9009N/A if is_number "$pid" && kill -0 "$pid" 2>/dev/null ; then
9009N/A postrun_debug "Waiting for $pid to release $this_lock"
9009N/A sleep 1
9009N/A else
9009N/A postrun_debug "Stale lock $this_lock from $pid being released"
9009N/A rm -f $this_lock
9009N/A fi
7082N/A done
9009N/A postrun_debug "postrun_lock escaped the loop - should not b here"
8282N/A
7082N/A}
7082N/A
7082N/A# release the lock
7082N/A# unlock the log file if $1 == 'log', unlock the spool otherwise
7082N/Apostrun_unlock() {
9009N/A typeset this_lock=$LOCKFILE
7082N/A if [ "x$1" = xlog ]; then
7082N/A this_lock=${LOCKFILE}.log
7082N/A fi
9009N/A postrun_debug "Releasing lock on $this_lock"
9009N/A
9009N/A typeset pid=$(ls -ld $this_lock \
9009N/A | nawk -F' -> ' '$1 ~ /^l/ && NF == 2 { print $NF }')
9009N/A
7082N/A if ! rm -f $this_lock; then
9009N/A echo "postrun: error: cannot remove lock file $this_lock" 1>&2
7082N/A exit 1
7082N/A fi
9009N/A postrun_debug "Released lock on $this_lock taken by pid $pid"
7082N/A}
7082N/A#LOCK_UNLOCK_FUNCTIONS_END
7082N/A
7082N/A# get the next job id
7082N/Apostrun_get_seq() {
7082N/A postrun_lock
7082N/A seq=`cat $SEQFILE 2>/dev/null`
7082N/A next_seq=$(($seq + 1))
7082N/A echo $next_seq > $SEQFILE
7082N/A postrun_unlock
7082N/A echo $next_seq
7082N/A}
7082N/A
7082N/Apostrun_spool_command() {
8283N/A if [ $postrun_no_spool = yes ]; then
8283N/A postrun_debug "Ignoring job, because -i was used and it's not possible to run it now"
8283N/A return 1
8283N/A fi
7082N/A cd $SPOOLDIR
7082N/A # check if there's already a spooled job for the same command
8137N/A new_job=0
7082N/A uniq_job_nr=
8858N/A job_seq=`postrun_get_seq`
7082N/A IFS=' '
7082N/A postrun_lock
7082N/A for f in *.cmd; do
7091N/A test -f "$f" || continue
7082N/A cmp -s $postrun_command_file $f && {
7082N/A if [ $postrun_is_uniq = yes ]; then
7082N/A uniq_job_nr=`basename $f .cmd`
7082N/A break
7082N/A fi
7082N/A egrep -s '^uniq_command: yes' `basename $f .cmd`.ctrl && {
7082N/A uniq_job_nr=`basename $f .cmd`
7082N/A break
7082N/A }
7082N/A }
7082N/A done
7082N/A if [ "x$uniq_job_nr" != x ]; then
7985N/A postrun_debug "matching spooled uniq job (#${uniq_job_nr}) found"
7082N/A # we found a matching spooled uniq job
8858N/A # we need to update the uniq time and make sure it's
7082N/A # flagged as a uniq job
7985N/A
8137N/A new_job=1
8137N/A
7985N/A #
7985N/A # FIXME: shouldn't use sed for this, safer to simply append
7985N/A # and process the duplicate entries when reading the file
7985N/A #
7082N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \
7086N/A -e 's/^\(pkginst: .*\)/\1, '$PKGINST'/' \
8858N/A -e 's/^uniq_time: /resubmit_time: /' \
7091N/A -e 's/^uniq_timeout: .*/uniq_timeout: '"$postrun_uniq_timeout"'/' \
7082N/A $uniq_job_nr.ctrl > $uniq_job_nr.ctrl.new
8858N/A echo 'uniq_time: '`date +%Y.%m.%d.%H.%M.%S` >> $uniq_job_nr.ctrl.new
8858N/A grep "^class: $postrun_job_class$" $uniq_job_nr.ctrl.new \
8858N/A >/dev/null || \
8283N/A echo "class: $postrun_job_class" >> $uniq_job_nr.ctrl.new
7985N/A #
8137N/A # FIXME: add the user name to the control file
7985N/A #
7985N/A
8858N/A # Use a new job id so that the job moves to the end of the queue.
8858N/A # Need to do this such a whay that if this process is interrupted at
8858N/A # any stage, the job is not lost and is not in the queue twice:
8858N/A
8858N/A # Step 1: copy the job to the new id:
8858N/A cp $uniq_job_nr.cmd $job_seq.cmd
8858N/A
8858N/A # Step 2: move the original job ctrl file to the new id:
8858N/A mv $uniq_job_nr.ctrl $job_seq.ctrl
8858N/A
8858N/A # Step 3: replace the original job ctrl file with the updated one:
8858N/A mv $uniq_job_nr.ctrl.new $job_seq.ctrl
8858N/A
8858N/A # Step 4: delete the original cmd file
8858N/A rm -f $uniq_job_nr.cmd
7082N/A else
7985N/A postrun_debug "spooling command as job #${job_seq}"
7985N/A user_name=${EMAIL:-root}
7082N/A ctrl_file="$SPOOLDIR/$job_seq.ctrl"
7082N/A cmd_file="$SPOOLDIR/$job_seq.cmd"
7082N/A cat $postrun_command_file > $cmd_file
7082N/A cat /dev/null > $ctrl_file
7082N/A echo "pkginst: $PKGINST" >> $ctrl_file
7082N/A echo "submit_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
7082N/A echo "uniq_command: $postrun_is_uniq" >> $ctrl_file
7082N/A echo "uniq_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
7082N/A echo "uniq_timeout: $postrun_uniq_timeout" >> $ctrl_file
7082N/A echo "background: $postrun_bg_job" >> $ctrl_file
7985N/A echo "user: $user_name" >> $ctrl_file
8283N/A echo "class: $postrun_job_class" >> $ctrl_file
7082N/A fi
7082N/A postrun_unlock
8137N/A
8137N/A return $new_job
7082N/A}
7082N/A
7082N/Apostrun_run_command() {
7082N/A cmdout=`mktemp /tmp/postrun.out.XXXX`
7082N/A # create a background jobs script that executes the commands
7082N/A # then locks the spool/log file and appends the output to the
7082N/A # log file and finally unlocks
7082N/A cmdfile=`mktemp /tmp/postrun.job.XXXX`
7082N/A cat /dev/null > $cmdfile
7082N/A cat /dev/null > $cmdout
7082N/A echo '#!/bin/ksh' >> $cmdfile
7082N/A # copy the postrun_lock and postrun_unlock commands from
7082N/A # this script to the background job script
7082N/A echo "LOCKFILE=$LOCKFILE" >> $cmdfile
7082N/A sed -e '1,/#LOCK_UNLOCK_FUNCTIONS_START/d' \
7082N/A -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile
7082N/A # save the stdout file description
7082N/A echo 'exec 3<&1' >> $cmdfile
7082N/A echo "exec >> $cmdout 2>&1" >> $cmdfile
7082N/A echo 'PATH=/usr/bin; export PATH' >> $cmdfile
8105N/A echo 'echo Starting postrun job at `LC_ALL=C date`' >> $cmdfile
7082N/A if [ "x$postrun_submit_time" != x ]; then
7082N/A if [ $postrun_bg_job = yes ]; then
7985N/A echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile
7082N/A else
7985N/A echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile
7082N/A fi
7082N/A echo "echo Job submitted by $postrun_pkginst at $postrun_submit_time" \
7082N/A >> $cmdfile
7082N/A else
7082N/A if [ $postrun_bg_job = yes ]; then
7082N/A echo 'echo This is an immediate background job' >> $cmdfile
7082N/A else
7082N/A echo 'echo This is an immediate foreground job' >> $cmdfile
7082N/A fi
7082N/A echo "echo Job submitted by $postrun_pkginst"\
7082N/A >> $cmdfile
7082N/A fi
7082N/A echo 'echo Running commands:' >> $cmdfile
7082N/A echo "echo '>>>' commands follow:" >> $cmdfile
7083N/A cat $postrun_command_file | \
7083N/A sed -e "s/'/'\"'\"'/g" | \
7083N/A sed -e "s/^/echo '/" \
7083N/A -e "s/\$/'/" >> $cmdfile
7082N/A echo "echo '<<<' commands end" >> $cmdfile
7082N/A echo "echo '>>>' Command output follows:" >> $cmdfile
8060N/A echo "chmod 700 $postrun_command_file" >> $cmdfile
7125N/A echo "$postrun_command_file" >> $cmdfile
7985N/A
7985N/A #
7985N/A # FIXME: send email to $postrun_user if the command failed
7985N/A #
7985N/A
7082N/A echo "echo '<<<' Command completed with exit status \$?" \
7082N/A >> $cmdfile
8105N/A echo 'echo Job finished at `LC_ALL=C date`' >> $cmdfile
7082N/A echo 'echo --' >> $cmdfile
7082N/A # restore PATH in case the command changed it
7082N/A echo 'PATH=/usr/bin; export PATH' >> $cmdfile
7082N/A # restore stdout
7082N/A echo 'exec 1<&3' >> $cmdfile
7082N/A # close file descriptor 3
7082N/A echo 'exec 3<&-' >> $cmdfile
7082N/A echo 'exec 2>&1' >> $cmdfile
7082N/A # append the messages to the real log file
7082N/A # need to lock the log file to avoid 2 postrun commands
7082N/A # writing at the same time and messing up the log
7082N/A echo 'postrun_lock log' >> $cmdfile
7082N/A echo "cat $cmdout >> $LOGFILE" >> $cmdfile
7082N/A echo 'postrun_unlock log' >> $cmdfile
7082N/A echo "rm -f $cmdout" >> $cmdfile
7082N/A echo "rm -f $cmdfile" >> $cmdfile
7411N/A echo "rm -f $postrun_command_file" >> $cmdfile
8060N/A chmod 700 $cmdfile
7082N/A if [ $postrun_bg_job = yes ]; then
7082N/A $cmdfile &
7082N/A else
7082N/A $cmdfile
7082N/A fi
7082N/A exitval=$?
7082N/A}
7082N/A
7985N/Ausername=${EMAIL:-root}
7985N/A
7082N/Apostrun_defaults() {
7082N/A # default settings
7082N/A postrun_pkginst="$PKGINST"
7985N/A postrun_submit_time=''
7985N/A postrun_uniq_time=''
7082N/A postrun_is_uniq=no
7082N/A postrun_uniq_timeout=5
7082N/A postrun_bg_job=no
7985N/A postrun_command_file=''
7985N/A postrun_user=${username}
7985N/A postrun_job_number='???'
7985N/A postrun_alt_root_okay=no
8283N/A postrun_job_class=other
8283N/A postrun_no_spool=no
7082N/A}
7082N/A
7082N/A# usage: is_leap_year yyyy
7082N/Ais_leap_year() {
7082N/A cal 02 $1 | egrep -s 29 && return 0
7082N/A return 1
7082N/A}
7082N/A
7082N/A# get_abstime yy mm dd hh mm ss
7082N/A#
7082N/A# prints the elapsed time in seconds since 1970.01.01.00.00.00
7082N/A#Length of the months:
7082N/A# JA FE MA AP MY JN JL AU SE OC NO DE
7082N/Aset -A MONTH 0 31 28 31 30 31 30 31 31 30 31 30 31
7082N/Aget_abstime() {
7082N/A # the absolute time since 1970...
7082N/A t=0
7082N/A
7082N/A # number of years
7082N/A t=$(($t + ($1 - 1970) * 31536000))
7082N/A
7082N/A # add 1 day for each leap year
7082N/A y=1972
7082N/A end_y=$1
7082N/A if [ $2 -lt 2 ]; then
7082N/A end_y=$(($1 - 1))
7082N/A fi
7082N/A while [ $y -le $end_y ]; do
7082N/A is_leap_year $y && t=$(($t + 86400))
7082N/A y=$(($y + 4))
7082N/A done
7082N/A
7082N/A # number of months
7082N/A m=1
7082N/A while [ $m -lt $2 ]; do
7082N/A t=$(($t + ${MONTH[$m]} * 86400))
7082N/A m=$(($m + 1))
7082N/A done
7082N/A
7082N/A # number of days, hours, minutes and seconds:
7082N/A echo $(($t + ($3 - 1) * 86400 + $4 * 3600 + $5 * 60 + $6))
7082N/A}
7082N/A
7082N/A# get_timediff: prints the difference in seconds between 2 time strings
7082N/A# the time strings should be of the following format:
7082N/A# YYYY.MM.DD.HH.MM.SS as printed by date +%Y.%m.%d.%H.%M.%S
7082N/A#
7082N/A# Works for dates after 1970.01.01.00.00.00
7082N/A#
7082N/Aget_timediff() {
7082N/A year1=$(expr "$1" : "^\([^.]*\)\..*")
7082N/A month1=$(expr "$1" : "^[^.]*\.\([^.]*\)\..*")
7082N/A day1=$(expr "$1" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
7082N/A hour1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
7082N/A min1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
7082N/A sec1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
7082N/A
7082N/A year2=$(expr "$2" : "^\([^.]*\)\..*")
7082N/A month2=$(expr "$2" : "^[^.]*\.\([^.]*\)\..*")
7082N/A day2=$(expr "$2" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
7082N/A hour2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
7082N/A min2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
7082N/A sec2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
7082N/A
7082N/A # calculate seconds since 1970.01.01.00.00.00
7082N/A t1=`get_abstime $year1 $month1 $day1 $hour1 $min1 $sec1`
7082N/A t2=`get_abstime $year2 $month2 $day2 $hour2 $min2 $sec2`
7082N/A
7082N/A # print difference
7082N/A expr $t1 - $t2
7082N/A}
7082N/A
7082N/Apostrun_runq() {
7082N/A cd $SPOOLDIR
12025N/A IFS='
12025N/A'
8137N/A timeleft=0
7082N/A postrun_lock
12025N/A all_jobs=`/bin/ls -1 *.ctrl | /bin/sort -n`
12025N/A for job in $all_jobs; do
7091N/A test -f "$job" || continue
7082N/A postrun_defaults
7082N/A while read var val; do
7082N/A case "$var" in
7082N/A pkginst: )
7082N/A postrun_pkginst="$val"
7082N/A ;;
7082N/A submit_time: )
7082N/A postrun_submit_time="$val"
7082N/A ;;
8858N/A resubmit_time: )
8858N/A ;;
7082N/A uniq_command: )
7082N/A postrun_is_uniq="$val"
7082N/A ;;
7082N/A uniq_time: )
7082N/A postrun_uniq_time="$val"
7082N/A ;;
7082N/A uniq_timeout: )
7091N/A postrun_uniq_timeout="$val"
7082N/A ;;
7082N/A background: )
7082N/A postrun_bg_job="$val"
7082N/A ;;
7985N/A user: )
7985N/A postrun_user="$val"
7985N/A ;;
8283N/A class: )
8283N/A postrun_job_class="$val"
8283N/A ;;
7082N/A * )
7082N/A echo "postrun: WARNING: invalid setting in $job: $var"
7082N/A ;;
7082N/A esac
7082N/A done < $job
7082N/A postrun_command_file=$SPOOLDIR/`basename $job .ctrl`.cmd
7985N/A postrun_job_number=`basename $job .ctrl`
7082N/A if [ $postrun_ignore_timeout = no ]; then
7082N/A # if it's a uniq job, check if it timed out
7082N/A if [ "x$postrun_is_uniq" = xyes ]; then
7082N/A # calculate time difference (seconds)
7082N/A tdiff=$(get_timediff $(date +%Y.%m.%d.%H.%M.%S) \
7082N/A $postrun_uniq_time)
7082N/A timeout_sec=$((postrun_uniq_timeout * 60))
7091N/A if [ $tdiff -ge $timeout_sec ]; then
7082N/A postrun_run_command
7411N/A rm -f $job
8137N/A else
8137N/A # try again in at least $tdiff sec time
8137N/A tl=$(($tdiff / 60 + 1))
8137N/A if [ $tl -gt $timeleft ]; then
8137N/A timeleft=$tl
8137N/A fi
7082N/A fi
7082N/A else
7082N/A postrun_run_command
7411N/A rm -f $job
7082N/A fi
7082N/A else
7082N/A # ignore timeout, just run the job
7082N/A postrun_run_command
7411N/A rm -f $job
7082N/A fi
7082N/A done
8137N/A if [ $timeleft -gt 0 ]; then
8137N/A echo "$MYDIR/postrun -q" |
8137N/A at now "+${timeleft}minutes" \
8137N/A > /dev/null 2>&1
8137N/A fi
7082N/A postrun_unlock
7082N/A exit 0
7082N/A}
7082N/A
7082N/Apostrun_defaults
7082N/Aexitval=0
7082N/A
7082N/Apostrun_ignore_timeout=no
7082N/Aif [ $# = 1 -a "x$1" = 'x-qf' ]; then
7082N/A # postrun-runq mode (ignore timeout for uniq jobs, since this is
7082N/A # expected to be run at system boot)
7082N/A postrun_ignore_timeout=yes
7082N/A postrun_runq
7082N/A exit 1
7082N/Afi
7082N/A
7082N/Aif [ $# = 1 -a "x$1" = 'x-q' ]; then
7082N/A # postrun-runq mode, to be run from at(1)
7082N/A postrun_runq
7082N/A exit 1
7082N/Afi
7082N/A
7082N/A# process the command line
7082N/Awhile [ $# -gt 0 ]; do
7082N/A case "$1" in
7082N/A -h|-\?|--help)
7082N/A usage
7082N/A ;;
7082N/A -u|--uniq)
7082N/A postrun_is_uniq=yes
7082N/A ;;
7082N/A -b|--bg)
7082N/A postrun_bg_job=yes
7082N/A ;;
7082N/A -t|--timeout)
7082N/A opt="$1"
7082N/A if [ $# == 0 ]; then
7082N/A echo "postrun: error: argument expected after $opt"
7082N/A exit 1
7082N/A fi
7082N/A shift
7082N/A timeout=$1
7082N/A if ! is_number "$timeout"; then
7082N/A echo "postrun: error: interger number expected after $opt (found \"$timeout\")"
7082N/A exit 1
7082N/A fi
7082N/A postrun_uniq_timeout=$timeout
7082N/A ;;
8283N/A -i|--ignore)
8283N/A postrun_no_spool=yes
8283N/A ;;
7082N/A -f)
7082N/A opt="$1"
7082N/A if [ $# == 0 ]; then
7082N/A echo "postrun: error: argument expected after $opt"
7082N/A exit 1
7082N/A fi
7082N/A shift
7082N/A postrun_command_file="$1"
7082N/A ;;
8283N/A -c)
8283N/A opt="$1"
8283N/A if [ $# == 0 ]; then
8283N/A echo "postrun: error: argument expected after $opt"
8283N/A exit 1
8283N/A fi
8283N/A shift
8283N/A postrun_job_class="$1"
8283N/A ;;
7985N/A -a)
7985N/A postrun_alt_root_okay=yes
7985N/A ;;
7082N/A --)
7082N/A break
7082N/A ;;
7082N/A *)
7082N/A echo "postrun: error: invalid argument: $1"
7082N/A exit 1
7082N/A ;;
7082N/A esac
7082N/A shift
7082N/Adone
7082N/A
8426N/Acheck_alt_root_okay () {
8426N/A if [ "x$PKG_INSTALL_ROOT" = x -o "x$PKG_INSTALL_ROOT" = x/ \
8426N/A -o "x$postrun_alt_root_okay" = xno ]; then
8427N/A return 1
8426N/A fi
8426N/A # need to verify if the architecture and Solaris minor version
8426N/A # of / is equal to that of $PKG_INSTALL_ROOT, otherwise
8426N/A # running the script is not okay
8427N/A pkginfo -q SUNWsolnm || return 1
8426N/A
8427N/A this_solnm_pkginfo="`pkginfo -R / -l SUNWsolnm 2>/dev/null`"
8426N/A alt_root_solnm_pkginfo="`pkginfo -R $PKG_INSTALL_ROOT -l SUNWsolnm 2>/dev/null`"
8426N/A
8426N/A this_sol_minor=`echo "$this_solnm_pkginfo" |grep VERSION| sed -e 's/^.*VERSION: *\([0-9]*\),REV=.*/\1/'`
8426N/A alt_root_sol_minor=`echo "$alt_root_solnm_pkginfo" |grep VERSION| sed -e 's/^.*VERSION: *\([0-9]*\),REV=.*/\1/'`
8426N/A if [ "x$this_sol_minor" != "x$alt_root_sol_minor" ]; then
8426N/A postrun_debug "/ is Solaris $this_sol_minor, $PKG_INSTALL_ROOT is Solaris $alt_root_sol_minor"
8427N/A return 1
8426N/A fi
8426N/A this_sol_arch=`echo "$this_solnm_pkginfo" |grep ARCH|sed -e 's/^.*ARCH: *\([a-z0-9]*\).*/\1/'`
8426N/A alt_root_sol_arch=`echo "$alt_root_solnm_pkginfo" |grep ARCH|sed -e 's/^.*ARCH: *\([a-z0-9]*\).*/\1/'`
8426N/A if [ "x$this_sol_arch" != "x$alt_root_sol_arch" ]; then
8426N/A postrun_debug "/ is $this_sol_arch, $PKG_INSTALL_ROOT is $alt_root_sol_arch"
8427N/A return 1
8426N/A fi
8427N/A return 0
8426N/A}
8426N/A
8427N/Acheck_alt_root_okay || postrun_alt_root_okay=no
8426N/A
7082N/Aif [ "x$postrun_command_file" = x ]; then
7082N/A # save the standard input in a temporary file
8392N/A tmp_cmd_file=`postrun_mktemp /tmp/postrun.cmd.XXXX`
7082N/A cat > $tmp_cmd_file
7082N/A postrun_command_file=$tmp_cmd_file
7082N/Afi
7082N/A
7082N/Aif [ "$LUBIN" != "" ]; then
7082N/A #
7082N/A # Live Upgrade. Unsafe to run the command now.
7082N/A # Put into spool and defer to next boot.
7082N/A #
7082N/A postrun_spool_command "${@}"
7985N/Aelif [ "$PKG_INSTALL_ROOT" != "" -a "$PKG_INSTALL_ROOT" != "/" -a \
7985N/A "x$postrun_alt_root_okay" != xyes ]; then
7082N/A #
7082N/A # Installation to an alternate root directory
7082N/A # Put command into spool and defer to next boot.
7082N/A #
7082N/A postrun_spool_command "${@}"
7082N/Aelse
7082N/A #
7082N/A # Local package install. Everything's shiny happy,
7082N/A # safe to run the command right now
7082N/A #
8283N/A # Note: for alt_root_okay jobs, -u only applies to the case
8283N/A # when we have to spool the job
8283N/A if [ x$postrun_is_uniq = xyes -a x$postrun_alt_root_okay != xyes ]; then
7082N/A # don't run the command yet in case the same command is requested
7082N/A # within the next postrun_uniq_timeout minutes
8137N/A postrun_spool_command "${@}" && {
8137N/A echo "$MYDIR/postrun -q" | \
8137N/A at now "+${postrun_uniq_timeout}minutes" > /dev/null 2>&1
8137N/A }
7082N/A else
7985N/A postrun_debug "Executing commands immediately"
7082N/A postrun_run_command "${@}"
7985N/A # do not delete the tmp_cmd_file because it's the only copy of the
7985N/A # commands (since the job is not spooled)
7985N/A tmp_cmd_file=''
7082N/A fi
7082N/Afi
7082N/A
7082N/Aif [ "x$tmp_cmd_file" != x ]; then
7082N/A rm -f $tmp_cmd_file
7082N/Afi
7082N/A
7082N/Aexit $exitval