postrun revision 8392
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#!/bin/ksh
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Script for starting a postponed post-installation command in
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# a Live-Upgrade-safe environment
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# CDDL HEADER START
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
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#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# or http://www.opensolaris.org/os/licensing.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# See the License for the specific language governing permissions
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# and limitations under the License.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
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#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# CDDL HEADER END
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Use is subject to license terms.
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyexport PATH=/usr/bin
0352c371e743d8dae996123f658b5d32c677614eYassir ElleyLC_ALL=C
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyexport LC_ALL
0352c371e743d8dae996123f658b5d32c677614eYassir ElleyMYDIR=$(cd $(dirname $0); pwd)
0352c371e743d8dae996123f658b5d32c677614eYassir ElleySPOOLDIR="$MYDIR/../../spool/postrun"
0352c371e743d8dae996123f658b5d32c677614eYassir ElleyLOCKFILE="$SPOOLDIR/.lock"
0352c371e743d8dae996123f658b5d32c677614eYassir ElleyLOGFILE="$MYDIR/../../log/postrun.log"
0352c371e743d8dae996123f658b5d32c677614eYassir ElleySEQFILE="$SPOOLDIR/.seq"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyid | grep " euid=" && \
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/'`
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyif [ "$myuid" != 0 ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "postrun: error: run this script as root"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley exit 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyfi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyumask 0133
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyusage() {
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek echo 'Usage: postrun [options]'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'Options:'
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek echo ' -u, --uniq'
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'
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek echo ' using the --timeout option'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' -t <n>, --timeout <n>'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Delay the execution of uniq commands by <n> minutes.'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' -b, --bg'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Run the command in the background and return control'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' immediately'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' -f <file>'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Read the commands from <file> instead of the standard'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' input.'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' -c <class>'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Assign this job to class <class>. Useful for querying'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' jobs with postrun-query'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' -i'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Ignore this job if it cannot be executed immediately.'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' -h, -?, --help'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo ' Display this help'
0352c371e743d8dae996123f658b5d32c677614eYassir Elley exit 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_debug() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ "x$POSTRUN_DEBUG" = xyes ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley for msg in "${@}"; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo '<POSTRUN_DEBUG>' "$msg" 1>&2
0352c371e743d8dae996123f658b5d32c677614eYassir Elley done
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Solaris 9 doesn't have mktemp, need a substitute in order to be
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# Live Upgrade compliant
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_mktemp () {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley test -x /usr/bin/mktemp && {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley /usr/bin/mktemp $1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley return
0352c371e743d8dae996123f658b5d32c677614eYassir Elley }
0352c371e743d8dae996123f658b5d32c677614eYassir Elley tempname="$1.$$.`date +%H%M%S`"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley mkdir -p $tempname && chmod 700 $tempname || exit 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo $tempname
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#LOCK_UNLOCK_FUNCTIONS_START
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyis_number() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "$1" | egrep -vs '^[0-9]+$' && return 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "$1" | egrep -s '^[0-9]+$' || return 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley return 0
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
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 Elleypostrun_lock() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley this_lock=$LOCKFILE
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ "x$1" = xlog ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley this_lock=${LOCKFILE}.log
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # lock file exists (contains the pid of the process that locked it)
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley while test -f $this_lock; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # get the pid that holds the lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley pid=`cat $this_lock 2>/dev/null` || continue
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # is the lock held by this process?
0352c371e743d8dae996123f658b5d32c677614eYassir Elley test "$pid" == "$$" && return
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # check if the process is still running or else it's a stale lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley is_number "$pid" && {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley ps -g $pid -o 'pid' | egrep -s "$pid\$" 2>&1 || {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley sleep 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_lock $1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley return
0352c371e743d8dae996123f658b5d32c677614eYassir Elley }
0352c371e743d8dae996123f658b5d32c677614eYassir Elley }
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # we have a stale lock situation, let's try and take ownership
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # of the lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley chmod 644 ${this_lock}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "$$" > ${this_lock}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley chmod 444 ${this_lock}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # if we successfully took ownership of the lock, this loop
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # will read our own pid back and return
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # otherwise
0352c371e743d8dae996123f658b5d32c677614eYassir Elley done
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # create a read-only lock file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley umask 0333
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # run false so that we enter the while loop
0352c371e743d8dae996123f658b5d32c677614eYassir Elley false
0352c371e743d8dae996123f658b5d32c677614eYassir Elley while [ $? != 0 ]; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # write the pid to the lock file
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 echo "$$" > $this_lock || postrun_lock $1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # read it back in case another process also wrote its pid there
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # in the meantime
0352c371e743d8dae996123f658b5d32c677614eYassir Elley pid=`cat $this_lock 2>/dev/null`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # the loop will restart is the file cannot be read
0352c371e743d8dae996123f658b5d32c677614eYassir Elley done
0352c371e743d8dae996123f658b5d32c677614eYassir Elley umask 0133
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # check if this process holds the lock or else try the whole thing again
0352c371e743d8dae996123f658b5d32c677614eYassir Elley test "$pid" == "$$" || postrun_lock $1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# release the lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# unlock the log file if $1 == 'log', unlock the spool otherwise
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_unlock() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley this_lock=$LOCKFILE
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ "x$1" = xlog ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley this_lock=${LOCKFILE}.log
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if ! rm -f $this_lock; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "postrun: error: cannot remove lock file $this_lock"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley exit 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley#LOCK_UNLOCK_FUNCTIONS_END
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley# get the next job id
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_get_seq() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley seq=`cat $SEQFILE 2>/dev/null`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley next_seq=$(($seq + 1))
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo $next_seq > $SEQFILE
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_unlock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo $next_seq
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_spool_command() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $postrun_no_spool = yes ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_debug "Ignoring job, because -i was used and it's not possible to run it now"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley return 1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cd $SPOOLDIR
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # check if there's already a spooled job for the same command
0352c371e743d8dae996123f658b5d32c677614eYassir Elley new_job=0
0352c371e743d8dae996123f658b5d32c677614eYassir Elley uniq_job_nr=
0352c371e743d8dae996123f658b5d32c677614eYassir Elley IFS=' '
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley for f in *.cmd; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley test -f "$f" || continue
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cmp -s $postrun_command_file $f && {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $postrun_is_uniq = yes ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley uniq_job_nr=`basename $f .cmd`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley break
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley egrep -s '^uniq_command: yes' `basename $f .cmd`.ctrl && {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley uniq_job_nr=`basename $f .cmd`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley break
0352c371e743d8dae996123f658b5d32c677614eYassir Elley }
0352c371e743d8dae996123f658b5d32c677614eYassir Elley }
0352c371e743d8dae996123f658b5d32c677614eYassir Elley done
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ "x$uniq_job_nr" != x ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_debug "matching spooled uniq job (#${uniq_job_nr}) found"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # we found a matching spooled uniq job
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # all we need to do is update the uniq time and make sure it's
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # flagged as a uniq job
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley new_job=1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley #
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 #
0352c371e743d8dae996123f658b5d32c677614eYassir Elley sed -e 's/^uniq_command: .*/uniq_command: yes/' \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley -e 's/^\(pkginst: .*\)/\1, '$PKGINST'/' \
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 $uniq_job_nr.ctrl > $uniq_job_nr.ctrl.new
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 #
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # FIXME: add the user name to the control file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley #
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley mv $uniq_job_nr.ctrl.new $uniq_job_nr.ctrl
0352c371e743d8dae996123f658b5d32c677614eYassir Elley else
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_unlock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley job_seq=`postrun_get_seq`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_debug "spooling command as job #${job_seq}"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_lock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley user_name=${EMAIL:-root}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley ctrl_file="$SPOOLDIR/$job_seq.ctrl"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cmd_file="$SPOOLDIR/$job_seq.cmd"
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cat $postrun_command_file > $cmd_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cat /dev/null > $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "pkginst: $PKGINST" >> $ctrl_file
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 echo "user: $user_name" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "class: $postrun_job_class" >> $ctrl_file
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley postrun_unlock
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley return $new_job
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_run_command() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cmdout=`mktemp /tmp/postrun.out.XXXX`
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 # log file and finally unlocks
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cmdfile=`mktemp /tmp/postrun.job.XXXX`
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cat /dev/null > $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cat /dev/null > $cmdout
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo '#!/bin/ksh' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # copy the postrun_lock and postrun_unlock commands from
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # this script to the background job script
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "LOCKFILE=$LOCKFILE" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley sed -e '1,/#LOCK_UNLOCK_FUNCTIONS_START/d' \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley -e '/#LOCK_UNLOCK_FUNCTIONS_END/,$d' $0 >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # save the stdout file description
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'exec 3<&1' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "exec >> $cmdout 2>&1" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'PATH=/usr/bin; export PATH' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo Starting postrun job at `LC_ALL=C date`' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ "x$postrun_submit_time" != x ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $postrun_bg_job = yes ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo 'This is a spooled background job (#${postrun_job_number})'" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley else
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo 'This is a spooled foreground job (#${postrun_job_number})'" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo Job submitted by $postrun_pkginst at $postrun_submit_time" \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley else
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $postrun_bg_job = yes ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo This is an immediate background job' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley else
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo This is an immediate foreground job' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo Job submitted by $postrun_pkginst"\
0352c371e743d8dae996123f658b5d32c677614eYassir Elley >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo Running commands:' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo '>>>' commands follow:" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley cat $postrun_command_file | \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley sed -e "s/'/'\"'\"'/g" | \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley sed -e "s/^/echo '/" \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley -e "s/\$/'/" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo '<<<' commands end" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo '>>>' Command output follows:" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "chmod 700 $postrun_command_file" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "$postrun_command_file" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley #
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # FIXME: send email to $postrun_user if the command failed
0352c371e743d8dae996123f658b5d32c677614eYassir Elley #
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "echo '<<<' Command completed with exit status \$?" \
0352c371e743d8dae996123f658b5d32c677614eYassir Elley >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo Job finished at `LC_ALL=C date`' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'echo --' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # restore PATH in case the command changed it
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'PATH=/usr/bin; export PATH' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # restore stdout
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'exec 1<&3' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # close file descriptor 3
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'exec 3<&-' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'exec 2>&1' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # append the messages to the real log file
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
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'postrun_lock log' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "cat $cmdout >> $LOGFILE" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo 'postrun_unlock log' >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "rm -f $cmdout" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "rm -f $cmdfile" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley echo "rm -f $postrun_command_file" >> $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley chmod 700 $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $postrun_bg_job = yes ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley $cmdfile &
0352c371e743d8dae996123f658b5d32c677614eYassir Elley else
0352c371e743d8dae996123f658b5d32c677614eYassir Elley $cmdfile
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley exitval=$?
0352c371e743d8dae996123f658b5d32c677614eYassir Elley}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleyusername=${EMAIL:-root}
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elleypostrun_defaults() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # default settings
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_pkginst="$PKGINST"
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_submit_time=''
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_uniq_time=''
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_is_uniq=no
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_uniq_timeout=5
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_bg_job=no
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_command_file=''
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_user=${username}
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_job_number='???'
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_alt_root_okay=no
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_job_class=other
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek postrun_no_spool=no
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek}
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek# usage: is_leap_year yyyy
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozekis_leap_year() {
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek cal 02 $1 | egrep -s 29 && return 0
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek return 1
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek}
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek# get_abstime yy mm dd hh mm ss
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek#
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 Elleyget_abstime() {
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # the absolute time since 1970...
0352c371e743d8dae996123f658b5d32c677614eYassir Elley t=0
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # number of years
0352c371e743d8dae996123f658b5d32c677614eYassir Elley t=$(($t + ($1 - 1970) * 31536000))
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # add 1 day for each leap year
0352c371e743d8dae996123f658b5d32c677614eYassir Elley y=1972
0352c371e743d8dae996123f658b5d32c677614eYassir Elley end_y=$1
0352c371e743d8dae996123f658b5d32c677614eYassir Elley if [ $2 -lt 2 ]; then
0352c371e743d8dae996123f658b5d32c677614eYassir Elley end_y=$(($1 - 1))
0352c371e743d8dae996123f658b5d32c677614eYassir Elley fi
0352c371e743d8dae996123f658b5d32c677614eYassir Elley while [ $y -le $end_y ]; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley is_leap_year $y && t=$(($t + 86400))
0352c371e743d8dae996123f658b5d32c677614eYassir Elley y=$(($y + 4))
6b57784f0f175275fd900eca21c77415e3a5ea52Jakub Hrozek done
0352c371e743d8dae996123f658b5d32c677614eYassir Elley
0352c371e743d8dae996123f658b5d32c677614eYassir Elley # number of months
0352c371e743d8dae996123f658b5d32c677614eYassir Elley m=1
4e5e846de22407f825fe3b4040d79606818a2419Jakub Hrozek while [ $m -lt $2 ]; do
0352c371e743d8dae996123f658b5d32c677614eYassir Elley t=$(($t + ${MONTH[$m]} * 86400))
m=$(($m + 1))
done
# 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:
# YYYY.MM.DD.HH.MM.SS as printed by date +%Y.%m.%d.%H.%M.%S
#
# Works for dates after 1970.01.01.00.00.00
#
get_timediff() {
year1=$(expr "$1" : "^\([^.]*\)\..*")
month1=$(expr "$1" : "^[^.]*\.\([^.]*\)\..*")
day1=$(expr "$1" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
hour1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
min1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
sec1=$(expr "$1" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
year2=$(expr "$2" : "^\([^.]*\)\..*")
month2=$(expr "$2" : "^[^.]*\.\([^.]*\)\..*")
day2=$(expr "$2" : "^[^.]*\.[^.]*\.\([^.]*\)\..*")
hour2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
min2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)\..*")
sec2=$(expr "$2" : "^[^.]*\.[^.]*\.[^.]*\.[^.]*\.[^.]*\.\([^.]*\)")
# calculate seconds since 1970.01.01.00.00.00
t1=`get_abstime $year1 $month1 $day1 $hour1 $min1 $sec1`
t2=`get_abstime $year2 $month2 $day2 $hour2 $min2 $sec2`
# print difference
expr $t1 - $t2
}
postrun_runq() {
cd $SPOOLDIR
IFS=' '
timeleft=0
postrun_lock
for job in *.ctrl; do
test -f "$job" || continue
postrun_defaults
while read var val; do
case "$var" in
pkginst: )
postrun_pkginst="$val"
;;
submit_time: )
postrun_submit_time="$val"
;;
uniq_command: )
postrun_is_uniq="$val"
;;
uniq_time: )
postrun_uniq_time="$val"
;;
uniq_timeout: )
postrun_uniq_timeout="$val"
;;
background: )
postrun_bg_job="$val"
;;
user: )
postrun_user="$val"
;;
class: )
postrun_job_class="$val"
;;
* )
echo "postrun: WARNING: invalid setting in $job: $var"
;;
esac
done < $job
postrun_command_file=$SPOOLDIR/`basename $job .ctrl`.cmd
postrun_job_number=`basename $job .ctrl`
if [ $postrun_ignore_timeout = no ]; then
# if it's a uniq job, check if it timed out
if [ "x$postrun_is_uniq" = xyes ]; then
# calculate time difference (seconds)
tdiff=$(get_timediff $(date +%Y.%m.%d.%H.%M.%S) \
$postrun_uniq_time)
timeout_sec=$((postrun_uniq_timeout * 60))
if [ $tdiff -ge $timeout_sec ]; then
postrun_run_command
rm -f $job
else
# try again in at least $tdiff sec time
tl=$(($tdiff / 60 + 1))
if [ $tl -gt $timeleft ]; then
timeleft=$tl
fi
fi
else
postrun_run_command
rm -f $job
fi
else
# ignore timeout, just run the job
postrun_run_command
rm -f $job
fi
done
if [ $timeleft -gt 0 ]; then
echo "$MYDIR/postrun -q" |
at now "+${timeleft}minutes" \
> /dev/null 2>&1
fi
postrun_unlock
exit 0
}
postrun_defaults
exitval=0
postrun_ignore_timeout=no
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)
postrun_ignore_timeout=yes
postrun_runq
exit 1
fi
if [ $# = 1 -a "x$1" = 'x-q' ]; then
# postrun-runq mode, to be run from at(1)
postrun_runq
exit 1
fi
# process the command line
while [ $# -gt 0 ]; do
case "$1" in
-h|-\?|--help)
usage
;;
-u|--uniq)
postrun_is_uniq=yes
;;
-b|--bg)
postrun_bg_job=yes
;;
-t|--timeout)
opt="$1"
if [ $# == 0 ]; then
echo "postrun: error: argument expected after $opt"
exit 1
fi
shift
timeout=$1
if ! is_number "$timeout"; then
echo "postrun: error: interger number expected after $opt (found \"$timeout\")"
exit 1
fi
postrun_uniq_timeout=$timeout
;;
-i|--ignore)
postrun_no_spool=yes
;;
-f)
opt="$1"
if [ $# == 0 ]; then
echo "postrun: error: argument expected after $opt"
exit 1
fi
shift
postrun_command_file="$1"
;;
-c)
opt="$1"
if [ $# == 0 ]; then
echo "postrun: error: argument expected after $opt"
exit 1
fi
shift
postrun_job_class="$1"
;;
-a)
postrun_alt_root_okay=yes
;;
--)
break
;;
*)
echo "postrun: error: invalid argument: $1"
exit 1
;;
esac
shift
done
if [ "x$postrun_command_file" = x ]; then
# save the standard input in a temporary file
tmp_cmd_file=`postrun_mktemp /tmp/postrun.cmd.XXXX`
cat > $tmp_cmd_file
postrun_command_file=$tmp_cmd_file
fi
if [ "$LUBIN" != "" ]; then
#
# Live Upgrade. Unsafe to run the command now.
# Put into spool and defer to next boot.
#
postrun_spool_command "${@}"
elif [ "$PKG_INSTALL_ROOT" != "" -a "$PKG_INSTALL_ROOT" != "/" -a \
"x$postrun_alt_root_okay" != xyes ]; then
#
# Installation to an alternate root directory
# Put command into spool and defer to next boot.
#
postrun_spool_command "${@}"
else
#
# 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
if [ x$postrun_is_uniq = xyes -a x$postrun_alt_root_okay != xyes ]; then
# don't run the command yet in case the same command is requested
# within the next postrun_uniq_timeout minutes
postrun_spool_command "${@}" && {
echo "$MYDIR/postrun -q" | \
at now "+${postrun_uniq_timeout}minutes" > /dev/null 2>&1
}
else
postrun_debug "Executing commands immediately"
postrun_run_command "${@}"
# do not delete the tmp_cmd_file because it's the only copy of the
# commands (since the job is not spooled)
tmp_cmd_file=''
fi
fi
if [ "x$tmp_cmd_file" != x ]; then
rm -f $tmp_cmd_file
fi
exit $exitval