summaryrefslogtreecommitdiff
path: root/usr.sbin/rcctl/rcctl.sh
blob: 30a0f0d4ad29007067fe2cf761157ee99b5ed2b6 (plain)
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
#!/bin/sh
#
# $OpenBSD: rcctl.sh,v 1.45 2014/10/15 07:38:24 ajacoutot Exp $
#
# Copyright (c) 2014 Antoine Jacoutot <ajacoutot@openbsd.org>
# Copyright (c) 2014 Ingo Schwarze <schwarze@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.

_special_services="accounting check_quotas ipsec multicast_host multicast_router pf spamd_black"
readonly _special_services

# get local functions from rc.subr(8)
FUNCS_ONLY=1
. /etc/rc.d/rc.subr
_rc_parse_conf

usage()
{
	_rc_err "usage: ${0##*/} [-df] enable|disable|status|default|action
             [service | daemon [flags [arguments]]]"
}

needs_root()
{
	[ "$(id -u)" -ne 0 ] && _rc_err "${0##*/} $1: need root privileges"
}

ls_rcscripts() {
	local _s

	cd /etc/rc.d && set -- *
	for _s; do
		[ "${_s}" = "rc.subr" ] && continue
		[ ! -d "${_s}" ] && echo "${_s}"
	done
}

rcconf_edit_begin()
{
	_TMP_RCCONF=$(mktemp -p /etc -t rc.conf.local.XXXXXXXXXX) || exit 1
	if [ -f /etc/rc.conf.local ]; then
		# only to keep permissions (file content is not needed)
		cp -p /etc/rc.conf.local ${_TMP_RCCONF} || exit 1
	else
		touch /etc/rc.conf.local || exit 1
	fi
}

rcconf_edit_end()
{
	sort -u -o ${_TMP_RCCONF} ${_TMP_RCCONF} || exit 1
	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 _svc=$1
	[ -n "${_svc}" ] || return
	local _ret=1

	_rc_parse_conf /etc/rc.conf
	svc_is_enabled ${_svc} && _ret=0
	_rc_parse_conf

	return ${_ret}
}

# to prevent namespace pollution, only call in a subshell
svc_default_enabled_flags()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	if svc_is_special ${_svc}; then
		svc_default_enabled ${_svc} && echo "YES" || echo "NO"
	else
		FUNCS_ONLY=1
		rc_cmd() { }
		. /etc/rc.d/${_svc} >/dev/null 2>&1
		[ -n "${daemon_flags}" ] && print -r -- "${daemon_flags}"
	fi
}

svc_get_defaults()
{
	local _i _svc=$1

	if [ -n "${_svc}" ]; then
		print -r -- "$(svc_default_enabled_flags ${_svc})"
		svc_default_enabled ${_svc}
	else
		for _i in $(ls_rcscripts); do
			echo "${_i}_flags=$(svc_default_enabled_flags ${_i})"
		done
		for _i in ${_special_services}; do
			echo "${_i}=$(svc_default_enabled_flags ${_i})"
		done
	fi
}

svc_get_flags()
{
	local _svc=$1
	[ -n "${_svc}" ] || return
	local daemon_flags

	if svc_is_special ${_svc}; then
		echo "$(eval echo \${${_svc}})"
	else
		# set pkg daemon_flags to "NO" to match base svc
		if ! svc_is_base ${_svc}; then
			if ! echo ${pkg_scripts} | grep -qw ${_svc}; then
				echo "NO" && return
			fi
		fi
		[ -z "${daemon_flags}" ] && \
			daemon_flags="$(eval echo \"\${${_svc}_flags}\")"
		[ -z "${daemon_flags}" ] && \
			daemon_flags="$(svc_default_enabled_flags ${_svc})"

		print -r -- "${daemon_flags}" | sed '/^$/d'
	fi
}

svc_get_status()
{
	local _i _svc=$1

	if [ -n "${_svc}" ]; then
		svc_get_flags ${_svc}
		svc_is_enabled ${_svc}
	else
		for _i in $(ls_rcscripts); do
			echo "${_i}_flags=$(svc_get_flags ${_i})"
		done
		for _i in ${_special_services}; do
			echo "${_i}=$(svc_get_flags ${_i})"
		done
	fi
}

svc_is_avail()
{
	local _svc=$1
	[ -n "${_svc}" ] || return 1

	[ "${_svc}" = "rc.subr" ] && return 1
	[ -x "/etc/rc.d/${_svc}" ] && return 0
	svc_is_special ${_svc}
}

