postrun revision 12025
12373N/A# Script for starting a postponed post-installation command in 12586N/A# a Live-Upgrade-safe environment 15170N/A# The contents of this file are subject to the terms of the 12586N/A# Common Development and Distribution License, Version 1.0 only 12586N/A# (the "License"). You may not use this file except in compliance 14471N/A# See the License for the specific language governing permissions 12373N/A# and limitations under the License. 12373N/A# When distributing Covered Code, include this CDDL HEADER in each 14471N/A# If applicable, add the following below this CDDL HEADER, with the 12773N/A# fields enclosed by brackets "[]" replaced with your own identifying 13320N/A# information: Portions Copyright [yyyy] [name of copyright owner] 13350N/A# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. 13350N/A# Use is subject to license terms. 15220N/A echo "postrun: error: run this script as root" 12373N/A echo 'Usage: postrun [options]' 12373N/A echo ' If the same command is requested multiple times, the command' 12373N/A echo ' is only run once. If it is safe to execute the command' 12373N/A echo ' immediately, it will be delayed by 5 minutes, or as set' 12373N/A echo ' using the --timeout option' 12373N/A echo ' Delay the execution of uniq commands by <n> minutes.' 12373N/A echo ' Run the command in the background and return control' 12373N/A echo ' Read the commands from <file> instead of the standard' 12373N/A echo ' Assign this job to class <class>. Useful for querying' 12373N/A echo ' jobs with postrun-query' 12373N/A echo ' Ignore this job if it cannot be executed immediately.' 12373N/A# Solaris 9 doesn't have mktemp, need a substitute in order to be 12373N/A# Note: only creating tmp files is implemented, directories are not 12373N/A# lock the postrun spool or log file 12373N/A# if $1 is 'log' then lock the log file, otherwise log the spool 12773N/A # Read who has the lock. If this process has the lock return 12773N/A | nawk -F' -> ' '$1 ~ /^l/ && NF == 2 { print $NF }') 12373N/A # check to be sure the process that holds the lock is still running 12783N/A # if so, wait for it to be freed. If the lock is stale, remove it 12373N/A # so that the next iteration of the loop can take the lock. 12373N/A# unlock the log file if $1 == 'log', unlock the spool otherwise 12373N/A | nawk -F' -> ' '$1 ~ /^l/ && NF == 2 { print $NF }') 14413N/A # check if there's already a spooled job for the same command 13350N/A # we found a matching spooled uniq job 13320N/A # we need to update the uniq time and make sure it's 13320N/A # FIXME: shouldn't use sed for this, safer to simply append 13320N/A # and process the duplicate entries when reading the file 13307N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \ 13085N/A -e 's/^uniq_time: /resubmit_time: /' \ 12783N/A # FIXME: add the user name to the control file 12783N/A # Use a new job id so that the job moves to the end of the queue. 12704N/A # Need to do this such a whay that if this process is interrupted at 12704N/A # any stage, the job is not lost and is not in the queue twice: 12666N/A # Step 1: copy the job to the new id: 12666N/A # Step 2: move the original job ctrl file to the new id: 12611N/A # Step 3: replace the original job ctrl file with the updated one: 12514N/A # Step 4: delete the original cmd file 12373N/A # create a background jobs script that executes the commands 12373N/A # copy the postrun_lock and postrun_unlock commands from 12373N/A # this script to the background job script 12373N/A # save the stdout file description 12373N/A echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile 12373N/A echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile 12373N/A # FIXME: send email to $postrun_user if the command failed 12373N/A echo "echo '<<<' Command completed with exit status \$?" \ 12373N/A # restore PATH in case the command changed it 12373N/A # append the messages to the real log file 12373N/A # need to lock the log file to avoid 2 postrun commands 12373N/A # 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: argument expected after $opt" echo "postrun: error: invalid argument: $1" # need to verify if the architecture and Solaris minor version # of / is equal to that of $PKG_INSTALL_ROOT, otherwise # running the script is not okay # 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 # Note: for alt_root_okay jobs, -u only applies to the case # when we have to spool the job # 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)