Home | History | Annotate | Download | only in ext-sources
      1 #!/bin/ksh
      2 #
      3 # Script for investigating postponed post-installation jobs submitted
      4 # using postrun
      5 #
      6 # CDDL HEADER START
      7 #
      8 # The contents of this file are subject to the terms of the
      9 # Common Development and Distribution License, Version 1.0 only
     10 # (the "License").  You may not use this file except in compliance
     11 # with the License.
     12 #
     13 # You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
     14 # or http://www.opensolaris.org/os/licensing.
     15 # See the License for the specific language governing permissions
     16 # and limitations under the License.
     17 #
     18 # When distributing Covered Code, include this CDDL HEADER in each
     19 # file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     20 # If applicable, add the following below this CDDL HEADER, with the
     21 # fields enclosed by brackets "[]" replaced with your own identifying
     22 # information: Portions Copyright [yyyy] [name of copyright owner]
     23 #
     24 # CDDL HEADER END
     25 #
     26 #
     27 # Copyright 2004-2005 Sun Microsystems, Inc.  All rights reserved.
     28 # Use is subject to license terms.
     29 #
     30 
     31 export PATH=/usr/bin
     32 LC_ALL=C
     33 export LC_ALL
     34 MYDIR=$(cd $(dirname $0); pwd)
     35 
     36 postrun_root_found=no
     37 if [ "$PKG_INSTALL_ROOT" != "" ]; then
     38     pkginfo -q -R $PKG_INSTALL_ROOT SUNWpostrun-root && postrun_root_found=yes
     39 else
     40     pkginfo -q SUNWpostrun-root && postrun_root_found=yes
     41 fi
     42 
     43 if [ $postrun_root_found = no ]; then
     44     echo "ERROR: postrun-query cannot find SUNWpostrun-root"
     45     exit 1
     46 fi
     47 
     48 if [ "$PKG_INSTALL_ROOT" != "" ]; then
     49     POSTRUN_ROOT_BASEDIR=`pkginfo -R $PKG_INSTALL_ROOT -l SUNWpostrun-root \
     50 	| grep BASEDIR: |  sed 's/BASEDIR:[ 	]*//' | sed 's/ *//'`
     51 else
     52     POSTRUN_ROOT_BASEDIR=`pkginfo -l SUNWpostrun-root \
     53 	| grep BASEDIR: |  sed 's/BASEDIR:[ 	]*//' | sed 's/ *//'`
     54 fi
     55 
     56 SPOOLDIR="$PKG_INSTALL_ROOT$POSTRUN_ROOT_BASEDIR/var/spool/postrun"
     57 
     58 usage() {
     59     echo 'Usage: postrun-query [options]'
     60     echo
     61     echo 'Options:'
     62     echo '    -c <class>, --class <class>'
     63     echo '        Only consider jobs the belong to class <class>'
     64     echo
     65     echo '    -e, --exists'
     66     echo '        return 0 if spooled jobs exist 1 otherwise'
     67     echo
     68     echo '    -n, --count'
     69     echo '        print the number of spooled jobs only.  The default'
     70     echo '        behaviour is to list all jobs'
     71     echo
     72     echo '    -j <job>, --job <job>'
     73     echo '        display job number <job>'
     74     echo
     75     echo '    -h, -?, --help'
     76     echo '        Display this help'
     77     exit 1
     78 }
     79 
     80 
     81 postrun_query_count=no
     82 postrun_query_class=
     83 postrun_query_check_exists=no
     84 postrun_query_job=
     85 
     86 # process the command line
     87 while [ $# -gt 0 ]; do
     88     case "$1" in
     89 	-h|-\?|--help)
     90             usage
     91             ;;
     92         -e|--exists)
     93 	    postrun_query_check_exists=yes
     94 	    ;;
     95         -n|--count)
     96 	    postrun_query_count=yes
     97 	    ;;
     98 	-j|--job)
     99 	    opt="$1"
    100 	    if [ $# == 0 ]; then
    101 		    echo "postrun-query: error: argument expected after $opt"
    102 		    exit 1
    103 	    fi
    104 	    shift
    105 	    postrun_query_job="$1"
    106 	    ;;
    107 	-c|--class)
    108 	    opt="$1"
    109 	    if [ $# == 0 ]; then
    110 		    echo "postrun-query: error: argument expected after $opt"
    111 		    exit 1
    112 	    fi
    113 	    shift
    114 	    postrun_query_class="$1\$"
    115 	    ;;
    116 	--)
    117 	    break
    118 	    ;;
    119 	*)
    120 	    echo "postrun: error: invalid argument: $1"
    121 	    exit 1
    122 	    ;;
    123     esac
    124     shift
    125 done
    126 
    127 # exit 0 if jobs exist
    128 # exit 1 if no jobs exist
    129 if [ $postrun_query_check_exists = yes ]; then
    130     grep "^class: $postrun_query_class" $SPOOLDIR/*.ctrl >/dev/null 2>&1 \
    131 	    && exit 0 || exit 1
    132 fi
    133 
    134 # print the # of jobs
    135 if [ $postrun_query_count = yes ]; then
    136     grep -l "^class: $postrun_query_class" $SPOOLDIR/*.ctrl 2>/dev/null \
    137 	| wc -l
    138     exit 0
    139 fi
    140 
    141 # print info about a job
    142 if [ "x$postrun_query_job" != x ]; then
    143     test -f $SPOOLDIR/$postrun_query_job.ctrl && \
    144 	job_details="`cat $SPOOLDIR/$postrun_query_job.ctrl`" && \
    145 	job_commands="`cat $SPOOLDIR/$postrun_query_job.cmd`" || {
    146 	echo "postrun-query: job $postrun_query_job not found"
    147 	exit 1
    148     }
    149     echo Job $postrun_query_job
    150     echo "Submitted on`echo "$job_details" | grep '^submit_time:' | cut -f2- -d:`"
    151     echo "Belongs to package(s):`echo "$job_details" | grep pkginst: | cut -f2 -d:`"
    152     classes=`echo "$job_details" | grep '^class:' | cut -f2 -d:`
    153     classes=`echo $classes | sed -e 's/ /,/g'`
    154     echo "Class(es): $classes"
    155     echo "---commands follow---"
    156     echo "$job_commands"
    157     echo "---end of commands---"
    158     exit 0
    159 fi
    160 
    161 # list the jobs
    162 ctrls=`grep -l "^class: $postrun_query_class" $SPOOLDIR/*.ctrl 2>/dev/null`
    163 test "x$ctrls" = x \
    164     && echo "No spooled jobs found." \
    165     || echo "Job #	Class(es)	Package(s)"
    166 for ctrl in $ctrls; do
    167     nr=`basename $ctrl .ctrl`
    168     classes=`grep '^class:' $ctrl | cut -f2 -d:`
    169     classes=`echo $classes | sed -e 's/ /,/g'`
    170     pkginst=`grep '^pkginst:' $ctrl | cut -f2 -d:`
    171     echo "$nr	$classes	$pkginst"
    172 done
    173