rc.subr revision 272868
1# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 2# $FreeBSD: stable/10/etc/rc.subr 272868 2014-10-09 23:45:26Z hrs $ 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 _d 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 1293 for _d in /etc ${local_startup%*/rc.d}; do 1294 if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1295 debug "Sourcing ${_d}/rc.conf.d/$_name" 1296 . ${_d}/rc.conf.d/"$_name" 1297 elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1298 local _rc 1299 for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1300 if [ -f "$_rc" ] ; then 1301 debug "Sourcing $_rc" 1302 . "$_rc" 1303 fi 1304 done 1305 fi 1306 done 1307 1308 # Set defaults if defined. 1309 for _var in $rcvar; do 1310 eval _defval=\$${_var}_defval 1311 if [ -n "$_defval" ]; then 1312 eval : \${$_var:=\$${_var}_defval} 1313 fi 1314 done 1315 1316 # check obsolete rc.conf variables 1317 for _var in $rcvars_obsolete; do 1318 eval _v=\$$_var 1319 eval _msg=\$${_var}_obsolete_msg 1320 eval _new=\$${_var}_newvar 1321 case $_v in 1322 "") 1323 ;; 1324 *) 1325 if [ -z "$_new" ]; then 1326 _msg="Ignored." 1327 else 1328 eval $_new=\"\$$_var\" 1329 if [ -z "$_msg" ]; then 1330 _msg="Use \$$_new instead." 1331 fi 1332 fi 1333 warn "\$$_var is obsolete. $_msg" 1334 ;; 1335 esac 1336 done 1337} 1338 1339# 1340# load_rc_config_var name var 1341# Read the rc.conf(5) var for name and set in the 1342# current shell, using load_rc_config in a subshell to prevent 1343# unwanted side effects from other variable assignments. 1344# 1345load_rc_config_var() 1346{ 1347 if [ $# -ne 2 ]; then 1348 err 3 'USAGE: load_rc_config_var name var' 1349 fi 1350 eval $(eval '( 1351 load_rc_config '$1' >/dev/null; 1352 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1353 echo '$2'=\'\''${'$2'}\'\''; 1354 fi 1355 )' ) 1356} 1357 1358# 1359# rc_usage commands 1360# Print a usage string for $0, with `commands' being a list of 1361# valid commands. 1362# 1363rc_usage() 1364{ 1365 echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 1366 1367 _sep= 1368 for _elem; do 1369 echo -n 1>&2 "$_sep$_elem" 1370 _sep="|" 1371 done 1372 echo 1>&2 ")" 1373 exit 1 1374} 1375 1376# 1377# err exitval message 1378# Display message to stderr and log to the syslog, and exit with exitval. 1379# 1380err() 1381{ 1382 exitval=$1 1383 shift 1384 1385 if [ -x /usr/bin/logger ]; then 1386 logger "$0: ERROR: $*" 1387 fi 1388 echo 1>&2 "$0: ERROR: $*" 1389 exit $exitval 1390} 1391 1392# 1393# warn message 1394# Display message to stderr and log to the syslog. 1395# 1396warn() 1397{ 1398 if [ -x /usr/bin/logger ]; then 1399 logger "$0: WARNING: $*" 1400 fi 1401 echo 1>&2 "$0: WARNING: $*" 1402} 1403 1404# 1405# info message 1406# Display informational message to stdout and log to syslog. 1407# 1408info() 1409{ 1410 case ${rc_info} in 1411 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1412 if [ -x /usr/bin/logger ]; then 1413 logger "$0: INFO: $*" 1414 fi 1415 echo "$0: INFO: $*" 1416 ;; 1417 esac 1418} 1419 1420# 1421# debug message 1422# If debugging is enabled in rc.conf output message to stderr. 1423# BEWARE that you don't call any subroutine that itself calls this 1424# function. 1425# 1426debug() 1427{ 1428 case ${rc_debug} in 1429 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1430 if [ -x /usr/bin/logger ]; then 1431 logger "$0: DEBUG: $*" 1432 fi 1433 echo 1>&2 "$0: DEBUG: $*" 1434 ;; 1435 esac 1436} 1437 1438# 1439# backup_file action file cur backup 1440# Make a backup copy of `file' into `cur', and save the previous 1441# version of `cur' as `backup' or use rcs for archiving. 1442# 1443# This routine checks the value of the backup_uses_rcs variable, 1444# which can be either YES or NO. 1445# 1446# The `action' keyword can be one of the following: 1447# 1448# add `file' is now being backed up (and is possibly 1449# being reentered into the backups system). `cur' 1450# is created and RCS files, if necessary, are 1451# created as well. 1452# 1453# update `file' has changed and needs to be backed up. 1454# If `cur' exists, it is copied to to `back' or 1455# checked into RCS (if the repository file is old), 1456# and then `file' is copied to `cur'. Another RCS 1457# check in done here if RCS is being used. 1458# 1459# remove `file' is no longer being tracked by the backups 1460# system. If RCS is not being used, `cur' is moved 1461# to `back', otherwise an empty file is checked in, 1462# and then `cur' is removed. 1463# 1464# 1465backup_file() 1466{ 1467 _action=$1 1468 _file=$2 1469 _cur=$3 1470 _back=$4 1471 1472 if checkyesno backup_uses_rcs; then 1473 _msg0="backup archive" 1474 _msg1="update" 1475 1476 # ensure that history file is not locked 1477 if [ -f $_cur,v ]; then 1478 rcs -q -u -U -M $_cur 1479 fi 1480 1481 # ensure after switching to rcs that the 1482 # current backup is not lost 1483 if [ -f $_cur ]; then 1484 # no archive, or current newer than archive 1485 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 1486 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1487 rcs -q -kb -U $_cur 1488 co -q -f -u $_cur 1489 fi 1490 fi 1491 1492 case $_action in 1493 add|update) 1494 cp -p $_file $_cur 1495 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1496 rcs -q -kb -U $_cur 1497 co -q -f -u $_cur 1498 chown root:wheel $_cur $_cur,v 1499 ;; 1500 remove) 1501 cp /dev/null $_cur 1502 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1503 rcs -q -kb -U $_cur 1504 chown root:wheel $_cur $_cur,v 1505 rm $_cur 1506 ;; 1507 esac 1508 else 1509 case $_action in 1510 add|update) 1511 if [ -f $_cur ]; then 1512 cp -p $_cur $_back 1513 fi 1514 cp -p $_file $_cur 1515 chown root:wheel $_cur 1516 ;; 1517 remove) 1518 mv -f $_cur $_back 1519 ;; 1520 esac 1521 fi 1522} 1523 1524# make_symlink src link 1525# Make a symbolic link 'link' to src from basedir. If the 1526# directory in which link is to be created does not exist 1527# a warning will be displayed and an error will be returned. 1528# Returns 0 on success, 1 otherwise. 1529# 1530make_symlink() 1531{ 1532 local src link linkdir _me 1533 src="$1" 1534 link="$2" 1535 linkdir="`dirname $link`" 1536 _me="make_symlink()" 1537 1538 if [ -z "$src" -o -z "$link" ]; then 1539 warn "$_me: requires two arguments." 1540 return 1 1541 fi 1542 if [ ! -d "$linkdir" ]; then 1543 warn "$_me: the directory $linkdir does not exist." 1544 return 1 1545 fi 1546 if ! ln -sf $src $link; then 1547 warn "$_me: unable to make a symbolic link from $link to $src" 1548 return 1 1549 fi 1550 return 0 1551} 1552 1553# devfs_rulesets_from_file file 1554# Reads a set of devfs commands from file, and creates 1555# the specified rulesets with their rules. Returns non-zero 1556# if there was an error. 1557# 1558devfs_rulesets_from_file() 1559{ 1560 local file _err _me _opts 1561 file="$1" 1562 _me="devfs_rulesets_from_file" 1563 _err=0 1564 1565 if [ -z "$file" ]; then 1566 warn "$_me: you must specify a file" 1567 return 1 1568 fi 1569 if [ ! -e "$file" ]; then 1570 debug "$_me: no such file ($file)" 1571 return 0 1572 fi 1573 1574 # Disable globbing so that the rule patterns are not expanded 1575 # by accident with matching filesystem entries. 1576 _opts=$-; set -f 1577 1578 debug "reading rulesets from file ($file)" 1579 { while read line 1580 do 1581 case $line in 1582 \#*) 1583 continue 1584 ;; 1585 \[*\]*) 1586 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1587 if [ -z "$rulenum" ]; then 1588 warn "$_me: cannot extract rule number ($line)" 1589 _err=1 1590 break 1591 fi 1592 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1593 if [ -z "$rulename" ]; then 1594 warn "$_me: cannot extract rule name ($line)" 1595 _err=1 1596 break; 1597 fi 1598 eval $rulename=\$rulenum 1599 debug "found ruleset: $rulename=$rulenum" 1600 if ! /sbin/devfs rule -s $rulenum delset; then 1601 _err=1 1602 break 1603 fi 1604 ;; 1605 *) 1606 rulecmd="${line%%"\#*"}" 1607 # evaluate the command incase it includes 1608 # other rules 1609 if [ -n "$rulecmd" ]; then 1610 debug "adding rule ($rulecmd)" 1611 if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1612 then 1613 _err=1 1614 break 1615 fi 1616 fi 1617 ;; 1618 esac 1619 if [ $_err -ne 0 ]; then 1620 debug "error in $_me" 1621 break 1622 fi 1623 done } < $file 1624 case $_opts in *f*) ;; *) set +f ;; esac 1625 return $_err 1626} 1627 1628# devfs_init_rulesets 1629# Initializes rulesets from configuration files. Returns 1630# non-zero if there was an error. 1631# 1632devfs_init_rulesets() 1633{ 1634 local file _me 1635 _me="devfs_init_rulesets" 1636 1637 # Go through this only once 1638 if [ -n "$devfs_rulesets_init" ]; then 1639 debug "$_me: devfs rulesets already initialized" 1640 return 1641 fi 1642 for file in $devfs_rulesets; do 1643 if ! devfs_rulesets_from_file $file; then 1644 warn "$_me: could not read rules from $file" 1645 return 1 1646 fi 1647 done 1648 devfs_rulesets_init=1 1649 debug "$_me: devfs rulesets initialized" 1650 return 0 1651} 1652 1653# devfs_set_ruleset ruleset [dir] 1654# Sets the default ruleset of dir to ruleset. The ruleset argument 1655# must be a ruleset name as specified in devfs.rules(5) file. 1656# Returns non-zero if it could not set it successfully. 1657# 1658devfs_set_ruleset() 1659{ 1660 local devdir rs _me 1661 [ -n "$1" ] && eval rs=\$$1 || rs= 1662 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1663 _me="devfs_set_ruleset" 1664 1665 if [ -z "$rs" ]; then 1666 warn "$_me: you must specify a ruleset number" 1667 return 1 1668 fi 1669 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1670 if ! /sbin/devfs $devdir ruleset $rs; then 1671 warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1672 return 1 1673 fi 1674 return 0 1675} 1676 1677# devfs_apply_ruleset ruleset [dir] 1678# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1679# The ruleset argument must be a ruleset name as specified 1680# in a devfs.rules(5) file. Returns 0 on success or non-zero 1681# if it could not apply the ruleset. 1682# 1683devfs_apply_ruleset() 1684{ 1685 local devdir rs _me 1686 [ -n "$1" ] && eval rs=\$$1 || rs= 1687 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1688 _me="devfs_apply_ruleset" 1689 1690 if [ -z "$rs" ]; then 1691 warn "$_me: you must specify a ruleset" 1692 return 1 1693 fi 1694 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1695 if ! /sbin/devfs $devdir rule -s $rs applyset; then 1696 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1697 return 1 1698 fi 1699 return 0 1700} 1701 1702# devfs_domount dir [ruleset] 1703# Mount devfs on dir. If ruleset is specified it is set 1704# on the mount-point. It must also be a ruleset name as specified 1705# in a devfs.rules(5) file. Returns 0 on success. 1706# 1707devfs_domount() 1708{ 1709 local devdir rs _me 1710 devdir="$1" 1711 [ -n "$2" ] && rs=$2 || rs= 1712 _me="devfs_domount()" 1713 1714 if [ -z "$devdir" ]; then 1715 warn "$_me: you must specify a mount-point" 1716 return 1 1717 fi 1718 debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1719 if ! mount -t devfs dev "$devdir"; then 1720 warn "$_me: Unable to mount devfs on $devdir" 1721 return 1 1722 fi 1723 if [ -n "$rs" ]; then 1724 devfs_init_rulesets 1725 devfs_set_ruleset $rs $devdir 1726 devfs -m $devdir rule applyset 1727 fi 1728 return 0 1729} 1730 1731# Provide a function for normalizing the mounting of memory 1732# filesystems. This should allow the rest of the code here to remain 1733# as close as possible between 5-current and 4-stable. 1734# $1 = size 1735# $2 = mount point 1736# $3 = (optional) extra mdmfs flags 1737mount_md() 1738{ 1739 if [ -n "$3" ]; then 1740 flags="$3" 1741 fi 1742 /sbin/mdmfs $flags -s $1 md $2 1743} 1744 1745# Code common to scripts that need to load a kernel module 1746# if it isn't in the kernel yet. Syntax: 1747# load_kld [-e regex] [-m module] file 1748# where -e or -m chooses the way to check if the module 1749# is already loaded: 1750# regex is egrep'd in the output from `kldstat -v', 1751# module is passed to `kldstat -m'. 1752# The default way is as though `-m file' were specified. 1753load_kld() 1754{ 1755 local _loaded _mod _opt _re 1756 1757 while getopts "e:m:" _opt; do 1758 case "$_opt" in 1759 e) _re="$OPTARG" ;; 1760 m) _mod="$OPTARG" ;; 1761 *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1762 esac 1763 done 1764 shift $(($OPTIND - 1)) 1765 if [ $# -ne 1 ]; then 1766 err 3 'USAGE: load_kld [-e regex] [-m module] file' 1767 fi 1768 _mod=${_mod:-$1} 1769 _loaded=false 1770 if [ -n "$_re" ]; then 1771 if kldstat -v | egrep -q -e "$_re"; then 1772 _loaded=true 1773 fi 1774 else 1775 if kldstat -q -m "$_mod"; then 1776 _loaded=true 1777 fi 1778 fi 1779 if ! $_loaded; then 1780 if ! kldload "$1"; then 1781 warn "Unable to load kernel module $1" 1782 return 1 1783 else 1784 info "$1 kernel module loaded." 1785 fi 1786 else 1787 debug "load_kld: $1 kernel module already loaded." 1788 fi 1789 return 0 1790} 1791 1792# ltr str src dst [var] 1793# Change every $src in $str to $dst. 1794# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1795# awk(1). If var is non-NULL, set it to the result. 1796ltr() 1797{ 1798 local _str _src _dst _out _com _var 1799 _str="$1" 1800 _src="$2" 1801 _dst="$3" 1802 _var="$4" 1803 _out="" 1804 1805 local IFS="${_src}" 1806 for _com in ${_str}; do 1807 if [ -z "${_out}" ]; then 1808 _out="${_com}" 1809 else 1810 _out="${_out}${_dst}${_com}" 1811 fi 1812 done 1813 if [ -n "${_var}" ]; then 1814 setvar "${_var}" "${_out}" 1815 else 1816 echo "${_out}" 1817 fi 1818} 1819 1820# Creates a list of providers for GELI encryption. 1821geli_make_list() 1822{ 1823 local devices devices2 1824 local provider mountpoint type options rest 1825 1826 # Create list of GELI providers from fstab. 1827 while read provider mountpoint type options rest ; do 1828 case ":${options}" in 1829 :*noauto*) 1830 noauto=yes 1831 ;; 1832 *) 1833 noauto=no 1834 ;; 1835 esac 1836 1837 case ":${provider}" in 1838 :#*) 1839 continue 1840 ;; 1841 *.eli) 1842 # Skip swap devices. 1843 if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1844 continue 1845 fi 1846 devices="${devices} ${provider}" 1847 ;; 1848 esac 1849 done < /etc/fstab 1850 1851 # Append providers from geli_devices. 1852 devices="${devices} ${geli_devices}" 1853 1854 for provider in ${devices}; do 1855 provider=${provider%.eli} 1856 provider=${provider#/dev/} 1857 devices2="${devices2} ${provider}" 1858 done 1859 1860 echo ${devices2} 1861} 1862 1863# Find scripts in local_startup directories that use the old syntax 1864# 1865find_local_scripts_old() { 1866 zlist='' 1867 slist='' 1868 for dir in ${local_startup}; do 1869 if [ -d "${dir}" ]; then 1870 for file in ${dir}/[0-9]*.sh; do 1871 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1872 continue 1873 zlist="$zlist $file" 1874 done 1875 for file in ${dir}/[!0-9]*.sh; do 1876 grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1877 continue 1878 slist="$slist $file" 1879 done 1880 fi 1881 done 1882} 1883 1884find_local_scripts_new() { 1885 local_rc='' 1886 for dir in ${local_startup}; do 1887 if [ -d "${dir}" ]; then 1888 for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1889 case "$file" in 1890 *.sample) ;; 1891 *) if [ -x "$file" ]; then 1892 local_rc="${local_rc} ${file}" 1893 fi 1894 ;; 1895 esac 1896 done 1897 fi 1898 done 1899} 1900 1901# check_required_{before|after} command 1902# Check for things required by the command before and after its precmd, 1903# respectively. The two separate functions are needed because some 1904# conditions should prevent precmd from being run while other things 1905# depend on precmd having already been run. 1906# 1907check_required_before() 1908{ 1909 local _f 1910 1911 case "$1" in 1912 start) 1913 for _f in $required_vars; do 1914 if ! checkyesno $_f; then 1915 warn "\$${_f} is not enabled." 1916 if [ -z "$rc_force" ]; then 1917 return 1 1918 fi 1919 fi 1920 done 1921 1922 for _f in $required_dirs; do 1923 if [ ! -d "${_f}/." ]; then 1924 warn "${_f} is not a directory." 1925 if [ -z "$rc_force" ]; then 1926 return 1 1927 fi 1928 fi 1929 done 1930 1931 for _f in $required_files; do 1932 if [ ! -r "${_f}" ]; then 1933 warn "${_f} is not readable." 1934 if [ -z "$rc_force" ]; then 1935 return 1 1936 fi 1937 fi 1938 done 1939 ;; 1940 esac 1941 1942 return 0 1943} 1944 1945check_required_after() 1946{ 1947 local _f _args 1948 1949 case "$1" in 1950 start) 1951 for _f in $required_modules; do 1952 case "${_f}" in 1953 *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1954 *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1955 *) _args="${_f}" ;; 1956 esac 1957 if ! load_kld ${_args}; then 1958 if [ -z "$rc_force" ]; then 1959 return 1 1960 fi 1961 fi 1962 done 1963 ;; 1964 esac 1965 1966 return 0 1967} 1968 1969# check_jail mib 1970# Return true if security.jail.$mib exists and set to 1. 1971 1972check_jail() 1973{ 1974 local _mib _v 1975 1976 _mib=$1 1977 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 1978 case $_v in 1979 1) return 0;; 1980 esac 1981 fi 1982 return 1 1983} 1984 1985# check_kern_features mib 1986# Return existence of kern.features.* sysctl MIB as true or 1987# false. The result will be cached in $_rc_cache_kern_features_ 1988# namespace. "0" means the kern.features.X exists. 1989 1990check_kern_features() 1991{ 1992 local _v 1993 1994 [ -n "$1" ] || return 1; 1995 eval _v=\$_rc_cache_kern_features_$1 1996 [ -n "$_v" ] && return "$_v"; 1997 1998 if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 1999 eval _rc_cache_kern_features_$1=0 2000 return 0 2001 else 2002 eval _rc_cache_kern_features_$1=1 2003 return 1 2004 fi 2005} 2006 2007# check_namevarlist var 2008# Return "0" if ${name}_var is reserved in rc.subr. 2009 2010_rc_namevarlist="program chroot chdir flags fib nice user group groups" 2011check_namevarlist() 2012{ 2013 local _v 2014 2015 for _v in $_rc_namevarlist; do 2016 case $1 in 2017 $_v) return 0 ;; 2018 esac 2019 done 2020 2021 return 1 2022} 2023 2024# _echoonce var msg mode 2025# mode=0: Echo $msg if ${$var} is empty. 2026# After doing echo, a string is set to ${$var}. 2027# 2028# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2029# 2030_echoonce() 2031{ 2032 local _var _msg _mode 2033 eval _var=\$$1 2034 _msg=$2 2035 _mode=$3 2036 2037 case $_mode in 2038 1) [ -n "$_var" ] && echo "$_msg" ;; 2039 *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2040 esac 2041} 2042 2043fi # [ -z "${_rc_subr_loaded}" ] 2044 2045_rc_subr_loaded=: 2046