xref: /freebsd/usr.sbin/service/service.sh (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1#!/bin/sh
2
3# $FreeBSD$
4
5#  Copyright (c) 2009 Douglas Barton
6#  All rights reserved.
7#
8#  Redistribution and use in source and binary forms, with or without
9#  modification, are permitted provided that the following conditions
10#  are met:
11#  1. Redistributions of source code must retain the above copyright
12#     notice, this list of conditions and the following disclaimer.
13#  2. Redistributions in binary form must reproduce the above copyright
14#     notice, this list of conditions and the following disclaimer in the
15#     documentation and/or other materials provided with the distribution.
16#
17#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27#  SUCH DAMAGE.
28
29. /etc/rc.subr
30load_rc_config 'XXX'
31
32usage () {
33	echo ''
34	echo 'Usage:'
35	echo "${0##*/} -e"
36	echo "${0##*/} -R"
37	echo "${0##*/} [-v] -l | -r"
38	echo "${0##*/} [-v] <rc.d script> start|stop|etc."
39	echo "${0##*/} -h"
40	echo ''
41	echo '-e	Show services that are enabled'
42	echo "-R	Stop and start enabled $local_startup services"
43	echo "-l	List all scripts in /etc/rc.d and $local_startup"
44	echo '-r	Show the results of boot time rcorder'
45	echo '-v	Verbose'
46	echo ''
47}
48
49while getopts 'ehlrRv' COMMAND_LINE_ARGUMENT ; do
50	case "${COMMAND_LINE_ARGUMENT}" in
51	e)	ENABLED=eopt ;;
52	h)	usage ; exit 0 ;;
53	l)	LIST=lopt ;;
54	r)	RCORDER=ropt ;;
55	R)	RESTART=Ropt ;;
56	v)	VERBOSE=vopt ;;
57	*)	usage ; exit 1 ;;
58	esac
59done
60shift $(( $OPTIND - 1 ))
61
62if [ -n "$RESTART" ]; then
63	skip="-s nostart"
64	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
65		skip="$skip -s nojail"
66	fi
67	[ -n "$local_startup" ] && find_local_scripts_new
68	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
69
70	for file in `reverse_list ${files}`; do
71		if grep -q ^rcvar $file; then
72			eval `grep ^name= $file`
73			eval `grep ^rcvar $file`
74			if [ -n "$rcvar" ]; then
75				load_rc_config_var ${name} ${rcvar}
76			fi
77			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
78		fi
79	done
80	for file in $files; do
81		if grep -q ^rcvar $file; then
82			eval `grep ^name= $file`
83			eval `grep ^rcvar $file`
84			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
85		fi
86	done
87
88	exit 0
89fi
90
91if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
92	# Copied from /etc/rc
93	skip="-s nostart"
94	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
95		skip="$skip -s nojail"
96	fi
97	[ -n "$local_startup" ] && find_local_scripts_new
98	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
99fi
100
101if [ -n "$ENABLED" ]; then
102	for file in $files; do
103		if grep -q ^rcvar $file; then
104			eval `grep ^name= $file`
105			eval `grep ^rcvar $file`
106			if [ -n "$rcvar" ]; then
107				load_rc_config_var ${name} ${rcvar}
108			fi
109			checkyesno $rcvar 2>/dev/null && echo $file
110		fi
111	done
112	exit 0
113fi
114
115if [ -n "$LIST" ]; then
116	for dir in /etc/rc.d $local_startup; do
117		[ -n "$VERBOSE" ] && echo "From ${dir}:"
118		[ -d ${dir} ] && /bin/ls -1 ${dir}
119	done
120	exit 0
121fi
122
123if [ -n "$RCORDER" ]; then
124	for file in $files; do
125		echo $file
126		if [ -n "$VERBOSE" ]; then
127			case "$file" in
128			*/${early_late_divider})
129				echo '========= Early/Late Divider =========' ;;
130			esac
131		fi
132	done
133	exit 0
134fi
135
136if [ $# -gt 1 ]; then
137	script=$1
138	shift
139else
140	usage
141	exit 1
142fi
143
144cd /
145for dir in /etc/rc.d $local_startup; do
146	if [ -x "$dir/$script" ]; then
147		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
148		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
149	fi
150done
151
152# If the script was not found
153echo "$script does not exist in /etc/rc.d or the local startup"
154echo "directories (${local_startup}), or is not executable"
155exit 1
156