xref: /netbsd/usr.sbin/service/service (revision c706eb5f)
1226f7e91Sast#!/bin/sh
2*c706eb5fSast#    $NetBSD: service,v 1.4 2015/03/23 23:28:55 ast Exp $
3226f7e91Sast#    service -- run or list system services
4226f7e91Sast#
5226f7e91Sast#  Taken from FreeBSD: releng/10.1/usr.sbin/service/service.sh 268098
6226f7e91Sast#  Modified for NetBSD by Adrian Steinmann in March, 2015
7226f7e91Sast#
8226f7e91Sast#  Copyright (c) 2009 Douglas Barton
9226f7e91Sast#  All rights reserved.
10226f7e91Sast#
11226f7e91Sast#  Redistribution and use in source and binary forms, with or without
12226f7e91Sast#  modification, are permitted provided that the following conditions
13226f7e91Sast#  are met:
14226f7e91Sast#  1. Redistributions of source code must retain the above copyright
15226f7e91Sast#     notice, this list of conditions and the following disclaimer.
16226f7e91Sast#  2. Redistributions in binary form must reproduce the above copyright
17226f7e91Sast#     notice, this list of conditions and the following disclaimer in the
18226f7e91Sast#     documentation and/or other materials provided with the distribution.
19226f7e91Sast#
20226f7e91Sast#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21226f7e91Sast#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22226f7e91Sast#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23226f7e91Sast#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24226f7e91Sast#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25226f7e91Sast#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26226f7e91Sast#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27226f7e91Sast#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28226f7e91Sast#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29226f7e91Sast#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30226f7e91Sast#  SUCH DAMAGE.
31226f7e91Sast
32226f7e91Sastexport PATH=/sbin:/bin:/usr/sbin:/usr/bin
33226f7e91Sast
34226f7e91Sastusage ()
35226f7e91Sast{
36226f7e91Sast    local me=${0##*/}
37b6991463Swiz    echo "usage: ${me} [-elv]"
38b6991463Swiz    echo "       ${me} [-ev] rc_script_name [rc_script_name2 [...]]"
39*c706eb5fSast    echo "       ${me} [-v] rc_script_name action"
40226f7e91Sast    echo "       -e: List enabled scripts; check if given scripts are enabled"
41b6991463Swiz    echo "       -l: List all scripts in rcorder"
42226f7e91Sast    echo "       -v: Verbose (mention in which directory script is found)"
43226f7e91Sast    echo "rc_directories is currently set to ${rc_directories}"
44*c706eb5fSast    exit 1
45226f7e91Sast}
46226f7e91Sast
47226f7e91Sastrc_files()
48226f7e91Sast{
49226f7e91Sast    local dir
50226f7e91Sast    for dir in ${rc_directories}; do
51226f7e91Sast        [ -d ${dir} ] && ls -P1 ${dir} 2>/dev/null
52226f7e91Sast    done | xargs rcorder -s nostart ${rc_rcorder_flags} 2>/dev/null
53226f7e91Sast    return 0
54226f7e91Sast}
55226f7e91Sast
56*c706eb5fSastwhile getopts elv o; do
57226f7e91Sast    case $o in
58*c706eb5fSast        e) ENABLED=1 ;;
59226f7e91Sast        l) LIST=1 ;;
60226f7e91Sast        v) VERBOSE=1 ;;
61*c706eb5fSast        *) usage ;;
62226f7e91Sast    esac
63226f7e91Sastdone
64*c706eb5fSastshift $( expr $OPTIND - 1 )
65*c706eb5fSast
66*c706eb5fSast[ -n "${ENABLED}" -a -n "${LIST}" ] && usage
67*c706eb5fSast
68*c706eb5fSast. /etc/rc.subr
69*c706eb5fSastload_rc_config :
70226f7e91Sast
71226f7e91Sastif [ -n "${ENABLED}" ]; then
72226f7e91Sast    [ -n "${VERBOSE}" ] && echo "rc_directories is ${rc_directories}" >&2
73226f7e91Sast    flt=cat
74226f7e91Sast    if [ $# -gt 0 ]
75226f7e91Sast    then
76226f7e91Sast        flt=$( echo $* | sed -e 's; ;|;g' -e 's;^;egrep /(;' -e 's;$;)$;' )
77226f7e91Sast    fi
78226f7e91Sast    rc_files | $flt | while read file
79226f7e91Sast    do
80226f7e91Sast        if grep -q ^rcvar $file; then
81226f7e91Sast            eval $( grep ^name= $file )
82226f7e91Sast            eval $( grep ^rcvar $file )
83226f7e91Sast            checkyesno ${rcvar} 2>/dev/null && echo ${file}
84226f7e91Sast        fi
85226f7e91Sast    done
86226f7e91Sast    exit 0
87226f7e91Sastfi
88226f7e91Sast
89226f7e91Sastif [ -n "${LIST}" ]; then
90226f7e91Sast    [ -n "${VERBOSE}" ] && echo "rc_directories is ${rc_directories}" >&2
91226f7e91Sast    rc_files
92226f7e91Sast    exit 0
93226f7e91Sastfi
94226f7e91Sast
95226f7e91Sastif [ $# -eq 2 ]; then
96226f7e91Sast    script=$1
97226f7e91Sast    arg=$2
98226f7e91Sastelse
99226f7e91Sast    usage
100226f7e91Sastfi
101226f7e91Sast
102226f7e91Sastfor dir in ${rc_directories}; do
103226f7e91Sast    if [ -x "${dir}/${script}" ]; then
104226f7e91Sast        [ -n "${VERBOSE}" ] && echo "${script} is located in ${dir}" >&2
105226f7e91Sast        # run as in /etc/rc
106226f7e91Sast        cd /
107226f7e91Sast        umask 022
108226f7e91Sast        exec env -i \
109226f7e91Sast            HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin \
110226f7e91Sast                ${dir}/${script} ${arg}
111226f7e91Sast        echo "Failed to exec ${dir}/${script} ${arg}" >&2
112226f7e91Sast        exit 255
113226f7e91Sast    fi
114226f7e91Sastdone
115226f7e91Sast
116226f7e91Sastecho "${script} does not exist in ${rc_directories}" >&2
117226f7e91Sastexit 1
118