xref: /freebsd/usr.sbin/service/service.sh (revision 7ea28254ec5376b5deb86c136e1838d0134dbb22)
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 'XXX'
31
32usage () {
33	echo ''
34	echo 'Usage:'
35	echo "${0##*/} [-j <jail name or id>] -e"
36	echo "${0##*/} [-j <jail name or id>] -R"
37	echo "${0##*/} [-j <jail name or id>] [-v] -l | -r"
38	echo "${0##*/} [-j <jail name or id>] [-v] [-E var=value] <rc.d script> start|stop|etc."
39	echo "${0##*/} -h"
40	echo ''
41	echo "-j		Perform actions within the named jail"
42	echo "-E n=val	Set variable n to val before executing the rc.d script"
43	echo '-e		Show services that are enabled'
44	echo "-R		Stop and start enabled $local_startup services"
45	echo "-l		List all scripts in /etc/rc.d and $local_startup"
46	echo '-r		Show the results of boot time rcorder'
47	echo '-v		Verbose'
48	echo ''
49}
50
51while getopts 'j:E:ehlrRv' COMMAND_LINE_ARGUMENT ; do
52	case "${COMMAND_LINE_ARGUMENT}" in
53	j)	JAIL="${OPTARG}" ;;
54	E)	VARS="${VARS} ${OPTARG}" ;;
55	e)	ENABLED=eopt ;;
56	h)	usage ; exit 0 ;;
57	l)	LIST=lopt ;;
58	r)	RCORDER=ropt ;;
59	R)	RESTART=Ropt ;;
60	v)	VERBOSE=vopt ;;
61	*)	usage ; exit 1 ;;
62	esac
63done
64shift $(( $OPTIND - 1 ))
65
66if [ -n "${JAIL}" ]; then
67	# We need to rebuild the command line before passing it on.
68	# We do not send the -j argument into the jail.
69	args=""
70	[ -n "${ENABLED}" ] && args="${args} -e"
71	[ -n "${LIST}" ] && args="${args} -l"
72	[ -n "${RCORDER}" ] && args="${args} -r"
73	[ -n "${RESTART}" ] && args="${args} -R"
74	[ -n "${VERBOSE}" ] && args="${args} -v"
75	for var in ${VARS}; do
76		args="${args} -E ${var}"
77	done
78
79	# Call jexec(8) with the rebuild args and any positional args that
80	# were left in $@
81	/usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
82	exit $?
83fi
84
85if [ -n "$RESTART" ]; then
86	skip="-s nostart"
87	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
88		skip="$skip -s nojail"
89		if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
90			skip="$skip -s nojailvnet"
91		fi
92	fi
93	[ -n "$local_startup" ] && find_local_scripts_new
94	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
95
96	for file in `reverse_list ${files}`; do
97		if grep -q ^rcvar $file; then
98			eval `grep ^name= $file`
99			eval `grep ^rcvar $file`
100			if [ -n "$rcvar" ]; then
101				load_rc_config_var ${name} ${rcvar}
102			fi
103			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
104		fi
105	done
106	for file in $files; do
107		if grep -q ^rcvar $file; then
108			eval `grep ^name= $file`
109			eval `grep ^rcvar $file`
110			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
111		fi
112	done
113
114	exit 0
115fi
116
117if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
118	# Copied from /etc/rc
119	skip="-s nostart"
120	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
121		skip="$skip -s nojail"
122		if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
123			skip="$skip -s nojailvnet"
124		fi
125	fi
126	[ -n "$local_startup" ] && find_local_scripts_new
127	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
128fi
129
130if [ -n "$ENABLED" ]; then
131	for file in $files; do
132		if grep -q ^rcvar $file; then
133			eval `grep ^name= $file`
134			eval `grep ^rcvar $file`
135			if [ -n "$rcvar" ]; then
136				load_rc_config_var ${name} ${rcvar}
137			fi
138			checkyesno $rcvar 2>/dev/null && echo $file
139		fi
140	done
141	exit 0
142fi
143
144if [ -n "$LIST" ]; then
145	for dir in /etc/rc.d $local_startup; do
146		[ -n "$VERBOSE" ] && echo "From ${dir}:"
147		[ -d ${dir} ] && /bin/ls -1 ${dir}
148	done
149	exit 0
150fi
151
152if [ -n "$RCORDER" ]; then
153	for file in $files; do
154		echo $file
155		if [ -n "$VERBOSE" ]; then
156			case "$file" in
157			*/${early_late_divider})
158				echo '========= Early/Late Divider =========' ;;
159			esac
160		fi
161	done
162	exit 0
163fi
164
165if [ $# -gt 1 ]; then
166	script=$1
167	shift
168else
169	usage
170	exit 1
171fi
172
173cd /
174for dir in /etc/rc.d $local_startup; do
175	if [ -x "$dir/$script" ]; then
176		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
177		exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@"
178	fi
179done
180
181# If the script was not found
182echo "$script does not exist in /etc/rc.d or the local startup"
183echo "directories (${local_startup}), or is not executable"
184exit 1
185