rc.subr revision 230103
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 230103 2012-01-14 08:59:02Z dougb $ 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# 1978344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2078344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2178344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2278344Sobrien# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2378344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2478344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2578344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2678344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2778344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2878344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2978344Sobrien# POSSIBILITY OF SUCH DAMAGE. 3078344Sobrien# 3178344Sobrien# rc.subr 3278344Sobrien# functions used by various rc scripts 3378344Sobrien# 3478344Sobrien 35169668Smtm: ${RC_PID:=$$}; export RC_PID 36157473Sflz 3778344Sobrien# 3898186Sgordon# Operating System dependent/independent variables 3998186Sgordon# 4098186Sgordon 41131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then 42131550Scperciva 43131550Scperciva_rc_subr_loaded="YES" 44131550Scperciva 4598186SgordonSYSCTL="/sbin/sysctl" 4698186SgordonSYSCTL_N="${SYSCTL} -n" 47202988SemasteSYSCTL_W="${SYSCTL}" 48124832SmtmID="/usr/bin/id" 49124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 50161435SyarPS="/bin/ps -ww" 51161435SyarJID=`$PS -p $$ -o jid=` 5298186Sgordon 5398186Sgordon# 5478344Sobrien# functions 5578344Sobrien# --------- 5678344Sobrien 57197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg] 58197144Shrs# Define obsolete variable. 59197144Shrs# Global variable $rcvars_obsolete is used. 6098186Sgordon# 61197144Shrsset_rcvar_obsolete() 62197144Shrs{ 63197144Shrs local _var 64197144Shrs _var=$1 65197144Shrs debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 66197144Shrs 67197144Shrs rcvars_obsolete="${rcvars_obsolete# } $1" 68197144Shrs eval ${1}_newvar=\"$2\" 69197144Shrs shift 2 70197144Shrs eval ${_var}_obsolete_msg=\"$*\" 71197144Shrs} 72197144Shrs 73197144Shrs# 7498186Sgordon# force_depend script 7598186Sgordon# Force a service to start. Intended for use by services 7698186Sgordon# to resolve dependency issues. It is assumed the caller 7798186Sgordon# has check to make sure this call is necessary 7898186Sgordon# $1 - filename of script, in /etc/rc.d, to run 7998186Sgordon# 8098186Sgordonforce_depend() 8198186Sgordon{ 8298186Sgordon _depend="$1" 8398186Sgordon 8498186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 85146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 8698186Sgordon warn "Unable to force ${_depend}. It may already be running." 8798186Sgordon return 1 8898186Sgordon fi 8998186Sgordon return 0 9098186Sgordon} 9198186Sgordon 9298186Sgordon# 9378344Sobrien# checkyesno var 9478344Sobrien# Test $1 variable, and warn if not set to YES or NO. 9578344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 9678344Sobrien# 9778344Sobriencheckyesno() 9878344Sobrien{ 9978344Sobrien eval _value=\$${1} 10098186Sgordon debug "checkyesno: $1 is set to $_value." 10178344Sobrien case $_value in 10278344Sobrien 10378344Sobrien # "yes", "true", "on", or "1" 10478344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 10578344Sobrien return 0 10678344Sobrien ;; 10778344Sobrien 10878344Sobrien # "no", "false", "off", or "0" 10978344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 11078344Sobrien return 1 11178344Sobrien ;; 11278344Sobrien *) 113229822Sdougb warn "\$${1} is not set properly - see rc.conf(5)." 11478344Sobrien return 1 11578344Sobrien ;; 11678344Sobrien esac 11778344Sobrien} 11878344Sobrien 119157473Sflz# 12098186Sgordon# reverse_list list 12198186Sgordon# print the list in reverse order 12278344Sobrien# 12398186Sgordonreverse_list() 12498186Sgordon{ 12598186Sgordon _revlist= 126126286Smtm for _revfile; do 12798186Sgordon _revlist="$_revfile $_revlist" 12898186Sgordon done 12998186Sgordon echo $_revlist 13098186Sgordon} 13198186Sgordon 132169668Smtm# stop_boot always 133169668Smtm# If booting directly to multiuser or $always is enabled, 134169668Smtm# send SIGTERM to the parent (/etc/rc) to abort the boot. 135169668Smtm# Otherwise just exit. 13678344Sobrien# 137169668Smtmstop_boot() 138169668Smtm{ 139169668Smtm local always 140169668Smtm 141178776Smaxim case $1 in 142178776Smaxim # "yes", "true", "on", or "1" 143178770Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 144169668Smtm always=true 145178770Smtm ;; 146178770Smtm *) 147169668Smtm always=false 148178770Smtm ;; 149178775Smaxim esac 150169668Smtm if [ "$autoboot" = yes -o "$always" = true ]; then 151169668Smtm echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 152169668Smtm kill -TERM ${RC_PID} 153169668Smtm fi 154169668Smtm exit 1 155169668Smtm} 156169668Smtm 157169668Smtm# 15898186Sgordon# mount_critical_filesystems type 15998186Sgordon# Go through the list of critical filesystems as provided in 16098186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 16198186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 16298186Sgordon# 16378344Sobrienmount_critical_filesystems() 16478344Sobrien{ 16598186Sgordon eval _fslist=\$critical_filesystems_${1} 16678344Sobrien for _fs in $_fslist; do 16778344Sobrien mount | ( 168126285Smtm _ismounted=false 16978344Sobrien while read what _on on _type type; do 17078344Sobrien if [ $on = $_fs ]; then 171126285Smtm _ismounted=true 17278344Sobrien fi 17378344Sobrien done 174126285Smtm if $_ismounted; then 175126285Smtm : 176126285Smtm else 17778344Sobrien mount $_fs >/dev/null 2>&1 17878344Sobrien fi 17998186Sgordon ) 18078344Sobrien done 18178344Sobrien} 18278344Sobrien 18378344Sobrien# 18498186Sgordon# check_pidfile pidfile procname [interpreter] 18598186Sgordon# Parses the first line of pidfile for a PID, and ensures 18678344Sobrien# that the process is running and matches procname. 18798186Sgordon# Prints the matching PID upon success, nothing otherwise. 18898186Sgordon# interpreter is optional; see _find_processes() for details. 18978344Sobrien# 19078344Sobriencheck_pidfile() 19178344Sobrien{ 19278344Sobrien _pidfile=$1 19378344Sobrien _procname=$2 19498186Sgordon _interpreter=$3 19578344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 19698186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 19778344Sobrien fi 19878344Sobrien if [ ! -f $_pidfile ]; then 199131061Smtm debug "pid file ($_pidfile): not readable." 20078344Sobrien return 20178344Sobrien fi 20278344Sobrien read _pid _junk < $_pidfile 20378344Sobrien if [ -z "$_pid" ]; then 204139949Skeramida debug "pid file ($_pidfile): no pid in file." 20578344Sobrien return 20678344Sobrien fi 20798186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 20878344Sobrien} 20978344Sobrien 21078344Sobrien# 21198186Sgordon# check_process procname [interpreter] 21278344Sobrien# Ensures that a process (or processes) named procname is running. 21398186Sgordon# Prints a list of matching PIDs. 21498186Sgordon# interpreter is optional; see _find_processes() for details. 21578344Sobrien# 21678344Sobriencheck_process() 21778344Sobrien{ 21878344Sobrien _procname=$1 21998186Sgordon _interpreter=$2 22078344Sobrien if [ -z "$_procname" ]; then 22198186Sgordon err 3 'USAGE: check_process procname [interpreter]' 22278344Sobrien fi 22398186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 22498186Sgordon} 22598186Sgordon 22698186Sgordon# 22798186Sgordon# _find_processes procname interpreter psargs 22898186Sgordon# Search for procname in the output of ps generated by psargs. 22998186Sgordon# Prints the PIDs of any matching processes, space separated. 23098186Sgordon# 23198186Sgordon# If interpreter == ".", check the following variations of procname 23298186Sgordon# against the first word of each command: 23398186Sgordon# procname 23498186Sgordon# `basename procname` 23598186Sgordon# `basename procname` + ":" 23698186Sgordon# "(" + `basename procname` + ")" 237155719Sceri# "[" + `basename procname` + "]" 23898186Sgordon# 23998186Sgordon# If interpreter != ".", read the first line of procname, remove the 24098186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 24198186Sgordon# match that against each command, either as is, or with extra words 242157841Sflz# at the end. As an alternative, to deal with interpreted daemons 243157841Sflz# using perl, the basename of the interpreter plus a colon is also 244157841Sflz# tried as the prefix to procname. 24598186Sgordon# 24698186Sgordon_find_processes() 24798186Sgordon{ 24898186Sgordon if [ $# -ne 3 ]; then 24998186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 25098186Sgordon fi 25198186Sgordon _procname=$1 25298186Sgordon _interpreter=$2 25398186Sgordon _psargs=$3 25498186Sgordon 25578344Sobrien _pref= 25698186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 257170282Syar _script=${_chroot}${_chroot:+"/"}$_procname 258170282Syar if [ -r $_script ]; then 259170282Syar read _interp < $_script # read interpreter name 260170282Syar case "$_interp" in 261170282Syar \#!*) 262170282Syar _interp=${_interp#\#!} # strip #! 263170282Syar set -- $_interp 264170282Syar case $1 in 265170282Syar */bin/env) 266170282Syar shift # drop env to get real name 267170282Syar ;; 268170282Syar esac 269170282Syar if [ $_interpreter != $1 ]; then 270170282Syar warn "\$command_interpreter $_interpreter != $1" 271170282Syar fi 272170282Syar ;; 273170282Syar *) 274170282Syar warn "no shebang line in $_script" 275170282Syar set -- $_interpreter 276170282Syar ;; 277170282Syar esac 278170282Syar else 279170282Syar warn "cannot read shebang line from $_script" 280170282Syar set -- $_interpreter 28178344Sobrien fi 28298186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 283157841Sflz _interpbn=${1##*/} 28498186Sgordon _fp_args='_argv' 28598186Sgordon _fp_match='case "$_argv" in 286157841Sflz ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)' 28798186Sgordon else # a normal daemon 28898186Sgordon _procnamebn=${_procname##*/} 28998186Sgordon _fp_args='_arg0 _argv' 29098186Sgordon _fp_match='case "$_arg0" in 291151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 29298186Sgordon fi 29398186Sgordon 294161435Syar _proccheck="\ 295161436Syar $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 296157657Sflz while read _npid _jid '"$_fp_args"'; do 297161436Syar '"$_fp_match"' 298157657Sflz if [ "$JID" -eq "$_jid" ]; 299157657Sflz then echo -n "$_pref$_npid"; 300157657Sflz _pref=" "; 301157657Sflz fi 30298186Sgordon ;; 30398186Sgordon esac 30498186Sgordon done' 30598186Sgordon 306114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 30798186Sgordon eval $_proccheck 30898186Sgordon} 30998186Sgordon 31098186Sgordon# 31198186Sgordon# wait_for_pids pid [pid ...] 31298186Sgordon# spins until none of the pids exist 31398186Sgordon# 31498186Sgordonwait_for_pids() 31598186Sgordon{ 316206248Sdougb local _list _prefix _nlist _j 317206248Sdougb 318126286Smtm _list="$@" 31998186Sgordon if [ -z "$_list" ]; then 32098186Sgordon return 32198186Sgordon fi 32298186Sgordon _prefix= 32398186Sgordon while true; do 32498186Sgordon _nlist=""; 32598186Sgordon for _j in $_list; do 32698186Sgordon if kill -0 $_j 2>/dev/null; then 32798186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 328206248Sdougb [ -n "$_prefix" ] && sleep 1 32998186Sgordon fi 33098186Sgordon done 33198186Sgordon if [ -z "$_nlist" ]; then 33298186Sgordon break 33378344Sobrien fi 33498186Sgordon _list=$_nlist 33598186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 33698186Sgordon _prefix=", " 337206248Sdougb pwait $_list 2>/dev/null 33878344Sobrien done 33998186Sgordon if [ -n "$_prefix" ]; then 34098186Sgordon echo "." 34198186Sgordon fi 34278344Sobrien} 34378344Sobrien 34478344Sobrien# 345220962Sdougb# get_pidfile_from_conf string file 346220962Sdougb# 347220962Sdougb# Takes a string to search for in the specified file. 348220962Sdougb# Ignores lines with traditional comment characters. 349220962Sdougb# 350220962Sdougb# Example: 351220962Sdougb# 352220962Sdougb# if get_pidfile_from_conf string file; then 353220962Sdougb# pidfile="$_pidfile_from_conf" 354220962Sdougb# else 355220962Sdougb# pidfile='appropriate default' 356220962Sdougb# fi 357220962Sdougb# 358220962Sdougbget_pidfile_from_conf() 359220962Sdougb{ 360220963Sdougb if [ -z "$1" -o -z "$2" ]; then 361220963Sdougb err 3 "USAGE: get_pidfile_from_conf string file ($name)" 362220963Sdougb fi 363220963Sdougb 364220962Sdougb local string file line 365220962Sdougb 366220962Sdougb string="$1" ; file="$2" 367220962Sdougb 368220963Sdougb if [ ! -s "$file" ]; then 369220963Sdougb err 3 "get_pidfile_from_conf: $file does not exist ($name)" 370220962Sdougb fi 371220962Sdougb 372220962Sdougb while read line; do 373220962Sdougb case "$line" in 374220962Sdougb *[#\;]*${string}*) continue ;; 375220962Sdougb *${string}*) break ;; 376220962Sdougb esac 377220962Sdougb done < $file 378220962Sdougb 379220962Sdougb if [ -n "$line" ]; then 380220962Sdougb line=${line#*/} 381220962Sdougb _pidfile_from_conf="/${line%%[\"\;]*}" 382220962Sdougb else 383220962Sdougb return 1 384220962Sdougb fi 385220962Sdougb} 386220962Sdougb 387220962Sdougb# 388197947Sdougb# check_startmsgs 389197947Sdougb# If rc_quiet is set (usually as a result of using faststart at 390197947Sdougb# boot time) check if rc_startmsgs is enabled. 391197947Sdougb# 392197947Sdougbcheck_startmsgs() 393197947Sdougb{ 394197947Sdougb if [ -n "$rc_quiet" ]; then 395197947Sdougb checkyesno rc_startmsgs 396197947Sdougb else 397197947Sdougb return 0 398197947Sdougb fi 399197947Sdougb} 400197947Sdougb 401197947Sdougb# 40298186Sgordon# run_rc_command argument 40398186Sgordon# Search for argument in the list of supported commands, which is: 40498186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 40598186Sgordon# If there's a match, run ${argument}_cmd or the default method 40698186Sgordon# (see below). 40778344Sobrien# 40898186Sgordon# If argument has a given prefix, then change the operation as follows: 40998186Sgordon# Prefix Operation 41078344Sobrien# ------ --------- 411175676Smtm# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 41298186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 413126303Smtm# one Set ${rcvar} to YES 414175676Smtm# quiet Don't output some diagnostics, and set rc_quiet=yes 41578344Sobrien# 41678344Sobrien# The following globals are used: 41778344Sobrien# 41898186Sgordon# Name Needed Purpose 41998186Sgordon# ---- ------ ------- 42078344Sobrien# name y Name of script. 42178344Sobrien# 42278344Sobrien# command n Full path to command. 42398186Sgordon# Not needed if ${rc_arg}_cmd is set for 42478344Sobrien# each keyword. 42578344Sobrien# 42678344Sobrien# command_args n Optional args/shell directives for command. 42778344Sobrien# 42898186Sgordon# command_interpreter n If not empty, command is interpreted, so 42998186Sgordon# call check_{pidfile,process}() appropriately. 43098186Sgordon# 431197144Shrs# desc n Description of script. 432197144Shrs# 43378344Sobrien# extra_commands n List of extra commands supported. 43478344Sobrien# 43598186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 43698186Sgordon# otherwise use check_process $command. 43798186Sgordon# In either case, only check if $command is set. 43878344Sobrien# 43998186Sgordon# procname n Process name to check for instead of $command. 44098186Sgordon# 44178344Sobrien# rcvar n This is checked with checkyesno to determine 44278344Sobrien# if the action should be run. 44378344Sobrien# 444157653Sflz# ${name}_program n Full path to command. 445157653Sflz# Meant to be used in /etc/rc.conf to override 446157653Sflz# ${command}. 447157653Sflz# 44878344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 44998186Sgordon# Requires /usr to be mounted. 45078344Sobrien# 45178344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 45278344Sobrien# (if not using ${name}_chroot). 45378344Sobrien# 45478344Sobrien# ${name}_flags n Arguments to call ${command} with. 45578344Sobrien# NOTE: $flags from the parent environment 45678344Sobrien# can be used to override this. 45778344Sobrien# 45878344Sobrien# ${name}_nice n Nice level to run ${command} at. 45978344Sobrien# 46078344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 46178344Sobrien# using ${name}_chroot. 46298186Sgordon# Requires /usr to be mounted. 46378344Sobrien# 46478344Sobrien# ${name}_group n Group to run chrooted ${command} as. 46598186Sgordon# Requires /usr to be mounted. 46678344Sobrien# 46798186Sgordon# ${name}_groups n Comma separated list of supplementary groups 46898186Sgordon# to run the chrooted ${command} with. 46998186Sgordon# Requires /usr to be mounted. 47078344Sobrien# 47198186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 47278344Sobrien# Otherwise, use default command (see below) 47378344Sobrien# 47498186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 47598186Sgordon# ${rc_arg}_cmd method in the default 47698186Sgordon# operation (i.e, after checking for required 47798186Sgordon# bits and process (non)existence). 47878344Sobrien# If this completes with a non-zero exit code, 47998186Sgordon# don't run ${rc_arg}_cmd. 48078344Sobrien# 48198186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 48298186Sgordon# ${rc_arg}_cmd method, if that method 48398186Sgordon# returned a zero exit code. 48498186Sgordon# 48578344Sobrien# required_dirs n If set, check for the existence of the given 486165565Syar# directories before running a (re)start command. 48778344Sobrien# 48878344Sobrien# required_files n If set, check for the readability of the given 489165565Syar# files before running a (re)start command. 49078344Sobrien# 491165565Syar# required_modules n If set, ensure the given kernel modules are 492165565Syar# loaded before running a (re)start command. 493165565Syar# The check and possible loads are actually 494165565Syar# done after start_precmd so that the modules 495165565Syar# aren't loaded in vain, should the precmd 496165565Syar# return a non-zero status to indicate a error. 497165565Syar# If a word in the list looks like "foo:bar", 498165565Syar# "foo" is the KLD file name and "bar" is the 499165565Syar# module name. If a word looks like "foo~bar", 500165565Syar# "foo" is the KLD file name and "bar" is a 501165565Syar# egrep(1) pattern matching the module name. 502165565Syar# Otherwise the module name is assumed to be 503165565Syar# the same as the KLD file name, which is most 504165565Syar# common. See load_kld(). 505165565Syar# 50678344Sobrien# required_vars n If set, perform checkyesno on each of the 50778344Sobrien# listed variables before running the default 50878344Sobrien# (re)start command. 50978344Sobrien# 51098186Sgordon# Default behaviour for a given argument, if no override method is 51198186Sgordon# provided: 51278344Sobrien# 51398186Sgordon# Argument Default behaviour 51498186Sgordon# -------- ----------------- 51578344Sobrien# start if !running && checkyesno ${rcvar} 51678344Sobrien# ${command} 51778344Sobrien# 51878344Sobrien# stop if ${pidfile} 51998186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 52078344Sobrien# else 52198186Sgordon# rc_pid=$(check_process $command) 52298186Sgordon# kill $sig_stop $rc_pid 52398186Sgordon# wait_for_pids $rc_pid 52498186Sgordon# ($sig_stop defaults to TERM.) 52578344Sobrien# 52698186Sgordon# reload Similar to stop, except use $sig_reload instead, 52798186Sgordon# and doesn't wait_for_pids. 52878344Sobrien# $sig_reload defaults to HUP. 529151685Syar# Note that `reload' isn't provided by default, 530151685Syar# it should be enabled via $extra_commands. 53178344Sobrien# 53278344Sobrien# restart Run `stop' then `start'. 53378344Sobrien# 53498186Sgordon# status Show if ${command} is running, etc. 53578344Sobrien# 53698186Sgordon# poll Wait for ${command} to exit. 53798186Sgordon# 53898186Sgordon# rcvar Display what rc.conf variable is used (if any). 53998186Sgordon# 54098186Sgordon# Variables available to methods, and after run_rc_command() has 54198186Sgordon# completed: 54298186Sgordon# 54398186Sgordon# Variable Purpose 54498186Sgordon# -------- ------- 545126303Smtm# rc_arg Argument to command, after fast/force/one processing 54698186Sgordon# performed 54798186Sgordon# 54898186Sgordon# rc_flags Flags to start the default command with. 54998186Sgordon# Defaults to ${name}_flags, unless overridden 55098186Sgordon# by $flags from the environment. 55198186Sgordon# This variable may be changed by the precmd method. 55298186Sgordon# 55398186Sgordon# rc_pid PID of command (if appropriate) 55498186Sgordon# 55598186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 55698186Sgordon# 55798186Sgordon# rc_force Not empty if "force" was provided (q.v.) 55898186Sgordon# 559175676Smtm# rc_quiet Not empty if "quiet" was provided 56098186Sgordon# 561175676Smtm# 56278344Sobrienrun_rc_command() 56378344Sobrien{ 564116097Smtm _return=0 56598186Sgordon rc_arg=$1 56678344Sobrien if [ -z "$name" ]; then 56798186Sgordon err 3 'run_rc_command: $name is not set.' 56878344Sobrien fi 56978344Sobrien 570132892Smtm # Don't repeat the first argument when passing additional command- 571132892Smtm # line arguments to the command subroutines. 572132892Smtm # 573132892Smtm shift 1 574132892Smtm rc_extra_args="$*" 575132892Smtm 576126303Smtm _rc_prefix= 57798186Sgordon case "$rc_arg" in 57878344Sobrien fast*) # "fast" prefix; don't check pid 57998186Sgordon rc_arg=${rc_arg#fast} 58098186Sgordon rc_fast=yes 581175676Smtm rc_quiet=yes 58278344Sobrien ;; 583198216Sed force*) # "force" prefix; always run 58498186Sgordon rc_force=yes 585126303Smtm _rc_prefix=force 586126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 58778344Sobrien if [ -n "${rcvar}" ]; then 58878344Sobrien eval ${rcvar}=YES 58978344Sobrien fi 59078344Sobrien ;; 591126303Smtm one*) # "one" prefix; set ${rcvar}=yes 592126303Smtm _rc_prefix=one 593126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 594126303Smtm if [ -n "${rcvar}" ]; then 595126303Smtm eval ${rcvar}=YES 596126303Smtm fi 597126303Smtm ;; 598175676Smtm quiet*) # "quiet" prefix; omit some messages 599175676Smtm _rc_prefix=quiet 600175676Smtm rc_arg=${rc_arg#${_rc_prefix}} 601175676Smtm rc_quiet=yes 602175676Smtm ;; 60378344Sobrien esac 60478344Sobrien 605161530Sflz eval _override_command=\$${name}_program 606198162Sdougb command=${_override_command:-$command} 607161530Sflz 60878344Sobrien _keywords="start stop restart rcvar $extra_commands" 60998186Sgordon rc_pid= 61078344Sobrien _pidcmd= 61198186Sgordon _procname=${procname:-${command}} 61298186Sgordon 613131135Smtm # setup pid check command 614131135Smtm if [ -n "$_procname" ]; then 61578344Sobrien if [ -n "$pidfile" ]; then 61698186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 61798186Sgordon else 61898186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 61978344Sobrien fi 62078344Sobrien if [ -n "$_pidcmd" ]; then 62198186Sgordon _keywords="${_keywords} status poll" 62278344Sobrien fi 62378344Sobrien fi 62478344Sobrien 62598186Sgordon if [ -z "$rc_arg" ]; then 626150796Syar rc_usage $_keywords 62778344Sobrien fi 62878344Sobrien 62978344Sobrien if [ -n "$flags" ]; then # allow override from environment 63098186Sgordon rc_flags=$flags 63178344Sobrien else 63298186Sgordon eval rc_flags=\$${name}_flags 63378344Sobrien fi 63498186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 63598186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 63698186Sgordon _group=\$${name}_group _groups=\$${name}_groups 63778344Sobrien 63898186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 639124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 64098186Sgordon unset _user 64198186Sgordon fi 64298186Sgordon fi 64398186Sgordon 644179870Smtm eval $_pidcmd # determine the pid if necessary 645179870Smtm 646179870Smtm for _elem in $_keywords; do 647179870Smtm if [ "$_elem" != "$rc_arg" ]; then 648179870Smtm continue 649179870Smtm fi 650206686Sdougb # if ${rcvar} is set, $1 is not "rcvar" 651206686Sdougb # and ${rc_pid} is not set, then run 65278344Sobrien # checkyesno ${rcvar} 65378344Sobrien # and return if that failed 65478344Sobrien # 655220760Sdougb if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 656220760Sdougb [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 657179870Smtm if ! checkyesno ${rcvar}; then 658179870Smtm if [ -n "${rc_quiet}" ]; then 659179870Smtm return 0 660179870Smtm fi 661179870Smtm echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 662179870Smtm echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 663179870Smtm echo "instead of '${rc_arg}'." 664175676Smtm return 0 665175676Smtm fi 66678344Sobrien fi 66778344Sobrien 66878344Sobrien # if there's a custom ${XXX_cmd}, 66978344Sobrien # run that instead of the default 67078344Sobrien # 671165565Syar eval _cmd=\$${rc_arg}_cmd \ 672165565Syar _precmd=\$${rc_arg}_precmd \ 673165565Syar _postcmd=\$${rc_arg}_postcmd 674165565Syar 67578344Sobrien if [ -n "$_cmd" ]; then 676165565Syar _run_rc_precmd || return 1 677165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 678165565Syar _run_rc_postcmd 679116097Smtm return $_return 68078344Sobrien fi 68178344Sobrien 68298186Sgordon case "$rc_arg" in # default operations... 68378344Sobrien 68478344Sobrien status) 685165565Syar _run_rc_precmd || return 1 68698186Sgordon if [ -n "$rc_pid" ]; then 68798186Sgordon echo "${name} is running as pid $rc_pid." 68878344Sobrien else 68978344Sobrien echo "${name} is not running." 69078344Sobrien return 1 69178344Sobrien fi 692165565Syar _run_rc_postcmd 69378344Sobrien ;; 69478344Sobrien 69578344Sobrien start) 696131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 697157473Sflz echo 1>&2 "${name} already running? (pid=$rc_pid)." 698153152Syar return 1 69978344Sobrien fi 70078344Sobrien 701167413Syar if [ ! -x ${_chroot}${_chroot:+"/"}${command} ]; then 702160667Syar warn "run_rc_command: cannot run $command" 703153152Syar return 1 70478344Sobrien fi 70578344Sobrien 706179946Smtm if ! _run_rc_precmd; then 707179946Smtm warn "failed precmd routine for ${name}" 708179946Smtm return 1 709179946Smtm fi 71078344Sobrien 711160668Syar # setup the full command to run 71278344Sobrien # 713197947Sdougb check_startmsgs && echo "Starting ${name}." 71478344Sobrien if [ -n "$_chroot" ]; then 71578344Sobrien _doit="\ 71678344Sobrien${_nice:+nice -n $_nice }\ 71778344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 71898186Sgordon$_chroot $command $rc_flags $command_args" 71978344Sobrien else 72078344Sobrien _doit="\ 721161396Syar${_chdir:+cd $_chdir && }\ 72298186Sgordon$command $rc_flags $command_args" 72398186Sgordon if [ -n "$_user" ]; then 72498186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 72598186Sgordon fi 726161396Syar if [ -n "$_nice" ]; then 727161396Syar if [ -z "$_user" ]; then 728161396Syar _doit="sh -c \"$_doit\"" 729201036Sdougb fi 730161396Syar _doit="nice -n $_nice $_doit" 731161396Syar fi 73278344Sobrien fi 73398186Sgordon 734165565Syar # run the full command 73598186Sgordon # 736179946Smtm if ! _run_rc_doit "$_doit"; then 737179946Smtm warn "failed to start ${name}" 738179946Smtm return 1 739179946Smtm fi 74098186Sgordon 74198186Sgordon # finally, run postcmd 74298186Sgordon # 743165565Syar _run_rc_postcmd 74478344Sobrien ;; 74578344Sobrien 74678344Sobrien stop) 74798186Sgordon if [ -z "$rc_pid" ]; then 748153152Syar [ -n "$rc_fast" ] && return 0 749165565Syar _run_rc_notrunning 750153152Syar return 1 75178344Sobrien fi 75278344Sobrien 753165565Syar _run_rc_precmd || return 1 75498186Sgordon 75598186Sgordon # send the signal to stop 75698186Sgordon # 75778344Sobrien echo "Stopping ${name}." 758165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 759165565Syar _run_rc_doit "$_doit" || return 1 76098186Sgordon 76198186Sgordon # wait for the command to exit, 76298186Sgordon # and run postcmd. 76398186Sgordon wait_for_pids $rc_pid 764165565Syar 765165565Syar _run_rc_postcmd 76678344Sobrien ;; 76778344Sobrien 76878344Sobrien reload) 76998186Sgordon if [ -z "$rc_pid" ]; then 770165565Syar _run_rc_notrunning 771153152Syar return 1 77278344Sobrien fi 773165565Syar 774165565Syar _run_rc_precmd || return 1 775165565Syar 776165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 777165565Syar _run_rc_doit "$_doit" || return 1 778165565Syar 779165565Syar _run_rc_postcmd 78078344Sobrien ;; 78178344Sobrien 78278344Sobrien restart) 78378344Sobrien # prevent restart being called more 78478344Sobrien # than once by any given script 78578344Sobrien # 786126285Smtm if ${_rc_restart_done:-false}; then 78778344Sobrien return 0 78878344Sobrien fi 789126285Smtm _rc_restart_done=true 79078344Sobrien 791165565Syar _run_rc_precmd || return 1 792165565Syar 793165565Syar # run those in a subshell to keep global variables 794152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 795165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 796165565Syar _return=$? 797165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 79898186Sgordon 799165565Syar _run_rc_postcmd 80078344Sobrien ;; 80178344Sobrien 80298186Sgordon poll) 803165565Syar _run_rc_precmd || return 1 80498186Sgordon if [ -n "$rc_pid" ]; then 80598186Sgordon wait_for_pids $rc_pid 80698186Sgordon fi 807165565Syar _run_rc_postcmd 80898186Sgordon ;; 80998186Sgordon 81078344Sobrien rcvar) 811197144Shrs echo -n "# $name" 812197144Shrs if [ -n "$desc" ]; then 813197144Shrs echo " : $desc" 814197144Shrs else 815197144Shrs echo "" 816197144Shrs fi 817197144Shrs echo "#" 818230103Sdougb # Get unique vars in $rcvar 819230103Sdougb for _v in $rcvar; do 820197144Shrs case $v in 821197144Shrs $_v\ *|\ *$_v|*\ $_v\ *) ;; 822197144Shrs *) v="${v# } $_v" ;; 823197144Shrs esac 824197144Shrs done 825197144Shrs 826197144Shrs # Display variables. 827197144Shrs for _v in $v; do 828197144Shrs if [ -z "$_v" ]; then 829197144Shrs continue 83078344Sobrien fi 831197144Shrs 832197144Shrs eval _desc=\$${_v}_desc 833197144Shrs eval _defval=\$${_v}_defval 834197144Shrs _h="-" 835197144Shrs 836197144Shrs eval echo \"$_v=\\\"\$$_v\\\"\" 837197144Shrs # decode multiple lines of _desc 838197144Shrs while [ -n "$_desc" ]; do 839197144Shrs case $_desc in 840197144Shrs *^^*) 841197144Shrs echo "# $_h ${_desc%%^^*}" 842197144Shrs _desc=${_desc#*^^} 843197144Shrs _h=" " 844197144Shrs ;; 845197144Shrs *) 846197144Shrs echo "# $_h ${_desc}" 847197144Shrs break 848197144Shrs ;; 849197144Shrs esac 850197144Shrs done 851197144Shrs echo "# (default: \"$_defval\")" 852197144Shrs done 853197144Shrs echo "" 85478344Sobrien ;; 85578344Sobrien 85678344Sobrien *) 857150796Syar rc_usage $_keywords 85878344Sobrien ;; 85978344Sobrien 86078344Sobrien esac 861116097Smtm return $_return 86278344Sobrien done 86378344Sobrien 86498186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 865150796Syar rc_usage $_keywords 866153152Syar # not reached 86778344Sobrien} 86878344Sobrien 86978344Sobrien# 870165565Syar# Helper functions for run_rc_command: common code. 871165565Syar# They use such global variables besides the exported rc_* ones: 872165565Syar# 873165565Syar# name R/W 874165565Syar# ------------------ 875165565Syar# _precmd R 876165565Syar# _postcmd R 877165565Syar# _return W 878165565Syar# 879165565Syar_run_rc_precmd() 880165565Syar{ 881165565Syar check_required_before "$rc_arg" || return 1 882165565Syar 883165565Syar if [ -n "$_precmd" ]; then 884165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 885165565Syar eval "$_precmd $rc_extra_args" 886165565Syar _return=$? 887165565Syar 888165565Syar # If precmd failed and force isn't set, request exit. 889165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 890165565Syar return 1 891165565Syar fi 892165565Syar fi 893165565Syar 894165565Syar check_required_after "$rc_arg" || return 1 895165565Syar 896165565Syar return 0 897165565Syar} 898165565Syar 899165565Syar_run_rc_postcmd() 900165565Syar{ 901165565Syar if [ -n "$_postcmd" ]; then 902165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 903165565Syar eval "$_postcmd $rc_extra_args" 904165565Syar _return=$? 905165565Syar fi 906165565Syar return 0 907165565Syar} 908165565Syar 909165565Syar_run_rc_doit() 910165565Syar{ 911165565Syar debug "run_rc_command: doit: $*" 912165565Syar eval "$@" 913165565Syar _return=$? 914165565Syar 915165565Syar # If command failed and force isn't set, request exit. 916165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 917165565Syar return 1 918165565Syar fi 919165565Syar 920165565Syar return 0 921165565Syar} 922165565Syar 923165565Syar_run_rc_notrunning() 924165565Syar{ 925165565Syar local _pidmsg 926165565Syar 927165565Syar if [ -n "$pidfile" ]; then 928165565Syar _pidmsg=" (check $pidfile)." 929165565Syar else 930165565Syar _pidmsg= 931165565Syar fi 932165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 933165565Syar} 934165565Syar 935165565Syar_run_rc_killcmd() 936165565Syar{ 937165565Syar local _cmd 938165565Syar 939165565Syar _cmd="kill -$1 $rc_pid" 940165565Syar if [ -n "$_user" ]; then 941165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 942165565Syar fi 943165565Syar echo "$_cmd" 944165565Syar} 945165565Syar 946165565Syar# 94778344Sobrien# run_rc_script file arg 94878344Sobrien# Start the script `file' with `arg', and correctly handle the 949201038Sdougb# return value from the script. 950201038Sdougb# If `file' ends with `.sh', it's sourced into the current environment 951201038Sdougb# when $rc_fast_and_loose is set, otherwise it is run as a child process. 952201038Sdougb# If `file' appears to be a backup or scratch file, ignore it. 953201038Sdougb# Otherwise if it is executable run as a child process. 95478344Sobrien# 95578344Sobrienrun_rc_script() 95678344Sobrien{ 95778344Sobrien _file=$1 95878344Sobrien _arg=$2 95978344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 96078344Sobrien err 3 'USAGE: run_rc_script file arg' 96178344Sobrien fi 96278344Sobrien 96398186Sgordon unset name command command_args command_interpreter \ 96498186Sgordon extra_commands pidfile procname \ 965230103Sdougb rcvar rcvars_obsolete required_dirs required_files \ 966197144Shrs required_vars 96798186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 96898186Sgordon 96978344Sobrien case "$_file" in 970193118Sdougb /etc/rc.d/*.sh) # no longer allowed in the base 971193118Sdougb warn "Ignoring old-style startup script $_file" 97278344Sobrien ;; 973153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 97498186Sgordon warn "Ignoring scratch file $_file" 97598186Sgordon ;; 97678344Sobrien *) # run in subshell 97798186Sgordon if [ -x $_file ]; then 97898186Sgordon if [ -n "$rc_fast_and_loose" ]; then 979146490Sschweikh set $_arg; . $_file 98098186Sgordon else 981130161Smtm ( trap "echo Script $_file interrupted; kill -QUIT $$" 3 982130161Smtm trap "echo Script $_file interrupted; exit 1" 2 983184317Sthompsa trap "echo Script $_file running" 29 984146490Sschweikh set $_arg; . $_file ) 98598186Sgordon fi 98698186Sgordon fi 98778344Sobrien ;; 98878344Sobrien esac 98978344Sobrien} 99078344Sobrien 99178344Sobrien# 992157653Sflz# load_rc_config name 993157653Sflz# Source in the configuration file for a given name. 99478344Sobrien# 99578344Sobrienload_rc_config() 99678344Sobrien{ 997230103Sdougb local _name _rcvar_val _var _defval _v _msg _new 998157653Sflz _name=$1 999157653Sflz if [ -z "$_name" ]; then 1000157653Sflz err 3 'USAGE: load_rc_config name' 100178344Sobrien fi 100278344Sobrien 1003219612Sdougb if ${_rc_conf_loaded:-false}; then 1004219612Sdougb : 1005219612Sdougb else 100698186Sgordon if [ -r /etc/defaults/rc.conf ]; then 100798186Sgordon debug "Sourcing /etc/defaults/rc.conf" 100898186Sgordon . /etc/defaults/rc.conf 100998186Sgordon source_rc_confs 101098186Sgordon elif [ -r /etc/rc.conf ]; then 101198186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 101298186Sgordon . /etc/rc.conf 101398186Sgordon fi 1014126285Smtm _rc_conf_loaded=true 101598186Sgordon fi 1016161530Sflz if [ -f /etc/rc.conf.d/"$_name" ]; then 1017157653Sflz debug "Sourcing /etc/rc.conf.d/${_name}" 1018161530Sflz . /etc/rc.conf.d/"$_name" 1019157653Sflz fi 1020179872Smtm 1021197144Shrs # Set defaults if defined. 1022230103Sdougb for _var in $rcvar; do 1023223227Sjilles eval _defval=\$${_var}_defval 1024197144Shrs if [ -n "$_defval" ]; then 1025197144Shrs eval : \${$_var:=\$${_var}_defval} 1026197144Shrs fi 1027197144Shrs done 1028197144Shrs 1029197144Shrs # check obsolete rc.conf variables 1030197144Shrs for _var in $rcvars_obsolete; do 1031223227Sjilles eval _v=\$$_var 1032223227Sjilles eval _msg=\$${_var}_obsolete_msg 1033223227Sjilles eval _new=\$${_var}_newvar 1034197144Shrs case $_v in 1035197144Shrs "") 1036197144Shrs ;; 1037197144Shrs *) 1038197144Shrs if [ -z "$_new" ]; then 1039197144Shrs _msg="Ignored." 1040197144Shrs else 1041197144Shrs eval $_new=\"\$$_var\" 1042197144Shrs if [ -z "$_msg" ]; then 1043197144Shrs _msg="Use \$$_new instead." 1044197144Shrs fi 1045197144Shrs fi 1046197144Shrs warn "\$$_var is obsolete. $_msg" 1047197144Shrs ;; 1048197144Shrs esac 1049197144Shrs done 105078344Sobrien} 1051201036Sdougb 1052157473Sflz# 1053157653Sflz# load_rc_config_var name var 1054157653Sflz# Read the rc.conf(5) var for name and set in the 1055157473Sflz# current shell, using load_rc_config in a subshell to prevent 1056157473Sflz# unwanted side effects from other variable assignments. 1057157473Sflz# 1058157473Sflzload_rc_config_var() 1059157473Sflz{ 1060157473Sflz if [ $# -ne 2 ]; then 1061157653Sflz err 3 'USAGE: load_rc_config_var name var' 1062157473Sflz fi 1063157473Sflz eval $(eval '( 1064157473Sflz load_rc_config '$1' >/dev/null; 1065157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1066157473Sflz echo '$2'=\'\''${'$2'}\'\''; 1067157473Sflz fi 1068157473Sflz )' ) 1069157473Sflz} 107078344Sobrien 107178344Sobrien# 107278344Sobrien# rc_usage commands 107378344Sobrien# Print a usage string for $0, with `commands' being a list of 107478344Sobrien# valid commands. 107578344Sobrien# 107678344Sobrienrc_usage() 107778344Sobrien{ 1078230007Srea echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 107978344Sobrien 108078344Sobrien _sep= 1081126286Smtm for _elem; do 108278344Sobrien echo -n 1>&2 "$_sep$_elem" 108378344Sobrien _sep="|" 108478344Sobrien done 108578344Sobrien echo 1>&2 ")" 108678344Sobrien exit 1 108778344Sobrien} 108878344Sobrien 108978344Sobrien# 109078344Sobrien# err exitval message 109178344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 109278344Sobrien# 109378344Sobrienerr() 109478344Sobrien{ 109578344Sobrien exitval=$1 109678344Sobrien shift 109778344Sobrien 1098106643Sgordon if [ -x /usr/bin/logger ]; then 1099106643Sgordon logger "$0: ERROR: $*" 1100106643Sgordon fi 1101106643Sgordon echo 1>&2 "$0: ERROR: $*" 110278344Sobrien exit $exitval 110378344Sobrien} 110478344Sobrien 110578344Sobrien# 110678344Sobrien# warn message 110778344Sobrien# Display message to stderr and log to the syslog. 110878344Sobrien# 110978344Sobrienwarn() 111078344Sobrien{ 1111106643Sgordon if [ -x /usr/bin/logger ]; then 1112106643Sgordon logger "$0: WARNING: $*" 1113106643Sgordon fi 1114106643Sgordon echo 1>&2 "$0: WARNING: $*" 111578344Sobrien} 111698186Sgordon 111798186Sgordon# 111898186Sgordon# info message 111998186Sgordon# Display informational message to stdout and log to syslog. 112098186Sgordon# 112198186Sgordoninfo() 112298186Sgordon{ 1123119170Smtm case ${rc_info} in 1124119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1125119170Smtm if [ -x /usr/bin/logger ]; then 1126119170Smtm logger "$0: INFO: $*" 1127119170Smtm fi 1128119170Smtm echo "$0: INFO: $*" 1129119170Smtm ;; 1130119170Smtm esac 113198186Sgordon} 113298186Sgordon 113398186Sgordon# 113498186Sgordon# debug message 1135106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 113698186Sgordon# BEWARE that you don't call any subroutine that itself calls this 113798186Sgordon# function. 113898186Sgordon# 113998186Sgordondebug() 114098186Sgordon{ 114198186Sgordon case ${rc_debug} in 114298186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1143106700Sgordon if [ -x /usr/bin/logger ]; then 1144162947Syar logger "$0: DEBUG: $*" 1145106700Sgordon fi 1146146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 114798186Sgordon ;; 114898186Sgordon esac 114998186Sgordon} 115098186Sgordon 115198186Sgordon# 115298186Sgordon# backup_file action file cur backup 115398186Sgordon# Make a backup copy of `file' into `cur', and save the previous 115498186Sgordon# version of `cur' as `backup' or use rcs for archiving. 115598186Sgordon# 115698186Sgordon# This routine checks the value of the backup_uses_rcs variable, 115798186Sgordon# which can be either YES or NO. 115898186Sgordon# 115998186Sgordon# The `action' keyword can be one of the following: 116098186Sgordon# 116198186Sgordon# add `file' is now being backed up (and is possibly 116298186Sgordon# being reentered into the backups system). `cur' 116398186Sgordon# is created and RCS files, if necessary, are 116498186Sgordon# created as well. 116598186Sgordon# 116698186Sgordon# update `file' has changed and needs to be backed up. 116798186Sgordon# If `cur' exists, it is copied to to `back' or 116898186Sgordon# checked into RCS (if the repository file is old), 116998186Sgordon# and then `file' is copied to `cur'. Another RCS 117098186Sgordon# check in done here if RCS is being used. 117198186Sgordon# 117298186Sgordon# remove `file' is no longer being tracked by the backups 117398186Sgordon# system. If RCS is not being used, `cur' is moved 117498186Sgordon# to `back', otherwise an empty file is checked in, 117598186Sgordon# and then `cur' is removed. 117698186Sgordon# 117798186Sgordon# 117898186Sgordonbackup_file() 117998186Sgordon{ 118098186Sgordon _action=$1 118198186Sgordon _file=$2 118298186Sgordon _cur=$3 118398186Sgordon _back=$4 118498186Sgordon 118598186Sgordon if checkyesno backup_uses_rcs; then 118698186Sgordon _msg0="backup archive" 118798186Sgordon _msg1="update" 118898186Sgordon 118998186Sgordon # ensure that history file is not locked 119098186Sgordon if [ -f $_cur,v ]; then 119198186Sgordon rcs -q -u -U -M $_cur 119298186Sgordon fi 119398186Sgordon 119498186Sgordon # ensure after switching to rcs that the 119598186Sgordon # current backup is not lost 119698186Sgordon if [ -f $_cur ]; then 119798186Sgordon # no archive, or current newer than archive 119898186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 119998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 120098186Sgordon rcs -q -kb -U $_cur 120198186Sgordon co -q -f -u $_cur 120298186Sgordon fi 120398186Sgordon fi 120498186Sgordon 120598186Sgordon case $_action in 120698186Sgordon add|update) 120798186Sgordon cp -p $_file $_cur 120898186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 120998186Sgordon rcs -q -kb -U $_cur 121098186Sgordon co -q -f -u $_cur 121198186Sgordon chown root:wheel $_cur $_cur,v 121298186Sgordon ;; 121398186Sgordon remove) 121498186Sgordon cp /dev/null $_cur 121598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 121698186Sgordon rcs -q -kb -U $_cur 121798186Sgordon chown root:wheel $_cur $_cur,v 121898186Sgordon rm $_cur 121998186Sgordon ;; 122098186Sgordon esac 122198186Sgordon else 122298186Sgordon case $_action in 122398186Sgordon add|update) 122498186Sgordon if [ -f $_cur ]; then 122598186Sgordon cp -p $_cur $_back 122698186Sgordon fi 122798186Sgordon cp -p $_file $_cur 122898186Sgordon chown root:wheel $_cur 122998186Sgordon ;; 123098186Sgordon remove) 123198186Sgordon mv -f $_cur $_back 123298186Sgordon ;; 123398186Sgordon esac 123498186Sgordon fi 123598186Sgordon} 1236119166Smtm 1237123344Smtm# make_symlink src link 1238123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1239123344Smtm# directory in which link is to be created does not exist 1240123344Smtm# a warning will be displayed and an error will be returned. 1241229783Suqs# Returns 0 on success, 1 otherwise. 1242119166Smtm# 1243123344Smtmmake_symlink() 1244119166Smtm{ 1245123344Smtm local src link linkdir _me 1246123344Smtm src="$1" 1247123344Smtm link="$2" 1248123344Smtm linkdir="`dirname $link`" 1249123344Smtm _me="make_symlink()" 1250119166Smtm 1251123344Smtm if [ -z "$src" -o -z "$link" ]; then 1252123344Smtm warn "$_me: requires two arguments." 1253119166Smtm return 1 1254119166Smtm fi 1255123344Smtm if [ ! -d "$linkdir" ]; then 1256160667Syar warn "$_me: the directory $linkdir does not exist." 1257119166Smtm return 1 1258119166Smtm fi 1259146490Sschweikh if ! ln -sf $src $link; then 1260123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1261119166Smtm return 1 1262119166Smtm fi 1263119166Smtm return 0 1264119166Smtm} 1265119166Smtm 1266119166Smtm# devfs_rulesets_from_file file 1267119166Smtm# Reads a set of devfs commands from file, and creates 1268119166Smtm# the specified rulesets with their rules. Returns non-zero 1269119166Smtm# if there was an error. 1270119166Smtm# 1271119166Smtmdevfs_rulesets_from_file() 1272119166Smtm{ 1273119166Smtm local file _err _me 1274119166Smtm file="$1" 1275119166Smtm _me="devfs_rulesets_from_file" 1276119166Smtm _err=0 1277119166Smtm 1278119166Smtm if [ -z "$file" ]; then 1279119166Smtm warn "$_me: you must specify a file" 1280119166Smtm return 1 1281119166Smtm fi 1282119166Smtm if [ ! -e "$file" ]; then 1283119166Smtm debug "$_me: no such file ($file)" 1284119166Smtm return 0 1285119166Smtm fi 1286119166Smtm debug "reading rulesets from file ($file)" 1287119166Smtm { while read line 1288119166Smtm do 1289119166Smtm case $line in 1290119166Smtm \#*) 1291119166Smtm continue 1292119166Smtm ;; 1293119166Smtm \[*\]*) 1294119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1295119166Smtm if [ -z "$rulenum" ]; then 1296119166Smtm warn "$_me: cannot extract rule number ($line)" 1297119166Smtm _err=1 1298119166Smtm break 1299119166Smtm fi 1300119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1301119166Smtm if [ -z "$rulename" ]; then 1302119166Smtm warn "$_me: cannot extract rule name ($line)" 1303119166Smtm _err=1 1304119166Smtm break; 1305119166Smtm fi 1306119166Smtm eval $rulename=\$rulenum 1307119166Smtm debug "found ruleset: $rulename=$rulenum" 1308146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1309119166Smtm _err=1 1310119166Smtm break 1311119166Smtm fi 1312119166Smtm ;; 1313119166Smtm *) 1314119166Smtm rulecmd="${line%%"\#*"}" 1315119166Smtm # evaluate the command incase it includes 1316119166Smtm # other rules 1317119166Smtm if [ -n "$rulecmd" ]; then 1318119166Smtm debug "adding rule ($rulecmd)" 1319119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1320119166Smtm then 1321119166Smtm _err=1 1322119166Smtm break 1323119166Smtm fi 1324119166Smtm fi 1325119166Smtm ;; 1326119166Smtm esac 1327119166Smtm if [ $_err -ne 0 ]; then 1328119166Smtm debug "error in $_me" 1329119166Smtm break 1330119166Smtm fi 1331119166Smtm done } < $file 1332119166Smtm return $_err 1333119166Smtm} 1334119166Smtm 1335119166Smtm# devfs_init_rulesets 1336119166Smtm# Initializes rulesets from configuration files. Returns 1337119166Smtm# non-zero if there was an error. 1338119166Smtm# 1339119166Smtmdevfs_init_rulesets() 1340119166Smtm{ 1341119166Smtm local file _me 1342119166Smtm _me="devfs_init_rulesets" 1343119166Smtm 1344119166Smtm # Go through this only once 1345119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1346119166Smtm debug "$_me: devfs rulesets already initialized" 1347119166Smtm return 1348119166Smtm fi 1349146490Sschweikh for file in $devfs_rulesets; do 1350217090Sjh if ! devfs_rulesets_from_file $file; then 1351217090Sjh warn "$_me: could not read rules from $file" 1352217090Sjh return 1 1353217090Sjh fi 1354119166Smtm done 1355119166Smtm devfs_rulesets_init=1 1356119166Smtm debug "$_me: devfs rulesets initialized" 1357119166Smtm return 0 1358119166Smtm} 1359119166Smtm 1360119166Smtm# devfs_set_ruleset ruleset [dir] 1361151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1362119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1363119166Smtm# Returns non-zero if it could not set it successfully. 1364119166Smtm# 1365119166Smtmdevfs_set_ruleset() 1366119166Smtm{ 1367119166Smtm local devdir rs _me 1368119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1369119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1370119166Smtm _me="devfs_set_ruleset" 1371119166Smtm 1372119166Smtm if [ -z "$rs" ]; then 1373119166Smtm warn "$_me: you must specify a ruleset number" 1374119166Smtm return 1 1375119166Smtm fi 1376119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1377146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1378119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1379119166Smtm return 1 1380119166Smtm fi 1381119166Smtm return 0 1382119166Smtm} 1383119166Smtm 1384119166Smtm# devfs_apply_ruleset ruleset [dir] 1385119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1386119166Smtm# The ruleset argument must be a ruleset name as specified 1387119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1388119166Smtm# if it could not apply the ruleset. 1389119166Smtm# 1390119166Smtmdevfs_apply_ruleset() 1391119166Smtm{ 1392119166Smtm local devdir rs _me 1393119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1394119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1395119166Smtm _me="devfs_apply_ruleset" 1396119166Smtm 1397119166Smtm if [ -z "$rs" ]; then 1398119166Smtm warn "$_me: you must specify a ruleset" 1399119166Smtm return 1 1400119166Smtm fi 1401119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1402146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1403119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1404119166Smtm return 1 1405119166Smtm fi 1406119166Smtm return 0 1407119166Smtm} 1408119166Smtm 1409119166Smtm# devfs_domount dir [ruleset] 1410119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1411119166Smtm# on the mount-point. It must also be a ruleset name as specified 1412119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1413119166Smtm# 1414119166Smtmdevfs_domount() 1415119166Smtm{ 1416119166Smtm local devdir rs _me 1417119166Smtm devdir="$1" 1418119166Smtm [ -n "$2" ] && rs=$2 || rs= 1419119166Smtm _me="devfs_domount()" 1420119166Smtm 1421119166Smtm if [ -z "$devdir" ]; then 1422119166Smtm warn "$_me: you must specify a mount-point" 1423119166Smtm return 1 1424119166Smtm fi 1425119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1426146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1427119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1428119166Smtm return 1 1429119166Smtm fi 1430119166Smtm if [ -n "$rs" ]; then 1431119166Smtm devfs_init_rulesets 1432119166Smtm devfs_set_ruleset $rs $devdir 1433124797Scperciva devfs -m $devdir rule applyset 1434119166Smtm fi 1435119166Smtm return 0 1436119166Smtm} 1437119166Smtm 1438119166Smtm# devfs_mount_jail dir [ruleset] 1439119166Smtm# Mounts a devfs file system appropriate for jails 1440119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1441119166Smtm# it names will be used instead. If present, ruleset must 1442119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1443119166Smtm# This function returns non-zero if an error occurs. 1444119166Smtm# 1445119166Smtmdevfs_mount_jail() 1446119166Smtm{ 1447119166Smtm local jdev rs _me 1448119166Smtm jdev="$1" 1449119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1450119166Smtm _me="devfs_mount_jail" 1451119166Smtm 1452119166Smtm devfs_init_rulesets 1453146490Sschweikh if ! devfs_domount "$jdev" $rs; then 1454119166Smtm warn "$_me: devfs was not mounted on $jdev" 1455119166Smtm return 1 1456119166Smtm fi 1457119166Smtm return 0 1458119166Smtm} 1459127345Sbrooks 1460127345Sbrooks# Provide a function for normalizing the mounting of memory 1461127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1462127345Sbrooks# as close as possible between 5-current and 4-stable. 1463127345Sbrooks# $1 = size 1464127345Sbrooks# $2 = mount point 1465137451Skeramida# $3 = (optional) extra mdmfs flags 1466146490Sschweikhmount_md() 1467146490Sschweikh{ 1468127345Sbrooks if [ -n "$3" ]; then 1469137451Skeramida flags="$3" 1470127345Sbrooks fi 1471149421Syar /sbin/mdmfs $flags -s $1 md $2 1472127345Sbrooks} 1473131550Scperciva 1474159828Syar# Code common to scripts that need to load a kernel module 1475159828Syar# if it isn't in the kernel yet. Syntax: 1476160666Syar# load_kld [-e regex] [-m module] file 1477159828Syar# where -e or -m chooses the way to check if the module 1478159828Syar# is already loaded: 1479160666Syar# regex is egrep'd in the output from `kldstat -v', 1480160666Syar# module is passed to `kldstat -m'. 1481160666Syar# The default way is as though `-m file' were specified. 1482159828Syarload_kld() 1483159828Syar{ 1484159828Syar local _loaded _mod _opt _re 1485159828Syar 1486159828Syar while getopts "e:m:" _opt; do 1487159828Syar case "$_opt" in 1488159828Syar e) _re="$OPTARG" ;; 1489159828Syar m) _mod="$OPTARG" ;; 1490160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1491159828Syar esac 1492159828Syar done 1493159828Syar shift $(($OPTIND - 1)) 1494160666Syar if [ $# -ne 1 ]; then 1495160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1496160666Syar fi 1497159828Syar _mod=${_mod:-$1} 1498159828Syar _loaded=false 1499159828Syar if [ -n "$_re" ]; then 1500159828Syar if kldstat -v | egrep -q -e "$_re"; then 1501159828Syar _loaded=true 1502159828Syar fi 1503159828Syar else 1504159828Syar if kldstat -q -m "$_mod"; then 1505159828Syar _loaded=true 1506159828Syar fi 1507159828Syar fi 1508159828Syar if ! $_loaded; then 1509159828Syar if ! kldload "$1"; then 1510159828Syar warn "Unable to load kernel module $1" 1511159828Syar return 1 1512160666Syar else 1513160666Syar info "$1 kernel module loaded." 1514159828Syar fi 1515160666Syar else 1516160666Syar debug "load_kld: $1 kernel module already loaded." 1517159828Syar fi 1518159828Syar return 0 1519159828Syar} 1520159828Syar 1521149049Spjd# ltr str src dst 1522149049Spjd# Change every $src in $str to $dst. 1523149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1524149049Spjd# awk(1). 1525149049Spjdltr() 1526149049Spjd{ 1527149049Spjd local _str _src _dst _out _com 1528149049Spjd _str=$1 1529149049Spjd _src=$2 1530149049Spjd _dst=$3 1531149049Spjd _out="" 1532149049Spjd 1533149049Spjd IFS=${_src} 1534149049Spjd for _com in ${_str}; do 1535149049Spjd if [ -z "${_out}" ]; then 1536149049Spjd _out="${_com}" 1537149049Spjd else 1538149049Spjd _out="${_out}${_dst}${_com}" 1539149049Spjd fi 1540149049Spjd done 1541149049Spjd echo "${_out}" 1542149049Spjd} 1543149049Spjd 1544149050Spjd# Creates a list of providers for GELI encryption. 1545149050Spjdgeli_make_list() 1546149050Spjd{ 1547149050Spjd local devices devices2 1548149050Spjd local provider mountpoint type options rest 1549149050Spjd 1550149050Spjd # Create list of GELI providers from fstab. 1551149050Spjd while read provider mountpoint type options rest ; do 1552155570Sflz case ":${options}" in 1553155570Sflz :*noauto*) 1554155570Sflz noauto=yes 1555155570Sflz ;; 1556155570Sflz *) 1557155570Sflz noauto=no 1558155570Sflz ;; 1559155570Sflz esac 1560155570Sflz 1561149050Spjd case ":${provider}" in 1562149050Spjd :#*) 1563149050Spjd continue 1564149050Spjd ;; 1565149050Spjd *.eli) 1566149050Spjd # Skip swap devices. 1567155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1568149050Spjd continue 1569149050Spjd fi 1570149050Spjd devices="${devices} ${provider}" 1571149050Spjd ;; 1572149050Spjd esac 1573149050Spjd done < /etc/fstab 1574149050Spjd 1575149050Spjd # Append providers from geli_devices. 1576149050Spjd devices="${devices} ${geli_devices}" 1577149050Spjd 1578149050Spjd for provider in ${devices}; do 1579149050Spjd provider=${provider%.eli} 1580149050Spjd provider=${provider#/dev/} 1581149050Spjd devices2="${devices2} ${provider}" 1582149050Spjd done 1583149050Spjd 1584149050Spjd echo ${devices2} 1585149050Spjd} 1586149050Spjd 1587153027Sdougb# Find scripts in local_startup directories that use the old syntax 1588153027Sdougb# 1589153027Sdougbfind_local_scripts_old () { 1590153027Sdougb zlist='' 1591153027Sdougb slist='' 1592153027Sdougb for dir in ${local_startup}; do 1593153027Sdougb if [ -d "${dir}" ]; then 1594153027Sdougb for file in ${dir}/[0-9]*.sh; do 1595153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1596153027Sdougb continue 1597153027Sdougb zlist="$zlist $file" 1598153027Sdougb done 1599227366Sjilles for file in ${dir}/[!0-9]*.sh; do 1600153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1601153027Sdougb continue 1602153027Sdougb slist="$slist $file" 1603153027Sdougb done 1604153027Sdougb fi 1605153027Sdougb done 1606153027Sdougb} 1607153027Sdougb 1608153027Sdougbfind_local_scripts_new () { 1609153027Sdougb local_rc='' 1610153027Sdougb for dir in ${local_startup}; do 1611153027Sdougb if [ -d "${dir}" ]; then 1612153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1613153027Sdougb case "$file" in 1614153027Sdougb *.sample) ;; 1615153027Sdougb *) if [ -x "$file" ]; then 1616153027Sdougb local_rc="${local_rc} ${file}" 1617153027Sdougb fi 1618153027Sdougb ;; 1619153027Sdougb esac 1620153027Sdougb done 1621153027Sdougb fi 1622153027Sdougb done 1623153027Sdougb} 1624153027Sdougb 1625165565Syar# check_required_{before|after} command 1626165565Syar# Check for things required by the command before and after its precmd, 1627165565Syar# respectively. The two separate functions are needed because some 1628165565Syar# conditions should prevent precmd from being run while other things 1629165565Syar# depend on precmd having already been run. 1630165565Syar# 1631165565Syarcheck_required_before() 1632165565Syar{ 1633165565Syar local _f 1634165565Syar 1635165565Syar case "$1" in 1636165565Syar start) 1637165565Syar for _f in $required_vars; do 1638165565Syar if ! checkyesno $_f; then 1639165565Syar warn "\$${_f} is not enabled." 1640165565Syar if [ -z "$rc_force" ]; then 1641165565Syar return 1 1642165565Syar fi 1643165565Syar fi 1644165565Syar done 1645165565Syar 1646165565Syar for _f in $required_dirs; do 1647165565Syar if [ ! -d "${_f}/." ]; then 1648165565Syar warn "${_f} is not a directory." 1649165565Syar if [ -z "$rc_force" ]; then 1650165565Syar return 1 1651165565Syar fi 1652165565Syar fi 1653165565Syar done 1654165565Syar 1655165565Syar for _f in $required_files; do 1656165565Syar if [ ! -r "${_f}" ]; then 1657165565Syar warn "${_f} is not readable." 1658165565Syar if [ -z "$rc_force" ]; then 1659165565Syar return 1 1660165565Syar fi 1661165565Syar fi 1662165565Syar done 1663165565Syar ;; 1664165565Syar esac 1665165565Syar 1666165565Syar return 0 1667165565Syar} 1668165565Syar 1669165565Syarcheck_required_after() 1670165565Syar{ 1671165565Syar local _f _args 1672165565Syar 1673165565Syar case "$1" in 1674165565Syar start) 1675165565Syar for _f in $required_modules; do 1676165565Syar case "${_f}" in 1677165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1678165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1679165565Syar *) _args="${_f}" ;; 1680165565Syar esac 1681165565Syar if ! load_kld ${_args}; then 1682165565Syar if [ -z "$rc_force" ]; then 1683165565Syar return 1 1684165565Syar fi 1685165565Syar fi 1686165565Syar done 1687165565Syar ;; 1688165565Syar esac 1689165565Syar 1690165565Syar return 0 1691165565Syar} 1692165565Syar 1693222996Shrs# check_kern_features mib 1694222996Shrs# Return existence of kern.features.* sysctl MIB as true or 1695222996Shrs# false. The result will be cached in $_rc_cache_kern_features_ 1696222996Shrs# namespace. "0" means the kern.features.X exists. 1697222996Shrs 1698222996Shrscheck_kern_features() 1699222996Shrs{ 1700222996Shrs local _v 1701222996Shrs 1702222996Shrs [ -n "$1" ] || return 1; 1703223292Sjilles eval _v=\$_rc_cache_kern_features_$1 1704222996Shrs [ -n "$_v" ] && return "$_v"; 1705222996Shrs 1706222996Shrs if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 1707222996Shrs eval _rc_cache_kern_features_$1=0 1708222996Shrs return 0 1709222996Shrs else 1710222996Shrs eval _rc_cache_kern_features_$1=1 1711222996Shrs return 1 1712222996Shrs fi 1713222996Shrs} 1714222996Shrs 1715197144Shrs# _echoonce var msg mode 1716197144Shrs# mode=0: Echo $msg if ${$var} is empty. 1717197144Shrs# After doing echo, a string is set to ${$var}. 1718197144Shrs# 1719197144Shrs# mode=1: Echo $msg if ${$var} is a string with non-zero length. 1720197144Shrs# 1721197144Shrs_echoonce() 1722197144Shrs{ 1723197144Shrs local _var _msg _mode 1724223227Sjilles eval _var=\$$1 1725197144Shrs _msg=$2 1726197144Shrs _mode=$3 1727197144Shrs 1728197144Shrs case $_mode in 1729197144Shrs 1) [ -n "$_var" ] && echo "$_msg" ;; 1730197144Shrs *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 1731197144Shrs esac 1732197144Shrs} 1733197144Shrs 1734223298Sjillesfi # [ -z "${_rc_subr_loaded}" ] 1735223298Sjilles 1736157841Sflz_rc_subr_loaded=: 1737