rc 3.84 KB
#!/bin/sh
# rc - Main Run Level Control Script
#
# Copyright (c) 2003-2011 by Silvan Calarco <silvan.calarco@mambasoft.it>
# Copyright (c) 2003-2009,2011 by Davide Madrisan <davide.madrisan@gmail.com>

. /etc/sysconfig/rc

if [ -r $rc_base/init.d/rc.embedded ]; then
   . $rc_base/init.d/rc.embedded
   exit 0
fi

. $rc_functions
. /etc/sysconfig/i18n

LC_ALL=$LANG
TEXTDOMAIN=initscripts
TEXTDOMAINDIR=/usr/share/locale/

HOME="/root"; export HOME

trap "" SIGINT SIGQUIT SIGTSTP

# detect kernel command line parameters
# $CMDLINE is exported from mkinitramfs
[ -z "$CMDLINE" ] && CMDLINE="`cat /proc/cmdline 2>/dev/null`"

debug=0
for cmd in $CMDLINE; do
   case "$cmd" in
      debug) debug=1 ;;
      debug=*) debug=${cmd/debug=/} ;;
      multithread=*) multithread=${cmd/multithread=/} ;;
   esac
done

[ $debug -gt 0 ] && MAKEADD="DEBUG=1"

# set current (new) and previous runlevels
runlevel=$1; export runlevel
previous=${PREVLEVEL:-N}
rex="[0-9][0-9]"

[ "$runlevel" ] ||
 { echo "Usage: $0 <runlevel>" >&2; exit 1; }

[ -d $rc_base/rc$runlevel.d ] ||
 { echo $"$rc_base/rc$runlevel.d does not exist" >&2; exit 1; }

[ "$previous" = "$runlevel" ] && exit 0

export RUNLEVEL

[ "$previous" != "N" ] && progressbase=10 || progressbase=65

if [ "$runlevel" = "0" -o "$runlevel" = "6" ]; then
   $PLYMOUTHD --attach-to-session
   $PLYMOUTH --show-splash
   if [ "$runlevel" = "0" ]; then
      $PLYMOUTH message --text=$"System shutting down, please wait..."
   else
      $PLYMOUTH message --text=$"System rebooting, please wait..."
   fi
else
   $PLYMOUTH message --text=$"Starting services..."
fi

allservicesnum=`ls -1 $rc_base/rc$runlevel.d/[S,K]* 2>/dev/null | wc -l`

if [ "$previous" != "N" ]; then
   if [ "$multithread" = "1" ]; then
      /bin/echo -e $"Stopping runlevel $runlevel services...""\r" >&2
      make -C /var/init --no-print-directory -i -j -f /var/init/runlevel.mk \
         JOB=stop \
         PROGRESS_TOT=${allservicesnum} \
         PROGRESS_BASE=${progressbase} \
         ALLSERVICESNUM=$allservicesnum $MAKEADD
   else
      i=0
      for servicefile in `ls $rc_base/rc$runlevel.d/K* 2>/dev/null`; do
         check_link $servicefile &&
          { service=${servicefile#$rc_base/rc$runlevel.d/K$rex}
            prev_start=$rc_base/rc$previous.d/S$rex$service
            sysinit_start=$rc_base/rcsysinit.d/S$rex$service

            if [ "$runlevel" != "0" ] && [ "$runlevel" != "6" ]; then
               [ ! -f $prev_start ] && [ ! -f $sysinit_start ] && continue
            fi

            progress=`expr $progressbase + $i \* \( 100 - $progressbase \) / ${allservicesnum}`
            $servicefile stop
            i=`expr $i + 1`
          }
      done
      [ "$progress" ] && progressbase=$progress
   fi
fi

if [ "$multithread" = "1" ]; then
   /bin/echo -e $"Starting runlevel $runlevel services...""\r" >&2

   # recreate multithreaded dependencies
   depinit >/dev/null 2>&1

   # now start multithreaded init
   rm -f /var/lock/subsys/local
   make -C /var/init --no-print-directory -i -j -k -f /var/init/runlevel.mk \
      JOB=start \
      PROGRESS_TOT=${allservicesnum} \
      PROGRESS_BASE=${progressbase} \
      $MAKEADD 2>/dev/null &
   while [ ! -e /var/lock/subsys/local ]; do
      [ "`jobs`" ] || break
      sleep 1
   done
else
   i=0
   for servicefile in `ls $rc_base/rc$runlevel.d/S* 2> /dev/null`; do
      check_link $servicefile || { i=`expr $i + 1`; continue; }

      if [ "$previous" != "N" ]; then
         service=${servicefile#$rc_base/rc$runlevel.d/S$rex}
         stop=$rc_base/rc$runlevel.d/K$rex$service
         prev_start=$rc_base/rc$previous.d/S$rex$service

         [ -f $prev_start ] && [ ! -f $stop ] && continue
      fi

      progress=`expr $progressbase + $i \* \( 100 - $progressbase \) / ${allservicesnum}`
      $servicefile start
      i=`expr $i + 1`
   done
fi

if [ ! "$runlevel" = "5" ]; then
   chvt 1
   $PLYMOUTH quit
fi