postrun revision 8282
e6d40133bc9f858308654afb1262b8b483ec5922Till Mossakowski# Script for starting a postponed post-installation command in
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder# a Live-Upgrade-safe environment
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder# CDDL HEADER START
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# The contents of this file are subject to the terms of the
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# Common Development and Distribution License, Version 1.0 only
e6d40133bc9f858308654afb1262b8b483ec5922Till Mossakowski# (the "License"). You may not use this file except in compliance
49588f3d624e56594d888bc622bc90618ae3c2c5Till Mossakowski# with the License.
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
49588f3d624e56594d888bc622bc90618ae3c2c5Till Mossakowski# or http://www.opensolaris.org/os/licensing.
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# See the License for the specific language governing permissions
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# and limitations under the License.
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# When distributing Covered Code, include this CDDL HEADER in each
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# If applicable, add the following below this CDDL HEADER, with the
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# fields enclosed by brackets "[]" replaced with your own identifying
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# information: Portions Copyright [yyyy] [name of copyright owner]
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# CDDL HEADER END
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
57d320fc4d0fe1a1c08cfe6cd9ebec09b86c2cbfTill Mossakowski# Use is subject to license terms.
0206ab93ef846e4e0885996d052b9b73b9dc66b0Christian Maeder myuid=`id | sed -e 's/.* euid=\([0-9][0-9]*\).*$/\1/'` || \
0206ab93ef846e4e0885996d052b9b73b9dc66b0Christian Maeder myuid=`id | sed -e 's/uid=\([0-9][0-9]*\).*/\1/'`
0206ab93ef846e4e0885996d052b9b73b9dc66b0Christian Maeder echo "postrun: error: run this script as root"
0206ab93ef846e4e0885996d052b9b73b9dc66b0Christian Maeder echo ' If the same command is requested multiple times, the command'
2272b992302eb61b2a039033cb8cdaf7809fe682Christian Maeder echo ' is only run once. If it is safe to execute the command'
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder echo ' immediately, it will be delayed by 5 minutes, or as set'
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder echo ' Delay the execution of uniq commands by <n> minutes.'
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo ' Run the command in the background and return control'
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo ' Read the commands from <file> instead of the standard'
2de8dfc30c926ee27254bfa32230a01435530efeKlaus Luettich#LOCK_UNLOCK_FUNCTIONS_START
ef7d1a1d5454458d46b9acefeda94b12bdc695b2Christian Maeder# lock the postrun spool or log file
ef7d1a1d5454458d46b9acefeda94b12bdc695b2Christian Maeder# if $1 is 'log' then lock the log file, otherwise log the spool
f64f3dc78de82101483fe97bf109a42ca4d59d77Klaus Luettich # lock file exists (contains the pid of the process that locked it)
2de8dfc30c926ee27254bfa32230a01435530efeKlaus Luettich while test -f $this_lock; do
7bf4436b6f9987b070033a323757b206c898c1beChristian Maeder pid=`cat $this_lock 2>/dev/null` || continue
7bf4436b6f9987b070033a323757b206c898c1beChristian Maeder # check if the process is still running or else it's a stale lock
ef7d1a1d5454458d46b9acefeda94b12bdc695b2Christian Maeder ps -g $pid -o 'pid' | egrep -s "$pid\$" 2>&1 || {
ef7d1a1d5454458d46b9acefeda94b12bdc695b2Christian Maeder # we have a stale lock situation, let's try and take ownership
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder # if we successfully took ownership of the lock, this loop
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # run false so that we enter the while loop
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder while [ $? != 0 ]; do
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # recurse if failed. recursion here helps to avoid an infinite
9603ad7198b72e812688ad7970e4eac4b553837aKlaus Luettich # loop -- ksh will kill the script after 128 attempts
f64f3dc78de82101483fe97bf109a42ca4d59d77Klaus Luettich # read it back in case another process also wrote its pid there
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder # the loop will restart is the file cannot be read
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # check if this process holds the lock or else try the whole thing again
f64f3dc78de82101483fe97bf109a42ca4d59d77Klaus Luettich# release the lock
f64f3dc78de82101483fe97bf109a42ca4d59d77Klaus Luettich# unlock the log file if $1 == 'log', unlock the spool otherwise
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder echo "postrun: error: cannot remove lock file $this_lock"
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder#LOCK_UNLOCK_FUNCTIONS_END
9603ad7198b72e812688ad7970e4eac4b553837aKlaus Luettich# get the next job id
7bf4436b6f9987b070033a323757b206c898c1beChristian Maeder # check if there's already a spooled job for the same command
4e2331b387b90a234dc36b12c778914d3e202718Christian Maeder egrep -s '^uniq_command: yes' `basename $f .cmd`.ctrl && {
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich postrun_debug "matching spooled uniq job (#${uniq_job_nr}) found"
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # all we need to do is update the uniq time and make sure it's
0ee3933098d65199ae39ea41cfc62634226dad15Klaus Luettich # FIXME: shouldn't use sed for this, safer to simply append
7bf4436b6f9987b070033a323757b206c898c1beChristian Maeder # and process the duplicate entries when reading the file
709653bffee501341e2fdc55b9223e4921047c65Till Mossakowski sed -e 's/^uniq_command: .*/uniq_command: yes/' \
6a9b85953df4e29a996536ffc7dbf7ef9dbc64c7Cui Jian -e 's/^uniq_time: .*/uniq_time: '`date +%Y.%m.%d.%H.%M.%S`'/' \
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder -e 's/^uniq_timeout: .*/uniq_timeout: '"$postrun_uniq_timeout"'/' \
57d320fc4d0fe1a1c08cfe6cd9ebec09b86c2cbfTill Mossakowski # FIXME: add the user name to the control file
04d04d19fdd5320953c78ad5b6d2d11f85bc4bcfChristian Maeder postrun_debug "spooling command as job #${job_seq}"
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo "submit_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo "uniq_command: $postrun_is_uniq" >> $ctrl_file
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo "uniq_time: `date +%Y.%m.%d.%H.%M.%S`" >> $ctrl_file
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo "uniq_timeout: $postrun_uniq_timeout" >> $ctrl_file
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder echo "background: $postrun_bg_job" >> $ctrl_file
e550da81778d64d06c7950b1d578a1cc307ee280Klaus Luettich # create a background jobs script that executes the commands
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich # then locks the spool/log file and appends the output to the
e550da81778d64d06c7950b1d578a1cc307ee280Klaus Luettich # copy the postrun_lock and postrun_unlock commands from
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo 'PATH=/usr/bin; export PATH' >> $cmdfile
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo 'echo Starting postrun job at `LC_ALL=C date`' >> $cmdfile
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich echo "echo Job submitted by $postrun_pkginst at $postrun_submit_time" \
5ef395ccb7c654b00f393715176a0c8f9ae69b77Klaus Luettich echo 'echo This is an immediate background job' >> $cmdfile
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder echo 'echo This is an immediate foreground job' >> $cmdfile
5ef395ccb7c654b00f393715176a0c8f9ae69b77Klaus Luettich echo "echo Job submitted by $postrun_pkginst"\
5ef395ccb7c654b00f393715176a0c8f9ae69b77Klaus Luettich echo "echo '>>>' commands follow:" >> $cmdfile
5ef395ccb7c654b00f393715176a0c8f9ae69b77Klaus Luettich echo "echo '>>>' Command output follows:" >> $cmdfile
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder echo "chmod 700 $postrun_command_file" >> $cmdfile
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # FIXME: send email to $postrun_user if the command failed
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder echo "echo '<<<' Command completed with exit status \$?" \
33c33fde308de14d34177617a28524312f5f0ad8Christian Maeder echo 'echo Job finished at `LC_ALL=C date`' >> $cmdfile
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich # restore PATH in case the command changed it
7f7460e7095628f3437b116ee78d3043d11f8febChristian Maeder echo 'PATH=/usr/bin; export PATH' >> $cmdfile
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # need to lock the log file to avoid 2 postrun commands
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder # writing at the same time and messing up the log
833baa690207430f9cc3ca599039954a7840fa30Klaus Luettich echo "rm -f $postrun_command_file" >> $cmdfile
5fcb1cb8c190e9bfb8d5c06c2e7d7a4b65f361acKlaus Luettich# usage: is_leap_year yyyy
eac3174ea16c143bfaeb3f2e2103a11a2f162c6cChristian Maeder cal 02 $1 | egrep -s 29 && return 0
end_y=$1
if [ $2 -lt 2 ]; then
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: )
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"
echo "postrun: error: argument expected after $opt"
if [ "x$postrun_command_file" = x ]; then
if [ "x$tmp_cmd_file" != x ]; then
exit $exitval