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