rc.subr revision 272974
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 272974 2014-10-11 23:49:27Z 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 57264243Sdteske# list_vars pattern 58264243Sdteske# List vars matching pattern. 59264243Sdteske# 60264243Sdteskelist_vars() 61264243Sdteske{ 62264243Sdteske set | { while read LINE; do 63264243Sdteske var="${LINE%%=*}" 64264243Sdteske case "$var" in 65264243Sdteske "$LINE"|*[!a-zA-Z0-9_]*) continue ;; 66264243Sdteske $1) echo $var 67264243Sdteske esac 68264243Sdteske done; } 69264243Sdteske} 70264243Sdteske 71272393Shrs# set_rcvar [var] [defval] [desc] 72272393Shrs# 73272393Shrs# Echo or define a rc.conf(5) variable name. Global variable 74272393Shrs# $rcvars is used. 75272393Shrs# 76272393Shrs# If no argument is specified, echo "${name}_enable". 77272393Shrs# 78272393Shrs# If only a var is specified, echo "${var}_enable". 79272393Shrs# 80272393Shrs# If var and defval are specified, the ${var} is defined as 81272393Shrs# rc.conf(5) variable and the default value is ${defvar}. An 82272393Shrs# optional argument $desc can also be specified to add a 83272393Shrs# description for that. 84272393Shrs# 85272393Shrsset_rcvar() 86272393Shrs{ 87272393Shrs local _var 88272393Shrs 89272393Shrs case $# in 90272393Shrs 0) echo ${name}_enable ;; 91272393Shrs 1) echo ${1}_enable ;; 92272393Shrs *) 93272393Shrs debug "set_rcvar: \$$1=$2 is added" \ 94272393Shrs " as a rc.conf(5) variable." 95272393Shrs _var=$1 96272393Shrs rcvars="${rcvars# } $_var" 97272393Shrs eval ${_var}_defval=\"$2\" 98272393Shrs shift 2 99272393Shrs eval ${_var}_desc=\"$*\" 100272393Shrs ;; 101272393Shrs esac 102272393Shrs} 103272393Shrs 104197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg] 105197144Shrs# Define obsolete variable. 106197144Shrs# Global variable $rcvars_obsolete is used. 10798186Sgordon# 108197144Shrsset_rcvar_obsolete() 109197144Shrs{ 110197144Shrs local _var 111197144Shrs _var=$1 112272393Shrs debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 113197144Shrs 114197144Shrs rcvars_obsolete="${rcvars_obsolete# } $1" 115197144Shrs eval ${1}_newvar=\"$2\" 116197144Shrs shift 2 117197144Shrs eval ${_var}_obsolete_msg=\"$*\" 118197144Shrs} 119197144Shrs 120197144Shrs# 121231667Sdougb# force_depend script [rcvar] 12298186Sgordon# Force a service to start. Intended for use by services 123231667Sdougb# to resolve dependency issues. 12498186Sgordon# $1 - filename of script, in /etc/rc.d, to run 125231667Sdougb# $2 - name of the script's rcvar (minus the _enable) 12698186Sgordon# 12798186Sgordonforce_depend() 12898186Sgordon{ 129231667Sdougb local _depend _dep_rcvar 130231667Sdougb 13198186Sgordon _depend="$1" 132231667Sdougb _dep_rcvar="${2:-$1}_enable" 13398186Sgordon 134231667Sdougb [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 135231667Sdougb checkyesno $_dep_rcvar && return 0 136231667Sdougb 137231667Sdougb /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 138231667Sdougb 13998186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 140146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 14198186Sgordon warn "Unable to force ${_depend}. It may already be running." 14298186Sgordon return 1 14398186Sgordon fi 14498186Sgordon} 14598186Sgordon 14698186Sgordon# 14778344Sobrien# checkyesno var 14878344Sobrien# Test $1 variable, and warn if not set to YES or NO. 14978344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 15078344Sobrien# 15178344Sobriencheckyesno() 15278344Sobrien{ 15378344Sobrien eval _value=\$${1} 15498186Sgordon debug "checkyesno: $1 is set to $_value." 15578344Sobrien case $_value in 15678344Sobrien 15778344Sobrien # "yes", "true", "on", or "1" 15878344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 15978344Sobrien return 0 16078344Sobrien ;; 16178344Sobrien 16278344Sobrien # "no", "false", "off", or "0" 16378344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 16478344Sobrien return 1 16578344Sobrien ;; 16678344Sobrien *) 167229822Sdougb warn "\$${1} is not set properly - see rc.conf(5)." 16878344Sobrien return 1 16978344Sobrien ;; 17078344Sobrien esac 17178344Sobrien} 17278344Sobrien 173157473Sflz# 17498186Sgordon# reverse_list list 17598186Sgordon# print the list in reverse order 17678344Sobrien# 17798186Sgordonreverse_list() 17898186Sgordon{ 17998186Sgordon _revlist= 180126286Smtm for _revfile; do 18198186Sgordon _revlist="$_revfile $_revlist" 18298186Sgordon done 18398186Sgordon echo $_revlist 18498186Sgordon} 18598186Sgordon 186169668Smtm# stop_boot always 187169668Smtm# If booting directly to multiuser or $always is enabled, 188169668Smtm# send SIGTERM to the parent (/etc/rc) to abort the boot. 189169668Smtm# Otherwise just exit. 19078344Sobrien# 191169668Smtmstop_boot() 192169668Smtm{ 193169668Smtm local always 194169668Smtm 195178776Smaxim case $1 in 196178776Smaxim # "yes", "true", "on", or "1" 197178770Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 198169668Smtm always=true 199178770Smtm ;; 200178770Smtm *) 201169668Smtm always=false 202178770Smtm ;; 203178775Smaxim esac 204169668Smtm if [ "$autoboot" = yes -o "$always" = true ]; then 205169668Smtm echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 206169668Smtm kill -TERM ${RC_PID} 207169668Smtm fi 208169668Smtm exit 1 209169668Smtm} 210169668Smtm 211169668Smtm# 21298186Sgordon# mount_critical_filesystems type 21398186Sgordon# Go through the list of critical filesystems as provided in 21498186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 21598186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 21698186Sgordon# 21778344Sobrienmount_critical_filesystems() 21878344Sobrien{ 21998186Sgordon eval _fslist=\$critical_filesystems_${1} 22078344Sobrien for _fs in $_fslist; do 22178344Sobrien mount | ( 222126285Smtm _ismounted=false 22378344Sobrien while read what _on on _type type; do 22478344Sobrien if [ $on = $_fs ]; then 225126285Smtm _ismounted=true 22678344Sobrien fi 22778344Sobrien done 228126285Smtm if $_ismounted; then 229126285Smtm : 230126285Smtm else 23178344Sobrien mount $_fs >/dev/null 2>&1 23278344Sobrien fi 23398186Sgordon ) 23478344Sobrien done 23578344Sobrien} 23678344Sobrien 23778344Sobrien# 23898186Sgordon# check_pidfile pidfile procname [interpreter] 23998186Sgordon# Parses the first line of pidfile for a PID, and ensures 24078344Sobrien# that the process is running and matches procname. 24198186Sgordon# Prints the matching PID upon success, nothing otherwise. 24298186Sgordon# interpreter is optional; see _find_processes() for details. 24378344Sobrien# 24478344Sobriencheck_pidfile() 24578344Sobrien{ 24678344Sobrien _pidfile=$1 24778344Sobrien _procname=$2 24898186Sgordon _interpreter=$3 24978344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 25098186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 25178344Sobrien fi 25278344Sobrien if [ ! -f $_pidfile ]; then 253131061Smtm debug "pid file ($_pidfile): not readable." 25478344Sobrien return 25578344Sobrien fi 25678344Sobrien read _pid _junk < $_pidfile 25778344Sobrien if [ -z "$_pid" ]; then 258139949Skeramida debug "pid file ($_pidfile): no pid in file." 25978344Sobrien return 26078344Sobrien fi 26198186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 26278344Sobrien} 26378344Sobrien 26478344Sobrien# 26598186Sgordon# check_process procname [interpreter] 26678344Sobrien# Ensures that a process (or processes) named procname is running. 26798186Sgordon# Prints a list of matching PIDs. 26898186Sgordon# interpreter is optional; see _find_processes() for details. 26978344Sobrien# 27078344Sobriencheck_process() 27178344Sobrien{ 27278344Sobrien _procname=$1 27398186Sgordon _interpreter=$2 27478344Sobrien if [ -z "$_procname" ]; then 27598186Sgordon err 3 'USAGE: check_process procname [interpreter]' 27678344Sobrien fi 27798186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 27898186Sgordon} 27998186Sgordon 28098186Sgordon# 28198186Sgordon# _find_processes procname interpreter psargs 28298186Sgordon# Search for procname in the output of ps generated by psargs. 28398186Sgordon# Prints the PIDs of any matching processes, space separated. 28498186Sgordon# 28598186Sgordon# If interpreter == ".", check the following variations of procname 28698186Sgordon# against the first word of each command: 28798186Sgordon# procname 28898186Sgordon# `basename procname` 28998186Sgordon# `basename procname` + ":" 29098186Sgordon# "(" + `basename procname` + ")" 291155719Sceri# "[" + `basename procname` + "]" 29298186Sgordon# 29398186Sgordon# If interpreter != ".", read the first line of procname, remove the 29498186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 29598186Sgordon# match that against each command, either as is, or with extra words 296157841Sflz# at the end. As an alternative, to deal with interpreted daemons 297157841Sflz# using perl, the basename of the interpreter plus a colon is also 298157841Sflz# tried as the prefix to procname. 29998186Sgordon# 30098186Sgordon_find_processes() 30198186Sgordon{ 30298186Sgordon if [ $# -ne 3 ]; then 30398186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 30498186Sgordon fi 30598186Sgordon _procname=$1 30698186Sgordon _interpreter=$2 30798186Sgordon _psargs=$3 30898186Sgordon 30978344Sobrien _pref= 31098186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 311242183Screes _script="${_chroot}${_chroot:+/}$_procname" 312242183Screes if [ -r "$_script" ]; then 313170282Syar read _interp < $_script # read interpreter name 314170282Syar case "$_interp" in 315170282Syar \#!*) 316170282Syar _interp=${_interp#\#!} # strip #! 317170282Syar set -- $_interp 318170282Syar case $1 in 319170282Syar */bin/env) 320170282Syar shift # drop env to get real name 321170282Syar ;; 322170282Syar esac 323170282Syar if [ $_interpreter != $1 ]; then 324170282Syar warn "\$command_interpreter $_interpreter != $1" 325170282Syar fi 326170282Syar ;; 327170282Syar *) 328170282Syar warn "no shebang line in $_script" 329170282Syar set -- $_interpreter 330170282Syar ;; 331170282Syar esac 332170282Syar else 333170282Syar warn "cannot read shebang line from $_script" 334170282Syar set -- $_interpreter 33578344Sobrien fi 33698186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 337157841Sflz _interpbn=${1##*/} 33898186Sgordon _fp_args='_argv' 33998186Sgordon _fp_match='case "$_argv" in 340245250Ssmh ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' 34198186Sgordon else # a normal daemon 34298186Sgordon _procnamebn=${_procname##*/} 34398186Sgordon _fp_args='_arg0 _argv' 34498186Sgordon _fp_match='case "$_arg0" in 345151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 34698186Sgordon fi 34798186Sgordon 348161435Syar _proccheck="\ 349161436Syar $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 350157657Sflz while read _npid _jid '"$_fp_args"'; do 351161436Syar '"$_fp_match"' 352157657Sflz if [ "$JID" -eq "$_jid" ]; 353157657Sflz then echo -n "$_pref$_npid"; 354157657Sflz _pref=" "; 355157657Sflz fi 35698186Sgordon ;; 35798186Sgordon esac 35898186Sgordon done' 35998186Sgordon 360114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 36198186Sgordon eval $_proccheck 36298186Sgordon} 36398186Sgordon 364264243Sdteske# sort_lite [-b] [-n] [-k POS] [-t SEP] 365264243Sdteske# A lite version of sort(1) (supporting a few options) that can be used 366264243Sdteske# before the real sort(1) is available (e.g., in scripts that run prior 367264243Sdteske# to mountcritremote). Requires only shell built-in functionality. 36898186Sgordon# 369264243Sdteskesort_lite() 370264243Sdteske{ 371264243Sdteske local funcname=sort_lite 372264243Sdteske local sort_sep="$IFS" sort_ignore_leading_space= 373264243Sdteske local sort_field=0 sort_strict_fields= sort_numeric= 374264243Sdteske local nitems=0 skip_leading=0 trim= 375264243Sdteske 376264243Sdteske local OPTIND flag 377264243Sdteske while getopts bnk:t: flag; do 378264243Sdteske case "$flag" in 379264243Sdteske b) sort_ignore_leading_space=1 ;; 380264243Sdteske n) sort_numeric=1 sort_ignore_leading_space=1 ;; 381264243Sdteske k) sort_field="${OPTARG%%,*}" ;; # only up to first comma 382264243Sdteske # NB: Unlike sort(1) only one POS allowed 383264243Sdteske t) sort_sep="$OPTARG" 384264243Sdteske if [ ${#sort_sep} -gt 1 ]; then 385264243Sdteske echo "$funcname: multi-character tab \`$sort_sep'" >&2 386264243Sdteske return 1 387264243Sdteske fi 388264243Sdteske sort_strict_fields=1 389264243Sdteske ;; 390264243Sdteske \?) return 1 ;; 391264243Sdteske esac 392264243Sdteske done 393264243Sdteske shift $(( $OPTIND - 1 )) 394264243Sdteske 395264243Sdteske # Create transformation pattern to trim leading text if desired 396264243Sdteske case "$sort_field" in 397264243Sdteske ""|[!0-9]*|*[!0-9.]*) 398264243Sdteske echo "$funcname: invalid sort field \`$sort_field'" >&2 399264243Sdteske return 1 400264243Sdteske ;; 401264243Sdteske *.*) 402264243Sdteske skip_leading=${sort_field#*.} sort_field=${sort_field%%.*} 403264243Sdteske while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do 404264243Sdteske trim="$trim?" skip_leading=$(( $skip_leading - 1 )) 405264243Sdteske done 406264243Sdteske esac 407264243Sdteske 408264243Sdteske # Copy input to series of local numbered variables 409264243Sdteske # NB: IFS of NULL preserves leading whitespace 410264243Sdteske local LINE 411264243Sdteske while IFS= read -r LINE || [ "$LINE" ]; do 412264243Sdteske nitems=$(( $nitems + 1 )) 413264243Sdteske local src_$nitems="$LINE" 414264243Sdteske done 415264243Sdteske 416264243Sdteske # 417264243Sdteske # Sort numbered locals using insertion sort 418264243Sdteske # 419264243Sdteske local curitem curitem_orig curitem_mod curitem_haskey 420264243Sdteske local dest dest_orig dest_mod dest_haskey 421264243Sdteske local d gt n 422264243Sdteske local i=1 423264243Sdteske while [ $i -le $nitems ]; do 424264243Sdteske curitem_haskey=1 # Assume sort field (-k POS) exists 425264243Sdteske eval curitem=\"\$src_$i\" 426264243Sdteske curitem_mod="$curitem" # for modified comparison 427264243Sdteske curitem_orig="$curitem" # for original comparison 428264243Sdteske 429264243Sdteske # Trim leading whitespace if desired 430264243Sdteske if [ "$sort_ignore_leading_space" ]; then 431264243Sdteske while case "$curitem_orig" in 432264243Sdteske [$IFS]*) : ;; *) false; esac 433264243Sdteske do 434264243Sdteske curitem_orig="${curitem_orig#?}" 435264243Sdteske done 436264243Sdteske curitem_mod="$curitem_orig" 437264243Sdteske fi 438264243Sdteske 439264243Sdteske # Shift modified comparison value if sort field (-k POS) is > 1 440264243Sdteske n=$sort_field 441264243Sdteske while [ $n -gt 1 ]; do 442264243Sdteske case "$curitem_mod" in 443264243Sdteske *[$sort_sep]*) 444264243Sdteske # Cut text up-to (and incl.) first separator 445264243Sdteske curitem_mod="${curitem_mod#*[$sort_sep]}" 446264243Sdteske 447264243Sdteske # Skip NULLs unless strict field splitting 448264243Sdteske [ "$sort_strict_fields" ] || 449264243Sdteske [ "${curitem_mod%%[$sort_sep]*}" ] || 450264243Sdteske [ $n -eq 2 ] || 451264243Sdteske continue 452264243Sdteske ;; 453264243Sdteske *) 454264243Sdteske # Asked for a field that doesn't exist 455264243Sdteske curitem_haskey= break 456264243Sdteske esac 457264243Sdteske n=$(( $n - 1 )) 458264243Sdteske done 459264243Sdteske 460264243Sdteske # Trim trailing words if sort field >= 1 461264243Sdteske [ $sort_field -ge 1 -a "$sort_numeric" ] && 462264243Sdteske curitem_mod="${curitem_mod%%[$sort_sep]*}" 463264243Sdteske 464264243Sdteske # Apply optional trim (-k POS.TRIM) to cut leading characters 465264243Sdteske curitem_mod="${curitem_mod#$trim}" 466264243Sdteske 467264243Sdteske # Determine the type of modified comparison to use initially 468264243Sdteske # NB: Prefer numerical if requested but fallback to standard 469264243Sdteske case "$curitem_mod" in 470264243Sdteske ""|[!0-9]*) # NULL or begins with non-number 471264243Sdteske gt=">" 472264243Sdteske [ "$sort_numeric" ] && curitem_mod=0 473264243Sdteske ;; 474264243Sdteske *) 475264243Sdteske if [ "$sort_numeric" ]; then 476264243Sdteske gt="-gt" 477264243Sdteske curitem_mod="${curitem_mod%%[!0-9]*}" 478264243Sdteske # NB: trailing non-digits removed 479264243Sdteske # otherwise numeric comparison fails 480264243Sdteske else 481264243Sdteske gt=">" 482264243Sdteske fi 483264243Sdteske esac 484264243Sdteske 485264243Sdteske # If first time through, short-circuit below position-search 486264243Sdteske if [ $i -le 1 ]; then 487264243Sdteske d=0 488264243Sdteske else 489264243Sdteske d=1 490264243Sdteske fi 491264243Sdteske 492264243Sdteske # 493264243Sdteske # Find appropriate element position 494264243Sdteske # 495264243Sdteske while [ $d -gt 0 ] 496264243Sdteske do 497264243Sdteske dest_haskey=$curitem_haskey 498264243Sdteske eval dest=\"\$dest_$d\" 499264243Sdteske dest_mod="$dest" # for modified comparison 500264243Sdteske dest_orig="$dest" # for original comparison 501264243Sdteske 502264243Sdteske # Trim leading whitespace if desired 503264243Sdteske if [ "$sort_ignore_leading_space" ]; then 504264243Sdteske while case "$dest_orig" in 505264243Sdteske [$IFS]*) : ;; *) false; esac 506264243Sdteske do 507264243Sdteske dest_orig="${dest_orig#?}" 508264243Sdteske done 509264243Sdteske dest_mod="$dest_orig" 510264243Sdteske fi 511264243Sdteske 512264243Sdteske # Shift modified value if sort field (-k POS) is > 1 513264243Sdteske n=$sort_field 514264243Sdteske while [ $n -gt 1 ]; do 515264243Sdteske case "$dest_mod" in 516264243Sdteske *[$sort_sep]*) 517264243Sdteske # Cut text up-to (and incl.) 1st sep 518264243Sdteske dest_mod="${dest_mod#*[$sort_sep]}" 519264243Sdteske 520264243Sdteske # Skip NULLs unless strict fields 521264243Sdteske [ "$sort_strict_fields" ] || 522264243Sdteske [ "${dest_mod%%[$sort_sep]*}" ] || 523264243Sdteske [ $n -eq 2 ] || 524264243Sdteske continue 525264243Sdteske ;; 526264243Sdteske *) 527264243Sdteske # Asked for a field that doesn't exist 528264243Sdteske dest_haskey= break 529264243Sdteske esac 530264243Sdteske n=$(( $n - 1 )) 531264243Sdteske done 532264243Sdteske 533264243Sdteske # Trim trailing words if sort field >= 1 534264243Sdteske [ $sort_field -ge 1 -a "$sort_numeric" ] && 535264243Sdteske dest_mod="${dest_mod%%[$sort_sep]*}" 536264243Sdteske 537264243Sdteske # Apply optional trim (-k POS.TRIM), cut leading chars 538264243Sdteske dest_mod="${dest_mod#$trim}" 539264243Sdteske 540264243Sdteske # Determine type of modified comparison to use 541264243Sdteske # NB: Prefer numerical if requested, fallback to std 542264243Sdteske case "$dest_mod" in 543264243Sdteske ""|[!0-9]*) # NULL or begins with non-number 544264243Sdteske gt=">" 545264243Sdteske [ "$sort_numeric" ] && dest_mod=0 546264243Sdteske ;; 547264243Sdteske *) 548264243Sdteske if [ "$sort_numeric" ]; then 549264243Sdteske gt="-gt" 550264243Sdteske dest_mod="${dest_mod%%[!0-9]*}" 551264243Sdteske # NB: kill trailing non-digits 552264243Sdteske # for numeric comparison safety 553264243Sdteske else 554264243Sdteske gt=">" 555264243Sdteske fi 556264243Sdteske esac 557264243Sdteske 558264243Sdteske # Break if we've found the proper element position 559264243Sdteske if [ "$curitem_haskey" -a "$dest_haskey" ]; then 560264243Sdteske if [ "$dest_mod" = "$curitem_mod" ]; then 561264243Sdteske [ "$dest_orig" ">" "$curitem_orig" ] && 562264243Sdteske break 563264243Sdteske elif [ "$dest_mod" $gt "$curitem_mod" ] \ 564264243Sdteske 2> /dev/null 565264243Sdteske then 566264243Sdteske break 567264243Sdteske fi 568264243Sdteske else 569264243Sdteske [ "$dest_orig" ">" "$curitem_orig" ] && break 570264243Sdteske fi 571264243Sdteske 572264243Sdteske # Break if we've hit the end 573264243Sdteske [ $d -ge $i ] && break 574264243Sdteske 575264243Sdteske d=$(( $d + 1 )) 576264243Sdteske done 577264243Sdteske 578264243Sdteske # Shift remaining positions forward, making room for new item 579264243Sdteske n=$i 580264243Sdteske while [ $n -ge $d ]; do 581264243Sdteske # Shift destination item forward one placement 582264243Sdteske eval dest_$(( $n + 1 ))=\"\$dest_$n\" 583264243Sdteske n=$(( $n - 1 )) 584264243Sdteske done 585264243Sdteske 586264243Sdteske # Place the element 587264243Sdteske if [ $i -eq 1 ]; then 588264243Sdteske local dest_1="$curitem" 589264243Sdteske else 590264243Sdteske local dest_$d="$curitem" 591264243Sdteske fi 592264243Sdteske 593264243Sdteske i=$(( $i + 1 )) 594264243Sdteske done 595264243Sdteske 596264243Sdteske # Print sorted results 597264243Sdteske d=1 598264243Sdteske while [ $d -le $nitems ]; do 599264243Sdteske eval echo \"\$dest_$d\" 600264243Sdteske d=$(( $d + 1 )) 601264243Sdteske done 602264243Sdteske} 603264243Sdteske 604264243Sdteske# 60598186Sgordon# wait_for_pids pid [pid ...] 60698186Sgordon# spins until none of the pids exist 60798186Sgordon# 60898186Sgordonwait_for_pids() 60998186Sgordon{ 610206248Sdougb local _list _prefix _nlist _j 611206248Sdougb 612126286Smtm _list="$@" 61398186Sgordon if [ -z "$_list" ]; then 61498186Sgordon return 61598186Sgordon fi 61698186Sgordon _prefix= 61798186Sgordon while true; do 61898186Sgordon _nlist=""; 61998186Sgordon for _j in $_list; do 62098186Sgordon if kill -0 $_j 2>/dev/null; then 62198186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 622206248Sdougb [ -n "$_prefix" ] && sleep 1 62398186Sgordon fi 62498186Sgordon done 62598186Sgordon if [ -z "$_nlist" ]; then 62698186Sgordon break 62778344Sobrien fi 62898186Sgordon _list=$_nlist 62998186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 63098186Sgordon _prefix=", " 631206248Sdougb pwait $_list 2>/dev/null 63278344Sobrien done 63398186Sgordon if [ -n "$_prefix" ]; then 63498186Sgordon echo "." 63598186Sgordon fi 63678344Sobrien} 63778344Sobrien 63878344Sobrien# 639220962Sdougb# get_pidfile_from_conf string file 640220962Sdougb# 641220962Sdougb# Takes a string to search for in the specified file. 642220962Sdougb# Ignores lines with traditional comment characters. 643220962Sdougb# 644220962Sdougb# Example: 645220962Sdougb# 646220962Sdougb# if get_pidfile_from_conf string file; then 647220962Sdougb# pidfile="$_pidfile_from_conf" 648220962Sdougb# else 649220962Sdougb# pidfile='appropriate default' 650220962Sdougb# fi 651220962Sdougb# 652220962Sdougbget_pidfile_from_conf() 653220962Sdougb{ 654220963Sdougb if [ -z "$1" -o -z "$2" ]; then 655220963Sdougb err 3 "USAGE: get_pidfile_from_conf string file ($name)" 656220963Sdougb fi 657220963Sdougb 658220962Sdougb local string file line 659220962Sdougb 660220962Sdougb string="$1" ; file="$2" 661220962Sdougb 662220963Sdougb if [ ! -s "$file" ]; then 663220963Sdougb err 3 "get_pidfile_from_conf: $file does not exist ($name)" 664220962Sdougb fi 665220962Sdougb 666220962Sdougb while read line; do 667220962Sdougb case "$line" in 668220962Sdougb *[#\;]*${string}*) continue ;; 669220962Sdougb *${string}*) break ;; 670220962Sdougb esac 671220962Sdougb done < $file 672220962Sdougb 673220962Sdougb if [ -n "$line" ]; then 674220962Sdougb line=${line#*/} 675220962Sdougb _pidfile_from_conf="/${line%%[\"\;]*}" 676220962Sdougb else 677220962Sdougb return 1 678220962Sdougb fi 679220962Sdougb} 680220962Sdougb 681220962Sdougb# 682197947Sdougb# check_startmsgs 683197947Sdougb# If rc_quiet is set (usually as a result of using faststart at 684197947Sdougb# boot time) check if rc_startmsgs is enabled. 685197947Sdougb# 686197947Sdougbcheck_startmsgs() 687197947Sdougb{ 688197947Sdougb if [ -n "$rc_quiet" ]; then 689197947Sdougb checkyesno rc_startmsgs 690197947Sdougb else 691197947Sdougb return 0 692197947Sdougb fi 693197947Sdougb} 694197947Sdougb 695197947Sdougb# 69698186Sgordon# run_rc_command argument 69798186Sgordon# Search for argument in the list of supported commands, which is: 69898186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 69998186Sgordon# If there's a match, run ${argument}_cmd or the default method 70098186Sgordon# (see below). 70178344Sobrien# 70298186Sgordon# If argument has a given prefix, then change the operation as follows: 70398186Sgordon# Prefix Operation 70478344Sobrien# ------ --------- 705175676Smtm# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 70698186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 707126303Smtm# one Set ${rcvar} to YES 708175676Smtm# quiet Don't output some diagnostics, and set rc_quiet=yes 70978344Sobrien# 71078344Sobrien# The following globals are used: 71178344Sobrien# 71298186Sgordon# Name Needed Purpose 71398186Sgordon# ---- ------ ------- 71478344Sobrien# name y Name of script. 71578344Sobrien# 71678344Sobrien# command n Full path to command. 71798186Sgordon# Not needed if ${rc_arg}_cmd is set for 71878344Sobrien# each keyword. 71978344Sobrien# 72078344Sobrien# command_args n Optional args/shell directives for command. 72178344Sobrien# 72298186Sgordon# command_interpreter n If not empty, command is interpreted, so 72398186Sgordon# call check_{pidfile,process}() appropriately. 72498186Sgordon# 725197144Shrs# desc n Description of script. 726197144Shrs# 72778344Sobrien# extra_commands n List of extra commands supported. 72878344Sobrien# 72998186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 73098186Sgordon# otherwise use check_process $command. 73198186Sgordon# In either case, only check if $command is set. 73278344Sobrien# 73398186Sgordon# procname n Process name to check for instead of $command. 73498186Sgordon# 73578344Sobrien# rcvar n This is checked with checkyesno to determine 73678344Sobrien# if the action should be run. 73778344Sobrien# 738157653Sflz# ${name}_program n Full path to command. 739157653Sflz# Meant to be used in /etc/rc.conf to override 740157653Sflz# ${command}. 741157653Sflz# 74278344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 74398186Sgordon# Requires /usr to be mounted. 74478344Sobrien# 74578344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 74678344Sobrien# (if not using ${name}_chroot). 74778344Sobrien# 74878344Sobrien# ${name}_flags n Arguments to call ${command} with. 74978344Sobrien# NOTE: $flags from the parent environment 75078344Sobrien# can be used to override this. 75178344Sobrien# 752272974Shrs# ${name}_env n Environment variables to run ${command} with. 753272974Shrs# 754242184Shrs# ${name}_fib n Routing table number to run ${command} with. 755242184Shrs# 75678344Sobrien# ${name}_nice n Nice level to run ${command} at. 75778344Sobrien# 75878344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 75978344Sobrien# using ${name}_chroot. 76098186Sgordon# Requires /usr to be mounted. 76178344Sobrien# 76278344Sobrien# ${name}_group n Group to run chrooted ${command} as. 76398186Sgordon# Requires /usr to be mounted. 76478344Sobrien# 76598186Sgordon# ${name}_groups n Comma separated list of supplementary groups 76698186Sgordon# to run the chrooted ${command} with. 76798186Sgordon# Requires /usr to be mounted. 76878344Sobrien# 769272974Shrs# ${name}_prepend n Command added before ${command}. 770272974Shrs# 77198186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 77278344Sobrien# Otherwise, use default command (see below) 77378344Sobrien# 77498186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 77598186Sgordon# ${rc_arg}_cmd method in the default 77698186Sgordon# operation (i.e, after checking for required 77798186Sgordon# bits and process (non)existence). 77878344Sobrien# If this completes with a non-zero exit code, 77998186Sgordon# don't run ${rc_arg}_cmd. 78078344Sobrien# 78198186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 78298186Sgordon# ${rc_arg}_cmd method, if that method 78398186Sgordon# returned a zero exit code. 78498186Sgordon# 78578344Sobrien# required_dirs n If set, check for the existence of the given 786165565Syar# directories before running a (re)start command. 78778344Sobrien# 78878344Sobrien# required_files n If set, check for the readability of the given 789165565Syar# files before running a (re)start command. 79078344Sobrien# 791165565Syar# required_modules n If set, ensure the given kernel modules are 792165565Syar# loaded before running a (re)start command. 793165565Syar# The check and possible loads are actually 794165565Syar# done after start_precmd so that the modules 795165565Syar# aren't loaded in vain, should the precmd 796165565Syar# return a non-zero status to indicate a error. 797165565Syar# If a word in the list looks like "foo:bar", 798165565Syar# "foo" is the KLD file name and "bar" is the 799165565Syar# module name. If a word looks like "foo~bar", 800165565Syar# "foo" is the KLD file name and "bar" is a 801165565Syar# egrep(1) pattern matching the module name. 802165565Syar# Otherwise the module name is assumed to be 803165565Syar# the same as the KLD file name, which is most 804165565Syar# common. See load_kld(). 805165565Syar# 80678344Sobrien# required_vars n If set, perform checkyesno on each of the 80778344Sobrien# listed variables before running the default 80878344Sobrien# (re)start command. 80978344Sobrien# 81098186Sgordon# Default behaviour for a given argument, if no override method is 81198186Sgordon# provided: 81278344Sobrien# 81398186Sgordon# Argument Default behaviour 81498186Sgordon# -------- ----------------- 81578344Sobrien# start if !running && checkyesno ${rcvar} 81678344Sobrien# ${command} 81778344Sobrien# 81878344Sobrien# stop if ${pidfile} 81998186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 82078344Sobrien# else 82198186Sgordon# rc_pid=$(check_process $command) 82298186Sgordon# kill $sig_stop $rc_pid 82398186Sgordon# wait_for_pids $rc_pid 82498186Sgordon# ($sig_stop defaults to TERM.) 82578344Sobrien# 82698186Sgordon# reload Similar to stop, except use $sig_reload instead, 82798186Sgordon# and doesn't wait_for_pids. 82878344Sobrien# $sig_reload defaults to HUP. 829151685Syar# Note that `reload' isn't provided by default, 830151685Syar# it should be enabled via $extra_commands. 83178344Sobrien# 83278344Sobrien# restart Run `stop' then `start'. 83378344Sobrien# 83498186Sgordon# status Show if ${command} is running, etc. 83578344Sobrien# 83698186Sgordon# poll Wait for ${command} to exit. 83798186Sgordon# 83898186Sgordon# rcvar Display what rc.conf variable is used (if any). 83998186Sgordon# 840255809Sdes# enabled Return true if the service is enabled. 841255809Sdes# 84298186Sgordon# Variables available to methods, and after run_rc_command() has 84398186Sgordon# completed: 84498186Sgordon# 84598186Sgordon# Variable Purpose 84698186Sgordon# -------- ------- 847126303Smtm# rc_arg Argument to command, after fast/force/one processing 84898186Sgordon# performed 84998186Sgordon# 85098186Sgordon# rc_flags Flags to start the default command with. 85198186Sgordon# Defaults to ${name}_flags, unless overridden 85298186Sgordon# by $flags from the environment. 85398186Sgordon# This variable may be changed by the precmd method. 85498186Sgordon# 85598186Sgordon# rc_pid PID of command (if appropriate) 85698186Sgordon# 85798186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 85898186Sgordon# 85998186Sgordon# rc_force Not empty if "force" was provided (q.v.) 86098186Sgordon# 861175676Smtm# rc_quiet Not empty if "quiet" was provided 86298186Sgordon# 863175676Smtm# 86478344Sobrienrun_rc_command() 86578344Sobrien{ 866116097Smtm _return=0 86798186Sgordon rc_arg=$1 86878344Sobrien if [ -z "$name" ]; then 86998186Sgordon err 3 'run_rc_command: $name is not set.' 87078344Sobrien fi 87178344Sobrien 872132892Smtm # Don't repeat the first argument when passing additional command- 873132892Smtm # line arguments to the command subroutines. 874132892Smtm # 875132892Smtm shift 1 876132892Smtm rc_extra_args="$*" 877132892Smtm 878126303Smtm _rc_prefix= 87998186Sgordon case "$rc_arg" in 88078344Sobrien fast*) # "fast" prefix; don't check pid 88198186Sgordon rc_arg=${rc_arg#fast} 88298186Sgordon rc_fast=yes 883175676Smtm rc_quiet=yes 88478344Sobrien ;; 885198216Sed force*) # "force" prefix; always run 88698186Sgordon rc_force=yes 887126303Smtm _rc_prefix=force 888126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 88978344Sobrien if [ -n "${rcvar}" ]; then 89078344Sobrien eval ${rcvar}=YES 89178344Sobrien fi 89278344Sobrien ;; 893126303Smtm one*) # "one" prefix; set ${rcvar}=yes 894126303Smtm _rc_prefix=one 895126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 896126303Smtm if [ -n "${rcvar}" ]; then 897126303Smtm eval ${rcvar}=YES 898126303Smtm fi 899126303Smtm ;; 900175676Smtm quiet*) # "quiet" prefix; omit some messages 901175676Smtm _rc_prefix=quiet 902175676Smtm rc_arg=${rc_arg#${_rc_prefix}} 903175676Smtm rc_quiet=yes 904175676Smtm ;; 90578344Sobrien esac 90678344Sobrien 907161530Sflz eval _override_command=\$${name}_program 908198162Sdougb command=${_override_command:-$command} 909161530Sflz 910255809Sdes _keywords="start stop restart rcvar enabled $extra_commands" 91198186Sgordon rc_pid= 91278344Sobrien _pidcmd= 91398186Sgordon _procname=${procname:-${command}} 91498186Sgordon 915131135Smtm # setup pid check command 916131135Smtm if [ -n "$_procname" ]; then 91778344Sobrien if [ -n "$pidfile" ]; then 91898186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 91998186Sgordon else 92098186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 92178344Sobrien fi 92278344Sobrien if [ -n "$_pidcmd" ]; then 92398186Sgordon _keywords="${_keywords} status poll" 92478344Sobrien fi 92578344Sobrien fi 92678344Sobrien 92798186Sgordon if [ -z "$rc_arg" ]; then 928150796Syar rc_usage $_keywords 92978344Sobrien fi 93078344Sobrien 931255809Sdes if [ "$rc_arg" = "enabled" ] ; then 932255809Sdes checkyesno ${rcvar} 933255809Sdes return $? 934255809Sdes fi 935255809Sdes 93678344Sobrien if [ -n "$flags" ]; then # allow override from environment 93798186Sgordon rc_flags=$flags 93878344Sobrien else 93998186Sgordon eval rc_flags=\$${name}_flags 94078344Sobrien fi 94198186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 94298186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 943242184Shrs _group=\$${name}_group _groups=\$${name}_groups \ 944272974Shrs _fib=\$${name}_fib _env=\$${name}_env \ 945272974Shrs _prepend=\$${name}_prepend 94678344Sobrien 94798186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 948124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 94998186Sgordon unset _user 95098186Sgordon fi 95198186Sgordon fi 95298186Sgordon 953230374Sdougb [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 954179870Smtm 955179870Smtm for _elem in $_keywords; do 956179870Smtm if [ "$_elem" != "$rc_arg" ]; then 957179870Smtm continue 958179870Smtm fi 959206686Sdougb # if ${rcvar} is set, $1 is not "rcvar" 960206686Sdougb # and ${rc_pid} is not set, then run 96178344Sobrien # checkyesno ${rcvar} 96278344Sobrien # and return if that failed 96378344Sobrien # 964220760Sdougb if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 965220760Sdougb [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 966179870Smtm if ! checkyesno ${rcvar}; then 967179870Smtm if [ -n "${rc_quiet}" ]; then 968179870Smtm return 0 969179870Smtm fi 970179870Smtm echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 971179870Smtm echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 972179870Smtm echo "instead of '${rc_arg}'." 973175676Smtm return 0 974175676Smtm fi 97578344Sobrien fi 97678344Sobrien 97778344Sobrien # if there's a custom ${XXX_cmd}, 97878344Sobrien # run that instead of the default 97978344Sobrien # 980165565Syar eval _cmd=\$${rc_arg}_cmd \ 981165565Syar _precmd=\$${rc_arg}_precmd \ 982165565Syar _postcmd=\$${rc_arg}_postcmd 983165565Syar 98478344Sobrien if [ -n "$_cmd" ]; then 985165565Syar _run_rc_precmd || return 1 986165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 987165565Syar _run_rc_postcmd 988116097Smtm return $_return 98978344Sobrien fi 99078344Sobrien 99198186Sgordon case "$rc_arg" in # default operations... 99278344Sobrien 99378344Sobrien status) 994165565Syar _run_rc_precmd || return 1 99598186Sgordon if [ -n "$rc_pid" ]; then 99698186Sgordon echo "${name} is running as pid $rc_pid." 99778344Sobrien else 99878344Sobrien echo "${name} is not running." 99978344Sobrien return 1 100078344Sobrien fi 1001165565Syar _run_rc_postcmd 100278344Sobrien ;; 100378344Sobrien 100478344Sobrien start) 1005131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 1006243324Shrs if [ -z "$rc_quiet" ]; then 1007243324Shrs echo 1>&2 "${name} already running? " \ 1008243324Shrs "(pid=$rc_pid)." 1009243324Shrs fi 1010153152Syar return 1 101178344Sobrien fi 101278344Sobrien 1013242183Screes if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 1014160667Syar warn "run_rc_command: cannot run $command" 1015153152Syar return 1 101678344Sobrien fi 101778344Sobrien 1018179946Smtm if ! _run_rc_precmd; then 1019179946Smtm warn "failed precmd routine for ${name}" 1020179946Smtm return 1 1021179946Smtm fi 102278344Sobrien 1023160668Syar # setup the full command to run 102478344Sobrien # 1025197947Sdougb check_startmsgs && echo "Starting ${name}." 102678344Sobrien if [ -n "$_chroot" ]; then 102778344Sobrien _doit="\ 102878344Sobrien${_nice:+nice -n $_nice }\ 1029242184Shrs${_fib:+setfib -F $_fib }\ 1030272974Shrs${_env:+env $_env }\ 103178344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 103298186Sgordon$_chroot $command $rc_flags $command_args" 103378344Sobrien else 103478344Sobrien _doit="\ 1035161396Syar${_chdir:+cd $_chdir && }\ 1036242184Shrs${_fib:+setfib -F $_fib }\ 1037272974Shrs${_env:+env $_env }\ 103898186Sgordon$command $rc_flags $command_args" 103998186Sgordon if [ -n "$_user" ]; then 104098186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 104198186Sgordon fi 1042161396Syar if [ -n "$_nice" ]; then 1043161396Syar if [ -z "$_user" ]; then 1044161396Syar _doit="sh -c \"$_doit\"" 1045201036Sdougb fi 1046161396Syar _doit="nice -n $_nice $_doit" 1047161396Syar fi 1048272974Shrs if [ -n "$_prepend" ]; then 1049272974Shrs _doit="$_prepend $_doit" 1050272974Shrs fi 105178344Sobrien fi 105298186Sgordon 1053165565Syar # run the full command 105498186Sgordon # 1055179946Smtm if ! _run_rc_doit "$_doit"; then 1056179946Smtm warn "failed to start ${name}" 1057179946Smtm return 1 1058179946Smtm fi 105998186Sgordon 106098186Sgordon # finally, run postcmd 106198186Sgordon # 1062165565Syar _run_rc_postcmd 106378344Sobrien ;; 106478344Sobrien 106578344Sobrien stop) 106698186Sgordon if [ -z "$rc_pid" ]; then 1067153152Syar [ -n "$rc_fast" ] && return 0 1068165565Syar _run_rc_notrunning 1069153152Syar return 1 107078344Sobrien fi 107178344Sobrien 1072165565Syar _run_rc_precmd || return 1 107398186Sgordon 107498186Sgordon # send the signal to stop 107598186Sgordon # 107678344Sobrien echo "Stopping ${name}." 1077165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1078165565Syar _run_rc_doit "$_doit" || return 1 107998186Sgordon 108098186Sgordon # wait for the command to exit, 108198186Sgordon # and run postcmd. 108298186Sgordon wait_for_pids $rc_pid 1083165565Syar 1084165565Syar _run_rc_postcmd 108578344Sobrien ;; 108678344Sobrien 108778344Sobrien reload) 108898186Sgordon if [ -z "$rc_pid" ]; then 1089165565Syar _run_rc_notrunning 1090153152Syar return 1 109178344Sobrien fi 1092165565Syar 1093165565Syar _run_rc_precmd || return 1 1094165565Syar 1095165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1096165565Syar _run_rc_doit "$_doit" || return 1 1097165565Syar 1098165565Syar _run_rc_postcmd 109978344Sobrien ;; 110078344Sobrien 110178344Sobrien restart) 110278344Sobrien # prevent restart being called more 110378344Sobrien # than once by any given script 110478344Sobrien # 1105126285Smtm if ${_rc_restart_done:-false}; then 110678344Sobrien return 0 110778344Sobrien fi 1108126285Smtm _rc_restart_done=true 110978344Sobrien 1110165565Syar _run_rc_precmd || return 1 1111165565Syar 1112165565Syar # run those in a subshell to keep global variables 1113152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1114165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1115165565Syar _return=$? 1116165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 111798186Sgordon 1118165565Syar _run_rc_postcmd 111978344Sobrien ;; 112078344Sobrien 112198186Sgordon poll) 1122165565Syar _run_rc_precmd || return 1 112398186Sgordon if [ -n "$rc_pid" ]; then 112498186Sgordon wait_for_pids $rc_pid 112598186Sgordon fi 1126165565Syar _run_rc_postcmd 112798186Sgordon ;; 112898186Sgordon 112978344Sobrien rcvar) 1130197144Shrs echo -n "# $name" 1131197144Shrs if [ -n "$desc" ]; then 1132197144Shrs echo " : $desc" 1133197144Shrs else 1134197144Shrs echo "" 1135197144Shrs fi 1136197144Shrs echo "#" 1137272393Shrs # Get unique vars in $rcvar $rcvars 1138272393Shrs for _v in $rcvar $rcvars; do 1139197144Shrs case $v in 1140197144Shrs $_v\ *|\ *$_v|*\ $_v\ *) ;; 1141197144Shrs *) v="${v# } $_v" ;; 1142197144Shrs esac 1143197144Shrs done 1144197144Shrs 1145197144Shrs # Display variables. 1146197144Shrs for _v in $v; do 1147197144Shrs if [ -z "$_v" ]; then 1148197144Shrs continue 114978344Sobrien fi 1150197144Shrs 1151197144Shrs eval _desc=\$${_v}_desc 1152197144Shrs eval _defval=\$${_v}_defval 1153197144Shrs _h="-" 1154197144Shrs 1155197144Shrs eval echo \"$_v=\\\"\$$_v\\\"\" 1156197144Shrs # decode multiple lines of _desc 1157197144Shrs while [ -n "$_desc" ]; do 1158197144Shrs case $_desc in 1159197144Shrs *^^*) 1160197144Shrs echo "# $_h ${_desc%%^^*}" 1161197144Shrs _desc=${_desc#*^^} 1162197144Shrs _h=" " 1163197144Shrs ;; 1164197144Shrs *) 1165197144Shrs echo "# $_h ${_desc}" 1166197144Shrs break 1167197144Shrs ;; 1168197144Shrs esac 1169197144Shrs done 1170197144Shrs echo "# (default: \"$_defval\")" 1171197144Shrs done 1172197144Shrs echo "" 117378344Sobrien ;; 117478344Sobrien 117578344Sobrien *) 1176150796Syar rc_usage $_keywords 117778344Sobrien ;; 117878344Sobrien 117978344Sobrien esac 1180116097Smtm return $_return 118178344Sobrien done 118278344Sobrien 118398186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 1184150796Syar rc_usage $_keywords 1185153152Syar # not reached 118678344Sobrien} 118778344Sobrien 118878344Sobrien# 1189165565Syar# Helper functions for run_rc_command: common code. 1190165565Syar# They use such global variables besides the exported rc_* ones: 1191165565Syar# 1192165565Syar# name R/W 1193165565Syar# ------------------ 1194165565Syar# _precmd R 1195165565Syar# _postcmd R 1196165565Syar# _return W 1197165565Syar# 1198165565Syar_run_rc_precmd() 1199165565Syar{ 1200165565Syar check_required_before "$rc_arg" || return 1 1201165565Syar 1202165565Syar if [ -n "$_precmd" ]; then 1203165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1204165565Syar eval "$_precmd $rc_extra_args" 1205165565Syar _return=$? 1206165565Syar 1207165565Syar # If precmd failed and force isn't set, request exit. 1208165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1209165565Syar return 1 1210165565Syar fi 1211165565Syar fi 1212165565Syar 1213165565Syar check_required_after "$rc_arg" || return 1 1214165565Syar 1215165565Syar return 0 1216165565Syar} 1217165565Syar 1218165565Syar_run_rc_postcmd() 1219165565Syar{ 1220165565Syar if [ -n "$_postcmd" ]; then 1221165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1222165565Syar eval "$_postcmd $rc_extra_args" 1223165565Syar _return=$? 1224165565Syar fi 1225165565Syar return 0 1226165565Syar} 1227165565Syar 1228165565Syar_run_rc_doit() 1229165565Syar{ 1230165565Syar debug "run_rc_command: doit: $*" 1231165565Syar eval "$@" 1232165565Syar _return=$? 1233165565Syar 1234165565Syar # If command failed and force isn't set, request exit. 1235165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1236165565Syar return 1 1237165565Syar fi 1238165565Syar 1239165565Syar return 0 1240165565Syar} 1241165565Syar 1242165565Syar_run_rc_notrunning() 1243165565Syar{ 1244165565Syar local _pidmsg 1245165565Syar 1246165565Syar if [ -n "$pidfile" ]; then 1247165565Syar _pidmsg=" (check $pidfile)." 1248165565Syar else 1249165565Syar _pidmsg= 1250165565Syar fi 1251165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 1252165565Syar} 1253165565Syar 1254165565Syar_run_rc_killcmd() 1255165565Syar{ 1256165565Syar local _cmd 1257165565Syar 1258165565Syar _cmd="kill -$1 $rc_pid" 1259165565Syar if [ -n "$_user" ]; then 1260165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1261165565Syar fi 1262165565Syar echo "$_cmd" 1263165565Syar} 1264165565Syar 1265165565Syar# 126678344Sobrien# run_rc_script file arg 126778344Sobrien# Start the script `file' with `arg', and correctly handle the 1268201038Sdougb# return value from the script. 1269201038Sdougb# If `file' ends with `.sh', it's sourced into the current environment 1270201038Sdougb# when $rc_fast_and_loose is set, otherwise it is run as a child process. 1271201038Sdougb# If `file' appears to be a backup or scratch file, ignore it. 1272201038Sdougb# Otherwise if it is executable run as a child process. 127378344Sobrien# 127478344Sobrienrun_rc_script() 127578344Sobrien{ 127678344Sobrien _file=$1 127778344Sobrien _arg=$2 127878344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 127978344Sobrien err 3 'USAGE: run_rc_script file arg' 128078344Sobrien fi 128178344Sobrien 128298186Sgordon unset name command command_args command_interpreter \ 128398186Sgordon extra_commands pidfile procname \ 1284272393Shrs rcvar rcvars rcvars_obsolete required_dirs required_files \ 1285197144Shrs required_vars 128698186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 128798186Sgordon 128878344Sobrien case "$_file" in 1289193118Sdougb /etc/rc.d/*.sh) # no longer allowed in the base 1290193118Sdougb warn "Ignoring old-style startup script $_file" 129178344Sobrien ;; 1292153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 129398186Sgordon warn "Ignoring scratch file $_file" 129498186Sgordon ;; 129578344Sobrien *) # run in subshell 129698186Sgordon if [ -x $_file ]; then 129798186Sgordon if [ -n "$rc_fast_and_loose" ]; then 1298146490Sschweikh set $_arg; . $_file 129998186Sgordon else 1300231888Sdelphij ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1301231888Sdelphij trap "echo Script $_file interrupted >&2 ; exit 1" 2 1302231888Sdelphij trap "echo Script $_file running >&2" 29 1303146490Sschweikh set $_arg; . $_file ) 130498186Sgordon fi 130598186Sgordon fi 130678344Sobrien ;; 130778344Sobrien esac 130878344Sobrien} 130978344Sobrien 131078344Sobrien# 1311157653Sflz# load_rc_config name 1312157653Sflz# Source in the configuration file for a given name. 131378344Sobrien# 131478344Sobrienload_rc_config() 131578344Sobrien{ 1316270698Shrs local _name _rcvar_val _var _defval _v _msg _new _d 1317157653Sflz _name=$1 1318157653Sflz if [ -z "$_name" ]; then 1319157653Sflz err 3 'USAGE: load_rc_config name' 132078344Sobrien fi 132178344Sobrien 1322219612Sdougb if ${_rc_conf_loaded:-false}; then 1323219612Sdougb : 1324219612Sdougb else 132598186Sgordon if [ -r /etc/defaults/rc.conf ]; then 132698186Sgordon debug "Sourcing /etc/defaults/rc.conf" 132798186Sgordon . /etc/defaults/rc.conf 132898186Sgordon source_rc_confs 132998186Sgordon elif [ -r /etc/rc.conf ]; then 133098186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 133198186Sgordon . /etc/rc.conf 133298186Sgordon fi 1333126285Smtm _rc_conf_loaded=true 133498186Sgordon fi 1335179872Smtm 1336270698Shrs for _d in /etc ${local_startup%*/rc.d}; do 1337270698Shrs if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1338270698Shrs debug "Sourcing ${_d}/rc.conf.d/$_name" 1339270698Shrs . ${_d}/rc.conf.d/"$_name" 1340270698Shrs elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1341270698Shrs local _rc 1342270698Shrs for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1343270698Shrs if [ -f "$_rc" ] ; then 1344270698Shrs debug "Sourcing $_rc" 1345270698Shrs . "$_rc" 1346270698Shrs fi 1347270698Shrs done 1348270698Shrs fi 1349270698Shrs done 1350270698Shrs 1351197144Shrs # Set defaults if defined. 1352272393Shrs for _var in $rcvar $rcvars; do 1353223227Sjilles eval _defval=\$${_var}_defval 1354197144Shrs if [ -n "$_defval" ]; then 1355197144Shrs eval : \${$_var:=\$${_var}_defval} 1356197144Shrs fi 1357197144Shrs done 1358197144Shrs 1359197144Shrs # check obsolete rc.conf variables 1360197144Shrs for _var in $rcvars_obsolete; do 1361223227Sjilles eval _v=\$$_var 1362223227Sjilles eval _msg=\$${_var}_obsolete_msg 1363223227Sjilles eval _new=\$${_var}_newvar 1364197144Shrs case $_v in 1365197144Shrs "") 1366197144Shrs ;; 1367197144Shrs *) 1368197144Shrs if [ -z "$_new" ]; then 1369197144Shrs _msg="Ignored." 1370197144Shrs else 1371197144Shrs eval $_new=\"\$$_var\" 1372197144Shrs if [ -z "$_msg" ]; then 1373197144Shrs _msg="Use \$$_new instead." 1374197144Shrs fi 1375197144Shrs fi 1376197144Shrs warn "\$$_var is obsolete. $_msg" 1377197144Shrs ;; 1378197144Shrs esac 1379197144Shrs done 138078344Sobrien} 1381201036Sdougb 1382157473Sflz# 1383157653Sflz# load_rc_config_var name var 1384157653Sflz# Read the rc.conf(5) var for name and set in the 1385157473Sflz# current shell, using load_rc_config in a subshell to prevent 1386157473Sflz# unwanted side effects from other variable assignments. 1387157473Sflz# 1388157473Sflzload_rc_config_var() 1389157473Sflz{ 1390157473Sflz if [ $# -ne 2 ]; then 1391157653Sflz err 3 'USAGE: load_rc_config_var name var' 1392157473Sflz fi 1393157473Sflz eval $(eval '( 1394157473Sflz load_rc_config '$1' >/dev/null; 1395157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1396157473Sflz echo '$2'=\'\''${'$2'}\'\''; 1397157473Sflz fi 1398157473Sflz )' ) 1399157473Sflz} 140078344Sobrien 140178344Sobrien# 140278344Sobrien# rc_usage commands 140378344Sobrien# Print a usage string for $0, with `commands' being a list of 140478344Sobrien# valid commands. 140578344Sobrien# 140678344Sobrienrc_usage() 140778344Sobrien{ 1408230007Srea echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 140978344Sobrien 141078344Sobrien _sep= 1411126286Smtm for _elem; do 141278344Sobrien echo -n 1>&2 "$_sep$_elem" 141378344Sobrien _sep="|" 141478344Sobrien done 141578344Sobrien echo 1>&2 ")" 141678344Sobrien exit 1 141778344Sobrien} 141878344Sobrien 141978344Sobrien# 142078344Sobrien# err exitval message 142178344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 142278344Sobrien# 142378344Sobrienerr() 142478344Sobrien{ 142578344Sobrien exitval=$1 142678344Sobrien shift 142778344Sobrien 1428106643Sgordon if [ -x /usr/bin/logger ]; then 1429106643Sgordon logger "$0: ERROR: $*" 1430106643Sgordon fi 1431106643Sgordon echo 1>&2 "$0: ERROR: $*" 143278344Sobrien exit $exitval 143378344Sobrien} 143478344Sobrien 143578344Sobrien# 143678344Sobrien# warn message 143778344Sobrien# Display message to stderr and log to the syslog. 143878344Sobrien# 143978344Sobrienwarn() 144078344Sobrien{ 1441106643Sgordon if [ -x /usr/bin/logger ]; then 1442106643Sgordon logger "$0: WARNING: $*" 1443106643Sgordon fi 1444106643Sgordon echo 1>&2 "$0: WARNING: $*" 144578344Sobrien} 144698186Sgordon 144798186Sgordon# 144898186Sgordon# info message 144998186Sgordon# Display informational message to stdout and log to syslog. 145098186Sgordon# 145198186Sgordoninfo() 145298186Sgordon{ 1453119170Smtm case ${rc_info} in 1454119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1455119170Smtm if [ -x /usr/bin/logger ]; then 1456119170Smtm logger "$0: INFO: $*" 1457119170Smtm fi 1458119170Smtm echo "$0: INFO: $*" 1459119170Smtm ;; 1460119170Smtm esac 146198186Sgordon} 146298186Sgordon 146398186Sgordon# 146498186Sgordon# debug message 1465106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 146698186Sgordon# BEWARE that you don't call any subroutine that itself calls this 146798186Sgordon# function. 146898186Sgordon# 146998186Sgordondebug() 147098186Sgordon{ 147198186Sgordon case ${rc_debug} in 147298186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1473106700Sgordon if [ -x /usr/bin/logger ]; then 1474162947Syar logger "$0: DEBUG: $*" 1475106700Sgordon fi 1476146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 147798186Sgordon ;; 147898186Sgordon esac 147998186Sgordon} 148098186Sgordon 148198186Sgordon# 148298186Sgordon# backup_file action file cur backup 148398186Sgordon# Make a backup copy of `file' into `cur', and save the previous 148498186Sgordon# version of `cur' as `backup' or use rcs for archiving. 148598186Sgordon# 148698186Sgordon# This routine checks the value of the backup_uses_rcs variable, 148798186Sgordon# which can be either YES or NO. 148898186Sgordon# 148998186Sgordon# The `action' keyword can be one of the following: 149098186Sgordon# 149198186Sgordon# add `file' is now being backed up (and is possibly 149298186Sgordon# being reentered into the backups system). `cur' 149398186Sgordon# is created and RCS files, if necessary, are 149498186Sgordon# created as well. 149598186Sgordon# 149698186Sgordon# update `file' has changed and needs to be backed up. 149798186Sgordon# If `cur' exists, it is copied to to `back' or 149898186Sgordon# checked into RCS (if the repository file is old), 149998186Sgordon# and then `file' is copied to `cur'. Another RCS 150098186Sgordon# check in done here if RCS is being used. 150198186Sgordon# 150298186Sgordon# remove `file' is no longer being tracked by the backups 150398186Sgordon# system. If RCS is not being used, `cur' is moved 150498186Sgordon# to `back', otherwise an empty file is checked in, 150598186Sgordon# and then `cur' is removed. 150698186Sgordon# 150798186Sgordon# 150898186Sgordonbackup_file() 150998186Sgordon{ 151098186Sgordon _action=$1 151198186Sgordon _file=$2 151298186Sgordon _cur=$3 151398186Sgordon _back=$4 151498186Sgordon 151598186Sgordon if checkyesno backup_uses_rcs; then 151698186Sgordon _msg0="backup archive" 151798186Sgordon _msg1="update" 151898186Sgordon 151998186Sgordon # ensure that history file is not locked 152098186Sgordon if [ -f $_cur,v ]; then 152198186Sgordon rcs -q -u -U -M $_cur 152298186Sgordon fi 152398186Sgordon 152498186Sgordon # ensure after switching to rcs that the 152598186Sgordon # current backup is not lost 152698186Sgordon if [ -f $_cur ]; then 152798186Sgordon # no archive, or current newer than archive 152898186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 152998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 153098186Sgordon rcs -q -kb -U $_cur 153198186Sgordon co -q -f -u $_cur 153298186Sgordon fi 153398186Sgordon fi 153498186Sgordon 153598186Sgordon case $_action in 153698186Sgordon add|update) 153798186Sgordon cp -p $_file $_cur 153898186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 153998186Sgordon rcs -q -kb -U $_cur 154098186Sgordon co -q -f -u $_cur 154198186Sgordon chown root:wheel $_cur $_cur,v 154298186Sgordon ;; 154398186Sgordon remove) 154498186Sgordon cp /dev/null $_cur 154598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 154698186Sgordon rcs -q -kb -U $_cur 154798186Sgordon chown root:wheel $_cur $_cur,v 154898186Sgordon rm $_cur 154998186Sgordon ;; 155098186Sgordon esac 155198186Sgordon else 155298186Sgordon case $_action in 155398186Sgordon add|update) 155498186Sgordon if [ -f $_cur ]; then 155598186Sgordon cp -p $_cur $_back 155698186Sgordon fi 155798186Sgordon cp -p $_file $_cur 155898186Sgordon chown root:wheel $_cur 155998186Sgordon ;; 156098186Sgordon remove) 156198186Sgordon mv -f $_cur $_back 156298186Sgordon ;; 156398186Sgordon esac 156498186Sgordon fi 156598186Sgordon} 1566119166Smtm 1567123344Smtm# make_symlink src link 1568123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1569123344Smtm# directory in which link is to be created does not exist 1570123344Smtm# a warning will be displayed and an error will be returned. 1571229783Suqs# Returns 0 on success, 1 otherwise. 1572119166Smtm# 1573123344Smtmmake_symlink() 1574119166Smtm{ 1575123344Smtm local src link linkdir _me 1576123344Smtm src="$1" 1577123344Smtm link="$2" 1578123344Smtm linkdir="`dirname $link`" 1579123344Smtm _me="make_symlink()" 1580119166Smtm 1581123344Smtm if [ -z "$src" -o -z "$link" ]; then 1582123344Smtm warn "$_me: requires two arguments." 1583119166Smtm return 1 1584119166Smtm fi 1585123344Smtm if [ ! -d "$linkdir" ]; then 1586160667Syar warn "$_me: the directory $linkdir does not exist." 1587119166Smtm return 1 1588119166Smtm fi 1589146490Sschweikh if ! ln -sf $src $link; then 1590123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1591119166Smtm return 1 1592119166Smtm fi 1593119166Smtm return 0 1594119166Smtm} 1595119166Smtm 1596119166Smtm# devfs_rulesets_from_file file 1597119166Smtm# Reads a set of devfs commands from file, and creates 1598119166Smtm# the specified rulesets with their rules. Returns non-zero 1599119166Smtm# if there was an error. 1600119166Smtm# 1601119166Smtmdevfs_rulesets_from_file() 1602119166Smtm{ 1603248820Savg local file _err _me _opts 1604119166Smtm file="$1" 1605119166Smtm _me="devfs_rulesets_from_file" 1606119166Smtm _err=0 1607119166Smtm 1608119166Smtm if [ -z "$file" ]; then 1609119166Smtm warn "$_me: you must specify a file" 1610119166Smtm return 1 1611119166Smtm fi 1612119166Smtm if [ ! -e "$file" ]; then 1613119166Smtm debug "$_me: no such file ($file)" 1614119166Smtm return 0 1615119166Smtm fi 1616248820Savg 1617248820Savg # Disable globbing so that the rule patterns are not expanded 1618248820Savg # by accident with matching filesystem entries. 1619248820Savg _opts=$-; set -f 1620248820Savg 1621119166Smtm debug "reading rulesets from file ($file)" 1622119166Smtm { while read line 1623119166Smtm do 1624119166Smtm case $line in 1625119166Smtm \#*) 1626119166Smtm continue 1627119166Smtm ;; 1628119166Smtm \[*\]*) 1629119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1630119166Smtm if [ -z "$rulenum" ]; then 1631119166Smtm warn "$_me: cannot extract rule number ($line)" 1632119166Smtm _err=1 1633119166Smtm break 1634119166Smtm fi 1635119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1636119166Smtm if [ -z "$rulename" ]; then 1637119166Smtm warn "$_me: cannot extract rule name ($line)" 1638119166Smtm _err=1 1639119166Smtm break; 1640119166Smtm fi 1641119166Smtm eval $rulename=\$rulenum 1642119166Smtm debug "found ruleset: $rulename=$rulenum" 1643146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1644119166Smtm _err=1 1645119166Smtm break 1646119166Smtm fi 1647119166Smtm ;; 1648119166Smtm *) 1649119166Smtm rulecmd="${line%%"\#*"}" 1650119166Smtm # evaluate the command incase it includes 1651119166Smtm # other rules 1652119166Smtm if [ -n "$rulecmd" ]; then 1653119166Smtm debug "adding rule ($rulecmd)" 1654119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1655119166Smtm then 1656119166Smtm _err=1 1657119166Smtm break 1658119166Smtm fi 1659119166Smtm fi 1660119166Smtm ;; 1661119166Smtm esac 1662119166Smtm if [ $_err -ne 0 ]; then 1663119166Smtm debug "error in $_me" 1664119166Smtm break 1665119166Smtm fi 1666119166Smtm done } < $file 1667248820Savg case $_opts in *f*) ;; *) set +f ;; esac 1668119166Smtm return $_err 1669119166Smtm} 1670119166Smtm 1671119166Smtm# devfs_init_rulesets 1672119166Smtm# Initializes rulesets from configuration files. Returns 1673119166Smtm# non-zero if there was an error. 1674119166Smtm# 1675119166Smtmdevfs_init_rulesets() 1676119166Smtm{ 1677119166Smtm local file _me 1678119166Smtm _me="devfs_init_rulesets" 1679119166Smtm 1680119166Smtm # Go through this only once 1681119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1682119166Smtm debug "$_me: devfs rulesets already initialized" 1683119166Smtm return 1684119166Smtm fi 1685146490Sschweikh for file in $devfs_rulesets; do 1686217090Sjh if ! devfs_rulesets_from_file $file; then 1687217090Sjh warn "$_me: could not read rules from $file" 1688217090Sjh return 1 1689217090Sjh fi 1690119166Smtm done 1691119166Smtm devfs_rulesets_init=1 1692119166Smtm debug "$_me: devfs rulesets initialized" 1693119166Smtm return 0 1694119166Smtm} 1695119166Smtm 1696119166Smtm# devfs_set_ruleset ruleset [dir] 1697151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1698119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1699119166Smtm# Returns non-zero if it could not set it successfully. 1700119166Smtm# 1701119166Smtmdevfs_set_ruleset() 1702119166Smtm{ 1703119166Smtm local devdir rs _me 1704119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1705119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1706119166Smtm _me="devfs_set_ruleset" 1707119166Smtm 1708119166Smtm if [ -z "$rs" ]; then 1709119166Smtm warn "$_me: you must specify a ruleset number" 1710119166Smtm return 1 1711119166Smtm fi 1712119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1713146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1714119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1715119166Smtm return 1 1716119166Smtm fi 1717119166Smtm return 0 1718119166Smtm} 1719119166Smtm 1720119166Smtm# devfs_apply_ruleset ruleset [dir] 1721119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1722119166Smtm# The ruleset argument must be a ruleset name as specified 1723119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1724119166Smtm# if it could not apply the ruleset. 1725119166Smtm# 1726119166Smtmdevfs_apply_ruleset() 1727119166Smtm{ 1728119166Smtm local devdir rs _me 1729119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1730119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1731119166Smtm _me="devfs_apply_ruleset" 1732119166Smtm 1733119166Smtm if [ -z "$rs" ]; then 1734119166Smtm warn "$_me: you must specify a ruleset" 1735119166Smtm return 1 1736119166Smtm fi 1737119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1738146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1739119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1740119166Smtm return 1 1741119166Smtm fi 1742119166Smtm return 0 1743119166Smtm} 1744119166Smtm 1745119166Smtm# devfs_domount dir [ruleset] 1746119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1747119166Smtm# on the mount-point. It must also be a ruleset name as specified 1748119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1749119166Smtm# 1750119166Smtmdevfs_domount() 1751119166Smtm{ 1752119166Smtm local devdir rs _me 1753119166Smtm devdir="$1" 1754119166Smtm [ -n "$2" ] && rs=$2 || rs= 1755119166Smtm _me="devfs_domount()" 1756119166Smtm 1757119166Smtm if [ -z "$devdir" ]; then 1758119166Smtm warn "$_me: you must specify a mount-point" 1759119166Smtm return 1 1760119166Smtm fi 1761119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1762146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1763119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1764119166Smtm return 1 1765119166Smtm fi 1766119166Smtm if [ -n "$rs" ]; then 1767119166Smtm devfs_init_rulesets 1768119166Smtm devfs_set_ruleset $rs $devdir 1769124797Scperciva devfs -m $devdir rule applyset 1770119166Smtm fi 1771119166Smtm return 0 1772119166Smtm} 1773119166Smtm 1774127345Sbrooks# Provide a function for normalizing the mounting of memory 1775127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1776127345Sbrooks# as close as possible between 5-current and 4-stable. 1777127345Sbrooks# $1 = size 1778127345Sbrooks# $2 = mount point 1779137451Skeramida# $3 = (optional) extra mdmfs flags 1780146490Sschweikhmount_md() 1781146490Sschweikh{ 1782127345Sbrooks if [ -n "$3" ]; then 1783137451Skeramida flags="$3" 1784127345Sbrooks fi 1785149421Syar /sbin/mdmfs $flags -s $1 md $2 1786127345Sbrooks} 1787131550Scperciva 1788159828Syar# Code common to scripts that need to load a kernel module 1789159828Syar# if it isn't in the kernel yet. Syntax: 1790160666Syar# load_kld [-e regex] [-m module] file 1791159828Syar# where -e or -m chooses the way to check if the module 1792159828Syar# is already loaded: 1793160666Syar# regex is egrep'd in the output from `kldstat -v', 1794160666Syar# module is passed to `kldstat -m'. 1795160666Syar# The default way is as though `-m file' were specified. 1796159828Syarload_kld() 1797159828Syar{ 1798159828Syar local _loaded _mod _opt _re 1799159828Syar 1800159828Syar while getopts "e:m:" _opt; do 1801159828Syar case "$_opt" in 1802159828Syar e) _re="$OPTARG" ;; 1803159828Syar m) _mod="$OPTARG" ;; 1804160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1805159828Syar esac 1806159828Syar done 1807159828Syar shift $(($OPTIND - 1)) 1808160666Syar if [ $# -ne 1 ]; then 1809160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1810160666Syar fi 1811159828Syar _mod=${_mod:-$1} 1812159828Syar _loaded=false 1813159828Syar if [ -n "$_re" ]; then 1814159828Syar if kldstat -v | egrep -q -e "$_re"; then 1815159828Syar _loaded=true 1816159828Syar fi 1817159828Syar else 1818159828Syar if kldstat -q -m "$_mod"; then 1819159828Syar _loaded=true 1820159828Syar fi 1821159828Syar fi 1822159828Syar if ! $_loaded; then 1823159828Syar if ! kldload "$1"; then 1824159828Syar warn "Unable to load kernel module $1" 1825159828Syar return 1 1826160666Syar else 1827160666Syar info "$1 kernel module loaded." 1828159828Syar fi 1829160666Syar else 1830160666Syar debug "load_kld: $1 kernel module already loaded." 1831159828Syar fi 1832159828Syar return 0 1833159828Syar} 1834159828Syar 1835264243Sdteske# ltr str src dst [var] 1836149049Spjd# Change every $src in $str to $dst. 1837149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1838264243Sdteske# awk(1). If var is non-NULL, set it to the result. 1839149049Spjdltr() 1840149049Spjd{ 1841264243Sdteske local _str _src _dst _out _com _var 1842264243Sdteske _str="$1" 1843264243Sdteske _src="$2" 1844264243Sdteske _dst="$3" 1845264243Sdteske _var="$4" 1846149049Spjd _out="" 1847149049Spjd 1848264243Sdteske local IFS="${_src}" 1849149049Spjd for _com in ${_str}; do 1850149049Spjd if [ -z "${_out}" ]; then 1851149049Spjd _out="${_com}" 1852149049Spjd else 1853149049Spjd _out="${_out}${_dst}${_com}" 1854149049Spjd fi 1855149049Spjd done 1856264243Sdteske if [ -n "${_var}" ]; then 1857264243Sdteske setvar "${_var}" "${_out}" 1858264243Sdteske else 1859264243Sdteske echo "${_out}" 1860264243Sdteske fi 1861149049Spjd} 1862149049Spjd 1863149050Spjd# Creates a list of providers for GELI encryption. 1864149050Spjdgeli_make_list() 1865149050Spjd{ 1866149050Spjd local devices devices2 1867149050Spjd local provider mountpoint type options rest 1868149050Spjd 1869149050Spjd # Create list of GELI providers from fstab. 1870149050Spjd while read provider mountpoint type options rest ; do 1871155570Sflz case ":${options}" in 1872155570Sflz :*noauto*) 1873155570Sflz noauto=yes 1874155570Sflz ;; 1875155570Sflz *) 1876155570Sflz noauto=no 1877155570Sflz ;; 1878155570Sflz esac 1879155570Sflz 1880149050Spjd case ":${provider}" in 1881149050Spjd :#*) 1882149050Spjd continue 1883149050Spjd ;; 1884149050Spjd *.eli) 1885149050Spjd # Skip swap devices. 1886155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1887149050Spjd continue 1888149050Spjd fi 1889149050Spjd devices="${devices} ${provider}" 1890149050Spjd ;; 1891149050Spjd esac 1892149050Spjd done < /etc/fstab 1893149050Spjd 1894149050Spjd # Append providers from geli_devices. 1895149050Spjd devices="${devices} ${geli_devices}" 1896149050Spjd 1897149050Spjd for provider in ${devices}; do 1898149050Spjd provider=${provider%.eli} 1899149050Spjd provider=${provider#/dev/} 1900149050Spjd devices2="${devices2} ${provider}" 1901149050Spjd done 1902149050Spjd 1903149050Spjd echo ${devices2} 1904149050Spjd} 1905149050Spjd 1906153027Sdougb# Find scripts in local_startup directories that use the old syntax 1907153027Sdougb# 1908238416Skevlofind_local_scripts_old() { 1909153027Sdougb zlist='' 1910153027Sdougb slist='' 1911153027Sdougb for dir in ${local_startup}; do 1912153027Sdougb if [ -d "${dir}" ]; then 1913153027Sdougb for file in ${dir}/[0-9]*.sh; do 1914153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1915153027Sdougb continue 1916153027Sdougb zlist="$zlist $file" 1917153027Sdougb done 1918227366Sjilles for file in ${dir}/[!0-9]*.sh; do 1919153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1920153027Sdougb continue 1921153027Sdougb slist="$slist $file" 1922153027Sdougb done 1923153027Sdougb fi 1924153027Sdougb done 1925153027Sdougb} 1926153027Sdougb 1927238416Skevlofind_local_scripts_new() { 1928153027Sdougb local_rc='' 1929153027Sdougb for dir in ${local_startup}; do 1930153027Sdougb if [ -d "${dir}" ]; then 1931153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1932153027Sdougb case "$file" in 1933153027Sdougb *.sample) ;; 1934153027Sdougb *) if [ -x "$file" ]; then 1935153027Sdougb local_rc="${local_rc} ${file}" 1936153027Sdougb fi 1937153027Sdougb ;; 1938153027Sdougb esac 1939153027Sdougb done 1940153027Sdougb fi 1941153027Sdougb done 1942153027Sdougb} 1943153027Sdougb 1944165565Syar# check_required_{before|after} command 1945165565Syar# Check for things required by the command before and after its precmd, 1946165565Syar# respectively. The two separate functions are needed because some 1947165565Syar# conditions should prevent precmd from being run while other things 1948165565Syar# depend on precmd having already been run. 1949165565Syar# 1950165565Syarcheck_required_before() 1951165565Syar{ 1952165565Syar local _f 1953165565Syar 1954165565Syar case "$1" in 1955165565Syar start) 1956165565Syar for _f in $required_vars; do 1957165565Syar if ! checkyesno $_f; then 1958165565Syar warn "\$${_f} is not enabled." 1959165565Syar if [ -z "$rc_force" ]; then 1960165565Syar return 1 1961165565Syar fi 1962165565Syar fi 1963165565Syar done 1964165565Syar 1965165565Syar for _f in $required_dirs; do 1966165565Syar if [ ! -d "${_f}/." ]; then 1967165565Syar warn "${_f} is not a directory." 1968165565Syar if [ -z "$rc_force" ]; then 1969165565Syar return 1 1970165565Syar fi 1971165565Syar fi 1972165565Syar done 1973165565Syar 1974165565Syar for _f in $required_files; do 1975165565Syar if [ ! -r "${_f}" ]; then 1976165565Syar warn "${_f} is not readable." 1977165565Syar if [ -z "$rc_force" ]; then 1978165565Syar return 1 1979165565Syar fi 1980165565Syar fi 1981165565Syar done 1982165565Syar ;; 1983165565Syar esac 1984165565Syar 1985165565Syar return 0 1986165565Syar} 1987165565Syar 1988165565Syarcheck_required_after() 1989165565Syar{ 1990165565Syar local _f _args 1991165565Syar 1992165565Syar case "$1" in 1993165565Syar start) 1994165565Syar for _f in $required_modules; do 1995165565Syar case "${_f}" in 1996165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1997165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1998165565Syar *) _args="${_f}" ;; 1999165565Syar esac 2000165565Syar if ! load_kld ${_args}; then 2001165565Syar if [ -z "$rc_force" ]; then 2002165565Syar return 1 2003165565Syar fi 2004165565Syar fi 2005165565Syar done 2006165565Syar ;; 2007165565Syar esac 2008165565Syar 2009165565Syar return 0 2010165565Syar} 2011165565Syar 2012271545Shrs# check_jail mib 2013271545Shrs# Return true if security.jail.$mib exists and set to 1. 2014271545Shrs 2015271545Shrscheck_jail() 2016271545Shrs{ 2017271545Shrs local _mib _v 2018271545Shrs 2019271545Shrs _mib=$1 2020271545Shrs if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 2021271545Shrs case $_v in 2022271545Shrs 1) return 0;; 2023271545Shrs esac 2024271545Shrs fi 2025271545Shrs return 1 2026271545Shrs} 2027271545Shrs 2028222996Shrs# check_kern_features mib 2029222996Shrs# Return existence of kern.features.* sysctl MIB as true or 2030222996Shrs# false. The result will be cached in $_rc_cache_kern_features_ 2031222996Shrs# namespace. "0" means the kern.features.X exists. 2032222996Shrs 2033222996Shrscheck_kern_features() 2034222996Shrs{ 2035222996Shrs local _v 2036222996Shrs 2037222996Shrs [ -n "$1" ] || return 1; 2038223292Sjilles eval _v=\$_rc_cache_kern_features_$1 2039222996Shrs [ -n "$_v" ] && return "$_v"; 2040222996Shrs 2041222996Shrs if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 2042222996Shrs eval _rc_cache_kern_features_$1=0 2043222996Shrs return 0 2044222996Shrs else 2045222996Shrs eval _rc_cache_kern_features_$1=1 2046222996Shrs return 1 2047222996Shrs fi 2048222996Shrs} 2049222996Shrs 2050243184Shrs# check_namevarlist var 2051243184Shrs# Return "0" if ${name}_var is reserved in rc.subr. 2052243184Shrs 2053243184Shrs_rc_namevarlist="program chroot chdir flags fib nice user group groups" 2054243184Shrscheck_namevarlist() 2055243184Shrs{ 2056243184Shrs local _v 2057243184Shrs 2058243184Shrs for _v in $_rc_namevarlist; do 2059243184Shrs case $1 in 2060243184Shrs $_v) return 0 ;; 2061243184Shrs esac 2062243184Shrs done 2063243184Shrs 2064243184Shrs return 1 2065243184Shrs} 2066243184Shrs 2067197144Shrs# _echoonce var msg mode 2068197144Shrs# mode=0: Echo $msg if ${$var} is empty. 2069197144Shrs# After doing echo, a string is set to ${$var}. 2070197144Shrs# 2071197144Shrs# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2072197144Shrs# 2073197144Shrs_echoonce() 2074197144Shrs{ 2075197144Shrs local _var _msg _mode 2076223227Sjilles eval _var=\$$1 2077197144Shrs _msg=$2 2078197144Shrs _mode=$3 2079197144Shrs 2080197144Shrs case $_mode in 2081197144Shrs 1) [ -n "$_var" ] && echo "$_msg" ;; 2082197144Shrs *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2083197144Shrs esac 2084197144Shrs} 2085197144Shrs 2086223298Sjillesfi # [ -z "${_rc_subr_loaded}" ] 2087223298Sjilles 2088157841Sflz_rc_subr_loaded=: 2089