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