rc.subr revision 298515
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 298515 2016-04-23 16:19:34Z lme $ 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}" 48295949SaraujoPROTECT="/usr/bin/protect" 49124832SmtmID="/usr/bin/id" 50124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 51161435SyarPS="/bin/ps -ww" 52161435SyarJID=`$PS -p $$ -o jid=` 5398186Sgordon 5498186Sgordon# 5578344Sobrien# functions 5678344Sobrien# --------- 5778344Sobrien 58264243Sdteske# list_vars pattern 59264243Sdteske# List vars matching pattern. 60264243Sdteske# 61264243Sdteskelist_vars() 62264243Sdteske{ 63264243Sdteske set | { while read LINE; do 64264243Sdteske var="${LINE%%=*}" 65264243Sdteske case "$var" in 66264243Sdteske "$LINE"|*[!a-zA-Z0-9_]*) continue ;; 67264243Sdteske $1) echo $var 68264243Sdteske esac 69264243Sdteske done; } 70264243Sdteske} 71264243Sdteske 72272393Shrs# set_rcvar [var] [defval] [desc] 73272393Shrs# 74272393Shrs# Echo or define a rc.conf(5) variable name. Global variable 75272393Shrs# $rcvars is used. 76272393Shrs# 77272393Shrs# If no argument is specified, echo "${name}_enable". 78272393Shrs# 79272393Shrs# If only a var is specified, echo "${var}_enable". 80272393Shrs# 81272393Shrs# If var and defval are specified, the ${var} is defined as 82272393Shrs# rc.conf(5) variable and the default value is ${defvar}. An 83272393Shrs# optional argument $desc can also be specified to add a 84272393Shrs# description for that. 85272393Shrs# 86272393Shrsset_rcvar() 87272393Shrs{ 88272393Shrs local _var 89272393Shrs 90272393Shrs case $# in 91272393Shrs 0) echo ${name}_enable ;; 92272393Shrs 1) echo ${1}_enable ;; 93272393Shrs *) 94272393Shrs debug "set_rcvar: \$$1=$2 is added" \ 95272393Shrs " as a rc.conf(5) variable." 96272393Shrs _var=$1 97272393Shrs rcvars="${rcvars# } $_var" 98272393Shrs eval ${_var}_defval=\"$2\" 99272393Shrs shift 2 100272393Shrs eval ${_var}_desc=\"$*\" 101272393Shrs ;; 102272393Shrs esac 103272393Shrs} 104272393Shrs 105197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg] 106197144Shrs# Define obsolete variable. 107197144Shrs# Global variable $rcvars_obsolete is used. 10898186Sgordon# 109197144Shrsset_rcvar_obsolete() 110197144Shrs{ 111197144Shrs local _var 112197144Shrs _var=$1 113272393Shrs debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 114197144Shrs 115197144Shrs rcvars_obsolete="${rcvars_obsolete# } $1" 116197144Shrs eval ${1}_newvar=\"$2\" 117197144Shrs shift 2 118197144Shrs eval ${_var}_obsolete_msg=\"$*\" 119197144Shrs} 120197144Shrs 121197144Shrs# 122231667Sdougb# force_depend script [rcvar] 12398186Sgordon# Force a service to start. Intended for use by services 124231667Sdougb# to resolve dependency issues. 12598186Sgordon# $1 - filename of script, in /etc/rc.d, to run 126231667Sdougb# $2 - name of the script's rcvar (minus the _enable) 12798186Sgordon# 12898186Sgordonforce_depend() 12998186Sgordon{ 130231667Sdougb local _depend _dep_rcvar 131231667Sdougb 13298186Sgordon _depend="$1" 133231667Sdougb _dep_rcvar="${2:-$1}_enable" 13498186Sgordon 135231667Sdougb [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 136231667Sdougb checkyesno $_dep_rcvar && return 0 137231667Sdougb 138231667Sdougb /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 139231667Sdougb 14098186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 141146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 14298186Sgordon warn "Unable to force ${_depend}. It may already be running." 14398186Sgordon return 1 14498186Sgordon fi 14598186Sgordon} 14698186Sgordon 14798186Sgordon# 14878344Sobrien# checkyesno var 14978344Sobrien# Test $1 variable, and warn if not set to YES or NO. 15078344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 15178344Sobrien# 15278344Sobriencheckyesno() 15378344Sobrien{ 15478344Sobrien eval _value=\$${1} 15598186Sgordon debug "checkyesno: $1 is set to $_value." 15678344Sobrien case $_value in 15778344Sobrien 15878344Sobrien # "yes", "true", "on", or "1" 15978344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 16078344Sobrien return 0 16178344Sobrien ;; 16278344Sobrien 16378344Sobrien # "no", "false", "off", or "0" 16478344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 16578344Sobrien return 1 16678344Sobrien ;; 16778344Sobrien *) 168229822Sdougb warn "\$${1} is not set properly - see rc.conf(5)." 16978344Sobrien return 1 17078344Sobrien ;; 17178344Sobrien esac 17278344Sobrien} 17378344Sobrien 174157473Sflz# 17598186Sgordon# reverse_list list 17698186Sgordon# print the list in reverse order 17778344Sobrien# 17898186Sgordonreverse_list() 17998186Sgordon{ 18098186Sgordon _revlist= 181126286Smtm for _revfile; do 18298186Sgordon _revlist="$_revfile $_revlist" 18398186Sgordon done 18498186Sgordon echo $_revlist 18598186Sgordon} 18698186Sgordon 187169668Smtm# stop_boot always 188169668Smtm# If booting directly to multiuser or $always is enabled, 189169668Smtm# send SIGTERM to the parent (/etc/rc) to abort the boot. 190169668Smtm# Otherwise just exit. 19178344Sobrien# 192169668Smtmstop_boot() 193169668Smtm{ 194169668Smtm local always 195169668Smtm 196178776Smaxim case $1 in 197178776Smaxim # "yes", "true", "on", or "1" 198178770Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 199169668Smtm always=true 200178770Smtm ;; 201178770Smtm *) 202169668Smtm always=false 203178770Smtm ;; 204178775Smaxim esac 205169668Smtm if [ "$autoboot" = yes -o "$always" = true ]; then 206169668Smtm echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 207169668Smtm kill -TERM ${RC_PID} 208169668Smtm fi 209169668Smtm exit 1 210169668Smtm} 211169668Smtm 212169668Smtm# 21398186Sgordon# mount_critical_filesystems type 21498186Sgordon# Go through the list of critical filesystems as provided in 21598186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 21698186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 21798186Sgordon# 21878344Sobrienmount_critical_filesystems() 21978344Sobrien{ 22098186Sgordon eval _fslist=\$critical_filesystems_${1} 22178344Sobrien for _fs in $_fslist; do 22278344Sobrien mount | ( 223126285Smtm _ismounted=false 22478344Sobrien while read what _on on _type type; do 22578344Sobrien if [ $on = $_fs ]; then 226126285Smtm _ismounted=true 22778344Sobrien fi 22878344Sobrien done 229126285Smtm if $_ismounted; then 230126285Smtm : 231126285Smtm else 23278344Sobrien mount $_fs >/dev/null 2>&1 23378344Sobrien fi 23498186Sgordon ) 23578344Sobrien done 23678344Sobrien} 23778344Sobrien 23878344Sobrien# 23998186Sgordon# check_pidfile pidfile procname [interpreter] 24098186Sgordon# Parses the first line of pidfile for a PID, and ensures 24178344Sobrien# that the process is running and matches procname. 24298186Sgordon# Prints the matching PID upon success, nothing otherwise. 24398186Sgordon# interpreter is optional; see _find_processes() for details. 24478344Sobrien# 24578344Sobriencheck_pidfile() 24678344Sobrien{ 24778344Sobrien _pidfile=$1 24878344Sobrien _procname=$2 24998186Sgordon _interpreter=$3 25078344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 25198186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 25278344Sobrien fi 25378344Sobrien if [ ! -f $_pidfile ]; then 254131061Smtm debug "pid file ($_pidfile): not readable." 25578344Sobrien return 25678344Sobrien fi 25778344Sobrien read _pid _junk < $_pidfile 25878344Sobrien if [ -z "$_pid" ]; then 259139949Skeramida debug "pid file ($_pidfile): no pid in file." 26078344Sobrien return 26178344Sobrien fi 26298186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 26378344Sobrien} 26478344Sobrien 26578344Sobrien# 26698186Sgordon# check_process procname [interpreter] 26778344Sobrien# Ensures that a process (or processes) named procname is running. 26898186Sgordon# Prints a list of matching PIDs. 26998186Sgordon# interpreter is optional; see _find_processes() for details. 27078344Sobrien# 27178344Sobriencheck_process() 27278344Sobrien{ 27378344Sobrien _procname=$1 27498186Sgordon _interpreter=$2 27578344Sobrien if [ -z "$_procname" ]; then 27698186Sgordon err 3 'USAGE: check_process procname [interpreter]' 27778344Sobrien fi 27898186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 27998186Sgordon} 28098186Sgordon 28198186Sgordon# 28298186Sgordon# _find_processes procname interpreter psargs 28398186Sgordon# Search for procname in the output of ps generated by psargs. 28498186Sgordon# Prints the PIDs of any matching processes, space separated. 28598186Sgordon# 28698186Sgordon# If interpreter == ".", check the following variations of procname 28798186Sgordon# against the first word of each command: 28898186Sgordon# procname 28998186Sgordon# `basename procname` 29098186Sgordon# `basename procname` + ":" 29198186Sgordon# "(" + `basename procname` + ")" 292155719Sceri# "[" + `basename procname` + "]" 29398186Sgordon# 29498186Sgordon# If interpreter != ".", read the first line of procname, remove the 29598186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 29698186Sgordon# match that against each command, either as is, or with extra words 297157841Sflz# at the end. As an alternative, to deal with interpreted daemons 298157841Sflz# using perl, the basename of the interpreter plus a colon is also 299157841Sflz# tried as the prefix to procname. 30098186Sgordon# 30198186Sgordon_find_processes() 30298186Sgordon{ 30398186Sgordon if [ $# -ne 3 ]; then 30498186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 30598186Sgordon fi 30698186Sgordon _procname=$1 30798186Sgordon _interpreter=$2 30898186Sgordon _psargs=$3 30998186Sgordon 31078344Sobrien _pref= 31198186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 312242183Screes _script="${_chroot}${_chroot:+/}$_procname" 313242183Screes if [ -r "$_script" ]; then 314170282Syar read _interp < $_script # read interpreter name 315170282Syar case "$_interp" in 316170282Syar \#!*) 317170282Syar _interp=${_interp#\#!} # strip #! 318170282Syar set -- $_interp 319170282Syar case $1 in 320170282Syar */bin/env) 321170282Syar shift # drop env to get real name 322170282Syar ;; 323170282Syar esac 324170282Syar if [ $_interpreter != $1 ]; then 325170282Syar warn "\$command_interpreter $_interpreter != $1" 326170282Syar fi 327170282Syar ;; 328170282Syar *) 329170282Syar warn "no shebang line in $_script" 330170282Syar set -- $_interpreter 331170282Syar ;; 332170282Syar esac 333170282Syar else 334170282Syar warn "cannot read shebang line from $_script" 335170282Syar set -- $_interpreter 33678344Sobrien fi 33798186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 338157841Sflz _interpbn=${1##*/} 33998186Sgordon _fp_args='_argv' 34098186Sgordon _fp_match='case "$_argv" in 341245250Ssmh ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' 34298186Sgordon else # a normal daemon 34398186Sgordon _procnamebn=${_procname##*/} 34498186Sgordon _fp_args='_arg0 _argv' 34598186Sgordon _fp_match='case "$_arg0" in 346151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 34798186Sgordon fi 34898186Sgordon 349161435Syar _proccheck="\ 350161436Syar $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 351157657Sflz while read _npid _jid '"$_fp_args"'; do 352161436Syar '"$_fp_match"' 353157657Sflz if [ "$JID" -eq "$_jid" ]; 354157657Sflz then echo -n "$_pref$_npid"; 355157657Sflz _pref=" "; 356157657Sflz fi 35798186Sgordon ;; 35898186Sgordon esac 35998186Sgordon done' 36098186Sgordon 361114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 36298186Sgordon eval $_proccheck 36398186Sgordon} 36498186Sgordon 365264243Sdteske# sort_lite [-b] [-n] [-k POS] [-t SEP] 366264243Sdteske# A lite version of sort(1) (supporting a few options) that can be used 367264243Sdteske# before the real sort(1) is available (e.g., in scripts that run prior 368264243Sdteske# to mountcritremote). Requires only shell built-in functionality. 36998186Sgordon# 370264243Sdteskesort_lite() 371264243Sdteske{ 372264243Sdteske local funcname=sort_lite 373264243Sdteske local sort_sep="$IFS" sort_ignore_leading_space= 374264243Sdteske local sort_field=0 sort_strict_fields= sort_numeric= 375264243Sdteske local nitems=0 skip_leading=0 trim= 376264243Sdteske 377264243Sdteske local OPTIND flag 378264243Sdteske while getopts bnk:t: flag; do 379264243Sdteske case "$flag" in 380264243Sdteske b) sort_ignore_leading_space=1 ;; 381264243Sdteske n) sort_numeric=1 sort_ignore_leading_space=1 ;; 382264243Sdteske k) sort_field="${OPTARG%%,*}" ;; # only up to first comma 383264243Sdteske # NB: Unlike sort(1) only one POS allowed 384264243Sdteske t) sort_sep="$OPTARG" 385264243Sdteske if [ ${#sort_sep} -gt 1 ]; then 386264243Sdteske echo "$funcname: multi-character tab \`$sort_sep'" >&2 387264243Sdteske return 1 388264243Sdteske fi 389264243Sdteske sort_strict_fields=1 390264243Sdteske ;; 391264243Sdteske \?) return 1 ;; 392264243Sdteske esac 393264243Sdteske done 394264243Sdteske shift $(( $OPTIND - 1 )) 395264243Sdteske 396264243Sdteske # Create transformation pattern to trim leading text if desired 397264243Sdteske case "$sort_field" in 398264243Sdteske ""|[!0-9]*|*[!0-9.]*) 399264243Sdteske echo "$funcname: invalid sort field \`$sort_field'" >&2 400264243Sdteske return 1 401264243Sdteske ;; 402264243Sdteske *.*) 403264243Sdteske skip_leading=${sort_field#*.} sort_field=${sort_field%%.*} 404264243Sdteske while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do 405264243Sdteske trim="$trim?" skip_leading=$(( $skip_leading - 1 )) 406264243Sdteske done 407264243Sdteske esac 408264243Sdteske 409264243Sdteske # Copy input to series of local numbered variables 410264243Sdteske # NB: IFS of NULL preserves leading whitespace 411264243Sdteske local LINE 412264243Sdteske while IFS= read -r LINE || [ "$LINE" ]; do 413264243Sdteske nitems=$(( $nitems + 1 )) 414264243Sdteske local src_$nitems="$LINE" 415264243Sdteske done 416264243Sdteske 417264243Sdteske # 418264243Sdteske # Sort numbered locals using insertion sort 419264243Sdteske # 420264243Sdteske local curitem curitem_orig curitem_mod curitem_haskey 421264243Sdteske local dest dest_orig dest_mod dest_haskey 422264243Sdteske local d gt n 423264243Sdteske local i=1 424264243Sdteske while [ $i -le $nitems ]; do 425264243Sdteske curitem_haskey=1 # Assume sort field (-k POS) exists 426264243Sdteske eval curitem=\"\$src_$i\" 427264243Sdteske curitem_mod="$curitem" # for modified comparison 428264243Sdteske curitem_orig="$curitem" # for original comparison 429264243Sdteske 430264243Sdteske # Trim leading whitespace if desired 431264243Sdteske if [ "$sort_ignore_leading_space" ]; then 432264243Sdteske while case "$curitem_orig" in 433264243Sdteske [$IFS]*) : ;; *) false; esac 434264243Sdteske do 435264243Sdteske curitem_orig="${curitem_orig#?}" 436264243Sdteske done 437264243Sdteske curitem_mod="$curitem_orig" 438264243Sdteske fi 439264243Sdteske 440264243Sdteske # Shift modified comparison value if sort field (-k POS) is > 1 441264243Sdteske n=$sort_field 442264243Sdteske while [ $n -gt 1 ]; do 443264243Sdteske case "$curitem_mod" in 444264243Sdteske *[$sort_sep]*) 445264243Sdteske # Cut text up-to (and incl.) first separator 446264243Sdteske curitem_mod="${curitem_mod#*[$sort_sep]}" 447264243Sdteske 448264243Sdteske # Skip NULLs unless strict field splitting 449264243Sdteske [ "$sort_strict_fields" ] || 450264243Sdteske [ "${curitem_mod%%[$sort_sep]*}" ] || 451264243Sdteske [ $n -eq 2 ] || 452264243Sdteske continue 453264243Sdteske ;; 454264243Sdteske *) 455264243Sdteske # Asked for a field that doesn't exist 456264243Sdteske curitem_haskey= break 457264243Sdteske esac 458264243Sdteske n=$(( $n - 1 )) 459264243Sdteske done 460264243Sdteske 461264243Sdteske # Trim trailing words if sort field >= 1 462264243Sdteske [ $sort_field -ge 1 -a "$sort_numeric" ] && 463264243Sdteske curitem_mod="${curitem_mod%%[$sort_sep]*}" 464264243Sdteske 465264243Sdteske # Apply optional trim (-k POS.TRIM) to cut leading characters 466264243Sdteske curitem_mod="${curitem_mod#$trim}" 467264243Sdteske 468264243Sdteske # Determine the type of modified comparison to use initially 469264243Sdteske # NB: Prefer numerical if requested but fallback to standard 470264243Sdteske case "$curitem_mod" in 471264243Sdteske ""|[!0-9]*) # NULL or begins with non-number 472264243Sdteske gt=">" 473264243Sdteske [ "$sort_numeric" ] && curitem_mod=0 474264243Sdteske ;; 475264243Sdteske *) 476264243Sdteske if [ "$sort_numeric" ]; then 477264243Sdteske gt="-gt" 478264243Sdteske curitem_mod="${curitem_mod%%[!0-9]*}" 479264243Sdteske # NB: trailing non-digits removed 480264243Sdteske # otherwise numeric comparison fails 481264243Sdteske else 482264243Sdteske gt=">" 483264243Sdteske fi 484264243Sdteske esac 485264243Sdteske 486264243Sdteske # If first time through, short-circuit below position-search 487264243Sdteske if [ $i -le 1 ]; then 488264243Sdteske d=0 489264243Sdteske else 490264243Sdteske d=1 491264243Sdteske fi 492264243Sdteske 493264243Sdteske # 494264243Sdteske # Find appropriate element position 495264243Sdteske # 496264243Sdteske while [ $d -gt 0 ] 497264243Sdteske do 498264243Sdteske dest_haskey=$curitem_haskey 499264243Sdteske eval dest=\"\$dest_$d\" 500264243Sdteske dest_mod="$dest" # for modified comparison 501264243Sdteske dest_orig="$dest" # for original comparison 502264243Sdteske 503264243Sdteske # Trim leading whitespace if desired 504264243Sdteske if [ "$sort_ignore_leading_space" ]; then 505264243Sdteske while case "$dest_orig" in 506264243Sdteske [$IFS]*) : ;; *) false; esac 507264243Sdteske do 508264243Sdteske dest_orig="${dest_orig#?}" 509264243Sdteske done 510264243Sdteske dest_mod="$dest_orig" 511264243Sdteske fi 512264243Sdteske 513264243Sdteske # Shift modified value if sort field (-k POS) is > 1 514264243Sdteske n=$sort_field 515264243Sdteske while [ $n -gt 1 ]; do 516264243Sdteske case "$dest_mod" in 517264243Sdteske *[$sort_sep]*) 518264243Sdteske # Cut text up-to (and incl.) 1st sep 519264243Sdteske dest_mod="${dest_mod#*[$sort_sep]}" 520264243Sdteske 521264243Sdteske # Skip NULLs unless strict fields 522264243Sdteske [ "$sort_strict_fields" ] || 523264243Sdteske [ "${dest_mod%%[$sort_sep]*}" ] || 524264243Sdteske [ $n -eq 2 ] || 525264243Sdteske continue 526264243Sdteske ;; 527264243Sdteske *) 528264243Sdteske # Asked for a field that doesn't exist 529264243Sdteske dest_haskey= break 530264243Sdteske esac 531264243Sdteske n=$(( $n - 1 )) 532264243Sdteske done 533264243Sdteske 534264243Sdteske # Trim trailing words if sort field >= 1 535264243Sdteske [ $sort_field -ge 1 -a "$sort_numeric" ] && 536264243Sdteske dest_mod="${dest_mod%%[$sort_sep]*}" 537264243Sdteske 538264243Sdteske # Apply optional trim (-k POS.TRIM), cut leading chars 539264243Sdteske dest_mod="${dest_mod#$trim}" 540264243Sdteske 541264243Sdteske # Determine type of modified comparison to use 542264243Sdteske # NB: Prefer numerical if requested, fallback to std 543264243Sdteske case "$dest_mod" in 544264243Sdteske ""|[!0-9]*) # NULL or begins with non-number 545264243Sdteske gt=">" 546264243Sdteske [ "$sort_numeric" ] && dest_mod=0 547264243Sdteske ;; 548264243Sdteske *) 549264243Sdteske if [ "$sort_numeric" ]; then 550264243Sdteske gt="-gt" 551264243Sdteske dest_mod="${dest_mod%%[!0-9]*}" 552264243Sdteske # NB: kill trailing non-digits 553264243Sdteske # for numeric comparison safety 554264243Sdteske else 555264243Sdteske gt=">" 556264243Sdteske fi 557264243Sdteske esac 558264243Sdteske 559264243Sdteske # Break if we've found the proper element position 560264243Sdteske if [ "$curitem_haskey" -a "$dest_haskey" ]; then 561264243Sdteske if [ "$dest_mod" = "$curitem_mod" ]; then 562264243Sdteske [ "$dest_orig" ">" "$curitem_orig" ] && 563264243Sdteske break 564264243Sdteske elif [ "$dest_mod" $gt "$curitem_mod" ] \ 565264243Sdteske 2> /dev/null 566264243Sdteske then 567264243Sdteske break 568264243Sdteske fi 569264243Sdteske else 570264243Sdteske [ "$dest_orig" ">" "$curitem_orig" ] && break 571264243Sdteske fi 572264243Sdteske 573264243Sdteske # Break if we've hit the end 574264243Sdteske [ $d -ge $i ] && break 575264243Sdteske 576264243Sdteske d=$(( $d + 1 )) 577264243Sdteske done 578264243Sdteske 579264243Sdteske # Shift remaining positions forward, making room for new item 580264243Sdteske n=$i 581264243Sdteske while [ $n -ge $d ]; do 582264243Sdteske # Shift destination item forward one placement 583264243Sdteske eval dest_$(( $n + 1 ))=\"\$dest_$n\" 584264243Sdteske n=$(( $n - 1 )) 585264243Sdteske done 586264243Sdteske 587264243Sdteske # Place the element 588264243Sdteske if [ $i -eq 1 ]; then 589264243Sdteske local dest_1="$curitem" 590264243Sdteske else 591264243Sdteske local dest_$d="$curitem" 592264243Sdteske fi 593264243Sdteske 594264243Sdteske i=$(( $i + 1 )) 595264243Sdteske done 596264243Sdteske 597264243Sdteske # Print sorted results 598264243Sdteske d=1 599264243Sdteske while [ $d -le $nitems ]; do 600264243Sdteske eval echo \"\$dest_$d\" 601264243Sdteske d=$(( $d + 1 )) 602264243Sdteske done 603264243Sdteske} 604264243Sdteske 605264243Sdteske# 60698186Sgordon# wait_for_pids pid [pid ...] 60798186Sgordon# spins until none of the pids exist 60898186Sgordon# 60998186Sgordonwait_for_pids() 61098186Sgordon{ 611206248Sdougb local _list _prefix _nlist _j 612206248Sdougb 613126286Smtm _list="$@" 61498186Sgordon if [ -z "$_list" ]; then 61598186Sgordon return 61698186Sgordon fi 61798186Sgordon _prefix= 61898186Sgordon while true; do 61998186Sgordon _nlist=""; 62098186Sgordon for _j in $_list; do 62198186Sgordon if kill -0 $_j 2>/dev/null; then 62298186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 623206248Sdougb [ -n "$_prefix" ] && sleep 1 62498186Sgordon fi 62598186Sgordon done 62698186Sgordon if [ -z "$_nlist" ]; then 62798186Sgordon break 62878344Sobrien fi 62998186Sgordon _list=$_nlist 63098186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 63198186Sgordon _prefix=", " 632206248Sdougb pwait $_list 2>/dev/null 63378344Sobrien done 63498186Sgordon if [ -n "$_prefix" ]; then 63598186Sgordon echo "." 63698186Sgordon fi 63778344Sobrien} 63878344Sobrien 63978344Sobrien# 640220962Sdougb# get_pidfile_from_conf string file 641220962Sdougb# 642220962Sdougb# Takes a string to search for in the specified file. 643220962Sdougb# Ignores lines with traditional comment characters. 644220962Sdougb# 645220962Sdougb# Example: 646220962Sdougb# 647220962Sdougb# if get_pidfile_from_conf string file; then 648220962Sdougb# pidfile="$_pidfile_from_conf" 649220962Sdougb# else 650220962Sdougb# pidfile='appropriate default' 651220962Sdougb# fi 652220962Sdougb# 653220962Sdougbget_pidfile_from_conf() 654220962Sdougb{ 655220963Sdougb if [ -z "$1" -o -z "$2" ]; then 656220963Sdougb err 3 "USAGE: get_pidfile_from_conf string file ($name)" 657220963Sdougb fi 658220963Sdougb 659220962Sdougb local string file line 660220962Sdougb 661220962Sdougb string="$1" ; file="$2" 662220962Sdougb 663220963Sdougb if [ ! -s "$file" ]; then 664220963Sdougb err 3 "get_pidfile_from_conf: $file does not exist ($name)" 665220962Sdougb fi 666220962Sdougb 667220962Sdougb while read line; do 668220962Sdougb case "$line" in 669220962Sdougb *[#\;]*${string}*) continue ;; 670220962Sdougb *${string}*) break ;; 671220962Sdougb esac 672220962Sdougb done < $file 673220962Sdougb 674220962Sdougb if [ -n "$line" ]; then 675220962Sdougb line=${line#*/} 676220962Sdougb _pidfile_from_conf="/${line%%[\"\;]*}" 677220962Sdougb else 678220962Sdougb return 1 679220962Sdougb fi 680220962Sdougb} 681220962Sdougb 682220962Sdougb# 683197947Sdougb# check_startmsgs 684197947Sdougb# If rc_quiet is set (usually as a result of using faststart at 685197947Sdougb# boot time) check if rc_startmsgs is enabled. 686197947Sdougb# 687197947Sdougbcheck_startmsgs() 688197947Sdougb{ 689197947Sdougb if [ -n "$rc_quiet" ]; then 690197947Sdougb checkyesno rc_startmsgs 691197947Sdougb else 692197947Sdougb return 0 693197947Sdougb fi 694197947Sdougb} 695197947Sdougb 696197947Sdougb# 69798186Sgordon# run_rc_command argument 69898186Sgordon# Search for argument in the list of supported commands, which is: 69998186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 70098186Sgordon# If there's a match, run ${argument}_cmd or the default method 70198186Sgordon# (see below). 70278344Sobrien# 70398186Sgordon# If argument has a given prefix, then change the operation as follows: 70498186Sgordon# Prefix Operation 70578344Sobrien# ------ --------- 706175676Smtm# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 70798186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 708126303Smtm# one Set ${rcvar} to YES 709175676Smtm# quiet Don't output some diagnostics, and set rc_quiet=yes 71078344Sobrien# 71178344Sobrien# The following globals are used: 71278344Sobrien# 71398186Sgordon# Name Needed Purpose 71498186Sgordon# ---- ------ ------- 71578344Sobrien# name y Name of script. 71678344Sobrien# 71778344Sobrien# command n Full path to command. 71898186Sgordon# Not needed if ${rc_arg}_cmd is set for 71978344Sobrien# each keyword. 72078344Sobrien# 72178344Sobrien# command_args n Optional args/shell directives for command. 72278344Sobrien# 72398186Sgordon# command_interpreter n If not empty, command is interpreted, so 72498186Sgordon# call check_{pidfile,process}() appropriately. 72598186Sgordon# 726197144Shrs# desc n Description of script. 727197144Shrs# 72878344Sobrien# extra_commands n List of extra commands supported. 72978344Sobrien# 73098186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 73198186Sgordon# otherwise use check_process $command. 73298186Sgordon# In either case, only check if $command is set. 73378344Sobrien# 73498186Sgordon# procname n Process name to check for instead of $command. 73598186Sgordon# 73678344Sobrien# rcvar n This is checked with checkyesno to determine 73778344Sobrien# if the action should be run. 73878344Sobrien# 739157653Sflz# ${name}_program n Full path to command. 740157653Sflz# Meant to be used in /etc/rc.conf to override 741157653Sflz# ${command}. 742157653Sflz# 74378344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 74498186Sgordon# Requires /usr to be mounted. 74578344Sobrien# 74678344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 74778344Sobrien# (if not using ${name}_chroot). 74878344Sobrien# 74978344Sobrien# ${name}_flags n Arguments to call ${command} with. 75078344Sobrien# NOTE: $flags from the parent environment 75178344Sobrien# can be used to override this. 75278344Sobrien# 753272974Shrs# ${name}_env n Environment variables to run ${command} with. 754272974Shrs# 755242184Shrs# ${name}_fib n Routing table number to run ${command} with. 756242184Shrs# 75778344Sobrien# ${name}_nice n Nice level to run ${command} at. 75878344Sobrien# 759295949Saraujo# ${name}_oomprotect n Don't kill ${command} when swap space is exhausted. 760295949Saraujo# 76178344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 76278344Sobrien# using ${name}_chroot. 76398186Sgordon# Requires /usr to be mounted. 76478344Sobrien# 76578344Sobrien# ${name}_group n Group to run chrooted ${command} as. 76698186Sgordon# Requires /usr to be mounted. 76778344Sobrien# 76898186Sgordon# ${name}_groups n Comma separated list of supplementary groups 76998186Sgordon# to run the chrooted ${command} with. 77098186Sgordon# Requires /usr to be mounted. 77178344Sobrien# 772272974Shrs# ${name}_prepend n Command added before ${command}. 773272974Shrs# 774288291Sadrian# ${name}_login_class n Login class to use, else "daemon". 775288291Sadrian# 77698186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 77778344Sobrien# Otherwise, use default command (see below) 77878344Sobrien# 77998186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 78098186Sgordon# ${rc_arg}_cmd method in the default 78198186Sgordon# operation (i.e, after checking for required 78298186Sgordon# bits and process (non)existence). 78378344Sobrien# If this completes with a non-zero exit code, 78498186Sgordon# don't run ${rc_arg}_cmd. 78578344Sobrien# 78698186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 78798186Sgordon# ${rc_arg}_cmd method, if that method 78898186Sgordon# returned a zero exit code. 78998186Sgordon# 79078344Sobrien# required_dirs n If set, check for the existence of the given 791165565Syar# directories before running a (re)start command. 79278344Sobrien# 79378344Sobrien# required_files n If set, check for the readability of the given 794165565Syar# files before running a (re)start command. 79578344Sobrien# 796165565Syar# required_modules n If set, ensure the given kernel modules are 797165565Syar# loaded before running a (re)start command. 798165565Syar# The check and possible loads are actually 799165565Syar# done after start_precmd so that the modules 800165565Syar# aren't loaded in vain, should the precmd 801165565Syar# return a non-zero status to indicate a error. 802165565Syar# If a word in the list looks like "foo:bar", 803165565Syar# "foo" is the KLD file name and "bar" is the 804165565Syar# module name. If a word looks like "foo~bar", 805165565Syar# "foo" is the KLD file name and "bar" is a 806165565Syar# egrep(1) pattern matching the module name. 807165565Syar# Otherwise the module name is assumed to be 808165565Syar# the same as the KLD file name, which is most 809165565Syar# common. See load_kld(). 810165565Syar# 81178344Sobrien# required_vars n If set, perform checkyesno on each of the 81278344Sobrien# listed variables before running the default 81378344Sobrien# (re)start command. 81478344Sobrien# 81598186Sgordon# Default behaviour for a given argument, if no override method is 81698186Sgordon# provided: 81778344Sobrien# 81898186Sgordon# Argument Default behaviour 81998186Sgordon# -------- ----------------- 82078344Sobrien# start if !running && checkyesno ${rcvar} 82178344Sobrien# ${command} 82278344Sobrien# 82378344Sobrien# stop if ${pidfile} 82498186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 82578344Sobrien# else 82698186Sgordon# rc_pid=$(check_process $command) 82798186Sgordon# kill $sig_stop $rc_pid 82898186Sgordon# wait_for_pids $rc_pid 82998186Sgordon# ($sig_stop defaults to TERM.) 83078344Sobrien# 83198186Sgordon# reload Similar to stop, except use $sig_reload instead, 83298186Sgordon# and doesn't wait_for_pids. 83378344Sobrien# $sig_reload defaults to HUP. 834151685Syar# Note that `reload' isn't provided by default, 835151685Syar# it should be enabled via $extra_commands. 83678344Sobrien# 83778344Sobrien# restart Run `stop' then `start'. 83878344Sobrien# 83998186Sgordon# status Show if ${command} is running, etc. 84078344Sobrien# 84198186Sgordon# poll Wait for ${command} to exit. 84298186Sgordon# 84398186Sgordon# rcvar Display what rc.conf variable is used (if any). 84498186Sgordon# 845255809Sdes# enabled Return true if the service is enabled. 846255809Sdes# 847298515Slme# describe Show the service's description 848298515Slme# 849298515Slme# extracommands Show the service's extra commands 850298515Slme# 85198186Sgordon# Variables available to methods, and after run_rc_command() has 85298186Sgordon# completed: 85398186Sgordon# 85498186Sgordon# Variable Purpose 85598186Sgordon# -------- ------- 856126303Smtm# rc_arg Argument to command, after fast/force/one processing 85798186Sgordon# performed 85898186Sgordon# 85998186Sgordon# rc_flags Flags to start the default command with. 86098186Sgordon# Defaults to ${name}_flags, unless overridden 86198186Sgordon# by $flags from the environment. 86298186Sgordon# This variable may be changed by the precmd method. 86398186Sgordon# 86498186Sgordon# rc_pid PID of command (if appropriate) 86598186Sgordon# 86698186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 86798186Sgordon# 86898186Sgordon# rc_force Not empty if "force" was provided (q.v.) 86998186Sgordon# 870175676Smtm# rc_quiet Not empty if "quiet" was provided 87198186Sgordon# 872175676Smtm# 87378344Sobrienrun_rc_command() 87478344Sobrien{ 875116097Smtm _return=0 87698186Sgordon rc_arg=$1 87778344Sobrien if [ -z "$name" ]; then 87898186Sgordon err 3 'run_rc_command: $name is not set.' 87978344Sobrien fi 88078344Sobrien 881132892Smtm # Don't repeat the first argument when passing additional command- 882132892Smtm # line arguments to the command subroutines. 883132892Smtm # 884132892Smtm shift 1 885132892Smtm rc_extra_args="$*" 886132892Smtm 887126303Smtm _rc_prefix= 88898186Sgordon case "$rc_arg" in 88978344Sobrien fast*) # "fast" prefix; don't check pid 89098186Sgordon rc_arg=${rc_arg#fast} 89198186Sgordon rc_fast=yes 892175676Smtm rc_quiet=yes 89378344Sobrien ;; 894198216Sed force*) # "force" prefix; always run 89598186Sgordon rc_force=yes 896126303Smtm _rc_prefix=force 897126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 89878344Sobrien if [ -n "${rcvar}" ]; then 89978344Sobrien eval ${rcvar}=YES 90078344Sobrien fi 90178344Sobrien ;; 902126303Smtm one*) # "one" prefix; set ${rcvar}=yes 903126303Smtm _rc_prefix=one 904126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 905126303Smtm if [ -n "${rcvar}" ]; then 906126303Smtm eval ${rcvar}=YES 907126303Smtm fi 908126303Smtm ;; 909175676Smtm quiet*) # "quiet" prefix; omit some messages 910175676Smtm _rc_prefix=quiet 911175676Smtm rc_arg=${rc_arg#${_rc_prefix}} 912175676Smtm rc_quiet=yes 913175676Smtm ;; 91478344Sobrien esac 91578344Sobrien 916161530Sflz eval _override_command=\$${name}_program 917198162Sdougb command=${_override_command:-$command} 918161530Sflz 919298515Slme _keywords="start stop restart rcvar enabled describe extracommands $extra_commands" 92098186Sgordon rc_pid= 92178344Sobrien _pidcmd= 92298186Sgordon _procname=${procname:-${command}} 92398186Sgordon 924131135Smtm # setup pid check command 925131135Smtm if [ -n "$_procname" ]; then 92678344Sobrien if [ -n "$pidfile" ]; then 92798186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 92898186Sgordon else 92998186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 93078344Sobrien fi 93178344Sobrien if [ -n "$_pidcmd" ]; then 93298186Sgordon _keywords="${_keywords} status poll" 93378344Sobrien fi 93478344Sobrien fi 93578344Sobrien 93698186Sgordon if [ -z "$rc_arg" ]; then 937150796Syar rc_usage $_keywords 93878344Sobrien fi 93978344Sobrien 940255809Sdes if [ "$rc_arg" = "enabled" ] ; then 941255809Sdes checkyesno ${rcvar} 942255809Sdes return $? 943255809Sdes fi 944255809Sdes 94578344Sobrien if [ -n "$flags" ]; then # allow override from environment 94698186Sgordon rc_flags=$flags 94778344Sobrien else 94898186Sgordon eval rc_flags=\$${name}_flags 94978344Sobrien fi 95098186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 95198186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 952242184Shrs _group=\$${name}_group _groups=\$${name}_groups \ 953272974Shrs _fib=\$${name}_fib _env=\$${name}_env \ 954295949Saraujo _prepend=\$${name}_prepend _login_class=\${${name}_login_class:-daemon} \ 955295949Saraujo _oomprotect=\$${name}_oomprotect 95678344Sobrien 95798186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 958124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 95998186Sgordon unset _user 96098186Sgordon fi 96198186Sgordon fi 96298186Sgordon 963230374Sdougb [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 964179870Smtm 965179870Smtm for _elem in $_keywords; do 966179870Smtm if [ "$_elem" != "$rc_arg" ]; then 967179870Smtm continue 968179870Smtm fi 969298515Slme # if ${rcvar} is set, $1 is not "rcvar" and not "describe" 970206686Sdougb # and ${rc_pid} is not set, then run 97178344Sobrien # checkyesno ${rcvar} 97278344Sobrien # and return if that failed 97378344Sobrien # 974298515Slme if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \ 975298515Slme -a "$rc_arg" != "describe" ] || 976220760Sdougb [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 977179870Smtm if ! checkyesno ${rcvar}; then 978179870Smtm if [ -n "${rc_quiet}" ]; then 979179870Smtm return 0 980179870Smtm fi 981179870Smtm echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 982179870Smtm echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 983179870Smtm echo "instead of '${rc_arg}'." 984175676Smtm return 0 985175676Smtm fi 98678344Sobrien fi 98778344Sobrien 988291770Sjilles if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then 989291770Sjilles if [ -z "$rc_quiet" ]; then 990291770Sjilles echo 1>&2 "${name} already running? " \ 991291770Sjilles "(pid=$rc_pid)." 992291770Sjilles fi 993291770Sjilles return 1 994291770Sjilles fi 995291770Sjilles 99678344Sobrien # if there's a custom ${XXX_cmd}, 99778344Sobrien # run that instead of the default 99878344Sobrien # 999165565Syar eval _cmd=\$${rc_arg}_cmd \ 1000165565Syar _precmd=\$${rc_arg}_precmd \ 1001165565Syar _postcmd=\$${rc_arg}_postcmd 1002165565Syar 100378344Sobrien if [ -n "$_cmd" ]; then 1004165565Syar _run_rc_precmd || return 1 1005165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 1006165565Syar _run_rc_postcmd 1007116097Smtm return $_return 100878344Sobrien fi 100978344Sobrien 101098186Sgordon case "$rc_arg" in # default operations... 101178344Sobrien 1012298515Slme describe) 1013298515Slme if [ -n "$desc" ]; then 1014298515Slme echo "$desc" 1015298515Slme fi 1016298515Slme ;; 1017298515Slme 1018298515Slme extracommands) 1019298515Slme echo "$extra_commands" 1020298515Slme ;; 1021298515Slme 102278344Sobrien status) 1023165565Syar _run_rc_precmd || return 1 102498186Sgordon if [ -n "$rc_pid" ]; then 102598186Sgordon echo "${name} is running as pid $rc_pid." 102678344Sobrien else 102778344Sobrien echo "${name} is not running." 102878344Sobrien return 1 102978344Sobrien fi 1030165565Syar _run_rc_postcmd 103178344Sobrien ;; 103278344Sobrien 103378344Sobrien start) 1034242183Screes if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 1035160667Syar warn "run_rc_command: cannot run $command" 1036153152Syar return 1 103778344Sobrien fi 103878344Sobrien 1039179946Smtm if ! _run_rc_precmd; then 1040179946Smtm warn "failed precmd routine for ${name}" 1041179946Smtm return 1 1042179946Smtm fi 104378344Sobrien 1044160668Syar # setup the full command to run 104578344Sobrien # 1046197947Sdougb check_startmsgs && echo "Starting ${name}." 104778344Sobrien if [ -n "$_chroot" ]; then 104878344Sobrien _doit="\ 104978344Sobrien${_nice:+nice -n $_nice }\ 1050242184Shrs${_fib:+setfib -F $_fib }\ 1051272974Shrs${_env:+env $_env }\ 105278344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 105398186Sgordon$_chroot $command $rc_flags $command_args" 105478344Sobrien else 105578344Sobrien _doit="\ 1056161396Syar${_chdir:+cd $_chdir && }\ 1057242184Shrs${_fib:+setfib -F $_fib }\ 1058272974Shrs${_env:+env $_env }\ 105998186Sgordon$command $rc_flags $command_args" 106098186Sgordon if [ -n "$_user" ]; then 106198186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 106298186Sgordon fi 1063161396Syar if [ -n "$_nice" ]; then 1064161396Syar if [ -z "$_user" ]; then 1065161396Syar _doit="sh -c \"$_doit\"" 1066201036Sdougb fi 1067161396Syar _doit="nice -n $_nice $_doit" 1068161396Syar fi 1069272974Shrs if [ -n "$_prepend" ]; then 1070272974Shrs _doit="$_prepend $_doit" 1071272974Shrs fi 107278344Sobrien fi 107398186Sgordon 1074288291Sadrian # Prepend default limits 1075288291Sadrian _doit="limits -C $_login_class $_doit" 1076288291Sadrian 1077165565Syar # run the full command 107898186Sgordon # 1079179946Smtm if ! _run_rc_doit "$_doit"; then 1080179946Smtm warn "failed to start ${name}" 1081179946Smtm return 1 1082179946Smtm fi 108398186Sgordon 108498186Sgordon # finally, run postcmd 108598186Sgordon # 1086165565Syar _run_rc_postcmd 108778344Sobrien ;; 108878344Sobrien 108978344Sobrien stop) 109098186Sgordon if [ -z "$rc_pid" ]; then 1091153152Syar [ -n "$rc_fast" ] && return 0 1092165565Syar _run_rc_notrunning 1093153152Syar return 1 109478344Sobrien fi 109578344Sobrien 1096165565Syar _run_rc_precmd || return 1 109798186Sgordon 109898186Sgordon # send the signal to stop 109998186Sgordon # 110078344Sobrien echo "Stopping ${name}." 1101165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1102165565Syar _run_rc_doit "$_doit" || return 1 110398186Sgordon 110498186Sgordon # wait for the command to exit, 110598186Sgordon # and run postcmd. 110698186Sgordon wait_for_pids $rc_pid 1107165565Syar 1108165565Syar _run_rc_postcmd 110978344Sobrien ;; 111078344Sobrien 111178344Sobrien reload) 111298186Sgordon if [ -z "$rc_pid" ]; then 1113165565Syar _run_rc_notrunning 1114153152Syar return 1 111578344Sobrien fi 1116165565Syar 1117165565Syar _run_rc_precmd || return 1 1118165565Syar 1119165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1120165565Syar _run_rc_doit "$_doit" || return 1 1121165565Syar 1122165565Syar _run_rc_postcmd 112378344Sobrien ;; 112478344Sobrien 112578344Sobrien restart) 112678344Sobrien # prevent restart being called more 112778344Sobrien # than once by any given script 112878344Sobrien # 1129126285Smtm if ${_rc_restart_done:-false}; then 113078344Sobrien return 0 113178344Sobrien fi 1132126285Smtm _rc_restart_done=true 113378344Sobrien 1134165565Syar _run_rc_precmd || return 1 1135165565Syar 1136165565Syar # run those in a subshell to keep global variables 1137152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1138165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1139165565Syar _return=$? 1140165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 114198186Sgordon 1142165565Syar _run_rc_postcmd 114378344Sobrien ;; 114478344Sobrien 114598186Sgordon poll) 1146165565Syar _run_rc_precmd || return 1 114798186Sgordon if [ -n "$rc_pid" ]; then 114898186Sgordon wait_for_pids $rc_pid 114998186Sgordon fi 1150165565Syar _run_rc_postcmd 115198186Sgordon ;; 115298186Sgordon 115378344Sobrien rcvar) 1154197144Shrs echo -n "# $name" 1155197144Shrs if [ -n "$desc" ]; then 1156197144Shrs echo " : $desc" 1157197144Shrs else 1158197144Shrs echo "" 1159197144Shrs fi 1160197144Shrs echo "#" 1161272393Shrs # Get unique vars in $rcvar $rcvars 1162272393Shrs for _v in $rcvar $rcvars; do 1163197144Shrs case $v in 1164197144Shrs $_v\ *|\ *$_v|*\ $_v\ *) ;; 1165197144Shrs *) v="${v# } $_v" ;; 1166197144Shrs esac 1167197144Shrs done 1168197144Shrs 1169197144Shrs # Display variables. 1170197144Shrs for _v in $v; do 1171197144Shrs if [ -z "$_v" ]; then 1172197144Shrs continue 117378344Sobrien fi 1174197144Shrs 1175197144Shrs eval _desc=\$${_v}_desc 1176197144Shrs eval _defval=\$${_v}_defval 1177197144Shrs _h="-" 1178197144Shrs 1179197144Shrs eval echo \"$_v=\\\"\$$_v\\\"\" 1180197144Shrs # decode multiple lines of _desc 1181197144Shrs while [ -n "$_desc" ]; do 1182197144Shrs case $_desc in 1183197144Shrs *^^*) 1184197144Shrs echo "# $_h ${_desc%%^^*}" 1185197144Shrs _desc=${_desc#*^^} 1186197144Shrs _h=" " 1187197144Shrs ;; 1188197144Shrs *) 1189197144Shrs echo "# $_h ${_desc}" 1190197144Shrs break 1191197144Shrs ;; 1192197144Shrs esac 1193197144Shrs done 1194197144Shrs echo "# (default: \"$_defval\")" 1195197144Shrs done 1196197144Shrs echo "" 119778344Sobrien ;; 119878344Sobrien 119978344Sobrien *) 1200150796Syar rc_usage $_keywords 120178344Sobrien ;; 120278344Sobrien 120378344Sobrien esac 1204295949Saraujo 1205295949Saraujo # Apply protect(1) to the PID if ${name}_oomprotect is set. 1206295949Saraujo case "$rc_arg" in 1207295949Saraujo start) 1208295949Saraujo if [ -n "$_oomprotect" ]; then 1209295949Saraujo if [ -f "${PROTECT}" ]; then 1210295949Saraujo pid=$(check_process $command) 1211295949Saraujo case $_oomprotect in 1212295949Saraujo [Aa][Ll][Ll]) 1213295949Saraujo ${PROTECT} -i -p ${pid} 1214295949Saraujo ;; 1215295949Saraujo [Yy][Ee][Ss]) 1216295949Saraujo ${PROTECT} -p ${pid} 1217295949Saraujo ;; 1218295949Saraujo esac 1219295949Saraujo fi 1220295949Saraujo fi 1221295949Saraujo ;; 1222295949Saraujo esac 1223295949Saraujo 1224116097Smtm return $_return 122578344Sobrien done 122678344Sobrien 122798186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 1228150796Syar rc_usage $_keywords 1229153152Syar # not reached 123078344Sobrien} 123178344Sobrien 123278344Sobrien# 1233165565Syar# Helper functions for run_rc_command: common code. 1234165565Syar# They use such global variables besides the exported rc_* ones: 1235165565Syar# 1236165565Syar# name R/W 1237165565Syar# ------------------ 1238165565Syar# _precmd R 1239165565Syar# _postcmd R 1240165565Syar# _return W 1241165565Syar# 1242165565Syar_run_rc_precmd() 1243165565Syar{ 1244165565Syar check_required_before "$rc_arg" || return 1 1245165565Syar 1246165565Syar if [ -n "$_precmd" ]; then 1247165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1248165565Syar eval "$_precmd $rc_extra_args" 1249165565Syar _return=$? 1250165565Syar 1251165565Syar # If precmd failed and force isn't set, request exit. 1252165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1253165565Syar return 1 1254165565Syar fi 1255165565Syar fi 1256165565Syar 1257165565Syar check_required_after "$rc_arg" || return 1 1258165565Syar 1259165565Syar return 0 1260165565Syar} 1261165565Syar 1262165565Syar_run_rc_postcmd() 1263165565Syar{ 1264165565Syar if [ -n "$_postcmd" ]; then 1265165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1266165565Syar eval "$_postcmd $rc_extra_args" 1267165565Syar _return=$? 1268165565Syar fi 1269165565Syar return 0 1270165565Syar} 1271165565Syar 1272165565Syar_run_rc_doit() 1273165565Syar{ 1274165565Syar debug "run_rc_command: doit: $*" 1275165565Syar eval "$@" 1276165565Syar _return=$? 1277165565Syar 1278165565Syar # If command failed and force isn't set, request exit. 1279165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1280165565Syar return 1 1281165565Syar fi 1282165565Syar 1283165565Syar return 0 1284165565Syar} 1285165565Syar 1286165565Syar_run_rc_notrunning() 1287165565Syar{ 1288165565Syar local _pidmsg 1289165565Syar 1290165565Syar if [ -n "$pidfile" ]; then 1291165565Syar _pidmsg=" (check $pidfile)." 1292165565Syar else 1293165565Syar _pidmsg= 1294165565Syar fi 1295165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 1296165565Syar} 1297165565Syar 1298165565Syar_run_rc_killcmd() 1299165565Syar{ 1300165565Syar local _cmd 1301165565Syar 1302165565Syar _cmd="kill -$1 $rc_pid" 1303165565Syar if [ -n "$_user" ]; then 1304165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1305165565Syar fi 1306165565Syar echo "$_cmd" 1307165565Syar} 1308165565Syar 1309165565Syar# 131078344Sobrien# run_rc_script file arg 131178344Sobrien# Start the script `file' with `arg', and correctly handle the 1312201038Sdougb# return value from the script. 1313286303Sngie# If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's 1314286303Sngie# an old-style startup file. 1315286303Sngie# If `file' ends with `.sh' and does not live in /etc/rc.d, it's sourced 1316286303Sngie# into the current environment if $rc_fast_and_loose is set; otherwise 1317286303Sngie# it is run as a child process. 1318201038Sdougb# If `file' appears to be a backup or scratch file, ignore it. 1319201038Sdougb# Otherwise if it is executable run as a child process. 132078344Sobrien# 132178344Sobrienrun_rc_script() 132278344Sobrien{ 132378344Sobrien _file=$1 132478344Sobrien _arg=$2 132578344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 132678344Sobrien err 3 'USAGE: run_rc_script file arg' 132778344Sobrien fi 132878344Sobrien 132998186Sgordon unset name command command_args command_interpreter \ 133098186Sgordon extra_commands pidfile procname \ 1331272393Shrs rcvar rcvars rcvars_obsolete required_dirs required_files \ 1332197144Shrs required_vars 133398186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 133498186Sgordon 133578344Sobrien case "$_file" in 1336193118Sdougb /etc/rc.d/*.sh) # no longer allowed in the base 1337193118Sdougb warn "Ignoring old-style startup script $_file" 133878344Sobrien ;; 1339153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 134098186Sgordon warn "Ignoring scratch file $_file" 134198186Sgordon ;; 134278344Sobrien *) # run in subshell 134398186Sgordon if [ -x $_file ]; then 134498186Sgordon if [ -n "$rc_fast_and_loose" ]; then 1345146490Sschweikh set $_arg; . $_file 134698186Sgordon else 1347231888Sdelphij ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1348231888Sdelphij trap "echo Script $_file interrupted >&2 ; exit 1" 2 1349231888Sdelphij trap "echo Script $_file running >&2" 29 1350146490Sschweikh set $_arg; . $_file ) 135198186Sgordon fi 135298186Sgordon fi 135378344Sobrien ;; 135478344Sobrien esac 135578344Sobrien} 135678344Sobrien 135778344Sobrien# 1358290007Sdes# load_rc_config [service] 1359290007Sdes# Source in the configuration file(s) for a given service. 1360290007Sdes# If no service is specified, only the global configuration 1361290007Sdes# file(s) will be loaded. 136278344Sobrien# 136378344Sobrienload_rc_config() 136478344Sobrien{ 1365270698Shrs local _name _rcvar_val _var _defval _v _msg _new _d 1366157653Sflz _name=$1 136778344Sobrien 1368219612Sdougb if ${_rc_conf_loaded:-false}; then 1369219612Sdougb : 1370219612Sdougb else 137198186Sgordon if [ -r /etc/defaults/rc.conf ]; then 137298186Sgordon debug "Sourcing /etc/defaults/rc.conf" 137398186Sgordon . /etc/defaults/rc.conf 137498186Sgordon source_rc_confs 137598186Sgordon elif [ -r /etc/rc.conf ]; then 137698186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 137798186Sgordon . /etc/rc.conf 137898186Sgordon fi 1379126285Smtm _rc_conf_loaded=true 138098186Sgordon fi 1381179872Smtm 1382275359Sdes # If a service name was specified, attempt to load 1383275359Sdes # service-specific configuration 1384275359Sdes if [ -n "$_name" ] ; then 1385286163Sjilles for _d in /etc ${local_startup}; do 1386286163Sjilles _d=${_d%/rc.d} 1387275359Sdes if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1388275359Sdes debug "Sourcing ${_d}/rc.conf.d/$_name" 1389275359Sdes . ${_d}/rc.conf.d/"$_name" 1390275359Sdes elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1391275359Sdes local _rc 1392275359Sdes for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1393275359Sdes if [ -f "$_rc" ] ; then 1394275359Sdes debug "Sourcing $_rc" 1395275359Sdes . "$_rc" 1396275359Sdes fi 1397275359Sdes done 1398275359Sdes fi 1399275359Sdes done 1400275359Sdes fi 1401270698Shrs 1402197144Shrs # Set defaults if defined. 1403272393Shrs for _var in $rcvar $rcvars; do 1404223227Sjilles eval _defval=\$${_var}_defval 1405197144Shrs if [ -n "$_defval" ]; then 1406197144Shrs eval : \${$_var:=\$${_var}_defval} 1407197144Shrs fi 1408197144Shrs done 1409197144Shrs 1410197144Shrs # check obsolete rc.conf variables 1411197144Shrs for _var in $rcvars_obsolete; do 1412223227Sjilles eval _v=\$$_var 1413223227Sjilles eval _msg=\$${_var}_obsolete_msg 1414223227Sjilles eval _new=\$${_var}_newvar 1415197144Shrs case $_v in 1416197144Shrs "") 1417197144Shrs ;; 1418197144Shrs *) 1419197144Shrs if [ -z "$_new" ]; then 1420197144Shrs _msg="Ignored." 1421197144Shrs else 1422197144Shrs eval $_new=\"\$$_var\" 1423197144Shrs if [ -z "$_msg" ]; then 1424197144Shrs _msg="Use \$$_new instead." 1425197144Shrs fi 1426197144Shrs fi 1427197144Shrs warn "\$$_var is obsolete. $_msg" 1428197144Shrs ;; 1429197144Shrs esac 1430197144Shrs done 143178344Sobrien} 1432201036Sdougb 1433157473Sflz# 1434157653Sflz# load_rc_config_var name var 1435157653Sflz# Read the rc.conf(5) var for name and set in the 1436157473Sflz# current shell, using load_rc_config in a subshell to prevent 1437157473Sflz# unwanted side effects from other variable assignments. 1438157473Sflz# 1439157473Sflzload_rc_config_var() 1440157473Sflz{ 1441157473Sflz if [ $# -ne 2 ]; then 1442157653Sflz err 3 'USAGE: load_rc_config_var name var' 1443157473Sflz fi 1444157473Sflz eval $(eval '( 1445157473Sflz load_rc_config '$1' >/dev/null; 1446157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1447157473Sflz echo '$2'=\'\''${'$2'}\'\''; 1448157473Sflz fi 1449157473Sflz )' ) 1450157473Sflz} 145178344Sobrien 145278344Sobrien# 145378344Sobrien# rc_usage commands 145478344Sobrien# Print a usage string for $0, with `commands' being a list of 145578344Sobrien# valid commands. 145678344Sobrien# 145778344Sobrienrc_usage() 145878344Sobrien{ 1459230007Srea echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 146078344Sobrien 146178344Sobrien _sep= 1462126286Smtm for _elem; do 146378344Sobrien echo -n 1>&2 "$_sep$_elem" 146478344Sobrien _sep="|" 146578344Sobrien done 146678344Sobrien echo 1>&2 ")" 146778344Sobrien exit 1 146878344Sobrien} 146978344Sobrien 147078344Sobrien# 147178344Sobrien# err exitval message 147278344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 147378344Sobrien# 147478344Sobrienerr() 147578344Sobrien{ 147678344Sobrien exitval=$1 147778344Sobrien shift 147878344Sobrien 1479106643Sgordon if [ -x /usr/bin/logger ]; then 1480106643Sgordon logger "$0: ERROR: $*" 1481106643Sgordon fi 1482106643Sgordon echo 1>&2 "$0: ERROR: $*" 148378344Sobrien exit $exitval 148478344Sobrien} 148578344Sobrien 148678344Sobrien# 148778344Sobrien# warn message 148878344Sobrien# Display message to stderr and log to the syslog. 148978344Sobrien# 149078344Sobrienwarn() 149178344Sobrien{ 1492106643Sgordon if [ -x /usr/bin/logger ]; then 1493106643Sgordon logger "$0: WARNING: $*" 1494106643Sgordon fi 1495106643Sgordon echo 1>&2 "$0: WARNING: $*" 149678344Sobrien} 149798186Sgordon 149898186Sgordon# 149998186Sgordon# info message 150098186Sgordon# Display informational message to stdout and log to syslog. 150198186Sgordon# 150298186Sgordoninfo() 150398186Sgordon{ 1504119170Smtm case ${rc_info} in 1505119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1506119170Smtm if [ -x /usr/bin/logger ]; then 1507119170Smtm logger "$0: INFO: $*" 1508119170Smtm fi 1509119170Smtm echo "$0: INFO: $*" 1510119170Smtm ;; 1511119170Smtm esac 151298186Sgordon} 151398186Sgordon 151498186Sgordon# 151598186Sgordon# debug message 1516106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 151798186Sgordon# BEWARE that you don't call any subroutine that itself calls this 151898186Sgordon# function. 151998186Sgordon# 152098186Sgordondebug() 152198186Sgordon{ 152298186Sgordon case ${rc_debug} in 152398186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1524106700Sgordon if [ -x /usr/bin/logger ]; then 1525162947Syar logger "$0: DEBUG: $*" 1526106700Sgordon fi 1527146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 152898186Sgordon ;; 152998186Sgordon esac 153098186Sgordon} 153198186Sgordon 153298186Sgordon# 153398186Sgordon# backup_file action file cur backup 153498186Sgordon# Make a backup copy of `file' into `cur', and save the previous 153598186Sgordon# version of `cur' as `backup' or use rcs for archiving. 153698186Sgordon# 153798186Sgordon# This routine checks the value of the backup_uses_rcs variable, 153898186Sgordon# which can be either YES or NO. 153998186Sgordon# 154098186Sgordon# The `action' keyword can be one of the following: 154198186Sgordon# 154298186Sgordon# add `file' is now being backed up (and is possibly 154398186Sgordon# being reentered into the backups system). `cur' 154498186Sgordon# is created and RCS files, if necessary, are 154598186Sgordon# created as well. 154698186Sgordon# 154798186Sgordon# update `file' has changed and needs to be backed up. 154898186Sgordon# If `cur' exists, it is copied to to `back' or 154998186Sgordon# checked into RCS (if the repository file is old), 155098186Sgordon# and then `file' is copied to `cur'. Another RCS 155198186Sgordon# check in done here if RCS is being used. 155298186Sgordon# 155398186Sgordon# remove `file' is no longer being tracked by the backups 155498186Sgordon# system. If RCS is not being used, `cur' is moved 155598186Sgordon# to `back', otherwise an empty file is checked in, 155698186Sgordon# and then `cur' is removed. 155798186Sgordon# 155898186Sgordon# 155998186Sgordonbackup_file() 156098186Sgordon{ 156198186Sgordon _action=$1 156298186Sgordon _file=$2 156398186Sgordon _cur=$3 156498186Sgordon _back=$4 156598186Sgordon 156698186Sgordon if checkyesno backup_uses_rcs; then 156798186Sgordon _msg0="backup archive" 156898186Sgordon _msg1="update" 156998186Sgordon 157098186Sgordon # ensure that history file is not locked 157198186Sgordon if [ -f $_cur,v ]; then 157298186Sgordon rcs -q -u -U -M $_cur 157398186Sgordon fi 157498186Sgordon 157598186Sgordon # ensure after switching to rcs that the 157698186Sgordon # current backup is not lost 157798186Sgordon if [ -f $_cur ]; then 157898186Sgordon # no archive, or current newer than archive 157998186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 158098186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 158198186Sgordon rcs -q -kb -U $_cur 158298186Sgordon co -q -f -u $_cur 158398186Sgordon fi 158498186Sgordon fi 158598186Sgordon 158698186Sgordon case $_action in 158798186Sgordon add|update) 158898186Sgordon cp -p $_file $_cur 158998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 159098186Sgordon rcs -q -kb -U $_cur 159198186Sgordon co -q -f -u $_cur 159298186Sgordon chown root:wheel $_cur $_cur,v 159398186Sgordon ;; 159498186Sgordon remove) 159598186Sgordon cp /dev/null $_cur 159698186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 159798186Sgordon rcs -q -kb -U $_cur 159898186Sgordon chown root:wheel $_cur $_cur,v 159998186Sgordon rm $_cur 160098186Sgordon ;; 160198186Sgordon esac 160298186Sgordon else 160398186Sgordon case $_action in 160498186Sgordon add|update) 160598186Sgordon if [ -f $_cur ]; then 160698186Sgordon cp -p $_cur $_back 160798186Sgordon fi 160898186Sgordon cp -p $_file $_cur 160998186Sgordon chown root:wheel $_cur 161098186Sgordon ;; 161198186Sgordon remove) 161298186Sgordon mv -f $_cur $_back 161398186Sgordon ;; 161498186Sgordon esac 161598186Sgordon fi 161698186Sgordon} 1617119166Smtm 1618123344Smtm# make_symlink src link 1619123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1620123344Smtm# directory in which link is to be created does not exist 1621123344Smtm# a warning will be displayed and an error will be returned. 1622229783Suqs# Returns 0 on success, 1 otherwise. 1623119166Smtm# 1624123344Smtmmake_symlink() 1625119166Smtm{ 1626123344Smtm local src link linkdir _me 1627123344Smtm src="$1" 1628123344Smtm link="$2" 1629123344Smtm linkdir="`dirname $link`" 1630123344Smtm _me="make_symlink()" 1631119166Smtm 1632123344Smtm if [ -z "$src" -o -z "$link" ]; then 1633123344Smtm warn "$_me: requires two arguments." 1634119166Smtm return 1 1635119166Smtm fi 1636123344Smtm if [ ! -d "$linkdir" ]; then 1637160667Syar warn "$_me: the directory $linkdir does not exist." 1638119166Smtm return 1 1639119166Smtm fi 1640146490Sschweikh if ! ln -sf $src $link; then 1641123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1642119166Smtm return 1 1643119166Smtm fi 1644119166Smtm return 0 1645119166Smtm} 1646119166Smtm 1647119166Smtm# devfs_rulesets_from_file file 1648119166Smtm# Reads a set of devfs commands from file, and creates 1649119166Smtm# the specified rulesets with their rules. Returns non-zero 1650119166Smtm# if there was an error. 1651119166Smtm# 1652119166Smtmdevfs_rulesets_from_file() 1653119166Smtm{ 1654248820Savg local file _err _me _opts 1655119166Smtm file="$1" 1656119166Smtm _me="devfs_rulesets_from_file" 1657119166Smtm _err=0 1658119166Smtm 1659119166Smtm if [ -z "$file" ]; then 1660119166Smtm warn "$_me: you must specify a file" 1661119166Smtm return 1 1662119166Smtm fi 1663119166Smtm if [ ! -e "$file" ]; then 1664119166Smtm debug "$_me: no such file ($file)" 1665119166Smtm return 0 1666119166Smtm fi 1667248820Savg 1668248820Savg # Disable globbing so that the rule patterns are not expanded 1669248820Savg # by accident with matching filesystem entries. 1670248820Savg _opts=$-; set -f 1671248820Savg 1672119166Smtm debug "reading rulesets from file ($file)" 1673119166Smtm { while read line 1674119166Smtm do 1675119166Smtm case $line in 1676119166Smtm \#*) 1677119166Smtm continue 1678119166Smtm ;; 1679119166Smtm \[*\]*) 1680119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1681119166Smtm if [ -z "$rulenum" ]; then 1682119166Smtm warn "$_me: cannot extract rule number ($line)" 1683119166Smtm _err=1 1684119166Smtm break 1685119166Smtm fi 1686119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1687119166Smtm if [ -z "$rulename" ]; then 1688119166Smtm warn "$_me: cannot extract rule name ($line)" 1689119166Smtm _err=1 1690119166Smtm break; 1691119166Smtm fi 1692119166Smtm eval $rulename=\$rulenum 1693119166Smtm debug "found ruleset: $rulename=$rulenum" 1694146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1695119166Smtm _err=1 1696119166Smtm break 1697119166Smtm fi 1698119166Smtm ;; 1699119166Smtm *) 1700119166Smtm rulecmd="${line%%"\#*"}" 1701119166Smtm # evaluate the command incase it includes 1702119166Smtm # other rules 1703119166Smtm if [ -n "$rulecmd" ]; then 1704119166Smtm debug "adding rule ($rulecmd)" 1705119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1706119166Smtm then 1707119166Smtm _err=1 1708119166Smtm break 1709119166Smtm fi 1710119166Smtm fi 1711119166Smtm ;; 1712119166Smtm esac 1713119166Smtm if [ $_err -ne 0 ]; then 1714119166Smtm debug "error in $_me" 1715119166Smtm break 1716119166Smtm fi 1717119166Smtm done } < $file 1718248820Savg case $_opts in *f*) ;; *) set +f ;; esac 1719119166Smtm return $_err 1720119166Smtm} 1721119166Smtm 1722119166Smtm# devfs_init_rulesets 1723119166Smtm# Initializes rulesets from configuration files. Returns 1724119166Smtm# non-zero if there was an error. 1725119166Smtm# 1726119166Smtmdevfs_init_rulesets() 1727119166Smtm{ 1728119166Smtm local file _me 1729119166Smtm _me="devfs_init_rulesets" 1730119166Smtm 1731119166Smtm # Go through this only once 1732119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1733119166Smtm debug "$_me: devfs rulesets already initialized" 1734119166Smtm return 1735119166Smtm fi 1736146490Sschweikh for file in $devfs_rulesets; do 1737217090Sjh if ! devfs_rulesets_from_file $file; then 1738217090Sjh warn "$_me: could not read rules from $file" 1739217090Sjh return 1 1740217090Sjh fi 1741119166Smtm done 1742119166Smtm devfs_rulesets_init=1 1743119166Smtm debug "$_me: devfs rulesets initialized" 1744119166Smtm return 0 1745119166Smtm} 1746119166Smtm 1747119166Smtm# devfs_set_ruleset ruleset [dir] 1748151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1749119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1750119166Smtm# Returns non-zero if it could not set it successfully. 1751119166Smtm# 1752119166Smtmdevfs_set_ruleset() 1753119166Smtm{ 1754119166Smtm local devdir rs _me 1755119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1756119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1757119166Smtm _me="devfs_set_ruleset" 1758119166Smtm 1759119166Smtm if [ -z "$rs" ]; then 1760119166Smtm warn "$_me: you must specify a ruleset number" 1761119166Smtm return 1 1762119166Smtm fi 1763119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1764146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1765119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1766119166Smtm return 1 1767119166Smtm fi 1768119166Smtm return 0 1769119166Smtm} 1770119166Smtm 1771119166Smtm# devfs_apply_ruleset ruleset [dir] 1772119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1773119166Smtm# The ruleset argument must be a ruleset name as specified 1774119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1775119166Smtm# if it could not apply the ruleset. 1776119166Smtm# 1777119166Smtmdevfs_apply_ruleset() 1778119166Smtm{ 1779119166Smtm local devdir rs _me 1780119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1781119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1782119166Smtm _me="devfs_apply_ruleset" 1783119166Smtm 1784119166Smtm if [ -z "$rs" ]; then 1785119166Smtm warn "$_me: you must specify a ruleset" 1786119166Smtm return 1 1787119166Smtm fi 1788119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1789146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1790119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1791119166Smtm return 1 1792119166Smtm fi 1793119166Smtm return 0 1794119166Smtm} 1795119166Smtm 1796119166Smtm# devfs_domount dir [ruleset] 1797119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1798119166Smtm# on the mount-point. It must also be a ruleset name as specified 1799119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1800119166Smtm# 1801119166Smtmdevfs_domount() 1802119166Smtm{ 1803119166Smtm local devdir rs _me 1804119166Smtm devdir="$1" 1805119166Smtm [ -n "$2" ] && rs=$2 || rs= 1806119166Smtm _me="devfs_domount()" 1807119166Smtm 1808119166Smtm if [ -z "$devdir" ]; then 1809119166Smtm warn "$_me: you must specify a mount-point" 1810119166Smtm return 1 1811119166Smtm fi 1812119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1813146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1814119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1815119166Smtm return 1 1816119166Smtm fi 1817119166Smtm if [ -n "$rs" ]; then 1818119166Smtm devfs_init_rulesets 1819119166Smtm devfs_set_ruleset $rs $devdir 1820124797Scperciva devfs -m $devdir rule applyset 1821119166Smtm fi 1822119166Smtm return 0 1823119166Smtm} 1824119166Smtm 1825127345Sbrooks# Provide a function for normalizing the mounting of memory 1826127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1827127345Sbrooks# as close as possible between 5-current and 4-stable. 1828127345Sbrooks# $1 = size 1829127345Sbrooks# $2 = mount point 1830137451Skeramida# $3 = (optional) extra mdmfs flags 1831146490Sschweikhmount_md() 1832146490Sschweikh{ 1833127345Sbrooks if [ -n "$3" ]; then 1834137451Skeramida flags="$3" 1835127345Sbrooks fi 1836149421Syar /sbin/mdmfs $flags -s $1 md $2 1837127345Sbrooks} 1838131550Scperciva 1839159828Syar# Code common to scripts that need to load a kernel module 1840159828Syar# if it isn't in the kernel yet. Syntax: 1841160666Syar# load_kld [-e regex] [-m module] file 1842159828Syar# where -e or -m chooses the way to check if the module 1843159828Syar# is already loaded: 1844160666Syar# regex is egrep'd in the output from `kldstat -v', 1845160666Syar# module is passed to `kldstat -m'. 1846160666Syar# The default way is as though `-m file' were specified. 1847159828Syarload_kld() 1848159828Syar{ 1849159828Syar local _loaded _mod _opt _re 1850159828Syar 1851159828Syar while getopts "e:m:" _opt; do 1852159828Syar case "$_opt" in 1853159828Syar e) _re="$OPTARG" ;; 1854159828Syar m) _mod="$OPTARG" ;; 1855160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1856159828Syar esac 1857159828Syar done 1858159828Syar shift $(($OPTIND - 1)) 1859160666Syar if [ $# -ne 1 ]; then 1860160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1861160666Syar fi 1862159828Syar _mod=${_mod:-$1} 1863159828Syar _loaded=false 1864159828Syar if [ -n "$_re" ]; then 1865159828Syar if kldstat -v | egrep -q -e "$_re"; then 1866159828Syar _loaded=true 1867159828Syar fi 1868159828Syar else 1869159828Syar if kldstat -q -m "$_mod"; then 1870159828Syar _loaded=true 1871159828Syar fi 1872159828Syar fi 1873159828Syar if ! $_loaded; then 1874159828Syar if ! kldload "$1"; then 1875159828Syar warn "Unable to load kernel module $1" 1876159828Syar return 1 1877160666Syar else 1878160666Syar info "$1 kernel module loaded." 1879159828Syar fi 1880160666Syar else 1881160666Syar debug "load_kld: $1 kernel module already loaded." 1882159828Syar fi 1883159828Syar return 0 1884159828Syar} 1885159828Syar 1886264243Sdteske# ltr str src dst [var] 1887149049Spjd# Change every $src in $str to $dst. 1888149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1889264243Sdteske# awk(1). If var is non-NULL, set it to the result. 1890149049Spjdltr() 1891149049Spjd{ 1892264243Sdteske local _str _src _dst _out _com _var 1893264243Sdteske _str="$1" 1894264243Sdteske _src="$2" 1895264243Sdteske _dst="$3" 1896264243Sdteske _var="$4" 1897149049Spjd _out="" 1898149049Spjd 1899264243Sdteske local IFS="${_src}" 1900149049Spjd for _com in ${_str}; do 1901149049Spjd if [ -z "${_out}" ]; then 1902149049Spjd _out="${_com}" 1903149049Spjd else 1904149049Spjd _out="${_out}${_dst}${_com}" 1905149049Spjd fi 1906149049Spjd done 1907264243Sdteske if [ -n "${_var}" ]; then 1908264243Sdteske setvar "${_var}" "${_out}" 1909264243Sdteske else 1910264243Sdteske echo "${_out}" 1911264243Sdteske fi 1912149049Spjd} 1913149049Spjd 1914149050Spjd# Creates a list of providers for GELI encryption. 1915149050Spjdgeli_make_list() 1916149050Spjd{ 1917149050Spjd local devices devices2 1918149050Spjd local provider mountpoint type options rest 1919149050Spjd 1920149050Spjd # Create list of GELI providers from fstab. 1921149050Spjd while read provider mountpoint type options rest ; do 1922155570Sflz case ":${options}" in 1923155570Sflz :*noauto*) 1924155570Sflz noauto=yes 1925155570Sflz ;; 1926155570Sflz *) 1927155570Sflz noauto=no 1928155570Sflz ;; 1929155570Sflz esac 1930155570Sflz 1931149050Spjd case ":${provider}" in 1932149050Spjd :#*) 1933149050Spjd continue 1934149050Spjd ;; 1935149050Spjd *.eli) 1936149050Spjd # Skip swap devices. 1937155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1938149050Spjd continue 1939149050Spjd fi 1940149050Spjd devices="${devices} ${provider}" 1941149050Spjd ;; 1942149050Spjd esac 1943149050Spjd done < /etc/fstab 1944149050Spjd 1945149050Spjd # Append providers from geli_devices. 1946149050Spjd devices="${devices} ${geli_devices}" 1947149050Spjd 1948149050Spjd for provider in ${devices}; do 1949149050Spjd provider=${provider%.eli} 1950149050Spjd provider=${provider#/dev/} 1951149050Spjd devices2="${devices2} ${provider}" 1952149050Spjd done 1953149050Spjd 1954149050Spjd echo ${devices2} 1955149050Spjd} 1956149050Spjd 1957153027Sdougb# Find scripts in local_startup directories that use the old syntax 1958153027Sdougb# 1959238416Skevlofind_local_scripts_old() { 1960153027Sdougb zlist='' 1961153027Sdougb slist='' 1962153027Sdougb for dir in ${local_startup}; do 1963153027Sdougb if [ -d "${dir}" ]; then 1964153027Sdougb for file in ${dir}/[0-9]*.sh; do 1965153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1966153027Sdougb continue 1967153027Sdougb zlist="$zlist $file" 1968153027Sdougb done 1969227366Sjilles for file in ${dir}/[!0-9]*.sh; do 1970153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1971153027Sdougb continue 1972153027Sdougb slist="$slist $file" 1973153027Sdougb done 1974153027Sdougb fi 1975153027Sdougb done 1976153027Sdougb} 1977153027Sdougb 1978238416Skevlofind_local_scripts_new() { 1979153027Sdougb local_rc='' 1980153027Sdougb for dir in ${local_startup}; do 1981153027Sdougb if [ -d "${dir}" ]; then 1982153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1983153027Sdougb case "$file" in 1984153027Sdougb *.sample) ;; 1985153027Sdougb *) if [ -x "$file" ]; then 1986153027Sdougb local_rc="${local_rc} ${file}" 1987153027Sdougb fi 1988153027Sdougb ;; 1989153027Sdougb esac 1990153027Sdougb done 1991153027Sdougb fi 1992153027Sdougb done 1993153027Sdougb} 1994153027Sdougb 1995165565Syar# check_required_{before|after} command 1996165565Syar# Check for things required by the command before and after its precmd, 1997165565Syar# respectively. The two separate functions are needed because some 1998165565Syar# conditions should prevent precmd from being run while other things 1999165565Syar# depend on precmd having already been run. 2000165565Syar# 2001165565Syarcheck_required_before() 2002165565Syar{ 2003165565Syar local _f 2004165565Syar 2005165565Syar case "$1" in 2006165565Syar start) 2007165565Syar for _f in $required_vars; do 2008165565Syar if ! checkyesno $_f; then 2009165565Syar warn "\$${_f} is not enabled." 2010165565Syar if [ -z "$rc_force" ]; then 2011165565Syar return 1 2012165565Syar fi 2013165565Syar fi 2014165565Syar done 2015165565Syar 2016165565Syar for _f in $required_dirs; do 2017165565Syar if [ ! -d "${_f}/." ]; then 2018165565Syar warn "${_f} is not a directory." 2019165565Syar if [ -z "$rc_force" ]; then 2020165565Syar return 1 2021165565Syar fi 2022165565Syar fi 2023165565Syar done 2024165565Syar 2025165565Syar for _f in $required_files; do 2026165565Syar if [ ! -r "${_f}" ]; then 2027165565Syar warn "${_f} is not readable." 2028165565Syar if [ -z "$rc_force" ]; then 2029165565Syar return 1 2030165565Syar fi 2031165565Syar fi 2032165565Syar done 2033165565Syar ;; 2034165565Syar esac 2035165565Syar 2036165565Syar return 0 2037165565Syar} 2038165565Syar 2039165565Syarcheck_required_after() 2040165565Syar{ 2041165565Syar local _f _args 2042165565Syar 2043165565Syar case "$1" in 2044165565Syar start) 2045165565Syar for _f in $required_modules; do 2046165565Syar case "${_f}" in 2047165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 2048165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 2049165565Syar *) _args="${_f}" ;; 2050165565Syar esac 2051165565Syar if ! load_kld ${_args}; then 2052165565Syar if [ -z "$rc_force" ]; then 2053165565Syar return 1 2054165565Syar fi 2055165565Syar fi 2056165565Syar done 2057165565Syar ;; 2058165565Syar esac 2059165565Syar 2060165565Syar return 0 2061165565Syar} 2062165565Syar 2063271545Shrs# check_jail mib 2064271545Shrs# Return true if security.jail.$mib exists and set to 1. 2065271545Shrs 2066271545Shrscheck_jail() 2067271545Shrs{ 2068271545Shrs local _mib _v 2069271545Shrs 2070271545Shrs _mib=$1 2071271545Shrs if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 2072271545Shrs case $_v in 2073271545Shrs 1) return 0;; 2074271545Shrs esac 2075271545Shrs fi 2076271545Shrs return 1 2077271545Shrs} 2078271545Shrs 2079222996Shrs# check_kern_features mib 2080222996Shrs# Return existence of kern.features.* sysctl MIB as true or 2081222996Shrs# false. The result will be cached in $_rc_cache_kern_features_ 2082222996Shrs# namespace. "0" means the kern.features.X exists. 2083222996Shrs 2084222996Shrscheck_kern_features() 2085222996Shrs{ 2086222996Shrs local _v 2087222996Shrs 2088222996Shrs [ -n "$1" ] || return 1; 2089223292Sjilles eval _v=\$_rc_cache_kern_features_$1 2090222996Shrs [ -n "$_v" ] && return "$_v"; 2091222996Shrs 2092222996Shrs if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 2093222996Shrs eval _rc_cache_kern_features_$1=0 2094222996Shrs return 0 2095222996Shrs else 2096222996Shrs eval _rc_cache_kern_features_$1=1 2097222996Shrs return 1 2098222996Shrs fi 2099222996Shrs} 2100222996Shrs 2101243184Shrs# check_namevarlist var 2102243184Shrs# Return "0" if ${name}_var is reserved in rc.subr. 2103243184Shrs 2104272976Shrs_rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend" 2105243184Shrscheck_namevarlist() 2106243184Shrs{ 2107243184Shrs local _v 2108243184Shrs 2109243184Shrs for _v in $_rc_namevarlist; do 2110243184Shrs case $1 in 2111243184Shrs $_v) return 0 ;; 2112243184Shrs esac 2113243184Shrs done 2114243184Shrs 2115243184Shrs return 1 2116243184Shrs} 2117243184Shrs 2118197144Shrs# _echoonce var msg mode 2119197144Shrs# mode=0: Echo $msg if ${$var} is empty. 2120197144Shrs# After doing echo, a string is set to ${$var}. 2121197144Shrs# 2122197144Shrs# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2123197144Shrs# 2124197144Shrs_echoonce() 2125197144Shrs{ 2126197144Shrs local _var _msg _mode 2127223227Sjilles eval _var=\$$1 2128197144Shrs _msg=$2 2129197144Shrs _mode=$3 2130197144Shrs 2131197144Shrs case $_mode in 2132197144Shrs 1) [ -n "$_var" ] && echo "$_msg" ;; 2133197144Shrs *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2134197144Shrs esac 2135197144Shrs} 2136197144Shrs 2137223298Sjillesfi # [ -z "${_rc_subr_loaded}" ] 2138223298Sjilles 2139157841Sflz_rc_subr_loaded=: 2140