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