xref: /freebsd/usr.sbin/service/service.sh (revision ee55fdb8fa1ce60d39bd7f2afde7d9772a2e3d58)
13d482827SDoug Barton#!/bin/sh
23d482827SDoug Barton
33d482827SDoug Barton# $FreeBSD$
43d482827SDoug Barton
53d482827SDoug Barton#  Copyright (c) 2009 Douglas Barton
63d482827SDoug Barton#  All rights reserved.
73d482827SDoug Barton#
83d482827SDoug Barton#  Redistribution and use in source and binary forms, with or without
93d482827SDoug Barton#  modification, are permitted provided that the following conditions
103d482827SDoug Barton#  are met:
113d482827SDoug Barton#  1. Redistributions of source code must retain the above copyright
123d482827SDoug Barton#     notice, this list of conditions and the following disclaimer.
133d482827SDoug Barton#  2. Redistributions in binary form must reproduce the above copyright
143d482827SDoug Barton#     notice, this list of conditions and the following disclaimer in the
153d482827SDoug Barton#     documentation and/or other materials provided with the distribution.
163d482827SDoug Barton#
173d482827SDoug Barton#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
183d482827SDoug Barton#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
193d482827SDoug Barton#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
203d482827SDoug Barton#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
213d482827SDoug Barton#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
223d482827SDoug Barton#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
233d482827SDoug Barton#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
243d482827SDoug Barton#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
253d482827SDoug Barton#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
263d482827SDoug Barton#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
273d482827SDoug Barton#  SUCH DAMAGE.
283d482827SDoug Barton
293d482827SDoug Barton. /etc/rc.subr
303d482827SDoug Bartonload_rc_config 'XXX'
313d482827SDoug Barton
323d482827SDoug Bartonusage () {
333d482827SDoug Barton	echo ''
343d482827SDoug Barton	echo 'Usage:'
353d482827SDoug Barton	echo "${0##*/} -e"
363d482827SDoug Barton	echo "${0##*/} [-v] -l|r"
373d482827SDoug Barton	echo "${0##*/} [-v] <rc.d script> start|stop|etc."
383d482827SDoug Barton	echo "${0##*/} -h"
393d482827SDoug Barton	echo ''
403d482827SDoug Barton	echo '-e	Show services that are enabled'
413d482827SDoug Barton	echo "-l	List all scripts in /etc/rc.d and $local_startup"
423d482827SDoug Barton	echo '-r	Show the results of boot time rcorder'
433d482827SDoug Barton	echo '-v	Verbose'
443d482827SDoug Barton	echo ''
453d482827SDoug Barton}
463d482827SDoug Barton
473d482827SDoug Bartonwhile getopts 'ehlrv' COMMAND_LINE_ARGUMENT ; do
483d482827SDoug Barton	case "${COMMAND_LINE_ARGUMENT}" in
493d482827SDoug Barton	e)	ENABLED=eopt ;;
503d482827SDoug Barton	h)	usage ; exit 0 ;;
513d482827SDoug Barton	l)	LIST=lopt ;;
523d482827SDoug Barton	r)	RCORDER=ropt ;;
533d482827SDoug Barton	v)	VERBOSE=vopt ;;
543d482827SDoug Barton	*)	usage ; exit 1 ;;
553d482827SDoug Barton	esac
563d482827SDoug Bartondone
573d482827SDoug Bartonshift $(( $OPTIND - 1 ))
583d482827SDoug Barton
593d482827SDoug Bartonif [ -n "$ENABLED" -o -n "$RCORDER" ]; then
603d482827SDoug Barton	# Copied from /etc/rc
613d482827SDoug Barton	skip="-s nostart"
623d482827SDoug Barton	if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
633d482827SDoug Barton		skip="$skip -s nojail"
643d482827SDoug Barton	fi
653d482827SDoug Barton	[ -n "$local_startup" ] && find_local_scripts_new
663d482827SDoug Barton	files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
673d482827SDoug Bartonfi
683d482827SDoug Barton
693d482827SDoug Bartonif [ -n "$ENABLED" ]; then
703d482827SDoug Barton	for file in $files; do
713d482827SDoug Barton		if grep -q ^rcvar $file; then
723d482827SDoug Barton			eval `grep ^name= $file`
733d482827SDoug Barton			eval `grep ^rcvar $file`
743d482827SDoug Barton			checkyesno $rcvar 2>/dev/null && echo $file
753d482827SDoug Barton		fi
763d482827SDoug Barton	done
773d482827SDoug Barton	exit 0
783d482827SDoug Bartonfi
793d482827SDoug Barton
803d482827SDoug Bartonif [ -n "$LIST" ]; then
813d482827SDoug Barton	for dir in /etc/rc.d $local_startup; do
823d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "From ${dir}:"
833d482827SDoug Barton		cd $dir && for file in *; do echo $file; done
843d482827SDoug Barton	done
853d482827SDoug Barton	exit 0
863d482827SDoug Bartonfi
873d482827SDoug Barton
883d482827SDoug Bartonif [ -n "$RCORDER" ]; then
893d482827SDoug Barton	for file in $files; do
903d482827SDoug Barton		echo $file
913d482827SDoug Barton		if [ -n "$VERBOSE" ]; then
923d482827SDoug Barton			case "$file" in
933d482827SDoug Barton			*/${early_late_divider})
943d482827SDoug Barton				echo '========= Early/Late Divider =========' ;;
953d482827SDoug Barton			esac
963d482827SDoug Barton		fi
973d482827SDoug Barton	done
983d482827SDoug Barton	exit 0
993d482827SDoug Bartonfi
1003d482827SDoug Barton
1013d482827SDoug Bartonif [ $# -gt 1 ]; then
1023d482827SDoug Barton	script=$1
1033d482827SDoug Barton	shift
1043d482827SDoug Bartonelse
1053d482827SDoug Barton	usage
1063d482827SDoug Barton	exit 1
1073d482827SDoug Bartonfi
1083d482827SDoug Barton
109ee55fdb8SDoug Bartoncd /
1103d482827SDoug Bartonfor dir in /etc/rc.d $local_startup; do
1113d482827SDoug Barton	if [ -x "$dir/$script" ]; then
1123d482827SDoug Barton		[ -n "$VERBOSE" ] && echo "$script is located in $dir"
113ee55fdb8SDoug Barton		exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $*
1143d482827SDoug Barton	fi
1153d482827SDoug Bartondone
1163d482827SDoug Barton
1173d482827SDoug Barton# If the script was not found
1183d482827SDoug Bartonecho "$script does not exist in /etc/rc.d or the local startup"
1193d482827SDoug Bartonecho "directories (${local_startup})"
1203d482827SDoug Bartonexit 1
121