/[vps]/baselayout-vserver/branches/baselayout-1_12/bin/rc-status
Gentoo

Contents of /baselayout-vserver/branches/baselayout-1_12/bin/rc-status

Parent Directory Parent Directory | Revision Log Revision Log


Revision 206 - (show annotations) (download)
Sun Jan 15 10:27:03 2006 UTC (8 years, 8 months ago) by phreak
File size: 9260 byte(s)
Merging r1822 for branches/baselayout-1_12
1 #!/bin/bash
2 ################################################################################
3 # #
4 # Author: Sean E. Russell <ser@germane-software.com> #
5 # Version: 1.0 #
6 # Date: Jun 26, 2002 #
7 # Adaptation: Mike Frysinger [SpanKY] <vapier@gentoo.org> #
8 # Original code was in Ruby ... recoded into bash (at syntax level) #
9 # #
10 # This application displays information about the RC system used by Gentoo. #
11 # In particular, it displays a tree-like format of a run level, showing #
12 # all of the services that are installed at that level, and what each #
13 # service's status is (running, stopped, etc.) #
14 # #
15 # -a can be used to display all runlevels #
16 # -d can be used to display service dependancies #
17 # -u will display all unassigned services #
18 # -s will display all services #
19 # -h will display help #
20 # <runlevel> is used to choose the run level for which information is #
21 # displayed #
22 # #
23 # By default, rc-status only displays information about the current #
24 # runlevel; services installed and services running. #
25 # #
26 ################################################################################
27
28 # grab code from functions.sh so we don't have to reproduce it
29 source /sbin/functions.sh
30 runleveldir=/etc/runlevels
31
32 # grab settings from conf.d/rc
33 source "${svclib}/sh/rc-daemon.sh"
34 conf="$(add_suffix /etc/conf.d/rc)"
35 [[ -e ${conf} ]] && source "${conf}"
36
37 ################################################################################
38 # Parse command line options #
39 ################################################################################
40 do_opt() {
41 case $1 in
42 --all|-a)
43 ALL=true
44 ;;
45 --depend)
46 DEPEND=true
47 ;;
48 --unused|-u)
49 ALL=true
50 UNUSED=true
51 ;;
52 --list|-l)
53 ls -1 ${runleveldir}
54 exit 0
55 ;;
56 --servicelist|-s)
57 ALL=true
58 SERVICELIST=true
59 ;;
60 --nocolor|-nc)
61 ;;
62 --help|-h|-*)
63 echo "USAGE: $0 [command | <runlevel>]"
64 echo
65 echo "Commands:"
66 echo " -a, --all Show services at all run levels"
67 echo " -l, --list Show list of run levels"
68 echo " -u, --unused Show services not assigned to any run level"
69 echo " -s, --servicelist Show service list"
70 echo " -nc,--nocolor Monochrome output only"
71 echo " <runlevel> Show services assigned to <runlevel>"
72 echo
73 echo "If no arguments are supplied, shows services for current run level."
74 exit 0
75 ;;
76 *)
77 runlevel=$1
78 ;;
79 esac
80 }
81 for opt in "$@" ; do
82 do_opt ${opt}
83 [[ -n $2 ]] && shift
84 done
85
86 ################################################################################
87 # Find the current runlevel being queried. This is either something supplied #
88 # on the command line, or pulled from softlevel #
89 ################################################################################
90 if [[ -z ${runlevel} ]] ; then
91 if [[ -e ${svcdir}/softlevel ]] ; then
92 runlevel=$(<${svcdir}/softlevel)
93 else
94 ewarn "Could not locate current runlevel in ${svcdir}/softlevel"
95 if [[ -d ${runleveldir}/default ]] ; then
96 runlevel=default
97 else
98 eerror "Your installation is probably broken ... please \`emerge baselayout-vserver\`"
99 exit 1
100 fi
101 ewarn "Assuming current runlevel is '${runlevel}'"
102 fi
103 fi
104 if [[ ! -d ${runleveldir}/${runlevel} ]] ; then
105 eerror "${runlevel} is not a valid run level !"
106 eerror "Valid runlevels (obtained from \`rc-status --list\`):"
107 rc-status --list
108 exit 1
109 fi
110
111 ################################################################################
112 # Build up a hash of the services associated with each run level. In the most #
113 # trivial case, this is simply the current runlevel. If --all was specified, #
114 # we gather information about all of the runlevels. If --unused was #
115 # specified, we pull info about all of the services and filter for the ones #
116 # that don't appear in any runlevel. #
117 ################################################################################
118 runlevelidxs=$(ls ${runleveldir})
119 declare -a runlevels
120 # For each directory in /etc/runlevels, do ...
121 arridx=0
122 for level in ${runlevelidxs} ; do
123 if [[ ${level} == ${runlevel} || -n ${ALL} ]] ; then
124 runlevels[${arridx}]=$(find ${runleveldir}/${level} -maxdepth 1 -type l -printf '%f ')
125 let "arridx += 1"
126 fi
127 done
128
129 # In case --all was specified, get a list of all the services set up in
130 # /etc/init.d; services can be added, but not enabled, and we need to
131 # identify these 'orphan' services.
132 in_list() { #$1=list $2=find
133 for ele in $1 ; do
134 if [[ ${ele} == $2 ]] ; then
135 echo 1
136 return 0
137 fi
138 done
139 echo 0
140 return 0
141 }
142 if [[ -n ${ALL} ]] ; then
143 unassigned=
144 allservices=
145 for service in $(ls -1 /etc/init.d | grep -v '\.sh$') ; do
146 if [[ $(in_list "${runlevels[*]}" "${service}") -eq 0 ]] ; then
147 unassigned="${unassigned} ${service}"
148 fi
149 allservices="${allservices} ${service}"
150 done
151 runlevelidxs="${runlevelidxs} UNASSIGNED"
152 runlevels[${arridx}]="${unassigned}"
153 runlevels[${arridx}+1]="${allservices}"
154 fi
155
156 ################################################################################
157 # Now collect information about the status of the various services; whether #
158 # they're started, broken, or failed. Put all of this into arrays. #
159 ################################################################################
160 if [[ -x ${svcdir}/started ]]; then
161 started=$(ls ${svcdir}/started)
162 # If we're root then update service statuses incase any naughty daemons
163 # stopped running without our say so
164 if [[ ${EUID} == 0 ]]; then
165 for service in ${started}; do
166 update_service_status "${service}"
167 done
168 started=$(ls ${svcdir}/started)
169 fi
170 fi
171 [[ -x ${svcdir}/starting ]] && starting=$(ls ${svcdir}/starting)
172 [[ -x ${svcdir}/inactive ]] && inactive=$(ls ${svcdir}/inactive)
173 [[ -x ${svcdir}/stopping ]] && stopping=$(ls ${svcdir}/stopping)
174
175 ################################################################################
176 # Now print out the information we've gathered. We do this by going through #
177 # the hash of 'runlevels' information, and for each String key/Array value #
178 # pair, print the runlevel; then for each service in that runlevel, print the #
179 # service name and its status. #
180 ################################################################################
181 # Define a helper method for printing the status of a service; '[ xxx ]'
182 print_msg() {
183 printf " %-$((COLS - 5 - ${#3}))s%s\n" "$1" "${BRACKET}[ $2$3 ${BRACKET}]${NORMAL}"
184 }
185
186 # if --all wasnt specified, dont print everything
187 [[ -z ${ALL} ]] && runlevelidxs=${runlevel}
188 if [[ -z ${UNUSED} ]] ; then
189 if [[ -z ${SERVICELIST} ]] ; then
190 arridx=0
191 else
192 runlevelidxs="all"
193 let "arridx += 1"
194 fi
195 else
196 runlevelidxs="unused"
197 fi
198
199 if [[ -f "/etc/runlevels/${BOOTLEVEL}/.critical" ]]; then
200 boot_crit=
201 for x in $(< "/etc/runlevels/${BOOTLEVEL}/.critical"); do
202 boot_crit="${boot_crit} ${x##*/}"
203 done
204 else
205 boot_crit="checkroot hostname modules checkfs localmount clock"
206 fi
207
208 broken=""
209 for level in ${runlevelidxs} ; do
210 echo "Runlevel: ${HILITE}${level}${NORMAL}"
211 for service in ${runlevels[${arridx}]} ; do
212 if [[ ! -e "${runleveldir}/${level}/${service}" \
213 && ${level} != "UNASSIGNED" \
214 ]] && [[ ${level} != "${BOOTLEVEL}" \
215 || " ${boot_crit} " != *" ${service} "* ]]; then
216 print_msg "${service}" "${BAD}" 'broken '
217 broken="${broken} ${service}"
218 elif [[ -n ${stopping} && $(in_list "${stopping}" "${service}") -eq 1 ]] ; then
219 print_msg "${service}" "${BAD}" 'stopping'
220 elif [[ -n ${starting} && $(in_list "${starting}" "${service}") -eq 1 ]] ; then
221 print_msg "${service}" "${GOOD}" 'starting'
222 elif [[ -n ${inactive} && $(in_list "${inactive}" "${service}") -eq 1 ]] ; then
223 print_msg "${service}" "${WARN}" 'inactive'
224 elif [[ $(in_list "${started}" "${service}") -eq 1 ]] ; then
225 print_msg "${service}" "${GOOD}" 'started '
226 else
227 print_msg "${service}" "${BAD}" 'stopped '
228 fi
229 done
230 let "arridx += 1"
231 [ -n "${UNUSED}" ] && exit 0
232 done
233
234 if [[ -n ${broken} ]]; then
235 eerror "You have some broken symbolic links as reported by the broken"
236 eerror "status above. This can be fixed by removing the broken service"
237 eerror "from its runlevel and re-adding it back using rc-update."
238 fi

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20