rc.subr revision 1.63
11.63Slukem# $NetBSD: rc.subr,v 1.63 2004/05/03 14:52:26 lukem Exp $ 21.11Slukem# 31.41Slukem# Copyright (c) 1997-2002 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.62Sjmmv: ${rcvar_manpage:='rc.conf(5)'} 421.62Sjmmv 431.11Slukem# 441.11Slukem# functions 451.11Slukem# --------- 461.1Scjs 471.5Slukem# 481.11Slukem# checkyesno var 491.5Slukem# Test $1 variable, and warn if not set to YES or NO. 501.11Slukem# Return 0 if it's "yes" (et al), nonzero otherwise. 511.5Slukem# 521.11Slukemcheckyesno() 531.11Slukem{ 541.11Slukem eval _value=\$${1} 551.11Slukem case $_value in 561.4Slukem 571.4Slukem # "yes", "true", "on", or "1" 581.4Slukem [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 591.4Slukem return 0 601.3Slukem ;; 611.4Slukem 621.4Slukem # "no", "false", "off", or "0" 631.4Slukem [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 641.4Slukem return 1 651.3Slukem ;; 661.3Slukem *) 671.62Sjmmv warn "\$${1} is not set properly - see ${rcvar_manpage}." 681.4Slukem return 1 691.3Slukem ;; 701.3Slukem esac 711.38Slukem} 721.38Slukem 731.38Slukem# reverse_list list 741.38Slukem# print the list in reverse order 751.38Slukem# 761.38Slukemreverse_list() 771.38Slukem{ 781.38Slukem _revlist= 791.56Schristos for _revfile; do 801.38Slukem _revlist="$_revfile $_revlist" 811.38Slukem done 821.38Slukem echo $_revlist 831.6Smellon} 841.6Smellon 851.6Smellon# 861.47Slukem# mount_critical_filesystems type 871.47Slukem# Go through the list of critical filesystems as provided in 881.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 891.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 901.6Smellon# 911.11Slukemmount_critical_filesystems() 921.11Slukem{ 931.47Slukem eval _fslist=\$critical_filesystems_${1} 941.17Slukem for _fs in $_fslist; do 951.6Smellon mount | ( 961.53Slukem _ismounted=false 971.6Smellon while read what _on on _type type; do 981.17Slukem if [ $on = $_fs ]; then 991.53Slukem _ismounted=true 1001.6Smellon fi 1011.6Smellon done 1021.53Slukem if $_ismounted; then 1031.55Slukem : 1041.55Slukem else 1051.17Slukem mount $_fs >/dev/null 2>&1 1061.6Smellon fi 1071.46Slukem ) 1081.6Smellon done 1091.7Scjs} 1101.7Scjs 1111.11Slukem# 1121.45Slukem# check_pidfile pidfile procname [interpreter] 1131.45Slukem# Parses the first line of pidfile for a PID, and ensures 1141.11Slukem# that the process is running and matches procname. 1151.45Slukem# Prints the matching PID upon success, nothing otherwise. 1161.45Slukem# interpreter is optional; see _find_processes() for details. 1171.11Slukem# 1181.11Slukemcheck_pidfile() 1191.11Slukem{ 1201.11Slukem _pidfile=$1 1211.11Slukem _procname=$2 1221.45Slukem _interpreter=$3 1231.11Slukem if [ -z "$_pidfile" -o -z "$_procname" ]; then 1241.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 1251.11Slukem fi 1261.11Slukem if [ ! -f $_pidfile ]; then 1271.11Slukem return 1281.11Slukem fi 1291.11Slukem read _pid _junk < $_pidfile 1301.11Slukem if [ -z "$_pid" ]; then 1311.11Slukem return 1321.11Slukem fi 1331.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 1341.11Slukem} 1351.11Slukem 1361.11Slukem# 1371.45Slukem# check_process procname [interpreter] 1381.11Slukem# Ensures that a process (or processes) named procname is running. 1391.45Slukem# Prints a list of matching PIDs. 1401.45Slukem# interpreter is optional; see _find_processes() for details. 1411.11Slukem# 1421.11Slukemcheck_process() 1431.11Slukem{ 1441.11Slukem _procname=$1 1451.45Slukem _interpreter=$2 1461.11Slukem if [ -z "$_procname" ]; then 1471.45Slukem err 3 'USAGE: check_process procname [interpreter]' 1481.45Slukem fi 1491.45Slukem _find_processes $_procname ${_interpreter:-.} '-ax' 1501.45Slukem} 1511.45Slukem 1521.46Slukem# 1531.45Slukem# _find_processes procname interpreter psargs 1541.45Slukem# Search for procname in the output of ps generated by psargs. 1551.45Slukem# Prints the PIDs of any matching processes, space separated. 1561.45Slukem# 1571.45Slukem# If interpreter == ".", check the following variations of procname 1581.45Slukem# against the first word of each command: 1591.45Slukem# procname 1601.45Slukem# `basename procname` 1611.45Slukem# `basename procname` + ":" 1621.45Slukem# "(" + `basename procname` + ")" 1631.45Slukem# 1641.45Slukem# If interpreter != ".", read the first line of procname, remove the 1651.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 1661.45Slukem# match that against each command, either as is, or with extra words 1671.45Slukem# at the end. 1681.45Slukem# 1691.45Slukem_find_processes() 1701.45Slukem{ 1711.45Slukem if [ $# -ne 3 ]; then 1721.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 1731.11Slukem fi 1741.45Slukem _procname=$1 1751.45Slukem _interpreter=$2 1761.45Slukem _psargs=$3 1771.45Slukem 1781.11Slukem _pref= 1791.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 1801.45Slukem read _interp < $_procname # read interpreter name 1811.45Slukem _interp=${_interp#\#!} # strip #! 1821.45Slukem set -- $_interp 1831.45Slukem if [ $_interpreter != $1 ]; then 1841.45Slukem warn "\$command_interpreter $_interpreter != $1" 1851.45Slukem fi 1861.45Slukem _interp="$* $_procname" # cleanup spaces, add _procname 1871.45Slukem _fp_args='_argv' 1881.45Slukem _fp_match='case "$_argv" in 1891.45Slukem ${_interp}|"${_interp} "*)' 1901.45Slukem else # a normal daemon 1911.45Slukem _procnamebn=${_procname##*/} 1921.45Slukem _fp_args='_arg0 _argv' 1931.45Slukem _fp_match='case "$_arg0" in 1941.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 1951.45Slukem fi 1961.45Slukem 1971.45Slukem _proccheck=' 1981.46Slukem ps -o "pid,command" '"$_psargs"' | 1991.45Slukem while read _npid '"$_fp_args"'; do 2001.45Slukem case "$_npid" in 2011.45Slukem PID) 2021.45Slukem continue ;; 2031.45Slukem esac ; '"$_fp_match"' 2041.45Slukem echo -n "$_pref$_npid" ; 2051.45Slukem _pref=" " 2061.45Slukem ;; 2071.45Slukem esac 2081.45Slukem done' 2091.45Slukem 2101.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 2111.45Slukem eval $_proccheck 2121.11Slukem} 2131.11Slukem 2141.11Slukem# 2151.33Slukem# wait_for_pids pid [pid ...] 2161.35Slukem# spins until none of the pids exist 2171.33Slukem# 2181.33Slukemwait_for_pids() 2191.33Slukem{ 2201.56Schristos _list="$@" 2211.33Slukem if [ -z "$_list" ]; then 2221.33Slukem return 2231.33Slukem fi 2241.35Slukem _prefix= 2251.35Slukem while true; do 2261.33Slukem _nlist=""; 2271.33Slukem for _j in $_list; do 2281.33Slukem if kill -0 $_j 2>/dev/null; then 2291.33Slukem _nlist="${_nlist}${_nlist:+ }$_j" 2301.33Slukem fi 2311.33Slukem done 2321.33Slukem if [ -z "$_nlist" ]; then 2331.33Slukem break 2341.33Slukem fi 2351.33Slukem _list=$_nlist 2361.35Slukem echo -n ${_prefix:-"Waiting for PIDS: "}$_list 2371.35Slukem _prefix=", " 2381.35Slukem sleep 2 2391.33Slukem done 2401.35Slukem if [ -n "$_prefix" ]; then 2411.35Slukem echo "." 2421.35Slukem fi 2431.33Slukem} 2441.33Slukem 2451.33Slukem# 2461.46Slukem# run_rc_command argument 2471.46Slukem# Search for argument in the list of supported commands, which is: 2481.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 2491.46Slukem# If there's a match, run ${argument}_cmd or the default method 2501.46Slukem# (see below). 2511.11Slukem# 2521.46Slukem# If argument has a given prefix, then change the operation as follows: 2531.46Slukem# Prefix Operation 2541.11Slukem# ------ --------- 2551.48Slukem# fast Skip the pid check, and set rc_fast=yes 2561.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 2571.61Slukem# one Set ${rcvar} to YES 2581.11Slukem# 2591.11Slukem# The following globals are used: 2601.24Slukem# 2611.46Slukem# Name Needed Purpose 2621.46Slukem# ---- ------ ------- 2631.11Slukem# name y Name of script. 2641.24Slukem# 2651.11Slukem# command n Full path to command. 2661.46Slukem# Not needed if ${rc_arg}_cmd is set for 2671.11Slukem# each keyword. 2681.24Slukem# 2691.11Slukem# command_args n Optional args/shell directives for command. 2701.24Slukem# 2711.45Slukem# command_interpreter n If not empty, command is interpreted, so 2721.45Slukem# call check_{pidfile,process}() appropriately. 2731.45Slukem# 2741.16Slukem# extra_commands n List of extra commands supported. 2751.24Slukem# 2761.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 2771.42Slukem# otherwise use check_process $command. 2781.42Slukem# In either case, only check if $command is set. 2791.42Slukem# 2801.42Slukem# procname n Process name to check for instead of $command. 2811.24Slukem# 2821.24Slukem# rcvar n This is checked with checkyesno to determine 2831.24Slukem# if the action should be run. 2841.24Slukem# 2851.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 2861.42Slukem# Requires /usr to be mounted. 2871.24Slukem# 2881.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 2891.22Slukem# (if not using ${name}_chroot). 2901.24Slukem# 2911.11Slukem# ${name}_flags n Arguments to call ${command} with. 2921.24Slukem# NOTE: $flags from the parent environment 2931.24Slukem# can be used to override this. 2941.24Slukem# 2951.23Slukem# ${name}_nice n Nice level to run ${command} at. 2961.24Slukem# 2971.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 2981.22Slukem# using ${name}_chroot. 2991.42Slukem# Requires /usr to be mounted. 3001.24Slukem# 3011.22Slukem# ${name}_group n Group to run chrooted ${command} as. 3021.42Slukem# Requires /usr to be mounted. 3031.24Slukem# 3041.32Slukem# ${name}_groups n Comma separated list of supplementary groups 3051.32Slukem# to run the chrooted ${command} with. 3061.42Slukem# Requires /usr to be mounted. 3071.24Slukem# 3081.50Satatat# ${name}_systrace n Flags passed to systrace(1) if it is used. 3091.50Satatat# Setting this variable enables systracing 3101.50Satatat# of the given program. The use of "-a" is 3111.50Satatat# recommended so that the boot process is not 3121.50Satatat# stalled. In order to pass no flags to 3131.50Satatat# systrace, set this variable to "--". 3141.50Satatat# 3151.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 3161.11Slukem# Otherwise, use default command (see below) 3171.24Slukem# 3181.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 3191.46Slukem# ${rc_arg}_cmd method in the default 3201.46Slukem# operation (i.e, after checking for required 3211.46Slukem# bits and process (non)existence). 3221.11Slukem# If this completes with a non-zero exit code, 3231.46Slukem# don't run ${rc_arg}_cmd. 3241.24Slukem# 3251.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 3261.46Slukem# ${rc_arg}_cmd method, if that method 3271.43Slukem# returned a zero exit code. 3281.43Slukem# 3291.11Slukem# required_dirs n If set, check for the existence of the given 3301.11Slukem# directories before running the default 3311.11Slukem# (re)start command. 3321.24Slukem# 3331.11Slukem# required_files n If set, check for the readability of the given 3341.11Slukem# files before running the default (re)start 3351.11Slukem# command. 3361.24Slukem# 3371.11Slukem# required_vars n If set, perform checkyesno on each of the 3381.11Slukem# listed variables before running the default 3391.11Slukem# (re)start command. 3401.11Slukem# 3411.46Slukem# Default behaviour for a given argument, if no override method is 3421.46Slukem# provided: 3431.24Slukem# 3441.46Slukem# Argument Default behaviour 3451.46Slukem# -------- ----------------- 3461.11Slukem# start if !running && checkyesno ${rcvar} 3471.11Slukem# ${command} 3481.24Slukem# 3491.11Slukem# stop if ${pidfile} 3501.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 3511.11Slukem# else 3521.46Slukem# rc_pid=$(check_process $command) 3531.46Slukem# kill $sig_stop $rc_pid 3541.46Slukem# wait_for_pids $rc_pid 3551.35Slukem# ($sig_stop defaults to TERM.) 3561.24Slukem# 3571.35Slukem# reload Similar to stop, except use $sig_reload instead, 3581.35Slukem# and doesn't wait_for_pids. 3591.11Slukem# $sig_reload defaults to HUP. 3601.24Slukem# 3611.11Slukem# restart Run `stop' then `start'. 3621.11Slukem# 3631.33Slukem# status Show if ${command} is running, etc. 3641.33Slukem# 3651.33Slukem# poll Wait for ${command} to exit. 3661.33Slukem# 3671.33Slukem# rcvar Display what rc.conf variable is used (if any). 3681.33Slukem# 3691.46Slukem# Variables available to methods, and after run_rc_command() has 3701.46Slukem# completed: 3711.46Slukem# 3721.46Slukem# Variable Purpose 3731.46Slukem# -------- ------- 3741.61Slukem# rc_arg Argument to command, after fast/force/one processing 3751.46Slukem# performed 3761.46Slukem# 3771.46Slukem# rc_flags Flags to start the default command with. 3781.46Slukem# Defaults to ${name}_flags, unless overridden 3791.46Slukem# by $flags from the environment. 3801.46Slukem# This variable may be changed by the precmd method. 3811.46Slukem# 3821.46Slukem# rc_pid PID of command (if appropriate) 3831.46Slukem# 3841.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 3851.46Slukem# 3861.46Slukem# rc_force Not empty if "force" was provided (q.v.) 3871.33Slukem# 3881.24Slukem# 3891.11Slukemrun_rc_command() 3901.11Slukem{ 3911.46Slukem rc_arg=$1 3921.24Slukem if [ -z "$name" ]; then 3931.30Slukem err 3 'run_rc_command: $name is not set.' 3941.11Slukem fi 3951.11Slukem 3961.61Slukem _rc_prefix= 3971.46Slukem case "$rc_arg" in 3981.24Slukem fast*) # "fast" prefix; don't check pid 3991.46Slukem rc_arg=${rc_arg#fast} 4001.48Slukem rc_fast=yes 4011.11Slukem ;; 4021.61Slukem force*) # "force" prefix; always run 4031.48Slukem rc_force=yes 4041.61Slukem _rc_prefix=force 4051.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4061.61Slukem if [ -n "${rcvar}" ]; then 4071.61Slukem eval ${rcvar}=YES 4081.61Slukem fi 4091.61Slukem ;; 4101.61Slukem one*) # "one" prefix; set ${rcvar}=yes 4111.61Slukem _rc_prefix=one 4121.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4131.29Slukem if [ -n "${rcvar}" ]; then 4141.29Slukem eval ${rcvar}=YES 4151.29Slukem fi 4161.11Slukem ;; 4171.11Slukem esac 4181.11Slukem 4191.16Slukem _keywords="start stop restart rcvar $extra_commands" 4201.46Slukem rc_pid= 4211.11Slukem _pidcmd= 4221.42Slukem _procname=${procname:-${command}} 4231.42Slukem 4241.24Slukem # setup pid check command if not fast 4251.46Slukem if [ -z "$rc_fast" -a -n "$_procname" ]; then 4261.11Slukem if [ -n "$pidfile" ]; then 4271.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 4281.42Slukem else 4291.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 4301.11Slukem fi 4311.11Slukem if [ -n "$_pidcmd" ]; then 4321.33Slukem _keywords="${_keywords} status poll" 4331.11Slukem fi 4341.11Slukem fi 4351.11Slukem 4361.46Slukem if [ -z "$rc_arg" ]; then 4371.11Slukem rc_usage "$_keywords" 4381.11Slukem fi 4391.11Slukem 4401.17Slukem if [ -n "$flags" ]; then # allow override from environment 4411.46Slukem rc_flags=$flags 4421.11Slukem else 4431.46Slukem eval rc_flags=\$${name}_flags 4441.11Slukem fi 4451.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 4461.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 4471.50Satatat _group=\$${name}_group _groups=\$${name}_groups \ 4481.50Satatat _systrace=\$${name}_systrace 4491.11Slukem 4501.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 4511.42Slukem if [ "$_user" = "$(id -un)" ]; then 4521.42Slukem unset _user 4531.42Slukem fi 4541.42Slukem fi 4551.42Slukem 4561.29Slukem # if ${rcvar} is set, and $1 is not 4571.40Slukem # "rcvar", then run 4581.29Slukem # checkyesno ${rcvar} 4591.29Slukem # and return if that failed 4601.24Slukem # 4611.46Slukem if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 4621.24Slukem if ! checkyesno ${rcvar}; then 4631.24Slukem return 0 4641.24Slukem fi 4651.24Slukem fi 4661.24Slukem 4671.24Slukem eval $_pidcmd # determine the pid if necessary 4681.11Slukem 4691.11Slukem for _elem in $_keywords; do 4701.46Slukem if [ "$_elem" != "$rc_arg" ]; then 4711.11Slukem continue 4721.11Slukem fi 4731.11Slukem 4741.24Slukem # if there's a custom ${XXX_cmd}, 4751.24Slukem # run that instead of the default 4761.24Slukem # 4771.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 4781.46Slukem _postcmd=\$${rc_arg}_postcmd 4791.11Slukem if [ -n "$_cmd" ]; then 4801.24Slukem # if the precmd failed and force 4811.24Slukem # isn't set, exit 4821.24Slukem # 4831.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 4841.24Slukem return 1 4851.24Slukem fi 4861.24Slukem 4871.46Slukem if ! eval $_cmd && [ -z "$rc_force" ]; then 4881.44Slukem return 1 4891.44Slukem fi 4901.44Slukem eval $_postcmd 4911.11Slukem return 0 4921.11Slukem fi 4931.11Slukem 4941.46Slukem case "$rc_arg" in # default operations... 4951.11Slukem 4961.11Slukem status) 4971.46Slukem if [ -n "$rc_pid" ]; then 4981.46Slukem echo "${name} is running as pid $rc_pid." 4991.11Slukem else 5001.11Slukem echo "${name} is not running." 5011.28Slukem return 1 5021.11Slukem fi 5031.11Slukem ;; 5041.11Slukem 5051.11Slukem start) 5061.46Slukem if [ -n "$rc_pid" ]; then 5071.63Slukem echo 1>&2 "${name} already running? (pid=$rc_pid)." 5081.11Slukem exit 1 5091.11Slukem fi 5101.11Slukem 5111.57Slukem if [ ! -x ${_chroot}${command} ]; then 5121.11Slukem return 0 5131.11Slukem fi 5141.11Slukem 5151.24Slukem # check for required variables, 5161.24Slukem # directories, and files 5171.24Slukem # 5181.11Slukem for _f in $required_vars; do 5191.11Slukem if ! checkyesno $_f; then 5201.24Slukem warn "\$${_f} is not set." 5211.46Slukem if [ -z "$rc_force" ]; then 5221.24Slukem return 1 5231.24Slukem fi 5241.11Slukem fi 5251.11Slukem done 5261.11Slukem for _f in $required_dirs; do 5271.11Slukem if [ ! -d "${_f}/." ]; then 5281.25Slukem warn "${_f} is not a directory." 5291.46Slukem if [ -z "$rc_force" ]; then 5301.24Slukem return 1 5311.24Slukem fi 5321.11Slukem fi 5331.11Slukem done 5341.11Slukem for _f in $required_files; do 5351.11Slukem if [ ! -r "${_f}" ]; then 5361.25Slukem warn "${_f} is not readable." 5371.46Slukem if [ -z "$rc_force" ]; then 5381.24Slukem return 1 5391.24Slukem fi 5401.11Slukem fi 5411.11Slukem done 5421.11Slukem 5431.24Slukem # if the precmd failed and force 5441.24Slukem # isn't set, exit 5451.24Slukem # 5461.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5471.24Slukem return 1 5481.24Slukem fi 5491.24Slukem 5501.24Slukem # setup the command to run, and run it 5511.29Slukem # 5521.11Slukem echo "Starting ${name}." 5531.22Slukem if [ -n "$_chroot" ]; then 5541.22Slukem _doit="\ 5551.23Slukem${_nice:+nice -n $_nice }\ 5561.50Satatat${_systrace:+systrace $_systrace }\ 5571.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 5581.46Slukem$_chroot $command $rc_flags $command_args" 5591.22Slukem else 5601.22Slukem _doit="\ 5611.18Slukem${_chdir:+cd $_chdir; }\ 5621.18Slukem${_nice:+nice -n $_nice }\ 5631.50Satatat${_systrace:+systrace $_systrace }\ 5641.46Slukem$command $rc_flags $command_args" 5651.34Slukem if [ -n "$_user" ]; then 5661.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 5671.34Slukem fi 5681.22Slukem fi 5691.43Slukem 5701.43Slukem # if the cmd failed and force 5711.43Slukem # isn't set, exit 5721.43Slukem # 5731.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 5741.43Slukem return 1 5751.43Slukem fi 5761.43Slukem 5771.43Slukem # finally, run postcmd 5781.43Slukem # 5791.43Slukem eval $_postcmd 5801.11Slukem ;; 5811.11Slukem 5821.11Slukem stop) 5831.46Slukem if [ -z "$rc_pid" ]; then 5841.24Slukem if [ -n "$pidfile" ]; then 5851.63Slukem echo 1>&2 \ 5861.24Slukem "${name} not running? (check $pidfile)." 5871.24Slukem else 5881.63Slukem echo 1>&2 "${name} not running?" 5891.11Slukem fi 5901.24Slukem exit 1 5911.11Slukem fi 5921.11Slukem 5931.43Slukem # if the precmd failed and force 5941.43Slukem # isn't set, exit 5951.43Slukem # 5961.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5971.24Slukem return 1 5981.24Slukem fi 5991.43Slukem 6001.43Slukem # send the signal to stop 6011.43Slukem # 6021.11Slukem echo "Stopping ${name}." 6031.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 6041.34Slukem if [ -n "$_user" ]; then 6051.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6061.34Slukem fi 6071.43Slukem 6081.43Slukem # if the stop cmd failed and force 6091.43Slukem # isn't set, exit 6101.43Slukem # 6111.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6121.43Slukem return 1 6131.43Slukem fi 6141.43Slukem 6151.43Slukem # wait for the command to exit, 6161.43Slukem # and run postcmd. 6171.46Slukem wait_for_pids $rc_pid 6181.43Slukem eval $_postcmd 6191.11Slukem ;; 6201.11Slukem 6211.11Slukem reload) 6221.46Slukem if [ -z "$rc_pid" ]; then 6231.24Slukem if [ -n "$pidfile" ]; then 6241.63Slukem echo 1>&2 \ 6251.11Slukem "${name} not running? (check $pidfile)." 6261.24Slukem else 6271.63Slukem echo 1>&2 "${name} not running?" 6281.11Slukem fi 6291.24Slukem exit 1 6301.11Slukem fi 6311.11Slukem echo "Reloading ${name} config files." 6321.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6331.24Slukem return 1 6341.24Slukem fi 6351.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 6361.34Slukem if [ -n "$_user" ]; then 6371.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6381.34Slukem fi 6391.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6401.43Slukem return 1 6411.43Slukem fi 6421.43Slukem eval $_postcmd 6431.11Slukem ;; 6441.11Slukem 6451.11Slukem restart) 6461.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6471.24Slukem return 1 6481.11Slukem fi 6491.29Slukem # prevent restart being called more 6501.29Slukem # than once by any given script 6511.29Slukem # 6521.53Slukem if ${_rc_restart_done:-false}; then 6531.29Slukem return 0 6541.29Slukem fi 6551.53Slukem _rc_restart_done=true 6561.33Slukem 6571.61Slukem ( $0 ${_rc_prefix}stop ) 6581.61Slukem $0 ${_rc_prefix}start 6591.11Slukem 6601.43Slukem eval $_postcmd 6611.33Slukem ;; 6621.33Slukem 6631.33Slukem poll) 6641.46Slukem if [ -n "$rc_pid" ]; then 6651.46Slukem wait_for_pids $rc_pid 6661.33Slukem fi 6671.11Slukem ;; 6681.11Slukem 6691.11Slukem rcvar) 6701.11Slukem echo "# $name" 6711.24Slukem if [ -n "$rcvar" ]; then 6721.24Slukem if checkyesno ${rcvar}; then 6731.24Slukem echo "\$${rcvar}=YES" 6741.24Slukem else 6751.24Slukem echo "\$${rcvar}=NO" 6761.24Slukem fi 6771.11Slukem fi 6781.11Slukem ;; 6791.11Slukem 6801.11Slukem *) 6811.11Slukem rc_usage "$_keywords" 6821.11Slukem ;; 6831.11Slukem 6841.11Slukem esac 6851.11Slukem return 0 6861.11Slukem done 6871.11Slukem 6881.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 6891.11Slukem rc_usage "$_keywords" 6901.11Slukem exit 1 6911.11Slukem} 6921.11Slukem 6931.11Slukem# 6941.11Slukem# run_rc_script file arg 6951.11Slukem# Start the script `file' with `arg', and correctly handle the 6961.17Slukem# return value from the script. If `file' ends with `.sh', it's 6971.37Slukem# sourced into the current environment. If `file' appears to be 6981.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 6991.37Slukem# executable run as a child process. 7001.17Slukem# 7011.11Slukemrun_rc_script() 7021.11Slukem{ 7031.11Slukem _file=$1 7041.11Slukem _arg=$2 7051.11Slukem if [ -z "$_file" -o -z "$_arg" ]; then 7061.11Slukem err 3 'USAGE: run_rc_script file arg' 7071.11Slukem fi 7081.11Slukem 7091.45Slukem unset name command command_args command_interpreter \ 7101.45Slukem extra_commands pidfile procname \ 7111.42Slukem rcvar required_dirs required_files required_vars 7121.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 7131.39Slukem 7141.39Slukem case "$_file" in 7151.39Slukem *.sh) # run in current shell 7161.11Slukem set $_arg ; . $_file 7171.39Slukem ;; 7181.60Slukem *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 7191.39Slukem warn "Ignoring scratch file $_file" 7201.39Slukem ;; 7211.39Slukem *) # run in subshell 7221.39Slukem if [ -x $_file ]; then 7231.39Slukem if [ -n "$rc_fast_and_loose" ]; then 7241.39Slukem set $_arg ; . $_file 7251.39Slukem else 7261.37Slukem ( set $_arg ; . $_file ) 7271.37Slukem fi 7281.39Slukem fi 7291.39Slukem ;; 7301.39Slukem esac 7311.11Slukem} 7321.19Slukem 7331.19Slukem# 7341.19Slukem# load_rc_config 7351.19Slukem# Source in the configuration file for a given command. 7361.19Slukem# 7371.19Slukemload_rc_config() 7381.19Slukem{ 7391.19Slukem _command=$1 7401.19Slukem if [ -z "$_command" ]; then 7411.19Slukem err 3 'USAGE: load_rc_config command' 7421.19Slukem fi 7431.19Slukem 7441.54Slukem if ${_rc_conf_loaded:-false}; then 7451.54Slukem : 7461.54Slukem else 7471.30Slukem . /etc/rc.conf 7481.53Slukem _rc_conf_loaded=true 7491.30Slukem fi 7501.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 7511.20Sfvdl . /etc/rc.conf.d/"$_command" 7521.20Sfvdl fi 7531.19Slukem} 7541.19Slukem 7551.11Slukem 7561.11Slukem# 7571.11Slukem# rc_usage commands 7581.11Slukem# Print a usage string for $0, with `commands' being a list of 7591.11Slukem# valid commands. 7601.11Slukem# 7611.11Slukemrc_usage() 7621.11Slukem{ 7631.61Slukem echo -n 1>&2 "Usage: $0 [fast|force|one](" 7641.11Slukem 7651.11Slukem _sep= 7661.56Schristos for _elem; do 7671.11Slukem echo -n 1>&2 "$_sep$_elem" 7681.11Slukem _sep="|" 7691.11Slukem done 7701.11Slukem echo 1>&2 ")" 7711.11Slukem exit 1 7721.11Slukem} 7731.11Slukem 7741.11Slukem# 7751.11Slukem# err exitval message 7761.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 7771.11Slukem# 7781.11Slukemerr() 7791.11Slukem{ 7801.11Slukem exitval=$1 7811.11Slukem shift 7821.11Slukem 7831.51Sgrant if [ -x /usr/bin/logger ]; then 7841.51Sgrant logger "$0: ERROR: $*" 7851.51Sgrant fi 7861.21Slukem echo 1>&2 "$0: ERROR: $*" 7871.11Slukem exit $exitval 7881.11Slukem} 7891.11Slukem 7901.11Slukem# 7911.11Slukem# warn message 7921.11Slukem# Display message to stderr and log to the syslog. 7931.11Slukem# 7941.11Slukemwarn() 7951.11Slukem{ 7961.51Sgrant if [ -x /usr/bin/logger ]; then 7971.51Sgrant logger "$0: WARNING: $*" 7981.51Sgrant fi 7991.21Slukem echo 1>&2 "$0: WARNING: $*" 8001.31Satatat} 8011.31Satatat 8021.31Satatat# 8031.31Satatat# backup_file action file cur backup 8041.31Satatat# Make a backup copy of `file' into `cur', and save the previous 8051.31Satatat# version of `cur' as `backup' or use rcs for archiving. 8061.31Satatat# 8071.31Satatat# This routine checks the value of the backup_uses_rcs variable, 8081.31Satatat# which can be either YES or NO. 8091.31Satatat# 8101.31Satatat# The `action' keyword can be one of the following: 8111.31Satatat# 8121.31Satatat# add `file' is now being backed up (and is possibly 8131.31Satatat# being reentered into the backups system). `cur' 8141.31Satatat# is created and RCS files, if necessary, are 8151.31Satatat# created as well. 8161.31Satatat# 8171.31Satatat# update `file' has changed and needs to be backed up. 8181.31Satatat# If `cur' exists, it is copied to to `back' or 8191.31Satatat# checked into RCS (if the repository file is old), 8201.31Satatat# and then `file' is copied to `cur'. Another RCS 8211.31Satatat# check in done here if RCS is being used. 8221.31Satatat# 8231.31Satatat# remove `file' is no longer being tracked by the backups 8241.31Satatat# system. If RCS is not being used, `cur' is moved 8251.31Satatat# to `back', otherwise an empty file is checked in, 8261.31Satatat# and then `cur' is removed. 8271.31Satatat# 8281.31Satatat# 8291.31Satatatbackup_file() 8301.31Satatat{ 8311.31Satatat _action=$1 8321.31Satatat _file=$2 8331.31Satatat _cur=$3 8341.31Satatat _back=$4 8351.31Satatat 8361.31Satatat if checkyesno backup_uses_rcs; then 8371.31Satatat _msg0="backup archive" 8381.31Satatat _msg1="update" 8391.31Satatat 8401.36Satatat # ensure that history file is not locked 8411.36Satatat if [ -f $_cur,v ]; then 8421.36Satatat rcs -q -u -U -M $_cur 8431.36Satatat fi 8441.36Satatat 8451.31Satatat # ensure after switching to rcs that the 8461.31Satatat # current backup is not lost 8471.31Satatat if [ -f $_cur ]; then 8481.31Satatat # no archive, or current newer than archive 8491.31Satatat if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 8501.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8511.36Satatat rcs -q -kb -U $_cur 8521.49Slukem co -q -f -u $_cur 8531.31Satatat fi 8541.31Satatat fi 8551.31Satatat 8561.31Satatat case $_action in 8571.31Satatat add|update) 8581.31Satatat cp -p $_file $_cur 8591.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8601.36Satatat rcs -q -kb -U $_cur 8611.49Slukem co -q -f -u $_cur 8621.31Satatat chown root:wheel $_cur $_cur,v 8631.31Satatat ;; 8641.31Satatat remove) 8651.31Satatat cp /dev/null $_cur 8661.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8671.36Satatat rcs -q -kb -U $_cur 8681.31Satatat chown root:wheel $_cur $_cur,v 8691.31Satatat rm $_cur 8701.31Satatat ;; 8711.31Satatat esac 8721.31Satatat else 8731.31Satatat case $_action in 8741.31Satatat add|update) 8751.31Satatat if [ -f $_cur ]; then 8761.31Satatat cp -p $_cur $_back 8771.31Satatat fi 8781.31Satatat cp -p $_file $_cur 8791.31Satatat chown root:wheel $_cur 8801.31Satatat ;; 8811.31Satatat remove) 8821.31Satatat mv -f $_cur $_back 8831.31Satatat ;; 8841.31Satatat esac 8851.31Satatat fi 8861.1Scjs} 887