1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 2# 3# Copyright (c) 1997-2004 The NetBSD Foundation, Inc. 4# All rights reserved. 5# 6# This code is derived from software contributed to The NetBSD Foundation 7# by Luke Mewburn. 8# 9# Redistribution and use in source and binary forms, with or without 10# modification, are permitted provided that the following conditions 11# are met: 12# 1. Redistributions of source code must retain the above copyright 13# notice, this list of conditions and the following disclaimer. 14# 2. Redistributions in binary form must reproduce the above copyright 15# notice, this list of conditions and the following disclaimer in the 16# documentation and/or other materials provided with the distribution. 17# 18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28# POSSIBILITY OF SUCH DAMAGE. 29# 30# rc.subr 31# functions used by various rc scripts 32# 33 34: ${RC_PID:=$$}; export RC_PID 35 36# 37# Operating System dependent/independent variables 38# 39 40if [ -n "${_rc_subr_loaded}" ]; then 41 return 42fi 43 44_rc_subr_loaded="YES" 45 46SYSCTL="/sbin/sysctl" 47SYSCTL_N="${SYSCTL} -n" 48SYSCTL_W="${SYSCTL}" 49PROTECT="/usr/bin/protect" 50ID="/usr/bin/id" 51IDCMD="if [ -x $ID ]; then $ID -un; fi" 52PS="/bin/ps -ww" 53JID=0 54CPUSET="/bin/cpuset" 55 56# rc_service provides the path to the service script that we are executing. 57# This is not being set here in an execution context, necessarily, so it's 58# really just a reasonable guess, and it will get overwritten later if 59# we are executing from some other means than direct execution by service(8) 60# or manual invocation of the service script. The prime example of this is 61# during system startup, all rc scripts will be invoked via /etc/rc, so 62# run_rc_script will overwrite rc_service with the file being sourced. 63rc_service="$0" 64 65# 66# functions 67# --------- 68 69# is_verified file 70# if VERIEXEC is active check that $file is verified 71# 72VERIEXEC="/sbin/veriexec" 73if test -x $VERIEXEC && $VERIEXEC -i active > /dev/null 2>&1; then 74 is_verified() { $VERIEXEC -x $1; } 75else 76 is_verified() { return 0; } 77fi 78 79# indicate that we have vdot 80_VDOT_SH=: 81 82# current state of O_VERIFY 83o_verify() 84{ 85 case $(echo $(set -o)) in 86 *verify" "off*) echo off;; 87 *verify" "on*) echo on;; 88 esac 89} 90 91## 92# o_verify_set want [save] 93# 94# record current state of verify in $save 95# and set it to $want if different 96# 97o_verify_set() { 98 local x=$(o_verify) 99 100 [ -z "$x" ] && return 0 101 [ -z "$2" ] || eval $2=$x 102 [ "$x" = "$1" ] && return 0 103 case "$1" in 104 on) 105 set -o verify 106 ;; 107 off) 108 set +o verify 109 ;; 110 esac 111} 112 113# for unverified files 114dotted= 115dot() 116{ 117 local f verify 118 119 o_verify_set off verify 120 for f in "$@"; do 121 if [ -f $f -a -s $f ]; then 122 dotted="$dotted $f" 123 . $f 124 fi 125 done 126 o_verify_set $verify 127} 128 129# try for verified, fallback to safe 130sdot() 131{ 132 local f 133 134 for f in "$@"; do 135 [ -f $f -a -s $f ] || continue 136 vdot $f || safe_dot $f 137 done 138} 139 140# convenience function - skip if not verified 141vdot() 142{ 143 local f rc=0 verify 144 145 o_verify_set on verify 146 for f in "$@"; do 147 [ -f $f -a -s $f ] || continue 148 if is_verified $f 2> /dev/null; then 149 dotted="$dotted $f" 150 . $f 151 else 152 rc=80 # EAUTH 153 fi 154 done 155 o_verify_set $verify 156 return $rc 157} 158 159# do we have $1 (could be a function) 160have() 161{ 162 type "$1" > /dev/null 2>&1 163} 164 165# provide consistent means of logging progress 166rc_log() 167{ 168 date "+@ %s [%Y-%m-%d %H:%M:%S %Z] $*" 169} 170 171# only rc_log if tracing enabled 172# and $level >= $RC_LEVEL 173rc_trace() 174{ 175 local level=$1; shift 176 local cf=/etc/rc.conf.d/rc_trace 177 178 if [ -z "$RC_LEVEL" ]; then 179 [ -f $cf ] || return 180 if [ -s $cf ]; then 181 # don't try to set RC_LEVEL without sed 182 if [ -x /usr/bin/sed ]; then 183 RC_LEVEL=$(sed -n '/^RC_LEVEL=/ { s/.*=//p;q; }' $cf) 184 RC_LEVEL=${RC_LEVEL:-0} 185 fi 186 else 187 RC_LEVEL=0 188 fi 189 fi 190 [ ${RC_LEVEL:-0} -ge ${level:-0} ] || return 191 rc_log "$@" 192} 193 194# list_vars pattern 195# List variables matching glob pattern. 196# 197list_vars() 198{ 199 # Localize 'set' option below. 200 local - 201 local IFS=$'\n' line varname 202 203 # Disable path expansion in unquoted 'for' parameters below. 204 set -o noglob 205 206 for line in $(set); do 207 varname="${line%%=*}" 208 209 case "$varname" in 210 "$line"|*[!a-zA-Z0-9_]*) 211 continue 212 ;; 213 $1) 214 echo $varname 215 ;; 216 esac 217 done 218} 219 220# set_rcvar [var] [defval] [desc] 221# 222# Echo or define a rc.conf(5) variable name. Global variable 223# $rcvars is used. 224# 225# If no argument is specified, echo "${name}_enable". 226# 227# If only a var is specified, echo "${var}_enable". 228# 229# If var and defval are specified, the ${var} is defined as 230# rc.conf(5) variable and the default value is ${defvar}. An 231# optional argument $desc can also be specified to add a 232# description for that. 233# 234set_rcvar() 235{ 236 local _var 237 238 case $# in 239 0) echo ${name}_enable ;; 240 1) echo ${1}_enable ;; 241 *) 242 debug "set_rcvar: \$$1=$2 is added" \ 243 " as a rc.conf(5) variable." 244 _var=$1 245 rcvars="${rcvars# } $_var" 246 eval ${_var}_defval=\"$2\" 247 shift 2 248 eval ${_var}_desc=\"$*\" 249 ;; 250 esac 251} 252 253# set_rcvar_obsolete oldvar [newvar] [msg] 254# Define obsolete variable. 255# Global variable $rcvars_obsolete is used. 256# 257set_rcvar_obsolete() 258{ 259 local _var 260 _var=$1 261 debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 262 263 rcvars_obsolete="${rcvars_obsolete# } $1" 264 eval ${1}_newvar=\"$2\" 265 shift 2 266 eval ${_var}_obsolete_msg=\"$*\" 267} 268 269# 270# force_depend script [rcvar] 271# Force a service to start. Intended for use by services 272# to resolve dependency issues. 273# $1 - filename of script, in /etc/rc.d, to run 274# $2 - name of the script's rcvar (minus the _enable) 275# 276force_depend() 277{ 278 local _depend _dep_rcvar 279 280 _depend="$1" 281 _dep_rcvar="${2:-$1}_enable" 282 283 [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 284 checkyesno $_dep_rcvar && return 0 285 286 /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 287 288 info "${name} depends on ${_depend}, which will be forced to start." 289 if ! /etc/rc.d/${_depend} forcestart; then 290 warn "Unable to force ${_depend}. It may already be running." 291 return 1 292 fi 293} 294 295# 296# checkyesno var 297# Test $1 variable, and warn if not set to YES or NO. 298# Return 0 if it's "yes" (et al), nonzero otherwise. 299# 300checkyesno() 301{ 302 eval _value=\$${1} 303 debug "checkyesno: $1 is set to $_value." 304 case $_value in 305 306 # "yes", "true", "on", or "1" 307 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 308 return 0 309 ;; 310 311 # "no", "false", "off", or "0" 312 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 313 return 1 314 ;; 315 *) 316 warn "\$${1} is not set properly - see rc.conf(5)." 317 return 1 318 ;; 319 esac 320} 321 322# 323# reverse_list list 324# print the list in reverse order 325# 326reverse_list() 327{ 328 _revlist= 329 for _revfile; do 330 _revlist="$_revfile $_revlist" 331 done 332 echo $_revlist 333} 334 335# stop_boot always 336# If booting directly to multiuser or $always is enabled, 337# send SIGTERM to the parent (/etc/rc) to abort the boot. 338# Otherwise just exit. 339# 340stop_boot() 341{ 342 local always 343 344 case $1 in 345 # "yes", "true", "on", or "1" 346 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 347 always=true 348 ;; 349 *) 350 always=false 351 ;; 352 esac 353 if [ "$autoboot" = yes -o "$always" = true ]; then 354 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 355 kill -TERM ${RC_PID} 356 fi 357 exit 1 358} 359 360# 361# mount_critical_filesystems type 362# Go through the list of critical filesystems as provided in 363# the rc.conf(5) variable $critical_filesystems_${type}, checking 364# each one to see if it is mounted, and if it is not, mounting it. 365# 366mount_critical_filesystems() 367{ 368 eval _fslist=\$critical_filesystems_${1} 369 for _fs in $_fslist; do 370 mount | ( 371 _ismounted=false 372 while read what _on on _type type; do 373 if [ $on = $_fs ]; then 374 _ismounted=true 375 fi 376 done 377 if $_ismounted; then 378 : 379 else 380 mount $_fs >/dev/null 2>&1 381 fi 382 ) 383 done 384} 385 386# 387# check_pidfile pidfile procname [interpreter] 388# Parses the first line of pidfile for a PID, and ensures 389# that the process is running and matches procname. 390# Prints the matching PID upon success, nothing otherwise. 391# interpreter is optional; see _find_processes() for details. 392# 393check_pidfile() 394{ 395 _pidfile=$1 396 _procname=$2 397 _interpreter=$3 398 if [ -z "$_pidfile" -o -z "$_procname" ]; then 399 err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 400 fi 401 if [ ! -f $_pidfile ]; then 402 debug "pid file ($_pidfile): not readable." 403 return 404 fi 405 read _pid _junk < $_pidfile 406 if [ -z "$_pid" ]; then 407 debug "pid file ($_pidfile): no pid in file." 408 return 409 fi 410 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 411} 412 413# 414# check_process procname [interpreter] 415# Ensures that a process (or processes) named procname is running. 416# Prints a list of matching PIDs. 417# interpreter is optional; see _find_processes() for details. 418# 419check_process() 420{ 421 _procname=$1 422 _interpreter=$2 423 if [ -z "$_procname" ]; then 424 err 3 'USAGE: check_process procname [interpreter]' 425 fi 426 _find_processes $_procname ${_interpreter:-.} '-ax' 427} 428 429# 430# _find_processes procname interpreter psargs 431# Search for procname in the output of ps generated by psargs. 432# Prints the PIDs of any matching processes, space separated. 433# 434# If interpreter == ".", check the following variations of procname 435# against the first word of each command: 436# procname 437# `basename procname` 438# `basename procname` + ":" 439# "(" + `basename procname` + ")" 440# "[" + `basename procname` + "]" 441# 442# If interpreter != ".", read the first line of procname, remove the 443# leading #!, normalise whitespace, append procname, and attempt to 444# match that against each command, either as is, or with extra words 445# at the end. As an alternative, to deal with interpreted daemons 446# using perl, the basename of the interpreter plus a colon is also 447# tried as the prefix to procname. 448# 449_find_processes() 450{ 451 if [ $# -ne 3 ]; then 452 err 3 'USAGE: _find_processes procname interpreter psargs' 453 fi 454 _procname=$1 455 _interpreter=$2 456 _psargs=$3 457 458 _pref= 459 if [ $_interpreter != "." ]; then # an interpreted script 460 _script="${_chroot}${_chroot:+/}$_procname" 461 if [ -r "$_script" ]; then 462 read _interp < $_script # read interpreter name 463 case "$_interp" in 464 \#!*) 465 _interp=${_interp#\#!} # strip #! 466 set -- $_interp 467 case $1 in 468 */bin/env) 469 shift # drop env to get real name 470 ;; 471 esac 472 if [ $_interpreter != $1 ]; then 473 warn "\$command_interpreter $_interpreter != $1" 474 fi 475 ;; 476 *) 477 warn "no shebang line in $_script" 478 set -- $_interpreter 479 ;; 480 esac 481 else 482 warn "cannot read shebang line from $_script" 483 set -- $_interpreter 484 fi 485 _interp="$* $_procname" # cleanup spaces, add _procname 486 _interpbn=${1##*/} 487 _fp_args='_argv' 488 _fp_match='case "$_argv" in 489 ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' 490 else # a normal daemon 491 _procnamebn=${_procname##*/} 492 _fp_args='_arg0 _argv' 493 _fp_match='case "$_arg0" in 494 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 495 fi 496 497 _proccheck="\ 498 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 499 while read _npid _jid '"$_fp_args"'; do 500 '"$_fp_match"' 501 if [ "$JID" -eq "$_jid" ]; 502 then echo -n "$_pref$_npid"; 503 _pref=" "; 504 fi 505 ;; 506 esac 507 done' 508 509# debug "in _find_processes: proccheck is ($_proccheck)." 510 eval $_proccheck 511} 512 513# sort_lite [-b] [-n] [-k POS] [-t SEP] 514# A lite version of sort(1) (supporting a few options) that can be used 515# before the real sort(1) is available (e.g., in scripts that run prior 516# to mountcritremote). Requires only shell built-in functionality. 517# 518sort_lite() 519{ 520 local funcname=sort_lite 521 local sort_sep="$IFS" sort_ignore_leading_space= 522 local sort_field=0 sort_strict_fields= sort_numeric= 523 local nitems=0 skip_leading=0 trim= 524 525 local OPTIND flag 526 while getopts bnk:t: flag; do 527 case "$flag" in 528 b) sort_ignore_leading_space=1 ;; 529 n) sort_numeric=1 sort_ignore_leading_space=1 ;; 530 k) sort_field="${OPTARG%%,*}" ;; # only up to first comma 531 # NB: Unlike sort(1) only one POS allowed 532 t) sort_sep="$OPTARG" 533 if [ ${#sort_sep} -gt 1 ]; then 534 echo "$funcname: multi-character tab \`$sort_sep'" >&2 535 return 1 536 fi 537 sort_strict_fields=1 538 ;; 539 \?) return 1 ;; 540 esac 541 done 542 shift $(( $OPTIND - 1 )) 543 544 # Create transformation pattern to trim leading text if desired 545 case "$sort_field" in 546 ""|[!0-9]*|*[!0-9.]*) 547 echo "$funcname: invalid sort field \`$sort_field'" >&2 548 return 1 549 ;; 550 *.*) 551 skip_leading=${sort_field#*.} sort_field=${sort_field%%.*} 552 while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do 553 trim="$trim?" skip_leading=$(( $skip_leading - 1 )) 554 done 555 esac 556 557 # Copy input to series of local numbered variables 558 # NB: IFS of NULL preserves leading whitespace 559 local LINE 560 while IFS= read -r LINE || [ "$LINE" ]; do 561 nitems=$(( $nitems + 1 )) 562 local src_$nitems="$LINE" 563 done 564 565 # 566 # Sort numbered locals using insertion sort 567 # 568 local curitem curitem_orig curitem_mod curitem_haskey 569 local dest dest_orig dest_mod dest_haskey 570 local d gt n 571 local i=1 572 while [ $i -le $nitems ]; do 573 curitem_haskey=1 # Assume sort field (-k POS) exists 574 eval curitem=\"\$src_$i\" 575 curitem_mod="$curitem" # for modified comparison 576 curitem_orig="$curitem" # for original comparison 577 578 # Trim leading whitespace if desired 579 if [ "$sort_ignore_leading_space" ]; then 580 while case "$curitem_orig" in 581 [$IFS]*) : ;; *) false; esac 582 do 583 curitem_orig="${curitem_orig#?}" 584 done 585 curitem_mod="$curitem_orig" 586 fi 587 588 # Shift modified comparison value if sort field (-k POS) is > 1 589 n=$sort_field 590 while [ $n -gt 1 ]; do 591 case "$curitem_mod" in 592 *[$sort_sep]*) 593 # Cut text up-to (and incl.) first separator 594 curitem_mod="${curitem_mod#*[$sort_sep]}" 595 596 # Skip NULLs unless strict field splitting 597 [ "$sort_strict_fields" ] || 598 [ "${curitem_mod%%[$sort_sep]*}" ] || 599 [ $n -eq 2 ] || 600 continue 601 ;; 602 *) 603 # Asked for a field that doesn't exist 604 curitem_haskey= break 605 esac 606 n=$(( $n - 1 )) 607 done 608 609 # Trim trailing words if sort field >= 1 610 [ $sort_field -ge 1 -a "$sort_numeric" ] && 611 curitem_mod="${curitem_mod%%[$sort_sep]*}" 612 613 # Apply optional trim (-k POS.TRIM) to cut leading characters 614 curitem_mod="${curitem_mod#$trim}" 615 616 # Determine the type of modified comparison to use initially 617 # NB: Prefer numerical if requested but fallback to standard 618 case "$curitem_mod" in 619 ""|[!0-9]*) # NULL or begins with non-number 620 gt=">" 621 [ "$sort_numeric" ] && curitem_mod=0 622 ;; 623 *) 624 if [ "$sort_numeric" ]; then 625 gt="-gt" 626 curitem_mod="${curitem_mod%%[!0-9]*}" 627 # NB: trailing non-digits removed 628 # otherwise numeric comparison fails 629 else 630 gt=">" 631 fi 632 esac 633 634 # If first time through, short-circuit below position-search 635 if [ $i -le 1 ]; then 636 d=0 637 else 638 d=1 639 fi 640 641 # 642 # Find appropriate element position 643 # 644 while [ $d -gt 0 ] 645 do 646 dest_haskey=$curitem_haskey 647 eval dest=\"\$dest_$d\" 648 dest_mod="$dest" # for modified comparison 649 dest_orig="$dest" # for original comparison 650 651 # Trim leading whitespace if desired 652 if [ "$sort_ignore_leading_space" ]; then 653 while case "$dest_orig" in 654 [$IFS]*) : ;; *) false; esac 655 do 656 dest_orig="${dest_orig#?}" 657 done 658 dest_mod="$dest_orig" 659 fi 660 661 # Shift modified value if sort field (-k POS) is > 1 662 n=$sort_field 663 while [ $n -gt 1 ]; do 664 case "$dest_mod" in 665 *[$sort_sep]*) 666 # Cut text up-to (and incl.) 1st sep 667 dest_mod="${dest_mod#*[$sort_sep]}" 668 669 # Skip NULLs unless strict fields 670 [ "$sort_strict_fields" ] || 671 [ "${dest_mod%%[$sort_sep]*}" ] || 672 [ $n -eq 2 ] || 673 continue 674 ;; 675 *) 676 # Asked for a field that doesn't exist 677 dest_haskey= break 678 esac 679 n=$(( $n - 1 )) 680 done 681 682 # Trim trailing words if sort field >= 1 683 [ $sort_field -ge 1 -a "$sort_numeric" ] && 684 dest_mod="${dest_mod%%[$sort_sep]*}" 685 686 # Apply optional trim (-k POS.TRIM), cut leading chars 687 dest_mod="${dest_mod#$trim}" 688 689 # Determine type of modified comparison to use 690 # NB: Prefer numerical if requested, fallback to std 691 case "$dest_mod" in 692 ""|[!0-9]*) # NULL or begins with non-number 693 gt=">" 694 [ "$sort_numeric" ] && dest_mod=0 695 ;; 696 *) 697 if [ "$sort_numeric" ]; then 698 gt="-gt" 699 dest_mod="${dest_mod%%[!0-9]*}" 700 # NB: kill trailing non-digits 701 # for numeric comparison safety 702 else 703 gt=">" 704 fi 705 esac 706 707 # Break if we've found the proper element position 708 if [ "$curitem_haskey" -a "$dest_haskey" ]; then 709 if [ "$dest_mod" = "$curitem_mod" ]; then 710 [ "$dest_orig" ">" "$curitem_orig" ] && 711 break 712 elif [ "$dest_mod" $gt "$curitem_mod" ] \ 713 2> /dev/null 714 then 715 break 716 fi 717 else 718 [ "$dest_orig" ">" "$curitem_orig" ] && break 719 fi 720 721 # Break if we've hit the end 722 [ $d -ge $i ] && break 723 724 d=$(( $d + 1 )) 725 done 726 727 # Shift remaining positions forward, making room for new item 728 n=$i 729 while [ $n -ge $d ]; do 730 # Shift destination item forward one placement 731 eval dest_$(( $n + 1 ))=\"\$dest_$n\" 732 n=$(( $n - 1 )) 733 done 734 735 # Place the element 736 if [ $i -eq 1 ]; then 737 local dest_1="$curitem" 738 else 739 local dest_$d="$curitem" 740 fi 741 742 i=$(( $i + 1 )) 743 done 744 745 # Print sorted results 746 d=1 747 while [ $d -le $nitems ]; do 748 eval echo \"\$dest_$d\" 749 d=$(( $d + 1 )) 750 done 751} 752 753# 754# wait_for_pids pid [pid ...] 755# spins until none of the pids exist 756# 757wait_for_pids() 758{ 759 local _list _prefix _nlist _j 760 761 _list="$@" 762 if [ -z "$_list" ]; then 763 return 764 fi 765 _prefix= 766 while true; do 767 _nlist=""; 768 for _j in $_list; do 769 if kill -0 $_j 2>/dev/null; then 770 _nlist="${_nlist}${_nlist:+ }$_j" 771 [ -n "$_prefix" ] && sleep 1 772 fi 773 done 774 if [ -z "$_nlist" ]; then 775 break 776 fi 777 _list=$_nlist 778 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 779 _prefix=", " 780 pwait $_list 2>/dev/null 781 done 782 if [ -n "$_prefix" ]; then 783 echo "." 784 fi 785} 786 787# 788# get_pidfile_from_conf string file 789# 790# Takes a string to search for in the specified file. 791# Ignores lines with traditional comment characters. 792# 793# Example: 794# 795# if get_pidfile_from_conf string file; then 796# pidfile="$_pidfile_from_conf" 797# else 798# pidfile='appropriate default' 799# fi 800# 801get_pidfile_from_conf() 802{ 803 if [ -z "$1" -o -z "$2" ]; then 804 err 3 "USAGE: get_pidfile_from_conf string file ($name)" 805 fi 806 807 local string file line 808 809 string="$1" ; file="$2" 810 811 if [ ! -s "$file" ]; then 812 err 3 "get_pidfile_from_conf: $file does not exist ($name)" 813 fi 814 815 while read line; do 816 case "$line" in 817 *[#\;]*${string}*) continue ;; 818 *${string}*) break ;; 819 esac 820 done < $file 821 822 if [ -n "$line" ]; then 823 line=${line#*/} 824 _pidfile_from_conf="/${line%%[\"\;]*}" 825 else 826 return 1 827 fi 828} 829 830# 831# check_startmsgs 832# If rc_quiet is set (usually as a result of using faststart at 833# boot time) check if rc_startmsgs is enabled. 834# 835check_startmsgs() 836{ 837 if [ -n "$rc_quiet" ]; then 838 checkyesno rc_startmsgs 839 else 840 return 0 841 fi 842} 843 844# 845# startmsg 846# Preferred method to use when displaying start messages in lieu of echo. 847# 848startmsg() 849{ 850 check_startmsgs && echo "$@" 851} 852 853# 854# run_rc_command argument 855# Search for argument in the list of supported commands, which is: 856# "start stop restart rcvar status poll ${extra_commands}" 857# If there's a match, run ${argument}_cmd or the default method 858# (see below). 859# 860# If argument has a given prefix, then change the operation as follows: 861# Prefix Operation 862# ------ --------- 863# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 864# force Set ${rcvar} to YES, and set rc_force=yes 865# one Set ${rcvar} to YES 866# quiet Don't output some diagnostics, and set rc_quiet=yes 867# 868# The following globals are used: 869# 870# Name Needed Purpose 871# ---- ------ ------- 872# name y Name of script. 873# 874# command n Full path to command. 875# Not needed if ${rc_arg}_cmd is set for 876# each keyword. 877# 878# command_args n Optional args/shell directives for command. 879# 880# command_interpreter n If not empty, command is interpreted, so 881# call check_{pidfile,process}() appropriately. 882# 883# desc n Description of script. 884# 885# extra_commands n List of extra commands supported. 886# 887# pidfile n If set, use check_pidfile $pidfile $command, 888# otherwise use check_process $command. 889# In either case, only check if $command is set. 890# 891# procname n Process name to check for instead of $command. 892# 893# rcvar n This is checked with checkyesno to determine 894# if the action should be run. 895# 896# ${name}_program n Full path to command. 897# Meant to be used in /etc/rc.conf to override 898# ${command}. 899# 900# ${name}_chroot n Directory to chroot to before running ${command} 901# Requires /usr to be mounted. 902# 903# ${name}_chdir n Directory to cd to before running ${command} 904# (if not using ${name}_chroot). 905# 906# ${name}_cpuset n A list of CPUs to run ${command} on. 907# Requires /usr to be mounted. 908# 909# ${name}_flags n Arguments to call ${command} with. 910# NOTE: $flags from the parent environment 911# can be used to override this. 912# 913# ${name}_env n Environment variables to run ${command} with. 914# 915# ${name}_env_file n File to source variables to run ${command} with. 916# 917# ${name}_fib n Routing table number to run ${command} with. 918# 919# ${name}_nice n Nice level to run ${command} at. 920# 921# ${name}_oomprotect n Don't kill ${command} when swap space is exhausted. 922# 923# ${name}_umask n The file creation mask to run ${command} with. 924# 925# ${name}_user n User to run ${command} as, using su(1) if not 926# using ${name}_chroot. 927# Requires /usr to be mounted. 928# 929# ${name}_group n Group to run chrooted ${command} as. 930# Requires /usr to be mounted. 931# 932# ${name}_groups n Comma separated list of supplementary groups 933# to run the chrooted ${command} with. 934# Requires /usr to be mounted. 935# 936# ${name}_prepend n Command added before ${command}. 937# 938# ${name}_setup n Command executed before ${command}. 939# 940# ${name}_login_class n Login class to use, else "daemon". 941# 942# ${name}_limits n limits(1) to apply to ${command}. 943# 944# ${rc_arg}_cmd n If set, use this as the method when invoked; 945# Otherwise, use default command (see below) 946# 947# ${rc_arg}_precmd n If set, run just before performing the 948# ${rc_arg}_cmd method in the default 949# operation (i.e, after checking for required 950# bits and process (non)existence). 951# If this completes with a non-zero exit code, 952# don't run ${rc_arg}_cmd. 953# 954# ${rc_arg}_postcmd n If set, run just after performing the 955# ${rc_arg}_cmd method, if that method 956# returned a zero exit code. 957# 958# required_dirs n If set, check for the existence of the given 959# directories before running a (re)start command. 960# 961# required_files n If set, check for the readability of the given 962# files before running a (re)start command. 963# 964# required_modules n If set, ensure the given kernel modules are 965# loaded before running a (re)start command. 966# The check and possible loads are actually 967# done after start_precmd so that the modules 968# aren't loaded in vain, should the precmd 969# return a non-zero status to indicate a error. 970# If a word in the list looks like "foo:bar", 971# "foo" is the KLD file name and "bar" is the 972# module name. If a word looks like "foo~bar", 973# "foo" is the KLD file name and "bar" is a 974# egrep(1) pattern matching the module name. 975# Otherwise the module name is assumed to be 976# the same as the KLD file name, which is most 977# common. See load_kld(). 978# 979# required_vars n If set, perform checkyesno on each of the 980# listed variables before running the default 981# (re)start command. 982# 983# Default behaviour for a given argument, if no override method is 984# provided: 985# 986# Argument Default behaviour 987# -------- ----------------- 988# start if !running && checkyesno ${rcvar} 989# ${command} 990# 991# stop if ${pidfile} 992# rc_pid=$(check_pidfile $pidfile $command) 993# else 994# rc_pid=$(check_process $command) 995# kill $sig_stop $rc_pid 996# wait_for_pids $rc_pid 997# ($sig_stop defaults to TERM.) 998# 999# reload Similar to stop, except use $sig_reload instead, 1000# and don't wait_for_pids. 1001# $sig_reload defaults to HUP. 1002# Note that `reload' isn't provided by default, 1003# it should be enabled via $extra_commands. 1004# 1005# restart Run `stop' then `start'. 1006# 1007# status Show if ${command} is running, etc. 1008# 1009# poll Wait for ${command} to exit. 1010# 1011# rcvar Display what rc.conf variable is used (if any). 1012# 1013# enabled Return true if the service is enabled. 1014# 1015# describe Show the service's description 1016# 1017# extracommands Show the service's extra commands 1018# 1019# Variables available to methods, and after run_rc_command() has 1020# completed: 1021# 1022# Variable Purpose 1023# -------- ------- 1024# rc_arg Argument to command, after fast/force/one processing 1025# performed 1026# 1027# rc_flags Flags to start the default command with. 1028# Defaults to ${name}_flags, unless overridden 1029# by $flags from the environment. 1030# This variable may be changed by the precmd method. 1031# 1032# rc_service Path to the service being executed, in case the service 1033# needs to re-invoke itself. 1034# 1035# rc_pid PID of command (if appropriate) 1036# 1037# rc_fast Not empty if "fast" was provided (q.v.) 1038# 1039# rc_force Not empty if "force" was provided (q.v.) 1040# 1041# rc_quiet Not empty if "quiet" was provided 1042# 1043# 1044run_rc_command() 1045{ 1046 _return=0 1047 rc_arg=$1 1048 if [ -z "$name" ]; then 1049 err 3 'run_rc_command: $name is not set.' 1050 fi 1051 1052 DebugOn rc:$name rc:$name:$rc_arg $name:$rc_arg 1053 1054 # Don't repeat the first argument when passing additional command- 1055 # line arguments to the command subroutines. 1056 # 1057 shift 1 1058 rc_extra_args="$*" 1059 1060 _rc_prefix= 1061 case "$rc_arg" in 1062 fast*) # "fast" prefix; don't check pid 1063 rc_arg=${rc_arg#fast} 1064 rc_fast=yes 1065 rc_quiet=yes 1066 ;; 1067 force*) # "force" prefix; always run 1068 rc_force=yes 1069 _rc_prefix=force 1070 rc_arg=${rc_arg#${_rc_prefix}} 1071 if [ -n "${rcvar}" ]; then 1072 eval ${rcvar}=YES 1073 fi 1074 ;; 1075 one*) # "one" prefix; set ${rcvar}=yes 1076 _rc_prefix=one 1077 rc_arg=${rc_arg#${_rc_prefix}} 1078 if [ -n "${rcvar}" ]; then 1079 eval ${rcvar}=YES 1080 fi 1081 ;; 1082 quiet*) # "quiet" prefix; omit some messages 1083 _rc_prefix=quiet 1084 rc_arg=${rc_arg#${_rc_prefix}} 1085 rc_quiet=yes 1086 ;; 1087 esac 1088 1089 eval _override_command=\$${name}_program 1090 command=${_override_command:-$command} 1091 1092 _keywords="start stop restart rcvar enable disable delete enabled describe extracommands $extra_commands" 1093 rc_pid= 1094 _pidcmd= 1095 _procname=${procname:-${command}} 1096 1097 eval _cpuset=\$${name}_cpuset 1098 1099 # Loose validation of the configured cpuset; just make sure it starts 1100 # with a number. There have also been cases in the past where a hyphen 1101 # in a service name has caused eval errors, which trickle down into 1102 # various variables; don't let a situation like that break a bunch of 1103 # services just because of cpuset(1). 1104 case "$_cpuset" in 1105 [0-9]*) ;; 1106 *) _cpuset="" ;; 1107 esac 1108 1109 _cpusetcmd= 1110 if [ -n "$_cpuset" ]; then 1111 _cpusetcmd="$CPUSET -l $_cpuset" 1112 fi 1113 1114 # setup pid check command 1115 if [ -n "$_procname" ]; then 1116 if [ -n "$pidfile" ]; then 1117 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 1118 else 1119 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 1120 fi 1121 _keywords="${_keywords} status poll" 1122 else 1123 if [ ! -z "${status_cmd}" ] 1124 then 1125 _keywords="${_keywords} status" 1126 fi 1127 fi 1128 1129 if [ -z "$rc_arg" ]; then 1130 rc_usage $_keywords 1131 fi 1132 1133 if [ "$rc_arg" = "enabled" ] ; then 1134 checkyesno ${rcvar} 1135 return $? 1136 fi 1137 1138 if [ -n "$flags" ]; then # allow override from environment 1139 rc_flags=$flags 1140 else 1141 eval rc_flags=\$${name}_flags 1142 fi 1143 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 1144 _nice=\$${name}_nice _user=\$${name}_user \ 1145 _group=\$${name}_group _groups=\$${name}_groups \ 1146 _fib=\$${name}_fib _env=\$${name}_env \ 1147 _prepend=\$${name}_prepend _login_class=\${${name}_login_class:-daemon} \ 1148 _limits=\$${name}_limits _oomprotect=\$${name}_oomprotect \ 1149 _setup=\$${name}_setup _env_file=\$${name}_env_file \ 1150 _umask=\$${name}_umask 1151 1152 if [ -n "$_env_file" ] && [ -r "${_env_file}" ]; then # load env from file 1153 set -a 1154 . $_env_file 1155 set +a 1156 fi 1157 1158 if [ -n "$_user" ]; then # unset $_user if running as that user 1159 if [ "$_user" = "$(eval $IDCMD)" ]; then 1160 unset _user 1161 fi 1162 fi 1163 1164 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 1165 1166 for _elem in $_keywords; do 1167 if [ "$_elem" != "$rc_arg" ]; then 1168 continue 1169 fi 1170 # if ${rcvar} is set, $1 is not "rcvar", "describe", 1171 # "enable", "delete" or "status", and ${rc_pid} is 1172 # not set, run: 1173 # checkyesno ${rcvar} 1174 # and return if that failed 1175 # 1176 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \ 1177 -a "$rc_arg" != "delete" -a "$rc_arg" != "enable" \ 1178 -a "$rc_arg" != "describe" -a "$rc_arg" != "status" ] || 1179 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 1180 if ! checkyesno ${rcvar}; then 1181 if [ -n "${rc_quiet}" ]; then 1182 return 0 1183 fi 1184 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 1185 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 1186 echo "instead of '${rc_arg}'." 1187 return 0 1188 fi 1189 fi 1190 1191 if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then 1192 if [ -z "$rc_quiet" ]; then 1193 echo 1>&2 "${name} already running? " \ 1194 "(pid=$rc_pid)." 1195 fi 1196 return 1 1197 fi 1198 1199 # if there's a custom ${XXX_cmd}, 1200 # run that instead of the default 1201 # 1202 eval _cmd=\$${rc_arg}_cmd \ 1203 _precmd=\$${rc_arg}_precmd \ 1204 _postcmd=\$${rc_arg}_postcmd 1205 1206 if [ -n "$_cmd" ]; then 1207 rc_trace 1 "$_cmd" 1208 if [ -n "$_env" ]; then 1209 eval "export -- $_env" 1210 fi 1211 _run_rc_precmd || return 1 1212 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1 1213 _run_rc_postcmd 1214 return $_return 1215 fi 1216 1217 case "$rc_arg" in # default operations... 1218 1219 describe) 1220 if [ -n "$desc" ]; then 1221 echo "$desc" 1222 fi 1223 ;; 1224 1225 extracommands) 1226 echo "$extra_commands" 1227 ;; 1228 1229 enable) 1230 _out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=YES") && 1231 echo "$name enabled in ${_out%%:*}" 1232 ;; 1233 1234 disable) 1235 _out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=NO") && 1236 echo "$name disabled in ${_out%%:*}" 1237 ;; 1238 1239 delete) 1240 _files= 1241 for _file in $(sysrc -lEs "$name"); do 1242 _out=$(sysrc -Fif $_file "$rcvar") && _files="$_files $_file" 1243 done 1244 /usr/sbin/sysrc -x "$rcvar" && echo "$rcvar deleted in ${_files# }" 1245 # delete file in rc.conf.d if desired and empty. 1246 checkyesno service_delete_empty || _files= 1247 for _file in $_files; do 1248 [ "$_file" = "${_file#*/rc.conf.d/}" ] && continue 1249 [ $(/usr/bin/stat -f%z $_file) -gt 0 ] && continue 1250 /bin/rm "$_file" && echo "Empty file $_file removed" 1251 done 1252 ;; 1253 1254 status) 1255 _run_rc_precmd || return 1 1256 if [ -n "$rc_pid" ]; then 1257 echo "${name} is running as pid $rc_pid." 1258 else 1259 echo "${name} is not running." 1260 return 1 1261 fi 1262 _run_rc_postcmd 1263 ;; 1264 1265 start) 1266 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 1267 warn "run_rc_command: cannot run $command" 1268 return 1 1269 fi 1270 1271 if ! _run_rc_precmd; then 1272 warn "failed precmd routine for ${name}" 1273 return 1 1274 fi 1275 1276 # setup the full command to run 1277 # 1278 startmsg "Starting ${name}." 1279 if [ -n "$_chroot" ]; then 1280 _cd= 1281 _doit="\ 1282${_nice:+nice -n $_nice }\ 1283$_cpusetcmd \ 1284${_fib:+setfib -F $_fib }\ 1285${_env:+env $_env }\ 1286chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 1287$_chroot $command $rc_flags $command_args" 1288 else 1289 _cd="${_chdir:+cd $_chdir && }" 1290 _doit="\ 1291${_fib:+setfib -F $_fib }\ 1292${_env:+env $_env }\ 1293$_cpusetcmd $command $rc_flags $command_args" 1294 if [ -n "$_user" ]; then 1295 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 1296 fi 1297 if [ -n "$_nice" ]; then 1298 if [ -z "$_user" ]; then 1299 _doit="sh -c \"$_doit\"" 1300 fi 1301 _doit="nice -n $_nice $_doit" 1302 fi 1303 if [ -n "$_prepend" ]; then 1304 _doit="$_prepend $_doit" 1305 fi 1306 fi 1307 1308 if [ -n "$_setup" ]; then 1309 if ! _run_rc_doit "$_setup"; then 1310 warn "failed to setup ${name}" 1311 fi 1312 fi 1313 1314 # Prepend default limits 1315 _doit="$_cd limits -C $_login_class $_limits $_doit" 1316 1317 # run the full command 1318 # 1319 if ! _run_rc_doit "$_doit"; then 1320 warn "failed to start ${name}" 1321 return 1 1322 fi 1323 1324 # finally, run postcmd 1325 # 1326 _run_rc_postcmd 1327 ;; 1328 1329 stop) 1330 if [ -z "$rc_pid" ]; then 1331 [ -n "$rc_fast" ] && return 0 1332 _run_rc_notrunning 1333 return 1 1334 fi 1335 1336 _run_rc_precmd || return 1 1337 1338 # send the signal to stop 1339 # 1340 echo "Stopping ${name}." 1341 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1342 _run_rc_doit "$_doit" || return 1 1343 1344 # wait for the command to exit, 1345 # and run postcmd. 1346 wait_for_pids $rc_pid 1347 1348 _run_rc_postcmd 1349 ;; 1350 1351 reload) 1352 if [ -z "$rc_pid" ]; then 1353 _run_rc_notrunning 1354 return 1 1355 fi 1356 1357 _run_rc_precmd || return 1 1358 1359 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1360 _run_rc_doit "$_doit" || return 1 1361 1362 _run_rc_postcmd 1363 ;; 1364 1365 restart) 1366 # prevent restart being called more 1367 # than once by any given script 1368 # 1369 if ${_rc_restart_done:-false}; then 1370 return 0 1371 fi 1372 _rc_restart_done=true 1373 1374 _run_rc_precmd || return 1 1375 1376 # run those in a subshell to keep global variables 1377 ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1378 ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1379 _return=$? 1380 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 1381 1382 _run_rc_postcmd 1383 ;; 1384 1385 poll) 1386 _run_rc_precmd || return 1 1387 if [ -n "$rc_pid" ]; then 1388 wait_for_pids $rc_pid 1389 fi 1390 _run_rc_postcmd 1391 ;; 1392 1393 rcvar) 1394 echo -n "# $name" 1395 if [ -n "$desc" ]; then 1396 echo " : $desc" 1397 else 1398 echo "" 1399 fi 1400 echo "#" 1401 # Get unique vars in $rcvar $rcvars 1402 for _v in $rcvar $rcvars; do 1403 case $v in 1404 $_v\ *|\ *$_v|*\ $_v\ *) ;; 1405 *) v="${v# } $_v" ;; 1406 esac 1407 done 1408 1409 # Display variables. 1410 for _v in $v; do 1411 if [ -z "$_v" ]; then 1412 continue 1413 fi 1414 1415 eval _desc=\$${_v}_desc 1416 eval _defval=\$${_v}_defval 1417 _h="-" 1418 1419 eval echo \"$_v=\\\"\$$_v\\\"\" 1420 # decode multiple lines of _desc 1421 while [ -n "$_desc" ]; do 1422 case $_desc in 1423 *^^*) 1424 echo "# $_h ${_desc%%^^*}" 1425 _desc=${_desc#*^^} 1426 _h=" " 1427 ;; 1428 *) 1429 echo "# $_h ${_desc}" 1430 break 1431 ;; 1432 esac 1433 done 1434 echo "# (default: \"$_defval\")" 1435 done 1436 echo "" 1437 ;; 1438 1439 *) 1440 rc_usage $_keywords 1441 ;; 1442 1443 esac 1444 1445 # Apply protect(1) to the PID if ${name}_oomprotect is set. 1446 case "$rc_arg" in 1447 start) 1448 # We cannot use protect(1) inside jails. 1449 if [ -n "$_oomprotect" ] && [ -f "${PROTECT}" ] && 1450 [ "$(sysctl -n security.jail.jailed)" -eq 0 ]; then 1451 [ -z "${rc_pid}" ] && eval $_pidcmd 1452 case $_oomprotect in 1453 [Aa][Ll][Ll]) 1454 ${PROTECT} -d -i -p ${rc_pid} 1455 ;; 1456 [Yy][Ee][Ss]) 1457 ${PROTECT} -p ${rc_pid} 1458 ;; 1459 esac 1460 fi 1461 ;; 1462 esac 1463 1464 return $_return 1465 done 1466 1467 echo 1>&2 "$0: unknown directive '$rc_arg'." 1468 rc_usage $_keywords 1469 # not reached 1470} 1471 1472# 1473# Helper functions for run_rc_command: common code. 1474# They use such global variables besides the exported rc_* ones: 1475# 1476# name R/W 1477# ------------------ 1478# _precmd R 1479# _postcmd R 1480# _return W 1481# 1482_run_rc_precmd() 1483{ 1484 check_required_before "$rc_arg" || return 1 1485 1486 if [ -n "$_precmd" ]; then 1487 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1488 eval "$_precmd $rc_extra_args" 1489 _return=$? 1490 1491 # If precmd failed and force isn't set, request exit. 1492 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1493 return 1 1494 fi 1495 fi 1496 1497 check_required_after "$rc_arg" || return 1 1498 1499 return 0 1500} 1501 1502_run_rc_postcmd() 1503{ 1504 if [ -n "$_postcmd" ]; then 1505 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1506 eval "$_postcmd $rc_extra_args" 1507 _return=$? 1508 fi 1509 return 0 1510} 1511 1512_run_rc_doit() 1513{ 1514 local _m 1515 1516 debug "run_rc_command: doit: $*" 1517 _m=$(umask) 1518 ${_umask:+umask ${_umask}} 1519 eval "$@" 1520 _return=$? 1521 umask ${_m} 1522 1523 # If command failed and force isn't set, request exit. 1524 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1525 return 1 1526 fi 1527 1528 return 0 1529} 1530 1531_run_rc_notrunning() 1532{ 1533 local _pidmsg 1534 1535 if [ -n "$pidfile" ]; then 1536 _pidmsg=" (check $pidfile)." 1537 else 1538 _pidmsg= 1539 fi 1540 echo 1>&2 "${name} not running?${_pidmsg}" 1541} 1542 1543_run_rc_killcmd() 1544{ 1545 local _cmd 1546 1547 _cmd="kill -$1 $rc_pid" 1548 if [ -n "$_user" ]; then 1549 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1550 fi 1551 echo "$_cmd" 1552} 1553 1554# 1555# run_rc_script file arg 1556# Start the script `file' with `arg', and correctly handle the 1557# return value from the script. 1558# If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's 1559# an old-style startup file. 1560# If `file' ends with `.sh' and does not live in /etc/rc.d, it's sourced 1561# into the current environment if $rc_fast_and_loose is set; otherwise 1562# it is run as a child process. 1563# If `file' appears to be a backup or scratch file, ignore it. 1564# Otherwise if it is executable run as a child process. 1565# 1566run_rc_script() 1567{ 1568 _file=$1 1569 _arg=$2 1570 if [ -z "$_file" -o -z "$_arg" ]; then 1571 err 3 'USAGE: run_rc_script file arg' 1572 fi 1573 1574 unset name command command_args command_interpreter \ 1575 extra_commands pidfile procname \ 1576 rcvar rcvars rcvars_obsolete required_dirs required_files \ 1577 required_vars 1578 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 1579 1580 rc_trace 0 "$_file $_arg" 1581 # don't use it if we don't trust it 1582 is_verified $_file || return 1583 1584 rc_service="$_file" 1585 case "$_file" in 1586 /etc/rc.d/*.sh) # no longer allowed in the base 1587 warn "Ignoring old-style startup script $_file" 1588 ;; 1589 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 1590 warn "Ignoring scratch file $_file" 1591 ;; 1592 *) # run in subshell 1593 if [ -x $_file ]; then 1594 DebugOn $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg 1595 1596 if [ -n "$rc_boottrace" ]; then 1597 boottrace_fn "$_file" "$_arg" 1598 elif [ -n "$rc_fast_and_loose" ]; then 1599 set $_arg; . $_file 1600 else 1601 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1602 trap "echo Script $_file interrupted >&2 ; exit 1" 2 1603 trap "echo Script $_file running >&2" 29 1604 set $_arg; . $_file ) 1605 fi 1606 DebugOff $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg 1607 fi 1608 ;; 1609 esac 1610} 1611 1612# 1613# run_rc_scripts [options] file [...] 1614# 1615# Call `run_rc_script' for each "file" unless already listed in 1616# $_rc_elem_done. 1617# 1618# Options: 1619# 1620# --arg "arg" 1621# Pass "arg" to `run_rc_script' default is $_boot. 1622# 1623# --break "marker" 1624# If any "file" matches "marker" stop processing. 1625# 1626_rc_elem_done= 1627run_rc_scripts() 1628{ 1629 local _arg=${_boot} 1630 local _rc_elem 1631 local _rc_breaks= 1632 1633 while :; do 1634 case "$1" in 1635 --arg) 1636 _arg="$2" 1637 shift 2 1638 ;; 1639 --break) 1640 _rc_breaks="$_rc_breaks $2" 1641 shift 2 1642 ;; 1643 *) 1644 break 1645 ;; 1646 esac 1647 done 1648 for _rc_elem in "$@"; do 1649 : _rc_elem=$_rc_elem 1650 case " $_rc_elem_done " in 1651 *" $_rc_elem "*) 1652 continue 1653 ;; 1654 esac 1655 run_rc_script ${_rc_elem} ${_arg} 1656 _rc_elem_done="$_rc_elem_done $_rc_elem" 1657 case " $_rc_breaks " in 1658 *" ${_rc_elem##*/} "*) 1659 break 1660 ;; 1661 esac 1662 done 1663} 1664 1665boottrace_fn() 1666{ 1667 local _file _arg 1668 _file=$1 1669 _arg=$2 1670 1671 if [ -n "$rc_fast_and_loose" ]; then 1672 boottrace_sysctl "$_file start" 1673 set $_arg; . $_file 1674 boottrace_sysctl "$_file done" 1675 else 1676 $boottrace_cmd "$_file" "$_arg" 1677 fi 1678} 1679 1680boottrace_sysctl() 1681{ 1682 ${SYSCTL} kern.boottrace.boottrace="$1" 1683} 1684 1685# 1686# load_rc_config [service] 1687# Source in the configuration file(s) for a given service. 1688# If no service is specified, only the global configuration 1689# file(s) will be loaded. 1690# 1691load_rc_config() 1692{ 1693 local _name _rcvar_val _var _defval _v _msg _new _d _dot 1694 _name=$1 1695 _dot=${load_rc_config_reader:-dot} 1696 1697 case "$_dot" in 1698 dot|[sv]dot) 1699 ;; 1700 *) warn "Ignoring invalid load_rc_config_reader" 1701 _dot=dot 1702 ;; 1703 esac 1704 case "$1" in 1705 -s|--safe) 1706 _dot=sdot 1707 _name=$2 1708 shift 1709 ;; 1710 -v|--verify) 1711 _dot=vdot 1712 _name=$2 1713 shift 1714 ;; 1715 esac 1716 1717 DebugOn rc:$_name $_name 1718 1719 if ${_rc_conf_loaded:-false}; then 1720 : 1721 else 1722 if [ -r /etc/defaults/rc.conf ]; then 1723 debug "Sourcing /etc/defaults/rc.conf" 1724 $_dot /etc/defaults/rc.conf 1725 source_rc_confs 1726 elif [ -r /etc/rc.conf ]; then 1727 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 1728 $_dot /etc/rc.conf 1729 fi 1730 _rc_conf_loaded=true 1731 fi 1732 1733 # If a service name was specified, attempt to load 1734 # service-specific configuration 1735 if [ -n "$_name" ] ; then 1736 for _d in /etc ${local_startup}; do 1737 _d=${_d%/rc.d} 1738 if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1739 debug "Sourcing ${_d}/rc.conf.d/$_name" 1740 $_dot ${_d}/rc.conf.d/"$_name" 1741 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1742 local _rc 1743 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1744 if [ -f "$_rc" ] ; then 1745 debug "Sourcing $_rc" 1746 $_dot "$_rc" 1747 fi 1748 done 1749 fi 1750 done 1751 fi 1752 1753 # Set defaults if defined. 1754 for _var in $rcvar $rcvars; do 1755 eval _defval=\$${_var}_defval 1756 if [ -n "$_defval" ]; then 1757 eval : \${$_var:=\$${_var}_defval} 1758 fi 1759 done 1760 1761 # check obsolete rc.conf variables 1762 for _var in $rcvars_obsolete; do 1763 eval _v=\$$_var 1764 eval _msg=\$${_var}_obsolete_msg 1765 eval _new=\$${_var}_newvar 1766 case $_v in 1767 "") 1768 ;; 1769 *) 1770 if [ -z "$_new" ]; then 1771 _msg="Ignored." 1772 else 1773 eval $_new=\"\$$_var\" 1774 if [ -z "$_msg" ]; then 1775 _msg="Use \$$_new instead." 1776 fi 1777 fi 1778 warn "\$$_var is obsolete. $_msg" 1779 ;; 1780 esac 1781 done 1782} 1783 1784# 1785# load_rc_config_var name var 1786# Read the rc.conf(5) var for name and set in the 1787# current shell, using load_rc_config in a subshell to prevent 1788# unwanted side effects from other variable assignments. 1789# 1790load_rc_config_var() 1791{ 1792 if [ $# -ne 2 ]; then 1793 err 3 'USAGE: load_rc_config_var name var' 1794 fi 1795 eval $(eval '( 1796 load_rc_config '$1' >/dev/null; 1797 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1798 echo '$2'=\'\''${'$2'}\'\''; 1799 fi 1800 )' ) 1801} 1802 1803# 1804# rc_usage commands 1805# Print a usage string for $0, with `commands' being a list of 1806# valid commands. 1807# 1808rc_usage() 1809{ 1810 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 1811 1812 _sep= 1813 for _elem; do 1814 echo -n 1>&2 "$_sep$_elem" 1815 _sep="|" 1816 done 1817 echo 1>&2 ")" 1818 exit 1 1819} 1820 1821# 1822# err exitval message 1823# Display message to stderr and log to the syslog, and exit with exitval. 1824# 1825err() 1826{ 1827 exitval=$1 1828 shift 1829 1830 if [ -x /usr/bin/logger ]; then 1831 logger "$0: ERROR: $*" 1832 fi 1833 echo 1>&2 "$0: ERROR: $*" 1834 exit $exitval 1835} 1836 1837# 1838# warn message 1839# Display message to stderr and log to the syslog. 1840# 1841warn() 1842{ 1843 if [ -x /usr/bin/logger ]; then 1844 logger "$0: WARNING: $*" 1845 fi 1846 echo 1>&2 "$0: WARNING: $*" 1847} 1848 1849# 1850# info message 1851# Display informational message to stdout and log to syslog. 1852# 1853info() 1854{ 1855 case ${rc_info} in 1856 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1857 if [ -x /usr/bin/logger ]; then 1858 logger "$0: INFO: $*" 1859 fi 1860 echo "$0: INFO: $*" 1861 ;; 1862 esac 1863} 1864 1865# 1866# debug message 1867# If debugging is enabled in rc.conf output message to stderr. 1868# BEWARE that you don't call any subroutine that itself calls this 1869# function. 1870# 1871debug() 1872{ 1873 case ${rc_debug} in 1874 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1875 if [ -x /usr/bin/logger ]; then 1876 logger "$0: DEBUG: $*" 1877 fi 1878 echo 1>&2 "$0: DEBUG: $*" 1879 ;; 1880 esac 1881} 1882 1883# 1884# backup_file action file cur backup 1885# Make a backup copy of `file' into `cur', and save the previous 1886# version of `cur' as `backup'. 1887# 1888# The `action' keyword can be one of the following: 1889# 1890# add `file' is now being backed up (and is possibly 1891# being reentered into the backups system). `cur' 1892# is created. 1893# 1894# update `file' has changed and needs to be backed up. 1895# If `cur' exists, it is copied to `back' 1896# and then `file' is copied to `cur'. 1897# 1898# remove `file' is no longer being tracked by the backups 1899# system. `cur' is moved `back'. 1900# 1901# 1902backup_file() 1903{ 1904 _action=$1 1905 _file=$2 1906 _cur=$3 1907 _back=$4 1908 1909 case $_action in 1910 add|update) 1911 if [ -f $_cur ]; then 1912 cp -p $_cur $_back 1913 fi 1914 cp -p $_file $_cur 1915 chown root:wheel $_cur 1916 ;; 1917 remove) 1918 mv -f $_cur $_back 1919 ;; 1920 esac 1921} 1922 1923# make_symlink src link 1924# Make a symbolic link 'link' to src from basedir. If the 1925# directory in which link is to be created does not exist 1926# a warning will be displayed and an error will be returned. 1927# Returns 0 on success, 1 otherwise. 1928# 1929make_symlink() 1930{ 1931 local src link linkdir _me 1932 src="$1" 1933 link="$2" 1934 linkdir="`dirname $link`" 1935 _me="make_symlink()" 1936 1937 if [ -z "$src" -o -z "$link" ]; then 1938 warn "$_me: requires two arguments." 1939 return 1 1940 fi 1941 if [ ! -d "$linkdir" ]; then 1942 warn "$_me: the directory $linkdir does not exist." 1943 return 1 1944 fi 1945 if ! ln -sf $src $link; then 1946 warn "$_me: unable to make a symbolic link from $link to $src" 1947 return 1 1948 fi 1949 return 0 1950} 1951 1952# devfs_rulesets_from_file file 1953# Reads a set of devfs commands from file, and creates 1954# the specified rulesets with their rules. Returns non-zero 1955# if there was an error. 1956# 1957devfs_rulesets_from_file() 1958{ 1959 local file _err _me _opts 1960 file="$1" 1961 _me="devfs_rulesets_from_file" 1962 _err=0 1963 1964 if [ -z "$file" ]; then 1965 warn "$_me: you must specify a file" 1966 return 1 1967 fi 1968 if [ ! -e "$file" ]; then 1969 debug "$_me: no such file ($file)" 1970 return 0 1971 fi 1972 1973 # Disable globbing so that the rule patterns are not expanded 1974 # by accident with matching filesystem entries. 1975 _opts=$-; set -f 1976 1977 debug "reading rulesets from file ($file)" 1978 { while read line 1979 do 1980 case $line in 1981 \#*) 1982 continue 1983 ;; 1984 \[*\]*) 1985 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1986 if [ -z "$rulenum" ]; then 1987 warn "$_me: cannot extract rule number ($line)" 1988 _err=1 1989 break 1990 fi 1991 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1992 if [ -z "$rulename" ]; then 1993 warn "$_me: cannot extract rule name ($line)" 1994 _err=1 1995 break; 1996 fi 1997 eval $rulename=\$rulenum 1998 debug "found ruleset: $rulename=$rulenum" 1999 if ! /sbin/devfs rule -s $rulenum delset; then 2000 _err=1 2001 break 2002 fi 2003 ;; 2004 *) 2005 rulecmd="${line%%"\#*"}" 2006 # evaluate the command incase it includes 2007 # other rules 2008 if [ -n "$rulecmd" ]; then 2009 debug "adding rule ($rulecmd)" 2010 if ! eval /sbin/devfs rule -s $rulenum $rulecmd 2011 then 2012 _err=1 2013 break 2014 fi 2015 fi 2016 ;; 2017 esac 2018 if [ $_err -ne 0 ]; then 2019 debug "error in $_me" 2020 break 2021 fi 2022 done } < $file 2023 case $_opts in *f*) ;; *) set +f ;; esac 2024 return $_err 2025} 2026 2027# devfs_init_rulesets 2028# Initializes rulesets from configuration files. Returns 2029# non-zero if there was an error. 2030# 2031devfs_init_rulesets() 2032{ 2033 local file _me 2034 _me="devfs_init_rulesets" 2035 2036 # Go through this only once 2037 if [ -n "$devfs_rulesets_init" ]; then 2038 debug "$_me: devfs rulesets already initialized" 2039 return 2040 fi 2041 for file in $devfs_rulesets; do 2042 if ! devfs_rulesets_from_file $file; then 2043 warn "$_me: could not read rules from $file" 2044 return 1 2045 fi 2046 done 2047 devfs_rulesets_init=1 2048 debug "$_me: devfs rulesets initialized" 2049 return 0 2050} 2051 2052# devfs_set_ruleset ruleset [dir] 2053# Sets the default ruleset of dir to ruleset. The ruleset argument 2054# must be a ruleset name as specified in devfs.rules(5) file. 2055# Returns non-zero if it could not set it successfully. 2056# 2057devfs_set_ruleset() 2058{ 2059 local devdir rs _me 2060 [ -n "$1" ] && eval rs=\$$1 || rs= 2061 [ -n "$2" ] && devdir="-m "$2"" || devdir= 2062 _me="devfs_set_ruleset" 2063 2064 if [ -z "$rs" ]; then 2065 warn "$_me: you must specify a ruleset number" 2066 return 1 2067 fi 2068 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 2069 if ! /sbin/devfs $devdir ruleset $rs; then 2070 warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 2071 return 1 2072 fi 2073 return 0 2074} 2075 2076# devfs_apply_ruleset ruleset [dir] 2077# Apply ruleset number $ruleset to the devfs mountpoint $dir. 2078# The ruleset argument must be a ruleset name as specified 2079# in a devfs.rules(5) file. Returns 0 on success or non-zero 2080# if it could not apply the ruleset. 2081# 2082devfs_apply_ruleset() 2083{ 2084 local devdir rs _me 2085 [ -n "$1" ] && eval rs=\$$1 || rs= 2086 [ -n "$2" ] && devdir="-m "$2"" || devdir= 2087 _me="devfs_apply_ruleset" 2088 2089 if [ -z "$rs" ]; then 2090 warn "$_me: you must specify a ruleset" 2091 return 1 2092 fi 2093 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 2094 if ! /sbin/devfs $devdir rule -s $rs applyset; then 2095 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 2096 return 1 2097 fi 2098 return 0 2099} 2100 2101# devfs_domount dir [ruleset] 2102# Mount devfs on dir. If ruleset is specified it is set 2103# on the mount-point. It must also be a ruleset name as specified 2104# in a devfs.rules(5) file. Returns 0 on success. 2105# 2106devfs_domount() 2107{ 2108 local devdir rs _me 2109 devdir="$1" 2110 [ -n "$2" ] && rs=$2 || rs= 2111 _me="devfs_domount()" 2112 2113 if [ -z "$devdir" ]; then 2114 warn "$_me: you must specify a mount-point" 2115 return 1 2116 fi 2117 debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 2118 if ! mount -t devfs dev "$devdir"; then 2119 warn "$_me: Unable to mount devfs on $devdir" 2120 return 1 2121 fi 2122 if [ -n "$rs" ]; then 2123 devfs_init_rulesets 2124 devfs_set_ruleset $rs $devdir 2125 devfs -m $devdir rule applyset 2126 fi 2127 return 0 2128} 2129 2130# Provide a function for normalizing the mounting of memory 2131# filesystems. This should allow the rest of the code here to remain 2132# as close as possible between 5-current and 4-stable. 2133# $1 = size 2134# $2 = mount point 2135# $3 = (optional) extra mdmfs flags 2136mount_md() 2137{ 2138 if [ -n "$3" ]; then 2139 flags="$3" 2140 fi 2141 /sbin/mdmfs $flags -s $1 ${mfs_type} $2 2142} 2143 2144# Code common to scripts that need to load a kernel module 2145# if it isn't in the kernel yet. Syntax: 2146# load_kld [-e regex] [-m module] file 2147# where -e or -m chooses the way to check if the module 2148# is already loaded: 2149# regex is egrep'd in the output from `kldstat -v', 2150# module is passed to `kldstat -m'. 2151# The default way is as though `-m file' were specified. 2152load_kld() 2153{ 2154 local _loaded _mod _opt _re 2155 2156 while getopts "e:m:" _opt; do 2157 case "$_opt" in 2158 e) _re="$OPTARG" ;; 2159 m) _mod="$OPTARG" ;; 2160 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 2161 esac 2162 done 2163 shift $(($OPTIND - 1)) 2164 if [ $# -ne 1 ]; then 2165 err 3 'USAGE: load_kld [-e regex] [-m module] file' 2166 fi 2167 _mod=${_mod:-$1} 2168 _loaded=false 2169 if [ -n "$_re" ]; then 2170 if kldstat -v | egrep -q -e "$_re"; then 2171 _loaded=true 2172 fi 2173 else 2174 if kldstat -q -m "$_mod"; then 2175 _loaded=true 2176 fi 2177 fi 2178 if ! $_loaded; then 2179 if ! kldload "$1"; then 2180 warn "Unable to load kernel module $1" 2181 return 1 2182 else 2183 info "$1 kernel module loaded." 2184 if [ -f "/etc/sysctl.kld.d/$1.conf" ]; then 2185 sysctl -f "/etc/sysctl.kld.d/$1.conf" 2186 fi 2187 fi 2188 else 2189 debug "load_kld: $1 kernel module already loaded." 2190 fi 2191 return 0 2192} 2193 2194# ltr str src dst [var] 2195# Change every $src in $str to $dst. 2196# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 2197# awk(1). If var is non-NULL, set it to the result. 2198ltr() 2199{ 2200 local _str _src _dst _out _com _var 2201 _str="$1" 2202 _src="$2" 2203 _dst="$3" 2204 _var="$4" 2205 _out="" 2206 2207 local IFS="${_src}" 2208 for _com in ${_str}; do 2209 if [ -z "${_out}" ]; then 2210 _out="${_com}" 2211 else 2212 _out="${_out}${_dst}${_com}" 2213 fi 2214 done 2215 if [ -n "${_var}" ]; then 2216 setvar "${_var}" "${_out}" 2217 else 2218 echo "${_out}" 2219 fi 2220} 2221 2222# Creates a list of providers for GELI encryption. 2223geli_make_list() 2224{ 2225 local devices devices2 2226 local provider mountpoint type options rest 2227 2228 # Create list of GELI providers from fstab. 2229 while read provider mountpoint type options rest ; do 2230 case ":${options}" in 2231 :*noauto*) 2232 noauto=yes 2233 ;; 2234 *) 2235 noauto=no 2236 ;; 2237 esac 2238 2239 case ":${provider}" in 2240 :#*) 2241 continue 2242 ;; 2243 *.eli) 2244 # Skip swap devices. 2245 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 2246 continue 2247 fi 2248 devices="${devices} ${provider}" 2249 ;; 2250 esac 2251 done < /etc/fstab 2252 2253 # Append providers from geli_devices. 2254 devices="${devices} ${geli_devices}" 2255 2256 for provider in ${devices}; do 2257 provider=${provider%.eli} 2258 provider=${provider#/dev/} 2259 devices2="${devices2} ${provider}" 2260 done 2261 2262 echo ${devices2} 2263} 2264 2265# Originally, root mount hold had to be released before mounting 2266# the root filesystem. This delayed the boot, so it was changed 2267# to only wait if the root device isn't readily available. This 2268# can result in rc scripts executing before all the devices - such 2269# as graid(8), or USB disks - can be accessed. This function can 2270# be used to explicitly wait for root mount holds to be released. 2271root_hold_wait() 2272{ 2273 local wait waited holders 2274 2275 waited=0 2276 while true; do 2277 holders="$(sysctl -n vfs.root_mount_hold)" 2278 if [ -z "${holders}" ]; then 2279 break; 2280 fi 2281 if [ ${waited} -eq 0 ]; then 2282 echo -n "Waiting ${root_hold_delay}s" \ 2283 "for the root mount holders: ${holders}" 2284 else 2285 echo -n . 2286 fi 2287 if [ ${waited} -ge ${root_hold_delay} ]; then 2288 echo 2289 break 2290 fi 2291 sleep 1 2292 waited=$(($waited + 1)) 2293 done 2294} 2295 2296# Find scripts in local_startup directories that use the old syntax 2297# 2298find_local_scripts_old() { 2299 zlist='' 2300 slist='' 2301 for dir in ${local_startup}; do 2302 if [ -d "${dir}" ]; then 2303 for file in ${dir}/[0-9]*.sh; do 2304 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 2305 continue 2306 zlist="$zlist $file" 2307 done 2308 for file in ${dir}/[!0-9]*.sh; do 2309 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 2310 continue 2311 slist="$slist $file" 2312 done 2313 fi 2314 done 2315} 2316 2317find_local_scripts_new() { 2318 local_rc='' 2319 for dir in ${local_startup}; do 2320 if [ -d "${dir}" ]; then 2321 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 2322 case "$file" in 2323 *.sample|*.pkgsave) ;; 2324 *) if [ -x "$file" ]; then 2325 local_rc="${local_rc} ${file}" 2326 fi 2327 ;; 2328 esac 2329 done 2330 fi 2331 done 2332} 2333 2334find_system_scripts() { 2335 system_rc='' 2336 for file in /etc/rc.d/*; do 2337 case "${file##*/}" in 2338 *.pkgsave) ;; 2339 *) if [ -x "$file" ]; then 2340 system_rc="${system_rc} ${file}" 2341 fi 2342 ;; 2343 esac 2344 done 2345} 2346 2347# check_required_{before|after} command 2348# Check for things required by the command before and after its precmd, 2349# respectively. The two separate functions are needed because some 2350# conditions should prevent precmd from being run while other things 2351# depend on precmd having already been run. 2352# 2353check_required_before() 2354{ 2355 local _f 2356 2357 case "$1" in 2358 start) 2359 for _f in $required_vars; do 2360 if ! checkyesno $_f; then 2361 warn "\$${_f} is not enabled." 2362 if [ -z "$rc_force" ]; then 2363 return 1 2364 fi 2365 fi 2366 done 2367 2368 for _f in $required_dirs; do 2369 if [ ! -d "${_f}/." ]; then 2370 warn "${_f} is not a directory." 2371 if [ -z "$rc_force" ]; then 2372 return 1 2373 fi 2374 fi 2375 done 2376 2377 for _f in $required_files; do 2378 if [ ! -r "${_f}" ]; then 2379 warn "${_f} is not readable." 2380 if [ -z "$rc_force" ]; then 2381 return 1 2382 fi 2383 fi 2384 done 2385 ;; 2386 esac 2387 2388 return 0 2389} 2390 2391check_required_after() 2392{ 2393 local _f _args 2394 2395 case "$1" in 2396 start) 2397 for _f in $required_modules; do 2398 case "${_f}" in 2399 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 2400 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 2401 *) _args="${_f}" ;; 2402 esac 2403 if ! load_kld ${_args}; then 2404 if [ -z "$rc_force" ]; then 2405 return 1 2406 fi 2407 fi 2408 done 2409 ;; 2410 esac 2411 2412 return 0 2413} 2414 2415# check_jail mib 2416# Return true if security.jail.$mib exists and set to 1. 2417 2418check_jail() 2419{ 2420 local _mib _v 2421 2422 _mib=$1 2423 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 2424 case $_v in 2425 1) return 0;; 2426 esac 2427 fi 2428 return 1 2429} 2430 2431# check_kern_features mib 2432# Return existence of kern.features.* sysctl MIB as true or 2433# false. The result will be cached in $_rc_cache_kern_features_ 2434# namespace. "0" means the kern.features.X exists. 2435 2436check_kern_features() 2437{ 2438 local _v 2439 2440 [ -n "$1" ] || return 1; 2441 eval _v=\$_rc_cache_kern_features_$1 2442 [ -n "$_v" ] && return "$_v"; 2443 2444 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 2445 eval _rc_cache_kern_features_$1=0 2446 return 0 2447 else 2448 eval _rc_cache_kern_features_$1=1 2449 return 1 2450 fi 2451} 2452 2453# check_namevarlist var 2454# Return "0" if ${name}_var is reserved in rc.subr. 2455 2456_rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend setup" 2457check_namevarlist() 2458{ 2459 local _v 2460 2461 for _v in $_rc_namevarlist; do 2462 case $1 in 2463 $_v) return 0 ;; 2464 esac 2465 done 2466 2467 return 1 2468} 2469 2470# _echoonce var msg mode 2471# mode=0: Echo $msg if ${$var} is empty. 2472# After doing echo, a string is set to ${$var}. 2473# 2474# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2475# 2476_echoonce() 2477{ 2478 local _var _msg _mode 2479 eval _var=\$$1 2480 _msg=$2 2481 _mode=$3 2482 2483 case $_mode in 2484 1) [ -n "$_var" ] && echo "$_msg" ;; 2485 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2486 esac 2487} 2488 2489# If the loader env variable rc.debug is set, turn on debugging. rc.conf will 2490# still override this, but /etc/defaults/rc.conf can't unconditionally set this 2491# since it would undo what we've done here. 2492if kenv -q rc.debug > /dev/null ; then 2493 rc_debug=YES 2494fi 2495 2496boottrace_cmd=`command -v boottrace` 2497if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then 2498 rc_boottrace=YES 2499fi 2500 2501# Allow for local additions and overrides. 2502# Use vdot to ensure the file has not been tampered with. 2503vdot /etc/local.rc.subr 2504 2505# Avoid noise - when we do not have /usr mounted, 2506# and we cannot use safe_dot without sed. 2507if ! have basename; then 2508 basename() 2509 { 2510 local b=${1%$2} 2511 echo ${b##*/} 2512 } 2513 tty() 2514 { 2515 return 0 2516 } 2517else 2518 # safe_eval.sh provides safe_dot - for untrusted files 2519 $_SAFE_EVAL_SH vdot /libexec/safe_eval.sh 2520fi 2521$_DEBUG_SH vdot /libexec/debug.sh 2522 2523# Ensure we can still operate if debug.sh and 2524# safe_eval.sh are not found. 2525if have DebugOn; then 2526 # allow DEBUG_SH to be set from loader prompt 2527 DEBUG_SH=${DEBUG_SH:-$(kenv -q DEBUG_SH)} 2528else 2529 DebugOn() { return 0; } 2530 DebugOff() { return 0; } 2531fi 2532if ! have save_dot; then 2533 safe_dot() { dot "$@"; } 2534fi 2535