rc.subr (95258) | rc.subr (98186) |
---|---|
1# $NetBSD: rc.subr,v 1.28 2000/11/06 00:08:30 lukem Exp $ | 1# $NetBSD: rc.subr,v 1.49 2002/05/21 12:31:01 lukem Exp $ 2# $FreeBSD: head/etc/rc.subr 98186 2002-06-13 22:19:42Z gordon $ |
2# | 3# |
3# Copyright (c) 1997-2000 The NetBSD Foundation, Inc. | 4# Copyright (c) 1997-2002 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: --- 22 unchanged lines hidden (view full) --- 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# | 5# All rights reserved. 6# 7# This code is derived from software contributed to The NetBSD Foundation 8# by Luke Mewburn. 9# 10# Redistribution and use in source and binary forms, with or without 11# modification, are permitted provided that the following conditions 12# are met: --- 22 unchanged lines hidden (view full) --- 35# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36# POSSIBILITY OF SUCH DAMAGE. 37# 38# rc.subr 39# functions used by various rc scripts 40# 41 42# |
43# Operating System dependent/independent variables 44# 45 46SYSCTL="/sbin/sysctl" 47SYSCTL_N="${SYSCTL} -n" 48CMD_OSTYPE="${SYSCTL_N} kern.ostype" 49 50case `${CMD_OSTYPE}` in 51FreeBSD) 52 SYSCTL_W="${SYSCTL}" 53 ;; 54NetBSD) 55 SYSCTL_W="${SYSCTL} -w" 56 ;; 57esac 58 59# |
|
42# functions 43# --------- 44 45# | 60# functions 61# --------- 62 63# |
64# set_rcvar base_var 65# Set the variable name enabling a specific service. 66# FreeBSD uses ${service}_enable, while NetBSD uses 67# just the name of the service. For example: 68# FreeBSD: sendmail_enable="YES" 69# NetBSD : sendmail="YES" 70# $1 - if $name is not the base to work of off, specify 71# a different one 72# 73set_rcvar() 74{ 75 if [ -z "$1" ]; then 76 base_var=${name} 77 else 78 base_var="$1" 79 fi 80 81 case `${CMD_OSTYPE}` in 82 FreeBSD) 83 echo ${base_var}_enable 84 ;; 85 NetBSD) 86 echo ${base_var} 87 ;; 88 *) 89 echo 'XXX' 90 ;; 91 esac 92} 93 94# 95# force_depend script 96# Force a service to start. Intended for use by services 97# to resolve dependency issues. It is assumed the caller 98# has check to make sure this call is necessary 99# $1 - filename of script, in /etc/rc.d, to run 100# 101force_depend() 102{ 103 _depend="$1" 104 105 info "${name} depends on ${_depend}, which will be forced to start." 106 if ! /etc/rc.d/${_depend} forcestart ; then 107 warn "Unable to force ${_depend}. It may already be running." 108 return 1 109 fi 110 return 0 111} 112 113# |
|
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} | 114# checkyesno var 115# Test $1 variable, and warn if not set to YES or NO. 116# Return 0 if it's "yes" (et al), nonzero otherwise. 117# 118checkyesno() 119{ 120 eval _value=\$${1} |
121 debug "checkyesno: $1 is set to $_value." |
|
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 | 122 case $_value in 123 124 # "yes", "true", "on", or "1" 125 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 126 return 0 127 ;; 128 129 # "no", "false", "off", or "0" 130 [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 131 return 1 132 ;; 133 *) 134 warn "\$${1} is not set properly." 135 return 1 136 ;; 137 esac 138} 139 |
140# reverse_list list 141# print the list in reverse order |
|
71# | 142# |
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. | 143reverse_list() 144{ 145 _revlist= 146 for _revfile in $*; do 147 _revlist="$_revfile $_revlist" 148 done 149 echo $_revlist 150} 151 |
75# | 152# |
153# mount_critical_filesystems type 154# Go through the list of critical filesystems as provided in 155# the rc.conf(5) variable $critical_filesystems_${type}, checking 156# each one to see if it is mounted, and if it is not, mounting it. 157# |
|
76mount_critical_filesystems() 77{ | 158mount_critical_filesystems() 159{ |
78 if [ $1 = local ]; then 79 _fslist=$critical_filesystems_beforenet 80 else 81 _fslist=$critical_filesystems 82 fi | 160 eval _fslist=\$critical_filesystems_${1} |
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 | 161 for _fs in $_fslist; do 162 mount | ( 163 _ismounted=no 164 while read what _on on _type type; do 165 if [ $on = $_fs ]; then 166 _ismounted=yes 167 fi 168 done |
91 if [ $_ismounted = no ]; then | 169 if [ $_ismounted = no ]; then |
92 mount $_fs >/dev/null 2>&1 93 fi | 170 mount $_fs >/dev/null 2>&1 171 fi |
94 ) | 172 ) |
95 done 96} 97 98# | 173 done 174} 175 176# |
99# check_pidfile pidfile procname 100# Parses the first line of pidfile for a pid, and ensures | 177# check_pidfile pidfile procname [interpreter] 178# Parses the first line of pidfile for a PID, and ensures |
101# that the process is running and matches procname. | 179# that the process is running and matches procname. |
102# Prints the matching pid upon success, nothing otherwise. | 180# Prints the matching PID upon success, nothing otherwise. 181# interpreter is optional; see _find_processes() for details. |
103# 104check_pidfile() 105{ 106 _pidfile=$1 107 _procname=$2 | 182# 183check_pidfile() 184{ 185 _pidfile=$1 186 _procname=$2 |
187 _interpreter=$3 |
|
108 if [ -z "$_pidfile" -o -z "$_procname" ]; then | 188 if [ -z "$_pidfile" -o -z "$_procname" ]; then |
109 err 3 'USAGE: check_pidfile pidfile procname' | 189 err 3 'USAGE: check_pidfile pidfile procname [interpreter]' |
110 fi 111 if [ ! -f $_pidfile ]; then | 190 fi 191 if [ ! -f $_pidfile ]; then |
192 debug "pid file {$_pidfile): not readable." |
|
112 return 113 fi 114 read _pid _junk < $_pidfile 115 if [ -z "$_pid" ]; then | 193 return 194 fi 195 read _pid _junk < $_pidfile 196 if [ -z "$_pid" ]; then |
197 debug "pid file {$_pidfile): no pid in file." |
|
116 return 117 fi | 198 return 199 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 | 200 _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" |
131} 132 133# | 201} 202 203# |
134# check_process procname | 204# check_process procname [interpreter] |
135# Ensures that a process (or processes) named procname is running. | 205# Ensures that a process (or processes) named procname is running. |
136# Prints a list of matching pids. | 206# Prints a list of matching PIDs. 207# interpreter is optional; see _find_processes() for details. |
137# 138check_process() 139{ 140 _procname=$1 | 208# 209check_process() 210{ 211 _procname=$1 |
212 _interpreter=$2 |
|
141 if [ -z "$_procname" ]; then | 213 if [ -z "$_procname" ]; then |
142 err 3 'USAGE: check_process procname' | 214 err 3 'USAGE: check_process procname [interpreter]' |
143 fi | 215 fi |
144 _procnamebn=${_procname##*/} | 216 _find_processes $_procname ${_interpreter:-.} '-ax' 217} 218 219# 220# _find_processes procname interpreter psargs 221# Search for procname in the output of ps generated by psargs. 222# Prints the PIDs of any matching processes, space separated. 223# 224# If interpreter == ".", check the following variations of procname 225# against the first word of each command: 226# procname 227# `basename procname` 228# `basename procname` + ":" 229# "(" + `basename procname` + ")" 230# 231# If interpreter != ".", read the first line of procname, remove the 232# leading #!, normalise whitespace, append procname, and attempt to 233# match that against each command, either as is, or with extra words 234# at the end. 235# 236_find_processes() 237{ 238 if [ $# -ne 3 ]; then 239 err 3 'USAGE: _find_processes procname interpreter psargs' 240 fi 241 _procname=$1 242 _interpreter=$2 243 _psargs=$3 244 |
145 _pref= | 245 _pref= |
146 ps -ax -o 'pid,command' | while read _npid _arg0 _argv; do 147 if [ "$_npid" = "PID" ]; then 148 continue | 246 if [ $_interpreter != "." ]; then # an interpreted script 247 read _interp < $_procname # read interpreter name 248 _interp=${_interp#\#!} # strip #! 249 set -- $_interp 250 if [ $_interpreter != $1 ]; then 251 warn "\$command_interpreter $_interpreter != $1" |
149 fi | 252 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=" " | 253 _interp="$* $_procname" # cleanup spaces, add _procname 254 _fp_args='_argv' 255 _fp_match='case "$_argv" in 256 ${_interp}|"${_interp} "*)' 257 else # a normal daemon 258 _procnamebn=${_procname##*/} 259 _fp_args='_arg0 _argv' 260 _fp_match='case "$_arg0" in 261 $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 262 fi 263 264 _proccheck=' 265 ps -o "pid,command" '"$_psargs"' | 266 while read _npid '"$_fp_args"'; do 267 case "$_npid" in 268 PID) 269 continue ;; 270 esac ; '"$_fp_match"' 271 echo -n "$_pref$_npid" ; 272 _pref=" " 273 ;; 274 esac 275 done' 276 277 debug "in _find_processes: proccheck is ($_proccheck)." 278 eval $_proccheck 279} 280 281# 282# wait_for_pids pid [pid ...] 283# spins until none of the pids exist 284# 285wait_for_pids() 286{ 287 _list=$* 288 if [ -z "$_list" ]; then 289 return 290 fi 291 _prefix= 292 while true; do 293 _nlist=""; 294 for _j in $_list; do 295 if kill -0 $_j 2>/dev/null; then 296 _nlist="${_nlist}${_nlist:+ }$_j" 297 fi 298 done 299 if [ -z "$_nlist" ]; then 300 break |
156 fi | 301 fi |
302 _list=$_nlist 303 echo -n ${_prefix:-"Waiting for PIDS: "}$_list 304 _prefix=", " 305 sleep 2 |
|
157 done | 306 done |
307 if [ -n "$_prefix" ]; then 308 echo "." 309 fi |
|
158} 159 160# | 310} 311 312# |
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). | 313# run_rc_command argument 314# Search for argument in the list of supported commands, which is: 315# "start stop restart rcvar status poll ${extra_commands}" 316# If there's a match, run ${argument}_cmd or the default method 317# (see below). |
165# | 318# |
166# If arg has a given prefix, then change the operation as follows: 167# prefix operation | 319# If argument has a given prefix, then change the operation as follows: 320# Prefix Operation |
168# ------ --------- | 321# ------ --------- |
169# fast Skip the pid check. 170# force Set ${rcvar} to YES. | 322# fast Skip the pid check, and set rc_fast=yes 323# force Set ${rcvar} to YES, and set rc_force=yes |
171# 172# The following globals are used: 173# | 324# 325# The following globals are used: 326# |
174# name needed function 175# ---- ------ -------- | 327# Name Needed Purpose 328# ---- ------ ------- |
176# name y Name of script. 177# 178# command n Full path to command. | 329# name y Name of script. 330# 331# command n Full path to command. |
179# Not needed if ${arg}_cmd is set for | 332# Not needed if ${rc_arg}_cmd is set for |
180# each keyword. 181# 182# command_args n Optional args/shell directives for command. 183# | 333# each keyword. 334# 335# command_args n Optional args/shell directives for command. 336# |
337# command_interpreter n If not empty, command is interpreted, so 338# call check_{pidfile,process}() appropriately. 339# |
|
184# extra_commands n List of extra commands supported. 185# | 340# extra_commands n List of extra commands supported. 341# |
186# pidfile n If set, use check_pidfile $pidfile, else if 187# $command is set, use check_process $command. | 342# pidfile n If set, use check_pidfile $pidfile $command, 343# otherwise use check_process $command. 344# In either case, only check if $command is set. |
188# | 345# |
346# procname n Process name to check for instead of $command. 347# |
|
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} | 348# rcvar n This is checked with checkyesno to determine 349# if the action should be run. 350# 351# ${name}_chroot n Directory to chroot to before running ${command} |
352# Requires /usr to be mounted. |
|
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. | 353# 354# ${name}_chdir n Directory to cd to before running ${command} 355# (if not using ${name}_chroot). 356# 357# ${name}_flags n Arguments to call ${command} with. 358# NOTE: $flags from the parent environment 359# can be used to override this. 360# 361# ${name}_nice n Nice level to run ${command} at. 362# 363# ${name}_user n User to run ${command} as, using su(1) if not 364# using ${name}_chroot. |
365# Requires /usr to be mounted. |
|
205# 206# ${name}_group n Group to run chrooted ${command} as. | 366# 367# ${name}_group n Group to run chrooted ${command} as. |
368# Requires /usr to be mounted. |
|
207# | 369# |
208# ${name}_groups n Supplementary group list to run chrooted 209# ${command} with. | 370# ${name}_groups n Comma separated list of supplementary groups 371# to run the chrooted ${command} with. 372# Requires /usr to be mounted. |
210# | 373# |
211# ${_arg}_cmd n If set, use this as the action when invoked; 212# $_arg is available to the action to use. | 374# ${rc_arg}_cmd n If set, use this as the method when invoked; |
213# Otherwise, use default command (see below) 214# | 375# Otherwise, use default command (see below) 376# |
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). | 377# ${rc_arg}_precmd n If set, run just before performing the 378# ${rc_arg}_cmd method in the default 379# operation (i.e, after checking for required 380# bits and process (non)existence). |
219# If this completes with a non-zero exit code, | 381# If this completes with a non-zero exit code, |
220# don't run ${_arg}_cmd. | 382# don't run ${rc_arg}_cmd. |
221# | 383# |
384# ${rc_arg}_postcmd n If set, run just after performing the 385# ${rc_arg}_cmd method, if that method 386# returned a zero exit code. 387# |
|
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# | 388# required_dirs n If set, check for the existence of the given 389# directories before running the default 390# (re)start command. 391# 392# required_files n If set, check for the readability of the given 393# files before running the default (re)start 394# command. 395# 396# required_vars n If set, perform checkyesno on each of the 397# listed variables before running the default 398# (re)start command. 399# |
234# Default commands for a given arg: | 400# Default behaviour for a given argument, if no override method is 401# provided: |
235# | 402# |
236# arg default 237# --- ------- 238# status Show if ${command} is running, etc. 239# | 403# Argument Default behaviour 404# -------- ----------------- |
240# start if !running && checkyesno ${rcvar} 241# ${command} 242# 243# stop if ${pidfile} | 405# start if !running && checkyesno ${rcvar} 406# ${command} 407# 408# stop if ${pidfile} |
244# kill $sig_stop `check_pidfile $pidfile` | 409# rc_pid=$(check_pidfile $pidfile $command) |
245# else | 410# else |
246# kill $sig_stop `check_process $command` 247# $sig_stop defaults to TERM. | 411# rc_pid=$(check_process $command) 412# kill $sig_stop $rc_pid 413# wait_for_pids $rc_pid 414# ($sig_stop defaults to TERM.) |
248# | 415# |
249# reload As stop, except use $sig_reload instead. | 416# reload Similar to stop, except use $sig_reload instead, 417# and doesn't wait_for_pids. |
250# $sig_reload defaults to HUP. 251# 252# restart Run `stop' then `start'. 253# | 418# $sig_reload defaults to HUP. 419# 420# restart Run `stop' then `start'. 421# |
422# status Show if ${command} is running, etc. |
|
254# | 423# |
424# poll Wait for ${command} to exit. 425# 426# rcvar Display what rc.conf variable is used (if any). 427# 428# Variables available to methods, and after run_rc_command() has 429# completed: 430# 431# Variable Purpose 432# -------- ------- 433# rc_arg Argument to command, after fast/force processing 434# performed 435# 436# rc_flags Flags to start the default command with. 437# Defaults to ${name}_flags, unless overridden 438# by $flags from the environment. 439# This variable may be changed by the precmd method. 440# 441# rc_pid PID of command (if appropriate) 442# 443# rc_fast Not empty if "fast" was provided (q.v.) 444# 445# rc_force Not empty if "force" was provided (q.v.) 446# 447# |
|
255run_rc_command() 256{ | 448run_rc_command() 449{ |
257 _arg=$1 | 450 rc_arg=$1 |
258 if [ -z "$name" ]; then | 451 if [ -z "$name" ]; then |
259 err 3 '$name is not set.' | 452 err 3 'run_rc_command: $name is not set.' |
260 fi 261 | 453 fi 454 |
262 case "$_arg" in | 455 case "$rc_arg" in |
263 fast*) # "fast" prefix; don't check pid | 456 fast*) # "fast" prefix; don't check pid |
264 _arg=${_arg#fast} 265 _rc_fast_run=YES | 457 rc_arg=${rc_arg#fast} 458 rc_fast=yes |
266 ;; 267 force*) # "force prefix; always start | 459 ;; 460 force*) # "force prefix; always start |
268 _arg=${_arg#force} 269 _rc_force_run=YES | 461 rc_arg=${rc_arg#force} 462 rc_force=yes |
270 if [ -n "${rcvar}" ]; then 271 eval ${rcvar}=YES 272 fi 273 ;; 274 esac 275 | 463 if [ -n "${rcvar}" ]; then 464 eval ${rcvar}=YES 465 fi 466 ;; 467 esac 468 |
469 eval _overide_command=\$${name}_program 470 if [ -n "$_overide_command" ]; then 471 command=$_overide_command 472 fi 473 |
|
276 _keywords="start stop restart rcvar $extra_commands" | 474 _keywords="start stop restart rcvar $extra_commands" |
277 _pid= | 475 rc_pid= |
278 _pidcmd= | 476 _pidcmd= |
477 _procname=${procname:-${command}} 478 |
|
279 # setup pid check command if not fast | 479 # setup pid check command if not fast |
280 if [ -z "$_rc_fast_run" ]; then | 480 if [ -z "$rc_fast" -a -n "$_procname" ]; then |
281 if [ -n "$pidfile" ]; then | 481 if [ -n "$pidfile" ]; then |
282 _pidcmd='_pid=`check_pidfile '$pidfile' '$command'`' 283 elif [ -n "$command" ]; then 284 _pidcmd='_pid=`check_process '$command'`' | 482 _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 483 else 484 _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' |
285 fi 286 if [ -n "$_pidcmd" ]; then | 485 fi 486 if [ -n "$_pidcmd" ]; then |
287 _keywords="${_keywords} status" | 487 _keywords="${_keywords} status poll" |
288 fi 289 fi 290 | 488 fi 489 fi 490 |
291 if [ -z "$_arg" ]; then | 491 if [ -z "$rc_arg" ]; then |
292 rc_usage "$_keywords" 293 fi 294 295 if [ -n "$flags" ]; then # allow override from environment | 492 rc_usage "$_keywords" 493 fi 494 495 if [ -n "$flags" ]; then # allow override from environment |
296 _flags=$flags | 496 rc_flags=$flags |
297 else | 497 else |
298 eval _flags=\$${name}_flags | 498 eval rc_flags=\$${name}_flags |
299 fi | 499 fi |
300 eval _chdir=\$${name}_chdir 301 eval _chroot=\$${name}_chroot 302 eval _nice=\$${name}_nice 303 eval _user=\$${name}_user 304 eval _group=\$${name}_group 305 eval _groups=\$${name}_groups | 500 eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 501 _nice=\$${name}_nice _user=\$${name}_user \ 502 _group=\$${name}_group _groups=\$${name}_groups |
306 | 503 |
504 if [ -n "$_user" ]; then # unset $_user if running as that user 505 if [ "$_user" = "$(id -un)" ]; then 506 unset _user 507 fi 508 fi 509 |
|
307 # if ${rcvar} is set, and $1 is not | 510 # if ${rcvar} is set, and $1 is not |
308 # "rcvar" or "status", then run | 511 # "rcvar", then run |
309 # checkyesno ${rcvar} 310 # and return if that failed 311 # | 512 # checkyesno ${rcvar} 513 # and return if that failed 514 # |
312 if [ -n "${rcvar}" -a "$_arg" != "rcvar" -a "$_arg" != "status" ]; then | 515 if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then |
313 if ! checkyesno ${rcvar}; then 314 return 0 315 fi 316 fi 317 318 eval $_pidcmd # determine the pid if necessary 319 320 for _elem in $_keywords; do | 516 if ! checkyesno ${rcvar}; then 517 return 0 518 fi 519 fi 520 521 eval $_pidcmd # determine the pid if necessary 522 523 for _elem in $_keywords; do |
321 if [ "$_elem" != "$_arg" ]; then | 524 if [ "$_elem" != "$rc_arg" ]; then |
322 continue 323 fi 324 325 # if there's a custom ${XXX_cmd}, 326 # run that instead of the default 327 # | 525 continue 526 fi 527 528 # if there's a custom ${XXX_cmd}, 529 # run that instead of the default 530 # |
328 eval _cmd=\$${_arg}_cmd 329 eval _precmd=\$${_arg}_precmd | 531 eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 532 _postcmd=\$${rc_arg}_postcmd |
330 if [ -n "$_cmd" ]; then | 533 if [ -n "$_cmd" ]; then |
534 debug "run_rc_command: using XXX_cmd functions." |
|
331 # if the precmd failed and force 332 # isn't set, exit 333 # | 535 # if the precmd failed and force 536 # isn't set, exit 537 # |
334 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then | 538 if ! eval $_precmd && [ -z "$rc_force" ]; then |
335 return 1 336 fi 337 | 539 return 1 540 fi 541 |
338 eval $_cmd | 542 if ! eval $_cmd && [ -z "$rc_force" ]; then 543 return 1 544 fi 545 eval $_postcmd |
339 return 0 340 fi 341 | 546 return 0 547 fi 548 |
342 case "$_arg" in # default operations... | 549 case "$rc_arg" in # default operations... |
343 344 status) | 550 551 status) |
345 if [ -n "$_pid" ]; then 346 echo "${name} is running as pid $_pid." | 552 if [ -n "$rc_pid" ]; then 553 echo "${name} is running as pid $rc_pid." |
347 else 348 echo "${name} is not running." 349 return 1 350 fi 351 ;; 352 353 start) | 554 else 555 echo "${name} is not running." 556 return 1 557 fi 558 ;; 559 560 start) |
354 if [ -n "$_pid" ]; then 355 echo "${name} already running? (pid=$_pid)." | 561 if [ -n "$rc_pid" ]; then 562 echo "${name} already running? (pid=$rc_pid)." |
356 exit 1 357 fi 358 359 if [ ! -x $command ]; then | 563 exit 1 564 fi 565 566 if [ ! -x $command ]; then |
567 info "run_rc_command: cannot run ($command)." |
|
360 return 0 361 fi 362 363 # check for required variables, 364 # directories, and files 365 # 366 for _f in $required_vars; do 367 if ! checkyesno $_f; then 368 warn "\$${_f} is not set." | 568 return 0 569 fi 570 571 # check for required variables, 572 # directories, and files 573 # 574 for _f in $required_vars; do 575 if ! checkyesno $_f; then 576 warn "\$${_f} is not set." |
369 if [ -z "$_rc_force_run" ]; then | 577 if [ -z "$rc_force" ]; then |
370 return 1 371 fi 372 fi 373 done 374 for _f in $required_dirs; do 375 if [ ! -d "${_f}/." ]; then 376 warn "${_f} is not a directory." | 578 return 1 579 fi 580 fi 581 done 582 for _f in $required_dirs; do 583 if [ ! -d "${_f}/." ]; then 584 warn "${_f} is not a directory." |
377 if [ -z "$_rc_force_run" ]; then | 585 if [ -z "$rc_force" ]; then |
378 return 1 379 fi 380 fi 381 done 382 for _f in $required_files; do 383 if [ ! -r "${_f}" ]; then 384 warn "${_f} is not readable." | 586 return 1 587 fi 588 fi 589 done 590 for _f in $required_files; do 591 if [ ! -r "${_f}" ]; then 592 warn "${_f} is not readable." |
385 if [ -z "$_rc_force_run" ]; then | 593 if [ -z "$rc_force" ]; then |
386 return 1 387 fi 388 fi 389 done 390 391 # if the precmd failed and force 392 # isn't set, exit 393 # | 594 return 1 595 fi 596 fi 597 done 598 599 # if the precmd failed and force 600 # isn't set, exit 601 # |
394 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then | 602 if ! eval $_precmd && [ -z "$rc_force" ]; then |
395 return 1 396 fi 397 | 603 return 1 604 fi 605 |
398 | |
399 # setup the command to run, and run it 400 # 401 echo "Starting ${name}." 402 if [ -n "$_chroot" ]; then 403 _doit="\ 404${_nice:+nice -n $_nice }\ 405chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ | 606 # setup the command to run, and run it 607 # 608 echo "Starting ${name}." 609 if [ -n "$_chroot" ]; then 610 _doit="\ 611${_nice:+nice -n $_nice }\ 612chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ |
406$_chroot $command $_flags $command_args" | 613$_chroot $command $rc_flags $command_args" |
407 else 408 _doit="\ | 614 else 615 _doit="\ |
409${_user:+su -m $_user -c 'sh -c \"}\ | |
410${_chdir:+cd $_chdir; }\ 411${_nice:+nice -n $_nice }\ | 616${_chdir:+cd $_chdir; }\ 617${_nice:+nice -n $_nice }\ |
412$command $_flags $command_args\ 413${_user:+\"'}" | 618$command $rc_flags $command_args" 619 if [ -n "$_user" ]; then 620 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 621 fi |
414 fi | 622 fi |
415 eval $_doit | 623 624 # if the cmd failed and force 625 # isn't set, exit 626 # 627 debug "run_rc_command: _doit: $_doit" 628 if ! eval $_doit && [ -z "$rc_force" ]; then 629 return 1 630 fi 631 632 # finally, run postcmd 633 # 634 eval $_postcmd |
416 ;; 417 418 stop) | 635 ;; 636 637 stop) |
419 if [ -z "$_pid" ]; then | 638 if [ -z "$rc_pid" ]; then |
420 if [ -n "$pidfile" ]; then 421 echo \ 422 "${name} not running? (check $pidfile)." 423 else 424 echo "${name} not running?" 425 fi 426 exit 1 427 fi 428 | 639 if [ -n "$pidfile" ]; then 640 echo \ 641 "${name} not running? (check $pidfile)." 642 else 643 echo "${name} not running?" 644 fi 645 exit 1 646 fi 647 |
429 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then | 648 # if the precmd failed and force 649 # isn't set, exit 650 # 651 if ! eval $_precmd && [ -z "$rc_force" ]; then |
430 return 1 431 fi | 652 return 1 653 fi |
654 655 # send the signal to stop 656 # |
|
432 echo "Stopping ${name}." | 657 echo "Stopping ${name}." |
433 _doit=\ 434"${_user:+su -m $_user -c '}kill -${sig_stop:-TERM} $_pid${_user:+'}" 435 eval $_doit | 658 _doit="kill -${sig_stop:-TERM} $rc_pid" 659 if [ -n "$_user" ]; then 660 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 661 fi 662 663 # if the stop cmd failed and force 664 # isn't set, exit 665 # 666 if ! eval $_doit && [ -z "$rc_force" ]; then 667 return 1 668 fi 669 670 # wait for the command to exit, 671 # and run postcmd. 672 wait_for_pids $rc_pid 673 eval $_postcmd |
436 ;; 437 438 reload) | 674 ;; 675 676 reload) |
439 if [ -z "$_pid" ]; then | 677 if [ -z "$rc_pid" ]; then |
440 if [ -n "$pidfile" ]; then 441 echo \ 442 "${name} not running? (check $pidfile)." 443 else 444 echo "${name} not running?" 445 fi 446 exit 1 447 fi 448 echo "Reloading ${name} config files." | 678 if [ -n "$pidfile" ]; then 679 echo \ 680 "${name} not running? (check $pidfile)." 681 else 682 echo "${name} not running?" 683 fi 684 exit 1 685 fi 686 echo "Reloading ${name} config files." |
449 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then | 687 if ! eval $_precmd && [ -z "$rc_force" ]; then |
450 return 1 451 fi | 688 return 1 689 fi |
452 _doit=\ 453"${_user:+su -m $_user -c '}kill -${sig_reload:-HUP} $_pid${_user:+'}" 454 eval $_doit | 690 _doit="kill -${sig_reload:-HUP} $rc_pid" 691 if [ -n "$_user" ]; then 692 _doit="su -m $_user -c 'sh -c \"$_doit\"'" 693 fi 694 if ! eval $_doit && [ -z "$rc_force" ]; then 695 return 1 696 fi 697 eval $_postcmd |
455 ;; 456 457 restart) | 698 ;; 699 700 restart) |
458 if ! eval $_precmd && [ -z "$_rc_force_run" ]; then | 701 if ! eval $_precmd && [ -z "$rc_force" ]; then |
459 return 1 460 fi 461 # prevent restart being called more 462 # than once by any given script 463 # 464 if [ -n "$_rc_restart_done" ]; then 465 return 0 466 fi 467 _rc_restart_done=YES | 702 return 1 703 fi 704 # prevent restart being called more 705 # than once by any given script 706 # 707 if [ -n "$_rc_restart_done" ]; then 708 return 0 709 fi 710 _rc_restart_done=YES |
468 ( $0 ${_rc_force_run:+force}stop ) 469 sleep 1 470 $0 ${_rc_force_run:+force}start | |
471 | 711 |
712 ( $0 ${rc_force:+force}stop ) 713 $0 ${rc_force:+force}start 714 715 eval $_postcmd |
|
472 ;; 473 | 716 ;; 717 |
718 poll) 719 if [ -n "$rc_pid" ]; then 720 wait_for_pids $rc_pid 721 fi 722 ;; 723 |
|
474 rcvar) 475 echo "# $name" 476 if [ -n "$rcvar" ]; then 477 if checkyesno ${rcvar}; then 478 echo "\$${rcvar}=YES" 479 else 480 echo "\$${rcvar}=NO" 481 fi 482 fi 483 ;; 484 485 *) 486 rc_usage "$_keywords" 487 ;; 488 489 esac 490 return 0 491 done 492 | 724 rcvar) 725 echo "# $name" 726 if [ -n "$rcvar" ]; then 727 if checkyesno ${rcvar}; then 728 echo "\$${rcvar}=YES" 729 else 730 echo "\$${rcvar}=NO" 731 fi 732 fi 733 ;; 734 735 *) 736 rc_usage "$_keywords" 737 ;; 738 739 esac 740 return 0 741 done 742 |
493 echo 1>&2 "$0: unknown directive '$_arg'." | 743 echo 1>&2 "$0: unknown directive '$rc_arg'." |
494 rc_usage "$_keywords" 495 exit 1 496} 497 498# 499# run_rc_script file arg 500# Start the script `file' with `arg', and correctly handle the 501# return value from the script. If `file' ends with `.sh', it's | 744 rc_usage "$_keywords" 745 exit 1 746} 747 748# 749# run_rc_script file arg 750# Start the script `file' with `arg', and correctly handle the 751# return value from the script. If `file' ends with `.sh', it's |
502# sourced into the current environment. Otherwise it's run as 503# a child process. | 752# sourced into the current environment. If `file' appears to be 753# a backup or scratch file, ignore it. Otherwise if it's 754# executable run as a child process. |
504# | 755# |
505# Note: because `.sh' files are sourced into the current environment 506# run_rc_command shouldn't be used because its difficult to ensure 507# that the global variable state before and after the sourcing of 508# the .sh file won't adversely affect other scripts. 509# | |
510run_rc_script() 511{ 512 _file=$1 513 _arg=$2 514 if [ -z "$_file" -o -z "$_arg" ]; then 515 err 3 'USAGE: run_rc_script file arg' 516 fi 517 | 756run_rc_script() 757{ 758 _file=$1 759 _arg=$2 760 if [ -z "$_file" -o -z "$_arg" ]; then 761 err 3 'USAGE: run_rc_script file arg' 762 fi 763 |
764 trap "echo 'Reboot interrupted'; exit 1" 3 765 766 unset name command command_args command_interpreter \ 767 extra_commands pidfile procname \ 768 rcvar required_dirs required_files required_vars 769 eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 770 |
|
518 case "$_file" in 519 *.sh) # run in current shell 520 set $_arg ; . $_file 521 ;; | 771 case "$_file" in 772 *.sh) # run in current shell 773 set $_arg ; . $_file 774 ;; |
775 *[~#]|*.OLD|*.orig) # scratch file; skip 776 warn "Ignoring scratch file $_file" 777 ;; |
|
522 *) # run in subshell | 778 *) # run in subshell |
523 ( set $_arg ; . $_file ) | 779 if [ -x $_file ]; then 780 if [ -n "$rc_fast_and_loose" ]; then 781 set $_arg ; . $_file 782 else 783 ( trap "echo 'Reboot interrupted'; exit 1" 3 784 set $_arg ; . $_file ) 785 fi 786 fi |
524 ;; 525 esac 526} 527 528# 529# load_rc_config 530# Source in the configuration file for a given command. 531# 532load_rc_config() 533{ 534 _command=$1 535 if [ -z "$_command" ]; then 536 err 3 'USAGE: load_rc_config command' 537 fi 538 | 787 ;; 788 esac 789} 790 791# 792# load_rc_config 793# Source in the configuration file for a given command. 794# 795load_rc_config() 796{ 797 _command=$1 798 if [ -z "$_command" ]; then 799 err 3 'USAGE: load_rc_config command' 800 fi 801 |
539 . /etc/rc.conf | 802 if [ -z "$_rc_conf_loaded" ]; then 803 if [ -r /etc/defaults/rc.conf ]; then 804 debug "Sourcing /etc/defaults/rc.conf" 805 . /etc/defaults/rc.conf 806 source_rc_confs 807 elif [ -r /etc/rc.conf ]; then 808 debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 809 . /etc/rc.conf 810 fi 811 _rc_conf_loaded=YES 812 fi |
540 if [ -f /etc/rc.conf.d/"$_command" ]; then | 813 if [ -f /etc/rc.conf.d/"$_command" ]; then |
814 debug "Sourcing /etc/rc.conf.d/${_command}" |
|
541 . /etc/rc.conf.d/"$_command" 542 fi 543} 544 545 546# 547# rc_usage commands 548# Print a usage string for $0, with `commands' being a list of --- 8 unchanged lines hidden (view full) --- 557 echo -n 1>&2 "$_sep$_elem" 558 _sep="|" 559 done 560 echo 1>&2 ")" 561 exit 1 562} 563 564# | 815 . /etc/rc.conf.d/"$_command" 816 fi 817} 818 819 820# 821# rc_usage commands 822# Print a usage string for $0, with `commands' being a list of --- 8 unchanged lines hidden (view full) --- 831 echo -n 1>&2 "$_sep$_elem" 832 _sep="|" 833 done 834 echo 1>&2 ")" 835 exit 1 836} 837 838# |
839# _echo prefix message 840# Display message preceded by "$prefix:". Log to syslog as well. 841# XXX - syslogd may not be listening (especially if this subroutine 842# is called at boot before syslogd has had a chance to startup). 843# 844_echo() 845{ 846 [ -x /usr/bin/logger ] && /usr/bin/logger "$0: $1: $2" 847 echo "$0: $1: $2" 848} 849 850# |
|
565# err exitval message 566# Display message to stderr and log to the syslog, and exit with exitval. 567# 568err() 569{ 570 exitval=$1 571 shift 572 | 851# err exitval message 852# Display message to stderr and log to the syslog, and exit with exitval. 853# 854err() 855{ 856 exitval=$1 857 shift 858 |
573 logger "$0: ERROR: $*" 574 echo 1>&2 "$0: ERROR: $*" | 859 _echo 1>&2 "ERROR" "$*" |
575 exit $exitval 576} 577 578# 579# warn message 580# Display message to stderr and log to the syslog. 581# 582warn() 583{ | 860 exit $exitval 861} 862 863# 864# warn message 865# Display message to stderr and log to the syslog. 866# 867warn() 868{ |
584 logger "$0: WARNING: $*" 585 echo 1>&2 "$0: WARNING: $*" | 869 _echo 1>&2 "WARNING" "$*" |
586} | 870} |
871 872# 873# info message 874# Display informational message to stdout and log to syslog. 875# 876info() 877{ 878 _echo "INFO" "$*" 879} 880 881# 882# debug message 883# If debugging is enabled in rc.conf output message to stderr and syslog. 884# BEWARE that you don't call any subroutine that itself calls this 885# function. 886# 887debug() 888{ 889 # This subroutine is provided as a convenience to script writers, who 890 # should enable debugging in /etc/rc.conf. 891 # 892 [ -f /etc/rc.conf ] && . /etc/rc.conf 893 894 case ${rc_debug} in 895 [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 896 _echo 1>&2 "DEBUG" "$*" 897 return 898 ;; 899 esac 900} 901 902# 903# backup_file action file cur backup 904# Make a backup copy of `file' into `cur', and save the previous 905# version of `cur' as `backup' or use rcs for archiving. 906# 907# This routine checks the value of the backup_uses_rcs variable, 908# which can be either YES or NO. 909# 910# The `action' keyword can be one of the following: 911# 912# add `file' is now being backed up (and is possibly 913# being reentered into the backups system). `cur' 914# is created and RCS files, if necessary, are 915# created as well. 916# 917# update `file' has changed and needs to be backed up. 918# If `cur' exists, it is copied to to `back' or 919# checked into RCS (if the repository file is old), 920# and then `file' is copied to `cur'. Another RCS 921# check in done here if RCS is being used. 922# 923# remove `file' is no longer being tracked by the backups 924# system. If RCS is not being used, `cur' is moved 925# to `back', otherwise an empty file is checked in, 926# and then `cur' is removed. 927# 928# 929backup_file() 930{ 931 _action=$1 932 _file=$2 933 _cur=$3 934 _back=$4 935 936 if checkyesno backup_uses_rcs; then 937 _msg0="backup archive" 938 _msg1="update" 939 940 # ensure that history file is not locked 941 if [ -f $_cur,v ]; then 942 rcs -q -u -U -M $_cur 943 fi 944 945 # ensure after switching to rcs that the 946 # current backup is not lost 947 if [ -f $_cur ]; then 948 # no archive, or current newer than archive 949 if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 950 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 951 rcs -q -kb -U $_cur 952 co -q -f -u $_cur 953 fi 954 fi 955 956 case $_action in 957 add|update) 958 cp -p $_file $_cur 959 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 960 rcs -q -kb -U $_cur 961 co -q -f -u $_cur 962 chown root:wheel $_cur $_cur,v 963 ;; 964 remove) 965 cp /dev/null $_cur 966 ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 967 rcs -q -kb -U $_cur 968 chown root:wheel $_cur $_cur,v 969 rm $_cur 970 ;; 971 esac 972 else 973 case $_action in 974 add|update) 975 if [ -f $_cur ]; then 976 cp -p $_cur $_back 977 fi 978 cp -p $_file $_cur 979 chown root:wheel $_cur 980 ;; 981 remove) 982 mv -f $_cur $_back 983 ;; 984 esac 985 fi 986} |
|