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. 8105N/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' 8283N/A echo ' Assign this job to class <class>. Useful for querying' 8283N/A echo ' jobs with postrun-query' 8283N/A echo ' Ignore this job if it cannot be executed immediately.' 8392N/A# Solaris 9 doesn't have mktemp, need a substitute in order to be 8409N/A# Note: only creating tmp files is implemented, directories are not 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 9009N/A # Read who has the lock. If this process has the lock return 9009N/A | nawk -F' -> ' '$1 ~ /^l/ && NF == 2 { print $NF }') 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. 7082N/A# unlock the log file if $1 == 'log', unlock the spool otherwise 9009N/A | nawk -F' -> ' '$1 ~ /^l/ && NF == 2 { print $NF }') 7082N/A # check if there's already a spooled job for the same command 7082N/A # we found a matching spooled uniq job 8858N/A # we need to update the uniq time and make sure it's 7985N/A # FIXME: shouldn't use sed for this, safer to simply append 7985N/A # and process the duplicate entries when reading the file 7082N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \ 8858N/A -e 's/^uniq_time: /resubmit_time: /' \ 8137N/A # FIXME: add the user name to the control file 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 # Step 1: copy the job to the new id: 8858N/A # Step 2: move the original job ctrl file to the new id: 8858N/A # Step 3: replace the original job ctrl file with the updated one: 8858N/A # Step 4: delete the original cmd file 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 7985N/A echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile 7985N/A echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile 7985N/A # FIXME: send email to $postrun_user if the command failed 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) 8137N/A # try again in at least $tdiff sec time 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" 8283N/A echo "postrun: error: argument expected after $opt" 7082N/A echo "postrun: error: invalid argument: $1" 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 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 8283N/A # Note: for alt_root_okay jobs, -u only applies to the case 8283N/A # when we have to spool the job 7082N/A # don't run the command yet in case the same command is requested 7082N/A # within the next postrun_uniq_timeout minutes 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)