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 _svcj_ipaddrs=\$${name}_svcj_ipaddrs 1201 1202 if [ -n "$_env_file" ] && [ -r "${_env_file}" ]; then # load env from file 1203 set -a 1204 . $_env_file 1205 set +a 1206 fi 1207 1208 if [ -n "$_user" ]; then # unset $_user if running as that user 1209 if [ "$_user" = "$(eval $IDCMD)" ]; then 1210 unset _user 1211 fi 1212 fi 1213 1214 _svcj_ip="inherit" 1215 _svcj_ip4_addrs="" 1216 _svcj_ip6_addrs="" 1217 1218 for addr in $_svcj_ipaddrs; do 1219 case $addr in 1220 *:*) _svcj_ip6_addrs="$addr,${_svcj_ip6_addrs}" ;; 1221 *) _svcj_ip4_addrs="$addr,${_svcj_ip4_addrs}" ;; 1222 esac 1223 done 1224 1225 _svcj_cmd_options="" 1226 1227 if [ -n "$_svcj_ip4_addrs" ]; then 1228 _svcj_cmd_options="ip4.addr=${_svcj_ip4_addrs%*,} ${_svcj_cmd_options}" 1229 _svcj_ip="new" 1230 fi 1231 1232 if [ -n "$_svcj_ip6_addrs" ]; then 1233 _svcj_cmd_options="ip6.addr=${_svcj_ip6_addrs%*,} ${_svcj_cmd_options}" 1234 _svcj_ip="new" 1235 fi 1236 1237 if [ -n "$_svcj_options" ]; then # translate service jail options 1238 _svcj_sysvipc_x=0 1239 for _svcj_option in $_svcj_options; do 1240 case "$_svcj_option" in 1241 mlock) 1242 _svcj_cmd_options="allow.mlock ${_svcj_cmd_options}" 1243 ;; 1244 netv4) 1245 _svcj_cmd_options="ip4=${_svcj_ip} allow.reserved_ports ${_svcj_cmd_options}" 1246 ;; 1247 netv6) 1248 _svcj_cmd_options="ip6=${_svcj_ip} allow.reserved_ports ${_svcj_cmd_options}" 1249 ;; 1250 net_basic) 1251 _svcj_cmd_options="ip4=${_svcj_ip} ip6=${_svcj_ip} allow.reserved_ports ${_svcj_cmd_options}" 1252 ;; 1253 net_raw) 1254 _svcj_cmd_options="allow.raw_sockets ${_svcj_cmd_options}" 1255 ;; 1256 net_all) 1257 _svcj_cmd_options="allow.socket_af allow.raw_sockets allow.reserved_ports ip4=${_svcj_ip} ip6=${_svcj_ip} ${_svcj_cmd_options}" 1258 ;; 1259 nfsd) 1260 _svcj_cmd_options="allow.nfsd enforce_statfs=1 ${_svcj_cmd_options}" 1261 ;; 1262 settime) 1263 _svcj_cmd_options="allow.settime ${_svcj_cmd_options}" 1264 ;; 1265 sysvipc) 1266 _svcj_sysvipc_x=$((${_svcj_sysvipc_x} + 1)) 1267 _svcj_cmd_options="sysvmsg=inherit sysvsem=inherit sysvshm=inherit ${_svcj_cmd_options}" 1268 ;; 1269 sysvipcnew) 1270 _svcj_sysvipc_x=$((${_svcj_sysvipc_x} + 1)) 1271 _svcj_cmd_options="sysvmsg=new sysvsem=new sysvshm=new ${_svcj_cmd_options}" 1272 ;; 1273 vmm) 1274 _svcj_cmd_options="allow.vmm ${_svcj_cmd_options}" 1275 ;; 1276 *) 1277 echo ${name}: unknown service jail option: $_svcj_option 1278 ;; 1279 esac 1280 done 1281 if [ ${_svcj_sysvipc_x} -gt 1 ]; then 1282 echo -n "ERROR: more than one sysvipc option is " 1283 echo "specified in ${name}_svcj_options: $_svcj_options" 1284 return 1 1285 fi 1286 fi 1287 1288 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 1289 1290 for _elem in $_keywords; do 1291 if [ "$_elem" != "$rc_arg" ]; then 1292 continue 1293 fi 1294 # if ${rcvar} is set, $1 is not "rcvar", "describe", 1295 # "enable", "delete" or "status", and ${rc_pid} is 1296 # not set, run: 1297 # checkyesno ${rcvar} 1298 # and return if that failed 1299 # 1300 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \ 1301 -a "$rc_arg" != "delete" -a "$rc_arg" != "enable" \ 1302 -a "$rc_arg" != "describe" -a "$rc_arg" != "status" ] || 1303 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 1304 if ! checkyesno ${rcvar}; then 1305 [ "$rc_arg" = "start" ] && _run_rc_offcmd 1306 if [ -z "${rc_quiet}" ]; then 1307 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 1308 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 1309 echo "instead of '${rc_arg}'." 1310 fi 1311 return 0 1312 fi 1313 fi 1314 1315 if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then 1316 if [ -z "$rc_quiet" ]; then 1317 echo 1>&2 "${name} already running? " \ 1318 "(pid=$rc_pid)." 1319 fi 1320 return 1 1321 fi 1322 1323 # if there's a custom ${XXX_cmd}, 1324 # run that instead of the default 1325 # 1326 eval _cmd=\$${rc_arg}_cmd \ 1327 _precmd=\$${rc_arg}_precmd \ 1328 _postcmd=\$${rc_arg}_postcmd 1329 1330 if [ -n "$_cmd" ]; then 1331 if [ "$_cmd" != : ]; then 1332 rc_trace 1 "$_cmd" 1333 fi 1334 if [ -n "$_env" ]; then 1335 eval "export -- $_env" 1336 fi 1337 1338 if [ "${_rc_svcj}" != jailing ]; then 1339 # service can redefine all so 1340 # check for valid setup target 1341 if [ "$rc_arg" = 'start' -o \ 1342 "$rc_arg" = 'restart' -o \ 1343 "$rc_arg" = 'reload' ]; then 1344 _run_rc_setup || \ 1345 warn "failed to setup ${name}" 1346 fi 1347 _run_rc_precmd || return 1 1348 fi 1349 if ! checkyesno ${name}_svcj; then 1350 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || return 1 1351 else 1352 case "$rc_arg" in 1353 start) 1354 if [ "${_rc_svcj}" != jailing ]; then 1355 _return=1 1356 _do_jailing=1 1357 1358 if check_jail jailed; then 1359 if [ $(${SYSCTL_N} security.jail.children.max) -eq 0 ]; then 1360 echo ERROR: jail parameter children.max is set to 0, can not create a new service jail. 1361 _do_jailing=0 1362 else 1363 _free_jails=$(($(${SYSCTL_N} security.jail.children.max) - $(${SYSCTL_N} security.jail.children.cur))) 1364 if [ ${_free_jails} -eq 0 ]; then 1365 echo ERROR: max number of jail children reached, can not create a new service jail. 1366 _do_jailing=0 1367 1368 fi 1369 fi 1370 fi 1371 if [ ${_do_jailing} -eq 1 ]; then 1372 $JAIL_CMD -c $_svcj_generic_params $_svcj_cmd_options \ 1373 exec.start="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}start $rc_extra_args" \ 1374 exec.stop="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}stop $rc_extra_args" \ 1375 exec.consolelog="/var/log/svcj_${name}_console.log" \ 1376 name=svcj-${name} && _return=0 1377 fi 1378 else 1379 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || _return=1 1380 fi 1381 ;; 1382 stop) 1383 if [ "${_rc_svcj}" != jailing ]; then 1384 $SERVICE -E _rc_svcj=jailing -j svcj-${name} ${name} ${_rc_prefix}stop $rc_extra_args || _return=1 1385 $JAIL_CMD -r svcj-${name} 2>/dev/null 1386 else 1387 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || _return=1 1388 fi 1389 ;; 1390 restart|status) ;; # no special case needed for svcj or handled somewhere else 1391 *) 1392 eval _rc_svcj_extra_cmd=\$${name}_${rc_arg}_svcj_enable 1393 : ${_rc_svcj_extra_cmd:=NO} 1394 if checkyesno _rc_svcj_extra_cmd && [ "${_rc_svcj}" != jailing ]; then 1395 $SERVICE -v -E _rc_svcj=jailing -j svcj-${name} ${name} ${_rc_prefix}${rc_arg} $rc_extra_args || _return=1 1396 else 1397 _run_rc_doit "$_cpusetcmd $_cmd $rc_extra_args" || _return=1 1398 fi 1399 ;; 1400 esac 1401 fi 1402 if [ "${_rc_svcj}" != jailing ]; then 1403 _run_rc_postcmd 1404 fi 1405 return $_return 1406 fi 1407 1408 case "$rc_arg" in # default operations... 1409 1410 describe) 1411 if [ -n "$desc" ]; then 1412 echo "$desc" 1413 fi 1414 ;; 1415 1416 extracommands) 1417 echo "$extra_commands" 1418 ;; 1419 1420 enable) 1421 _out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=YES") && 1422 echo "$name enabled in ${_out%%:*}" 1423 ;; 1424 1425 disable) 1426 _out=$(/usr/sbin/sysrc -vs "$name" "$rcvar=NO") && 1427 echo "$name disabled in ${_out%%:*}" 1428 ;; 1429 1430 delete) 1431 _files= 1432 for _file in $(/usr/sbin/sysrc -lEs "$name"); do 1433 _out=$(/usr/sbin/sysrc -Fif $_file "$rcvar") && _files="$_files $_file" 1434 done 1435 /usr/sbin/sysrc -x "$rcvar" && echo "$rcvar deleted in ${_files# }" 1436 # delete file in rc.conf.d if desired and empty. 1437 checkyesno service_delete_empty || _files= 1438 for _file in $_files; do 1439 [ "$_file" = "${_file#*/rc.conf.d/}" ] && continue 1440 [ $(/usr/bin/stat -f%z $_file) -gt 0 ] && continue 1441 /bin/rm "$_file" && echo "Empty file $_file removed" 1442 done 1443 ;; 1444 1445 status) 1446 _run_rc_precmd || return 1 1447 if [ -n "$rc_pid" ]; then 1448 echo "${name} is running as pid $rc_pid." 1449 else 1450 echo "${name} is not running." 1451 return 1 1452 fi 1453 _run_rc_postcmd 1454 ;; 1455 1456 start) 1457 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 1458 warn "run_rc_command: cannot run $command" 1459 return 1 1460 fi 1461 1462 if [ "${_rc_svcj}" != jailing ]; then 1463 _run_rc_setup || warn "failed to setup ${name}" 1464 1465 if ! _run_rc_precmd; then 1466 warn "failed precmd routine for ${name}" 1467 return 1 1468 fi 1469 fi 1470 1471 if checkyesno ${name}_svcj; then 1472 if [ "${_rc_svcj}" != jailing ]; then 1473 if check_jail jailed; then 1474 if [ $(${SYSCTL_N} security.jail.children.max) -eq 0 ]; then 1475 echo ERROR: jail parameter children.max is set to 0, can not create a new service jail. 1476 return 1 1477 else 1478 _free_jails=$(($(${SYSCTL_N} security.jail.children.max) - $(${SYSCTL_N} security.jail.children.cur))) 1479 if [ ${_free_jails} -eq 0 ]; then 1480 echo ERROR: max number of jail children reached, can not create a new service jail. 1481 return 1 1482 fi 1483 fi 1484 fi 1485 $JAIL_CMD -c $_svcj_generic_params $_svcj_cmd_options\ 1486 exec.start="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}start $rc_extra_args" \ 1487 exec.stop="${SERVICE} -E _rc_svcj=jailing ${name} ${_rc_prefix}stop $rc_extra_args" \ 1488 exec.consolelog="/var/log/svcj_${name}_console.log" \ 1489 name=svcj-${name} || return 1 1490 fi 1491 fi 1492 1493 # setup the full command to run 1494 # 1495 startmsg "Starting ${name}." 1496 if [ -n "$_chroot" ]; then 1497 _cd= 1498 _doit="\ 1499${_nice:+nice -n $_nice }\ 1500$_cpusetcmd \ 1501${_fib:+setfib -F $_fib }\ 1502${_env:+env $_env }\ 1503chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 1504$_chroot $command $rc_flags $command_args" 1505 else 1506 _cd="${_chdir:+cd $_chdir && }" 1507 _doit="\ 1508${_fib:+setfib -F $_fib }\ 1509${_env:+env $_env }\ 1510$_cpusetcmd $command $rc_flags $command_args" 1511 if [ -n "$_user" ]; then 1512 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 1513 fi 1514 if [ -n "$_nice" ]; then 1515 if [ -z "$_user" ]; then 1516 _doit="sh -c \"$_doit\"" 1517 fi 1518 _doit="nice -n $_nice $_doit" 1519 fi 1520 if [ -n "$_prepend" ]; then 1521 _doit="$_prepend $_doit" 1522 fi 1523 fi 1524 1525 # Prepend default limits 1526 _doit="$_cd limits -C $_login_class $_limits $_doit" 1527 1528 local _really_run_it=true 1529 if checkyesno ${name}_svcj; then 1530 if [ "${_rc_svcj}" != jailing ]; then 1531 _really_run_it=false 1532 fi 1533 fi 1534 1535 if [ "$_really_run_it" = true ]; then 1536 # run the full command 1537 # 1538 if ! _run_rc_doit "$_doit"; then 1539 warn "failed to start ${name}" 1540 return 1 1541 fi 1542 fi 1543 1544 if [ "${_rc_svcj}" != jailing ]; then 1545 # finally, run postcmd 1546 # 1547 _run_rc_postcmd 1548 fi 1549 ;; 1550 1551 stop) 1552 if [ -z "$rc_pid" ]; then 1553 [ -n "$rc_fast" ] && return 0 1554 _run_rc_notrunning 1555 return 1 1556 fi 1557 1558 _run_rc_precmd || return 1 1559 1560 # send the signal to stop 1561 # 1562 echo "Stopping ${name}." 1563 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1564 _run_rc_doit "$_doit" || return 1 1565 1566 # wait for the command to exit, 1567 # and run postcmd. 1568 wait_for_pids $rc_pid 1569 1570 if checkyesno ${name}_svcj; then 1571 # remove service jail 1572 $JAIL_CMD -r svcj-${name} 2>/dev/null 1573 fi 1574 1575 _run_rc_postcmd 1576 ;; 1577 1578 reload) 1579 if [ -z "$rc_pid" ]; then 1580 _run_rc_notrunning 1581 return 1 1582 fi 1583 1584 _run_rc_setup || warn "failed to setup ${name}" 1585 1586 _run_rc_precmd || return 1 1587 1588 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1589 _run_rc_doit "$_doit" || return 1 1590 1591 _run_rc_postcmd 1592 ;; 1593 1594 restart) 1595 _run_rc_setup || warn "failed to setup ${name}" 1596 1597 # prevent restart being called more 1598 # than once by any given script 1599 # 1600 if ${_rc_restart_done:-false}; then 1601 return 0 1602 fi 1603 _rc_restart_done=true 1604 1605 _run_rc_precmd || return 1 1606 1607 # run those in a subshell to keep global variables 1608 ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1609 ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1610 _return=$? 1611 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 1612 1613 _run_rc_postcmd 1614 ;; 1615 1616 poll) 1617 _run_rc_precmd || return 1 1618 if [ -n "$rc_pid" ]; then 1619 wait_for_pids $rc_pid 1620 fi 1621 _run_rc_postcmd 1622 ;; 1623 1624 rcvar) 1625 echo -n "# $name" 1626 if [ -n "$desc" ]; then 1627 echo " : $desc" 1628 else 1629 echo "" 1630 fi 1631 echo "#" 1632 # Get unique vars in $rcvar $rcvars 1633 for _v in $rcvar $rcvars; do 1634 case $v in 1635 $_v\ *|\ *$_v|*\ $_v\ *) ;; 1636 *) v="${v# } $_v" ;; 1637 esac 1638 done 1639 1640 # Display variables. 1641 for _v in $v; do 1642 if [ -z "$_v" ]; then 1643 continue 1644 fi 1645 1646 eval _desc=\$${_v}_desc 1647 eval _defval=\$${_v}_defval 1648 _h="-" 1649 1650 eval echo \"$_v=\\\"\$$_v\\\"\" 1651 # decode multiple lines of _desc 1652 while [ -n "$_desc" ]; do 1653 case $_desc in 1654 *^^*) 1655 echo "# $_h ${_desc%%^^*}" 1656 _desc=${_desc#*^^} 1657 _h=" " 1658 ;; 1659 *) 1660 echo "# $_h ${_desc}" 1661 break 1662 ;; 1663 esac 1664 done 1665 echo "# (default: \"$_defval\")" 1666 done 1667 echo "" 1668 ;; 1669 1670 *) 1671 rc_usage $_keywords 1672 ;; 1673 1674 esac 1675 1676 # Apply protect(1) to the PID if ${name}_oomprotect is set. 1677 case "$rc_arg" in 1678 start) 1679 # We cannot use protect(1) inside jails. 1680 if [ -n "$_oomprotect" ] && [ -f "${PROTECT}" ] && 1681 [ "$(sysctl -n security.jail.jailed)" -eq 0 ]; then 1682 [ -z "${rc_pid}" ] && eval $_pidcmd 1683 case $_oomprotect in 1684 [Aa][Ll][Ll]) 1685 ${PROTECT} -d -i -p ${rc_pid} 1686 ;; 1687 [Yy][Ee][Ss]) 1688 ${PROTECT} -p ${rc_pid} 1689 ;; 1690 esac 1691 fi 1692 ;; 1693 esac 1694 1695 return $_return 1696 done 1697 1698 echo 1>&2 "$0: unknown directive '$rc_arg'." 1699 rc_usage $_keywords 1700 # not reached 1701} 1702 1703# 1704# Helper functions for run_rc_command: common code. 1705# They use such global variables besides the exported rc_* ones: 1706# 1707# name R/W 1708# ------------------ 1709# _offcmd R 1710# _precmd R 1711# _postcmd R 1712# _return W 1713# _setup R 1714# 1715_run_rc_offcmd() 1716{ 1717 eval _offcmd=\$${name}_offcmd 1718 if [ -n "$_offcmd" ]; then 1719 if [ -n "$_env" ]; then 1720 eval "export -- $_env" 1721 fi 1722 debug "run_rc_command: ${name}_offcmd: $_offcmd $rc_extra_args" 1723 eval "$_offcmd $rc_extra_args" 1724 _return=$? 1725 fi 1726 return 0 1727} 1728 1729_run_rc_precmd() 1730{ 1731 check_required_before "$rc_arg" || return 1 1732 1733 if [ -n "$_precmd" ]; then 1734 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1735 eval "$_precmd $rc_extra_args" 1736 _return=$? 1737 1738 # If precmd failed and force isn't set, request exit. 1739 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1740 return 1 1741 fi 1742 fi 1743 1744 check_required_after "$rc_arg" || return 1 1745 1746 return 0 1747} 1748 1749_run_rc_postcmd() 1750{ 1751 if [ -n "$_postcmd" ]; then 1752 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1753 eval "$_postcmd $rc_extra_args" 1754 _return=$? 1755 fi 1756 return 0 1757} 1758 1759_run_rc_setup() 1760{ 1761 # prevent multiple execution on restart => stop/start split 1762 if ! ${_rc_restart_done:-false} && [ -n "$_setup" ]; then 1763 debug "run_rc_command: ${rc_arg}_setup: $_setup" 1764 eval "$_setup" 1765 _return=$? 1766 if [ $_return -ne 0 ]; then 1767 return 1 1768 fi 1769 fi 1770 return 0 1771} 1772 1773_run_rc_doit() 1774{ 1775 local _m 1776 1777 debug "run_rc_command: doit: $*" 1778 _m=$(umask) 1779 ${_umask:+umask ${_umask}} 1780 eval "$@" 1781 _return=$? 1782 umask ${_m} 1783 1784 # If command failed and force isn't set, request exit. 1785 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1786 return 1 1787 fi 1788 1789 return 0 1790} 1791 1792_run_rc_notrunning() 1793{ 1794 local _pidmsg 1795 1796 if [ -n "$pidfile" ]; then 1797 _pidmsg=" (check $pidfile)." 1798 else 1799 _pidmsg= 1800 fi 1801 echo 1>&2 "${name} not running?${_pidmsg}" 1802} 1803 1804_run_rc_killcmd() 1805{ 1806 local _cmd 1807 1808 _cmd="kill -$1 $rc_pid" 1809 if [ -n "$_user" ]; then 1810 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1811 fi 1812 echo "$_cmd" 1813} 1814 1815# 1816# run_rc_script file arg 1817# Start the script `file' with `arg', and correctly handle the 1818# return value from the script. 1819# If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's 1820# an old-style startup file. 1821# If `file' appears to be a backup or scratch file, ignore it. 1822# Otherwise if it is executable run as a child process. 1823# 1824run_rc_script() 1825{ 1826 _file=$1 1827 _arg=$2 1828 if [ -z "$_file" -o -z "$_arg" ]; then 1829 err 3 'USAGE: run_rc_script file arg' 1830 fi 1831 1832 unset name command command_args command_interpreter \ 1833 extra_commands pidfile procname \ 1834 rcvar rcvars rcvars_obsolete required_dirs required_files \ 1835 required_vars 1836 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 1837 1838 rc_trace 0 "$_file $_arg" 1839 # don't use it if we don't trust it 1840 is_verified $_file || return 1841 1842 rc_service="$_file" 1843 case "$_file" in 1844 /etc/rc.d/*.sh) # no longer allowed in the base 1845 warn "Ignoring old-style startup script $_file" 1846 ;; 1847 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 1848 warn "Ignoring scratch file $_file" 1849 ;; 1850 *) # run in subshell 1851 if [ -x $_file ]; then 1852 DebugOn $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg 1853 1854 if [ -n "$rc_boottrace" ]; then 1855 boottrace_fn "$_file" "$_arg" 1856 else 1857 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1858 trap "echo Script $_file interrupted >&2 ; exit 1" 2 1859 trap "echo Script $_file running >&2" 29 1860 set $_arg; . $_file ) 1861 fi 1862 DebugOff rc=$? $_file $_file:$_arg rc:${_file##*/} rc:${_file##*/}:$_arg ${_file##*/} ${_file##*/}:$_arg 1863 fi 1864 ;; 1865 esac 1866} 1867 1868# 1869# run_rc_scripts [options] file [...] 1870# 1871# Call `run_rc_script' for each "file" unless already listed in 1872# $_rc_elem_done. 1873# 1874# Options: 1875# 1876# --arg "arg" 1877# Pass "arg" to `run_rc_script' default is $_boot. 1878# 1879# --break "marker" 1880# If any "file" matches "marker" stop processing. 1881# 1882_rc_elem_done= 1883run_rc_scripts() 1884{ 1885 local _arg=${_boot} 1886 local _rc_elem 1887 local _rc_breaks= 1888 1889 while :; do 1890 case "$1" in 1891 --arg) 1892 _arg="$2" 1893 shift 2 1894 ;; 1895 --break) 1896 _rc_breaks="$_rc_breaks $2" 1897 shift 2 1898 ;; 1899 *) 1900 break 1901 ;; 1902 esac 1903 done 1904 for _rc_elem in "$@"; do 1905 : _rc_elem=$_rc_elem 1906 case " $_rc_elem_done " in 1907 *" $_rc_elem "*) 1908 continue 1909 ;; 1910 esac 1911 run_rc_script ${_rc_elem} ${_arg} 1912 _rc_elem_done="$_rc_elem_done $_rc_elem" 1913 case " $_rc_breaks " in 1914 *" ${_rc_elem##*/} "*) 1915 break 1916 ;; 1917 esac 1918 done 1919} 1920 1921boottrace_fn() 1922{ 1923 local _file _arg 1924 _file=$1 1925 _arg=$2 1926 1927 _boot="${_boot}" rc_fast="${rc_fast}" autoboot="${autoboot}" \ 1928 $boottrace_cmd "$_file" "$_arg" 1929} 1930 1931# 1932# load_rc_config [service] 1933# Source in the configuration file(s) for a given service. 1934# If no service is specified, only the global configuration 1935# file(s) will be loaded. 1936# 1937load_rc_config() 1938{ 1939 local _name _rcvar_val _var _defval _v _msg _new _d _dot 1940 _name=$1 1941 _dot=${load_rc_config_reader:-dot} 1942 1943 case "$_dot" in 1944 dot|[sv]dot) 1945 ;; 1946 *) warn "Ignoring invalid load_rc_config_reader" 1947 _dot=dot 1948 ;; 1949 esac 1950 case "$1" in 1951 -s|--safe) 1952 _dot=sdot 1953 _name=$2 1954 shift 1955 ;; 1956 -v|--verify) 1957 _dot=vdot 1958 _name=$2 1959 shift 1960 ;; 1961 esac 1962 1963 DebugOn rc:$_name $_name 1964 1965 if ${_rc_conf_loaded:-false}; then 1966 : 1967 else 1968 if [ -r /etc/defaults/rc.conf ]; then 1969 debug "Sourcing /etc/defaults/rc.conf" 1970 $_dot /etc/defaults/rc.conf 1971 source_rc_confs 1972 elif [ -r /etc/rc.conf ]; then 1973 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 1974 $_dot /etc/rc.conf 1975 fi 1976 _rc_conf_loaded=true 1977 fi 1978 1979 # If a service name was specified, attempt to load 1980 # service-specific configuration 1981 if [ -n "$_name" ] ; then 1982 for _d in /etc ${local_startup}; do 1983 _d=${_d%/rc.d} 1984 if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1985 debug "Sourcing ${_d}/rc.conf.d/$_name" 1986 $_dot ${_d}/rc.conf.d/"$_name" 1987 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1988 local _rc 1989 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1990 if [ -f "$_rc" ] ; then 1991 debug "Sourcing $_rc" 1992 $_dot "$_rc" 1993 fi 1994 done 1995 fi 1996 done 1997 fi 1998 1999 # Set defaults if defined. 2000 for _var in $rcvar $rcvars; do 2001 eval _defval=\$${_var}_defval 2002 if [ -n "$_defval" ]; then 2003 eval : \${$_var:=\$${_var}_defval} 2004 fi 2005 done 2006 2007 # check obsolete rc.conf variables 2008 for _var in $rcvars_obsolete; do 2009 eval _v=\$$_var 2010 eval _msg=\$${_var}_obsolete_msg 2011 eval _new=\$${_var}_newvar 2012 case $_v in 2013 "") 2014 ;; 2015 *) 2016 if [ -z "$_new" ]; then 2017 _msg="Ignored." 2018 else 2019 eval $_new=\"\$$_var\" 2020 if [ -z "$_msg" ]; then 2021 _msg="Use \$$_new instead." 2022 fi 2023 fi 2024 warn "\$$_var is obsolete. $_msg" 2025 ;; 2026 esac 2027 done 2028} 2029 2030# 2031# load_rc_config_var name var 2032# Read the rc.conf(5) var for name and set in the 2033# current shell, using load_rc_config in a subshell to prevent 2034# unwanted side effects from other variable assignments. 2035# 2036load_rc_config_var() 2037{ 2038 if [ $# -ne 2 ]; then 2039 err 3 'USAGE: load_rc_config_var name var' 2040 fi 2041 eval $(eval '( 2042 load_rc_config '$1' >/dev/null; 2043 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 2044 echo '$2'=\'\''${'$2'}\'\''; 2045 fi 2046 )' ) 2047} 2048 2049# 2050# rc_usage commands 2051# Print a usage string for $0, with `commands' being a list of 2052# valid commands. 2053# 2054rc_usage() 2055{ 2056 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 2057 2058 _sep= 2059 for _elem; do 2060 echo -n 1>&2 "$_sep$_elem" 2061 _sep="|" 2062 done 2063 echo 1>&2 ")" 2064 exit 1 2065} 2066 2067# 2068# err exitval message 2069# Display message to stderr and log to the syslog, and exit with exitval. 2070# 2071err() 2072{ 2073 exitval=$1 2074 shift 2075 2076 if [ -x /usr/bin/logger ]; then 2077 logger "$0: ERROR: $*" 2078 fi 2079 echo 1>&2 "$0: ERROR: $*" 2080 exit $exitval 2081} 2082 2083# 2084# warn message 2085# Display message to stderr and log to the syslog. 2086# 2087warn() 2088{ 2089 if [ -x /usr/bin/logger ]; then 2090 logger "$0: WARNING: $*" 2091 fi 2092 echo 1>&2 "$0: WARNING: $*" 2093} 2094 2095# 2096# info message 2097# Display informational message to stdout and log to syslog. 2098# 2099info() 2100{ 2101 case ${rc_info} in 2102 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 2103 if [ -x /usr/bin/logger ]; then 2104 logger "$0: INFO: $*" 2105 fi 2106 echo "$0: INFO: $*" 2107 ;; 2108 esac 2109} 2110 2111# 2112# debug message 2113# If debugging is enabled in rc.conf output message to stderr. 2114# BEWARE that you don't call any subroutine that itself calls this 2115# function. 2116# 2117debug() 2118{ 2119 case ${rc_debug} in 2120 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 2121 if [ -x /usr/bin/logger ]; then 2122 logger "$0: DEBUG: $*" 2123 fi 2124 echo 1>&2 "$0: DEBUG: $*" 2125 ;; 2126 esac 2127} 2128 2129# 2130# backup_file action file cur backup 2131# Make a backup copy of `file' into `cur', and save the previous 2132# version of `cur' as `backup'. 2133# 2134# The `action' keyword can be one of the following: 2135# 2136# add `file' is now being backed up (and is possibly 2137# being reentered into the backups system). `cur' 2138# is created. 2139# 2140# update `file' has changed and needs to be backed up. 2141# If `cur' exists, it is copied to `back' 2142# and then `file' is copied to `cur'. 2143# 2144# remove `file' is no longer being tracked by the backups 2145# system. `cur' is moved `back'. 2146# 2147# 2148backup_file() 2149{ 2150 _action=$1 2151 _file=$2 2152 _cur=$3 2153 _back=$4 2154 2155 case $_action in 2156 add|update) 2157 if [ -f $_cur ]; then 2158 cp -p $_cur $_back 2159 fi 2160 cp -p $_file $_cur 2161 chown root:wheel $_cur 2162 ;; 2163 remove) 2164 mv -f $_cur $_back 2165 ;; 2166 esac 2167} 2168 2169# make_symlink src link 2170# Make a symbolic link 'link' to src from basedir. If the 2171# directory in which link is to be created does not exist 2172# a warning will be displayed and an error will be returned. 2173# Returns 0 on success, 1 otherwise. 2174# 2175make_symlink() 2176{ 2177 local src link linkdir _me 2178 src="$1" 2179 link="$2" 2180 linkdir="`dirname $link`" 2181 _me="make_symlink()" 2182 2183 if [ -z "$src" -o -z "$link" ]; then 2184 warn "$_me: requires two arguments." 2185 return 1 2186 fi 2187 if [ ! -d "$linkdir" ]; then 2188 warn "$_me: the directory $linkdir does not exist." 2189 return 1 2190 fi 2191 if ! ln -sf $src $link; then 2192 warn "$_me: unable to make a symbolic link from $link to $src" 2193 return 1 2194 fi 2195 return 0 2196} 2197 2198# devfs_rulesets_from_file file 2199# Reads a set of devfs commands from file, and creates 2200# the specified rulesets with their rules. Returns non-zero 2201# if there was an error. 2202# 2203devfs_rulesets_from_file() 2204{ 2205 local file _err _me _opts 2206 file="$1" 2207 _me="devfs_rulesets_from_file" 2208 _err=0 2209 2210 if [ -z "$file" ]; then 2211 warn "$_me: you must specify a file" 2212 return 1 2213 fi 2214 if [ ! -e "$file" ]; then 2215 debug "$_me: no such file ($file)" 2216 return 0 2217 fi 2218 2219 # Disable globbing so that the rule patterns are not expanded 2220 # by accident with matching filesystem entries. 2221 _opts=$-; set -f 2222 2223 debug "reading rulesets from file ($file)" 2224 { while read line 2225 do 2226 case $line in 2227 \#*) 2228 continue 2229 ;; 2230 \[*\]*) 2231 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 2232 if [ -z "$rulenum" ]; then 2233 warn "$_me: cannot extract rule number ($line)" 2234 _err=1 2235 break 2236 fi 2237 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 2238 if [ -z "$rulename" ]; then 2239 warn "$_me: cannot extract rule name ($line)" 2240 _err=1 2241 break; 2242 fi 2243 eval $rulename=\$rulenum 2244 debug "found ruleset: $rulename=$rulenum" 2245 if ! /sbin/devfs rule -s $rulenum delset; then 2246 _err=1 2247 break 2248 fi 2249 ;; 2250 *) 2251 rulecmd="${line%%"\#*"}" 2252 # evaluate the command incase it includes 2253 # other rules 2254 if [ -n "$rulecmd" ]; then 2255 debug "adding rule ($rulecmd)" 2256 if ! eval /sbin/devfs rule -s $rulenum $rulecmd 2257 then 2258 _err=1 2259 break 2260 fi 2261 fi 2262 ;; 2263 esac 2264 if [ $_err -ne 0 ]; then 2265 debug "error in $_me" 2266 break 2267 fi 2268 done } < $file 2269 case $_opts in *f*) ;; *) set +f ;; esac 2270 return $_err 2271} 2272 2273# devfs_init_rulesets 2274# Initializes rulesets from configuration files. Returns 2275# non-zero if there was an error. 2276# 2277devfs_init_rulesets() 2278{ 2279 local file _me 2280 _me="devfs_init_rulesets" 2281 2282 # Go through this only once 2283 if [ -n "$devfs_rulesets_init" ]; then 2284 debug "$_me: devfs rulesets already initialized" 2285 return 2286 fi 2287 for file in $devfs_rulesets; do 2288 if ! devfs_rulesets_from_file $file; then 2289 warn "$_me: could not read rules from $file" 2290 return 1 2291 fi 2292 done 2293 devfs_rulesets_init=1 2294 debug "$_me: devfs rulesets initialized" 2295 return 0 2296} 2297 2298# devfs_set_ruleset ruleset [dir] 2299# Sets the default ruleset of dir to ruleset. The ruleset argument 2300# must be a ruleset name as specified in devfs.rules(5) file. 2301# Returns non-zero if it could not set it successfully. 2302# 2303devfs_set_ruleset() 2304{ 2305 local devdir rs _me 2306 [ -n "$1" ] && eval rs=\$$1 || rs= 2307 [ -n "$2" ] && devdir="-m "$2"" || devdir= 2308 _me="devfs_set_ruleset" 2309 2310 if [ -z "$rs" ]; then 2311 warn "$_me: you must specify a ruleset number" 2312 return 1 2313 fi 2314 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 2315 if ! /sbin/devfs $devdir ruleset $rs; then 2316 warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 2317 return 1 2318 fi 2319 return 0 2320} 2321 2322# devfs_apply_ruleset ruleset [dir] 2323# Apply ruleset number $ruleset to the devfs mountpoint $dir. 2324# The ruleset argument must be a ruleset name as specified 2325# in a devfs.rules(5) file. Returns 0 on success or non-zero 2326# if it could not apply the ruleset. 2327# 2328devfs_apply_ruleset() 2329{ 2330 local devdir rs _me 2331 [ -n "$1" ] && eval rs=\$$1 || rs= 2332 [ -n "$2" ] && devdir="-m "$2"" || devdir= 2333 _me="devfs_apply_ruleset" 2334 2335 if [ -z "$rs" ]; then 2336 warn "$_me: you must specify a ruleset" 2337 return 1 2338 fi 2339 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 2340 if ! /sbin/devfs $devdir rule -s $rs applyset; then 2341 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 2342 return 1 2343 fi 2344 return 0 2345} 2346 2347# devfs_domount dir [ruleset] 2348# Mount devfs on dir. If ruleset is specified it is set 2349# on the mount-point. It must also be a ruleset name as specified 2350# in a devfs.rules(5) file. Returns 0 on success. 2351# 2352devfs_domount() 2353{ 2354 local devdir rs _me 2355 devdir="$1" 2356 [ -n "$2" ] && rs=$2 || rs= 2357 _me="devfs_domount()" 2358 2359 if [ -z "$devdir" ]; then 2360 warn "$_me: you must specify a mount-point" 2361 return 1 2362 fi 2363 debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 2364 if ! mount -t devfs dev "$devdir"; then 2365 warn "$_me: Unable to mount devfs on $devdir" 2366 return 1 2367 fi 2368 if [ -n "$rs" ]; then 2369 devfs_init_rulesets 2370 devfs_set_ruleset $rs $devdir 2371 devfs -m $devdir rule applyset 2372 fi 2373 return 0 2374} 2375 2376# Provide a function for normalizing the mounting of memory 2377# filesystems. This should allow the rest of the code here to remain 2378# as close as possible between 5-current and 4-stable. 2379# $1 = size 2380# $2 = mount point 2381# $3 = (optional) extra mdmfs flags 2382mount_md() 2383{ 2384 if [ -n "$3" ]; then 2385 flags="$3" 2386 fi 2387 /sbin/mdmfs $flags -s $1 ${mfs_type} $2 2388} 2389 2390# Code common to scripts that need to load a kernel module 2391# if it isn't in the kernel yet. Syntax: 2392# load_kld [-e regex] [-m module] file 2393# where -e or -m chooses the way to check if the module 2394# is already loaded: 2395# regex is egrep'd in the output from `kldstat -v', 2396# module is passed to `kldstat -m'. 2397# The default way is as though `-m file' were specified. 2398load_kld() 2399{ 2400 local _loaded _mod _opt _re 2401 2402 while getopts "e:m:" _opt; do 2403 case "$_opt" in 2404 e) _re="$OPTARG" ;; 2405 m) _mod="$OPTARG" ;; 2406 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 2407 esac 2408 done 2409 shift $(($OPTIND - 1)) 2410 if [ $# -ne 1 ]; then 2411 err 3 'USAGE: load_kld [-e regex] [-m module] file' 2412 fi 2413 _mod=${_mod:-$1} 2414 _loaded=false 2415 if [ -n "$_re" ]; then 2416 if kldstat -v | egrep -q -e "$_re"; then 2417 _loaded=true 2418 fi 2419 else 2420 if kldstat -q -m "$_mod"; then 2421 _loaded=true 2422 fi 2423 fi 2424 if ! $_loaded; then 2425 if ! kldload "$1"; then 2426 warn "Unable to load kernel module $1" 2427 return 1 2428 else 2429 info "$1 kernel module loaded." 2430 if [ -f "/etc/sysctl.kld.d/$1.conf" ]; then 2431 sysctl -f "/etc/sysctl.kld.d/$1.conf" 2432 fi 2433 fi 2434 else 2435 debug "load_kld: $1 kernel module already loaded." 2436 fi 2437 return 0 2438} 2439 2440# ltr str src dst [var] 2441# Change every $src in $str to $dst. 2442# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 2443# awk(1). If var is non-NULL, set it to the result. 2444ltr() 2445{ 2446 local _str _src _dst _out _com _var 2447 _str="$1" 2448 _src="$2" 2449 _dst="$3" 2450 _var="$4" 2451 _out="" 2452 2453 local IFS="${_src}" 2454 for _com in ${_str}; do 2455 if [ -z "${_out}" ]; then 2456 _out="${_com}" 2457 else 2458 _out="${_out}${_dst}${_com}" 2459 fi 2460 done 2461 if [ -n "${_var}" ]; then 2462 setvar "${_var}" "${_out}" 2463 else 2464 echo "${_out}" 2465 fi 2466} 2467 2468# Creates a list of providers for GELI encryption. 2469geli_make_list() 2470{ 2471 local devices devices2 2472 local provider mountpoint type options rest 2473 2474 # Create list of GELI providers from fstab. 2475 while read provider mountpoint type options rest ; do 2476 case ":${options}" in 2477 :*noauto*) 2478 noauto=yes 2479 ;; 2480 *) 2481 noauto=no 2482 ;; 2483 esac 2484 2485 case ":${provider}" in 2486 :#*) 2487 continue 2488 ;; 2489 *.eli) 2490 # Skip swap devices. 2491 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 2492 continue 2493 fi 2494 devices="${devices} ${provider}" 2495 ;; 2496 esac 2497 done < /etc/fstab 2498 2499 # Append providers from geli_devices. 2500 devices="${devices} ${geli_devices}" 2501 2502 for provider in ${devices}; do 2503 provider=${provider%.eli} 2504 provider=${provider#/dev/} 2505 devices2="${devices2} ${provider}" 2506 done 2507 2508 echo ${devices2} 2509} 2510 2511# Originally, root mount hold had to be released before mounting 2512# the root filesystem. This delayed the boot, so it was changed 2513# to only wait if the root device isn't readily available. This 2514# can result in rc scripts executing before all the devices - such 2515# as graid(8), or USB disks - can be accessed. This function can 2516# be used to explicitly wait for root mount holds to be released. 2517root_hold_wait() 2518{ 2519 local wait waited holders 2520 2521 waited=0 2522 while true; do 2523 holders="$(sysctl -n vfs.root_mount_hold)" 2524 if [ -z "${holders}" ]; then 2525 break; 2526 fi 2527 if [ ${waited} -eq 0 ]; then 2528 echo -n "Waiting ${root_hold_delay}s" \ 2529 "for the root mount holders: ${holders}" 2530 else 2531 echo -n . 2532 fi 2533 if [ ${waited} -ge ${root_hold_delay} ]; then 2534 echo 2535 break 2536 fi 2537 sleep 1 2538 waited=$(($waited + 1)) 2539 done 2540} 2541 2542# Find scripts in local_startup directories that use the old syntax 2543# 2544find_local_scripts_old() { 2545 zlist='' 2546 slist='' 2547 for dir in ${local_startup}; do 2548 if [ -d "${dir}" ]; then 2549 for file in ${dir}/[0-9]*.sh; do 2550 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 2551 continue 2552 zlist="$zlist $file" 2553 done 2554 for file in ${dir}/[!0-9]*.sh; do 2555 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 2556 continue 2557 slist="$slist $file" 2558 done 2559 fi 2560 done 2561} 2562 2563find_local_scripts_new() { 2564 local_rc='' 2565 for dir in ${local_startup}; do 2566 if [ -d "${dir}" ]; then 2567 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 2568 case "$file" in 2569 *.sample|*.pkgsave) ;; 2570 *) if [ -x "$file" ]; then 2571 local_rc="${local_rc} ${file}" 2572 fi 2573 ;; 2574 esac 2575 done 2576 fi 2577 done 2578} 2579 2580find_system_scripts() { 2581 system_rc='' 2582 for file in /etc/rc.d/*; do 2583 case "${file##*/}" in 2584 *.pkgsave) ;; 2585 *) if [ -x "$file" ]; then 2586 system_rc="${system_rc} ${file}" 2587 fi 2588 ;; 2589 esac 2590 done 2591} 2592 2593# check_required_{before|after} command 2594# Check for things required by the command before and after its precmd, 2595# respectively. The two separate functions are needed because some 2596# conditions should prevent precmd from being run while other things 2597# depend on precmd having already been run. 2598# 2599check_required_before() 2600{ 2601 local _f 2602 2603 case "$1" in 2604 start) 2605 for _f in $required_vars; do 2606 if ! checkyesno $_f; then 2607 warn "\$${_f} is not enabled." 2608 if [ -z "$rc_force" ]; then 2609 return 1 2610 fi 2611 fi 2612 done 2613 2614 for _f in $required_dirs; do 2615 if [ ! -d "${_f}/." ]; then 2616 warn "${_f} is not a directory." 2617 if [ -z "$rc_force" ]; then 2618 return 1 2619 fi 2620 fi 2621 done 2622 2623 for _f in $required_files; do 2624 if [ ! -r "${_f}" ]; then 2625 warn "${_f} is not readable." 2626 if [ -z "$rc_force" ]; then 2627 return 1 2628 fi 2629 fi 2630 done 2631 ;; 2632 esac 2633 2634 return 0 2635} 2636 2637check_required_after() 2638{ 2639 local _f _args 2640 2641 case "$1" in 2642 start) 2643 for _f in $required_modules; do 2644 case "${_f}" in 2645 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 2646 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 2647 *) _args="${_f}" ;; 2648 esac 2649 if ! load_kld ${_args}; then 2650 if [ -z "$rc_force" ]; then 2651 return 1 2652 fi 2653 fi 2654 done 2655 ;; 2656 esac 2657 2658 return 0 2659} 2660 2661# check_jail mib 2662# Return true if security.jail.$mib exists and set to 1. 2663 2664check_jail() 2665{ 2666 local _mib _v 2667 2668 _mib=$1 2669 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 2670 case $_v in 2671 1) return 0;; 2672 esac 2673 fi 2674 return 1 2675} 2676 2677# check_kern_features mib 2678# Return existence of kern.features.* sysctl MIB as true or 2679# false. The result will be cached in $_rc_cache_kern_features_ 2680# namespace. "0" means the kern.features.X exists. 2681 2682check_kern_features() 2683{ 2684 local _v 2685 2686 [ -n "$1" ] || return 1; 2687 eval _v=\$_rc_cache_kern_features_$1 2688 [ -n "$_v" ] && return "$_v"; 2689 2690 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 2691 eval _rc_cache_kern_features_$1=0 2692 return 0 2693 else 2694 eval _rc_cache_kern_features_$1=1 2695 return 1 2696 fi 2697} 2698 2699# check_namevarlist var 2700# Return "0" if ${name}_var is reserved in rc.subr. 2701 2702_rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend setup" 2703check_namevarlist() 2704{ 2705 local _v 2706 2707 for _v in $_rc_namevarlist; do 2708 case $1 in 2709 $_v) return 0 ;; 2710 esac 2711 done 2712 2713 return 1 2714} 2715 2716# _echoonce var msg mode 2717# mode=0: Echo $msg if ${$var} is empty. 2718# After doing echo, a string is set to ${$var}. 2719# 2720# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2721# 2722_echoonce() 2723{ 2724 local _var _msg _mode 2725 eval _var=\$$1 2726 _msg=$2 2727 _mode=$3 2728 2729 case $_mode in 2730 1) [ -n "$_var" ] && echo "$_msg" ;; 2731 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2732 esac 2733} 2734 2735# If the loader env variable rc.debug is set, turn on debugging. rc.conf will 2736# still override this, but /etc/defaults/rc.conf can't unconditionally set this 2737# since it would undo what we've done here. 2738if kenv -q rc.debug > /dev/null ; then 2739 rc_debug=YES 2740fi 2741 2742boottrace_cmd=`command -v boottrace` 2743if [ -n "$boottrace_cmd" ] && [ "`${SYSCTL_N} -q kern.boottrace.enabled`" = "1" ]; then 2744 rc_boottrace=YES 2745fi 2746 2747SED=${SED:-$(Exists -x /usr/bin/sed /rescue/sed)} 2748 2749# Allow for local additions and overrides. 2750# Use vdot to ensure the file has not been tampered with. 2751vdot /etc/local.rc.subr 2752 2753# Avoid noise - when we do not have /usr mounted, 2754# and we cannot use safe_dot without sed. 2755if ! have basename; then 2756 basename() 2757 { 2758 local b=${1%$2} 2759 echo ${b##*/} 2760 } 2761 tty() 2762 { 2763 return 0 2764 } 2765 # we cannot use safe_dot without sed 2766 [ -z "$SED" ] && _SAFE_EVAL_SH=: 2767fi 2768# safe_eval.sh provides safe_dot - for untrusted files 2769$_SAFE_EVAL_SH vdot /libexec/safe_eval.sh 2770$_DEBUG_SH vdot /libexec/debug.sh 2771 2772# Ensure we can still operate if debug.sh and 2773# safe_eval.sh are not found. 2774if ! have DebugOn; then 2775 DebugOn() { return 0; } 2776 DebugOff() { 2777 local _rc=0 2778 while : 2779 do 2780 case "$1" in 2781 -[eo]) shift;; # ignore it 2782 rc=*) eval "_$1"; shift;; 2783 *) break;; 2784 esac 2785 done 2786 return $_rc 2787 } 2788fi 2789if ! have safe_dot; then 2790 safe_dot() { dot "$@"; } 2791fi 2792