#!/bin/sh # $FreeBSD: stable/11/usr.sbin/service/service.sh 287581 2015-09-08 22:50:17Z allanjude $ # Copyright (c) 2009 Douglas Barton # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. source /etc/rc.subr load_rc_config 'XXX' proc usage { echo '' echo 'Usage:' echo "$(0##*/) -e" echo "$(0##*/) -R" echo "$(0##*/) [-v] -l | -r" echo "$(0##*/) [-v] start|stop|etc." echo "$(0##*/) -h" echo '' echo '-e Show services that are enabled' echo "-R Stop and start enabled $local_startup services" echo "-l List all scripts in /etc/rc.d and $local_startup" echo '-r Show the results of boot time rcorder' echo '-v Verbose' echo '' } while getopts 'ehlrRv' COMMAND_LINE_ARGUMENT { match $(COMMAND_LINE_ARGUMENT) { with e setglobal ENABLED = 'eopt' with h usage ; exit 0 with l setglobal LIST = 'lopt' with r setglobal RCORDER = 'ropt' with R setglobal RESTART = 'Ropt' with v setglobal VERBOSE = 'vopt' with * usage ; exit 1 } } shift $shExpr(' $OPTIND - 1 ') if test -n $RESTART { setglobal skip = '"-s nostart'" if test $[/sbin/sysctl -n security.jail.jailed] -eq 1 { setglobal skip = ""$skip -s nojail"" } test -n $local_startup && find_local_scripts_new setglobal files = $[rcorder $(skip) $(local_rc) !2 >/dev/null] for file in [$[reverse_list $(files)]] { if grep -q ^rcvar $file { eval $[grep ^name= $file] eval $[grep ^rcvar $file] if test -n $rcvar { load_rc_config_var $(name) $(rcvar) } checkyesno $rcvar !2 >/dev/null && run_rc_script $(file) stop } } for file in [$files] { if grep -q ^rcvar $file { eval $[grep ^name= $file] eval $[grep ^rcvar $file] checkyesno $rcvar !2 >/dev/null && run_rc_script $(file) start } } exit 0 } if test -n $ENABLED -o -n $RCORDER { # Copied from /etc/rc setglobal skip = '"-s nostart'" if test $[/sbin/sysctl -n security.jail.jailed] -eq 1 { setglobal skip = ""$skip -s nojail"" } test -n $local_startup && find_local_scripts_new setglobal files = $[rcorder $(skip) /etc/rc.d/* $(local_rc) !2 >/dev/null] } if test -n $ENABLED { for file in [$files] { if grep -q ^rcvar $file { eval $[grep ^name= $file] eval $[grep ^rcvar $file] if test -n $rcvar { load_rc_config_var $(name) $(rcvar) } checkyesno $rcvar !2 >/dev/null && echo $file } } exit 0 } if test -n $LIST { for dir in [/etc/rc.d $local_startup] { test -n $VERBOSE && echo "From $(dir):" test -d $(dir) && /bin/ls -1 $(dir) } exit 0 } if test -n $RCORDER { for file in [$files] { echo $file if test -n $VERBOSE { match $file { with */${early_late_divider} echo '========= Early/Late Divider =========' } } } exit 0 } if test $Argc -gt 1 { setglobal script = $1 shift } else { usage exit 1 } cd / for dir in [/etc/rc.d $local_startup] { if test -x "$dir/$script" { test -n $VERBOSE && echo "$script is located in $dir" exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $ifsjoin(Argv) } } # If the script was not found echo "$script does not exist in /etc/rc.d or the local startup" echo "directories ($(local_startup)), or is not executable" exit 1