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