xref: /freebsd/usr.sbin/service/service.sh (revision 4f52dfbb)
1#!/bin/sh
2
3# $FreeBSD$
4
5# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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] <rc.d script> start|stop|etc."
41	echo "${0##*/} -h"
42	echo ''
43	echo "-j	Perform actions within the named jail"
44	echo '-e	Show services that are enabled'
45	echo "-R	Stop and start enabled $local_startup services"
46	echo "-l	List all scripts in /etc/rc.d and $local_startup"
47	echo '-r	Show the results of boot time rcorder'
48	echo '-v	Verbose'
49	echo ''
50}
51
52while getopts 'j:ehlrRv' COMMAND_LINE_ARGUMENT ; do
53	case "${COMMAND_LINE_ARGUMENT}" in
54	j)	JAIL="${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
76	# Call jexec(8) with the rebuild args and any positional args that
77	# were left in $@
78	/usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
79	exit $?
80fi
81
82if [ -n "$RESTART" ]; then
83	skip="-s nostart"
84	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
85		skip="$skip -s nojail"
86	fi
87	[ -n "$local_startup" ] && find_local_scripts_new
88	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
89
90	for file in `reverse_list ${files}`; do
91		if grep -q ^rcvar $file; then
92			eval `grep ^name= $file`
93			eval `grep ^rcvar $file`
94			if [ -n "$rcvar" ]; then
95				load_rc_config_var ${name} ${rcvar}
96			fi
97			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
98		fi
99	done
100	for file in $files; do
101		if grep -q ^rcvar $file; then
102			eval `grep ^name= $file`
103			eval `grep ^rcvar $file`
104			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
105		fi
106	done
107
108	exit 0
109fi
110
111if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
112	# Copied from /etc/rc
113	skip="-s nostart"
114	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
115		skip="$skip -s nojail"
116	fi
117	[ -n "$local_startup" ] && find_local_scripts_new
118	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
119fi
120
121if [ -n "$ENABLED" ]; then
122	for file in $files; do
123		if grep -q ^rcvar $file; then
124			eval `grep ^name= $file`
125			eval `grep ^rcvar $file`
126			if [ -n "$rcvar" ]; then
127				load_rc_config_var ${name} ${rcvar}
128			fi
129			checkyesno $rcvar 2>/dev/null && echo $file
130		fi
131	done
132	exit 0
133fi
134
135if [ -n "$LIST" ]; then
136	for dir in /etc/rc.d $local_startup; do
137		[ -n "$VERBOSE" ] && echo "From ${dir}:"
138		[ -d ${dir} ] && /bin/ls -1 ${dir}
139	done
140	exit 0
141fi
142
143if [ -n "$RCORDER" ]; then
144	for file in $files; do
145		echo $file
146		if [ -n "$VERBOSE" ]; then
147			case "$file" in
148			*/${early_late_divider})
149				echo '========= Early/Late Divider =========' ;;
150			esac
151		fi
152	done
153	exit 0
154fi
155
156if [ $# -gt 1 ]; then
157	script=$1
158	shift
159else
160	usage
161	exit 1
162fi
163
164cd /
165for dir in /etc/rc.d $local_startup; do
166	if [ -x "$dir/$script" ]; then
167		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
168		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
169	fi
170done
171
172# If the script was not found
173echo "$script does not exist in /etc/rc.d or the local startup"
174echo "directories (${local_startup}), or is not executable"
175exit 1
176