postrun revision 7082
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 10139N/A# (the "License"). You may not use this file except in compliance 10139N/A# See the License for the specific language governing permissions 10139N/A# and limitations under the License. 10139N/A# When distributing Covered Code, include this CDDL HEADER in each 10139N/A# If applicable, add the following below this CDDL HEADER, with the 10139N/A# fields enclosed by brackets "[]" replaced with your own identifying 10139N/A# information: Portions Copyright [yyyy] [name of copyright owner] 10139N/A# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved. 10139N/A# Use is subject to license terms. 10139N/A echo "postrun: error: run this script as root" 10139N/A echo 'Usage: postrun [options]' 10139N/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' 10139N/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 10139N/A # already locked by this process 10139N/A # check if the process is still running or else delete the lock 10139N/A # run false so that we enter the while loop 10139N/A # write the pid to the lock file 10139N/A # read it back in case another process also wrote it's pid there 10139N/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 10915N/A # check if there's already a spooled job for the same command 10139N/A # we found a matching spooled uniq job 10139N/A # all we need to do is update the uniq time and make sure it's 10139N/A sed -e 's/^uniq_command: .*/uniq_command: yes/' \ 10139N/A -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \ 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 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 10139N/A# JA FE MA AP MY JN JL AU SE OC NO DE 10139N/A # the absolute time since 1970... 10139N/A t=$(($t + ($1 - 1970) * 31536000)) 10139N/A # number of days, hours, minutes and seconds: 10139N/A echo $(($t + ($3 - 1) * 86400 + $4 * 3600 + $5 * 60 + $6)) 10139N/A# get_timediff: prints the difference in seconds between 2 time strings 10139N/A# the time strings should be of the following format: 11161N/A# Works for dates after 1970.01.01.00.00.00 10139N/A # calculate seconds since 1970.01.01.00.00.00 10139N/A # if it's a uniq job, check if it timed out 10139N/A # calculate time difference (seconds) 10139N/A # ignore timeout, just run the job 10139N/Aif [ $# =
1 -a
"x$1" =
'x-qf' ];
then 10139N/A # postrun-runq mode (ignore timeout for uniq jobs, since this is 10139N/A # expected to be run at system boot) 10139N/Aif [ $# =
1 -a
"x$1" =
'x-q' ];
then 10139N/A # postrun-runq mode, to be run from at(1) 10139N/A echo "postrun: error: argument expected after $opt" 10139N/A echo "postrun: error: argument expected after $opt" 10139N/A echo "postrun: error: invalid argument: $1" 10139N/A # save the standard input in a temporary file 10139N/A # Live Upgrade. Unsafe to run the command now. 10139N/A # Put into spool and defer to next boot. 10139N/A # Installation to an alternate root directory 10139N/A # Put command into spool and defer to next boot. 10139N/A # Local package install. Everything's shiny happy, 10139N/A # safe to run the command right now 10139N/A # don't run the command yet in case the same command is requested 10139N/A # within the next postrun_uniq_timeout minutes 11925N/A # run the spooled jobs in postrun_uniq_timeout minutes