rc.subr revision 101850
198186Sgordon# $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 101850 2002-08-14 05:37:15Z gordon $ 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" 4998186Sgordon 5098186Sgordoncase `${CMD_OSTYPE}` in 5198186SgordonFreeBSD) 5298186Sgordon SYSCTL_W="${SYSCTL}" 5398186Sgordon ;; 5498186SgordonNetBSD) 5598186Sgordon SYSCTL_W="${SYSCTL} -w" 5698186Sgordon ;; 5798186Sgordonesac 5898186Sgordon 5998186Sgordon# 6078344Sobrien# functions 6178344Sobrien# --------- 6278344Sobrien 6378344Sobrien# 6498186Sgordon# set_rcvar base_var 6598186Sgordon# Set the variable name enabling a specific service. 6698186Sgordon# FreeBSD uses ${service}_enable, while NetBSD uses 6798186Sgordon# just the name of the service. For example: 6898186Sgordon# FreeBSD: sendmail_enable="YES" 6998186Sgordon# NetBSD : sendmail="YES" 7098186Sgordon# $1 - if $name is not the base to work of off, specify 7198186Sgordon# a different one 7298186Sgordon# 7398186Sgordonset_rcvar() 7498186Sgordon{ 7598186Sgordon if [ -z "$1" ]; then 7698186Sgordon base_var=${name} 7798186Sgordon else 7898186Sgordon base_var="$1" 7998186Sgordon fi 8098186Sgordon 8198186Sgordon case `${CMD_OSTYPE}` in 8298186Sgordon FreeBSD) 8398186Sgordon echo ${base_var}_enable 8498186Sgordon ;; 8598186Sgordon NetBSD) 8698186Sgordon echo ${base_var} 8798186Sgordon ;; 8898186Sgordon *) 8998186Sgordon echo 'XXX' 9098186Sgordon ;; 9198186Sgordon esac 9298186Sgordon} 9398186Sgordon 9498186Sgordon# 9598186Sgordon# force_depend script 9698186Sgordon# Force a service to start. Intended for use by services 9798186Sgordon# to resolve dependency issues. It is assumed the caller 9898186Sgordon# has check to make sure this call is necessary 9998186Sgordon# $1 - filename of script, in /etc/rc.d, to run 10098186Sgordon# 10198186Sgordonforce_depend() 10298186Sgordon{ 10398186Sgordon _depend="$1" 10498186Sgordon 10598186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 10698186Sgordon if ! /etc/rc.d/${_depend} forcestart ; then 10798186Sgordon warn "Unable to force ${_depend}. It may already be running." 10898186Sgordon return 1 10998186Sgordon fi 11098186Sgordon return 0 11198186Sgordon} 11298186Sgordon 11398186Sgordon# 11478344Sobrien# checkyesno var 11578344Sobrien# Test $1 variable, and warn if not set to YES or NO. 11678344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 11778344Sobrien# 11878344Sobriencheckyesno() 11978344Sobrien{ 12078344Sobrien eval _value=\$${1} 12198186Sgordon debug "checkyesno: $1 is set to $_value." 12278344Sobrien case $_value in 12378344Sobrien 12478344Sobrien # "yes", "true", "on", or "1" 12578344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 12678344Sobrien return 0 12778344Sobrien ;; 12878344Sobrien 12978344Sobrien # "no", "false", "off", or "0" 13078344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 13178344Sobrien return 1 13278344Sobrien ;; 13378344Sobrien *) 13478344Sobrien warn "\$${1} is not set properly." 13578344Sobrien return 1 13678344Sobrien ;; 13778344Sobrien esac 13878344Sobrien} 13978344Sobrien 14098186Sgordon# reverse_list list 14198186Sgordon# print the list in reverse order 14278344Sobrien# 14398186Sgordonreverse_list() 14498186Sgordon{ 14598186Sgordon _revlist= 14698186Sgordon for _revfile in $*; do 14798186Sgordon _revlist="$_revfile $_revlist" 14898186Sgordon done 14998186Sgordon echo $_revlist 15098186Sgordon} 15198186Sgordon 15278344Sobrien# 15398186Sgordon# mount_critical_filesystems type 15498186Sgordon# Go through the list of critical filesystems as provided in 15598186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 15698186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 15798186Sgordon# 15878344Sobrienmount_critical_filesystems() 15978344Sobrien{ 16098186Sgordon eval _fslist=\$critical_filesystems_${1} 16178344Sobrien for _fs in $_fslist; do 16278344Sobrien mount | ( 16378344Sobrien _ismounted=no 16478344Sobrien while read what _on on _type type; do 16578344Sobrien if [ $on = $_fs ]; then 16678344Sobrien _ismounted=yes 16778344Sobrien fi 16878344Sobrien done 16998186Sgordon if [ $_ismounted = no ]; then 17078344Sobrien mount $_fs >/dev/null 2>&1 17178344Sobrien fi 17298186Sgordon ) 17378344Sobrien done 17478344Sobrien} 17578344Sobrien 17678344Sobrien# 17798186Sgordon# check_pidfile pidfile procname [interpreter] 17898186Sgordon# Parses the first line of pidfile for a PID, and ensures 17978344Sobrien# that the process is running and matches procname. 18098186Sgordon# Prints the matching PID upon success, nothing otherwise. 18198186Sgordon# interpreter is optional; see _find_processes() for details. 18278344Sobrien# 18378344Sobriencheck_pidfile() 18478344Sobrien{ 18578344Sobrien _pidfile=$1 18678344Sobrien _procname=$2 18798186Sgordon _interpreter=$3 18878344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 18998186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 19078344Sobrien fi 19178344Sobrien if [ ! -f $_pidfile ]; then 19298186Sgordon debug "pid file {$_pidfile): not readable." 19378344Sobrien return 19478344Sobrien fi 19578344Sobrien read _pid _junk < $_pidfile 19678344Sobrien if [ -z "$_pid" ]; then 19798186Sgordon debug "pid file {$_pidfile): no pid in file." 19878344Sobrien return 19978344Sobrien fi 20098186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 20178344Sobrien} 20278344Sobrien 20378344Sobrien# 20498186Sgordon# check_process procname [interpreter] 20578344Sobrien# Ensures that a process (or processes) named procname is running. 20698186Sgordon# Prints a list of matching PIDs. 20798186Sgordon# interpreter is optional; see _find_processes() for details. 20878344Sobrien# 20978344Sobriencheck_process() 21078344Sobrien{ 21178344Sobrien _procname=$1 21298186Sgordon _interpreter=$2 21378344Sobrien if [ -z "$_procname" ]; then 21498186Sgordon err 3 'USAGE: check_process procname [interpreter]' 21578344Sobrien fi 21698186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 21798186Sgordon} 21898186Sgordon 21998186Sgordon# 22098186Sgordon# _find_processes procname interpreter psargs 22198186Sgordon# Search for procname in the output of ps generated by psargs. 22298186Sgordon# Prints the PIDs of any matching processes, space separated. 22398186Sgordon# 22498186Sgordon# If interpreter == ".", check the following variations of procname 22598186Sgordon# against the first word of each command: 22698186Sgordon# procname 22798186Sgordon# `basename procname` 22898186Sgordon# `basename procname` + ":" 22998186Sgordon# "(" + `basename procname` + ")" 23098186Sgordon# 23198186Sgordon# If interpreter != ".", read the first line of procname, remove the 23298186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 23398186Sgordon# match that against each command, either as is, or with extra words 23498186Sgordon# at the end. 23598186Sgordon# 23698186Sgordon_find_processes() 23798186Sgordon{ 23898186Sgordon if [ $# -ne 3 ]; then 23998186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 24098186Sgordon fi 24198186Sgordon _procname=$1 24298186Sgordon _interpreter=$2 24398186Sgordon _psargs=$3 24498186Sgordon 24578344Sobrien _pref= 24698186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 24798186Sgordon read _interp < $_procname # read interpreter name 24898186Sgordon _interp=${_interp#\#!} # strip #! 24998186Sgordon set -- $_interp 25098186Sgordon if [ $_interpreter != $1 ]; then 25198186Sgordon warn "\$command_interpreter $_interpreter != $1" 25278344Sobrien fi 25398186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 25498186Sgordon _fp_args='_argv' 25598186Sgordon _fp_match='case "$_argv" in 25698186Sgordon ${_interp}|"${_interp} "*)' 25798186Sgordon else # a normal daemon 25898186Sgordon _procnamebn=${_procname##*/} 25998186Sgordon _fp_args='_arg0 _argv' 26098186Sgordon _fp_match='case "$_arg0" in 26198186Sgordon $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 26298186Sgordon fi 26398186Sgordon 26498186Sgordon _proccheck=' 26598186Sgordon ps -o "pid,command" '"$_psargs"' | 26698186Sgordon while read _npid '"$_fp_args"'; do 26798186Sgordon case "$_npid" in 26898186Sgordon PID) 26998186Sgordon continue ;; 27098186Sgordon esac ; '"$_fp_match"' 27198186Sgordon echo -n "$_pref$_npid" ; 27298186Sgordon _pref=" " 27398186Sgordon ;; 27498186Sgordon esac 27598186Sgordon done' 27698186Sgordon 27798186Sgordon debug "in _find_processes: proccheck is ($_proccheck)." 27898186Sgordon eval $_proccheck 27998186Sgordon} 28098186Sgordon 28198186Sgordon# 28298186Sgordon# wait_for_pids pid [pid ...] 28398186Sgordon# spins until none of the pids exist 28498186Sgordon# 28598186Sgordonwait_for_pids() 28698186Sgordon{ 28798186Sgordon _list=$* 28898186Sgordon if [ -z "$_list" ]; then 28998186Sgordon return 29098186Sgordon fi 29198186Sgordon _prefix= 29298186Sgordon while true; do 29398186Sgordon _nlist=""; 29498186Sgordon for _j in $_list; do 29598186Sgordon if kill -0 $_j 2>/dev/null; then 29698186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 29798186Sgordon fi 29898186Sgordon done 29998186Sgordon if [ -z "$_nlist" ]; then 30098186Sgordon break 30178344Sobrien fi 30298186Sgordon _list=$_nlist 30398186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 30498186Sgordon _prefix=", " 30598186Sgordon sleep 2 30678344Sobrien done 30798186Sgordon if [ -n "$_prefix" ]; then 30898186Sgordon echo "." 30998186Sgordon fi 31078344Sobrien} 31178344Sobrien 31278344Sobrien# 31398186Sgordon# run_rc_command argument 31498186Sgordon# Search for argument in the list of supported commands, which is: 31598186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 31698186Sgordon# If there's a match, run ${argument}_cmd or the default method 31798186Sgordon# (see below). 31878344Sobrien# 31998186Sgordon# If argument has a given prefix, then change the operation as follows: 32098186Sgordon# Prefix Operation 32178344Sobrien# ------ --------- 32298186Sgordon# fast Skip the pid check, and set rc_fast=yes 32398186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 32478344Sobrien# 32578344Sobrien# The following globals are used: 32678344Sobrien# 32798186Sgordon# Name Needed Purpose 32898186Sgordon# ---- ------ ------- 32978344Sobrien# name y Name of script. 33078344Sobrien# 33178344Sobrien# command n Full path to command. 33298186Sgordon# Not needed if ${rc_arg}_cmd is set for 33378344Sobrien# each keyword. 33478344Sobrien# 33578344Sobrien# command_args n Optional args/shell directives for command. 33678344Sobrien# 33798186Sgordon# command_interpreter n If not empty, command is interpreted, so 33898186Sgordon# call check_{pidfile,process}() appropriately. 33998186Sgordon# 34078344Sobrien# extra_commands n List of extra commands supported. 34178344Sobrien# 34298186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 34398186Sgordon# otherwise use check_process $command. 34498186Sgordon# In either case, only check if $command is set. 34578344Sobrien# 34698186Sgordon# procname n Process name to check for instead of $command. 34798186Sgordon# 34878344Sobrien# rcvar n This is checked with checkyesno to determine 34978344Sobrien# if the action should be run. 35078344Sobrien# 35178344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 35298186Sgordon# Requires /usr to be mounted. 35378344Sobrien# 35478344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 35578344Sobrien# (if not using ${name}_chroot). 35678344Sobrien# 35778344Sobrien# ${name}_flags n Arguments to call ${command} with. 35878344Sobrien# NOTE: $flags from the parent environment 35978344Sobrien# can be used to override this. 36078344Sobrien# 36178344Sobrien# ${name}_nice n Nice level to run ${command} at. 36278344Sobrien# 36378344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 36478344Sobrien# using ${name}_chroot. 36598186Sgordon# Requires /usr to be mounted. 36678344Sobrien# 36778344Sobrien# ${name}_group n Group to run chrooted ${command} as. 36898186Sgordon# Requires /usr to be mounted. 36978344Sobrien# 37098186Sgordon# ${name}_groups n Comma separated list of supplementary groups 37198186Sgordon# to run the chrooted ${command} with. 37298186Sgordon# Requires /usr to be mounted. 37378344Sobrien# 37498186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 37578344Sobrien# Otherwise, use default command (see below) 37678344Sobrien# 37798186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 37898186Sgordon# ${rc_arg}_cmd method in the default 37998186Sgordon# operation (i.e, after checking for required 38098186Sgordon# bits and process (non)existence). 38178344Sobrien# If this completes with a non-zero exit code, 38298186Sgordon# don't run ${rc_arg}_cmd. 38378344Sobrien# 38498186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 38598186Sgordon# ${rc_arg}_cmd method, if that method 38698186Sgordon# returned a zero exit code. 38798186Sgordon# 38878344Sobrien# required_dirs n If set, check for the existence of the given 38978344Sobrien# directories before running the default 39078344Sobrien# (re)start command. 39178344Sobrien# 39278344Sobrien# required_files n If set, check for the readability of the given 39378344Sobrien# files before running the default (re)start 39478344Sobrien# command. 39578344Sobrien# 39678344Sobrien# required_vars n If set, perform checkyesno on each of the 39778344Sobrien# listed variables before running the default 39878344Sobrien# (re)start command. 39978344Sobrien# 40098186Sgordon# Default behaviour for a given argument, if no override method is 40198186Sgordon# provided: 40278344Sobrien# 40398186Sgordon# Argument Default behaviour 40498186Sgordon# -------- ----------------- 40578344Sobrien# start if !running && checkyesno ${rcvar} 40678344Sobrien# ${command} 40778344Sobrien# 40878344Sobrien# stop if ${pidfile} 40998186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 41078344Sobrien# else 41198186Sgordon# rc_pid=$(check_process $command) 41298186Sgordon# kill $sig_stop $rc_pid 41398186Sgordon# wait_for_pids $rc_pid 41498186Sgordon# ($sig_stop defaults to TERM.) 41578344Sobrien# 41698186Sgordon# reload Similar to stop, except use $sig_reload instead, 41798186Sgordon# and doesn't wait_for_pids. 41878344Sobrien# $sig_reload defaults to HUP. 41978344Sobrien# 42078344Sobrien# restart Run `stop' then `start'. 42178344Sobrien# 42298186Sgordon# status Show if ${command} is running, etc. 42378344Sobrien# 42498186Sgordon# poll Wait for ${command} to exit. 42598186Sgordon# 42698186Sgordon# rcvar Display what rc.conf variable is used (if any). 42798186Sgordon# 42898186Sgordon# Variables available to methods, and after run_rc_command() has 42998186Sgordon# completed: 43098186Sgordon# 43198186Sgordon# Variable Purpose 43298186Sgordon# -------- ------- 43398186Sgordon# rc_arg Argument to command, after fast/force processing 43498186Sgordon# performed 43598186Sgordon# 43698186Sgordon# rc_flags Flags to start the default command with. 43798186Sgordon# Defaults to ${name}_flags, unless overridden 43898186Sgordon# by $flags from the environment. 43998186Sgordon# This variable may be changed by the precmd method. 44098186Sgordon# 44198186Sgordon# rc_pid PID of command (if appropriate) 44298186Sgordon# 44398186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 44498186Sgordon# 44598186Sgordon# rc_force Not empty if "force" was provided (q.v.) 44698186Sgordon# 44798186Sgordon# 44878344Sobrienrun_rc_command() 44978344Sobrien{ 45098186Sgordon rc_arg=$1 45178344Sobrien if [ -z "$name" ]; then 45298186Sgordon err 3 'run_rc_command: $name is not set.' 45378344Sobrien fi 45478344Sobrien 45598186Sgordon case "$rc_arg" in 45678344Sobrien fast*) # "fast" prefix; don't check pid 45798186Sgordon rc_arg=${rc_arg#fast} 45898186Sgordon rc_fast=yes 45978344Sobrien ;; 46078344Sobrien force*) # "force prefix; always start 46198186Sgordon rc_arg=${rc_arg#force} 46298186Sgordon rc_force=yes 46378344Sobrien if [ -n "${rcvar}" ]; then 46478344Sobrien eval ${rcvar}=YES 46578344Sobrien fi 46678344Sobrien ;; 46778344Sobrien esac 46878344Sobrien 46998186Sgordon eval _overide_command=\$${name}_program 47098186Sgordon if [ -n "$_overide_command" ]; then 47198186Sgordon command=$_overide_command 47298186Sgordon fi 47398186Sgordon 47478344Sobrien _keywords="start stop restart rcvar $extra_commands" 47598186Sgordon rc_pid= 47678344Sobrien _pidcmd= 47798186Sgordon _procname=${procname:-${command}} 47898186Sgordon 47978344Sobrien # setup pid check command if not fast 48098186Sgordon if [ -z "$rc_fast" -a -n "$_procname" ]; then 48178344Sobrien if [ -n "$pidfile" ]; then 48298186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 48398186Sgordon else 48498186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 48578344Sobrien fi 48678344Sobrien if [ -n "$_pidcmd" ]; then 48798186Sgordon _keywords="${_keywords} status poll" 48878344Sobrien fi 48978344Sobrien fi 49078344Sobrien 49198186Sgordon if [ -z "$rc_arg" ]; then 49278344Sobrien rc_usage "$_keywords" 49378344Sobrien fi 49478344Sobrien 49578344Sobrien if [ -n "$flags" ]; then # allow override from environment 49698186Sgordon rc_flags=$flags 49778344Sobrien else 49898186Sgordon eval rc_flags=\$${name}_flags 49978344Sobrien fi 50098186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 50198186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 50298186Sgordon _group=\$${name}_group _groups=\$${name}_groups 50378344Sobrien 50498186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 50598186Sgordon if [ "$_user" = "$(id -un)" ]; then 50698186Sgordon unset _user 50798186Sgordon fi 50898186Sgordon fi 50998186Sgordon 51078344Sobrien # if ${rcvar} is set, and $1 is not 51198186Sgordon # "rcvar", then run 51278344Sobrien # checkyesno ${rcvar} 51378344Sobrien # and return if that failed 51478344Sobrien # 51598186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 51678344Sobrien if ! checkyesno ${rcvar}; then 51778344Sobrien return 0 51878344Sobrien fi 51978344Sobrien fi 52078344Sobrien 52178344Sobrien eval $_pidcmd # determine the pid if necessary 52278344Sobrien 52378344Sobrien for _elem in $_keywords; do 52498186Sgordon if [ "$_elem" != "$rc_arg" ]; then 52578344Sobrien continue 52678344Sobrien fi 52778344Sobrien 52878344Sobrien # if there's a custom ${XXX_cmd}, 52978344Sobrien # run that instead of the default 53078344Sobrien # 53198186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 53298186Sgordon _postcmd=\$${rc_arg}_postcmd 53378344Sobrien if [ -n "$_cmd" ]; then 53498186Sgordon debug "run_rc_command: using XXX_cmd functions." 53578344Sobrien # if the precmd failed and force 53678344Sobrien # isn't set, exit 53778344Sobrien # 53898186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 53978344Sobrien return 1 54078344Sobrien fi 54178344Sobrien 54298186Sgordon if ! eval $_cmd && [ -z "$rc_force" ]; then 54398186Sgordon return 1 54498186Sgordon fi 54598186Sgordon eval $_postcmd 54678344Sobrien return 0 54778344Sobrien fi 54878344Sobrien 54998186Sgordon case "$rc_arg" in # default operations... 55078344Sobrien 55178344Sobrien status) 55298186Sgordon if [ -n "$rc_pid" ]; then 55398186Sgordon echo "${name} is running as pid $rc_pid." 55478344Sobrien else 55578344Sobrien echo "${name} is not running." 55678344Sobrien return 1 55778344Sobrien fi 55878344Sobrien ;; 55978344Sobrien 56078344Sobrien start) 56198186Sgordon if [ -n "$rc_pid" ]; then 56298186Sgordon echo "${name} already running? (pid=$rc_pid)." 56378344Sobrien exit 1 56478344Sobrien fi 56578344Sobrien 56678344Sobrien if [ ! -x $command ]; then 56798186Sgordon info "run_rc_command: cannot run ($command)." 56878344Sobrien return 0 56978344Sobrien fi 57078344Sobrien 57178344Sobrien # check for required variables, 57278344Sobrien # directories, and files 57378344Sobrien # 57478344Sobrien for _f in $required_vars; do 57578344Sobrien if ! checkyesno $_f; then 57678344Sobrien warn "\$${_f} is not set." 57798186Sgordon if [ -z "$rc_force" ]; then 57878344Sobrien return 1 57978344Sobrien fi 58078344Sobrien fi 58178344Sobrien done 58278344Sobrien for _f in $required_dirs; do 58378344Sobrien if [ ! -d "${_f}/." ]; then 58478344Sobrien warn "${_f} is not a directory." 58598186Sgordon if [ -z "$rc_force" ]; then 58678344Sobrien return 1 58778344Sobrien fi 58878344Sobrien fi 58978344Sobrien done 59078344Sobrien for _f in $required_files; do 59178344Sobrien if [ ! -r "${_f}" ]; then 59278344Sobrien warn "${_f} is not readable." 59398186Sgordon if [ -z "$rc_force" ]; then 59478344Sobrien return 1 59578344Sobrien fi 59678344Sobrien fi 59778344Sobrien done 59878344Sobrien 59978344Sobrien # if the precmd failed and force 60078344Sobrien # isn't set, exit 60178344Sobrien # 60298186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 60378344Sobrien return 1 60478344Sobrien fi 60578344Sobrien 60678344Sobrien # setup the command to run, and run it 60778344Sobrien # 60878344Sobrien echo "Starting ${name}." 60978344Sobrien if [ -n "$_chroot" ]; then 61078344Sobrien _doit="\ 61178344Sobrien${_nice:+nice -n $_nice }\ 61278344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 61398186Sgordon$_chroot $command $rc_flags $command_args" 61478344Sobrien else 61578344Sobrien _doit="\ 61678344Sobrien${_chdir:+cd $_chdir; }\ 61778344Sobrien${_nice:+nice -n $_nice }\ 61898186Sgordon$command $rc_flags $command_args" 61998186Sgordon if [ -n "$_user" ]; then 62098186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 62198186Sgordon fi 62278344Sobrien fi 62398186Sgordon 62498186Sgordon # if the cmd failed and force 62598186Sgordon # isn't set, exit 62698186Sgordon # 62798186Sgordon debug "run_rc_command: _doit: $_doit" 62898186Sgordon if ! eval $_doit && [ -z "$rc_force" ]; then 62998186Sgordon return 1 63098186Sgordon fi 63198186Sgordon 63298186Sgordon # finally, run postcmd 63398186Sgordon # 63498186Sgordon eval $_postcmd 63578344Sobrien ;; 63678344Sobrien 63778344Sobrien stop) 63898186Sgordon if [ -z "$rc_pid" ]; then 63978344Sobrien if [ -n "$pidfile" ]; then 64078344Sobrien echo \ 64178344Sobrien "${name} not running? (check $pidfile)." 64278344Sobrien else 64378344Sobrien echo "${name} not running?" 64478344Sobrien fi 64578344Sobrien exit 1 64678344Sobrien fi 64778344Sobrien 64898186Sgordon # if the precmd failed and force 64998186Sgordon # isn't set, exit 65098186Sgordon # 65198186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 65278344Sobrien return 1 65378344Sobrien fi 65498186Sgordon 65598186Sgordon # send the signal to stop 65698186Sgordon # 65778344Sobrien echo "Stopping ${name}." 65898186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 65998186Sgordon if [ -n "$_user" ]; then 66098186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 66198186Sgordon fi 66298186Sgordon 66398186Sgordon # if the stop cmd failed and force 66498186Sgordon # isn't set, exit 66598186Sgordon # 66698186Sgordon if ! eval $_doit && [ -z "$rc_force" ]; then 66798186Sgordon return 1 66898186Sgordon fi 66998186Sgordon 67098186Sgordon # wait for the command to exit, 67198186Sgordon # and run postcmd. 67298186Sgordon wait_for_pids $rc_pid 67398186Sgordon eval $_postcmd 67478344Sobrien ;; 67578344Sobrien 67678344Sobrien reload) 67798186Sgordon if [ -z "$rc_pid" ]; then 67878344Sobrien if [ -n "$pidfile" ]; then 67978344Sobrien echo \ 68078344Sobrien "${name} not running? (check $pidfile)." 68178344Sobrien else 68278344Sobrien echo "${name} not running?" 68378344Sobrien fi 68478344Sobrien exit 1 68578344Sobrien fi 68678344Sobrien echo "Reloading ${name} config files." 68798186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 68878344Sobrien return 1 68978344Sobrien fi 69098186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 69198186Sgordon if [ -n "$_user" ]; then 69298186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 69398186Sgordon fi 69498186Sgordon if ! eval $_doit && [ -z "$rc_force" ]; then 69598186Sgordon return 1 69698186Sgordon fi 69798186Sgordon eval $_postcmd 69878344Sobrien ;; 69978344Sobrien 70078344Sobrien restart) 70198186Sgordon if ! eval $_precmd && [ -z "$rc_force" ]; then 70278344Sobrien return 1 70378344Sobrien fi 70478344Sobrien # prevent restart being called more 70578344Sobrien # than once by any given script 70678344Sobrien # 70778344Sobrien if [ -n "$_rc_restart_done" ]; then 70878344Sobrien return 0 70978344Sobrien fi 71078344Sobrien _rc_restart_done=YES 71178344Sobrien 71298186Sgordon ( $0 ${rc_force:+force}stop ) 71398186Sgordon $0 ${rc_force:+force}start 71498186Sgordon 71598186Sgordon eval $_postcmd 71678344Sobrien ;; 71778344Sobrien 71898186Sgordon poll) 71998186Sgordon if [ -n "$rc_pid" ]; then 72098186Sgordon wait_for_pids $rc_pid 72198186Sgordon fi 72298186Sgordon ;; 72398186Sgordon 72478344Sobrien rcvar) 72578344Sobrien echo "# $name" 72678344Sobrien if [ -n "$rcvar" ]; then 72778344Sobrien if checkyesno ${rcvar}; then 72878344Sobrien echo "\$${rcvar}=YES" 72978344Sobrien else 73078344Sobrien echo "\$${rcvar}=NO" 73178344Sobrien fi 73278344Sobrien fi 73378344Sobrien ;; 73478344Sobrien 73578344Sobrien *) 73678344Sobrien rc_usage "$_keywords" 73778344Sobrien ;; 73878344Sobrien 73978344Sobrien esac 74078344Sobrien return 0 74178344Sobrien done 74278344Sobrien 74398186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 74478344Sobrien rc_usage "$_keywords" 74578344Sobrien exit 1 74678344Sobrien} 74778344Sobrien 74878344Sobrien# 74978344Sobrien# run_rc_script file arg 75078344Sobrien# Start the script `file' with `arg', and correctly handle the 75178344Sobrien# return value from the script. If `file' ends with `.sh', it's 75298186Sgordon# sourced into the current environment. If `file' appears to be 75398186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 75498186Sgordon# executable run as a child process. 75578344Sobrien# 75678344Sobrienrun_rc_script() 75778344Sobrien{ 75878344Sobrien _file=$1 75978344Sobrien _arg=$2 76078344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 76178344Sobrien err 3 'USAGE: run_rc_script file arg' 76278344Sobrien fi 76378344Sobrien 76498186Sgordon trap "echo 'Reboot interrupted'; exit 1" 3 76598186Sgordon 76698186Sgordon unset name command command_args command_interpreter \ 76798186Sgordon extra_commands pidfile procname \ 76898186Sgordon rcvar required_dirs required_files required_vars 76998186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 77098186Sgordon 77178344Sobrien case "$_file" in 77278344Sobrien *.sh) # run in current shell 77378344Sobrien set $_arg ; . $_file 77478344Sobrien ;; 77598186Sgordon *[~#]|*.OLD|*.orig) # scratch file; skip 77698186Sgordon warn "Ignoring scratch file $_file" 77798186Sgordon ;; 77878344Sobrien *) # run in subshell 77998186Sgordon if [ -x $_file ]; then 78098186Sgordon if [ -n "$rc_fast_and_loose" ]; then 78198186Sgordon set $_arg ; . $_file 78298186Sgordon else 78398186Sgordon ( trap "echo 'Reboot interrupted'; exit 1" 3 78498186Sgordon set $_arg ; . $_file ) 78598186Sgordon fi 78698186Sgordon fi 78778344Sobrien ;; 78878344Sobrien esac 78978344Sobrien} 79078344Sobrien 79178344Sobrien# 79278344Sobrien# load_rc_config 79378344Sobrien# Source in the configuration file for a given command. 79478344Sobrien# 79578344Sobrienload_rc_config() 79678344Sobrien{ 79778344Sobrien _command=$1 79878344Sobrien if [ -z "$_command" ]; then 79978344Sobrien err 3 'USAGE: load_rc_config command' 80078344Sobrien fi 80178344Sobrien 80298186Sgordon if [ -z "$_rc_conf_loaded" ]; then 80398186Sgordon if [ -r /etc/defaults/rc.conf ]; then 80498186Sgordon debug "Sourcing /etc/defaults/rc.conf" 80598186Sgordon . /etc/defaults/rc.conf 80698186Sgordon source_rc_confs 80798186Sgordon elif [ -r /etc/rc.conf ]; then 80898186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 80998186Sgordon . /etc/rc.conf 81098186Sgordon fi 81198186Sgordon _rc_conf_loaded=YES 81298186Sgordon fi 81378344Sobrien if [ -f /etc/rc.conf.d/"$_command" ]; then 81498186Sgordon debug "Sourcing /etc/rc.conf.d/${_command}" 81578344Sobrien . /etc/rc.conf.d/"$_command" 81678344Sobrien fi 817101850Sgordon 818101850Sgordon # XXX - Deprecated variable name support 819101850Sgordon # 820101850Sgordon case `${CMD_OSTYPE}` in 821101850Sgordon FreeBSD) 822101850Sgordon [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 823101850Sgordon [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 824101850Sgordon [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 825101850Sgordon [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 826101850Sgordon [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 827101850Sgordon [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 828101850Sgordon [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 829101850Sgordon ;; 830101850Sgordon esac 831101850Sgordon 83278344Sobrien} 83378344Sobrien 83478344Sobrien# 83578344Sobrien# rc_usage commands 83678344Sobrien# Print a usage string for $0, with `commands' being a list of 83778344Sobrien# valid commands. 83878344Sobrien# 83978344Sobrienrc_usage() 84078344Sobrien{ 84195258Sdes echo -n 1>&2 "usage: $0 [fast|force](" 84278344Sobrien 84378344Sobrien _sep= 84478344Sobrien for _elem in $*; do 84578344Sobrien echo -n 1>&2 "$_sep$_elem" 84678344Sobrien _sep="|" 84778344Sobrien done 84878344Sobrien echo 1>&2 ")" 84978344Sobrien exit 1 85078344Sobrien} 85178344Sobrien 85278344Sobrien# 85398186Sgordon# _echo prefix message 85498186Sgordon# Display message preceded by "$prefix:". Log to syslog as well. 85598186Sgordon# XXX - syslogd may not be listening (especially if this subroutine 85698186Sgordon# is called at boot before syslogd has had a chance to startup). 85798186Sgordon# 85898186Sgordon_echo() 85998186Sgordon{ 86098186Sgordon [ -x /usr/bin/logger ] && /usr/bin/logger "$0: $1: $2" 86198186Sgordon echo "$0: $1: $2" 86298186Sgordon} 86398186Sgordon 86498186Sgordon# 86578344Sobrien# err exitval message 86678344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 86778344Sobrien# 86878344Sobrienerr() 86978344Sobrien{ 87078344Sobrien exitval=$1 87178344Sobrien shift 87278344Sobrien 87398186Sgordon _echo 1>&2 "ERROR" "$*" 87478344Sobrien exit $exitval 87578344Sobrien} 87678344Sobrien 87778344Sobrien# 87878344Sobrien# warn message 87978344Sobrien# Display message to stderr and log to the syslog. 88078344Sobrien# 88178344Sobrienwarn() 88278344Sobrien{ 88398186Sgordon _echo 1>&2 "WARNING" "$*" 88478344Sobrien} 88598186Sgordon 88698186Sgordon# 88798186Sgordon# info message 88898186Sgordon# Display informational message to stdout and log to syslog. 88998186Sgordon# 89098186Sgordoninfo() 89198186Sgordon{ 89298186Sgordon _echo "INFO" "$*" 89398186Sgordon} 89498186Sgordon 89598186Sgordon# 89698186Sgordon# debug message 89798186Sgordon# If debugging is enabled in rc.conf output message to stderr and syslog. 89898186Sgordon# BEWARE that you don't call any subroutine that itself calls this 89998186Sgordon# function. 90098186Sgordon# 90198186Sgordondebug() 90298186Sgordon{ 90398186Sgordon # This subroutine is provided as a convenience to script writers, who 90498186Sgordon # should enable debugging in /etc/rc.conf. 90598186Sgordon # 90698186Sgordon [ -f /etc/rc.conf ] && . /etc/rc.conf 90798186Sgordon 90898186Sgordon case ${rc_debug} in 90998186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 91098186Sgordon _echo 1>&2 "DEBUG" "$*" 91198186Sgordon return 91298186Sgordon ;; 91398186Sgordon esac 91498186Sgordon} 91598186Sgordon 91698186Sgordon# 91798186Sgordon# backup_file action file cur backup 91898186Sgordon# Make a backup copy of `file' into `cur', and save the previous 91998186Sgordon# version of `cur' as `backup' or use rcs for archiving. 92098186Sgordon# 92198186Sgordon# This routine checks the value of the backup_uses_rcs variable, 92298186Sgordon# which can be either YES or NO. 92398186Sgordon# 92498186Sgordon# The `action' keyword can be one of the following: 92598186Sgordon# 92698186Sgordon# add `file' is now being backed up (and is possibly 92798186Sgordon# being reentered into the backups system). `cur' 92898186Sgordon# is created and RCS files, if necessary, are 92998186Sgordon# created as well. 93098186Sgordon# 93198186Sgordon# update `file' has changed and needs to be backed up. 93298186Sgordon# If `cur' exists, it is copied to to `back' or 93398186Sgordon# checked into RCS (if the repository file is old), 93498186Sgordon# and then `file' is copied to `cur'. Another RCS 93598186Sgordon# check in done here if RCS is being used. 93698186Sgordon# 93798186Sgordon# remove `file' is no longer being tracked by the backups 93898186Sgordon# system. If RCS is not being used, `cur' is moved 93998186Sgordon# to `back', otherwise an empty file is checked in, 94098186Sgordon# and then `cur' is removed. 94198186Sgordon# 94298186Sgordon# 94398186Sgordonbackup_file() 94498186Sgordon{ 94598186Sgordon _action=$1 94698186Sgordon _file=$2 94798186Sgordon _cur=$3 94898186Sgordon _back=$4 94998186Sgordon 95098186Sgordon if checkyesno backup_uses_rcs; then 95198186Sgordon _msg0="backup archive" 95298186Sgordon _msg1="update" 95398186Sgordon 95498186Sgordon # ensure that history file is not locked 95598186Sgordon if [ -f $_cur,v ]; then 95698186Sgordon rcs -q -u -U -M $_cur 95798186Sgordon fi 95898186Sgordon 95998186Sgordon # ensure after switching to rcs that the 96098186Sgordon # current backup is not lost 96198186Sgordon if [ -f $_cur ]; then 96298186Sgordon # no archive, or current newer than archive 96398186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 96498186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 96598186Sgordon rcs -q -kb -U $_cur 96698186Sgordon co -q -f -u $_cur 96798186Sgordon fi 96898186Sgordon fi 96998186Sgordon 97098186Sgordon case $_action in 97198186Sgordon add|update) 97298186Sgordon cp -p $_file $_cur 97398186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 97498186Sgordon rcs -q -kb -U $_cur 97598186Sgordon co -q -f -u $_cur 97698186Sgordon chown root:wheel $_cur $_cur,v 97798186Sgordon ;; 97898186Sgordon remove) 97998186Sgordon cp /dev/null $_cur 98098186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 98198186Sgordon rcs -q -kb -U $_cur 98298186Sgordon chown root:wheel $_cur $_cur,v 98398186Sgordon rm $_cur 98498186Sgordon ;; 98598186Sgordon esac 98698186Sgordon else 98798186Sgordon case $_action in 98898186Sgordon add|update) 98998186Sgordon if [ -f $_cur ]; then 99098186Sgordon cp -p $_cur $_back 99198186Sgordon fi 99298186Sgordon cp -p $_file $_cur 99398186Sgordon chown root:wheel $_cur 99498186Sgordon ;; 99598186Sgordon remove) 99698186Sgordon mv -f $_cur $_back 99798186Sgordon ;; 99898186Sgordon esac 99998186Sgordon fi 100098186Sgordon} 1001