rc.subr revision 1.39
1# $NetBSD: rc.subr,v 1.39 2002/02/25 06:58:14 lukem Exp $ 2# 3# Copyright (c) 1997-2000 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, else if 230# $command is set, use check_process $command. 231# 232# rcvar n This is checked with checkyesno to determine 233# if the action should be run. 234# 235# ${name}_chroot n Directory to chroot to before running ${command} 236# 237# ${name}_chdir n Directory to cd to before running ${command} 238# (if not using ${name}_chroot). 239# 240# ${name}_flags n Arguments to call ${command} with. 241# NOTE: $flags from the parent environment 242# can be used to override this. 243# 244# ${name}_nice n Nice level to run ${command} at. 245# 246# ${name}_user n User to run ${command} as, using su(1) if not 247# using ${name}_chroot. 248# 249# ${name}_group n Group to run chrooted ${command} as. 250# 251# ${name}_groups n Comma separated list of supplementary groups 252# to run the chrooted ${command} with. 253# 254# ${_arg}_cmd n If set, use this as the action when invoked; 255# $_arg is available to the action to use. 256# Otherwise, use default command (see below) 257# 258# ${_arg}_precmd n If set, run just before performing the main 259# action in the default command (i.e, after 260# checking for required bits and process 261# (non)existance). 262# If this completes with a non-zero exit code, 263# don't run ${_arg}_cmd. 264# 265# required_dirs n If set, check for the existence of the given 266# directories before running the default 267# (re)start command. 268# 269# required_files n If set, check for the readability of the given 270# files before running the default (re)start 271# command. 272# 273# required_vars n If set, perform checkyesno on each of the 274# listed variables before running the default 275# (re)start command. 276# 277# Default commands for a given arg: 278# 279# arg default 280# --- ------- 281# start if !running && checkyesno ${rcvar} 282# ${command} 283# 284# stop if ${pidfile} 285# _pid=`check_pidfile $pidfile` 286# else 287# _pid=`check_process $command` 288# kill $sig_stop $_pid 289# wait_for_pids $_pid 290# ($sig_stop defaults to TERM.) 291# 292# reload Similar to stop, except use $sig_reload instead, 293# and doesn't wait_for_pids. 294# $sig_reload defaults to HUP. 295# 296# restart Run `stop' then `start'. 297# 298# status Show if ${command} is running, etc. 299# 300# poll Wait for ${command} to exit. 301# 302# rcvar Display what rc.conf variable is used (if any). 303# 304# 305# 306run_rc_command() 307{ 308 _arg=$1 309 if [ -z "$name" ]; then 310 err 3 'run_rc_command: $name is not set.' 311 fi 312 313 case "$_arg" in 314 fast*) # "fast" prefix; don't check pid 315 _arg=${_arg#fast} 316 _rc_fast_run=YES 317 ;; 318 force*) # "force prefix; always start 319 _arg=${_arg#force} 320 _rc_force_run=YES 321 if [ -n "${rcvar}" ]; then 322 eval ${rcvar}=YES 323 fi 324 ;; 325 esac 326 327 _keywords="start stop restart rcvar $extra_commands" 328 _pid= 329 _pidcmd= 330 # setup pid check command if not fast 331 if [ -z "$_rc_fast_run" ]; then 332 if [ -n "$pidfile" ]; then 333 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 334 elif [ -n "$command" ]; then 335 _pidcmd='_pid=`check_process '$command'`' 336 fi 337 if [ -n "$_pidcmd" ]; then 338 _keywords="${_keywords} status poll" 339 fi 340 fi 341 342 if [ -z "$_arg" ]; then 343 rc_usage "$_keywords" 344 fi 345 346 if [ -n "$flags" ]; then # allow override from environment 347 _flags=$flags 348 else 349 eval _flags=\$${name}_flags 350 fi 351 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 352 _nice=\$${name}_nice _user=\$${name}_user \ 353 _group=\$${name}_group _groups=\$${name}_groups 354 355 # if ${rcvar} is set, and $1 is not 356 # "rcvar" or "status", then run 357 # checkyesno ${rcvar} 358 # and return if that failed 359 # 360 # XXXX use case? 361 if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then 362 if ! checkyesno ${rcvar}; then 363 return 0 364 fi 365 fi 366 367 eval $_pidcmd # determine the pid if necessary 368 369 for _elem in $_keywords; do 370 if [ "$_elem" != "$_arg" ]; then 371 continue 372 fi 373 374 # if there's a custom ${XXX_cmd}, 375 # run that instead of the default 376 # 377 eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd 378 if [ -n "$_cmd" ]; then 379 # if the precmd failed and force 380 # isn't set, exit 381 # 382 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 383 return 1 384 fi 385 386 eval $_cmd 387 return 0 388 fi 389 390 case "$_arg" in # default operations... 391 392 status) 393 if [ -n "$_pid" ]; then 394 echo "${name} is running as pid $_pid." 395 else 396 echo "${name} is not running." 397 return 1 398 fi 399 ;; 400 401 start) 402 if [ -n "$_pid" ]; then 403 echo "${name} already running? (pid=$_pid)." 404 exit 1 405 fi 406 407 if [ ! -x $command ]; then 408 return 0 409 fi 410 411 # check for required variables, 412 # directories, and files 413 # 414 for _f in $required_vars; do 415 if ! checkyesno $_f; then 416 warn "\$${_f} is not set." 417 if [ -z "$_rc_force_run" ]; then 418 return 1 419 fi 420 fi 421 done 422 for _f in $required_dirs; do 423 if [ ! -d "${_f}/." ]; then 424 warn "${_f} is not a directory." 425 if [ -z "$_rc_force_run" ]; then 426 return 1 427 fi 428 fi 429 done 430 for _f in $required_files; do 431 if [ ! -r "${_f}" ]; then 432 warn "${_f} is not readable." 433 if [ -z "$_rc_force_run" ]; then 434 return 1 435 fi 436 fi 437 done 438 439 # if the precmd failed and force 440 # isn't set, exit 441 # 442 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 443 return 1 444 fi 445 446 447 # setup the command to run, and run it 448 # 449 echo "Starting ${name}." 450 if [ -n "$_chroot" ]; then 451 _doit="\ 452${_nice:+nice -n $_nice }\ 453chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 454$_chroot $command $_flags $command_args" 455 else 456 _doit="\ 457${_chdir:+cd $_chdir; }\ 458${_nice:+nice -n $_nice }\ 459$command $_flags $command_args" 460 if [ -n "$_user" ]; then 461 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 462 fi 463 fi 464 eval $_doit 465 ;; 466 467 stop) 468 if [ -z "$_pid" ]; then 469 if [ -n "$pidfile" ]; then 470 echo \ 471 "${name} not running? (check $pidfile)." 472 else 473 echo "${name} not running?" 474 fi 475 exit 1 476 fi 477 478 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 479 return 1 480 fi 481 echo "Stopping ${name}." 482 _doit="kill -${sig_stop:-TERM} $_pid" 483 if [ -n "$_user" ]; then 484 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 485 fi 486 eval $_doit 487 wait_for_pids $_pid 488 ;; 489 490 reload) 491 if [ -z "$_pid" ]; then 492 if [ -n "$pidfile" ]; then 493 echo \ 494 "${name} not running? (check $pidfile)." 495 else 496 echo "${name} not running?" 497 fi 498 exit 1 499 fi 500 echo "Reloading ${name} config files." 501 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 502 return 1 503 fi 504 _doit="kill -${sig_reload:-HUP} $_pid" 505 if [ -n "$_user" ]; then 506 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 507 fi 508 eval $_doit 509 ;; 510 511 restart) 512 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 513 return 1 514 fi 515 # prevent restart being called more 516 # than once by any given script 517 # 518 if [ -n "$_rc_restart_done" ]; then 519 return 0 520 fi 521 _rc_restart_done=YES 522 523 ( $0 ${_rc_force_run:+force}stop ) 524 $0 ${_rc_force_run:+force}start 525 526 ;; 527 528 poll) 529 if [ -n "$_pid" ]; then 530 wait_for_pids $_pid 531 fi 532 ;; 533 534 rcvar) 535 echo "# $name" 536 if [ -n "$rcvar" ]; then 537 if checkyesno ${rcvar}; then 538 echo "\$${rcvar}=YES" 539 else 540 echo "\$${rcvar}=NO" 541 fi 542 fi 543 ;; 544 545 *) 546 rc_usage "$_keywords" 547 ;; 548 549 esac 550 return 0 551 done 552 553 echo 1>&2 "$0: unknown directive '$_arg'." 554 rc_usage "$_keywords" 555 exit 1 556} 557 558# 559# run_rc_script file arg 560# Start the script `file' with `arg', and correctly handle the 561# return value from the script. If `file' ends with `.sh', it's 562# sourced into the current environment. If `file' appears to be 563# a backup or scratch file, ignore it. Otherwise if it's 564# executable run as a child process. 565# 566run_rc_script() 567{ 568 _file=$1 569 _arg=$2 570 if [ -z "$_file" -o -z "$_arg" ]; then 571 err 3 'USAGE: run_rc_script file arg' 572 fi 573 574 unset name command command_args extra_commands pidfile rcvar \ 575 required_dirs required_files required_vars 576 eval unset ${_arg}_cmd ${_arg}_precmd 577 578 case "$_file" in 579 *.sh) # run in current shell 580 set $_arg ; . $_file 581 ;; 582 *[~#]|*.OLD|*.orig) # scratch file; skip 583 warn "Ignoring scratch file $_file" 584 ;; 585 *) # run in subshell 586 if [ -x $_file ]; then 587 if [ -n "$rc_fast_and_loose" ]; then 588 set $_arg ; . $_file 589 else 590 ( set $_arg ; . $_file ) 591 fi 592 fi 593 ;; 594 esac 595} 596 597# 598# load_rc_config 599# Source in the configuration file for a given command. 600# 601load_rc_config() 602{ 603 _command=$1 604 if [ -z "$_command" ]; then 605 err 3 'USAGE: load_rc_config command' 606 fi 607 608 if [ -z "$_rc_conf_loaded" ]; then 609 . /etc/rc.conf 610 _rc_conf_loaded=YES 611 fi 612 if [ -f /etc/rc.conf.d/"$_command" ]; then 613 . /etc/rc.conf.d/"$_command" 614 fi 615} 616 617 618# 619# rc_usage commands 620# Print a usage string for $0, with `commands' being a list of 621# valid commands. 622# 623rc_usage() 624{ 625 echo -n 1>&2 "Usage: $0 [fast|force](" 626 627 _sep= 628 for _elem in $*; do 629 echo -n 1>&2 "$_sep$_elem" 630 _sep="|" 631 done 632 echo 1>&2 ")" 633 exit 1 634} 635 636# 637# err exitval message 638# Display message to stderr and log to the syslog, and exit with exitval. 639# 640err() 641{ 642 exitval=$1 643 shift 644 645 logger "$0: ERROR: $*" 646 echo 1>&2 "$0: ERROR: $*" 647 exit $exitval 648} 649 650# 651# warn message 652# Display message to stderr and log to the syslog. 653# 654warn() 655{ 656 logger "$0: WARNING: $*" 657 echo 1>&2 "$0: WARNING: $*" 658} 659 660# 661# backup_file action file cur backup 662# Make a backup copy of `file' into `cur', and save the previous 663# version of `cur' as `backup' or use rcs for archiving. 664# 665# This routine checks the value of the backup_uses_rcs variable, 666# which can be either YES or NO. 667# 668# The `action' keyword can be one of the following: 669# 670# add `file' is now being backed up (and is possibly 671# being reentered into the backups system). `cur' 672# is created and RCS files, if necessary, are 673# created as well. 674# 675# update `file' has changed and needs to be backed up. 676# If `cur' exists, it is copied to to `back' or 677# checked into RCS (if the repository file is old), 678# and then `file' is copied to `cur'. Another RCS 679# check in done here if RCS is being used. 680# 681# remove `file' is no longer being tracked by the backups 682# system. If RCS is not being used, `cur' is moved 683# to `back', otherwise an empty file is checked in, 684# and then `cur' is removed. 685# 686# 687backup_file() 688{ 689 _action=$1 690 _file=$2 691 _cur=$3 692 _back=$4 693 694 if checkyesno backup_uses_rcs; then 695 _msg0="backup archive" 696 _msg1="update" 697 698 # ensure that history file is not locked 699 if [ -f $_cur,v ]; then 700 rcs -q -u -U -M $_cur 701 fi 702 703 # ensure after switching to rcs that the 704 # current backup is not lost 705 if [ -f $_cur ]; then 706 # no archive, or current newer than archive 707 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 708 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 709 rcs -q -kb -U $_cur 710 fi 711 fi 712 713 case $_action in 714 add|update) 715 cp -p $_file $_cur 716 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 717 rcs -q -kb -U $_cur 718 chown root:wheel $_cur $_cur,v 719 ;; 720 remove) 721 cp /dev/null $_cur 722 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 723 rcs -q -kb -U $_cur 724 chown root:wheel $_cur $_cur,v 725 rm $_cur 726 ;; 727 esac 728 else 729 case $_action in 730 add|update) 731 if [ -f $_cur ]; then 732 cp -p $_cur $_back 733 fi 734 cp -p $_file $_cur 735 chown root:wheel $_cur 736 ;; 737 remove) 738 mv -f $_cur $_back 739 ;; 740 esac 741 fi 742} 743