/[baselayout]/branches/baselayout-1_12/sbin/rc-daemon.sh
Gentoo

Contents of /branches/baselayout-1_12/sbin/rc-daemon.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1809 - (show annotations) (download) (as text)
Thu Jan 12 11:28:13 2006 UTC (8 years, 11 months ago) by uberlord
File MIME type: text/x-sh
File size: 10964 byte(s)
Always call rc_stop_daemon on start failure to clean up
1 # Copyright 1999-2006 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # RC functions to work with daemons
5 # Basically we're a fancy wrapper for start-stop-daemon
6 # and should be called as such. This means that our init scripts
7 # should work as is with zero modification :)
8
9 # Actually, the above is a small as lie we have some init scripts which try to
10 # get start-stop-daemon to launch a shell script. While this does work with
11 # the start-stop-daemon program in /sbin, it does cause a problem for us
12 # when we're testing for the daemon to be running. I (Roy Marples) view this
13 # as behaviour by design as start-stop-daemon should not be used to run shell
14 # scripts!
15 # At the time of writing, the only culprit I know of is courier-imap.
16 # There may be others!
17
18 RC_GOT_DAEMON="yes"
19
20 [[ ${RC_GOT_FUNCTIONS} != "yes" ]] && source /sbin/functions.sh
21 [[ ${RC_GOT_SERVICES} != "yes" ]] && source "${svclib}/sh/rc-services.sh"
22
23 RC_RETRY_KILL="no"
24 RC_RETRY_TIMEOUT=1
25 RC_RETRY_COUNT=5
26 RC_FAIL_ON_ZOMBIE="no"
27 RC_KILL_CHILDREN="no"
28 RC_WAIT_ON_START="0.1"
29
30 # void rc_shift_args(void)
31 #
32 # Proccess vars - makes things easier by using the shift command
33 # and indirect variables
34 rc_shift_args() {
35 local addvar
36
37 while [[ $# != "0" ]]; do
38 if [[ $1 != "-"* && -n ${addvar} ]]; then
39 if [[ -z ${!addvar} ]]; then
40 eval "${addvar}=\"$1\""
41 else
42 eval "${addvar}=\"${!addvar} $1\""
43 fi
44 shift
45 continue
46 fi
47 unset addvar
48 case "$1" in
49 -S|--start)
50 stopping=false
51 ;;
52 -K|--stop)
53 stopping=true
54 ;;
55 -n|--name)
56 addvar="name"
57 ;;
58 -x|--exec|-a|--startas)
59 addvar="cmd"
60 ;;
61 -p|--pidfile)
62 addvar="pidfile"
63 ;;
64 --pidfile=*)
65 pidfile="${1##--pidfile=}"
66 ;;
67 --pid=*)
68 pidfile="${1##--pid=}"
69 ;;
70 -R|--retry)
71 unset RC_RETRY_COUNT
72 addvar="RC_RETRY_COUNT"
73 ;;
74 -s|--signal)
75 addvar="signal"
76 ;;
77 -t|--test|-o|--oknodo)
78 nothing=true
79 ;;
80 esac
81 shift
82 done
83
84 [[ -z ${RC_RETRY_COUNT} ]] && RC_RETRY_COUNT=5
85 }
86
87 # void rc_setup_daemon_vars(void)
88 #
89 # Setup our vars based on the start-stop-daemon command
90 rc_setup_daemon_vars() {
91 local name i
92 local -a sargs=( "${args%% \'--\' *}" )
93 local -a eargs
94 local x="${args// \'--\' /}"
95 [[ ${x} != "${args}" ]] && eargs=( "${args##* \'--\' }" )
96
97 eval rc_shift_args "${sargs[@]}"
98
99 [[ -z ${cmd} ]] && cmd="${name}"
100
101 # We may want to launch the daemon with a custom command
102 # This is mainly useful for debugging with apps like valgrind, strace
103 local bash_service=$( bash_variable "${myservice}" )
104 eval x=\"\$\{RC_DAEMON_${bash_service}\}\"
105 if [[ -n ${x} ]]; then
106 local -a d=( ${x} )
107 if ${stopping}; then
108 args="--stop"
109 else
110 args="--start"
111 fi
112
113 # Add -- or - arguments as s-s-d options
114 j=${#d[@]}
115 for (( i=0; i<j; i++ )); do
116 [[ ${d[i]:0:1} != "-" ]] && break
117 args="${args} ${d[i]}"
118 unset d[i]
119 done
120 d=$( "${d[@]}" )
121
122 eval args=\"${args} --exec '${d[0]}' -- ${d[@]:1} '${cmd}' ${eargs[@]}\"
123 ! ${stopping} && cmd="${d[0]}"
124 fi
125
126 return 0
127 }
128
129 # bool rc_try_kill_pid(int pid, char* signal, bool session)
130 #
131 # Repeatedly kill the pid with the given signal until it dies
132 # If session is true then we use tread pid as session and send it
133 # via pkill
134 # Returns 0 if successfuly otherwise 1
135 rc_try_kill_pid() {
136 local pid="$1" signal="${2:-TERM}" session="${3:-false}" i s p e
137
138 # We split RC_RETRY_TIMEOUT into tenths of seconds
139 # So we return as fast as possible
140 s=$(( ${RC_RETRY_TIMEOUT}/10 )).$(( ${RC_RETRY_TIMEOUT}%10 ))
141
142 for (( i=0; i<RC_RETRY_COUNT*10; i++ )); do
143 if ${session} ; then
144 if [[ -x /usr/bin/pkill ]]; then
145 pkill "-${signal}" -s "${pid}"
146 pgrep -s "${pid}" >/dev/null || return 0
147 else
148 local pids=$(ps -eo pid,sid | sed -n 's/'${pid}'$//p')
149 [[ -z ${pids} ]] && return 0
150 kill -s "${signal}" ${pids} 2>/dev/null
151 e=false
152 for p in ${pids}; do
153 if [[ -d "/proc/${p}" ]]; then
154 e=true
155 break
156 fi
157 done
158 ${e} || return 0
159 fi
160 else
161 kill -s "${signal}" "${pid}" 2>/dev/null
162 [[ ! -d "/proc/${pid}" ]] && return 0
163 fi
164 LC_ALL=C /bin/sleep "${s}"
165 done
166
167 return 1
168 }
169
170 # bool rc_kill_pid(int pid, bool session)
171 #
172 # Kills the given pid/session
173 # Returns 1 if we fail to kill the pid (if it's valid) otherwise 0
174 rc_kill_pid() {
175 local pid="$1" session="${2:-false}"
176
177 rc_try_kill_pid "${pid}" "${signal}" "${session}" && return 0
178
179 [[ ${RC_RETRY_KILL} == "yes" ]] \
180 && rc_try_kill_pid "${pid}" KILL "${session}" && return 0
181
182 return 1
183 }
184
185 # char* pidof(char* cmd, ...)
186 #
187 # Returns a space seperated list of pids associated with the command
188 # This is to handle the rpc.nfsd program which acts weird
189 pidof() {
190 local arg args
191
192 for arg in "$@"; do
193 [[ ${arg##*/} == "rpc.nfsd" ]] && arg="${arg%/*}/nfsd"
194 args="${args} '"${arg}"'"
195 done
196
197 eval /bin/pidof -x ${args}
198 }
199
200 # bool is_daemon_running(char* cmd, char* pidfile)
201 #
202 # Returns 0 if the given daemon is running, otherwise 1
203 # If a pidfile is supplied, the pid inside it must match
204 # a pid in the list of pidof ${cmd}
205 is_daemon_running() {
206 local cmd pidfile pids pid
207
208 if [[ $# == "1" ]]; then
209 cmd="$1"
210 else
211 local i j="$#"
212 for (( i=0; i<j-1; i++ )); do
213 cmd="${cmd} $1"
214 shift
215 done
216 pidfile="$1"
217 fi
218
219 pids=$( pidof ${cmd} )
220 [[ -z ${pids} ]] && return 1
221
222 [[ -s ${pidfile} ]] || return 0
223
224 read pid < "${pidfile}"
225 pids=" ${pids} "
226 [[ ${pids// ${pid} /} != "${pids}" ]]
227 }
228
229 # int rc_start_daemon(void)
230 #
231 # We don't do anyting fancy - just pass the given options
232 # to start-stop-daemon and return the value
233 rc_start_daemon() {
234 eval /sbin/start-stop-daemon "${args}"
235 local retval="$?"
236
237 [[ ${retval} != "0" ]] && return "${retval}"
238 [[ ${RC_WAIT_ON_START} == "0" ]] && return "${retval}"
239
240 # Give the daemon upto 1 second to fork after s-s-d returns
241 # Some daemons like acpid and vsftpd need this when system is under load
242 # Seems to be only daemons that do not create pid files though ...
243 local i=0
244 for ((i=0; i<10; i++)); do
245 is_daemon_running ${cmd} "${pidfile}" && break
246 LC_ALL=C /bin/sleep "0.1"
247 done
248
249 # We pause for RC_WAIT_ON_START seconds and then
250 # check if the daemon is still running - this is mainly
251 # to handle daemons who launch and then fail due to invalid
252 # configuration files
253 LC_ALL=C /bin/sleep "${RC_WAIT_ON_START}"
254 is_daemon_running ${cmd} "${pidfile}"
255 retval="$?"
256 [[ ${retval} == "0" ]] && return 0
257
258 # Stop if we can to clean things up
259 [[ -n ${pidfile} ]] && rc_stop_daemon
260
261 return "${retval}"
262 }
263
264 # bool rc_stop_daemon(void)
265 #
266 # Instead of calling start-stop-daemon we instead try and
267 # kill the process ourselves and any children left over
268 # Returns 0 if everything was successful otherwise 1
269 rc_stop_daemon() {
270 local pid pids retval="0"
271
272 if [[ -n ${cmd} ]]; then
273 if ! is_daemon_running ${cmd} "${pidfile}" ; then
274 [[ ${RC_FAIL_ON_ZOMBIE} == "yes" ]] && return 1
275 fi
276 pids=$( pidof ${cmd} )
277 fi
278
279 if [[ -s ${pidfile} ]]; then
280 read pid < "${pidfile}"
281 # Check that the given program is actually running the pid
282 if [[ -n ${pids} ]]; then
283 pids=" ${pids} "
284 [[ ${pids// ${pid} /} == "${pids}" ]] && return 1
285 fi
286 pids="${pid}"
287 fi
288
289 # If there's nothing to kill then return without error
290 [[ -z ${pids} ]] && return 0
291
292 # We may not have pgrep to find our children, so we provide
293 # two methods
294 if [[ ${RC_KILL_CHILDREN} == "yes" ]]; then
295 if [[ -x /usr/bin/pgrep ]]; then
296 pids="${pids} $(pgrep -P ${pids// /,})"
297 else
298 local npids
299 for pid in ${pids} ; do
300 npids="${npids} $(ps -eo pid,ppid | sed -n 's/'${pid}'$//p')"
301 done
302 pids="${pids} ${npids}"
303 fi
304 fi
305
306 for pid in ${pids}; do
307 if [[ ${RC_FAIL_ON_ZOMBIE} == "yes" ]]; then
308 ps -p "${pid}" &>/dev/null || return 1
309 fi
310
311 if rc_kill_pid "${pid}" false ; then
312 # Remove the pidfile if the process didn't
313 [[ -f ${pidfile} ]] && rm -f "${pidfile}"
314 else
315 retval=1
316 fi
317
318 if [[ ${RC_KILL_CHILDREN} == "yes" ]]; then
319 rc_kill_pid "${pid}" true || retval=1
320 fi
321 done
322
323 return "${retval}"
324 }
325
326 # void update_service_status(char *service)
327 #
328 # Loads the service state file and ensures that all listed daemons are still
329 # running - hopefully on their correct pids too
330 # If not, we stop the service
331 update_service_status() {
332 local service="$1" daemonfile="${svcdir}/daemons/$1" i
333 local -a RC_DAEMONS=() RC_PIDFILES=()
334
335 # We only care about marking started services as stopped if the daemon(s)
336 # for it are no longer running
337 ! service_started "${service}" && return
338 [[ ! -f ${daemonfile} ]] && return
339
340 # OK, now check that every daemon launched is active
341 # If the --start command was any good a pidfile was specified too
342 source "${daemonfile}"
343 for (( i=0; i<${#RC_DAEMONS[@]}; i++ )); do
344 if ! is_daemon_running ${RC_DAEMONS[i]} "${RC_PIDFILES[i]}" ; then
345 if [[ -e "/etc/init.d/${service}" ]]; then
346 ( /etc/init.d/"${service}" stop &>/dev/null )
347 break
348 fi
349 fi
350 done
351 }
352
353 # int start-stop-daemon(...)
354 #
355 # Provide a wrapper to start-stop-daemon
356 # Return the result of start_daemon or stop_daemon depending on
357 # how we are called
358 start-stop-daemon() {
359 local args=$( requote "$@" ) result i
360 local cmd pidfile pid stopping signal nothing=false
361 local daemonfile="${svcdir}/daemons/${myservice}"
362 local -a RC_DAEMONS=() RC_PIDFILES=()
363
364 [[ -e ${daemonfile} ]] && source "${daemonfile}"
365
366 rc_setup_daemon_vars
367
368 # We pass --oknodo and --test directly to start-stop-daemon and return
369 if ${nothing}; then
370 eval /sbin/start-stop-daemon "${args}"
371 return "$?"
372 fi
373
374 if ${stopping}; then
375 rc_stop_daemon
376 result="$?"
377 if [[ ${result} == "0" ]]; then
378 # We stopped the daemon successfully
379 # so we remove it from our state
380 for (( i=0; i<${#RC_DAEMONS[@]}; i++ )); do
381 # We should really check for valid cmd AND pidfile
382 # But most called to --stop only set the pidfile
383 if [[ ${RC_DAEMONS[i]} == "{cmd}" \
384 || ${RC_PIDFILES[i]}="${pidfile}" ]]; then
385 unset RC_DAEMONS[i] RC_PIDFILES[i]
386 RC_DAEMONS=( "${RC_DAEMONS[@]}" )
387 RC_PIDFILES=( "${RC_PIDFILES[@]}" )
388 break
389 fi
390 done
391 fi
392 else
393 rc_start_daemon
394 result="$?"
395 if [[ ${result} == "0" ]]; then
396 # We started the daemon sucessfully
397 # so we add it to our state
398 local max="${#RC_DAEMONS[@]}"
399 for (( i=0; i<${max}; i++ )); do
400 if [[ ${RC_DAEMONS[i]} == "{cmd}" \
401 && ${RC_PIDFILES[i]}="${pidfile}" ]]; then
402 break
403 fi
404 done
405
406 if [[ ${i} == "${max}" ]]; then
407 RC_DAEMONS[max]="${cmd}"
408 RC_PIDFILES[max]="${pidfile}"
409 fi
410 fi
411 fi
412
413 # Write the new list of daemon states for this service
414 if [[ ${#RC_DAEMONS[@]} == "0" ]]; then
415 [[ -f ${daemonfile} ]] && rm -f "${daemonfile}"
416 else
417 echo "RC_DAEMONS[0]=\"${RC_DAEMONS[0]}\"" > "${daemonfile}"
418 echo "RC_PIDFILES[0]=\"${RC_PIDFILES[0]}\"" >> "${daemonfile}"
419
420 for (( i=1; i<${#RC_DAEMONS[@]}; i++ )); do
421 echo "RC_DAEMONS[${i}]=\"${RC_DAEMONS[i]}\"" >> "${daemonfile}"
422 echo "RC_PIDFILES[${i}]=\"${RC_PIDFILES[i]}\"" >> "${daemonfile}"
423 done
424 fi
425
426 return "${result}"
427 }
428
429 # vim:ts=4

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20