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