rc.subr revision 1.18
1# $NetBSD: rc.subr,v 1.18 2000/05/13 03:07:17 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=`basename $_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=`basename $_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# name needed function 174# ---- ------ -------- 175# name y Name of script. 176# command n Full path to command. 177# Not needed if ${arg}_cmd is set for 178# each keyword. 179# command_args n Optional args/shell directives for command. 180# extra_commands n List of extra commands supported. 181# pidfile n If set, use check_pidfile $pidfile, else if 182# $command is set, use check_process $command. 183# rcvar n If the default command is being run, this is 184# checked with checkyesno to determine if 185# the action should be run. 186# If this variable isn't set, ${name} is checked 187# instead. 188# ${name}_chdir n Directory to cd to before running ${command}. 189# ${name}_flags n Arguments to call ${command} with. 190# NOTE: if $flags is set (e.g, from the parent 191# environment), it overrides this. 192# ${name}_nice n Nice level to run ${command} at. 193# ${name}_user n User to run ${command} as. 194# ${_arg}_cmd n If set, use this as the action when invoked; 195# $_arg is available to the action to use. 196# Otherwise, use default command (see below) 197# NOTE: checkyesno ${rcvar} is NOT performed 198# for ${_arg}_cmd; use ${_arg}_precmd to 199# do this. 200# ${_arg}_precmd n If set, run just before performing the main 201# action in the default command (i.e, after 202# checking for required bits and process 203# (non)existance). 204# If this completes with a non-zero exit code, 205# don't run ${_arg}_cmd. 206# required_dirs n If set, check for the existence of the given 207# directories before running the default 208# (re)start command. 209# required_files n If set, check for the readability of the given 210# files before running the default (re)start 211# command. 212# required_vars n If set, perform checkyesno on each of the 213# listed variables before running the default 214# (re)start command. 215# 216# Default commands for a given arg: 217# arg default 218# --- ------- 219# status Show if ${command} is running, etc. 220# start if !running && checkyesno ${rcvar} 221# ${command} 222# stop if ${pidfile} 223# kill $sig_stop `check_pidfile $pidfile` 224# else 225# kill $sig_stop `check_process $command` 226# $sig_stop defaults to TERM. 227# reload As stop, except use $sig_reload instead. 228# $sig_reload defaults to HUP. 229# restart Run `stop' then `start'. 230# 231run_rc_command() 232{ 233 _arg=$1 234 _ckvar=${rcvar:-$name} 235 if [ -z "$_ckvar" ]; then 236 err 3 'neither $rcvar or $name is set.' 237 fi 238 239 case "$_arg" in 240 fast*) 241 _arg=${_arg#fast} 242 _rc_fast_run=YES 243 ;; 244 force*) 245 _arg=${_arg#force} 246 eval ${_ckvar}=YES 247 ;; 248 esac 249 250 _keywords="start stop restart rcvar $extra_commands" 251 _pid= 252 _pidcmd= 253 if [ -z "$_rc_fast_run" ]; then 254 if [ -n "$pidfile" ]; then 255 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 256 elif [ -n "$command" ]; then 257 _pidcmd='_pid=`check_process '$command'`' 258 fi 259 if [ -n "$_pidcmd" ]; then 260 _keywords="${_keywords} status" 261 fi 262 fi 263 264 if [ -z "$_arg" ]; then 265 rc_usage "$_keywords" 266 fi 267 268 if [ -n "$flags" ]; then # allow override from environment 269 _flags=$flags 270 else 271 eval _flags=\$${name}_flags 272 fi 273 eval _chdir=\$${name}_chdir 274 eval _nice=\$${name}_nice 275 eval _user=\$${name}_user 276 277 eval $_pidcmd 278 279 for _elem in $_keywords; do 280 if [ "$_elem" != "$_arg" ]; then 281 continue 282 fi 283 284 eval _cmd=\$${_arg}_cmd 285 eval _precmd=\$${_arg}_precmd 286 if [ -n "$_cmd" ]; then 287 eval $_precmd || return 1 288 eval $_cmd 289 return 0 290 fi 291 292 case "$_arg" in 293 294 status) 295 if [ -n "$_pid" ]; then 296 echo "${name} is running as pid $_pid." 297 else 298 echo "${name} is not running." 299 fi 300 ;; 301 302 start) 303 if [ -n "$_pid" ]; then 304 echo "${name} already running? (pid=$_pid)." 305 exit 1 306 fi 307 308 if ! checkyesno ${_ckvar} || [ ! -x $command ]; then 309 return 0 310 fi 311 312 for _f in $required_vars; do 313 if ! checkyesno $_f; then 314 warn \ 315 "\$${_f} is not set; ${name} not started." 316 return 1 317 fi 318 done 319 for _f in $required_dirs; do 320 if [ ! -d "${_f}/." ]; then 321 warn \ 322 "${_f} is not a directory; ${name} not started." 323 return 1 324 fi 325 done 326 for _f in $required_files; do 327 if [ ! -r "${_f}" ]; then 328 warn \ 329 "${_f} is not readable; ${name} not started." 330 return 1 331 fi 332 done 333 334 eval $_precmd || return 1 335 echo "Starting ${name}." 336 _doit="\ 337${_user:+su -m $_user -c 'sh -c \"}\ 338${_chdir:+cd $_chdir; }\ 339${_nice:+nice -n $_nice }\ 340$command $_flags $command_args\ 341${_user:+\"'}" 342 eval $_doit 343 ;; 344 345 stop) 346 if [ -z "$_pid" ]; then 347 if checkyesno ${_ckvar}; then 348 if [ -n "$pidfile" ]; then 349 echo \ 350 "${name} not running? (check $pidfile)." 351 else 352 echo "${name} not running?" 353 fi 354 exit 1 355 fi 356 return 0 357 fi 358 359 eval $_precmd || return 1 360 echo "Stopping ${name}." 361 _doit=\ 362"${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}" 363 eval $_doit 364 ;; 365 366 reload) 367 if [ -z "$_pid" ]; then 368 if checkyesno ${_ckvar}; then 369 if [ -n "$pidfile" ]; then 370 echo \ 371 "${name} not running? (check $pidfile)." 372 else 373 echo "${name} not running?" 374 fi 375 exit 1 376 fi 377 return 0 378 fi 379 echo "Reloading ${name} config files." 380 eval $_precmd || return 1 381 _doit=\ 382"${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}" 383 eval $_doit 384 ;; 385 386 restart) 387 if ! checkyesno ${_ckvar}; then 388 return 0 389 fi 390 eval $_precmd || return 1 391 ( $0 stop ) 392 sleep 1 393 $0 start 394 395 ;; 396 397 rcvar) 398 echo "# $name" 399 if checkyesno ${_ckvar}; then 400 echo "\$${_ckvar}=YES" 401 else 402 echo "\$${_ckvar}=NO" 403 fi 404 ;; 405 406 *) 407 rc_usage "$_keywords" 408 ;; 409 410 esac 411 return 0 412 done 413 414 echo 1>&2 "$0: unknown directive '$_arg'." 415 rc_usage "$_keywords" 416 exit 1 417} 418 419# 420# run_rc_script file arg 421# Start the script `file' with `arg', and correctly handle the 422# return value from the script. If `file' ends with `.sh', it's 423# sourced into the current environment. Otherwise it's run as 424# a child process. 425# 426# Note: because `.sh' files are sourced into the current environment 427# run_rc_command shouldn't be used because its difficult to ensure 428# that the global variable state before and after the sourcing of 429# the .sh file won't adversely affect other scripts. 430# 431run_rc_script() 432{ 433 _file=$1 434 _arg=$2 435 if [ -z "$_file" -o -z "$_arg" ]; then 436 err 3 'USAGE: run_rc_script file arg' 437 fi 438 439 case "$_file" in 440 *.sh) # run in current shell 441 set $_arg ; . $_file 442 ;; 443 *) # run in subshell 444 ( set $_arg ; . $_file ) 445 ;; 446 esac 447} 448 449# 450# rc_usage commands 451# Print a usage string for $0, with `commands' being a list of 452# valid commands. 453# 454rc_usage() 455{ 456 echo -n 1>&2 "Usage: $0 [fast|force](" 457 458 _sep= 459 for _elem in $*; do 460 echo -n 1>&2 "$_sep$_elem" 461 _sep="|" 462 done 463 echo 1>&2 ")" 464 exit 1 465} 466 467# 468# err exitval message 469# Display message to stderr and log to the syslog, and exit with exitval. 470# 471err() 472{ 473 exitval=$1 474 shift 475 476 logger "$0: ERROR $*" 477 echo 1>&2 "$0: ERROR $*" 478 exit $exitval 479} 480 481# 482# warn message 483# Display message to stderr and log to the syslog. 484# 485warn() 486{ 487 logger "$0: WARNING $*" 488 echo 1>&2 "$0: WARNING $*" 489} 490