postrun revision 7091
7082N/A# Script for starting a postponed post-installation command in 7082N/A# a Live-Upgrade-safe environment 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# See the License for the specific language governing permissions 7082N/A# and limitations under the License. 7082N/A# When distributing Covered Code, include this CDDL HEADER in each 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# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. 7082N/A# Use is subject to license terms. 7082N/A echo "postrun: error: run this script as root" 7082N/A echo 'Usage: postrun [options]' 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 ' -t <n>, --timeout <n>' 7082N/A echo ' Delay the execution of uniq commands by <n> minutes.' 7082N/A echo ' Run the command in the background and return control' 7082N/A echo ' Read the commands from <file> instead of the standard' 7082N/A#LOCK_UNLOCK_FUNCTIONS_START 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/A # lock file exists (contains the pid of the process that locked it 7082N/A # get the pid that holds the lock 7082N/A # already locked by this process 7082N/A # check if the process is still running or else delete the lock 7082N/A # run false so that we enter the while loop 7082N/A # write the pid to the lock file 7082N/A # read it back in case another process also wrote it's pid there 7082N/A # the loop will restart is the file cannot be read 7082N/A # check if this process holds the lock or else try the whole thing again 7082N/A# unlock the log file if $1 == 'log', unlock the spool otherwise 7082N/A # check if there's already a spooled job for the same command 7082N/A # we found a matching spooled uniq job 7082N/A # all we need to do is update the uniq time and make sure it's 7082N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \ 7082N/A -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \ 7091N/A # run the spooled jobs in postrun_uniq_timeout minutes 7082N/A # create a background jobs script that executes the commands 7082N/A # log file and finally unlocks 7082N/A # copy the postrun_lock and postrun_unlock commands from 7082N/A # this script to the background job script 7082N/A sed -e '1,/#LOCK_UNLOCK_FUNCTIONS_START/d' \ 7082N/A # save the stdout file description 7082N/A echo "echo '<<<' Command completed with exit status \$?" \ 7082N/A # restore PATH in case the command changed it 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# get_abstime yy mm dd hh mm ss 7082N/A# prints the elapsed time in seconds since 1970.01.01.00.00.00 7082N/A# JA FE MA AP MY JN JL AU SE OC NO DE 7082N/A # the absolute time since 1970... 7082N/A t=$(($t + ($1 - 1970) * 31536000)) 7082N/A # add 1 day for each leap year 7082N/A # number of days, hours, minutes and seconds: 7082N/A echo $(($t + ($3 - 1) * 86400 + $4 * 3600 + $5 * 60 + $6)) 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# Works for dates after 1970.01.01.00.00.00 7082N/A sec1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)") 7082N/A sec2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)") 7082N/A # calculate seconds since 1970.01.01.00.00.00 7082N/A # if it's a uniq job, check if it timed out 7082N/A # calculate time difference (seconds) 7082N/A # ignore timeout, just run the job 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/Aif [ $# =
1 -a
"x$1" =
'x-q' ];
then 7082N/A # postrun-runq mode, to be run from at(1) 7082N/A echo "postrun: error: argument expected after $opt" 7082N/A echo "postrun: error: argument expected after $opt" 7082N/A echo "postrun: error: invalid argument: $1" 7082N/A # save the standard input in a temporary file 7082N/A # Live Upgrade. Unsafe to run the command now. 7082N/A # Put into spool and defer to next boot. 7082N/A # Installation to an alternate root directory 7082N/A # Put command into spool and defer to next boot. 7082N/A # Local package install. Everything's shiny happy, 7082N/A # safe to run the command right now 7082N/A # don't run the command yet in case the same command is requested 7082N/A # within the next postrun_uniq_timeout minutes 7082N/A # run the spooled jobs in postrun_uniq_timeout minutes