rc.subr revision 1.75
11.75Sreed# $NetBSD: rc.subr,v 1.75 2009/04/28 03:03:52 reed 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# 181.11Slukem# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 191.11Slukem# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 201.11Slukem# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 211.11Slukem# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 221.11Slukem# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 231.11Slukem# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 241.11Slukem# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 251.11Slukem# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 261.11Slukem# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 271.11Slukem# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 281.11Slukem# POSSIBILITY OF SUCH DAMAGE. 291.11Slukem# 301.11Slukem# rc.subr 311.11Slukem# functions used by various rc scripts 321.11Slukem# 331.11Slukem 341.62Sjmmv: ${rcvar_manpage:='rc.conf(5)'} 351.69Sapb: ${RC_PID:=$$} ; export RC_PID 361.62Sjmmv 371.11Slukem# 381.11Slukem# functions 391.11Slukem# --------- 401.1Scjs 411.5Slukem# 421.11Slukem# checkyesno var 431.5Slukem# Test $1 variable, and warn if not set to YES or NO. 441.11Slukem# Return 0 if it's "yes" (et al), nonzero otherwise. 451.5Slukem# 461.11Slukemcheckyesno() 471.11Slukem{ 481.11Slukem eval _value=\$${1} 491.11Slukem case $_value in 501.4Slukem 511.4Slukem # "yes", "true", "on", or "1" 521.4Slukem [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 531.4Slukem return 0 541.3Slukem ;; 551.4Slukem 561.4Slukem # "no", "false", "off", or "0" 571.4Slukem [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 581.4Slukem return 1 591.3Slukem ;; 601.3Slukem *) 611.62Sjmmv warn "\$${1} is not set properly - see ${rcvar_manpage}." 621.4Slukem return 1 631.3Slukem ;; 641.3Slukem esac 651.38Slukem} 661.38Slukem 671.65Slukem# 681.38Slukem# reverse_list list 691.38Slukem# print the list in reverse order 701.38Slukem# 711.38Slukemreverse_list() 721.38Slukem{ 731.38Slukem _revlist= 741.56Schristos for _revfile; do 751.38Slukem _revlist="$_revfile $_revlist" 761.38Slukem done 771.38Slukem echo $_revlist 781.6Smellon} 791.6Smellon 801.6Smellon# 811.69Sapb# If booting directly to multiuser, send SIGTERM to 821.69Sapb# the parent (/etc/rc) to abort the boot. 831.69Sapb# Otherwise just exit. 841.69Sapb# 851.69Sapbstop_boot() 861.69Sapb{ 871.69Sapb if [ "$autoboot" = yes ]; then 881.69Sapb echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 891.69Sapb kill -TERM ${RC_PID} 901.69Sapb fi 911.69Sapb exit 1 921.69Sapb} 931.69Sapb 941.69Sapb# 951.47Slukem# mount_critical_filesystems type 961.47Slukem# Go through the list of critical filesystems as provided in 971.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 981.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 991.6Smellon# 1001.11Slukemmount_critical_filesystems() 1011.11Slukem{ 1021.47Slukem eval _fslist=\$critical_filesystems_${1} 1031.17Slukem for _fs in $_fslist; do 1041.6Smellon mount | ( 1051.53Slukem _ismounted=false 1061.6Smellon while read what _on on _type type; do 1071.17Slukem if [ $on = $_fs ]; then 1081.53Slukem _ismounted=true 1091.6Smellon fi 1101.6Smellon done 1111.53Slukem if $_ismounted; then 1121.55Slukem : 1131.55Slukem else 1141.17Slukem mount $_fs >/dev/null 2>&1 1151.6Smellon fi 1161.46Slukem ) 1171.6Smellon done 1181.7Scjs} 1191.7Scjs 1201.11Slukem# 1211.45Slukem# check_pidfile pidfile procname [interpreter] 1221.45Slukem# Parses the first line of pidfile for a PID, and ensures 1231.11Slukem# that the process is running and matches procname. 1241.45Slukem# Prints the matching PID upon success, nothing otherwise. 1251.45Slukem# interpreter is optional; see _find_processes() for details. 1261.11Slukem# 1271.11Slukemcheck_pidfile() 1281.11Slukem{ 1291.11Slukem _pidfile=$1 1301.11Slukem _procname=$2 1311.45Slukem _interpreter=$3 1321.11Slukem if [ -z "$_pidfile" -o -z "$_procname" ]; then 1331.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 1341.11Slukem fi 1351.11Slukem if [ ! -f $_pidfile ]; then 1361.11Slukem return 1371.11Slukem fi 1381.11Slukem read _pid _junk < $_pidfile 1391.11Slukem if [ -z "$_pid" ]; then 1401.11Slukem return 1411.11Slukem fi 1421.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 1431.11Slukem} 1441.11Slukem 1451.11Slukem# 1461.45Slukem# check_process procname [interpreter] 1471.11Slukem# Ensures that a process (or processes) named procname is running. 1481.45Slukem# Prints a list of matching PIDs. 1491.45Slukem# interpreter is optional; see _find_processes() for details. 1501.11Slukem# 1511.11Slukemcheck_process() 1521.11Slukem{ 1531.11Slukem _procname=$1 1541.45Slukem _interpreter=$2 1551.11Slukem if [ -z "$_procname" ]; then 1561.45Slukem err 3 'USAGE: check_process procname [interpreter]' 1571.45Slukem fi 1581.45Slukem _find_processes $_procname ${_interpreter:-.} '-ax' 1591.45Slukem} 1601.45Slukem 1611.46Slukem# 1621.45Slukem# _find_processes procname interpreter psargs 1631.45Slukem# Search for procname in the output of ps generated by psargs. 1641.45Slukem# Prints the PIDs of any matching processes, space separated. 1651.45Slukem# 1661.45Slukem# If interpreter == ".", check the following variations of procname 1671.45Slukem# against the first word of each command: 1681.45Slukem# procname 1691.45Slukem# `basename procname` 1701.45Slukem# `basename procname` + ":" 1711.45Slukem# "(" + `basename procname` + ")" 1721.45Slukem# 1731.45Slukem# If interpreter != ".", read the first line of procname, remove the 1741.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 1751.45Slukem# match that against each command, either as is, or with extra words 1761.73Sdholland# at the end. As an alternative, to deal with interpreted daemons 1771.66She# using perl, the basename of the interpreter plus a colon is also 1781.66She# tried as the prefix to procname. 1791.45Slukem# 1801.45Slukem_find_processes() 1811.45Slukem{ 1821.45Slukem if [ $# -ne 3 ]; then 1831.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 1841.11Slukem fi 1851.45Slukem _procname=$1 1861.45Slukem _interpreter=$2 1871.45Slukem _psargs=$3 1881.45Slukem 1891.11Slukem _pref= 1901.68Shubertf _procnamebn=${_procname##*/} 1911.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 1921.67Selad read _interp < ${_chroot:-}/$_procname # read interpreter name 1931.45Slukem _interp=${_interp#\#!} # strip #! 1941.45Slukem set -- $_interp 1951.45Slukem if [ $_interpreter != $1 ]; then 1961.45Slukem warn "\$command_interpreter $_interpreter != $1" 1971.45Slukem fi 1981.45Slukem _interp="$* $_procname" # cleanup spaces, add _procname 1991.66She _interpbn=${1##*/} 2001.45Slukem _fp_args='_argv' 2011.45Slukem _fp_match='case "$_argv" in 2021.68Shubertf ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)' 2031.45Slukem else # a normal daemon 2041.45Slukem _fp_args='_arg0 _argv' 2051.45Slukem _fp_match='case "$_arg0" in 2061.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 2071.45Slukem fi 2081.45Slukem 2091.45Slukem _proccheck=' 2101.46Slukem ps -o "pid,command" '"$_psargs"' | 2111.45Slukem while read _npid '"$_fp_args"'; do 2121.45Slukem case "$_npid" in 2131.45Slukem PID) 2141.45Slukem continue ;; 2151.45Slukem esac ; '"$_fp_match"' 2161.45Slukem echo -n "$_pref$_npid" ; 2171.45Slukem _pref=" " 2181.45Slukem ;; 2191.45Slukem esac 2201.45Slukem done' 2211.45Slukem 2221.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 2231.45Slukem eval $_proccheck 2241.11Slukem} 2251.11Slukem 2261.11Slukem# 2271.33Slukem# wait_for_pids pid [pid ...] 2281.35Slukem# spins until none of the pids exist 2291.33Slukem# 2301.33Slukemwait_for_pids() 2311.33Slukem{ 2321.56Schristos _list="$@" 2331.33Slukem if [ -z "$_list" ]; then 2341.33Slukem return 2351.33Slukem fi 2361.35Slukem _prefix= 2371.35Slukem while true; do 2381.33Slukem _nlist=""; 2391.33Slukem for _j in $_list; do 2401.33Slukem if kill -0 $_j 2>/dev/null; then 2411.33Slukem _nlist="${_nlist}${_nlist:+ }$_j" 2421.33Slukem fi 2431.33Slukem done 2441.33Slukem if [ -z "$_nlist" ]; then 2451.33Slukem break 2461.33Slukem fi 2471.33Slukem _list=$_nlist 2481.35Slukem echo -n ${_prefix:-"Waiting for PIDS: "}$_list 2491.35Slukem _prefix=", " 2501.35Slukem sleep 2 2511.33Slukem done 2521.35Slukem if [ -n "$_prefix" ]; then 2531.35Slukem echo "." 2541.35Slukem fi 2551.33Slukem} 2561.33Slukem 2571.33Slukem# 2581.46Slukem# run_rc_command argument 2591.46Slukem# Search for argument in the list of supported commands, which is: 2601.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 2611.46Slukem# If there's a match, run ${argument}_cmd or the default method 2621.46Slukem# (see below). 2631.11Slukem# 2641.46Slukem# If argument has a given prefix, then change the operation as follows: 2651.46Slukem# Prefix Operation 2661.11Slukem# ------ --------- 2671.48Slukem# fast Skip the pid check, and set rc_fast=yes 2681.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 2691.61Slukem# one Set ${rcvar} to YES 2701.11Slukem# 2711.11Slukem# The following globals are used: 2721.24Slukem# 2731.46Slukem# Name Needed Purpose 2741.46Slukem# ---- ------ ------- 2751.11Slukem# name y Name of script. 2761.24Slukem# 2771.11Slukem# command n Full path to command. 2781.46Slukem# Not needed if ${rc_arg}_cmd is set for 2791.11Slukem# each keyword. 2801.24Slukem# 2811.11Slukem# command_args n Optional args/shell directives for command. 2821.24Slukem# 2831.45Slukem# command_interpreter n If not empty, command is interpreted, so 2841.45Slukem# call check_{pidfile,process}() appropriately. 2851.45Slukem# 2861.16Slukem# extra_commands n List of extra commands supported. 2871.24Slukem# 2881.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 2891.42Slukem# otherwise use check_process $command. 2901.42Slukem# In either case, only check if $command is set. 2911.42Slukem# 2921.42Slukem# procname n Process name to check for instead of $command. 2931.24Slukem# 2941.24Slukem# rcvar n This is checked with checkyesno to determine 2951.24Slukem# if the action should be run. 2961.24Slukem# 2971.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 2981.42Slukem# Requires /usr to be mounted. 2991.24Slukem# 3001.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 3011.22Slukem# (if not using ${name}_chroot). 3021.24Slukem# 3031.11Slukem# ${name}_flags n Arguments to call ${command} with. 3041.24Slukem# NOTE: $flags from the parent environment 3051.24Slukem# can be used to override this. 3061.24Slukem# 3071.72She# ${name}_env n Additional environment variable settings 3081.72She# for running ${command} 3091.72She# 3101.23Slukem# ${name}_nice n Nice level to run ${command} at. 3111.24Slukem# 3121.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 3131.22Slukem# using ${name}_chroot. 3141.42Slukem# Requires /usr to be mounted. 3151.24Slukem# 3161.22Slukem# ${name}_group n Group to run chrooted ${command} as. 3171.42Slukem# Requires /usr to be mounted. 3181.24Slukem# 3191.32Slukem# ${name}_groups n Comma separated list of supplementary groups 3201.32Slukem# to run the chrooted ${command} with. 3211.42Slukem# Requires /usr to be mounted. 3221.24Slukem# 3231.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 3241.11Slukem# Otherwise, use default command (see below) 3251.24Slukem# 3261.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 3271.46Slukem# ${rc_arg}_cmd method in the default 3281.46Slukem# operation (i.e, after checking for required 3291.46Slukem# bits and process (non)existence). 3301.11Slukem# If this completes with a non-zero exit code, 3311.46Slukem# don't run ${rc_arg}_cmd. 3321.24Slukem# 3331.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 3341.46Slukem# ${rc_arg}_cmd method, if that method 3351.43Slukem# returned a zero exit code. 3361.43Slukem# 3371.11Slukem# required_dirs n If set, check for the existence of the given 3381.11Slukem# directories before running the default 3391.11Slukem# (re)start command. 3401.24Slukem# 3411.11Slukem# required_files n If set, check for the readability of the given 3421.11Slukem# files before running the default (re)start 3431.11Slukem# command. 3441.24Slukem# 3451.11Slukem# required_vars n If set, perform checkyesno on each of the 3461.11Slukem# listed variables before running the default 3471.11Slukem# (re)start command. 3481.11Slukem# 3491.46Slukem# Default behaviour for a given argument, if no override method is 3501.46Slukem# provided: 3511.24Slukem# 3521.46Slukem# Argument Default behaviour 3531.46Slukem# -------- ----------------- 3541.11Slukem# start if !running && checkyesno ${rcvar} 3551.11Slukem# ${command} 3561.24Slukem# 3571.11Slukem# stop if ${pidfile} 3581.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 3591.11Slukem# else 3601.46Slukem# rc_pid=$(check_process $command) 3611.46Slukem# kill $sig_stop $rc_pid 3621.46Slukem# wait_for_pids $rc_pid 3631.35Slukem# ($sig_stop defaults to TERM.) 3641.24Slukem# 3651.35Slukem# reload Similar to stop, except use $sig_reload instead, 3661.35Slukem# and doesn't wait_for_pids. 3671.11Slukem# $sig_reload defaults to HUP. 3681.24Slukem# 3691.11Slukem# restart Run `stop' then `start'. 3701.11Slukem# 3711.33Slukem# status Show if ${command} is running, etc. 3721.33Slukem# 3731.33Slukem# poll Wait for ${command} to exit. 3741.33Slukem# 3751.33Slukem# rcvar Display what rc.conf variable is used (if any). 3761.33Slukem# 3771.46Slukem# Variables available to methods, and after run_rc_command() has 3781.46Slukem# completed: 3791.46Slukem# 3801.46Slukem# Variable Purpose 3811.46Slukem# -------- ------- 3821.61Slukem# rc_arg Argument to command, after fast/force/one processing 3831.46Slukem# performed 3841.46Slukem# 3851.46Slukem# rc_flags Flags to start the default command with. 3861.46Slukem# Defaults to ${name}_flags, unless overridden 3871.46Slukem# by $flags from the environment. 3881.46Slukem# This variable may be changed by the precmd method. 3891.46Slukem# 3901.46Slukem# rc_pid PID of command (if appropriate) 3911.46Slukem# 3921.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 3931.46Slukem# 3941.46Slukem# rc_force Not empty if "force" was provided (q.v.) 3951.33Slukem# 3961.24Slukem# 3971.11Slukemrun_rc_command() 3981.11Slukem{ 3991.46Slukem rc_arg=$1 4001.24Slukem if [ -z "$name" ]; then 4011.30Slukem err 3 'run_rc_command: $name is not set.' 4021.11Slukem fi 4031.11Slukem 4041.61Slukem _rc_prefix= 4051.46Slukem case "$rc_arg" in 4061.24Slukem fast*) # "fast" prefix; don't check pid 4071.46Slukem rc_arg=${rc_arg#fast} 4081.48Slukem rc_fast=yes 4091.11Slukem ;; 4101.61Slukem force*) # "force" prefix; always run 4111.48Slukem rc_force=yes 4121.61Slukem _rc_prefix=force 4131.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4141.61Slukem if [ -n "${rcvar}" ]; then 4151.61Slukem eval ${rcvar}=YES 4161.61Slukem fi 4171.61Slukem ;; 4181.61Slukem one*) # "one" prefix; set ${rcvar}=yes 4191.61Slukem _rc_prefix=one 4201.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4211.29Slukem if [ -n "${rcvar}" ]; then 4221.29Slukem eval ${rcvar}=YES 4231.29Slukem fi 4241.11Slukem ;; 4251.11Slukem esac 4261.11Slukem 4271.75Sreed _keywords="start stop restart rcvar" 4281.75Sreed if [ -n "$extra_commands" ]; then 4291.75Sreed _keywords="${_keywords} ${extra_commands}" 4301.75Sreed fi 4311.46Slukem rc_pid= 4321.11Slukem _pidcmd= 4331.42Slukem _procname=${procname:-${command}} 4341.42Slukem 4351.24Slukem # setup pid check command if not fast 4361.46Slukem if [ -z "$rc_fast" -a -n "$_procname" ]; then 4371.11Slukem if [ -n "$pidfile" ]; then 4381.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 4391.42Slukem else 4401.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 4411.11Slukem fi 4421.11Slukem if [ -n "$_pidcmd" ]; then 4431.33Slukem _keywords="${_keywords} status poll" 4441.11Slukem fi 4451.11Slukem fi 4461.11Slukem 4471.46Slukem if [ -z "$rc_arg" ]; then 4481.11Slukem rc_usage "$_keywords" 4491.11Slukem fi 4501.11Slukem 4511.17Slukem if [ -n "$flags" ]; then # allow override from environment 4521.46Slukem rc_flags=$flags 4531.11Slukem else 4541.46Slukem eval rc_flags=\$${name}_flags 4551.11Slukem fi 4561.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 4571.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 4581.72She _group=\$${name}_group _groups=\$${name}_groups \ 4591.72She _env=\"\$${name}_env\" 4601.11Slukem 4611.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 4621.42Slukem if [ "$_user" = "$(id -un)" ]; then 4631.42Slukem unset _user 4641.42Slukem fi 4651.42Slukem fi 4661.42Slukem 4671.29Slukem # if ${rcvar} is set, and $1 is not 4681.40Slukem # "rcvar", then run 4691.29Slukem # checkyesno ${rcvar} 4701.74Ssalo # and return if that failed or warn 4711.74Ssalo # user and exit when interactive 4721.24Slukem # 4731.46Slukem if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 4741.24Slukem if ! checkyesno ${rcvar}; then 4751.74Ssalo # check whether interactive or not 4761.74Ssalo if [ -n "$_run_rc_script" ]; then 4771.74Ssalo return 0 4781.74Ssalo fi 4791.74Ssalo for _elem in $_keywords; do 4801.74Ssalo if [ "$_elem" = "$rc_arg" ]; then 4811.74Ssalo echo 1>&2 "\$${rcvar} is not enabled - see ${rcvar_manpage}." 4821.74Ssalo echo 1>&2 "Use the following if you wish to perform the operation:" 4831.74Ssalo echo 1>&2 " $0 one${rc_arg}" 4841.74Ssalo exit 1 4851.74Ssalo fi 4861.74Ssalo done 4871.74Ssalo echo 1>&2 "$0: unknown directive '$rc_arg'." 4881.74Ssalo rc_usage "$_keywords" 4891.24Slukem fi 4901.24Slukem fi 4911.24Slukem 4921.24Slukem eval $_pidcmd # determine the pid if necessary 4931.11Slukem 4941.11Slukem for _elem in $_keywords; do 4951.46Slukem if [ "$_elem" != "$rc_arg" ]; then 4961.11Slukem continue 4971.11Slukem fi 4981.11Slukem 4991.24Slukem # if there's a custom ${XXX_cmd}, 5001.24Slukem # run that instead of the default 5011.24Slukem # 5021.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 5031.46Slukem _postcmd=\$${rc_arg}_postcmd 5041.11Slukem if [ -n "$_cmd" ]; then 5051.24Slukem # if the precmd failed and force 5061.24Slukem # isn't set, exit 5071.24Slukem # 5081.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5091.24Slukem return 1 5101.24Slukem fi 5111.24Slukem 5121.46Slukem if ! eval $_cmd && [ -z "$rc_force" ]; then 5131.44Slukem return 1 5141.44Slukem fi 5151.44Slukem eval $_postcmd 5161.11Slukem return 0 5171.11Slukem fi 5181.11Slukem 5191.46Slukem case "$rc_arg" in # default operations... 5201.11Slukem 5211.11Slukem status) 5221.46Slukem if [ -n "$rc_pid" ]; then 5231.46Slukem echo "${name} is running as pid $rc_pid." 5241.11Slukem else 5251.11Slukem echo "${name} is not running." 5261.28Slukem return 1 5271.11Slukem fi 5281.11Slukem ;; 5291.11Slukem 5301.11Slukem start) 5311.46Slukem if [ -n "$rc_pid" ]; then 5321.63Slukem echo 1>&2 "${name} already running? (pid=$rc_pid)." 5331.11Slukem exit 1 5341.11Slukem fi 5351.11Slukem 5361.57Slukem if [ ! -x ${_chroot}${command} ]; then 5371.11Slukem return 0 5381.11Slukem fi 5391.11Slukem 5401.24Slukem # check for required variables, 5411.24Slukem # directories, and files 5421.24Slukem # 5431.11Slukem for _f in $required_vars; do 5441.11Slukem if ! checkyesno $_f; then 5451.65Slukem warn "\$${_f} is not enabled." 5461.46Slukem if [ -z "$rc_force" ]; then 5471.24Slukem return 1 5481.24Slukem fi 5491.11Slukem fi 5501.11Slukem done 5511.11Slukem for _f in $required_dirs; do 5521.11Slukem if [ ! -d "${_f}/." ]; then 5531.25Slukem warn "${_f} is not a directory." 5541.46Slukem if [ -z "$rc_force" ]; then 5551.24Slukem return 1 5561.24Slukem fi 5571.11Slukem fi 5581.11Slukem done 5591.11Slukem for _f in $required_files; do 5601.11Slukem if [ ! -r "${_f}" ]; then 5611.25Slukem warn "${_f} is not readable." 5621.46Slukem if [ -z "$rc_force" ]; then 5631.24Slukem return 1 5641.24Slukem fi 5651.11Slukem fi 5661.11Slukem done 5671.11Slukem 5681.24Slukem # if the precmd failed and force 5691.24Slukem # isn't set, exit 5701.24Slukem # 5711.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5721.24Slukem return 1 5731.24Slukem fi 5741.24Slukem 5751.24Slukem # setup the command to run, and run it 5761.29Slukem # 5771.11Slukem echo "Starting ${name}." 5781.22Slukem if [ -n "$_chroot" ]; then 5791.22Slukem _doit="\ 5801.72She${_env:+env $_env }\ 5811.23Slukem${_nice:+nice -n $_nice }\ 5821.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 5831.46Slukem$_chroot $command $rc_flags $command_args" 5841.22Slukem else 5851.22Slukem _doit="\ 5861.18Slukem${_chdir:+cd $_chdir; }\ 5871.72She${_env:+env $_env }\ 5881.18Slukem${_nice:+nice -n $_nice }\ 5891.46Slukem$command $rc_flags $command_args" 5901.34Slukem if [ -n "$_user" ]; then 5911.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 5921.34Slukem fi 5931.22Slukem fi 5941.43Slukem 5951.43Slukem # if the cmd failed and force 5961.43Slukem # isn't set, exit 5971.43Slukem # 5981.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 5991.43Slukem return 1 6001.43Slukem fi 6011.43Slukem 6021.43Slukem # finally, run postcmd 6031.43Slukem # 6041.43Slukem eval $_postcmd 6051.11Slukem ;; 6061.11Slukem 6071.11Slukem stop) 6081.46Slukem if [ -z "$rc_pid" ]; then 6091.24Slukem if [ -n "$pidfile" ]; then 6101.63Slukem echo 1>&2 \ 6111.24Slukem "${name} not running? (check $pidfile)." 6121.24Slukem else 6131.63Slukem echo 1>&2 "${name} not running?" 6141.11Slukem fi 6151.24Slukem exit 1 6161.11Slukem fi 6171.11Slukem 6181.43Slukem # if the precmd failed and force 6191.43Slukem # isn't set, exit 6201.43Slukem # 6211.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6221.24Slukem return 1 6231.24Slukem fi 6241.43Slukem 6251.43Slukem # send the signal to stop 6261.43Slukem # 6271.11Slukem echo "Stopping ${name}." 6281.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 6291.34Slukem if [ -n "$_user" ]; then 6301.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6311.34Slukem fi 6321.43Slukem 6331.43Slukem # if the stop cmd failed and force 6341.43Slukem # isn't set, exit 6351.43Slukem # 6361.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6371.43Slukem return 1 6381.43Slukem fi 6391.43Slukem 6401.43Slukem # wait for the command to exit, 6411.43Slukem # and run postcmd. 6421.46Slukem wait_for_pids $rc_pid 6431.43Slukem eval $_postcmd 6441.11Slukem ;; 6451.11Slukem 6461.11Slukem reload) 6471.46Slukem if [ -z "$rc_pid" ]; then 6481.24Slukem if [ -n "$pidfile" ]; then 6491.63Slukem echo 1>&2 \ 6501.11Slukem "${name} not running? (check $pidfile)." 6511.24Slukem else 6521.63Slukem echo 1>&2 "${name} not running?" 6531.11Slukem fi 6541.24Slukem exit 1 6551.11Slukem fi 6561.11Slukem echo "Reloading ${name} config files." 6571.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6581.24Slukem return 1 6591.24Slukem fi 6601.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 6611.34Slukem if [ -n "$_user" ]; then 6621.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6631.34Slukem fi 6641.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6651.43Slukem return 1 6661.43Slukem fi 6671.43Slukem eval $_postcmd 6681.11Slukem ;; 6691.11Slukem 6701.11Slukem restart) 6711.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6721.24Slukem return 1 6731.11Slukem fi 6741.29Slukem # prevent restart being called more 6751.29Slukem # than once by any given script 6761.29Slukem # 6771.53Slukem if ${_rc_restart_done:-false}; then 6781.29Slukem return 0 6791.29Slukem fi 6801.53Slukem _rc_restart_done=true 6811.33Slukem 6821.61Slukem ( $0 ${_rc_prefix}stop ) 6831.61Slukem $0 ${_rc_prefix}start 6841.11Slukem 6851.43Slukem eval $_postcmd 6861.33Slukem ;; 6871.33Slukem 6881.33Slukem poll) 6891.46Slukem if [ -n "$rc_pid" ]; then 6901.46Slukem wait_for_pids $rc_pid 6911.33Slukem fi 6921.11Slukem ;; 6931.11Slukem 6941.11Slukem rcvar) 6951.11Slukem echo "# $name" 6961.24Slukem if [ -n "$rcvar" ]; then 6971.24Slukem if checkyesno ${rcvar}; then 6981.24Slukem echo "\$${rcvar}=YES" 6991.24Slukem else 7001.24Slukem echo "\$${rcvar}=NO" 7011.24Slukem fi 7021.11Slukem fi 7031.11Slukem ;; 7041.11Slukem 7051.11Slukem *) 7061.11Slukem rc_usage "$_keywords" 7071.11Slukem ;; 7081.11Slukem 7091.11Slukem esac 7101.11Slukem return 0 7111.11Slukem done 7121.11Slukem 7131.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 7141.11Slukem rc_usage "$_keywords" 7151.11Slukem exit 1 7161.11Slukem} 7171.11Slukem 7181.11Slukem# 7191.11Slukem# run_rc_script file arg 7201.11Slukem# Start the script `file' with `arg', and correctly handle the 7211.17Slukem# return value from the script. If `file' ends with `.sh', it's 7221.37Slukem# sourced into the current environment. If `file' appears to be 7231.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 7241.37Slukem# executable run as a child process. 7251.17Slukem# 7261.11Slukemrun_rc_script() 7271.11Slukem{ 7281.11Slukem _file=$1 7291.11Slukem _arg=$2 7301.11Slukem if [ -z "$_file" -o -z "$_arg" ]; then 7311.11Slukem err 3 'USAGE: run_rc_script file arg' 7321.11Slukem fi 7331.11Slukem 7341.74Ssalo _run_rc_script=true 7351.74Ssalo 7361.45Slukem unset name command command_args command_interpreter \ 7371.45Slukem extra_commands pidfile procname \ 7381.42Slukem rcvar required_dirs required_files required_vars 7391.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 7401.39Slukem 7411.39Slukem case "$_file" in 7421.39Slukem *.sh) # run in current shell 7431.11Slukem set $_arg ; . $_file 7441.39Slukem ;; 7451.60Slukem *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 7461.39Slukem warn "Ignoring scratch file $_file" 7471.39Slukem ;; 7481.39Slukem *) # run in subshell 7491.39Slukem if [ -x $_file ]; then 7501.39Slukem if [ -n "$rc_fast_and_loose" ]; then 7511.39Slukem set $_arg ; . $_file 7521.39Slukem else 7531.37Slukem ( set $_arg ; . $_file ) 7541.37Slukem fi 7551.39Slukem fi 7561.39Slukem ;; 7571.39Slukem esac 7581.11Slukem} 7591.19Slukem 7601.19Slukem# 7611.65Slukem# load_rc_config command 7621.19Slukem# Source in the configuration file for a given command. 7631.19Slukem# 7641.19Slukemload_rc_config() 7651.19Slukem{ 7661.19Slukem _command=$1 7671.19Slukem if [ -z "$_command" ]; then 7681.19Slukem err 3 'USAGE: load_rc_config command' 7691.19Slukem fi 7701.19Slukem 7711.54Slukem if ${_rc_conf_loaded:-false}; then 7721.54Slukem : 7731.54Slukem else 7741.30Slukem . /etc/rc.conf 7751.53Slukem _rc_conf_loaded=true 7761.30Slukem fi 7771.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 7781.20Sfvdl . /etc/rc.conf.d/"$_command" 7791.20Sfvdl fi 7801.19Slukem} 7811.19Slukem 7821.65Slukem# 7831.65Slukem# load_rc_config_var cmd var 7841.65Slukem# Read the rc.conf(5) var for cmd and set in the 7851.65Slukem# current shell, using load_rc_config in a subshell to prevent 7861.65Slukem# unwanted side effects from other variable assignments. 7871.65Slukem# 7881.65Slukemload_rc_config_var() 7891.65Slukem{ 7901.65Slukem if [ $# -ne 2 ]; then 7911.65Slukem err 3 'USAGE: load_rc_config_var cmd var' 7921.65Slukem fi 7931.65Slukem eval $(eval '( 7941.65Slukem load_rc_config '$1' >/dev/null; 7951.65Slukem if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 7961.65Slukem echo '$2'=\'\''${'$2'}\'\''; 7971.65Slukem fi 7981.65Slukem )' ) 7991.65Slukem} 8001.11Slukem 8011.11Slukem# 8021.11Slukem# rc_usage commands 8031.11Slukem# Print a usage string for $0, with `commands' being a list of 8041.11Slukem# valid commands. 8051.11Slukem# 8061.11Slukemrc_usage() 8071.11Slukem{ 8081.61Slukem echo -n 1>&2 "Usage: $0 [fast|force|one](" 8091.11Slukem 8101.11Slukem _sep= 8111.56Schristos for _elem; do 8121.11Slukem echo -n 1>&2 "$_sep$_elem" 8131.11Slukem _sep="|" 8141.11Slukem done 8151.11Slukem echo 1>&2 ")" 8161.11Slukem exit 1 8171.11Slukem} 8181.11Slukem 8191.11Slukem# 8201.11Slukem# err exitval message 8211.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 8221.11Slukem# 8231.11Slukemerr() 8241.11Slukem{ 8251.11Slukem exitval=$1 8261.11Slukem shift 8271.11Slukem 8281.51Sgrant if [ -x /usr/bin/logger ]; then 8291.51Sgrant logger "$0: ERROR: $*" 8301.51Sgrant fi 8311.21Slukem echo 1>&2 "$0: ERROR: $*" 8321.11Slukem exit $exitval 8331.11Slukem} 8341.11Slukem 8351.11Slukem# 8361.11Slukem# warn message 8371.11Slukem# Display message to stderr and log to the syslog. 8381.11Slukem# 8391.11Slukemwarn() 8401.11Slukem{ 8411.51Sgrant if [ -x /usr/bin/logger ]; then 8421.51Sgrant logger "$0: WARNING: $*" 8431.51Sgrant fi 8441.21Slukem echo 1>&2 "$0: WARNING: $*" 8451.31Satatat} 8461.31Satatat 8471.31Satatat# 8481.31Satatat# backup_file action file cur backup 8491.31Satatat# Make a backup copy of `file' into `cur', and save the previous 8501.31Satatat# version of `cur' as `backup' or use rcs for archiving. 8511.31Satatat# 8521.31Satatat# This routine checks the value of the backup_uses_rcs variable, 8531.31Satatat# which can be either YES or NO. 8541.31Satatat# 8551.31Satatat# The `action' keyword can be one of the following: 8561.31Satatat# 8571.31Satatat# add `file' is now being backed up (and is possibly 8581.31Satatat# being reentered into the backups system). `cur' 8591.31Satatat# is created and RCS files, if necessary, are 8601.31Satatat# created as well. 8611.31Satatat# 8621.31Satatat# update `file' has changed and needs to be backed up. 8631.31Satatat# If `cur' exists, it is copied to to `back' or 8641.31Satatat# checked into RCS (if the repository file is old), 8651.31Satatat# and then `file' is copied to `cur'. Another RCS 8661.31Satatat# check in done here if RCS is being used. 8671.31Satatat# 8681.31Satatat# remove `file' is no longer being tracked by the backups 8691.31Satatat# system. If RCS is not being used, `cur' is moved 8701.31Satatat# to `back', otherwise an empty file is checked in, 8711.31Satatat# and then `cur' is removed. 8721.31Satatat# 8731.31Satatat# 8741.31Satatatbackup_file() 8751.31Satatat{ 8761.31Satatat _action=$1 8771.31Satatat _file=$2 8781.31Satatat _cur=$3 8791.31Satatat _back=$4 8801.31Satatat 8811.31Satatat if checkyesno backup_uses_rcs; then 8821.31Satatat _msg0="backup archive" 8831.31Satatat _msg1="update" 8841.31Satatat 8851.36Satatat # ensure that history file is not locked 8861.36Satatat if [ -f $_cur,v ]; then 8871.36Satatat rcs -q -u -U -M $_cur 8881.36Satatat fi 8891.36Satatat 8901.31Satatat # ensure after switching to rcs that the 8911.31Satatat # current backup is not lost 8921.31Satatat if [ -f $_cur ]; then 8931.31Satatat # no archive, or current newer than archive 8941.31Satatat if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 8951.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 8961.36Satatat rcs -q -kb -U $_cur 8971.49Slukem co -q -f -u $_cur 8981.31Satatat fi 8991.31Satatat fi 9001.31Satatat 9011.31Satatat case $_action in 9021.31Satatat add|update) 9031.31Satatat cp -p $_file $_cur 9041.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 9051.36Satatat rcs -q -kb -U $_cur 9061.49Slukem co -q -f -u $_cur 9071.31Satatat chown root:wheel $_cur $_cur,v 9081.31Satatat ;; 9091.31Satatat remove) 9101.31Satatat cp /dev/null $_cur 9111.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 9121.36Satatat rcs -q -kb -U $_cur 9131.31Satatat chown root:wheel $_cur $_cur,v 9141.31Satatat rm $_cur 9151.31Satatat ;; 9161.31Satatat esac 9171.31Satatat else 9181.31Satatat case $_action in 9191.31Satatat add|update) 9201.31Satatat if [ -f $_cur ]; then 9211.31Satatat cp -p $_cur $_back 9221.31Satatat fi 9231.31Satatat cp -p $_file $_cur 9241.31Satatat chown root:wheel $_cur 9251.31Satatat ;; 9261.31Satatat remove) 9271.31Satatat mv -f $_cur $_back 9281.31Satatat ;; 9291.31Satatat esac 9301.31Satatat fi 9311.1Scjs} 9321.64Smycroft 9331.64Smycroft_rc_subr_loaded=: 934