rc.subr revision 271545
1164640Sflz# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 271545 2014-09-13 18:54:15Z hrs $ 378344Sobrien# 4157473Sflz# Copyright (c) 1997-2004 The NetBSD Foundation, Inc. 578344Sobrien# All rights reserved. 678344Sobrien# 778344Sobrien# This code is derived from software contributed to The NetBSD Foundation 878344Sobrien# by Luke Mewburn. 978344Sobrien# 1078344Sobrien# Redistribution and use in source and binary forms, with or without 1178344Sobrien# modification, are permitted provided that the following conditions 1278344Sobrien# are met: 1378344Sobrien# 1. Redistributions of source code must retain the above copyright 1478344Sobrien# notice, this list of conditions and the following disclaimer. 1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1678344Sobrien# notice, this list of conditions and the following disclaimer in the 1778344Sobrien# documentation and/or other materials provided with the distribution. 1878344Sobrien# 1978344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2078344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2178344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2278344Sobrien# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 2378344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2478344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2578344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2678344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2778344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2878344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2978344Sobrien# POSSIBILITY OF SUCH DAMAGE. 3078344Sobrien# 3178344Sobrien# rc.subr 3278344Sobrien# functions used by various rc scripts 3378344Sobrien# 3478344Sobrien 35169668Smtm: ${RC_PID:=$$}; export RC_PID 36157473Sflz 3778344Sobrien# 3898186Sgordon# Operating System dependent/independent variables 3998186Sgordon# 4098186Sgordon 41131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then 42131550Scperciva 43131550Scperciva_rc_subr_loaded="YES" 44131550Scperciva 4598186SgordonSYSCTL="/sbin/sysctl" 4698186SgordonSYSCTL_N="${SYSCTL} -n" 47202988SemasteSYSCTL_W="${SYSCTL}" 48124832SmtmID="/usr/bin/id" 49124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 50161435SyarPS="/bin/ps -ww" 51161435SyarJID=`$PS -p $$ -o jid=` 5298186Sgordon 5398186Sgordon# 5478344Sobrien# functions 5578344Sobrien# --------- 5678344Sobrien 57264243Sdteske# list_vars pattern 58264243Sdteske# List vars matching pattern. 59264243Sdteske# 60264243Sdteskelist_vars() 61264243Sdteske{ 62264243Sdteske set | { while read LINE; do 63264243Sdteske var="${LINE%%=*}" 64264243Sdteske case "$var" in 65264243Sdteske "$LINE"|*[!a-zA-Z0-9_]*) continue ;; 66264243Sdteske $1) echo $var 67264243Sdteske esac 68264243Sdteske done; } 69264243Sdteske} 70264243Sdteske 71197144Shrs# set_rcvar_obsolete oldvar [newvar] [msg] 72197144Shrs# Define obsolete variable. 73197144Shrs# Global variable $rcvars_obsolete is used. 7498186Sgordon# 75197144Shrsset_rcvar_obsolete() 76197144Shrs{ 77197144Shrs local _var 78197144Shrs _var=$1 79197144Shrs debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" 80197144Shrs 81197144Shrs rcvars_obsolete="${rcvars_obsolete# } $1" 82197144Shrs eval ${1}_newvar=\"$2\" 83197144Shrs shift 2 84197144Shrs eval ${_var}_obsolete_msg=\"$*\" 85197144Shrs} 86197144Shrs 87197144Shrs# 88231667Sdougb# force_depend script [rcvar] 8998186Sgordon# Force a service to start. Intended for use by services 90231667Sdougb# to resolve dependency issues. 9198186Sgordon# $1 - filename of script, in /etc/rc.d, to run 92231667Sdougb# $2 - name of the script's rcvar (minus the _enable) 9398186Sgordon# 9498186Sgordonforce_depend() 9598186Sgordon{ 96231667Sdougb local _depend _dep_rcvar 97231667Sdougb 9898186Sgordon _depend="$1" 99231667Sdougb _dep_rcvar="${2:-$1}_enable" 10098186Sgordon 101231667Sdougb [ -n "$rc_fast" ] && ! checkyesno always_force_depends && 102231667Sdougb checkyesno $_dep_rcvar && return 0 103231667Sdougb 104231667Sdougb /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 105231667Sdougb 10698186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 107146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 10898186Sgordon warn "Unable to force ${_depend}. It may already be running." 10998186Sgordon return 1 11098186Sgordon fi 11198186Sgordon} 11298186Sgordon 11398186Sgordon# 11478344Sobrien# checkyesno var 11578344Sobrien# Test $1 variable, and warn if not set to YES or NO. 11678344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 11778344Sobrien# 11878344Sobriencheckyesno() 11978344Sobrien{ 12078344Sobrien eval _value=\$${1} 12198186Sgordon debug "checkyesno: $1 is set to $_value." 12278344Sobrien case $_value in 12378344Sobrien 12478344Sobrien # "yes", "true", "on", or "1" 12578344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 12678344Sobrien return 0 12778344Sobrien ;; 12878344Sobrien 12978344Sobrien # "no", "false", "off", or "0" 13078344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 13178344Sobrien return 1 13278344Sobrien ;; 13378344Sobrien *) 134229822Sdougb warn "\$${1} is not set properly - see rc.conf(5)." 13578344Sobrien return 1 13678344Sobrien ;; 13778344Sobrien esac 13878344Sobrien} 13978344Sobrien 140157473Sflz# 14198186Sgordon# reverse_list list 14298186Sgordon# print the list in reverse order 14378344Sobrien# 14498186Sgordonreverse_list() 14598186Sgordon{ 14698186Sgordon _revlist= 147126286Smtm for _revfile; do 14898186Sgordon _revlist="$_revfile $_revlist" 14998186Sgordon done 15098186Sgordon echo $_revlist 15198186Sgordon} 15298186Sgordon 153169668Smtm# stop_boot always 154169668Smtm# If booting directly to multiuser or $always is enabled, 155169668Smtm# send SIGTERM to the parent (/etc/rc) to abort the boot. 156169668Smtm# Otherwise just exit. 15778344Sobrien# 158169668Smtmstop_boot() 159169668Smtm{ 160169668Smtm local always 161169668Smtm 162178776Smaxim case $1 in 163178776Smaxim # "yes", "true", "on", or "1" 164178770Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 165169668Smtm always=true 166178770Smtm ;; 167178770Smtm *) 168169668Smtm always=false 169178770Smtm ;; 170178775Smaxim esac 171169668Smtm if [ "$autoboot" = yes -o "$always" = true ]; then 172169668Smtm echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 173169668Smtm kill -TERM ${RC_PID} 174169668Smtm fi 175169668Smtm exit 1 176169668Smtm} 177169668Smtm 178169668Smtm# 17998186Sgordon# mount_critical_filesystems type 18098186Sgordon# Go through the list of critical filesystems as provided in 18198186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 18298186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 18398186Sgordon# 18478344Sobrienmount_critical_filesystems() 18578344Sobrien{ 18698186Sgordon eval _fslist=\$critical_filesystems_${1} 18778344Sobrien for _fs in $_fslist; do 18878344Sobrien mount | ( 189126285Smtm _ismounted=false 19078344Sobrien while read what _on on _type type; do 19178344Sobrien if [ $on = $_fs ]; then 192126285Smtm _ismounted=true 19378344Sobrien fi 19478344Sobrien done 195126285Smtm if $_ismounted; then 196126285Smtm : 197126285Smtm else 19878344Sobrien mount $_fs >/dev/null 2>&1 19978344Sobrien fi 20098186Sgordon ) 20178344Sobrien done 20278344Sobrien} 20378344Sobrien 20478344Sobrien# 20598186Sgordon# check_pidfile pidfile procname [interpreter] 20698186Sgordon# Parses the first line of pidfile for a PID, and ensures 20778344Sobrien# that the process is running and matches procname. 20898186Sgordon# Prints the matching PID upon success, nothing otherwise. 20998186Sgordon# interpreter is optional; see _find_processes() for details. 21078344Sobrien# 21178344Sobriencheck_pidfile() 21278344Sobrien{ 21378344Sobrien _pidfile=$1 21478344Sobrien _procname=$2 21598186Sgordon _interpreter=$3 21678344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 21798186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 21878344Sobrien fi 21978344Sobrien if [ ! -f $_pidfile ]; then 220131061Smtm debug "pid file ($_pidfile): not readable." 22178344Sobrien return 22278344Sobrien fi 22378344Sobrien read _pid _junk < $_pidfile 22478344Sobrien if [ -z "$_pid" ]; then 225139949Skeramida debug "pid file ($_pidfile): no pid in file." 22678344Sobrien return 22778344Sobrien fi 22898186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 22978344Sobrien} 23078344Sobrien 23178344Sobrien# 23298186Sgordon# check_process procname [interpreter] 23378344Sobrien# Ensures that a process (or processes) named procname is running. 23498186Sgordon# Prints a list of matching PIDs. 23598186Sgordon# interpreter is optional; see _find_processes() for details. 23678344Sobrien# 23778344Sobriencheck_process() 23878344Sobrien{ 23978344Sobrien _procname=$1 24098186Sgordon _interpreter=$2 24178344Sobrien if [ -z "$_procname" ]; then 24298186Sgordon err 3 'USAGE: check_process procname [interpreter]' 24378344Sobrien fi 24498186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 24598186Sgordon} 24698186Sgordon 24798186Sgordon# 24898186Sgordon# _find_processes procname interpreter psargs 24998186Sgordon# Search for procname in the output of ps generated by psargs. 25098186Sgordon# Prints the PIDs of any matching processes, space separated. 25198186Sgordon# 25298186Sgordon# If interpreter == ".", check the following variations of procname 25398186Sgordon# against the first word of each command: 25498186Sgordon# procname 25598186Sgordon# `basename procname` 25698186Sgordon# `basename procname` + ":" 25798186Sgordon# "(" + `basename procname` + ")" 258155719Sceri# "[" + `basename procname` + "]" 25998186Sgordon# 26098186Sgordon# If interpreter != ".", read the first line of procname, remove the 26198186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 26298186Sgordon# match that against each command, either as is, or with extra words 263157841Sflz# at the end. As an alternative, to deal with interpreted daemons 264157841Sflz# using perl, the basename of the interpreter plus a colon is also 265157841Sflz# tried as the prefix to procname. 26698186Sgordon# 26798186Sgordon_find_processes() 26898186Sgordon{ 26998186Sgordon if [ $# -ne 3 ]; then 27098186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 27198186Sgordon fi 27298186Sgordon _procname=$1 27398186Sgordon _interpreter=$2 27498186Sgordon _psargs=$3 27598186Sgordon 27678344Sobrien _pref= 27798186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 278242183Screes _script="${_chroot}${_chroot:+/}$_procname" 279242183Screes if [ -r "$_script" ]; then 280170282Syar read _interp < $_script # read interpreter name 281170282Syar case "$_interp" in 282170282Syar \#!*) 283170282Syar _interp=${_interp#\#!} # strip #! 284170282Syar set -- $_interp 285170282Syar case $1 in 286170282Syar */bin/env) 287170282Syar shift # drop env to get real name 288170282Syar ;; 289170282Syar esac 290170282Syar if [ $_interpreter != $1 ]; then 291170282Syar warn "\$command_interpreter $_interpreter != $1" 292170282Syar fi 293170282Syar ;; 294170282Syar *) 295170282Syar warn "no shebang line in $_script" 296170282Syar set -- $_interpreter 297170282Syar ;; 298170282Syar esac 299170282Syar else 300170282Syar warn "cannot read shebang line from $_script" 301170282Syar set -- $_interpreter 30278344Sobrien fi 30398186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 304157841Sflz _interpbn=${1##*/} 30598186Sgordon _fp_args='_argv' 30698186Sgordon _fp_match='case "$_argv" in 307245250Ssmh ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' 30898186Sgordon else # a normal daemon 30998186Sgordon _procnamebn=${_procname##*/} 31098186Sgordon _fp_args='_arg0 _argv' 31198186Sgordon _fp_match='case "$_arg0" in 312151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 31398186Sgordon fi 31498186Sgordon 315161435Syar _proccheck="\ 316161436Syar $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | 317157657Sflz while read _npid _jid '"$_fp_args"'; do 318161436Syar '"$_fp_match"' 319157657Sflz if [ "$JID" -eq "$_jid" ]; 320157657Sflz then echo -n "$_pref$_npid"; 321157657Sflz _pref=" "; 322157657Sflz fi 32398186Sgordon ;; 32498186Sgordon esac 32598186Sgordon done' 32698186Sgordon 327114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 32898186Sgordon eval $_proccheck 32998186Sgordon} 33098186Sgordon 331264243Sdteske# sort_lite [-b] [-n] [-k POS] [-t SEP] 332264243Sdteske# A lite version of sort(1) (supporting a few options) that can be used 333264243Sdteske# before the real sort(1) is available (e.g., in scripts that run prior 334264243Sdteske# to mountcritremote). Requires only shell built-in functionality. 33598186Sgordon# 336264243Sdteskesort_lite() 337264243Sdteske{ 338264243Sdteske local funcname=sort_lite 339264243Sdteske local sort_sep="$IFS" sort_ignore_leading_space= 340264243Sdteske local sort_field=0 sort_strict_fields= sort_numeric= 341264243Sdteske local nitems=0 skip_leading=0 trim= 342264243Sdteske 343264243Sdteske local OPTIND flag 344264243Sdteske while getopts bnk:t: flag; do 345264243Sdteske case "$flag" in 346264243Sdteske b) sort_ignore_leading_space=1 ;; 347264243Sdteske n) sort_numeric=1 sort_ignore_leading_space=1 ;; 348264243Sdteske k) sort_field="${OPTARG%%,*}" ;; # only up to first comma 349264243Sdteske # NB: Unlike sort(1) only one POS allowed 350264243Sdteske t) sort_sep="$OPTARG" 351264243Sdteske if [ ${#sort_sep} -gt 1 ]; then 352264243Sdteske echo "$funcname: multi-character tab \`$sort_sep'" >&2 353264243Sdteske return 1 354264243Sdteske fi 355264243Sdteske sort_strict_fields=1 356264243Sdteske ;; 357264243Sdteske \?) return 1 ;; 358264243Sdteske esac 359264243Sdteske done 360264243Sdteske shift $(( $OPTIND - 1 )) 361264243Sdteske 362264243Sdteske # Create transformation pattern to trim leading text if desired 363264243Sdteske case "$sort_field" in 364264243Sdteske ""|[!0-9]*|*[!0-9.]*) 365264243Sdteske echo "$funcname: invalid sort field \`$sort_field'" >&2 366264243Sdteske return 1 367264243Sdteske ;; 368264243Sdteske *.*) 369264243Sdteske skip_leading=${sort_field#*.} sort_field=${sort_field%%.*} 370264243Sdteske while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do 371264243Sdteske trim="$trim?" skip_leading=$(( $skip_leading - 1 )) 372264243Sdteske done 373264243Sdteske esac 374264243Sdteske 375264243Sdteske # Copy input to series of local numbered variables 376264243Sdteske # NB: IFS of NULL preserves leading whitespace 377264243Sdteske local LINE 378264243Sdteske while IFS= read -r LINE || [ "$LINE" ]; do 379264243Sdteske nitems=$(( $nitems + 1 )) 380264243Sdteske local src_$nitems="$LINE" 381264243Sdteske done 382264243Sdteske 383264243Sdteske # 384264243Sdteske # Sort numbered locals using insertion sort 385264243Sdteske # 386264243Sdteske local curitem curitem_orig curitem_mod curitem_haskey 387264243Sdteske local dest dest_orig dest_mod dest_haskey 388264243Sdteske local d gt n 389264243Sdteske local i=1 390264243Sdteske while [ $i -le $nitems ]; do 391264243Sdteske curitem_haskey=1 # Assume sort field (-k POS) exists 392264243Sdteske eval curitem=\"\$src_$i\" 393264243Sdteske curitem_mod="$curitem" # for modified comparison 394264243Sdteske curitem_orig="$curitem" # for original comparison 395264243Sdteske 396264243Sdteske # Trim leading whitespace if desired 397264243Sdteske if [ "$sort_ignore_leading_space" ]; then 398264243Sdteske while case "$curitem_orig" in 399264243Sdteske [$IFS]*) : ;; *) false; esac 400264243Sdteske do 401264243Sdteske curitem_orig="${curitem_orig#?}" 402264243Sdteske done 403264243Sdteske curitem_mod="$curitem_orig" 404264243Sdteske fi 405264243Sdteske 406264243Sdteske # Shift modified comparison value if sort field (-k POS) is > 1 407264243Sdteske n=$sort_field 408264243Sdteske while [ $n -gt 1 ]; do 409264243Sdteske case "$curitem_mod" in 410264243Sdteske *[$sort_sep]*) 411264243Sdteske # Cut text up-to (and incl.) first separator 412264243Sdteske curitem_mod="${curitem_mod#*[$sort_sep]}" 413264243Sdteske 414264243Sdteske # Skip NULLs unless strict field splitting 415264243Sdteske [ "$sort_strict_fields" ] || 416264243Sdteske [ "${curitem_mod%%[$sort_sep]*}" ] || 417264243Sdteske [ $n -eq 2 ] || 418264243Sdteske continue 419264243Sdteske ;; 420264243Sdteske *) 421264243Sdteske # Asked for a field that doesn't exist 422264243Sdteske curitem_haskey= break 423264243Sdteske esac 424264243Sdteske n=$(( $n - 1 )) 425264243Sdteske done 426264243Sdteske 427264243Sdteske # Trim trailing words if sort field >= 1 428264243Sdteske [ $sort_field -ge 1 -a "$sort_numeric" ] && 429264243Sdteske curitem_mod="${curitem_mod%%[$sort_sep]*}" 430264243Sdteske 431264243Sdteske # Apply optional trim (-k POS.TRIM) to cut leading characters 432264243Sdteske curitem_mod="${curitem_mod#$trim}" 433264243Sdteske 434264243Sdteske # Determine the type of modified comparison to use initially 435264243Sdteske # NB: Prefer numerical if requested but fallback to standard 436264243Sdteske case "$curitem_mod" in 437264243Sdteske ""|[!0-9]*) # NULL or begins with non-number 438264243Sdteske gt=">" 439264243Sdteske [ "$sort_numeric" ] && curitem_mod=0 440264243Sdteske ;; 441264243Sdteske *) 442264243Sdteske if [ "$sort_numeric" ]; then 443264243Sdteske gt="-gt" 444264243Sdteske curitem_mod="${curitem_mod%%[!0-9]*}" 445264243Sdteske # NB: trailing non-digits removed 446264243Sdteske # otherwise numeric comparison fails 447264243Sdteske else 448264243Sdteske gt=">" 449264243Sdteske fi 450264243Sdteske esac 451264243Sdteske 452264243Sdteske # If first time through, short-circuit below position-search 453264243Sdteske if [ $i -le 1 ]; then 454264243Sdteske d=0 455264243Sdteske else 456264243Sdteske d=1 457264243Sdteske fi 458264243Sdteske 459264243Sdteske # 460264243Sdteske # Find appropriate element position 461264243Sdteske # 462264243Sdteske while [ $d -gt 0 ] 463264243Sdteske do 464264243Sdteske dest_haskey=$curitem_haskey 465264243Sdteske eval dest=\"\$dest_$d\" 466264243Sdteske dest_mod="$dest" # for modified comparison 467264243Sdteske dest_orig="$dest" # for original comparison 468264243Sdteske 469264243Sdteske # Trim leading whitespace if desired 470264243Sdteske if [ "$sort_ignore_leading_space" ]; then 471264243Sdteske while case "$dest_orig" in 472264243Sdteske [$IFS]*) : ;; *) false; esac 473264243Sdteske do 474264243Sdteske dest_orig="${dest_orig#?}" 475264243Sdteske done 476264243Sdteske dest_mod="$dest_orig" 477264243Sdteske fi 478264243Sdteske 479264243Sdteske # Shift modified value if sort field (-k POS) is > 1 480264243Sdteske n=$sort_field 481264243Sdteske while [ $n -gt 1 ]; do 482264243Sdteske case "$dest_mod" in 483264243Sdteske *[$sort_sep]*) 484264243Sdteske # Cut text up-to (and incl.) 1st sep 485264243Sdteske dest_mod="${dest_mod#*[$sort_sep]}" 486264243Sdteske 487264243Sdteske # Skip NULLs unless strict fields 488264243Sdteske [ "$sort_strict_fields" ] || 489264243Sdteske [ "${dest_mod%%[$sort_sep]*}" ] || 490264243Sdteske [ $n -eq 2 ] || 491264243Sdteske continue 492264243Sdteske ;; 493264243Sdteske *) 494264243Sdteske # Asked for a field that doesn't exist 495264243Sdteske dest_haskey= break 496264243Sdteske esac 497264243Sdteske n=$(( $n - 1 )) 498264243Sdteske done 499264243Sdteske 500264243Sdteske # Trim trailing words if sort field >= 1 501264243Sdteske [ $sort_field -ge 1 -a "$sort_numeric" ] && 502264243Sdteske dest_mod="${dest_mod%%[$sort_sep]*}" 503264243Sdteske 504264243Sdteske # Apply optional trim (-k POS.TRIM), cut leading chars 505264243Sdteske dest_mod="${dest_mod#$trim}" 506264243Sdteske 507264243Sdteske # Determine type of modified comparison to use 508264243Sdteske # NB: Prefer numerical if requested, fallback to std 509264243Sdteske case "$dest_mod" in 510264243Sdteske ""|[!0-9]*) # NULL or begins with non-number 511264243Sdteske gt=">" 512264243Sdteske [ "$sort_numeric" ] && dest_mod=0 513264243Sdteske ;; 514264243Sdteske *) 515264243Sdteske if [ "$sort_numeric" ]; then 516264243Sdteske gt="-gt" 517264243Sdteske dest_mod="${dest_mod%%[!0-9]*}" 518264243Sdteske # NB: kill trailing non-digits 519264243Sdteske # for numeric comparison safety 520264243Sdteske else 521264243Sdteske gt=">" 522264243Sdteske fi 523264243Sdteske esac 524264243Sdteske 525264243Sdteske # Break if we've found the proper element position 526264243Sdteske if [ "$curitem_haskey" -a "$dest_haskey" ]; then 527264243Sdteske if [ "$dest_mod" = "$curitem_mod" ]; then 528264243Sdteske [ "$dest_orig" ">" "$curitem_orig" ] && 529264243Sdteske break 530264243Sdteske elif [ "$dest_mod" $gt "$curitem_mod" ] \ 531264243Sdteske 2> /dev/null 532264243Sdteske then 533264243Sdteske break 534264243Sdteske fi 535264243Sdteske else 536264243Sdteske [ "$dest_orig" ">" "$curitem_orig" ] && break 537264243Sdteske fi 538264243Sdteske 539264243Sdteske # Break if we've hit the end 540264243Sdteske [ $d -ge $i ] && break 541264243Sdteske 542264243Sdteske d=$(( $d + 1 )) 543264243Sdteske done 544264243Sdteske 545264243Sdteske # Shift remaining positions forward, making room for new item 546264243Sdteske n=$i 547264243Sdteske while [ $n -ge $d ]; do 548264243Sdteske # Shift destination item forward one placement 549264243Sdteske eval dest_$(( $n + 1 ))=\"\$dest_$n\" 550264243Sdteske n=$(( $n - 1 )) 551264243Sdteske done 552264243Sdteske 553264243Sdteske # Place the element 554264243Sdteske if [ $i -eq 1 ]; then 555264243Sdteske local dest_1="$curitem" 556264243Sdteske else 557264243Sdteske local dest_$d="$curitem" 558264243Sdteske fi 559264243Sdteske 560264243Sdteske i=$(( $i + 1 )) 561264243Sdteske done 562264243Sdteske 563264243Sdteske # Print sorted results 564264243Sdteske d=1 565264243Sdteske while [ $d -le $nitems ]; do 566264243Sdteske eval echo \"\$dest_$d\" 567264243Sdteske d=$(( $d + 1 )) 568264243Sdteske done 569264243Sdteske} 570264243Sdteske 571264243Sdteske# 57298186Sgordon# wait_for_pids pid [pid ...] 57398186Sgordon# spins until none of the pids exist 57498186Sgordon# 57598186Sgordonwait_for_pids() 57698186Sgordon{ 577206248Sdougb local _list _prefix _nlist _j 578206248Sdougb 579126286Smtm _list="$@" 58098186Sgordon if [ -z "$_list" ]; then 58198186Sgordon return 58298186Sgordon fi 58398186Sgordon _prefix= 58498186Sgordon while true; do 58598186Sgordon _nlist=""; 58698186Sgordon for _j in $_list; do 58798186Sgordon if kill -0 $_j 2>/dev/null; then 58898186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 589206248Sdougb [ -n "$_prefix" ] && sleep 1 59098186Sgordon fi 59198186Sgordon done 59298186Sgordon if [ -z "$_nlist" ]; then 59398186Sgordon break 59478344Sobrien fi 59598186Sgordon _list=$_nlist 59698186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 59798186Sgordon _prefix=", " 598206248Sdougb pwait $_list 2>/dev/null 59978344Sobrien done 60098186Sgordon if [ -n "$_prefix" ]; then 60198186Sgordon echo "." 60298186Sgordon fi 60378344Sobrien} 60478344Sobrien 60578344Sobrien# 606220962Sdougb# get_pidfile_from_conf string file 607220962Sdougb# 608220962Sdougb# Takes a string to search for in the specified file. 609220962Sdougb# Ignores lines with traditional comment characters. 610220962Sdougb# 611220962Sdougb# Example: 612220962Sdougb# 613220962Sdougb# if get_pidfile_from_conf string file; then 614220962Sdougb# pidfile="$_pidfile_from_conf" 615220962Sdougb# else 616220962Sdougb# pidfile='appropriate default' 617220962Sdougb# fi 618220962Sdougb# 619220962Sdougbget_pidfile_from_conf() 620220962Sdougb{ 621220963Sdougb if [ -z "$1" -o -z "$2" ]; then 622220963Sdougb err 3 "USAGE: get_pidfile_from_conf string file ($name)" 623220963Sdougb fi 624220963Sdougb 625220962Sdougb local string file line 626220962Sdougb 627220962Sdougb string="$1" ; file="$2" 628220962Sdougb 629220963Sdougb if [ ! -s "$file" ]; then 630220963Sdougb err 3 "get_pidfile_from_conf: $file does not exist ($name)" 631220962Sdougb fi 632220962Sdougb 633220962Sdougb while read line; do 634220962Sdougb case "$line" in 635220962Sdougb *[#\;]*${string}*) continue ;; 636220962Sdougb *${string}*) break ;; 637220962Sdougb esac 638220962Sdougb done < $file 639220962Sdougb 640220962Sdougb if [ -n "$line" ]; then 641220962Sdougb line=${line#*/} 642220962Sdougb _pidfile_from_conf="/${line%%[\"\;]*}" 643220962Sdougb else 644220962Sdougb return 1 645220962Sdougb fi 646220962Sdougb} 647220962Sdougb 648220962Sdougb# 649197947Sdougb# check_startmsgs 650197947Sdougb# If rc_quiet is set (usually as a result of using faststart at 651197947Sdougb# boot time) check if rc_startmsgs is enabled. 652197947Sdougb# 653197947Sdougbcheck_startmsgs() 654197947Sdougb{ 655197947Sdougb if [ -n "$rc_quiet" ]; then 656197947Sdougb checkyesno rc_startmsgs 657197947Sdougb else 658197947Sdougb return 0 659197947Sdougb fi 660197947Sdougb} 661197947Sdougb 662197947Sdougb# 66398186Sgordon# run_rc_command argument 66498186Sgordon# Search for argument in the list of supported commands, which is: 66598186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 66698186Sgordon# If there's a match, run ${argument}_cmd or the default method 66798186Sgordon# (see below). 66878344Sobrien# 66998186Sgordon# If argument has a given prefix, then change the operation as follows: 67098186Sgordon# Prefix Operation 67178344Sobrien# ------ --------- 672175676Smtm# fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes 67398186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 674126303Smtm# one Set ${rcvar} to YES 675175676Smtm# quiet Don't output some diagnostics, and set rc_quiet=yes 67678344Sobrien# 67778344Sobrien# The following globals are used: 67878344Sobrien# 67998186Sgordon# Name Needed Purpose 68098186Sgordon# ---- ------ ------- 68178344Sobrien# name y Name of script. 68278344Sobrien# 68378344Sobrien# command n Full path to command. 68498186Sgordon# Not needed if ${rc_arg}_cmd is set for 68578344Sobrien# each keyword. 68678344Sobrien# 68778344Sobrien# command_args n Optional args/shell directives for command. 68878344Sobrien# 68998186Sgordon# command_interpreter n If not empty, command is interpreted, so 69098186Sgordon# call check_{pidfile,process}() appropriately. 69198186Sgordon# 692197144Shrs# desc n Description of script. 693197144Shrs# 69478344Sobrien# extra_commands n List of extra commands supported. 69578344Sobrien# 69698186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 69798186Sgordon# otherwise use check_process $command. 69898186Sgordon# In either case, only check if $command is set. 69978344Sobrien# 70098186Sgordon# procname n Process name to check for instead of $command. 70198186Sgordon# 70278344Sobrien# rcvar n This is checked with checkyesno to determine 70378344Sobrien# if the action should be run. 70478344Sobrien# 705157653Sflz# ${name}_program n Full path to command. 706157653Sflz# Meant to be used in /etc/rc.conf to override 707157653Sflz# ${command}. 708157653Sflz# 70978344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 71098186Sgordon# Requires /usr to be mounted. 71178344Sobrien# 71278344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 71378344Sobrien# (if not using ${name}_chroot). 71478344Sobrien# 71578344Sobrien# ${name}_flags n Arguments to call ${command} with. 71678344Sobrien# NOTE: $flags from the parent environment 71778344Sobrien# can be used to override this. 71878344Sobrien# 719242184Shrs# ${name}_fib n Routing table number to run ${command} with. 720242184Shrs# 72178344Sobrien# ${name}_nice n Nice level to run ${command} at. 72278344Sobrien# 72378344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 72478344Sobrien# using ${name}_chroot. 72598186Sgordon# Requires /usr to be mounted. 72678344Sobrien# 72778344Sobrien# ${name}_group n Group to run chrooted ${command} as. 72898186Sgordon# Requires /usr to be mounted. 72978344Sobrien# 73098186Sgordon# ${name}_groups n Comma separated list of supplementary groups 73198186Sgordon# to run the chrooted ${command} with. 73298186Sgordon# Requires /usr to be mounted. 73378344Sobrien# 73498186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 73578344Sobrien# Otherwise, use default command (see below) 73678344Sobrien# 73798186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 73898186Sgordon# ${rc_arg}_cmd method in the default 73998186Sgordon# operation (i.e, after checking for required 74098186Sgordon# bits and process (non)existence). 74178344Sobrien# If this completes with a non-zero exit code, 74298186Sgordon# don't run ${rc_arg}_cmd. 74378344Sobrien# 74498186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 74598186Sgordon# ${rc_arg}_cmd method, if that method 74698186Sgordon# returned a zero exit code. 74798186Sgordon# 74878344Sobrien# required_dirs n If set, check for the existence of the given 749165565Syar# directories before running a (re)start command. 75078344Sobrien# 75178344Sobrien# required_files n If set, check for the readability of the given 752165565Syar# files before running a (re)start command. 75378344Sobrien# 754165565Syar# required_modules n If set, ensure the given kernel modules are 755165565Syar# loaded before running a (re)start command. 756165565Syar# The check and possible loads are actually 757165565Syar# done after start_precmd so that the modules 758165565Syar# aren't loaded in vain, should the precmd 759165565Syar# return a non-zero status to indicate a error. 760165565Syar# If a word in the list looks like "foo:bar", 761165565Syar# "foo" is the KLD file name and "bar" is the 762165565Syar# module name. If a word looks like "foo~bar", 763165565Syar# "foo" is the KLD file name and "bar" is a 764165565Syar# egrep(1) pattern matching the module name. 765165565Syar# Otherwise the module name is assumed to be 766165565Syar# the same as the KLD file name, which is most 767165565Syar# common. See load_kld(). 768165565Syar# 76978344Sobrien# required_vars n If set, perform checkyesno on each of the 77078344Sobrien# listed variables before running the default 77178344Sobrien# (re)start command. 77278344Sobrien# 77398186Sgordon# Default behaviour for a given argument, if no override method is 77498186Sgordon# provided: 77578344Sobrien# 77698186Sgordon# Argument Default behaviour 77798186Sgordon# -------- ----------------- 77878344Sobrien# start if !running && checkyesno ${rcvar} 77978344Sobrien# ${command} 78078344Sobrien# 78178344Sobrien# stop if ${pidfile} 78298186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 78378344Sobrien# else 78498186Sgordon# rc_pid=$(check_process $command) 78598186Sgordon# kill $sig_stop $rc_pid 78698186Sgordon# wait_for_pids $rc_pid 78798186Sgordon# ($sig_stop defaults to TERM.) 78878344Sobrien# 78998186Sgordon# reload Similar to stop, except use $sig_reload instead, 79098186Sgordon# and doesn't wait_for_pids. 79178344Sobrien# $sig_reload defaults to HUP. 792151685Syar# Note that `reload' isn't provided by default, 793151685Syar# it should be enabled via $extra_commands. 79478344Sobrien# 79578344Sobrien# restart Run `stop' then `start'. 79678344Sobrien# 79798186Sgordon# status Show if ${command} is running, etc. 79878344Sobrien# 79998186Sgordon# poll Wait for ${command} to exit. 80098186Sgordon# 80198186Sgordon# rcvar Display what rc.conf variable is used (if any). 80298186Sgordon# 803255809Sdes# enabled Return true if the service is enabled. 804255809Sdes# 80598186Sgordon# Variables available to methods, and after run_rc_command() has 80698186Sgordon# completed: 80798186Sgordon# 80898186Sgordon# Variable Purpose 80998186Sgordon# -------- ------- 810126303Smtm# rc_arg Argument to command, after fast/force/one processing 81198186Sgordon# performed 81298186Sgordon# 81398186Sgordon# rc_flags Flags to start the default command with. 81498186Sgordon# Defaults to ${name}_flags, unless overridden 81598186Sgordon# by $flags from the environment. 81698186Sgordon# This variable may be changed by the precmd method. 81798186Sgordon# 81898186Sgordon# rc_pid PID of command (if appropriate) 81998186Sgordon# 82098186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 82198186Sgordon# 82298186Sgordon# rc_force Not empty if "force" was provided (q.v.) 82398186Sgordon# 824175676Smtm# rc_quiet Not empty if "quiet" was provided 82598186Sgordon# 826175676Smtm# 82778344Sobrienrun_rc_command() 82878344Sobrien{ 829116097Smtm _return=0 83098186Sgordon rc_arg=$1 83178344Sobrien if [ -z "$name" ]; then 83298186Sgordon err 3 'run_rc_command: $name is not set.' 83378344Sobrien fi 83478344Sobrien 835132892Smtm # Don't repeat the first argument when passing additional command- 836132892Smtm # line arguments to the command subroutines. 837132892Smtm # 838132892Smtm shift 1 839132892Smtm rc_extra_args="$*" 840132892Smtm 841126303Smtm _rc_prefix= 84298186Sgordon case "$rc_arg" in 84378344Sobrien fast*) # "fast" prefix; don't check pid 84498186Sgordon rc_arg=${rc_arg#fast} 84598186Sgordon rc_fast=yes 846175676Smtm rc_quiet=yes 84778344Sobrien ;; 848198216Sed force*) # "force" prefix; always run 84998186Sgordon rc_force=yes 850126303Smtm _rc_prefix=force 851126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 85278344Sobrien if [ -n "${rcvar}" ]; then 85378344Sobrien eval ${rcvar}=YES 85478344Sobrien fi 85578344Sobrien ;; 856126303Smtm one*) # "one" prefix; set ${rcvar}=yes 857126303Smtm _rc_prefix=one 858126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 859126303Smtm if [ -n "${rcvar}" ]; then 860126303Smtm eval ${rcvar}=YES 861126303Smtm fi 862126303Smtm ;; 863175676Smtm quiet*) # "quiet" prefix; omit some messages 864175676Smtm _rc_prefix=quiet 865175676Smtm rc_arg=${rc_arg#${_rc_prefix}} 866175676Smtm rc_quiet=yes 867175676Smtm ;; 86878344Sobrien esac 86978344Sobrien 870161530Sflz eval _override_command=\$${name}_program 871198162Sdougb command=${_override_command:-$command} 872161530Sflz 873255809Sdes _keywords="start stop restart rcvar enabled $extra_commands" 87498186Sgordon rc_pid= 87578344Sobrien _pidcmd= 87698186Sgordon _procname=${procname:-${command}} 87798186Sgordon 878131135Smtm # setup pid check command 879131135Smtm if [ -n "$_procname" ]; then 88078344Sobrien if [ -n "$pidfile" ]; then 88198186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 88298186Sgordon else 88398186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 88478344Sobrien fi 88578344Sobrien if [ -n "$_pidcmd" ]; then 88698186Sgordon _keywords="${_keywords} status poll" 88778344Sobrien fi 88878344Sobrien fi 88978344Sobrien 89098186Sgordon if [ -z "$rc_arg" ]; then 891150796Syar rc_usage $_keywords 89278344Sobrien fi 89378344Sobrien 894255809Sdes if [ "$rc_arg" = "enabled" ] ; then 895255809Sdes checkyesno ${rcvar} 896255809Sdes return $? 897255809Sdes fi 898255809Sdes 89978344Sobrien if [ -n "$flags" ]; then # allow override from environment 90098186Sgordon rc_flags=$flags 90178344Sobrien else 90298186Sgordon eval rc_flags=\$${name}_flags 90378344Sobrien fi 90498186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 90598186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 906242184Shrs _group=\$${name}_group _groups=\$${name}_groups \ 907242184Shrs _fib=\$${name}_fib 90878344Sobrien 90998186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 910124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 91198186Sgordon unset _user 91298186Sgordon fi 91398186Sgordon fi 91498186Sgordon 915230374Sdougb [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary 916179870Smtm 917179870Smtm for _elem in $_keywords; do 918179870Smtm if [ "$_elem" != "$rc_arg" ]; then 919179870Smtm continue 920179870Smtm fi 921206686Sdougb # if ${rcvar} is set, $1 is not "rcvar" 922206686Sdougb # and ${rc_pid} is not set, then run 92378344Sobrien # checkyesno ${rcvar} 92478344Sobrien # and return if that failed 92578344Sobrien # 926220760Sdougb if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" ] || 927220760Sdougb [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then 928179870Smtm if ! checkyesno ${rcvar}; then 929179870Smtm if [ -n "${rc_quiet}" ]; then 930179870Smtm return 0 931179870Smtm fi 932179870Smtm echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " 933179870Smtm echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " 934179870Smtm echo "instead of '${rc_arg}'." 935175676Smtm return 0 936175676Smtm fi 93778344Sobrien fi 93878344Sobrien 93978344Sobrien # if there's a custom ${XXX_cmd}, 94078344Sobrien # run that instead of the default 94178344Sobrien # 942165565Syar eval _cmd=\$${rc_arg}_cmd \ 943165565Syar _precmd=\$${rc_arg}_precmd \ 944165565Syar _postcmd=\$${rc_arg}_postcmd 945165565Syar 94678344Sobrien if [ -n "$_cmd" ]; then 947165565Syar _run_rc_precmd || return 1 948165565Syar _run_rc_doit "$_cmd $rc_extra_args" || return 1 949165565Syar _run_rc_postcmd 950116097Smtm return $_return 95178344Sobrien fi 95278344Sobrien 95398186Sgordon case "$rc_arg" in # default operations... 95478344Sobrien 95578344Sobrien status) 956165565Syar _run_rc_precmd || return 1 95798186Sgordon if [ -n "$rc_pid" ]; then 95898186Sgordon echo "${name} is running as pid $rc_pid." 95978344Sobrien else 96078344Sobrien echo "${name} is not running." 96178344Sobrien return 1 96278344Sobrien fi 963165565Syar _run_rc_postcmd 96478344Sobrien ;; 96578344Sobrien 96678344Sobrien start) 967131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 968243324Shrs if [ -z "$rc_quiet" ]; then 969243324Shrs echo 1>&2 "${name} already running? " \ 970243324Shrs "(pid=$rc_pid)." 971243324Shrs fi 972153152Syar return 1 97378344Sobrien fi 97478344Sobrien 975242183Screes if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then 976160667Syar warn "run_rc_command: cannot run $command" 977153152Syar return 1 97878344Sobrien fi 97978344Sobrien 980179946Smtm if ! _run_rc_precmd; then 981179946Smtm warn "failed precmd routine for ${name}" 982179946Smtm return 1 983179946Smtm fi 98478344Sobrien 985160668Syar # setup the full command to run 98678344Sobrien # 987197947Sdougb check_startmsgs && echo "Starting ${name}." 98878344Sobrien if [ -n "$_chroot" ]; then 98978344Sobrien _doit="\ 99078344Sobrien${_nice:+nice -n $_nice }\ 991242184Shrs${_fib:+setfib -F $_fib }\ 99278344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 99398186Sgordon$_chroot $command $rc_flags $command_args" 99478344Sobrien else 99578344Sobrien _doit="\ 996161396Syar${_chdir:+cd $_chdir && }\ 997242184Shrs${_fib:+setfib -F $_fib }\ 99898186Sgordon$command $rc_flags $command_args" 99998186Sgordon if [ -n "$_user" ]; then 100098186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 100198186Sgordon fi 1002161396Syar if [ -n "$_nice" ]; then 1003161396Syar if [ -z "$_user" ]; then 1004161396Syar _doit="sh -c \"$_doit\"" 1005201036Sdougb fi 1006161396Syar _doit="nice -n $_nice $_doit" 1007161396Syar fi 100878344Sobrien fi 100998186Sgordon 1010165565Syar # run the full command 101198186Sgordon # 1012179946Smtm if ! _run_rc_doit "$_doit"; then 1013179946Smtm warn "failed to start ${name}" 1014179946Smtm return 1 1015179946Smtm fi 101698186Sgordon 101798186Sgordon # finally, run postcmd 101898186Sgordon # 1019165565Syar _run_rc_postcmd 102078344Sobrien ;; 102178344Sobrien 102278344Sobrien stop) 102398186Sgordon if [ -z "$rc_pid" ]; then 1024153152Syar [ -n "$rc_fast" ] && return 0 1025165565Syar _run_rc_notrunning 1026153152Syar return 1 102778344Sobrien fi 102878344Sobrien 1029165565Syar _run_rc_precmd || return 1 103098186Sgordon 103198186Sgordon # send the signal to stop 103298186Sgordon # 103378344Sobrien echo "Stopping ${name}." 1034165565Syar _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") 1035165565Syar _run_rc_doit "$_doit" || return 1 103698186Sgordon 103798186Sgordon # wait for the command to exit, 103898186Sgordon # and run postcmd. 103998186Sgordon wait_for_pids $rc_pid 1040165565Syar 1041165565Syar _run_rc_postcmd 104278344Sobrien ;; 104378344Sobrien 104478344Sobrien reload) 104598186Sgordon if [ -z "$rc_pid" ]; then 1046165565Syar _run_rc_notrunning 1047153152Syar return 1 104878344Sobrien fi 1049165565Syar 1050165565Syar _run_rc_precmd || return 1 1051165565Syar 1052165565Syar _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") 1053165565Syar _run_rc_doit "$_doit" || return 1 1054165565Syar 1055165565Syar _run_rc_postcmd 105678344Sobrien ;; 105778344Sobrien 105878344Sobrien restart) 105978344Sobrien # prevent restart being called more 106078344Sobrien # than once by any given script 106178344Sobrien # 1062126285Smtm if ${_rc_restart_done:-false}; then 106378344Sobrien return 0 106478344Sobrien fi 1065126285Smtm _rc_restart_done=true 106678344Sobrien 1067165565Syar _run_rc_precmd || return 1 1068165565Syar 1069165565Syar # run those in a subshell to keep global variables 1070152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 1071165565Syar ( run_rc_command ${_rc_prefix}start $rc_extra_args ) 1072165565Syar _return=$? 1073165565Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 107498186Sgordon 1075165565Syar _run_rc_postcmd 107678344Sobrien ;; 107778344Sobrien 107898186Sgordon poll) 1079165565Syar _run_rc_precmd || return 1 108098186Sgordon if [ -n "$rc_pid" ]; then 108198186Sgordon wait_for_pids $rc_pid 108298186Sgordon fi 1083165565Syar _run_rc_postcmd 108498186Sgordon ;; 108598186Sgordon 108678344Sobrien rcvar) 1087197144Shrs echo -n "# $name" 1088197144Shrs if [ -n "$desc" ]; then 1089197144Shrs echo " : $desc" 1090197144Shrs else 1091197144Shrs echo "" 1092197144Shrs fi 1093197144Shrs echo "#" 1094230103Sdougb # Get unique vars in $rcvar 1095230103Sdougb for _v in $rcvar; do 1096197144Shrs case $v in 1097197144Shrs $_v\ *|\ *$_v|*\ $_v\ *) ;; 1098197144Shrs *) v="${v# } $_v" ;; 1099197144Shrs esac 1100197144Shrs done 1101197144Shrs 1102197144Shrs # Display variables. 1103197144Shrs for _v in $v; do 1104197144Shrs if [ -z "$_v" ]; then 1105197144Shrs continue 110678344Sobrien fi 1107197144Shrs 1108197144Shrs eval _desc=\$${_v}_desc 1109197144Shrs eval _defval=\$${_v}_defval 1110197144Shrs _h="-" 1111197144Shrs 1112197144Shrs eval echo \"$_v=\\\"\$$_v\\\"\" 1113197144Shrs # decode multiple lines of _desc 1114197144Shrs while [ -n "$_desc" ]; do 1115197144Shrs case $_desc in 1116197144Shrs *^^*) 1117197144Shrs echo "# $_h ${_desc%%^^*}" 1118197144Shrs _desc=${_desc#*^^} 1119197144Shrs _h=" " 1120197144Shrs ;; 1121197144Shrs *) 1122197144Shrs echo "# $_h ${_desc}" 1123197144Shrs break 1124197144Shrs ;; 1125197144Shrs esac 1126197144Shrs done 1127197144Shrs echo "# (default: \"$_defval\")" 1128197144Shrs done 1129197144Shrs echo "" 113078344Sobrien ;; 113178344Sobrien 113278344Sobrien *) 1133150796Syar rc_usage $_keywords 113478344Sobrien ;; 113578344Sobrien 113678344Sobrien esac 1137116097Smtm return $_return 113878344Sobrien done 113978344Sobrien 114098186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 1141150796Syar rc_usage $_keywords 1142153152Syar # not reached 114378344Sobrien} 114478344Sobrien 114578344Sobrien# 1146165565Syar# Helper functions for run_rc_command: common code. 1147165565Syar# They use such global variables besides the exported rc_* ones: 1148165565Syar# 1149165565Syar# name R/W 1150165565Syar# ------------------ 1151165565Syar# _precmd R 1152165565Syar# _postcmd R 1153165565Syar# _return W 1154165565Syar# 1155165565Syar_run_rc_precmd() 1156165565Syar{ 1157165565Syar check_required_before "$rc_arg" || return 1 1158165565Syar 1159165565Syar if [ -n "$_precmd" ]; then 1160165565Syar debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" 1161165565Syar eval "$_precmd $rc_extra_args" 1162165565Syar _return=$? 1163165565Syar 1164165565Syar # If precmd failed and force isn't set, request exit. 1165165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1166165565Syar return 1 1167165565Syar fi 1168165565Syar fi 1169165565Syar 1170165565Syar check_required_after "$rc_arg" || return 1 1171165565Syar 1172165565Syar return 0 1173165565Syar} 1174165565Syar 1175165565Syar_run_rc_postcmd() 1176165565Syar{ 1177165565Syar if [ -n "$_postcmd" ]; then 1178165565Syar debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" 1179165565Syar eval "$_postcmd $rc_extra_args" 1180165565Syar _return=$? 1181165565Syar fi 1182165565Syar return 0 1183165565Syar} 1184165565Syar 1185165565Syar_run_rc_doit() 1186165565Syar{ 1187165565Syar debug "run_rc_command: doit: $*" 1188165565Syar eval "$@" 1189165565Syar _return=$? 1190165565Syar 1191165565Syar # If command failed and force isn't set, request exit. 1192165565Syar if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then 1193165565Syar return 1 1194165565Syar fi 1195165565Syar 1196165565Syar return 0 1197165565Syar} 1198165565Syar 1199165565Syar_run_rc_notrunning() 1200165565Syar{ 1201165565Syar local _pidmsg 1202165565Syar 1203165565Syar if [ -n "$pidfile" ]; then 1204165565Syar _pidmsg=" (check $pidfile)." 1205165565Syar else 1206165565Syar _pidmsg= 1207165565Syar fi 1208165565Syar echo 1>&2 "${name} not running?${_pidmsg}" 1209165565Syar} 1210165565Syar 1211165565Syar_run_rc_killcmd() 1212165565Syar{ 1213165565Syar local _cmd 1214165565Syar 1215165565Syar _cmd="kill -$1 $rc_pid" 1216165565Syar if [ -n "$_user" ]; then 1217165565Syar _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" 1218165565Syar fi 1219165565Syar echo "$_cmd" 1220165565Syar} 1221165565Syar 1222165565Syar# 122378344Sobrien# run_rc_script file arg 122478344Sobrien# Start the script `file' with `arg', and correctly handle the 1225201038Sdougb# return value from the script. 1226201038Sdougb# If `file' ends with `.sh', it's sourced into the current environment 1227201038Sdougb# when $rc_fast_and_loose is set, otherwise it is run as a child process. 1228201038Sdougb# If `file' appears to be a backup or scratch file, ignore it. 1229201038Sdougb# Otherwise if it is executable run as a child process. 123078344Sobrien# 123178344Sobrienrun_rc_script() 123278344Sobrien{ 123378344Sobrien _file=$1 123478344Sobrien _arg=$2 123578344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 123678344Sobrien err 3 'USAGE: run_rc_script file arg' 123778344Sobrien fi 123878344Sobrien 123998186Sgordon unset name command command_args command_interpreter \ 124098186Sgordon extra_commands pidfile procname \ 1241230103Sdougb rcvar rcvars_obsolete required_dirs required_files \ 1242197144Shrs required_vars 124398186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 124498186Sgordon 124578344Sobrien case "$_file" in 1246193118Sdougb /etc/rc.d/*.sh) # no longer allowed in the base 1247193118Sdougb warn "Ignoring old-style startup script $_file" 124878344Sobrien ;; 1249153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 125098186Sgordon warn "Ignoring scratch file $_file" 125198186Sgordon ;; 125278344Sobrien *) # run in subshell 125398186Sgordon if [ -x $_file ]; then 125498186Sgordon if [ -n "$rc_fast_and_loose" ]; then 1255146490Sschweikh set $_arg; . $_file 125698186Sgordon else 1257231888Sdelphij ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 1258231888Sdelphij trap "echo Script $_file interrupted >&2 ; exit 1" 2 1259231888Sdelphij trap "echo Script $_file running >&2" 29 1260146490Sschweikh set $_arg; . $_file ) 126198186Sgordon fi 126298186Sgordon fi 126378344Sobrien ;; 126478344Sobrien esac 126578344Sobrien} 126678344Sobrien 126778344Sobrien# 1268157653Sflz# load_rc_config name 1269157653Sflz# Source in the configuration file for a given name. 127078344Sobrien# 127178344Sobrienload_rc_config() 127278344Sobrien{ 1273270698Shrs local _name _rcvar_val _var _defval _v _msg _new _d 1274157653Sflz _name=$1 1275157653Sflz if [ -z "$_name" ]; then 1276157653Sflz err 3 'USAGE: load_rc_config name' 127778344Sobrien fi 127878344Sobrien 1279219612Sdougb if ${_rc_conf_loaded:-false}; then 1280219612Sdougb : 1281219612Sdougb else 128298186Sgordon if [ -r /etc/defaults/rc.conf ]; then 128398186Sgordon debug "Sourcing /etc/defaults/rc.conf" 128498186Sgordon . /etc/defaults/rc.conf 128598186Sgordon source_rc_confs 128698186Sgordon elif [ -r /etc/rc.conf ]; then 128798186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 128898186Sgordon . /etc/rc.conf 128998186Sgordon fi 1290126285Smtm _rc_conf_loaded=true 129198186Sgordon fi 1292179872Smtm 1293270698Shrs for _d in /etc ${local_startup%*/rc.d}; do 1294270698Shrs if [ -f ${_d}/rc.conf.d/"$_name" ]; then 1295270698Shrs debug "Sourcing ${_d}/rc.conf.d/$_name" 1296270698Shrs . ${_d}/rc.conf.d/"$_name" 1297270698Shrs elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then 1298270698Shrs local _rc 1299270698Shrs for _rc in ${_d}/rc.conf.d/"$_name"/* ; do 1300270698Shrs if [ -f "$_rc" ] ; then 1301270698Shrs debug "Sourcing $_rc" 1302270698Shrs . "$_rc" 1303270698Shrs fi 1304270698Shrs done 1305270698Shrs fi 1306270698Shrs done 1307270698Shrs 1308197144Shrs # Set defaults if defined. 1309230103Sdougb for _var in $rcvar; do 1310223227Sjilles eval _defval=\$${_var}_defval 1311197144Shrs if [ -n "$_defval" ]; then 1312197144Shrs eval : \${$_var:=\$${_var}_defval} 1313197144Shrs fi 1314197144Shrs done 1315197144Shrs 1316197144Shrs # check obsolete rc.conf variables 1317197144Shrs for _var in $rcvars_obsolete; do 1318223227Sjilles eval _v=\$$_var 1319223227Sjilles eval _msg=\$${_var}_obsolete_msg 1320223227Sjilles eval _new=\$${_var}_newvar 1321197144Shrs case $_v in 1322197144Shrs "") 1323197144Shrs ;; 1324197144Shrs *) 1325197144Shrs if [ -z "$_new" ]; then 1326197144Shrs _msg="Ignored." 1327197144Shrs else 1328197144Shrs eval $_new=\"\$$_var\" 1329197144Shrs if [ -z "$_msg" ]; then 1330197144Shrs _msg="Use \$$_new instead." 1331197144Shrs fi 1332197144Shrs fi 1333197144Shrs warn "\$$_var is obsolete. $_msg" 1334197144Shrs ;; 1335197144Shrs esac 1336197144Shrs done 133778344Sobrien} 1338201036Sdougb 1339157473Sflz# 1340157653Sflz# load_rc_config_var name var 1341157653Sflz# Read the rc.conf(5) var for name and set in the 1342157473Sflz# current shell, using load_rc_config in a subshell to prevent 1343157473Sflz# unwanted side effects from other variable assignments. 1344157473Sflz# 1345157473Sflzload_rc_config_var() 1346157473Sflz{ 1347157473Sflz if [ $# -ne 2 ]; then 1348157653Sflz err 3 'USAGE: load_rc_config_var name var' 1349157473Sflz fi 1350157473Sflz eval $(eval '( 1351157473Sflz load_rc_config '$1' >/dev/null; 1352157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 1353157473Sflz echo '$2'=\'\''${'$2'}\'\''; 1354157473Sflz fi 1355157473Sflz )' ) 1356157473Sflz} 135778344Sobrien 135878344Sobrien# 135978344Sobrien# rc_usage commands 136078344Sobrien# Print a usage string for $0, with `commands' being a list of 136178344Sobrien# valid commands. 136278344Sobrien# 136378344Sobrienrc_usage() 136478344Sobrien{ 1365230007Srea echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" 136678344Sobrien 136778344Sobrien _sep= 1368126286Smtm for _elem; do 136978344Sobrien echo -n 1>&2 "$_sep$_elem" 137078344Sobrien _sep="|" 137178344Sobrien done 137278344Sobrien echo 1>&2 ")" 137378344Sobrien exit 1 137478344Sobrien} 137578344Sobrien 137678344Sobrien# 137778344Sobrien# err exitval message 137878344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 137978344Sobrien# 138078344Sobrienerr() 138178344Sobrien{ 138278344Sobrien exitval=$1 138378344Sobrien shift 138478344Sobrien 1385106643Sgordon if [ -x /usr/bin/logger ]; then 1386106643Sgordon logger "$0: ERROR: $*" 1387106643Sgordon fi 1388106643Sgordon echo 1>&2 "$0: ERROR: $*" 138978344Sobrien exit $exitval 139078344Sobrien} 139178344Sobrien 139278344Sobrien# 139378344Sobrien# warn message 139478344Sobrien# Display message to stderr and log to the syslog. 139578344Sobrien# 139678344Sobrienwarn() 139778344Sobrien{ 1398106643Sgordon if [ -x /usr/bin/logger ]; then 1399106643Sgordon logger "$0: WARNING: $*" 1400106643Sgordon fi 1401106643Sgordon echo 1>&2 "$0: WARNING: $*" 140278344Sobrien} 140398186Sgordon 140498186Sgordon# 140598186Sgordon# info message 140698186Sgordon# Display informational message to stdout and log to syslog. 140798186Sgordon# 140898186Sgordoninfo() 140998186Sgordon{ 1410119170Smtm case ${rc_info} in 1411119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1412119170Smtm if [ -x /usr/bin/logger ]; then 1413119170Smtm logger "$0: INFO: $*" 1414119170Smtm fi 1415119170Smtm echo "$0: INFO: $*" 1416119170Smtm ;; 1417119170Smtm esac 141898186Sgordon} 141998186Sgordon 142098186Sgordon# 142198186Sgordon# debug message 1422106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 142398186Sgordon# BEWARE that you don't call any subroutine that itself calls this 142498186Sgordon# function. 142598186Sgordon# 142698186Sgordondebug() 142798186Sgordon{ 142898186Sgordon case ${rc_debug} in 142998186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1430106700Sgordon if [ -x /usr/bin/logger ]; then 1431162947Syar logger "$0: DEBUG: $*" 1432106700Sgordon fi 1433146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 143498186Sgordon ;; 143598186Sgordon esac 143698186Sgordon} 143798186Sgordon 143898186Sgordon# 143998186Sgordon# backup_file action file cur backup 144098186Sgordon# Make a backup copy of `file' into `cur', and save the previous 144198186Sgordon# version of `cur' as `backup' or use rcs for archiving. 144298186Sgordon# 144398186Sgordon# This routine checks the value of the backup_uses_rcs variable, 144498186Sgordon# which can be either YES or NO. 144598186Sgordon# 144698186Sgordon# The `action' keyword can be one of the following: 144798186Sgordon# 144898186Sgordon# add `file' is now being backed up (and is possibly 144998186Sgordon# being reentered into the backups system). `cur' 145098186Sgordon# is created and RCS files, if necessary, are 145198186Sgordon# created as well. 145298186Sgordon# 145398186Sgordon# update `file' has changed and needs to be backed up. 145498186Sgordon# If `cur' exists, it is copied to to `back' or 145598186Sgordon# checked into RCS (if the repository file is old), 145698186Sgordon# and then `file' is copied to `cur'. Another RCS 145798186Sgordon# check in done here if RCS is being used. 145898186Sgordon# 145998186Sgordon# remove `file' is no longer being tracked by the backups 146098186Sgordon# system. If RCS is not being used, `cur' is moved 146198186Sgordon# to `back', otherwise an empty file is checked in, 146298186Sgordon# and then `cur' is removed. 146398186Sgordon# 146498186Sgordon# 146598186Sgordonbackup_file() 146698186Sgordon{ 146798186Sgordon _action=$1 146898186Sgordon _file=$2 146998186Sgordon _cur=$3 147098186Sgordon _back=$4 147198186Sgordon 147298186Sgordon if checkyesno backup_uses_rcs; then 147398186Sgordon _msg0="backup archive" 147498186Sgordon _msg1="update" 147598186Sgordon 147698186Sgordon # ensure that history file is not locked 147798186Sgordon if [ -f $_cur,v ]; then 147898186Sgordon rcs -q -u -U -M $_cur 147998186Sgordon fi 148098186Sgordon 148198186Sgordon # ensure after switching to rcs that the 148298186Sgordon # current backup is not lost 148398186Sgordon if [ -f $_cur ]; then 148498186Sgordon # no archive, or current newer than archive 148598186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 148698186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 148798186Sgordon rcs -q -kb -U $_cur 148898186Sgordon co -q -f -u $_cur 148998186Sgordon fi 149098186Sgordon fi 149198186Sgordon 149298186Sgordon case $_action in 149398186Sgordon add|update) 149498186Sgordon cp -p $_file $_cur 149598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 149698186Sgordon rcs -q -kb -U $_cur 149798186Sgordon co -q -f -u $_cur 149898186Sgordon chown root:wheel $_cur $_cur,v 149998186Sgordon ;; 150098186Sgordon remove) 150198186Sgordon cp /dev/null $_cur 150298186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 150398186Sgordon rcs -q -kb -U $_cur 150498186Sgordon chown root:wheel $_cur $_cur,v 150598186Sgordon rm $_cur 150698186Sgordon ;; 150798186Sgordon esac 150898186Sgordon else 150998186Sgordon case $_action in 151098186Sgordon add|update) 151198186Sgordon if [ -f $_cur ]; then 151298186Sgordon cp -p $_cur $_back 151398186Sgordon fi 151498186Sgordon cp -p $_file $_cur 151598186Sgordon chown root:wheel $_cur 151698186Sgordon ;; 151798186Sgordon remove) 151898186Sgordon mv -f $_cur $_back 151998186Sgordon ;; 152098186Sgordon esac 152198186Sgordon fi 152298186Sgordon} 1523119166Smtm 1524123344Smtm# make_symlink src link 1525123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1526123344Smtm# directory in which link is to be created does not exist 1527123344Smtm# a warning will be displayed and an error will be returned. 1528229783Suqs# Returns 0 on success, 1 otherwise. 1529119166Smtm# 1530123344Smtmmake_symlink() 1531119166Smtm{ 1532123344Smtm local src link linkdir _me 1533123344Smtm src="$1" 1534123344Smtm link="$2" 1535123344Smtm linkdir="`dirname $link`" 1536123344Smtm _me="make_symlink()" 1537119166Smtm 1538123344Smtm if [ -z "$src" -o -z "$link" ]; then 1539123344Smtm warn "$_me: requires two arguments." 1540119166Smtm return 1 1541119166Smtm fi 1542123344Smtm if [ ! -d "$linkdir" ]; then 1543160667Syar warn "$_me: the directory $linkdir does not exist." 1544119166Smtm return 1 1545119166Smtm fi 1546146490Sschweikh if ! ln -sf $src $link; then 1547123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1548119166Smtm return 1 1549119166Smtm fi 1550119166Smtm return 0 1551119166Smtm} 1552119166Smtm 1553119166Smtm# devfs_rulesets_from_file file 1554119166Smtm# Reads a set of devfs commands from file, and creates 1555119166Smtm# the specified rulesets with their rules. Returns non-zero 1556119166Smtm# if there was an error. 1557119166Smtm# 1558119166Smtmdevfs_rulesets_from_file() 1559119166Smtm{ 1560248820Savg local file _err _me _opts 1561119166Smtm file="$1" 1562119166Smtm _me="devfs_rulesets_from_file" 1563119166Smtm _err=0 1564119166Smtm 1565119166Smtm if [ -z "$file" ]; then 1566119166Smtm warn "$_me: you must specify a file" 1567119166Smtm return 1 1568119166Smtm fi 1569119166Smtm if [ ! -e "$file" ]; then 1570119166Smtm debug "$_me: no such file ($file)" 1571119166Smtm return 0 1572119166Smtm fi 1573248820Savg 1574248820Savg # Disable globbing so that the rule patterns are not expanded 1575248820Savg # by accident with matching filesystem entries. 1576248820Savg _opts=$-; set -f 1577248820Savg 1578119166Smtm debug "reading rulesets from file ($file)" 1579119166Smtm { while read line 1580119166Smtm do 1581119166Smtm case $line in 1582119166Smtm \#*) 1583119166Smtm continue 1584119166Smtm ;; 1585119166Smtm \[*\]*) 1586119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1587119166Smtm if [ -z "$rulenum" ]; then 1588119166Smtm warn "$_me: cannot extract rule number ($line)" 1589119166Smtm _err=1 1590119166Smtm break 1591119166Smtm fi 1592119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1593119166Smtm if [ -z "$rulename" ]; then 1594119166Smtm warn "$_me: cannot extract rule name ($line)" 1595119166Smtm _err=1 1596119166Smtm break; 1597119166Smtm fi 1598119166Smtm eval $rulename=\$rulenum 1599119166Smtm debug "found ruleset: $rulename=$rulenum" 1600146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1601119166Smtm _err=1 1602119166Smtm break 1603119166Smtm fi 1604119166Smtm ;; 1605119166Smtm *) 1606119166Smtm rulecmd="${line%%"\#*"}" 1607119166Smtm # evaluate the command incase it includes 1608119166Smtm # other rules 1609119166Smtm if [ -n "$rulecmd" ]; then 1610119166Smtm debug "adding rule ($rulecmd)" 1611119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1612119166Smtm then 1613119166Smtm _err=1 1614119166Smtm break 1615119166Smtm fi 1616119166Smtm fi 1617119166Smtm ;; 1618119166Smtm esac 1619119166Smtm if [ $_err -ne 0 ]; then 1620119166Smtm debug "error in $_me" 1621119166Smtm break 1622119166Smtm fi 1623119166Smtm done } < $file 1624248820Savg case $_opts in *f*) ;; *) set +f ;; esac 1625119166Smtm return $_err 1626119166Smtm} 1627119166Smtm 1628119166Smtm# devfs_init_rulesets 1629119166Smtm# Initializes rulesets from configuration files. Returns 1630119166Smtm# non-zero if there was an error. 1631119166Smtm# 1632119166Smtmdevfs_init_rulesets() 1633119166Smtm{ 1634119166Smtm local file _me 1635119166Smtm _me="devfs_init_rulesets" 1636119166Smtm 1637119166Smtm # Go through this only once 1638119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1639119166Smtm debug "$_me: devfs rulesets already initialized" 1640119166Smtm return 1641119166Smtm fi 1642146490Sschweikh for file in $devfs_rulesets; do 1643217090Sjh if ! devfs_rulesets_from_file $file; then 1644217090Sjh warn "$_me: could not read rules from $file" 1645217090Sjh return 1 1646217090Sjh fi 1647119166Smtm done 1648119166Smtm devfs_rulesets_init=1 1649119166Smtm debug "$_me: devfs rulesets initialized" 1650119166Smtm return 0 1651119166Smtm} 1652119166Smtm 1653119166Smtm# devfs_set_ruleset ruleset [dir] 1654151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1655119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1656119166Smtm# Returns non-zero if it could not set it successfully. 1657119166Smtm# 1658119166Smtmdevfs_set_ruleset() 1659119166Smtm{ 1660119166Smtm local devdir rs _me 1661119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1662119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1663119166Smtm _me="devfs_set_ruleset" 1664119166Smtm 1665119166Smtm if [ -z "$rs" ]; then 1666119166Smtm warn "$_me: you must specify a ruleset number" 1667119166Smtm return 1 1668119166Smtm fi 1669119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1670146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1671119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1672119166Smtm return 1 1673119166Smtm fi 1674119166Smtm return 0 1675119166Smtm} 1676119166Smtm 1677119166Smtm# devfs_apply_ruleset ruleset [dir] 1678119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1679119166Smtm# The ruleset argument must be a ruleset name as specified 1680119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1681119166Smtm# if it could not apply the ruleset. 1682119166Smtm# 1683119166Smtmdevfs_apply_ruleset() 1684119166Smtm{ 1685119166Smtm local devdir rs _me 1686119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1687119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1688119166Smtm _me="devfs_apply_ruleset" 1689119166Smtm 1690119166Smtm if [ -z "$rs" ]; then 1691119166Smtm warn "$_me: you must specify a ruleset" 1692119166Smtm return 1 1693119166Smtm fi 1694119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1695146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1696119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1697119166Smtm return 1 1698119166Smtm fi 1699119166Smtm return 0 1700119166Smtm} 1701119166Smtm 1702119166Smtm# devfs_domount dir [ruleset] 1703119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1704119166Smtm# on the mount-point. It must also be a ruleset name as specified 1705119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1706119166Smtm# 1707119166Smtmdevfs_domount() 1708119166Smtm{ 1709119166Smtm local devdir rs _me 1710119166Smtm devdir="$1" 1711119166Smtm [ -n "$2" ] && rs=$2 || rs= 1712119166Smtm _me="devfs_domount()" 1713119166Smtm 1714119166Smtm if [ -z "$devdir" ]; then 1715119166Smtm warn "$_me: you must specify a mount-point" 1716119166Smtm return 1 1717119166Smtm fi 1718119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1719146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1720119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1721119166Smtm return 1 1722119166Smtm fi 1723119166Smtm if [ -n "$rs" ]; then 1724119166Smtm devfs_init_rulesets 1725119166Smtm devfs_set_ruleset $rs $devdir 1726124797Scperciva devfs -m $devdir rule applyset 1727119166Smtm fi 1728119166Smtm return 0 1729119166Smtm} 1730119166Smtm 1731127345Sbrooks# Provide a function for normalizing the mounting of memory 1732127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1733127345Sbrooks# as close as possible between 5-current and 4-stable. 1734127345Sbrooks# $1 = size 1735127345Sbrooks# $2 = mount point 1736137451Skeramida# $3 = (optional) extra mdmfs flags 1737146490Sschweikhmount_md() 1738146490Sschweikh{ 1739127345Sbrooks if [ -n "$3" ]; then 1740137451Skeramida flags="$3" 1741127345Sbrooks fi 1742149421Syar /sbin/mdmfs $flags -s $1 md $2 1743127345Sbrooks} 1744131550Scperciva 1745159828Syar# Code common to scripts that need to load a kernel module 1746159828Syar# if it isn't in the kernel yet. Syntax: 1747160666Syar# load_kld [-e regex] [-m module] file 1748159828Syar# where -e or -m chooses the way to check if the module 1749159828Syar# is already loaded: 1750160666Syar# regex is egrep'd in the output from `kldstat -v', 1751160666Syar# module is passed to `kldstat -m'. 1752160666Syar# The default way is as though `-m file' were specified. 1753159828Syarload_kld() 1754159828Syar{ 1755159828Syar local _loaded _mod _opt _re 1756159828Syar 1757159828Syar while getopts "e:m:" _opt; do 1758159828Syar case "$_opt" in 1759159828Syar e) _re="$OPTARG" ;; 1760159828Syar m) _mod="$OPTARG" ;; 1761160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1762159828Syar esac 1763159828Syar done 1764159828Syar shift $(($OPTIND - 1)) 1765160666Syar if [ $# -ne 1 ]; then 1766160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1767160666Syar fi 1768159828Syar _mod=${_mod:-$1} 1769159828Syar _loaded=false 1770159828Syar if [ -n "$_re" ]; then 1771159828Syar if kldstat -v | egrep -q -e "$_re"; then 1772159828Syar _loaded=true 1773159828Syar fi 1774159828Syar else 1775159828Syar if kldstat -q -m "$_mod"; then 1776159828Syar _loaded=true 1777159828Syar fi 1778159828Syar fi 1779159828Syar if ! $_loaded; then 1780159828Syar if ! kldload "$1"; then 1781159828Syar warn "Unable to load kernel module $1" 1782159828Syar return 1 1783160666Syar else 1784160666Syar info "$1 kernel module loaded." 1785159828Syar fi 1786160666Syar else 1787160666Syar debug "load_kld: $1 kernel module already loaded." 1788159828Syar fi 1789159828Syar return 0 1790159828Syar} 1791159828Syar 1792264243Sdteske# ltr str src dst [var] 1793149049Spjd# Change every $src in $str to $dst. 1794149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1795264243Sdteske# awk(1). If var is non-NULL, set it to the result. 1796149049Spjdltr() 1797149049Spjd{ 1798264243Sdteske local _str _src _dst _out _com _var 1799264243Sdteske _str="$1" 1800264243Sdteske _src="$2" 1801264243Sdteske _dst="$3" 1802264243Sdteske _var="$4" 1803149049Spjd _out="" 1804149049Spjd 1805264243Sdteske local IFS="${_src}" 1806149049Spjd for _com in ${_str}; do 1807149049Spjd if [ -z "${_out}" ]; then 1808149049Spjd _out="${_com}" 1809149049Spjd else 1810149049Spjd _out="${_out}${_dst}${_com}" 1811149049Spjd fi 1812149049Spjd done 1813264243Sdteske if [ -n "${_var}" ]; then 1814264243Sdteske setvar "${_var}" "${_out}" 1815264243Sdteske else 1816264243Sdteske echo "${_out}" 1817264243Sdteske fi 1818149049Spjd} 1819149049Spjd 1820149050Spjd# Creates a list of providers for GELI encryption. 1821149050Spjdgeli_make_list() 1822149050Spjd{ 1823149050Spjd local devices devices2 1824149050Spjd local provider mountpoint type options rest 1825149050Spjd 1826149050Spjd # Create list of GELI providers from fstab. 1827149050Spjd while read provider mountpoint type options rest ; do 1828155570Sflz case ":${options}" in 1829155570Sflz :*noauto*) 1830155570Sflz noauto=yes 1831155570Sflz ;; 1832155570Sflz *) 1833155570Sflz noauto=no 1834155570Sflz ;; 1835155570Sflz esac 1836155570Sflz 1837149050Spjd case ":${provider}" in 1838149050Spjd :#*) 1839149050Spjd continue 1840149050Spjd ;; 1841149050Spjd *.eli) 1842149050Spjd # Skip swap devices. 1843155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1844149050Spjd continue 1845149050Spjd fi 1846149050Spjd devices="${devices} ${provider}" 1847149050Spjd ;; 1848149050Spjd esac 1849149050Spjd done < /etc/fstab 1850149050Spjd 1851149050Spjd # Append providers from geli_devices. 1852149050Spjd devices="${devices} ${geli_devices}" 1853149050Spjd 1854149050Spjd for provider in ${devices}; do 1855149050Spjd provider=${provider%.eli} 1856149050Spjd provider=${provider#/dev/} 1857149050Spjd devices2="${devices2} ${provider}" 1858149050Spjd done 1859149050Spjd 1860149050Spjd echo ${devices2} 1861149050Spjd} 1862149050Spjd 1863153027Sdougb# Find scripts in local_startup directories that use the old syntax 1864153027Sdougb# 1865238416Skevlofind_local_scripts_old() { 1866153027Sdougb zlist='' 1867153027Sdougb slist='' 1868153027Sdougb for dir in ${local_startup}; do 1869153027Sdougb if [ -d "${dir}" ]; then 1870153027Sdougb for file in ${dir}/[0-9]*.sh; do 1871153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1872153027Sdougb continue 1873153027Sdougb zlist="$zlist $file" 1874153027Sdougb done 1875227366Sjilles for file in ${dir}/[!0-9]*.sh; do 1876153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1877153027Sdougb continue 1878153027Sdougb slist="$slist $file" 1879153027Sdougb done 1880153027Sdougb fi 1881153027Sdougb done 1882153027Sdougb} 1883153027Sdougb 1884238416Skevlofind_local_scripts_new() { 1885153027Sdougb local_rc='' 1886153027Sdougb for dir in ${local_startup}; do 1887153027Sdougb if [ -d "${dir}" ]; then 1888153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1889153027Sdougb case "$file" in 1890153027Sdougb *.sample) ;; 1891153027Sdougb *) if [ -x "$file" ]; then 1892153027Sdougb local_rc="${local_rc} ${file}" 1893153027Sdougb fi 1894153027Sdougb ;; 1895153027Sdougb esac 1896153027Sdougb done 1897153027Sdougb fi 1898153027Sdougb done 1899153027Sdougb} 1900153027Sdougb 1901165565Syar# check_required_{before|after} command 1902165565Syar# Check for things required by the command before and after its precmd, 1903165565Syar# respectively. The two separate functions are needed because some 1904165565Syar# conditions should prevent precmd from being run while other things 1905165565Syar# depend on precmd having already been run. 1906165565Syar# 1907165565Syarcheck_required_before() 1908165565Syar{ 1909165565Syar local _f 1910165565Syar 1911165565Syar case "$1" in 1912165565Syar start) 1913165565Syar for _f in $required_vars; do 1914165565Syar if ! checkyesno $_f; then 1915165565Syar warn "\$${_f} is not enabled." 1916165565Syar if [ -z "$rc_force" ]; then 1917165565Syar return 1 1918165565Syar fi 1919165565Syar fi 1920165565Syar done 1921165565Syar 1922165565Syar for _f in $required_dirs; do 1923165565Syar if [ ! -d "${_f}/." ]; then 1924165565Syar warn "${_f} is not a directory." 1925165565Syar if [ -z "$rc_force" ]; then 1926165565Syar return 1 1927165565Syar fi 1928165565Syar fi 1929165565Syar done 1930165565Syar 1931165565Syar for _f in $required_files; do 1932165565Syar if [ ! -r "${_f}" ]; then 1933165565Syar warn "${_f} is not readable." 1934165565Syar if [ -z "$rc_force" ]; then 1935165565Syar return 1 1936165565Syar fi 1937165565Syar fi 1938165565Syar done 1939165565Syar ;; 1940165565Syar esac 1941165565Syar 1942165565Syar return 0 1943165565Syar} 1944165565Syar 1945165565Syarcheck_required_after() 1946165565Syar{ 1947165565Syar local _f _args 1948165565Syar 1949165565Syar case "$1" in 1950165565Syar start) 1951165565Syar for _f in $required_modules; do 1952165565Syar case "${_f}" in 1953165565Syar *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; 1954165565Syar *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; 1955165565Syar *) _args="${_f}" ;; 1956165565Syar esac 1957165565Syar if ! load_kld ${_args}; then 1958165565Syar if [ -z "$rc_force" ]; then 1959165565Syar return 1 1960165565Syar fi 1961165565Syar fi 1962165565Syar done 1963165565Syar ;; 1964165565Syar esac 1965165565Syar 1966165565Syar return 0 1967165565Syar} 1968165565Syar 1969271545Shrs# check_jail mib 1970271545Shrs# Return true if security.jail.$mib exists and set to 1. 1971271545Shrs 1972271545Shrscheck_jail() 1973271545Shrs{ 1974271545Shrs local _mib _v 1975271545Shrs 1976271545Shrs _mib=$1 1977271545Shrs if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then 1978271545Shrs case $_v in 1979271545Shrs 1) return 0;; 1980271545Shrs esac 1981271545Shrs fi 1982271545Shrs return 1 1983271545Shrs} 1984271545Shrs 1985222996Shrs# check_kern_features mib 1986222996Shrs# Return existence of kern.features.* sysctl MIB as true or 1987222996Shrs# false. The result will be cached in $_rc_cache_kern_features_ 1988222996Shrs# namespace. "0" means the kern.features.X exists. 1989222996Shrs 1990222996Shrscheck_kern_features() 1991222996Shrs{ 1992222996Shrs local _v 1993222996Shrs 1994222996Shrs [ -n "$1" ] || return 1; 1995223292Sjilles eval _v=\$_rc_cache_kern_features_$1 1996222996Shrs [ -n "$_v" ] && return "$_v"; 1997222996Shrs 1998222996Shrs if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then 1999222996Shrs eval _rc_cache_kern_features_$1=0 2000222996Shrs return 0 2001222996Shrs else 2002222996Shrs eval _rc_cache_kern_features_$1=1 2003222996Shrs return 1 2004222996Shrs fi 2005222996Shrs} 2006222996Shrs 2007243184Shrs# check_namevarlist var 2008243184Shrs# Return "0" if ${name}_var is reserved in rc.subr. 2009243184Shrs 2010243184Shrs_rc_namevarlist="program chroot chdir flags fib nice user group groups" 2011243184Shrscheck_namevarlist() 2012243184Shrs{ 2013243184Shrs local _v 2014243184Shrs 2015243184Shrs for _v in $_rc_namevarlist; do 2016243184Shrs case $1 in 2017243184Shrs $_v) return 0 ;; 2018243184Shrs esac 2019243184Shrs done 2020243184Shrs 2021243184Shrs return 1 2022243184Shrs} 2023243184Shrs 2024197144Shrs# _echoonce var msg mode 2025197144Shrs# mode=0: Echo $msg if ${$var} is empty. 2026197144Shrs# After doing echo, a string is set to ${$var}. 2027197144Shrs# 2028197144Shrs# mode=1: Echo $msg if ${$var} is a string with non-zero length. 2029197144Shrs# 2030197144Shrs_echoonce() 2031197144Shrs{ 2032197144Shrs local _var _msg _mode 2033223227Sjilles eval _var=\$$1 2034197144Shrs _msg=$2 2035197144Shrs _mode=$3 2036197144Shrs 2037197144Shrs case $_mode in 2038197144Shrs 1) [ -n "$_var" ] && echo "$_msg" ;; 2039197144Shrs *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; 2040197144Shrs esac 2041197144Shrs} 2042197144Shrs 2043223298Sjillesfi # [ -z "${_rc_subr_loaded}" ] 2044223298Sjilles 2045157841Sflz_rc_subr_loaded=: 2046