home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / base / root.15 / sbin / shutdown / shutdown~
Text File  |  1998-08-19  |  5KB  |  199 lines

  1. #!/sbin/sh
  2.  
  3. # Copyright (c) 1998 The Santa Cruz Operation, Inc.. All Rights Reserved. 
  4. #                                                                         
  5. #        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF THE               
  6. #                   SANTA CRUZ OPERATION INC.                             
  7. #                                                                         
  8. #   The copyright notice above does not evidence any actual or intended   
  9. #   publication of such source code.                                      
  10.  
  11. #ident    "@(#)initpkg:i386/cmd/initpkg/shutdown.sh    1.8.21.3"
  12. #ident  "$Header: $"
  13.  
  14. if [ -z "$LC_ALL" -a -z "$LC_MESSAGES" ]
  15. then
  16.     if [ -z "$LANG" ]
  17.     then
  18.         LNG=`/usr/bin/defadm locale LANG 2>/dev/null`
  19.         if [ "$?" != 0 ]
  20.         then LANG=C
  21.         else eval $LNG
  22.         fi
  23.     fi
  24.     # if catalogs aren't under /usr/lib/locale, check /etc/inst/locale
  25.     if [ -d /usr/lib/locale/$LANG ] 
  26.     then LC_MESSAGES=$LANG
  27.     else LC_MESSAGES=/etc/inst/locale/$LANG
  28.     fi
  29.     export LANG LC_MESSAGES
  30. fi
  31. LABEL="UX:shutdown"
  32. CAT="uxshutdown"
  33.  
  34. #    Sequence performed to change the init state of a machine.
  35.  
  36. #    This procedure checks to see if you are permitted and allows an
  37. #    interactive shutdown.  The actual change of state, killing of
  38. #    processes and such are performed by the new init state, say 0,
  39. #    and its /sbin/rc0.
  40.  
  41. #    Usage:  shutdown [ -y ] [ -g<grace-period> ] [ -i<init-state> ]
  42.  
  43. priv -allprivs +sysops work    # turn off all working privileges
  44.  
  45. if [ `/usr/bin/pwd` != / ]
  46. then
  47.     /usr/bin/pfmt -l $LABEL -s error -g $CAT:4 "You must be in the / directory to run /sbin/shutdown.\n"
  48.     exit 1
  49. fi
  50.  
  51. # Make sure /usr is mounted
  52. if [ ! -d /usr/bin ]
  53. then
  54.     /usr/bin/pfmt -l $LABEL -s error -g $CAT:5 "/usr is not mounted.  Mount /usr or use init to shutdown.\n"
  55.     exit 1
  56. fi
  57.  
  58. askconfirmation=yes
  59.  
  60. if /sbin/i386
  61. then
  62.     initstate=0
  63. else
  64.     initstate=s
  65. fi
  66.  
  67. while getopts ?yg:i: c
  68. do
  69.     case $c in
  70.     i)    initstate=$OPTARG; 
  71.         case $initstate in
  72.         2|3|4)
  73.             /usr/bin/pfmt -l $LABEL -s error -g $CAT:8 "Initstate %s is not for system shutdown.\n" $initstate
  74.             exit 1
  75.         esac
  76.         ;;
  77.     g)    grace=$OPTARG; 
  78.         ;;
  79.     y)    askconfirmation=
  80.         ;;
  81.     \?)    /usr/bin/pfmt -l $LABEL -s action -g $CAT:9 "Usage:  %s [ -y ] [ -g<grace> ] [ -i<initstate> ]\n" $0
  82.         exit 2
  83.         ;;
  84.     *)     
  85.         /usr/bin/pfmt -l $LABEL -s action -g $CAT:9 "Usage:  %s [ -y ] [ -g<grace> ] [ -i<initstate> ]\n" $0
  86.         exit 2
  87.         ;;
  88.     esac
  89. done
  90. if [ -z "$grace" ]
  91. then
  92.     if [ -r /etc/default/shutdown ]
  93.     then
  94.         grace=`/usr/bin/grep grace /etc/default/shutdown`
  95.         if [ "${grace}" = "" ]
  96.         then
  97.             /usr/bin/pfmt -l $LABEL -s warn -g $CAT:6 "Could not read /etc/default/shutdown.\n"
  98.             /usr/bin/pfmt -s nostd -g $CAT:7 "\tSetting default grace period to 60 seconds.\n"
  99.             grace=60
  100.         else
  101.             eval $grace
  102.         fi
  103.     else
  104.         /usr/bin/pfmt -l $LABEL -s warn -g $CAT:6 "Could not read /etc/default/shutdown.\n"
  105.         /usr/bin/pfmt -s nostd -g $CAT:7 "\tSetting default grace period to 60 seconds.\n"
  106.         grace=60
  107.     fi
  108. fi
  109.  
  110. shift `/usr/bin/expr $OPTIND - 1`
  111.  
  112. if [ -x /usr/alarm/bin/event ]
  113. then
  114.     /usr/alarm/bin/event -c gen -e shutdown -- -t $grace
  115. fi
  116.  
  117. if [ -z "${TZ}"  -a  -r /etc/TIMEZONE ]
  118. then
  119.     . /etc/TIMEZONE
  120. fi
  121.  
  122. /usr/bin/pfmt -l $LABEL -s info -g $CAT:10 '\nShutdown started.    ' 2>&1
  123. /usr/bin/date
  124. echo
  125.  
  126. /sbin/sync&
  127.  
  128. trap "exit 1"  1 2 15
  129.  
  130. WALL()
  131. {
  132.     SAVE_LC=$LC_ALL
  133.     LC_ALL=`/usr/bin/defadm locale LANG | /usr/bin/cut -d= -f2` 
  134.     export LC_ALL
  135.     /usr/bin/pfmt -l UX:shutdown -s warn "$@" 2>&1 | /usr/sbin/wall
  136.     LC_ALL=$SAVE_LC
  137. }
  138.  
  139. set -- `/sbin/who`
  140. if [ $? -eq 0 -a $# -gt 5  -a  ${grace} -gt 0 ]
  141. then
  142.     priv +macwrite +dacwrite +dev work
  143.     WALL -g uxshutdown:1 "The system will be shut down in %s seconds.\nPlease log off now.\n\n" ${grace}
  144.     priv -allprivs +sysops work
  145.     /usr/bin/sleep ${grace} 2>/dev/null
  146.     if [ $? -eq 2 ]
  147.     then
  148.         /usr/bin/pfmt -l $LABEL -s error -g $CAT:11 "Invalid grace period provided.\n"
  149.         /usr/bin/pfmt -l $LABEL -s action -g $CAT:9 "Usage:  %s [ -y ] [ -g<grace> ] [ -i<initstate> ]\n" $0
  150.         exit 2
  151.     fi
  152.     priv +macwrite +dacwrite +dev work
  153.  
  154.     WALL -g uxshutdown:2 "THE SYSTEM IS BEING SHUT DOWN NOW ! ! !\nLog off now or risk your files being damaged.\n\n"
  155.     priv -allprivs +sysops work
  156. fi    
  157.  
  158. if [ ${grace} -ne 0 ]
  159. then
  160.     /usr/bin/sleep ${grace} 2>/dev/null
  161. fi
  162. if [ $? -eq 2 ]
  163. then
  164.     /usr/bin/pfmt -l $LABEL -s error -g $CAT:11 "Invalid grace period provided.\n"
  165.     /usr/bin/pfmt -l $LABEL -s action -g $CAT:9 "Usage:  %s [ -y ] [ -g<grace> ] [ -i<initstate> ]\n" $0
  166.     exit 2
  167. fi
  168.  
  169. y=`/usr/bin/gettxt $CAT:12 "y"`
  170. n=`/usr/bin/gettxt $CAT:13 "n"`
  171. if [ "${askconfirmation}" ]
  172. then
  173.     /usr/bin/pfmt -s nostd -g $CAT:14 "Do you want to continue? (%s or %s):   " $y $n 2>&1
  174.     read b
  175. else
  176.     b=$y
  177. fi
  178. if [ "$b" != "$y" ]
  179. then
  180.     priv +macwrite +dacwrite +dev work
  181.     WALL -g uxshutdown:3 "False Alarm:  The system will not be brought down.\n"
  182.     priv -allprivs +sysops work
  183.     /usr/bin/pfmt -l $LABEL -s info -g $CAT:15 'Shut down aborted.\n'
  184.     exit 1
  185. fi
  186. case "${initstate}" in
  187. s | S )
  188.     priv +allprivs work
  189.     . /sbin/rc0 shutdown_s
  190.     priv -allprivs +sysops work
  191. esac
  192.  
  193. RET=0
  194. priv +owner +compat work
  195. /sbin/init ${initstate}
  196. RET=$?
  197. priv -allprivs +sysops work
  198. exit ${RET}
  199.