postrun revision 8282
5372N/A# Script for starting a postponed post-installation command in 5372N/A# a Live-Upgrade-safe environment 5372N/A# The contents of this file are subject to the terms of the 5372N/A# Common Development and Distribution License, Version 1.0 only 5372N/A# (the "License"). You may not use this file except in compliance 5372N/A# See the License for the specific language governing permissions 5372N/A# and limitations under the License. 5372N/A# When distributing Covered Code, include this CDDL HEADER in each 5372N/A# If applicable, add the following below this CDDL HEADER, with the 5372N/A# fields enclosed by brackets "[]" replaced with your own identifying 5372N/A# information: Portions Copyright [yyyy] [name of copyright owner] 5372N/A# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. 5372N/A# Use is subject to license terms. 5372N/A echo "postrun: error: run this script as root" 5372N/A echo 'Usage: postrun [options]' 5372N/A echo ' If the same command is requested multiple times, the command' 5386N/A echo ' is only run once. If it is safe to execute the command' 5372N/A echo ' immediately, it will be delayed by 5 minutes, or as set' 5372N/A echo ' using the --timeout option' 5372N/A echo ' -t <n>, --timeout <n>' 5372N/A echo ' Delay the execution of uniq commands by <n> minutes.' 5372N/A echo ' Run the command in the background and return control' 5372N/A echo ' Read the commands from <file> instead of the standard' 5372N/A#LOCK_UNLOCK_FUNCTIONS_START 5372N/A# lock the postrun spool or log file 5372N/A# if $1 is 'log' then lock the log file, otherwise log the spool 5372N/A # lock file exists (contains the pid of the process that locked it) 5372N/A # get the pid that holds the lock 5372N/A # is the lock held by this process? 5372N/A # check if the process is still running or else it's a stale lock 5372N/A # we have a stale lock situation, let's try and take ownership 5372N/A # if we successfully took ownership of the lock, this loop 5401N/A # will read our own pid back and return 5372N/A # create a read-only lock file 5372N/A # run false so that we enter the while loop 5372N/A # write the pid to the lock file 5372N/A # recurse if failed. recursion here helps to avoid an infinite 5372N/A # loop -- ksh will kill the script after 128 attempts # read it back in case another process also wrote its pid there # the loop will restart is the file cannot be read # check if this process holds the lock or else try the whole thing again # unlock the log file if $1 == 'log', unlock the spool otherwise if [ "x$1" = xlog ]; then echo "postrun: error: cannot remove lock file $this_lock" #LOCK_UNLOCK_FUNCTIONS_END # check if there's already a spooled job for the same command postrun_debug "matching spooled uniq job (#${uniq_job_nr}) found" # we found a matching spooled uniq job # all we need to do is update the uniq time and make sure it's # FIXME: shouldn't use sed for this, safer to simply append # and process the duplicate entries when reading the file sed -e 's/^uniq_command: .*/uniq_command: yes/' \ -e 's/^\(pkginst: .*\)/\1, '$PKGINST'/' \ -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \ # FIXME: add the user name to the control file # create a background jobs script that executes the commands # then locks the spool/log file and appends the output to the # log file and finally unlocks # copy the postrun_lock and postrun_unlock commands from # this script to the background job script sed -e '1,/#LOCK_UNLOCK_FUNCTIONS_START/d' \ -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile # save the stdout file description echo 'echo Starting postrun job at `LC_ALL=C date`' >> $cmdfile echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile echo 'echo This is an immediate background job' >> $cmdfile echo 'echo This is an immediate foreground job' >> $cmdfile echo 'echo Running commands:' >> $cmdfile echo "echo '>>>' commands follow:" >> $cmdfile sed -e "s/'/'\"'\"'/g" | \ echo "echo '<<<' commands end" >> $cmdfile echo "echo '>>>' Command output follows:" >> $cmdfile # FIXME: send email to $postrun_user if the command failed echo "echo '<<<' Command completed with exit status \$?" \ echo 'echo Job finished at `LC_ALL=C date`' >> $cmdfile # restore PATH in case the command changed it # close file descriptor 3 # append the messages to the real log file # need to lock the log file to avoid 2 postrun commands # writing at the same time and messing up the log # usage: is_leap_year yyyy # get_abstime yy mm dd hh mm ss # prints the elapsed time in seconds since 1970.01.01.00.00.00 # JA FE MA AP MY JN JL AU SE OC NO DE set -A
MONTH 0 31 28 31 30 31 30 31 31 30 31 30 31 # the absolute time since 1970... t=$(($t + ($1 - 1970) * 31536000)) # add 1 day for each leap year t=$(($t + ${MONTH[$m]} * 86400)) # 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: # Works for dates after 1970.01.01.00.00.00 day1=$(expr "$1" : "^[^.]*\.[^.]*\.\([^.]*\)\..*") hour1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*") min1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*") sec1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)") day2=$(expr "$2" : "^[^.]*\.[^.]*\.\([^.]*\)\..*") hour2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*") min2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*") sec2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)") # calculate seconds since 1970.01.01.00.00.00 test -f "$job" || continue echo "postrun: WARNING: invalid setting in $job: $var" # if it's a uniq job, check if it timed out # calculate time difference (seconds) # try again in at least $tdiff sec time # ignore timeout, just run the job at now "+${timeleft}minutes" \ 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) if [ $# =
1 -a
"x$1" =
'x-q' ];
then # postrun-runq mode, to be run from at(1) # process the command line echo "postrun: error: argument expected after $opt" echo "postrun: error: interger number expected after $opt (found \"$timeout\")" echo "postrun: error: argument expected after $opt" echo "postrun: error: invalid argument: $1" # save the standard input in a temporary file # Live Upgrade. Unsafe to run the command now. # Put into spool and defer to next boot. # Installation to an alternate root directory # Put command into spool and defer to next boot. # Local package install. Everything's shiny happy, # safe to run the command right now # don't run the command yet in case the same command is requested # within the next postrun_uniq_timeout minutes # do not delete the tmp_cmd_file because it's the only copy of the # commands (since the job is not spooled)