rc.subr revision 109582
198186Sgordon# $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 109582 2003-01-20 19:30:15Z mtm $ 378344Sobrien# 498186Sgordon# Copyright (c) 1997-2002 The NetBSD Foundation, Inc. 578344Sobrien# All rights reserved. 678344Sobrien# 778344Sobrien# This code is derived from software contributed to The NetBSD Foundation 878344Sobrien# by Luke Mewburn. 978344Sobrien# 1078344Sobrien# Redistribution and use in source and binary forms, with or without 1178344Sobrien# modification, are permitted provided that the following conditions 1278344Sobrien# are met: 1378344Sobrien# 1. Redistributions of source code must retain the above copyright 1478344Sobrien# notice, this list of conditions and the following disclaimer. 1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1678344Sobrien# notice, this list of conditions and the following disclaimer in the 1778344Sobrien# documentation and/or other materials provided with the distribution. 1878344Sobrien# 3. All advertising materials mentioning features or use of this software 1978344Sobrien# must display the following acknowledgement: 2078344Sobrien# This product includes software developed by the NetBSD 2178344Sobrien# Foundation, Inc. and its contributors. 2278344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its 2378344Sobrien# contributors may be used to endorse or promote products derived 2478344Sobrien# from this software without specific prior written permission. 2578344Sobrien# 2678344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2778344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2878344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2978344Sobrien# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3078344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3178344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3278344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3378344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3478344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3578344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3678344Sobrien# POSSIBILITY OF SUCH DAMAGE. 3778344Sobrien# 3878344Sobrien# rc.subr 3978344Sobrien# functions used by various rc scripts 4078344Sobrien# 4178344Sobrien 4278344Sobrien# 4398186Sgordon# Operating System dependent/independent variables 4498186Sgordon# 4598186Sgordon 4698186SgordonSYSCTL="/sbin/sysctl" 4798186SgordonSYSCTL_N="${SYSCTL} -n" 4898186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype" 49103018SgordonOSTYPE=`${CMD_OSTYPE}` 5098186Sgordon 51103018Sgordoncase ${OSTYPE} in 5298186SgordonFreeBSD) 5398186Sgordon SYSCTL_W="${SYSCTL}" 5498186Sgordon ;; 5598186SgordonNetBSD) 5698186Sgordon SYSCTL_W="${SYSCTL} -w" 5798186Sgordon ;; 5898186Sgordonesac 5998186Sgordon 6098186Sgordon# 6178344Sobrien# functions 6278344Sobrien# --------- 6378344Sobrien 6478344Sobrien# 6598186Sgordon# set_rcvar base_var 6698186Sgordon# Set the variable name enabling a specific service. 6798186Sgordon# FreeBSD uses ${service}_enable, while NetBSD uses 6898186Sgordon# just the name of the service. For example: 6998186Sgordon# FreeBSD: sendmail_enable="YES" 7098186Sgordon# NetBSD : sendmail="YES" 7198186Sgordon# $1 - if $name is not the base to work of off, specify 7298186Sgordon# a different one 7398186Sgordon# 7498186Sgordonset_rcvar() 7598186Sgordon{ 7698186Sgordon if [ -z "$1" ]; then 7798186Sgordon base_var=${name} 7898186Sgordon else 7998186Sgordon base_var="$1" 8098186Sgordon fi 8198186Sgordon 82103018Sgordon case ${OSTYPE} in 8398186Sgordon FreeBSD) 8498186Sgordon echo ${base_var}_enable 8598186Sgordon ;; 8698186Sgordon NetBSD) 8798186Sgordon echo ${base_var} 8898186Sgordon ;; 8998186Sgordon *) 9098186Sgordon echo 'XXX' 9198186Sgordon ;; 9298186Sgordon esac 9398186Sgordon} 9498186Sgordon 9598186Sgordon# 9698186Sgordon# force_depend script 9798186Sgordon# Force a service to start. Intended for use by services 9898186Sgordon# to resolve dependency issues. It is assumed the caller 9998186Sgordon# has check to make sure this call is necessary 10098186Sgordon# $1 - filename of script, in /etc/rc.d, to run 10198186Sgordon# 10298186Sgordonforce_depend() 10398186Sgordon{ 10498186Sgordon _depend="$1" 10598186Sgordon 10698186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 10798186Sgordon if ! /etc/rc.d/${_depend} forcestart ; then 10898186Sgordon warn "Unable to force ${_depend}. It may already be running." 10998186Sgordon return 1 11098186Sgordon fi 11198186Sgordon return 0 11298186Sgordon} 11398186Sgordon 11498186Sgordon# 11578344Sobrien# checkyesno var 11678344Sobrien# Test $1 variable, and warn if not set to YES or NO. 11778344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 11878344Sobrien# 11978344Sobriencheckyesno() 12078344Sobrien{ 12178344Sobrien eval _value=\$${1} 12298186Sgordon debug "checkyesno: $1 is set to $_value." 12378344Sobrien case $_value in 12478344Sobrien 12578344Sobrien # "yes", "true", "on", or "1" 12678344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 12778344Sobrien return 0 12878344Sobrien ;; 12978344Sobrien 13078344Sobrien # "no", "false", "off", or "0" 13178344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 13278344Sobrien return 1 13378344Sobrien ;; 13478344Sobrien *) 135106643Sgordon warn "\$${1} is not set properly - see rc.conf(5)." 13678344Sobrien return 1 13778344Sobrien ;; 13878344Sobrien esac 13978344Sobrien} 14078344Sobrien 14198186Sgordon# reverse_list list 14298186Sgordon# print the list in reverse order 14378344Sobrien# 14498186Sgordonreverse_list() 14598186Sgordon{ 14698186Sgordon _revlist= 14798186Sgordon for _revfile in $*; do 14898186Sgordon _revlist="$_revfile $_revlist" 14998186Sgordon done 15098186Sgordon echo $_revlist 15198186Sgordon} 15298186Sgordon 15378344Sobrien# 15498186Sgordon# mount_critical_filesystems type 15598186Sgordon# Go through the list of critical filesystems as provided in 15698186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 15798186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 15898186Sgordon# 15978344Sobrienmount_critical_filesystems() 16078344Sobrien{ 16198186Sgordon eval _fslist=\$critical_filesystems_${1} 16278344Sobrien for _fs in $_fslist; do 16378344Sobrien mount | ( 16478344Sobrien _ismounted=no 16578344Sobrien while read what _on on _type type; do 16678344Sobrien if [ $on = $_fs ]; then 16778344Sobrien _ismounted=yes 16878344Sobrien fi 16978344Sobrien done 17098186Sgordon if [ $_ismounted = no ]; then 17178344Sobrien mount $_fs >/dev/null 2>&1 17278344Sobrien fi 17398186Sgordon ) 17478344Sobrien done 17578344Sobrien} 17678344Sobrien 17778344Sobrien# 17898186Sgordon# check_pidfile pidfile procname [interpreter] 17998186Sgordon# Parses the first line of pidfile for a PID, and ensures 18078344Sobrien# that the process is running and matches procname. 18198186Sgordon# Prints the matching PID upon success, nothing otherwise. 18298186Sgordon# interpreter is optional; see _find_processes() for details. 18378344Sobrien# 18478344Sobriencheck_pidfile() 18578344Sobrien{ 18678344Sobrien _pidfile=$1 18778344Sobrien _procname=$2 18898186Sgordon _interpreter=$3 18978344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 19098186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 19178344Sobrien fi 19278344Sobrien if [ ! -f $_pidfile ]; then 19398186Sgordon debug "pid file {$_pidfile): not readable." 19478344Sobrien return 19578344Sobrien fi 19678344Sobrien read _pid _junk < $_pidfile 19778344Sobrien if [ -z "$_pid" ]; then 19898186Sgordon debug "pid file {$_pidfile): no pid in file." 19978344Sobrien return 20078344Sobrien fi 20198186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 20278344Sobrien} 20378344Sobrien 20478344Sobrien# 20598186Sgordon# check_process procname [interpreter] 20678344Sobrien# Ensures that a process (or processes) named procname is running. 20798186Sgordon# Prints a list of matching PIDs. 20898186Sgordon# interpreter is optional; see _find_processes() for details. 20978344Sobrien# 21078344Sobriencheck_process() 21178344Sobrien{ 21278344Sobrien _procname=$1 21398186Sgordon _interpreter=$2 21478344Sobrien if [ -z "$_procname" ]; then 21598186Sgordon err 3 'USAGE: check_process procname [interpreter]' 21678344Sobrien fi 21798186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 21898186Sgordon} 21998186Sgordon 22098186Sgordon# 22198186Sgordon# _find_processes procname interpreter psargs 22298186Sgordon# Search for procname in the output of ps generated by psargs. 22398186Sgordon# Prints the PIDs of any matching processes, space separated. 22498186Sgordon# 22598186Sgordon# If interpreter == ".", check the following variations of procname 22698186Sgordon# against the first word of each command: 22798186Sgordon# procname 22898186Sgordon# `basename procname` 22998186Sgordon# `basename procname` + ":" 23098186Sgordon# "(" + `basename procname` + ")" 23198186Sgordon# 23298186Sgordon# If interpreter != ".", read the first line of procname, remove the 23398186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 23498186Sgordon# match that against each command, either as is, or with extra words 23598186Sgordon# at the end. 23698186Sgordon# 23798186Sgordon_find_processes() 23898186Sgordon{ 23998186Sgordon if [ $# -ne 3 ]; then 24098186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 24198186Sgordon fi 24298186Sgordon _procname=$1 24398186Sgordon _interpreter=$2 24498186Sgordon _psargs=$3 24598186Sgordon 24678344Sobrien _pref= 24798186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 24898186Sgordon read _interp < $_procname # read interpreter name 24998186Sgordon _interp=${_interp#\#!} # strip #! 25098186Sgordon set -- $_interp 25198186Sgordon if [ $_interpreter != $1 ]; then 25298186Sgordon warn "\$command_interpreter $_interpreter != $1" 25378344Sobrien fi 25498186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 25598186Sgordon _fp_args='_argv' 25698186Sgordon _fp_match='case "$_argv" in 25798186Sgordon ${_interp}|"${_interp} "*)' 25898186Sgordon else # a normal daemon 25998186Sgordon _procnamebn=${_procname##*/} 26098186Sgordon _fp_args='_arg0 _argv' 26198186Sgordon _fp_match='case "$_arg0" in 26298186Sgordon $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 26398186Sgordon fi 26498186Sgordon 26598186Sgordon _proccheck=' 26698186Sgordon ps -o "pid,command" '"$_psargs"' | 26798186Sgordon while read _npid '"$_fp_args"'; do 26898186Sgordon case "$_npid" in 26998186Sgordon PID) 27098186Sgordon continue ;; 27198186Sgordon esac ; '"$_fp_match"' 27298186Sgordon echo -n "$_pref$_npid" ; 27398186Sgordon _pref=" " 27498186Sgordon ;; 27598186Sgordon esac 27698186Sgordon done' 27798186Sgordon 27898186Sgordon debug "in _find_processes: proccheck is ($_proccheck)." 27998186Sgordon eval $_proccheck 28098186Sgordon} 28198186Sgordon 28298186Sgordon# 28398186Sgordon# wait_for_pids pid [pid ...] 28498186Sgordon# spins until none of the pids exist 28598186Sgordon# 28698186Sgordonwait_for_pids() 28798186Sgordon{ 28898186Sgordon _list=$* 28998186Sgordon if [ -z "$_list" ]; then 29098186Sgordon return 29198186Sgordon fi 29298186Sgordon _prefix= 29398186Sgordon while true; do 29498186Sgordon _nlist=""; 29598186Sgordon for _j in $_list; do 29698186Sgordon if kill -0 $_j 2>/dev/null; then 29798186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 29898186Sgordon fi 29998186Sgordon done 30098186Sgordon if [ -z "$_nlist" ]; then 30198186Sgordon break 30278344Sobrien fi 30398186Sgordon _list=$_nlist 30498186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 30598186Sgordon _prefix=", " 30698186Sgordon sleep 2 30778344Sobrien done 30898186Sgordon if [ -n "$_prefix" ]; then 30998186Sgordon echo "." 31098186Sgordon fi 31178344Sobrien} 31278344Sobrien 31378344Sobrien# 31498186Sgordon# run_rc_command argument 31598186Sgordon# Search for argument in the list of supported commands, which is: 31698186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 31798186Sgordon# If there's a match, run ${argument}_cmd or the default method 31898186Sgordon# (see below). 31978344Sobrien# 32098186Sgordon# If argument has a given prefix, then change the operation as follows: 32198186Sgordon# Prefix Operation 32278344Sobrien# ------ --------- 32398186Sgordon# fast Skip the pid check, and set rc_fast=yes 32498186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 32578344Sobrien# 32678344Sobrien# The following globals are used: 32778344Sobrien# 32898186Sgordon# Name Needed Purpose 32998186Sgordon# ---- ------ ------- 33078344Sobrien# name y Name of script. 33178344Sobrien# 33278344Sobrien# command n Full path to command. 33398186Sgordon# Not needed if ${rc_arg}_cmd is set for 33478344Sobrien# each keyword. 33578344Sobrien# 33678344Sobrien# command_args n Optional args/shell directives for command. 33778344Sobrien# 33898186Sgordon# command_interpreter n If not empty, command is interpreted, so 33998186Sgordon# call check_{pidfile,process}() appropriately. 34098186Sgordon# 34178344Sobrien# extra_commands n List of extra commands supported. 34278344Sobrien# 34398186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 34498186Sgordon# otherwise use check_process $command. 34598186Sgordon# In either case, only check if $command is set. 34678344Sobrien# 34798186Sgordon# procname n Process name to check for instead of $command. 34898186Sgordon# 34978344Sobrien# rcvar n This is checked with checkyesno to determine 35078344Sobrien# if the action should be run. 35178344Sobrien# 35278344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 35398186Sgordon# Requires /usr to be mounted. 35478344Sobrien# 35578344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 35678344Sobrien# (if not using ${name}_chroot). 35778344Sobrien# 35878344Sobrien# ${name}_flags n Arguments to call ${command} with. 35978344Sobrien# NOTE: $flags from the parent environment 36078344Sobrien# can be used to override this. 36178344Sobrien# 36278344Sobrien# ${name}_nice n Nice level to run ${command} at. 36378344Sobrien# 36478344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 36578344Sobrien# using ${name}_chroot. 36698186Sgordon# Requires /usr to be mounted. 36778344Sobrien# 36878344Sobrien# ${name}_group n Group to run chrooted ${command} as. 36998186Sgordon# Requires /usr to be mounted. 37078344Sobrien# 37198186Sgordon# ${name}_groups n Comma separated list of supplementary groups 37298186Sgordon# to run the chrooted ${command} with. 37398186Sgordon# Requires /usr to be mounted. 37478344Sobrien# 37598186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 37678344Sobrien# Otherwise, use default command (see below) 37778344Sobrien# 37898186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 37998186Sgordon# ${rc_arg}_cmd method in the default 38098186Sgordon# operation (i.e, after checking for required 38198186Sgordon# bits and process (non)existence). 38278344Sobrien# If this completes with a non-zero exit code, 38398186Sgordon# don't run ${rc_arg}_cmd. 38478344Sobrien# 38598186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 38698186Sgordon# ${rc_arg}_cmd method, if that method 38798186Sgordon# returned a zero exit code. 38898186Sgordon# 38978344Sobrien# required_dirs n If set, check for the existence of the given 39078344Sobrien# directories before running the default 39178344Sobrien# (re)start command. 39278344Sobrien# 39378344Sobrien# required_files n If set, check for the readability of the given 39478344Sobrien# files before running the default (re)start 39578344Sobrien# command. 39678344Sobrien# 39778344Sobrien# required_vars n If set, perform checkyesno on each of the 39878344Sobrien# listed variables before running the default 39978344Sobrien# (re)start command. 40078344Sobrien# 40198186Sgordon# Default behaviour for a given argument, if no override method is 40298186Sgordon# provided: 40378344Sobrien# 40498186Sgordon# Argument Default behaviour 40598186Sgordon# -------- ----------------- 40678344Sobrien# start if !running && checkyesno ${rcvar} 40778344Sobrien# ${command} 40878344Sobrien# 40978344Sobrien# stop if ${pidfile} 41098186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 41178344Sobrien# else 41298186Sgordon# rc_pid=$(check_process $command) 41398186Sgordon# kill $sig_stop $rc_pid 41498186Sgordon# wait_for_pids $rc_pid 41598186Sgordon# ($sig_stop defaults to TERM.) 41678344Sobrien# 41798186Sgordon# reload Similar to stop, except use $sig_reload instead, 41898186Sgordon# and doesn't wait_for_pids. 41978344Sobrien# $sig_reload defaults to HUP. 42078344Sobrien# 42178344Sobrien# restart Run `stop' then `start'. 42278344Sobrien# 42398186Sgordon# status Show if ${command} is running, etc. 42478344Sobrien# 42598186Sgordon# poll Wait for ${command} to exit. 42698186Sgordon# 42798186Sgordon# rcvar Display what rc.conf variable is used (if any). 42898186Sgordon# 42998186Sgordon# Variables available to methods, and after run_rc_command() has 43098186Sgordon# completed: 43198186Sgordon# 43298186Sgordon# Variable Purpose 43398186Sgordon# -------- ------- 43498186Sgordon# rc_arg Argument to command, after fast/force processing 43598186Sgordon# performed 43698186Sgordon# 43798186Sgordon# rc_flags Flags to start the default command with. 43898186Sgordon# Defaults to ${name}_flags, unless overridden 43998186Sgordon# by $flags from the environment. 44098186Sgordon# This variable may be changed by the precmd method. 44198186Sgordon# 44298186Sgordon# rc_pid PID of command (if appropriate) 44398186Sgordon# 44498186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 44598186Sgordon# 44698186Sgordon# rc_force Not empty if "force" was provided (q.v.) 44798186Sgordon# 44898186Sgordon# 44978344Sobrienrun_rc_command() 45078344Sobrien{ 45198186Sgordon rc_arg=$1 45278344Sobrien if [ -z "$name" ]; then 45398186Sgordon err 3 'run_rc_command: $name is not set.' 45478344Sobrien fi 45578344Sobrien 45698186Sgordon case "$rc_arg" in 45778344Sobrien fast*) # "fast" prefix; don't check pid 45898186Sgordon rc_arg=${rc_arg#fast} 45998186Sgordon rc_fast=yes 46078344Sobrien ;; 46178344Sobrien force*) # "force prefix; always start 46298186Sgordon rc_arg=${rc_arg#force} 46398186Sgordon rc_force=yes 46478344Sobrien if [ -n "${rcvar}" ]; then 46578344Sobrien eval ${rcvar}=YES 46678344Sobrien fi 46778344Sobrien ;; 46878344Sobrien esac 46978344Sobrien 47098186Sgordon eval _overide_command=\$${name}_program 47198186Sgordon if [ -n "$_overide_command" ]; then 47298186Sgordon command=$_overide_command 47398186Sgordon fi 47498186Sgordon 47578344Sobrien _keywords="start stop restart rcvar $extra_commands" 47698186Sgordon rc_pid= 47778344Sobrien _pidcmd= 47898186Sgordon _procname=${procname:-${command}} 47998186Sgordon 48078344Sobrien # setup pid check command if not fast 48198186Sgordon if [ -z "$rc_fast" -a -n "$_procname" ]; then 48278344Sobrien if [ -n "$pidfile" ]; then 48398186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 48498186Sgordon else 48598186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 48678344Sobrien fi 48778344Sobrien if [ -n "$_pidcmd" ]; then 48898186Sgordon _keywords="${_keywords} status poll" 48978344Sobrien fi 49078344Sobrien fi 49178344Sobrien 49298186Sgordon if [ -z "$rc_arg" ]; then 49378344Sobrien rc_usage "$_keywords" 49478344Sobrien fi 49578344Sobrien 49678344Sobrien if [ -n "$flags" ]; then # allow override from environment 49798186Sgordon rc_flags=$flags 49878344Sobrien else 49998186Sgordon eval rc_flags=\$${name}_flags 50078344Sobrien fi 50198186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 50298186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 50398186Sgordon _group=\$${name}_group _groups=\$${name}_groups 50478344Sobrien 50598186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 50698186Sgordon if [ "$_user" = "$(id -un)" ]; then 50798186Sgordon unset _user 50898186Sgordon fi 50998186Sgordon fi 51098186Sgordon 51178344Sobrien # if ${rcvar} is set, and $1 is not 51298186Sgordon # "rcvar", then run 51378344Sobrien # checkyesno ${rcvar} 51478344Sobrien # and return if that failed 51578344Sobrien # 51698186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 51778344Sobrien if ! checkyesno ${rcvar}; then 51878344Sobrien return 0 51978344Sobrien fi 52078344Sobrien fi 52178344Sobrien 52278344Sobrien eval $_pidcmd # determine the pid if necessary 52378344Sobrien 52478344Sobrien for _elem in $_keywords; do 52598186Sgordon if [ "$_elem" != "$rc_arg" ]; then 52678344Sobrien continue 52778344Sobrien fi 52878344Sobrien 52978344Sobrien # if there's a custom ${XXX_cmd}, 53078344Sobrien # run that instead of the default 53178344Sobrien # 53298186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 53398186Sgordon _postcmd=\$${rc_arg}_postcmd 53478344Sobrien if [ -n "$_cmd" ]; then 53578344Sobrien # if the precmd failed and force 53678344Sobrien # isn't set, exit 53778344Sobrien # 538109582Smtm [ -n "$_precmd" ] && 539109582Smtm debug "run_rc_command: evaluating ${_precmd}()." 54098186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 54178344Sobrien return 1 54278344Sobrien fi 54378344Sobrien 544109582Smtm [ -n "$_cmd" ] && 545109582Smtm debug "run_rc_command: evaluating ${_cmd}()." 54698186Sgordon if ! eval $_cmd && [ -z "$rc_force" ]; then 54798186Sgordon return 1 54898186Sgordon fi 549109582Smtm 550109582Smtm [ -n "$_postcmd" ] && 551109582Smtm debug "run_rc_command: evaluating ${_postcmd}()." 55298186Sgordon eval $_postcmd 55378344Sobrien return 0 55478344Sobrien fi 55578344Sobrien 55698186Sgordon case "$rc_arg" in # default operations... 55778344Sobrien 55878344Sobrien status) 55998186Sgordon if [ -n "$rc_pid" ]; then 56098186Sgordon echo "${name} is running as pid $rc_pid." 56178344Sobrien else 56278344Sobrien echo "${name} is not running." 56378344Sobrien return 1 56478344Sobrien fi 56578344Sobrien ;; 56678344Sobrien 56778344Sobrien start) 56898186Sgordon if [ -n "$rc_pid" ]; then 56998186Sgordon echo "${name} already running? (pid=$rc_pid)." 57078344Sobrien exit 1 57178344Sobrien fi 57278344Sobrien 57378344Sobrien if [ ! -x $command ]; then 57498186Sgordon info "run_rc_command: cannot run ($command)." 57578344Sobrien return 0 57678344Sobrien fi 57778344Sobrien 57878344Sobrien # check for required variables, 57978344Sobrien # directories, and files 58078344Sobrien # 58178344Sobrien for _f in $required_vars; do 58278344Sobrien if ! checkyesno $_f; then 58378344Sobrien warn "\$${_f} is not set." 58498186Sgordon if [ -z "$rc_force" ]; then 58578344Sobrien return 1 58678344Sobrien fi 58778344Sobrien fi 58878344Sobrien done 58978344Sobrien for _f in $required_dirs; do 59078344Sobrien if [ ! -d "${_f}/." ]; then 59178344Sobrien warn "${_f} is not a directory." 59298186Sgordon if [ -z "$rc_force" ]; then 59378344Sobrien return 1 59478344Sobrien fi 59578344Sobrien fi 59678344Sobrien done 59778344Sobrien for _f in $required_files; do 59878344Sobrien if [ ! -r "${_f}" ]; then 59978344Sobrien warn "${_f} is not readable." 60098186Sgordon if [ -z "$rc_force" ]; then 60178344Sobrien return 1 60278344Sobrien fi 60378344Sobrien fi 60478344Sobrien done 60578344Sobrien 60678344Sobrien # if the precmd failed and force 60778344Sobrien # isn't set, exit 60878344Sobrien # 609109582Smtm [ -n "${_precmd}" ] && 610109582Smtm debug "run_rc_command: evaluating ${_precmd}()." 61198186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 61278344Sobrien return 1 61378344Sobrien fi 61478344Sobrien 61578344Sobrien # setup the command to run, and run it 61678344Sobrien # 61778344Sobrien echo "Starting ${name}." 61878344Sobrien if [ -n "$_chroot" ]; then 61978344Sobrien _doit="\ 62078344Sobrien${_nice:+nice -n $_nice }\ 62178344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 62298186Sgordon$_chroot $command $rc_flags $command_args" 62378344Sobrien else 62478344Sobrien _doit="\ 62578344Sobrien${_chdir:+cd $_chdir; }\ 62678344Sobrien${_nice:+nice -n $_nice }\ 62798186Sgordon$command $rc_flags $command_args" 62898186Sgordon if [ -n "$_user" ]; then 62998186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 63098186Sgordon fi 63178344Sobrien fi 63298186Sgordon 63398186Sgordon # if the cmd failed and force 63498186Sgordon # isn't set, exit 63598186Sgordon # 63698186Sgordon debug "run_rc_command: _doit: $_doit" 63798186Sgordon if ! eval $_doit && [ -z "$rc_force" ]; then 63898186Sgordon return 1 63998186Sgordon fi 64098186Sgordon 64198186Sgordon # finally, run postcmd 64298186Sgordon # 643109582Smtm [ -n "${_postcmd}" ] && 644109582Smtm debug "run_rc_command: evaluating ${_postcmd}()." 64598186Sgordon eval $_postcmd 64678344Sobrien ;; 64778344Sobrien 64878344Sobrien stop) 64998186Sgordon if [ -z "$rc_pid" ]; then 65078344Sobrien if [ -n "$pidfile" ]; then 65178344Sobrien echo \ 65278344Sobrien "${name} not running? (check $pidfile)." 65378344Sobrien else 65478344Sobrien echo "${name} not running?" 65578344Sobrien fi 65678344Sobrien exit 1 65778344Sobrien fi 65878344Sobrien 65998186Sgordon # if the precmd failed and force 66098186Sgordon # isn't set, exit 66198186Sgordon # 66298186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 66378344Sobrien return 1 66478344Sobrien fi 66598186Sgordon 66698186Sgordon # send the signal to stop 66798186Sgordon # 66878344Sobrien echo "Stopping ${name}." 66998186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 67098186Sgordon if [ -n "$_user" ]; then 67198186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 67298186Sgordon fi 67398186Sgordon 67498186Sgordon # if the stop cmd failed and force 67598186Sgordon # isn't set, exit 67698186Sgordon # 67798186Sgordon if ! eval $_doit && [ -z "$rc_force" ]; then 67898186Sgordon return 1 67998186Sgordon fi 68098186Sgordon 68198186Sgordon # wait for the command to exit, 68298186Sgordon # and run postcmd. 68398186Sgordon wait_for_pids $rc_pid 68498186Sgordon eval $_postcmd 68578344Sobrien ;; 68678344Sobrien 68778344Sobrien reload) 68898186Sgordon if [ -z "$rc_pid" ]; then 68978344Sobrien if [ -n "$pidfile" ]; then 69078344Sobrien echo \ 69178344Sobrien "${name} not running? (check $pidfile)." 69278344Sobrien else 69378344Sobrien echo "${name} not running?" 69478344Sobrien fi 69578344Sobrien exit 1 69678344Sobrien fi 69778344Sobrien echo "Reloading ${name} config files." 69898186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 69978344Sobrien return 1 70078344Sobrien fi 70198186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 70298186Sgordon if [ -n "$_user" ]; then 70398186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 70498186Sgordon fi 70598186Sgordon if ! eval $_doit && [ -z "$rc_force" ]; then 70698186Sgordon return 1 70798186Sgordon fi 70898186Sgordon eval $_postcmd 70978344Sobrien ;; 71078344Sobrien 71178344Sobrien restart) 71298186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 71378344Sobrien return 1 71478344Sobrien fi 71578344Sobrien # prevent restart being called more 71678344Sobrien # than once by any given script 71778344Sobrien # 71878344Sobrien if [ -n "$_rc_restart_done" ]; then 71978344Sobrien return 0 72078344Sobrien fi 72178344Sobrien _rc_restart_done=YES 72278344Sobrien 72398186Sgordon ( $0 ${rc_force:+force}stop ) 72498186Sgordon $0 ${rc_force:+force}start 72598186Sgordon 72698186Sgordon eval $_postcmd 72778344Sobrien ;; 72878344Sobrien 72998186Sgordon poll) 73098186Sgordon if [ -n "$rc_pid" ]; then 73198186Sgordon wait_for_pids $rc_pid 73298186Sgordon fi 73398186Sgordon ;; 73498186Sgordon 73578344Sobrien rcvar) 73678344Sobrien echo "# $name" 73778344Sobrien if [ -n "$rcvar" ]; then 73878344Sobrien if checkyesno ${rcvar}; then 73978344Sobrien echo "\$${rcvar}=YES" 74078344Sobrien else 74178344Sobrien echo "\$${rcvar}=NO" 74278344Sobrien fi 74378344Sobrien fi 74478344Sobrien ;; 74578344Sobrien 74678344Sobrien *) 74778344Sobrien rc_usage "$_keywords" 74878344Sobrien ;; 74978344Sobrien 75078344Sobrien esac 75178344Sobrien return 0 75278344Sobrien done 75378344Sobrien 75498186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 75578344Sobrien rc_usage "$_keywords" 75678344Sobrien exit 1 75778344Sobrien} 75878344Sobrien 75978344Sobrien# 76078344Sobrien# run_rc_script file arg 76178344Sobrien# Start the script `file' with `arg', and correctly handle the 76278344Sobrien# return value from the script. If `file' ends with `.sh', it's 76398186Sgordon# sourced into the current environment. If `file' appears to be 76498186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 76598186Sgordon# executable run as a child process. 76678344Sobrien# 76778344Sobrienrun_rc_script() 76878344Sobrien{ 76978344Sobrien _file=$1 77078344Sobrien _arg=$2 77178344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 77278344Sobrien err 3 'USAGE: run_rc_script file arg' 77378344Sobrien fi 77478344Sobrien 77598186Sgordon trap "echo 'Reboot interrupted'; exit 1" 3 77698186Sgordon 77798186Sgordon unset name command command_args command_interpreter \ 77898186Sgordon extra_commands pidfile procname \ 77998186Sgordon rcvar required_dirs required_files required_vars 78098186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 78198186Sgordon 78278344Sobrien case "$_file" in 78378344Sobrien *.sh) # run in current shell 78478344Sobrien set $_arg ; . $_file 78578344Sobrien ;; 78698186Sgordon *[~#]|*.OLD|*.orig) # scratch file; skip 78798186Sgordon warn "Ignoring scratch file $_file" 78898186Sgordon ;; 78978344Sobrien *) # run in subshell 79098186Sgordon if [ -x $_file ]; then 79198186Sgordon if [ -n "$rc_fast_and_loose" ]; then 79298186Sgordon set $_arg ; . $_file 79398186Sgordon else 79498186Sgordon ( trap "echo 'Reboot interrupted'; exit 1" 3 79598186Sgordon set $_arg ; . $_file ) 79698186Sgordon fi 79798186Sgordon fi 79878344Sobrien ;; 79978344Sobrien esac 80078344Sobrien} 80178344Sobrien 80278344Sobrien# 80378344Sobrien# load_rc_config 80478344Sobrien# Source in the configuration file for a given command. 80578344Sobrien# 80678344Sobrienload_rc_config() 80778344Sobrien{ 80878344Sobrien _command=$1 80978344Sobrien if [ -z "$_command" ]; then 81078344Sobrien err 3 'USAGE: load_rc_config command' 81178344Sobrien fi 81278344Sobrien 81398186Sgordon if [ -z "$_rc_conf_loaded" ]; then 81498186Sgordon if [ -r /etc/defaults/rc.conf ]; then 81598186Sgordon debug "Sourcing /etc/defaults/rc.conf" 81698186Sgordon . /etc/defaults/rc.conf 81798186Sgordon source_rc_confs 81898186Sgordon elif [ -r /etc/rc.conf ]; then 81998186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 82098186Sgordon . /etc/rc.conf 82198186Sgordon fi 82298186Sgordon _rc_conf_loaded=YES 82398186Sgordon fi 82478344Sobrien if [ -f /etc/rc.conf.d/"$_command" ]; then 82598186Sgordon debug "Sourcing /etc/rc.conf.d/${_command}" 82678344Sobrien . /etc/rc.conf.d/"$_command" 82778344Sobrien fi 828101850Sgordon 829101850Sgordon # XXX - Deprecated variable name support 830101850Sgordon # 831103018Sgordon case ${OSTYPE} in 832101850Sgordon FreeBSD) 833101850Sgordon [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 834101850Sgordon [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 835101850Sgordon [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 836101850Sgordon [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 837101850Sgordon [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 838101850Sgordon [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 839101850Sgordon [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 840101850Sgordon ;; 841101850Sgordon esac 842101850Sgordon 84378344Sobrien} 84478344Sobrien 84578344Sobrien# 84678344Sobrien# rc_usage commands 84778344Sobrien# Print a usage string for $0, with `commands' being a list of 84878344Sobrien# valid commands. 84978344Sobrien# 85078344Sobrienrc_usage() 85178344Sobrien{ 852106643Sgordon echo -n 1>&2 "Usage: $0 [fast|force](" 85378344Sobrien 85478344Sobrien _sep= 85578344Sobrien for _elem in $*; do 85678344Sobrien echo -n 1>&2 "$_sep$_elem" 85778344Sobrien _sep="|" 85878344Sobrien done 85978344Sobrien echo 1>&2 ")" 86078344Sobrien exit 1 86178344Sobrien} 86278344Sobrien 86378344Sobrien# 86478344Sobrien# err exitval message 86578344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 86678344Sobrien# 86778344Sobrienerr() 86878344Sobrien{ 86978344Sobrien exitval=$1 87078344Sobrien shift 87178344Sobrien 872106643Sgordon if [ -x /usr/bin/logger ]; then 873106643Sgordon logger "$0: ERROR: $*" 874106643Sgordon fi 875106643Sgordon echo 1>&2 "$0: ERROR: $*" 87678344Sobrien exit $exitval 87778344Sobrien} 87878344Sobrien 87978344Sobrien# 88078344Sobrien# warn message 88178344Sobrien# Display message to stderr and log to the syslog. 88278344Sobrien# 88378344Sobrienwarn() 88478344Sobrien{ 885106643Sgordon if [ -x /usr/bin/logger ]; then 886106643Sgordon logger "$0: WARNING: $*" 887106643Sgordon fi 888106643Sgordon echo 1>&2 "$0: WARNING: $*" 88978344Sobrien} 89098186Sgordon 89198186Sgordon# 89298186Sgordon# info message 89398186Sgordon# Display informational message to stdout and log to syslog. 89498186Sgordon# 89598186Sgordoninfo() 89698186Sgordon{ 897106643Sgordon if [ -x /usr/bin/logger ]; then 898106643Sgordon logger "$0: INFO: $*" 899106643Sgordon fi 900106643Sgordon echo "$0: INFO: $*" 90198186Sgordon} 90298186Sgordon 90398186Sgordon# 90498186Sgordon# debug message 905106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 90698186Sgordon# BEWARE that you don't call any subroutine that itself calls this 90798186Sgordon# function. 90898186Sgordon# 90998186Sgordondebug() 91098186Sgordon{ 91198186Sgordon case ${rc_debug} in 91298186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 913106700Sgordon if [ -x /usr/bin/logger ]; then 914106700Sgordon logger "$0: INFO: $*" 915106700Sgordon fi 916106643Sgordon echo 1>&2 "$0: DEBUG: $*" 91798186Sgordon ;; 91898186Sgordon esac 91998186Sgordon} 92098186Sgordon 92198186Sgordon# 92298186Sgordon# backup_file action file cur backup 92398186Sgordon# Make a backup copy of `file' into `cur', and save the previous 92498186Sgordon# version of `cur' as `backup' or use rcs for archiving. 92598186Sgordon# 92698186Sgordon# This routine checks the value of the backup_uses_rcs variable, 92798186Sgordon# which can be either YES or NO. 92898186Sgordon# 92998186Sgordon# The `action' keyword can be one of the following: 93098186Sgordon# 93198186Sgordon# add `file' is now being backed up (and is possibly 93298186Sgordon# being reentered into the backups system). `cur' 93398186Sgordon# is created and RCS files, if necessary, are 93498186Sgordon# created as well. 93598186Sgordon# 93698186Sgordon# update `file' has changed and needs to be backed up. 93798186Sgordon# If `cur' exists, it is copied to to `back' or 93898186Sgordon# checked into RCS (if the repository file is old), 93998186Sgordon# and then `file' is copied to `cur'. Another RCS 94098186Sgordon# check in done here if RCS is being used. 94198186Sgordon# 94298186Sgordon# remove `file' is no longer being tracked by the backups 94398186Sgordon# system. If RCS is not being used, `cur' is moved 94498186Sgordon# to `back', otherwise an empty file is checked in, 94598186Sgordon# and then `cur' is removed. 94698186Sgordon# 94798186Sgordon# 94898186Sgordonbackup_file() 94998186Sgordon{ 95098186Sgordon _action=$1 95198186Sgordon _file=$2 95298186Sgordon _cur=$3 95398186Sgordon _back=$4 95498186Sgordon 95598186Sgordon if checkyesno backup_uses_rcs; then 95698186Sgordon _msg0="backup archive" 95798186Sgordon _msg1="update" 95898186Sgordon 95998186Sgordon # ensure that history file is not locked 96098186Sgordon if [ -f $_cur,v ]; then 96198186Sgordon rcs -q -u -U -M $_cur 96298186Sgordon fi 96398186Sgordon 96498186Sgordon # ensure after switching to rcs that the 96598186Sgordon # current backup is not lost 96698186Sgordon if [ -f $_cur ]; then 96798186Sgordon # no archive, or current newer than archive 96898186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 96998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 97098186Sgordon rcs -q -kb -U $_cur 97198186Sgordon co -q -f -u $_cur 97298186Sgordon fi 97398186Sgordon fi 97498186Sgordon 97598186Sgordon case $_action in 97698186Sgordon add|update) 97798186Sgordon cp -p $_file $_cur 97898186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 97998186Sgordon rcs -q -kb -U $_cur 98098186Sgordon co -q -f -u $_cur 98198186Sgordon chown root:wheel $_cur $_cur,v 98298186Sgordon ;; 98398186Sgordon remove) 98498186Sgordon cp /dev/null $_cur 98598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 98698186Sgordon rcs -q -kb -U $_cur 98798186Sgordon chown root:wheel $_cur $_cur,v 98898186Sgordon rm $_cur 98998186Sgordon ;; 99098186Sgordon esac 99198186Sgordon else 99298186Sgordon case $_action in 99398186Sgordon add|update) 99498186Sgordon if [ -f $_cur ]; then 99598186Sgordon cp -p $_cur $_back 99698186Sgordon fi 99798186Sgordon cp -p $_file $_cur 99898186Sgordon chown root:wheel $_cur 99998186Sgordon ;; 100098186Sgordon remove) 100198186Sgordon mv -f $_cur $_back 100298186Sgordon ;; 100398186Sgordon esac 100498186Sgordon fi 100598186Sgordon} 1006