rc.subr revision 245250
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 2# $FreeBSD: head/etc/rc.subr 245250 2013-01-10 11:08:22Z smh $ 3# 4# Copyright (c) 1997-2004 The NetBSD Foundation, Inc. 5# All rights reserved. 6# 7# This code is derived from software contributed to The NetBSD Foundation 8# by Luke Mewburn. 9# 10# Redistribution and use in source and binary forms, with or without 11# modification, are permitted provided that the following conditions 12# are met: 13# 1. Redistributions of source code must retain the above copyright 14# notice, this list of conditions and the following disclaimer. 15# 2. Redistributions in binary form must reproduce the above copyright 16# notice, this list of conditions and the following disclaimer in the 17# documentation and/or other materials provided with the distribution. 18# 19# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29# POSSIBILITY OF SUCH DAMAGE. 30# 31# rc.subr 32# functions used by various rc scripts 33# 34 35: ${RC_PID:=$$}; export RC_PID 36 37# 38# Operating System dependent/independent variables 39# 40 41if [ -z "${_rc_subr_loaded}" ]; then 42 43_rc_subr_loaded="YES" 44 45SYSCTL="/sbin/sysctl" 46SYSCTL_N="${SYSCTL} -n" 47SYSCTL_W="${SYSCTL}" 48ID="/usr/bin/id" 49IDCMD="if [ -x $ID ]; then $ID -un; fi" 50PS="/bin/ps -ww" 51JID=`$PS -p $$ -o jid=` 52 53# 54# functions 55# --------- 56 57# set_rcvar_obsolete oldvar [newvar] [msg] 58# Define obsolete variable. 59# Global variable $rcvars_obsolete is used. 60# 61set_rcvar_obsolete() 62{ 63 local _var 64 _var=$1 65 debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 66 67 rcvars_obsolete="${rcvars_obsolete# } $1" 68 eval ${1}_newvar=\"$2\" 69 shift 2 70 eval ${_var}_obsolete_msg=\"$*\" 71} 72 73# 74# force_depend script [rcvar] 75# Force a service to start. Intended for use by services 76# to resolve dependency issues. 77# $1 - filename of script, in /etc/rc.d, to run 78# $2 - name of the script's rcvar (minus the _enable) 79# 80force_depend() 81{ 82 local _depend _dep_rcvar 83 84 _depend="$1" 85 _dep_rcvar="${2:-$1}_enable" 86 87 [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 88 checkyesno $_dep_rcvar && return 0 89 90 /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 91 92 info "${name} depends on ${_depend}, which will be forced to start." 93 if ! /etc/rc.d/${_depend} forcestart; then 94 warn "Unable to force ${_depend}. It may already be running." 95 return 1 96 fi 97} 98 99# 100# checkyesno var 101# Test $1 variable, and warn if not set to YES or NO. 102# Return 0 if it's "yes" (et al), nonzero otherwise. 103# 104checkyesno() 105{ 106 eval _value=\$${1} 107 debug "checkyesno: $1 is set to $_value." 108 case $_value in 109 110 # "yes", "true", "on", or "1" 111 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 112 return 0 113 ;; 114 115 # "no", "false", "off", or "0" 116 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 117 return 1 118 ;; 119 *) 120 warn "\$${1} is not set properly - see rc.conf(5)." 121 return 1 122 ;; 123 esac 124} 125 126# 127# reverse_list list 128# print the list in reverse order 129# 130reverse_list() 131{ 132 _revlist= 133 for _revfile; do 134 _revlist="$_revfile $_revlist" 135 done 136 echo $_revlist 137} 138 139# stop_boot always 140# If booting directly to multiuser or $always is enabled, 141# send SIGTERM to the parent (/etc/rc) to abort the boot. 142# Otherwise just exit. 143# 144stop_boot() 145{ 146 local always 147 148 case $1 in 149 # "yes", "true", "on", or "1" 150 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 151 always=true 152 ;; 153 *) 154 always=false 155 ;; 156 esac 157 if [ "$autoboot" = yes -o "$always" = true ]; then 158 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 159 kill -TERM ${RC_PID} 160 fi 161 exit 1 162} 163 164# 165# mount_critical_filesystems type 166# Go through the list of critical filesystems as provided in 167# the rc.conf(5) variable $critical_filesystems_${type}, checking 168# each one to see if it is mounted, and if it is not, mounting it. 169# 170mount_critical_filesystems() 171{ 172 eval _fslist=\$critical_filesystems_${1} 173 for _fs in $_fslist; do 174 mount | ( 175 _ismounted=false 176 while read what _on on _type type; do 177 if [ $on = $_fs ]; then 178 _ismounted=true 179 fi 180 done 181 if $_ismounted; then 182 : 183 else 184 mount $_fs >/dev/null 2>&1 185 fi 186 ) 187 done 188} 189 190# 191# check_pidfile pidfile procname [interpreter] 192# Parses the first line of pidfile for a PID, and ensures 193# that the process is running and matches procname. 194# Prints the matching PID upon success, nothing otherwise. 195# interpreter is optional; see _find_processes() for details. 196# 197check_pidfile() 198{ 199 _pidfile=$1 200 _procname=$2 201 _interpreter=$3 202 if [ -z "$_pidfile" -o -z "$_procname" ]; then 203 err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 204 fi 205 if [ ! -f $_pidfile ]; then 206 debug "pid file ($_pidfile): not readable." 207 return 208 fi 209 read _pid _junk < $_pidfile 210 if [ -z "$_pid" ]; then 211 debug "pid file ($_pidfile): no pid in file." 212 return 213 fi 214 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 215} 216 217# 218# check_process procname [interpreter] 219# Ensures that a process (or processes) named procname is running. 220# Prints a list of matching PIDs. 221# interpreter is optional; see _find_processes() for details. 222# 223check_process() 224{ 225 _procname=$1 226 _interpreter=$2 227 if [ -z "$_procname" ]; then 228 err 3 'USAGE: check_process procname [interpreter]' 229 fi 230 _find_processes $_procname ${_interpreter:-.} '-ax' 231} 232 233# 234# _find_processes procname interpreter psargs 235# Search for procname in the output of ps generated by psargs. 236# Prints the PIDs of any matching processes, space separated. 237# 238# If interpreter == ".", check the following variations of procname 239# against the first word of each command: 240# procname 241# `basename procname` 242# `basename procname` + ":" 243# "(" + `basename procname` + ")" 244# "[" + `basename procname` + "]" 245# 246# If interpreter != ".", read the first line of procname, remove the 247# leading #!, normalise whitespace, append procname, and attempt to 248# match that against each command, either as is, or with extra words 249# at the end. As an alternative, to deal with interpreted daemons 250# using perl, the basename of the interpreter plus a colon is also 251# tried as the prefix to procname. 252# 253_find_processes() 254{ 255 if [ $# -ne 3 ]; then 256 err 3 'USAGE: _find_processes procname interpreter psargs' 257 fi 258 _procname=$1 259 _interpreter=$2 260 _psargs=$3 261 262 _pref= 263 if [ $_interpreter != "." ]; then # an interpreted script 264 _script="${_chroot}${_chroot:+/}$_procname" 265 if [ -r "$_script" ]; then 266 read _interp < $_script # read interpreter name 267 case "$_interp" in 268 \#!*) 269 _interp=${_interp#\#!} # strip #! 270 set -- $_interp 271 case $1 in 272 */bin/env) 273 shift # drop env to get real name 274 ;; 275 esac 276 if [ $_interpreter != $1 ]; then 277 warn "\$command_interpreter $_interpreter != $1" 278 fi 279 ;; 280 *) 281 warn "no shebang line in $_script" 282 set -- $_interpreter 283 ;; 284 esac 285 else 286 warn "cannot read shebang line from $_script" 287 set -- $_interpreter 288 fi 289 _interp="$* $_procname" # cleanup spaces, add _procname 290 _interpbn=${1##*/} 291 _fp_args='_argv' 292 _fp_match='case "$_argv" in 293 ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' 294 else # a normal daemon 295 _procnamebn=${_procname##*/} 296 _fp_args='_arg0 _argv' 297 _fp_match='case "$_arg0" in 298 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 299 fi 300 301 _proccheck="\ 302 $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 303 while read _npid _jid '"$_fp_args"'; do 304 '"$_fp_match"' 305 if [ "$JID" -eq "$_jid" ]; 306 then echo -n "$_pref$_npid"; 307 _pref=" "; 308 fi 309 ;; 310 esac 311 done' 312 313# debug "in _find_processes: proccheck is ($_proccheck)." 314 eval $_proccheck 315} 316 317# 318# wait_for_pids pid [pid ...] 319# spins until none of the pids exist 320# 321wait_for_pids() 322{ 323 local _list _prefix _nlist _j 324 325 _list="$@" 326 if [ -z "$_list" ]; then 327 return 328 fi 329 _prefix= 330 while true; do 331 _nlist=""; 332 for _j in $_list; do 333 if kill -0 $_j 2>/dev/null; then 334 _nlist="${_nlist}${_nlist:+ }$_j" 335 [ -n "$_prefix" ] && sleep 1 336 fi 337 done 338 if [ -z "$_nlist" ]; then 339 break 340 fi 341 _list=$_nlist 342 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 343 _prefix=", " 344 pwait $_list 2>/dev/null 345 done 346 if [ -n "$_prefix" ]; then 347 echo "." 348 fi 349} 350 351# 352# get_pidfile_from_conf string file 353# 354# Takes a string to search for in the specified file. 355# Ignores lines with traditional comment characters. 356# 357# Example: 358# 359# if get_pidfile_from_conf string file; then 360# pidfile="$_pidfile_from_conf" 361# else 362# pidfile='appropriate default' 363# fi 364# 365get_pidfile_from_conf() 366{ 367 if [ -z "$1" -o -z "$2" ]; then 368 err 3 "USAGE: get_pidfile_from_conf string file ($name)" 369 fi 370 371 local string file line 372 373 string="$1" ; file="$2" 374 375 if [ ! -s "$file" ]; then 376 err 3 "get_pidfile_from_conf: $file does not exist ($name)" 377 fi 378 379 while read line; do 380 case "$line" in 381 *[#\;]*${string}*) continue ;; 382 *${string}*) break ;; 383 esac 384 done < $file 385 386 if [ -n "$line" ]; then 387 line=${line#*/} 388 _pidfile_from_conf="/${line%%[\"\;]*}" 389 else 390 return 1 391 fi 392} 393 394# 395# check_startmsgs 396# If rc_quiet is set (usually as a result of using faststart at 397# boot time) check if rc_startmsgs is enabled. 398# 399check_startmsgs() 400{ 401 if [ -n "$rc_quiet" ]; then 402 checkyesno rc_startmsgs 403 else 404 return 0 405 fi 406} 407 408# 409# run_rc_command argument 410# Search for argument in the list of supported commands, which is: 411# "start stop restart rcvar status poll ${extra_commands}" 412# If there's a match, run ${argument}_cmd or the default method 413# (see below). 414# 415# If argument has a given prefix, then change the operation as follows: 416# Prefix Operation 417# ------ --------- 418# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 419# force Set ${rcvar} to YES, and set rc_force=yes 420# one Set ${rcvar} to YES 421# quiet Don't output some diagnostics, and set rc_quiet=yes 422# 423# The following globals are used: 424# 425# Name Needed Purpose 426# ---- ------ ------- 427# name y Name of script. 428# 429# command n Full path to command. 430# Not needed if ${rc_arg}_cmd is set for 431# each keyword. 432# 433# command_args n Optional args/shell directives for command. 434# 435# command_interpreter n If not empty, command is interpreted, so 436# call check_{pidfile,process}() appropriately. 437# 438# desc n Description of script. 439# 440# extra_commands n List of extra commands supported. 441# 442# pidfile n If set, use check_pidfile $pidfile $command, 443# otherwise use check_process $command. 444# In either case, only check if $command is set. 445# 446# procname n Process name to check for instead of $command. 447# 448# rcvar n This is checked with checkyesno to determine 449# if the action should be run. 450# 451# ${name}_program n Full path to command. 452# Meant to be used in /etc/rc.conf to override 453# ${command}. 454# 455# ${name}_chroot n Directory to chroot to before running ${command} 456# Requires /usr to be mounted. 457# 458# ${name}_chdir n Directory to cd to before running ${command} 459# (if not using ${name}_chroot). 460# 461# ${name}_flags n Arguments to call ${command} with. 462# NOTE: $flags from the parent environment 463# can be used to override this. 464# 465# ${name}_fib n Routing table number to run ${command} with. 466# 467# ${name}_nice n Nice level to run ${command} at. 468# 469# ${name}_user n User to run ${command} as, using su(1) if not 470# using ${name}_chroot. 471# Requires /usr to be mounted. 472# 473# ${name}_group n Group to run chrooted ${command} as. 474# Requires /usr to be mounted. 475# 476# ${name}_groups n Comma separated list of supplementary groups 477# to run the chrooted ${command} with. 478# Requires /usr to be mounted. 479# 480# ${rc_arg}_cmd n If set, use this as the method when invoked; 481# Otherwise, use default command (see below) 482# 483# ${rc_arg}_precmd n If set, run just before performing the 484# ${rc_arg}_cmd method in the default 485# operation (i.e, after checking for required 486# bits and process (non)existence). 487# If this completes with a non-zero exit code, 488# don't run ${rc_arg}_cmd. 489# 490# ${rc_arg}_postcmd n If set, run just after performing the 491# ${rc_arg}_cmd method, if that method 492# returned a zero exit code. 493# 494# required_dirs n If set, check for the existence of the given 495# directories before running a (re)start command. 496# 497# required_files n If set, check for the readability of the given 498# files before running a (re)start command. 499# 500# required_modules n If set, ensure the given kernel modules are 501# loaded before running a (re)start command. 502# The check and possible loads are actually 503# done after start_precmd so that the modules 504# aren't loaded in vain, should the precmd 505# return a non-zero status to indicate a error. 506# If a word in the list looks like "foo:bar", 507# "foo" is the KLD file name and "bar" is the 508# module name. If a word looks like "foo~bar", 509# "foo" is the KLD file name and "bar" is a 510# egrep(1) pattern matching the module name. 511# Otherwise the module name is assumed to be 512# the same as the KLD file name, which is most 513# common. See load_kld(). 514# 515# required_vars n If set, perform checkyesno on each of the 516# listed variables before running the default 517# (re)start command. 518# 519# Default behaviour for a given argument, if no override method is 520# provided: 521# 522# Argument Default behaviour 523# -------- ----------------- 524# start if !running && checkyesno ${rcvar} 525# ${command} 526# 527# stop if ${pidfile} 528# rc_pid=$(check_pidfile $pidfile $command) 529# else 530# rc_pid=$(check_process $command) 531# kill $sig_stop $rc_pid 532# wait_for_pids $rc_pid 533# ($sig_stop defaults to TERM.) 534# 535# reload Similar to stop, except use $sig_reload instead, 536# and doesn't wait_for_pids. 537# $sig_reload defaults to HUP. 538# Note that `reload' isn't provided by default, 539# it should be enabled via $extra_commands. 540# 541# restart Run `stop' then `start'. 542# 543# status Show if ${command} is running, etc. 544# 545# poll Wait for ${command} to exit. 546# 547# rcvar Display what rc.conf variable is used (if any). 548# 549# Variables available to methods, and after run_rc_command() has 550# completed: 551# 552# Variable Purpose 553# -------- ------- 554# rc_arg Argument to command, after fast/force/one processing 555# performed 556# 557# rc_flags Flags to start the default command with. 558# Defaults to ${name}_flags, unless overridden 559# by $flags from the environment. 560# This variable may be changed by the precmd method. 561# 562# rc_pid PID of command (if appropriate) 563# 564# rc_fast Not empty if "fast" was provided (q.v.) 565# 566# rc_force Not empty if "force" was provided (q.v.) 567# 568# rc_quiet Not empty if "quiet" was provided 569# 570# 571run_rc_command() 572{ 573 _return=0 574 rc_arg=$1 575 if [ -z "$name" ]; then 576 err 3 'run_rc_command: $name is not set.' 577 fi 578 579 # Don't repeat the first argument when passing additional command- 580 # line arguments to the command subroutines. 581 # 582 shift 1 583 rc_extra_args="$*" 584 585 _rc_prefix= 586 case "$rc_arg" in 587 fast*) # "fast" prefix; don't check pid 588 rc_arg=${rc_arg#fast} 589 rc_fast=yes 590 rc_quiet=yes 591 ;; 592 force*) # "force" prefix; always run 593 rc_force=yes 594 _rc_prefix=force 595 rc_arg=${rc_arg#${_rc_prefix}} 596 if [ -n "${rcvar}" ]; then 597 eval ${rcvar}=YES 598 fi 599 ;; 600 one*) # "one" prefix; set ${rcvar}=yes 601 _rc_prefix=one 602 rc_arg=${rc_arg#${_rc_prefix}} 603 if [ -n "${rcvar}" ]; then 604 eval ${rcvar}=YES 605 fi 606 ;; 607 quiet*) # "quiet" prefix; omit some messages 608 _rc_prefix=quiet 609 rc_arg=${rc_arg#${_rc_prefix}} 610 rc_quiet=yes 611 ;; 612 esac 613 614 eval _override_command=\$${name}_program 615 command=${_override_command:-$command} 616 617 _keywords="start stop restart rcvar $extra_commands" 618 rc_pid= 619 _pidcmd= 620 _procname=${procname:-${command}} 621 622 # setup pid check command 623 if [ -n "$_procname" ]; then 624 if [ -n "$pidfile" ]; then 625 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 626 else 627 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 628 fi 629 if [ -n "$_pidcmd" ]; then 630 _keywords="${_keywords} status poll" 631 fi 632 fi 633 634 if [ -z "$rc_arg" ]; then 635 rc_usage $_keywords 636 fi 637 638 if [ -n "$flags" ]; then # allow override from environment 639 rc_flags=$flags 640 else 641 eval rc_flags=\$${name}_flags 642 fi 643 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 644 _nice=\$${name}_nice _user=\$${name}_user \ 645 _group=\$${name}_group _groups=\$${name}_groups \ 646 _fib=\$${name}_fib 647 648 if [ -n "$_user" ]; then # unset $_user if running as that user 649 if [ "$_user" = "$(eval $IDCMD)" ]; then 650 unset _user 651 fi 652 fi 653 654 [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 655 656 for _elem in $_keywords; do 657 if [ "$_elem" != "$rc_arg" ]; then 658 continue 659 fi 660 # if ${rcvar} is set, $1 is not "rcvar" 661 # and ${rc_pid} is not set, then run 662 # checkyesno ${rcvar} 663 # and return if that failed 664 # 665 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 666 [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 667 if ! checkyesno ${rcvar}; then 668 if [ -n "${rc_quiet}" ]; then 669 return 0 670 fi 671 echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 672 echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 673 echo "instead of '${rc_arg}'." 674 return 0 675 fi 676 fi 677 678 # if there's a custom ${XXX_cmd}, 679 # run that instead of the default 680 # 681 eval _cmd=\$${rc_arg}_cmd \ 682 _precmd=\$${rc_arg}_precmd \ 683 _postcmd=\$${rc_arg}_postcmd 684 685 if [ -n "$_cmd" ]; then 686 _run_rc_precmd || return 1 687 _run_rc_doit "$_cmd $rc_extra_args" || return 1 688 _run_rc_postcmd 689 return $_return 690 fi 691 692 case "$rc_arg" in # default operations... 693 694 status) 695 _run_rc_precmd || return 1 696 if [ -n "$rc_pid" ]; then 697 echo "${name} is running as pid $rc_pid." 698 else 699 echo "${name} is not running." 700 return 1 701 fi 702 _run_rc_postcmd 703 ;; 704 705 start) 706 if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 707 if [ -z "$rc_quiet" ]; then 708 echo 1>&2 "${name} already running? " \ 709 "(pid=$rc_pid)." 710 fi 711 return 1 712 fi 713 714 if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 715 warn "run_rc_command: cannot run $command" 716 return 1 717 fi 718 719 if ! _run_rc_precmd; then 720 warn "failed precmd routine for ${name}" 721 return 1 722 fi 723 724 # setup the full command to run 725 # 726 check_startmsgs && echo "Starting ${name}." 727 if [ -n "$_chroot" ]; then 728 _doit="\ 729${_nice:+nice -n $_nice }\ 730${_fib:+setfib -F $_fib }\ 731chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 732$_chroot $command $rc_flags $command_args" 733 else 734 _doit="\ 735${_chdir:+cd $_chdir && }\ 736${_fib:+setfib -F $_fib }\ 737$command $rc_flags $command_args" 738 if [ -n "$_user" ]; then 739 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 740 fi 741 if [ -n "$_nice" ]; then 742 if [ -z "$_user" ]; then 743 _doit="sh -c \"$_doit\"" 744 fi 745 _doit="nice -n $_nice $_doit" 746 fi 747 fi 748 749 # run the full command 750 # 751 if ! _run_rc_doit "$_doit"; then 752 warn "failed to start ${name}" 753 return 1 754 fi 755 756 # finally, run postcmd 757 # 758 _run_rc_postcmd 759 ;; 760 761 stop) 762 if [ -z "$rc_pid" ]; then 763 [ -n "$rc_fast" ] && return 0 764 _run_rc_notrunning 765 return 1 766 fi 767 768 _run_rc_precmd || return 1 769 770 # send the signal to stop 771 # 772 echo "Stopping ${name}." 773 _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 774 _run_rc_doit "$_doit" || return 1 775 776 # wait for the command to exit, 777 # and run postcmd. 778 wait_for_pids $rc_pid 779 780 _run_rc_postcmd 781 ;; 782 783 reload) 784 if [ -z "$rc_pid" ]; then 785 _run_rc_notrunning 786 return 1 787 fi 788 789 _run_rc_precmd || return 1 790 791 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 792 _run_rc_doit "$_doit" || return 1 793 794 _run_rc_postcmd 795 ;; 796 797 restart) 798 # prevent restart being called more 799 # than once by any given script 800 # 801 if ${_rc_restart_done:-false}; then 802 return 0 803 fi 804 _rc_restart_done=true 805 806 _run_rc_precmd || return 1 807 808 # run those in a subshell to keep global variables 809 ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 810 ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 811 _return=$? 812 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 813 814 _run_rc_postcmd 815 ;; 816 817 poll) 818 _run_rc_precmd || return 1 819 if [ -n "$rc_pid" ]; then 820 wait_for_pids $rc_pid 821 fi 822 _run_rc_postcmd 823 ;; 824 825 rcvar) 826 echo -n "# $name" 827 if [ -n "$desc" ]; then 828 echo " : $desc" 829 else 830 echo "" 831 fi 832 echo "#" 833 # Get unique vars in $rcvar 834 for _v in $rcvar; do 835 case $v in 836 $_v\ *|\ *$_v|*\ $_v\ *) ;; 837 *) v="${v# } $_v" ;; 838 esac 839 done 840 841 # Display variables. 842 for _v in $v; do 843 if [ -z "$_v" ]; then 844 continue 845 fi 846 847 eval _desc=\$${_v}_desc 848 eval _defval=\$${_v}_defval 849 _h="-" 850 851 eval echo \"$_v=\\\"\$$_v\\\"\" 852 # decode multiple lines of _desc 853 while [ -n "$_desc" ]; do 854 case $_desc in 855 *^^*) 856 echo "# $_h ${_desc%%^^*}" 857 _desc=${_desc#*^^} 858 _h=" " 859 ;; 860 *) 861 echo "# $_h ${_desc}" 862 break 863 ;; 864 esac 865 done 866 echo "# (default: \"$_defval\")" 867 done 868 echo "" 869 ;; 870 871 *) 872 rc_usage $_keywords 873 ;; 874 875 esac 876 return $_return 877 done 878 879 echo 1>&2 "$0: unknown directive '$rc_arg'." 880 rc_usage $_keywords 881 # not reached 882} 883 884# 885# Helper functions for run_rc_command: common code. 886# They use such global variables besides the exported rc_* ones: 887# 888# name R/W 889# ------------------ 890# _precmd R 891# _postcmd R 892# _return W 893# 894_run_rc_precmd() 895{ 896 check_required_before "$rc_arg" || return 1 897 898 if [ -n "$_precmd" ]; then 899 debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 900 eval "$_precmd $rc_extra_args" 901 _return=$? 902 903 # If precmd failed and force isn't set, request exit. 904 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 905 return 1 906 fi 907 fi 908 909 check_required_after "$rc_arg" || return 1 910 911 return 0 912} 913 914_run_rc_postcmd() 915{ 916 if [ -n "$_postcmd" ]; then 917 debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 918 eval "$_postcmd $rc_extra_args" 919 _return=$? 920 fi 921 return 0 922} 923 924_run_rc_doit() 925{ 926 debug "run_rc_command: doit: $*" 927 eval "$@" 928 _return=$? 929 930 # If command failed and force isn't set, request exit. 931 if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 932 return 1 933 fi 934 935 return 0 936} 937 938_run_rc_notrunning() 939{ 940 local _pidmsg 941 942 if [ -n "$pidfile" ]; then 943 _pidmsg=" (check $pidfile)." 944 else 945 _pidmsg= 946 fi 947 echo 1>&2 "${name} not running?${_pidmsg}" 948} 949 950_run_rc_killcmd() 951{ 952 local _cmd 953 954 _cmd="kill -$1 $rc_pid" 955 if [ -n "$_user" ]; then 956 _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 957 fi 958 echo "$_cmd" 959} 960 961# 962# run_rc_script file arg 963# Start the script `file' with `arg', and correctly handle the 964# return value from the script. 965# If `file' ends with `.sh', it's sourced into the current environment 966# when $rc_fast_and_loose is set, otherwise it is run as a child process. 967# If `file' appears to be a backup or scratch file, ignore it. 968# Otherwise if it is executable run as a child process. 969# 970run_rc_script() 971{ 972 _file=$1 973 _arg=$2 974 if [ -z "$_file" -o -z "$_arg" ]; then 975 err 3 'USAGE: run_rc_script file arg' 976 fi 977 978 unset name command command_args command_interpreter \ 979 extra_commands pidfile procname \ 980 rcvar rcvars_obsolete required_dirs required_files \ 981 required_vars 982 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 983 984 case "$_file" in 985 /etc/rc.d/*.sh) # no longer allowed in the base 986 warn "Ignoring old-style startup script $_file" 987 ;; 988 *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 989 warn "Ignoring scratch file $_file" 990 ;; 991 *) # run in subshell 992 if [ -x $_file ]; then 993 if [ -n "$rc_fast_and_loose" ]; then 994 set $_arg; . $_file 995 else 996 ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 997 trap "echo Script $_file interrupted >&2 ; exit 1" 2 998 trap "echo Script $_file running >&2" 29 999 set $_arg; . $_file ) 1000 fi 1001 fi 1002 ;; 1003 esac 1004} 1005 1006# 1007# load_rc_config name 1008# Source in the configuration file for a given name. 1009# 1010load_rc_config() 1011{ 1012 local _name _rcvar_val _var _defval _v _msg _new 1013 _name=$1 1014 if [ -z "$_name" ]; then 1015 err 3 'USAGE: load_rc_config name' 1016 fi 1017 1018 if ${_rc_conf_loaded:-false}; then 1019 : 1020 else 1021 if [ -r /etc/defaults/rc.conf ]; then 1022 debug "Sourcing /etc/defaults/rc.conf" 1023 . /etc/defaults/rc.conf 1024 source_rc_confs 1025 elif [ -r /etc/rc.conf ]; then 1026 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 1027 . /etc/rc.conf 1028 fi 1029 _rc_conf_loaded=true 1030 fi 1031 if [ -f /etc/rc.conf.d/"$_name" ]; then 1032 debug "Sourcing /etc/rc.conf.d/${_name}" 1033 . /etc/rc.conf.d/"$_name" 1034 fi 1035 1036 # Set defaults if defined. 1037 for _var in $rcvar; do 1038 eval _defval=\$${_var}_defval 1039 if [ -n "$_defval" ]; then 1040 eval : \${$_var:=\$${_var}_defval} 1041 fi 1042 done 1043 1044 # check obsolete rc.conf variables 1045 for _var in $rcvars_obsolete; do 1046 eval _v=\$$_var 1047 eval _msg=\$${_var}_obsolete_msg 1048 eval _new=\$${_var}_newvar 1049 case $_v in 1050 "") 1051 ;; 1052 *) 1053 if [ -z "$_new" ]; then 1054 _msg="Ignored." 1055 else 1056 eval $_new=\"\$$_var\" 1057 if [ -z "$_msg" ]; then 1058 _msg="Use \$$_new instead." 1059 fi 1060 fi 1061 warn "\$$_var is obsolete. $_msg" 1062 ;; 1063 esac 1064 done 1065} 1066 1067# 1068# load_rc_config_var name var 1069# Read the rc.conf(5) var for name and set in the 1070# current shell, using load_rc_config in a subshell to prevent 1071# unwanted side effects from other variable assignments. 1072# 1073load_rc_config_var() 1074{ 1075 if [ $# -ne 2 ]; then 1076 err 3 'USAGE: load_rc_config_var name var' 1077 fi 1078 eval $(eval '( 1079 load_rc_config '$1' >/dev/null; 1080 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1081 echo '$2'=\'\''${'$2'}\'\''; 1082 fi 1083 )' ) 1084} 1085 1086# 1087# rc_usage commands 1088# Print a usage string for $0, with `commands' being a list of 1089# valid commands. 1090# 1091rc_usage() 1092{ 1093 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 1094 1095 _sep= 1096 for _elem; do 1097 echo -n 1>&2 "$_sep$_elem" 1098 _sep="|" 1099 done 1100 echo 1>&2 ")" 1101 exit 1 1102} 1103 1104# 1105# err exitval message 1106# Display message to stderr and log to the syslog, and exit with exitval. 1107# 1108err() 1109{ 1110 exitval=$1 1111 shift 1112 1113 if [ -x /usr/bin/logger ]; then 1114 logger "$0: ERROR: $*" 1115 fi 1116 echo 1>&2 "$0: ERROR: $*" 1117 exit $exitval 1118} 1119 1120# 1121# warn message 1122# Display message to stderr and log to the syslog. 1123# 1124warn() 1125{ 1126 if [ -x /usr/bin/logger ]; then 1127 logger "$0: WARNING: $*" 1128 fi 1129 echo 1>&2 "$0: WARNING: $*" 1130} 1131 1132# 1133# info message 1134# Display informational message to stdout and log to syslog. 1135# 1136info() 1137{ 1138 case ${rc_info} in 1139 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1140 if [ -x /usr/bin/logger ]; then 1141 logger "$0: INFO: $*" 1142 fi 1143 echo "$0: INFO: $*" 1144 ;; 1145 esac 1146} 1147 1148# 1149# debug message 1150# If debugging is enabled in rc.conf output message to stderr. 1151# BEWARE that you don't call any subroutine that itself calls this 1152# function. 1153# 1154debug() 1155{ 1156 case ${rc_debug} in 1157 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1158 if [ -x /usr/bin/logger ]; then 1159 logger "$0: DEBUG: $*" 1160 fi 1161 echo 1>&2 "$0: DEBUG: $*" 1162 ;; 1163 esac 1164} 1165 1166# 1167# backup_file action file cur backup 1168# Make a backup copy of `file' into `cur', and save the previous 1169# version of `cur' as `backup' or use rcs for archiving. 1170# 1171# This routine checks the value of the backup_uses_rcs variable, 1172# which can be either YES or NO. 1173# 1174# The `action' keyword can be one of the following: 1175# 1176# add `file' is now being backed up (and is possibly 1177# being reentered into the backups system). `cur' 1178# is created and RCS files, if necessary, are 1179# created as well. 1180# 1181# update `file' has changed and needs to be backed up. 1182# If `cur' exists, it is copied to to `back' or 1183# checked into RCS (if the repository file is old), 1184# and then `file' is copied to `cur'. Another RCS 1185# check in done here if RCS is being used. 1186# 1187# remove `file' is no longer being tracked by the backups 1188# system. If RCS is not being used, `cur' is moved 1189# to `back', otherwise an empty file is checked in, 1190# and then `cur' is removed. 1191# 1192# 1193backup_file() 1194{ 1195 _action=$1 1196 _file=$2 1197 _cur=$3 1198 _back=$4 1199 1200 if checkyesno backup_uses_rcs; then 1201 _msg0="backup archive" 1202 _msg1="update" 1203 1204 # ensure that history file is not locked 1205 if [ -f $_cur,v ]; then 1206 rcs -q -u -U -M $_cur 1207 fi 1208 1209 # ensure after switching to rcs that the 1210 # current backup is not lost 1211 if [ -f $_cur ]; then 1212 # no archive, or current newer than archive 1213 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 1214 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1215 rcs -q -kb -U $_cur 1216 co -q -f -u $_cur 1217 fi 1218 fi 1219 1220 case $_action in 1221 add|update) 1222 cp -p $_file $_cur 1223 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1224 rcs -q -kb -U $_cur 1225 co -q -f -u $_cur 1226 chown root:wheel $_cur $_cur,v 1227 ;; 1228 remove) 1229 cp /dev/null $_cur 1230 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1231 rcs -q -kb -U $_cur 1232 chown root:wheel $_cur $_cur,v 1233 rm $_cur 1234 ;; 1235 esac 1236 else 1237 case $_action in 1238 add|update) 1239 if [ -f $_cur ]; then 1240 cp -p $_cur $_back 1241 fi 1242 cp -p $_file $_cur 1243 chown root:wheel $_cur 1244 ;; 1245 remove) 1246 mv -f $_cur $_back 1247 ;; 1248 esac 1249 fi 1250} 1251 1252# make_symlink src link 1253# Make a symbolic link 'link' to src from basedir. If the 1254# directory in which link is to be created does not exist 1255# a warning will be displayed and an error will be returned. 1256# Returns 0 on success, 1 otherwise. 1257# 1258make_symlink() 1259{ 1260 local src link linkdir _me 1261 src="$1" 1262 link="$2" 1263 linkdir="`dirname $link`" 1264 _me="make_symlink()" 1265 1266 if [ -z "$src" -o -z "$link" ]; then 1267 warn "$_me: requires two arguments." 1268 return 1 1269 fi 1270 if [ ! -d "$linkdir" ]; then 1271 warn "$_me: the directory $linkdir does not exist." 1272 return 1 1273 fi 1274 if ! ln -sf $src $link; then 1275 warn "$_me: unable to make a symbolic link from $link to $src" 1276 return 1 1277 fi 1278 return 0 1279} 1280 1281# devfs_rulesets_from_file file 1282# Reads a set of devfs commands from file, and creates 1283# the specified rulesets with their rules. Returns non-zero 1284# if there was an error. 1285# 1286devfs_rulesets_from_file() 1287{ 1288 local file _err _me 1289 file="$1" 1290 _me="devfs_rulesets_from_file" 1291 _err=0 1292 1293 if [ -z "$file" ]; then 1294 warn "$_me: you must specify a file" 1295 return 1 1296 fi 1297 if [ ! -e "$file" ]; then 1298 debug "$_me: no such file ($file)" 1299 return 0 1300 fi 1301 debug "reading rulesets from file ($file)" 1302 { while read line 1303 do 1304 case $line in 1305 \#*) 1306 continue 1307 ;; 1308 \[*\]*) 1309 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1310 if [ -z "$rulenum" ]; then 1311 warn "$_me: cannot extract rule number ($line)" 1312 _err=1 1313 break 1314 fi 1315 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1316 if [ -z "$rulename" ]; then 1317 warn "$_me: cannot extract rule name ($line)" 1318 _err=1 1319 break; 1320 fi 1321 eval $rulename=\$rulenum 1322 debug "found ruleset: $rulename=$rulenum" 1323 if ! /sbin/devfs rule -s $rulenum delset; then 1324 _err=1 1325 break 1326 fi 1327 ;; 1328 *) 1329 rulecmd="${line%%"\#*"}" 1330 # evaluate the command incase it includes 1331 # other rules 1332 if [ -n "$rulecmd" ]; then 1333 debug "adding rule ($rulecmd)" 1334 if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1335 then 1336 _err=1 1337 break 1338 fi 1339 fi 1340 ;; 1341 esac 1342 if [ $_err -ne 0 ]; then 1343 debug "error in $_me" 1344 break 1345 fi 1346 done } < $file 1347 return $_err 1348} 1349 1350# devfs_init_rulesets 1351# Initializes rulesets from configuration files. Returns 1352# non-zero if there was an error. 1353# 1354devfs_init_rulesets() 1355{ 1356 local file _me 1357 _me="devfs_init_rulesets" 1358 1359 # Go through this only once 1360 if [ -n "$devfs_rulesets_init" ]; then 1361 debug "$_me: devfs rulesets already initialized" 1362 return 1363 fi 1364 for file in $devfs_rulesets; do 1365 if ! devfs_rulesets_from_file $file; then 1366 warn "$_me: could not read rules from $file" 1367 return 1 1368 fi 1369 done 1370 devfs_rulesets_init=1 1371 debug "$_me: devfs rulesets initialized" 1372 return 0 1373} 1374 1375# devfs_set_ruleset ruleset [dir] 1376# Sets the default ruleset of dir to ruleset. The ruleset argument 1377# must be a ruleset name as specified in devfs.rules(5) file. 1378# Returns non-zero if it could not set it successfully. 1379# 1380devfs_set_ruleset() 1381{ 1382 local devdir rs _me 1383 [ -n "$1" ] && eval rs=\$$1 || rs= 1384 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1385 _me="devfs_set_ruleset" 1386 1387 if [ -z "$rs" ]; then 1388 warn "$_me: you must specify a ruleset number" 1389 return 1 1390 fi 1391 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1392 if ! /sbin/devfs $devdir ruleset $rs; then 1393 warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1394 return 1 1395 fi 1396 return 0 1397} 1398 1399# devfs_apply_ruleset ruleset [dir] 1400# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1401# The ruleset argument must be a ruleset name as specified 1402# in a devfs.rules(5) file. Returns 0 on success or non-zero 1403# if it could not apply the ruleset. 1404# 1405devfs_apply_ruleset() 1406{ 1407 local devdir rs _me 1408 [ -n "$1" ] && eval rs=\$$1 || rs= 1409 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1410 _me="devfs_apply_ruleset" 1411 1412 if [ -z "$rs" ]; then 1413 warn "$_me: you must specify a ruleset" 1414 return 1 1415 fi 1416 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1417 if ! /sbin/devfs $devdir rule -s $rs applyset; then 1418 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1419 return 1 1420 fi 1421 return 0 1422} 1423 1424# devfs_domount dir [ruleset] 1425# Mount devfs on dir. If ruleset is specified it is set 1426# on the mount-point. It must also be a ruleset name as specified 1427# in a devfs.rules(5) file. Returns 0 on success. 1428# 1429devfs_domount() 1430{ 1431 local devdir rs _me 1432 devdir="$1" 1433 [ -n "$2" ] && rs=$2 || rs= 1434 _me="devfs_domount()" 1435 1436 if [ -z "$devdir" ]; then 1437 warn "$_me: you must specify a mount-point" 1438 return 1 1439 fi 1440 debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1441 if ! mount -t devfs dev "$devdir"; then 1442 warn "$_me: Unable to mount devfs on $devdir" 1443 return 1 1444 fi 1445 if [ -n "$rs" ]; then 1446 devfs_init_rulesets 1447 devfs_set_ruleset $rs $devdir 1448 devfs -m $devdir rule applyset 1449 fi 1450 return 0 1451} 1452 1453# devfs_mount_jail dir [ruleset] 1454# Mounts a devfs file system appropriate for jails 1455# on the directory dir. If ruleset is specified, the ruleset 1456# it names will be used instead. If present, ruleset must 1457# be the name of a ruleset as defined in a devfs.rules(5) file. 1458# This function returns non-zero if an error occurs. 1459# 1460devfs_mount_jail() 1461{ 1462 local jdev rs _me 1463 jdev="$1" 1464 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1465 _me="devfs_mount_jail" 1466 1467 devfs_init_rulesets 1468 if ! devfs_domount "$jdev" $rs; then 1469 warn "$_me: devfs was not mounted on $jdev" 1470 return 1 1471 fi 1472 return 0 1473} 1474 1475# Provide a function for normalizing the mounting of memory 1476# filesystems. This should allow the rest of the code here to remain 1477# as close as possible between 5-current and 4-stable. 1478# $1 = size 1479# $2 = mount point 1480# $3 = (optional) extra mdmfs flags 1481mount_md() 1482{ 1483 if [ -n "$3" ]; then 1484 flags="$3" 1485 fi 1486 /sbin/mdmfs $flags -s $1 md $2 1487} 1488 1489# Code common to scripts that need to load a kernel module 1490# if it isn't in the kernel yet. Syntax: 1491# load_kld [-e regex] [-m module] file 1492# where -e or -m chooses the way to check if the module 1493# is already loaded: 1494# regex is egrep'd in the output from `kldstat -v', 1495# module is passed to `kldstat -m'. 1496# The default way is as though `-m file' were specified. 1497load_kld() 1498{ 1499 local _loaded _mod _opt _re 1500 1501 while getopts "e:m:" _opt; do 1502 case "$_opt" in 1503 e) _re="$OPTARG" ;; 1504 m) _mod="$OPTARG" ;; 1505 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1506 esac 1507 done 1508 shift $(($OPTIND - 1)) 1509 if [ $# -ne 1 ]; then 1510 err 3 'USAGE: load_kld [-e regex] [-m module] file' 1511 fi 1512 _mod=${_mod:-$1} 1513 _loaded=false 1514 if [ -n "$_re" ]; then 1515 if kldstat -v | egrep -q -e "$_re"; then 1516 _loaded=true 1517 fi 1518 else 1519 if kldstat -q -m "$_mod"; then 1520 _loaded=true 1521 fi 1522 fi 1523 if ! $_loaded; then 1524 if ! kldload "$1"; then 1525 warn "Unable to load kernel module $1" 1526 return 1 1527 else 1528 info "$1 kernel module loaded." 1529 fi 1530 else 1531 debug "load_kld: $1 kernel module already loaded." 1532 fi 1533 return 0 1534} 1535 1536# ltr str src dst 1537# Change every $src in $str to $dst. 1538# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1539# awk(1). 1540ltr() 1541{ 1542 local _str _src _dst _out _com 1543 _str=$1 1544 _src=$2 1545 _dst=$3 1546 _out="" 1547 1548 IFS=${_src} 1549 for _com in ${_str}; do 1550 if [ -z "${_out}" ]; then 1551 _out="${_com}" 1552 else 1553 _out="${_out}${_dst}${_com}" 1554 fi 1555 done 1556 echo "${_out}" 1557} 1558 1559# Creates a list of providers for GELI encryption. 1560geli_make_list() 1561{ 1562 local devices devices2 1563 local provider mountpoint type options rest 1564 1565 # Create list of GELI providers from fstab. 1566 while read provider mountpoint type options rest ; do 1567 case ":${options}" in 1568 :*noauto*) 1569 noauto=yes 1570 ;; 1571 *) 1572 noauto=no 1573 ;; 1574 esac 1575 1576 case ":${provider}" in 1577 :#*) 1578 continue 1579 ;; 1580 *.eli) 1581 # Skip swap devices. 1582 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1583 continue 1584 fi 1585 devices="${devices} ${provider}" 1586 ;; 1587 esac 1588 done < /etc/fstab 1589 1590 # Append providers from geli_devices. 1591 devices="${devices} ${geli_devices}" 1592 1593 for provider in ${devices}; do 1594 provider=${provider%.eli} 1595 provider=${provider#/dev/} 1596 devices2="${devices2} ${provider}" 1597 done 1598 1599 echo ${devices2} 1600} 1601 1602# Find scripts in local_startup directories that use the old syntax 1603# 1604find_local_scripts_old() { 1605 zlist='' 1606 slist='' 1607 for dir in ${local_startup}; do 1608 if [ -d "${dir}" ]; then 1609 for file in ${dir}/[0-9]*.sh; do 1610 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1611 continue 1612 zlist="$zlist $file" 1613 done 1614 for file in ${dir}/[!0-9]*.sh; do 1615 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1616 continue 1617 slist="$slist $file" 1618 done 1619 fi 1620 done 1621} 1622 1623find_local_scripts_new() { 1624 local_rc='' 1625 for dir in ${local_startup}; do 1626 if [ -d "${dir}" ]; then 1627 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1628 case "$file" in 1629 *.sample) ;; 1630 *) if [ -x "$file" ]; then 1631 local_rc="${local_rc} ${file}" 1632 fi 1633 ;; 1634 esac 1635 done 1636 fi 1637 done 1638} 1639 1640# check_required_{before|after} command 1641# Check for things required by the command before and after its precmd, 1642# respectively. The two separate functions are needed because some 1643# conditions should prevent precmd from being run while other things 1644# depend on precmd having already been run. 1645# 1646check_required_before() 1647{ 1648 local _f 1649 1650 case "$1" in 1651 start) 1652 for _f in $required_vars; do 1653 if ! checkyesno $_f; then 1654 warn "\$${_f} is not enabled." 1655 if [ -z "$rc_force" ]; then 1656 return 1 1657 fi 1658 fi 1659 done 1660 1661 for _f in $required_dirs; do 1662 if [ ! -d "${_f}/." ]; then 1663 warn "${_f} is not a directory." 1664 if [ -z "$rc_force" ]; then 1665 return 1 1666 fi 1667 fi 1668 done 1669 1670 for _f in $required_files; do 1671 if [ ! -r "${_f}" ]; then 1672 warn "${_f} is not readable." 1673 if [ -z "$rc_force" ]; then 1674 return 1 1675 fi 1676 fi 1677 done 1678 ;; 1679 esac 1680 1681 return 0 1682} 1683 1684check_required_after() 1685{ 1686 local _f _args 1687 1688 case "$1" in 1689 start) 1690 for _f in $required_modules; do 1691 case "${_f}" in 1692 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1693 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1694 *) _args="${_f}" ;; 1695 esac 1696 if ! load_kld ${_args}; then 1697 if [ -z "$rc_force" ]; then 1698 return 1 1699 fi 1700 fi 1701 done 1702 ;; 1703 esac 1704 1705 return 0 1706} 1707 1708# check_kern_features mib 1709# Return existence of kern.features.* sysctl MIB as true or 1710# false. The result will be cached in $_rc_cache_kern_features_ 1711# namespace. "0" means the kern.features.X exists. 1712 1713check_kern_features() 1714{ 1715 local _v 1716 1717 [ -n "$1" ] || return 1; 1718 eval _v=\$_rc_cache_kern_features_$1 1719 [ -n "$_v" ] && return "$_v"; 1720 1721 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 1722 eval _rc_cache_kern_features_$1=0 1723 return 0 1724 else 1725 eval _rc_cache_kern_features_$1=1 1726 return 1 1727 fi 1728} 1729 1730# check_namevarlist var 1731# Return "0" if ${name}_var is reserved in rc.subr. 1732 1733_rc_namevarlist="program chroot chdir flags fib nice user group groups" 1734check_namevarlist() 1735{ 1736 local _v 1737 1738 for _v in $_rc_namevarlist; do 1739 case $1 in 1740 $_v) return 0 ;; 1741 esac 1742 done 1743 1744 return 1 1745} 1746 1747# _echoonce var msg mode 1748# mode=0: Echo $msg if ${$var} is empty. 1749# After doing echo, a string is set to ${$var}. 1750# 1751# mode=1: Echo $msg if ${$var} is a string with non-zero length. 1752# 1753_echoonce() 1754{ 1755 local _var _msg _mode 1756 eval _var=\$$1 1757 _msg=$2 1758 _mode=$3 1759 1760 case $_mode in 1761 1) [ -n "$_var" ] && echo "$_msg" ;; 1762 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 1763 esac 1764} 1765 1766fi # [ -z "${_rc_subr_loaded}" ] 1767 1768_rc_subr_loaded=: 1769