postrun revision 8392
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Script for starting a postponed post-installation command in
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# a Live-Upgrade-safe environment
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# CDDL HEADER START
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# The contents of this file are subject to the terms of the
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Common Development and Distribution License, Version 1.0 only
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# (the "License"). You may not use this file except in compliance
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# with the License.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# See the License for the specific language governing permissions
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# and limitations under the License.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# When distributing Covered Code, include this CDDL HEADER in each
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# If applicable, add the following below this CDDL HEADER, with the
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# fields enclosed by brackets "[]" replaced with your own identifying
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# information: Portions Copyright [yyyy] [name of copyright owner]
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# CDDL HEADER END
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Use is subject to license terms.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley myuid=`id | sed -e 's/.* euid=\([0-9][0-9]*\).*$/\1/'` || \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley myuid=`id | sed -e 's/uid=\([0-9][0-9]*\).*/\1/'`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "postrun: error: run this script as root"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' If the same command is requested multiple times, the command'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' is only run once. If it is safe to execute the command'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' immediately, it will be delayed by 5 minutes, or as set'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Delay the execution of uniq commands by <n> minutes.'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Run the command in the background and return control'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Read the commands from <file> instead of the standard'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Assign this job to class <class>. Useful for querying'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Ignore this job if it cannot be executed immediately.'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Solaris 9 doesn't have mktemp, need a substitute in order to be
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Live Upgrade compliant
0352c371e743d8dae996123f658b5d32c677614eYassir Elley mkdir -p $tempname && chmod 700 $tempname || exit 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#LOCK_UNLOCK_FUNCTIONS_START
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# lock the postrun spool or log file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# if $1 is 'log' then lock the log file, otherwise log the spool
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # lock file exists (contains the pid of the process that locked it)
0352c371e743d8dae996123f658b5d32c677614eYassir Elley while test -f $this_lock; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # check if the process is still running or else it's a stale lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley ps -g $pid -o 'pid' | egrep -s "$pid\$" 2>&1 || {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # we have a stale lock situation, let's try and take ownership
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # if we successfully took ownership of the lock, this loop
0352c371e743d8dae996123f658b5d32c677614eYassir Elley while [ $? != 0 ]; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # recurse if failed. recursion here helps to avoid an infinite
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # loop -- ksh will kill the script after 128 attempts
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # read it back in case another process also wrote its pid there
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # the loop will restart is the file cannot be read
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # check if this process holds the lock or else try the whole thing again
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# release the lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# unlock the log file if $1 == 'log', unlock the spool otherwise
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if ! rm -f $this_lock; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "postrun: error: cannot remove lock file $this_lock"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#LOCK_UNLOCK_FUNCTIONS_END
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# get the next job id
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_debug "Ignoring job, because -i was used and it's not possible to run it now"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # check if there's already a spooled job for the same command
0352c371e743d8dae996123f658b5d32c677614eYassir Elley egrep -s '^uniq_command: yes' `basename $f .cmd`.ctrl && {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_debug "matching spooled uniq job (#${uniq_job_nr}) found"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # all we need to do is update the uniq time and make sure it's
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # FIXME: shouldn't use sed for this, safer to simply append
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # and process the duplicate entries when reading the file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley sed -e 's/^uniq_command: .*/uniq_command: yes/' \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley -e 's/^uniq_timeout: .*/uniq_timeout: '"$postrun_uniq_timeout"'/' \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley grep "^class: $postrun_job_class$" $uniq_job_nr.ctrl.new >/dev/null || \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "class: $postrun_job_class" >> $uniq_job_nr.ctrl.new
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_debug "spooling command as job #${job_seq}"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "submit_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "uniq_command: $postrun_is_uniq" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "uniq_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "uniq_timeout: $postrun_uniq_timeout" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "background: $postrun_bg_job" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # create a background jobs script that executes the commands
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # then locks the spool/log file and appends the output to the
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # copy the postrun_lock and postrun_unlock commands from
0352c371e743d8dae996123f658b5d32c677614eYassir Elley -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo Starting postrun job at `LC_ALL=C date`' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo Job submitted by $postrun_pkginst at $postrun_submit_time" \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo This is an immediate background job' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo This is an immediate foreground job' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo '>>>' Command output follows:" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "chmod 700 $postrun_command_file" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # FIXME: send email to $postrun_user if the command failed
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo '<<<' Command completed with exit status \$?" \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo Job finished at `LC_ALL=C date`' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # need to lock the log file to avoid 2 postrun commands
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # writing at the same time and messing up the log
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek# usage: is_leap_year yyyy
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cal 02 $1 | egrep -s 29 && return 0
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek# get_abstime yy mm dd hh mm ss
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek# prints the elapsed time in seconds since 1970.01.01.00.00.00
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek#Length of the months:
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek# JA FE MA AP MY JN JL AU SE OC NO DE
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyset -A MONTH 0 31 28 31 30 31 30 31 31 30 31 30 31
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $2 -lt 2 ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley while [ $y -le $end_y ]; do
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek while [ $m -lt $2 ]; do
# YYYY.MM.DD.HH.MM.SS as printed by date +%Y.%m.%d.%H.%M.%S
cd $SPOOLDIR
timeleft=0
pkginst: )
submit_time: )
uniq_command: )
uniq_time: )
uniq_timeout: )
background: )
user: )
class: )
done < $job
if [ $postrun_ignore_timeout = no ]; then
if [ $tdiff -ge $timeout_sec ]; then
-h|-\?|--help)
-u|--uniq)
-t|--timeout)
echo "postrun: error: argument expected after $opt"
-i|--ignore)
echo "postrun: error: argument expected after $opt"
echo "postrun: error: argument expected after $opt"
if [ "x$postrun_command_file" = x ]; then
if [ "x$tmp_cmd_file" != x ]; then
exit $exitval