xref: /freebsd/usr.sbin/service/service.sh (revision 24e4dcf4ba5e9dedcf89efd358ea3e1fe5867020)
1#!/bin/sh
2
3# SPDX-License-Identifier: BSD-2-Clause
4#
5#  Copyright (c) 2009 Douglas Barton
6#  All rights reserved.
7#
8#  Redistribution and use in source and binary forms, with or without
9#  modification, are permitted provided that the following conditions
10#  are met:
11#  1. Redistributions of source code must retain the above copyright
12#     notice, this list of conditions and the following disclaimer.
13#  2. Redistributions in binary form must reproduce the above copyright
14#     notice, this list of conditions and the following disclaimer in the
15#     documentation and/or other materials provided with the distribution.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27#  SUCH DAMAGE.
28
29. /etc/rc.subr
30load_rc_config
31
32usage () {
33	echo ''
34	echo 'Usage:'
35	echo "${0##*/} [-j <jail name or id>] -e"
36	echo "${0##*/} [-j <jail name or id>] [-q] -R"
37	echo "${0##*/} [-j <jail name or id>] [-v] -l"
38	echo "${0##*/} [-j <jail name or id>] [-v] -r"
39	echo "${0##*/} [-j <jail name or id>] [-dqv] [-E var=value] <rc.d script> start|stop|etc."
40	echo "${0##*/} -h"
41	echo ''
42	echo "-d                Enable debugging of rc.d scripts"
43	echo "-j		Perform actions within the named jail"
44	echo "-E n=val	Set variable n to val before executing the rc.d script"
45	echo '-e		Show services that are enabled'
46	echo "-R		Stop and start enabled $local_startup services"
47	echo "-l		List all scripts in /etc/rc.d and $local_startup"
48	echo '-r		Show the results of boot time rcorder'
49	echo '-q		quiet'
50	echo '-v		Verbose'
51	echo ''
52}
53
54while getopts 'dE:ehj:lqrRv' COMMAND_LINE_ARGUMENT ; do
55	case "${COMMAND_LINE_ARGUMENT}" in
56	d)	DEBUG=dopt ;;
57	E)	VARS="${VARS} ${OPTARG}" ;;
58	e)	ENABLED=eopt ;;
59	h)	usage ; exit 0 ;;
60	j)	JAIL="${OPTARG}" ;;
61	l)	LIST=lopt ;;
62	q)	QUIET=qopt ;;
63	r)	RCORDER=ropt ;;
64	R)	RESTART=Ropt ;;
65	v)	VERBOSE=vopt ;;
66	*)	usage ; exit 1 ;;
67	esac
68done
69shift $(( $OPTIND - 1 ))
70
71if [ -n "${JAIL}" ]; then
72	# We need to rebuild the command line before passing it on.
73	# We do not send the -j argument into the jail.
74	args=""
75	[ -n "${ENABLED}" ] && args="${args} -e"
76	[ -n "${LIST}" ] && args="${args} -l"
77	[ -n "${QUIET}" ] && args="${args} -q"
78	[ -n "${RCORDER}" ] && args="${args} -r"
79	[ -n "${RESTART}" ] && args="${args} -R"
80	[ -n "${VERBOSE}" ] && args="${args} -v"
81	for var in ${VARS}; do
82		args="${args} -E ${var}"
83	done
84
85	# Call jexec(8) with the rebuild args and any positional args that
86	# were left in $@
87	/usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
88	exit $?
89fi
90
91if [ -n "$DEBUG" ]; then
92	VARS="${VARS} rc_debug=yes"
93fi
94
95if [ -n "$RESTART" ]; then
96	skip="-s nostart"
97	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
98		skip="$skip -s nojail"
99		if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
100			skip="$skip -s nojailvnet"
101		fi
102	fi
103	[ -n "$local_startup" ] && find_local_scripts_new
104	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
105
106	for file in `reverse_list ${files}`; do
107		if grep -q ^rcvar $file; then
108			eval `grep ^name= $file`
109			eval `grep ^rcvar $file`
110			if [ -n "$rcvar" ]; then
111				load_rc_config_var ${name} ${rcvar}
112			fi
113			if [ -n "$QUIET" ]; then
114				checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop >/dev/null 2>&1
115			else
116				checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
117			fi
118		fi
119	done
120	for file in $files; do
121		if grep -q ^rcvar $file; then
122			eval `grep ^name= $file`
123			eval `grep ^rcvar $file`
124			if [ -n "$QUIET" ]; then
125				checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start >/dev/null 2>&1
126			else
127				checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
128			fi
129		fi
130	done
131
132	exit 0
133fi
134
135if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
136	# Copied from /etc/rc
137	skip="-s nostart"
138	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
139		skip="$skip -s nojail"
140		if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
141			skip="$skip -s nojailvnet"
142		fi
143	fi
144	[ -n "$local_startup" ] && find_local_scripts_new
145	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
146fi
147
148if [ -n "$ENABLED" ]; then
149	for file in $files; do
150		if grep -q ^rcvar $file; then
151			eval `grep ^name= $file`
152			eval `grep ^rcvar $file`
153			if [ -n "$rcvar" ]; then
154				load_rc_config_var ${name} ${rcvar}
155			fi
156			checkyesno $rcvar 2>/dev/null && echo $file
157		fi
158	done
159	exit 0
160fi
161
162if [ -n "$LIST" ]; then
163	for dir in /etc/rc.d $local_startup; do
164		[ -n "$VERBOSE" ] && echo "From ${dir}:"
165		[ -d ${dir} ] && /bin/ls -1 ${dir}
166	done
167	exit 0
168fi
169
170if [ -n "$RCORDER" ]; then
171	for file in $files; do
172		echo $file
173		if [ -n "$VERBOSE" ]; then
174			case "$file" in
175			*/${early_late_divider})
176				echo '========= Early/Late Divider =========' ;;
177			esac
178		fi
179	done
180	exit 0
181fi
182
183if [ $# -gt 0 ]; then
184	script=$1
185	shift
186else
187	usage
188	exit 1
189fi
190
191cd /
192for dir in /etc/rc.d $local_startup; do
193	if [ -x "$dir/$script" ]; then
194		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
195		if [ -n "$QUIET" ]; then
196			exec /usr/bin/env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@" > /dev/null 2>&1
197		else
198			exec /usr/bin/env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@"
199		fi
200	fi
201done
202
203# If the script was not found
204echo "$script does not exist in /etc/rc.d or the local startup"
205echo "directories (${local_startup}), or is not executable"
206exit 1
207