rc.subr revision 1.28
1# $NetBSD: rc.subr,v 1.28 2000/11/06 00:08:30 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 if [ "$_npid" = "PID" ]; then 121 continue 122 fi 123 if [ "$_arg0" = "$_procname" \ 124 -o "$_arg0" = "$_procnamebn" \ 125 -o "$_arg0" = "${_procnamebn}:" \ 126 -o "$_arg0" = "(${_procnamebn})" ]; then 127 echo $_npid 128 return 129 fi 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 if [ "$_npid" = "PID" ]; then 148 continue 149 fi 150 if [ "$_arg0" = "$_procname" \ 151 -o "$_arg0" = "$_procnamebn" \ 152 -o "$_arg0" = "${_procnamebn}:" \ 153 -o "$_arg0" = "(${_procnamebn})" ]; then 154 echo -n "$_pref$_npid" 155 _pref=" " 156 fi 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 '$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 eval ${rcvar}=YES 271 ;; 272 esac 273 274 _keywords="start stop restart rcvar $extra_commands" 275 _pid= 276 _pidcmd= 277 # setup pid check command if not fast 278 if [ -z "$_rc_fast_run" ]; then 279 if [ -n "$pidfile" ]; then 280 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 281 elif [ -n "$command" ]; then 282 _pidcmd='_pid=`check_process '$command'`' 283 fi 284 if [ -n "$_pidcmd" ]; then 285 _keywords="${_keywords} status" 286 fi 287 fi 288 289 if [ -z "$_arg" ]; then 290 rc_usage "$_keywords" 291 fi 292 293 if [ -n "$flags" ]; then # allow override from environment 294 _flags=$flags 295 else 296 eval _flags=\$${name}_flags 297 fi 298 eval _chdir=\$${name}_chdir 299 eval _chroot=\$${name}_chroot 300 eval _nice=\$${name}_nice 301 eval _user=\$${name}_user 302 eval _group=\$${name}_group 303 eval _groups=\$${name}_groups 304 305 # if ${rcvar} is set and we're not 306 # running `rcvar', then check it 307 # 308 if [ -n "${rcvar}" -a "$_arg" != "rcvar" ]; then 309 if ! checkyesno ${rcvar}; then 310 return 0 311 fi 312 fi 313 314 eval $_pidcmd # determine the pid if necessary 315 316 for _elem in $_keywords; do 317 if [ "$_elem" != "$_arg" ]; then 318 continue 319 fi 320 321 # if there's a custom ${XXX_cmd}, 322 # run that instead of the default 323 # 324 eval _cmd=\$${_arg}_cmd 325 eval _precmd=\$${_arg}_precmd 326 if [ -n "$_cmd" ]; then 327 # if the precmd failed and force 328 # isn't set, exit 329 # 330 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 331 return 1 332 fi 333 334 eval $_cmd 335 return 0 336 fi 337 338 case "$_arg" in # default operations... 339 340 status) 341 if [ -n "$_pid" ]; then 342 echo "${name} is running as pid $_pid." 343 else 344 echo "${name} is not running." 345 return 1 346 fi 347 ;; 348 349 start) 350 if [ -n "$_pid" ]; then 351 echo "${name} already running? (pid=$_pid)." 352 exit 1 353 fi 354 355 if [ ! -x $command ]; then 356 return 0 357 fi 358 359 # check for required variables, 360 # directories, and files 361 # 362 for _f in $required_vars; do 363 if ! checkyesno $_f; then 364 warn "\$${_f} is not set." 365 if [ -z "$_rc_force_run" ]; then 366 return 1 367 fi 368 fi 369 done 370 for _f in $required_dirs; do 371 if [ ! -d "${_f}/." ]; then 372 warn "${_f} is not a directory." 373 if [ -z "$_rc_force_run" ]; then 374 return 1 375 fi 376 fi 377 done 378 for _f in $required_files; do 379 if [ ! -r "${_f}" ]; then 380 warn "${_f} is not readable." 381 if [ -z "$_rc_force_run" ]; then 382 return 1 383 fi 384 fi 385 done 386 387 # if the precmd failed and force 388 # isn't set, exit 389 # 390 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 391 return 1 392 fi 393 394 395 # setup the command to run, and run it 396 echo "Starting ${name}." 397 if [ -n "$_chroot" ]; then 398 _doit="\ 399${_nice:+nice -n $_nice }\ 400chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 401$_chroot $command $_flags $command_args" 402 else 403 _doit="\ 404${_user:+su -m $_user -c 'sh -c \"}\ 405${_chdir:+cd $_chdir; }\ 406${_nice:+nice -n $_nice }\ 407$command $_flags $command_args\ 408${_user:+\"'}" 409 fi 410 eval $_doit 411 ;; 412 413 stop) 414 if [ -z "$_pid" ]; then 415 if [ -n "$pidfile" ]; then 416 echo \ 417 "${name} not running? (check $pidfile)." 418 else 419 echo "${name} not running?" 420 fi 421 exit 1 422 fi 423 424 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 425 return 1 426 fi 427 echo "Stopping ${name}." 428 _doit=\ 429"${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}" 430 eval $_doit 431 ;; 432 433 reload) 434 if [ -z "$_pid" ]; then 435 if [ -n "$pidfile" ]; then 436 echo \ 437 "${name} not running? (check $pidfile)." 438 else 439 echo "${name} not running?" 440 fi 441 exit 1 442 fi 443 echo "Reloading ${name} config files." 444 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 445 return 1 446 fi 447 _doit=\ 448"${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}" 449 eval $_doit 450 ;; 451 452 restart) 453 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then 454 return 1 455 fi 456 ( $0 ${_rc_force_run:+force}stop ) 457 sleep 1 458 $0 ${_rc_force_run:+force}start 459 460 ;; 461 462 rcvar) 463 echo "# $name" 464 if [ -n "$rcvar" ]; then 465 if checkyesno ${rcvar}; then 466 echo "\$${rcvar}=YES" 467 else 468 echo "\$${rcvar}=NO" 469 fi 470 fi 471 ;; 472 473 *) 474 rc_usage "$_keywords" 475 ;; 476 477 esac 478 return 0 479 done 480 481 echo 1>&2 "$0: unknown directive '$_arg'." 482 rc_usage "$_keywords" 483 exit 1 484} 485 486# 487# run_rc_script file arg 488# Start the script `file' with `arg', and correctly handle the 489# return value from the script. If `file' ends with `.sh', it's 490# sourced into the current environment. Otherwise it's run as 491# a child process. 492# 493# Note: because `.sh' files are sourced into the current environment 494# run_rc_command shouldn't be used because its difficult to ensure 495# that the global variable state before and after the sourcing of 496# the .sh file won't adversely affect other scripts. 497# 498run_rc_script() 499{ 500 _file=$1 501 _arg=$2 502 if [ -z "$_file" -o -z "$_arg" ]; then 503 err 3 'USAGE: run_rc_script file arg' 504 fi 505 506 case "$_file" in 507 *.sh) # run in current shell 508 set $_arg ; . $_file 509 ;; 510 *) # run in subshell 511 ( set $_arg ; . $_file ) 512 ;; 513 esac 514} 515 516# 517# load_rc_config 518# Source in the configuration file for a given command. 519# 520load_rc_config() 521{ 522 _command=$1 523 if [ -z "$_command" ]; then 524 err 3 'USAGE: load_rc_config command' 525 fi 526 527 . /etc/rc.conf 528 if [ -f /etc/rc.conf.d/"$_command" ]; then 529 . /etc/rc.conf.d/"$_command" 530 fi 531} 532 533 534# 535# rc_usage commands 536# Print a usage string for $0, with `commands' being a list of 537# valid commands. 538# 539rc_usage() 540{ 541 echo -n 1>&2 "Usage: $0 [fast|force](" 542 543 _sep= 544 for _elem in $*; do 545 echo -n 1>&2 "$_sep$_elem" 546 _sep="|" 547 done 548 echo 1>&2 ")" 549 exit 1 550} 551 552# 553# err exitval message 554# Display message to stderr and log to the syslog, and exit with exitval. 555# 556err() 557{ 558 exitval=$1 559 shift 560 561 logger "$0: ERROR: $*" 562 echo 1>&2 "$0: ERROR: $*" 563 exit $exitval 564} 565 566# 567# warn message 568# Display message to stderr and log to the syslog. 569# 570warn() 571{ 572 logger "$0: WARNING: $*" 573 echo 1>&2 "$0: WARNING: $*" 574} 575