rc.subr revision 286163
1515SN/A# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 2515SN/A# $FreeBSD: head/etc/rc.subr 286163 2015-08-01 22:00:25Z jilles $ 3515SN/A# 4515SN/A# Copyright (c) 1997-2004 The NetBSD Foundation, Inc. 5515SN/A# All rights reserved. 61365Ssundar# 7515SN/A# This code is derived from software contributed to The NetBSD Foundation 8515SN/A# by Luke Mewburn. 9515SN/A# 10515SN/A# Redistribution and use in source and binary forms, with or without 11515SN/A# modification, are permitted provided that the following conditions 12515SN/A# are met: 131365Ssundar# 1. Redistributions of source code must retain the above copyright 14515SN/A# notice, this list of conditions and the following disclaimer. 15515SN/A# 2. Redistributions in binary form must reproduce the above copyright 16515SN/A# notice, this list of conditions and the following disclaimer in the 171365Ssundar# documentation and/or other materials provided with the distribution. 18515SN/A# 19515SN/A# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20515SN/A# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21515SN/A# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22515SN/A# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23515SN/A# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24515SN/A# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25515SN/A# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26515SN/A# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27515SN/A# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28889SN/A# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29818SN/A# POSSIBILITY OF SUCH DAMAGE. 30818SN/A# 31889SN/A# rc.subr 32889SN/A# functions used by various rc scripts 33889SN/A# 34515SN/A 35515SN/A: ${RC_PID:=$$}; export RC_PID 36889SN/A 37515SN/A# 38515SN/A# Operating System dependent/independent variables 39515SN/A# 40515SN/A 41889SN/Aif [ -z "${_rc_subr_loaded}" ]; then 42889SN/A 43515SN/A_rc_subr_loaded="YES" 44515SN/A 45515SN/ASYSCTL="/sbin/sysctl" 46515SN/ASYSCTL_N="${SYSCTL} -n" 47515SN/ASYSCTL_W="${SYSCTL}" 48515SN/AID="/usr/bin/id" 49515SN/AIDCMD="if [ -x $ID ]; then $ID -un; fi" 50515SN/APS="/bin/ps -ww" 51515SN/AJID=`$PS -p $$ -o jid=` 52515SN/A 53515SN/A# 54515SN/A# functions 55515SN/A# --------- 56523SN/A 57877SN/A# list_vars pattern 58515SN/A# List vars matching pattern. 59889SN/A# 60889SN/Alist_vars() 61889SN/A{ 62889SN/A set | { while read LINE; do 63889SN/A var="${LINE%%=*}" 64515SN/A case "$var" in 65515SN/A "$LINE"|*[!a-zA-Z0-9_]*) continue ;; 66515SN/A $1) echo $var 67515SN/A esac 68515SN/A done; } 69515SN/A} 70515SN/A 71515SN/A# set_rcvar [var] [defval] [desc] 72515SN/A# 73515SN/A# Echo or define a rc.conf(5) variable name. Global variable 74515SN/A# $rcvars is used. 75515SN/A# 76515SN/A# If no argument is specified, echo "${name}_enable". 77515SN/A# 78515SN/A# If only a var is specified, echo "${var}_enable". 79515SN/A# 80515SN/A# If var and defval are specified, the ${var} is defined as 81515SN/A# rc.conf(5) variable and the default value is ${defvar}. An 82515SN/A# optional argument $desc can also be specified to add a 83515SN/A# description for that. 84515SN/A# 85515SN/Aset_rcvar() 86515SN/A{ 87515SN/A local _var 88889SN/A 89889SN/A case $# in 90889SN/A 0) echo ${name}_enable ;; 91889SN/A 1) echo ${1}_enable ;; 92889SN/A *) 93889SN/A debug "set_rcvar: \$$1=$2 is added" \ 94889SN/A " as a rc.conf(5) variable." 95889SN/A _var=$1 96889SN/A rcvars="${rcvars# } $_var" 97889SN/A eval ${_var}_defval=\"$2\" 98889SN/A shift 2 99889SN/A eval ${_var}_desc=\"$*\" 100889SN/A ;; 101889SN/A esac 102889SN/A} 103889SN/A 104889SN/A# set_rcvar_obsolete oldvar [newvar] [msg] 105889SN/A# Define obsolete variable. 106889SN/A# Global variable $rcvars_obsolete is used. 107889SN/A# 108889SN/Aset_rcvar_obsolete() 109889SN/A{ 110889SN/A local _var 111889SN/A _var=$1 112889SN/A debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 113889SN/A 114889SN/A rcvars_obsolete="${rcvars_obsolete# } $1" 115889SN/A eval ${1}_newvar=\"$2\" 116889SN/A shift 2 117889SN/A eval ${_var}_obsolete_msg=\"$*\" 118889SN/A} 119889SN/A 120889SN/A# 121889SN/A# force_depend script [rcvar] 122889SN/A# Force a service to start. Intended for use by services 123889SN/A# to resolve dependency issues. 124889SN/A# $1 - filename of script, in /etc/rc.d, to run 125889SN/A# $2 - name of the script's rcvar (minus the _enable) 126889SN/A# 127889SN/Aforce_depend() 128889SN/A{ 129889SN/A local _depend _dep_rcvar 130889SN/A 131889SN/A _depend="$1" 132889SN/A _dep_rcvar="${2:-$1}_enable" 133889SN/A 134889SN/A [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 135889SN/A checkyesno $_dep_rcvar && return 0 136515SN/A 137515SN/A /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 138515SN/A 139515SN/A info "${name} depends on ${_depend}, which will be forced to start." 140515SN/A if ! /etc/rc.d/${_depend} forcestart; then 141766SN/A warn "Unable to force ${_depend}. It may already be running." 142515SN/A return 1 143515SN/A fi 144515SN/A} 145528SN/A 146528SN/A# 147528SN/A# checkyesno var 148528SN/A# Test $1 variable, and warn if not set to YES or NO. 149528SN/A# Return 0 if it's "yes" (et al), nonzero otherwise. 150889SN/A# 151528SN/Acheckyesno() 152528SN/A{ 153766SN/A eval _value=\$${1} 154528SN/A debug "checkyesno: $1 is set to $_value." 155528SN/A case $_value in 156528SN/A 157528SN/A # "yes", "true", "on", or "1" 158528SN/A [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1591318Ssundar return 0 160877SN/A ;; 161528SN/A 162528SN/A # "no", "false", "off", or "0" 163528SN/A [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 164528SN/A return 1 165528SN/A ;; 166515SN/A *) 167515SN/A warn "\$${1} is not set properly - see rc.conf(5)." 168515SN/A return 1 169515SN/A ;; 170515SN/A esac 171515SN/A} 172515SN/A 173515SN/A# 174515SN/A# reverse_list list 175515SN/A# print the list in reverse order 176515SN/A# 177515SN/Areverse_list() 178515SN/A{ 179515SN/A _revlist= 180515SN/A for _revfile; do 181515SN/A _revlist="$_revfile $_revlist" 182515SN/A done 183515SN/A echo $_revlist 184515SN/A} 185515SN/A 186515SN/A# stop_boot always 187515SN/A# If booting directly to multiuser or $always is enabled, 188515SN/A# send SIGTERM to the parent (/etc/rc) to abort the boot. 189515SN/A# Otherwise just exit. 190515SN/A# 191515SN/Astop_boot() 192515SN/A{ 193515SN/A local always 194515SN/A 195515SN/A case $1 in 196515SN/A # "yes", "true", "on", or "1" 197515SN/A [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 198515SN/A always=true 199515SN/A ;; 200523SN/A *) 201515SN/A always=false 202515SN/A ;; 203515SN/A esac 204515SN/A if [ "$autoboot" = yes -o "$always" = true ]; then 205515SN/A echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 206515SN/A kill -TERM ${RC_PID} 207515SN/A fi 208515SN/A exit 1 209515SN/A} 210515SN/A 211515SN/A# 212515SN/A# mount_critical_filesystems type 213515SN/A# Go through the list of critical filesystems as provided in 214515SN/A# the rc.conf(5) variable $critical_filesystems_${type}, checking 215515SN/A# each one to see if it is mounted, and if it is not, mounting it. 216515SN/A# 217515SN/Amount_critical_filesystems() 218515SN/A{ 219515SN/A eval _fslist=\$critical_filesystems_${1} 220515SN/A for _fs in $_fslist; do 221515SN/A mount | ( 222528SN/A _ismounted=false 223515SN/A while read what _on on _type type; do 224515SN/A if [ $on = $_fs ]; then 225515SN/A _ismounted=true 226515SN/A fi 227515SN/A done 228515SN/A if $_ismounted; then 229515SN/A : 230515SN/A else 231515SN/A mount $_fs >/dev/null 2>&1 232515SN/A fi 233515SN/A ) 234515SN/A done 235515SN/A} 236515SN/A 237515SN/A# 238515SN/A# check_pidfile pidfile procname [interpreter] 239515SN/A# Parses the first line of pidfile for a PID, and ensures 240515SN/A# that the process is running and matches procname. 241515SN/A# Prints the matching PID upon success, nothing otherwise. 2421571Shannesw# interpreter is optional; see _find_processes() for details. 243515SN/A# 244515SN/Acheck_pidfile() 245515SN/A{ 246515SN/A _pidfile=$1 247851SN/A _procname=$2 248515SN/A _interpreter=$3 249515SN/A if [ -z "$_pidfile" -o -z "$_procname" ]; then 250515SN/A err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 251515SN/A fi 252515SN/A if [ ! -f $_pidfile ]; then 253515SN/A debug "pid file ($_pidfile): not readable." 254515SN/A return 255515SN/A fi 256515SN/A read _pid _junk < $_pidfile 257515SN/A if [ -z "$_pid" ]; then 258515SN/A debug "pid file ($_pidfile): no pid in file." 259528SN/A return 260515SN/A fi 261515SN/A _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 262515SN/A} 263515SN/A 264515SN/A# 265515SN/A# check_process procname [interpreter] 266515SN/A# Ensures that a process (or processes) named procname is running. 267515SN/A# Prints a list of matching PIDs. 268515SN/A# interpreter is optional; see _find_processes() for details. 269515SN/A# 270515SN/Acheck_process() 271515SN/A{ 272515SN/A _procname=$1 273515SN/A _interpreter=$2 274515SN/A if [ -z "$_procname" ]; then 275515SN/A err 3 'USAGE: check_process procname [interpreter]' 276515SN/A fi 277515SN/A _find_processes $_procname ${_interpreter:-.} '-ax' 278515SN/A} 279515SN/A 280515SN/A# 281515SN/A# _find_processes procname interpreter psargs 282515SN/A# Search for procname in the output of ps generated by psargs. 283515SN/A# Prints the PIDs of any matching processes, space separated. 284515SN/A# 285515SN/A# If interpreter == ".", check the following variations of procname 286523SN/A# against the first word of each command: 287515SN/A# procname 288515SN/A# `basename procname` 289515SN/A# `basename procname` + ":" 290515SN/A# "(" + `basename procname` + ")" 291515SN/A# "[" + `basename procname` + "]" 292515SN/A# 293515SN/A# If interpreter != ".", read the first line of procname, remove the 294515SN/A# leading #!, normalise whitespace, append procname, and attempt to 295515SN/A# match that against each command, either as is, or with extra words 296515SN/A# at the end. As an alternative, to deal with interpreted daemons 297515SN/A# using perl, the basename of the interpreter plus a colon is also 298515SN/A# tried as the prefix to procname. 299515SN/A# 300515SN/A_find_processes() 301889SN/A{ 302515SN/A if [ $# -ne 3 ]; then 303515SN/A err 3 'USAGE: _find_processes procname interpreter psargs' 304515SN/A fi 305515SN/A _procname=$1 306515SN/A _interpreter=$2 307515SN/A _psargs=$3 308515SN/A 309684SN/A _pref= 310515SN/A if [ $_interpreter != "." ]; then # an interpreted script 311515SN/A _script="${_chroot}${_chroot:+/}$_procname" 312515SN/A if [ -r "$_script" ]; then 313515SN/A read _interp < $_script # read interpreter name 314515SN/A case "$_interp" in 315515SN/A \#!*) 316515SN/A _interp=${_interp#\#!} # strip #! 317515SN/A set -- $_interp 318515SN/A case $1 in 319515SN/A */bin/env) 320515SN/A shift # drop env to get real name 321515SN/A ;; 322515SN/A esac 323515SN/A if [ $_interpreter != $1 ]; then 324515SN/A warn "\$command_interpreter $_interpreter != $1" 325515SN/A fi 326515SN/A ;; 327523SN/A *) 328515SN/A warn "no shebang line in $_script" 329515SN/A set -- $_interpreter 330515SN/A ;; 331515SN/A esac 332515SN/A else 333515SN/A warn "cannot read shebang line from $_script" 334515SN/A set -- $_interpreter 335515SN/A fi 336515SN/A _interp="$* $_procname" # cleanup spaces, add _procname 337515SN/A _interpbn=${1##*/} 338515SN/A _fp_args='_argv' 339515SN/A _fp_match='case "$_argv" in 340515SN/A ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' 341515SN/A else # a normal daemon 342515SN/A _procnamebn=${_procname##*/} 343515SN/A _fp_args='_arg0 _argv' 344515SN/A _fp_match='case "$_arg0" in 345515SN/A $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 346515SN/A fi 347515SN/A 348515SN/A _proccheck="\ 349515SN/A $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 350515SN/A while read _npid _jid '"$_fp_args"'; do 351515SN/A '"$_fp_match"' 352515SN/A if [ "$JID" -eq "$_jid" ]; 353515SN/A then echo -n "$_pref$_npid"; 354515SN/A _pref=" "; 355515SN/A fi 356515SN/A ;; 357515SN/A esac 358515SN/A done' 359515SN/A 360515SN/A# debug "in _find_processes: proccheck is ($_proccheck)." 361515SN/A eval $_proccheck 362515SN/A} 363515SN/A 364515SN/A# sort_lite [-b] [-n] [-k POS] [-t SEP] 365515SN/A# A lite version of sort(1) (supporting a few options) that can be used 366515SN/A# before the real sort(1) is available (e.g., in scripts that run prior 367515SN/A# to mountcritremote). Requires only shell built-in functionality. 368515SN/A# 369515SN/Asort_lite() 370515SN/A{ 371515SN/A local funcname=sort_lite 372515SN/A local sort_sep="$IFS" sort_ignore_leading_space= 373515SN/A local sort_field=0 sort_strict_fields= sort_numeric= 374515SN/A local nitems=0 skip_leading=0 trim= 375515SN/A 376515SN/A local OPTIND flag 377515SN/A while getopts bnk:t: flag; do 378515SN/A case "$flag" in 379515SN/A b) sort_ignore_leading_space=1 ;; 380515SN/A n) sort_numeric=1 sort_ignore_leading_space=1 ;; 381515SN/A k) sort_field="${OPTARG%%,*}" ;; # only up to first comma 382515SN/A # NB: Unlike sort(1) only one POS allowed 383515SN/A t) sort_sep="$OPTARG" 384515SN/A if [ ${#sort_sep} -gt 1 ]; then 385515SN/A echo "$funcname: multi-character tab \`$sort_sep'" >&2 386515SN/A return 1 387515SN/A fi 388515SN/A sort_strict_fields=1 389515SN/A ;; 390515SN/A \?) return 1 ;; 391515SN/A esac 392515SN/A done 393515SN/A shift $(( $OPTIND - 1 )) 394 395 # Create transformation pattern to trim leading text if desired 396 case "$sort_field" in 397 ""|[!0-9]*|*[!0-9.]*) 398 echo "$funcname: invalid sort field \`$sort_field'" >&2 399 return 1 400 ;; 401 *.*) 402 skip_leading=${sort_field#*.} sort_field=${sort_field%%.*} 403 while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do 404 trim="$trim?" skip_leading=$(( $skip_leading - 1 )) 405 done 406 esac 407 408 # Copy input to series of local numbered variables 409 # NB: IFS of NULL preserves leading whitespace 410 local LINE 411 while IFS= read -r LINE || [ "$LINE" ]; do 412 nitems=$(( $nitems + 1 )) 413 local src_$nitems="$LINE" 414 done 415 416 # 417 # Sort numbered locals using insertion sort 418 # 419 local curitem curitem_orig curitem_mod curitem_haskey 420 local dest dest_orig dest_mod dest_haskey 421 local d gt n 422 local i=1 423 while [ $i -le $nitems ]; do 424 curitem_haskey=1 # Assume sort field (-k POS) exists 425 eval curitem=\"\$src_$i\" 426 curitem_mod="$curitem" # for modified comparison 427 curitem_orig="$curitem" # for original comparison 428 429 # Trim leading whitespace if desired 430 if [ "$sort_ignore_leading_space" ]; then 431 while case "$curitem_orig" in 432 [$IFS]*) : ;; *) false; esac 433 do 434 curitem_orig="${curitem_orig#?}" 435 done 436 curitem_mod="$curitem_orig" 437 fi 438 439 # Shift modified comparison value if sort field (-k POS) is > 1 440 n=$sort_field 441 while [ $n -gt 1 ]; do 442 case "$curitem_mod" in 443 *[$sort_sep]*) 444 # Cut text up-to (and incl.) first separator 445 curitem_mod="${curitem_mod#*[$sort_sep]}" 446 447 # Skip NULLs unless strict field splitting 448 [ "$sort_strict_fields" ] || 449 [ "${curitem_mod%%[$sort_sep]*}" ] || 450 [ $n -eq 2 ] || 451 continue 452 ;; 453 *) 454 # Asked for a field that doesn't exist 455 curitem_haskey= break 456 esac 457 n=$(( $n - 1 )) 458 done 459 460 # Trim trailing words if sort field >= 1 461 [ $sort_field -ge 1 -a "$sort_numeric" ] && 462 curitem_mod="${curitem_mod%%[$sort_sep]*}" 463 464 # Apply optional trim (-k POS.TRIM) to cut leading characters 465 curitem_mod="${curitem_mod#$trim}" 466 467 # Determine the type of modified comparison to use initially 468 # NB: Prefer numerical if requested but fallback to standard 469 case "$curitem_mod" in 470 ""|[!0-9]*) # NULL or begins with non-number 471 gt=">" 472 [ "$sort_numeric" ] && curitem_mod=0 473 ;; 474 *) 475 if [ "$sort_numeric" ]; then 476 gt="-gt" 477 curitem_mod="${curitem_mod%%[!0-9]*}" 478 # NB: trailing non-digits removed 479 # otherwise numeric comparison fails 480 else 481 gt=">" 482 fi 483 esac 484 485 # If first time through, short-circuit below position-search 486 if [ $i -le 1 ]; then 487 d=0 488 else 489 d=1 490 fi 491 492 # 493 # Find appropriate element position 494 # 495 while [ $d -gt 0 ] 496 do 497 dest_haskey=$curitem_haskey 498 eval dest=\"\$dest_$d\" 499 dest_mod="$dest" # for modified comparison 500 dest_orig="$dest" # for original comparison 501 502 # Trim leading whitespace if desired 503 if [ "$sort_ignore_leading_space" ]; then 504 while case "$dest_orig" in 505 [$IFS]*) : ;; *) false; esac 506 do 507 dest_orig="${dest_orig#?}" 508 done 509 dest_mod="$dest_orig" 510 fi 511 512 # Shift modified value if sort field (-k POS) is > 1 513 n=$sort_field 514 while [ $n -gt 1 ]; do 515 case "$dest_mod" in 516 *[$sort_sep]*) 517 # Cut text up-to (and incl.) 1st sep 518 dest_mod="${dest_mod#*[$sort_sep]}" 519 520 # Skip NULLs unless strict fields 521 [ "$sort_strict_fields" ] || 522 [ "${dest_mod%%[$sort_sep]*}" ] || 523 [ $n -eq 2 ] || 524 continue 525 ;; 526 *) 527 # Asked for a field that doesn't exist 528 dest_haskey= break 529 esac 530 n=$(( $n - 1 )) 531 done 532 533 # Trim trailing words if sort field >= 1 534 [ $sort_field -ge 1 -a "$sort_numeric" ] && 535 dest_mod="${dest_mod%%[$sort_sep]*}" 536 537 # Apply optional trim (-k POS.TRIM), cut leading chars 538 dest_mod="${dest_mod#$trim}" 539 540 # Determine type of modified comparison to use 541 # NB: Prefer numerical if requested, fallback to std 542 case "$dest_mod" in 543 ""|[!0-9]*) # NULL or begins with non-number 544 gt=">" 545 [ "$sort_numeric" ] && dest_mod=0 546 ;; 547 *) 548 if [ "$sort_numeric" ]; then 549 gt="-gt" 550 dest_mod="${dest_mod%%[!0-9]*}" 551 # NB: kill trailing non-digits 552 # for numeric comparison safety 553 else 554 gt=">" 555 fi 556 esac 557 558 # Break if we've found the proper element position 559 if [ "$curitem_haskey" -a "$dest_haskey" ]; then 560 if [ "$dest_mod" = "$curitem_mod" ]; then 561 [ "$dest_orig" ">" "$curitem_orig" ] && 562 break 563 elif [ "$dest_mod" $gt "$curitem_mod" ] \ 564 2> /dev/null 565 then 566 break 567 fi 568 else 569 [ "$dest_orig" ">" "$curitem_orig" ] && break 570 fi 571 572 # Break if we've hit the end 573 [ $d -ge $i ] && break 574 575 d=$(( $d + 1 )) 576 done 577 578 # Shift remaining positions forward, making room for new item 579 n=$i 580 while [ $n -ge $d ]; do 581 # Shift destination item forward one placement 582 eval dest_$(( $n + 1 ))=\"\$dest_$n\" 583 n=$(( $n - 1 )) 584 done 585 586 # Place the element 587 if [ $i -eq 1 ]; then 588 local dest_1="$curitem" 589 else 590 local dest_$d="$curitem" 591 fi 592 593 i=$(( $i + 1 )) 594 done 595 596 # Print sorted results 597 d=1 598 while [ $d -le $nitems ]; do 599 eval echo \"\$dest_$d\" 600 d=$(( $d + 1 )) 601 done 602} 603 604# 605# wait_for_pids pid [pid ...] 606# spins until none of the pids exist 607# 608wait_for_pids() 609{ 610 local _list _prefix _nlist _j 611 612 _list="$@" 613 if [ -z "$_list" ]; then 614 return 615 fi 616 _prefix= 617 while true; do 618 _nlist=""; 619 for _j in $_list; do 620 if kill -0 $_j 2>/dev/null; then 621 _nlist="${_nlist}${_nlist:+ }$_j" 622 [ -n "$_prefix" ] && sleep 1 623 fi 624 done 625 if [ -z "$_nlist" ]; then 626 break 627 fi 628 _list=$_nlist 629 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 630 _prefix=", " 631 pwait $_list 2>/dev/null 632 done 633 if [ -n "$_prefix" ]; then 634 echo "." 635 fi 636} 637 638# 639# get_pidfile_from_conf string file 640# 641# Takes a string to search for in the specified file. 642# Ignores lines with traditional comment characters. 643# 644# Example: 645# 646# if get_pidfile_from_conf string file; then 647# pidfile="$_pidfile_from_conf" 648# else 649# pidfile='appropriate default' 650# fi 651# 652get_pidfile_from_conf() 653{ 654 if [ -z "$1" -o -z "$2" ]; then 655 err 3 "USAGE: get_pidfile_from_conf string file ($name)" 656 fi 657 658 local string file line 659 660 string="$1" ; file="$2" 661 662 if [ ! -s "$file" ]; then 663 err 3 "get_pidfile_from_conf: $file does not exist ($name)" 664 fi 665 666 while read line; do 667 case "$line" in 668 *[#\;]*${string}*) continue ;; 669 *${string}*) break ;; 670 esac 671 done < $file 672 673 if [ -n "$line" ]; then 674 line=${line#*/} 675 _pidfile_from_conf="/${line%%[\"\;]*}" 676 else 677 return 1 678 fi 679} 680 681# 682# check_startmsgs 683# If rc_quiet is set (usually as a result of using faststart at 684# boot time) check if rc_startmsgs is enabled. 685# 686check_startmsgs() 687{ 688 if [ -n "$rc_quiet" ]; then 689 checkyesno rc_startmsgs 690 else 691 return 0 692 fi 693} 694 695# 696# run_rc_command argument 697# Search for argument in the list of supported commands, which is: 698# "start stop restart rcvar status poll ${extra_commands}" 699# If there's a match, run ${argument}_cmd or the default method 700# (see below). 701# 702# If argument has a given prefix, then change the operation as follows: 703# Prefix Operation 704# ------ --------- 705# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 706# force Set ${rcvar} to YES, and set rc_force=yes 707# one Set ${rcvar} to YES 708# quiet Don't output some diagnostics, and set rc_quiet=yes 709# 710# The following globals are used: 711# 712# Name Needed Purpose 713# ---- ------ ------- 714# name y Name of script. 715# 716# command n Full path to command. 717# Not needed if ${rc_arg}_cmd is set for 718# each keyword. 719# 720# command_args n Optional args/shell directives for command. 721# 722# command_interpreter n If not empty, command is interpreted, so 723# call check_{pidfile,process}() appropriately. 724# 725# desc n Description of script. 726# 727# extra_commands n List of extra commands supported. 728# 729# pidfile n If set, use check_pidfile $pidfile $command, 730# otherwise use check_process $command. 731# In either case, only check if $command is set. 732# 733# procname n Process name to check for instead of $command. 734# 735# rcvar n This is checked with checkyesno to determine 736# if the action should be run. 737# 738# ${name}_program n Full path to command. 739# Meant to be used in /etc/rc.conf to override 740# ${command}. 741# 742# ${name}_chroot n Directory to chroot to before running ${command} 743# Requires /usr to be mounted. 744# 745# ${name}_chdir n Directory to cd to before running ${command} 746# (if not using ${name}_chroot). 747# 748# ${name}_flags n Arguments to call ${command} with. 749# NOTE: $flags from the parent environment 750# can be used to override this. 751# 752# ${name}_env n Environment variables to run ${command} with. 753# 754# ${name}_fib n Routing table number to run ${command} with. 755# 756# ${name}_nice n Nice level to run ${command} at. 757# 758# ${name}_user n User to run ${command} as, using su(1) if not 759# using ${name}_chroot. 760# Requires /usr to be mounted. 761# 762# ${name}_group n Group to run chrooted ${command} as. 763# Requires /usr to be mounted. 764# 765# ${name}_groups n Comma separated list of supplementary groups 766# to run the chrooted ${command} with. 767# Requires /usr to be mounted. 768# 769# ${name}_prepend n Command added before ${command}. 770# 771# ${rc_arg}_cmd n If set, use this as the method when invoked; 772# Otherwise, use default command (see below) 773# 774# ${rc_arg}_precmd n If set, run just before performing the 775# ${rc_arg}_cmd method in the default 776# operation (i.e, after checking for required 777# bits and process (non)existence). 778# If this completes with a non-zero exit code, 779# don't run ${rc_arg}_cmd. 780# 781# ${rc_arg}_postcmd n If set, run just after performing the 782# ${rc_arg}_cmd method, if that method 783# returned a zero exit code. 784# 785# required_dirs n If set, check for the existence of the given 786# directories before running a (re)start command. 787# 788# required_files n If set, check for the readability of the given 789# files before running a (re)start command. 790# 791# required_modules n If set, ensure the given kernel modules are 792# loaded before running a (re)start command. 793# The check and possible loads are actually 794# done after start_precmd so that the modules 795# aren't loaded in vain, should the precmd 796# return a non-zero status to indicate a error. 797# If a word in the list looks like "foo:bar", 798# "foo" is the KLD file name and "bar" is the 799# module name. If a word looks like "foo~bar", 800# "foo" is the KLD file name and "bar" is a 801# egrep(1) pattern matching the module name. 802# Otherwise the module name is assumed to be 803# the same as the KLD file name, which is most 804# common. See load_kld(). 805# 806# required_vars n If set, perform checkyesno on each of the 807# listed variables before running the default 808# (re)start command. 809# 810# Default behaviour for a given argument, if no override method is 811# provided: 812# 813# Argument Default behaviour 814# -------- ----------------- 815# start if !running && checkyesno ${rcvar} 816# ${command} 817# 818# stop if ${pidfile} 819# rc_pid=$(check_pidfile $pidfile $command) 820# else 821# rc_pid=$(check_process $command) 822# kill $sig_stop $rc_pid 823# wait_for_pids $rc_pid 824# ($sig_stop defaults to TERM.) 825# 826# reload Similar to stop, except use $sig_reload instead, 827# and doesn't wait_for_pids. 828# $sig_reload defaults to HUP. 829# Note that `reload' isn't provided by default, 830# it should be enabled via $extra_commands. 831# 832# restart Run `stop' then `start'. 833# 834# status Show if ${command} is running, etc. 835# 836# poll Wait for ${command} to exit. 837# 838# rcvar Display what rc.conf variable is used (if any). 839# 840# enabled Return true if the service is enabled. 841# 842# Variables available to methods, and after run_rc_command() has 843# completed: 844# 845# Variable Purpose 846# -------- ------- 847# rc_arg Argument to command, after fast/force/one processing 848# performed 849# 850# rc_flags Flags to start the default command with. 851# Defaults to ${name}_flags, unless overridden 852# by $flags from the environment. 853# This variable may be changed by the precmd method. 854# 855# rc_pid PID of command (if appropriate) 856# 857# rc_fast Not empty if "fast" was provided (q.v.) 858# 859# rc_force Not empty if "force" was provided (q.v.) 860# 861# rc_quiet Not empty if "quiet" was provided 862# 863# 864run_rc_command() 865{ 866 _return=0 867 rc_arg=$1 868 if [ -z "$name" ]; then 869 err 3 'run_rc_command: $name is not set.' 870 fi 871 872 # Don't repeat the first argument when passing additional command- 873 # line arguments to the command subroutines. 874 # 875 shift 1 876 rc_extra_args="$*" 877 878 _rc_prefix= 879 case "$rc_arg" in 880 fast*) # "fast" prefix; don't check pid 881 rc_arg=${rc_arg#fast} 882 rc_fast=yes 883 rc_quiet=yes 884 ;; 885 force*) # "force" prefix; always run 886 rc_force=yes 887 _rc_prefix=force 888 rc_arg=${rc_arg#${_rc_prefix}} 889 if [ -n "${rcvar}" ]; then 890 eval ${rcvar}=YES 891 fi 892 ;; 893 one*) # "one" prefix; set ${rcvar}=yes 894 _rc_prefix=one 895 rc_arg=${rc_arg#${_rc_prefix}} 896 if [ -n "${rcvar}" ]; then 897 eval ${rcvar}=YES 898 fi 899 ;; 900 quiet*) # "quiet" prefix; omit some messages 901 _rc_prefix=quiet 902 rc_arg=${rc_arg#${_rc_prefix}} 903 rc_quiet=yes 904 ;; 905 esac 906 907 eval _override_command=\$${name}_program 908 command=${_override_command:-$command} 909 910 _keywords="start stop restart rcvar enabled $extra_commands" 911 rc_pid= 912 _pidcmd= 913 _procname=${procname:-${command}} 914 915 # setup pid check command 916 if [ -n "$_procname" ]; then 917 if [ -n "$pidfile" ]; then 918 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 919 else 920 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 921 fi 922 if [ -n "$_pidcmd" ]; then 923 _keywords="${_keywords} status poll" 924 fi 925 fi 926 927 if [ -z "$rc_arg" ]; then 928 rc_usage $_keywords 929 fi 930 931 if [ "$rc_arg" = "enabled" ] ; then 932 checkyesno ${rcvar} 933 return $? 934 fi 935 936 if [ -n "$flags" ]; then # allow override from environment 937 rc_flags=$flags 938 else 939 eval rc_flags=\$${name}_flags 940 fi 941 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 942 _nice=\$${name}_nice _user=\$${name}_user \ 943 _group=\$${name}_group _groups=\$${name}_groups \ 944 _fib=\$${name}_fib _env=\$${name}_env \ 945 _prepend=\$${name}_prepend 946 947 if [ -n "$_user" ]; then # unset $_user if running as that user 948 if [ "$_user" = "$(eval $IDCMD)" ]; then 949 unset _user 950 fi 951 fi 952 953 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 954 955 for _elem in $_keywords; do 956 if [ "$_elem" != "$rc_arg" ]; then 957 continue 958 fi 959 # if ${rcvar} is set, $1 is not "rcvar" 960 # and ${rc_pid} is not set, then run 961 # checkyesno ${rcvar} 962 # and return if that failed 963 # 964 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 965 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 966 if ! checkyesno ${rcvar}; then 967 if [ -n "${rc_quiet}" ]; then 968 return 0 969 fi 970 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 971 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 972 echo "instead of '${rc_arg}'." 973 return 0 974 fi 975 fi 976 977 # if there's a custom ${XXX_cmd}, 978 # run that instead of the default 979 # 980 eval _cmd=\$${rc_arg}_cmd \ 981 _precmd=\$${rc_arg}_precmd \ 982 _postcmd=\$${rc_arg}_postcmd 983 984 if [ -n "$_cmd" ]; then 985 _run_rc_precmd || return 1 986 _run_rc_doit "$_cmd $rc_extra_args" || return 1 987 _run_rc_postcmd 988 return $_return 989 fi 990 991 case "$rc_arg" in # default operations... 992 993 status) 994 _run_rc_precmd || return 1 995 if [ -n "$rc_pid" ]; then 996 echo "${name} is running as pid $rc_pid." 997 else 998 echo "${name} is not running." 999 return 1 1000 fi 1001 _run_rc_postcmd 1002 ;; 1003 1004 start) 1005 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 1006 if [ -z "$rc_quiet" ]; then 1007 echo 1>&2 "${name} already running? " \ 1008 "(pid=$rc_pid)." 1009 fi 1010 return 1 1011 fi 1012 1013 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 1014 warn "run_rc_command: cannot run $command" 1015 return 1 1016 fi 1017 1018 if ! _run_rc_precmd; then 1019 warn "failed precmd routine for ${name}" 1020 return 1 1021 fi 1022 1023 # setup the full command to run 1024 # 1025 check_startmsgs && echo "Starting ${name}." 1026 if [ -n "$_chroot" ]; then 1027 _doit="\ 1028${_nice:+nice -n $_nice }\ 1029${_fib:+setfib -F $_fib }\ 1030${_env:+env $_env }\ 1031chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 1032$_chroot $command $rc_flags $command_args" 1033 else 1034 _doit="\ 1035${_chdir:+cd $_chdir && }\ 1036${_fib:+setfib -F $_fib }\ 1037${_env:+env $_env }\ 1038$command $rc_flags $command_args" 1039 if [ -n "$_user" ]; then 1040 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 1041 fi 1042 if [ -n "$_nice" ]; then 1043 if [ -z "$_user" ]; then 1044 _doit="sh -c \"$_doit\"" 1045 fi 1046 _doit="nice -n $_nice $_doit" 1047 fi 1048 if [ -n "$_prepend" ]; then 1049 _doit="$_prepend $_doit" 1050 fi 1051 fi 1052 1053 # run the full command 1054 # 1055 if ! _run_rc_doit "$_doit"; then 1056 warn "failed to start ${name}" 1057 return 1 1058 fi 1059 1060 # finally, run postcmd 1061 # 1062 _run_rc_postcmd 1063 ;; 1064 1065 stop) 1066 if [ -z "$rc_pid" ]; then 1067 [ -n "$rc_fast" ] && return 0 1068 _run_rc_notrunning 1069 return 1 1070 fi 1071 1072 _run_rc_precmd || return 1 1073 1074 # send the signal to stop 1075 # 1076 echo "Stopping ${name}." 1077 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1078 _run_rc_doit "$_doit" || return 1 1079 1080 # wait for the command to exit, 1081 # and run postcmd. 1082 wait_for_pids $rc_pid 1083 1084 _run_rc_postcmd 1085 ;; 1086 1087 reload) 1088 if [ -z "$rc_pid" ]; then 1089 _run_rc_notrunning 1090 return 1 1091 fi 1092 1093 _run_rc_precmd || return 1 1094 1095 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1096 _run_rc_doit "$_doit" || return 1 1097 1098 _run_rc_postcmd 1099 ;; 1100 1101 restart) 1102 # prevent restart being called more 1103 # than once by any given script 1104 # 1105 if ${_rc_restart_done:-false}; then 1106 return 0 1107 fi 1108 _rc_restart_done=true 1109 1110 _run_rc_precmd || return 1 1111 1112 # run those in a subshell to keep global variables 1113 ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1114 ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1115 _return=$? 1116 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 1117 1118 _run_rc_postcmd 1119 ;; 1120 1121 poll) 1122 _run_rc_precmd || return 1 1123 if [ -n "$rc_pid" ]; then 1124 wait_for_pids $rc_pid 1125 fi 1126 _run_rc_postcmd 1127 ;; 1128 1129 rcvar) 1130 echo -n "# $name" 1131 if [ -n "$desc" ]; then 1132 echo " : $desc" 1133 else 1134 echo "" 1135 fi 1136 echo "#" 1137 # Get unique vars in $rcvar $rcvars 1138 for _v in $rcvar $rcvars; do 1139 case $v in 1140 $_v\ *|\ *$_v|*\ $_v\ *) ;; 1141 *) v="${v# } $_v" ;; 1142 esac 1143 done 1144 1145 # Display variables. 1146 for _v in $v; do 1147 if [ -z "$_v" ]; then 1148 continue 1149 fi 1150 1151 eval _desc=\$${_v}_desc 1152 eval _defval=\$${_v}_defval 1153 _h="-" 1154 1155 eval echo \"$_v=\\\"\$$_v\\\"\" 1156 # decode multiple lines of _desc 1157 while [ -n "$_desc" ]; do 1158 case $_desc in 1159 *^^*) 1160 echo "# $_h ${_desc%%^^*}" 1161 _desc=${_desc#*^^} 1162 _h=" " 1163 ;; 1164 *) 1165 echo "# $_h ${_desc}" 1166 break 1167 ;; 1168 esac 1169 done 1170 echo "# (default: \"$_defval\")" 1171 done 1172 echo "" 1173 ;; 1174 1175 *) 1176 rc_usage $_keywords 1177 ;; 1178 1179 esac 1180 return $_return 1181 done 1182 1183 echo 1>&2 "$0: unknown directive '$rc_arg'." 1184 rc_usage $_keywords 1185 # not reached 1186} 1187 1188# 1189# Helper functions for run_rc_command: common code. 1190# They use such global variables besides the exported rc_* ones: 1191# 1192# name R/W 1193# ------------------ 1194# _precmd R 1195# _postcmd R 1196# _return W 1197# 1198_run_rc_precmd() 1199{ 1200 check_required_before "$rc_arg" || return 1 1201 1202 if [ -n "$_precmd" ]; then 1203 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1204 eval "$_precmd $rc_extra_args" 1205 _return=$? 1206 1207 # If precmd failed and force isn't set, request exit. 1208 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1209 return 1 1210 fi 1211 fi 1212 1213 check_required_after "$rc_arg" || return 1 1214 1215 return 0 1216} 1217 1218_run_rc_postcmd() 1219{ 1220 if [ -n "$_postcmd" ]; then 1221 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1222 eval "$_postcmd $rc_extra_args" 1223 _return=$? 1224 fi 1225 return 0 1226} 1227 1228_run_rc_doit() 1229{ 1230 debug "run_rc_command: doit: $*" 1231 eval "$@" 1232 _return=$? 1233 1234 # If command failed and force isn't set, request exit. 1235 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1236 return 1 1237 fi 1238 1239 return 0 1240} 1241 1242_run_rc_notrunning() 1243{ 1244 local _pidmsg 1245 1246 if [ -n "$pidfile" ]; then 1247 _pidmsg=" (check $pidfile)." 1248 else 1249 _pidmsg= 1250 fi 1251 echo 1>&2 "${name} not running?${_pidmsg}" 1252} 1253 1254_run_rc_killcmd() 1255{ 1256 local _cmd 1257 1258 _cmd="kill -$1 $rc_pid" 1259 if [ -n "$_user" ]; then 1260 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1261 fi 1262 echo "$_cmd" 1263} 1264 1265# 1266# run_rc_script file arg 1267# Start the script `file' with `arg', and correctly handle the 1268# return value from the script. 1269# If `file' ends with `.sh', it's sourced into the current environment 1270# when $rc_fast_and_loose is set, otherwise it is run as a child process. 1271# If `file' appears to be a backup or scratch file, ignore it. 1272# Otherwise if it is executable run as a child process. 1273# 1274run_rc_script() 1275{ 1276 _file=$1 1277 _arg=$2 1278 if [ -z "$_file" -o -z "$_arg" ]; then 1279 err 3 'USAGE: run_rc_script file arg' 1280 fi 1281 1282 unset name command command_args command_interpreter \ 1283 extra_commands pidfile procname \ 1284 rcvar rcvars rcvars_obsolete required_dirs required_files \ 1285 required_vars 1286 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 1287 1288 case "$_file" in 1289 /etc/rc.d/*.sh) # no longer allowed in the base 1290 warn "Ignoring old-style startup script $_file" 1291 ;; 1292 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 1293 warn "Ignoring scratch file $_file" 1294 ;; 1295 *) # run in subshell 1296 if [ -x $_file ]; then 1297 if [ -n "$rc_fast_and_loose" ]; then 1298 set $_arg; . $_file 1299 else 1300 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1301 trap "echo Script $_file interrupted >&2 ; exit 1" 2 1302 trap "echo Script $_file running >&2" 29 1303 set $_arg; . $_file ) 1304 fi 1305 fi 1306 ;; 1307 esac 1308} 1309 1310# 1311# load_rc_config name 1312# Source in the configuration file for a given name. 1313# 1314load_rc_config() 1315{ 1316 local _name _rcvar_val _var _defval _v _msg _new _d 1317 _name=$1 1318 1319 if ${_rc_conf_loaded:-false}; then 1320 : 1321 else 1322 if [ -r /etc/defaults/rc.conf ]; then 1323 debug "Sourcing /etc/defaults/rc.conf" 1324 . /etc/defaults/rc.conf 1325 source_rc_confs 1326 elif [ -r /etc/rc.conf ]; then 1327 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 1328 . /etc/rc.conf 1329 fi 1330 _rc_conf_loaded=true 1331 fi 1332 1333 # If a service name was specified, attempt to load 1334 # service-specific configuration 1335 if [ -n "$_name" ] ; then 1336 for _d in /etc ${local_startup}; do 1337 _d=${_d%/rc.d} 1338 if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1339 debug "Sourcing ${_d}/rc.conf.d/$_name" 1340 . ${_d}/rc.conf.d/"$_name" 1341 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1342 local _rc 1343 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1344 if [ -f "$_rc" ] ; then 1345 debug "Sourcing $_rc" 1346 . "$_rc" 1347 fi 1348 done 1349 fi 1350 done 1351 fi 1352 1353 # Set defaults if defined. 1354 for _var in $rcvar $rcvars; do 1355 eval _defval=\$${_var}_defval 1356 if [ -n "$_defval" ]; then 1357 eval : \${$_var:=\$${_var}_defval} 1358 fi 1359 done 1360 1361 # check obsolete rc.conf variables 1362 for _var in $rcvars_obsolete; do 1363 eval _v=\$$_var 1364 eval _msg=\$${_var}_obsolete_msg 1365 eval _new=\$${_var}_newvar 1366 case $_v in 1367 "") 1368 ;; 1369 *) 1370 if [ -z "$_new" ]; then 1371 _msg="Ignored." 1372 else 1373 eval $_new=\"\$$_var\" 1374 if [ -z "$_msg" ]; then 1375 _msg="Use \$$_new instead." 1376 fi 1377 fi 1378 warn "\$$_var is obsolete. $_msg" 1379 ;; 1380 esac 1381 done 1382} 1383 1384# 1385# load_rc_config_var name var 1386# Read the rc.conf(5) var for name and set in the 1387# current shell, using load_rc_config in a subshell to prevent 1388# unwanted side effects from other variable assignments. 1389# 1390load_rc_config_var() 1391{ 1392 if [ $# -ne 2 ]; then 1393 err 3 'USAGE: load_rc_config_var name var' 1394 fi 1395 eval $(eval '( 1396 load_rc_config '$1' >/dev/null; 1397 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1398 echo '$2'=\'\''${'$2'}\'\''; 1399 fi 1400 )' ) 1401} 1402 1403# 1404# rc_usage commands 1405# Print a usage string for $0, with `commands' being a list of 1406# valid commands. 1407# 1408rc_usage() 1409{ 1410 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 1411 1412 _sep= 1413 for _elem; do 1414 echo -n 1>&2 "$_sep$_elem" 1415 _sep="|" 1416 done 1417 echo 1>&2 ")" 1418 exit 1 1419} 1420 1421# 1422# err exitval message 1423# Display message to stderr and log to the syslog, and exit with exitval. 1424# 1425err() 1426{ 1427 exitval=$1 1428 shift 1429 1430 if [ -x /usr/bin/logger ]; then 1431 logger "$0: ERROR: $*" 1432 fi 1433 echo 1>&2 "$0: ERROR: $*" 1434 exit $exitval 1435} 1436 1437# 1438# warn message 1439# Display message to stderr and log to the syslog. 1440# 1441warn() 1442{ 1443 if [ -x /usr/bin/logger ]; then 1444 logger "$0: WARNING: $*" 1445 fi 1446 echo 1>&2 "$0: WARNING: $*" 1447} 1448 1449# 1450# info message 1451# Display informational message to stdout and log to syslog. 1452# 1453info() 1454{ 1455 case ${rc_info} in 1456 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1457 if [ -x /usr/bin/logger ]; then 1458 logger "$0: INFO: $*" 1459 fi 1460 echo "$0: INFO: $*" 1461 ;; 1462 esac 1463} 1464 1465# 1466# debug message 1467# If debugging is enabled in rc.conf output message to stderr. 1468# BEWARE that you don't call any subroutine that itself calls this 1469# function. 1470# 1471debug() 1472{ 1473 case ${rc_debug} in 1474 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1475 if [ -x /usr/bin/logger ]; then 1476 logger "$0: DEBUG: $*" 1477 fi 1478 echo 1>&2 "$0: DEBUG: $*" 1479 ;; 1480 esac 1481} 1482 1483# 1484# backup_file action file cur backup 1485# Make a backup copy of `file' into `cur', and save the previous 1486# version of `cur' as `backup' or use rcs for archiving. 1487# 1488# This routine checks the value of the backup_uses_rcs variable, 1489# which can be either YES or NO. 1490# 1491# The `action' keyword can be one of the following: 1492# 1493# add `file' is now being backed up (and is possibly 1494# being reentered into the backups system). `cur' 1495# is created and RCS files, if necessary, are 1496# created as well. 1497# 1498# update `file' has changed and needs to be backed up. 1499# If `cur' exists, it is copied to to `back' or 1500# checked into RCS (if the repository file is old), 1501# and then `file' is copied to `cur'. Another RCS 1502# check in done here if RCS is being used. 1503# 1504# remove `file' is no longer being tracked by the backups 1505# system. If RCS is not being used, `cur' is moved 1506# to `back', otherwise an empty file is checked in, 1507# and then `cur' is removed. 1508# 1509# 1510backup_file() 1511{ 1512 _action=$1 1513 _file=$2 1514 _cur=$3 1515 _back=$4 1516 1517 if checkyesno backup_uses_rcs; then 1518 _msg0="backup archive" 1519 _msg1="update" 1520 1521 # ensure that history file is not locked 1522 if [ -f $_cur,v ]; then 1523 rcs -q -u -U -M $_cur 1524 fi 1525 1526 # ensure after switching to rcs that the 1527 # current backup is not lost 1528 if [ -f $_cur ]; then 1529 # no archive, or current newer than archive 1530 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 1531 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1532 rcs -q -kb -U $_cur 1533 co -q -f -u $_cur 1534 fi 1535 fi 1536 1537 case $_action in 1538 add|update) 1539 cp -p $_file $_cur 1540 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1541 rcs -q -kb -U $_cur 1542 co -q -f -u $_cur 1543 chown root:wheel $_cur $_cur,v 1544 ;; 1545 remove) 1546 cp /dev/null $_cur 1547 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1548 rcs -q -kb -U $_cur 1549 chown root:wheel $_cur $_cur,v 1550 rm $_cur 1551 ;; 1552 esac 1553 else 1554 case $_action in 1555 add|update) 1556 if [ -f $_cur ]; then 1557 cp -p $_cur $_back 1558 fi 1559 cp -p $_file $_cur 1560 chown root:wheel $_cur 1561 ;; 1562 remove) 1563 mv -f $_cur $_back 1564 ;; 1565 esac 1566 fi 1567} 1568 1569# make_symlink src link 1570# Make a symbolic link 'link' to src from basedir. If the 1571# directory in which link is to be created does not exist 1572# a warning will be displayed and an error will be returned. 1573# Returns 0 on success, 1 otherwise. 1574# 1575make_symlink() 1576{ 1577 local src link linkdir _me 1578 src="$1" 1579 link="$2" 1580 linkdir="`dirname $link`" 1581 _me="make_symlink()" 1582 1583 if [ -z "$src" -o -z "$link" ]; then 1584 warn "$_me: requires two arguments." 1585 return 1 1586 fi 1587 if [ ! -d "$linkdir" ]; then 1588 warn "$_me: the directory $linkdir does not exist." 1589 return 1 1590 fi 1591 if ! ln -sf $src $link; then 1592 warn "$_me: unable to make a symbolic link from $link to $src" 1593 return 1 1594 fi 1595 return 0 1596} 1597 1598# devfs_rulesets_from_file file 1599# Reads a set of devfs commands from file, and creates 1600# the specified rulesets with their rules. Returns non-zero 1601# if there was an error. 1602# 1603devfs_rulesets_from_file() 1604{ 1605 local file _err _me _opts 1606 file="$1" 1607 _me="devfs_rulesets_from_file" 1608 _err=0 1609 1610 if [ -z "$file" ]; then 1611 warn "$_me: you must specify a file" 1612 return 1 1613 fi 1614 if [ ! -e "$file" ]; then 1615 debug "$_me: no such file ($file)" 1616 return 0 1617 fi 1618 1619 # Disable globbing so that the rule patterns are not expanded 1620 # by accident with matching filesystem entries. 1621 _opts=$-; set -f 1622 1623 debug "reading rulesets from file ($file)" 1624 { while read line 1625 do 1626 case $line in 1627 \#*) 1628 continue 1629 ;; 1630 \[*\]*) 1631 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1632 if [ -z "$rulenum" ]; then 1633 warn "$_me: cannot extract rule number ($line)" 1634 _err=1 1635 break 1636 fi 1637 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1638 if [ -z "$rulename" ]; then 1639 warn "$_me: cannot extract rule name ($line)" 1640 _err=1 1641 break; 1642 fi 1643 eval $rulename=\$rulenum 1644 debug "found ruleset: $rulename=$rulenum" 1645 if ! /sbin/devfs rule -s $rulenum delset; then 1646 _err=1 1647 break 1648 fi 1649 ;; 1650 *) 1651 rulecmd="${line%%"\#*"}" 1652 # evaluate the command incase it includes 1653 # other rules 1654 if [ -n "$rulecmd" ]; then 1655 debug "adding rule ($rulecmd)" 1656 if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1657 then 1658 _err=1 1659 break 1660 fi 1661 fi 1662 ;; 1663 esac 1664 if [ $_err -ne 0 ]; then 1665 debug "error in $_me" 1666 break 1667 fi 1668 done } < $file 1669 case $_opts in *f*) ;; *) set +f ;; esac 1670 return $_err 1671} 1672 1673# devfs_init_rulesets 1674# Initializes rulesets from configuration files. Returns 1675# non-zero if there was an error. 1676# 1677devfs_init_rulesets() 1678{ 1679 local file _me 1680 _me="devfs_init_rulesets" 1681 1682 # Go through this only once 1683 if [ -n "$devfs_rulesets_init" ]; then 1684 debug "$_me: devfs rulesets already initialized" 1685 return 1686 fi 1687 for file in $devfs_rulesets; do 1688 if ! devfs_rulesets_from_file $file; then 1689 warn "$_me: could not read rules from $file" 1690 return 1 1691 fi 1692 done 1693 devfs_rulesets_init=1 1694 debug "$_me: devfs rulesets initialized" 1695 return 0 1696} 1697 1698# devfs_set_ruleset ruleset [dir] 1699# Sets the default ruleset of dir to ruleset. The ruleset argument 1700# must be a ruleset name as specified in devfs.rules(5) file. 1701# Returns non-zero if it could not set it successfully. 1702# 1703devfs_set_ruleset() 1704{ 1705 local devdir rs _me 1706 [ -n "$1" ] && eval rs=\$$1 || rs= 1707 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1708 _me="devfs_set_ruleset" 1709 1710 if [ -z "$rs" ]; then 1711 warn "$_me: you must specify a ruleset number" 1712 return 1 1713 fi 1714 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1715 if ! /sbin/devfs $devdir ruleset $rs; then 1716 warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1717 return 1 1718 fi 1719 return 0 1720} 1721 1722# devfs_apply_ruleset ruleset [dir] 1723# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1724# The ruleset argument must be a ruleset name as specified 1725# in a devfs.rules(5) file. Returns 0 on success or non-zero 1726# if it could not apply the ruleset. 1727# 1728devfs_apply_ruleset() 1729{ 1730 local devdir rs _me 1731 [ -n "$1" ] && eval rs=\$$1 || rs= 1732 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1733 _me="devfs_apply_ruleset" 1734 1735 if [ -z "$rs" ]; then 1736 warn "$_me: you must specify a ruleset" 1737 return 1 1738 fi 1739 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1740 if ! /sbin/devfs $devdir rule -s $rs applyset; then 1741 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1742 return 1 1743 fi 1744 return 0 1745} 1746 1747# devfs_domount dir [ruleset] 1748# Mount devfs on dir. If ruleset is specified it is set 1749# on the mount-point. It must also be a ruleset name as specified 1750# in a devfs.rules(5) file. Returns 0 on success. 1751# 1752devfs_domount() 1753{ 1754 local devdir rs _me 1755 devdir="$1" 1756 [ -n "$2" ] && rs=$2 || rs= 1757 _me="devfs_domount()" 1758 1759 if [ -z "$devdir" ]; then 1760 warn "$_me: you must specify a mount-point" 1761 return 1 1762 fi 1763 debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1764 if ! mount -t devfs dev "$devdir"; then 1765 warn "$_me: Unable to mount devfs on $devdir" 1766 return 1 1767 fi 1768 if [ -n "$rs" ]; then 1769 devfs_init_rulesets 1770 devfs_set_ruleset $rs $devdir 1771 devfs -m $devdir rule applyset 1772 fi 1773 return 0 1774} 1775 1776# Provide a function for normalizing the mounting of memory 1777# filesystems. This should allow the rest of the code here to remain 1778# as close as possible between 5-current and 4-stable. 1779# $1 = size 1780# $2 = mount point 1781# $3 = (optional) extra mdmfs flags 1782mount_md() 1783{ 1784 if [ -n "$3" ]; then 1785 flags="$3" 1786 fi 1787 /sbin/mdmfs $flags -s $1 md $2 1788} 1789 1790# Code common to scripts that need to load a kernel module 1791# if it isn't in the kernel yet. Syntax: 1792# load_kld [-e regex] [-m module] file 1793# where -e or -m chooses the way to check if the module 1794# is already loaded: 1795# regex is egrep'd in the output from `kldstat -v', 1796# module is passed to `kldstat -m'. 1797# The default way is as though `-m file' were specified. 1798load_kld() 1799{ 1800 local _loaded _mod _opt _re 1801 1802 while getopts "e:m:" _opt; do 1803 case "$_opt" in 1804 e) _re="$OPTARG" ;; 1805 m) _mod="$OPTARG" ;; 1806 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1807 esac 1808 done 1809 shift $(($OPTIND - 1)) 1810 if [ $# -ne 1 ]; then 1811 err 3 'USAGE: load_kld [-e regex] [-m module] file' 1812 fi 1813 _mod=${_mod:-$1} 1814 _loaded=false 1815 if [ -n "$_re" ]; then 1816 if kldstat -v | egrep -q -e "$_re"; then 1817 _loaded=true 1818 fi 1819 else 1820 if kldstat -q -m "$_mod"; then 1821 _loaded=true 1822 fi 1823 fi 1824 if ! $_loaded; then 1825 if ! kldload "$1"; then 1826 warn "Unable to load kernel module $1" 1827 return 1 1828 else 1829 info "$1 kernel module loaded." 1830 fi 1831 else 1832 debug "load_kld: $1 kernel module already loaded." 1833 fi 1834 return 0 1835} 1836 1837# ltr str src dst [var] 1838# Change every $src in $str to $dst. 1839# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1840# awk(1). If var is non-NULL, set it to the result. 1841ltr() 1842{ 1843 local _str _src _dst _out _com _var 1844 _str="$1" 1845 _src="$2" 1846 _dst="$3" 1847 _var="$4" 1848 _out="" 1849 1850 local IFS="${_src}" 1851 for _com in ${_str}; do 1852 if [ -z "${_out}" ]; then 1853 _out="${_com}" 1854 else 1855 _out="${_out}${_dst}${_com}" 1856 fi 1857 done 1858 if [ -n "${_var}" ]; then 1859 setvar "${_var}" "${_out}" 1860 else 1861 echo "${_out}" 1862 fi 1863} 1864 1865# Creates a list of providers for GELI encryption. 1866geli_make_list() 1867{ 1868 local devices devices2 1869 local provider mountpoint type options rest 1870 1871 # Create list of GELI providers from fstab. 1872 while read provider mountpoint type options rest ; do 1873 case ":${options}" in 1874 :*noauto*) 1875 noauto=yes 1876 ;; 1877 *) 1878 noauto=no 1879 ;; 1880 esac 1881 1882 case ":${provider}" in 1883 :#*) 1884 continue 1885 ;; 1886 *.eli) 1887 # Skip swap devices. 1888 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1889 continue 1890 fi 1891 devices="${devices} ${provider}" 1892 ;; 1893 esac 1894 done < /etc/fstab 1895 1896 # Append providers from geli_devices. 1897 devices="${devices} ${geli_devices}" 1898 1899 for provider in ${devices}; do 1900 provider=${provider%.eli} 1901 provider=${provider#/dev/} 1902 devices2="${devices2} ${provider}" 1903 done 1904 1905 echo ${devices2} 1906} 1907 1908# Find scripts in local_startup directories that use the old syntax 1909# 1910find_local_scripts_old() { 1911 zlist='' 1912 slist='' 1913 for dir in ${local_startup}; do 1914 if [ -d "${dir}" ]; then 1915 for file in ${dir}/[0-9]*.sh; do 1916 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1917 continue 1918 zlist="$zlist $file" 1919 done 1920 for file in ${dir}/[!0-9]*.sh; do 1921 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1922 continue 1923 slist="$slist $file" 1924 done 1925 fi 1926 done 1927} 1928 1929find_local_scripts_new() { 1930 local_rc='' 1931 for dir in ${local_startup}; do 1932 if [ -d "${dir}" ]; then 1933 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1934 case "$file" in 1935 *.sample) ;; 1936 *) if [ -x "$file" ]; then 1937 local_rc="${local_rc} ${file}" 1938 fi 1939 ;; 1940 esac 1941 done 1942 fi 1943 done 1944} 1945 1946# check_required_{before|after} command 1947# Check for things required by the command before and after its precmd, 1948# respectively. The two separate functions are needed because some 1949# conditions should prevent precmd from being run while other things 1950# depend on precmd having already been run. 1951# 1952check_required_before() 1953{ 1954 local _f 1955 1956 case "$1" in 1957 start) 1958 for _f in $required_vars; do 1959 if ! checkyesno $_f; then 1960 warn "\$${_f} is not enabled." 1961 if [ -z "$rc_force" ]; then 1962 return 1 1963 fi 1964 fi 1965 done 1966 1967 for _f in $required_dirs; do 1968 if [ ! -d "${_f}/." ]; then 1969 warn "${_f} is not a directory." 1970 if [ -z "$rc_force" ]; then 1971 return 1 1972 fi 1973 fi 1974 done 1975 1976 for _f in $required_files; do 1977 if [ ! -r "${_f}" ]; then 1978 warn "${_f} is not readable." 1979 if [ -z "$rc_force" ]; then 1980 return 1 1981 fi 1982 fi 1983 done 1984 ;; 1985 esac 1986 1987 return 0 1988} 1989 1990check_required_after() 1991{ 1992 local _f _args 1993 1994 case "$1" in 1995 start) 1996 for _f in $required_modules; do 1997 case "${_f}" in 1998 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1999 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 2000 *) _args="${_f}" ;; 2001 esac 2002 if ! load_kld ${_args}; then 2003 if [ -z "$rc_force" ]; then 2004 return 1 2005 fi 2006 fi 2007 done 2008 ;; 2009 esac 2010 2011 return 0 2012} 2013 2014# check_jail mib 2015# Return true if security.jail.$mib exists and set to 1. 2016 2017check_jail() 2018{ 2019 local _mib _v 2020 2021 _mib=$1 2022 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 2023 case $_v in 2024 1) return 0;; 2025 esac 2026 fi 2027 return 1 2028} 2029 2030# check_kern_features mib 2031# Return existence of kern.features.* sysctl MIB as true or 2032# false. The result will be cached in $_rc_cache_kern_features_ 2033# namespace. "0" means the kern.features.X exists. 2034 2035check_kern_features() 2036{ 2037 local _v 2038 2039 [ -n "$1" ] || return 1; 2040 eval _v=\$_rc_cache_kern_features_$1 2041 [ -n "$_v" ] && return "$_v"; 2042 2043 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 2044 eval _rc_cache_kern_features_$1=0 2045 return 0 2046 else 2047 eval _rc_cache_kern_features_$1=1 2048 return 1 2049 fi 2050} 2051 2052# check_namevarlist var 2053# Return "0" if ${name}_var is reserved in rc.subr. 2054 2055_rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend" 2056check_namevarlist() 2057{ 2058 local _v 2059 2060 for _v in $_rc_namevarlist; do 2061 case $1 in 2062 $_v) return 0 ;; 2063 esac 2064 done 2065 2066 return 1 2067} 2068 2069# _echoonce var msg mode 2070# mode=0: Echo $msg if ${$var} is empty. 2071# After doing echo, a string is set to ${$var}. 2072# 2073# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2074# 2075_echoonce() 2076{ 2077 local _var _msg _mode 2078 eval _var=\$$1 2079 _msg=$2 2080 _mode=$3 2081 2082 case $_mode in 2083 1) [ -n "$_var" ] && echo "$_msg" ;; 2084 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2085 esac 2086} 2087 2088fi # [ -z "${_rc_subr_loaded}" ] 2089 2090_rc_subr_loaded=: 2091