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
|
# $OpenBSD: rc.subr,v 1.74 2014/07/12 15:32:43 robert Exp $
#
# Copyright (c) 2010, 2011, 2014 Antoine Jacoutot <ajacoutot@openbsd.org>
# Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
# Copyright (c) 2010, 2011, 2014 Robert Nagy <robert@openbsd.org>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Default functions and variables used by rc.d(8) scripts.
_rc_err() {
echo $1 1>&2
exit 1
}
_rc_is_supported() {
local _enotsup
eval _enotsup=\${rc_$1}
[ X"${_enotsup}" != X"NO" ]
}
_rc_usage() {
local _a _allsup
for _a in start stop restart reload check; do
_rc_is_supported ${_a} && _allsup="${_allsup:+$_allsup|}${_a}"
done
_rc_err "usage: $0 [-df] (${_allsup})"
}
_rc_write_runfile() {
[ -d ${_RC_RUNDIR} ] || mkdir -p ${_RC_RUNDIR} && \
print -rn -- "${pexp}" > ${_RC_RUNFILE}
}
_rc_read_runfile() {
local _new_pexp
[ -f ${_RC_RUNFILE} ] && _new_pexp=$(< ${_RC_RUNFILE})
[ -n "${_new_pexp}" ] && pexp="${_new_pexp}"
}
_rc_rm_runfile() {
rm -f ${_RC_RUNFILE}
}
_rc_do() {
if [ -n "${_RC_DEBUG}" ]; then
echo "doing $@" && "$@"
else
"$@" >/dev/null 2>&1
fi
}
_rc_exit() {
local _pfix
[ -z "${INRC}" -o X"$1" != X"ok" ] && _pfix="($1)"
echo ${INRC:+'-n'} "${_pfix}"
[ X"$1" = X"ok" ] && exit 0 || exit 1
}
_rc_wait() {
local _i=0
while [ $_i -lt ${daemon_timeout} ]; do
case "$1" in
reload|start)
_rc_do rc_check && return 0
;;
stop)
_rc_do rc_check || return 0
;;
*)
break
;;
esac
sleep 1
_i=$((_i+1))
done
return 1
}
_rc_quirks() {
# special care needed for spamlogd to avoid starting it up and failing
# all the time
if [ X"${spamd_flags}" = X"NO" -o X"${spamd_black}" != X"NO" ]; then
spamlogd_flags=NO
fi
# special care needed for pflogd to avoid starting it up and failing
# if pf is not enabled
if [ X"${pf}" = X"NO" ]; then
pflogd_flags=NO
fi
# special care needed if nfs_server=YES to startup nfsd and mountd with
# sane default flags
if [ X"${nfs_server}" = X"YES" ]; then
[ X"${nfsd_flags}" = X"NO" ] && nfsd_flags="-tun 4"
[ X"${mountd_flags}" = X"NO" ] && mountd_flags=
fi
# in case domainname is set and /var/yp/binding exists enable ypbind
[ X"`domainname`" != X"" -a -d /var/yp/binding ] && ypbind_flags=
}
_rc_parse_conf() {
typeset -l _key
local _l _val
local _rcconf="/etc/rc.conf"
local _rcconf_local="/etc/rc.conf.local"
set -A _allowed_keys -- \
spamd_black pf ipsec check_quotas accounting \
multicast_hosts multicast_router amd_master \
pf_rules ipsec_rules shlib_dirs pkg_scripts \
nfs_server
for _rcfile in $_rcconf $_rcconf_local; do
[[ -f $_rcfile ]] || return
while IFS=' ' read -r _l; do
[[ $_l == [!#=]*=* ]] || continue
_key=${_l%%*([[:blank:]])=*}
[[ $_key == *_@(flags|user|timeout) ]] || \
[[ " ${_allowed_keys[*]} " == *" $_key "* ]] || \
continue
[[ $_key == "" ]] && continue
_val=${_l##*([!=])=*([[:blank:]])}
_val=${_val%%#*}
_val=${_val%%*([[:blank:]])}
# remove leading and trailing quotes (backwards compat)
[[ $_val == @(\"*\"|\'*\') ]] && _val=${_val#?} _val=${_val%?}
[ -n "${_RC_DEBUG}" ] && printf "%18s\t>$_val<\n" $_key
eval "${_key}=\${_val}"
done < $_rcfile
done
_rc_do _rc_quirks
}
[ -n "${FUNCS_ONLY}" ] && return
rc_start() {
${rcexec} "${daemon} ${daemon_flags} ${_bg}"
}
rc_check() {
pgrep -q -f "^${pexp}"
}
rc_reload() {
pkill -HUP -f "^${pexp}"
}
rc_stop() {
pkill -f "^${pexp}"
}
rc_cmd() {
local _bg _n
[ "$(id -u)" -eq 0 ] || \
[ X"${rc_usercheck}" != X"NO" -a X"$1" = "Xcheck" ] || \
_rc_err "$0: need root privileges"
if ! (_rc_is_supported start && _rc_is_supported stop); then
rc_restart=NO
fi
if ! _rc_is_supported $1; then
[ -n "${INRC}" ] && exit 1
_rc_err "$0: $1 is not supported"
fi
[ X"${rc_bg}" = X"YES" ] && _bg="&"
[ -n "${_RC_DEBUG}" ] || _n="-n"
_rc_do _rc_read_runfile
case "$1" in
check)
_rc_do rc_check
;;
start)
if [ X"${daemon_flags}" = X"NO" ]; then
_rc_err "$0: need -f to force $1 since ${_name}_flags=NO"
exit 1
fi
[ -z "${INRC}" ] && _rc_do rc_check && exit 0
echo $_n "${INRC:+ }${_name}"
while true; do # no real loop, only needed to break
if type rc_pre >/dev/null; then
_rc_do rc_pre || break
fi
# XXX only checks the status of the return code,
# and _not_ that the daemon is actually running
_rc_do rc_start || break
if [ -n "${_bg}" ]; then
sleep 1
_rc_do _rc_wait start || break
fi
_rc_do _rc_write_runfile
_rc_exit ok
done
# handle failure
type rc_post >/dev/null && _rc_do rc_post
_rc_do _rc_rm_runfile
_rc_exit failed
;;
stop)
_rc_do rc_check || exit 0
echo $_n "${INRC:+ }${_name}"
_rc_do rc_stop || _rc_exit failed
_rc_do _rc_wait stop || _rc_exit failed
if type rc_post >/dev/null; then \
_rc_do rc_post || _rc_exit failed
fi
_rc_do _rc_rm_runfile
_rc_exit ok
;;
reload)
_rc_do rc_check || exit 0
echo $_n "${INRC:+ }${_name}"
_rc_do rc_reload || _rc_exit failed
_rc_do _rc_wait reload || _rc_exit failed
_rc_exit ok
;;
restart)
$0 ${_RC_DEBUG} ${_RC_FORCE} stop &&
$0 ${_RC_DEBUG} ${_RC_FORCE} start
;;
*)
_rc_usage
;;
esac
}
[ -n "${daemon}" ] || _rc_err "$0: daemon is not set"
unset _RC_DEBUG _RC_FORCE
while getopts "df" c; do
case "$c" in
d) _RC_DEBUG=-d;;
f) _RC_FORCE=-f;;
*) _rc_usage;;
esac
done
shift $((OPTIND-1))
_name=$(basename $0)
_RC_RUNDIR=/var/run/rc.d
_RC_RUNFILE=${_RC_RUNDIR}/${_name}
# parse /etc/rc.conf{.local} for the daemon_flags
_rc_do _rc_parse_conf
eval _rcflags=\${${_name}_flags}
eval _rcuser=\${${_name}_user}
eval _rctimeout=\${${_name}_timeout}
getcap -f /etc/login.conf ${_name} 1>/dev/null 2>&1 && \
daemon_class=${_name}
[ -z "${daemon_class}" ] && daemon_class=daemon
[ -z "${daemon_user}" ] && daemon_user=root
[ -z "${daemon_timeout}" ] && daemon_timeout=30
[ -n "${_RC_FORCE}" ] && [ X"${_rcflags}" = X"NO" ] && unset _rcflags
[ -n "${_rcflags}" ] && daemon_flags=${_rcflags}
[ -n "${_rcuser}" ] && daemon_user=${_rcuser}
[ -n "${_rctimeout}" ] && daemon_timeout=${_rctimeout}
# sanitize
daemon_flags=$(printf ' %s' ${daemon_flags})
daemon_flags=${daemon_flags## }
readonly daemon_class
unset _rcflags _rcuser
pexp="${daemon}${daemon_flags:+ ${daemon_flags}}"
rcexec="su -l -c ${daemon_class} -s /bin/sh ${daemon_user} -c"
|