xref: /freebsd/usr.sbin/service/service.sh (revision 65136f65bd1d3294866e5a331ff2c29474f59463)
13d482827SDoug Barton#!/bin/sh
23d482827SDoug Barton
33d482827SDoug Barton# $FreeBSD$
43d482827SDoug Barton
51de7b4b8SPedro F. Giffuni# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
61de7b4b8SPedro F. Giffuni#
73d482827SDoug Barton#  Copyright (c) 2009 Douglas Barton
83d482827SDoug Barton#  All rights reserved.
93d482827SDoug Barton#
103d482827SDoug Barton#  Redistribution and use in source and binary forms, with or without
113d482827SDoug Barton#  modification, are permitted provided that the following conditions
123d482827SDoug Barton#  are met:
133d482827SDoug Barton#  1. Redistributions of source code must retain the above copyright
143d482827SDoug Barton#     notice, this list of conditions and the following disclaimer.
153d482827SDoug Barton#  2. Redistributions in binary form must reproduce the above copyright
163d482827SDoug Barton#     notice, this list of conditions and the following disclaimer in the
173d482827SDoug Barton#     documentation and/or other materials provided with the distribution.
183d482827SDoug Barton#
193d482827SDoug Barton#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
203d482827SDoug Barton#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
213d482827SDoug Barton#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
223d482827SDoug Barton#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
233d482827SDoug Barton#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
243d482827SDoug Barton#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
253d482827SDoug Barton#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
263d482827SDoug Barton#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
273d482827SDoug Barton#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
283d482827SDoug Barton#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
293d482827SDoug Barton#  SUCH DAMAGE.
303d482827SDoug Barton
313d482827SDoug Barton. /etc/rc.subr
323d482827SDoug Bartonload_rc_config 'XXX'
333d482827SDoug Barton
343d482827SDoug Bartonusage () {
353d482827SDoug Barton	echo ''
363d482827SDoug Barton	echo 'Usage:'
37*65136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] -e"
38*65136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] -R"
39*65136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] [-v] -l | -r"
40*65136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] [-v] <rc.d script> start|stop|etc."
413d482827SDoug Barton	echo "${0##*/} -h"
423d482827SDoug Barton	echo ''
43*65136f65SKyle Evans	echo "-j	Perform actions within the named jail"
443d482827SDoug Barton	echo '-e	Show services that are enabled'
45f292f6ddSXin LI	echo "-R	Stop and start enabled $local_startup services"
463d482827SDoug Barton	echo "-l	List all scripts in /etc/rc.d and $local_startup"
473d482827SDoug Barton	echo '-r	Show the results of boot time rcorder'
483d482827SDoug Barton	echo '-v	Verbose'
493d482827SDoug Barton	echo ''
503d482827SDoug Barton}
513d482827SDoug Barton
52*65136f65SKyle Evansaccepted_argstr='jehlrRv'
53*65136f65SKyle Evans
54*65136f65SKyle Evans# Only deal with the -j option here. If found, JAIL is set and the opt and
55*65136f65SKyle Evans# arg are shifted out. OPTIND is left untouched. We strip the -j option out
56*65136f65SKyle Evans# here because we'll be proxying this invocation through to the jail via
57*65136f65SKyle Evans# jls(8) instead of handling it ourselves.
58*65136f65SKyle Evanswhile getopts ${accepted_argstr} COMMAND_LINE_ARGUMENT ; do
59*65136f65SKyle Evans	case "${COMMAND_LINE_ARGUMENT}" in
60*65136f65SKyle Evans	j)	JAIL="$2" ; shift ; shift ;;
61*65136f65SKyle Evans	esac
62*65136f65SKyle Evansdone
63*65136f65SKyle Evans
64*65136f65SKyle Evans# If -j was provided, then we pass everthing along to the jexec command
65*65136f65SKyle Evans# and execute `service` within the named JAIL. Provided that the jail
66*65136f65SKyle Evans# actually exists, as checked by `jls`.
67*65136f65SKyle Evans# We do this so that if the jail does exist, we can then return the exit
68*65136f65SKyle Evans# code of `jexec` and it should be the exit code of whatever ran in the jail.
69*65136f65SKyle Evans# There is a race condition here in that the jail might exist at `jls` time
70*65136f65SKyle Evans# and be gone by `jexec` time, but it shouldn't be a big deal.
71*65136f65SKyle Evansif [ -n "$JAIL" ]; then
72*65136f65SKyle Evans	/usr/sbin/jls -j "$JAIL" 2>/dev/null >/dev/null
73*65136f65SKyle Evans	if [ $? -ne 0 ]; then
74*65136f65SKyle Evans		echo "Jail '$JAIL' does not exist."
75*65136f65SKyle Evans		exit 1
76*65136f65SKyle Evans	fi
77*65136f65SKyle Evans
78*65136f65SKyle Evans	/usr/sbin/jexec -l "$JAIL" /usr/sbin/service $*
79*65136f65SKyle Evans	exit $?
80*65136f65SKyle Evansfi
81*65136f65SKyle Evans
82*65136f65SKyle Evanswhile getopts ${accepted_argstr} COMMAND_LINE_ARGUMENT ; do
833d482827SDoug Barton	case "${COMMAND_LINE_ARGUMENT}" in
843d482827SDoug Barton	e)	ENABLED=eopt ;;
853d482827SDoug Barton	h)	usage ; exit 0 ;;
863d482827SDoug Barton	l)	LIST=lopt ;;
873d482827SDoug Barton	r)	RCORDER=ropt ;;
88f292f6ddSXin LI	R)	RESTART=Ropt ;;
893d482827SDoug Barton	v)	VERBOSE=vopt ;;
903d482827SDoug Barton	*)	usage ; exit 1 ;;
913d482827SDoug Barton	esac
923d482827SDoug Bartondone
933d482827SDoug Bartonshift $(( $OPTIND - 1 ))
943d482827SDoug Barton
95f292f6ddSXin LIif [ -n "$RESTART" ]; then
96f292f6ddSXin LI	skip="-s nostart"
97f292f6ddSXin LI	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
98f292f6ddSXin LI		skip="$skip -s nojail"
99f292f6ddSXin LI	fi
100f292f6ddSXin LI	[ -n "$local_startup" ] && find_local_scripts_new
101f292f6ddSXin LI	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
102f292f6ddSXin LI
103f292f6ddSXin LI	for file in `reverse_list ${files}`; do
104f292f6ddSXin LI		if grep -q ^rcvar $file; then
105f292f6ddSXin LI			eval `grep ^name= $file`
106f292f6ddSXin LI			eval `grep ^rcvar $file`
1071289db48SAllan Jude			if [ -n "$rcvar" ]; then
10832047ac5SAllan Jude				load_rc_config_var ${name} ${rcvar}
1091289db48SAllan Jude			fi
110f292f6ddSXin LI			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
111f292f6ddSXin LI		fi
112f292f6ddSXin LI	done
113f292f6ddSXin LI	for file in $files; do
114f292f6ddSXin LI		if grep -q ^rcvar $file; then
115f292f6ddSXin LI			eval `grep ^name= $file`
116f292f6ddSXin LI			eval `grep ^rcvar $file`
117f292f6ddSXin LI			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
118f292f6ddSXin LI		fi
119f292f6ddSXin LI	done
120f292f6ddSXin LI
121f292f6ddSXin LI	exit 0
122f292f6ddSXin LIfi
123f292f6ddSXin LI
1243d482827SDoug Bartonif [ -n "$ENABLED" -o -n "$RCORDER" ]; then
1253d482827SDoug Barton	# Copied from /etc/rc
1263d482827SDoug Barton	skip="-s nostart"
1273d482827SDoug Barton	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
1283d482827SDoug Barton		skip="$skip -s nojail"
1293d482827SDoug Barton	fi
1303d482827SDoug Barton	[ -n "$local_startup" ] && find_local_scripts_new
1313d482827SDoug Barton	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
1323d482827SDoug Bartonfi
1333d482827SDoug Barton
1343d482827SDoug Bartonif [ -n "$ENABLED" ]; then
1353d482827SDoug Barton	for file in $files; do
1363d482827SDoug Barton		if grep -q ^rcvar $file; then
1373d482827SDoug Barton			eval `grep ^name= $file`
1383d482827SDoug Barton			eval `grep ^rcvar $file`
1391289db48SAllan Jude			if [ -n "$rcvar" ]; then
14032047ac5SAllan Jude				load_rc_config_var ${name} ${rcvar}
1411289db48SAllan Jude			fi
1423d482827SDoug Barton			checkyesno $rcvar 2>/dev/null && echo $file
1433d482827SDoug Barton		fi
1443d482827SDoug Barton	done
1453d482827SDoug Barton	exit 0
1463d482827SDoug Bartonfi
1473d482827SDoug Barton
1483d482827SDoug Bartonif [ -n "$LIST" ]; then
1493d482827SDoug Barton	for dir in /etc/rc.d $local_startup; do
1503d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "From ${dir}:"
1517627c244SXin LI		[ -d ${dir} ] && /bin/ls -1 ${dir}
1523d482827SDoug Barton	done
1533d482827SDoug Barton	exit 0
1543d482827SDoug Bartonfi
1553d482827SDoug Barton
1563d482827SDoug Bartonif [ -n "$RCORDER" ]; then
1573d482827SDoug Barton	for file in $files; do
1583d482827SDoug Barton		echo $file
1593d482827SDoug Barton		if [ -n "$VERBOSE" ]; then
1603d482827SDoug Barton			case "$file" in
1613d482827SDoug Barton			*/${early_late_divider})
1623d482827SDoug Barton				echo '========= Early/Late Divider =========' ;;
1633d482827SDoug Barton			esac
1643d482827SDoug Barton		fi
1653d482827SDoug Barton	done
1663d482827SDoug Barton	exit 0
1673d482827SDoug Bartonfi
1683d482827SDoug Barton
1693d482827SDoug Bartonif [ $# -gt 1 ]; then
1703d482827SDoug Barton	script=$1
1713d482827SDoug Barton	shift
1723d482827SDoug Bartonelse
1733d482827SDoug Barton	usage
1743d482827SDoug Barton	exit 1
1753d482827SDoug Bartonfi
1763d482827SDoug Barton
177ee55fdb8SDoug Bartoncd /
1783d482827SDoug Bartonfor dir in /etc/rc.d $local_startup; do
1793d482827SDoug Barton	if [ -x "$dir/$script" ]; then
1803d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
181dab49686SDevin Teske		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
1823d482827SDoug Barton	fi
1833d482827SDoug Bartondone
1843d482827SDoug Barton
1853d482827SDoug Barton# If the script was not found
1863d482827SDoug Bartonecho "$script does not exist in /etc/rc.d or the local startup"
18771ac3e32SEitan Adlerecho "directories (${local_startup}), or is not executable"
1883d482827SDoug Bartonexit 1
189