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