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
|
#!/bin/sh
#
# Copyright (c) 2014 Antoine Jacoutot <ajacoutot@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.
# get local functions from rc.subr(8)
FUNCS_ONLY=1
. /etc/rc.d/rc.subr
_rc_parse_conf
usage() {
_rc_err "usage: ${0##*/} enable|disable|status|action [service [flags [...]]]"
}
needs_root()
{
if [ "$(id -u)" -ne 0 ]; then
_rc_err "${0##*/} ${1:+$1: }need root privileges"
fi
}
rcconf_edit_begin() {
_TMP_RCCONF=$(mktemp -p /etc -t rc.conf.local.XXXXXXXXXX) || exit 1
if [ -f /etc/rc.conf.local ]; then
cp -p /etc/rc.conf.local ${_TMP_RCCONF} || exit 1
else
touch /etc/rc.conf.local || exit 1
fi
}
rcconf_edit_end() {
mv ${_TMP_RCCONF} /etc/rc.conf.local || exit 1
if [ ! -s /etc/rc.conf.local ]; then
rm /etc/rc.conf.local || exit 1
fi
}
svc_default_enabled() {
local _ret=1
local _svc=$1
[ -n "${_svc}" ] || return
# get _defaults_ values only
_rc_parse_conf /etc/rc.conf
svc_is_enabled ${_svc} && _ret=0
# reparse _all_ values
svc_is_base ${_svc} && _rc_parse_conf
return ${_ret}
}
svc_get_all()
{
local _i
(
ls -A /etc/rc.d | grep -v rc.subr
for _i in ${_allowed_keys[@]}; do
echo ${_i}
done | grep -Ev '(nfs_server|savecore_flag|amd_master|pf_rules|ipsec_rules|shlib_dirs|pkg_scripts)'
) | sort
}
svc_get_flags() {
local daemon_flags
local _svc=$1
[ -n "${_svc}" ] || return
if svc_is_special ${_svc}; then
echo "$(eval echo \${${_svc}})"
elif ! svc_is_base ${_svc}; then
daemon_flags="$(eval echo \${${_svc}_flags})"
if [ -z "${daemon_flags}" ]; then
eval $(grep '^daemon_flags=' /etc/rc.d/${_svc})
fi
echo ${daemon_flags}
else
echo "$(eval echo \${${_svc}_flags})"
fi
}
svc_get_status() {
local _svc=$1
if [ -n "${_svc}" ]; then
svc_get_flags ${_svc} | sed '/^$/d'
svc_is_enabled ${_svc}
else
for _i in $(svc_get_all); do
printf "%18s" ${_i}
svc_is_enabled ${_i} && echo -n "(enabled)" || echo -n "(disabled)"
echo -n "\tflags="
svc_get_flags ${_i}
done
fi
}
svc_is_avail()
{
local _i
for _i in $(svc_get_all); do
if [ ${_i} = "$1" ]; then
return 0
fi
done
return 1
}
svc_is_base()
{
local _svc=$1
[ -n "${_svc}" ] || return
grep "^start_daemon " /etc/rc | cut -d ' ' -f2- | grep -qw ${_svc}
}
svc_is_enabled()
{
local _flags _i
local _svc=$1
[ -n "${_svc}" ] || return
if svc_is_base ${_svc}; then
eval _flags=\${${_svc}_flags}
if [ "${_flags}" != "NO" ]; then
return
fi
elif svc_is_special ${_svc}; then
eval _flags=\${${_svc}}
if [ "${_flags}" != "NO" ]; then
return
fi
else
echo ${pkg_scripts} | grep -qw ${_svc} && return
fi
return 1
}
svc_is_special()
{
local _svc=$1
[ -n "${_svc}" ] || return
echo ${_allowed_keys[@]} | grep -qw ${_svc}
}
append_to_pkg_scripts()
{
local _svc=$1
[ -n "${_svc}" ] || return
svc_is_enabled ${_svc} && return
rcconf_edit_begin
if [ -n "${pkg_scripts}" ]; then
grep -v "^pkg_scripts.*=" /etc/rc.conf.local >${_TMP_RCCONF}
echo pkg_scripts="${pkg_scripts} ${_svc}" >>${_TMP_RCCONF}
else
echo pkg_scripts="${_svc}" >>${_TMP_RCCONF}
fi
rcconf_edit_end
}
rm_from_pkg_scripts()
{
local _i _pkg_scripts
local _svc=$1
[ -n "${_svc}" ] || return
[ -z "${pkg_scripts}" ] && return
for _i in ${pkg_scripts}; do
if [ ${_i} != ${_svc} ]; then
_pkg_scripts="${_pkg_scripts} ${_i}"
fi
done
pkg_scripts=$(printf ' %s' ${_pkg_scripts})
pkg_scripts=${_pkg_scripts## }
rcconf_edit_begin
grep -v "^pkg_scripts.*=" /etc/rc.conf.local >${_TMP_RCCONF}
if [ -n "${pkg_scripts}" ]; then
echo pkg_scripts="${pkg_scripts}" >>${_TMP_RCCONF}
fi
rcconf_edit_end
}
add_flags()
{
local _flags _numargs=$#
local _svc=$2
[ -n "${_svc}" ] || return
if [ -n "$3" -a "$3" = "flags" ]; then
if [ -n "$4" ]; then
while [ "${_numargs}" -ge 4 ]
do
eval _flags=\"\$${_numargs} ${_flags}\"
let _numargs--
done
set -A _flags -- ${_flags}
fi
elif svc_is_base ${_svc}; then
# base svc: save current flags because they are reset below
set -A _flags -- $(eval echo \${${_svc}_flags})
fi
# special var
if svc_is_special ${_svc}; then
rcconf_edit_begin
grep -v "^${_svc}.*=" /etc/rc.conf.local >${_TMP_RCCONF}
if ! svc_default_enabled ${_svc}; then
echo "${_svc}=YES" >>${_TMP_RCCONF}
fi
rcconf_edit_end
return
fi
# base-system script
if svc_is_base ${_svc}; then
rcconf_edit_begin
grep -v "^${_svc}_flags.*=" /etc/rc.conf.local >${_TMP_RCCONF}
if ! svc_default_enabled ${_svc} || test "${#_flags[*]}" -gt 0; then
echo ${_svc}_flags=${_flags[@]} >>${_TMP_RCCONF}
fi
rcconf_edit_end
return
fi
# pkg script
if [ -n "$3" -a "$3" = "flags" ]; then
rcconf_edit_begin
grep -v "^${_svc}_flags.*=" /etc/rc.conf.local >${_TMP_RCCONF}
if [ "${#_flags[*]}" -gt 0 ]; then
echo ${_svc}_flags=${_flags[@]} >>${_TMP_RCCONF}
fi
rcconf_edit_end
fi
}
rm_flags()
{
local _svc=$1
[ -n "${_svc}" ] || return
rcconf_edit_begin
if svc_is_special ${_svc}; then
grep -v "^${_svc}.*=" /etc/rc.conf.local >${_TMP_RCCONF}
if svc_default_enabled ${_svc}; then
echo "${_svc}=NO" >>${_TMP_RCCONF}
fi
else
grep -v "^${_svc}_flags.*=" /etc/rc.conf.local >${_TMP_RCCONF}
if svc_default_enabled ${_svc}; then
echo "${_svc}_flags=NO" >>${_TMP_RCCONF}
fi
fi
rcconf_edit_end
}
if [ $# -gt 0 ]; then
if [ -n "$2" ]; then
if ! svc_is_avail $2; then
_rc_err "service $2 does not exist"
fi
elif [ "$1" != "status" ]; then
usage
fi
if [ -n "$3" ]; then
if [ "$3" = "flags" ]; then
if [ "$1" != "enable" ]; then
_rc_err "\"flags\" can only be set with \"enable\""
fi
if svc_is_special $2; then
_rc_err "\"$2\" is a special variable, cannot set \"flags\""
fi
else
usage
fi
fi
case $1 in
disable)
needs_root $1
if ! svc_is_base $2 && ! svc_is_special $2; then
rm_from_pkg_scripts $2
fi
rm_flags $2
;;
enable)
needs_root $1
add_flags $*
if ! svc_is_base $2 && ! svc_is_special $2; then
append_to_pkg_scripts $2
fi
;;
status)
svc_get_status $2
;;
start|stop|restart|reload|check)
if svc_is_special $2; then
_rc_err "\"$2\" is a special variable, no rc.d(8) script"
fi
/etc/rc.d/$2 $1
;;
*)
usage
;;
esac
else
usage
fi
|