svc_is_base()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	grep "^start_daemon " /etc/rc | cut -d ' ' -f2- | grep -qw ${_svc}
}

svc_is_enabled()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	[ "$(svc_get_flags ${_svc})" != "NO" ]
}

svc_is_special()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	echo ${_special_services} | grep -qw ${_svc}
}

append_to_pkg_scripts()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	rcconf_edit_begin
	if [ -z "${pkg_scripts}" ]; then
		echo pkg_scripts="${_svc}" >>${_TMP_RCCONF}
	elif ! echo ${pkg_scripts} | grep -qw ${_svc}; then
		grep -v "^pkg_scripts.*=" /etc/rc.conf.local >${_TMP_RCCONF}
		echo pkg_scripts="${pkg_scripts} ${_svc}" >>${_TMP_RCCONF}
	fi
	rcconf_edit_end
}

rm_from_pkg_scripts()
{
	local _svc=$1
	[ -n "${_svc}" ] || return

	[ -z "${pkg_scripts}" ] && return

	rcconf_edit_begin
	sed "/^pkg_scripts[[:>:]]/{s/[[:<:]]${_svc}[[:>:]]//g
	    s/['\"]//g;s/ *= */=/;s/   */ /g;s/ $//;/=$/d;}" \
	    /etc/rc.conf.local >${_TMP_RCCONF}
	rcconf_edit_end
}

add_flags()
{
	local _svc=$2
	[ -n "${_svc}" ] || return

	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

	local _flags

	if [ -n "$3" ]; then
		shift 3
		_flags="$*"
	else
		# keep our flags since none were given
		eval "_flags=\"\${${_svc}_flags}\""
		[ "${_flags}" = "NO" ] && unset _flags
	fi

	# unset flags if they match the default enabled ones
	if [ -n "${_flags}" ]; then
		[ "${_flags}" = "$(svc_default_enabled_flags ${_svc})" ] && \
			unset _flags
	fi

	# protect leading whitespace
	[ "${_flags}" = "${_flags# }" ] || _flags="\"${_flags}\""

	rcconf_edit_begin
	grep -v "^${_svc}_flags.*=" /etc/rc.conf.local >${_TMP_RCCONF}
	if [ -n "${_flags}" ] || \
	   ( svc_is_base ${_svc} && ! svc_default_enabled ${_svc} ); then
		echo "${_svc}_flags=${_flags}" >>${_TMP_RCCONF}
	fi
	rcconf_edit_end
}

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
}

unset _RC_DEBUG _RC_FORCE
while getopts "df" c; do
	case "$c" in
		d) _RC_DEBUG=-d;;
		f) _RC_FORCE=-f;;
		*) usage;;
	esac
done
shift $((OPTIND-1))
[ $# -gt 0 ] || usage

action=$1
svc=$2
flag=$3
[ $# -ge 3 ] && shift 3 || shift $#
flags="$*"

if [ -n "$svc" ]; then
	if ! svc_is_avail $svc; then
		_rc_err "${0##*/}: service $svc does not exist" 2
	fi
elif [ "$action" != "default" -a "$action" != "status" ] ; then
	usage
fi

if [ -n "$flag" ]; then
	if [ "$flag" = "flags" ]; then
		if [ "$action" != "enable" ]; then
			_rc_err "${0##*/}: \"flags\" can only be set with \"enable\""
		fi
		if svc_is_special $svc && [ -n "$flags" ]; then
			_rc_err "${0##*/}: \"$svc\" is a special variable, cannot set \"flags\""
		fi
		if [ "$flags" = "NO" ]; then
			_rc_err "${0##*/}: \"flags NO\" contradicts \"enable\""
		fi
	else
		usage
	fi
fi

case $action in
	default)
		svc_get_defaults $svc
		;;
	disable)
		needs_root $action
		if ! svc_is_base $svc && ! svc_is_special $svc; then
			rm_from_pkg_scripts $svc
		fi
		rm_flags $svc
		;;
	enable)
		needs_root $action
		add_flags $action $svc "$flag" "$flags"
		if ! svc_is_base $svc && ! svc_is_special $svc; then
			append_to_pkg_scripts $svc
		fi
		;;
	status)
		svc_get_status $svc
		;;
	start|stop|restart|reload|check)
		if svc_is_special $svc; then
			_rc_err "${0##*/}: \"$svc\" is a special variable, no rc.d(8) script"
		fi
		/etc/rc.d/$svc ${_RC_DEBUG} ${_RC_FORCE} $action
		;;
	*)
		usage
		;;
esac