xref: /freebsd/usr.sbin/service/service.sh (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
13d482827SDoug Barton#!/bin/sh
23d482827SDoug Barton
34d846d26SWarner Losh# SPDX-License-Identifier: BSD-2-Clause
41de7b4b8SPedro F. Giffuni#
53d482827SDoug Barton#  Copyright (c) 2009 Douglas Barton
63d482827SDoug Barton#  All rights reserved.
73d482827SDoug Barton#
83d482827SDoug Barton#  Redistribution and use in source and binary forms, with or without
93d482827SDoug Barton#  modification, are permitted provided that the following conditions
103d482827SDoug Barton#  are met:
113d482827SDoug Barton#  1. Redistributions of source code must retain the above copyright
123d482827SDoug Barton#     notice, this list of conditions and the following disclaimer.
133d482827SDoug Barton#  2. Redistributions in binary form must reproduce the above copyright
143d482827SDoug Barton#     notice, this list of conditions and the following disclaimer in the
153d482827SDoug Barton#     documentation and/or other materials provided with the distribution.
163d482827SDoug Barton#
173d482827SDoug Barton#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183d482827SDoug Barton#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193d482827SDoug Barton#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203d482827SDoug Barton#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213d482827SDoug Barton#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223d482827SDoug Barton#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233d482827SDoug Barton#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243d482827SDoug Barton#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253d482827SDoug Barton#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263d482827SDoug Barton#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273d482827SDoug Barton#  SUCH DAMAGE.
283d482827SDoug Barton
293d482827SDoug Barton. /etc/rc.subr
303d482827SDoug Bartonload_rc_config 'XXX'
313d482827SDoug Barton
323d482827SDoug Bartonusage () {
333d482827SDoug Barton	echo ''
343d482827SDoug Barton	echo 'Usage:'
3565136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] -e"
3665136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] -R"
3765136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] [-v] -l | -r"
38*194e059bSAlexander Leidinger	echo "${0##*/} [-j <jail name or id>] [-v] [-E var=value] <rc.d script> start|stop|etc."
393d482827SDoug Barton	echo "${0##*/} -h"
403d482827SDoug Barton	echo ''
4165136f65SKyle Evans	echo "-j		Perform actions within the named jail"
42*194e059bSAlexander Leidinger	echo "-E n=val	Set variable n to val before executing the rc.d script"
433d482827SDoug Barton	echo '-e		Show services that are enabled'
44f292f6ddSXin LI	echo "-R		Stop and start enabled $local_startup services"
453d482827SDoug Barton	echo "-l		List all scripts in /etc/rc.d and $local_startup"
463d482827SDoug Barton	echo '-r		Show the results of boot time rcorder'
473d482827SDoug Barton	echo '-v		Verbose'
483d482827SDoug Barton	echo ''
493d482827SDoug Barton}
503d482827SDoug Barton
51*194e059bSAlexander Leidingerwhile getopts 'j:E:ehlrRv' COMMAND_LINE_ARGUMENT ; do
5265136f65SKyle Evans	case "${COMMAND_LINE_ARGUMENT}" in
53d5b15ddbSKyle Evans	j)	JAIL="${OPTARG}" ;;
54*194e059bSAlexander Leidinger	E)	VARS="${VARS} ${OPTARG}" ;;
553d482827SDoug Barton	e)	ENABLED=eopt ;;
563d482827SDoug Barton	h)	usage ; exit 0 ;;
573d482827SDoug Barton	l)	LIST=lopt ;;
583d482827SDoug Barton	r)	RCORDER=ropt ;;
59f292f6ddSXin LI	R)	RESTART=Ropt ;;
603d482827SDoug Barton	v)	VERBOSE=vopt ;;
613d482827SDoug Barton	*)	usage ; exit 1 ;;
623d482827SDoug Barton	esac
633d482827SDoug Bartondone
643d482827SDoug Bartonshift $(( $OPTIND - 1 ))
653d482827SDoug Barton
66d5b15ddbSKyle Evansif [ -n "${JAIL}" ]; then
67d5b15ddbSKyle Evans	# We need to rebuild the command line before passing it on.
68d5b15ddbSKyle Evans	# We do not send the -j argument into the jail.
69d5b15ddbSKyle Evans	args=""
70d5b15ddbSKyle Evans	[ -n "${ENABLED}" ] && args="${args} -e"
71d5b15ddbSKyle Evans	[ -n "${LIST}" ] && args="${args} -l"
72d5b15ddbSKyle Evans	[ -n "${RCORDER}" ] && args="${args} -r"
73d5b15ddbSKyle Evans	[ -n "${RESTART}" ] && args="${args} -R"
74d5b15ddbSKyle Evans	[ -n "${VERBOSE}" ] && args="${args} -v"
75*194e059bSAlexander Leidinger	for var in ${VARS}; do
76*194e059bSAlexander Leidinger		args="${args} -E ${var}"
77*194e059bSAlexander Leidinger	done
78d5b15ddbSKyle Evans
79d5b15ddbSKyle Evans	# Call jexec(8) with the rebuild args and any positional args that
80d5b15ddbSKyle Evans	# were left in $@
81d5b15ddbSKyle Evans	/usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
82d5b15ddbSKyle Evans	exit $?
83d5b15ddbSKyle Evansfi
84d5b15ddbSKyle Evans
85f292f6ddSXin LIif [ -n "$RESTART" ]; then
86f292f6ddSXin LI	skip="-s nostart"
87f292f6ddSXin LI	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
88f292f6ddSXin LI		skip="$skip -s nojail"
8905b267e2SMariusz Zaborski		if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
9005b267e2SMariusz Zaborski			skip="$skip -s nojailvnet"
9105b267e2SMariusz Zaborski		fi
92f292f6ddSXin LI	fi
93f292f6ddSXin LI	[ -n "$local_startup" ] && find_local_scripts_new
94f292f6ddSXin LI	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
95f292f6ddSXin LI
96f292f6ddSXin LI	for file in `reverse_list ${files}`; do
97f292f6ddSXin LI		if grep -q ^rcvar $file; then
98f292f6ddSXin LI			eval `grep ^name= $file`
99f292f6ddSXin LI			eval `grep ^rcvar $file`
1001289db48SAllan Jude			if [ -n "$rcvar" ]; then
10132047ac5SAllan Jude				load_rc_config_var ${name} ${rcvar}
1021289db48SAllan Jude			fi
103f292f6ddSXin LI			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
104f292f6ddSXin LI		fi
105f292f6ddSXin LI	done
106f292f6ddSXin LI	for file in $files; do
107f292f6ddSXin LI		if grep -q ^rcvar $file; then
108f292f6ddSXin LI			eval `grep ^name= $file`
109f292f6ddSXin LI			eval `grep ^rcvar $file`
110f292f6ddSXin LI			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
111f292f6ddSXin LI		fi
112f292f6ddSXin LI	done
113f292f6ddSXin LI
114f292f6ddSXin LI	exit 0
115f292f6ddSXin LIfi
116f292f6ddSXin LI
1173d482827SDoug Bartonif [ -n "$ENABLED" -o -n "$RCORDER" ]; then
1183d482827SDoug Barton	# Copied from /etc/rc
1193d482827SDoug Barton	skip="-s nostart"
1203d482827SDoug Barton	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
1213d482827SDoug Barton		skip="$skip -s nojail"
12205b267e2SMariusz Zaborski		if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
12305b267e2SMariusz Zaborski			skip="$skip -s nojailvnet"
12405b267e2SMariusz Zaborski		fi
1253d482827SDoug Barton	fi
1263d482827SDoug Barton	[ -n "$local_startup" ] && find_local_scripts_new
1273d482827SDoug Barton	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
1283d482827SDoug Bartonfi
1293d482827SDoug Barton
1303d482827SDoug Bartonif [ -n "$ENABLED" ]; then
1313d482827SDoug Barton	for file in $files; do
1323d482827SDoug Barton		if grep -q ^rcvar $file; then
1333d482827SDoug Barton			eval `grep ^name= $file`
1343d482827SDoug Barton			eval `grep ^rcvar $file`
1351289db48SAllan Jude			if [ -n "$rcvar" ]; then
13632047ac5SAllan Jude				load_rc_config_var ${name} ${rcvar}
1371289db48SAllan Jude			fi
1383d482827SDoug Barton			checkyesno $rcvar 2>/dev/null && echo $file
1393d482827SDoug Barton		fi
1403d482827SDoug Barton	done
1413d482827SDoug Barton	exit 0
1423d482827SDoug Bartonfi
1433d482827SDoug Barton
1443d482827SDoug Bartonif [ -n "$LIST" ]; then
1453d482827SDoug Barton	for dir in /etc/rc.d $local_startup; do
1463d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "From ${dir}:"
1477627c244SXin LI		[ -d ${dir} ] && /bin/ls -1 ${dir}
1483d482827SDoug Barton	done
1493d482827SDoug Barton	exit 0
1503d482827SDoug Bartonfi
1513d482827SDoug Barton
1523d482827SDoug Bartonif [ -n "$RCORDER" ]; then
1533d482827SDoug Barton	for file in $files; do
1543d482827SDoug Barton		echo $file
1553d482827SDoug Barton		if [ -n "$VERBOSE" ]; then
1563d482827SDoug Barton			case "$file" in
1573d482827SDoug Barton			*/${early_late_divider})
1583d482827SDoug Barton				echo '========= Early/Late Divider =========' ;;
1593d482827SDoug Barton			esac
1603d482827SDoug Barton		fi
1613d482827SDoug Barton	done
1623d482827SDoug Barton	exit 0
1633d482827SDoug Bartonfi
1643d482827SDoug Barton
1653d482827SDoug Bartonif [ $# -gt 1 ]; then
1663d482827SDoug Barton	script=$1
1673d482827SDoug Barton	shift
1683d482827SDoug Bartonelse
1693d482827SDoug Barton	usage
1703d482827SDoug Barton	exit 1
1713d482827SDoug Bartonfi
1723d482827SDoug Barton
173ee55fdb8SDoug Bartoncd /
1743d482827SDoug Bartonfor dir in /etc/rc.d $local_startup; do
1753d482827SDoug Barton	if [ -x "$dir/$script" ]; then
1763d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
177*194e059bSAlexander Leidinger		exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@"
1783d482827SDoug Barton	fi
1793d482827SDoug Bartondone
1803d482827SDoug Barton
1813d482827SDoug Barton# If the script was not found
1823d482827SDoug Bartonecho "$script does not exist in /etc/rc.d or the local startup"
18371ac3e32SEitan Adlerecho "directories (${local_startup}), or is not executable"
1843d482827SDoug Bartonexit 1
185