rc.subr revision 243324
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 243324 2012-11-20 04:45:04Z hrs $ 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# 74231667Sdougb# force_depend script [rcvar] 7598186Sgordon# Force a service to start. Intended for use by services 76231667Sdougb# to resolve dependency issues. 7798186Sgordon# $1 - filename of script, in /etc/rc.d, to run 78231667Sdougb# $2 - name of the script's rcvar (minus the _enable) 7998186Sgordon# 8098186Sgordonforce_depend() 8198186Sgordon{ 82231667Sdougb local _depend _dep_rcvar 83231667Sdougb 8498186Sgordon _depend="$1" 85231667Sdougb _dep_rcvar="${2:-$1}_enable" 8698186Sgordon 87231667Sdougb [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 88231667Sdougb checkyesno $_dep_rcvar && return 0 89231667Sdougb 90231667Sdougb /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 91231667Sdougb 9298186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 93146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 9498186Sgordon warn "Unable to force ${_depend}. It may already be running." 9598186Sgordon return 1 9698186Sgordon fi 9798186Sgordon} 9898186Sgordon 9998186Sgordon# 10078344Sobrien# checkyesno var 10178344Sobrien# Test $1 variable, and warn if not set to YES or NO. 10278344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 10378344Sobrien# 10478344Sobriencheckyesno() 10578344Sobrien{ 10678344Sobrien eval _value=\$${1} 10798186Sgordon debug "checkyesno: $1 is set to $_value." 10878344Sobrien case $_value in 10978344Sobrien 11078344Sobrien # "yes", "true", "on", or "1" 11178344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 11278344Sobrien return 0 11378344Sobrien ;; 11478344Sobrien 11578344Sobrien # "no", "false", "off", or "0" 11678344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 11778344Sobrien return 1 11878344Sobrien ;; 11978344Sobrien *) 120229822Sdougb warn "\$${1} is not set properly - see rc.conf(5)." 12178344Sobrien return 1 12278344Sobrien ;; 12378344Sobrien esac 12478344Sobrien} 12578344Sobrien 126157473Sflz# 12798186Sgordon# reverse_list list 12898186Sgordon# print the list in reverse order 12978344Sobrien# 13098186Sgordonreverse_list() 13198186Sgordon{ 13298186Sgordon _revlist= 133126286Smtm for _revfile; do 13498186Sgordon _revlist="$_revfile $_revlist" 13598186Sgordon done 13698186Sgordon echo $_revlist 13798186Sgordon} 13898186Sgordon 139169668Smtm# stop_boot always 140169668Smtm# If booting directly to multiuser or $always is enabled, 141169668Smtm# send SIGTERM to the parent (/etc/rc) to abort the boot. 142169668Smtm# Otherwise just exit. 14378344Sobrien# 144169668Smtmstop_boot() 145169668Smtm{ 146169668Smtm local always 147169668Smtm 148178776Smaxim case $1 in 149178776Smaxim # "yes", "true", "on", or "1" 150178770Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 151169668Smtm always=true 152178770Smtm ;; 153178770Smtm *) 154169668Smtm always=false 155178770Smtm ;; 156178775Smaxim esac 157169668Smtm if [ "$autoboot" = yes -o "$always" = true ]; then 158169668Smtm echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 159169668Smtm kill -TERM ${RC_PID} 160169668Smtm fi 161169668Smtm exit 1 162169668Smtm} 163169668Smtm 164169668Smtm# 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 264242183Screes _script="${_chroot}${_chroot:+/}$_procname" 265242183Screes if [ -r "$_script" ]; then 266170282Syar read _interp < $_script # read interpreter name 267170282Syar case "$_interp" in 268170282Syar \#!*) 269170282Syar _interp=${_interp#\#!} # strip #! 270170282Syar set -- $_interp 271170282Syar case $1 in 272170282Syar */bin/env) 273170282Syar shift # drop env to get real name 274170282Syar ;; 275170282Syar esac 276170282Syar if [ $_interpreter != $1 ]; then 277170282Syar warn "\$command_interpreter $_interpreter != $1" 278170282Syar fi 279170282Syar ;; 280170282Syar *) 281170282Syar warn "no shebang line in $_script" 282170282Syar set -- $_interpreter 283170282Syar ;; 284170282Syar esac 285170282Syar else 286170282Syar warn "cannot read shebang line from $_script" 287170282Syar set -- $_interpreter 28878344Sobrien fi 28998186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 290157841Sflz _interpbn=${1##*/} 29198186Sgordon _fp_args='_argv' 29298186Sgordon _fp_match='case "$_argv" in 293157841Sflz ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)' 29498186Sgordon else # a normal daemon 29598186Sgordon _procnamebn=${_procname##*/} 29698186Sgordon _fp_args='_arg0 _argv' 29798186Sgordon _fp_match='case "$_arg0" in 298151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 29998186Sgordon fi 30098186Sgordon 301161435Syar _proccheck="\ 302161436Syar $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 303157657Sflz while read _npid _jid '"$_fp_args"'; do 304161436Syar '"$_fp_match"' 305157657Sflz if [ "$JID" -eq "$_jid" ]; 306157657Sflz then echo -n "$_pref$_npid"; 307157657Sflz _pref=" "; 308157657Sflz fi 30998186Sgordon ;; 31098186Sgordon esac 31198186Sgordon done' 31298186Sgordon 313114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 31498186Sgordon eval $_proccheck 31598186Sgordon} 31698186Sgordon 31798186Sgordon# 31898186Sgordon# wait_for_pids pid [pid ...] 31998186Sgordon# spins until none of the pids exist 32098186Sgordon# 32198186Sgordonwait_for_pids() 32298186Sgordon{ 323206248Sdougb local _list _prefix _nlist _j 324206248Sdougb 325126286Smtm _list="$@" 32698186Sgordon if [ -z "$_list" ]; then 32798186Sgordon return 32898186Sgordon fi 32998186Sgordon _prefix= 33098186Sgordon while true; do 33198186Sgordon _nlist=""; 33298186Sgordon for _j in $_list; do 33398186Sgordon if kill -0 $_j 2>/dev/null; then 33498186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 335206248Sdougb [ -n "$_prefix" ] && sleep 1 33698186Sgordon fi 33798186Sgordon done 33898186Sgordon if [ -z "$_nlist" ]; then 33998186Sgordon break 34078344Sobrien fi 34198186Sgordon _list=$_nlist 34298186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 34398186Sgordon _prefix=", " 344206248Sdougb pwait $_list 2>/dev/null 34578344Sobrien done 34698186Sgordon if [ -n "$_prefix" ]; then 34798186Sgordon echo "." 34898186Sgordon fi 34978344Sobrien} 35078344Sobrien 35178344Sobrien# 352220962Sdougb# get_pidfile_from_conf string file 353220962Sdougb# 354220962Sdougb# Takes a string to search for in the specified file. 355220962Sdougb# Ignores lines with traditional comment characters. 356220962Sdougb# 357220962Sdougb# Example: 358220962Sdougb# 359220962Sdougb# if get_pidfile_from_conf string file; then 360220962Sdougb# pidfile="$_pidfile_from_conf" 361220962Sdougb# else 362220962Sdougb# pidfile='appropriate default' 363220962Sdougb# fi 364220962Sdougb# 365220962Sdougbget_pidfile_from_conf() 366220962Sdougb{ 367220963Sdougb if [ -z "$1" -o -z "$2" ]; then 368220963Sdougb err 3 "USAGE: get_pidfile_from_conf string file ($name)" 369220963Sdougb fi 370220963Sdougb 371220962Sdougb local string file line 372220962Sdougb 373220962Sdougb string="$1" ; file="$2" 374220962Sdougb 375220963Sdougb if [ ! -s "$file" ]; then 376220963Sdougb err 3 "get_pidfile_from_conf: $file does not exist ($name)" 377220962Sdougb fi 378220962Sdougb 379220962Sdougb while read line; do 380220962Sdougb case "$line" in 381220962Sdougb *[#\;]*${string}*) continue ;; 382220962Sdougb *${string}*) break ;; 383220962Sdougb esac 384220962Sdougb done < $file 385220962Sdougb 386220962Sdougb if [ -n "$line" ]; then 387220962Sdougb line=${line#*/} 388220962Sdougb _pidfile_from_conf="/${line%%[\"\;]*}" 389220962Sdougb else 390220962Sdougb return 1 391220962Sdougb fi 392220962Sdougb} 393220962Sdougb 394220962Sdougb# 395197947Sdougb# check_startmsgs 396197947Sdougb# If rc_quiet is set (usually as a result of using faststart at 397197947Sdougb# boot time) check if rc_startmsgs is enabled. 398197947Sdougb# 399197947Sdougbcheck_startmsgs() 400197947Sdougb{ 401197947Sdougb if [ -n "$rc_quiet" ]; then 402197947Sdougb checkyesno rc_startmsgs 403197947Sdougb else 404197947Sdougb return 0 405197947Sdougb fi 406197947Sdougb} 407197947Sdougb 408197947Sdougb# 40998186Sgordon# run_rc_command argument 41098186Sgordon# Search for argument in the list of supported commands, which is: 41198186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 41298186Sgordon# If there's a match, run ${argument}_cmd or the default method 41398186Sgordon# (see below). 41478344Sobrien# 41598186Sgordon# If argument has a given prefix, then change the operation as follows: 41698186Sgordon# Prefix Operation 41778344Sobrien# ------ --------- 418175676Smtm# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 41998186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 420126303Smtm# one Set ${rcvar} to YES 421175676Smtm# quiet Don't output some diagnostics, and set rc_quiet=yes 42278344Sobrien# 42378344Sobrien# The following globals are used: 42478344Sobrien# 42598186Sgordon# Name Needed Purpose 42698186Sgordon# ---- ------ ------- 42778344Sobrien# name y Name of script. 42878344Sobrien# 42978344Sobrien# command n Full path to command. 43098186Sgordon# Not needed if ${rc_arg}_cmd is set for 43178344Sobrien# each keyword. 43278344Sobrien# 43378344Sobrien# command_args n Optional args/shell directives for command. 43478344Sobrien# 43598186Sgordon# command_interpreter n If not empty, command is interpreted, so 43698186Sgordon# call check_{pidfile,process}() appropriately. 43798186Sgordon# 438197144Shrs# desc n Description of script. 439197144Shrs# 44078344Sobrien# extra_commands n List of extra commands supported. 44178344Sobrien# 44298186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 44398186Sgordon# otherwise use check_process $command. 44498186Sgordon# In either case, only check if $command is set. 44578344Sobrien# 44698186Sgordon# procname n Process name to check for instead of $command. 44798186Sgordon# 44878344Sobrien# rcvar n This is checked with checkyesno to determine 44978344Sobrien# if the action should be run. 45078344Sobrien# 451157653Sflz# ${name}_program n Full path to command. 452157653Sflz# Meant to be used in /etc/rc.conf to override 453157653Sflz# ${command}. 454157653Sflz# 45578344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 45698186Sgordon# Requires /usr to be mounted. 45778344Sobrien# 45878344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 45978344Sobrien# (if not using ${name}_chroot). 46078344Sobrien# 46178344Sobrien# ${name}_flags n Arguments to call ${command} with. 46278344Sobrien# NOTE: $flags from the parent environment 46378344Sobrien# can be used to override this. 46478344Sobrien# 465242184Shrs# ${name}_fib n Routing table number to run ${command} with. 466242184Shrs# 46778344Sobrien# ${name}_nice n Nice level to run ${command} at. 46878344Sobrien# 46978344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 47078344Sobrien# using ${name}_chroot. 47198186Sgordon# Requires /usr to be mounted. 47278344Sobrien# 47378344Sobrien# ${name}_group n Group to run chrooted ${command} as. 47498186Sgordon# Requires /usr to be mounted. 47578344Sobrien# 47698186Sgordon# ${name}_groups n Comma separated list of supplementary groups 47798186Sgordon# to run the chrooted ${command} with. 47898186Sgordon# Requires /usr to be mounted. 47978344Sobrien# 48098186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 48178344Sobrien# Otherwise, use default command (see below) 48278344Sobrien# 48398186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 48498186Sgordon# ${rc_arg}_cmd method in the default 48598186Sgordon# operation (i.e, after checking for required 48698186Sgordon# bits and process (non)existence). 48778344Sobrien# If this completes with a non-zero exit code, 48898186Sgordon# don't run ${rc_arg}_cmd. 48978344Sobrien# 49098186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 49198186Sgordon# ${rc_arg}_cmd method, if that method 49298186Sgordon# returned a zero exit code. 49398186Sgordon# 49478344Sobrien# required_dirs n If set, check for the existence of the given 495165565Syar# directories before running a (re)start command. 49678344Sobrien# 49778344Sobrien# required_files n If set, check for the readability of the given 498165565Syar# files before running a (re)start command. 49978344Sobrien# 500165565Syar# required_modules n If set, ensure the given kernel modules are 501165565Syar# loaded before running a (re)start command. 502165565Syar# The check and possible loads are actually 503165565Syar# done after start_precmd so that the modules 504165565Syar# aren't loaded in vain, should the precmd 505165565Syar# return a non-zero status to indicate a error. 506165565Syar# If a word in the list looks like "foo:bar", 507165565Syar# "foo" is the KLD file name and "bar" is the 508165565Syar# module name. If a word looks like "foo~bar", 509165565Syar# "foo" is the KLD file name and "bar" is a 510165565Syar# egrep(1) pattern matching the module name. 511165565Syar# Otherwise the module name is assumed to be 512165565Syar# the same as the KLD file name, which is most 513165565Syar# common. See load_kld(). 514165565Syar# 51578344Sobrien# required_vars n If set, perform checkyesno on each of the 51678344Sobrien# listed variables before running the default 51778344Sobrien# (re)start command. 51878344Sobrien# 51998186Sgordon# Default behaviour for a given argument, if no override method is 52098186Sgordon# provided: 52178344Sobrien# 52298186Sgordon# Argument Default behaviour 52398186Sgordon# -------- ----------------- 52478344Sobrien# start if !running && checkyesno ${rcvar} 52578344Sobrien# ${command} 52678344Sobrien# 52778344Sobrien# stop if ${pidfile} 52898186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 52978344Sobrien# else 53098186Sgordon# rc_pid=$(check_process $command) 53198186Sgordon# kill $sig_stop $rc_pid 53298186Sgordon# wait_for_pids $rc_pid 53398186Sgordon# ($sig_stop defaults to TERM.) 53478344Sobrien# 53598186Sgordon# reload Similar to stop, except use $sig_reload instead, 53698186Sgordon# and doesn't wait_for_pids. 53778344Sobrien# $sig_reload defaults to HUP. 538151685Syar# Note that `reload' isn't provided by default, 539151685Syar# it should be enabled via $extra_commands. 54078344Sobrien# 54178344Sobrien# restart Run `stop' then `start'. 54278344Sobrien# 54398186Sgordon# status Show if ${command} is running, etc. 54478344Sobrien# 54598186Sgordon# poll Wait for ${command} to exit. 54698186Sgordon# 54798186Sgordon# rcvar Display what rc.conf variable is used (if any). 54898186Sgordon# 54998186Sgordon# Variables available to methods, and after run_rc_command() has 55098186Sgordon# completed: 55198186Sgordon# 55298186Sgordon# Variable Purpose 55398186Sgordon# -------- ------- 554126303Smtm# rc_arg Argument to command, after fast/force/one processing 55598186Sgordon# performed 55698186Sgordon# 55798186Sgordon# rc_flags Flags to start the default command with. 55898186Sgordon# Defaults to ${name}_flags, unless overridden 55998186Sgordon# by $flags from the environment. 56098186Sgordon# This variable may be changed by the precmd method. 56198186Sgordon# 56298186Sgordon# rc_pid PID of command (if appropriate) 56398186Sgordon# 56498186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 56598186Sgordon# 56698186Sgordon# rc_force Not empty if "force" was provided (q.v.) 56798186Sgordon# 568175676Smtm# rc_quiet Not empty if "quiet" was provided 56998186Sgordon# 570175676Smtm# 57178344Sobrienrun_rc_command() 57278344Sobrien{ 573116097Smtm _return=0 57498186Sgordon rc_arg=$1 57578344Sobrien if [ -z "$name" ]; then 57698186Sgordon err 3 'run_rc_command: $name is not set.' 57778344Sobrien fi 57878344Sobrien 579132892Smtm # Don't repeat the first argument when passing additional command- 580132892Smtm # line arguments to the command subroutines. 581132892Smtm # 582132892Smtm shift 1 583132892Smtm rc_extra_args="$*" 584132892Smtm 585126303Smtm _rc_prefix= 58698186Sgordon case "$rc_arg" in 58778344Sobrien fast*) # "fast" prefix; don't check pid 58898186Sgordon rc_arg=${rc_arg#fast} 58998186Sgordon rc_fast=yes 590175676Smtm rc_quiet=yes 59178344Sobrien ;; 592198216Sed force*) # "force" prefix; always run 59398186Sgordon rc_force=yes 594126303Smtm _rc_prefix=force 595126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 59678344Sobrien if [ -n "${rcvar}" ]; then 59778344Sobrien eval ${rcvar}=YES 59878344Sobrien fi 59978344Sobrien ;; 600126303Smtm one*) # "one" prefix; set ${rcvar}=yes 601126303Smtm _rc_prefix=one 602126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 603126303Smtm if [ -n "${rcvar}" ]; then 604126303Smtm eval ${rcvar}=YES 605126303Smtm fi 606126303Smtm ;; 607175676Smtm quiet*) # "quiet" prefix; omit some messages 608175676Smtm _rc_prefix=quiet 609175676Smtm rc_arg=${rc_arg#${_rc_prefix}} 610175676Smtm rc_quiet=yes 611175676Smtm ;; 61278344Sobrien esac 61378344Sobrien 614161530Sflz eval _override_command=\$${name}_program 615198162Sdougb command=${_override_command:-$command} 616161530Sflz 61778344Sobrien _keywords="start stop restart rcvar $extra_commands" 61898186Sgordon rc_pid= 61978344Sobrien _pidcmd= 62098186Sgordon _procname=${procname:-${command}} 62198186Sgordon 622131135Smtm # setup pid check command 623131135Smtm if [ -n "$_procname" ]; then 62478344Sobrien if [ -n "$pidfile" ]; then 62598186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 62698186Sgordon else 62798186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 62878344Sobrien fi 62978344Sobrien if [ -n "$_pidcmd" ]; then 63098186Sgordon _keywords="${_keywords} status poll" 63178344Sobrien fi 63278344Sobrien fi 63378344Sobrien 63498186Sgordon if [ -z "$rc_arg" ]; then 635150796Syar rc_usage $_keywords 63678344Sobrien fi 63778344Sobrien 63878344Sobrien if [ -n "$flags" ]; then # allow override from environment 63998186Sgordon rc_flags=$flags 64078344Sobrien else 64198186Sgordon eval rc_flags=\$${name}_flags 64278344Sobrien fi 64398186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 64498186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 645242184Shrs _group=\$${name}_group _groups=\$${name}_groups \ 646242184Shrs _fib=\$${name}_fib 64778344Sobrien 64898186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 649124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 65098186Sgordon unset _user 65198186Sgordon fi 65298186Sgordon fi 65398186Sgordon 654230374Sdougb [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 655179870Smtm 656179870Smtm for _elem in $_keywords; do 657179870Smtm if [ "$_elem" != "$rc_arg" ]; then 658179870Smtm continue 659179870Smtm fi 660206686Sdougb # if ${rcvar} is set, $1 is not "rcvar" 661206686Sdougb # and ${rc_pid} is not set, then run 66278344Sobrien # checkyesno ${rcvar} 66378344Sobrien # and return if that failed 66478344Sobrien # 665220760Sdougb if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 666220760Sdougb [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 667179870Smtm if ! checkyesno ${rcvar}; then 668179870Smtm if [ -n "${rc_quiet}" ]; then 669179870Smtm return 0 670179870Smtm fi 671179870Smtm echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 672179870Smtm echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 673179870Smtm echo "instead of '${rc_arg}'." 674175676Smtm return 0 675175676Smtm fi 67678344Sobrien fi 67778344Sobrien 67878344Sobrien # if there's a custom ${XXX_cmd}, 67978344Sobrien # run that instead of the default 68078344Sobrien # 681165565Syar eval _cmd=\$${rc_arg}_cmd \ 682165565Syar _precmd=\$${rc_arg}_precmd \ 683165565Syar _postcmd=\$${rc_arg}_postcmd 684165565Syar 68578344Sobrien if [ -n "$_cmd" ]; then 686165565Syar _run_rc_precmd || return 1 687165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 688165565Syar _run_rc_postcmd 689116097Smtm return $_return 69078344Sobrien fi 69178344Sobrien 69298186Sgordon case "$rc_arg" in # default operations... 69378344Sobrien 69478344Sobrien status) 695165565Syar _run_rc_precmd || return 1 69698186Sgordon if [ -n "$rc_pid" ]; then 69798186Sgordon echo "${name} is running as pid $rc_pid." 69878344Sobrien else 69978344Sobrien echo "${name} is not running." 70078344Sobrien return 1 70178344Sobrien fi 702165565Syar _run_rc_postcmd 70378344Sobrien ;; 70478344Sobrien 70578344Sobrien start) 706131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 707243324Shrs if [ -z "$rc_quiet" ]; then 708243324Shrs echo 1>&2 "${name} already running? " \ 709243324Shrs "(pid=$rc_pid)." 710243324Shrs fi 711153152Syar return 1 71278344Sobrien fi 71378344Sobrien 714242183Screes if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 715160667Syar warn "run_rc_command: cannot run $command" 716153152Syar return 1 71778344Sobrien fi 71878344Sobrien 719179946Smtm if ! _run_rc_precmd; then 720179946Smtm warn "failed precmd routine for ${name}" 721179946Smtm return 1 722179946Smtm fi 72378344Sobrien 724160668Syar # setup the full command to run 72578344Sobrien # 726197947Sdougb check_startmsgs && echo "Starting ${name}." 72778344Sobrien if [ -n "$_chroot" ]; then 72878344Sobrien _doit="\ 72978344Sobrien${_nice:+nice -n $_nice }\ 730242184Shrs${_fib:+setfib -F $_fib }\ 73178344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 73298186Sgordon$_chroot $command $rc_flags $command_args" 73378344Sobrien else 73478344Sobrien _doit="\ 735161396Syar${_chdir:+cd $_chdir && }\ 736242184Shrs${_fib:+setfib -F $_fib }\ 73798186Sgordon$command $rc_flags $command_args" 73898186Sgordon if [ -n "$_user" ]; then 73998186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 74098186Sgordon fi 741161396Syar if [ -n "$_nice" ]; then 742161396Syar if [ -z "$_user" ]; then 743161396Syar _doit="sh -c \"$_doit\"" 744201036Sdougb fi 745161396Syar _doit="nice -n $_nice $_doit" 746161396Syar fi 74778344Sobrien fi 74898186Sgordon 749165565Syar # run the full command 75098186Sgordon # 751179946Smtm if ! _run_rc_doit "$_doit"; then 752179946Smtm warn "failed to start ${name}" 753179946Smtm return 1 754179946Smtm fi 75598186Sgordon 75698186Sgordon # finally, run postcmd 75798186Sgordon # 758165565Syar _run_rc_postcmd 75978344Sobrien ;; 76078344Sobrien 76178344Sobrien stop) 76298186Sgordon if [ -z "$rc_pid" ]; then 763153152Syar [ -n "$rc_fast" ] && return 0 764165565Syar _run_rc_notrunning 765153152Syar return 1 76678344Sobrien fi 76778344Sobrien 768165565Syar _run_rc_precmd || return 1 76998186Sgordon 77098186Sgordon # send the signal to stop 77198186Sgordon # 77278344Sobrien echo "Stopping ${name}." 773165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 774165565Syar _run_rc_doit "$_doit" || return 1 77598186Sgordon 77698186Sgordon # wait for the command to exit, 77798186Sgordon # and run postcmd. 77898186Sgordon wait_for_pids $rc_pid 779165565Syar 780165565Syar _run_rc_postcmd 78178344Sobrien ;; 78278344Sobrien 78378344Sobrien reload) 78498186Sgordon if [ -z "$rc_pid" ]; then 785165565Syar _run_rc_notrunning 786153152Syar return 1 78778344Sobrien fi 788165565Syar 789165565Syar _run_rc_precmd || return 1 790165565Syar 791165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 792165565Syar _run_rc_doit "$_doit" || return 1 793165565Syar 794165565Syar _run_rc_postcmd 79578344Sobrien ;; 79678344Sobrien 79778344Sobrien restart) 79878344Sobrien # prevent restart being called more 79978344Sobrien # than once by any given script 80078344Sobrien # 801126285Smtm if ${_rc_restart_done:-false}; then 80278344Sobrien return 0 80378344Sobrien fi 804126285Smtm _rc_restart_done=true 80578344Sobrien 806165565Syar _run_rc_precmd || return 1 807165565Syar 808165565Syar # run those in a subshell to keep global variables 809152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 810165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 811165565Syar _return=$? 812165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 81398186Sgordon 814165565Syar _run_rc_postcmd 81578344Sobrien ;; 81678344Sobrien 81798186Sgordon poll) 818165565Syar _run_rc_precmd || return 1 81998186Sgordon if [ -n "$rc_pid" ]; then 82098186Sgordon wait_for_pids $rc_pid 82198186Sgordon fi 822165565Syar _run_rc_postcmd 82398186Sgordon ;; 82498186Sgordon 82578344Sobrien rcvar) 826197144Shrs echo -n "# $name" 827197144Shrs if [ -n "$desc" ]; then 828197144Shrs echo " : $desc" 829197144Shrs else 830197144Shrs echo "" 831197144Shrs fi 832197144Shrs echo "#" 833230103Sdougb # Get unique vars in $rcvar 834230103Sdougb for _v in $rcvar; do 835197144Shrs case $v in 836197144Shrs $_v\ *|\ *$_v|*\ $_v\ *) ;; 837197144Shrs *) v="${v# } $_v" ;; 838197144Shrs esac 839197144Shrs done 840197144Shrs 841197144Shrs # Display variables. 842197144Shrs for _v in $v; do 843197144Shrs if [ -z "$_v" ]; then 844197144Shrs continue 84578344Sobrien fi 846197144Shrs 847197144Shrs eval _desc=\$${_v}_desc 848197144Shrs eval _defval=\$${_v}_defval 849197144Shrs _h="-" 850197144Shrs 851197144Shrs eval echo \"$_v=\\\"\$$_v\\\"\" 852197144Shrs # decode multiple lines of _desc 853197144Shrs while [ -n "$_desc" ]; do 854197144Shrs case $_desc in 855197144Shrs *^^*) 856197144Shrs echo "# $_h ${_desc%%^^*}" 857197144Shrs _desc=${_desc#*^^} 858197144Shrs _h=" " 859197144Shrs ;; 860197144Shrs *) 861197144Shrs echo "# $_h ${_desc}" 862197144Shrs break 863197144Shrs ;; 864197144Shrs esac 865197144Shrs done 866197144Shrs echo "# (default: \"$_defval\")" 867197144Shrs done 868197144Shrs echo "" 86978344Sobrien ;; 87078344Sobrien 87178344Sobrien *) 872150796Syar rc_usage $_keywords 87378344Sobrien ;; 87478344Sobrien 87578344Sobrien esac 876116097Smtm return $_return 87778344Sobrien done 87878344Sobrien 87998186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 880150796Syar rc_usage $_keywords 881153152Syar # not reached 88278344Sobrien} 88378344Sobrien 88478344Sobrien# 885165565Syar# Helper functions for run_rc_command: common code. 886165565Syar# They use such global variables besides the exported rc_* ones: 887165565Syar# 888165565Syar# name R/W 889165565Syar# ------------------ 890165565Syar# _precmd R 891165565Syar# _postcmd R 892165565Syar# _return W 893165565Syar# 894165565Syar_run_rc_precmd() 895165565Syar{ 896165565Syar check_required_before "$rc_arg" || return 1 897165565Syar 898165565Syar if [ -n "$_precmd" ]; then 899165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 900165565Syar eval "$_precmd $rc_extra_args" 901165565Syar _return=$? 902165565Syar 903165565Syar # If precmd failed and force isn't set, request exit. 904165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 905165565Syar return 1 906165565Syar fi 907165565Syar fi 908165565Syar 909165565Syar check_required_after "$rc_arg" || return 1 910165565Syar 911165565Syar return 0 912165565Syar} 913165565Syar 914165565Syar_run_rc_postcmd() 915165565Syar{ 916165565Syar if [ -n "$_postcmd" ]; then 917165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 918165565Syar eval "$_postcmd $rc_extra_args" 919165565Syar _return=$? 920165565Syar fi 921165565Syar return 0 922165565Syar} 923165565Syar 924165565Syar_run_rc_doit() 925165565Syar{ 926165565Syar debug "run_rc_command: doit: $*" 927165565Syar eval "$@" 928165565Syar _return=$? 929165565Syar 930165565Syar # If command failed and force isn't set, request exit. 931165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 932165565Syar return 1 933165565Syar fi 934165565Syar 935165565Syar return 0 936165565Syar} 937165565Syar 938165565Syar_run_rc_notrunning() 939165565Syar{ 940165565Syar local _pidmsg 941165565Syar 942165565Syar if [ -n "$pidfile" ]; then 943165565Syar _pidmsg=" (check $pidfile)." 944165565Syar else 945165565Syar _pidmsg= 946165565Syar fi 947165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 948165565Syar} 949165565Syar 950165565Syar_run_rc_killcmd() 951165565Syar{ 952165565Syar local _cmd 953165565Syar 954165565Syar _cmd="kill -$1 $rc_pid" 955165565Syar if [ -n "$_user" ]; then 956165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 957165565Syar fi 958165565Syar echo "$_cmd" 959165565Syar} 960165565Syar 961165565Syar# 96278344Sobrien# run_rc_script file arg 96378344Sobrien# Start the script `file' with `arg', and correctly handle the 964201038Sdougb# return value from the script. 965201038Sdougb# If `file' ends with `.sh', it's sourced into the current environment 966201038Sdougb# when $rc_fast_and_loose is set, otherwise it is run as a child process. 967201038Sdougb# If `file' appears to be a backup or scratch file, ignore it. 968201038Sdougb# Otherwise if it is executable run as a child process. 96978344Sobrien# 97078344Sobrienrun_rc_script() 97178344Sobrien{ 97278344Sobrien _file=$1 97378344Sobrien _arg=$2 97478344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 97578344Sobrien err 3 'USAGE: run_rc_script file arg' 97678344Sobrien fi 97778344Sobrien 97898186Sgordon unset name command command_args command_interpreter \ 97998186Sgordon extra_commands pidfile procname \ 980230103Sdougb rcvar rcvars_obsolete required_dirs required_files \ 981197144Shrs required_vars 98298186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 98398186Sgordon 98478344Sobrien case "$_file" in 985193118Sdougb /etc/rc.d/*.sh) # no longer allowed in the base 986193118Sdougb warn "Ignoring old-style startup script $_file" 98778344Sobrien ;; 988153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 98998186Sgordon warn "Ignoring scratch file $_file" 99098186Sgordon ;; 99178344Sobrien *) # run in subshell 99298186Sgordon if [ -x $_file ]; then 99398186Sgordon if [ -n "$rc_fast_and_loose" ]; then 994146490Sschweikh set $_arg; . $_file 99598186Sgordon else 996231888Sdelphij ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 997231888Sdelphij trap "echo Script $_file interrupted >&2 ; exit 1" 2 998231888Sdelphij trap "echo Script $_file running >&2" 29 999146490Sschweikh set $_arg; . $_file ) 100098186Sgordon fi 100198186Sgordon fi 100278344Sobrien ;; 100378344Sobrien esac 100478344Sobrien} 100578344Sobrien 100678344Sobrien# 1007157653Sflz# load_rc_config name 1008157653Sflz# Source in the configuration file for a given name. 100978344Sobrien# 101078344Sobrienload_rc_config() 101178344Sobrien{ 1012230103Sdougb local _name _rcvar_val _var _defval _v _msg _new 1013157653Sflz _name=$1 1014157653Sflz if [ -z "$_name" ]; then 1015157653Sflz err 3 'USAGE: load_rc_config name' 101678344Sobrien fi 101778344Sobrien 1018219612Sdougb if ${_rc_conf_loaded:-false}; then 1019219612Sdougb : 1020219612Sdougb else 102198186Sgordon if [ -r /etc/defaults/rc.conf ]; then 102298186Sgordon debug "Sourcing /etc/defaults/rc.conf" 102398186Sgordon . /etc/defaults/rc.conf 102498186Sgordon source_rc_confs 102598186Sgordon elif [ -r /etc/rc.conf ]; then 102698186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 102798186Sgordon . /etc/rc.conf 102898186Sgordon fi 1029126285Smtm _rc_conf_loaded=true 103098186Sgordon fi 1031161530Sflz if [ -f /etc/rc.conf.d/"$_name" ]; then 1032157653Sflz debug "Sourcing /etc/rc.conf.d/${_name}" 1033161530Sflz . /etc/rc.conf.d/"$_name" 1034157653Sflz fi 1035179872Smtm 1036197144Shrs # Set defaults if defined. 1037230103Sdougb for _var in $rcvar; do 1038223227Sjilles eval _defval=\$${_var}_defval 1039197144Shrs if [ -n "$_defval" ]; then 1040197144Shrs eval : \${$_var:=\$${_var}_defval} 1041197144Shrs fi 1042197144Shrs done 1043197144Shrs 1044197144Shrs # check obsolete rc.conf variables 1045197144Shrs for _var in $rcvars_obsolete; do 1046223227Sjilles eval _v=\$$_var 1047223227Sjilles eval _msg=\$${_var}_obsolete_msg 1048223227Sjilles eval _new=\$${_var}_newvar 1049197144Shrs case $_v in 1050197144Shrs "") 1051197144Shrs ;; 1052197144Shrs *) 1053197144Shrs if [ -z "$_new" ]; then 1054197144Shrs _msg="Ignored." 1055197144Shrs else 1056197144Shrs eval $_new=\"\$$_var\" 1057197144Shrs if [ -z "$_msg" ]; then 1058197144Shrs _msg="Use \$$_new instead." 1059197144Shrs fi 1060197144Shrs fi 1061197144Shrs warn "\$$_var is obsolete. $_msg" 1062197144Shrs ;; 1063197144Shrs esac 1064197144Shrs done 106578344Sobrien} 1066201036Sdougb 1067157473Sflz# 1068157653Sflz# load_rc_config_var name var 1069157653Sflz# Read the rc.conf(5) var for name and set in the 1070157473Sflz# current shell, using load_rc_config in a subshell to prevent 1071157473Sflz# unwanted side effects from other variable assignments. 1072157473Sflz# 1073157473Sflzload_rc_config_var() 1074157473Sflz{ 1075157473Sflz if [ $# -ne 2 ]; then 1076157653Sflz err 3 'USAGE: load_rc_config_var name var' 1077157473Sflz fi 1078157473Sflz eval $(eval '( 1079157473Sflz load_rc_config '$1' >/dev/null; 1080157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1081157473Sflz echo '$2'=\'\''${'$2'}\'\''; 1082157473Sflz fi 1083157473Sflz )' ) 1084157473Sflz} 108578344Sobrien 108678344Sobrien# 108778344Sobrien# rc_usage commands 108878344Sobrien# Print a usage string for $0, with `commands' being a list of 108978344Sobrien# valid commands. 109078344Sobrien# 109178344Sobrienrc_usage() 109278344Sobrien{ 1093230007Srea echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 109478344Sobrien 109578344Sobrien _sep= 1096126286Smtm for _elem; do 109778344Sobrien echo -n 1>&2 "$_sep$_elem" 109878344Sobrien _sep="|" 109978344Sobrien done 110078344Sobrien echo 1>&2 ")" 110178344Sobrien exit 1 110278344Sobrien} 110378344Sobrien 110478344Sobrien# 110578344Sobrien# err exitval message 110678344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 110778344Sobrien# 110878344Sobrienerr() 110978344Sobrien{ 111078344Sobrien exitval=$1 111178344Sobrien shift 111278344Sobrien 1113106643Sgordon if [ -x /usr/bin/logger ]; then 1114106643Sgordon logger "$0: ERROR: $*" 1115106643Sgordon fi 1116106643Sgordon echo 1>&2 "$0: ERROR: $*" 111778344Sobrien exit $exitval 111878344Sobrien} 111978344Sobrien 112078344Sobrien# 112178344Sobrien# warn message 112278344Sobrien# Display message to stderr and log to the syslog. 112378344Sobrien# 112478344Sobrienwarn() 112578344Sobrien{ 1126106643Sgordon if [ -x /usr/bin/logger ]; then 1127106643Sgordon logger "$0: WARNING: $*" 1128106643Sgordon fi 1129106643Sgordon echo 1>&2 "$0: WARNING: $*" 113078344Sobrien} 113198186Sgordon 113298186Sgordon# 113398186Sgordon# info message 113498186Sgordon# Display informational message to stdout and log to syslog. 113598186Sgordon# 113698186Sgordoninfo() 113798186Sgordon{ 1138119170Smtm case ${rc_info} in 1139119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1140119170Smtm if [ -x /usr/bin/logger ]; then 1141119170Smtm logger "$0: INFO: $*" 1142119170Smtm fi 1143119170Smtm echo "$0: INFO: $*" 1144119170Smtm ;; 1145119170Smtm esac 114698186Sgordon} 114798186Sgordon 114898186Sgordon# 114998186Sgordon# debug message 1150106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 115198186Sgordon# BEWARE that you don't call any subroutine that itself calls this 115298186Sgordon# function. 115398186Sgordon# 115498186Sgordondebug() 115598186Sgordon{ 115698186Sgordon case ${rc_debug} in 115798186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1158106700Sgordon if [ -x /usr/bin/logger ]; then 1159162947Syar logger "$0: DEBUG: $*" 1160106700Sgordon fi 1161146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 116298186Sgordon ;; 116398186Sgordon esac 116498186Sgordon} 116598186Sgordon 116698186Sgordon# 116798186Sgordon# backup_file action file cur backup 116898186Sgordon# Make a backup copy of `file' into `cur', and save the previous 116998186Sgordon# version of `cur' as `backup' or use rcs for archiving. 117098186Sgordon# 117198186Sgordon# This routine checks the value of the backup_uses_rcs variable, 117298186Sgordon# which can be either YES or NO. 117398186Sgordon# 117498186Sgordon# The `action' keyword can be one of the following: 117598186Sgordon# 117698186Sgordon# add `file' is now being backed up (and is possibly 117798186Sgordon# being reentered into the backups system). `cur' 117898186Sgordon# is created and RCS files, if necessary, are 117998186Sgordon# created as well. 118098186Sgordon# 118198186Sgordon# update `file' has changed and needs to be backed up. 118298186Sgordon# If `cur' exists, it is copied to to `back' or 118398186Sgordon# checked into RCS (if the repository file is old), 118498186Sgordon# and then `file' is copied to `cur'. Another RCS 118598186Sgordon# check in done here if RCS is being used. 118698186Sgordon# 118798186Sgordon# remove `file' is no longer being tracked by the backups 118898186Sgordon# system. If RCS is not being used, `cur' is moved 118998186Sgordon# to `back', otherwise an empty file is checked in, 119098186Sgordon# and then `cur' is removed. 119198186Sgordon# 119298186Sgordon# 119398186Sgordonbackup_file() 119498186Sgordon{ 119598186Sgordon _action=$1 119698186Sgordon _file=$2 119798186Sgordon _cur=$3 119898186Sgordon _back=$4 119998186Sgordon 120098186Sgordon if checkyesno backup_uses_rcs; then 120198186Sgordon _msg0="backup archive" 120298186Sgordon _msg1="update" 120398186Sgordon 120498186Sgordon # ensure that history file is not locked 120598186Sgordon if [ -f $_cur,v ]; then 120698186Sgordon rcs -q -u -U -M $_cur 120798186Sgordon fi 120898186Sgordon 120998186Sgordon # ensure after switching to rcs that the 121098186Sgordon # current backup is not lost 121198186Sgordon if [ -f $_cur ]; then 121298186Sgordon # no archive, or current newer than archive 121398186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 121498186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 121598186Sgordon rcs -q -kb -U $_cur 121698186Sgordon co -q -f -u $_cur 121798186Sgordon fi 121898186Sgordon fi 121998186Sgordon 122098186Sgordon case $_action in 122198186Sgordon add|update) 122298186Sgordon cp -p $_file $_cur 122398186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 122498186Sgordon rcs -q -kb -U $_cur 122598186Sgordon co -q -f -u $_cur 122698186Sgordon chown root:wheel $_cur $_cur,v 122798186Sgordon ;; 122898186Sgordon remove) 122998186Sgordon cp /dev/null $_cur 123098186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 123198186Sgordon rcs -q -kb -U $_cur 123298186Sgordon chown root:wheel $_cur $_cur,v 123398186Sgordon rm $_cur 123498186Sgordon ;; 123598186Sgordon esac 123698186Sgordon else 123798186Sgordon case $_action in 123898186Sgordon add|update) 123998186Sgordon if [ -f $_cur ]; then 124098186Sgordon cp -p $_cur $_back 124198186Sgordon fi 124298186Sgordon cp -p $_file $_cur 124398186Sgordon chown root:wheel $_cur 124498186Sgordon ;; 124598186Sgordon remove) 124698186Sgordon mv -f $_cur $_back 124798186Sgordon ;; 124898186Sgordon esac 124998186Sgordon fi 125098186Sgordon} 1251119166Smtm 1252123344Smtm# make_symlink src link 1253123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1254123344Smtm# directory in which link is to be created does not exist 1255123344Smtm# a warning will be displayed and an error will be returned. 1256229783Suqs# Returns 0 on success, 1 otherwise. 1257119166Smtm# 1258123344Smtmmake_symlink() 1259119166Smtm{ 1260123344Smtm local src link linkdir _me 1261123344Smtm src="$1" 1262123344Smtm link="$2" 1263123344Smtm linkdir="`dirname $link`" 1264123344Smtm _me="make_symlink()" 1265119166Smtm 1266123344Smtm if [ -z "$src" -o -z "$link" ]; then 1267123344Smtm warn "$_me: requires two arguments." 1268119166Smtm return 1 1269119166Smtm fi 1270123344Smtm if [ ! -d "$linkdir" ]; then 1271160667Syar warn "$_me: the directory $linkdir does not exist." 1272119166Smtm return 1 1273119166Smtm fi 1274146490Sschweikh if ! ln -sf $src $link; then 1275123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1276119166Smtm return 1 1277119166Smtm fi 1278119166Smtm return 0 1279119166Smtm} 1280119166Smtm 1281119166Smtm# devfs_rulesets_from_file file 1282119166Smtm# Reads a set of devfs commands from file, and creates 1283119166Smtm# the specified rulesets with their rules. Returns non-zero 1284119166Smtm# if there was an error. 1285119166Smtm# 1286119166Smtmdevfs_rulesets_from_file() 1287119166Smtm{ 1288119166Smtm local file _err _me 1289119166Smtm file="$1" 1290119166Smtm _me="devfs_rulesets_from_file" 1291119166Smtm _err=0 1292119166Smtm 1293119166Smtm if [ -z "$file" ]; then 1294119166Smtm warn "$_me: you must specify a file" 1295119166Smtm return 1 1296119166Smtm fi 1297119166Smtm if [ ! -e "$file" ]; then 1298119166Smtm debug "$_me: no such file ($file)" 1299119166Smtm return 0 1300119166Smtm fi 1301119166Smtm debug "reading rulesets from file ($file)" 1302119166Smtm { while read line 1303119166Smtm do 1304119166Smtm case $line in 1305119166Smtm \#*) 1306119166Smtm continue 1307119166Smtm ;; 1308119166Smtm \[*\]*) 1309119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1310119166Smtm if [ -z "$rulenum" ]; then 1311119166Smtm warn "$_me: cannot extract rule number ($line)" 1312119166Smtm _err=1 1313119166Smtm break 1314119166Smtm fi 1315119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1316119166Smtm if [ -z "$rulename" ]; then 1317119166Smtm warn "$_me: cannot extract rule name ($line)" 1318119166Smtm _err=1 1319119166Smtm break; 1320119166Smtm fi 1321119166Smtm eval $rulename=\$rulenum 1322119166Smtm debug "found ruleset: $rulename=$rulenum" 1323146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1324119166Smtm _err=1 1325119166Smtm break 1326119166Smtm fi 1327119166Smtm ;; 1328119166Smtm *) 1329119166Smtm rulecmd="${line%%"\#*"}" 1330119166Smtm # evaluate the command incase it includes 1331119166Smtm # other rules 1332119166Smtm if [ -n "$rulecmd" ]; then 1333119166Smtm debug "adding rule ($rulecmd)" 1334119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1335119166Smtm then 1336119166Smtm _err=1 1337119166Smtm break 1338119166Smtm fi 1339119166Smtm fi 1340119166Smtm ;; 1341119166Smtm esac 1342119166Smtm if [ $_err -ne 0 ]; then 1343119166Smtm debug "error in $_me" 1344119166Smtm break 1345119166Smtm fi 1346119166Smtm done } < $file 1347119166Smtm return $_err 1348119166Smtm} 1349119166Smtm 1350119166Smtm# devfs_init_rulesets 1351119166Smtm# Initializes rulesets from configuration files. Returns 1352119166Smtm# non-zero if there was an error. 1353119166Smtm# 1354119166Smtmdevfs_init_rulesets() 1355119166Smtm{ 1356119166Smtm local file _me 1357119166Smtm _me="devfs_init_rulesets" 1358119166Smtm 1359119166Smtm # Go through this only once 1360119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1361119166Smtm debug "$_me: devfs rulesets already initialized" 1362119166Smtm return 1363119166Smtm fi 1364146490Sschweikh for file in $devfs_rulesets; do 1365217090Sjh if ! devfs_rulesets_from_file $file; then 1366217090Sjh warn "$_me: could not read rules from $file" 1367217090Sjh return 1 1368217090Sjh fi 1369119166Smtm done 1370119166Smtm devfs_rulesets_init=1 1371119166Smtm debug "$_me: devfs rulesets initialized" 1372119166Smtm return 0 1373119166Smtm} 1374119166Smtm 1375119166Smtm# devfs_set_ruleset ruleset [dir] 1376151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1377119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1378119166Smtm# Returns non-zero if it could not set it successfully. 1379119166Smtm# 1380119166Smtmdevfs_set_ruleset() 1381119166Smtm{ 1382119166Smtm local devdir rs _me 1383119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1384119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1385119166Smtm _me="devfs_set_ruleset" 1386119166Smtm 1387119166Smtm if [ -z "$rs" ]; then 1388119166Smtm warn "$_me: you must specify a ruleset number" 1389119166Smtm return 1 1390119166Smtm fi 1391119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1392146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1393119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1394119166Smtm return 1 1395119166Smtm fi 1396119166Smtm return 0 1397119166Smtm} 1398119166Smtm 1399119166Smtm# devfs_apply_ruleset ruleset [dir] 1400119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1401119166Smtm# The ruleset argument must be a ruleset name as specified 1402119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1403119166Smtm# if it could not apply the ruleset. 1404119166Smtm# 1405119166Smtmdevfs_apply_ruleset() 1406119166Smtm{ 1407119166Smtm local devdir rs _me 1408119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1409119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1410119166Smtm _me="devfs_apply_ruleset" 1411119166Smtm 1412119166Smtm if [ -z "$rs" ]; then 1413119166Smtm warn "$_me: you must specify a ruleset" 1414119166Smtm return 1 1415119166Smtm fi 1416119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1417146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1418119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1419119166Smtm return 1 1420119166Smtm fi 1421119166Smtm return 0 1422119166Smtm} 1423119166Smtm 1424119166Smtm# devfs_domount dir [ruleset] 1425119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1426119166Smtm# on the mount-point. It must also be a ruleset name as specified 1427119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1428119166Smtm# 1429119166Smtmdevfs_domount() 1430119166Smtm{ 1431119166Smtm local devdir rs _me 1432119166Smtm devdir="$1" 1433119166Smtm [ -n "$2" ] && rs=$2 || rs= 1434119166Smtm _me="devfs_domount()" 1435119166Smtm 1436119166Smtm if [ -z "$devdir" ]; then 1437119166Smtm warn "$_me: you must specify a mount-point" 1438119166Smtm return 1 1439119166Smtm fi 1440119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1441146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1442119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1443119166Smtm return 1 1444119166Smtm fi 1445119166Smtm if [ -n "$rs" ]; then 1446119166Smtm devfs_init_rulesets 1447119166Smtm devfs_set_ruleset $rs $devdir 1448124797Scperciva devfs -m $devdir rule applyset 1449119166Smtm fi 1450119166Smtm return 0 1451119166Smtm} 1452119166Smtm 1453119166Smtm# devfs_mount_jail dir [ruleset] 1454119166Smtm# Mounts a devfs file system appropriate for jails 1455119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1456119166Smtm# it names will be used instead. If present, ruleset must 1457119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1458119166Smtm# This function returns non-zero if an error occurs. 1459119166Smtm# 1460119166Smtmdevfs_mount_jail() 1461119166Smtm{ 1462119166Smtm local jdev rs _me 1463119166Smtm jdev="$1" 1464119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1465119166Smtm _me="devfs_mount_jail" 1466119166Smtm 1467119166Smtm devfs_init_rulesets 1468146490Sschweikh if ! devfs_domount "$jdev" $rs; then 1469119166Smtm warn "$_me: devfs was not mounted on $jdev" 1470119166Smtm return 1 1471119166Smtm fi 1472119166Smtm return 0 1473119166Smtm} 1474127345Sbrooks 1475127345Sbrooks# Provide a function for normalizing the mounting of memory 1476127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1477127345Sbrooks# as close as possible between 5-current and 4-stable. 1478127345Sbrooks# $1 = size 1479127345Sbrooks# $2 = mount point 1480137451Skeramida# $3 = (optional) extra mdmfs flags 1481146490Sschweikhmount_md() 1482146490Sschweikh{ 1483127345Sbrooks if [ -n "$3" ]; then 1484137451Skeramida flags="$3" 1485127345Sbrooks fi 1486149421Syar /sbin/mdmfs $flags -s $1 md $2 1487127345Sbrooks} 1488131550Scperciva 1489159828Syar# Code common to scripts that need to load a kernel module 1490159828Syar# if it isn't in the kernel yet. Syntax: 1491160666Syar# load_kld [-e regex] [-m module] file 1492159828Syar# where -e or -m chooses the way to check if the module 1493159828Syar# is already loaded: 1494160666Syar# regex is egrep'd in the output from `kldstat -v', 1495160666Syar# module is passed to `kldstat -m'. 1496160666Syar# The default way is as though `-m file' were specified. 1497159828Syarload_kld() 1498159828Syar{ 1499159828Syar local _loaded _mod _opt _re 1500159828Syar 1501159828Syar while getopts "e:m:" _opt; do 1502159828Syar case "$_opt" in 1503159828Syar e) _re="$OPTARG" ;; 1504159828Syar m) _mod="$OPTARG" ;; 1505160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1506159828Syar esac 1507159828Syar done 1508159828Syar shift $(($OPTIND - 1)) 1509160666Syar if [ $# -ne 1 ]; then 1510160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1511160666Syar fi 1512159828Syar _mod=${_mod:-$1} 1513159828Syar _loaded=false 1514159828Syar if [ -n "$_re" ]; then 1515159828Syar if kldstat -v | egrep -q -e "$_re"; then 1516159828Syar _loaded=true 1517159828Syar fi 1518159828Syar else 1519159828Syar if kldstat -q -m "$_mod"; then 1520159828Syar _loaded=true 1521159828Syar fi 1522159828Syar fi 1523159828Syar if ! $_loaded; then 1524159828Syar if ! kldload "$1"; then 1525159828Syar warn "Unable to load kernel module $1" 1526159828Syar return 1 1527160666Syar else 1528160666Syar info "$1 kernel module loaded." 1529159828Syar fi 1530160666Syar else 1531160666Syar debug "load_kld: $1 kernel module already loaded." 1532159828Syar fi 1533159828Syar return 0 1534159828Syar} 1535159828Syar 1536149049Spjd# ltr str src dst 1537149049Spjd# Change every $src in $str to $dst. 1538149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1539149049Spjd# awk(1). 1540149049Spjdltr() 1541149049Spjd{ 1542149049Spjd local _str _src _dst _out _com 1543149049Spjd _str=$1 1544149049Spjd _src=$2 1545149049Spjd _dst=$3 1546149049Spjd _out="" 1547149049Spjd 1548149049Spjd IFS=${_src} 1549149049Spjd for _com in ${_str}; do 1550149049Spjd if [ -z "${_out}" ]; then 1551149049Spjd _out="${_com}" 1552149049Spjd else 1553149049Spjd _out="${_out}${_dst}${_com}" 1554149049Spjd fi 1555149049Spjd done 1556149049Spjd echo "${_out}" 1557149049Spjd} 1558149049Spjd 1559149050Spjd# Creates a list of providers for GELI encryption. 1560149050Spjdgeli_make_list() 1561149050Spjd{ 1562149050Spjd local devices devices2 1563149050Spjd local provider mountpoint type options rest 1564149050Spjd 1565149050Spjd # Create list of GELI providers from fstab. 1566149050Spjd while read provider mountpoint type options rest ; do 1567155570Sflz case ":${options}" in 1568155570Sflz :*noauto*) 1569155570Sflz noauto=yes 1570155570Sflz ;; 1571155570Sflz *) 1572155570Sflz noauto=no 1573155570Sflz ;; 1574155570Sflz esac 1575155570Sflz 1576149050Spjd case ":${provider}" in 1577149050Spjd :#*) 1578149050Spjd continue 1579149050Spjd ;; 1580149050Spjd *.eli) 1581149050Spjd # Skip swap devices. 1582155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1583149050Spjd continue 1584149050Spjd fi 1585149050Spjd devices="${devices} ${provider}" 1586149050Spjd ;; 1587149050Spjd esac 1588149050Spjd done < /etc/fstab 1589149050Spjd 1590149050Spjd # Append providers from geli_devices. 1591149050Spjd devices="${devices} ${geli_devices}" 1592149050Spjd 1593149050Spjd for provider in ${devices}; do 1594149050Spjd provider=${provider%.eli} 1595149050Spjd provider=${provider#/dev/} 1596149050Spjd devices2="${devices2} ${provider}" 1597149050Spjd done 1598149050Spjd 1599149050Spjd echo ${devices2} 1600149050Spjd} 1601149050Spjd 1602153027Sdougb# Find scripts in local_startup directories that use the old syntax 1603153027Sdougb# 1604238416Skevlofind_local_scripts_old() { 1605153027Sdougb zlist='' 1606153027Sdougb slist='' 1607153027Sdougb for dir in ${local_startup}; do 1608153027Sdougb if [ -d "${dir}" ]; then 1609153027Sdougb for file in ${dir}/[0-9]*.sh; do 1610153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1611153027Sdougb continue 1612153027Sdougb zlist="$zlist $file" 1613153027Sdougb done 1614227366Sjilles for file in ${dir}/[!0-9]*.sh; do 1615153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1616153027Sdougb continue 1617153027Sdougb slist="$slist $file" 1618153027Sdougb done 1619153027Sdougb fi 1620153027Sdougb done 1621153027Sdougb} 1622153027Sdougb 1623238416Skevlofind_local_scripts_new() { 1624153027Sdougb local_rc='' 1625153027Sdougb for dir in ${local_startup}; do 1626153027Sdougb if [ -d "${dir}" ]; then 1627153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1628153027Sdougb case "$file" in 1629153027Sdougb *.sample) ;; 1630153027Sdougb *) if [ -x "$file" ]; then 1631153027Sdougb local_rc="${local_rc} ${file}" 1632153027Sdougb fi 1633153027Sdougb ;; 1634153027Sdougb esac 1635153027Sdougb done 1636153027Sdougb fi 1637153027Sdougb done 1638153027Sdougb} 1639153027Sdougb 1640165565Syar# check_required_{before|after} command 1641165565Syar# Check for things required by the command before and after its precmd, 1642165565Syar# respectively. The two separate functions are needed because some 1643165565Syar# conditions should prevent precmd from being run while other things 1644165565Syar# depend on precmd having already been run. 1645165565Syar# 1646165565Syarcheck_required_before() 1647165565Syar{ 1648165565Syar local _f 1649165565Syar 1650165565Syar case "$1" in 1651165565Syar start) 1652165565Syar for _f in $required_vars; do 1653165565Syar if ! checkyesno $_f; then 1654165565Syar warn "\$${_f} is not enabled." 1655165565Syar if [ -z "$rc_force" ]; then 1656165565Syar return 1 1657165565Syar fi 1658165565Syar fi 1659165565Syar done 1660165565Syar 1661165565Syar for _f in $required_dirs; do 1662165565Syar if [ ! -d "${_f}/." ]; then 1663165565Syar warn "${_f} is not a directory." 1664165565Syar if [ -z "$rc_force" ]; then 1665165565Syar return 1 1666165565Syar fi 1667165565Syar fi 1668165565Syar done 1669165565Syar 1670165565Syar for _f in $required_files; do 1671165565Syar if [ ! -r "${_f}" ]; then 1672165565Syar warn "${_f} is not readable." 1673165565Syar if [ -z "$rc_force" ]; then 1674165565Syar return 1 1675165565Syar fi 1676165565Syar fi 1677165565Syar done 1678165565Syar ;; 1679165565Syar esac 1680165565Syar 1681165565Syar return 0 1682165565Syar} 1683165565Syar 1684165565Syarcheck_required_after() 1685165565Syar{ 1686165565Syar local _f _args 1687165565Syar 1688165565Syar case "$1" in 1689165565Syar start) 1690165565Syar for _f in $required_modules; do 1691165565Syar case "${_f}" in 1692165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1693165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1694165565Syar *) _args="${_f}" ;; 1695165565Syar esac 1696165565Syar if ! load_kld ${_args}; then 1697165565Syar if [ -z "$rc_force" ]; then 1698165565Syar return 1 1699165565Syar fi 1700165565Syar fi 1701165565Syar done 1702165565Syar ;; 1703165565Syar esac 1704165565Syar 1705165565Syar return 0 1706165565Syar} 1707165565Syar 1708222996Shrs# check_kern_features mib 1709222996Shrs# Return existence of kern.features.* sysctl MIB as true or 1710222996Shrs# false. The result will be cached in $_rc_cache_kern_features_ 1711222996Shrs# namespace. "0" means the kern.features.X exists. 1712222996Shrs 1713222996Shrscheck_kern_features() 1714222996Shrs{ 1715222996Shrs local _v 1716222996Shrs 1717222996Shrs [ -n "$1" ] || return 1; 1718223292Sjilles eval _v=\$_rc_cache_kern_features_$1 1719222996Shrs [ -n "$_v" ] && return "$_v"; 1720222996Shrs 1721222996Shrs if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 1722222996Shrs eval _rc_cache_kern_features_$1=0 1723222996Shrs return 0 1724222996Shrs else 1725222996Shrs eval _rc_cache_kern_features_$1=1 1726222996Shrs return 1 1727222996Shrs fi 1728222996Shrs} 1729222996Shrs 1730243184Shrs# check_namevarlist var 1731243184Shrs# Return "0" if ${name}_var is reserved in rc.subr. 1732243184Shrs 1733243184Shrs_rc_namevarlist="program chroot chdir flags fib nice user group groups" 1734243184Shrscheck_namevarlist() 1735243184Shrs{ 1736243184Shrs local _v 1737243184Shrs 1738243184Shrs for _v in $_rc_namevarlist; do 1739243184Shrs case $1 in 1740243184Shrs $_v) return 0 ;; 1741243184Shrs esac 1742243184Shrs done 1743243184Shrs 1744243184Shrs return 1 1745243184Shrs} 1746243184Shrs 1747197144Shrs# _echoonce var msg mode 1748197144Shrs# mode=0: Echo $msg if ${$var} is empty. 1749197144Shrs# After doing echo, a string is set to ${$var}. 1750197144Shrs# 1751197144Shrs# mode=1: Echo $msg if ${$var} is a string with non-zero length. 1752197144Shrs# 1753197144Shrs_echoonce() 1754197144Shrs{ 1755197144Shrs local _var _msg _mode 1756223227Sjilles eval _var=\$$1 1757197144Shrs _msg=$2 1758197144Shrs _mode=$3 1759197144Shrs 1760197144Shrs case $_mode in 1761197144Shrs 1) [ -n "$_var" ] && echo "$_msg" ;; 1762197144Shrs *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 1763197144Shrs esac 1764197144Shrs} 1765197144Shrs 1766223298Sjillesfi # [ -z "${_rc_subr_loaded}" ] 1767223298Sjilles 1768157841Sflz_rc_subr_loaded=: 1769