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