]> TLD Linux GIT Repositories - rc-scripts.git/blob - rc.d/rc
eba065216057ae8b472ed76d3b6dd01f5e2493a5
[rc-scripts.git] / rc.d / rc
1 #!/bin/sh
2 #
3 # rc            This file is responsible for starting/stopping
4 #               services when the runlevel changes. It is also
5 #               responsible for the very first setup of basic
6 #               things, such as setting the hostname.
7 #
8 #
9 # Original Author:
10 #               Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
11 #
12
13 # avoid being interrupted by child or keyboard
14 trap ":" INT QUIT TSTP
15
16 # Save argument
17 argv1="$1"
18 argv2="$2"
19
20 # Get terminal size of standard input of the system console
21 [ -z "$CONSOLE" ] && CONSOLE=/dev/console
22 if [ -c $CONSOLE ]; then
23         set -- $(stty size < $CONSOLE 2>/dev/null)
24         LINES=$1
25         COLUMNS=$2
26 else
27         LINES=
28         COLUMNS=
29 fi
30 export LINES COLUMNS CONSOLE
31
32 # NLS
33 if [ -f /etc/sysconfig/i18n ]; then
34         . /etc/sysconfig/i18n
35         [ -n "$LANG" ] && export LANG || unset LANG
36         [ -n "$LC_CTYPE" ] && export LC_CTYPE || unset LC_CTYPE
37         [ -n "$LC_COLLATE" ] && export LC_COLLATE || unset LC_COLLATE
38         [ -n "$LC_MESSAGES" ] && export LC_MESSAGES || unset LC_MESSAGES
39         [ -n "$LC_NUMERIC" ] && export LC_NUMERIC || unset LC_NUMERIC
40         [ -n "$LC_MONETARY" ] && export LC_MONETARY || unset LC_MONETARY
41         [ -n "$LC_TIME" ] && export LC_TIME || unset LC_TIME
42         [ -n "$LC_ALL" ] && export LC_ALL || unset LC_ALL
43         [ -n "$LANGUAGE" ] && export LANGUAGE || unset LANGUAGE
44         [ -n "$LINGUAS" ] && export LINGUAS || unset LINGUAS
45 fi
46
47 # Source function library.
48 . /etc/rc.d/init.d/functions
49
50 if [ "$argv2" != "chroot" ]; then
51         # Now find out what the current and what the previous runlevel are.
52         set $(runlevel)
53         runlevel=$2
54         previous=$1
55         export runlevel previous
56 fi
57
58 # Get first argument. Set new runlevel to this argument.
59 [ -n "$argv1" ] && runlevel="$argv1"
60
61 # if previous runlevel = current runlevel do nothing
62 [ "$runlevel" = "$previous" ] && exit 0
63
64 # Tell linuxconf what runlevel we are in
65 [ -d /var/run -a -w /var/run ] && echo "/etc/rc.d/rc$runlevel.d" > /var/run/runlevel.dir
66
67 # if runlevel is 0 (halt) or 6 (reboot) change to first
68 # virtual console, and send messages to /dev/console
69 # (it can be serial console too)
70 if [ "$COLUMNS" -gt 0 ]; then
71         if [ "$argv2" != "chroot" ] && ! is_yes "$VSERVER" && \
72                 [ "$runlevel" = "0" -o "$runlevel" = "6" ]; then
73                 CONSOLE=/dev/tty1
74                 [ -x /usr/bin/chvt ] && /usr/bin/chvt 1 && echo > $CONSOLE
75                 exec 0<> /dev/console 1>&0 2>&0
76         fi
77 else
78         # Serial: columns and lines are not defined
79           LINES=24
80         COLUMNS=80
81 fi
82
83 # See if we want to be in user confirmation mode
84 if [ "$previous" = "N" ]; then
85         if ! is_yes "$VSERVER" && grep -qi confirm /proc/cmdline >/dev/null 2>/dev/null \
86                 || [ -f /var/run/confirm ]; then
87                 rm -f /var/run/confirm
88                 CONFIRM="yes"
89                 nls "Entering interactive startup"
90         else
91                 CONFIRM=
92                 nls "Entering non-interactive startup"
93         fi
94 fi
95
96 # calculate bootsplash variables
97 sscripts=0
98 for i in /etc/rc.d/rc$runlevel.d/S*; do
99         [ ! -f $i ] && continue
100         [ "${1%.rpmsave}" != "${1}" ] && continue
101         [ "${1%.rpmorig}" != "${1}" ] && continue
102         [ "${1%.rpmnew}" != "${1}" ] && continue
103         subsys=${i#/etc/rc.d/rc$runlevel.d/S??}
104         [ -f /var/lock/subsys/$subsys ] && \
105                 [ ! -f /var/lock/subsys/${subsys}.init ] && continue
106         sscripts=$(( sscripts + 1 ))
107 done
108 export sscripts
109
110 if test "$previous" != "N" ; then
111         kscripts=0
112         for i in /etc/rc.d/rc$runlevel.d/K*; do
113                 [ ! -f $i ] && continue
114                 [ "${1%.rpmsave}" != "${1}" ] && continue
115                 [ "${1%.rpmorig}" != "${1}" ] && continue
116                 [ "${1%.rpmnew}" != "${1}" ] && continue
117                 subsys=${i#/etc/rc.d/rc$runlevel.d/K??}
118                 [ ! -f /var/lock/subsys/$subsys ] && \
119                 [ ! -f /var/lock/subsys/${subsys}.init ] && continue
120                 kscripts=$(( kscripts + 1 ))
121         done
122         export kscripts
123 fi
124
125 if test "$previous" = "N" -a "$runlevel" = "5" ; then
126         export progress=16
127         sscripts=$(( $sscripts + 15 ))
128 else
129         export progress=0
130 fi
131
132 if is_yes "$VSERVER"; then
133         # if runlevel is not 0 (halt) or 6 (reboot) run rc.sysinit
134         # BUT only if rc is run standalone (not run by init, as init will run rc.sysinit)
135         if [ "$previous" != "N" -a "$runlevel" != "0" -a "$runlevel" != "6" ]; then
136                 /etc/rc.d/rc.sysinit $runlevel
137         fi
138 fi
139
140 # set onlcr to avoid staircase effect and do not lock scrolling
141 stty onlcr -ixon 0>&1 2>/dev/null
142
143 # Say something ;)
144 af2=$(termput setaf $CRESMAN)
145 af6=$(termput setaf $CBRACKETS)
146 af7=$(termput op)
147 text="$(termput op)$(nls '%sResource Manager: %sEntering runlevel number' "$af2" "$af7")"
148 text_size="$(nls '%sResource Manager: %sEntering runlevel number' "" "")"
149 resp_size="$(nls 'DONE')"
150 {
151         local _len=${#text_size}
152         local _last_col=$(($INIT_COL+${#resp_size}-${#runlevel}))
153         while [ $((_len++)) -lt $_last_col ]; do
154                 text="$text."
155         done
156         echo -n "$text"
157 }
158 echo "${af6}[${af2} $runlevel ${af6}]${af7}"
159
160 # Is there an rc directory for this new runlevel?
161 if [ -d /etc/rc.d/rc$runlevel.d ]; then
162         # First, run the KILL scripts.
163         for i in /etc/rc.d/rc$runlevel.d/K*; do
164                 # Check if the script is there.
165                 [ ! -f $i ] && continue
166
167                 # Don't run [KS]??foo.{rpmsave,rpmorig,rpmnew} scripts
168                 [ "${1%.rpmsave}" != "${1}" ] && continue
169                 [ "${1%.rpmorig}" != "${1}" ] && continue
170                 [ "${1%.rpmnew}" != "${1}" ] && continue
171
172                 # Check if the subsystem is already up.
173                 subsys=${i#/etc/rc.d/rc$runlevel.d/K??}
174                 [ ! -f /var/lock/subsys/$subsys ] && \
175                 [ ! -f /var/lock/subsys/${subsys}.init ] && continue
176
177                 rc_splash "$i stop"
178
179                 # Bring the subsystem down.
180                 $i stop
181         done
182
183         # Now run the START scripts.
184         for i in /etc/rc.d/rc$runlevel.d/S*; do
185                 # Check if the script is there.
186                 [ ! -f $i ] && continue
187
188                 # Don't run [KS]??foo.{rpmsave,rpmorig} scripts
189                 [ "${1%.rpmsave}" != "${1}" ] && continue
190                 [ "${1%.rpmorig}" != "${1}" ] && continue
191                 [ "${1%.rpmnew}" != "${1}" ] && continue
192
193                 # Check if the subsystem is already up.
194                 subsys=${i#/etc/rc.d/rc$runlevel.d/S??}
195                 [ -f /var/lock/subsys/$subsys ] || \
196                 [ -f /var/lock/subsys/${subsys}.init ] && continue
197
198                 # If we're in confirmation mode, get user confirmation
199                 [ -n "$CONFIRM" ] &&
200                 {
201                         confirm $subsys
202                         case $? in
203                           0)
204                                 :
205                                 ;;
206                           2)
207                                 CONFIRM=
208                                 ;;
209                           *)
210                                 continue
211                                 ;;
212                         esac
213                 }
214
215                 rc_splash "$i start"
216                 # Bring the subsystem up.
217                 $i start
218         done
219 fi
220
221 # if runlevel is 0 (halt) or 6 (reboot) run rc.shutdown
222 if [ "$runlevel" = "0" ] || [ "$runlevel" = "6" ]; then
223         unset LANG
224         unset LC_CTYPE
225         unset LC_NUMERIC
226         unset LC_TIME
227         unset LC_COLLATE
228         unset LC_MONETARY
229         unset LC_MESSAGES
230         unset LC_PAPER
231         unset LC_NAME
232         unset LC_ADDRESS
233         unset LC_TELEPHONE
234         unset LC_MEASUREMENT
235         unset LC_IDENTIFICATION
236         unset LC_ALL
237         TMPDIR=/dev/null
238         export TMPDIR
239         if [ "$argv2" != "chroot" ]; then
240                 exec /etc/rc.d/rc.shutdown $runlevel $previous
241         else
242                 exit 0
243         fi
244 else
245         if is_yes "$RUN_LDCONFIG" || [ ! -f /etc/ld.so.cache ] ; then
246                 if [ -x /sbin/ldconfig ]; then
247                         run_cmd "Setting up /etc/ld.so.cache" /sbin/ldconfig -X
248                 fi
249         fi
250 fi
251
252 rc_splash "master"
253
254 # Say something ;)
255 text="$(nls '%sResource Manager: %sRunlevel has been reached' "$af2" "$af7")"
256 text_size="$(nls '%sResource Manager: %sRunlevel has been reached' "" "")"
257
258 _len=${#text_size}
259 _last_col=$(($INIT_COL+${#resp_size}-${#runlevel}))
260 while [ $((_len++)) -lt $_last_col ]; do
261         text="$text."
262 done
263 echo -n "$text"
264 unset _len _last_col
265
266 echo "${af6}[${af2} $runlevel ${af6}]${af7}"
267 unset af2 af6 af7
268
269 exit 0