rc.subr revision 1.36
1# $NetBSD: rc.subr,v 1.36 2001/05/10 14:04:22 atatat 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# 72# mount_critical_filesystems 73# Go through the list of critical filesystems, checking each one 74# to see if it is mounted, and if it is not, mounting it. 75# 76mount_critical_filesystems() 77{ 78 if [ $1 = local ]; then 79 _fslist=$critical_filesystems_beforenet 80 else 81 _fslist=$critical_filesystems 82 fi 83 for _fs in $_fslist; do 84 mount | ( 85 _ismounted=no 86 while read what _on on _type type; do 87 if [ $on = $_fs ]; then 88 _ismounted=yes 89 fi 90 done 91 if [ $_ismounted = no ]; then 92 mount $_fs >/dev/null 2>&1 93 fi 94 ) 95 done 96} 97 98# 99# check_pidfile pidfile procname 100# Parses the first line of pidfile for a pid, and ensures 101# that the process is running and matches procname. 102# Prints the matching pid upon success, nothing otherwise. 103# 104check_pidfile() 105{ 106 _pidfile=$1 107 _procname=$2 108 if [ -z "$_pidfile" -o -z "$_procname" ]; then 109 err 3 'USAGE: check_pidfile pidfile procname' 110 fi 111 if [ ! -f $_pidfile ]; then 112 return 113 fi 114 read _pid _junk < $_pidfile 115 if [ -z "$_pid" ]; then 116 return 117 fi 118 _procnamebn=${_procname##*/} 119 ps -p $_pid -o 'pid,command' | while read _npid _arg0 _argv; do 120 case "$_npid" in 121 PID) 122 continue ;; 123 esac 124 case "$_arg0" in 125 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})") 126 echo $_npid 127 return 128 ;; 129 esac 130 done 131} 132 133# 134# check_process procname 135# Ensures that a process (or processes) named procname is running. 136# Prints a list of matching pids. 137# 138check_process() 139{ 140 _procname=$1 141 if [ -z "$_procname" ]; then 142 err 3 'USAGE: check_process procname' 143 fi 144 _procnamebn=${_procname##*/} 145 _pref= 146 ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do 147 case "$_npid" in 148 PID) 149 continue ;; 150 esac 151 case "$_arg0" in 152 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})") 153 echo -n "$_pref$_npid" 154 _pref=" " 155 ;; 156 esac 157 done 158} 159 160# 161# wait_for_pids pid [pid ...] 162# spins until none of the pids exist 163# 164wait_for_pids() 165{ 166 _list=$* 167 if [ -z "$_list" ]; then 168 return 169 fi 170 _prefix= 171 while true; do 172 _nlist=""; 173 for _j in $_list; do 174 if kill -0 $_j 2>/dev/null; then 175 _nlist="${_nlist}${_nlist:+ }$_j" 176 fi 177 done 178 if [ -z "$_nlist" ]; then 179 break 180 fi 181 _list=$_nlist 182 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 183 _prefix=", " 184 sleep 2 185 done 186 if [ -n "$_prefix" ]; then 187 echo "." 188 fi 189} 190 191# 192# run_rc_command arg 193# Search for arg in the list of supported commands, which is: 194# "start stop restart rcvar status poll ${extra_commands}" 195# If there's a match, run ${arg}_cmd or the default command (see below). 196# 197# If arg has a given prefix, then change the operation as follows: 198# prefix operation 199# ------ --------- 200# fast Skip the pid check. 201# force Set ${rcvar} to YES. 202# 203# The following globals are used: 204# 205# name needed function 206# ---- ------ -------- 207# name y Name of script. 208# 209# command n Full path to command. 210# Not needed if ${arg}_cmd is set for 211# each keyword. 212# 213# command_args n Optional args/shell directives for command. 214# 215# extra_commands n List of extra commands supported. 216# 217# pidfile n If set, use check_pidfile $pidfile, else if 218# $command is set, use check_process $command. 219# 220# rcvar n This is checked with checkyesno to determine 221# if the action should be run. 222# 223# ${name}_chroot n Directory to chroot to before running ${command} 224# 225# ${name}_chdir n Directory to cd to before running ${command} 226# (if not using ${name}_chroot). 227# 228# ${name}_flags n Arguments to call ${command} with. 229# NOTE: $flags from the parent environment 230# can be used to override this. 231# 232# ${name}_nice n Nice level to run ${command} at. 233# 234# ${name}_user n User to run ${command} as, using su(1) if not 235# using ${name}_chroot. 236# 237# ${name}_group n Group to run chrooted ${command} as. 238# 239# ${name}_groups n Comma separated list of supplementary groups 240# to run the chrooted ${command} with. 241# 242# ${_arg}_cmd n If set, use this as the action when invoked; 243# $_arg is available to the action to use. 244# Otherwise, use default command (see below) 245# 246# ${_arg}_precmd n If set, run just before performing the main 247# action in the default command (i.e, after 248# checking for required bits and process 249# (non)existance). 250# If this completes with a non-zero exit code, 251# don't run ${_arg}_cmd. 252# 253# required_dirs n If set, check for the existence of the given 254# directories before running the default 255# (re)start command. 256# 257# required_files n If set, check for the readability of the given 258# files before running the default (re)start 259# command. 260# 261# required_vars n If set, perform checkyesno on each of the 262# listed variables before running the default 263# (re)start command. 264# 265# Default commands for a given arg: 266# 267# arg default 268# --- ------- 269# start if !running && checkyesno ${rcvar} 270# ${command} 271# 272# stop if ${pidfile} 273# _pid=`check_pidfile $pidfile` 274# else 275# _pid=`check_process $command` 276# kill $sig_stop $_pid 277# wait_for_pids $_pid 278# ($sig_stop defaults to TERM.) 279# 280# reload Similar to stop, except use $sig_reload instead, 281# and doesn't wait_for_pids. 282# $sig_reload defaults to HUP. 283# 284# restart Run `stop' then `start'. 285# 286# status Show if ${command} is running, etc. 287# 288# poll Wait for ${command} to exit. 289# 290# rcvar Display what rc.conf variable is used (if any). 291# 292# 293# 294run_rc_command() 295{ 296 _arg=$1 297 if [ -z "$name" ]; then 298 err 3 'run_rc_command: $name is not set.' 299 fi 300 301 case "$_arg" in 302 fast*) # "fast" prefix; don't check pid 303 _arg=${_arg#fast} 304 _rc_fast_run=YES 305 ;; 306 force*) # "force prefix; always start 307 _arg=${_arg#force} 308 _rc_force_run=YES 309 if [ -n "${rcvar}" ]; then 310 eval ${rcvar}=YES 311 fi 312 ;; 313 esac 314 315 _keywords="start stop restart rcvar $extra_commands" 316 _pid= 317 _pidcmd= 318 # setup pid check command if not fast 319 if [ -z "$_rc_fast_run" ]; then 320 if [ -n "$pidfile" ]; then 321 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 322 elif [ -n "$command" ]; then 323 _pidcmd='_pid=`check_process '$command'`' 324 fi 325 if [ -n "$_pidcmd" ]; then 326 _keywords="${_keywords} status poll" 327 fi 328 fi 329 330 if [ -z "$_arg" ]; then 331 rc_usage "$_keywords" 332 fi 333 334 if [ -n "$flags" ]; then # allow override from environment 335 _flags=$flags 336 else 337 eval _flags=\$${name}_flags 338 fi 339 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 340 _nice=\$${name}_nice _user=\$${name}_user \ 341 _group=\$${name}_group _groups=\$${name}_groups 342 343 # if ${rcvar} is set, and $1 is not 344 # "rcvar" or "status", then run 345 # checkyesno ${rcvar} 346 # and return if that failed 347 # 348 # XXXX use case? 349 if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then 350 if ! checkyesno ${rcvar}; then 351 return 0 352 fi 353 fi 354 355 eval $_pidcmd # determine the pid if necessary 356 357 for _elem in $_keywords; do 358 if [ "$_elem" != "$_arg" ]; then 359 continue 360 fi 361 362 # if there's a custom ${XXX_cmd}, 363 # run that instead of the default 364 # 365 eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd 366 if [ -n "$_cmd" ]; then 367 # if the precmd failed and force 368 # isn't set, exit 369 # 370 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 371 return 1 372 fi 373 374 eval $_cmd 375 return 0 376 fi 377 378 case "$_arg" in # default operations... 379 380 status) 381 if [ -n "$_pid" ]; then 382 echo "${name} is running as pid $_pid." 383 else 384 echo "${name} is not running." 385 return 1 386 fi 387 ;; 388 389 start) 390 if [ -n "$_pid" ]; then 391 echo "${name} already running? (pid=$_pid)." 392 exit 1 393 fi 394 395 if [ ! -x $command ]; then 396 return 0 397 fi 398 399 # check for required variables, 400 # directories, and files 401 # 402 for _f in $required_vars; do 403 if ! checkyesno $_f; then 404 warn "\$${_f} is not set." 405 if [ -z "$_rc_force_run" ]; then 406 return 1 407 fi 408 fi 409 done 410 for _f in $required_dirs; do 411 if [ ! -d "${_f}/." ]; then 412 warn "${_f} is not a directory." 413 if [ -z "$_rc_force_run" ]; then 414 return 1 415 fi 416 fi 417 done 418 for _f in $required_files; do 419 if [ ! -r "${_f}" ]; then 420 warn "${_f} is not readable." 421 if [ -z "$_rc_force_run" ]; then 422 return 1 423 fi 424 fi 425 done 426 427 # if the precmd failed and force 428 # isn't set, exit 429 # 430 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 431 return 1 432 fi 433 434 435 # setup the command to run, and run it 436 # 437 echo "Starting ${name}." 438 if [ -n "$_chroot" ]; then 439 _doit="\ 440${_nice:+nice -n $_nice }\ 441chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 442$_chroot $command $_flags $command_args" 443 else 444 _doit="\ 445${_chdir:+cd $_chdir; }\ 446${_nice:+nice -n $_nice }\ 447$command $_flags $command_args" 448 if [ -n "$_user" ]; then 449 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 450 fi 451 fi 452 eval $_doit 453 ;; 454 455 stop) 456 if [ -z "$_pid" ]; then 457 if [ -n "$pidfile" ]; then 458 echo \ 459 "${name} not running? (check $pidfile)." 460 else 461 echo "${name} not running?" 462 fi 463 exit 1 464 fi 465 466 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 467 return 1 468 fi 469 echo "Stopping ${name}." 470 _doit="kill -${sig_stop:-TERM} $_pid" 471 if [ -n "$_user" ]; then 472 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 473 fi 474 eval $_doit 475 wait_for_pids $_pid 476 ;; 477 478 reload) 479 if [ -z "$_pid" ]; then 480 if [ -n "$pidfile" ]; then 481 echo \ 482 "${name} not running? (check $pidfile)." 483 else 484 echo "${name} not running?" 485 fi 486 exit 1 487 fi 488 echo "Reloading ${name} config files." 489 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 490 return 1 491 fi 492 _doit="kill -${sig_reload:-HUP} $_pid" 493 if [ -n "$_user" ]; then 494 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 495 fi 496 eval $_doit 497 ;; 498 499 restart) 500 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 501 return 1 502 fi 503 # prevent restart being called more 504 # than once by any given script 505 # 506 if [ -n "$_rc_restart_done" ]; then 507 return 0 508 fi 509 _rc_restart_done=YES 510 511 ( $0 ${_rc_force_run:+force}stop ) 512 $0 ${_rc_force_run:+force}start 513 514 ;; 515 516 poll) 517 if [ -n "$_pid" ]; then 518 wait_for_pids $_pid 519 fi 520 ;; 521 522 rcvar) 523 echo "# $name" 524 if [ -n "$rcvar" ]; then 525 if checkyesno ${rcvar}; then 526 echo "\$${rcvar}=YES" 527 else 528 echo "\$${rcvar}=NO" 529 fi 530 fi 531 ;; 532 533 *) 534 rc_usage "$_keywords" 535 ;; 536 537 esac 538 return 0 539 done 540 541 echo 1>&2 "$0: unknown directive '$_arg'." 542 rc_usage "$_keywords" 543 exit 1 544} 545 546# 547# run_rc_script file arg 548# Start the script `file' with `arg', and correctly handle the 549# return value from the script. If `file' ends with `.sh', it's 550# sourced into the current environment. Otherwise it's run as 551# a child process. 552# 553# Note: because `.sh' files are sourced into the current environment 554# run_rc_command shouldn't be used because its difficult to ensure 555# that the global variable state before and after the sourcing of 556# the .sh file won't adversely affect other scripts. 557# 558run_rc_script() 559{ 560 _file=$1 561 _arg=$2 562 if [ -z "$_file" -o -z "$_arg" ]; then 563 err 3 'USAGE: run_rc_script file arg' 564 fi 565 566 if [ -n "$rc_fast_and_loose" ]; then 567 unset name command command_args extra_commands pidfile rcvar 568 unset required_dirs required_files required_vars 569 eval unset ${_arg}_cmd ${_arg}_precmd 570 set $_arg ; . $_file 571 else 572 case "$_file" in 573 *.sh) # run in current shell 574 set $_arg ; . $_file 575 ;; 576 *) # run in subshell 577 ( set $_arg ; . $_file ) 578 ;; 579 esac 580 fi 581} 582 583# 584# load_rc_config 585# Source in the configuration file for a given command. 586# 587load_rc_config() 588{ 589 _command=$1 590 if [ -z "$_command" ]; then 591 err 3 'USAGE: load_rc_config command' 592 fi 593 594 if [ -z "$_rc_conf_loaded" ]; then 595 . /etc/rc.conf 596 _rc_conf_loaded=YES 597 fi 598 if [ -f /etc/rc.conf.d/"$_command" ]; then 599 . /etc/rc.conf.d/"$_command" 600 fi 601} 602 603 604# 605# rc_usage commands 606# Print a usage string for $0, with `commands' being a list of 607# valid commands. 608# 609rc_usage() 610{ 611 echo -n 1>&2 "Usage: $0 [fast|force](" 612 613 _sep= 614 for _elem in $*; do 615 echo -n 1>&2 "$_sep$_elem" 616 _sep="|" 617 done 618 echo 1>&2 ")" 619 exit 1 620} 621 622# 623# err exitval message 624# Display message to stderr and log to the syslog, and exit with exitval. 625# 626err() 627{ 628 exitval=$1 629 shift 630 631 logger "$0: ERROR: $*" 632 echo 1>&2 "$0: ERROR: $*" 633 exit $exitval 634} 635 636# 637# warn message 638# Display message to stderr and log to the syslog. 639# 640warn() 641{ 642 logger "$0: WARNING: $*" 643 echo 1>&2 "$0: WARNING: $*" 644} 645 646# 647# backup_file action file cur backup 648# Make a backup copy of `file' into `cur', and save the previous 649# version of `cur' as `backup' or use rcs for archiving. 650# 651# This routine checks the value of the backup_uses_rcs variable, 652# which can be either YES or NO. 653# 654# The `action' keyword can be one of the following: 655# 656# add `file' is now being backed up (and is possibly 657# being reentered into the backups system). `cur' 658# is created and RCS files, if necessary, are 659# created as well. 660# 661# update `file' has changed and needs to be backed up. 662# If `cur' exists, it is copied to to `back' or 663# checked into RCS (if the repository file is old), 664# and then `file' is copied to `cur'. Another RCS 665# check in done here if RCS is being used. 666# 667# remove `file' is no longer being tracked by the backups 668# system. If RCS is not being used, `cur' is moved 669# to `back', otherwise an empty file is checked in, 670# and then `cur' is removed. 671# 672# 673backup_file() 674{ 675 _action=$1 676 _file=$2 677 _cur=$3 678 _back=$4 679 680 if checkyesno backup_uses_rcs; then 681 _msg0="backup archive" 682 _msg1="update" 683 684 # ensure that history file is not locked 685 if [ -f $_cur,v ]; then 686 rcs -q -u -U -M $_cur 687 fi 688 689 # ensure after switching to rcs that the 690 # current backup is not lost 691 if [ -f $_cur ]; then 692 # no archive, or current newer than archive 693 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 694 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 695 rcs -q -kb -U $_cur 696 fi 697 fi 698 699 case $_action in 700 add|update) 701 cp -p $_file $_cur 702 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 703 rcs -q -kb -U $_cur 704 chown root:wheel $_cur $_cur,v 705 ;; 706 remove) 707 cp /dev/null $_cur 708 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 709 rcs -q -kb -U $_cur 710 chown root:wheel $_cur $_cur,v 711 rm $_cur 712 ;; 713 esac 714 else 715 case $_action in 716 add|update) 717 if [ -f $_cur ]; then 718 cp -p $_cur $_back 719 fi 720 cp -p $_file $_cur 721 chown root:wheel $_cur 722 ;; 723 remove) 724 mv -f $_cur $_back 725 ;; 726 esac 727 fi 728} 729