rc.subr revision 1.11
11.11Slukem# $NetBSD: rc.subr,v 1.11 2000/03/10 11:39:27 lukem Exp $ 21.11Slukem# 31.11Slukem# Copyright (c) 1997-2000 The NetBSD Foundation, Inc. 41.11Slukem# All rights reserved. 51.11Slukem# 61.11Slukem# This code is derived from software contributed to The NetBSD Foundation 71.11Slukem# by Luke Mewburn. 81.11Slukem# 91.11Slukem# Redistribution and use in source and binary forms, with or without 101.11Slukem# modification, are permitted provided that the following conditions 111.11Slukem# are met: 121.11Slukem# 1. Redistributions of source code must retain the above copyright 131.11Slukem# notice, this list of conditions and the following disclaimer. 141.11Slukem# 2. Redistributions in binary form must reproduce the above copyright 151.11Slukem# notice, this list of conditions and the following disclaimer in the 161.11Slukem# documentation and/or other materials provided with the distribution. 171.11Slukem# 3. All advertising materials mentioning features or use of this software 181.11Slukem# must display the following acknowledgement: 191.11Slukem# This product includes software developed by the NetBSD 201.11Slukem# Foundation, Inc. and its contributors. 211.11Slukem# 4. Neither the name of The NetBSD Foundation nor the names of its 221.11Slukem# contributors may be used to endorse or promote products derived 231.11Slukem# from this software without specific prior written permission. 241.11Slukem# 251.11Slukem# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 261.11Slukem# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 271.11Slukem# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 281.11Slukem# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 291.11Slukem# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 301.11Slukem# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 311.11Slukem# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 321.11Slukem# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 331.11Slukem# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 341.11Slukem# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 351.11Slukem# POSSIBILITY OF SUCH DAMAGE. 361.11Slukem# 371.11Slukem# rc.subr 381.11Slukem# functions used by various rc scripts 391.11Slukem# 401.11Slukem 411.11Slukem# 421.11Slukem# functions 431.11Slukem# --------- 441.1Scjs 451.5Slukem# 461.11Slukem# checkyesno var 471.5Slukem# Test $1 variable, and warn if not set to YES or NO. 481.11Slukem# Return 0 if it's "yes" (et al), nonzero otherwise. 491.5Slukem# 501.11Slukemcheckyesno() 511.11Slukem{ 521.11Slukem eval _value=\$${1} 531.11Slukem case $_value in 541.4Slukem 551.4Slukem # "yes", "true", "on", or "1" 561.4Slukem [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 571.4Slukem return 0 581.3Slukem ;; 591.4Slukem 601.4Slukem # "no", "false", "off", or "0" 611.4Slukem [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 621.4Slukem return 1 631.3Slukem ;; 641.3Slukem *) 651.11Slukem warn "\$${1} is not set properly." 661.4Slukem return 1 671.3Slukem ;; 681.3Slukem esac 691.6Smellon} 701.6Smellon 711.6Smellon# 721.6Smellon# mount_critical_filesystems 731.6Smellon# Go through the list of critical filesystems, checking each one 741.6Smellon# to see if it is mounted, and if it is not, mounting it. 751.6Smellon# 761.11Slukemmount_critical_filesystems() 771.11Slukem{ 781.10Sdrochner if [ $1 = local ]; then 791.10Sdrochner _fslist=$critical_filesystems_beforenet 801.10Sdrochner else 811.10Sdrochner _fslist=$critical_filesystems 821.10Sdrochner fi 831.10Sdrochner for fs in $_fslist; do 841.6Smellon mount | ( 851.6Smellon ismounted=no 861.6Smellon while read what _on on _type type; do 871.6Smellon if [ $on = $fs ]; then 881.6Smellon ismounted=yes 891.6Smellon fi 901.6Smellon done 911.6Smellon if [ $ismounted = no ]; then 921.6Smellon mount $fs >/dev/null 2>&1 931.6Smellon fi 941.6Smellon ) 951.6Smellon done 961.7Scjs} 971.7Scjs 981.11Slukem# 991.11Slukem# check_pidfile pidfile procname 1001.11Slukem# Parses the first line of pidfile for a pid, and ensures 1011.11Slukem# that the process is running and matches procname. 1021.11Slukem# Prints the matching pid upon success, nothing otherwise. 1031.11Slukem# 1041.11Slukemcheck_pidfile() 1051.11Slukem{ 1061.11Slukem _pidfile=$1 1071.11Slukem _procname=$2 1081.11Slukem if [ -z "$_pidfile" -o -z "$_procname" ]; then 1091.11Slukem err 3 'USAGE: check_pidfile pidfile procname' 1101.11Slukem fi 1111.11Slukem if [ ! -f $_pidfile ]; then 1121.11Slukem return 1131.11Slukem fi 1141.11Slukem read _pid _junk < $_pidfile 1151.11Slukem if [ -z "$_pid" ]; then 1161.11Slukem return 1171.11Slukem fi 1181.11Slukem _procnamebn=`basename $_procname` 1191.11Slukem ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do 1201.11Slukem if [ "$_npid" = "PID" ]; then 1211.11Slukem continue 1221.11Slukem fi 1231.11Slukem if [ "$_arg0" = "$_procname" -o "$_arg0" = "$_procnamebn" \ 1241.11Slukem -o "$_arg0" = "${_procnamebn}:" ]; then 1251.11Slukem echo $_npid 1261.11Slukem return 1271.11Slukem fi 1281.11Slukem done 1291.11Slukem} 1301.11Slukem 1311.11Slukem# 1321.11Slukem# check_process procname 1331.11Slukem# Ensures that a process (or processes) named procname is running. 1341.11Slukem# Prints a list of matching pids. 1351.11Slukem# 1361.11Slukemcheck_process() 1371.11Slukem{ 1381.11Slukem _procname=$1 1391.11Slukem if [ -z "$_procname" ]; then 1401.11Slukem err 3 'USAGE: check_process procname' 1411.11Slukem fi 1421.11Slukem _procnamebn=`basename $_procname` 1431.11Slukem _pref= 1441.11Slukem ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do 1451.11Slukem if [ "$_npid" = "PID" ]; then 1461.11Slukem continue 1471.11Slukem fi 1481.11Slukem if [ "$_arg0" = "$_procname" -o "$_arg0" = "$_procnamebn" \ 1491.11Slukem -o "$_arg0" = "${_procnamebn}:" ]; then 1501.11Slukem echo -n "$_pref$_npid" 1511.11Slukem _pref=" " 1521.11Slukem fi 1531.11Slukem done 1541.11Slukem} 1551.11Slukem 1561.11Slukem# 1571.11Slukem# run_rc_command arg [supported_args] 1581.11Slukem# Scan supported_args (which has "start stop restart rcvar status" 1591.11Slukem# prepended) for arg. 1601.11Slukem# If there's a match, run ${arg}_cmd or the default command (see below). 1611.11Slukem# 1621.11Slukem# If arg has a given prefix, then: 1631.11Slukem# prefix operation 1641.11Slukem# ------ --------- 1651.11Slukem# fast Skip the pid check. 1661.11Slukem# force Set ${rcvar} to YES. 1671.11Slukem# 1681.11Slukem# The following globals are used: 1691.11Slukem# name needed function 1701.11Slukem# ---- ------ -------- 1711.11Slukem# name y Name of script. 1721.11Slukem# command n Full path to command. 1731.11Slukem# Not needed if ${arg}_cmd is set for 1741.11Slukem# each keyword. 1751.11Slukem# command_args n Optional args/shell directives for command. 1761.11Slukem# pidfile n If set, use check_pidfile $pidfile, else if 1771.11Slukem# $command is set, use check_process $command. 1781.11Slukem# ${rcvar} n If the default command is being run, this is 1791.11Slukem# checked with checkyesno to determine if 1801.11Slukem# the action should be run. 1811.11Slukem# If this variable isn't set, ${name} is checked 1821.11Slukem# instead. 1831.11Slukem# ${name}_flags n Arguments to call ${command} with. 1841.11Slukem# NOTE: if $flags is set (e.g, from the parent 1851.11Slukem# environment), it overrides this. 1861.11Slukem# ${_arg}_cmd n If set, use this as the action when invoked; 1871.11Slukem# $_arg is available to the action to use. 1881.11Slukem# Otherwise, use default command (see below) 1891.11Slukem# NOTE: checkyesno ${rcvar} is NOT performed 1901.11Slukem# for ${_arg}_cmd; use ${_arg}_precmd to 1911.11Slukem# do this. 1921.11Slukem# ${_arg}_precmd n If set, run just before performing the main 1931.11Slukem# action in the default command (i.e, after 1941.11Slukem# checking for required bits and process 1951.11Slukem# (non)existance). 1961.11Slukem# If this completes with a non-zero exit code, 1971.11Slukem# don't run ${_arg}_cmd. 1981.11Slukem# required_dirs n If set, check for the existence of the given 1991.11Slukem# directories before running the default 2001.11Slukem# (re)start command. 2011.11Slukem# required_files n If set, check for the readability of the given 2021.11Slukem# files before running the default (re)start 2031.11Slukem# command. 2041.11Slukem# required_vars n If set, perform checkyesno on each of the 2051.11Slukem# listed variables before running the default 2061.11Slukem# (re)start command. 2071.11Slukem# 2081.11Slukem# Default commands for a given arg: 2091.11Slukem# arg default 2101.11Slukem# --- ------- 2111.11Slukem# status Show if ${command} is running, etc. 2121.11Slukem# start if !running && checkyesno ${rcvar} 2131.11Slukem# ${command} 2141.11Slukem# stop if ${pidfile} 2151.11Slukem# kill $sig_stop `check_pidfile $pidfile` 2161.11Slukem# else 2171.11Slukem# kill $sig_stop `check_process $command` 2181.11Slukem# $sig_stop defaults to TERM. 2191.11Slukem# reload As stop, except use $sig_reload instead. 2201.11Slukem# $sig_reload defaults to HUP. 2211.11Slukem# restart Run `stop' then `start'. 2221.11Slukem# 2231.11Slukemrun_rc_command() 2241.11Slukem{ 2251.11Slukem if ! checkyesno rc_configured; then 2261.11Slukem err 1 '/etc/rc.conf is not configured' 2271.11Slukem fi 2281.11Slukem 2291.11Slukem _arg=$1 2301.11Slukem shift 2311.11Slukem _ckvar=${rcvar:-$name} 2321.11Slukem if [ -z "$_ckvar" ]; then 2331.11Slukem err 3 'neither $rcvar or $name is set.' 2341.11Slukem fi 2351.11Slukem 2361.11Slukem case "$_arg" in 2371.11Slukem fast*) 2381.11Slukem _arg=${_arg#fast} 2391.11Slukem _rc_fast_run=YES 2401.11Slukem ;; 2411.11Slukem force*) 2421.11Slukem _arg=${_arg#force} 2431.11Slukem eval ${_ckvar}=YES 2441.11Slukem ;; 2451.11Slukem esac 2461.11Slukem 2471.11Slukem _keywords="start stop restart rcvar $*" 2481.11Slukem _pidcmd= 2491.11Slukem if [ -z "$_rc_fast_run" ]; then 2501.11Slukem if [ -n "$pidfile" ]; then 2511.11Slukem _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 2521.11Slukem elif [ -n "$command" ]; then 2531.11Slukem _pidcmd='_pid=`check_process '$command'`' 2541.11Slukem fi 2551.11Slukem if [ -n "$_pidcmd" ]; then 2561.11Slukem _keywords="${_keywords} status" 2571.11Slukem fi 2581.11Slukem fi 2591.11Slukem 2601.11Slukem if [ -z "$_arg" ]; then 2611.11Slukem rc_usage "$_keywords" 2621.11Slukem fi 2631.11Slukem 2641.11Slukem if [ -n "$flags" ]; then 2651.11Slukem _flags=$flags 2661.11Slukem else 2671.11Slukem eval _flags=\$${name}_flags 2681.11Slukem fi 2691.11Slukem 2701.11Slukem eval $_pidcmd 2711.11Slukem 2721.11Slukem for _elem in $_keywords; do 2731.11Slukem if [ "$_elem" != "$_arg" ]; then 2741.11Slukem continue 2751.11Slukem fi 2761.11Slukem 2771.11Slukem eval _cmd=\$${_arg}_cmd 2781.11Slukem eval _precmd=\$${_arg}_precmd 2791.11Slukem if [ -n "$_cmd" ]; then 2801.11Slukem eval $_precmd || return 1 2811.11Slukem eval $_cmd 2821.11Slukem return 0 2831.11Slukem fi 2841.11Slukem 2851.11Slukem case "$_arg" in 2861.11Slukem 2871.11Slukem status) 2881.11Slukem if [ -n "$_pid" ]; then 2891.11Slukem echo "${name} is running as pid $_pid." 2901.11Slukem else 2911.11Slukem echo "${name} is not running." 2921.11Slukem fi 2931.11Slukem ;; 2941.11Slukem 2951.11Slukem start) 2961.11Slukem if [ -n "$_pid" ]; then 2971.11Slukem echo "${name} already running? (pid=$_pid)." 2981.11Slukem exit 1 2991.11Slukem fi 3001.11Slukem 3011.11Slukem if ! checkyesno ${_ckvar} || [ ! -x $command ]; then 3021.11Slukem return 0 3031.11Slukem fi 3041.11Slukem 3051.11Slukem for _f in $required_vars; do 3061.11Slukem if ! checkyesno $_f; then 3071.11Slukem warn \ 3081.11Slukem "\$${_f} is not set; ${name} not started." 3091.11Slukem return 1 3101.11Slukem fi 3111.11Slukem done 3121.11Slukem for _f in $required_dirs; do 3131.11Slukem if [ ! -d "${_f}/." ]; then 3141.11Slukem warn \ 3151.11Slukem "${_f} is not a directory; ${name} not started." 3161.11Slukem return 1 3171.11Slukem fi 3181.11Slukem done 3191.11Slukem for _f in $required_files; do 3201.11Slukem if [ ! -r "${_f}" ]; then 3211.11Slukem warn \ 3221.11Slukem "${_f} is not readable; ${name} not started." 3231.11Slukem return 1 3241.11Slukem fi 3251.11Slukem done 3261.11Slukem 3271.11Slukem eval $_precmd || return 1 3281.11Slukem echo "Starting ${name}." 3291.11Slukem eval $command $_flags $command_args 3301.11Slukem ;; 3311.11Slukem 3321.11Slukem stop) 3331.11Slukem if [ -z "$_pid" ]; then 3341.11Slukem if checkyesno ${_ckvar}; then 3351.11Slukem if [ -n "$pidfile" ]; then 3361.11Slukem echo \ 3371.11Slukem "${name} not running? (check $pidfile)." 3381.11Slukem else 3391.11Slukem echo "${name} not running?" 3401.11Slukem fi 3411.11Slukem exit 1 3421.11Slukem fi 3431.11Slukem return 0 3441.11Slukem fi 3451.11Slukem 3461.11Slukem eval $_precmd || return 1 3471.11Slukem echo "Stopping ${name}." 3481.11Slukem kill -${sig_stop:-TERM} $_pid 3491.11Slukem ;; 3501.11Slukem 3511.11Slukem reload) 3521.11Slukem if [ -z "$_pid" ]; then 3531.11Slukem if checkyesno ${_ckvar}; then 3541.11Slukem if [ -n "$pidfile" ]; then 3551.11Slukem echo \ 3561.11Slukem "${name} not running? (check $pidfile)." 3571.11Slukem else 3581.11Slukem echo "${name} not running?" 3591.11Slukem fi 3601.11Slukem exit 1 3611.11Slukem fi 3621.11Slukem return 0 3631.11Slukem fi 3641.11Slukem echo "Reloading ${name} config files." 3651.11Slukem eval $_precmd || return 1 3661.11Slukem kill -${sig_reload:-HUP} $_pid 3671.11Slukem ;; 3681.11Slukem 3691.11Slukem restart) 3701.11Slukem if ! checkyesno ${_ckvar}; then 3711.11Slukem return 0 3721.11Slukem fi 3731.11Slukem eval $_precmd || return 1 3741.11Slukem ( $0 stop ) 3751.11Slukem sleep 1 3761.11Slukem $0 start 3771.11Slukem 3781.11Slukem ;; 3791.11Slukem 3801.11Slukem rcvar) 3811.11Slukem echo "# $name" 3821.11Slukem if checkyesno ${_ckvar}; then 3831.11Slukem echo "\$${_ckvar}=YES" 3841.11Slukem else 3851.11Slukem echo "\$${_ckvar}=NO" 3861.11Slukem fi 3871.11Slukem ;; 3881.11Slukem 3891.11Slukem *) 3901.11Slukem rc_usage "$_keywords" 3911.11Slukem ;; 3921.11Slukem 3931.11Slukem esac 3941.11Slukem return 0 3951.11Slukem done 3961.11Slukem 3971.11Slukem echo 1>&2 "$0: unknown directive '$_arg'." 3981.11Slukem rc_usage "$_keywords" 3991.11Slukem exit 1 4001.11Slukem} 4011.11Slukem 4021.11Slukem# 4031.11Slukem# run_rc_script file arg 4041.11Slukem# Start the script `file' with `arg', and correctly handle the 4051.11Slukem# return value from the script. If `file' ends with `.sh', it's 4061.11Slukem# sourced into the current environment. Otherwise it's run as 4071.11Slukem# a child process. 4081.11Slukem# Note: because `.sh' files are sourced into the current environment 4091.11Slukem# run_rc_command shouldn't be used because its difficult to ensure 4101.11Slukem# that the global variable state before and after the sourcing of 4111.11Slukem# the .sh file won't adversely affect other scripts. 4121.11Slukem# 4131.11Slukemrun_rc_script() 4141.11Slukem{ 4151.11Slukem _file=$1 4161.11Slukem _arg=$2 4171.11Slukem if [ -z "$_file" -o -z "$_arg" ]; then 4181.11Slukem err 3 'USAGE: run_rc_script file arg' 4191.11Slukem fi 4201.11Slukem 4211.11Slukem _narg=$_arg 4221.11Slukem case "$_narg" in 4231.11Slukem fast*) 4241.11Slukem _narg=${_narg#fast} 4251.11Slukem ;; 4261.11Slukem force*) 4271.11Slukem _narg=${_narg#force} 4281.11Slukem ;; 4291.11Slukem esac 4301.11Slukem eval ${_narg}_precmd="" 4311.11Slukem eval ${_narg}_cmd="" 4321.11Slukem 4331.11Slukem case "$_file" in 4341.11Slukem *.sh) # run in current shell 4351.11Slukem set $_arg ; . $_file 4361.11Slukem ;; 4371.11Slukem *) # run in subshell 4381.11Slukem ( set $_arg ; . $_file ) 4391.11Slukem ;; 4401.11Slukem esac 4411.11Slukem} 4421.11Slukem 4431.11Slukem# 4441.11Slukem# rc_usage commands 4451.11Slukem# Print a usage string for $0, with `commands' being a list of 4461.11Slukem# valid commands. 4471.11Slukem# 4481.11Slukemrc_usage() 4491.11Slukem{ 4501.11Slukem echo -n 1>&2 "Usage: $0 [fast|force](" 4511.11Slukem 4521.11Slukem _sep= 4531.11Slukem for _elem in $*; do 4541.11Slukem echo -n 1>&2 "$_sep$_elem" 4551.11Slukem _sep="|" 4561.11Slukem done 4571.11Slukem echo 1>&2 ")" 4581.11Slukem exit 1 4591.11Slukem} 4601.11Slukem 4611.11Slukem# 4621.11Slukem# err exitval message 4631.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 4641.11Slukem# 4651.11Slukemerr() 4661.11Slukem{ 4671.11Slukem exitval=$1 4681.11Slukem shift 4691.11Slukem 4701.11Slukem logger "$0: ERROR $*" 4711.11Slukem echo 1>&2 "$0: ERROR $*" 4721.11Slukem exit $exitval 4731.11Slukem} 4741.11Slukem 4751.11Slukem# 4761.11Slukem# warn message 4771.11Slukem# Display message to stderr and log to the syslog. 4781.11Slukem# 4791.11Slukemwarn() 4801.11Slukem{ 4811.11Slukem logger "$0: WARNING $*" 4821.11Slukem echo 1>&2 "$0: WARNING $*" 4831.1Scjs} 484