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