home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / init.d / rc < prev    next >
Encoding:
Text File  |  2012-03-24  |  8.4 KB  |  339 lines

  1. #! /bin/sh
  2. #
  3. # rc
  4. #
  5. # Starts/stops services on runlevel changes.
  6. #
  7. # Optimization: A start script is not run when the service was already
  8. # configured to run in the previous runlevel.  A stop script is not run
  9. # when the the service was already configured not to run in the previous
  10. # runlevel.
  11. #
  12. # Authors:
  13. #     Miquel van Smoorenburg <miquels@cistron.nl>
  14. #     Bruce Perens <Bruce@Pixar.com>
  15.  
  16. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  17. export PATH
  18.  
  19. # Un-comment the following for interactive debugging. Do not un-comment
  20. # this for debugging a real boot process as no scripts will be executed.
  21. # debug=echo
  22.  
  23. # Specify method used to enable concurrent init.d scripts.
  24. # Valid options are 'none' and 'makefile'.  Obsolete options
  25. # used earlier are 'shell' and 'startpar'.  The obsolete options
  26. # are aliases for 'makefile' since 2010-05-14.  The default since
  27. # the same date is 'makefile', as the init.d scripts in Debian now
  28. # include dependency information and are ordered using this
  29. # information.  See insserv for information on dependency based
  30. # boot sequencing.
  31. CONCURRENCY=makefile
  32.  
  33. # Make sure the name survive changing the argument list
  34. scriptname="$0"
  35.  
  36. umask 022
  37.  
  38. on_exit() {
  39.     echo "error: '$scriptname' exited outside the expected code flow."
  40. }
  41. trap on_exit EXIT # Enable emergency handler
  42.  
  43. # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
  44. trap ":" INT QUIT TSTP
  45.  
  46. # Set onlcr to avoid staircase effect.
  47. stty onlcr 0>&1
  48.  
  49. # Functions for splash progress bars
  50. if [ -e /lib/init/splash-functions-base ] ; then
  51.     . /lib/init/splash-functions-base
  52. else
  53.     # Quiet down script if old initscripts version without /lib/init/splash-functions-base is used.
  54.     splash_progress() { return 1; }
  55.     splash_stop() { return 1; }
  56. fi
  57.  
  58. # Now find out what the current and what the previous runlevel are.
  59.  
  60. runlevel=$RUNLEVEL
  61. # Get first argument. Set new runlevel to this argument.
  62. [ "$1" != "" ] && runlevel=$1
  63. if [ "$runlevel" = "" ]
  64. then
  65.     echo "Usage: $scriptname <runlevel>" >&2
  66.     exit 1
  67. fi
  68. previous=$PREVLEVEL
  69. [ "$previous" = "" ] && previous=N
  70.  
  71. export runlevel previous
  72.  
  73. if [ -f /etc/default/rcS ] ; then
  74.     . /etc/default/rcS
  75. fi
  76. export VERBOSE
  77.  
  78. if [ -f /lib/lsb/init-functions ] ; then
  79.     . /lib/lsb/init-functions
  80. else
  81.     log_action_msg() { echo $@; }
  82.     log_failure_msg() { echo $@; }
  83.     log_warning_msg() { echo $@; }
  84. fi
  85.  
  86. #
  87. # Stub to do progress bar ticks (for splash programs) on startup
  88. #
  89. startup_progress() {
  90.     # Avoid divide by zero if anyone moved xdm/kdm/gdm first in a runlevel.
  91.     if [ 0 -eq "$num_steps" ] ; then return; fi
  92.  
  93.     step=$(($step + $step_change))
  94.     progress=$(($step * $progress_size / $num_steps + $first_step))
  95.     $debug splash_progress "$progress" || true
  96. }
  97.  
  98. #
  99. # Check if we are able to use make like booting.  It require the
  100. # insserv package to be enabled. Boot concurrency also requires
  101. # startpar to be installed.
  102. #
  103. if [ "none" != "$CONCURRENCY" ] ; then
  104.     test -s /etc/init.d/.depend.boot  || CONCURRENCY="none"
  105.     test -s /etc/init.d/.depend.start || CONCURRENCY="none"
  106.     test -s /etc/init.d/.depend.stop  || CONCURRENCY="none"
  107.     if test -e /etc/init.d/.legacy-bootordering ; then
  108.         CONCURRENCY="none"
  109.     fi
  110.     startpar -v      > /dev/null 2>&1 || CONCURRENCY="none"
  111. fi
  112.  
  113. #
  114. # Start script or program.
  115. #
  116. case "$CONCURRENCY" in
  117.     makefile|startpar|shell) # startpar and shell are obsolete
  118.         CONCURRENCY=makefile
  119.         log_action_msg "Using makefile-style concurrent boot in runlevel $runlevel"
  120.         # The splash API is not handled with this CONCURRENCY mode.
  121.         # It need to be implented in startpar.  Until that is done
  122.         # stop the splash screen before starting services, to avoid
  123.         # usplash and X to confuse each other during boot.
  124.         startup() {
  125.             if [ start = "$1" ] || [ boot = "$1" ]
  126.             then
  127.                 $debug splash_stop || true
  128.             fi
  129.             eval "$(startpar -p 4 -t 20 -T 3 -M $1 -P $previous -R $runlevel)"
  130.  
  131.             if [ -n "$failed_service" ]
  132.             then
  133.                 log_failure_msg "startpar: service(s) returned failure: $failed_service"
  134.             fi
  135.  
  136.             if [ -n "$skipped_service" ]
  137.             then
  138.                 log_warning_msg "startpar: service(s) skipped: $skipped_service"
  139.             fi
  140.  
  141.             unset failed_service skipped_service
  142.         }
  143.         ;;
  144.     none|*)
  145.         startup() {
  146.             action=$1
  147.             shift
  148.             scripts="$@"
  149.             for script in $scripts ; do
  150.                 $debug "$script" $action
  151.                 startup_progress
  152.             done
  153.         }
  154.         ;;
  155. esac
  156.  
  157. # Check if the splash screen should be stopped before the given
  158. # script.
  159. is_splash_stop_scripts() {
  160.     scriptname=$1
  161.     case "$scriptname" in
  162.         # killprocs is used in runlevel 1
  163.         gdm|xdm|kdm|ltsp-client|ltsp-client-core|reboot|halt|killprocs)
  164.             return 0
  165.             ;;
  166.     esac
  167.     return 1
  168. }
  169.  
  170. # Is there an rc directory for this new runlevel?
  171. if [ -d /etc/rc$runlevel.d ]
  172. then
  173.     # Find out where in the progress bar the initramfs got to.
  174.     PROGRESS_STATE=0
  175.     if [ -f /dev/.initramfs/progress_state ]; then
  176.         . /dev/.initramfs/progress_state
  177.     fi
  178.  
  179.     # Split the remaining portion of the progress bar into thirds
  180.     progress_size=$(((100 - $PROGRESS_STATE) / 3))
  181.  
  182.     case "$runlevel" in
  183.         0|6)
  184.             ACTION=stop
  185.             # Count down from 0 to -100 and use the entire bar
  186.             first_step=0
  187.             progress_size=100
  188.             step_change=-1
  189.             ;;
  190.         S)
  191.             ACTION=start
  192.             # Begin where the initramfs left off and use 2/3
  193.             # of the remaining space
  194.             first_step=$PROGRESS_STATE
  195.             progress_size=$(($progress_size * 2))
  196.             step_change=1
  197.             ;;
  198.         *)
  199.             ACTION=start
  200.             # Begin where rcS left off and use the final 1/3 of
  201.             # the space (by leaving progress_size unchanged)
  202.             first_step=$(($progress_size * 2 + $PROGRESS_STATE))
  203.             step_change=1
  204.             ;;
  205.     esac
  206.  
  207.     # Count the number of scripts we need to run
  208.     # (for progress bars)
  209.     num_steps=0
  210.     for s in /etc/rc$runlevel.d/[SK]*; do
  211.         if is_splash_stop_scripts "${s##/etc/rc$runlevel.d/S??}" ; then
  212.             break
  213.         fi
  214.         num_steps=$(($num_steps + 1))
  215.     done
  216.     step=0
  217.  
  218.     # First, run the KILL scripts.
  219.     if [ makefile = "$CONCURRENCY" ]
  220.     then
  221.         if [ "$ACTION" = "start" ] && [ "$previous" != N ]
  222.         then
  223.             startup stop
  224.         fi
  225.     elif [ "$previous" != N ]
  226.     then
  227.         # Run all scripts with the same level in parallel
  228.         CURLEVEL=""
  229.         for s in /etc/rc$runlevel.d/K*
  230.         do
  231.             # Extract order value from symlink
  232.             level=${s#/etc/rc$runlevel.d/K}
  233.             level=${level%%[a-zA-Z]*}
  234.             if [ "$level" = "$CURLEVEL" ]
  235.             then
  236.                 continue
  237.             fi
  238.             CURLEVEL=$level
  239.             SCRIPTS=""
  240.             for i in /etc/rc$runlevel.d/K$level*
  241.             do
  242.                 # Check if the script is there.
  243.                 [ ! -f $i ] && continue
  244.  
  245.                 #
  246.                 # Find stop script in previous runlevel but
  247.                 # no start script there.
  248.                 #
  249.                 suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
  250.                 previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  251.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  252.                 #
  253.                 # If there is a stop script in the previous level
  254.                 # and _no_ start script there, we don't
  255.                 # have to re-stop the service.
  256.                 #
  257.                 [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  258.  
  259.                 # Stop the service.
  260.                 SCRIPTS="$SCRIPTS $i"
  261.                 if is_splash_stop_scripts "$suffix" ; then
  262.                     $debug splash_stop || true
  263.                 fi
  264.             done
  265.             startup stop $SCRIPTS
  266.         done
  267.     fi
  268.  
  269.     if [ makefile = "$CONCURRENCY" ]
  270.     then
  271.         if [ S = "$runlevel" ]
  272.         then
  273.             startup boot
  274.         else
  275.             startup $ACTION
  276.         fi
  277.     else
  278.         # Now run the START scripts for this runlevel.
  279.         # Run all scripts with the same level in parallel
  280.         CURLEVEL=""
  281.         for s in /etc/rc$runlevel.d/S*
  282.         do
  283.             # Extract order value from symlink
  284.             level=${s#/etc/rc$runlevel.d/S}
  285.             level=${level%%[a-zA-Z]*}
  286.             if [ "$level" = "$CURLEVEL" ]
  287.             then
  288.                 continue
  289.             fi
  290.             CURLEVEL=$level
  291.             SCRIPTS=""
  292.             for i in /etc/rc$runlevel.d/S$level*
  293.             do
  294.                 [ ! -f $i ] && continue
  295.  
  296.                 suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
  297.                 if [ "$previous" != N ]
  298.                 then
  299.                     #
  300.                     # Find start script in previous runlevel and
  301.                     # stop script in this runlevel.
  302.                     #
  303.                     stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
  304.                     previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  305.                     #
  306.                     # If there is a start script in the previous level
  307.                     # and _no_ stop script in this level, we don't
  308.                     # have to re-start the service.
  309.                     #
  310.                     if [ start = "$ACTION" ] ; then
  311.                         [ -f $previous_start ] && [ ! -f $stop ] && continue
  312.                     else
  313.                         # Workaround for the special
  314.                         # handling of runlevels 0 and 6.
  315.                         previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  316.                         #
  317.                         # If there is a stop script in the previous level
  318.                         # and _no_ start script there, we don't
  319.                         # have to re-stop the service.
  320.                         #
  321.                         [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  322.                     fi
  323.  
  324.                 fi
  325.                 SCRIPTS="$SCRIPTS $i"
  326.                 if is_splash_stop_scripts "$suffix" ; then
  327.                     $debug splash_stop || true
  328.                 fi
  329.             done
  330.             startup $ACTION $SCRIPTS
  331.         done
  332.     fi
  333. fi
  334.  
  335. trap - EXIT # Disable emergency handler
  336.  
  337. exit 0
  338.  
  339.