]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/service/service.sh
service(8): use an environment more consistent with init(8)
[FreeBSD/FreeBSD.git] / usr.sbin / service / service.sh
1 #!/bin/sh
2
3 # $FreeBSD$
4
5 # SPDX-License-Identifier: BSD-2-Clause-FreeBSD
6 #
7 #  Copyright (c) 2009 Douglas Barton
8 #  All rights reserved.
9 #
10 #  Redistribution and use in source and binary forms, with or without
11 #  modification, are permitted provided that the following conditions
12 #  are met:
13 #  1. Redistributions of source code must retain the above copyright
14 #     notice, this list of conditions and the following disclaimer.
15 #  2. Redistributions in binary form must reproduce the above copyright
16 #     notice, this list of conditions and the following disclaimer in the
17 #     documentation and/or other materials provided with the distribution.
18 #
19 #  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 #  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 #  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 #  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 #  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 #  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 #  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 #  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 #  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 #  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #  SUCH DAMAGE.
30
31 . /etc/rc.subr
32 load_rc_config 'XXX'
33
34 usage () {
35         echo ''
36         echo 'Usage:'
37         echo "${0##*/} [-j <jail name or id>] -e"
38         echo "${0##*/} [-j <jail name or id>] -R"
39         echo "${0##*/} [-j <jail name or id>] [-v] -l | -r"
40         echo "${0##*/} [-j <jail name or id>] [-v] <rc.d script> start|stop|etc."
41         echo "${0##*/} -h"
42         echo ''
43         echo "-j        Perform actions within the named jail"
44         echo '-e        Show services that are enabled'
45         echo "-R        Stop and start enabled $local_startup services"
46         echo "-l        List all scripts in /etc/rc.d and $local_startup"
47         echo '-r        Show the results of boot time rcorder'
48         echo '-v        Verbose'
49         echo ''
50 }
51
52 while getopts 'j:ehlrRv' COMMAND_LINE_ARGUMENT ; do
53         case "${COMMAND_LINE_ARGUMENT}" in
54         j)      JAIL="${OPTARG}" ;;
55         e)      ENABLED=eopt ;;
56         h)      usage ; exit 0 ;;
57         l)      LIST=lopt ;;
58         r)      RCORDER=ropt ;;
59         R)      RESTART=Ropt ;;
60         v)      VERBOSE=vopt ;;
61         *)      usage ; exit 1 ;;
62         esac
63 done
64 shift $(( $OPTIND - 1 ))
65
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.
69         args=""
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
76         # Call jexec(8) with the rebuild args and any positional args that
77         # were left in $@
78         /usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@"
79         exit $?
80 fi
81
82 if [ -n "$RESTART" ]; then
83         skip="-s nostart"
84         if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
85                 skip="$skip -s nojail"
86                 if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
87                         skip="$skip -s nojailvnet"
88                 fi
89         fi
90         [ -n "$local_startup" ] && find_local_scripts_new
91         files=`rcorder ${skip} ${local_rc} 2>/dev/null`
92
93         for file in `reverse_list ${files}`; do
94                 if grep -q ^rcvar $file; then
95                         eval `grep ^name= $file`
96                         eval `grep ^rcvar $file`
97                         if [ -n "$rcvar" ]; then
98                                 load_rc_config_var ${name} ${rcvar}
99                         fi
100                         checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop
101                 fi
102         done
103         for file in $files; do
104                 if grep -q ^rcvar $file; then
105                         eval `grep ^name= $file`
106                         eval `grep ^rcvar $file`
107                         checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start
108                 fi
109         done
110
111         exit 0
112 fi
113
114 if [ -n "$ENABLED" -o -n "$RCORDER" ]; then
115         # Copied from /etc/rc
116         skip="-s nostart"
117         if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then
118                 skip="$skip -s nojail"
119                 if [ `/sbin/sysctl -n security.jail.vnet` -ne 1 ]; then
120                         skip="$skip -s nojailvnet"
121                 fi
122         fi
123         [ -n "$local_startup" ] && find_local_scripts_new
124         files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null`
125 fi
126
127 if [ -n "$ENABLED" ]; then
128         for file in $files; do
129                 if grep -q ^rcvar $file; then
130                         eval `grep ^name= $file`
131                         eval `grep ^rcvar $file`
132                         if [ -n "$rcvar" ]; then
133                                 load_rc_config_var ${name} ${rcvar}
134                         fi
135                         checkyesno $rcvar 2>/dev/null && echo $file
136                 fi
137         done
138         exit 0
139 fi
140
141 if [ -n "$LIST" ]; then
142         for dir in /etc/rc.d $local_startup; do
143                 [ -n "$VERBOSE" ] && echo "From ${dir}:"
144                 [ -d ${dir} ] && /bin/ls -1 ${dir}
145         done
146         exit 0
147 fi
148
149 if [ -n "$RCORDER" ]; then
150         for file in $files; do
151                 echo $file
152                 if [ -n "$VERBOSE" ]; then
153                         case "$file" in
154                         */${early_late_divider})
155                                 echo '========= Early/Late Divider =========' ;;
156                         esac
157                 fi
158         done
159         exit 0
160 fi
161
162 if [ $# -gt 1 ]; then
163         script=$1
164         shift
165 else
166         usage
167         exit 1
168 fi
169
170 cd /
171 for dir in /etc/rc.d $local_startup; do
172         if [ -x "$dir/$script" ]; then
173                 [ -n "$VERBOSE" ] && echo "$script is located in $dir"
174                 exec env -i -L -/daemon HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin "$dir/$script" "$@"
175         fi
176 done
177
178 # If the script was not found
179 echo "$script does not exist in /etc/rc.d or the local startup"
180 echo "directories (${local_startup}), or is not executable"
181 exit 1