3 # SPDX-License-Identifier: BSD-2-Clause
5 # Copyright (c) 2009 Douglas Barton
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
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.
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
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."
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'
51 while getopts 'j:E:ehlrRv' COMMAND_LINE_ARGUMENT ; do
52 case "${COMMAND_LINE_ARGUMENT}" in
53 j) JAIL="${OPTARG}" ;;
54 E) VARS="${VARS} ${OPTARG}" ;;
64 shift $(( $OPTIND - 1 ))
66 if [ -n "${JAIL}" ]; then
67 # We need to rebuild the command line before passing it on.
68 # We do not send the -j argument into the jail.
70 [ -n "${ENABLED}" ] && args="${args} -e"
71 [ -n "${LIST}" ] && args="${args} -l"
72 [ -n "${RCORDER}" ] && args="${args} -r"
73 [ -n "${RESTART}" ] && args="${args} -R"
74 [ -n "${VERBOSE}" ] && args="${args} -v"
75 for var in ${VARS}; do
76 args="${args} -E ${var}"
79 # Call jexec(8) with the rebuild args and any positional args that
81 /usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
85 if [ -n "$RESTART" ]; then
87 if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
88 skip="$skip -s nojail"
89 if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
90 skip="$skip -s nojailvnet"
93 [ -n "$local_startup" ] && find_local_scripts_new
94 files=`rcorder ${skip} ${local_rc} 2>/dev/null`
96 for file in `reverse_list ${files}`; do
97 if grep -q ^rcvar $file; then
98 eval `grep ^name= $file`
99 eval `grep ^rcvar $file`
100 if [ -n "$rcvar" ]; then
101 load_rc_config_var ${name} ${rcvar}
103 checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
106 for file in $files; do
107 if grep -q ^rcvar $file; then
108 eval `grep ^name= $file`
109 eval `grep ^rcvar $file`
110 checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
117 if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
118 # Copied from /etc/rc
120 if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
121 skip="$skip -s nojail"
122 if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
123 skip="$skip -s nojailvnet"
126 [ -n "$local_startup" ] && find_local_scripts_new
127 files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
130 if [ -n "$ENABLED" ]; then
131 for file in $files; do
132 if grep -q ^rcvar $file; then
133 eval `grep ^name= $file`
134 eval `grep ^rcvar $file`
135 if [ -n "$rcvar" ]; then
136 load_rc_config_var ${name} ${rcvar}
138 checkyesno $rcvar 2>/dev/null && echo $file
144 if [ -n "$LIST" ]; then
145 for dir in /etc/rc.d $local_startup; do
146 [ -n "$VERBOSE" ] && echo "From ${dir}:"
147 [ -d ${dir} ] && /bin/ls -1 ${dir}
152 if [ -n "$RCORDER" ]; then
153 for file in $files; do
155 if [ -n "$VERBOSE" ]; then
157 */${early_late_divider})
158 echo '========= Early/Late Divider =========' ;;
165 if [ $# -gt 1 ]; then
174 for dir in /etc/rc.d $local_startup; do
175 if [ -x "$dir/$script" ]; then
176 [ -n "$VERBOSE" ] && echo "$script is located in $dir"
177 exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin ${VARS} "$dir/$script" "$@"
181 # If the script was not found
182 echo "$script does not exist in /etc/rc.d or the local startup"
183 echo "directories (${local_startup}), or is not executable"