rc.subr revision 167413
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 167413 2007-03-10 13:37:44Z yar $ 378344Sobrien# 4157473Sflz# Copyright (c) 1997-2004 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 42157473Sflz: ${rcvar_manpage:='rc.conf(5)'} 43157473Sflz 4478344Sobrien# 4598186Sgordon# Operating System dependent/independent variables 4698186Sgordon# 4798186Sgordon 48131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then 49131550Scperciva 50131550Scperciva_rc_subr_loaded="YES" 51131550Scperciva 5298186SgordonSYSCTL="/sbin/sysctl" 5398186SgordonSYSCTL_N="${SYSCTL} -n" 5498186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype" 55103018SgordonOSTYPE=`${CMD_OSTYPE}` 56124832SmtmID="/usr/bin/id" 57124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 58161435SyarPS="/bin/ps -ww" 59161435SyarJID=`$PS -p $$ -o jid=` 6098186Sgordon 61103018Sgordoncase ${OSTYPE} in 6298186SgordonFreeBSD) 6398186Sgordon SYSCTL_W="${SYSCTL}" 6498186Sgordon ;; 6598186SgordonNetBSD) 6698186Sgordon SYSCTL_W="${SYSCTL} -w" 6798186Sgordon ;; 6898186Sgordonesac 6998186Sgordon 7098186Sgordon# 7178344Sobrien# functions 7278344Sobrien# --------- 7378344Sobrien 7478344Sobrien# 7598186Sgordon# set_rcvar base_var 7698186Sgordon# Set the variable name enabling a specific service. 7798186Sgordon# FreeBSD uses ${service}_enable, while NetBSD uses 7898186Sgordon# just the name of the service. For example: 7998186Sgordon# FreeBSD: sendmail_enable="YES" 8098186Sgordon# NetBSD : sendmail="YES" 8198186Sgordon# $1 - if $name is not the base to work of off, specify 8298186Sgordon# a different one 8398186Sgordon# 8498186Sgordonset_rcvar() 8598186Sgordon{ 8698186Sgordon if [ -z "$1" ]; then 8798186Sgordon base_var=${name} 8898186Sgordon else 8998186Sgordon base_var="$1" 9098186Sgordon fi 9198186Sgordon 92103018Sgordon case ${OSTYPE} in 9398186Sgordon FreeBSD) 9498186Sgordon echo ${base_var}_enable 9598186Sgordon ;; 9698186Sgordon NetBSD) 9798186Sgordon echo ${base_var} 9898186Sgordon ;; 9998186Sgordon *) 10098186Sgordon echo 'XXX' 10198186Sgordon ;; 10298186Sgordon esac 10398186Sgordon} 10498186Sgordon 10598186Sgordon# 10698186Sgordon# force_depend script 10798186Sgordon# Force a service to start. Intended for use by services 10898186Sgordon# to resolve dependency issues. It is assumed the caller 10998186Sgordon# has check to make sure this call is necessary 11098186Sgordon# $1 - filename of script, in /etc/rc.d, to run 11198186Sgordon# 11298186Sgordonforce_depend() 11398186Sgordon{ 11498186Sgordon _depend="$1" 11598186Sgordon 11698186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 117146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 11898186Sgordon warn "Unable to force ${_depend}. It may already be running." 11998186Sgordon return 1 12098186Sgordon fi 12198186Sgordon return 0 12298186Sgordon} 12398186Sgordon 12498186Sgordon# 12578344Sobrien# checkyesno var 12678344Sobrien# Test $1 variable, and warn if not set to YES or NO. 12778344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 12878344Sobrien# 12978344Sobriencheckyesno() 13078344Sobrien{ 13178344Sobrien eval _value=\$${1} 13298186Sgordon debug "checkyesno: $1 is set to $_value." 13378344Sobrien case $_value in 13478344Sobrien 13578344Sobrien # "yes", "true", "on", or "1" 13678344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 13778344Sobrien return 0 13878344Sobrien ;; 13978344Sobrien 14078344Sobrien # "no", "false", "off", or "0" 14178344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 14278344Sobrien return 1 14378344Sobrien ;; 14478344Sobrien *) 145157473Sflz warn "\$${1} is not set properly - see ${rcvar_manpage}." 14678344Sobrien return 1 14778344Sobrien ;; 14878344Sobrien esac 14978344Sobrien} 15078344Sobrien 151157473Sflz# 15298186Sgordon# reverse_list list 15398186Sgordon# print the list in reverse order 15478344Sobrien# 15598186Sgordonreverse_list() 15698186Sgordon{ 15798186Sgordon _revlist= 158126286Smtm for _revfile; do 15998186Sgordon _revlist="$_revfile $_revlist" 16098186Sgordon done 16198186Sgordon echo $_revlist 16298186Sgordon} 16398186Sgordon 16478344Sobrien# 16598186Sgordon# mount_critical_filesystems type 16698186Sgordon# Go through the list of critical filesystems as provided in 16798186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 16898186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 16998186Sgordon# 17078344Sobrienmount_critical_filesystems() 17178344Sobrien{ 17298186Sgordon eval _fslist=\$critical_filesystems_${1} 17378344Sobrien for _fs in $_fslist; do 17478344Sobrien mount | ( 175126285Smtm _ismounted=false 17678344Sobrien while read what _on on _type type; do 17778344Sobrien if [ $on = $_fs ]; then 178126285Smtm _ismounted=true 17978344Sobrien fi 18078344Sobrien done 181126285Smtm if $_ismounted; then 182126285Smtm : 183126285Smtm else 18478344Sobrien mount $_fs >/dev/null 2>&1 18578344Sobrien fi 18698186Sgordon ) 18778344Sobrien done 18878344Sobrien} 18978344Sobrien 19078344Sobrien# 19198186Sgordon# check_pidfile pidfile procname [interpreter] 19298186Sgordon# Parses the first line of pidfile for a PID, and ensures 19378344Sobrien# that the process is running and matches procname. 19498186Sgordon# Prints the matching PID upon success, nothing otherwise. 19598186Sgordon# interpreter is optional; see _find_processes() for details. 19678344Sobrien# 19778344Sobriencheck_pidfile() 19878344Sobrien{ 19978344Sobrien _pidfile=$1 20078344Sobrien _procname=$2 20198186Sgordon _interpreter=$3 20278344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 20398186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 20478344Sobrien fi 20578344Sobrien if [ ! -f $_pidfile ]; then 206131061Smtm debug "pid file ($_pidfile): not readable." 20778344Sobrien return 20878344Sobrien fi 20978344Sobrien read _pid _junk < $_pidfile 21078344Sobrien if [ -z "$_pid" ]; then 211139949Skeramida debug "pid file ($_pidfile): no pid in file." 21278344Sobrien return 21378344Sobrien fi 21498186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 21578344Sobrien} 21678344Sobrien 21778344Sobrien# 21898186Sgordon# check_process procname [interpreter] 21978344Sobrien# Ensures that a process (or processes) named procname is running. 22098186Sgordon# Prints a list of matching PIDs. 22198186Sgordon# interpreter is optional; see _find_processes() for details. 22278344Sobrien# 22378344Sobriencheck_process() 22478344Sobrien{ 22578344Sobrien _procname=$1 22698186Sgordon _interpreter=$2 22778344Sobrien if [ -z "$_procname" ]; then 22898186Sgordon err 3 'USAGE: check_process procname [interpreter]' 22978344Sobrien fi 23098186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 23198186Sgordon} 23298186Sgordon 23398186Sgordon# 23498186Sgordon# _find_processes procname interpreter psargs 23598186Sgordon# Search for procname in the output of ps generated by psargs. 23698186Sgordon# Prints the PIDs of any matching processes, space separated. 23798186Sgordon# 23898186Sgordon# If interpreter == ".", check the following variations of procname 23998186Sgordon# against the first word of each command: 24098186Sgordon# procname 24198186Sgordon# `basename procname` 24298186Sgordon# `basename procname` + ":" 24398186Sgordon# "(" + `basename procname` + ")" 244155719Sceri# "[" + `basename procname` + "]" 24598186Sgordon# 24698186Sgordon# If interpreter != ".", read the first line of procname, remove the 24798186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 24898186Sgordon# match that against each command, either as is, or with extra words 249157841Sflz# at the end. As an alternative, to deal with interpreted daemons 250157841Sflz# using perl, the basename of the interpreter plus a colon is also 251157841Sflz# tried as the prefix to procname. 25298186Sgordon# 25398186Sgordon_find_processes() 25498186Sgordon{ 25598186Sgordon if [ $# -ne 3 ]; then 25698186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 25798186Sgordon fi 25898186Sgordon _procname=$1 25998186Sgordon _interpreter=$2 26098186Sgordon _psargs=$3 26198186Sgordon 26278344Sobrien _pref= 26398186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 264167413Syar # read interpreter name 265167413Syar read _interp < ${_chroot}${_chroot:+"/"}$_procname 26698186Sgordon _interp=${_interp#\#!} # strip #! 26798186Sgordon set -- $_interp 268165684Syar case $1 in 269165684Syar */bin/env) 270165684Syar shift # drop env to get real name 271165684Syar ;; 272165684Syar esac 27398186Sgordon if [ $_interpreter != $1 ]; then 27498186Sgordon warn "\$command_interpreter $_interpreter != $1" 27578344Sobrien fi 27698186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 277157841Sflz _interpbn=${1##*/} 27898186Sgordon _fp_args='_argv' 27998186Sgordon _fp_match='case "$_argv" in 280157841Sflz ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)' 28198186Sgordon else # a normal daemon 28298186Sgordon _procnamebn=${_procname##*/} 28398186Sgordon _fp_args='_arg0 _argv' 28498186Sgordon _fp_match='case "$_arg0" in 285151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 28698186Sgordon fi 28798186Sgordon 288161435Syar _proccheck="\ 289161436Syar $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 290157657Sflz while read _npid _jid '"$_fp_args"'; do 291161436Syar '"$_fp_match"' 292157657Sflz if [ "$JID" -eq "$_jid" ]; 293157657Sflz then echo -n "$_pref$_npid"; 294157657Sflz _pref=" "; 295157657Sflz fi 29698186Sgordon ;; 29798186Sgordon esac 29898186Sgordon done' 29998186Sgordon 300114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 30198186Sgordon eval $_proccheck 30298186Sgordon} 30398186Sgordon 30498186Sgordon# 30598186Sgordon# wait_for_pids pid [pid ...] 30698186Sgordon# spins until none of the pids exist 30798186Sgordon# 30898186Sgordonwait_for_pids() 30998186Sgordon{ 310126286Smtm _list="$@" 31198186Sgordon if [ -z "$_list" ]; then 31298186Sgordon return 31398186Sgordon fi 31498186Sgordon _prefix= 31598186Sgordon while true; do 31698186Sgordon _nlist=""; 31798186Sgordon for _j in $_list; do 31898186Sgordon if kill -0 $_j 2>/dev/null; then 31998186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 32098186Sgordon fi 32198186Sgordon done 32298186Sgordon if [ -z "$_nlist" ]; then 32398186Sgordon break 32478344Sobrien fi 32598186Sgordon _list=$_nlist 32698186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 32798186Sgordon _prefix=", " 32898186Sgordon sleep 2 32978344Sobrien done 33098186Sgordon if [ -n "$_prefix" ]; then 33198186Sgordon echo "." 33298186Sgordon fi 33378344Sobrien} 33478344Sobrien 33578344Sobrien# 33698186Sgordon# run_rc_command argument 33798186Sgordon# Search for argument in the list of supported commands, which is: 33898186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 33998186Sgordon# If there's a match, run ${argument}_cmd or the default method 34098186Sgordon# (see below). 34178344Sobrien# 34298186Sgordon# If argument has a given prefix, then change the operation as follows: 34398186Sgordon# Prefix Operation 34478344Sobrien# ------ --------- 34598186Sgordon# fast Skip the pid check, and set rc_fast=yes 34698186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 347126303Smtm# one Set ${rcvar} to YES 34878344Sobrien# 34978344Sobrien# The following globals are used: 35078344Sobrien# 35198186Sgordon# Name Needed Purpose 35298186Sgordon# ---- ------ ------- 35378344Sobrien# name y Name of script. 35478344Sobrien# 35578344Sobrien# command n Full path to command. 35698186Sgordon# Not needed if ${rc_arg}_cmd is set for 35778344Sobrien# each keyword. 35878344Sobrien# 35978344Sobrien# command_args n Optional args/shell directives for command. 36078344Sobrien# 36198186Sgordon# command_interpreter n If not empty, command is interpreted, so 36298186Sgordon# call check_{pidfile,process}() appropriately. 36398186Sgordon# 36478344Sobrien# extra_commands n List of extra commands supported. 36578344Sobrien# 36698186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 36798186Sgordon# otherwise use check_process $command. 36898186Sgordon# In either case, only check if $command is set. 36978344Sobrien# 37098186Sgordon# procname n Process name to check for instead of $command. 37198186Sgordon# 37278344Sobrien# rcvar n This is checked with checkyesno to determine 37378344Sobrien# if the action should be run. 37478344Sobrien# 375157653Sflz# ${name}_program n Full path to command. 376157653Sflz# Meant to be used in /etc/rc.conf to override 377157653Sflz# ${command}. 378157653Sflz# 37978344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 38098186Sgordon# Requires /usr to be mounted. 38178344Sobrien# 38278344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 38378344Sobrien# (if not using ${name}_chroot). 38478344Sobrien# 38578344Sobrien# ${name}_flags n Arguments to call ${command} with. 38678344Sobrien# NOTE: $flags from the parent environment 38778344Sobrien# can be used to override this. 38878344Sobrien# 38978344Sobrien# ${name}_nice n Nice level to run ${command} at. 39078344Sobrien# 39178344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 39278344Sobrien# using ${name}_chroot. 39398186Sgordon# Requires /usr to be mounted. 39478344Sobrien# 39578344Sobrien# ${name}_group n Group to run chrooted ${command} as. 39698186Sgordon# Requires /usr to be mounted. 39778344Sobrien# 39898186Sgordon# ${name}_groups n Comma separated list of supplementary groups 39998186Sgordon# to run the chrooted ${command} with. 40098186Sgordon# Requires /usr to be mounted. 40178344Sobrien# 40298186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 40378344Sobrien# Otherwise, use default command (see below) 40478344Sobrien# 40598186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 40698186Sgordon# ${rc_arg}_cmd method in the default 40798186Sgordon# operation (i.e, after checking for required 40898186Sgordon# bits and process (non)existence). 40978344Sobrien# If this completes with a non-zero exit code, 41098186Sgordon# don't run ${rc_arg}_cmd. 41178344Sobrien# 41298186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 41398186Sgordon# ${rc_arg}_cmd method, if that method 41498186Sgordon# returned a zero exit code. 41598186Sgordon# 41678344Sobrien# required_dirs n If set, check for the existence of the given 417165565Syar# directories before running a (re)start command. 41878344Sobrien# 41978344Sobrien# required_files n If set, check for the readability of the given 420165565Syar# files before running a (re)start command. 42178344Sobrien# 422165565Syar# required_modules n If set, ensure the given kernel modules are 423165565Syar# loaded before running a (re)start command. 424165565Syar# The check and possible loads are actually 425165565Syar# done after start_precmd so that the modules 426165565Syar# aren't loaded in vain, should the precmd 427165565Syar# return a non-zero status to indicate a error. 428165565Syar# If a word in the list looks like "foo:bar", 429165565Syar# "foo" is the KLD file name and "bar" is the 430165565Syar# module name. If a word looks like "foo~bar", 431165565Syar# "foo" is the KLD file name and "bar" is a 432165565Syar# egrep(1) pattern matching the module name. 433165565Syar# Otherwise the module name is assumed to be 434165565Syar# the same as the KLD file name, which is most 435165565Syar# common. See load_kld(). 436165565Syar# 43778344Sobrien# required_vars n If set, perform checkyesno on each of the 43878344Sobrien# listed variables before running the default 43978344Sobrien# (re)start command. 44078344Sobrien# 44198186Sgordon# Default behaviour for a given argument, if no override method is 44298186Sgordon# provided: 44378344Sobrien# 44498186Sgordon# Argument Default behaviour 44598186Sgordon# -------- ----------------- 44678344Sobrien# start if !running && checkyesno ${rcvar} 44778344Sobrien# ${command} 44878344Sobrien# 44978344Sobrien# stop if ${pidfile} 45098186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 45178344Sobrien# else 45298186Sgordon# rc_pid=$(check_process $command) 45398186Sgordon# kill $sig_stop $rc_pid 45498186Sgordon# wait_for_pids $rc_pid 45598186Sgordon# ($sig_stop defaults to TERM.) 45678344Sobrien# 45798186Sgordon# reload Similar to stop, except use $sig_reload instead, 45898186Sgordon# and doesn't wait_for_pids. 45978344Sobrien# $sig_reload defaults to HUP. 460151685Syar# Note that `reload' isn't provided by default, 461151685Syar# it should be enabled via $extra_commands. 46278344Sobrien# 46378344Sobrien# restart Run `stop' then `start'. 46478344Sobrien# 46598186Sgordon# status Show if ${command} is running, etc. 46678344Sobrien# 46798186Sgordon# poll Wait for ${command} to exit. 46898186Sgordon# 46998186Sgordon# rcvar Display what rc.conf variable is used (if any). 47098186Sgordon# 47198186Sgordon# Variables available to methods, and after run_rc_command() has 47298186Sgordon# completed: 47398186Sgordon# 47498186Sgordon# Variable Purpose 47598186Sgordon# -------- ------- 476126303Smtm# rc_arg Argument to command, after fast/force/one processing 47798186Sgordon# performed 47898186Sgordon# 47998186Sgordon# rc_flags Flags to start the default command with. 48098186Sgordon# Defaults to ${name}_flags, unless overridden 48198186Sgordon# by $flags from the environment. 48298186Sgordon# This variable may be changed by the precmd method. 48398186Sgordon# 48498186Sgordon# rc_pid PID of command (if appropriate) 48598186Sgordon# 48698186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 48798186Sgordon# 48898186Sgordon# rc_force Not empty if "force" was provided (q.v.) 48998186Sgordon# 49098186Sgordon# 49178344Sobrienrun_rc_command() 49278344Sobrien{ 493116097Smtm _return=0 49498186Sgordon rc_arg=$1 49578344Sobrien if [ -z "$name" ]; then 49698186Sgordon err 3 'run_rc_command: $name is not set.' 49778344Sobrien fi 49878344Sobrien 499132892Smtm # Don't repeat the first argument when passing additional command- 500132892Smtm # line arguments to the command subroutines. 501132892Smtm # 502132892Smtm shift 1 503132892Smtm rc_extra_args="$*" 504132892Smtm 505126303Smtm _rc_prefix= 50698186Sgordon case "$rc_arg" in 50778344Sobrien fast*) # "fast" prefix; don't check pid 50898186Sgordon rc_arg=${rc_arg#fast} 50998186Sgordon rc_fast=yes 51078344Sobrien ;; 511126303Smtm force*) # "force prefix; always run 51298186Sgordon rc_force=yes 513126303Smtm _rc_prefix=force 514126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 51578344Sobrien if [ -n "${rcvar}" ]; then 51678344Sobrien eval ${rcvar}=YES 51778344Sobrien fi 51878344Sobrien ;; 519126303Smtm one*) # "one" prefix; set ${rcvar}=yes 520126303Smtm _rc_prefix=one 521126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 522126303Smtm if [ -n "${rcvar}" ]; then 523126303Smtm eval ${rcvar}=YES 524126303Smtm fi 525126303Smtm ;; 52678344Sobrien esac 52778344Sobrien 528161530Sflz eval _override_command=\$${name}_program 529161530Sflz command=${command:+${_override_command:-$command}} 530161530Sflz 53178344Sobrien _keywords="start stop restart rcvar $extra_commands" 53298186Sgordon rc_pid= 53378344Sobrien _pidcmd= 53498186Sgordon _procname=${procname:-${command}} 53598186Sgordon 536131135Smtm # setup pid check command 537131135Smtm if [ -n "$_procname" ]; then 53878344Sobrien if [ -n "$pidfile" ]; then 53998186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 54098186Sgordon else 54198186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 54278344Sobrien fi 54378344Sobrien if [ -n "$_pidcmd" ]; then 54498186Sgordon _keywords="${_keywords} status poll" 54578344Sobrien fi 54678344Sobrien fi 54778344Sobrien 54898186Sgordon if [ -z "$rc_arg" ]; then 549150796Syar rc_usage $_keywords 55078344Sobrien fi 55178344Sobrien 55278344Sobrien if [ -n "$flags" ]; then # allow override from environment 55398186Sgordon rc_flags=$flags 55478344Sobrien else 55598186Sgordon eval rc_flags=\$${name}_flags 55678344Sobrien fi 55798186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 55898186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 55998186Sgordon _group=\$${name}_group _groups=\$${name}_groups 56078344Sobrien 56198186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 562124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 56398186Sgordon unset _user 56498186Sgordon fi 56598186Sgordon fi 56698186Sgordon 56778344Sobrien # if ${rcvar} is set, and $1 is not 56898186Sgordon # "rcvar", then run 56978344Sobrien # checkyesno ${rcvar} 57078344Sobrien # and return if that failed 57178344Sobrien # 57298186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 57378344Sobrien if ! checkyesno ${rcvar}; then 57478344Sobrien return 0 57578344Sobrien fi 57678344Sobrien fi 57778344Sobrien 57878344Sobrien eval $_pidcmd # determine the pid if necessary 57978344Sobrien 58078344Sobrien for _elem in $_keywords; do 58198186Sgordon if [ "$_elem" != "$rc_arg" ]; then 58278344Sobrien continue 58378344Sobrien fi 58478344Sobrien # if there's a custom ${XXX_cmd}, 58578344Sobrien # run that instead of the default 58678344Sobrien # 587165565Syar eval _cmd=\$${rc_arg}_cmd \ 588165565Syar _precmd=\$${rc_arg}_precmd \ 589165565Syar _postcmd=\$${rc_arg}_postcmd 590165565Syar 59178344Sobrien if [ -n "$_cmd" ]; then 592165565Syar _run_rc_precmd || return 1 593165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 594165565Syar _run_rc_postcmd 595116097Smtm return $_return 59678344Sobrien fi 59778344Sobrien 59898186Sgordon case "$rc_arg" in # default operations... 59978344Sobrien 60078344Sobrien status) 601165565Syar _run_rc_precmd || return 1 60298186Sgordon if [ -n "$rc_pid" ]; then 60398186Sgordon echo "${name} is running as pid $rc_pid." 60478344Sobrien else 60578344Sobrien echo "${name} is not running." 60678344Sobrien return 1 60778344Sobrien fi 608165565Syar _run_rc_postcmd 60978344Sobrien ;; 61078344Sobrien 61178344Sobrien start) 612131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 613157473Sflz echo 1>&2 "${name} already running? (pid=$rc_pid)." 614153152Syar return 1 61578344Sobrien fi 61678344Sobrien 617167413Syar if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then 618160667Syar warn "run_rc_command: cannot run $command" 619153152Syar return 1 62078344Sobrien fi 62178344Sobrien 622165565Syar _run_rc_precmd || return 1 62378344Sobrien 624160668Syar # setup the full command to run 62578344Sobrien # 62678344Sobrien echo "Starting ${name}." 62778344Sobrien if [ -n "$_chroot" ]; then 62878344Sobrien _doit="\ 62978344Sobrien${_nice:+nice -n $_nice }\ 63078344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 63198186Sgordon$_chroot $command $rc_flags $command_args" 63278344Sobrien else 63378344Sobrien _doit="\ 634161396Syar${_chdir:+cd $_chdir && }\ 63598186Sgordon$command $rc_flags $command_args" 63698186Sgordon if [ -n "$_user" ]; then 63798186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 63898186Sgordon fi 639161396Syar if [ -n "$_nice" ]; then 640161396Syar if [ -z "$_user" ]; then 641161396Syar _doit="sh -c \"$_doit\"" 642161396Syar fi 643161396Syar _doit="nice -n $_nice $_doit" 644161396Syar fi 64578344Sobrien fi 64698186Sgordon 647165565Syar # run the full command 64898186Sgordon # 649165565Syar _run_rc_doit "$_doit" || return 1 65098186Sgordon 65198186Sgordon # finally, run postcmd 65298186Sgordon # 653165565Syar _run_rc_postcmd 65478344Sobrien ;; 65578344Sobrien 65678344Sobrien stop) 65798186Sgordon if [ -z "$rc_pid" ]; then 658153152Syar [ -n "$rc_fast" ] && return 0 659165565Syar _run_rc_notrunning 660153152Syar return 1 66178344Sobrien fi 66278344Sobrien 663165565Syar _run_rc_precmd || return 1 66498186Sgordon 66598186Sgordon # send the signal to stop 66698186Sgordon # 66778344Sobrien echo "Stopping ${name}." 668165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 669165565Syar _run_rc_doit "$_doit" || return 1 67098186Sgordon 67198186Sgordon # wait for the command to exit, 67298186Sgordon # and run postcmd. 67398186Sgordon wait_for_pids $rc_pid 674165565Syar 675165565Syar _run_rc_postcmd 67678344Sobrien ;; 67778344Sobrien 67878344Sobrien reload) 67998186Sgordon if [ -z "$rc_pid" ]; then 680165565Syar _run_rc_notrunning 681153152Syar return 1 68278344Sobrien fi 683165565Syar 684165565Syar _run_rc_precmd || return 1 685165565Syar 686165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 687165565Syar _run_rc_doit "$_doit" || return 1 688165565Syar 689165565Syar _run_rc_postcmd 69078344Sobrien ;; 69178344Sobrien 69278344Sobrien restart) 69378344Sobrien # prevent restart being called more 69478344Sobrien # than once by any given script 69578344Sobrien # 696126285Smtm if ${_rc_restart_done:-false}; then 69778344Sobrien return 0 69878344Sobrien fi 699126285Smtm _rc_restart_done=true 70078344Sobrien 701165565Syar _run_rc_precmd || return 1 702165565Syar 703165565Syar # run those in a subshell to keep global variables 704152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 705165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 706165565Syar _return=$? 707165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 70898186Sgordon 709165565Syar _run_rc_postcmd 71078344Sobrien ;; 71178344Sobrien 71298186Sgordon poll) 713165565Syar _run_rc_precmd || return 1 71498186Sgordon if [ -n "$rc_pid" ]; then 71598186Sgordon wait_for_pids $rc_pid 71698186Sgordon fi 717165565Syar _run_rc_postcmd 71898186Sgordon ;; 71998186Sgordon 72078344Sobrien rcvar) 72178344Sobrien echo "# $name" 72278344Sobrien if [ -n "$rcvar" ]; then 72378344Sobrien if checkyesno ${rcvar}; then 724164629Sflz echo "${rcvar}=YES" 72578344Sobrien else 726164629Sflz echo "${rcvar}=NO" 72778344Sobrien fi 72878344Sobrien fi 72978344Sobrien ;; 73078344Sobrien 73178344Sobrien *) 732150796Syar rc_usage $_keywords 73378344Sobrien ;; 73478344Sobrien 73578344Sobrien esac 736116097Smtm return $_return 73778344Sobrien done 73878344Sobrien 73998186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 740150796Syar rc_usage $_keywords 741153152Syar # not reached 74278344Sobrien} 74378344Sobrien 74478344Sobrien# 745165565Syar# Helper functions for run_rc_command: common code. 746165565Syar# They use such global variables besides the exported rc_* ones: 747165565Syar# 748165565Syar# name R/W 749165565Syar# ------------------ 750165565Syar# _precmd R 751165565Syar# _postcmd R 752165565Syar# _return W 753165565Syar# 754165565Syar_run_rc_precmd() 755165565Syar{ 756165565Syar check_required_before "$rc_arg" || return 1 757165565Syar 758165565Syar if [ -n "$_precmd" ]; then 759165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 760165565Syar eval "$_precmd $rc_extra_args" 761165565Syar _return=$? 762165565Syar 763165565Syar # If precmd failed and force isn't set, request exit. 764165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 765165565Syar return 1 766165565Syar fi 767165565Syar fi 768165565Syar 769165565Syar check_required_after "$rc_arg" || return 1 770165565Syar 771165565Syar return 0 772165565Syar} 773165565Syar 774165565Syar_run_rc_postcmd() 775165565Syar{ 776165565Syar if [ -n "$_postcmd" ]; then 777165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 778165565Syar eval "$_postcmd $rc_extra_args" 779165565Syar _return=$? 780165565Syar fi 781165565Syar return 0 782165565Syar} 783165565Syar 784165565Syar_run_rc_doit() 785165565Syar{ 786165565Syar debug "run_rc_command: doit: $*" 787165565Syar eval "$@" 788165565Syar _return=$? 789165565Syar 790165565Syar # If command failed and force isn't set, request exit. 791165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 792165565Syar return 1 793165565Syar fi 794165565Syar 795165565Syar return 0 796165565Syar} 797165565Syar 798165565Syar_run_rc_notrunning() 799165565Syar{ 800165565Syar local _pidmsg 801165565Syar 802165565Syar if [ -n "$pidfile" ]; then 803165565Syar _pidmsg=" (check $pidfile)." 804165565Syar else 805165565Syar _pidmsg= 806165565Syar fi 807165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 808165565Syar} 809165565Syar 810165565Syar_run_rc_killcmd() 811165565Syar{ 812165565Syar local _cmd 813165565Syar 814165565Syar _cmd="kill -$1 $rc_pid" 815165565Syar if [ -n "$_user" ]; then 816165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 817165565Syar fi 818165565Syar echo "$_cmd" 819165565Syar} 820165565Syar 821165565Syar# 82278344Sobrien# run_rc_script file arg 82378344Sobrien# Start the script `file' with `arg', and correctly handle the 82478344Sobrien# return value from the script. If `file' ends with `.sh', it's 82598186Sgordon# sourced into the current environment. If `file' appears to be 82698186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 82798186Sgordon# executable run as a child process. 82878344Sobrien# 82978344Sobrienrun_rc_script() 83078344Sobrien{ 83178344Sobrien _file=$1 83278344Sobrien _arg=$2 83378344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 83478344Sobrien err 3 'USAGE: run_rc_script file arg' 83578344Sobrien fi 83678344Sobrien 83798186Sgordon unset name command command_args command_interpreter \ 83898186Sgordon extra_commands pidfile procname \ 83998186Sgordon rcvar required_dirs required_files required_vars 84098186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 84198186Sgordon 84278344Sobrien case "$_file" in 843153105Sdougb /etc/rc.d/*.sh) # run in current shell 844146490Sschweikh set $_arg; . $_file 84578344Sobrien ;; 846153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 84798186Sgordon warn "Ignoring scratch file $_file" 84898186Sgordon ;; 84978344Sobrien *) # run in subshell 85098186Sgordon if [ -x $_file ]; then 85198186Sgordon if [ -n "$rc_fast_and_loose" ]; then 852146490Sschweikh set $_arg; . $_file 85398186Sgordon else 854130161Smtm ( trap "echo Script $_file interrupted; kill -QUIT $$" 3 855130161Smtm trap "echo Script $_file interrupted; exit 1" 2 856146490Sschweikh set $_arg; . $_file ) 85798186Sgordon fi 85898186Sgordon fi 85978344Sobrien ;; 86078344Sobrien esac 86178344Sobrien} 86278344Sobrien 86378344Sobrien# 864157653Sflz# load_rc_config name 865157653Sflz# Source in the configuration file for a given name. 86678344Sobrien# 86778344Sobrienload_rc_config() 86878344Sobrien{ 869157653Sflz _name=$1 870157653Sflz if [ -z "$_name" ]; then 871157653Sflz err 3 'USAGE: load_rc_config name' 87278344Sobrien fi 87378344Sobrien 874126285Smtm if ${_rc_conf_loaded:-false}; then 875126285Smtm : 876126285Smtm else 87798186Sgordon if [ -r /etc/defaults/rc.conf ]; then 87898186Sgordon debug "Sourcing /etc/defaults/rc.conf" 87998186Sgordon . /etc/defaults/rc.conf 88098186Sgordon source_rc_confs 88198186Sgordon elif [ -r /etc/rc.conf ]; then 88298186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 88398186Sgordon . /etc/rc.conf 88498186Sgordon fi 885126285Smtm _rc_conf_loaded=true 88698186Sgordon fi 887161530Sflz if [ -f /etc/rc.conf.d/"$_name" ]; then 888157653Sflz debug "Sourcing /etc/rc.conf.d/${_name}" 889161530Sflz . /etc/rc.conf.d/"$_name" 890157653Sflz fi 891157653Sflz 892101850Sgordon # XXX - Deprecated variable name support 893101850Sgordon # 894103018Sgordon case ${OSTYPE} in 895101850Sgordon FreeBSD) 896146490Sschweikh [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 897146490Sschweikh [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 898146490Sschweikh [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 899146490Sschweikh [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 900146490Sschweikh [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 901146490Sschweikh [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 902146490Sschweikh [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 903115950Smtm [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program" 904115950Smtm [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags" 905146490Sschweikh ;; 906101850Sgordon esac 90778344Sobrien} 908157473Sflz 909157473Sflz# 910157653Sflz# load_rc_config_var name var 911157653Sflz# Read the rc.conf(5) var for name and set in the 912157473Sflz# current shell, using load_rc_config in a subshell to prevent 913157473Sflz# unwanted side effects from other variable assignments. 914157473Sflz# 915157473Sflzload_rc_config_var() 916157473Sflz{ 917157473Sflz if [ $# -ne 2 ]; then 918157653Sflz err 3 'USAGE: load_rc_config_var name var' 919157473Sflz fi 920157473Sflz eval $(eval '( 921157473Sflz load_rc_config '$1' >/dev/null; 922157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 923157473Sflz echo '$2'=\'\''${'$2'}\'\''; 924157473Sflz fi 925157473Sflz )' ) 926157473Sflz} 92778344Sobrien 92878344Sobrien# 92978344Sobrien# rc_usage commands 93078344Sobrien# Print a usage string for $0, with `commands' being a list of 93178344Sobrien# valid commands. 93278344Sobrien# 93378344Sobrienrc_usage() 93478344Sobrien{ 935126303Smtm echo -n 1>&2 "Usage: $0 [fast|force|one](" 93678344Sobrien 93778344Sobrien _sep= 938126286Smtm for _elem; do 93978344Sobrien echo -n 1>&2 "$_sep$_elem" 94078344Sobrien _sep="|" 94178344Sobrien done 94278344Sobrien echo 1>&2 ")" 94378344Sobrien exit 1 94478344Sobrien} 94578344Sobrien 94678344Sobrien# 94778344Sobrien# err exitval message 94878344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 94978344Sobrien# 95078344Sobrienerr() 95178344Sobrien{ 95278344Sobrien exitval=$1 95378344Sobrien shift 95478344Sobrien 955106643Sgordon if [ -x /usr/bin/logger ]; then 956106643Sgordon logger "$0: ERROR: $*" 957106643Sgordon fi 958106643Sgordon echo 1>&2 "$0: ERROR: $*" 95978344Sobrien exit $exitval 96078344Sobrien} 96178344Sobrien 96278344Sobrien# 96378344Sobrien# warn message 96478344Sobrien# Display message to stderr and log to the syslog. 96578344Sobrien# 96678344Sobrienwarn() 96778344Sobrien{ 968106643Sgordon if [ -x /usr/bin/logger ]; then 969106643Sgordon logger "$0: WARNING: $*" 970106643Sgordon fi 971106643Sgordon echo 1>&2 "$0: WARNING: $*" 97278344Sobrien} 97398186Sgordon 97498186Sgordon# 97598186Sgordon# info message 97698186Sgordon# Display informational message to stdout and log to syslog. 97798186Sgordon# 97898186Sgordoninfo() 97998186Sgordon{ 980119170Smtm case ${rc_info} in 981119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 982119170Smtm if [ -x /usr/bin/logger ]; then 983119170Smtm logger "$0: INFO: $*" 984119170Smtm fi 985119170Smtm echo "$0: INFO: $*" 986119170Smtm ;; 987119170Smtm esac 98898186Sgordon} 98998186Sgordon 99098186Sgordon# 99198186Sgordon# debug message 992106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 99398186Sgordon# BEWARE that you don't call any subroutine that itself calls this 99498186Sgordon# function. 99598186Sgordon# 99698186Sgordondebug() 99798186Sgordon{ 99898186Sgordon case ${rc_debug} in 99998186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1000106700Sgordon if [ -x /usr/bin/logger ]; then 1001162947Syar logger "$0: DEBUG: $*" 1002106700Sgordon fi 1003146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 100498186Sgordon ;; 100598186Sgordon esac 100698186Sgordon} 100798186Sgordon 100898186Sgordon# 100998186Sgordon# backup_file action file cur backup 101098186Sgordon# Make a backup copy of `file' into `cur', and save the previous 101198186Sgordon# version of `cur' as `backup' or use rcs for archiving. 101298186Sgordon# 101398186Sgordon# This routine checks the value of the backup_uses_rcs variable, 101498186Sgordon# which can be either YES or NO. 101598186Sgordon# 101698186Sgordon# The `action' keyword can be one of the following: 101798186Sgordon# 101898186Sgordon# add `file' is now being backed up (and is possibly 101998186Sgordon# being reentered into the backups system). `cur' 102098186Sgordon# is created and RCS files, if necessary, are 102198186Sgordon# created as well. 102298186Sgordon# 102398186Sgordon# update `file' has changed and needs to be backed up. 102498186Sgordon# If `cur' exists, it is copied to to `back' or 102598186Sgordon# checked into RCS (if the repository file is old), 102698186Sgordon# and then `file' is copied to `cur'. Another RCS 102798186Sgordon# check in done here if RCS is being used. 102898186Sgordon# 102998186Sgordon# remove `file' is no longer being tracked by the backups 103098186Sgordon# system. If RCS is not being used, `cur' is moved 103198186Sgordon# to `back', otherwise an empty file is checked in, 103298186Sgordon# and then `cur' is removed. 103398186Sgordon# 103498186Sgordon# 103598186Sgordonbackup_file() 103698186Sgordon{ 103798186Sgordon _action=$1 103898186Sgordon _file=$2 103998186Sgordon _cur=$3 104098186Sgordon _back=$4 104198186Sgordon 104298186Sgordon if checkyesno backup_uses_rcs; then 104398186Sgordon _msg0="backup archive" 104498186Sgordon _msg1="update" 104598186Sgordon 104698186Sgordon # ensure that history file is not locked 104798186Sgordon if [ -f $_cur,v ]; then 104898186Sgordon rcs -q -u -U -M $_cur 104998186Sgordon fi 105098186Sgordon 105198186Sgordon # ensure after switching to rcs that the 105298186Sgordon # current backup is not lost 105398186Sgordon if [ -f $_cur ]; then 105498186Sgordon # no archive, or current newer than archive 105598186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 105698186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 105798186Sgordon rcs -q -kb -U $_cur 105898186Sgordon co -q -f -u $_cur 105998186Sgordon fi 106098186Sgordon fi 106198186Sgordon 106298186Sgordon case $_action in 106398186Sgordon add|update) 106498186Sgordon cp -p $_file $_cur 106598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 106698186Sgordon rcs -q -kb -U $_cur 106798186Sgordon co -q -f -u $_cur 106898186Sgordon chown root:wheel $_cur $_cur,v 106998186Sgordon ;; 107098186Sgordon remove) 107198186Sgordon cp /dev/null $_cur 107298186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 107398186Sgordon rcs -q -kb -U $_cur 107498186Sgordon chown root:wheel $_cur $_cur,v 107598186Sgordon rm $_cur 107698186Sgordon ;; 107798186Sgordon esac 107898186Sgordon else 107998186Sgordon case $_action in 108098186Sgordon add|update) 108198186Sgordon if [ -f $_cur ]; then 108298186Sgordon cp -p $_cur $_back 108398186Sgordon fi 108498186Sgordon cp -p $_file $_cur 108598186Sgordon chown root:wheel $_cur 108698186Sgordon ;; 108798186Sgordon remove) 108898186Sgordon mv -f $_cur $_back 108998186Sgordon ;; 109098186Sgordon esac 109198186Sgordon fi 109298186Sgordon} 1093119166Smtm 1094123344Smtm# make_symlink src link 1095123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1096123344Smtm# directory in which link is to be created does not exist 1097123344Smtm# a warning will be displayed and an error will be returned. 1098123344Smtm# Returns 0 on sucess, 1 otherwise. 1099119166Smtm# 1100123344Smtmmake_symlink() 1101119166Smtm{ 1102123344Smtm local src link linkdir _me 1103123344Smtm src="$1" 1104123344Smtm link="$2" 1105123344Smtm linkdir="`dirname $link`" 1106123344Smtm _me="make_symlink()" 1107119166Smtm 1108123344Smtm if [ -z "$src" -o -z "$link" ]; then 1109123344Smtm warn "$_me: requires two arguments." 1110119166Smtm return 1 1111119166Smtm fi 1112123344Smtm if [ ! -d "$linkdir" ]; then 1113160667Syar warn "$_me: the directory $linkdir does not exist." 1114119166Smtm return 1 1115119166Smtm fi 1116146490Sschweikh if ! ln -sf $src $link; then 1117123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1118119166Smtm return 1 1119119166Smtm fi 1120119166Smtm return 0 1121119166Smtm} 1122119166Smtm 1123119166Smtm# devfs_rulesets_from_file file 1124119166Smtm# Reads a set of devfs commands from file, and creates 1125119166Smtm# the specified rulesets with their rules. Returns non-zero 1126119166Smtm# if there was an error. 1127119166Smtm# 1128119166Smtmdevfs_rulesets_from_file() 1129119166Smtm{ 1130119166Smtm local file _err _me 1131119166Smtm file="$1" 1132119166Smtm _me="devfs_rulesets_from_file" 1133119166Smtm _err=0 1134119166Smtm 1135119166Smtm if [ -z "$file" ]; then 1136119166Smtm warn "$_me: you must specify a file" 1137119166Smtm return 1 1138119166Smtm fi 1139119166Smtm if [ ! -e "$file" ]; then 1140119166Smtm debug "$_me: no such file ($file)" 1141119166Smtm return 0 1142119166Smtm fi 1143119166Smtm debug "reading rulesets from file ($file)" 1144119166Smtm { while read line 1145119166Smtm do 1146119166Smtm case $line in 1147119166Smtm \#*) 1148119166Smtm continue 1149119166Smtm ;; 1150119166Smtm \[*\]*) 1151119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1152119166Smtm if [ -z "$rulenum" ]; then 1153119166Smtm warn "$_me: cannot extract rule number ($line)" 1154119166Smtm _err=1 1155119166Smtm break 1156119166Smtm fi 1157119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1158119166Smtm if [ -z "$rulename" ]; then 1159119166Smtm warn "$_me: cannot extract rule name ($line)" 1160119166Smtm _err=1 1161119166Smtm break; 1162119166Smtm fi 1163119166Smtm eval $rulename=\$rulenum 1164119166Smtm debug "found ruleset: $rulename=$rulenum" 1165146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1166119166Smtm _err=1 1167119166Smtm break 1168119166Smtm fi 1169119166Smtm ;; 1170119166Smtm *) 1171119166Smtm rulecmd="${line%%"\#*"}" 1172119166Smtm # evaluate the command incase it includes 1173119166Smtm # other rules 1174119166Smtm if [ -n "$rulecmd" ]; then 1175119166Smtm debug "adding rule ($rulecmd)" 1176119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1177119166Smtm then 1178119166Smtm _err=1 1179119166Smtm break 1180119166Smtm fi 1181119166Smtm fi 1182119166Smtm ;; 1183119166Smtm esac 1184119166Smtm if [ $_err -ne 0 ]; then 1185119166Smtm debug "error in $_me" 1186119166Smtm break 1187119166Smtm fi 1188119166Smtm done } < $file 1189119166Smtm return $_err 1190119166Smtm} 1191119166Smtm 1192119166Smtm# devfs_init_rulesets 1193119166Smtm# Initializes rulesets from configuration files. Returns 1194119166Smtm# non-zero if there was an error. 1195119166Smtm# 1196119166Smtmdevfs_init_rulesets() 1197119166Smtm{ 1198119166Smtm local file _me 1199119166Smtm _me="devfs_init_rulesets" 1200119166Smtm 1201119166Smtm # Go through this only once 1202119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1203119166Smtm debug "$_me: devfs rulesets already initialized" 1204119166Smtm return 1205119166Smtm fi 1206146490Sschweikh for file in $devfs_rulesets; do 1207119166Smtm devfs_rulesets_from_file $file || return 1 1208119166Smtm done 1209119166Smtm devfs_rulesets_init=1 1210119166Smtm debug "$_me: devfs rulesets initialized" 1211119166Smtm return 0 1212119166Smtm} 1213119166Smtm 1214119166Smtm# devfs_set_ruleset ruleset [dir] 1215151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1216119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1217119166Smtm# Returns non-zero if it could not set it successfully. 1218119166Smtm# 1219119166Smtmdevfs_set_ruleset() 1220119166Smtm{ 1221119166Smtm local devdir rs _me 1222119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1223119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1224119166Smtm _me="devfs_set_ruleset" 1225119166Smtm 1226119166Smtm if [ -z "$rs" ]; then 1227119166Smtm warn "$_me: you must specify a ruleset number" 1228119166Smtm return 1 1229119166Smtm fi 1230119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1231146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1232119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1233119166Smtm return 1 1234119166Smtm fi 1235119166Smtm return 0 1236119166Smtm} 1237119166Smtm 1238119166Smtm# devfs_apply_ruleset ruleset [dir] 1239119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1240119166Smtm# The ruleset argument must be a ruleset name as specified 1241119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1242119166Smtm# if it could not apply the ruleset. 1243119166Smtm# 1244119166Smtmdevfs_apply_ruleset() 1245119166Smtm{ 1246119166Smtm local devdir rs _me 1247119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1248119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1249119166Smtm _me="devfs_apply_ruleset" 1250119166Smtm 1251119166Smtm if [ -z "$rs" ]; then 1252119166Smtm warn "$_me: you must specify a ruleset" 1253119166Smtm return 1 1254119166Smtm fi 1255119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1256146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1257119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1258119166Smtm return 1 1259119166Smtm fi 1260119166Smtm return 0 1261119166Smtm} 1262119166Smtm 1263119166Smtm# devfs_domount dir [ruleset] 1264119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1265119166Smtm# on the mount-point. It must also be a ruleset name as specified 1266119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1267119166Smtm# 1268119166Smtmdevfs_domount() 1269119166Smtm{ 1270119166Smtm local devdir rs _me 1271119166Smtm devdir="$1" 1272119166Smtm [ -n "$2" ] && rs=$2 || rs= 1273119166Smtm _me="devfs_domount()" 1274119166Smtm 1275119166Smtm if [ -z "$devdir" ]; then 1276119166Smtm warn "$_me: you must specify a mount-point" 1277119166Smtm return 1 1278119166Smtm fi 1279119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1280146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1281119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1282119166Smtm return 1 1283119166Smtm fi 1284119166Smtm if [ -n "$rs" ]; then 1285119166Smtm devfs_init_rulesets 1286119166Smtm devfs_set_ruleset $rs $devdir 1287124797Scperciva devfs -m $devdir rule applyset 1288119166Smtm fi 1289119166Smtm return 0 1290119166Smtm} 1291119166Smtm 1292119166Smtm# devfs_mount_jail dir [ruleset] 1293119166Smtm# Mounts a devfs file system appropriate for jails 1294119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1295119166Smtm# it names will be used instead. If present, ruleset must 1296119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1297119166Smtm# This function returns non-zero if an error occurs. 1298119166Smtm# 1299119166Smtmdevfs_mount_jail() 1300119166Smtm{ 1301119166Smtm local jdev rs _me 1302119166Smtm jdev="$1" 1303119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1304119166Smtm _me="devfs_mount_jail" 1305119166Smtm 1306119166Smtm devfs_init_rulesets 1307146490Sschweikh if ! devfs_domount "$jdev" $rs; then 1308119166Smtm warn "$_me: devfs was not mounted on $jdev" 1309119166Smtm return 1 1310119166Smtm fi 1311119166Smtm return 0 1312119166Smtm} 1313127345Sbrooks 1314127345Sbrooks# Provide a function for normalizing the mounting of memory 1315127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1316127345Sbrooks# as close as possible between 5-current and 4-stable. 1317127345Sbrooks# $1 = size 1318127345Sbrooks# $2 = mount point 1319137451Skeramida# $3 = (optional) extra mdmfs flags 1320146490Sschweikhmount_md() 1321146490Sschweikh{ 1322127345Sbrooks if [ -n "$3" ]; then 1323137451Skeramida flags="$3" 1324127345Sbrooks fi 1325149421Syar /sbin/mdmfs $flags -s $1 md $2 1326127345Sbrooks} 1327131550Scperciva 1328159828Syar# Code common to scripts that need to load a kernel module 1329159828Syar# if it isn't in the kernel yet. Syntax: 1330160666Syar# load_kld [-e regex] [-m module] file 1331159828Syar# where -e or -m chooses the way to check if the module 1332159828Syar# is already loaded: 1333160666Syar# regex is egrep'd in the output from `kldstat -v', 1334160666Syar# module is passed to `kldstat -m'. 1335160666Syar# The default way is as though `-m file' were specified. 1336159828Syarload_kld() 1337159828Syar{ 1338159828Syar local _loaded _mod _opt _re 1339159828Syar 1340159828Syar while getopts "e:m:" _opt; do 1341159828Syar case "$_opt" in 1342159828Syar e) _re="$OPTARG" ;; 1343159828Syar m) _mod="$OPTARG" ;; 1344160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1345159828Syar esac 1346159828Syar done 1347159828Syar shift $(($OPTIND - 1)) 1348160666Syar if [ $# -ne 1 ]; then 1349160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1350160666Syar fi 1351159828Syar _mod=${_mod:-$1} 1352159828Syar _loaded=false 1353159828Syar if [ -n "$_re" ]; then 1354159828Syar if kldstat -v | egrep -q -e "$_re"; then 1355159828Syar _loaded=true 1356159828Syar fi 1357159828Syar else 1358159828Syar if kldstat -q -m "$_mod"; then 1359159828Syar _loaded=true 1360159828Syar fi 1361159828Syar fi 1362159828Syar if ! $_loaded; then 1363159828Syar if ! kldload "$1"; then 1364159828Syar warn "Unable to load kernel module $1" 1365159828Syar return 1 1366160666Syar else 1367160666Syar info "$1 kernel module loaded." 1368159828Syar fi 1369160666Syar else 1370160666Syar debug "load_kld: $1 kernel module already loaded." 1371159828Syar fi 1372159828Syar return 0 1373159828Syar} 1374159828Syar 1375149049Spjd# ltr str src dst 1376149049Spjd# Change every $src in $str to $dst. 1377149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1378149049Spjd# awk(1). 1379149049Spjdltr() 1380149049Spjd{ 1381149049Spjd local _str _src _dst _out _com 1382149049Spjd _str=$1 1383149049Spjd _src=$2 1384149049Spjd _dst=$3 1385149049Spjd _out="" 1386149049Spjd 1387149049Spjd IFS=${_src} 1388149049Spjd for _com in ${_str}; do 1389149049Spjd if [ -z "${_out}" ]; then 1390149049Spjd _out="${_com}" 1391149049Spjd else 1392149049Spjd _out="${_out}${_dst}${_com}" 1393149049Spjd fi 1394149049Spjd done 1395149049Spjd echo "${_out}" 1396149049Spjd} 1397149049Spjd 1398149050Spjd# Creates a list of providers for GELI encryption. 1399149050Spjdgeli_make_list() 1400149050Spjd{ 1401149050Spjd local devices devices2 1402149050Spjd local provider mountpoint type options rest 1403149050Spjd 1404149050Spjd # Create list of GELI providers from fstab. 1405149050Spjd while read provider mountpoint type options rest ; do 1406155570Sflz case ":${options}" in 1407155570Sflz :*noauto*) 1408155570Sflz noauto=yes 1409155570Sflz ;; 1410155570Sflz *) 1411155570Sflz noauto=no 1412155570Sflz ;; 1413155570Sflz esac 1414155570Sflz 1415149050Spjd case ":${provider}" in 1416149050Spjd :#*) 1417149050Spjd continue 1418149050Spjd ;; 1419149050Spjd *.eli) 1420149050Spjd # Skip swap devices. 1421155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1422149050Spjd continue 1423149050Spjd fi 1424149050Spjd devices="${devices} ${provider}" 1425149050Spjd ;; 1426149050Spjd esac 1427149050Spjd done < /etc/fstab 1428149050Spjd 1429149050Spjd # Append providers from geli_devices. 1430149050Spjd devices="${devices} ${geli_devices}" 1431149050Spjd 1432149050Spjd for provider in ${devices}; do 1433149050Spjd provider=${provider%.eli} 1434149050Spjd provider=${provider#/dev/} 1435149050Spjd devices2="${devices2} ${provider}" 1436149050Spjd done 1437149050Spjd 1438149050Spjd echo ${devices2} 1439149050Spjd} 1440149050Spjd 1441153027Sdougb# Find scripts in local_startup directories that use the old syntax 1442153027Sdougb# 1443153027Sdougbfind_local_scripts_old () { 1444153027Sdougb zlist='' 1445153027Sdougb slist='' 1446153027Sdougb for dir in ${local_startup}; do 1447153027Sdougb if [ -d "${dir}" ]; then 1448153027Sdougb for file in ${dir}/[0-9]*.sh; do 1449153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1450153027Sdougb continue 1451153027Sdougb zlist="$zlist $file" 1452153027Sdougb done 1453153027Sdougb for file in ${dir}/[^0-9]*.sh; do 1454153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1455153027Sdougb continue 1456153027Sdougb slist="$slist $file" 1457153027Sdougb done 1458153027Sdougb fi 1459153027Sdougb done 1460153027Sdougb} 1461153027Sdougb 1462153027Sdougbfind_local_scripts_new () { 1463153027Sdougb local_rc='' 1464153027Sdougb for dir in ${local_startup}; do 1465153027Sdougb if [ -d "${dir}" ]; then 1466153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1467153027Sdougb case "$file" in 1468153027Sdougb *.sample) ;; 1469153027Sdougb *) if [ -x "$file" ]; then 1470153027Sdougb local_rc="${local_rc} ${file}" 1471153027Sdougb fi 1472153027Sdougb ;; 1473153027Sdougb esac 1474153027Sdougb done 1475153027Sdougb fi 1476153027Sdougb done 1477153027Sdougb} 1478153027Sdougb 1479165565Syar# check_required_{before|after} command 1480165565Syar# Check for things required by the command before and after its precmd, 1481165565Syar# respectively. The two separate functions are needed because some 1482165565Syar# conditions should prevent precmd from being run while other things 1483165565Syar# depend on precmd having already been run. 1484165565Syar# 1485165565Syarcheck_required_before() 1486165565Syar{ 1487165565Syar local _f 1488165565Syar 1489165565Syar case "$1" in 1490165565Syar start) 1491165565Syar for _f in $required_vars; do 1492165565Syar if ! checkyesno $_f; then 1493165565Syar warn "\$${_f} is not enabled." 1494165565Syar if [ -z "$rc_force" ]; then 1495165565Syar return 1 1496165565Syar fi 1497165565Syar fi 1498165565Syar done 1499165565Syar 1500165565Syar for _f in $required_dirs; do 1501165565Syar if [ ! -d "${_f}/." ]; then 1502165565Syar warn "${_f} is not a directory." 1503165565Syar if [ -z "$rc_force" ]; then 1504165565Syar return 1 1505165565Syar fi 1506165565Syar fi 1507165565Syar done 1508165565Syar 1509165565Syar for _f in $required_files; do 1510165565Syar if [ ! -r "${_f}" ]; then 1511165565Syar warn "${_f} is not readable." 1512165565Syar if [ -z "$rc_force" ]; then 1513165565Syar return 1 1514165565Syar fi 1515165565Syar fi 1516165565Syar done 1517165565Syar ;; 1518165565Syar esac 1519165565Syar 1520165565Syar return 0 1521165565Syar} 1522165565Syar 1523165565Syarcheck_required_after() 1524165565Syar{ 1525165565Syar local _f _args 1526165565Syar 1527165565Syar case "$1" in 1528165565Syar start) 1529165565Syar for _f in $required_modules; do 1530165565Syar case "${_f}" in 1531165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1532165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1533165565Syar *) _args="${_f}" ;; 1534165565Syar esac 1535165565Syar if ! load_kld ${_args}; then 1536165565Syar if [ -z "$rc_force" ]; then 1537165565Syar return 1 1538165565Syar fi 1539165565Syar fi 1540165565Syar done 1541165565Syar ;; 1542165565Syar esac 1543165565Syar 1544165565Syar return 0 1545165565Syar} 1546165565Syar 1547131550Scpercivafi 1548157841Sflz 1549157841Sflz_rc_subr_loaded=: 1550