rc.subr revision 126286
1# $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $ 2# $FreeBSD: head/etc/rc.subr 126286 2004-02-26 12:30:38Z mtm $ 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" 49OSTYPE=`${CMD_OSTYPE}` 50ID="/usr/bin/id" 51IDCMD="if [ -x $ID ]; then $ID -un; fi" 52 53case ${OSTYPE} in 54FreeBSD) 55 SYSCTL_W="${SYSCTL}" 56 ;; 57NetBSD) 58 SYSCTL_W="${SYSCTL} -w" 59 ;; 60esac 61 62# 63# functions 64# --------- 65 66# 67# set_rcvar base_var 68# Set the variable name enabling a specific service. 69# FreeBSD uses ${service}_enable, while NetBSD uses 70# just the name of the service. For example: 71# FreeBSD: sendmail_enable="YES" 72# NetBSD : sendmail="YES" 73# $1 - if $name is not the base to work of off, specify 74# a different one 75# 76set_rcvar() 77{ 78 if [ -z "$1" ]; then 79 base_var=${name} 80 else 81 base_var="$1" 82 fi 83 84 case ${OSTYPE} in 85 FreeBSD) 86 echo ${base_var}_enable 87 ;; 88 NetBSD) 89 echo ${base_var} 90 ;; 91 *) 92 echo 'XXX' 93 ;; 94 esac 95} 96 97# 98# force_depend script 99# Force a service to start. Intended for use by services 100# to resolve dependency issues. It is assumed the caller 101# has check to make sure this call is necessary 102# $1 - filename of script, in /etc/rc.d, to run 103# 104force_depend() 105{ 106 _depend="$1" 107 108 info "${name} depends on ${_depend}, which will be forced to start." 109 if ! /etc/rc.d/${_depend} forcestart ; then 110 warn "Unable to force ${_depend}. It may already be running." 111 return 1 112 fi 113 return 0 114} 115 116# 117# checkyesno var 118# Test $1 variable, and warn if not set to YES or NO. 119# Return 0 if it's "yes" (et al), nonzero otherwise. 120# 121checkyesno() 122{ 123 eval _value=\$${1} 124 debug "checkyesno: $1 is set to $_value." 125 case $_value in 126 127 # "yes", "true", "on", or "1" 128 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 129 return 0 130 ;; 131 132 # "no", "false", "off", or "0" 133 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 134 return 1 135 ;; 136 *) 137 warn "\$${1} is not set properly - see rc.conf(5)." 138 return 1 139 ;; 140 esac 141} 142 143# reverse_list list 144# print the list in reverse order 145# 146reverse_list() 147{ 148 _revlist= 149 for _revfile; do 150 _revlist="$_revfile $_revlist" 151 done 152 echo $_revlist 153} 154 155# 156# mount_critical_filesystems type 157# Go through the list of critical filesystems as provided in 158# the rc.conf(5) variable $critical_filesystems_${type}, checking 159# each one to see if it is mounted, and if it is not, mounting it. 160# 161mount_critical_filesystems() 162{ 163 eval _fslist=\$critical_filesystems_${1} 164 for _fs in $_fslist; do 165 mount | ( 166 _ismounted=false 167 while read what _on on _type type; do 168 if [ $on = $_fs ]; then 169 _ismounted=true 170 fi 171 done 172 if $_ismounted; then 173 : 174 else 175 mount $_fs >/dev/null 2>&1 176 fi 177 ) 178 done 179} 180 181# 182# check_pidfile pidfile procname [interpreter] 183# Parses the first line of pidfile for a PID, and ensures 184# that the process is running and matches procname. 185# Prints the matching PID upon success, nothing otherwise. 186# interpreter is optional; see _find_processes() for details. 187# 188check_pidfile() 189{ 190 _pidfile=$1 191 _procname=$2 192 _interpreter=$3 193 if [ -z "$_pidfile" -o -z "$_procname" ]; then 194 err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 195 fi 196 if [ ! -f $_pidfile ]; then 197 debug "pid file {$_pidfile): not readable." 198 return 199 fi 200 read _pid _junk < $_pidfile 201 if [ -z "$_pid" ]; then 202 debug "pid file {$_pidfile): no pid in file." 203 return 204 fi 205 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 206} 207 208# 209# check_process procname [interpreter] 210# Ensures that a process (or processes) named procname is running. 211# Prints a list of matching PIDs. 212# interpreter is optional; see _find_processes() for details. 213# 214check_process() 215{ 216 _procname=$1 217 _interpreter=$2 218 if [ -z "$_procname" ]; then 219 err 3 'USAGE: check_process procname [interpreter]' 220 fi 221 _find_processes $_procname ${_interpreter:-.} '-ax' 222} 223 224# 225# _find_processes procname interpreter psargs 226# Search for procname in the output of ps generated by psargs. 227# Prints the PIDs of any matching processes, space separated. 228# 229# If interpreter == ".", check the following variations of procname 230# against the first word of each command: 231# procname 232# `basename procname` 233# `basename procname` + ":" 234# "(" + `basename procname` + ")" 235# 236# If interpreter != ".", read the first line of procname, remove the 237# leading #!, normalise whitespace, append procname, and attempt to 238# match that against each command, either as is, or with extra words 239# at the end. 240# 241_find_processes() 242{ 243 if [ $# -ne 3 ]; then 244 err 3 'USAGE: _find_processes procname interpreter psargs' 245 fi 246 _procname=$1 247 _interpreter=$2 248 _psargs=$3 249 250 _pref= 251 if [ $_interpreter != "." ]; then # an interpreted script 252 read _interp < $_procname # read interpreter name 253 _interp=${_interp#\#!} # strip #! 254 set -- $_interp 255 if [ $_interpreter != $1 ]; then 256 warn "\$command_interpreter $_interpreter != $1" 257 fi 258 _interp="$* $_procname" # cleanup spaces, add _procname 259 _fp_args='_argv' 260 _fp_match='case "$_argv" in 261 ${_interp}|"${_interp} "*)' 262 else # a normal daemon 263 _procnamebn=${_procname##*/} 264 _fp_args='_arg0 _argv' 265 _fp_match='case "$_arg0" in 266 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 267 fi 268 269 _proccheck=' 270 ps -o "pid,command" '"$_psargs"' | 271 while read _npid '"$_fp_args"'; do 272 case "$_npid" in 273 PID) 274 continue ;; 275 esac ; '"$_fp_match"' 276 echo -n "$_pref$_npid" ; 277 _pref=" " 278 ;; 279 esac 280 done' 281 282# debug "in _find_processes: proccheck is ($_proccheck)." 283 eval $_proccheck 284} 285 286# 287# wait_for_pids pid [pid ...] 288# spins until none of the pids exist 289# 290wait_for_pids() 291{ 292 _list="$@" 293 if [ -z "$_list" ]; then 294 return 295 fi 296 _prefix= 297 while true; do 298 _nlist=""; 299 for _j in $_list; do 300 if kill -0 $_j 2>/dev/null; then 301 _nlist="${_nlist}${_nlist:+ }$_j" 302 fi 303 done 304 if [ -z "$_nlist" ]; then 305 break 306 fi 307 _list=$_nlist 308 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 309 _prefix=", " 310 sleep 2 311 done 312 if [ -n "$_prefix" ]; then 313 echo "." 314 fi 315} 316 317# 318# run_rc_command argument 319# Search for argument in the list of supported commands, which is: 320# "start stop restart rcvar status poll ${extra_commands}" 321# If there's a match, run ${argument}_cmd or the default method 322# (see below). 323# 324# If argument has a given prefix, then change the operation as follows: 325# Prefix Operation 326# ------ --------- 327# fast Skip the pid check, and set rc_fast=yes 328# force Set ${rcvar} to YES, and set rc_force=yes 329# 330# The following globals are used: 331# 332# Name Needed Purpose 333# ---- ------ ------- 334# name y Name of script. 335# 336# command n Full path to command. 337# Not needed if ${rc_arg}_cmd is set for 338# each keyword. 339# 340# command_args n Optional args/shell directives for command. 341# 342# command_interpreter n If not empty, command is interpreted, so 343# call check_{pidfile,process}() appropriately. 344# 345# extra_commands n List of extra commands supported. 346# 347# pidfile n If set, use check_pidfile $pidfile $command, 348# otherwise use check_process $command. 349# In either case, only check if $command is set. 350# 351# procname n Process name to check for instead of $command. 352# 353# rcvar n This is checked with checkyesno to determine 354# if the action should be run. 355# 356# ${name}_chroot n Directory to chroot to before running ${command} 357# Requires /usr to be mounted. 358# 359# ${name}_chdir n Directory to cd to before running ${command} 360# (if not using ${name}_chroot). 361# 362# ${name}_flags n Arguments to call ${command} with. 363# NOTE: $flags from the parent environment 364# can be used to override this. 365# 366# ${name}_nice n Nice level to run ${command} at. 367# 368# ${name}_user n User to run ${command} as, using su(1) if not 369# using ${name}_chroot. 370# Requires /usr to be mounted. 371# 372# ${name}_group n Group to run chrooted ${command} as. 373# Requires /usr to be mounted. 374# 375# ${name}_groups n Comma separated list of supplementary groups 376# to run the chrooted ${command} with. 377# Requires /usr to be mounted. 378# 379# ${rc_arg}_cmd n If set, use this as the method when invoked; 380# Otherwise, use default command (see below) 381# 382# ${rc_arg}_precmd n If set, run just before performing the 383# ${rc_arg}_cmd method in the default 384# operation (i.e, after checking for required 385# bits and process (non)existence). 386# If this completes with a non-zero exit code, 387# don't run ${rc_arg}_cmd. 388# 389# ${rc_arg}_postcmd n If set, run just after performing the 390# ${rc_arg}_cmd method, if that method 391# returned a zero exit code. 392# 393# required_dirs n If set, check for the existence of the given 394# directories before running the default 395# (re)start command. 396# 397# required_files n If set, check for the readability of the given 398# files before running the default (re)start 399# command. 400# 401# required_vars n If set, perform checkyesno on each of the 402# listed variables before running the default 403# (re)start command. 404# 405# Default behaviour for a given argument, if no override method is 406# provided: 407# 408# Argument Default behaviour 409# -------- ----------------- 410# start if !running && checkyesno ${rcvar} 411# ${command} 412# 413# stop if ${pidfile} 414# rc_pid=$(check_pidfile $pidfile $command) 415# else 416# rc_pid=$(check_process $command) 417# kill $sig_stop $rc_pid 418# wait_for_pids $rc_pid 419# ($sig_stop defaults to TERM.) 420# 421# reload Similar to stop, except use $sig_reload instead, 422# and doesn't wait_for_pids. 423# $sig_reload defaults to HUP. 424# 425# restart Run `stop' then `start'. 426# 427# status Show if ${command} is running, etc. 428# 429# poll Wait for ${command} to exit. 430# 431# rcvar Display what rc.conf variable is used (if any). 432# 433# Variables available to methods, and after run_rc_command() has 434# completed: 435# 436# Variable Purpose 437# -------- ------- 438# rc_arg Argument to command, after fast/force processing 439# performed 440# 441# rc_flags Flags to start the default command with. 442# Defaults to ${name}_flags, unless overridden 443# by $flags from the environment. 444# This variable may be changed by the precmd method. 445# 446# rc_pid PID of command (if appropriate) 447# 448# rc_fast Not empty if "fast" was provided (q.v.) 449# 450# rc_force Not empty if "force" was provided (q.v.) 451# 452# 453run_rc_command() 454{ 455 _return=0 456 rc_arg=$1 457 if [ -z "$name" ]; then 458 err 3 'run_rc_command: $name is not set.' 459 fi 460 461 case "$rc_arg" in 462 fast*) # "fast" prefix; don't check pid 463 rc_arg=${rc_arg#fast} 464 rc_fast=yes 465 ;; 466 force*) # "force prefix; always start 467 rc_arg=${rc_arg#force} 468 rc_force=yes 469 if [ -n "${rcvar}" ]; then 470 eval ${rcvar}=YES 471 fi 472 ;; 473 esac 474 475 eval _overide_command=\$${name}_program 476 if [ -n "$_overide_command" ]; then 477 command=$_overide_command 478 fi 479 480 _keywords="start stop restart rcvar $extra_commands" 481 rc_pid= 482 _pidcmd= 483 _procname=${procname:-${command}} 484 485 # setup pid check command if not fast 486 if [ -z "$rc_fast" -a -n "$_procname" ]; then 487 if [ -n "$pidfile" ]; then 488 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 489 else 490 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 491 fi 492 if [ -n "$_pidcmd" ]; then 493 _keywords="${_keywords} status poll" 494 fi 495 fi 496 497 if [ -z "$rc_arg" ]; then 498 rc_usage "$_keywords" 499 fi 500 501 if [ -n "$flags" ]; then # allow override from environment 502 rc_flags=$flags 503 else 504 eval rc_flags=\$${name}_flags 505 fi 506 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 507 _nice=\$${name}_nice _user=\$${name}_user \ 508 _group=\$${name}_group _groups=\$${name}_groups 509 510 if [ -n "$_user" ]; then # unset $_user if running as that user 511 if [ "$_user" = "$(eval $IDCMD)" ]; then 512 unset _user 513 fi 514 fi 515 516 # if ${rcvar} is set, and $1 is not 517 # "rcvar", then run 518 # checkyesno ${rcvar} 519 # and return if that failed 520 # 521 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 522 if ! checkyesno ${rcvar}; then 523 return 0 524 fi 525 fi 526 527 eval $_pidcmd # determine the pid if necessary 528 529 for _elem in $_keywords; do 530 if [ "$_elem" != "$rc_arg" ]; then 531 continue 532 fi 533 534 # if there's a custom ${XXX_cmd}, 535 # run that instead of the default 536 # 537 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 538 _postcmd=\$${rc_arg}_postcmd 539 if [ -n "$_cmd" ]; then 540 # if the precmd failed and force 541 # isn't set, exit 542 # 543 if [ -n "$_precmd" ]; then 544 debug "run_rc_command: evaluating ${_precmd}()." 545 eval $_precmd 546 _return=$? 547 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 548 return 1 549 fi 550 551 if [ -n "$_cmd" ]; then 552 debug "run_rc_command: evaluating ${_cmd}()." 553 eval $_cmd 554 _return=$? 555 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 556 return 1 557 fi 558 559 if [ -n "$_postcmd" ]; then 560 debug "run_rc_command: evaluating ${_postcmd}()." 561 eval $_postcmd 562 _return=$? 563 fi 564 return $_return 565 fi 566 567 case "$rc_arg" in # default operations... 568 569 status) 570 if [ -n "$rc_pid" ]; then 571 echo "${name} is running as pid $rc_pid." 572 else 573 echo "${name} is not running." 574 return 1 575 fi 576 ;; 577 578 start) 579 if [ -n "$rc_pid" ]; then 580 echo "${name} already running? (pid=$rc_pid)." 581 exit 1 582 fi 583 584 if [ ! -x $command ]; then 585 info "run_rc_command: cannot run ($command)." 586 return 0 587 fi 588 589 # check for required variables, 590 # directories, and files 591 # 592 for _f in $required_vars; do 593 if ! checkyesno $_f; then 594 warn "\$${_f} is not set." 595 if [ -z "$rc_force" ]; then 596 return 1 597 fi 598 fi 599 done 600 for _f in $required_dirs; do 601 if [ ! -d "${_f}/." ]; then 602 warn "${_f} is not a directory." 603 if [ -z "$rc_force" ]; then 604 return 1 605 fi 606 fi 607 done 608 for _f in $required_files; do 609 if [ ! -r "${_f}" ]; then 610 warn "${_f} is not readable." 611 if [ -z "$rc_force" ]; then 612 return 1 613 fi 614 fi 615 done 616 617 # if the precmd failed and force 618 # isn't set, exit 619 # 620 if [ -n "${_precmd}" ]; then 621 debug "run_rc_command: evaluating ${_precmd}()." 622 eval $_precmd 623 _return=$? 624 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 625 return 1 626 fi 627 628 # setup the command to run, and run it 629 # 630 echo "Starting ${name}." 631 if [ -n "$_chroot" ]; then 632 _doit="\ 633${_nice:+nice -n $_nice }\ 634chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 635$_chroot $command $rc_flags $command_args" 636 else 637 _doit="\ 638${_chdir:+cd $_chdir; }\ 639${_nice:+nice -n $_nice }\ 640$command $rc_flags $command_args" 641 if [ -n "$_user" ]; then 642 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 643 fi 644 fi 645 646 # if the cmd failed and force 647 # isn't set, exit 648 # 649 debug "run_rc_command: _doit: $_doit" 650 eval $_doit 651 _return=$? 652 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 653 654 # finally, run postcmd 655 # 656 if [ -n "${_postcmd}" ]; then 657 debug "run_rc_command: evaluating ${_postcmd}()." 658 eval $_postcmd 659 fi 660 ;; 661 662 stop) 663 if [ -z "$rc_pid" ]; then 664 if [ -n "$pidfile" ]; then 665 echo \ 666 "${name} not running? (check $pidfile)." 667 else 668 echo "${name} not running?" 669 fi 670 exit 1 671 fi 672 673 # if the precmd failed and force 674 # isn't set, exit 675 # 676 if [ -n "$_precmd" ]; then 677 eval $_precmd 678 _return=$? 679 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 680 return 1 681 fi 682 683 # send the signal to stop 684 # 685 echo "Stopping ${name}." 686 _doit="kill -${sig_stop:-TERM} $rc_pid" 687 if [ -n "$_user" ]; then 688 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 689 fi 690 691 # if the stop cmd failed and force 692 # isn't set, exit 693 # 694 eval $_doit 695 _return=$? 696 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 697 698 # wait for the command to exit, 699 # and run postcmd. 700 wait_for_pids $rc_pid 701 if [ -n "$_postcmd" ]; then 702 eval $_postcmd 703 _return=$? 704 fi 705 ;; 706 707 reload) 708 if [ -z "$rc_pid" ]; then 709 if [ -n "$pidfile" ]; then 710 echo \ 711 "${name} not running? (check $pidfile)." 712 else 713 echo "${name} not running?" 714 fi 715 exit 1 716 fi 717 echo "Reloading ${name} config files." 718 if [ -n "$_precmd" ]; then 719 eval $_precmd 720 _return=$? 721 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 722 return 1 723 fi 724 _doit="kill -${sig_reload:-HUP} $rc_pid" 725 if [ -n "$_user" ]; then 726 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 727 fi 728 eval $_doit 729 _return=$? 730 [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 731 if [ -n "$_postcmd" ]; then 732 eval $_postcmd 733 _return=$? 734 fi 735 ;; 736 737 restart) 738 if [ -n "$_precmd" ]; then 739 eval $_precmd 740 _return=$? 741 [ $_return -ne 0 ] && [ -z "$rc_force" ] && 742 return 1 743 fi 744 # prevent restart being called more 745 # than once by any given script 746 # 747 if ${_rc_restart_done:-false}; then 748 return 0 749 fi 750 _rc_restart_done=true 751 752 ( $0 ${rc_force:+force}stop ) 753 $0 ${rc_force:+force}start 754 755 if [ -n "$_postcmd" ]; then 756 eval $_postcmd 757 _return=$? 758 fi 759 ;; 760 761 poll) 762 if [ -n "$rc_pid" ]; then 763 wait_for_pids $rc_pid 764 fi 765 ;; 766 767 rcvar) 768 echo "# $name" 769 if [ -n "$rcvar" ]; then 770 if checkyesno ${rcvar}; then 771 echo "\$${rcvar}=YES" 772 else 773 echo "\$${rcvar}=NO" 774 fi 775 fi 776 ;; 777 778 *) 779 rc_usage "$_keywords" 780 ;; 781 782 esac 783 return $_return 784 done 785 786 echo 1>&2 "$0: unknown directive '$rc_arg'." 787 rc_usage "$_keywords" 788 exit 1 789} 790 791# 792# run_rc_script file arg 793# Start the script `file' with `arg', and correctly handle the 794# return value from the script. If `file' ends with `.sh', it's 795# sourced into the current environment. If `file' appears to be 796# a backup or scratch file, ignore it. Otherwise if it's 797# executable run as a child process. 798# 799run_rc_script() 800{ 801 _file=$1 802 _arg=$2 803 if [ -z "$_file" -o -z "$_arg" ]; then 804 err 3 'USAGE: run_rc_script file arg' 805 fi 806 807 trap "echo 'Reboot interrupted'; exit 1" 3 808 809 unset name command command_args command_interpreter \ 810 extra_commands pidfile procname \ 811 rcvar required_dirs required_files required_vars 812 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 813 814 case "$_file" in 815 *.sh) # run in current shell 816 set $_arg ; . $_file 817 ;; 818 *[~#]|*.OLD|*.orig) # scratch file; skip 819 warn "Ignoring scratch file $_file" 820 ;; 821 *) # run in subshell 822 if [ -x $_file ]; then 823 if [ -n "$rc_fast_and_loose" ]; then 824 set $_arg ; . $_file 825 else 826 ( trap "echo 'Reboot interrupted'; exit 1" 3 827 set $_arg ; . $_file ) 828 fi 829 fi 830 ;; 831 esac 832} 833 834# 835# load_rc_config 836# Source in the configuration file for a given command. 837# 838load_rc_config() 839{ 840 _command=$1 841 if [ -z "$_command" ]; then 842 err 3 'USAGE: load_rc_config command' 843 fi 844 845 if ${_rc_conf_loaded:-false}; then 846 : 847 else 848 if [ -r /etc/defaults/rc.conf ]; then 849 debug "Sourcing /etc/defaults/rc.conf" 850 . /etc/defaults/rc.conf 851 source_rc_confs 852 elif [ -r /etc/rc.conf ]; then 853 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 854 . /etc/rc.conf 855 fi 856 _rc_conf_loaded=true 857 fi 858 if [ -f /etc/rc.conf.d/"$_command" ]; then 859 debug "Sourcing /etc/rc.conf.d/${_command}" 860 . /etc/rc.conf.d/"$_command" 861 fi 862 863 # XXX - Deprecated variable name support 864 # 865 case ${OSTYPE} in 866 FreeBSD) 867 [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 868 [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 869 [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 870 [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 871 [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 872 [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 873 [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 874 [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program" 875 [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags" 876 ;; 877 esac 878 879} 880 881# 882# rc_usage commands 883# Print a usage string for $0, with `commands' being a list of 884# valid commands. 885# 886rc_usage() 887{ 888 echo -n 1>&2 "Usage: $0 [fast|force](" 889 890 _sep= 891 for _elem; do 892 echo -n 1>&2 "$_sep$_elem" 893 _sep="|" 894 done 895 echo 1>&2 ")" 896 exit 1 897} 898 899# 900# err exitval message 901# Display message to stderr and log to the syslog, and exit with exitval. 902# 903err() 904{ 905 exitval=$1 906 shift 907 908 if [ -x /usr/bin/logger ]; then 909 logger "$0: ERROR: $*" 910 fi 911 echo 1>&2 "$0: ERROR: $*" 912 exit $exitval 913} 914 915# 916# warn message 917# Display message to stderr and log to the syslog. 918# 919warn() 920{ 921 if [ -x /usr/bin/logger ]; then 922 logger "$0: WARNING: $*" 923 fi 924 echo 1>&2 "$0: WARNING: $*" 925} 926 927# 928# info message 929# Display informational message to stdout and log to syslog. 930# 931info() 932{ 933 case ${rc_info} in 934 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 935 if [ -x /usr/bin/logger ]; then 936 logger "$0: INFO: $*" 937 fi 938 echo "$0: INFO: $*" 939 ;; 940 esac 941} 942 943# 944# debug message 945# If debugging is enabled in rc.conf output message to stderr. 946# BEWARE that you don't call any subroutine that itself calls this 947# function. 948# 949debug() 950{ 951 case ${rc_debug} in 952 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 953 if [ -x /usr/bin/logger ]; then 954 logger "$0: INFO: $*" 955 fi 956 echo 1>&2 "$0: DEBUG: $*" 957 ;; 958 esac 959} 960 961# 962# backup_file action file cur backup 963# Make a backup copy of `file' into `cur', and save the previous 964# version of `cur' as `backup' or use rcs for archiving. 965# 966# This routine checks the value of the backup_uses_rcs variable, 967# which can be either YES or NO. 968# 969# The `action' keyword can be one of the following: 970# 971# add `file' is now being backed up (and is possibly 972# being reentered into the backups system). `cur' 973# is created and RCS files, if necessary, are 974# created as well. 975# 976# update `file' has changed and needs to be backed up. 977# If `cur' exists, it is copied to to `back' or 978# checked into RCS (if the repository file is old), 979# and then `file' is copied to `cur'. Another RCS 980# check in done here if RCS is being used. 981# 982# remove `file' is no longer being tracked by the backups 983# system. If RCS is not being used, `cur' is moved 984# to `back', otherwise an empty file is checked in, 985# and then `cur' is removed. 986# 987# 988backup_file() 989{ 990 _action=$1 991 _file=$2 992 _cur=$3 993 _back=$4 994 995 if checkyesno backup_uses_rcs; then 996 _msg0="backup archive" 997 _msg1="update" 998 999 # ensure that history file is not locked 1000 if [ -f $_cur,v ]; then 1001 rcs -q -u -U -M $_cur 1002 fi 1003 1004 # ensure after switching to rcs that the 1005 # current backup is not lost 1006 if [ -f $_cur ]; then 1007 # no archive, or current newer than archive 1008 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 1009 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1010 rcs -q -kb -U $_cur 1011 co -q -f -u $_cur 1012 fi 1013 fi 1014 1015 case $_action in 1016 add|update) 1017 cp -p $_file $_cur 1018 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1019 rcs -q -kb -U $_cur 1020 co -q -f -u $_cur 1021 chown root:wheel $_cur $_cur,v 1022 ;; 1023 remove) 1024 cp /dev/null $_cur 1025 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 1026 rcs -q -kb -U $_cur 1027 chown root:wheel $_cur $_cur,v 1028 rm $_cur 1029 ;; 1030 esac 1031 else 1032 case $_action in 1033 add|update) 1034 if [ -f $_cur ]; then 1035 cp -p $_cur $_back 1036 fi 1037 cp -p $_file $_cur 1038 chown root:wheel $_cur 1039 ;; 1040 remove) 1041 mv -f $_cur $_back 1042 ;; 1043 esac 1044 fi 1045} 1046 1047# make_symlink src link 1048# Make a symbolic link 'link' to src from basedir. If the 1049# directory in which link is to be created does not exist 1050# a warning will be displayed and an error will be returned. 1051# Returns 0 on sucess, 1 otherwise. 1052# 1053make_symlink() 1054{ 1055 local src link linkdir _me 1056 src="$1" 1057 link="$2" 1058 linkdir="`dirname $link`" 1059 _me="make_symlink()" 1060 1061 if [ -z "$src" -o -z "$link" ]; then 1062 warn "$_me: requires two arguments." 1063 return 1 1064 fi 1065 if [ ! -d "$linkdir" ]; then 1066 warn "$_me: the directory $linkdir does not exist" 1067 return 1 1068 fi 1069 if ! ln -sf $src $link ; then 1070 warn "$_me: unable to make a symbolic link from $link to $src" 1071 return 1 1072 fi 1073 return 0 1074} 1075 1076# devfs_rulesets_from_file file 1077# Reads a set of devfs commands from file, and creates 1078# the specified rulesets with their rules. Returns non-zero 1079# if there was an error. 1080# 1081devfs_rulesets_from_file() 1082{ 1083 local file _err _me 1084 file="$1" 1085 _me="devfs_rulesets_from_file" 1086 _err=0 1087 1088 if [ -z "$file" ]; then 1089 warn "$_me: you must specify a file" 1090 return 1 1091 fi 1092 if [ ! -e "$file" ]; then 1093 debug "$_me: no such file ($file)" 1094 return 0 1095 fi 1096 debug "reading rulesets from file ($file)" 1097 { while read line 1098 do 1099 case $line in 1100 \#*) 1101 continue 1102 ;; 1103 \[*\]*) 1104 rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1105 if [ -z "$rulenum" ]; then 1106 warn "$_me: cannot extract rule number ($line)" 1107 _err=1 1108 break 1109 fi 1110 rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1111 if [ -z "$rulename" ]; then 1112 warn "$_me: cannot extract rule name ($line)" 1113 _err=1 1114 break; 1115 fi 1116 eval $rulename=\$rulenum 1117 debug "found ruleset: $rulename=$rulenum" 1118 if ! /sbin/devfs rule -s $rulenum delset ; then 1119 _err=1 1120 break 1121 fi 1122 ;; 1123 *) 1124 rulecmd="${line%%"\#*"}" 1125 # evaluate the command incase it includes 1126 # other rules 1127 if [ -n "$rulecmd" ]; then 1128 debug "adding rule ($rulecmd)" 1129 if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1130 then 1131 _err=1 1132 break 1133 fi 1134 fi 1135 ;; 1136 esac 1137 if [ $_err -ne 0 ]; then 1138 debug "error in $_me" 1139 break 1140 fi 1141 done } < $file 1142 return $_err 1143} 1144 1145# devfs_init_rulesets 1146# Initializes rulesets from configuration files. Returns 1147# non-zero if there was an error. 1148# 1149devfs_init_rulesets() 1150{ 1151 local file _me 1152 _me="devfs_init_rulesets" 1153 1154 # Go through this only once 1155 if [ -n "$devfs_rulesets_init" ]; then 1156 debug "$_me: devfs rulesets already initialized" 1157 return 1158 fi 1159 for file in $devfs_rulesets ; do 1160 devfs_rulesets_from_file $file || return 1 1161 done 1162 devfs_rulesets_init=1 1163 debug "$_me: devfs rulesets initialized" 1164 return 0 1165} 1166 1167# devfs_set_ruleset ruleset [dir] 1168# Sets the default ruleset of dir to ruleset. The ruleset arguement 1169# must be a ruleset name as specified in devfs.rules(5) file. 1170# Returns non-zero if it could not set it successfully. 1171# 1172devfs_set_ruleset() 1173{ 1174 local devdir rs _me 1175 [ -n "$1" ] && eval rs=\$$1 || rs= 1176 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1177 _me="devfs_set_ruleset" 1178 1179 if [ -z "$rs" ]; then 1180 warn "$_me: you must specify a ruleset number" 1181 return 1 1182 fi 1183 debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1184 if ! /sbin/devfs $devdir ruleset $rs ; then 1185 warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1186 return 1 1187 fi 1188 return 0 1189} 1190 1191# devfs_apply_ruleset ruleset [dir] 1192# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1193# The ruleset argument must be a ruleset name as specified 1194# in a devfs.rules(5) file. Returns 0 on success or non-zero 1195# if it could not apply the ruleset. 1196# 1197devfs_apply_ruleset() 1198{ 1199 local devdir rs _me 1200 [ -n "$1" ] && eval rs=\$$1 || rs= 1201 [ -n "$2" ] && devdir="-m "$2"" || devdir= 1202 _me="devfs_apply_ruleset" 1203 1204 if [ -z "$rs" ]; then 1205 warn "$_me: you must specify a ruleset" 1206 return 1 1207 fi 1208 debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1209 if ! /sbin/devfs $devdir rule -s $rs applyset ; then 1210 warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1211 return 1 1212 fi 1213 return 0 1214} 1215 1216# devfs_domount dir [ruleset] 1217# Mount devfs on dir. If ruleset is specified it is set 1218# on the mount-point. It must also be a ruleset name as specified 1219# in a devfs.rules(5) file. Returns 0 on success. 1220# 1221devfs_domount() 1222{ 1223 local devdir rs _me 1224 devdir="$1" 1225 [ -n "$2" ] && rs=$2 || rs= 1226 _me="devfs_domount()" 1227 1228 if [ -z "$devdir" ]; then 1229 warn "$_me: you must specify a mount-point" 1230 return 1 1231 fi 1232 debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1233 if ! mount -t devfs dev "$devdir" ; then 1234 warn "$_me: Unable to mount devfs on $devdir" 1235 return 1 1236 fi 1237 if [ -n "$rs" ]; then 1238 devfs_init_rulesets 1239 devfs_set_ruleset $rs $devdir 1240 devfs -m $devdir rule applyset 1241 fi 1242 return 0 1243} 1244 1245# devfs_mount_jail dir [ruleset] 1246# Mounts a devfs file system appropriate for jails 1247# on the directory dir. If ruleset is specified, the ruleset 1248# it names will be used instead. If present, ruleset must 1249# be the name of a ruleset as defined in a devfs.rules(5) file. 1250# This function returns non-zero if an error occurs. 1251# 1252devfs_mount_jail() 1253{ 1254 local jdev rs _me 1255 jdev="$1" 1256 [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1257 _me="devfs_mount_jail" 1258 1259 devfs_init_rulesets 1260 if ! devfs_domount "$jdev" $rs ; then 1261 warn "$_me: devfs was not mounted on $jdev" 1262 return 1 1263 fi 1264 return 0 1265} 1266