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