Annotation of /trunk/initscripts/sysvinit/rc/functions
Parent Directory | Revision Log
Revision 3 -
(hide annotations)
(download)
Mon Dec 13 22:52:07 2004 UTC (19 years, 9 months ago) by niro
Original Path: trunk/magellan-initscripts/etc/rc.d/init.d/functions
File size: 6078 byte(s)
Mon Dec 13 22:52:07 2004 UTC (19 years, 9 months ago) by niro
Original Path: trunk/magellan-initscripts/etc/rc.d/init.d/functions
File size: 6078 byte(s)
This commit was generated by cvs2svn to compensate for changes in r2, which included commits to RCS files with non-trunk default branches.
1 | niro | 2 | #!/bin/bash |
2 | # Begin $rc_base/init.d/functions - Run Level Control Functions | ||
3 | |||
4 | # Based on functions script from LFS-3.1 and earlier. | ||
5 | # Rewritten by Gerard Beekmans - gerard@linuxfromscratch.org | ||
6 | |||
7 | # With code based on Matthias Benkmann's simpleinit-msb @ | ||
8 | # http://winterdrache.de/linux/newboot/index.html | ||
9 | |||
10 | umask 022 | ||
11 | |||
12 | export PATH="/bin:/usr/bin:/sbin:/usr/sbin" | ||
13 | |||
14 | KILLDELAY=3 | ||
15 | |||
16 | COLUMNS=$(stty size) | ||
17 | COLUMNS=${COLUMNS##* } | ||
18 | COL=$[ $COLUMNS - 10 ] | ||
19 | WCOL=$[ $COLUMNS - 30 ] | ||
20 | WWCOL=$[ $COLUMNS - 40 ] | ||
21 | SET_COL="echo -en \\033[${COL}G" | ||
22 | SET_WCOL="echo -en \\033[${WCOL}G" | ||
23 | SET_WWCOL="echo -en \\033[${WWCOL}G" | ||
24 | CURS_UP="echo -en \\033[A" | ||
25 | |||
26 | NORMAL="echo -en \\033[0;39m" | ||
27 | SUCCESS="echo -en \\033[1;32m" | ||
28 | WARNING="echo -en \\033[1;33m" | ||
29 | FAILURE="echo -en \\033[1;31m" | ||
30 | |||
31 | COLRED="\033[1;6m\033[31m" | ||
32 | COLGREEN="\033[1;6m\033[32m" | ||
33 | COLYELLOW="\033[1;6m\033[33m" | ||
34 | COLBLUE="\033[1;6m\033[34m" | ||
35 | COLMAGENTA="\033[1;6m\033[35m" | ||
36 | COLWHITE="\033[1;6m\033[37m" | ||
37 | COLDEFAULT="\033[0m" | ||
38 | |||
39 | COLOREDSTAR="${COLBLUE}(${COLGREEN}*${COLBLUE})${COLDEFAULT} " | ||
40 | |||
41 | #location to save the started services | ||
42 | svcdir="/var/lib/init.d" | ||
43 | svclib="/lib/rcscripts" | ||
44 | svcmount="no" | ||
45 | svcfstype="tmpfs" | ||
46 | svcsize=1024 | ||
47 | |||
48 | |||
49 | # dummy function; needed if splashutils are not installed | ||
50 | splash() { | ||
51 | return 0 | ||
52 | } | ||
53 | |||
54 | |||
55 | #source splash functions if exists | ||
56 | [ -f /etc/init.d/splash-functions ] && source /etc/init.d/splash-functions | ||
57 | |||
58 | |||
59 | |||
60 | print_error_msg() | ||
61 | { | ||
62 | return 0 | ||
63 | echo | ||
64 | $FAILURE | ||
65 | echo -e " An Error occurred." | ||
66 | echo -e " Script: ${COLWHITE}$i${COLRED}" | ||
67 | echo -e " Exitcode: ${COLWHITE}$error_value${COLRED}" | ||
68 | echo | ||
69 | $NORMAL | ||
70 | echo | ||
71 | echo "Press Enter to continue..." | ||
72 | read | ||
73 | } | ||
74 | |||
75 | check_script_status() | ||
76 | { | ||
77 | if [ ! -f $i ] | ||
78 | then | ||
79 | echo "$i is not a valid symlink" | ||
80 | continue | ||
81 | fi | ||
82 | |||
83 | if [ ! -x $i ] | ||
84 | then | ||
85 | echo "$i is not executable, skipping" | ||
86 | continue | ||
87 | fi | ||
88 | } | ||
89 | |||
90 | update_svcstatus() | ||
91 | { | ||
92 | #$1 script | ||
93 | #$2 start/stop | ||
94 | |||
95 | local SVCDIR_MOUNTED | ||
96 | local SVCD_INITSCRIPT | ||
97 | local x | ||
98 | |||
99 | |||
100 | #do this only if proc is mounted | ||
101 | [ ! -f /proc/mounts ] && return 0 | ||
102 | |||
103 | #check if svcdir is mounted | ||
104 | SVCDIR_MOUNTED="$(cat /proc/mounts | grep ${svcdir} | cut -d ' ' -f2)" | ||
105 | if [ "${SVCDIR_MOUNTED}" == "${svcdir}" ] | ||
106 | then | ||
107 | #check if statedir exists | ||
108 | [ ! -d ${svcdir}/started ] && mkdir ${svcdir}/started | ||
109 | |||
110 | #get real name of the initscript, not from the symlink | ||
111 | if [ -L "$0" ] | ||
112 | then | ||
113 | x="$(readlink $0)" | ||
114 | SVCD_INITSCRIPT="$(basename ${x})" | ||
115 | else | ||
116 | SVCD_INITSCRIPT="$(basename $0)" | ||
117 | fi | ||
118 | |||
119 | case $1 in | ||
120 | start) | ||
121 | #write to svcddir | ||
122 | #write state | ||
123 | echo ok > ${svcdir}/started/"${SVCD_INITSCRIPT}" | ||
124 | ;; | ||
125 | stop) | ||
126 | #echo "$(ls ${svcdir}/started)" | ||
127 | if [ -f ${svcdir}/started/${SVCD_INITSCRIPT} ] | ||
128 | then | ||
129 | rm ${svcdir}/started/${SVCD_INITSCRIPT} | ||
130 | fi | ||
131 | ;; | ||
132 | esac | ||
133 | fi | ||
134 | } | ||
135 | |||
136 | evaluate_retval() | ||
137 | { | ||
138 | error_value=$? | ||
139 | |||
140 | if [ $error_value = 0 ] | ||
141 | then | ||
142 | print_status success | ||
143 | else | ||
144 | print_status failure | ||
145 | fi | ||
146 | |||
147 | return $error_value | ||
148 | } | ||
149 | |||
150 | print_status() | ||
151 | { | ||
152 | if [ $# = 0 ] | ||
153 | then | ||
154 | echo "Usage: $0 {success|warning|failure}" | ||
155 | return 1 | ||
156 | fi | ||
157 | |||
158 | $CURS_UP | ||
159 | |||
160 | case "$1" in | ||
161 | success) | ||
162 | $SET_COL | ||
163 | echo -e -n $COLBLUE"[ " | ||
164 | $SUCCESS | ||
165 | echo -e -n "OK" | ||
166 | echo -e $COLBLUE" ]" | ||
167 | $NORMAL | ||
168 | ;; | ||
169 | warning) | ||
170 | case "$2" in | ||
171 | running) | ||
172 | $SET_WCOL | ||
173 | echo "Already running" | ||
174 | $CURS_UP | ||
175 | ;; | ||
176 | not_running) | ||
177 | $SET_WCOL | ||
178 | echo "Not running" | ||
179 | $CURS_UP | ||
180 | ;; | ||
181 | esac | ||
182 | $SET_COL | ||
183 | echo -e -n $COLBLUE"[ " | ||
184 | $WARNING | ||
185 | echo -e -n "ATTN" | ||
186 | echo -e $COLBLUE" ]" | ||
187 | $NORMAL | ||
188 | ;; | ||
189 | failure) | ||
190 | $SET_COL | ||
191 | echo -e -n $COLBLUE"[" | ||
192 | $FAILURE | ||
193 | echo -e -n "FAILED" | ||
194 | echo -e $COLBLUE"]" | ||
195 | $NORMAL | ||
196 | ;; | ||
197 | esac | ||
198 | } | ||
199 | |||
200 | getpids() | ||
201 | { | ||
202 | base=${1##*/} | ||
203 | pidlist=$(pidof -o $$ -o $PPID -x $base) | ||
204 | } | ||
205 | |||
206 | loadproc() | ||
207 | { | ||
208 | if [ $# = 0 ] | ||
209 | then | ||
210 | echo "Usage: loadproc {program}" | ||
211 | exit 1 | ||
212 | fi | ||
213 | |||
214 | getpids $1 | ||
215 | |||
216 | if [ -z "$pidlist" ] | ||
217 | then | ||
218 | "$@" | ||
219 | evaluate_retval | ||
220 | else | ||
221 | $SET_WCOL | ||
222 | print_status warning running | ||
223 | fi | ||
224 | } | ||
225 | |||
226 | killproc() | ||
227 | { | ||
228 | if [ $# = 0 ] | ||
229 | then | ||
230 | echo "Usage: killproc {program} [signal]" | ||
231 | exit 1 | ||
232 | fi | ||
233 | |||
234 | if [ -z "$2" ]; then | ||
235 | signal=TERM | ||
236 | fallback=KILL | ||
237 | else | ||
238 | signal=${2##-} | ||
239 | signal=${signal##SIG} | ||
240 | fallback="" | ||
241 | fi | ||
242 | |||
243 | getpids $1 | ||
244 | |||
245 | if [ -n "$pidlist" ]; then | ||
246 | failure=0 | ||
247 | |||
248 | for pid in $pidlist | ||
249 | do | ||
250 | kill -$signal $pid 2>/dev/null | ||
251 | |||
252 | for ((i=0; $i<5000; i=$i+1)); do :; done | ||
253 | |||
254 | for ((i=0; $i<$KILLDELAY; i=$i+1)); do | ||
255 | kill -0 $pid 2>/dev/null || break | ||
256 | sleep 1 | ||
257 | done | ||
258 | |||
259 | if [ -n "$fallback" ]; then kill -$fallback $pid 2>/dev/null; fi | ||
260 | |||
261 | kill -0 $pid 2>/dev/null && failure=1 | ||
262 | done | ||
263 | |||
264 | base=${1##*/} | ||
265 | if [ $failure = 0 ]; then rm -f /var/run/$base.pid; fi | ||
266 | |||
267 | (exit $failure) | ||
268 | evaluate_retval | ||
269 | else | ||
270 | $SET_WCOL | ||
271 | print_status warning not_running | ||
272 | fi | ||
273 | } | ||
274 | |||
275 | reloadproc() | ||
276 | { | ||
277 | if [ $# = 0 ] | ||
278 | then | ||
279 | echo "Usage: reloadproc {program} [signal]" | ||
280 | exit 1 | ||
281 | fi | ||
282 | |||
283 | if [ -z "$2" ]; then | ||
284 | signal=HUP | ||
285 | else | ||
286 | signal=${2##-} | ||
287 | signal=${signal##SIG} | ||
288 | |||
289 | fi | ||
290 | |||
291 | getpids $1 | ||
292 | |||
293 | if [ -n "$pidlist" ] | ||
294 | then | ||
295 | failure=0 | ||
296 | |||
297 | for pid in $pidlist | ||
298 | do | ||
299 | kill -$signal $pid || failure=1 | ||
300 | done | ||
301 | |||
302 | (exit $failure) | ||
303 | evaluate_retval | ||
304 | else | ||
305 | $SET_WCOL | ||
306 | print_status warning not_running | ||
307 | fi | ||
308 | } | ||
309 | |||
310 | statusproc() | ||
311 | { | ||
312 | if [ $# = 0 ] | ||
313 | then | ||
314 | echo "Usage: statusproc {program}" | ||
315 | exit 1 | ||
316 | fi | ||
317 | |||
318 | base=${1##*/} | ||
319 | getpids $base | ||
320 | |||
321 | if [ -n "$pidlist" ] | ||
322 | then | ||
323 | echo "$base is running with Process ID(s) $pidlist" | ||
324 | else | ||
325 | if [ -s /var/run/$base.pid ] | ||
326 | then | ||
327 | echo "$1 is not running but /var/run/$base.pid exists" | ||
328 | return 1 | ||
329 | else | ||
330 | echo "$1 is not running" | ||
331 | fi | ||
332 | fi | ||
333 | } | ||
334 | |||
335 | progressbar() | ||
336 | { | ||
337 | if [ $# != 1 ] | ||
338 | then | ||
339 | echo "Usage: progressbar {progress}" | ||
340 | exit 1 | ||
341 | fi | ||
342 | |||
343 | if [ -f /proc/splash ] | ||
344 | then | ||
345 | echo "show $(( 65534 * $1 / 100 ))" > /proc/splash | ||
346 | fi | ||
347 | } | ||
348 | |||
349 | kernel_major_version() | ||
350 | { | ||
351 | local KV | ||
352 | KV="$(uname -r|cut -d. -f1-2)" | ||
353 | echo "${KV}" | ||
354 | } | ||
355 | |||
356 | dolisting() | ||
357 | { | ||
358 | local x= | ||
359 | local y= | ||
360 | local tmpstr= | ||
361 | local mylist= | ||
362 | local mypath="${*}" | ||
363 | |||
364 | if [ "${mypath%/\*}" != "${mypath}" ] | ||
365 | then | ||
366 | mypath="${mypath%/\*}" | ||
367 | fi | ||
368 | |||
369 | for x in ${mypath} | ||
370 | do | ||
371 | [ ! -e "${x}" ] && continue | ||
372 | |||
373 | if [ ! -d "${x}" ] && ( [ -L "${x}" -o -f "${x}" ] ) | ||
374 | then | ||
375 | mylist="${mylist} $(ls "${x}" 2> /dev/null)" | ||
376 | else | ||
377 | [ "${x%/}" != "${x}" ] && x="${x%/}" | ||
378 | |||
379 | cd "${x}"; tmpstr="$(ls)" | ||
380 | |||
381 | for y in ${tmpstr} | ||
382 | do | ||
383 | mylist="${mylist} ${x}/${y}" | ||
384 | done | ||
385 | fi | ||
386 | done | ||
387 | |||
388 | echo "${mylist}" | ||
389 | } |
Properties
Name | Value |
---|---|
svn:executable | * |