rc.subr revision 1.52
11.52Sabs# $NetBSD: rc.subr,v 1.52 2002/08/26 17:46:57 abs 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.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.52Sabs warn "\$${1} is not set properly - see rc.conf(5)." 661.4Slukem return 1 671.3Slukem ;; 681.3Slukem esac 691.38Slukem} 701.38Slukem 711.38Slukem# reverse_list list 721.38Slukem# print the list in reverse order 731.38Slukem# 741.38Slukemreverse_list() 751.38Slukem{ 761.38Slukem _revlist= 771.38Slukem for _revfile in $*; do 781.38Slukem _revlist="$_revfile $_revlist" 791.38Slukem done 801.38Slukem echo $_revlist 811.6Smellon} 821.6Smellon 831.6Smellon# 841.47Slukem# mount_critical_filesystems type 851.47Slukem# Go through the list of critical filesystems as provided in 861.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 871.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 881.6Smellon# 891.11Slukemmount_critical_filesystems() 901.11Slukem{ 911.47Slukem eval _fslist=\$critical_filesystems_${1} 921.17Slukem for _fs in $_fslist; do 931.6Smellon mount | ( 941.17Slukem _ismounted=no 951.6Smellon while read what _on on _type type; do 961.17Slukem if [ $on = $_fs ]; then 971.17Slukem _ismounted=yes 981.6Smellon fi 991.6Smellon done 1001.46Slukem if [ $_ismounted = no ]; then 1011.17Slukem mount $_fs >/dev/null 2>&1 1021.6Smellon fi 1031.46Slukem ) 1041.6Smellon done 1051.7Scjs} 1061.7Scjs 1071.11Slukem# 1081.45Slukem# check_pidfile pidfile procname [interpreter] 1091.45Slukem# Parses the first line of pidfile for a PID, and ensures 1101.11Slukem# that the process is running and matches procname. 1111.45Slukem# Prints the matching PID upon success, nothing otherwise. 1121.45Slukem# interpreter is optional; see _find_processes() for details. 1131.11Slukem# 1141.11Slukemcheck_pidfile() 1151.11Slukem{ 1161.11Slukem _pidfile=$1 1171.11Slukem _procname=$2 1181.45Slukem _interpreter=$3 1191.11Slukem if [ -z "$_pidfile" -o -z "$_procname" ]; then 1201.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 1211.11Slukem fi 1221.11Slukem if [ ! -f $_pidfile ]; then 1231.11Slukem return 1241.11Slukem fi 1251.11Slukem read _pid _junk < $_pidfile 1261.11Slukem if [ -z "$_pid" ]; then 1271.11Slukem return 1281.11Slukem fi 1291.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 1301.11Slukem} 1311.11Slukem 1321.11Slukem# 1331.45Slukem# check_process procname [interpreter] 1341.11Slukem# Ensures that a process (or processes) named procname is running. 1351.45Slukem# Prints a list of matching PIDs. 1361.45Slukem# interpreter is optional; see _find_processes() for details. 1371.11Slukem# 1381.11Slukemcheck_process() 1391.11Slukem{ 1401.11Slukem _procname=$1 1411.45Slukem _interpreter=$2 1421.11Slukem if [ -z "$_procname" ]; then 1431.45Slukem err 3 'USAGE: check_process procname [interpreter]' 1441.45Slukem fi 1451.45Slukem _find_processes $_procname ${_interpreter:-.} '-ax' 1461.45Slukem} 1471.45Slukem 1481.46Slukem# 1491.45Slukem# _find_processes procname interpreter psargs 1501.45Slukem# Search for procname in the output of ps generated by psargs. 1511.45Slukem# Prints the PIDs of any matching processes, space separated. 1521.45Slukem# 1531.45Slukem# If interpreter == ".", check the following variations of procname 1541.45Slukem# against the first word of each command: 1551.45Slukem# procname 1561.45Slukem# `basename procname` 1571.45Slukem# `basename procname` + ":" 1581.45Slukem# "(" + `basename procname` + ")" 1591.45Slukem# 1601.45Slukem# If interpreter != ".", read the first line of procname, remove the 1611.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 1621.45Slukem# match that against each command, either as is, or with extra words 1631.45Slukem# at the end. 1641.45Slukem# 1651.45Slukem_find_processes() 1661.45Slukem{ 1671.45Slukem if [ $# -ne 3 ]; then 1681.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 1691.11Slukem fi 1701.45Slukem _procname=$1 1711.45Slukem _interpreter=$2 1721.45Slukem _psargs=$3 1731.45Slukem 1741.11Slukem _pref= 1751.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 1761.45Slukem read _interp < $_procname # read interpreter name 1771.45Slukem _interp=${_interp#\#!} # strip #! 1781.45Slukem set -- $_interp 1791.45Slukem if [ $_interpreter != $1 ]; then 1801.45Slukem warn "\$command_interpreter $_interpreter != $1" 1811.45Slukem fi 1821.45Slukem _interp="$* $_procname" # cleanup spaces, add _procname 1831.45Slukem _fp_args='_argv' 1841.45Slukem _fp_match='case "$_argv" in 1851.45Slukem ${_interp}|"${_interp} "*)' 1861.45Slukem else # a normal daemon 1871.45Slukem _procnamebn=${_procname##*/} 1881.45Slukem _fp_args='_arg0 _argv' 1891.45Slukem _fp_match='case "$_arg0" in 1901.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 1911.45Slukem fi 1921.45Slukem 1931.45Slukem _proccheck=' 1941.46Slukem ps -o "pid,command" '"$_psargs"' | 1951.45Slukem while read _npid '"$_fp_args"'; do 1961.45Slukem case "$_npid" in 1971.45Slukem PID) 1981.45Slukem continue ;; 1991.45Slukem esac ; '"$_fp_match"' 2001.45Slukem echo -n "$_pref$_npid" ; 2011.45Slukem _pref=" " 2021.45Slukem ;; 2031.45Slukem esac 2041.45Slukem done' 2051.45Slukem 2061.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 2071.45Slukem eval $_proccheck 2081.11Slukem} 2091.11Slukem 2101.11Slukem# 2111.33Slukem# wait_for_pids pid [pid ...] 2121.35Slukem# spins until none of the pids exist 2131.33Slukem# 2141.33Slukemwait_for_pids() 2151.33Slukem{ 2161.33Slukem _list=$* 2171.33Slukem if [ -z "$_list" ]; then 2181.33Slukem return 2191.33Slukem fi 2201.35Slukem _prefix= 2211.35Slukem while true; do 2221.33Slukem _nlist=""; 2231.33Slukem for _j in $_list; do 2241.33Slukem if kill -0 $_j 2>/dev/null; then 2251.33Slukem _nlist="${_nlist}${_nlist:+ }$_j" 2261.33Slukem fi 2271.33Slukem done 2281.33Slukem if [ -z "$_nlist" ]; then 2291.33Slukem break 2301.33Slukem fi 2311.33Slukem _list=$_nlist 2321.35Slukem echo -n ${_prefix:-"Waiting for PIDS: "}$_list 2331.35Slukem _prefix=", " 2341.35Slukem sleep 2 2351.33Slukem done 2361.35Slukem if [ -n "$_prefix" ]; then 2371.35Slukem echo "." 2381.35Slukem fi 2391.33Slukem} 2401.33Slukem 2411.33Slukem# 2421.46Slukem# run_rc_command argument 2431.46Slukem# Search for argument in the list of supported commands, which is: 2441.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 2451.46Slukem# If there's a match, run ${argument}_cmd or the default method 2461.46Slukem# (see below). 2471.11Slukem# 2481.46Slukem# If argument has a given prefix, then change the operation as follows: 2491.46Slukem# Prefix Operation 2501.11Slukem# ------ --------- 2511.48Slukem# fast Skip the pid check, and set rc_fast=yes 2521.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 2531.11Slukem# 2541.11Slukem# The following globals are used: 2551.24Slukem# 2561.46Slukem# Name Needed Purpose 2571.46Slukem# ---- ------ ------- 2581.11Slukem# name y Name of script. 2591.24Slukem# 2601.11Slukem# command n Full path to command. 2611.46Slukem# Not needed if ${rc_arg}_cmd is set for 2621.11Slukem# each keyword. 2631.24Slukem# 2641.11Slukem# command_args n Optional args/shell directives for command. 2651.24Slukem# 2661.45Slukem# command_interpreter n If not empty, command is interpreted, so 2671.45Slukem# call check_{pidfile,process}() appropriately. 2681.45Slukem# 2691.16Slukem# extra_commands n List of extra commands supported. 2701.24Slukem# 2711.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 2721.42Slukem# otherwise use check_process $command. 2731.42Slukem# In either case, only check if $command is set. 2741.42Slukem# 2751.42Slukem# procname n Process name to check for instead of $command. 2761.24Slukem# 2771.24Slukem# rcvar n This is checked with checkyesno to determine 2781.24Slukem# if the action should be run. 2791.24Slukem# 2801.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 2811.42Slukem# Requires /usr to be mounted. 2821.24Slukem# 2831.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 2841.22Slukem# (if not using ${name}_chroot). 2851.24Slukem# 2861.11Slukem# ${name}_flags n Arguments to call ${command} with. 2871.24Slukem# NOTE: $flags from the parent environment 2881.24Slukem# can be used to override this. 2891.24Slukem# 2901.23Slukem# ${name}_nice n Nice level to run ${command} at. 2911.24Slukem# 2921.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 2931.22Slukem# using ${name}_chroot. 2941.42Slukem# Requires /usr to be mounted. 2951.24Slukem# 2961.22Slukem# ${name}_group n Group to run chrooted ${command} as. 2971.42Slukem# Requires /usr to be mounted. 2981.24Slukem# 2991.32Slukem# ${name}_groups n Comma separated list of supplementary groups 3001.32Slukem# to run the chrooted ${command} with. 3011.42Slukem# Requires /usr to be mounted. 3021.24Slukem# 3031.50Satatat# ${name}_systrace n Flags passed to systrace(1) if it is used. 3041.50Satatat# Setting this variable enables systracing 3051.50Satatat# of the given program. The use of "-a" is 3061.50Satatat# recommended so that the boot process is not 3071.50Satatat# stalled. In order to pass no flags to 3081.50Satatat# systrace, set this variable to "--". 3091.50Satatat# 3101.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 3111.11Slukem# Otherwise, use default command (see below) 3121.24Slukem# 3131.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 3141.46Slukem# ${rc_arg}_cmd method in the default 3151.46Slukem# operation (i.e, after checking for required 3161.46Slukem# bits and process (non)existence). 3171.11Slukem# If this completes with a non-zero exit code, 3181.46Slukem# don't run ${rc_arg}_cmd. 3191.24Slukem# 3201.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 3211.46Slukem# ${rc_arg}_cmd method, if that method 3221.43Slukem# returned a zero exit code. 3231.43Slukem# 3241.11Slukem# required_dirs n If set, check for the existence of the given 3251.11Slukem# directories before running the default 3261.11Slukem# (re)start command. 3271.24Slukem# 3281.11Slukem# required_files n If set, check for the readability of the given 3291.11Slukem# files before running the default (re)start 3301.11Slukem# command. 3311.24Slukem# 3321.11Slukem# required_vars n If set, perform checkyesno on each of the 3331.11Slukem# listed variables before running the default 3341.11Slukem# (re)start command. 3351.11Slukem# 3361.46Slukem# Default behaviour for a given argument, if no override method is 3371.46Slukem# provided: 3381.24Slukem# 3391.46Slukem# Argument Default behaviour 3401.46Slukem# -------- ----------------- 3411.11Slukem# start if !running && checkyesno ${rcvar} 3421.11Slukem# ${command} 3431.24Slukem# 3441.11Slukem# stop if ${pidfile} 3451.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 3461.11Slukem# else 3471.46Slukem# rc_pid=$(check_process $command) 3481.46Slukem# kill $sig_stop $rc_pid 3491.46Slukem# wait_for_pids $rc_pid 3501.35Slukem# ($sig_stop defaults to TERM.) 3511.24Slukem# 3521.35Slukem# reload Similar to stop, except use $sig_reload instead, 3531.35Slukem# and doesn't wait_for_pids. 3541.11Slukem# $sig_reload defaults to HUP. 3551.24Slukem# 3561.11Slukem# restart Run `stop' then `start'. 3571.11Slukem# 3581.33Slukem# status Show if ${command} is running, etc. 3591.33Slukem# 3601.33Slukem# poll Wait for ${command} to exit. 3611.33Slukem# 3621.33Slukem# rcvar Display what rc.conf variable is used (if any). 3631.33Slukem# 3641.46Slukem# Variables available to methods, and after run_rc_command() has 3651.46Slukem# completed: 3661.46Slukem# 3671.46Slukem# Variable Purpose 3681.46Slukem# -------- ------- 3691.46Slukem# rc_arg Argument to command, after fast/force processing 3701.46Slukem# performed 3711.46Slukem# 3721.46Slukem# rc_flags Flags to start the default command with. 3731.46Slukem# Defaults to ${name}_flags, unless overridden 3741.46Slukem# by $flags from the environment. 3751.46Slukem# This variable may be changed by the precmd method. 3761.46Slukem# 3771.46Slukem# rc_pid PID of command (if appropriate) 3781.46Slukem# 3791.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 3801.46Slukem# 3811.46Slukem# rc_force Not empty if "force" was provided (q.v.) 3821.33Slukem# 3831.24Slukem# 3841.11Slukemrun_rc_command() 3851.11Slukem{ 3861.46Slukem rc_arg=$1 3871.24Slukem if [ -z "$name" ]; then 3881.30Slukem err 3 'run_rc_command: $name is not set.' 3891.11Slukem fi 3901.11Slukem 3911.46Slukem case "$rc_arg" in 3921.24Slukem fast*) # "fast" prefix; don't check pid 3931.46Slukem rc_arg=${rc_arg#fast} 3941.48Slukem rc_fast=yes 3951.11Slukem ;; 3961.24Slukem force*) # "force prefix; always start 3971.46Slukem rc_arg=${rc_arg#force} 3981.48Slukem rc_force=yes 3991.29Slukem if [ -n "${rcvar}" ]; then 4001.29Slukem eval ${rcvar}=YES 4011.29Slukem fi 4021.11Slukem ;; 4031.11Slukem esac 4041.11Slukem 4051.16Slukem _keywords="start stop restart rcvar $extra_commands" 4061.46Slukem rc_pid= 4071.11Slukem _pidcmd= 4081.42Slukem _procname=${procname:-${command}} 4091.42Slukem 4101.24Slukem # setup pid check command if not fast 4111.46Slukem if [ -z "$rc_fast" -a -n "$_procname" ]; then 4121.11Slukem if [ -n "$pidfile" ]; then 4131.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 4141.42Slukem else 4151.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 4161.11Slukem fi 4171.11Slukem if [ -n "$_pidcmd" ]; then 4181.33Slukem _keywords="${_keywords} status poll" 4191.11Slukem fi 4201.11Slukem fi 4211.11Slukem 4221.46Slukem if [ -z "$rc_arg" ]; then 4231.11Slukem rc_usage "$_keywords" 4241.11Slukem fi 4251.11Slukem 4261.17Slukem if [ -n "$flags" ]; then # allow override from environment 4271.46Slukem rc_flags=$flags 4281.11Slukem else 4291.46Slukem eval rc_flags=\$${name}_flags 4301.11Slukem fi 4311.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 4321.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 4331.50Satatat _group=\$${name}_group _groups=\$${name}_groups \ 4341.50Satatat _systrace=\$${name}_systrace 4351.11Slukem 4361.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 4371.42Slukem if [ "$_user" = "$(id -un)" ]; then 4381.42Slukem unset _user 4391.42Slukem fi 4401.42Slukem fi 4411.42Slukem 4421.29Slukem # if ${rcvar} is set, and $1 is not 4431.40Slukem # "rcvar", then run 4441.29Slukem # checkyesno ${rcvar} 4451.29Slukem # and return if that failed 4461.24Slukem # 4471.46Slukem if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 4481.24Slukem if ! checkyesno ${rcvar}; then 4491.24Slukem return 0 4501.24Slukem fi 4511.24Slukem fi 4521.24Slukem 4531.24Slukem eval $_pidcmd # determine the pid if necessary 4541.11Slukem 4551.11Slukem for _elem in $_keywords; do 4561.46Slukem if [ "$_elem" != "$rc_arg" ]; then 4571.11Slukem continue 4581.11Slukem fi 4591.11Slukem 4601.24Slukem # if there's a custom ${XXX_cmd}, 4611.24Slukem # run that instead of the default 4621.24Slukem # 4631.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 4641.46Slukem _postcmd=\$${rc_arg}_postcmd 4651.11Slukem if [ -n "$_cmd" ]; then 4661.24Slukem # if the precmd failed and force 4671.24Slukem # isn't set, exit 4681.24Slukem # 4691.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 4701.24Slukem return 1 4711.24Slukem fi 4721.24Slukem 4731.46Slukem if ! eval $_cmd && [ -z "$rc_force" ]; then 4741.44Slukem return 1 4751.44Slukem fi 4761.44Slukem eval $_postcmd 4771.11Slukem return 0 4781.11Slukem fi 4791.11Slukem 4801.46Slukem case "$rc_arg" in # default operations... 4811.11Slukem 4821.11Slukem status) 4831.46Slukem if [ -n "$rc_pid" ]; then 4841.46Slukem echo "${name} is running as pid $rc_pid." 4851.11Slukem else 4861.11Slukem echo "${name} is not running." 4871.28Slukem return 1 4881.11Slukem fi 4891.11Slukem ;; 4901.11Slukem 4911.11Slukem start) 4921.46Slukem if [ -n "$rc_pid" ]; then 4931.46Slukem echo "${name} already running? (pid=$rc_pid)." 4941.11Slukem exit 1 4951.11Slukem fi 4961.11Slukem 4971.24Slukem if [ ! -x $command ]; then 4981.11Slukem return 0 4991.11Slukem fi 5001.11Slukem 5011.24Slukem # check for required variables, 5021.24Slukem # directories, and files 5031.24Slukem # 5041.11Slukem for _f in $required_vars; do 5051.11Slukem if ! checkyesno $_f; then 5061.24Slukem warn "\$${_f} is not set." 5071.46Slukem if [ -z "$rc_force" ]; then 5081.24Slukem return 1 5091.24Slukem fi 5101.11Slukem fi 5111.11Slukem done 5121.11Slukem for _f in $required_dirs; do 5131.11Slukem if [ ! -d "${_f}/." ]; then 5141.25Slukem warn "${_f} is not a directory." 5151.46Slukem if [ -z "$rc_force" ]; then 5161.24Slukem return 1 5171.24Slukem fi 5181.11Slukem fi 5191.11Slukem done 5201.11Slukem for _f in $required_files; do 5211.11Slukem if [ ! -r "${_f}" ]; then 5221.25Slukem warn "${_f} is not readable." 5231.46Slukem if [ -z "$rc_force" ]; then 5241.24Slukem return 1 5251.24Slukem fi 5261.11Slukem fi 5271.11Slukem done 5281.11Slukem 5291.24Slukem # if the precmd failed and force 5301.24Slukem # isn't set, exit 5311.24Slukem # 5321.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5331.24Slukem return 1 5341.24Slukem fi 5351.24Slukem 5361.24Slukem # setup the command to run, and run it 5371.29Slukem # 5381.11Slukem echo "Starting ${name}." 5391.22Slukem if [ -n "$_chroot" ]; then 5401.22Slukem _doit="\ 5411.23Slukem${_nice:+nice -n $_nice }\ 5421.50Satatat${_systrace:+systrace $_systrace }\ 5431.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 5441.46Slukem$_chroot $command $rc_flags $command_args" 5451.22Slukem else 5461.22Slukem _doit="\ 5471.18Slukem${_chdir:+cd $_chdir; }\ 5481.18Slukem${_nice:+nice -n $_nice }\ 5491.50Satatat${_systrace:+systrace $_systrace }\ 5501.46Slukem$command $rc_flags $command_args" 5511.34Slukem if [ -n "$_user" ]; then 5521.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 5531.34Slukem fi 5541.22Slukem fi 5551.43Slukem 5561.43Slukem # if the cmd failed and force 5571.43Slukem # isn't set, exit 5581.43Slukem # 5591.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 5601.43Slukem return 1 5611.43Slukem fi 5621.43Slukem 5631.43Slukem # finally, run postcmd 5641.43Slukem # 5651.43Slukem eval $_postcmd 5661.11Slukem ;; 5671.11Slukem 5681.11Slukem stop) 5691.46Slukem if [ -z "$rc_pid" ]; then 5701.24Slukem if [ -n "$pidfile" ]; then 5711.24Slukem echo \ 5721.24Slukem "${name} not running? (check $pidfile)." 5731.24Slukem else 5741.24Slukem echo "${name} not running?" 5751.11Slukem fi 5761.24Slukem exit 1 5771.11Slukem fi 5781.11Slukem 5791.43Slukem # if the precmd failed and force 5801.43Slukem # isn't set, exit 5811.43Slukem # 5821.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5831.24Slukem return 1 5841.24Slukem fi 5851.43Slukem 5861.43Slukem # send the signal to stop 5871.43Slukem # 5881.11Slukem echo "Stopping ${name}." 5891.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 5901.34Slukem if [ -n "$_user" ]; then 5911.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 5921.34Slukem fi 5931.43Slukem 5941.43Slukem # if the stop cmd failed and force 5951.43Slukem # isn't set, exit 5961.43Slukem # 5971.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 5981.43Slukem return 1 5991.43Slukem fi 6001.43Slukem 6011.43Slukem # wait for the command to exit, 6021.43Slukem # and run postcmd. 6031.46Slukem wait_for_pids $rc_pid 6041.43Slukem eval $_postcmd 6051.11Slukem ;; 6061.11Slukem 6071.11Slukem reload) 6081.46Slukem if [ -z "$rc_pid" ]; then 6091.24Slukem if [ -n "$pidfile" ]; then 6101.24Slukem echo \ 6111.11Slukem "${name} not running? (check $pidfile)." 6121.24Slukem else 6131.24Slukem echo "${name} not running?" 6141.11Slukem fi 6151.24Slukem exit 1 6161.11Slukem fi 6171.11Slukem echo "Reloading ${name} config files." 6181.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6191.24Slukem return 1 6201.24Slukem fi 6211.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 6221.34Slukem if [ -n "$_user" ]; then 6231.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6241.34Slukem fi 6251.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6261.43Slukem return 1 6271.43Slukem fi 6281.43Slukem eval $_postcmd 6291.11Slukem ;; 6301.11Slukem 6311.11Slukem restart) 6321.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6331.24Slukem return 1 6341.11Slukem fi 6351.29Slukem # prevent restart being called more 6361.29Slukem # than once by any given script 6371.29Slukem # 6381.29Slukem if [ -n "$_rc_restart_done" ]; then 6391.29Slukem return 0 6401.29Slukem fi 6411.29Slukem _rc_restart_done=YES 6421.33Slukem 6431.46Slukem ( $0 ${rc_force:+force}stop ) 6441.46Slukem $0 ${rc_force:+force}start 6451.11Slukem 6461.43Slukem eval $_postcmd 6471.33Slukem ;; 6481.33Slukem 6491.33Slukem poll) 6501.46Slukem if [ -n "$rc_pid" ]; then 6511.46Slukem wait_for_pids $rc_pid 6521.33Slukem fi 6531.11Slukem ;; 6541.11Slukem 6551.11Slukem rcvar) 6561.11Slukem echo "# $name" 6571.24Slukem if [ -n "$rcvar" ]; then 6581.24Slukem if checkyesno ${rcvar}; then 6591.24Slukem echo "\$${rcvar}=YES" 6601.24Slukem else 6611.24Slukem echo "\$${rcvar}=NO" 6621.24Slukem fi 6631.11Slukem fi 6641.11Slukem ;; 6651.11Slukem 6661.11Slukem *) 6671.11Slukem rc_usage "$_keywords" 6681.11Slukem ;; 6691.11Slukem 6701.11Slukem esac 6711.11Slukem return 0 6721.11Slukem done 6731.11Slukem 6741.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 6751.11Slukem rc_usage "$_keywords" 6761.11Slukem exit 1 6771.11Slukem} 6781.11Slukem 6791.11Slukem# 6801.11Slukem# run_rc_script file arg 6811.11Slukem# Start the script `file' with `arg', and correctly handle the 6821.17Slukem# return value from the script. If `file' ends with `.sh', it's 6831.37Slukem# sourced into the current environment. If `file' appears to be 6841.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 6851.37Slukem# executable run as a child process. 6861.17Slukem# 6871.11Slukemrun_rc_script() 6881.11Slukem{ 6891.11Slukem _file=$1 6901.11Slukem _arg=$2 6911.11Slukem if [ -z "$_file" -o -z "$_arg" ]; then 6921.11Slukem err 3 'USAGE: run_rc_script file arg' 6931.11Slukem fi 6941.11Slukem 6951.45Slukem unset name command command_args command_interpreter \ 6961.45Slukem extra_commands pidfile procname \ 6971.42Slukem rcvar required_dirs required_files required_vars 6981.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 6991.39Slukem 7001.39Slukem case "$_file" in 7011.39Slukem *.sh) # run in current shell 7021.11Slukem set $_arg ; . $_file 7031.39Slukem ;; 7041.39Slukem *[~#]|*.OLD|*.orig) # scratch file; skip 7051.39Slukem warn "Ignoring scratch file $_file" 7061.39Slukem ;; 7071.39Slukem *) # run in subshell 7081.39Slukem if [ -x $_file ]; then 7091.39Slukem if [ -n "$rc_fast_and_loose" ]; then 7101.39Slukem set $_arg ; . $_file 7111.39Slukem else 7121.37Slukem ( set $_arg ; . $_file ) 7131.37Slukem fi 7141.39Slukem fi 7151.39Slukem ;; 7161.39Slukem esac 7171.11Slukem} 7181.19Slukem 7191.19Slukem# 7201.19Slukem# load_rc_config 7211.19Slukem# Source in the configuration file for a given command. 7221.19Slukem# 7231.19Slukemload_rc_config() 7241.19Slukem{ 7251.19Slukem _command=$1 7261.19Slukem if [ -z "$_command" ]; then 7271.19Slukem err 3 'USAGE: load_rc_config command' 7281.19Slukem fi 7291.19Slukem 7301.30Slukem if [ -z "$_rc_conf_loaded" ]; then 7311.30Slukem . /etc/rc.conf 7321.30Slukem _rc_conf_loaded=YES 7331.30Slukem fi 7341.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 7351.20Sfvdl . /etc/rc.conf.d/"$_command" 7361.20Sfvdl fi 7371.19Slukem} 7381.19Slukem 7391.11Slukem 7401.11Slukem# 7411.11Slukem# rc_usage commands 7421.11Slukem# Print a usage string for $0, with `commands' being a list of 7431.11Slukem# valid commands. 7441.11Slukem# 7451.11Slukemrc_usage() 7461.11Slukem{ 7471.11Slukem echo -n 1>&2 "Usage: $0 [fast|force](" 7481.11Slukem 7491.11Slukem _sep= 7501.11Slukem for _elem in $*; do 7511.11Slukem echo -n 1>&2 "$_sep$_elem" 7521.11Slukem _sep="|" 7531.11Slukem done 7541.11Slukem echo 1>&2 ")" 7551.11Slukem exit 1 7561.11Slukem} 7571.11Slukem 7581.11Slukem# 7591.11Slukem# err exitval message 7601.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 7611.11Slukem# 7621.11Slukemerr() 7631.11Slukem{ 7641.11Slukem exitval=$1 7651.11Slukem shift 7661.11Slukem 7671.51Sgrant if [ -x /usr/bin/logger ]; then 7681.51Sgrant logger "$0: ERROR: $*" 7691.51Sgrant fi 7701.21Slukem echo 1>&2 "$0: ERROR: $*" 7711.11Slukem exit $exitval 7721.11Slukem} 7731.11Slukem 7741.11Slukem# 7751.11Slukem# warn message 7761.11Slukem# Display message to stderr and log to the syslog. 7771.11Slukem# 7781.11Slukemwarn() 7791.11Slukem{ 7801.51Sgrant if [ -x /usr/bin/logger ]; then 7811.51Sgrant logger "$0: WARNING: $*" 7821.51Sgrant fi 7831.21Slukem echo 1>&2 "$0: WARNING: $*" 7841.31Satatat} 7851.31Satatat 7861.31Satatat# 7871.31Satatat# backup_file action file cur backup 7881.31Satatat# Make a backup copy of `file' into `cur', and save the previous 7891.31Satatat# version of `cur' as `backup' or use rcs for archiving. 7901.31Satatat# 7911.31Satatat# This routine checks the value of the backup_uses_rcs variable, 7921.31Satatat# which can be either YES or NO. 7931.31Satatat# 7941.31Satatat# The `action' keyword can be one of the following: 7951.31Satatat# 7961.31Satatat# add `file' is now being backed up (and is possibly 7971.31Satatat# being reentered into the backups system). `cur' 7981.31Satatat# is created and RCS files, if necessary, are 7991.31Satatat# created as well. 8001.31Satatat# 8011.31Satatat# update `file' has changed and needs to be backed up. 8021.31Satatat# If `cur' exists, it is copied to to `back' or 8031.31Satatat# checked into RCS (if the repository file is old), 8041.31Satatat# and then `file' is copied to `cur'. Another RCS 8051.31Satatat# check in done here if RCS is being used. 8061.31Satatat# 8071.31Satatat# remove `file' is no longer being tracked by the backups 8081.31Satatat# system. If RCS is not being used, `cur' is moved 8091.31Satatat# to `back', otherwise an empty file is checked in, 8101.31Satatat# and then `cur' is removed. 8111.31Satatat# 8121.31Satatat# 8131.31Satatatbackup_file() 8141.31Satatat{ 8151.31Satatat _action=$1 8161.31Satatat _file=$2 8171.31Satatat _cur=$3 8181.31Satatat _back=$4 8191.31Satatat 8201.31Satatat if checkyesno backup_uses_rcs; then 8211.31Satatat _msg0="backup archive" 8221.31Satatat _msg1="update" 8231.31Satatat 8241.36Satatat # ensure that history file is not locked 8251.36Satatat if [ -f $_cur,v ]; then 8261.36Satatat rcs -q -u -U -M $_cur 8271.36Satatat fi 8281.36Satatat 8291.31Satatat # ensure after switching to rcs that the 8301.31Satatat # current backup is not lost 8311.31Satatat if [ -f $_cur ]; then 8321.31Satatat # no archive, or current newer than archive 8331.31Satatat if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 8341.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8351.36Satatat rcs -q -kb -U $_cur 8361.49Slukem co -q -f -u $_cur 8371.31Satatat fi 8381.31Satatat fi 8391.31Satatat 8401.31Satatat case $_action in 8411.31Satatat add|update) 8421.31Satatat cp -p $_file $_cur 8431.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8441.36Satatat rcs -q -kb -U $_cur 8451.49Slukem co -q -f -u $_cur 8461.31Satatat chown root:wheel $_cur $_cur,v 8471.31Satatat ;; 8481.31Satatat remove) 8491.31Satatat cp /dev/null $_cur 8501.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8511.36Satatat rcs -q -kb -U $_cur 8521.31Satatat chown root:wheel $_cur $_cur,v 8531.31Satatat rm $_cur 8541.31Satatat ;; 8551.31Satatat esac 8561.31Satatat else 8571.31Satatat case $_action in 8581.31Satatat add|update) 8591.31Satatat if [ -f $_cur ]; then 8601.31Satatat cp -p $_cur $_back 8611.31Satatat fi 8621.31Satatat cp -p $_file $_cur 8631.31Satatat chown root:wheel $_cur 8641.31Satatat ;; 8651.31Satatat remove) 8661.31Satatat mv -f $_cur $_back 8671.31Satatat ;; 8681.31Satatat esac 8691.31Satatat fi 8701.1Scjs} 871