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