xref: /freebsd/usr.sbin/service/service.sh (revision d5b15ddbfc55578099b5b8ac7accdb1f1fbd6dff)
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:'
3765136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] -e"
3865136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] -R"
3965136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] [-v] -l | -r"
4065136f65SKyle Evans	echo "${0##*/} [-j <jail name or id>] [-v] <rc.d script> start|stop|etc."
413d482827SDoug Barton	echo "${0##*/} -h"
423d482827SDoug Barton	echo ''
4365136f65SKyle 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*d5b15ddbSKyle Evanswhile getopts 'j:ehlrRv' COMMAND_LINE_ARGUMENT ; do
5365136f65SKyle Evans	case "${COMMAND_LINE_ARGUMENT}" in
54*d5b15ddbSKyle Evans	j)	JAIL="${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
66*d5b15ddbSKyle Evansif [ -n "${JAIL}" ]; then
67*d5b15ddbSKyle Evans	# We need to rebuild the command line before passing it on.
68*d5b15ddbSKyle Evans	# We do not send the -j argument into the jail.
69*d5b15ddbSKyle Evans	args=""
70*d5b15ddbSKyle Evans	[ -n "${ENABLED}" ] && args="${args} -e"
71*d5b15ddbSKyle Evans	[ -n "${LIST}" ] && args="${args} -l"
72*d5b15ddbSKyle Evans	[ -n "${RCORDER}" ] && args="${args} -r"
73*d5b15ddbSKyle Evans	[ -n "${RESTART}" ] && args="${args} -R"
74*d5b15ddbSKyle Evans	[ -n "${VERBOSE}" ] && args="${args} -v"
75*d5b15ddbSKyle Evans
76*d5b15ddbSKyle Evans	# Call jexec(8) with the rebuild args and any positional args that
77*d5b15ddbSKyle Evans	# were left in $@
78*d5b15ddbSKyle Evans	/usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
79*d5b15ddbSKyle Evans	exit $?
80*d5b15ddbSKyle Evansfi
81*d5b15ddbSKyle Evans
82f292f6ddSXin LIif [ -n "$RESTART" ]; then
83f292f6ddSXin LI	skip="-s nostart"
84f292f6ddSXin LI	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
85f292f6ddSXin LI		skip="$skip -s nojail"
86f292f6ddSXin LI	fi
87f292f6ddSXin LI	[ -n "$local_startup" ] && find_local_scripts_new
88f292f6ddSXin LI	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
89f292f6ddSXin LI
90f292f6ddSXin LI	for file in `reverse_list ${files}`; do
91f292f6ddSXin LI		if grep -q ^rcvar $file; then
92f292f6ddSXin LI			eval `grep ^name= $file`
93f292f6ddSXin LI			eval `grep ^rcvar $file`
941289db48SAllan Jude			if [ -n "$rcvar" ]; then
9532047ac5SAllan Jude				load_rc_config_var ${name} ${rcvar}
961289db48SAllan Jude			fi
97f292f6ddSXin LI			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
98f292f6ddSXin LI		fi
99f292f6ddSXin LI	done
100f292f6ddSXin LI	for file in $files; do
101f292f6ddSXin LI		if grep -q ^rcvar $file; then
102f292f6ddSXin LI			eval `grep ^name= $file`
103f292f6ddSXin LI			eval `grep ^rcvar $file`
104f292f6ddSXin LI			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
105f292f6ddSXin LI		fi
106f292f6ddSXin LI	done
107f292f6ddSXin LI
108f292f6ddSXin LI	exit 0
109f292f6ddSXin LIfi
110f292f6ddSXin LI
1113d482827SDoug Bartonif [ -n "$ENABLED" -o -n "$RCORDER" ]; then
1123d482827SDoug Barton	# Copied from /etc/rc
1133d482827SDoug Barton	skip="-s nostart"
1143d482827SDoug Barton	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
1153d482827SDoug Barton		skip="$skip -s nojail"
1163d482827SDoug Barton	fi
1173d482827SDoug Barton	[ -n "$local_startup" ] && find_local_scripts_new
1183d482827SDoug Barton	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
1193d482827SDoug Bartonfi
1203d482827SDoug Barton
1213d482827SDoug Bartonif [ -n "$ENABLED" ]; then
1223d482827SDoug Barton	for file in $files; do
1233d482827SDoug Barton		if grep -q ^rcvar $file; then
1243d482827SDoug Barton			eval `grep ^name= $file`
1253d482827SDoug Barton			eval `grep ^rcvar $file`
1261289db48SAllan Jude			if [ -n "$rcvar" ]; then
12732047ac5SAllan Jude				load_rc_config_var ${name} ${rcvar}
1281289db48SAllan Jude			fi
1293d482827SDoug Barton			checkyesno $rcvar 2>/dev/null && echo $file
1303d482827SDoug Barton		fi
1313d482827SDoug Barton	done
1323d482827SDoug Barton	exit 0
1333d482827SDoug Bartonfi
1343d482827SDoug Barton
1353d482827SDoug Bartonif [ -n "$LIST" ]; then
1363d482827SDoug Barton	for dir in /etc/rc.d $local_startup; do
1373d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "From ${dir}:"
1387627c244SXin LI		[ -d ${dir} ] && /bin/ls -1 ${dir}
1393d482827SDoug Barton	done
1403d482827SDoug Barton	exit 0
1413d482827SDoug Bartonfi
1423d482827SDoug Barton
1433d482827SDoug Bartonif [ -n "$RCORDER" ]; then
1443d482827SDoug Barton	for file in $files; do
1453d482827SDoug Barton		echo $file
1463d482827SDoug Barton		if [ -n "$VERBOSE" ]; then
1473d482827SDoug Barton			case "$file" in
1483d482827SDoug Barton			*/${early_late_divider})
1493d482827SDoug Barton				echo '========= Early/Late Divider =========' ;;
1503d482827SDoug Barton			esac
1513d482827SDoug Barton		fi
1523d482827SDoug Barton	done
1533d482827SDoug Barton	exit 0
1543d482827SDoug Bartonfi
1553d482827SDoug Barton
1563d482827SDoug Bartonif [ $# -gt 1 ]; then
1573d482827SDoug Barton	script=$1
1583d482827SDoug Barton	shift
1593d482827SDoug Bartonelse
1603d482827SDoug Barton	usage
1613d482827SDoug Barton	exit 1
1623d482827SDoug Bartonfi
1633d482827SDoug Barton
164ee55fdb8SDoug Bartoncd /
1653d482827SDoug Bartonfor dir in /etc/rc.d $local_startup; do
1663d482827SDoug Barton	if [ -x "$dir/$script" ]; then
1673d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
168dab49686SDevin Teske		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
1693d482827SDoug Barton	fi
1703d482827SDoug Bartondone
1713d482827SDoug Barton
1723d482827SDoug Barton# If the script was not found
1733d482827SDoug Bartonecho "$script does not exist in /etc/rc.d or the local startup"
17471ac3e32SEitan Adlerecho "directories (${local_startup}), or is not executable"
1753d482827SDoug Bartonexit 1
176