1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
|
#!/bin/bash
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# Common functions
[[ ${RC_GOT_FUNCTIONS} != "yes" ]] && source /sbin/functions.sh
# User must be root to run most script stuff (except status)
if [[ ${EUID} != "0" ]] && ! [[ $2 == "status" && $# -eq 2 ]] ; then
eerror "$0: must be root to run init scripts"
exit 1
fi
myscript="$1"
if [[ -L $1 && ! -L "/etc/init.d/${1##*/}" ]] ; then
SVCNAME="$(readlink "$1")"
else
SVCNAME="$1"
fi
declare -r SVCNAME="${SVCNAME##*/}"
export SVCNAME
# Support deprecated myservice variable
myservice="${SVCNAME}"
# coldplug events can trigger init scripts, but we don't want to run them
# until after rc sysinit has completed so we punt them to the boot runlevel
if [[ -e /dev/.rcsysinit ]] ; then
eerror "ERROR: cannot run ${SVCNAME} until sysinit completes"
eerror "${SVCNAME} will be started in the ${BOOTLEVEL} runlevel"
if [[ ! -L /dev/.rcboot/"${SVCNAME}" ]] ; then
[[ ! -d /dev/.rcboot ]] && mkdir /dev/.rcboot
ln -snf "$1" /dev/.rcboot/"${SVCNAME}"
fi
exit 1
fi
svc_trap() {
trap 'eerror "ERROR: ${SVCNAME} caught an interrupt"; exit 1' \
INT QUIT TSTP
}
# Setup a default trap
svc_trap
# State variables
svcpause="no"
svcrestart="no"
# Functions to handle dependencies and services
[[ ${RC_GOT_SERVICES} != "yes" ]] && source "${svclib}/sh/rc-services.sh"
# Functions to control daemons
[[ ${RC_GOT_DAEMON} != "yes" ]] && source "${svclib}/sh/rc-daemon.sh"
if [[ ${SVCNAME%%.*} == "net" && ${SVCNAME#*.} != "${SVCNAME}" ]] ; then
NETSERVICE="yes"
else
NETSERVICE="no"
fi
# Check if the textdomain is non-default
search_lang="${LC_ALL:-${LC_MESSAGES:-${LANG}}}"
[[ -f ${TEXTDOMAINDIR}/${search_lang%.*}/LC_MESSAGES/${myservice}.mo ]] \
&& TEXTDOMAIN="${myservice}"
# Source configuration files.
# (1) Source /etc/conf.d/net if it is a net.* service
# (2) Source /etc/conf.d/${SVCNAME} to get initscript-specific
# configuration (if it exists).
# (3) Source /etc/rc.conf to pick up potentially overriding
# configuration, if the system administrator chose to put it
# there (if it exists).
if [[ ${NETSERVICE} == "yes" ]] ; then
conf="$(add_suffix /etc/conf.d/net)"
[[ -e ${conf} ]] && source "${conf}"
fi
conf="$(add_suffix "/etc/conf.d/${SVCNAME}")"
[[ -e ${conf} ]] && source "${conf}"
conf="$(add_suffix /etc/rc.conf)"
[[ -e ${conf} ]] && source "${conf}"
mylevel="${SOFTLEVEL}"
[[ ${SOFTLEVEL} == "${BOOTLEVEL}" \
|| ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] \
&& mylevel="${DEFAULTLEVEL}"
# Call svc_quit if we abort AND we have obtained a lock
service_started "${SVCNAME}"
svcstarted="$?"
service_inactive "${SVCNAME}"
svcinactive="$?"
svc_quit() {
eerror "ERROR: ${SVCNAME} caught an interrupt"
if service_inactive "${SVCNAME}" || [[ ${svcinactive} == "0" ]] ; then
mark_service_inactive "${SVCNAME}"
elif [[ ${svcstarted} == "0" ]] ; then
mark_service_started "${SVCNAME}"
else
mark_service_stopped "${SVCNAME}"
fi
exit 1
}
usage() {
local IFS="|"
myline="Usage: ${SVCNAME} { $* "
echo
eerror "${myline}}"
eerror " ${SVCNAME} without arguments for full help"
}
stop() {
# Return success so the symlink gets removed
return 0
}
start() {
eerror "ERROR: ${SVCNAME} does not have a start function."
# Return failure so the symlink doesn't get created
return 1
}
restart() {
svc_restart
}
status() {
# Dummy function
return 0
}
svc_schedule_start() {
local service="$1" start="$2"
[[ ! -d "${svcdir}/scheduled/${service}" ]] \
&& mkdir -p "${svcdir}/scheduled/${service}"
[[ ! -e "${svcdir}/scheduled/${service}/${start}" ]] \
&& ln -snf "/etc/init.d/${service}" \
"${svcdir}/scheduled/${service}/${start}"
}
svc_start_scheduled() {
[[ ! -d "${svcdir}/scheduled/${SVCNAME}" ]] && return
local x= services=
for x in $(dolisting "${svcdir}/scheduled/${SVCNAME}/") ; do
services="${services} ${x##*/}"
done
for x in ${services} ; do
service_stopped "${x}" && start_service "${x}"
rm -f "${svcdir}/scheduled/${SVCNAME}/${x}"
done
rmdir "${svcdir}/scheduled/${SVCNAME}"
}
svc_stop() {
local x= mydep= mydeps= retval=0
local -a servicelist=()
# Do not try to stop if it had already failed to do so
if is_runlevel_stop && service_failed "${SVCNAME}" ; then
return 1
elif service_stopped "${SVCNAME}" ; then
ewarn "WARNING: ${SVCNAME} has not yet been started."
return 0
fi
if ! mark_service_stopping "${SVCNAME}" ; then
eerror "ERROR: ${SVCNAME} is already stopping."
return 1
fi
# Ensure that we clean up if we abort for any reason
trap "svc_quit" INT QUIT TSTP
mark_service_starting "${SVCNAME}"
service_message "Service ${SVCNAME} stopping"
if in_runlevel "${SVCNAME}" "${BOOTLEVEL}" && \
[[ ${SOFTLEVEL} != "reboot" && ${SOFTLEVEL} != "shutdown" ]] ; then
ewarn "WARNING: you are stopping a boot service."
fi
if [[ ${svcpause} != "yes" && ${RC_NO_DEPS} != "yes" ]] ; then
if [[ ${NETSERVICE} == "yes" ]] ; then
# A net.* service
if in_runlevel "${SVCNAME}" "${BOOTLEVEL}" || \
in_runlevel "${SVCNAME}" "${mylevel}" ; then
# Only worry about net.* services if this is the last one
# running or if RC_NET_STRICT_CHECKING is set ...
! is_net_up && mydeps="net"
fi
mydeps="${mydeps} ${SVCNAME}"
else
mydeps="${SVCNAME}"
fi
fi
# Save the IN_BACKGROUND var as we need to clear it for stopping depends
local ib_save="${IN_BACKGROUND}"
unset IN_BACKGROUND
for mydep in ${mydeps} ; do
for x in $(needsme "${mydep}") ; do
# Service not currently running, continue
if service_started "${x}" ; then
stop_service "${x}"
service_list=( "${service_list[@]}" "${x}" )
fi
done
done
for x in "${service_list[@]}" ; do
# We need to test if the service has been marked stopped
# as the fifo may still be around if called by custom code
# such as postup from a net script.
service_stopped "${mynetservice}" && continue
wait_service "${x}"
if ! service_stopped "${x}" ; then
retval=1
break
fi
done
IN_BACKGROUND="${ib_save}"
if [[ ${retval} != "0" ]] ; then
eerror "ERROR: problems stopping dependent services."
eerror " ${SVCNAME} is still up."
else
# Now that deps are stopped, stop our service
(
exit() {
RC_QUIET_STDOUT="no"
eerror "DO NOT USE EXIT IN INIT.D SCRIPTS"
eerror "This IS a bug, please fix your broken init.d"
unset -f exit
exit "$@"
}
# Stop einfo/ebegin/eend from working as parallel messes us up
[[ ${RC_PARALLEL_STARTUP} == "yes" ]] && RC_QUIET_STDOUT="yes"
stop
)
retval="$?"
# If a service has been marked inactive, exit now as something
# may attempt to start it again later
if [[ ${retval} == "0" ]] && service_inactive "${SVCNAME}" ; then
svcinactive=0
return 0
fi
fi
if [[ ${retval} != 0 ]] ; then
# Did we fail to stop? create symlink to stop multible attempts at
# runlevel change. Note this is only used at runlevel change ...
is_runlevel_stop && mark_service_failed "${SVCNAME}"
# If we are halting the system, do it as cleanly as possible
if [[ ${SOFTLEVEL} == "reboot" || ${SOFTLEVEL} == "shutdown" ]] ; then
mark_service_stopped "${SVCNAME}"
else
if [[ ${svcinactive} == "0" ]] ; then
mark_service_inactive "${SVCNAME}"
else
mark_service_started "${SVCNAME}"
fi
fi
service_message "eerror" "ERROR: ${SVCNAME} failed to stop"
else
svcstarted=1
if service_inactive "${SVCNAME}" ; then
svcinactive=0
else
mark_service_stopped "${SVCNAME}"
fi
service_message "Service ${SVCNAME} stopped"
fi
# Reset the trap
svc_trap
return "${retval}"
}
svc_start() {
local x= y= retval=0 startfail= startinactive=
# Do not try to start if i have done so already on runlevel change
if is_runlevel_start && service_failed "${SVCNAME}" ; then
return 1
elif service_started "${SVCNAME}" ; then
ewarn "WARNING: ${SVCNAME} has already been started."
return 0
elif service_inactive "${SVCNAME}" ; then
if [[ ${IN_BACKGROUND} != "true" ]] ; then
ewarn "WARNING: ${SVCNAME} has already been started."
return 0
fi
fi
if ! mark_service_starting "${SVCNAME}" ; then
if service_stopping "${SVCNAME}" ; then
eerror "ERROR: ${SVCNAME} is already stopping."
else
eerror "ERROR: ${SVCNAME} is already starting."
fi
return 1
fi
# Ensure that we clean up if we abort for any reason
trap "svc_quit" INT QUIT TSTP
service_message "Service ${SVCNAME} starting"
if broken "${SVCNAME}" ; then
eerror "ERROR: Some services needed are missing. Run"
eerror " './${SVCNAME} broken' for a list of those"
eerror " services. ${SVCNAME} was not started."
retval=1
fi
# Save the IN_BACKGROUND var as we need to clear it for starting depends
local ib_save="${IN_BACKGROUND}"
unset IN_BACKGROUND
if [[ ${retval} == "0" && ${RC_NO_DEPS} != "yes" ]] ; then
local startupservices="$(ineed "${SVCNAME}") $(valid_iuse "${SVCNAME}")"
local netservices=
for x in $(dolisting "/etc/runlevels/${BOOTLEVEL}/net.*") \
$(dolisting "/etc/runlevels/${mylevel}/net.*") ; do
netservices="${netservices} ${x##*/}"
done
# Start dependencies, if any.
if ! is_runlevel_start ; then
for x in ${startupservices} ; do
if [[ ${x} == "net" && ${NETSERVICE} != "yes" ]] && ! is_net_up ; then
for y in ${netservices} ; do
service_stopped "${y}" && start_service "${y}"
done
elif [[ ${x} != "net" ]] ; then
if service_stopped "${x}" ; then
start_service "${x}"
fi
fi
done
fi
# We also wait for any services we're after to finish incase they
# have a "before" dep but we don't dep on them.
if is_runlevel_start ; then
startupservices="${startupservices} $(valid_iafter "${SVCNAME}")"
fi
if [[ " ${startupservices} " == *" net "* ]] ; then
startupservices=" ${startupservices} "
startupservices="${startupservices/ net / ${netservices} }"
startupservices="${startupservices// net /}"
fi
# Wait for dependencies to finish.
for x in ${startupservices} ; do
service_started "${x}" && continue
wait_service "${x}"
if ! service_started "${x}" ; then
# A 'need' dependency is critical for startup
if ineed -t "${SVCNAME}" "${x}" >/dev/null \
|| net_service "${x}" && ineed -t "${SVCNAME}" net \
&& ! is_net_up ; then
if service_inactive "${x}" || service_wasinactive "${x}" || \
[[ -n $(ls "${svcdir}"/scheduled/*/"${x}" 2>/dev/null) ]] ; then
svc_schedule_start "${x}" "${SVCNAME}"
[[ -n ${startinactive} ]] && startinactive="${startinactive}, "
startinactive="${startinactive}${x}"
else
startfail="${x}"
break
fi
fi
fi
done
if [[ -n ${startfail} ]] ; then
eerror "ERROR: Problem starting needed service ${startfail}"
eerror " ${SVCNAME} was not started."
retval=1
elif [[ -n ${startinactive} ]] ; then
# Change the last , to or for correct grammar.
x="${startinactive##*, }"
startinactive="${startinactive/%, ${x}/ or ${x}}"
ewarn "WARNING: ${SVCNAME} is scheduled to start when ${startinactive} has started."
retval=1
fi
fi
if [[ ${retval} == "0" ]] ; then
IN_BACKGROUND="${ib_save}"
(
exit() {
RC_QUIET_STDOUT="no"
eerror "DO NOT USE EXIT IN INIT.D SCRIPTS"
eerror "This IS a bug, please fix your broken init.d"
unset -f exit
exit "$@"
}
# Apply any ulimits if defined
[[ -n ${RC_ULIMIT} ]] && ulimit ${RC_ULIMIT}
# Stop einfo/ebegin/eend from working as parallel messes us up
[[ ${RC_PARALLEL_STARTUP} == "yes" ]] && RC_QUIET_STDOUT="yes"
start
)
retval="$?"
# If a service has been marked inactive, exit now as something
# may attempt to start it again later
if [[ ${retval} == "0" ]] && service_inactive "${SVCNAME}" ; then
svcinactive=0
service_message "ewarn" "WARNING: ${SVCNAME} has started but is inactive"
return 1
fi
fi
if [[ ${retval} != "0" ]] ; then
if [[ ${svcinactive} == "0" ]] ; then
mark_service_inactive "${SVCNAME}"
else
mark_service_stopped "${SVCNAME}"
fi
if [[ -z ${startinactive} ]] ; then
is_runlevel_start && mark_service_failed "${SVCNAME}"
service_message "eerror" "ERROR: ${SVCNAME} failed to start"
fi
else
svcstarted=0
mark_service_started "${SVCNAME}"
service_message "Service ${SVCNAME} started"
fi
# Reset the trap
svc_trap
return "${retval}"
}
svc_restart() {
if ! service_stopped "${SVCNAME}" ; then
svc_stop || return "$?"
fi
svc_start
}
svc_status() {
# The basic idea here is to have some sort of consistent
# output in the status() function which scripts can use
# as an generic means to detect status. Any other output
# should thus be formatted in the custom status() function
# to work with the printed " * status: foo".
local efunc="" state=""
# If we are effectively root, check to see if required daemons are running
# and update our status accordingly
[[ ${EUID} == 0 ]] && update_service_status "${SVCNAME}"
if service_stopping "${SVCNAME}" ; then
efunc="eerror"
state="stopping"
elif service_starting "${SVCNAME}" ; then
efunc="einfo"
state="starting"
elif service_inactive "${SVCNAME}" ; then
efunc="ewarn"
state="inactive"
elif service_started "${SVCNAME}" ; then
efunc="einfo"
state="started"
else
efunc="eerror"
state="stopped"
fi
[[ ${RC_QUIET_STDOUT} != "yes" ]] \
&& ${efunc} "status: ${state}"
status
# Return 0 if started, otherwise 1
[[ ${state} == "started" ]]
}
rcscript_errors="$(bash -n "${myscript}" 2>&1)" || {
[[ -n ${rcscript_errors} ]] && echo "${rcscript_errors}" >&2
eerror "ERROR: ${myscript} has syntax errors in it; aborting ..."
exit 1
}
# set *after* wrap_rcscript, else we get duplicates.
opts="start stop restart"
source "${myscript}"
# make sure whe have valid $opts
if [[ -z ${opts} ]] ; then
opts="start stop restart"
fi
svc_homegrown() {
local x arg="$1"
shift
# Walk through the list of available options, looking for the
# requested one.
for x in ${opts} ; do
if [[ ${x} == "${arg}" ]] ; then
if typeset -F "${x}" &>/dev/null ; then
# Run the homegrown function
"${x}"
return $?
fi
fi
done
x=""
# If we're here, then the function wasn't in $opts.
[[ -n $* ]] && x="/ $* "
eerror "ERROR: wrong args ( "${arg}" ${x})"
# Do not quote this either ...
usage ${opts}
exit 1
}
shift
if [[ $# -lt 1 ]] ; then
eerror "ERROR: not enough args."
usage ${opts}
exit 1
fi
for arg in $* ; do
case "${arg}" in
--quiet)
RC_QUIET="yes"
RC_QUIET_STDOUT="yes"
;;
# We check this in functions.sh ...
# --nocolor)
# RC_NOCOLOR="yes"
# ;;
--nodeps)
RC_NO_DEPS="yes"
;;
--verbose)
RC_VERBOSE="yes"
;;
esac
done
retval=0
for arg in $* ; do
case "${arg}" in
stop)
if [[ -e "${svcdir}/scheduled/${SVCNAME}" ]] ; then
rm -Rf "${svcdir}/scheduled/${SVCNAME}"
fi
# Stoped from the background - treat this as a restart so that
# stopped services come back up again when started.
if [[ ${IN_BACKGROUND} == "true" ]] ; then
rm -rf "${svcdir}/snapshot/$$"
mkdir -p "${svcdir}/snapshot/$$"
cp -pP "${svcdir}"/started/* "${svcdir}/snapshot/$$/"
rm -f "${svcdir}/snapshot/$$/${SVCNAME}"
fi
svc_stop
retval="$?"
if [[ ${IN_BACKGROUND} == "true" ]] ; then
for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
if service_stopped "${x##*/}" ; then
svc_schedule_start "${SVCNAME}" "${x##*/}"
fi
done
else
rm -f "${svcdir}"/scheduled/*/"${SVCNAME}"
fi
;;
start)
svc_start
retval="$?"
service_started "${SVCNAME}" && svc_start_scheduled
;;
needsme|ineed|usesme|iuse|broken)
trace_dependencies "-${arg}"
;;
status)
svc_status
retval="$?"
;;
zap)
einfo "Manually resetting ${SVCNAME} to stopped state."
mark_service_stopped "${SVCNAME}"
;;
restart)
svcrestart="yes"
# We don't kill child processes if we're restarting
# This is especically important for sshd ....
RC_KILL_CHILDREN="no"
# Create a snapshot of started services
rm -rf "${svcdir}/snapshot/$$"
mkdir -p "${svcdir}/snapshot/$$"
cp -pP "${svcdir}"/started/* "${svcdir}/snapshot/$$/"
rm -f "${svcdir}/snapshot/$$/${SVCNAME}"
# Simple way to try and detect if the service use svc_{start,stop}
# to restart if it have a custom restart() funtion.
if [[ -n $(egrep '^[[:space:]]*restart[[:space:]]*()' "/etc/init.d/${SVCNAME}") ]] ; then
if [[ -z $(egrep 'svc_stop' "/etc/init.d/${SVCNAME}") || \
-z $(egrep 'svc_start' "/etc/init.d/${SVCNAME}") ]] ; then
echo
ewarn "Please use 'svc_stop; svc_start' and not 'stop; start' to"
ewarn "restart the service in its custom 'restart()' function."
ewarn "Run ${SVCNAME} without arguments for more info."
echo
svc_restart
else
restart
fi
else
restart
fi
retval="$?"
[[ -e "${svcdir}/scheduled/${SVCNAME}" ]] \
&& rm -Rf "${svcdir}/scheduled/${SVCNAME}"
# Restart dependencies as well
for x in $(dolisting "${svcdir}/snapshot/$$/") ; do
if service_stopped "${x##*/}" ; then
if service_inactive "${SVCNAME}" \
|| service_wasinactive "${SVCNAME}" ; then
svc_schedule_start "${SVCNAME}" "${x##*/}"
ewarn "WARNING: ${x##*/} is scheduled to start when ${SVCNAME} has started."
elif service_started "${SVCNAME}" ; then
start_service "${x##*/}"
fi
fi
done
rm -rf "${svcdir}/snapshot/$$"
service_started "${SVCNAME}" && svc_start_scheduled
# Wait for services to come up
[[ ${RC_PARALLEL_STARTUP} == "yes" ]] && wait
svcrestart="no"
;;
pause)
svcpause="yes"
svc_stop
retval="$?"
svcpause="no"
;;
--quiet|--nocolor|--nodeps)
;;
help)
exec "${svclib}"/sh/rc-help.sh "${myscript}" help
;;
*)
# Allow for homegrown functions
svc_homegrown ${arg}
retval="$?"
;;
esac
done
exit "${retval}"
# vim:ts=4
|