rc.subr revision 272393
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 272393 2014-10-02 01:16:30Z 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# 752242184Shrs# ${name}_fib n Routing table number to run ${command} with. 753242184Shrs# 75478344Sobrien# ${name}_nice n Nice level to run ${command} at. 75578344Sobrien# 75678344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 75778344Sobrien# using ${name}_chroot. 75898186Sgordon# Requires /usr to be mounted. 75978344Sobrien# 76078344Sobrien# ${name}_group n Group to run chrooted ${command} as. 76198186Sgordon# Requires /usr to be mounted. 76278344Sobrien# 76398186Sgordon# ${name}_groups n Comma separated list of supplementary groups 76498186Sgordon# to run the chrooted ${command} with. 76598186Sgordon# Requires /usr to be mounted. 76678344Sobrien# 76798186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 76878344Sobrien# Otherwise, use default command (see below) 76978344Sobrien# 77098186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 77198186Sgordon# ${rc_arg}_cmd method in the default 77298186Sgordon# operation (i.e, after checking for required 77398186Sgordon# bits and process (non)existence). 77478344Sobrien# If this completes with a non-zero exit code, 77598186Sgordon# don't run ${rc_arg}_cmd. 77678344Sobrien# 77798186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 77898186Sgordon# ${rc_arg}_cmd method, if that method 77998186Sgordon# returned a zero exit code. 78098186Sgordon# 78178344Sobrien# required_dirs n If set, check for the existence of the given 782165565Syar# directories before running a (re)start command. 78378344Sobrien# 78478344Sobrien# required_files n If set, check for the readability of the given 785165565Syar# files before running a (re)start command. 78678344Sobrien# 787165565Syar# required_modules n If set, ensure the given kernel modules are 788165565Syar# loaded before running a (re)start command. 789165565Syar# The check and possible loads are actually 790165565Syar# done after start_precmd so that the modules 791165565Syar# aren't loaded in vain, should the precmd 792165565Syar# return a non-zero status to indicate a error. 793165565Syar# If a word in the list looks like "foo:bar", 794165565Syar# "foo" is the KLD file name and "bar" is the 795165565Syar# module name. If a word looks like "foo~bar", 796165565Syar# "foo" is the KLD file name and "bar" is a 797165565Syar# egrep(1) pattern matching the module name. 798165565Syar# Otherwise the module name is assumed to be 799165565Syar# the same as the KLD file name, which is most 800165565Syar# common. See load_kld(). 801165565Syar# 80278344Sobrien# required_vars n If set, perform checkyesno on each of the 80378344Sobrien# listed variables before running the default 80478344Sobrien# (re)start command. 80578344Sobrien# 80698186Sgordon# Default behaviour for a given argument, if no override method is 80798186Sgordon# provided: 80878344Sobrien# 80998186Sgordon# Argument Default behaviour 81098186Sgordon# -------- ----------------- 81178344Sobrien# start if !running && checkyesno ${rcvar} 81278344Sobrien# ${command} 81378344Sobrien# 81478344Sobrien# stop if ${pidfile} 81598186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 81678344Sobrien# else 81798186Sgordon# rc_pid=$(check_process $command) 81898186Sgordon# kill $sig_stop $rc_pid 81998186Sgordon# wait_for_pids $rc_pid 82098186Sgordon# ($sig_stop defaults to TERM.) 82178344Sobrien# 82298186Sgordon# reload Similar to stop, except use $sig_reload instead, 82398186Sgordon# and doesn't wait_for_pids. 82478344Sobrien# $sig_reload defaults to HUP. 825151685Syar# Note that `reload' isn't provided by default, 826151685Syar# it should be enabled via $extra_commands. 82778344Sobrien# 82878344Sobrien# restart Run `stop' then `start'. 82978344Sobrien# 83098186Sgordon# status Show if ${command} is running, etc. 83178344Sobrien# 83298186Sgordon# poll Wait for ${command} to exit. 83398186Sgordon# 83498186Sgordon# rcvar Display what rc.conf variable is used (if any). 83598186Sgordon# 836255809Sdes# enabled Return true if the service is enabled. 837255809Sdes# 83898186Sgordon# Variables available to methods, and after run_rc_command() has 83998186Sgordon# completed: 84098186Sgordon# 84198186Sgordon# Variable Purpose 84298186Sgordon# -------- ------- 843126303Smtm# rc_arg Argument to command, after fast/force/one processing 84498186Sgordon# performed 84598186Sgordon# 84698186Sgordon# rc_flags Flags to start the default command with. 84798186Sgordon# Defaults to ${name}_flags, unless overridden 84898186Sgordon# by $flags from the environment. 84998186Sgordon# This variable may be changed by the precmd method. 85098186Sgordon# 85198186Sgordon# rc_pid PID of command (if appropriate) 85298186Sgordon# 85398186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 85498186Sgordon# 85598186Sgordon# rc_force Not empty if "force" was provided (q.v.) 85698186Sgordon# 857175676Smtm# rc_quiet Not empty if "quiet" was provided 85898186Sgordon# 859175676Smtm# 86078344Sobrienrun_rc_command() 86178344Sobrien{ 862116097Smtm _return=0 86398186Sgordon rc_arg=$1 86478344Sobrien if [ -z "$name" ]; then 86598186Sgordon err 3 'run_rc_command: $name is not set.' 86678344Sobrien fi 86778344Sobrien 868132892Smtm # Don't repeat the first argument when passing additional command- 869132892Smtm # line arguments to the command subroutines. 870132892Smtm # 871132892Smtm shift 1 872132892Smtm rc_extra_args="$*" 873132892Smtm 874126303Smtm _rc_prefix= 87598186Sgordon case "$rc_arg" in 87678344Sobrien fast*) # "fast" prefix; don't check pid 87798186Sgordon rc_arg=${rc_arg#fast} 87898186Sgordon rc_fast=yes 879175676Smtm rc_quiet=yes 88078344Sobrien ;; 881198216Sed force*) # "force" prefix; always run 88298186Sgordon rc_force=yes 883126303Smtm _rc_prefix=force 884126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 88578344Sobrien if [ -n "${rcvar}" ]; then 88678344Sobrien eval ${rcvar}=YES 88778344Sobrien fi 88878344Sobrien ;; 889126303Smtm one*) # "one" prefix; set ${rcvar}=yes 890126303Smtm _rc_prefix=one 891126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 892126303Smtm if [ -n "${rcvar}" ]; then 893126303Smtm eval ${rcvar}=YES 894126303Smtm fi 895126303Smtm ;; 896175676Smtm quiet*) # "quiet" prefix; omit some messages 897175676Smtm _rc_prefix=quiet 898175676Smtm rc_arg=${rc_arg#${_rc_prefix}} 899175676Smtm rc_quiet=yes 900175676Smtm ;; 90178344Sobrien esac 90278344Sobrien 903161530Sflz eval _override_command=\$${name}_program 904198162Sdougb command=${_override_command:-$command} 905161530Sflz 906255809Sdes _keywords="start stop restart rcvar enabled $extra_commands" 90798186Sgordon rc_pid= 90878344Sobrien _pidcmd= 90998186Sgordon _procname=${procname:-${command}} 91098186Sgordon 911131135Smtm # setup pid check command 912131135Smtm if [ -n "$_procname" ]; then 91378344Sobrien if [ -n "$pidfile" ]; then 91498186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 91598186Sgordon else 91698186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 91778344Sobrien fi 91878344Sobrien if [ -n "$_pidcmd" ]; then 91998186Sgordon _keywords="${_keywords} status poll" 92078344Sobrien fi 92178344Sobrien fi 92278344Sobrien 92398186Sgordon if [ -z "$rc_arg" ]; then 924150796Syar rc_usage $_keywords 92578344Sobrien fi 92678344Sobrien 927255809Sdes if [ "$rc_arg" = "enabled" ] ; then 928255809Sdes checkyesno ${rcvar} 929255809Sdes return $? 930255809Sdes fi 931255809Sdes 93278344Sobrien if [ -n "$flags" ]; then # allow override from environment 93398186Sgordon rc_flags=$flags 93478344Sobrien else 93598186Sgordon eval rc_flags=\$${name}_flags 93678344Sobrien fi 93798186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 93898186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 939242184Shrs _group=\$${name}_group _groups=\$${name}_groups \ 940242184Shrs _fib=\$${name}_fib 94178344Sobrien 94298186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 943124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 94498186Sgordon unset _user 94598186Sgordon fi 94698186Sgordon fi 94798186Sgordon 948230374Sdougb [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 949179870Smtm 950179870Smtm for _elem in $_keywords; do 951179870Smtm if [ "$_elem" != "$rc_arg" ]; then 952179870Smtm continue 953179870Smtm fi 954206686Sdougb # if ${rcvar} is set, $1 is not "rcvar" 955206686Sdougb # and ${rc_pid} is not set, then run 95678344Sobrien # checkyesno ${rcvar} 95778344Sobrien # and return if that failed 95878344Sobrien # 959220760Sdougb if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 960220760Sdougb [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 961179870Smtm if ! checkyesno ${rcvar}; then 962179870Smtm if [ -n "${rc_quiet}" ]; then 963179870Smtm return 0 964179870Smtm fi 965179870Smtm echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 966179870Smtm echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 967179870Smtm echo "instead of '${rc_arg}'." 968175676Smtm return 0 969175676Smtm fi 97078344Sobrien fi 97178344Sobrien 97278344Sobrien # if there's a custom ${XXX_cmd}, 97378344Sobrien # run that instead of the default 97478344Sobrien # 975165565Syar eval _cmd=\$${rc_arg}_cmd \ 976165565Syar _precmd=\$${rc_arg}_precmd \ 977165565Syar _postcmd=\$${rc_arg}_postcmd 978165565Syar 97978344Sobrien if [ -n "$_cmd" ]; then 980165565Syar _run_rc_precmd || return 1 981165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 982165565Syar _run_rc_postcmd 983116097Smtm return $_return 98478344Sobrien fi 98578344Sobrien 98698186Sgordon case "$rc_arg" in # default operations... 98778344Sobrien 98878344Sobrien status) 989165565Syar _run_rc_precmd || return 1 99098186Sgordon if [ -n "$rc_pid" ]; then 99198186Sgordon echo "${name} is running as pid $rc_pid." 99278344Sobrien else 99378344Sobrien echo "${name} is not running." 99478344Sobrien return 1 99578344Sobrien fi 996165565Syar _run_rc_postcmd 99778344Sobrien ;; 99878344Sobrien 99978344Sobrien start) 1000131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 1001243324Shrs if [ -z "$rc_quiet" ]; then 1002243324Shrs echo 1>&2 "${name} already running? " \ 1003243324Shrs "(pid=$rc_pid)." 1004243324Shrs fi 1005153152Syar return 1 100678344Sobrien fi 100778344Sobrien 1008242183Screes if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 1009160667Syar warn "run_rc_command: cannot run $command" 1010153152Syar return 1 101178344Sobrien fi 101278344Sobrien 1013179946Smtm if ! _run_rc_precmd; then 1014179946Smtm warn "failed precmd routine for ${name}" 1015179946Smtm return 1 1016179946Smtm fi 101778344Sobrien 1018160668Syar # setup the full command to run 101978344Sobrien # 1020197947Sdougb check_startmsgs && echo "Starting ${name}." 102178344Sobrien if [ -n "$_chroot" ]; then 102278344Sobrien _doit="\ 102378344Sobrien${_nice:+nice -n $_nice }\ 1024242184Shrs${_fib:+setfib -F $_fib }\ 102578344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 102698186Sgordon$_chroot $command $rc_flags $command_args" 102778344Sobrien else 102878344Sobrien _doit="\ 1029161396Syar${_chdir:+cd $_chdir && }\ 1030242184Shrs${_fib:+setfib -F $_fib }\ 103198186Sgordon$command $rc_flags $command_args" 103298186Sgordon if [ -n "$_user" ]; then 103398186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 103498186Sgordon fi 1035161396Syar if [ -n "$_nice" ]; then 1036161396Syar if [ -z "$_user" ]; then 1037161396Syar _doit="sh -c \"$_doit\"" 1038201036Sdougb fi 1039161396Syar _doit="nice -n $_nice $_doit" 1040161396Syar fi 104178344Sobrien fi 104298186Sgordon 1043165565Syar # run the full command 104498186Sgordon # 1045179946Smtm if ! _run_rc_doit "$_doit"; then 1046179946Smtm warn "failed to start ${name}" 1047179946Smtm return 1 1048179946Smtm fi 104998186Sgordon 105098186Sgordon # finally, run postcmd 105198186Sgordon # 1052165565Syar _run_rc_postcmd 105378344Sobrien ;; 105478344Sobrien 105578344Sobrien stop) 105698186Sgordon if [ -z "$rc_pid" ]; then 1057153152Syar [ -n "$rc_fast" ] && return 0 1058165565Syar _run_rc_notrunning 1059153152Syar return 1 106078344Sobrien fi 106178344Sobrien 1062165565Syar _run_rc_precmd || return 1 106398186Sgordon 106498186Sgordon # send the signal to stop 106598186Sgordon # 106678344Sobrien echo "Stopping ${name}." 1067165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1068165565Syar _run_rc_doit "$_doit" || return 1 106998186Sgordon 107098186Sgordon # wait for the command to exit, 107198186Sgordon # and run postcmd. 107298186Sgordon wait_for_pids $rc_pid 1073165565Syar 1074165565Syar _run_rc_postcmd 107578344Sobrien ;; 107678344Sobrien 107778344Sobrien reload) 107898186Sgordon if [ -z "$rc_pid" ]; then 1079165565Syar _run_rc_notrunning 1080153152Syar return 1 108178344Sobrien fi 1082165565Syar 1083165565Syar _run_rc_precmd || return 1 1084165565Syar 1085165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1086165565Syar _run_rc_doit "$_doit" || return 1 1087165565Syar 1088165565Syar _run_rc_postcmd 108978344Sobrien ;; 109078344Sobrien 109178344Sobrien restart) 109278344Sobrien # prevent restart being called more 109378344Sobrien # than once by any given script 109478344Sobrien # 1095126285Smtm if ${_rc_restart_done:-false}; then 109678344Sobrien return 0 109778344Sobrien fi 1098126285Smtm _rc_restart_done=true 109978344Sobrien 1100165565Syar _run_rc_precmd || return 1 1101165565Syar 1102165565Syar # run those in a subshell to keep global variables 1103152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1104165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1105165565Syar _return=$? 1106165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 110798186Sgordon 1108165565Syar _run_rc_postcmd 110978344Sobrien ;; 111078344Sobrien 111198186Sgordon poll) 1112165565Syar _run_rc_precmd || return 1 111398186Sgordon if [ -n "$rc_pid" ]; then 111498186Sgordon wait_for_pids $rc_pid 111598186Sgordon fi 1116165565Syar _run_rc_postcmd 111798186Sgordon ;; 111898186Sgordon 111978344Sobrien rcvar) 1120197144Shrs echo -n "# $name" 1121197144Shrs if [ -n "$desc" ]; then 1122197144Shrs echo " : $desc" 1123197144Shrs else 1124197144Shrs echo "" 1125197144Shrs fi 1126197144Shrs echo "#" 1127272393Shrs # Get unique vars in $rcvar $rcvars 1128272393Shrs for _v in $rcvar $rcvars; do 1129197144Shrs case $v in 1130197144Shrs $_v\ *|\ *$_v|*\ $_v\ *) ;; 1131197144Shrs *) v="${v# } $_v" ;; 1132197144Shrs esac 1133197144Shrs done 1134197144Shrs 1135197144Shrs # Display variables. 1136197144Shrs for _v in $v; do 1137197144Shrs if [ -z "$_v" ]; then 1138197144Shrs continue 113978344Sobrien fi 1140197144Shrs 1141197144Shrs eval _desc=\$${_v}_desc 1142197144Shrs eval _defval=\$${_v}_defval 1143197144Shrs _h="-" 1144197144Shrs 1145197144Shrs eval echo \"$_v=\\\"\$$_v\\\"\" 1146197144Shrs # decode multiple lines of _desc 1147197144Shrs while [ -n "$_desc" ]; do 1148197144Shrs case $_desc in 1149197144Shrs *^^*) 1150197144Shrs echo "# $_h ${_desc%%^^*}" 1151197144Shrs _desc=${_desc#*^^} 1152197144Shrs _h=" " 1153197144Shrs ;; 1154197144Shrs *) 1155197144Shrs echo "# $_h ${_desc}" 1156197144Shrs break 1157197144Shrs ;; 1158197144Shrs esac 1159197144Shrs done 1160197144Shrs echo "# (default: \"$_defval\")" 1161197144Shrs done 1162197144Shrs echo "" 116378344Sobrien ;; 116478344Sobrien 116578344Sobrien *) 1166150796Syar rc_usage $_keywords 116778344Sobrien ;; 116878344Sobrien 116978344Sobrien esac 1170116097Smtm return $_return 117178344Sobrien done 117278344Sobrien 117398186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 1174150796Syar rc_usage $_keywords 1175153152Syar # not reached 117678344Sobrien} 117778344Sobrien 117878344Sobrien# 1179165565Syar# Helper functions for run_rc_command: common code. 1180165565Syar# They use such global variables besides the exported rc_* ones: 1181165565Syar# 1182165565Syar# name R/W 1183165565Syar# ------------------ 1184165565Syar# _precmd R 1185165565Syar# _postcmd R 1186165565Syar# _return W 1187165565Syar# 1188165565Syar_run_rc_precmd() 1189165565Syar{ 1190165565Syar check_required_before "$rc_arg" || return 1 1191165565Syar 1192165565Syar if [ -n "$_precmd" ]; then 1193165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1194165565Syar eval "$_precmd $rc_extra_args" 1195165565Syar _return=$? 1196165565Syar 1197165565Syar # If precmd failed and force isn't set, request exit. 1198165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1199165565Syar return 1 1200165565Syar fi 1201165565Syar fi 1202165565Syar 1203165565Syar check_required_after "$rc_arg" || return 1 1204165565Syar 1205165565Syar return 0 1206165565Syar} 1207165565Syar 1208165565Syar_run_rc_postcmd() 1209165565Syar{ 1210165565Syar if [ -n "$_postcmd" ]; then 1211165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1212165565Syar eval "$_postcmd $rc_extra_args" 1213165565Syar _return=$? 1214165565Syar fi 1215165565Syar return 0 1216165565Syar} 1217165565Syar 1218165565Syar_run_rc_doit() 1219165565Syar{ 1220165565Syar debug "run_rc_command: doit: $*" 1221165565Syar eval "$@" 1222165565Syar _return=$? 1223165565Syar 1224165565Syar # If command failed and force isn't set, request exit. 1225165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1226165565Syar return 1 1227165565Syar fi 1228165565Syar 1229165565Syar return 0 1230165565Syar} 1231165565Syar 1232165565Syar_run_rc_notrunning() 1233165565Syar{ 1234165565Syar local _pidmsg 1235165565Syar 1236165565Syar if [ -n "$pidfile" ]; then 1237165565Syar _pidmsg=" (check $pidfile)." 1238165565Syar else 1239165565Syar _pidmsg= 1240165565Syar fi 1241165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 1242165565Syar} 1243165565Syar 1244165565Syar_run_rc_killcmd() 1245165565Syar{ 1246165565Syar local _cmd 1247165565Syar 1248165565Syar _cmd="kill -$1 $rc_pid" 1249165565Syar if [ -n "$_user" ]; then 1250165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1251165565Syar fi 1252165565Syar echo "$_cmd" 1253165565Syar} 1254165565Syar 1255165565Syar# 125678344Sobrien# run_rc_script file arg 125778344Sobrien# Start the script `file' with `arg', and correctly handle the 1258201038Sdougb# return value from the script. 1259201038Sdougb# If `file' ends with `.sh', it's sourced into the current environment 1260201038Sdougb# when $rc_fast_and_loose is set, otherwise it is run as a child process. 1261201038Sdougb# If `file' appears to be a backup or scratch file, ignore it. 1262201038Sdougb# Otherwise if it is executable run as a child process. 126378344Sobrien# 126478344Sobrienrun_rc_script() 126578344Sobrien{ 126678344Sobrien _file=$1 126778344Sobrien _arg=$2 126878344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 126978344Sobrien err 3 'USAGE: run_rc_script file arg' 127078344Sobrien fi 127178344Sobrien 127298186Sgordon unset name command command_args command_interpreter \ 127398186Sgordon extra_commands pidfile procname \ 1274272393Shrs rcvar rcvars rcvars_obsolete required_dirs required_files \ 1275197144Shrs required_vars 127698186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 127798186Sgordon 127878344Sobrien case "$_file" in 1279193118Sdougb /etc/rc.d/*.sh) # no longer allowed in the base 1280193118Sdougb warn "Ignoring old-style startup script $_file" 128178344Sobrien ;; 1282153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 128398186Sgordon warn "Ignoring scratch file $_file" 128498186Sgordon ;; 128578344Sobrien *) # run in subshell 128698186Sgordon if [ -x $_file ]; then 128798186Sgordon if [ -n "$rc_fast_and_loose" ]; then 1288146490Sschweikh set $_arg; . $_file 128998186Sgordon else 1290231888Sdelphij ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1291231888Sdelphij trap "echo Script $_file interrupted >&2 ; exit 1" 2 1292231888Sdelphij trap "echo Script $_file running >&2" 29 1293146490Sschweikh set $_arg; . $_file ) 129498186Sgordon fi 129598186Sgordon fi 129678344Sobrien ;; 129778344Sobrien esac 129878344Sobrien} 129978344Sobrien 130078344Sobrien# 1301157653Sflz# load_rc_config name 1302157653Sflz# Source in the configuration file for a given name. 130378344Sobrien# 130478344Sobrienload_rc_config() 130578344Sobrien{ 1306270698Shrs local _name _rcvar_val _var _defval _v _msg _new _d 1307157653Sflz _name=$1 1308157653Sflz if [ -z "$_name" ]; then 1309157653Sflz err 3 'USAGE: load_rc_config name' 131078344Sobrien fi 131178344Sobrien 1312219612Sdougb if ${_rc_conf_loaded:-false}; then 1313219612Sdougb : 1314219612Sdougb else 131598186Sgordon if [ -r /etc/defaults/rc.conf ]; then 131698186Sgordon debug "Sourcing /etc/defaults/rc.conf" 131798186Sgordon . /etc/defaults/rc.conf 131898186Sgordon source_rc_confs 131998186Sgordon elif [ -r /etc/rc.conf ]; then 132098186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 132198186Sgordon . /etc/rc.conf 132298186Sgordon fi 1323126285Smtm _rc_conf_loaded=true 132498186Sgordon fi 1325179872Smtm 1326270698Shrs for _d in /etc ${local_startup%*/rc.d}; do 1327270698Shrs if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1328270698Shrs debug "Sourcing ${_d}/rc.conf.d/$_name" 1329270698Shrs . ${_d}/rc.conf.d/"$_name" 1330270698Shrs elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1331270698Shrs local _rc 1332270698Shrs for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1333270698Shrs if [ -f "$_rc" ] ; then 1334270698Shrs debug "Sourcing $_rc" 1335270698Shrs . "$_rc" 1336270698Shrs fi 1337270698Shrs done 1338270698Shrs fi 1339270698Shrs done 1340270698Shrs 1341197144Shrs # Set defaults if defined. 1342272393Shrs for _var in $rcvar $rcvars; do 1343223227Sjilles eval _defval=\$${_var}_defval 1344197144Shrs if [ -n "$_defval" ]; then 1345197144Shrs eval : \${$_var:=\$${_var}_defval} 1346197144Shrs fi 1347197144Shrs done 1348197144Shrs 1349197144Shrs # check obsolete rc.conf variables 1350197144Shrs for _var in $rcvars_obsolete; do 1351223227Sjilles eval _v=\$$_var 1352223227Sjilles eval _msg=\$${_var}_obsolete_msg 1353223227Sjilles eval _new=\$${_var}_newvar 1354197144Shrs case $_v in 1355197144Shrs "") 1356197144Shrs ;; 1357197144Shrs *) 1358197144Shrs if [ -z "$_new" ]; then 1359197144Shrs _msg="Ignored." 1360197144Shrs else 1361197144Shrs eval $_new=\"\$$_var\" 1362197144Shrs if [ -z "$_msg" ]; then 1363197144Shrs _msg="Use \$$_new instead." 1364197144Shrs fi 1365197144Shrs fi 1366197144Shrs warn "\$$_var is obsolete. $_msg" 1367197144Shrs ;; 1368197144Shrs esac 1369197144Shrs done 137078344Sobrien} 1371201036Sdougb 1372157473Sflz# 1373157653Sflz# load_rc_config_var name var 1374157653Sflz# Read the rc.conf(5) var for name and set in the 1375157473Sflz# current shell, using load_rc_config in a subshell to prevent 1376157473Sflz# unwanted side effects from other variable assignments. 1377157473Sflz# 1378157473Sflzload_rc_config_var() 1379157473Sflz{ 1380157473Sflz if [ $# -ne 2 ]; then 1381157653Sflz err 3 'USAGE: load_rc_config_var name var' 1382157473Sflz fi 1383157473Sflz eval $(eval '( 1384157473Sflz load_rc_config '$1' >/dev/null; 1385157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1386157473Sflz echo '$2'=\'\''${'$2'}\'\''; 1387157473Sflz fi 1388157473Sflz )' ) 1389157473Sflz} 139078344Sobrien 139178344Sobrien# 139278344Sobrien# rc_usage commands 139378344Sobrien# Print a usage string for $0, with `commands' being a list of 139478344Sobrien# valid commands. 139578344Sobrien# 139678344Sobrienrc_usage() 139778344Sobrien{ 1398230007Srea echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 139978344Sobrien 140078344Sobrien _sep= 1401126286Smtm for _elem; do 140278344Sobrien echo -n 1>&2 "$_sep$_elem" 140378344Sobrien _sep="|" 140478344Sobrien done 140578344Sobrien echo 1>&2 ")" 140678344Sobrien exit 1 140778344Sobrien} 140878344Sobrien 140978344Sobrien# 141078344Sobrien# err exitval message 141178344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 141278344Sobrien# 141378344Sobrienerr() 141478344Sobrien{ 141578344Sobrien exitval=$1 141678344Sobrien shift 141778344Sobrien 1418106643Sgordon if [ -x /usr/bin/logger ]; then 1419106643Sgordon logger "$0: ERROR: $*" 1420106643Sgordon fi 1421106643Sgordon echo 1>&2 "$0: ERROR: $*" 142278344Sobrien exit $exitval 142378344Sobrien} 142478344Sobrien 142578344Sobrien# 142678344Sobrien# warn message 142778344Sobrien# Display message to stderr and log to the syslog. 142878344Sobrien# 142978344Sobrienwarn() 143078344Sobrien{ 1431106643Sgordon if [ -x /usr/bin/logger ]; then 1432106643Sgordon logger "$0: WARNING: $*" 1433106643Sgordon fi 1434106643Sgordon echo 1>&2 "$0: WARNING: $*" 143578344Sobrien} 143698186Sgordon 143798186Sgordon# 143898186Sgordon# info message 143998186Sgordon# Display informational message to stdout and log to syslog. 144098186Sgordon# 144198186Sgordoninfo() 144298186Sgordon{ 1443119170Smtm case ${rc_info} in 1444119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1445119170Smtm if [ -x /usr/bin/logger ]; then 1446119170Smtm logger "$0: INFO: $*" 1447119170Smtm fi 1448119170Smtm echo "$0: INFO: $*" 1449119170Smtm ;; 1450119170Smtm esac 145198186Sgordon} 145298186Sgordon 145398186Sgordon# 145498186Sgordon# debug message 1455106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 145698186Sgordon# BEWARE that you don't call any subroutine that itself calls this 145798186Sgordon# function. 145898186Sgordon# 145998186Sgordondebug() 146098186Sgordon{ 146198186Sgordon case ${rc_debug} in 146298186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1463106700Sgordon if [ -x /usr/bin/logger ]; then 1464162947Syar logger "$0: DEBUG: $*" 1465106700Sgordon fi 1466146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 146798186Sgordon ;; 146898186Sgordon esac 146998186Sgordon} 147098186Sgordon 147198186Sgordon# 147298186Sgordon# backup_file action file cur backup 147398186Sgordon# Make a backup copy of `file' into `cur', and save the previous 147498186Sgordon# version of `cur' as `backup' or use rcs for archiving. 147598186Sgordon# 147698186Sgordon# This routine checks the value of the backup_uses_rcs variable, 147798186Sgordon# which can be either YES or NO. 147898186Sgordon# 147998186Sgordon# The `action' keyword can be one of the following: 148098186Sgordon# 148198186Sgordon# add `file' is now being backed up (and is possibly 148298186Sgordon# being reentered into the backups system). `cur' 148398186Sgordon# is created and RCS files, if necessary, are 148498186Sgordon# created as well. 148598186Sgordon# 148698186Sgordon# update `file' has changed and needs to be backed up. 148798186Sgordon# If `cur' exists, it is copied to to `back' or 148898186Sgordon# checked into RCS (if the repository file is old), 148998186Sgordon# and then `file' is copied to `cur'. Another RCS 149098186Sgordon# check in done here if RCS is being used. 149198186Sgordon# 149298186Sgordon# remove `file' is no longer being tracked by the backups 149398186Sgordon# system. If RCS is not being used, `cur' is moved 149498186Sgordon# to `back', otherwise an empty file is checked in, 149598186Sgordon# and then `cur' is removed. 149698186Sgordon# 149798186Sgordon# 149898186Sgordonbackup_file() 149998186Sgordon{ 150098186Sgordon _action=$1 150198186Sgordon _file=$2 150298186Sgordon _cur=$3 150398186Sgordon _back=$4 150498186Sgordon 150598186Sgordon if checkyesno backup_uses_rcs; then 150698186Sgordon _msg0="backup archive" 150798186Sgordon _msg1="update" 150898186Sgordon 150998186Sgordon # ensure that history file is not locked 151098186Sgordon if [ -f $_cur,v ]; then 151198186Sgordon rcs -q -u -U -M $_cur 151298186Sgordon fi 151398186Sgordon 151498186Sgordon # ensure after switching to rcs that the 151598186Sgordon # current backup is not lost 151698186Sgordon if [ -f $_cur ]; then 151798186Sgordon # no archive, or current newer than archive 151898186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 151998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 152098186Sgordon rcs -q -kb -U $_cur 152198186Sgordon co -q -f -u $_cur 152298186Sgordon fi 152398186Sgordon fi 152498186Sgordon 152598186Sgordon case $_action in 152698186Sgordon add|update) 152798186Sgordon cp -p $_file $_cur 152898186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 152998186Sgordon rcs -q -kb -U $_cur 153098186Sgordon co -q -f -u $_cur 153198186Sgordon chown root:wheel $_cur $_cur,v 153298186Sgordon ;; 153398186Sgordon remove) 153498186Sgordon cp /dev/null $_cur 153598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 153698186Sgordon rcs -q -kb -U $_cur 153798186Sgordon chown root:wheel $_cur $_cur,v 153898186Sgordon rm $_cur 153998186Sgordon ;; 154098186Sgordon esac 154198186Sgordon else 154298186Sgordon case $_action in 154398186Sgordon add|update) 154498186Sgordon if [ -f $_cur ]; then 154598186Sgordon cp -p $_cur $_back 154698186Sgordon fi 154798186Sgordon cp -p $_file $_cur 154898186Sgordon chown root:wheel $_cur 154998186Sgordon ;; 155098186Sgordon remove) 155198186Sgordon mv -f $_cur $_back 155298186Sgordon ;; 155398186Sgordon esac 155498186Sgordon fi 155598186Sgordon} 1556119166Smtm 1557123344Smtm# make_symlink src link 1558123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1559123344Smtm# directory in which link is to be created does not exist 1560123344Smtm# a warning will be displayed and an error will be returned. 1561229783Suqs# Returns 0 on success, 1 otherwise. 1562119166Smtm# 1563123344Smtmmake_symlink() 1564119166Smtm{ 1565123344Smtm local src link linkdir _me 1566123344Smtm src="$1" 1567123344Smtm link="$2" 1568123344Smtm linkdir="`dirname $link`" 1569123344Smtm _me="make_symlink()" 1570119166Smtm 1571123344Smtm if [ -z "$src" -o -z "$link" ]; then 1572123344Smtm warn "$_me: requires two arguments." 1573119166Smtm return 1 1574119166Smtm fi 1575123344Smtm if [ ! -d "$linkdir" ]; then 1576160667Syar warn "$_me: the directory $linkdir does not exist." 1577119166Smtm return 1 1578119166Smtm fi 1579146490Sschweikh if ! ln -sf $src $link; then 1580123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1581119166Smtm return 1 1582119166Smtm fi 1583119166Smtm return 0 1584119166Smtm} 1585119166Smtm 1586119166Smtm# devfs_rulesets_from_file file 1587119166Smtm# Reads a set of devfs commands from file, and creates 1588119166Smtm# the specified rulesets with their rules. Returns non-zero 1589119166Smtm# if there was an error. 1590119166Smtm# 1591119166Smtmdevfs_rulesets_from_file() 1592119166Smtm{ 1593248820Savg local file _err _me _opts 1594119166Smtm file="$1" 1595119166Smtm _me="devfs_rulesets_from_file" 1596119166Smtm _err=0 1597119166Smtm 1598119166Smtm if [ -z "$file" ]; then 1599119166Smtm warn "$_me: you must specify a file" 1600119166Smtm return 1 1601119166Smtm fi 1602119166Smtm if [ ! -e "$file" ]; then 1603119166Smtm debug "$_me: no such file ($file)" 1604119166Smtm return 0 1605119166Smtm fi 1606248820Savg 1607248820Savg # Disable globbing so that the rule patterns are not expanded 1608248820Savg # by accident with matching filesystem entries. 1609248820Savg _opts=$-; set -f 1610248820Savg 1611119166Smtm debug "reading rulesets from file ($file)" 1612119166Smtm { while read line 1613119166Smtm do 1614119166Smtm case $line in 1615119166Smtm \#*) 1616119166Smtm continue 1617119166Smtm ;; 1618119166Smtm \[*\]*) 1619119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1620119166Smtm if [ -z "$rulenum" ]; then 1621119166Smtm warn "$_me: cannot extract rule number ($line)" 1622119166Smtm _err=1 1623119166Smtm break 1624119166Smtm fi 1625119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1626119166Smtm if [ -z "$rulename" ]; then 1627119166Smtm warn "$_me: cannot extract rule name ($line)" 1628119166Smtm _err=1 1629119166Smtm break; 1630119166Smtm fi 1631119166Smtm eval $rulename=\$rulenum 1632119166Smtm debug "found ruleset: $rulename=$rulenum" 1633146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1634119166Smtm _err=1 1635119166Smtm break 1636119166Smtm fi 1637119166Smtm ;; 1638119166Smtm *) 1639119166Smtm rulecmd="${line%%"\#*"}" 1640119166Smtm # evaluate the command incase it includes 1641119166Smtm # other rules 1642119166Smtm if [ -n "$rulecmd" ]; then 1643119166Smtm debug "adding rule ($rulecmd)" 1644119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1645119166Smtm then 1646119166Smtm _err=1 1647119166Smtm break 1648119166Smtm fi 1649119166Smtm fi 1650119166Smtm ;; 1651119166Smtm esac 1652119166Smtm if [ $_err -ne 0 ]; then 1653119166Smtm debug "error in $_me" 1654119166Smtm break 1655119166Smtm fi 1656119166Smtm done } < $file 1657248820Savg case $_opts in *f*) ;; *) set +f ;; esac 1658119166Smtm return $_err 1659119166Smtm} 1660119166Smtm 1661119166Smtm# devfs_init_rulesets 1662119166Smtm# Initializes rulesets from configuration files. Returns 1663119166Smtm# non-zero if there was an error. 1664119166Smtm# 1665119166Smtmdevfs_init_rulesets() 1666119166Smtm{ 1667119166Smtm local file _me 1668119166Smtm _me="devfs_init_rulesets" 1669119166Smtm 1670119166Smtm # Go through this only once 1671119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1672119166Smtm debug "$_me: devfs rulesets already initialized" 1673119166Smtm return 1674119166Smtm fi 1675146490Sschweikh for file in $devfs_rulesets; do 1676217090Sjh if ! devfs_rulesets_from_file $file; then 1677217090Sjh warn "$_me: could not read rules from $file" 1678217090Sjh return 1 1679217090Sjh fi 1680119166Smtm done 1681119166Smtm devfs_rulesets_init=1 1682119166Smtm debug "$_me: devfs rulesets initialized" 1683119166Smtm return 0 1684119166Smtm} 1685119166Smtm 1686119166Smtm# devfs_set_ruleset ruleset [dir] 1687151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1688119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1689119166Smtm# Returns non-zero if it could not set it successfully. 1690119166Smtm# 1691119166Smtmdevfs_set_ruleset() 1692119166Smtm{ 1693119166Smtm local devdir rs _me 1694119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1695119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1696119166Smtm _me="devfs_set_ruleset" 1697119166Smtm 1698119166Smtm if [ -z "$rs" ]; then 1699119166Smtm warn "$_me: you must specify a ruleset number" 1700119166Smtm return 1 1701119166Smtm fi 1702119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1703146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1704119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1705119166Smtm return 1 1706119166Smtm fi 1707119166Smtm return 0 1708119166Smtm} 1709119166Smtm 1710119166Smtm# devfs_apply_ruleset ruleset [dir] 1711119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1712119166Smtm# The ruleset argument must be a ruleset name as specified 1713119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1714119166Smtm# if it could not apply the ruleset. 1715119166Smtm# 1716119166Smtmdevfs_apply_ruleset() 1717119166Smtm{ 1718119166Smtm local devdir rs _me 1719119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1720119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1721119166Smtm _me="devfs_apply_ruleset" 1722119166Smtm 1723119166Smtm if [ -z "$rs" ]; then 1724119166Smtm warn "$_me: you must specify a ruleset" 1725119166Smtm return 1 1726119166Smtm fi 1727119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1728146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1729119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1730119166Smtm return 1 1731119166Smtm fi 1732119166Smtm return 0 1733119166Smtm} 1734119166Smtm 1735119166Smtm# devfs_domount dir [ruleset] 1736119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1737119166Smtm# on the mount-point. It must also be a ruleset name as specified 1738119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1739119166Smtm# 1740119166Smtmdevfs_domount() 1741119166Smtm{ 1742119166Smtm local devdir rs _me 1743119166Smtm devdir="$1" 1744119166Smtm [ -n "$2" ] && rs=$2 || rs= 1745119166Smtm _me="devfs_domount()" 1746119166Smtm 1747119166Smtm if [ -z "$devdir" ]; then 1748119166Smtm warn "$_me: you must specify a mount-point" 1749119166Smtm return 1 1750119166Smtm fi 1751119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1752146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1753119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1754119166Smtm return 1 1755119166Smtm fi 1756119166Smtm if [ -n "$rs" ]; then 1757119166Smtm devfs_init_rulesets 1758119166Smtm devfs_set_ruleset $rs $devdir 1759124797Scperciva devfs -m $devdir rule applyset 1760119166Smtm fi 1761119166Smtm return 0 1762119166Smtm} 1763119166Smtm 1764127345Sbrooks# Provide a function for normalizing the mounting of memory 1765127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1766127345Sbrooks# as close as possible between 5-current and 4-stable. 1767127345Sbrooks# $1 = size 1768127345Sbrooks# $2 = mount point 1769137451Skeramida# $3 = (optional) extra mdmfs flags 1770146490Sschweikhmount_md() 1771146490Sschweikh{ 1772127345Sbrooks if [ -n "$3" ]; then 1773137451Skeramida flags="$3" 1774127345Sbrooks fi 1775149421Syar /sbin/mdmfs $flags -s $1 md $2 1776127345Sbrooks} 1777131550Scperciva 1778159828Syar# Code common to scripts that need to load a kernel module 1779159828Syar# if it isn't in the kernel yet. Syntax: 1780160666Syar# load_kld [-e regex] [-m module] file 1781159828Syar# where -e or -m chooses the way to check if the module 1782159828Syar# is already loaded: 1783160666Syar# regex is egrep'd in the output from `kldstat -v', 1784160666Syar# module is passed to `kldstat -m'. 1785160666Syar# The default way is as though `-m file' were specified. 1786159828Syarload_kld() 1787159828Syar{ 1788159828Syar local _loaded _mod _opt _re 1789159828Syar 1790159828Syar while getopts "e:m:" _opt; do 1791159828Syar case "$_opt" in 1792159828Syar e) _re="$OPTARG" ;; 1793159828Syar m) _mod="$OPTARG" ;; 1794160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1795159828Syar esac 1796159828Syar done 1797159828Syar shift $(($OPTIND - 1)) 1798160666Syar if [ $# -ne 1 ]; then 1799160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1800160666Syar fi 1801159828Syar _mod=${_mod:-$1} 1802159828Syar _loaded=false 1803159828Syar if [ -n "$_re" ]; then 1804159828Syar if kldstat -v | egrep -q -e "$_re"; then 1805159828Syar _loaded=true 1806159828Syar fi 1807159828Syar else 1808159828Syar if kldstat -q -m "$_mod"; then 1809159828Syar _loaded=true 1810159828Syar fi 1811159828Syar fi 1812159828Syar if ! $_loaded; then 1813159828Syar if ! kldload "$1"; then 1814159828Syar warn "Unable to load kernel module $1" 1815159828Syar return 1 1816160666Syar else 1817160666Syar info "$1 kernel module loaded." 1818159828Syar fi 1819160666Syar else 1820160666Syar debug "load_kld: $1 kernel module already loaded." 1821159828Syar fi 1822159828Syar return 0 1823159828Syar} 1824159828Syar 1825264243Sdteske# ltr str src dst [var] 1826149049Spjd# Change every $src in $str to $dst. 1827149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1828264243Sdteske# awk(1). If var is non-NULL, set it to the result. 1829149049Spjdltr() 1830149049Spjd{ 1831264243Sdteske local _str _src _dst _out _com _var 1832264243Sdteske _str="$1" 1833264243Sdteske _src="$2" 1834264243Sdteske _dst="$3" 1835264243Sdteske _var="$4" 1836149049Spjd _out="" 1837149049Spjd 1838264243Sdteske local IFS="${_src}" 1839149049Spjd for _com in ${_str}; do 1840149049Spjd if [ -z "${_out}" ]; then 1841149049Spjd _out="${_com}" 1842149049Spjd else 1843149049Spjd _out="${_out}${_dst}${_com}" 1844149049Spjd fi 1845149049Spjd done 1846264243Sdteske if [ -n "${_var}" ]; then 1847264243Sdteske setvar "${_var}" "${_out}" 1848264243Sdteske else 1849264243Sdteske echo "${_out}" 1850264243Sdteske fi 1851149049Spjd} 1852149049Spjd 1853149050Spjd# Creates a list of providers for GELI encryption. 1854149050Spjdgeli_make_list() 1855149050Spjd{ 1856149050Spjd local devices devices2 1857149050Spjd local provider mountpoint type options rest 1858149050Spjd 1859149050Spjd # Create list of GELI providers from fstab. 1860149050Spjd while read provider mountpoint type options rest ; do 1861155570Sflz case ":${options}" in 1862155570Sflz :*noauto*) 1863155570Sflz noauto=yes 1864155570Sflz ;; 1865155570Sflz *) 1866155570Sflz noauto=no 1867155570Sflz ;; 1868155570Sflz esac 1869155570Sflz 1870149050Spjd case ":${provider}" in 1871149050Spjd :#*) 1872149050Spjd continue 1873149050Spjd ;; 1874149050Spjd *.eli) 1875149050Spjd # Skip swap devices. 1876155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1877149050Spjd continue 1878149050Spjd fi 1879149050Spjd devices="${devices} ${provider}" 1880149050Spjd ;; 1881149050Spjd esac 1882149050Spjd done < /etc/fstab 1883149050Spjd 1884149050Spjd # Append providers from geli_devices. 1885149050Spjd devices="${devices} ${geli_devices}" 1886149050Spjd 1887149050Spjd for provider in ${devices}; do 1888149050Spjd provider=${provider%.eli} 1889149050Spjd provider=${provider#/dev/} 1890149050Spjd devices2="${devices2} ${provider}" 1891149050Spjd done 1892149050Spjd 1893149050Spjd echo ${devices2} 1894149050Spjd} 1895149050Spjd 1896153027Sdougb# Find scripts in local_startup directories that use the old syntax 1897153027Sdougb# 1898238416Skevlofind_local_scripts_old() { 1899153027Sdougb zlist='' 1900153027Sdougb slist='' 1901153027Sdougb for dir in ${local_startup}; do 1902153027Sdougb if [ -d "${dir}" ]; then 1903153027Sdougb for file in ${dir}/[0-9]*.sh; do 1904153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1905153027Sdougb continue 1906153027Sdougb zlist="$zlist $file" 1907153027Sdougb done 1908227366Sjilles for file in ${dir}/[!0-9]*.sh; do 1909153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1910153027Sdougb continue 1911153027Sdougb slist="$slist $file" 1912153027Sdougb done 1913153027Sdougb fi 1914153027Sdougb done 1915153027Sdougb} 1916153027Sdougb 1917238416Skevlofind_local_scripts_new() { 1918153027Sdougb local_rc='' 1919153027Sdougb for dir in ${local_startup}; do 1920153027Sdougb if [ -d "${dir}" ]; then 1921153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1922153027Sdougb case "$file" in 1923153027Sdougb *.sample) ;; 1924153027Sdougb *) if [ -x "$file" ]; then 1925153027Sdougb local_rc="${local_rc} ${file}" 1926153027Sdougb fi 1927153027Sdougb ;; 1928153027Sdougb esac 1929153027Sdougb done 1930153027Sdougb fi 1931153027Sdougb done 1932153027Sdougb} 1933153027Sdougb 1934165565Syar# check_required_{before|after} command 1935165565Syar# Check for things required by the command before and after its precmd, 1936165565Syar# respectively. The two separate functions are needed because some 1937165565Syar# conditions should prevent precmd from being run while other things 1938165565Syar# depend on precmd having already been run. 1939165565Syar# 1940165565Syarcheck_required_before() 1941165565Syar{ 1942165565Syar local _f 1943165565Syar 1944165565Syar case "$1" in 1945165565Syar start) 1946165565Syar for _f in $required_vars; do 1947165565Syar if ! checkyesno $_f; then 1948165565Syar warn "\$${_f} is not enabled." 1949165565Syar if [ -z "$rc_force" ]; then 1950165565Syar return 1 1951165565Syar fi 1952165565Syar fi 1953165565Syar done 1954165565Syar 1955165565Syar for _f in $required_dirs; do 1956165565Syar if [ ! -d "${_f}/." ]; then 1957165565Syar warn "${_f} is not a directory." 1958165565Syar if [ -z "$rc_force" ]; then 1959165565Syar return 1 1960165565Syar fi 1961165565Syar fi 1962165565Syar done 1963165565Syar 1964165565Syar for _f in $required_files; do 1965165565Syar if [ ! -r "${_f}" ]; then 1966165565Syar warn "${_f} is not readable." 1967165565Syar if [ -z "$rc_force" ]; then 1968165565Syar return 1 1969165565Syar fi 1970165565Syar fi 1971165565Syar done 1972165565Syar ;; 1973165565Syar esac 1974165565Syar 1975165565Syar return 0 1976165565Syar} 1977165565Syar 1978165565Syarcheck_required_after() 1979165565Syar{ 1980165565Syar local _f _args 1981165565Syar 1982165565Syar case "$1" in 1983165565Syar start) 1984165565Syar for _f in $required_modules; do 1985165565Syar case "${_f}" in 1986165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1987165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1988165565Syar *) _args="${_f}" ;; 1989165565Syar esac 1990165565Syar if ! load_kld ${_args}; then 1991165565Syar if [ -z "$rc_force" ]; then 1992165565Syar return 1 1993165565Syar fi 1994165565Syar fi 1995165565Syar done 1996165565Syar ;; 1997165565Syar esac 1998165565Syar 1999165565Syar return 0 2000165565Syar} 2001165565Syar 2002271545Shrs# check_jail mib 2003271545Shrs# Return true if security.jail.$mib exists and set to 1. 2004271545Shrs 2005271545Shrscheck_jail() 2006271545Shrs{ 2007271545Shrs local _mib _v 2008271545Shrs 2009271545Shrs _mib=$1 2010271545Shrs if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 2011271545Shrs case $_v in 2012271545Shrs 1) return 0;; 2013271545Shrs esac 2014271545Shrs fi 2015271545Shrs return 1 2016271545Shrs} 2017271545Shrs 2018222996Shrs# check_kern_features mib 2019222996Shrs# Return existence of kern.features.* sysctl MIB as true or 2020222996Shrs# false. The result will be cached in $_rc_cache_kern_features_ 2021222996Shrs# namespace. "0" means the kern.features.X exists. 2022222996Shrs 2023222996Shrscheck_kern_features() 2024222996Shrs{ 2025222996Shrs local _v 2026222996Shrs 2027222996Shrs [ -n "$1" ] || return 1; 2028223292Sjilles eval _v=\$_rc_cache_kern_features_$1 2029222996Shrs [ -n "$_v" ] && return "$_v"; 2030222996Shrs 2031222996Shrs if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 2032222996Shrs eval _rc_cache_kern_features_$1=0 2033222996Shrs return 0 2034222996Shrs else 2035222996Shrs eval _rc_cache_kern_features_$1=1 2036222996Shrs return 1 2037222996Shrs fi 2038222996Shrs} 2039222996Shrs 2040243184Shrs# check_namevarlist var 2041243184Shrs# Return "0" if ${name}_var is reserved in rc.subr. 2042243184Shrs 2043243184Shrs_rc_namevarlist="program chroot chdir flags fib nice user group groups" 2044243184Shrscheck_namevarlist() 2045243184Shrs{ 2046243184Shrs local _v 2047243184Shrs 2048243184Shrs for _v in $_rc_namevarlist; do 2049243184Shrs case $1 in 2050243184Shrs $_v) return 0 ;; 2051243184Shrs esac 2052243184Shrs done 2053243184Shrs 2054243184Shrs return 1 2055243184Shrs} 2056243184Shrs 2057197144Shrs# _echoonce var msg mode 2058197144Shrs# mode=0: Echo $msg if ${$var} is empty. 2059197144Shrs# After doing echo, a string is set to ${$var}. 2060197144Shrs# 2061197144Shrs# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2062197144Shrs# 2063197144Shrs_echoonce() 2064197144Shrs{ 2065197144Shrs local _var _msg _mode 2066223227Sjilles eval _var=\$$1 2067197144Shrs _msg=$2 2068197144Shrs _mode=$3 2069197144Shrs 2070197144Shrs case $_mode in 2071197144Shrs 1) [ -n "$_var" ] && echo "$_msg" ;; 2072197144Shrs *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2073197144Shrs esac 2074197144Shrs} 2075197144Shrs 2076223298Sjillesfi # [ -z "${_rc_subr_loaded}" ] 2077223298Sjilles 2078157841Sflz_rc_subr_loaded=: 2079