* Fixed problem when BASEDIR paths were given without a trailing '/'.
[linux_from_scratch.git] / bootscripts / lfs / init.d / functions
blob5202c95f65d0682a3f7c807175e10ce998ca2649
1 #!/bin/sh
2 # Begin $rc_base/init.d/functions - Run Level Control Functions
4 # Based on functions script from LFS-3.1 and earlier.
5 # Rewritten by Gerard Beekmans - gerard@linuxfromscratch.org
7 # With code based on Matthias Benkmann's simpleinit-msb @
8 # http://winterdrache.de/linux/newboot/index.html
10 # Number of seconds between term signal and kill signal when stopping processes
11 KILLDELAY=3
13 umask 022
14 export PATH="/bin:/usr/bin:/sbin:/usr/sbin"
16 # Width of the Screen
17 COLUMNS=$(stty size)
18 COLUMNS=${COLUMNS##* }
19 # When using remote connections, such as a serial port, stty size returns 0
20 if [ "$COLUMNS" = "0" ]; then COLUMNS=80; fi
22 # Measurements for positioning result messages
23 COL=$(($COLUMNS - 10))
24 WCOL=$(($COLUMNS - 30))
26 # Set Cursur Position Commands, used via echo -e
27 SET_COL="\\033[${COL}G"
28 SET_WCOL="\\033[${WCOL}G"
29 CURS_UP="\\033[A"
31 # Set color commands, used via echo -e
32 NORMAL="\\033[0;39m"
33 SUCCESS="\\033[1;32m"
34 WARNING="\\033[1;33m"
35 FAILURE="\\033[1;31m"
37 echo_ok()
39 echo -e "$CURS_UP$SET_COL"["$SUCCESS"" OK ""$NORMAL"]
42 echo_failure()
44 echo -e "$CURS_UP$SET_COL"["$FAILURE"FAILED"$NORMAL"]
47 echo_warning()
49 echo -e "$CURS_UP$SET_WCOL$@$SET_COL"["$WARNING" WARN "$NORMAL"]
52 # $i is inherited by the rc script
53 print_error_msg()
55 echo -e -n $FAILURE
56 echo
57 echo "You should not be reading this error message. It means"
58 echo "that an unforseen error took place in $i,"
59 echo "which exited with a return value of $error_value"
60 echo
61 echo "If you're able to track this error down to a bug in one"
62 echo "of the files provided by the LFS book, please be so kind"
63 echo "to inform us at lfs-dev@linuxfromscratch.org"
64 echo -e -n $NORMAL
65 echo
66 echo
67 echo "Press Enter to continue..."
68 read ENTER
71 # $i is inherited by the rc script
72 check_script_status()
74 if [ ! -f $i ]
75 then
76 echo "$i is not a valid symlink"
77 continue
80 if [ ! -x $i ]
81 then
82 echo "$i is not executable, skipping"
83 continue
87 evaluate_retval()
89 error_value=$?
91 if [ $error_value = 0 ]
92 then
93 print_status success
94 else
95 print_status failure
96 sleep 5
99 return 0
100 #return $error_value
103 print_status()
105 if [ $# = 0 ]
106 then
107 echo "Usage: $0 {success|warning|failure}"
108 return 1
111 case "$1" in
112 success)
113 echo_ok
115 warning)
116 case "$2" in
117 running)
118 echo_warning "Already running"
120 not_running)
121 echo_warning "Not running"
123 esac
125 failure)
126 echo_failure
128 esac
131 # Returns all of the pid #'s for $1 process
132 getpids()
134 base=${1##*/}
135 local lpids=""
136 local pid
138 pidlist=""
139 lpids=$(pidof $base)
140 for pid in $lpids
142 if [ $pid -ne $$ ] && [ $pid -ne $PPID ]
143 then
144 pidlist="$pidlist $pid"
146 done
149 # Starts a program if it is currently not running
150 loadproc()
152 if [ $# = 0 ]
153 then
154 echo "Usage: loadproc {program}"
155 exit 1
158 getpids $1
160 if [ -z "$pidlist" ]
161 then
162 "$@"
163 evaluate_retval
164 else
165 print_status warning running
169 # Stops a process if it is running
170 killproc()
172 if [ $# = 0 ]
173 then
174 echo "Usage: killproc {program} [signal]"
175 exit 1
178 if [ -z "$2" ]; then
179 signal=TERM
180 fallback=KILL
181 else
182 signal=${2##-}
183 signal=${signal##SIG}
184 fallback=""
187 getpids $1
189 if [ -n "$pidlist" ]; then
190 local i=0
192 for pid in $pidlist
194 kill -$signal $pid 2>/dev/null
196 while [ $i -lt $KILLDELAY ]; do
197 kill -0 $pid 2>/dev/null || break
198 sleep 1
199 i=$(($i+1))
200 done
202 if [ -n "$fallback" ]; then kill -$fallback $pid 2>/dev/null; fi
204 kill -0 $pid 2>/dev/null
205 done
207 getpids $1
209 base=${1##*/}
211 if [ -n "$pidlist" ]; then
212 failure=1
213 else
214 failure=0
215 rm -f /var/run/$base.pid
218 (exit $failure)
219 evaluate_retval
220 else
221 print_status warning not_running
225 reloadproc()
227 if [ $# = 0 ]
228 then
229 echo "Usage: reloadproc {program} [signal]"
230 exit 1
233 if [ -z "$2" ]; then
234 signal=HUP
235 else
236 signal=${2##-}
237 signal=${signal##SIG}
241 getpids $1
243 if [ -n "$pidlist" ]
244 then
245 failure=0
247 for pid in $pidlist
249 kill -$signal $pid || failure=1
250 done
252 (exit $failure)
253 evaluate_retval
254 else
255 print_status warning not_running
259 statusproc()
261 if [ $# = 0 ]
262 then
263 echo "Usage: statusproc {program}"
264 exit 1
267 base=${1##*/}
268 getpids $base
270 if [ -n "$pidlist" ]
271 then
272 echo "$base is running with Process ID(s) $pidlist"
273 else
274 if [ -s /var/run/$base.pid ]
275 then
276 echo "$1 is not running but /var/run/$base.pid exists"
277 return 1
278 else
279 echo "$1 is not running"
284 # End $rc_base/init.d/functions