xref: /freebsd/usr.sbin/service/service.sh (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
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##*/} -e"
38	echo "${0##*/} -R"
39	echo "${0##*/} [-v] -l | -r"
40	echo "${0##*/} [-v] <rc.d script> start|stop|etc."
41	echo "${0##*/} -h"
42	echo ''
43	echo '-e	Show services that are enabled'
44	echo "-R	Stop and start enabled $local_startup services"
45	echo "-l	List all scripts in /etc/rc.d and $local_startup"
46	echo '-r	Show the results of boot time rcorder'
47	echo '-v	Verbose'
48	echo ''
49}
50
51while getopts 'ehlrRv' COMMAND_LINE_ARGUMENT ; do
52	case "${COMMAND_LINE_ARGUMENT}" in
53	e)	ENABLED=eopt ;;
54	h)	usage ; exit 0 ;;
55	l)	LIST=lopt ;;
56	r)	RCORDER=ropt ;;
57	R)	RESTART=Ropt ;;
58	v)	VERBOSE=vopt ;;
59	*)	usage ; exit 1 ;;
60	esac
61done
62shift $(( $OPTIND - 1 ))
63
64if [ -n "$RESTART" ]; then
65	skip="-s nostart"
66	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
67		skip="$skip -s nojail"
68	fi
69	[ -n "$local_startup" ] && find_local_scripts_new
70	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
71
72	for file in `reverse_list ${files}`; do
73		if grep -q ^rcvar $file; then
74			eval `grep ^name= $file`
75			eval `grep ^rcvar $file`
76			if [ -n "$rcvar" ]; then
77				load_rc_config_var ${name} ${rcvar}
78			fi
79			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
80		fi
81	done
82	for file in $files; do
83		if grep -q ^rcvar $file; then
84			eval `grep ^name= $file`
85			eval `grep ^rcvar $file`
86			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
87		fi
88	done
89
90	exit 0
91fi
92
93if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
94	# Copied from /etc/rc
95	skip="-s nostart"
96	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
97		skip="$skip -s nojail"
98	fi
99	[ -n "$local_startup" ] && find_local_scripts_new
100	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
101fi
102
103if [ -n "$ENABLED" ]; then
104	for file in $files; do
105		if grep -q ^rcvar $file; then
106			eval `grep ^name= $file`
107			eval `grep ^rcvar $file`
108			if [ -n "$rcvar" ]; then
109				load_rc_config_var ${name} ${rcvar}
110			fi
111			checkyesno $rcvar 2>/dev/null && echo $file
112		fi
113	done
114	exit 0
115fi
116
117if [ -n "$LIST" ]; then
118	for dir in /etc/rc.d $local_startup; do
119		[ -n "$VERBOSE" ] && echo "From ${dir}:"
120		[ -d ${dir} ] && /bin/ls -1 ${dir}
121	done
122	exit 0
123fi
124
125if [ -n "$RCORDER" ]; then
126	for file in $files; do
127		echo $file
128		if [ -n "$VERBOSE" ]; then
129			case "$file" in
130			*/${early_late_divider})
131				echo '========= Early/Late Divider =========' ;;
132			esac
133		fi
134	done
135	exit 0
136fi
137
138if [ $# -gt 1 ]; then
139	script=$1
140	shift
141else
142	usage
143	exit 1
144fi
145
146cd /
147for dir in /etc/rc.d $local_startup; do
148	if [ -x "$dir/$script" ]; then
149		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
150		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
151	fi
152done
153
154# If the script was not found
155echo "$script does not exist in /etc/rc.d or the local startup"
156echo "directories (${local_startup}), or is not executable"
157exit 1
158