Drop main() prototype. Syncs with NetBSD-8
[minix.git] / usr.sbin / service / service
blobbf5310a71756b2f9267181560f0f14117cc83d34
1 #!/bin/sh
2 # $NetBSD: service,v 1.7 2015/04/05 11:33:15 apb Exp $
3 # service -- run or list system services
5 # Taken from FreeBSD: releng/10.1/usr.sbin/service/service.sh 268098
6 # Modified for NetBSD by Adrian Steinmann in March, 2015
8 # Copyright (c) 2009 Douglas Barton
9 # All rights reserved.
11 # Redistribution and use in source and binary forms, with or without
12 # modification, are permitted provided that the following conditions
13 # are met:
14 # 1. Redistributions of source code must retain the above copyright
15 # notice, this list of conditions and the following disclaimer.
16 # 2. Redistributions in binary form must reproduce the above copyright
17 # notice, this list of conditions and the following disclaimer in the
18 # documentation and/or other materials provided with the distribution.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
32 export PATH=/sbin:/bin:/usr/sbin:/usr/bin
34 usage ()
36 local me="${0##*/}"
37 echo "usage: ${me} [-elv]"
38 echo " ${me} [-ev] rc_script_name [rc_script_name2 [...]]"
39 echo " ${me} [-v] rc_script_name action"
40 echo " -e: List enabled scripts; check if given scripts are enabled"
41 echo " -l: List all scripts in rcorder"
42 echo " -v: Verbose (mention in which directory script is found)"
43 echo "rc_directories is currently set to ${rc_directories}"
44 exit 1
47 # list all files in rc_directories with absolute pathnames
48 # written to be compatible with ls(1) from pre netbsd-7
49 _rc_files()
51 local _d _f
52 for _d in ${rc_directories}; do
53 if [ -d "$_d" ]; then
54 for _f in "$_d"/*; do
55 [ -f "$_f" -a -x "$_f" ] && echo "$_f"
56 done
58 done | xargs rcorder -s nostart ${rc_rcorder_flags} 2>/dev/null
59 return 0
62 while getopts elv o; do
63 case "$o" in
64 e) ENABLED=1 ;;
65 l) LIST=1 ;;
66 v) VERBOSE=1 ;;
67 *) usage ;;
68 esac
69 done
70 shift $( expr $OPTIND - 1 )
72 [ -n "${ENABLED}" -a -n "${LIST}" ] && usage
74 . /etc/rc.subr
75 load_rc_config :
77 if [ -n "${ENABLED}" ]; then
78 [ -n "${VERBOSE}" ] && echo "rc_directories is ${rc_directories}" >&2
79 flt=cat
80 if [ $# -gt 0 ]
81 then
82 flt=$( echo $* | sed -e 's; ;|;g' -e 's;^;egrep /(;' -e 's;$;)$;' )
84 _rc_files | $flt | while read file
86 if grep -q ^rcvar "$file"; then
87 eval $( grep ^name= "$file" )
88 eval $( grep ^rcvar "$file" )
89 if [ -n "${rcvar}" ]; then
90 load_rc_config ${rcvar}
91 checkyesno ${rcvar} 2>/dev/null && echo ${file}
94 done
95 exit 0
98 if [ -n "${LIST}" ]; then
99 [ -n "${VERBOSE}" ] && echo "rc_directories is ${rc_directories}" >&2
100 _rc_files
101 exit 0
104 if [ $# -eq 2 ]; then
105 script=$1
106 arg=$2
107 else
108 usage
111 for dir in ${rc_directories}; do
112 if [ -x "${dir}/${script}" ]; then
113 [ -n "${VERBOSE}" ] && echo "${script} is located in ${dir}" >&2
114 # run as in /etc/rc
115 cd /
116 umask 022
117 exec env -i \
118 HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin \
119 "${dir}/${script}" "${arg}"
120 echo "Failed to exec ${dir}/${script} ${arg}" >&2
121 exit 255
123 done
125 echo "${script} does not exist in ${rc_directories}" >&2
126 exit 1