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`
74294100Sallanjude			if [ -n "$rcvar" ]; then
75294100Sallanjude				load_rc_config_var ${name} ${rcvar}
76294100Sallanjude			fi
77244132Sdelphij			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
78244132Sdelphij		fi
79244132Sdelphij	done
80244132Sdelphij	for file in $files; do
81244132Sdelphij		if grep -q ^rcvar $file; then
82244132Sdelphij			eval `grep ^name= $file`
83244132Sdelphij			eval `grep ^rcvar $file`
84244132Sdelphij			checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
85244132Sdelphij		fi
86244132Sdelphij	done
87244132Sdelphij
88244132Sdelphij	exit 0
89244132Sdelphijfi
90244132Sdelphij
91200743Sdougbif [ -n "$ENABLED" -o -n "$RCORDER" ]; then
92200743Sdougb	# Copied from /etc/rc
93200743Sdougb	skip="-s nostart"
94200743Sdougb	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
95200743Sdougb		skip="$skip -s nojail"
96200743Sdougb	fi
97200743Sdougb	[ -n "$local_startup" ] && find_local_scripts_new
98200743Sdougb	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
99200743Sdougbfi
100200743Sdougb
101200743Sdougbif [ -n "$ENABLED" ]; then
102200743Sdougb	for file in $files; do
103200743Sdougb		if grep -q ^rcvar $file; then
104200743Sdougb			eval `grep ^name= $file`
105200743Sdougb			eval `grep ^rcvar $file`
106294100Sallanjude			if [ -n "$rcvar" ]; then
107294100Sallanjude				load_rc_config_var ${name} ${rcvar}
108294100Sallanjude			fi
109200743Sdougb			checkyesno $rcvar 2>/dev/null && echo $file
110200743Sdougb		fi
111200743Sdougb	done
112200743Sdougb	exit 0
113200743Sdougbfi
114200743Sdougb
115200743Sdougbif [ -n "$LIST" ]; then
116200743Sdougb	for dir in /etc/rc.d $local_startup; do
117200743Sdougb		[ -n "$VERBOSE" ] && echo "From ${dir}:"
118268098Sdelphij		[ -d ${dir} ] && /bin/ls -1 ${dir}
119200743Sdougb	done
120200743Sdougb	exit 0
121200743Sdougbfi
122200743Sdougb
123200743Sdougbif [ -n "$RCORDER" ]; then
124200743Sdougb	for file in $files; do
125200743Sdougb		echo $file
126200743Sdougb		if [ -n "$VERBOSE" ]; then
127200743Sdougb			case "$file" in
128200743Sdougb			*/${early_late_divider})
129200743Sdougb				echo '========= Early/Late Divider =========' ;;
130200743Sdougb			esac
131200743Sdougb		fi
132200743Sdougb	done
133200743Sdougb	exit 0
134200743Sdougbfi
135200743Sdougb
136200743Sdougbif [ $# -gt 1 ]; then
137200743Sdougb	script=$1
138200743Sdougb	shift
139200743Sdougbelse
140200743Sdougb	usage
141200743Sdougb	exit 1
142200743Sdougbfi
143200743Sdougb
144200809Sdougbcd /
145200743Sdougbfor dir in /etc/rc.d $local_startup; do
146200743Sdougb	if [ -x "$dir/$script" ]; then
147200743Sdougb		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
148200809Sdougb		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
149200743Sdougb	fi
150200743Sdougbdone
151200743Sdougb
152200743Sdougb# If the script was not found
153200743Sdougbecho "$script does not exist in /etc/rc.d or the local startup"
154200743Sdougbecho "directories (${local_startup})"
155200743Sdougbexit 1
156