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