postrun revision 8060
10139N/A# Script for starting a postponed post-installation command in 10139N/A# a Live-Upgrade-safe environment 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# See the License for the specific language governing permissions 13598N/A# and limitations under the License. 19958N/A# When distributing Covered Code, include this CDDL HEADER in each 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# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. 19142N/A# Use is subject to license terms. 19997N/A echo "postrun: error: run this script as root" 17240N/A echo 'Usage: postrun [options]' 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 ' Delay the execution of uniq commands by <n> minutes.' 10139N/A echo ' Run the command in the background and return control' 16575N/A echo ' Read the commands from <file> instead of the standard' 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/A # lock file exists (contains the pid of the process that locked it) 10139N/A # get the pid that holds the lock 13598N/A # is the lock held by this process? 10139N/A # check if the process is still running or else delete the stale lock 15964N/A # run false so that we enter the while loop 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 10139N/A # read it back in case another process also wrote its pid there 16745N/A # the loop will restart is the file cannot be read 10139N/A # check if this process holds the lock or else try the whole thing again 10139N/A# unlock the log file if $1 == 'log', unlock the spool otherwise 10139N/A # check if there's already a spooled job for the same command 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 # FIXME: shouldn't use sed for this, safer to simply append 14348N/A # and process the duplicate entries when reading the file 14348N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \ 14348N/A -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \ 14348N/A # FIXME: add the user name to the contol file 14348N/A # run the spooled jobs in postrun_uniq_timeout minutes 14348N/A # FIXME: kill the previous at(1) job? 10139N/A # create a background jobs script that executes the commands 10139N/A # copy the postrun_lock and postrun_unlock commands from 10139N/A # this script to the background job script 10139N/A # save the stdout file description 17240N/A echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile 16745N/A echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile 10139N/A # FIXME: send email to $postrun_user if the command failed 10139N/A echo "echo '<<<' Command completed with exit status \$?" \ 10139N/A # restore PATH in case the command changed it 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# prints the elapsed time in seconds since 1970.01.01.00.00.00 19252N/A# JA FE MA AP MY JN JL AU SE OC NO DE # 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) # ignore timeout, just run the job 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 # run the spooled jobs in 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)