1#!/bin/sh 2# $NetBSD: service,v 1.7 2015/04/05 11:33:15 apb Exp $ 3# service -- run or list system services 4# 5# Taken from FreeBSD: releng/10.1/usr.sbin/service/service.sh 268098 6# Modified for NetBSD by Adrian Steinmann in March, 2015 7# 8# Copyright (c) 2009 Douglas Barton 9# All rights reserved. 10# 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. 19# 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. 31 32export PATH=/sbin:/bin:/usr/sbin:/usr/bin 33 34usage () 35{ 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 45} 46 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() 50{ 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 57 fi 58 done | xargs rcorder -s nostart ${rc_rcorder_flags} 2>/dev/null 59 return 0 60} 61 62while getopts elv o; do 63 case "$o" in 64 e) ENABLED=1 ;; 65 l) LIST=1 ;; 66 v) VERBOSE=1 ;; 67 *) usage ;; 68 esac 69done 70shift $( expr $OPTIND - 1 ) 71 72[ -n "${ENABLED}" -a -n "${LIST}" ] && usage 73 74. /etc/rc.subr 75load_rc_config : 76 77if [ -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;$;)$;' ) 83 fi 84 _rc_files | $flt | while read file 85 do 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} 92 fi 93 fi 94 done 95 exit 0 96fi 97 98if [ -n "${LIST}" ]; then 99 [ -n "${VERBOSE}" ] && echo "rc_directories is ${rc_directories}" >&2 100 _rc_files 101 exit 0 102fi 103 104if [ $# -eq 2 ]; then 105 script=$1 106 arg=$2 107else 108 usage 109fi 110 111for 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 122 fi 123done 124 125echo "${script} does not exist in ${rc_directories}" >&2 126exit 1 127