rc.subr revision 1.67
11.67Selad# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 21.11Slukem# 31.65Slukem# Copyright (c) 1997-2004 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.65Slukem# 741.38Slukem# reverse_list list 751.38Slukem# print the list in reverse order 761.38Slukem# 771.38Slukemreverse_list() 781.38Slukem{ 791.38Slukem _revlist= 801.56Schristos for _revfile; do 811.38Slukem _revlist="$_revfile $_revlist" 821.38Slukem done 831.38Slukem echo $_revlist 841.6Smellon} 851.6Smellon 861.6Smellon# 871.47Slukem# mount_critical_filesystems type 881.47Slukem# Go through the list of critical filesystems as provided in 891.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 901.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 911.6Smellon# 921.11Slukemmount_critical_filesystems() 931.11Slukem{ 941.47Slukem eval _fslist=\$critical_filesystems_${1} 951.17Slukem for _fs in $_fslist; do 961.6Smellon mount | ( 971.53Slukem _ismounted=false 981.6Smellon while read what _on on _type type; do 991.17Slukem if [ $on = $_fs ]; then 1001.53Slukem _ismounted=true 1011.6Smellon fi 1021.6Smellon done 1031.53Slukem if $_ismounted; then 1041.55Slukem : 1051.55Slukem else 1061.17Slukem mount $_fs >/dev/null 2>&1 1071.6Smellon fi 1081.46Slukem ) 1091.6Smellon done 1101.7Scjs} 1111.7Scjs 1121.11Slukem# 1131.45Slukem# check_pidfile pidfile procname [interpreter] 1141.45Slukem# Parses the first line of pidfile for a PID, and ensures 1151.11Slukem# that the process is running and matches procname. 1161.45Slukem# Prints the matching PID upon success, nothing otherwise. 1171.45Slukem# interpreter is optional; see _find_processes() for details. 1181.11Slukem# 1191.11Slukemcheck_pidfile() 1201.11Slukem{ 1211.11Slukem _pidfile=$1 1221.11Slukem _procname=$2 1231.45Slukem _interpreter=$3 1241.11Slukem if [ -z "$_pidfile" -o -z "$_procname" ]; then 1251.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 1261.11Slukem fi 1271.11Slukem if [ ! -f $_pidfile ]; then 1281.11Slukem return 1291.11Slukem fi 1301.11Slukem read _pid _junk < $_pidfile 1311.11Slukem if [ -z "$_pid" ]; then 1321.11Slukem return 1331.11Slukem fi 1341.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 1351.11Slukem} 1361.11Slukem 1371.11Slukem# 1381.45Slukem# check_process procname [interpreter] 1391.11Slukem# Ensures that a process (or processes) named procname is running. 1401.45Slukem# Prints a list of matching PIDs. 1411.45Slukem# interpreter is optional; see _find_processes() for details. 1421.11Slukem# 1431.11Slukemcheck_process() 1441.11Slukem{ 1451.11Slukem _procname=$1 1461.45Slukem _interpreter=$2 1471.11Slukem if [ -z "$_procname" ]; then 1481.45Slukem err 3 'USAGE: check_process procname [interpreter]' 1491.45Slukem fi 1501.45Slukem _find_processes $_procname ${_interpreter:-.} '-ax' 1511.45Slukem} 1521.45Slukem 1531.46Slukem# 1541.45Slukem# _find_processes procname interpreter psargs 1551.45Slukem# Search for procname in the output of ps generated by psargs. 1561.45Slukem# Prints the PIDs of any matching processes, space separated. 1571.45Slukem# 1581.45Slukem# If interpreter == ".", check the following variations of procname 1591.45Slukem# against the first word of each command: 1601.45Slukem# procname 1611.45Slukem# `basename procname` 1621.45Slukem# `basename procname` + ":" 1631.45Slukem# "(" + `basename procname` + ")" 1641.45Slukem# 1651.45Slukem# If interpreter != ".", read the first line of procname, remove the 1661.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 1671.45Slukem# match that against each command, either as is, or with extra words 1681.66She# at the end. As an alternative, to deal with interpreted deaemons 1691.66She# using perl, the basename of the interpreter plus a colon is also 1701.66She# tried as the prefix to procname. 1711.45Slukem# 1721.45Slukem_find_processes() 1731.45Slukem{ 1741.45Slukem if [ $# -ne 3 ]; then 1751.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 1761.11Slukem fi 1771.45Slukem _procname=$1 1781.45Slukem _interpreter=$2 1791.45Slukem _psargs=$3 1801.45Slukem 1811.11Slukem _pref= 1821.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 1831.67Selad read _interp < ${_chroot:-}/$_procname # read interpreter name 1841.45Slukem _interp=${_interp#\#!} # strip #! 1851.45Slukem set -- $_interp 1861.45Slukem if [ $_interpreter != $1 ]; then 1871.45Slukem warn "\$command_interpreter $_interpreter != $1" 1881.45Slukem fi 1891.45Slukem _interp="$* $_procname" # cleanup spaces, add _procname 1901.66She _interpbn=${1##*/} 1911.45Slukem _fp_args='_argv' 1921.45Slukem _fp_match='case "$_argv" in 1931.66She ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)' 1941.45Slukem else # a normal daemon 1951.45Slukem _procnamebn=${_procname##*/} 1961.45Slukem _fp_args='_arg0 _argv' 1971.45Slukem _fp_match='case "$_arg0" in 1981.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 1991.45Slukem fi 2001.45Slukem 2011.45Slukem _proccheck=' 2021.46Slukem ps -o "pid,command" '"$_psargs"' | 2031.45Slukem while read _npid '"$_fp_args"'; do 2041.45Slukem case "$_npid" in 2051.45Slukem PID) 2061.45Slukem continue ;; 2071.45Slukem esac ; '"$_fp_match"' 2081.45Slukem echo -n "$_pref$_npid" ; 2091.45Slukem _pref=" " 2101.45Slukem ;; 2111.45Slukem esac 2121.45Slukem done' 2131.45Slukem 2141.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 2151.45Slukem eval $_proccheck 2161.11Slukem} 2171.11Slukem 2181.11Slukem# 2191.33Slukem# wait_for_pids pid [pid ...] 2201.35Slukem# spins until none of the pids exist 2211.33Slukem# 2221.33Slukemwait_for_pids() 2231.33Slukem{ 2241.56Schristos _list="$@" 2251.33Slukem if [ -z "$_list" ]; then 2261.33Slukem return 2271.33Slukem fi 2281.35Slukem _prefix= 2291.35Slukem while true; do 2301.33Slukem _nlist=""; 2311.33Slukem for _j in $_list; do 2321.33Slukem if kill -0 $_j 2>/dev/null; then 2331.33Slukem _nlist="${_nlist}${_nlist:+ }$_j" 2341.33Slukem fi 2351.33Slukem done 2361.33Slukem if [ -z "$_nlist" ]; then 2371.33Slukem break 2381.33Slukem fi 2391.33Slukem _list=$_nlist 2401.35Slukem echo -n ${_prefix:-"Waiting for PIDS: "}$_list 2411.35Slukem _prefix=", " 2421.35Slukem sleep 2 2431.33Slukem done 2441.35Slukem if [ -n "$_prefix" ]; then 2451.35Slukem echo "." 2461.35Slukem fi 2471.33Slukem} 2481.33Slukem 2491.33Slukem# 2501.46Slukem# run_rc_command argument 2511.46Slukem# Search for argument in the list of supported commands, which is: 2521.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 2531.46Slukem# If there's a match, run ${argument}_cmd or the default method 2541.46Slukem# (see below). 2551.11Slukem# 2561.46Slukem# If argument has a given prefix, then change the operation as follows: 2571.46Slukem# Prefix Operation 2581.11Slukem# ------ --------- 2591.48Slukem# fast Skip the pid check, and set rc_fast=yes 2601.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 2611.61Slukem# one Set ${rcvar} to YES 2621.11Slukem# 2631.11Slukem# The following globals are used: 2641.24Slukem# 2651.46Slukem# Name Needed Purpose 2661.46Slukem# ---- ------ ------- 2671.11Slukem# name y Name of script. 2681.24Slukem# 2691.11Slukem# command n Full path to command. 2701.46Slukem# Not needed if ${rc_arg}_cmd is set for 2711.11Slukem# each keyword. 2721.24Slukem# 2731.11Slukem# command_args n Optional args/shell directives for command. 2741.24Slukem# 2751.45Slukem# command_interpreter n If not empty, command is interpreted, so 2761.45Slukem# call check_{pidfile,process}() appropriately. 2771.45Slukem# 2781.16Slukem# extra_commands n List of extra commands supported. 2791.24Slukem# 2801.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 2811.42Slukem# otherwise use check_process $command. 2821.42Slukem# In either case, only check if $command is set. 2831.42Slukem# 2841.42Slukem# procname n Process name to check for instead of $command. 2851.24Slukem# 2861.24Slukem# rcvar n This is checked with checkyesno to determine 2871.24Slukem# if the action should be run. 2881.24Slukem# 2891.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 2901.42Slukem# Requires /usr to be mounted. 2911.24Slukem# 2921.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 2931.22Slukem# (if not using ${name}_chroot). 2941.24Slukem# 2951.11Slukem# ${name}_flags n Arguments to call ${command} with. 2961.24Slukem# NOTE: $flags from the parent environment 2971.24Slukem# can be used to override this. 2981.24Slukem# 2991.23Slukem# ${name}_nice n Nice level to run ${command} at. 3001.24Slukem# 3011.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 3021.22Slukem# using ${name}_chroot. 3031.42Slukem# Requires /usr to be mounted. 3041.24Slukem# 3051.22Slukem# ${name}_group n Group to run chrooted ${command} as. 3061.42Slukem# Requires /usr to be mounted. 3071.24Slukem# 3081.32Slukem# ${name}_groups n Comma separated list of supplementary groups 3091.32Slukem# to run the chrooted ${command} with. 3101.42Slukem# Requires /usr to be mounted. 3111.24Slukem# 3121.50Satatat# ${name}_systrace n Flags passed to systrace(1) if it is used. 3131.50Satatat# Setting this variable enables systracing 3141.50Satatat# of the given program. The use of "-a" is 3151.50Satatat# recommended so that the boot process is not 3161.50Satatat# stalled. In order to pass no flags to 3171.50Satatat# systrace, set this variable to "--". 3181.50Satatat# 3191.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 3201.11Slukem# Otherwise, use default command (see below) 3211.24Slukem# 3221.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 3231.46Slukem# ${rc_arg}_cmd method in the default 3241.46Slukem# operation (i.e, after checking for required 3251.46Slukem# bits and process (non)existence). 3261.11Slukem# If this completes with a non-zero exit code, 3271.46Slukem# don't run ${rc_arg}_cmd. 3281.24Slukem# 3291.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 3301.46Slukem# ${rc_arg}_cmd method, if that method 3311.43Slukem# returned a zero exit code. 3321.43Slukem# 3331.11Slukem# required_dirs n If set, check for the existence of the given 3341.11Slukem# directories before running the default 3351.11Slukem# (re)start command. 3361.24Slukem# 3371.11Slukem# required_files n If set, check for the readability of the given 3381.11Slukem# files before running the default (re)start 3391.11Slukem# command. 3401.24Slukem# 3411.11Slukem# required_vars n If set, perform checkyesno on each of the 3421.11Slukem# listed variables before running the default 3431.11Slukem# (re)start command. 3441.11Slukem# 3451.46Slukem# Default behaviour for a given argument, if no override method is 3461.46Slukem# provided: 3471.24Slukem# 3481.46Slukem# Argument Default behaviour 3491.46Slukem# -------- ----------------- 3501.11Slukem# start if !running && checkyesno ${rcvar} 3511.11Slukem# ${command} 3521.24Slukem# 3531.11Slukem# stop if ${pidfile} 3541.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 3551.11Slukem# else 3561.46Slukem# rc_pid=$(check_process $command) 3571.46Slukem# kill $sig_stop $rc_pid 3581.46Slukem# wait_for_pids $rc_pid 3591.35Slukem# ($sig_stop defaults to TERM.) 3601.24Slukem# 3611.35Slukem# reload Similar to stop, except use $sig_reload instead, 3621.35Slukem# and doesn't wait_for_pids. 3631.11Slukem# $sig_reload defaults to HUP. 3641.24Slukem# 3651.11Slukem# restart Run `stop' then `start'. 3661.11Slukem# 3671.33Slukem# status Show if ${command} is running, etc. 3681.33Slukem# 3691.33Slukem# poll Wait for ${command} to exit. 3701.33Slukem# 3711.33Slukem# rcvar Display what rc.conf variable is used (if any). 3721.33Slukem# 3731.46Slukem# Variables available to methods, and after run_rc_command() has 3741.46Slukem# completed: 3751.46Slukem# 3761.46Slukem# Variable Purpose 3771.46Slukem# -------- ------- 3781.61Slukem# rc_arg Argument to command, after fast/force/one processing 3791.46Slukem# performed 3801.46Slukem# 3811.46Slukem# rc_flags Flags to start the default command with. 3821.46Slukem# Defaults to ${name}_flags, unless overridden 3831.46Slukem# by $flags from the environment. 3841.46Slukem# This variable may be changed by the precmd method. 3851.46Slukem# 3861.46Slukem# rc_pid PID of command (if appropriate) 3871.46Slukem# 3881.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 3891.46Slukem# 3901.46Slukem# rc_force Not empty if "force" was provided (q.v.) 3911.33Slukem# 3921.24Slukem# 3931.11Slukemrun_rc_command() 3941.11Slukem{ 3951.46Slukem rc_arg=$1 3961.24Slukem if [ -z "$name" ]; then 3971.30Slukem err 3 'run_rc_command: $name is not set.' 3981.11Slukem fi 3991.11Slukem 4001.61Slukem _rc_prefix= 4011.46Slukem case "$rc_arg" in 4021.24Slukem fast*) # "fast" prefix; don't check pid 4031.46Slukem rc_arg=${rc_arg#fast} 4041.48Slukem rc_fast=yes 4051.11Slukem ;; 4061.61Slukem force*) # "force" prefix; always run 4071.48Slukem rc_force=yes 4081.61Slukem _rc_prefix=force 4091.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4101.61Slukem if [ -n "${rcvar}" ]; then 4111.61Slukem eval ${rcvar}=YES 4121.61Slukem fi 4131.61Slukem ;; 4141.61Slukem one*) # "one" prefix; set ${rcvar}=yes 4151.61Slukem _rc_prefix=one 4161.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4171.29Slukem if [ -n "${rcvar}" ]; then 4181.29Slukem eval ${rcvar}=YES 4191.29Slukem fi 4201.11Slukem ;; 4211.11Slukem esac 4221.11Slukem 4231.16Slukem _keywords="start stop restart rcvar $extra_commands" 4241.46Slukem rc_pid= 4251.11Slukem _pidcmd= 4261.42Slukem _procname=${procname:-${command}} 4271.42Slukem 4281.24Slukem # setup pid check command if not fast 4291.46Slukem if [ -z "$rc_fast" -a -n "$_procname" ]; then 4301.11Slukem if [ -n "$pidfile" ]; then 4311.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 4321.42Slukem else 4331.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 4341.11Slukem fi 4351.11Slukem if [ -n "$_pidcmd" ]; then 4361.33Slukem _keywords="${_keywords} status poll" 4371.11Slukem fi 4381.11Slukem fi 4391.11Slukem 4401.46Slukem if [ -z "$rc_arg" ]; then 4411.11Slukem rc_usage "$_keywords" 4421.11Slukem fi 4431.11Slukem 4441.17Slukem if [ -n "$flags" ]; then # allow override from environment 4451.46Slukem rc_flags=$flags 4461.11Slukem else 4471.46Slukem eval rc_flags=\$${name}_flags 4481.11Slukem fi 4491.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 4501.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 4511.50Satatat _group=\$${name}_group _groups=\$${name}_groups \ 4521.50Satatat _systrace=\$${name}_systrace 4531.11Slukem 4541.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 4551.42Slukem if [ "$_user" = "$(id -un)" ]; then 4561.42Slukem unset _user 4571.42Slukem fi 4581.42Slukem fi 4591.42Slukem 4601.29Slukem # if ${rcvar} is set, and $1 is not 4611.40Slukem # "rcvar", then run 4621.29Slukem # checkyesno ${rcvar} 4631.29Slukem # and return if that failed 4641.24Slukem # 4651.46Slukem if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 4661.24Slukem if ! checkyesno ${rcvar}; then 4671.24Slukem return 0 4681.24Slukem fi 4691.24Slukem fi 4701.24Slukem 4711.24Slukem eval $_pidcmd # determine the pid if necessary 4721.11Slukem 4731.11Slukem for _elem in $_keywords; do 4741.46Slukem if [ "$_elem" != "$rc_arg" ]; then 4751.11Slukem continue 4761.11Slukem fi 4771.11Slukem 4781.24Slukem # if there's a custom ${XXX_cmd}, 4791.24Slukem # run that instead of the default 4801.24Slukem # 4811.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 4821.46Slukem _postcmd=\$${rc_arg}_postcmd 4831.11Slukem if [ -n "$_cmd" ]; then 4841.24Slukem # if the precmd failed and force 4851.24Slukem # isn't set, exit 4861.24Slukem # 4871.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 4881.24Slukem return 1 4891.24Slukem fi 4901.24Slukem 4911.46Slukem if ! eval $_cmd && [ -z "$rc_force" ]; then 4921.44Slukem return 1 4931.44Slukem fi 4941.44Slukem eval $_postcmd 4951.11Slukem return 0 4961.11Slukem fi 4971.11Slukem 4981.46Slukem case "$rc_arg" in # default operations... 4991.11Slukem 5001.11Slukem status) 5011.46Slukem if [ -n "$rc_pid" ]; then 5021.46Slukem echo "${name} is running as pid $rc_pid." 5031.11Slukem else 5041.11Slukem echo "${name} is not running." 5051.28Slukem return 1 5061.11Slukem fi 5071.11Slukem ;; 5081.11Slukem 5091.11Slukem start) 5101.46Slukem if [ -n "$rc_pid" ]; then 5111.63Slukem echo 1>&2 "${name} already running? (pid=$rc_pid)." 5121.11Slukem exit 1 5131.11Slukem fi 5141.11Slukem 5151.57Slukem if [ ! -x ${_chroot}${command} ]; then 5161.11Slukem return 0 5171.11Slukem fi 5181.11Slukem 5191.24Slukem # check for required variables, 5201.24Slukem # directories, and files 5211.24Slukem # 5221.11Slukem for _f in $required_vars; do 5231.11Slukem if ! checkyesno $_f; then 5241.65Slukem warn "\$${_f} is not enabled." 5251.46Slukem if [ -z "$rc_force" ]; then 5261.24Slukem return 1 5271.24Slukem fi 5281.11Slukem fi 5291.11Slukem done 5301.11Slukem for _f in $required_dirs; do 5311.11Slukem if [ ! -d "${_f}/." ]; then 5321.25Slukem warn "${_f} is not a directory." 5331.46Slukem if [ -z "$rc_force" ]; then 5341.24Slukem return 1 5351.24Slukem fi 5361.11Slukem fi 5371.11Slukem done 5381.11Slukem for _f in $required_files; do 5391.11Slukem if [ ! -r "${_f}" ]; then 5401.25Slukem warn "${_f} is not readable." 5411.46Slukem if [ -z "$rc_force" ]; then 5421.24Slukem return 1 5431.24Slukem fi 5441.11Slukem fi 5451.11Slukem done 5461.11Slukem 5471.24Slukem # if the precmd failed and force 5481.24Slukem # isn't set, exit 5491.24Slukem # 5501.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5511.24Slukem return 1 5521.24Slukem fi 5531.24Slukem 5541.24Slukem # setup the command to run, and run it 5551.29Slukem # 5561.11Slukem echo "Starting ${name}." 5571.22Slukem if [ -n "$_chroot" ]; then 5581.22Slukem _doit="\ 5591.23Slukem${_nice:+nice -n $_nice }\ 5601.50Satatat${_systrace:+systrace $_systrace }\ 5611.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 5621.46Slukem$_chroot $command $rc_flags $command_args" 5631.22Slukem else 5641.22Slukem _doit="\ 5651.18Slukem${_chdir:+cd $_chdir; }\ 5661.18Slukem${_nice:+nice -n $_nice }\ 5671.50Satatat${_systrace:+systrace $_systrace }\ 5681.46Slukem$command $rc_flags $command_args" 5691.34Slukem if [ -n "$_user" ]; then 5701.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 5711.34Slukem fi 5721.22Slukem fi 5731.43Slukem 5741.43Slukem # if the cmd failed and force 5751.43Slukem # isn't set, exit 5761.43Slukem # 5771.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 5781.43Slukem return 1 5791.43Slukem fi 5801.43Slukem 5811.43Slukem # finally, run postcmd 5821.43Slukem # 5831.43Slukem eval $_postcmd 5841.11Slukem ;; 5851.11Slukem 5861.11Slukem stop) 5871.46Slukem if [ -z "$rc_pid" ]; then 5881.24Slukem if [ -n "$pidfile" ]; then 5891.63Slukem echo 1>&2 \ 5901.24Slukem "${name} not running? (check $pidfile)." 5911.24Slukem else 5921.63Slukem echo 1>&2 "${name} not running?" 5931.11Slukem fi 5941.24Slukem exit 1 5951.11Slukem fi 5961.11Slukem 5971.43Slukem # if the precmd failed and force 5981.43Slukem # isn't set, exit 5991.43Slukem # 6001.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6011.24Slukem return 1 6021.24Slukem fi 6031.43Slukem 6041.43Slukem # send the signal to stop 6051.43Slukem # 6061.11Slukem echo "Stopping ${name}." 6071.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 6081.34Slukem if [ -n "$_user" ]; then 6091.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6101.34Slukem fi 6111.43Slukem 6121.43Slukem # if the stop cmd failed and force 6131.43Slukem # isn't set, exit 6141.43Slukem # 6151.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6161.43Slukem return 1 6171.43Slukem fi 6181.43Slukem 6191.43Slukem # wait for the command to exit, 6201.43Slukem # and run postcmd. 6211.46Slukem wait_for_pids $rc_pid 6221.43Slukem eval $_postcmd 6231.11Slukem ;; 6241.11Slukem 6251.11Slukem reload) 6261.46Slukem if [ -z "$rc_pid" ]; then 6271.24Slukem if [ -n "$pidfile" ]; then 6281.63Slukem echo 1>&2 \ 6291.11Slukem "${name} not running? (check $pidfile)." 6301.24Slukem else 6311.63Slukem echo 1>&2 "${name} not running?" 6321.11Slukem fi 6331.24Slukem exit 1 6341.11Slukem fi 6351.11Slukem echo "Reloading ${name} config files." 6361.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6371.24Slukem return 1 6381.24Slukem fi 6391.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 6401.34Slukem if [ -n "$_user" ]; then 6411.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6421.34Slukem fi 6431.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6441.43Slukem return 1 6451.43Slukem fi 6461.43Slukem eval $_postcmd 6471.11Slukem ;; 6481.11Slukem 6491.11Slukem restart) 6501.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6511.24Slukem return 1 6521.11Slukem fi 6531.29Slukem # prevent restart being called more 6541.29Slukem # than once by any given script 6551.29Slukem # 6561.53Slukem if ${_rc_restart_done:-false}; then 6571.29Slukem return 0 6581.29Slukem fi 6591.53Slukem _rc_restart_done=true 6601.33Slukem 6611.61Slukem ( $0 ${_rc_prefix}stop ) 6621.61Slukem $0 ${_rc_prefix}start 6631.11Slukem 6641.43Slukem eval $_postcmd 6651.33Slukem ;; 6661.33Slukem 6671.33Slukem poll) 6681.46Slukem if [ -n "$rc_pid" ]; then 6691.46Slukem wait_for_pids $rc_pid 6701.33Slukem fi 6711.11Slukem ;; 6721.11Slukem 6731.11Slukem rcvar) 6741.11Slukem echo "# $name" 6751.24Slukem if [ -n "$rcvar" ]; then 6761.24Slukem if checkyesno ${rcvar}; then 6771.24Slukem echo "\$${rcvar}=YES" 6781.24Slukem else 6791.24Slukem echo "\$${rcvar}=NO" 6801.24Slukem fi 6811.11Slukem fi 6821.11Slukem ;; 6831.11Slukem 6841.11Slukem *) 6851.11Slukem rc_usage "$_keywords" 6861.11Slukem ;; 6871.11Slukem 6881.11Slukem esac 6891.11Slukem return 0 6901.11Slukem done 6911.11Slukem 6921.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 6931.11Slukem rc_usage "$_keywords" 6941.11Slukem exit 1 6951.11Slukem} 6961.11Slukem 6971.11Slukem# 6981.11Slukem# run_rc_script file arg 6991.11Slukem# Start the script `file' with `arg', and correctly handle the 7001.17Slukem# return value from the script. If `file' ends with `.sh', it's 7011.37Slukem# sourced into the current environment. If `file' appears to be 7021.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 7031.37Slukem# executable run as a child process. 7041.17Slukem# 7051.11Slukemrun_rc_script() 7061.11Slukem{ 7071.11Slukem _file=$1 7081.11Slukem _arg=$2 7091.11Slukem if [ -z "$_file" -o -z "$_arg" ]; then 7101.11Slukem err 3 'USAGE: run_rc_script file arg' 7111.11Slukem fi 7121.11Slukem 7131.45Slukem unset name command command_args command_interpreter \ 7141.45Slukem extra_commands pidfile procname \ 7151.42Slukem rcvar required_dirs required_files required_vars 7161.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 7171.39Slukem 7181.39Slukem case "$_file" in 7191.39Slukem *.sh) # run in current shell 7201.11Slukem set $_arg ; . $_file 7211.39Slukem ;; 7221.60Slukem *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 7231.39Slukem warn "Ignoring scratch file $_file" 7241.39Slukem ;; 7251.39Slukem *) # run in subshell 7261.39Slukem if [ -x $_file ]; then 7271.39Slukem if [ -n "$rc_fast_and_loose" ]; then 7281.39Slukem set $_arg ; . $_file 7291.39Slukem else 7301.37Slukem ( set $_arg ; . $_file ) 7311.37Slukem fi 7321.39Slukem fi 7331.39Slukem ;; 7341.39Slukem esac 7351.11Slukem} 7361.19Slukem 7371.19Slukem# 7381.65Slukem# load_rc_config command 7391.19Slukem# Source in the configuration file for a given command. 7401.19Slukem# 7411.19Slukemload_rc_config() 7421.19Slukem{ 7431.19Slukem _command=$1 7441.19Slukem if [ -z "$_command" ]; then 7451.19Slukem err 3 'USAGE: load_rc_config command' 7461.19Slukem fi 7471.19Slukem 7481.54Slukem if ${_rc_conf_loaded:-false}; then 7491.54Slukem : 7501.54Slukem else 7511.30Slukem . /etc/rc.conf 7521.53Slukem _rc_conf_loaded=true 7531.30Slukem fi 7541.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 7551.20Sfvdl . /etc/rc.conf.d/"$_command" 7561.20Sfvdl fi 7571.19Slukem} 7581.19Slukem 7591.65Slukem# 7601.65Slukem# load_rc_config_var cmd var 7611.65Slukem# Read the rc.conf(5) var for cmd and set in the 7621.65Slukem# current shell, using load_rc_config in a subshell to prevent 7631.65Slukem# unwanted side effects from other variable assignments. 7641.65Slukem# 7651.65Slukemload_rc_config_var() 7661.65Slukem{ 7671.65Slukem if [ $# -ne 2 ]; then 7681.65Slukem err 3 'USAGE: load_rc_config_var cmd var' 7691.65Slukem fi 7701.65Slukem eval $(eval '( 7711.65Slukem load_rc_config '$1' >/dev/null; 7721.65Slukem if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 7731.65Slukem echo '$2'=\'\''${'$2'}\'\''; 7741.65Slukem fi 7751.65Slukem )' ) 7761.65Slukem} 7771.11Slukem 7781.11Slukem# 7791.11Slukem# rc_usage commands 7801.11Slukem# Print a usage string for $0, with `commands' being a list of 7811.11Slukem# valid commands. 7821.11Slukem# 7831.11Slukemrc_usage() 7841.11Slukem{ 7851.61Slukem echo -n 1>&2 "Usage: $0 [fast|force|one](" 7861.11Slukem 7871.11Slukem _sep= 7881.56Schristos for _elem; do 7891.11Slukem echo -n 1>&2 "$_sep$_elem" 7901.11Slukem _sep="|" 7911.11Slukem done 7921.11Slukem echo 1>&2 ")" 7931.11Slukem exit 1 7941.11Slukem} 7951.11Slukem 7961.11Slukem# 7971.11Slukem# err exitval message 7981.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 7991.11Slukem# 8001.11Slukemerr() 8011.11Slukem{ 8021.11Slukem exitval=$1 8031.11Slukem shift 8041.11Slukem 8051.51Sgrant if [ -x /usr/bin/logger ]; then 8061.51Sgrant logger "$0: ERROR: $*" 8071.51Sgrant fi 8081.21Slukem echo 1>&2 "$0: ERROR: $*" 8091.11Slukem exit $exitval 8101.11Slukem} 8111.11Slukem 8121.11Slukem# 8131.11Slukem# warn message 8141.11Slukem# Display message to stderr and log to the syslog. 8151.11Slukem# 8161.11Slukemwarn() 8171.11Slukem{ 8181.51Sgrant if [ -x /usr/bin/logger ]; then 8191.51Sgrant logger "$0: WARNING: $*" 8201.51Sgrant fi 8211.21Slukem echo 1>&2 "$0: WARNING: $*" 8221.31Satatat} 8231.31Satatat 8241.31Satatat# 8251.31Satatat# backup_file action file cur backup 8261.31Satatat# Make a backup copy of `file' into `cur', and save the previous 8271.31Satatat# version of `cur' as `backup' or use rcs for archiving. 8281.31Satatat# 8291.31Satatat# This routine checks the value of the backup_uses_rcs variable, 8301.31Satatat# which can be either YES or NO. 8311.31Satatat# 8321.31Satatat# The `action' keyword can be one of the following: 8331.31Satatat# 8341.31Satatat# add `file' is now being backed up (and is possibly 8351.31Satatat# being reentered into the backups system). `cur' 8361.31Satatat# is created and RCS files, if necessary, are 8371.31Satatat# created as well. 8381.31Satatat# 8391.31Satatat# update `file' has changed and needs to be backed up. 8401.31Satatat# If `cur' exists, it is copied to to `back' or 8411.31Satatat# checked into RCS (if the repository file is old), 8421.31Satatat# and then `file' is copied to `cur'. Another RCS 8431.31Satatat# check in done here if RCS is being used. 8441.31Satatat# 8451.31Satatat# remove `file' is no longer being tracked by the backups 8461.31Satatat# system. If RCS is not being used, `cur' is moved 8471.31Satatat# to `back', otherwise an empty file is checked in, 8481.31Satatat# and then `cur' is removed. 8491.31Satatat# 8501.31Satatat# 8511.31Satatatbackup_file() 8521.31Satatat{ 8531.31Satatat _action=$1 8541.31Satatat _file=$2 8551.31Satatat _cur=$3 8561.31Satatat _back=$4 8571.31Satatat 8581.31Satatat if checkyesno backup_uses_rcs; then 8591.31Satatat _msg0="backup archive" 8601.31Satatat _msg1="update" 8611.31Satatat 8621.36Satatat # ensure that history file is not locked 8631.36Satatat if [ -f $_cur,v ]; then 8641.36Satatat rcs -q -u -U -M $_cur 8651.36Satatat fi 8661.36Satatat 8671.31Satatat # ensure after switching to rcs that the 8681.31Satatat # current backup is not lost 8691.31Satatat if [ -f $_cur ]; then 8701.31Satatat # no archive, or current newer than archive 8711.31Satatat if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 8721.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8731.36Satatat rcs -q -kb -U $_cur 8741.49Slukem co -q -f -u $_cur 8751.31Satatat fi 8761.31Satatat fi 8771.31Satatat 8781.31Satatat case $_action in 8791.31Satatat add|update) 8801.31Satatat cp -p $_file $_cur 8811.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8821.36Satatat rcs -q -kb -U $_cur 8831.49Slukem co -q -f -u $_cur 8841.31Satatat chown root:wheel $_cur $_cur,v 8851.31Satatat ;; 8861.31Satatat remove) 8871.31Satatat cp /dev/null $_cur 8881.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8891.36Satatat rcs -q -kb -U $_cur 8901.31Satatat chown root:wheel $_cur $_cur,v 8911.31Satatat rm $_cur 8921.31Satatat ;; 8931.31Satatat esac 8941.31Satatat else 8951.31Satatat case $_action in 8961.31Satatat add|update) 8971.31Satatat if [ -f $_cur ]; then 8981.31Satatat cp -p $_cur $_back 8991.31Satatat fi 9001.31Satatat cp -p $_file $_cur 9011.31Satatat chown root:wheel $_cur 9021.31Satatat ;; 9031.31Satatat remove) 9041.31Satatat mv -f $_cur $_back 9051.31Satatat ;; 9061.31Satatat esac 9071.31Satatat fi 9081.1Scjs} 9091.64Smycroft 9101.64Smycroft_rc_subr_loaded=: 911