rc.subr revision 1.43
1# $NetBSD: rc.subr,v 1.43 2002/03/13 04:50:08 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 eval $_cmd 407 return 0 408 fi 409 410 case "$_arg" in # default operations... 411 412 status) 413 if [ -n "$_pid" ]; then 414 echo "${name} is running as pid $_pid." 415 else 416 echo "${name} is not running." 417 return 1 418 fi 419 ;; 420 421 start) 422 if [ -n "$_pid" ]; then 423 echo "${name} already running? (pid=$_pid)." 424 exit 1 425 fi 426 427 if [ ! -x $command ]; then 428 return 0 429 fi 430 431 # check for required variables, 432 # directories, and files 433 # 434 for _f in $required_vars; do 435 if ! checkyesno $_f; then 436 warn "\$${_f} is not set." 437 if [ -z "$_rc_force_run" ]; then 438 return 1 439 fi 440 fi 441 done 442 for _f in $required_dirs; do 443 if [ ! -d "${_f}/." ]; then 444 warn "${_f} is not a directory." 445 if [ -z "$_rc_force_run" ]; then 446 return 1 447 fi 448 fi 449 done 450 for _f in $required_files; do 451 if [ ! -r "${_f}" ]; then 452 warn "${_f} is not readable." 453 if [ -z "$_rc_force_run" ]; then 454 return 1 455 fi 456 fi 457 done 458 459 # if the precmd failed and force 460 # isn't set, exit 461 # 462 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 463 return 1 464 fi 465 466 # setup the command to run, and run it 467 # 468 echo "Starting ${name}." 469 if [ -n "$_chroot" ]; then 470 _doit="\ 471${_nice:+nice -n $_nice }\ 472chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 473$_chroot $command $_flags $command_args" 474 else 475 _doit="\ 476${_chdir:+cd $_chdir; }\ 477${_nice:+nice -n $_nice }\ 478$command $_flags $command_args" 479 if [ -n "$_user" ]; then 480 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 481 fi 482 fi 483 484 # if the cmd failed and force 485 # isn't set, exit 486 # 487 if ! eval $_doit && [ -z "$_rc_force_run" ]; then 488 return 1 489 fi 490 491 # finally, run postcmd 492 # 493 eval $_postcmd 494 ;; 495 496 stop) 497 if [ -z "$_pid" ]; then 498 if [ -n "$pidfile" ]; then 499 echo \ 500 "${name} not running? (check $pidfile)." 501 else 502 echo "${name} not running?" 503 fi 504 exit 1 505 fi 506 507 # if the precmd failed and force 508 # isn't set, exit 509 # 510 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 511 return 1 512 fi 513 514 # send the signal to stop 515 # 516 echo "Stopping ${name}." 517 _doit="kill -${sig_stop:-TERM} $_pid" 518 if [ -n "$_user" ]; then 519 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 520 fi 521 522 # if the stop cmd failed and force 523 # isn't set, exit 524 # 525 if ! eval $_doit && [ -z "$_rc_force_run" ]; then 526 return 1 527 fi 528 529 # wait for the command to exit, 530 # and run postcmd. 531 wait_for_pids $_pid 532 eval $_postcmd 533 ;; 534 535 reload) 536 if [ -z "$_pid" ]; then 537 if [ -n "$pidfile" ]; then 538 echo \ 539 "${name} not running? (check $pidfile)." 540 else 541 echo "${name} not running?" 542 fi 543 exit 1 544 fi 545 echo "Reloading ${name} config files." 546 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 547 return 1 548 fi 549 _doit="kill -${sig_reload:-HUP} $_pid" 550 if [ -n "$_user" ]; then 551 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 552 fi 553 if ! eval $_doit && [ -z "$_rc_force_run" ]; then 554 return 1 555 fi 556 eval $_postcmd 557 ;; 558 559 restart) 560 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 561 return 1 562 fi 563 # prevent restart being called more 564 # than once by any given script 565 # 566 if [ -n "$_rc_restart_done" ]; then 567 return 0 568 fi 569 _rc_restart_done=YES 570 571 ( $0 ${_rc_force_run:+force}stop ) 572 $0 ${_rc_force_run:+force}start 573 574 eval $_postcmd 575 ;; 576 577 poll) 578 if [ -n "$_pid" ]; then 579 wait_for_pids $_pid 580 fi 581 ;; 582 583 rcvar) 584 echo "# $name" 585 if [ -n "$rcvar" ]; then 586 if checkyesno ${rcvar}; then 587 echo "\$${rcvar}=YES" 588 else 589 echo "\$${rcvar}=NO" 590 fi 591 fi 592 ;; 593 594 *) 595 rc_usage "$_keywords" 596 ;; 597 598 esac 599 return 0 600 done 601 602 echo 1>&2 "$0: unknown directive '$_arg'." 603 rc_usage "$_keywords" 604 exit 1 605} 606 607# 608# run_rc_script file arg 609# Start the script `file' with `arg', and correctly handle the 610# return value from the script. If `file' ends with `.sh', it's 611# sourced into the current environment. If `file' appears to be 612# a backup or scratch file, ignore it. Otherwise if it's 613# executable run as a child process. 614# 615run_rc_script() 616{ 617 _file=$1 618 _arg=$2 619 if [ -z "$_file" -o -z "$_arg" ]; then 620 err 3 'USAGE: run_rc_script file arg' 621 fi 622 623 unset name command command_args extra_commands pidfile procname \ 624 rcvar required_dirs required_files required_vars 625 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 626 627 case "$_file" in 628 *.sh) # run in current shell 629 set $_arg ; . $_file 630 ;; 631 *[~#]|*.OLD|*.orig) # scratch file; skip 632 warn "Ignoring scratch file $_file" 633 ;; 634 *) # run in subshell 635 if [ -x $_file ]; then 636 if [ -n "$rc_fast_and_loose" ]; then 637 set $_arg ; . $_file 638 else 639 ( set $_arg ; . $_file ) 640 fi 641 fi 642 ;; 643 esac 644} 645 646# 647# load_rc_config 648# Source in the configuration file for a given command. 649# 650load_rc_config() 651{ 652 _command=$1 653 if [ -z "$_command" ]; then 654 err 3 'USAGE: load_rc_config command' 655 fi 656 657 if [ -z "$_rc_conf_loaded" ]; then 658 . /etc/rc.conf 659 _rc_conf_loaded=YES 660 fi 661 if [ -f /etc/rc.conf.d/"$_command" ]; then 662 . /etc/rc.conf.d/"$_command" 663 fi 664} 665 666 667# 668# rc_usage commands 669# Print a usage string for $0, with `commands' being a list of 670# valid commands. 671# 672rc_usage() 673{ 674 echo -n 1>&2 "Usage: $0 [fast|force](" 675 676 _sep= 677 for _elem in $*; do 678 echo -n 1>&2 "$_sep$_elem" 679 _sep="|" 680 done 681 echo 1>&2 ")" 682 exit 1 683} 684 685# 686# err exitval message 687# Display message to stderr and log to the syslog, and exit with exitval. 688# 689err() 690{ 691 exitval=$1 692 shift 693 694 logger "$0: ERROR: $*" 695 echo 1>&2 "$0: ERROR: $*" 696 exit $exitval 697} 698 699# 700# warn message 701# Display message to stderr and log to the syslog. 702# 703warn() 704{ 705 logger "$0: WARNING: $*" 706 echo 1>&2 "$0: WARNING: $*" 707} 708 709# 710# backup_file action file cur backup 711# Make a backup copy of `file' into `cur', and save the previous 712# version of `cur' as `backup' or use rcs for archiving. 713# 714# This routine checks the value of the backup_uses_rcs variable, 715# which can be either YES or NO. 716# 717# The `action' keyword can be one of the following: 718# 719# add `file' is now being backed up (and is possibly 720# being reentered into the backups system). `cur' 721# is created and RCS files, if necessary, are 722# created as well. 723# 724# update `file' has changed and needs to be backed up. 725# If `cur' exists, it is copied to to `back' or 726# checked into RCS (if the repository file is old), 727# and then `file' is copied to `cur'. Another RCS 728# check in done here if RCS is being used. 729# 730# remove `file' is no longer being tracked by the backups 731# system. If RCS is not being used, `cur' is moved 732# to `back', otherwise an empty file is checked in, 733# and then `cur' is removed. 734# 735# 736backup_file() 737{ 738 _action=$1 739 _file=$2 740 _cur=$3 741 _back=$4 742 743 if checkyesno backup_uses_rcs; then 744 _msg0="backup archive" 745 _msg1="update" 746 747 # ensure that history file is not locked 748 if [ -f $_cur,v ]; then 749 rcs -q -u -U -M $_cur 750 fi 751 752 # ensure after switching to rcs that the 753 # current backup is not lost 754 if [ -f $_cur ]; then 755 # no archive, or current newer than archive 756 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 757 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 758 rcs -q -kb -U $_cur 759 fi 760 fi 761 762 case $_action in 763 add|update) 764 cp -p $_file $_cur 765 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 766 rcs -q -kb -U $_cur 767 chown root:wheel $_cur $_cur,v 768 ;; 769 remove) 770 cp /dev/null $_cur 771 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 772 rcs -q -kb -U $_cur 773 chown root:wheel $_cur $_cur,v 774 rm $_cur 775 ;; 776 esac 777 else 778 case $_action in 779 add|update) 780 if [ -f $_cur ]; then 781 cp -p $_cur $_back 782 fi 783 cp -p $_file $_cur 784 chown root:wheel $_cur 785 ;; 786 remove) 787 mv -f $_cur $_back 788 ;; 789 esac 790 fi 791} 792