home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / etc / init.d / rc < prev    next >
Encoding:
Text File  |  2007-04-10  |  8.0 KB  |  347 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:/bin:/usr/sbin:/usr/bin
  17. export PATH
  18.  
  19. # Un-comment the following for debugging.
  20. # debug=echo
  21.  
  22. # Specify method used to enable concurrent init.d scripts.
  23. # Valid options are 'none' and 'shell'.
  24. CONCURRENCY=none
  25.  
  26. # Make sure the name survive changing the argument list
  27. scriptname="$0"
  28.  
  29. umask 022
  30.  
  31. #
  32. # Stub to do progress bar ticks (currently just for usplash) on startup
  33. #
  34. startup_progress() {
  35.     $@
  36.     step=$(($step + $step_change))
  37.     progress=$(($step * $progress_size / $num_steps + $first_step))
  38.     if type usplash_write >/dev/null 2>&1; then
  39.         usplash_write "PROGRESS $progress" || true
  40.     fi
  41. }
  42.  
  43. #
  44. # Start script or program.
  45. #
  46. case "$CONCURRENCY" in
  47.   none)
  48.     startup() {
  49.         action=$1
  50.         shift
  51.         scripts="$@"
  52.         sh=sh
  53.         # Debian Policy º9.3.1 requires .sh scripts in runlevel S to be sourced
  54.         # However, some important packages currently contain .sh scripts
  55.         # that do "exit" at some point, thus killing this process.  Bad!
  56.         #[ S = "$runlevel" ] && sh=.
  57.         for script in $scripts ; do
  58.             case "$script" in
  59.               *.sh)
  60.                 if [ "." = "$sh" ] ; then
  61.                     set "$action"
  62.                     RC_SAVE_PATH="$PATH"
  63.                     startup_progress $debug . "$script"
  64.                     PATH="$RC_SAVE_PATH"
  65.                 else
  66.                     startup_progress $debug $sh "$script" $action
  67.                 fi
  68.                 ;;
  69.               *)
  70.                 startup_progress $debug "$script" $action
  71.                 ;;
  72.             esac
  73.         done
  74.     }
  75.     ;;
  76.   shell)
  77.     startup() {
  78.         action=$1
  79.         shift
  80.         scripts="$@"
  81.         sh=sh
  82.         # Debian Policy º9.3.1 requires .sh scripts in runlevel S to be sourced
  83.         # However, some important packages currently contain .sh scripts
  84.         # that do "exit" at some point, thus killing this process.  Bad!
  85.         #[ S = "$runlevel" ] && sh=.
  86.         backgrounded=0
  87.         for script in $scripts ; do
  88.             case "$script" in
  89.               *.sh)
  90.                 if [ "." = "$sh" ] ; then
  91.                     set "$action"
  92.                     RC_SAVE_PATH="$PATH"
  93.                     startup_progress $debug . "$script"
  94.                     PATH="$RC_SAVE_PATH"
  95.                 else
  96.                     startup_progress $debug $sh "$script" $action
  97.                 fi
  98.                 ;;
  99.               *)
  100.                 startup_progress $debug "$script" $action &
  101.                 backgrounded=1
  102.                 ;;
  103.             esac
  104.         done
  105.         [ 1 = "$backgrounded" ] && wait
  106.     }
  107.     ;;
  108.   startpar)
  109.     startup() {
  110.         action=$1
  111.         shift
  112.         scripts="$@"
  113.         sh=sh
  114.         # Debian Policy º9.3.1 requires .sh scripts in runlevel S to be sourced
  115.         # However, some important packages currently contain .sh scripts
  116.         # that do "exit" at some point, thus killing this process.  Bad!
  117.         #[ S = "$runlevel" ] && sh=.
  118.         # Make sure .sh scripts are sourced in runlevel S
  119.         if [ "." = "$sh" ] ; then
  120.             newscripts=
  121.             for script in $scripts ; do
  122.                 case "$script" in
  123.                   *.sh)
  124.                     set "$action"
  125.                     RC_SAVE_PATH="$PATH"
  126.                     startup_progress $debug . "$script"
  127.                     PATH="$RC_SAVE_PATH"
  128.                     ;;
  129.                   *)
  130.                     newscripts="$newscripts $script"
  131.                     ;;
  132.                 esac
  133.             done
  134.             scripts="$newscripts"
  135.         fi
  136.  
  137.         # startpar is not working as it should yet [pere 2005-09-10]
  138.         [ -n "$scripts" ] && startup_progress $debug startpar -a $action $scripts
  139.         startup_progress $debug startpar -a $action $scripts
  140.     }
  141.     ;;
  142. esac
  143.  
  144. on_exit() {
  145.     echo "error: '$scriptname' exited outside the expected code flow."
  146. }
  147. trap on_exit EXIT # Enable emergency handler
  148.  
  149. # Ignore CTRL-C only in this shell, so we can interrupt subprocesses.
  150. trap ":" INT QUIT TSTP
  151.  
  152. # Should we also output to the console?
  153. # /proc won't be mounted if we don't have an initramfs, but then we're
  154. # dealing with custom systems so it's their own fault that they'll get
  155. # output from rcS ;)
  156. if grep -w -q quiet /proc/cmdline 2>/dev/null; then
  157.     QUIET=yes
  158. else
  159.     QUIET=no
  160. fi
  161. export QUIET
  162.  
  163. # Set onlcr to avoid staircase effect.
  164. if [ "$QUIET" != yes ]; then
  165.     stty onlcr </dev/console >/dev/console 2>&1
  166. fi
  167.  
  168. # Now find out what the current and what the previous runlevel are.
  169. runlevel=$RUNLEVEL
  170.  
  171. # Get first argument. Set new runlevel to this argument.
  172. [ "$1" != "" ] && runlevel=$1
  173. if [ "$runlevel" = "" ]
  174. then
  175.     echo "Usage: $scriptname <runlevel>" >&2
  176.     exit 1
  177. fi
  178. previous=$PREVLEVEL
  179. [ "$previous" = "" ] && previous=N
  180.  
  181. export runlevel previous
  182.  
  183. if [ S = "$runlevel" ]
  184. then
  185.     #
  186.     # See if system needs to be setup. This is ONLY meant to
  187.     # be used for the initial setup after a fresh installation!
  188.     #
  189.     if [ -x /sbin/unconfigured.sh ]
  190.     then
  191.         /sbin/unconfigured.sh
  192.     fi
  193. fi
  194.  
  195. . /etc/default/rcS
  196. export VERBOSE
  197.  
  198. # Is there an rc directory for this new runlevel?
  199. if [ -d /etc/rc$runlevel.d ]
  200. then
  201.     # Find out where in the progress bar the initramfs got to.
  202.     PROGRESS_STATE=0
  203.     if [ -f /dev/.initramfs/progress_state ]; then
  204.         . /dev/.initramfs/progress_state
  205.     fi
  206.  
  207.     # Split the remaining portion of the progress bar into thirds
  208.     progress_size=$(((100 - $PROGRESS_STATE) / 3))
  209.  
  210.     case "$runlevel" in
  211.         0|6)
  212.             ACTION=stop
  213.             # Count down from 0 to -100 and use the entire bar
  214.             first_step=0
  215.             progress_size=100
  216.             step_change=-1
  217.             ;;
  218.             S)
  219.                 ACTION=start
  220.             # Begin where the initramfs left off and use 2/3
  221.             # of the remaining space
  222.             first_step=$PROGRESS_STATE
  223.             progress_size=$(($progress_size * 2))
  224.             step_change=1
  225.             ;;
  226.         *)
  227.             ACTION=start
  228.             # Begin where rcS left off and use the final 1/3 of
  229.             # the space (by leaving progress_size unchanged)
  230.             first_step=$(($progress_size * 2 + $PROGRESS_STATE))
  231.             step_change=1
  232.             ;;
  233.     esac
  234.  
  235.     # Count the number of scripts we need to run (for usplash progress bar)
  236.     num_steps=0
  237.     for s in /etc/rc$runlevel.d/[SK]*; do
  238.             case "${s##/etc/rc$runlevel.d/S??}" in
  239.                 gdm|xdm|kdm|ltsp-client|reboot|halt)
  240.                     break
  241.                     ;;
  242.             esac
  243.             num_steps=$(($num_steps + 1))
  244.         done
  245.  
  246.         step=0
  247.  
  248.     # First, run the KILL scripts.
  249.     if [ "$previous" != N ]
  250.     then
  251.         # Run all scripts with the same level in parallel
  252.         CURLEVEL=""
  253.         for s in /etc/rc$runlevel.d/K*
  254.         do
  255.             level=$(echo $s | sed 's/.*\/K\([0-9][0-9]\).*/\1/')
  256.             if [ "$level" = "$CURLEVEL" ]
  257.             then
  258.                 continue
  259.             fi
  260.             CURLEVEL=$level
  261.             SCRIPTS=""
  262.             for i in /etc/rc$runlevel.d/K$level*
  263.             do
  264.                 # Check if the script is there.
  265.                 [ ! -f $i ] && continue
  266.  
  267.                 #
  268.                 # Find stop script in previous runlevel but
  269.                 # no start script there.
  270.                 #
  271.                 suffix=${i#/etc/rc$runlevel.d/K[0-9][0-9]}
  272.                 previous_stop=/etc/rc$previous.d/K[0-9][0-9]$suffix
  273.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  274.                 #
  275.                 # If there is a stop script in the previous level
  276.                 # and _no_ start script there, we don't
  277.                 # have to re-stop the service.
  278.                 #
  279.                 [ -f $previous_stop ] && [ ! -f $previous_start ] && continue
  280.  
  281.                 # Stop the service.
  282.                 SCRIPTS="$SCRIPTS $i"
  283.             done
  284.             startup stop $SCRIPTS
  285.         done
  286.     fi
  287.  
  288.     # Now run the START scripts for this runlevel.
  289.     # Run all scripts with the same level in parallel
  290.     CURLEVEL=""
  291.     for s in /etc/rc$runlevel.d/S*
  292.     do
  293.         level=$(echo $s | sed 's/.*\/S\([0-9][0-9]\).*/\1/')
  294.         if [ "$level" = "$CURLEVEL" ]
  295.         then
  296.             continue
  297.         fi
  298.         CURLEVEL=$level
  299.         SCRIPTS=""
  300.         for i in /etc/rc$runlevel.d/S$level*
  301.         do
  302.             [ ! -f $i ] && continue
  303.  
  304.             if [ "$previous" != N ]
  305.             then
  306.                 #
  307.                 # Find start script in previous runlevel and
  308.                 # stop script in this runlevel.
  309.                 #
  310.                 suffix=${i#/etc/rc$runlevel.d/S[0-9][0-9]}
  311.                 stop=/etc/rc$runlevel.d/K[0-9][0-9]$suffix
  312.                 previous_start=/etc/rc$previous.d/S[0-9][0-9]$suffix
  313.                 #
  314.                 # If there is a start script in the previous level
  315.                 # and _no_ stop script in this level, we don't
  316.                 # have to re-start the service.
  317.                 #
  318.                 [ -f $previous_start ] && [ ! -f $stop ] && continue
  319.             fi
  320.             SCRIPTS="$SCRIPTS $i"
  321.         done
  322.         startup $ACTION $SCRIPTS
  323.     done
  324. fi
  325.  
  326. if [ S = "$runlevel" ]
  327. then
  328.     #
  329.     # For compatibility, run the files in /etc/rc.boot too.
  330.     #
  331.     [ -d /etc/rc.boot ] && run-parts /etc/rc.boot
  332.  
  333.     #
  334.     # Finish setup if needed. The comment above about
  335.     # /sbin/unconfigured.sh applies here as well!
  336.     #
  337.     if [ -x /sbin/setup.sh ]
  338.     then
  339.         /sbin/setup.sh
  340.     fi
  341. fi
  342.  
  343. trap - EXIT # Disable emergency handler
  344.  
  345. exit 0
  346.  
  347.