1200743Sdougb#!/bin/sh
2200743Sdougb
3200743Sdougb# $FreeBSD$
4200743Sdougb
5200743Sdougb#  Copyright (c) 2009 Douglas Barton
6200743Sdougb#  All rights reserved.
7200743Sdougb#
8200743Sdougb#  Redistribution and use in source and binary forms, with or without
9200743Sdougb#  modification, are permitted provided that the following conditions
10200743Sdougb#  are met:
11200743Sdougb#  1. Redistributions of source code must retain the above copyright
12200743Sdougb#     notice, this list of conditions and the following disclaimer.
13200743Sdougb#  2. Redistributions in binary form must reproduce the above copyright
14200743Sdougb#     notice, this list of conditions and the following disclaimer in the
15200743Sdougb#     documentation and/or other materials provided with the distribution.
16200743Sdougb#
17200743Sdougb#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18200743Sdougb#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19200743Sdougb#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20200743Sdougb#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21200743Sdougb#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22200743Sdougb#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23200743Sdougb#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24200743Sdougb#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25200743Sdougb#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26200743Sdougb#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27200743Sdougb#  SUCH DAMAGE.
28200743Sdougb
29200743Sdougb. /etc/rc.subr
30200743Sdougbload_rc_config 'XXX'
31200743Sdougb
32200743Sdougbusage () {
33200743Sdougb	echo ''
34200743Sdougb	echo 'Usage:'
35200743Sdougb	echo "${0##*/} -e"
36244132Sdelphij	echo "${0##*/} -R"
37201070Sdougb	echo "${0##*/} [-v] -l | -r"
38200743Sdougb	echo "${0##*/} [-v] <rc.d script> start|stop|etc."
39200743Sdougb	echo "${0##*/} -h"
40200743Sdougb	echo ''
41200743Sdougb	echo '-e	Show services that are enabled'
42244132Sdelphij	echo "-R	Stop and start enabled $local_startup services"
43200743Sdougb	echo "-l	List all scripts in /etc/rc.d and $local_startup"
44200743Sdougb	echo '-r	Show the results of boot time rcorder'
45200743Sdougb	echo '-v	Verbose'
46200743Sdougb	echo ''
47200743Sdougb}
48200743Sdougb
49244132Sdelphijwhile getopts 'ehlrRv' COMMAND_LINE_ARGUMENT ; do
50200743Sdougb	case "${COMMAND_LINE_ARGUMENT}" in
51200743Sdougb	e)	ENABLED=eopt ;;
52200743Sdougb	h)	usage ; exit 0 ;;
53200743Sdougb	l)	LIST=lopt ;;
54200743Sdougb	r)	RCORDER=ropt ;;
55244132Sdelphij	R)	RESTART=Ropt ;;
56200743Sdougb	v)	VERBOSE=vopt ;;
57200743Sdougb	*)	usage ; exit 1 ;;
58200743Sdougb	esac
59200743Sdougbdone
60200743Sdougbshift $(( $OPTIND - 1 ))
61200743Sdougb
62244132Sdelphijif [ -n "$RESTART" ]; then
63244132Sdelphij	skip="-s nostart"
64244132Sdelphij	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
65244132Sdelphij		skip="$skip -s nojail"
66244132Sdelphij	fi
67244132Sdelphij	[ -n "$local_startup" ] && find_local_scripts_new
68244132Sdelphij	files=`rcorder ${skip} ${local_rc} 2>/dev/null`
69244132Sdelphij
70244132Sdelphij	for file in `reverse_list ${files}`; do
71244132Sdelphij		if grep -q ^rcvar $file; then
72244132Sdelphij			eval `grep ^name= $file`
73244132Sdelphij			eval `grep ^rcvar $file`
74244132Sdelphij			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
75244132Sdelphij		fi
76244132Sdelphij	done
77244132Sdelphij	for file in $files; do
78244132Sdelphij		if grep -q ^rcvar $file; then
79244132Sdelphij			eval `grep ^name= $file`
80244132Sdelphij			eval `grep ^rcvar $file`
81244132Sdelphij			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
82244132Sdelphij		fi
83244132Sdelphij	done
84244132Sdelphij
85244132Sdelphij	exit 0
86244132Sdelphijfi
87244132Sdelphij
88200743Sdougbif [ -n "$ENABLED" -o -n "$RCORDER" ]; then
89200743Sdougb	# Copied from /etc/rc
90200743Sdougb	skip="-s nostart"
91200743Sdougb	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
92200743Sdougb		skip="$skip -s nojail"
93200743Sdougb	fi
94200743Sdougb	[ -n "$local_startup" ] && find_local_scripts_new
95200743Sdougb	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
96200743Sdougbfi
97200743Sdougb
98200743Sdougbif [ -n "$ENABLED" ]; then
99200743Sdougb	for file in $files; do
100200743Sdougb		if grep -q ^rcvar $file; then
101200743Sdougb			eval `grep ^name= $file`
102200743Sdougb			eval `grep ^rcvar $file`
103200743Sdougb			checkyesno $rcvar 2>/dev/null && echo $file
104200743Sdougb		fi
105200743Sdougb	done
106200743Sdougb	exit 0
107200743Sdougbfi
108200743Sdougb
109200743Sdougbif [ -n "$LIST" ]; then
110200743Sdougb	for dir in /etc/rc.d $local_startup; do
111200743Sdougb		[ -n "$VERBOSE" ] && echo "From ${dir}:"
112268098Sdelphij		[ -d ${dir} ] && /bin/ls -1 ${dir}
113200743Sdougb	done
114200743Sdougb	exit 0
115200743Sdougbfi
116200743Sdougb
117200743Sdougbif [ -n "$RCORDER" ]; then
118200743Sdougb	for file in $files; do
119200743Sdougb		echo $file
120200743Sdougb		if [ -n "$VERBOSE" ]; then
121200743Sdougb			case "$file" in
122200743Sdougb			*/${early_late_divider})
123200743Sdougb				echo '========= Early/Late Divider =========' ;;
124200743Sdougb			esac
125200743Sdougb		fi
126200743Sdougb	done
127200743Sdougb	exit 0
128200743Sdougbfi
129200743Sdougb
130200743Sdougbif [ $# -gt 1 ]; then
131200743Sdougb	script=$1
132200743Sdougb	shift
133200743Sdougbelse
134200743Sdougb	usage
135200743Sdougb	exit 1
136200743Sdougbfi
137200743Sdougb
138200809Sdougbcd /
139200743Sdougbfor dir in /etc/rc.d $local_startup; do
140200743Sdougb	if [ -x "$dir/$script" ]; then
141200743Sdougb		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
142200809Sdougb		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
143200743Sdougb	fi
144200743Sdougbdone
145200743Sdougb
146200743Sdougb# If the script was not found
147200743Sdougbecho "$script does not exist in /etc/rc.d or the local startup"
148200743Sdougbecho "directories (${local_startup})"
149200743Sdougbexit 1
150