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