rc.subr revision 98186
1# $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $ 2# $FreeBSD: head/etc/rc.subr 98186 2002-06-13 22:19:42Z gordon $ 3# 4# Copyright (c) 1997-2002 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# 3. All advertising materials mentioning features or use of this software 19# must display the following acknowledgement: 20# This product includes software developed by the NetBSD 21# Foundation, Inc. and its contributors. 22# 4. Neither the name of The NetBSD Foundation nor the names of its 23# contributors may be used to endorse or promote products derived 24# from this software without specific prior written permission. 25# 26# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36# POSSIBILITY OF SUCH DAMAGE. 37# 38# rc.subr 39# functions used by various rc scripts 40# 41 42# 43# Operating System dependent/independent variables 44# 45 46SYSCTL="/sbin/sysctl" 47SYSCTL_N="${SYSCTL} -n" 48CMD_OSTYPE="${SYSCTL_N} kern.ostype" 49 50case `${CMD_OSTYPE}` in 51FreeBSD) 52 SYSCTL_W="${SYSCTL}" 53 ;; 54NetBSD) 55 SYSCTL_W="${SYSCTL} -w" 56 ;; 57esac 58 59# 60# functions 61# --------- 62 63# 64# set_rcvar base_var 65# Set the variable name enabling a specific service. 66# FreeBSD uses ${service}_enable, while NetBSD uses 67# just the name of the service. For example: 68# FreeBSD: sendmail_enable="YES" 69# NetBSD : sendmail="YES" 70# $1 - if $name is not the base to work of off, specify 71# a different one 72# 73set_rcvar() 74{ 75 if [ -z "$1" ]; then 76 base_var=${name} 77 else 78 base_var="$1" 79 fi 80 81 case `${CMD_OSTYPE}` in 82 FreeBSD) 83 echo ${base_var}_enable 84 ;; 85 NetBSD) 86 echo ${base_var} 87 ;; 88 *) 89 echo 'XXX' 90 ;; 91 esac 92} 93 94# 95# force_depend script 96# Force a service to start. Intended for use by services 97# to resolve dependency issues. It is assumed the caller 98# has check to make sure this call is necessary 99# $1 - filename of script, in /etc/rc.d, to run 100# 101force_depend() 102{ 103 _depend="$1" 104 105 info "${name} depends on ${_depend}, which will be forced to start." 106 if ! /etc/rc.d/${_depend} forcestart ; then 107 warn "Unable to force ${_depend}. It may already be running." 108 return 1 109 fi 110 return 0 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." 135 return 1 136 ;; 137 esac 138} 139 140# reverse_list list 141# print the list in reverse order 142# 143reverse_list() 144{ 145 _revlist= 146 for _revfile in $*; do 147 _revlist="$_revfile $_revlist" 148 done 149 echo $_revlist 150} 151 152# 153# mount_critical_filesystems type 154# Go through the list of critical filesystems as provided in 155# the rc.conf(5) variable $critical_filesystems_${type}, checking 156# each one to see if it is mounted, and if it is not, mounting it. 157# 158mount_critical_filesystems() 159{ 160 eval _fslist=\$critical_filesystems_${1} 161 for _fs in $_fslist; do 162 mount | ( 163 _ismounted=no 164 while read what _on on _type type; do 165 if [ $on = $_fs ]; then 166 _ismounted=yes 167 fi 168 done 169 if [ $_ismounted = no ]; then 170 mount $_fs >/dev/null 2>&1 171 fi 172 ) 173 done 174} 175 176# 177# check_pidfile pidfile procname [interpreter] 178# Parses the first line of pidfile for a PID, and ensures 179# that the process is running and matches procname. 180# Prints the matching PID upon success, nothing otherwise. 181# interpreter is optional; see _find_processes() for details. 182# 183check_pidfile() 184{ 185 _pidfile=$1 186 _procname=$2 187 _interpreter=$3 188 if [ -z "$_pidfile" -o -z "$_procname" ]; then 189 err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 190 fi 191 if [ ! -f $_pidfile ]; then 192 debug "pid file {$_pidfile): not readable." 193 return 194 fi 195 read _pid _junk < $_pidfile 196 if [ -z "$_pid" ]; then 197 debug "pid file {$_pidfile): no pid in file." 198 return 199 fi 200 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 201} 202 203# 204# check_process procname [interpreter] 205# Ensures that a process (or processes) named procname is running. 206# Prints a list of matching PIDs. 207# interpreter is optional; see _find_processes() for details. 208# 209check_process() 210{ 211 _procname=$1 212 _interpreter=$2 213 if [ -z "$_procname" ]; then 214 err 3 'USAGE: check_process procname [interpreter]' 215 fi 216 _find_processes $_procname ${_interpreter:-.} '-ax' 217} 218 219# 220# _find_processes procname interpreter psargs 221# Search for procname in the output of ps generated by psargs. 222# Prints the PIDs of any matching processes, space separated. 223# 224# If interpreter == ".", check the following variations of procname 225# against the first word of each command: 226# procname 227# `basename procname` 228# `basename procname` + ":" 229# "(" + `basename procname` + ")" 230# 231# If interpreter != ".", read the first line of procname, remove the 232# leading #!, normalise whitespace, append procname, and attempt to 233# match that against each command, either as is, or with extra words 234# at the end. 235# 236_find_processes() 237{ 238 if [ $# -ne 3 ]; then 239 err 3 'USAGE: _find_processes procname interpreter psargs' 240 fi 241 _procname=$1 242 _interpreter=$2 243 _psargs=$3 244 245 _pref= 246 if [ $_interpreter != "." ]; then # an interpreted script 247 read _interp < $_procname # read interpreter name 248 _interp=${_interp#\#!} # strip #! 249 set -- $_interp 250 if [ $_interpreter != $1 ]; then 251 warn "\$command_interpreter $_interpreter != $1" 252 fi 253 _interp="$* $_procname" # cleanup spaces, add _procname 254 _fp_args='_argv' 255 _fp_match='case "$_argv" in 256 ${_interp}|"${_interp} "*)' 257 else # a normal daemon 258 _procnamebn=${_procname##*/} 259 _fp_args='_arg0 _argv' 260 _fp_match='case "$_arg0" in 261 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 262 fi 263 264 _proccheck=' 265 ps -o "pid,command" '"$_psargs"' | 266 while read _npid '"$_fp_args"'; do 267 case "$_npid" in 268 PID) 269 continue ;; 270 esac ; '"$_fp_match"' 271 echo -n "$_pref$_npid" ; 272 _pref=" " 273 ;; 274 esac 275 done' 276 277 debug "in _find_processes: proccheck is ($_proccheck)." 278 eval $_proccheck 279} 280 281# 282# wait_for_pids pid [pid ...] 283# spins until none of the pids exist 284# 285wait_for_pids() 286{ 287 _list=$* 288 if [ -z "$_list" ]; then 289 return 290 fi 291 _prefix= 292 while true; do 293 _nlist=""; 294 for _j in $_list; do 295 if kill -0 $_j 2>/dev/null; then 296 _nlist="${_nlist}${_nlist:+ }$_j" 297 fi 298 done 299 if [ -z "$_nlist" ]; then 300 break 301 fi 302 _list=$_nlist 303 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 304 _prefix=", " 305 sleep 2 306 done 307 if [ -n "$_prefix" ]; then 308 echo "." 309 fi 310} 311 312# 313# run_rc_command argument 314# Search for argument in the list of supported commands, which is: 315# "start stop restart rcvar status poll ${extra_commands}" 316# If there's a match, run ${argument}_cmd or the default method 317# (see below). 318# 319# If argument has a given prefix, then change the operation as follows: 320# Prefix Operation 321# ------ --------- 322# fast Skip the pid check, and set rc_fast=yes 323# force Set ${rcvar} to YES, and set rc_force=yes 324# 325# The following globals are used: 326# 327# Name Needed Purpose 328# ---- ------ ------- 329# name y Name of script. 330# 331# command n Full path to command. 332# Not needed if ${rc_arg}_cmd is set for 333# each keyword. 334# 335# command_args n Optional args/shell directives for command. 336# 337# command_interpreter n If not empty, command is interpreted, so 338# call check_{pidfile,process}() appropriately. 339# 340# extra_commands n List of extra commands supported. 341# 342# pidfile n If set, use check_pidfile $pidfile $command, 343# otherwise use check_process $command. 344# In either case, only check if $command is set. 345# 346# procname n Process name to check for instead of $command. 347# 348# rcvar n This is checked with checkyesno to determine 349# if the action should be run. 350# 351# ${name}_chroot n Directory to chroot to before running ${command} 352# Requires /usr to be mounted. 353# 354# ${name}_chdir n Directory to cd to before running ${command} 355# (if not using ${name}_chroot). 356# 357# ${name}_flags n Arguments to call ${command} with. 358# NOTE: $flags from the parent environment 359# can be used to override this. 360# 361# ${name}_nice n Nice level to run ${command} at. 362# 363# ${name}_user n User to run ${command} as, using su(1) if not 364# using ${name}_chroot. 365# Requires /usr to be mounted. 366# 367# ${name}_group n Group to run chrooted ${command} as. 368# Requires /usr to be mounted. 369# 370# ${name}_groups n Comma separated list of supplementary groups 371# to run the chrooted ${command} with. 372# Requires /usr to be mounted. 373# 374# ${rc_arg}_cmd n If set, use this as the method when invoked; 375# Otherwise, use default command (see below) 376# 377# ${rc_arg}_precmd n If set, run just before performing the 378# ${rc_arg}_cmd method in the default 379# operation (i.e, after checking for required 380# bits and process (non)existence). 381# If this completes with a non-zero exit code, 382# don't run ${rc_arg}_cmd. 383# 384# ${rc_arg}_postcmd n If set, run just after performing the 385# ${rc_arg}_cmd method, if that method 386# returned a zero exit code. 387# 388# required_dirs n If set, check for the existence of the given 389# directories before running the default 390# (re)start command. 391# 392# required_files n If set, check for the readability of the given 393# files before running the default (re)start 394# command. 395# 396# required_vars n If set, perform checkyesno on each of the 397# listed variables before running the default 398# (re)start command. 399# 400# Default behaviour for a given argument, if no override method is 401# provided: 402# 403# Argument Default behaviour 404# -------- ----------------- 405# start if !running && checkyesno ${rcvar} 406# ${command} 407# 408# stop if ${pidfile} 409# rc_pid=$(check_pidfile $pidfile $command) 410# else 411# rc_pid=$(check_process $command) 412# kill $sig_stop $rc_pid 413# wait_for_pids $rc_pid 414# ($sig_stop defaults to TERM.) 415# 416# reload Similar to stop, except use $sig_reload instead, 417# and doesn't wait_for_pids. 418# $sig_reload defaults to HUP. 419# 420# restart Run `stop' then `start'. 421# 422# status Show if ${command} is running, etc. 423# 424# poll Wait for ${command} to exit. 425# 426# rcvar Display what rc.conf variable is used (if any). 427# 428# Variables available to methods, and after run_rc_command() has 429# completed: 430# 431# Variable Purpose 432# -------- ------- 433# rc_arg Argument to command, after fast/force processing 434# performed 435# 436# rc_flags Flags to start the default command with. 437# Defaults to ${name}_flags, unless overridden 438# by $flags from the environment. 439# This variable may be changed by the precmd method. 440# 441# rc_pid PID of command (if appropriate) 442# 443# rc_fast Not empty if "fast" was provided (q.v.) 444# 445# rc_force Not empty if "force" was provided (q.v.) 446# 447# 448run_rc_command() 449{ 450 rc_arg=$1 451 if [ -z "$name" ]; then 452 err 3 'run_rc_command: $name is not set.' 453 fi 454 455 case "$rc_arg" in 456 fast*) # "fast" prefix; don't check pid 457 rc_arg=${rc_arg#fast} 458 rc_fast=yes 459 ;; 460 force*) # "force prefix; always start 461 rc_arg=${rc_arg#force} 462 rc_force=yes 463 if [ -n "${rcvar}" ]; then 464 eval ${rcvar}=YES 465 fi 466 ;; 467 esac 468 469 eval _overide_command=\$${name}_program 470 if [ -n "$_overide_command" ]; then 471 command=$_overide_command 472 fi 473 474 _keywords="start stop restart rcvar $extra_commands" 475 rc_pid= 476 _pidcmd= 477 _procname=${procname:-${command}} 478 479 # setup pid check command if not fast 480 if [ -z "$rc_fast" -a -n "$_procname" ]; then 481 if [ -n "$pidfile" ]; then 482 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 483 else 484 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 485 fi 486 if [ -n "$_pidcmd" ]; then 487 _keywords="${_keywords} status poll" 488 fi 489 fi 490 491 if [ -z "$rc_arg" ]; then 492 rc_usage "$_keywords" 493 fi 494 495 if [ -n "$flags" ]; then # allow override from environment 496 rc_flags=$flags 497 else 498 eval rc_flags=\$${name}_flags 499 fi 500 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 501 _nice=\$${name}_nice _user=\$${name}_user \ 502 _group=\$${name}_group _groups=\$${name}_groups 503 504 if [ -n "$_user" ]; then # unset $_user if running as that user 505 if [ "$_user" = "$(id -un)" ]; then 506 unset _user 507 fi 508 fi 509 510 # if ${rcvar} is set, and $1 is not 511 # "rcvar", then run 512 # checkyesno ${rcvar} 513 # and return if that failed 514 # 515 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 516 if ! checkyesno ${rcvar}; then 517 return 0 518 fi 519 fi 520 521 eval $_pidcmd # determine the pid if necessary 522 523 for _elem in $_keywords; do 524 if [ "$_elem" != "$rc_arg" ]; then 525 continue 526 fi 527 528 # if there's a custom ${XXX_cmd}, 529 # run that instead of the default 530 # 531 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 532 _postcmd=\$${rc_arg}_postcmd 533 if [ -n "$_cmd" ]; then 534 debug "run_rc_command: using XXX_cmd functions." 535 # if the precmd failed and force 536 # isn't set, exit 537 # 538 if ! eval $_precmd && [ -z "$rc_force" ]; then 539 return 1 540 fi 541 542 if ! eval $_cmd && [ -z "$rc_force" ]; then 543 return 1 544 fi 545 eval $_postcmd 546 return 0 547 fi 548 549 case "$rc_arg" in # default operations... 550 551 status) 552 if [ -n "$rc_pid" ]; then 553 echo "${name} is running as pid $rc_pid." 554 else 555 echo "${name} is not running." 556 return 1 557 fi 558 ;; 559 560 start) 561 if [ -n "$rc_pid" ]; then 562 echo "${name} already running? (pid=$rc_pid)." 563 exit 1 564 fi 565 566 if [ ! -x $command ]; then 567 info "run_rc_command: cannot run ($command)." 568 return 0 569 fi 570 571 # check for required variables, 572 # directories, and files 573 # 574 for _f in $required_vars; do 575 if ! checkyesno $_f; then 576 warn "\$${_f} is not set." 577 if [ -z "$rc_force" ]; then 578 return 1 579 fi 580 fi 581 done 582 for _f in $required_dirs; do 583 if [ ! -d "${_f}/." ]; then 584 warn "${_f} is not a directory." 585 if [ -z "$rc_force" ]; then 586 return 1 587 fi 588 fi 589 done 590 for _f in $required_files; do 591 if [ ! -r "${_f}" ]; then 592 warn "${_f} is not readable." 593 if [ -z "$rc_force" ]; then 594 return 1 595 fi 596 fi 597 done 598 599 # if the precmd failed and force 600 # isn't set, exit 601 # 602 if ! eval $_precmd && [ -z "$rc_force" ]; then 603 return 1 604 fi 605 606 # setup the command to run, and run it 607 # 608 echo "Starting ${name}." 609 if [ -n "$_chroot" ]; then 610 _doit="\ 611${_nice:+nice -n $_nice }\ 612chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 613$_chroot $command $rc_flags $command_args" 614 else 615 _doit="\ 616${_chdir:+cd $_chdir; }\ 617${_nice:+nice -n $_nice }\ 618$command $rc_flags $command_args" 619 if [ -n "$_user" ]; then 620 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 621 fi 622 fi 623 624 # if the cmd failed and force 625 # isn't set, exit 626 # 627 debug "run_rc_command: _doit: $_doit" 628 if ! eval $_doit && [ -z "$rc_force" ]; then 629 return 1 630 fi 631 632 # finally, run postcmd 633 # 634 eval $_postcmd 635 ;; 636 637 stop) 638 if [ -z "$rc_pid" ]; then 639 if [ -n "$pidfile" ]; then 640 echo \ 641 "${name} not running? (check $pidfile)." 642 else 643 echo "${name} not running?" 644 fi 645 exit 1 646 fi 647 648 # if the precmd failed and force 649 # isn't set, exit 650 # 651 if ! eval $_precmd && [ -z "$rc_force" ]; then 652 return 1 653 fi 654 655 # send the signal to stop 656 # 657 echo "Stopping ${name}." 658 _doit="kill -${sig_stop:-TERM} $rc_pid" 659 if [ -n "$_user" ]; then 660 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 661 fi 662 663 # if the stop cmd failed and force 664 # isn't set, exit 665 # 666 if ! eval $_doit && [ -z "$rc_force" ]; then 667 return 1 668 fi 669 670 # wait for the command to exit, 671 # and run postcmd. 672 wait_for_pids $rc_pid 673 eval $_postcmd 674 ;; 675 676 reload) 677 if [ -z "$rc_pid" ]; then 678 if [ -n "$pidfile" ]; then 679 echo \ 680 "${name} not running? (check $pidfile)." 681 else 682 echo "${name} not running?" 683 fi 684 exit 1 685 fi 686 echo "Reloading ${name} config files." 687 if ! eval $_precmd && [ -z "$rc_force" ]; then 688 return 1 689 fi 690 _doit="kill -${sig_reload:-HUP} $rc_pid" 691 if [ -n "$_user" ]; then 692 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 693 fi 694 if ! eval $_doit && [ -z "$rc_force" ]; then 695 return 1 696 fi 697 eval $_postcmd 698 ;; 699 700 restart) 701 if ! eval $_precmd && [ -z "$rc_force" ]; then 702 return 1 703 fi 704 # prevent restart being called more 705 # than once by any given script 706 # 707 if [ -n "$_rc_restart_done" ]; then 708 return 0 709 fi 710 _rc_restart_done=YES 711 712 ( $0 ${rc_force:+force}stop ) 713 $0 ${rc_force:+force}start 714 715 eval $_postcmd 716 ;; 717 718 poll) 719 if [ -n "$rc_pid" ]; then 720 wait_for_pids $rc_pid 721 fi 722 ;; 723 724 rcvar) 725 echo "# $name" 726 if [ -n "$rcvar" ]; then 727 if checkyesno ${rcvar}; then 728 echo "\$${rcvar}=YES" 729 else 730 echo "\$${rcvar}=NO" 731 fi 732 fi 733 ;; 734 735 *) 736 rc_usage "$_keywords" 737 ;; 738 739 esac 740 return 0 741 done 742 743 echo 1>&2 "$0: unknown directive '$rc_arg'." 744 rc_usage "$_keywords" 745 exit 1 746} 747 748# 749# run_rc_script file arg 750# Start the script `file' with `arg', and correctly handle the 751# return value from the script. If `file' ends with `.sh', it's 752# sourced into the current environment. If `file' appears to be 753# a backup or scratch file, ignore it. Otherwise if it's 754# executable run as a child process. 755# 756run_rc_script() 757{ 758 _file=$1 759 _arg=$2 760 if [ -z "$_file" -o -z "$_arg" ]; then 761 err 3 'USAGE: run_rc_script file arg' 762 fi 763 764 trap "echo 'Reboot interrupted'; exit 1" 3 765 766 unset name command command_args command_interpreter \ 767 extra_commands pidfile procname \ 768 rcvar required_dirs required_files required_vars 769 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 770 771 case "$_file" in 772 *.sh) # run in current shell 773 set $_arg ; . $_file 774 ;; 775 *[~#]|*.OLD|*.orig) # scratch file; skip 776 warn "Ignoring scratch file $_file" 777 ;; 778 *) # run in subshell 779 if [ -x $_file ]; then 780 if [ -n "$rc_fast_and_loose" ]; then 781 set $_arg ; . $_file 782 else 783 ( trap "echo 'Reboot interrupted'; exit 1" 3 784 set $_arg ; . $_file ) 785 fi 786 fi 787 ;; 788 esac 789} 790 791# 792# load_rc_config 793# Source in the configuration file for a given command. 794# 795load_rc_config() 796{ 797 _command=$1 798 if [ -z "$_command" ]; then 799 err 3 'USAGE: load_rc_config command' 800 fi 801 802 if [ -z "$_rc_conf_loaded" ]; then 803 if [ -r /etc/defaults/rc.conf ]; then 804 debug "Sourcing /etc/defaults/rc.conf" 805 . /etc/defaults/rc.conf 806 source_rc_confs 807 elif [ -r /etc/rc.conf ]; then 808 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 809 . /etc/rc.conf 810 fi 811 _rc_conf_loaded=YES 812 fi 813 if [ -f /etc/rc.conf.d/"$_command" ]; then 814 debug "Sourcing /etc/rc.conf.d/${_command}" 815 . /etc/rc.conf.d/"$_command" 816 fi 817} 818 819 820# 821# rc_usage commands 822# Print a usage string for $0, with `commands' being a list of 823# valid commands. 824# 825rc_usage() 826{ 827 echo -n 1>&2 "usage: $0 [fast|force](" 828 829 _sep= 830 for _elem in $*; do 831 echo -n 1>&2 "$_sep$_elem" 832 _sep="|" 833 done 834 echo 1>&2 ")" 835 exit 1 836} 837 838# 839# _echo prefix message 840# Display message preceded by "$prefix:". Log to syslog as well. 841# XXX - syslogd may not be listening (especially if this subroutine 842# is called at boot before syslogd has had a chance to startup). 843# 844_echo() 845{ 846 [ -x /usr/bin/logger ] && /usr/bin/logger "$0: $1: $2" 847 echo "$0: $1: $2" 848} 849 850# 851# err exitval message 852# Display message to stderr and log to the syslog, and exit with exitval. 853# 854err() 855{ 856 exitval=$1 857 shift 858 859 _echo 1>&2 "ERROR" "$*" 860 exit $exitval 861} 862 863# 864# warn message 865# Display message to stderr and log to the syslog. 866# 867warn() 868{ 869 _echo 1>&2 "WARNING" "$*" 870} 871 872# 873# info message 874# Display informational message to stdout and log to syslog. 875# 876info() 877{ 878 _echo "INFO" "$*" 879} 880 881# 882# debug message 883# If debugging is enabled in rc.conf output message to stderr and syslog. 884# BEWARE that you don't call any subroutine that itself calls this 885# function. 886# 887debug() 888{ 889 # This subroutine is provided as a convenience to script writers, who 890 # should enable debugging in /etc/rc.conf. 891 # 892 [ -f /etc/rc.conf ] && . /etc/rc.conf 893 894 case ${rc_debug} in 895 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 896 _echo 1>&2 "DEBUG" "$*" 897 return 898 ;; 899 esac 900} 901 902# 903# backup_file action file cur backup 904# Make a backup copy of `file' into `cur', and save the previous 905# version of `cur' as `backup' or use rcs for archiving. 906# 907# This routine checks the value of the backup_uses_rcs variable, 908# which can be either YES or NO. 909# 910# The `action' keyword can be one of the following: 911# 912# add `file' is now being backed up (and is possibly 913# being reentered into the backups system). `cur' 914# is created and RCS files, if necessary, are 915# created as well. 916# 917# update `file' has changed and needs to be backed up. 918# If `cur' exists, it is copied to to `back' or 919# checked into RCS (if the repository file is old), 920# and then `file' is copied to `cur'. Another RCS 921# check in done here if RCS is being used. 922# 923# remove `file' is no longer being tracked by the backups 924# system. If RCS is not being used, `cur' is moved 925# to `back', otherwise an empty file is checked in, 926# and then `cur' is removed. 927# 928# 929backup_file() 930{ 931 _action=$1 932 _file=$2 933 _cur=$3 934 _back=$4 935 936 if checkyesno backup_uses_rcs; then 937 _msg0="backup archive" 938 _msg1="update" 939 940 # ensure that history file is not locked 941 if [ -f $_cur,v ]; then 942 rcs -q -u -U -M $_cur 943 fi 944 945 # ensure after switching to rcs that the 946 # current backup is not lost 947 if [ -f $_cur ]; then 948 # no archive, or current newer than archive 949 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 950 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 951 rcs -q -kb -U $_cur 952 co -q -f -u $_cur 953 fi 954 fi 955 956 case $_action in 957 add|update) 958 cp -p $_file $_cur 959 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 960 rcs -q -kb -U $_cur 961 co -q -f -u $_cur 962 chown root:wheel $_cur $_cur,v 963 ;; 964 remove) 965 cp /dev/null $_cur 966 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 967 rcs -q -kb -U $_cur 968 chown root:wheel $_cur $_cur,v 969 rm $_cur 970 ;; 971 esac 972 else 973 case $_action in 974 add|update) 975 if [ -f $_cur ]; then 976 cp -p $_cur $_back 977 fi 978 cp -p $_file $_cur 979 chown root:wheel $_cur 980 ;; 981 remove) 982 mv -f $_cur $_back 983 ;; 984 esac 985 fi 986} 987