/[vps]/baselayout-vserver/trunk/sbin/rc-daemon.sh
Gentoo

Contents of /baselayout-vserver/trunk/sbin/rc-daemon.sh

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20