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