1#!/bin/sh 2 3# SPDX-License-Identifier: BSD-2-Clause 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##*/} [-j <jail name or id>] -e" 36 echo "${0##*/} [-j <jail name or id>] -R" 37 echo "${0##*/} [-j <jail name or id>] [-v] -l | -r" 38 echo "${0##*/} [-j <jail name or id>] [-v] [-E var=value] <rc.d script> start|stop|etc." 39 echo "${0##*/} -h" 40 echo '' 41 echo "-j Perform actions within the named jail" 42 echo "-E n=val Set variable n to val before executing the rc.d script" 43 echo '-e Show services that are enabled' 44 echo "-R Stop and start enabled $local_startup services" 45 echo "-l List all scripts in /etc/rc.d and $local_startup" 46 echo '-r Show the results of boot time rcorder' 47 echo '-q quiet' 48 echo '-v Verbose' 49 echo '' 50} 51 52while getopts 'j:E:ehlqrRv' COMMAND_LINE_ARGUMENT ; do 53 case "${COMMAND_LINE_ARGUMENT}" in 54 j) JAIL="${OPTARG}" ;; 55 E) VARS="${VARS} ${OPTARG}" ;; 56 e) ENABLED=eopt ;; 57 h) usage ; exit 0 ;; 58 l) LIST=lopt ;; 59 q) QUIET=qopt ;; 60 r) RCORDER=ropt ;; 61 R) RESTART=Ropt ;; 62 v) VERBOSE=vopt ;; 63 *) usage ; exit 1 ;; 64 esac 65done 66shift $(( $OPTIND - 1 )) 67 68if [ -n "${JAIL}" ]; then 69 # We need to rebuild the command line before passing it on. 70 # We do not send the -j argument into the jail. 71 args="" 72 [ -n "${ENABLED}" ] && args="${args} -e" 73 [ -n "${LIST}" ] && args="${args} -l" 74 [ -n "${QUIET}" ] && args="${args} -q" 75 [ -n "${RCORDER}" ] && args="${args} -r" 76 [ -n "${RESTART}" ] && args="${args} -R" 77 [ -n "${VERBOSE}" ] && args="${args} -v" 78 for var in ${VARS}; do 79 args="${args} -E ${var}" 80 done 81 82 # Call jexec(8) with the rebuild args and any positional args that 83 # were left in $@ 84 /usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@" 85 exit $? 86fi 87 88if [ -n "$RESTART" ]; then 89 skip="-s nostart" 90 if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then 91 skip="$skip -s nojail" 92 if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then 93 skip="$skip -s nojailvnet" 94 fi 95 fi 96 [ -n "$local_startup" ] && find_local_scripts_new 97 files=`rcorder ${skip} ${local_rc} 2>/dev/null` 98 99 for file in `reverse_list ${files}`; do 100 if grep -q ^rcvar $file; then 101 eval `grep ^name= $file` 102 eval `grep ^rcvar $file` 103 if [ -n "$rcvar" ]; then 104 load_rc_config_var ${name} ${rcvar} 105 fi 106 if [ -n "$QUIET" ]; then 107 checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop >/dev/null 2>&1 108 else 109 checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop 110 fi 111 fi 112 done 113 for file in $files; do 114 if grep -q ^rcvar $file; then 115 eval `grep ^name= $file` 116 eval `grep ^rcvar $file` 117 if [ -n "$QUIET" ]; then 118 checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start >/dev/null 2>&1 119 else 120 checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start 121 fi 122 fi 123 done 124 125 exit 0 126fi 127 128if [ -n "$ENABLED" -o -n "$RCORDER" ]; then 129 # Copied from /etc/rc 130 skip="-s nostart" 131 if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then 132 skip="$skip -s nojail" 133 if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then 134 skip="$skip -s nojailvnet" 135 fi 136 fi 137 [ -n "$local_startup" ] && find_local_scripts_new 138 files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null` 139fi 140 141if [ -n "$ENABLED" ]; then 142 for file in $files; do 143 if grep -q ^rcvar $file; then 144 eval `grep ^name= $file` 145 eval `grep ^rcvar $file` 146 if [ -n "$rcvar" ]; then 147 load_rc_config_var ${name} ${rcvar} 148 fi 149 checkyesno $rcvar 2>/dev/null && echo $file 150 fi 151 done 152 exit 0 153fi 154 155if [ -n "$LIST" ]; then 156 for dir in /etc/rc.d $local_startup; do 157 [ -n "$VERBOSE" ] && echo "From ${dir}:" 158 [ -d ${dir} ] && /bin/ls -1 ${dir} 159 done 160 exit 0 161fi 162 163if [ -n "$RCORDER" ]; then 164 for file in $files; do 165 echo $file 166 if [ -n "$VERBOSE" ]; then 167 case "$file" in 168 */${early_late_divider}) 169 echo '========= Early/Late Divider =========' ;; 170 esac 171 fi 172 done 173 exit 0 174fi 175 176if [ $# -gt 0 ]; then 177 script=$1 178 shift 179else 180 usage 181 exit 1 182fi 183 184cd / 185for dir in /etc/rc.d $local_startup; do 186 if [ -x "$dir/$script" ]; then 187 [ -n "$VERBOSE" ] && echo "$script is located in $dir" 188 if [ -n "$QUIET" ]; then 189 exec /usr/bin/env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@" > /dev/null 2>&1 190 else 191 exec /usr/bin/env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@" 192 fi 193 fi 194done 195 196# If the script was not found 197echo "$script does not exist in /etc/rc.d or the local startup" 198echo "directories (${local_startup}), or is not executable" 199exit 1 200