rc.subr revision 1.78
1# $NetBSD: rc.subr,v 1.78 2009/09/11 18:17:04 apb Exp $ 2# 3# Copyright (c) 1997-2004 The NetBSD Foundation, Inc. 4# All rights reserved. 5# 6# This code is derived from software contributed to The NetBSD Foundation 7# by Luke Mewburn. 8# 9# Redistribution and use in source and binary forms, with or without 10# modification, are permitted provided that the following conditions 11# are met: 12# 1. Redistributions of source code must retain the above copyright 13# notice, this list of conditions and the following disclaimer. 14# 2. Redistributions in binary form must reproduce the above copyright 15# notice, this list of conditions and the following disclaimer in the 16# documentation and/or other materials provided with the distribution. 17# 18# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28# POSSIBILITY OF SUCH DAMAGE. 29# 30# rc.subr 31# functions used by various rc scripts 32# 33 34: ${rcvar_manpage:='rc.conf(5)'} 35: ${RC_PID:=$$} ; export RC_PID 36nl=' 37' # a literal newline 38 39# 40# functions 41# --------- 42 43# 44# checkyesno var 45# Test $1 variable, and warn if not set to YES or NO. 46# Return 0 if it's "yes" (et al), nonzero otherwise. 47# 48checkyesno() 49{ 50 eval _value=\$${1} 51 case $_value in 52 53 # "yes", "true", "on", or "1" 54 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 55 return 0 56 ;; 57 58 # "no", "false", "off", or "0" 59 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 60 return 1 61 ;; 62 *) 63 warn "\$${1} is not set properly - see ${rcvar_manpage}." 64 return 1 65 ;; 66 esac 67} 68 69# 70# yesno_to_truefalse var 71# Convert the value of a variable from any of the values 72# understood by checkyesno() to "true" or "false". 73# 74yesno_to_truefalse() 75{ 76 local var=$1 77 if checkyesno $var; then 78 eval $var=true 79 return 0 80 else 81 eval $var=false 82 return 1 83 fi 84} 85 86# 87# reverse_list list 88# print the list in reverse order 89# 90reverse_list() 91{ 92 _revlist= 93 for _revfile; do 94 _revlist="$_revfile $_revlist" 95 done 96 echo $_revlist 97} 98 99# 100# If booting directly to multiuser, send SIGTERM to 101# the parent (/etc/rc) to abort the boot. 102# Otherwise just exit. 103# 104stop_boot() 105{ 106 if [ "$autoboot" = yes ]; then 107 echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 108 kill -TERM ${RC_PID} 109 fi 110 exit 1 111} 112 113# 114# mount_critical_filesystems type 115# Go through the list of critical filesystems as provided in 116# the rc.conf(5) variable $critical_filesystems_${type}, checking 117# each one to see if it is mounted, and if it is not, mounting it. 118# 119mount_critical_filesystems() 120{ 121 eval _fslist=\$critical_filesystems_${1} 122 for _fs in $_fslist; do 123 mount | ( 124 _ismounted=false 125 while read what _on on _type type; do 126 if [ $on = $_fs ]; then 127 _ismounted=true 128 fi 129 done 130 if $_ismounted; then 131 : 132 else 133 mount $_fs >/dev/null 2>&1 134 fi 135 ) 136 done 137} 138 139# 140# check_pidfile pidfile procname [interpreter] 141# Parses the first line of pidfile for a PID, and ensures 142# that the process is running and matches procname. 143# Prints the matching PID upon success, nothing otherwise. 144# interpreter is optional; see _find_processes() for details. 145# 146check_pidfile() 147{ 148 _pidfile=$1 149 _procname=$2 150 _interpreter=$3 151 if [ -z "$_pidfile" -o -z "$_procname" ]; then 152 err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 153 fi 154 if [ ! -f $_pidfile ]; then 155 return 156 fi 157 read _pid _junk < $_pidfile 158 if [ -z "$_pid" ]; then 159 return 160 fi 161 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 162} 163 164# 165# check_process procname [interpreter] 166# Ensures that a process (or processes) named procname is running. 167# Prints a list of matching PIDs. 168# interpreter is optional; see _find_processes() for details. 169# 170check_process() 171{ 172 _procname=$1 173 _interpreter=$2 174 if [ -z "$_procname" ]; then 175 err 3 'USAGE: check_process procname [interpreter]' 176 fi 177 _find_processes $_procname ${_interpreter:-.} '-ax' 178} 179 180# 181# _find_processes procname interpreter psargs 182# Search for procname in the output of ps generated by psargs. 183# Prints the PIDs of any matching processes, space separated. 184# 185# If interpreter == ".", check the following variations of procname 186# against the first word of each command: 187# procname 188# `basename procname` 189# `basename procname` + ":" 190# "(" + `basename procname` + ")" 191# 192# If interpreter != ".", read the first line of procname, remove the 193# leading #!, normalise whitespace, append procname, and attempt to 194# match that against each command, either as is, or with extra words 195# at the end. As an alternative, to deal with interpreted daemons 196# using perl, the basename of the interpreter plus a colon is also 197# tried as the prefix to procname. 198# 199_find_processes() 200{ 201 if [ $# -ne 3 ]; then 202 err 3 'USAGE: _find_processes procname interpreter psargs' 203 fi 204 _procname=$1 205 _interpreter=$2 206 _psargs=$3 207 208 _pref= 209 _procnamebn=${_procname##*/} 210 if [ $_interpreter != "." ]; then # an interpreted script 211 read _interp < ${_chroot:-}/$_procname # read interpreter name 212 _interp=${_interp#\#!} # strip #! 213 set -- $_interp 214 if [ $_interpreter != $1 ]; then 215 warn "\$command_interpreter $_interpreter != $1" 216 fi 217 _interp="$* $_procname" # cleanup spaces, add _procname 218 _interpbn=${1##*/} 219 _fp_args='_argv' 220 _fp_match='case "$_argv" in 221 ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)' 222 else # a normal daemon 223 _fp_args='_arg0 _argv' 224 _fp_match='case "$_arg0" in 225 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 226 fi 227 228 _proccheck=' 229 ps -o "pid,command" '"$_psargs"' | 230 while read _npid '"$_fp_args"'; do 231 case "$_npid" in 232 PID) 233 continue ;; 234 esac ; '"$_fp_match"' 235 echo -n "$_pref$_npid" ; 236 _pref=" " 237 ;; 238 esac 239 done' 240 241#echo 1>&2 "proccheck is :$_proccheck:" 242 eval $_proccheck 243} 244 245# 246# wait_for_pids pid [pid ...] 247# spins until none of the pids exist 248# 249wait_for_pids() 250{ 251 _list="$@" 252 if [ -z "$_list" ]; then 253 return 254 fi 255 _prefix= 256 while true; do 257 _nlist=""; 258 for _j in $_list; do 259 if kill -0 $_j 2>/dev/null; then 260 _nlist="${_nlist}${_nlist:+ }$_j" 261 fi 262 done 263 if [ -z "$_nlist" ]; then 264 break 265 fi 266 _list=$_nlist 267 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 268 _prefix=", " 269 sleep 2 270 done 271 if [ -n "$_prefix" ]; then 272 echo "." 273 fi 274} 275 276# 277# run_rc_command argument 278# Search for argument in the list of supported commands, which is: 279# "start stop restart rcvar status poll ${extra_commands}" 280# If there's a match, run ${argument}_cmd or the default method 281# (see below). 282# 283# If argument has a given prefix, then change the operation as follows: 284# Prefix Operation 285# ------ --------- 286# fast Skip the pid check, and set rc_fast=yes 287# force Set ${rcvar} to YES, and set rc_force=yes 288# one Set ${rcvar} to YES 289# 290# The following globals are used: 291# 292# Name Needed Purpose 293# ---- ------ ------- 294# name y Name of script. 295# 296# command n Full path to command. 297# Not needed if ${rc_arg}_cmd is set for 298# each keyword. 299# 300# command_args n Optional args/shell directives for command. 301# 302# command_interpreter n If not empty, command is interpreted, so 303# call check_{pidfile,process}() appropriately. 304# 305# extra_commands n List of extra commands supported. 306# 307# pidfile n If set, use check_pidfile $pidfile $command, 308# otherwise use check_process $command. 309# In either case, only check if $command is set. 310# 311# procname n Process name to check for instead of $command. 312# 313# rcvar n This is checked with checkyesno to determine 314# if the action should be run. 315# 316# ${name}_chroot n Directory to chroot to before running ${command} 317# Requires /usr to be mounted. 318# 319# ${name}_chdir n Directory to cd to before running ${command} 320# (if not using ${name}_chroot). 321# 322# ${name}_flags n Arguments to call ${command} with. 323# NOTE: $flags from the parent environment 324# can be used to override this. 325# 326# ${name}_env n Additional environment variable settings 327# for running ${command} 328# 329# ${name}_nice n Nice level to run ${command} at. 330# 331# ${name}_user n User to run ${command} as, using su(1) if not 332# using ${name}_chroot. 333# Requires /usr to be mounted. 334# 335# ${name}_group n Group to run chrooted ${command} as. 336# Requires /usr to be mounted. 337# 338# ${name}_groups n Comma separated list of supplementary groups 339# to run the chrooted ${command} with. 340# Requires /usr to be mounted. 341# 342# ${rc_arg}_cmd n If set, use this as the method when invoked; 343# Otherwise, use default command (see below) 344# 345# ${rc_arg}_precmd n If set, run just before performing the 346# ${rc_arg}_cmd method in the default 347# operation (i.e, after checking for required 348# bits and process (non)existence). 349# If this completes with a non-zero exit code, 350# don't run ${rc_arg}_cmd. 351# 352# ${rc_arg}_postcmd n If set, run just after performing the 353# ${rc_arg}_cmd method, if that method 354# returned a zero exit code. 355# 356# required_dirs n If set, check for the existence of the given 357# directories before running the default 358# (re)start command. 359# 360# required_files n If set, check for the readability of the given 361# files before running the default (re)start 362# command. 363# 364# required_vars n If set, perform checkyesno on each of the 365# listed variables before running the default 366# (re)start command. 367# 368# Default behaviour for a given argument, if no override method is 369# provided: 370# 371# Argument Default behaviour 372# -------- ----------------- 373# start if !running && checkyesno ${rcvar} 374# ${command} 375# 376# stop if ${pidfile} 377# rc_pid=$(check_pidfile $pidfile $command) 378# else 379# rc_pid=$(check_process $command) 380# kill $sig_stop $rc_pid 381# wait_for_pids $rc_pid 382# ($sig_stop defaults to TERM.) 383# 384# reload Similar to stop, except use $sig_reload instead, 385# and doesn't wait_for_pids. 386# $sig_reload defaults to HUP. 387# 388# restart Run `stop' then `start'. 389# 390# status Show if ${command} is running, etc. 391# 392# poll Wait for ${command} to exit. 393# 394# rcvar Display what rc.conf variable is used (if any). 395# 396# Variables available to methods, and after run_rc_command() has 397# completed: 398# 399# Variable Purpose 400# -------- ------- 401# rc_arg Argument to command, after fast/force/one processing 402# performed 403# 404# rc_flags Flags to start the default command with. 405# Defaults to ${name}_flags, unless overridden 406# by $flags from the environment. 407# This variable may be changed by the precmd method. 408# 409# rc_pid PID of command (if appropriate) 410# 411# rc_fast Not empty if "fast" was provided (q.v.) 412# 413# rc_force Not empty if "force" was provided (q.v.) 414# 415# 416run_rc_command() 417{ 418 rc_arg=$1 419 if [ -z "$name" ]; then 420 err 3 'run_rc_command: $name is not set.' 421 fi 422 423 _rc_prefix= 424 case "$rc_arg" in 425 fast*) # "fast" prefix; don't check pid 426 rc_arg=${rc_arg#fast} 427 rc_fast=yes 428 ;; 429 force*) # "force" prefix; always run 430 rc_force=yes 431 _rc_prefix=force 432 rc_arg=${rc_arg#${_rc_prefix}} 433 if [ -n "${rcvar}" ]; then 434 eval ${rcvar}=YES 435 fi 436 ;; 437 one*) # "one" prefix; set ${rcvar}=yes 438 _rc_prefix=one 439 rc_arg=${rc_arg#${_rc_prefix}} 440 if [ -n "${rcvar}" ]; then 441 eval ${rcvar}=YES 442 fi 443 ;; 444 esac 445 446 _keywords="start stop restart rcvar" 447 if [ -n "$extra_commands" ]; then 448 _keywords="${_keywords} ${extra_commands}" 449 fi 450 rc_pid= 451 _pidcmd= 452 _procname=${procname:-${command}} 453 454 # setup pid check command if not fast 455 if [ -z "$rc_fast" -a -n "$_procname" ]; then 456 if [ -n "$pidfile" ]; then 457 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 458 else 459 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 460 fi 461 if [ -n "$_pidcmd" ]; then 462 _keywords="${_keywords} status poll" 463 fi 464 fi 465 466 if [ -z "$rc_arg" ]; then 467 rc_usage "$_keywords" 468 fi 469 470 if [ -n "$flags" ]; then # allow override from environment 471 rc_flags=$flags 472 else 473 eval rc_flags=\$${name}_flags 474 fi 475 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 476 _nice=\$${name}_nice _user=\$${name}_user \ 477 _group=\$${name}_group _groups=\$${name}_groups \ 478 _env=\"\$${name}_env\" 479 480 if [ -n "$_user" ]; then # unset $_user if running as that user 481 if [ "$_user" = "$(id -un)" ]; then 482 unset _user 483 fi 484 fi 485 486 # if ${rcvar} is set, and $1 is not 487 # "rcvar", then run 488 # checkyesno ${rcvar} 489 # and return if that failed or warn 490 # user and exit when interactive 491 # 492 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 493 if ! checkyesno ${rcvar}; then 494 # check whether interactive or not 495 if [ -n "$_run_rc_script" ]; then 496 return 0 497 fi 498 for _elem in $_keywords; do 499 if [ "$_elem" = "$rc_arg" ]; then 500 echo 1>&2 "\$${rcvar} is not enabled - see ${rcvar_manpage}." 501 echo 1>&2 "Use the following if you wish to perform the operation:" 502 echo 1>&2 " $0 one${rc_arg}" 503 exit 1 504 fi 505 done 506 echo 1>&2 "$0: unknown directive '$rc_arg'." 507 rc_usage "$_keywords" 508 fi 509 fi 510 511 eval $_pidcmd # determine the pid if necessary 512 513 for _elem in $_keywords; do 514 if [ "$_elem" != "$rc_arg" ]; then 515 continue 516 fi 517 518 # if there's a custom ${XXX_cmd}, 519 # run that instead of the default 520 # 521 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 522 _postcmd=\$${rc_arg}_postcmd 523 if [ -n "$_cmd" ]; then 524 # if the precmd failed and force 525 # isn't set, exit 526 # 527 if ! eval $_precmd && [ -z "$rc_force" ]; then 528 return 1 529 fi 530 531 if ! eval $_cmd && [ -z "$rc_force" ]; then 532 return 1 533 fi 534 eval $_postcmd 535 return 0 536 fi 537 538 case "$rc_arg" in # default operations... 539 540 status) 541 if [ -n "$rc_pid" ]; then 542 echo "${name} is running as pid $rc_pid." 543 else 544 echo "${name} is not running." 545 return 1 546 fi 547 ;; 548 549 start) 550 if [ -n "$rc_pid" ]; then 551 echo 1>&2 "${name} already running? (pid=$rc_pid)." 552 exit 1 553 fi 554 555 if [ ! -x ${_chroot}${command} ]; then 556 return 0 557 fi 558 559 # check for required variables, 560 # directories, and files 561 # 562 for _f in $required_vars; do 563 if ! checkyesno $_f; then 564 warn "\$${_f} is not enabled." 565 if [ -z "$rc_force" ]; then 566 return 1 567 fi 568 fi 569 done 570 for _f in $required_dirs; do 571 if [ ! -d "${_f}/." ]; then 572 warn "${_f} is not a directory." 573 if [ -z "$rc_force" ]; then 574 return 1 575 fi 576 fi 577 done 578 for _f in $required_files; do 579 if [ ! -r "${_f}" ]; then 580 warn "${_f} is not readable." 581 if [ -z "$rc_force" ]; then 582 return 1 583 fi 584 fi 585 done 586 587 # if the precmd failed and force 588 # isn't set, exit 589 # 590 if ! eval $_precmd && [ -z "$rc_force" ]; then 591 return 1 592 fi 593 594 # setup the command to run, and run it 595 # 596 echo "Starting ${name}." 597 if [ -n "$_chroot" ]; then 598 _doit="\ 599${_env:+env $_env }\ 600${_nice:+nice -n $_nice }\ 601chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 602$_chroot $command $rc_flags $command_args" 603 else 604 _doit="\ 605${_chdir:+cd $_chdir; }\ 606${_env:+env $_env }\ 607${_nice:+nice -n $_nice }\ 608$command $rc_flags $command_args" 609 if [ -n "$_user" ]; then 610 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 611 fi 612 fi 613 614 # if the cmd failed and force 615 # isn't set, exit 616 # 617 if ! eval $_doit && [ -z "$rc_force" ]; then 618 return 1 619 fi 620 621 # finally, run postcmd 622 # 623 eval $_postcmd 624 ;; 625 626 stop) 627 if [ -z "$rc_pid" ]; then 628 if [ -n "$pidfile" ]; then 629 echo 1>&2 \ 630 "${name} not running? (check $pidfile)." 631 else 632 echo 1>&2 "${name} not running?" 633 fi 634 exit 1 635 fi 636 637 # if the precmd failed and force 638 # isn't set, exit 639 # 640 if ! eval $_precmd && [ -z "$rc_force" ]; then 641 return 1 642 fi 643 644 # send the signal to stop 645 # 646 echo "Stopping ${name}." 647 _doit="kill -${sig_stop:-TERM} $rc_pid" 648 if [ -n "$_user" ]; then 649 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 650 fi 651 652 # if the stop cmd failed and force 653 # isn't set, exit 654 # 655 if ! eval $_doit && [ -z "$rc_force" ]; then 656 return 1 657 fi 658 659 # wait for the command to exit, 660 # and run postcmd. 661 wait_for_pids $rc_pid 662 eval $_postcmd 663 ;; 664 665 reload) 666 if [ -z "$rc_pid" ]; then 667 if [ -n "$pidfile" ]; then 668 echo 1>&2 \ 669 "${name} not running? (check $pidfile)." 670 else 671 echo 1>&2 "${name} not running?" 672 fi 673 exit 1 674 fi 675 echo "Reloading ${name} config files." 676 if ! eval $_precmd && [ -z "$rc_force" ]; then 677 return 1 678 fi 679 _doit="kill -${sig_reload:-HUP} $rc_pid" 680 if [ -n "$_user" ]; then 681 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 682 fi 683 if ! eval $_doit && [ -z "$rc_force" ]; then 684 return 1 685 fi 686 eval $_postcmd 687 ;; 688 689 restart) 690 if ! eval $_precmd && [ -z "$rc_force" ]; then 691 return 1 692 fi 693 # prevent restart being called more 694 # than once by any given script 695 # 696 if ${_rc_restart_done:-false}; then 697 return 0 698 fi 699 _rc_restart_done=true 700 701 ( $0 ${_rc_prefix}stop ) 702 $0 ${_rc_prefix}start 703 704 eval $_postcmd 705 ;; 706 707 poll) 708 if [ -n "$rc_pid" ]; then 709 wait_for_pids $rc_pid 710 fi 711 ;; 712 713 rcvar) 714 echo "# $name" 715 if [ -n "$rcvar" ]; then 716 if checkyesno ${rcvar}; then 717 echo "\$${rcvar}=YES" 718 else 719 echo "\$${rcvar}=NO" 720 fi 721 fi 722 ;; 723 724 *) 725 rc_usage "$_keywords" 726 ;; 727 728 esac 729 return 0 730 done 731 732 echo 1>&2 "$0: unknown directive '$rc_arg'." 733 rc_usage "$_keywords" 734 exit 1 735} 736 737# 738# run_rc_script file arg 739# Start the script `file' with `arg', and correctly handle the 740# return value from the script. If `file' ends with `.sh', it's 741# sourced into the current environment. If `file' appears to be 742# a backup or scratch file, ignore it. Otherwise if it's 743# executable run as a child process. 744# 745# If `file' contains "KEYWORD: interactive" and if we are 746# running inside /etc/rc with postprocessing (as signified by 747# _rc_postprocessor_fd being defined) then the script's stdout 748# and stderr are redirected to $_rc_original_stdout_fd and 749# $_rc_original_stderr_fd, so the output will be displayed on the 750# console but not intercepted by /etc/rc's postprocessor. 751# 752run_rc_script() 753{ 754 _file=$1 755 _arg=$2 756 if [ -z "$_file" -o -z "$_arg" ]; then 757 err 3 'USAGE: run_rc_script file arg' 758 fi 759 760 _run_rc_script=true 761 762 unset name command command_args command_interpreter \ 763 extra_commands pidfile procname \ 764 rcvar required_dirs required_files required_vars 765 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 766 767 _must_redirect=false 768 if ! [ -n "${_rc_postprocessor_fd}" ] \ 769 && _has_rcorder_keyword interactive $_file 770 then 771 _must_redirect=true 772 fi 773 774 case "$_file" in 775 *.sh) # run in current shell 776 if $_must_redirect; then 777 print_rc_metadata \ 778 "note:Output from ${_file} is not logged" 779 set $_arg ; no_rc_postprocess . $_file 780 else 781 set $_arg ; . $_file 782 fi 783 ;; 784 *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 785 warn "Ignoring scratch file $_file" 786 ;; 787 *) # run in subshell 788 if [ -x $_file ] && $_must_redirect; then 789 print_rc_metadata \ 790 "note:Output from ${_file} is not logged" 791 if [ -n "$rc_fast_and_loose" ]; then 792 set $_arg ; no_rc_postprocess . $_file 793 else 794 ( set $_arg ; no_rc_postprocess . $_file ) 795 fi 796 elif [ -x $_file ]; then 797 if [ -n "$rc_fast_and_loose" ]; then 798 set $_arg ; . $_file 799 else 800 ( set $_arg ; . $_file ) 801 fi 802 else 803 warn "Ignoring non-executable file $_file" 804 fi 805 ;; 806 esac 807} 808 809# 810# load_rc_config command 811# Source in the configuration file for a given command. 812# 813load_rc_config() 814{ 815 _command=$1 816 if [ -z "$_command" ]; then 817 err 3 'USAGE: load_rc_config command' 818 fi 819 820 if ${_rc_conf_loaded:-false}; then 821 : 822 else 823 . /etc/rc.conf 824 _rc_conf_loaded=true 825 fi 826 if [ -f /etc/rc.conf.d/"$_command" ]; then 827 . /etc/rc.conf.d/"$_command" 828 fi 829} 830 831# 832# load_rc_config_var cmd var 833# Read the rc.conf(5) var for cmd and set in the 834# current shell, using load_rc_config in a subshell to prevent 835# unwanted side effects from other variable assignments. 836# 837load_rc_config_var() 838{ 839 if [ $# -ne 2 ]; then 840 err 3 'USAGE: load_rc_config_var cmd var' 841 fi 842 eval $(eval '( 843 load_rc_config '$1' >/dev/null; 844 if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 845 echo '$2'=\'\''${'$2'}\'\''; 846 fi 847 )' ) 848} 849 850# 851# rc_usage commands 852# Print a usage string for $0, with `commands' being a list of 853# valid commands. 854# 855rc_usage() 856{ 857 echo -n 1>&2 "Usage: $0 [fast|force|one](" 858 859 _sep= 860 for _elem; do 861 echo -n 1>&2 "$_sep$_elem" 862 _sep="|" 863 done 864 echo 1>&2 ")" 865 exit 1 866} 867 868# 869# err exitval message 870# Display message to stderr and log to the syslog, and exit with exitval. 871# 872err() 873{ 874 exitval=$1 875 shift 876 877 if [ -x /usr/bin/logger ]; then 878 logger "$0: ERROR: $*" 879 fi 880 echo 1>&2 "$0: ERROR: $*" 881 exit $exitval 882} 883 884# 885# warn message 886# Display message to stderr and log to the syslog. 887# 888warn() 889{ 890 if [ -x /usr/bin/logger ]; then 891 logger "$0: WARNING: $*" 892 fi 893 echo 1>&2 "$0: WARNING: $*" 894} 895 896# 897# backup_file action file cur backup 898# Make a backup copy of `file' into `cur', and save the previous 899# version of `cur' as `backup' or use rcs for archiving. 900# 901# This routine checks the value of the backup_uses_rcs variable, 902# which can be either YES or NO. 903# 904# The `action' keyword can be one of the following: 905# 906# add `file' is now being backed up (and is possibly 907# being reentered into the backups system). `cur' 908# is created and RCS files, if necessary, are 909# created as well. 910# 911# update `file' has changed and needs to be backed up. 912# If `cur' exists, it is copied to to `back' or 913# checked into RCS (if the repository file is old), 914# and then `file' is copied to `cur'. Another RCS 915# check in done here if RCS is being used. 916# 917# remove `file' is no longer being tracked by the backups 918# system. If RCS is not being used, `cur' is moved 919# to `back', otherwise an empty file is checked in, 920# and then `cur' is removed. 921# 922# 923backup_file() 924{ 925 _action=$1 926 _file=$2 927 _cur=$3 928 _back=$4 929 930 if checkyesno backup_uses_rcs; then 931 _msg0="backup archive" 932 _msg1="update" 933 934 # ensure that history file is not locked 935 if [ -f $_cur,v ]; then 936 rcs -q -u -U -M $_cur 937 fi 938 939 # ensure after switching to rcs that the 940 # current backup is not lost 941 if [ -f $_cur ]; then 942 # no archive, or current newer than archive 943 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 944 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 945 rcs -q -kb -U $_cur 946 co -q -f -u $_cur 947 fi 948 fi 949 950 case $_action in 951 add|update) 952 cp -p $_file $_cur 953 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 954 rcs -q -kb -U $_cur 955 co -q -f -u $_cur 956 chown root:wheel $_cur $_cur,v 957 ;; 958 remove) 959 cp /dev/null $_cur 960 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 961 rcs -q -kb -U $_cur 962 chown root:wheel $_cur $_cur,v 963 rm $_cur 964 ;; 965 esac 966 else 967 case $_action in 968 add|update) 969 if [ -f $_cur ]; then 970 cp -p $_cur $_back 971 fi 972 cp -p $_file $_cur 973 chown root:wheel $_cur 974 ;; 975 remove) 976 mv -f $_cur $_back 977 ;; 978 esac 979 fi 980} 981 982# 983# handle_fsck_error fsck_exit_code 984# Take action depending on the return code from fsck. 985# 986handle_fsck_error() 987{ 988 case $1 in 989 0) # OK 990 return 991 ;; 992 2) # Needs re-run, still fs errors 993 echo "File system still has errors; re-run fsck manually!" 994 ;; 995 4) # Root modified 996 echo "Root filesystem was modified, rebooting ..." 997 reboot -n 998 echo "Reboot failed; help!" 999 ;; 1000 8) # Check failed 1001 echo "Automatic file system check failed; help!" 1002 ;; 1003 12) # Got signal 1004 echo "Boot interrupted." 1005 ;; 1006 *) 1007 echo "Unknown error $1; help!" 1008 ;; 1009 esac 1010 stop_boot 1011} 1012 1013# 1014# _has_rcorder_keyword word file 1015# Check whether a file contains a "# KEYWORD:" comment with a 1016# specified keyword in the style used by rcorder(8). 1017# 1018_has_rcorder_keyword() 1019{ 1020 local word="$1" 1021 local file="$2" 1022 local line 1023 1024 [ -r "$file" ] || return 1 1025 while read line; do 1026 case "${line} " in 1027 "# KEYWORD:"*[\ \ ]"${word}"[\ \ ]*) 1028 return 0 1029 ;; 1030 "#"*) 1031 continue 1032 ;; 1033 *[A-Za-z0-9]*) 1034 # give up at the first non-empty non-comment line 1035 return 1 1036 ;; 1037 esac 1038 done <"$file" 1039 return 1 1040} 1041 1042# 1043# print_rc_metadata string 1044# Print the specified string in such a way that the post-processor 1045# inside /etc/rc will treat it as meta-data. 1046# 1047# If we are not running inside /etc/rc, do nothing. 1048# 1049# For public use by any rc.d script, the string must begin with 1050# "note:", followed by arbitrary text. The intent is that the text 1051# will appear in a log file but not on the console. 1052# 1053# For private use within /etc/rc, the string must contain a 1054# keyword recognised by the rc_postprocess_metadata() function 1055# defined in /etc/rc, followed by a colon, followed by one or more 1056# colon-separated arguments associated with the keyword. 1057# 1058print_rc_metadata() 1059{ 1060 # _rc_postprocessor fd, if defined, is the fd to which we must 1061 # print, prefixing the output with $_rc_metadata_prefix. 1062 # 1063 if [ -n "$_rc_postprocessor_fd" ]; then 1064 printf "%s%s\n" "$rc_metadata_prefix" "$1" \ 1065 >&${_rc_postprocessor_fd} 1066 fi 1067} 1068 1069# 1070# print_rc_normal string 1071# Print the specified string in such way that it is treated as 1072# normal output, regardless of whether or not we are running 1073# inside /etc/rc with post-processing. 1074# 1075# Ths intent is that a script that is run via the 1076# no_rc_postprocess() function (so its output would ordinarily be 1077# invisible to the post-processor) can nevertheless arrange for 1078# the post-processor to see things printed with print_rc_normal(). 1079# 1080print_rc_normal() 1081{ 1082 # If _rc_postprocessor_fd is defined, then it is the fd 1083 # to shich we must print; otherwise print to stdout. 1084 # 1085 printf "%s\n" "$1" >&${_rc_postprocessor_fd:-1} 1086} 1087 1088# 1089# no_rc_postprocess cmd... 1090# Execute the specified command in such a way that its output 1091# bypasses the post-processor that handles the output from 1092# most commands that are run inside /etc/rc. If we are not 1093# inside /etc/rc, then just execute the command without special 1094# treatment. 1095# 1096# The intent is that interactive commands can be run via 1097# no_rc_postprocess(), and their output will apear immediately 1098# on the console instead of being hidden or delayed by the 1099# post-processor. An unfortunate consequence of the output 1100# bypassing the post-processor is that the output will not be 1101# logged. 1102# 1103no_rc_postprocess() 1104{ 1105 if [ -n "${_rc_postprocessor_fd}" ]; then 1106 "$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd} 1107 else 1108 "$@" 1109 fi 1110} 1111 1112# 1113# twiddle 1114# On each call, print a different one of "/", "-", "\\", "|", 1115# followed by a backspace. The most recently printed value is 1116# saved in $_twiddle_state. 1117# 1118# Output is to /dev/tty, so this function may be useful even inside 1119# a script whose output is redirected. 1120# 1121twiddle() 1122{ 1123 case "$_twiddle_state" in 1124 '/') _next='-' ;; 1125 '-') _next='\' ;; 1126 '\') _next='|' ;; 1127 *) _next='/' ;; 1128 esac 1129 printf "%s\b" "$_next" >/dev/tty 1130 _twiddle_state="$_next" 1131} 1132 1133_rc_subr_loaded=: 1134