rc.subr revision 1.30
1# $NetBSD: rc.subr,v 1.30 2001/02/28 16:49:19 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# run_rc_command arg 162# Search for arg in the list of supported commands, which is: 163# "start stop restart rcvar status ${extra_commands}" 164# If there's a match, run ${arg}_cmd or the default command (see below). 165# 166# If arg has a given prefix, then change the operation as follows: 167# prefix operation 168# ------ --------- 169# fast Skip the pid check. 170# force Set ${rcvar} to YES. 171# 172# The following globals are used: 173# 174# name needed function 175# ---- ------ -------- 176# name y Name of script. 177# 178# command n Full path to command. 179# Not needed if ${arg}_cmd is set for 180# each keyword. 181# 182# command_args n Optional args/shell directives for command. 183# 184# extra_commands n List of extra commands supported. 185# 186# pidfile n If set, use check_pidfile $pidfile, else if 187# $command is set, use check_process $command. 188# 189# rcvar n This is checked with checkyesno to determine 190# if the action should be run. 191# 192# ${name}_chroot n Directory to chroot to before running ${command} 193# 194# ${name}_chdir n Directory to cd to before running ${command} 195# (if not using ${name}_chroot). 196# 197# ${name}_flags n Arguments to call ${command} with. 198# NOTE: $flags from the parent environment 199# can be used to override this. 200# 201# ${name}_nice n Nice level to run ${command} at. 202# 203# ${name}_user n User to run ${command} as, using su(1) if not 204# using ${name}_chroot. 205# 206# ${name}_group n Group to run chrooted ${command} as. 207# 208# ${name}_groups n Supplementary group list to run chrooted 209# ${command} with. 210# 211# ${_arg}_cmd n If set, use this as the action when invoked; 212# $_arg is available to the action to use. 213# Otherwise, use default command (see below) 214# 215# ${_arg}_precmd n If set, run just before performing the main 216# action in the default command (i.e, after 217# checking for required bits and process 218# (non)existance). 219# If this completes with a non-zero exit code, 220# don't run ${_arg}_cmd. 221# 222# required_dirs n If set, check for the existence of the given 223# directories before running the default 224# (re)start command. 225# 226# required_files n If set, check for the readability of the given 227# files before running the default (re)start 228# command. 229# 230# required_vars n If set, perform checkyesno on each of the 231# listed variables before running the default 232# (re)start command. 233# 234# Default commands for a given arg: 235# 236# arg default 237# --- ------- 238# status Show if ${command} is running, etc. 239# 240# start if !running && checkyesno ${rcvar} 241# ${command} 242# 243# stop if ${pidfile} 244# kill $sig_stop `check_pidfile $pidfile` 245# else 246# kill $sig_stop `check_process $command` 247# $sig_stop defaults to TERM. 248# 249# reload As stop, except use $sig_reload instead. 250# $sig_reload defaults to HUP. 251# 252# restart Run `stop' then `start'. 253# 254# 255run_rc_command() 256{ 257 _arg=$1 258 if [ -z "$name" ]; then 259 err 3 'run_rc_command: $name is not set.' 260 fi 261 262 case "$_arg" in 263 fast*) # "fast" prefix; don't check pid 264 _arg=${_arg#fast} 265 _rc_fast_run=YES 266 ;; 267 force*) # "force prefix; always start 268 _arg=${_arg#force} 269 _rc_force_run=YES 270 if [ -n "${rcvar}" ]; then 271 eval ${rcvar}=YES 272 fi 273 ;; 274 esac 275 276 _keywords="start stop restart rcvar $extra_commands" 277 _pid= 278 _pidcmd= 279 # setup pid check command if not fast 280 if [ -z "$_rc_fast_run" ]; then 281 if [ -n "$pidfile" ]; then 282 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 283 elif [ -n "$command" ]; then 284 _pidcmd='_pid=`check_process '$command'`' 285 fi 286 if [ -n "$_pidcmd" ]; then 287 _keywords="${_keywords} status" 288 fi 289 fi 290 291 if [ -z "$_arg" ]; then 292 rc_usage "$_keywords" 293 fi 294 295 if [ -n "$flags" ]; then # allow override from environment 296 _flags=$flags 297 else 298 eval _flags=\$${name}_flags 299 fi 300 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 301 _nice=\$${name}_nice _user=\$${name}_user \ 302 _group=\$${name}_group _groups=\$${name}_groups 303 304 # if ${rcvar} is set, and $1 is not 305 # "rcvar" or "status", then run 306 # checkyesno ${rcvar} 307 # and return if that failed 308 # 309 # XXXX use case? 310 if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then 311 if ! checkyesno ${rcvar}; then 312 return 0 313 fi 314 fi 315 316 eval $_pidcmd # determine the pid if necessary 317 318 for _elem in $_keywords; do 319 if [ "$_elem" != "$_arg" ]; then 320 continue 321 fi 322 323 # if there's a custom ${XXX_cmd}, 324 # run that instead of the default 325 # 326 eval _cmd=\$${_arg}_cmd _precmd=\$${_arg}_precmd 327 if [ -n "$_cmd" ]; then 328 # if the precmd failed and force 329 # isn't set, exit 330 # 331 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 332 return 1 333 fi 334 335 eval $_cmd 336 return 0 337 fi 338 339 case "$_arg" in # default operations... 340 341 status) 342 if [ -n "$_pid" ]; then 343 echo "${name} is running as pid $_pid." 344 else 345 echo "${name} is not running." 346 return 1 347 fi 348 ;; 349 350 start) 351 if [ -n "$_pid" ]; then 352 echo "${name} already running? (pid=$_pid)." 353 exit 1 354 fi 355 356 if [ ! -x $command ]; then 357 return 0 358 fi 359 360 # check for required variables, 361 # directories, and files 362 # 363 for _f in $required_vars; do 364 if ! checkyesno $_f; then 365 warn "\$${_f} is not set." 366 if [ -z "$_rc_force_run" ]; then 367 return 1 368 fi 369 fi 370 done 371 for _f in $required_dirs; do 372 if [ ! -d "${_f}/." ]; then 373 warn "${_f} is not a directory." 374 if [ -z "$_rc_force_run" ]; then 375 return 1 376 fi 377 fi 378 done 379 for _f in $required_files; do 380 if [ ! -r "${_f}" ]; then 381 warn "${_f} is not readable." 382 if [ -z "$_rc_force_run" ]; then 383 return 1 384 fi 385 fi 386 done 387 388 # if the precmd failed and force 389 # isn't set, exit 390 # 391 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 392 return 1 393 fi 394 395 396 # setup the command to run, and run it 397 # 398 echo "Starting ${name}." 399 if [ -n "$_chroot" ]; then 400 _doit="\ 401${_nice:+nice -n $_nice }\ 402chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 403$_chroot $command $_flags $command_args" 404 else 405 _doit="\ 406${_user:+su -m $_user -c 'sh -c \"}\ 407${_chdir:+cd $_chdir; }\ 408${_nice:+nice -n $_nice }\ 409$command $_flags $command_args\ 410${_user:+\"'}" 411 fi 412 eval $_doit 413 ;; 414 415 stop) 416 if [ -z "$_pid" ]; then 417 if [ -n "$pidfile" ]; then 418 echo \ 419 "${name} not running? (check $pidfile)." 420 else 421 echo "${name} not running?" 422 fi 423 exit 1 424 fi 425 426 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 427 return 1 428 fi 429 echo "Stopping ${name}." 430 _doit=\ 431"${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}" 432 eval $_doit 433 ;; 434 435 reload) 436 if [ -z "$_pid" ]; then 437 if [ -n "$pidfile" ]; then 438 echo \ 439 "${name} not running? (check $pidfile)." 440 else 441 echo "${name} not running?" 442 fi 443 exit 1 444 fi 445 echo "Reloading ${name} config files." 446 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 447 return 1 448 fi 449 _doit=\ 450"${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}" 451 eval $_doit 452 ;; 453 454 restart) 455 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 456 return 1 457 fi 458 # prevent restart being called more 459 # than once by any given script 460 # 461 if [ -n "$_rc_restart_done" ]; then 462 return 0 463 fi 464 _rc_restart_done=YES 465 ( $0 ${_rc_force_run:+force}stop ) 466 sleep 1 467 $0 ${_rc_force_run:+force}start 468 469 ;; 470 471 rcvar) 472 echo "# $name" 473 if [ -n "$rcvar" ]; then 474 if checkyesno ${rcvar}; then 475 echo "\$${rcvar}=YES" 476 else 477 echo "\$${rcvar}=NO" 478 fi 479 fi 480 ;; 481 482 *) 483 rc_usage "$_keywords" 484 ;; 485 486 esac 487 return 0 488 done 489 490 echo 1>&2 "$0: unknown directive '$_arg'." 491 rc_usage "$_keywords" 492 exit 1 493} 494 495# 496# run_rc_script file arg 497# Start the script `file' with `arg', and correctly handle the 498# return value from the script. If `file' ends with `.sh', it's 499# sourced into the current environment. Otherwise it's run as 500# a child process. 501# 502# Note: because `.sh' files are sourced into the current environment 503# run_rc_command shouldn't be used because its difficult to ensure 504# that the global variable state before and after the sourcing of 505# the .sh file won't adversely affect other scripts. 506# 507run_rc_script() 508{ 509 _file=$1 510 _arg=$2 511 if [ -z "$_file" -o -z "$_arg" ]; then 512 err 3 'USAGE: run_rc_script file arg' 513 fi 514 515 if [ -n "$rc_fast_and_loose" ]; then 516 unset name command command_args extra_commands pidfile rcvar 517 unset required_dirs required_files required_vars 518 eval unset ${_arg}_cmd ${_arg}_precmd 519 set $_arg ; . $_file 520 else 521 case "$_file" in 522 *.sh) # run in current shell 523 set $_arg ; . $_file 524 ;; 525 *) # run in subshell 526 ( set $_arg ; . $_file ) 527 ;; 528 esac 529 fi 530} 531 532# 533# load_rc_config 534# Source in the configuration file for a given command. 535# 536load_rc_config() 537{ 538 _command=$1 539 if [ -z "$_command" ]; then 540 err 3 'USAGE: load_rc_config command' 541 fi 542 543 if [ -z "$_rc_conf_loaded" ]; then 544 . /etc/rc.conf 545 _rc_conf_loaded=YES 546 fi 547 if [ -f /etc/rc.conf.d/"$_command" ]; then 548 . /etc/rc.conf.d/"$_command" 549 fi 550} 551 552 553# 554# rc_usage commands 555# Print a usage string for $0, with `commands' being a list of 556# valid commands. 557# 558rc_usage() 559{ 560 echo -n 1>&2 "Usage: $0 [fast|force](" 561 562 _sep= 563 for _elem in $*; do 564 echo -n 1>&2 "$_sep$_elem" 565 _sep="|" 566 done 567 echo 1>&2 ")" 568 exit 1 569} 570 571# 572# err exitval message 573# Display message to stderr and log to the syslog, and exit with exitval. 574# 575err() 576{ 577 exitval=$1 578 shift 579 580 logger "$0: ERROR: $*" 581 echo 1>&2 "$0: ERROR: $*" 582 exit $exitval 583} 584 585# 586# warn message 587# Display message to stderr and log to the syslog. 588# 589warn() 590{ 591 logger "$0: WARNING: $*" 592 echo 1>&2 "$0: WARNING: $*" 593} 594