/[vps]/baselayout-vserver/trunk/bin/rc-status
Gentoo

Contents of /baselayout-vserver/trunk/bin/rc-status

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20