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