rc.subr revision 1.44
1# $NetBSD: rc.subr,v 1.44 2002/03/13 06:58:34 lukem Exp $ 2# 3# Copyright (c) 1997-2002 The NetBSD Foundation, Inc. 4# All rights reserved. 5# 6# This code is derived from software contributed to The NetBSD Foundation 7# by Luke Mewburn. 8# 9# Redistribution and use in source and binary forms, with or without 10# modification, are permitted provided that the following conditions 11# are met: 12# 1. Redistributions of source code must retain the above copyright 13# notice, this list of conditions and the following disclaimer. 14# 2. Redistributions in binary form must reproduce the above copyright 15# notice, this list of conditions and the following disclaimer in the 16# documentation and/or other materials provided with the distribution. 17# 3. All advertising materials mentioning features or use of this software 18# must display the following acknowledgement: 19# This product includes software developed by the NetBSD 20# Foundation, Inc. and its contributors. 21# 4. Neither the name of The NetBSD Foundation nor the names of its 22# contributors may be used to endorse or promote products derived 23# from this software without specific prior written permission. 24# 25# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35# POSSIBILITY OF SUCH DAMAGE. 36# 37# rc.subr 38# functions used by various rc scripts 39# 40 41# 42# functions 43# --------- 44 45# 46# checkyesno var 47# Test $1 variable, and warn if not set to YES or NO. 48# Return 0 if it's "yes" (et al), nonzero otherwise. 49# 50checkyesno() 51{ 52 eval _value=\$${1} 53 case $_value in 54 55 # "yes", "true", "on", or "1" 56 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 57 return 0 58 ;; 59 60 # "no", "false", "off", or "0" 61 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 62 return 1 63 ;; 64 *) 65 warn "\$${1} is not set properly." 66 return 1 67 ;; 68 esac 69} 70 71# reverse_list list 72# print the list in reverse order 73# 74reverse_list() 75{ 76 _revlist= 77 for _revfile in $*; do 78 _revlist="$_revfile $_revlist" 79 done 80 echo $_revlist 81} 82 83# 84# mount_critical_filesystems 85# Go through the list of critical filesystems, checking each one 86# to see if it is mounted, and if it is not, mounting it. 87# 88mount_critical_filesystems() 89{ 90 if [ $1 = local ]; then 91 _fslist=$critical_filesystems_beforenet 92 else 93 _fslist=$critical_filesystems 94 fi 95 for _fs in $_fslist; do 96 mount | ( 97 _ismounted=no 98 while read what _on on _type type; do 99 if [ $on = $_fs ]; then 100 _ismounted=yes 101 fi 102 done 103 if [ $_ismounted = no ]; then 104 mount $_fs >/dev/null 2>&1 105 fi 106 ) 107 done 108} 109 110# 111# check_pidfile pidfile procname 112# Parses the first line of pidfile for a pid, and ensures 113# that the process is running and matches procname. 114# Prints the matching pid upon success, nothing otherwise. 115# 116check_pidfile() 117{ 118 _pidfile=$1 119 _procname=$2 120 if [ -z "$_pidfile" -o -z "$_procname" ]; then 121 err 3 'USAGE: check_pidfile pidfile procname' 122 fi 123 if [ ! -f $_pidfile ]; then 124 return 125 fi 126 read _pid _junk < $_pidfile 127 if [ -z "$_pid" ]; then 128 return 129 fi 130 _procnamebn=${_procname##*/} 131 ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do 132 case "$_npid" in 133 PID) 134 continue ;; 135 esac 136 case "$_arg0" in 137 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})") 138 echo $_npid 139 return 140 ;; 141 esac 142 done 143} 144 145# 146# check_process procname 147# Ensures that a process (or processes) named procname is running. 148# Prints a list of matching pids. 149# 150check_process() 151{ 152 _procname=$1 153 if [ -z "$_procname" ]; then 154 err 3 'USAGE: check_process procname' 155 fi 156 _procnamebn=${_procname##*/} 157 _pref= 158 ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do 159 case "$_npid" in 160 PID) 161 continue ;; 162 esac 163 case "$_arg0" in 164 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})") 165 echo -n "$_pref$_npid" 166 _pref=" " 167 ;; 168 esac 169 done 170} 171 172# 173# wait_for_pids pid [pid ...] 174# spins until none of the pids exist 175# 176wait_for_pids() 177{ 178 _list=$* 179 if [ -z "$_list" ]; then 180 return 181 fi 182 _prefix= 183 while true; do 184 _nlist=""; 185 for _j in $_list; do 186 if kill -0 $_j 2>/dev/null; then 187 _nlist="${_nlist}${_nlist:+ }$_j" 188 fi 189 done 190 if [ -z "$_nlist" ]; then 191 break 192 fi 193 _list=$_nlist 194 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 195 _prefix=", " 196 sleep 2 197 done 198 if [ -n "$_prefix" ]; then 199 echo "." 200 fi 201} 202 203# 204# run_rc_command arg 205# Search for arg in the list of supported commands, which is: 206# "start stop restart rcvar status poll ${extra_commands}" 207# If there's a match, run ${arg}_cmd or the default command (see below). 208# 209# If arg has a given prefix, then change the operation as follows: 210# prefix operation 211# ------ --------- 212# fast Skip the pid check. 213# force Set ${rcvar} to YES. 214# 215# The following globals are used: 216# 217# name needed function 218# ---- ------ -------- 219# name y Name of script. 220# 221# command n Full path to command. 222# Not needed if ${arg}_cmd is set for 223# each keyword. 224# 225# command_args n Optional args/shell directives for command. 226# 227# extra_commands n List of extra commands supported. 228# 229# pidfile n If set, use check_pidfile $pidfile $command, 230# otherwise use check_process $command. 231# In either case, only check if $command is set. 232# 233# procname n Process name to check for instead of $command. 234# 235# rcvar n This is checked with checkyesno to determine 236# if the action should be run. 237# 238# ${name}_chroot n Directory to chroot to before running ${command} 239# Requires /usr to be mounted. 240# 241# ${name}_chdir n Directory to cd to before running ${command} 242# (if not using ${name}_chroot). 243# 244# ${name}_flags n Arguments to call ${command} with. 245# NOTE: $flags from the parent environment 246# can be used to override this. 247# 248# ${name}_nice n Nice level to run ${command} at. 249# 250# ${name}_user n User to run ${command} as, using su(1) if not 251# using ${name}_chroot. 252# Requires /usr to be mounted. 253# 254# ${name}_group n Group to run chrooted ${command} as. 255# Requires /usr to be mounted. 256# 257# ${name}_groups n Comma separated list of supplementary groups 258# to run the chrooted ${command} with. 259# Requires /usr to be mounted. 260# 261# ${_arg}_cmd n If set, use this as the action when invoked; 262# $_arg is available to the action to use. 263# Otherwise, use default command (see below) 264# 265# ${_arg}_precmd n If set, run just before performing the main 266# action in the default command (i.e, after 267# checking for required bits and process 268# (non)existence). 269# If this completes with a non-zero exit code, 270# don't run ${_arg}_cmd. 271# 272# ${_arg}_postcmd n If set, run just after performing the main 273# action in the default command, if that action 274# returned a zero exit code. 275# 276# required_dirs n If set, check for the existence of the given 277# directories before running the default 278# (re)start command. 279# 280# required_files n If set, check for the readability of the given 281# files before running the default (re)start 282# command. 283# 284# required_vars n If set, perform checkyesno on each of the 285# listed variables before running the default 286# (re)start command. 287# 288# Default commands for a given arg: 289# 290# arg default 291# --- ------- 292# start if !running && checkyesno ${rcvar} 293# ${command} 294# 295# stop if ${pidfile} 296# _pid=$(check_pidfile $pidfile $command) 297# else 298# _pid=$(check_process $command) 299# kill $sig_stop $_pid 300# wait_for_pids $_pid 301# ($sig_stop defaults to TERM.) 302# 303# reload Similar to stop, except use $sig_reload instead, 304# and doesn't wait_for_pids. 305# $sig_reload defaults to HUP. 306# 307# restart Run `stop' then `start'. 308# 309# status Show if ${command} is running, etc. 310# 311# poll Wait for ${command} to exit. 312# 313# rcvar Display what rc.conf variable is used (if any). 314# 315# 316# 317run_rc_command() 318{ 319 _arg=$1 320 if [ -z "$name" ]; then 321 err 3 'run_rc_command: $name is not set.' 322 fi 323 324 case "$_arg" in 325 fast*) # "fast" prefix; don't check pid 326 _arg=${_arg#fast} 327 _rc_fast_run=YES 328 ;; 329 force*) # "force prefix; always start 330 _arg=${_arg#force} 331 _rc_force_run=YES 332 if [ -n "${rcvar}" ]; then 333 eval ${rcvar}=YES 334 fi 335 ;; 336 esac 337 338 _keywords="start stop restart rcvar $extra_commands" 339 _pid= 340 _pidcmd= 341 _procname=${procname:-${command}} 342 343 # setup pid check command if not fast 344 if [ -z "$_rc_fast_run" -a -n "$_procname" ]; then 345 if [ -n "$pidfile" ]; then 346 _pidcmd='_pid=$(check_pidfile '$pidfile' '$_procname')' 347 else 348 _pidcmd='_pid=$(check_process '$_procname')' 349 fi 350 if [ -n "$_pidcmd" ]; then 351 _keywords="${_keywords} status poll" 352 fi 353 fi 354 355 if [ -z "$_arg" ]; then 356 rc_usage "$_keywords" 357 fi 358 359 if [ -n "$flags" ]; then # allow override from environment 360 _flags=$flags 361 else 362 eval _flags=\$${name}_flags 363 fi 364 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 365 _nice=\$${name}_nice _user=\$${name}_user \ 366 _group=\$${name}_group _groups=\$${name}_groups 367 368 if [ -n "$_user" ]; then # unset $_user if running as that user 369 if [ "$_user" = "$(id -un)" ]; then 370 unset _user 371 fi 372 fi 373 374 # if ${rcvar} is set, and $1 is not 375 # "rcvar", then run 376 # checkyesno ${rcvar} 377 # and return if that failed 378 # 379 # XXXX use case? 380 if [ -n "${rcvar}" -a "$_arg" != "rcvar" ]; then 381 if ! checkyesno ${rcvar}; then 382 return 0 383 fi 384 fi 385 386 eval $_pidcmd # determine the pid if necessary 387 388 for _elem in $_keywords; do 389 if [ "$_elem" != "$_arg" ]; then 390 continue 391 fi 392 393 # if there's a custom ${XXX_cmd}, 394 # run that instead of the default 395 # 396 eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd \ 397 _postcmd=\$${_arg}_postcmd 398 if [ -n "$_cmd" ]; then 399 # if the precmd failed and force 400 # isn't set, exit 401 # 402 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 403 return 1 404 fi 405 406 if ! eval $_cmd && [ -z "$_rc_force_run" ]; then 407 return 1 408 fi 409 eval $_postcmd 410 return 0 411 fi 412 413 case "$_arg" in # default operations... 414 415 status) 416 if [ -n "$_pid" ]; then 417 echo "${name} is running as pid $_pid." 418 else 419 echo "${name} is not running." 420 return 1 421 fi 422 ;; 423 424 start) 425 if [ -n "$_pid" ]; then 426 echo "${name} already running? (pid=$_pid)." 427 exit 1 428 fi 429 430 if [ ! -x $command ]; then 431 return 0 432 fi 433 434 # check for required variables, 435 # directories, and files 436 # 437 for _f in $required_vars; do 438 if ! checkyesno $_f; then 439 warn "\$${_f} is not set." 440 if [ -z "$_rc_force_run" ]; then 441 return 1 442 fi 443 fi 444 done 445 for _f in $required_dirs; do 446 if [ ! -d "${_f}/." ]; then 447 warn "${_f} is not a directory." 448 if [ -z "$_rc_force_run" ]; then 449 return 1 450 fi 451 fi 452 done 453 for _f in $required_files; do 454 if [ ! -r "${_f}" ]; then 455 warn "${_f} is not readable." 456 if [ -z "$_rc_force_run" ]; then 457 return 1 458 fi 459 fi 460 done 461 462 # if the precmd failed and force 463 # isn't set, exit 464 # 465 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 466 return 1 467 fi 468 469 # setup the command to run, and run it 470 # 471 echo "Starting ${name}." 472 if [ -n "$_chroot" ]; then 473 _doit="\ 474${_nice:+nice -n $_nice }\ 475chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 476$_chroot $command $_flags $command_args" 477 else 478 _doit="\ 479${_chdir:+cd $_chdir; }\ 480${_nice:+nice -n $_nice }\ 481$command $_flags $command_args" 482 if [ -n "$_user" ]; then 483 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 484 fi 485 fi 486 487 # if the cmd failed and force 488 # isn't set, exit 489 # 490 if ! eval $_doit && [ -z "$_rc_force_run" ]; then 491 return 1 492 fi 493 494 # finally, run postcmd 495 # 496 eval $_postcmd 497 ;; 498 499 stop) 500 if [ -z "$_pid" ]; then 501 if [ -n "$pidfile" ]; then 502 echo \ 503 "${name} not running? (check $pidfile)." 504 else 505 echo "${name} not running?" 506 fi 507 exit 1 508 fi 509 510 # if the precmd failed and force 511 # isn't set, exit 512 # 513 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 514 return 1 515 fi 516 517 # send the signal to stop 518 # 519 echo "Stopping ${name}." 520 _doit="kill -${sig_stop:-TERM} $_pid" 521 if [ -n "$_user" ]; then 522 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 523 fi 524 525 # if the stop cmd failed and force 526 # isn't set, exit 527 # 528 if ! eval $_doit && [ -z "$_rc_force_run" ]; then 529 return 1 530 fi 531 532 # wait for the command to exit, 533 # and run postcmd. 534 wait_for_pids $_pid 535 eval $_postcmd 536 ;; 537 538 reload) 539 if [ -z "$_pid" ]; then 540 if [ -n "$pidfile" ]; then 541 echo \ 542 "${name} not running? (check $pidfile)." 543 else 544 echo "${name} not running?" 545 fi 546 exit 1 547 fi 548 echo "Reloading ${name} config files." 549 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 550 return 1 551 fi 552 _doit="kill -${sig_reload:-HUP} $_pid" 553 if [ -n "$_user" ]; then 554 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 555 fi 556 if ! eval $_doit && [ -z "$_rc_force_run" ]; then 557 return 1 558 fi 559 eval $_postcmd 560 ;; 561 562 restart) 563 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 564 return 1 565 fi 566 # prevent restart being called more 567 # than once by any given script 568 # 569 if [ -n "$_rc_restart_done" ]; then 570 return 0 571 fi 572 _rc_restart_done=YES 573 574 ( $0 ${_rc_force_run:+force}stop ) 575 $0 ${_rc_force_run:+force}start 576 577 eval $_postcmd 578 ;; 579 580 poll) 581 if [ -n "$_pid" ]; then 582 wait_for_pids $_pid 583 fi 584 ;; 585 586 rcvar) 587 echo "# $name" 588 if [ -n "$rcvar" ]; then 589 if checkyesno ${rcvar}; then 590 echo "\$${rcvar}=YES" 591 else 592 echo "\$${rcvar}=NO" 593 fi 594 fi 595 ;; 596 597 *) 598 rc_usage "$_keywords" 599 ;; 600 601 esac 602 return 0 603 done 604 605 echo 1>&2 "$0: unknown directive '$_arg'." 606 rc_usage "$_keywords" 607 exit 1 608} 609 610# 611# run_rc_script file arg 612# Start the script `file' with `arg', and correctly handle the 613# return value from the script. If `file' ends with `.sh', it's 614# sourced into the current environment. If `file' appears to be 615# a backup or scratch file, ignore it. Otherwise if it's 616# executable run as a child process. 617# 618run_rc_script() 619{ 620 _file=$1 621 _arg=$2 622 if [ -z "$_file" -o -z "$_arg" ]; then 623 err 3 'USAGE: run_rc_script file arg' 624 fi 625 626 unset name command command_args extra_commands pidfile procname \ 627 rcvar required_dirs required_files required_vars 628 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 629 630 case "$_file" in 631 *.sh) # run in current shell 632 set $_arg ; . $_file 633 ;; 634 *[~#]|*.OLD|*.orig) # scratch file; skip 635 warn "Ignoring scratch file $_file" 636 ;; 637 *) # run in subshell 638 if [ -x $_file ]; then 639 if [ -n "$rc_fast_and_loose" ]; then 640 set $_arg ; . $_file 641 else 642 ( set $_arg ; . $_file ) 643 fi 644 fi 645 ;; 646 esac 647} 648 649# 650# load_rc_config 651# Source in the configuration file for a given command. 652# 653load_rc_config() 654{ 655 _command=$1 656 if [ -z "$_command" ]; then 657 err 3 'USAGE: load_rc_config command' 658 fi 659 660 if [ -z "$_rc_conf_loaded" ]; then 661 . /etc/rc.conf 662 _rc_conf_loaded=YES 663 fi 664 if [ -f /etc/rc.conf.d/"$_command" ]; then 665 . /etc/rc.conf.d/"$_command" 666 fi 667} 668 669 670# 671# rc_usage commands 672# Print a usage string for $0, with `commands' being a list of 673# valid commands. 674# 675rc_usage() 676{ 677 echo -n 1>&2 "Usage: $0 [fast|force](" 678 679 _sep= 680 for _elem in $*; do 681 echo -n 1>&2 "$_sep$_elem" 682 _sep="|" 683 done 684 echo 1>&2 ")" 685 exit 1 686} 687 688# 689# err exitval message 690# Display message to stderr and log to the syslog, and exit with exitval. 691# 692err() 693{ 694 exitval=$1 695 shift 696 697 logger "$0: ERROR: $*" 698 echo 1>&2 "$0: ERROR: $*" 699 exit $exitval 700} 701 702# 703# warn message 704# Display message to stderr and log to the syslog. 705# 706warn() 707{ 708 logger "$0: WARNING: $*" 709 echo 1>&2 "$0: WARNING: $*" 710} 711 712# 713# backup_file action file cur backup 714# Make a backup copy of `file' into `cur', and save the previous 715# version of `cur' as `backup' or use rcs for archiving. 716# 717# This routine checks the value of the backup_uses_rcs variable, 718# which can be either YES or NO. 719# 720# The `action' keyword can be one of the following: 721# 722# add `file' is now being backed up (and is possibly 723# being reentered into the backups system). `cur' 724# is created and RCS files, if necessary, are 725# created as well. 726# 727# update `file' has changed and needs to be backed up. 728# If `cur' exists, it is copied to to `back' or 729# checked into RCS (if the repository file is old), 730# and then `file' is copied to `cur'. Another RCS 731# check in done here if RCS is being used. 732# 733# remove `file' is no longer being tracked by the backups 734# system. If RCS is not being used, `cur' is moved 735# to `back', otherwise an empty file is checked in, 736# and then `cur' is removed. 737# 738# 739backup_file() 740{ 741 _action=$1 742 _file=$2 743 _cur=$3 744 _back=$4 745 746 if checkyesno backup_uses_rcs; then 747 _msg0="backup archive" 748 _msg1="update" 749 750 # ensure that history file is not locked 751 if [ -f $_cur,v ]; then 752 rcs -q -u -U -M $_cur 753 fi 754 755 # ensure after switching to rcs that the 756 # current backup is not lost 757 if [ -f $_cur ]; then 758 # no archive, or current newer than archive 759 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 760 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 761 rcs -q -kb -U $_cur 762 fi 763 fi 764 765 case $_action in 766 add|update) 767 cp -p $_file $_cur 768 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 769 rcs -q -kb -U $_cur 770 chown root:wheel $_cur $_cur,v 771 ;; 772 remove) 773 cp /dev/null $_cur 774 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 775 rcs -q -kb -U $_cur 776 chown root:wheel $_cur $_cur,v 777 rm $_cur 778 ;; 779 esac 780 else 781 case $_action in 782 add|update) 783 if [ -f $_cur ]; then 784 cp -p $_cur $_back 785 fi 786 cp -p $_file $_cur 787 chown root:wheel $_cur 788 ;; 789 remove) 790 mv -f $_cur $_back 791 ;; 792 esac 793 fi 794} 795