rc.subr revision 157048
1126288Smtm# $NetBSD: rc.subr,v 1.60 2003/07/26 05:13:47 lukem Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 157048 2006-03-23 16:48:32Z flz $ 378344Sobrien# 498186Sgordon# Copyright (c) 1997-2002 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# 3. All advertising materials mentioning features or use of this software 1978344Sobrien# must display the following acknowledgement: 2078344Sobrien# This product includes software developed by the NetBSD 2178344Sobrien# Foundation, Inc. and its contributors. 2278344Sobrien# 4. Neither the name of The NetBSD Foundation nor the names of its 2378344Sobrien# contributors may be used to endorse or promote products derived 2478344Sobrien# from this software without specific prior written permission. 2578344Sobrien# 2678344Sobrien# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 2778344Sobrien# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 2878344Sobrien# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 2978344Sobrien# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 3078344Sobrien# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3178344Sobrien# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3278344Sobrien# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3378344Sobrien# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3478344Sobrien# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3578344Sobrien# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3678344Sobrien# POSSIBILITY OF SUCH DAMAGE. 3778344Sobrien# 3878344Sobrien# rc.subr 3978344Sobrien# functions used by various rc scripts 4078344Sobrien# 4178344Sobrien 4278344Sobrien# 4398186Sgordon# Operating System dependent/independent variables 4498186Sgordon# 4598186Sgordon 46131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then 47131550Scperciva 48131550Scperciva_rc_subr_loaded="YES" 49131550Scperciva 5098186SgordonSYSCTL="/sbin/sysctl" 5198186SgordonSYSCTL_N="${SYSCTL} -n" 5298186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype" 53103018SgordonOSTYPE=`${CMD_OSTYPE}` 54124832SmtmID="/usr/bin/id" 55124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 5698186Sgordon 57103018Sgordoncase ${OSTYPE} in 5898186SgordonFreeBSD) 5998186Sgordon SYSCTL_W="${SYSCTL}" 6098186Sgordon ;; 6198186SgordonNetBSD) 6298186Sgordon SYSCTL_W="${SYSCTL} -w" 6398186Sgordon ;; 6498186Sgordonesac 6598186Sgordon 6698186Sgordon# 6778344Sobrien# functions 6878344Sobrien# --------- 6978344Sobrien 7078344Sobrien# 7198186Sgordon# set_rcvar base_var 7298186Sgordon# Set the variable name enabling a specific service. 7398186Sgordon# FreeBSD uses ${service}_enable, while NetBSD uses 7498186Sgordon# just the name of the service. For example: 7598186Sgordon# FreeBSD: sendmail_enable="YES" 7698186Sgordon# NetBSD : sendmail="YES" 7798186Sgordon# $1 - if $name is not the base to work of off, specify 7898186Sgordon# a different one 7998186Sgordon# 8098186Sgordonset_rcvar() 8198186Sgordon{ 8298186Sgordon if [ -z "$1" ]; then 8398186Sgordon base_var=${name} 8498186Sgordon else 8598186Sgordon base_var="$1" 8698186Sgordon fi 8798186Sgordon 88103018Sgordon case ${OSTYPE} in 8998186Sgordon FreeBSD) 9098186Sgordon echo ${base_var}_enable 9198186Sgordon ;; 9298186Sgordon NetBSD) 9398186Sgordon echo ${base_var} 9498186Sgordon ;; 9598186Sgordon *) 9698186Sgordon echo 'XXX' 9798186Sgordon ;; 9898186Sgordon esac 9998186Sgordon} 10098186Sgordon 10198186Sgordon# 10298186Sgordon# force_depend script 10398186Sgordon# Force a service to start. Intended for use by services 10498186Sgordon# to resolve dependency issues. It is assumed the caller 10598186Sgordon# has check to make sure this call is necessary 10698186Sgordon# $1 - filename of script, in /etc/rc.d, to run 10798186Sgordon# 10898186Sgordonforce_depend() 10998186Sgordon{ 11098186Sgordon _depend="$1" 11198186Sgordon 11298186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 113146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 11498186Sgordon warn "Unable to force ${_depend}. It may already be running." 11598186Sgordon return 1 11698186Sgordon fi 11798186Sgordon return 0 11898186Sgordon} 11998186Sgordon 12098186Sgordon# 12178344Sobrien# checkyesno var 12278344Sobrien# Test $1 variable, and warn if not set to YES or NO. 12378344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 12478344Sobrien# 12578344Sobriencheckyesno() 12678344Sobrien{ 12778344Sobrien eval _value=\$${1} 12898186Sgordon debug "checkyesno: $1 is set to $_value." 12978344Sobrien case $_value in 13078344Sobrien 13178344Sobrien # "yes", "true", "on", or "1" 13278344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 13378344Sobrien return 0 13478344Sobrien ;; 13578344Sobrien 13678344Sobrien # "no", "false", "off", or "0" 13778344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 13878344Sobrien return 1 13978344Sobrien ;; 14078344Sobrien *) 141106643Sgordon warn "\$${1} is not set properly - see rc.conf(5)." 14278344Sobrien return 1 14378344Sobrien ;; 14478344Sobrien esac 14578344Sobrien} 14678344Sobrien 14798186Sgordon# reverse_list list 14898186Sgordon# print the list in reverse order 14978344Sobrien# 15098186Sgordonreverse_list() 15198186Sgordon{ 15298186Sgordon _revlist= 153126286Smtm for _revfile; do 15498186Sgordon _revlist="$_revfile $_revlist" 15598186Sgordon done 15698186Sgordon echo $_revlist 15798186Sgordon} 15898186Sgordon 15978344Sobrien# 16098186Sgordon# mount_critical_filesystems type 16198186Sgordon# Go through the list of critical filesystems as provided in 16298186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 16398186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 16498186Sgordon# 16578344Sobrienmount_critical_filesystems() 16678344Sobrien{ 16798186Sgordon eval _fslist=\$critical_filesystems_${1} 16878344Sobrien for _fs in $_fslist; do 16978344Sobrien mount | ( 170126285Smtm _ismounted=false 17178344Sobrien while read what _on on _type type; do 17278344Sobrien if [ $on = $_fs ]; then 173126285Smtm _ismounted=true 17478344Sobrien fi 17578344Sobrien done 176126285Smtm if $_ismounted; then 177126285Smtm : 178126285Smtm else 17978344Sobrien mount $_fs >/dev/null 2>&1 18078344Sobrien fi 18198186Sgordon ) 18278344Sobrien done 18378344Sobrien} 18478344Sobrien 18578344Sobrien# 18698186Sgordon# check_pidfile pidfile procname [interpreter] 18798186Sgordon# Parses the first line of pidfile for a PID, and ensures 18878344Sobrien# that the process is running and matches procname. 18998186Sgordon# Prints the matching PID upon success, nothing otherwise. 19098186Sgordon# interpreter is optional; see _find_processes() for details. 19178344Sobrien# 19278344Sobriencheck_pidfile() 19378344Sobrien{ 19478344Sobrien _pidfile=$1 19578344Sobrien _procname=$2 19698186Sgordon _interpreter=$3 19778344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 19898186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 19978344Sobrien fi 20078344Sobrien if [ ! -f $_pidfile ]; then 201131061Smtm debug "pid file ($_pidfile): not readable." 20278344Sobrien return 20378344Sobrien fi 20478344Sobrien read _pid _junk < $_pidfile 20578344Sobrien if [ -z "$_pid" ]; then 206139949Skeramida debug "pid file ($_pidfile): no pid in file." 20778344Sobrien return 20878344Sobrien fi 20998186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 21078344Sobrien} 21178344Sobrien 21278344Sobrien# 21398186Sgordon# check_process procname [interpreter] 21478344Sobrien# Ensures that a process (or processes) named procname is running. 21598186Sgordon# Prints a list of matching PIDs. 21698186Sgordon# interpreter is optional; see _find_processes() for details. 21778344Sobrien# 21878344Sobriencheck_process() 21978344Sobrien{ 22078344Sobrien _procname=$1 22198186Sgordon _interpreter=$2 22278344Sobrien if [ -z "$_procname" ]; then 22398186Sgordon err 3 'USAGE: check_process procname [interpreter]' 22478344Sobrien fi 22598186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 22698186Sgordon} 22798186Sgordon 22898186Sgordon# 22998186Sgordon# _find_processes procname interpreter psargs 23098186Sgordon# Search for procname in the output of ps generated by psargs. 23198186Sgordon# Prints the PIDs of any matching processes, space separated. 23298186Sgordon# 23398186Sgordon# If interpreter == ".", check the following variations of procname 23498186Sgordon# against the first word of each command: 23598186Sgordon# procname 23698186Sgordon# `basename procname` 23798186Sgordon# `basename procname` + ":" 23898186Sgordon# "(" + `basename procname` + ")" 239155719Sceri# "[" + `basename procname` + "]" 24098186Sgordon# 24198186Sgordon# If interpreter != ".", read the first line of procname, remove the 24298186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 24398186Sgordon# match that against each command, either as is, or with extra words 24498186Sgordon# at the end. 24598186Sgordon# 24698186Sgordon_find_processes() 24798186Sgordon{ 24898186Sgordon if [ $# -ne 3 ]; then 24998186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 25098186Sgordon fi 25198186Sgordon _procname=$1 25298186Sgordon _interpreter=$2 25398186Sgordon _psargs=$3 25498186Sgordon 25578344Sobrien _pref= 25698186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 25798186Sgordon read _interp < $_procname # read interpreter name 25898186Sgordon _interp=${_interp#\#!} # strip #! 25998186Sgordon set -- $_interp 26098186Sgordon if [ $_interpreter != $1 ]; then 26198186Sgordon warn "\$command_interpreter $_interpreter != $1" 26278344Sobrien fi 26398186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 26498186Sgordon _fp_args='_argv' 26598186Sgordon _fp_match='case "$_argv" in 26698186Sgordon ${_interp}|"${_interp} "*)' 26798186Sgordon else # a normal daemon 26898186Sgordon _procnamebn=${_procname##*/} 26998186Sgordon _fp_args='_arg0 _argv' 27098186Sgordon _fp_match='case "$_arg0" in 271151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 27298186Sgordon fi 27398186Sgordon 27498186Sgordon _proccheck=' 275126556Smtm ps 2>/dev/null -o "pid,command" '"$_psargs"' | 27698186Sgordon while read _npid '"$_fp_args"'; do 27798186Sgordon case "$_npid" in 278146490Sschweikh PID) 279146490Sschweikh continue;; 280146490Sschweikh esac; '"$_fp_match"' 281146490Sschweikh echo -n "$_pref$_npid"; 28298186Sgordon _pref=" " 28398186Sgordon ;; 28498186Sgordon esac 28598186Sgordon done' 28698186Sgordon 287114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 28898186Sgordon eval $_proccheck 28998186Sgordon} 29098186Sgordon 29198186Sgordon# 29298186Sgordon# wait_for_pids pid [pid ...] 29398186Sgordon# spins until none of the pids exist 29498186Sgordon# 29598186Sgordonwait_for_pids() 29698186Sgordon{ 297126286Smtm _list="$@" 29898186Sgordon if [ -z "$_list" ]; then 29998186Sgordon return 30098186Sgordon fi 30198186Sgordon _prefix= 30298186Sgordon while true; do 30398186Sgordon _nlist=""; 30498186Sgordon for _j in $_list; do 30598186Sgordon if kill -0 $_j 2>/dev/null; then 30698186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 30798186Sgordon fi 30898186Sgordon done 30998186Sgordon if [ -z "$_nlist" ]; then 31098186Sgordon break 31178344Sobrien fi 31298186Sgordon _list=$_nlist 31398186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 31498186Sgordon _prefix=", " 31598186Sgordon sleep 2 31678344Sobrien done 31798186Sgordon if [ -n "$_prefix" ]; then 31898186Sgordon echo "." 31998186Sgordon fi 32078344Sobrien} 32178344Sobrien 32278344Sobrien# 32398186Sgordon# run_rc_command argument 32498186Sgordon# Search for argument in the list of supported commands, which is: 32598186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 32698186Sgordon# If there's a match, run ${argument}_cmd or the default method 32798186Sgordon# (see below). 32878344Sobrien# 32998186Sgordon# If argument has a given prefix, then change the operation as follows: 33098186Sgordon# Prefix Operation 33178344Sobrien# ------ --------- 33298186Sgordon# fast Skip the pid check, and set rc_fast=yes 33398186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 334126303Smtm# one Set ${rcvar} to YES 33578344Sobrien# 33678344Sobrien# The following globals are used: 33778344Sobrien# 33898186Sgordon# Name Needed Purpose 33998186Sgordon# ---- ------ ------- 34078344Sobrien# name y Name of script. 34178344Sobrien# 34278344Sobrien# command n Full path to command. 34398186Sgordon# Not needed if ${rc_arg}_cmd is set for 34478344Sobrien# each keyword. 34578344Sobrien# 34678344Sobrien# command_args n Optional args/shell directives for command. 34778344Sobrien# 34898186Sgordon# command_interpreter n If not empty, command is interpreted, so 34998186Sgordon# call check_{pidfile,process}() appropriately. 35098186Sgordon# 35178344Sobrien# extra_commands n List of extra commands supported. 35278344Sobrien# 35398186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 35498186Sgordon# otherwise use check_process $command. 35598186Sgordon# In either case, only check if $command is set. 35678344Sobrien# 35798186Sgordon# procname n Process name to check for instead of $command. 35898186Sgordon# 35978344Sobrien# rcvar n This is checked with checkyesno to determine 36078344Sobrien# if the action should be run. 36178344Sobrien# 36278344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 36398186Sgordon# Requires /usr to be mounted. 36478344Sobrien# 36578344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 36678344Sobrien# (if not using ${name}_chroot). 36778344Sobrien# 36878344Sobrien# ${name}_flags n Arguments to call ${command} with. 36978344Sobrien# NOTE: $flags from the parent environment 37078344Sobrien# can be used to override this. 37178344Sobrien# 37278344Sobrien# ${name}_nice n Nice level to run ${command} at. 37378344Sobrien# 37478344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 37578344Sobrien# using ${name}_chroot. 37698186Sgordon# Requires /usr to be mounted. 37778344Sobrien# 37878344Sobrien# ${name}_group n Group to run chrooted ${command} as. 37998186Sgordon# Requires /usr to be mounted. 38078344Sobrien# 38198186Sgordon# ${name}_groups n Comma separated list of supplementary groups 38298186Sgordon# to run the chrooted ${command} with. 38398186Sgordon# Requires /usr to be mounted. 38478344Sobrien# 38598186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 38678344Sobrien# Otherwise, use default command (see below) 38778344Sobrien# 38898186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 38998186Sgordon# ${rc_arg}_cmd method in the default 39098186Sgordon# operation (i.e, after checking for required 39198186Sgordon# bits and process (non)existence). 39278344Sobrien# If this completes with a non-zero exit code, 39398186Sgordon# don't run ${rc_arg}_cmd. 39478344Sobrien# 39598186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 39698186Sgordon# ${rc_arg}_cmd method, if that method 39798186Sgordon# returned a zero exit code. 39898186Sgordon# 39978344Sobrien# required_dirs n If set, check for the existence of the given 40078344Sobrien# directories before running the default 40178344Sobrien# (re)start command. 40278344Sobrien# 40378344Sobrien# required_files n If set, check for the readability of the given 40478344Sobrien# files before running the default (re)start 40578344Sobrien# command. 40678344Sobrien# 40778344Sobrien# required_vars n If set, perform checkyesno on each of the 40878344Sobrien# listed variables before running the default 40978344Sobrien# (re)start command. 41078344Sobrien# 41198186Sgordon# Default behaviour for a given argument, if no override method is 41298186Sgordon# provided: 41378344Sobrien# 41498186Sgordon# Argument Default behaviour 41598186Sgordon# -------- ----------------- 41678344Sobrien# start if !running && checkyesno ${rcvar} 41778344Sobrien# ${command} 41878344Sobrien# 41978344Sobrien# stop if ${pidfile} 42098186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 42178344Sobrien# else 42298186Sgordon# rc_pid=$(check_process $command) 42398186Sgordon# kill $sig_stop $rc_pid 42498186Sgordon# wait_for_pids $rc_pid 42598186Sgordon# ($sig_stop defaults to TERM.) 42678344Sobrien# 42798186Sgordon# reload Similar to stop, except use $sig_reload instead, 42898186Sgordon# and doesn't wait_for_pids. 42978344Sobrien# $sig_reload defaults to HUP. 430151685Syar# Note that `reload' isn't provided by default, 431151685Syar# it should be enabled via $extra_commands. 43278344Sobrien# 43378344Sobrien# restart Run `stop' then `start'. 43478344Sobrien# 43598186Sgordon# status Show if ${command} is running, etc. 43678344Sobrien# 43798186Sgordon# poll Wait for ${command} to exit. 43898186Sgordon# 43998186Sgordon# rcvar Display what rc.conf variable is used (if any). 44098186Sgordon# 44198186Sgordon# Variables available to methods, and after run_rc_command() has 44298186Sgordon# completed: 44398186Sgordon# 44498186Sgordon# Variable Purpose 44598186Sgordon# -------- ------- 446126303Smtm# rc_arg Argument to command, after fast/force/one processing 44798186Sgordon# performed 44898186Sgordon# 44998186Sgordon# rc_flags Flags to start the default command with. 45098186Sgordon# Defaults to ${name}_flags, unless overridden 45198186Sgordon# by $flags from the environment. 45298186Sgordon# This variable may be changed by the precmd method. 45398186Sgordon# 45498186Sgordon# rc_pid PID of command (if appropriate) 45598186Sgordon# 45698186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 45798186Sgordon# 45898186Sgordon# rc_force Not empty if "force" was provided (q.v.) 45998186Sgordon# 46098186Sgordon# 46178344Sobrienrun_rc_command() 46278344Sobrien{ 463116097Smtm _return=0 46498186Sgordon rc_arg=$1 46578344Sobrien if [ -z "$name" ]; then 46698186Sgordon err 3 'run_rc_command: $name is not set.' 46778344Sobrien fi 46878344Sobrien 469132892Smtm # Don't repeat the first argument when passing additional command- 470132892Smtm # line arguments to the command subroutines. 471132892Smtm # 472132892Smtm shift 1 473132892Smtm rc_extra_args="$*" 474132892Smtm 475126303Smtm _rc_prefix= 47698186Sgordon case "$rc_arg" in 47778344Sobrien fast*) # "fast" prefix; don't check pid 47898186Sgordon rc_arg=${rc_arg#fast} 47998186Sgordon rc_fast=yes 48078344Sobrien ;; 481126303Smtm force*) # "force prefix; always run 48298186Sgordon rc_force=yes 483126303Smtm _rc_prefix=force 484126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 48578344Sobrien if [ -n "${rcvar}" ]; then 48678344Sobrien eval ${rcvar}=YES 48778344Sobrien fi 48878344Sobrien ;; 489126303Smtm one*) # "one" prefix; set ${rcvar}=yes 490126303Smtm _rc_prefix=one 491126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 492126303Smtm if [ -n "${rcvar}" ]; then 493126303Smtm eval ${rcvar}=YES 494126303Smtm fi 495126303Smtm ;; 49678344Sobrien esac 49778344Sobrien 498157048Sflz eval _override_command=\$${name}_program 499157048Sflz command=${command:+${_override_command:-$command}} 50098186Sgordon 50178344Sobrien _keywords="start stop restart rcvar $extra_commands" 50298186Sgordon rc_pid= 50378344Sobrien _pidcmd= 50498186Sgordon _procname=${procname:-${command}} 50598186Sgordon 506131135Smtm # setup pid check command 507131135Smtm if [ -n "$_procname" ]; then 50878344Sobrien if [ -n "$pidfile" ]; then 50998186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 51098186Sgordon else 51198186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 51278344Sobrien fi 51378344Sobrien if [ -n "$_pidcmd" ]; then 51498186Sgordon _keywords="${_keywords} status poll" 51578344Sobrien fi 51678344Sobrien fi 51778344Sobrien 51898186Sgordon if [ -z "$rc_arg" ]; then 519150796Syar rc_usage $_keywords 52078344Sobrien fi 52178344Sobrien 52278344Sobrien if [ -n "$flags" ]; then # allow override from environment 52398186Sgordon rc_flags=$flags 52478344Sobrien else 52598186Sgordon eval rc_flags=\$${name}_flags 52678344Sobrien fi 52798186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 52898186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 52998186Sgordon _group=\$${name}_group _groups=\$${name}_groups 53078344Sobrien 53198186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 532124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 53398186Sgordon unset _user 53498186Sgordon fi 53598186Sgordon fi 53698186Sgordon 53778344Sobrien # if ${rcvar} is set, and $1 is not 53898186Sgordon # "rcvar", then run 53978344Sobrien # checkyesno ${rcvar} 54078344Sobrien # and return if that failed 54178344Sobrien # 54298186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 54378344Sobrien if ! checkyesno ${rcvar}; then 54478344Sobrien return 0 54578344Sobrien fi 54678344Sobrien fi 54778344Sobrien 54878344Sobrien eval $_pidcmd # determine the pid if necessary 54978344Sobrien 55078344Sobrien for _elem in $_keywords; do 55198186Sgordon if [ "$_elem" != "$rc_arg" ]; then 55278344Sobrien continue 55378344Sobrien fi 55478344Sobrien 55578344Sobrien # if there's a custom ${XXX_cmd}, 55678344Sobrien # run that instead of the default 55778344Sobrien # 55898186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 55998186Sgordon _postcmd=\$${rc_arg}_postcmd 56078344Sobrien if [ -n "$_cmd" ]; then 56178344Sobrien # if the precmd failed and force 56278344Sobrien # isn't set, exit 56378344Sobrien # 564116097Smtm if [ -n "$_precmd" ]; then 565116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 566132892Smtm eval $_precmd $rc_extra_args 567116097Smtm _return=$? 568116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 569116097Smtm return 1 57078344Sobrien fi 57178344Sobrien 572116097Smtm if [ -n "$_cmd" ]; then 573116097Smtm debug "run_rc_command: evaluating ${_cmd}()." 574132892Smtm eval $_cmd $rc_extra_args 575116097Smtm _return=$? 576116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 577116097Smtm return 1 57898186Sgordon fi 579109582Smtm 580116097Smtm if [ -n "$_postcmd" ]; then 581116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 582132892Smtm eval $_postcmd $rc_extra_args 583116097Smtm _return=$? 584116097Smtm fi 585116097Smtm return $_return 58678344Sobrien fi 58778344Sobrien 58898186Sgordon case "$rc_arg" in # default operations... 58978344Sobrien 59078344Sobrien status) 59198186Sgordon if [ -n "$rc_pid" ]; then 59298186Sgordon echo "${name} is running as pid $rc_pid." 59378344Sobrien else 59478344Sobrien echo "${name} is not running." 59578344Sobrien return 1 59678344Sobrien fi 59778344Sobrien ;; 59878344Sobrien 59978344Sobrien start) 600131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 60198186Sgordon echo "${name} already running? (pid=$rc_pid)." 602153152Syar return 1 60378344Sobrien fi 60478344Sobrien 605126287Smtm if [ ! -x ${_chroot}${command} ]; then 60698186Sgordon info "run_rc_command: cannot run ($command)." 607153152Syar return 1 60878344Sobrien fi 60978344Sobrien 61078344Sobrien # check for required variables, 61178344Sobrien # directories, and files 61278344Sobrien # 61378344Sobrien for _f in $required_vars; do 61478344Sobrien if ! checkyesno $_f; then 61578344Sobrien warn "\$${_f} is not set." 61698186Sgordon if [ -z "$rc_force" ]; then 61778344Sobrien return 1 61878344Sobrien fi 61978344Sobrien fi 62078344Sobrien done 62178344Sobrien for _f in $required_dirs; do 62278344Sobrien if [ ! -d "${_f}/." ]; then 62378344Sobrien warn "${_f} is not a directory." 62498186Sgordon if [ -z "$rc_force" ]; then 62578344Sobrien return 1 62678344Sobrien fi 62778344Sobrien fi 62878344Sobrien done 62978344Sobrien for _f in $required_files; do 63078344Sobrien if [ ! -r "${_f}" ]; then 63178344Sobrien warn "${_f} is not readable." 63298186Sgordon if [ -z "$rc_force" ]; then 63378344Sobrien return 1 63478344Sobrien fi 63578344Sobrien fi 63678344Sobrien done 63778344Sobrien 63878344Sobrien # if the precmd failed and force 63978344Sobrien # isn't set, exit 64078344Sobrien # 641116097Smtm if [ -n "${_precmd}" ]; then 642116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 643116097Smtm eval $_precmd 644116097Smtm _return=$? 645116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 646116097Smtm return 1 64778344Sobrien fi 64878344Sobrien 64978344Sobrien # setup the command to run, and run it 65078344Sobrien # 65178344Sobrien echo "Starting ${name}." 65278344Sobrien if [ -n "$_chroot" ]; then 65378344Sobrien _doit="\ 65478344Sobrien${_nice:+nice -n $_nice }\ 65578344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 65698186Sgordon$_chroot $command $rc_flags $command_args" 65778344Sobrien else 65878344Sobrien _doit="\ 65978344Sobrien${_chdir:+cd $_chdir; }\ 66078344Sobrien${_nice:+nice -n $_nice }\ 66198186Sgordon$command $rc_flags $command_args" 66298186Sgordon if [ -n "$_user" ]; then 66398186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 66498186Sgordon fi 66578344Sobrien fi 66698186Sgordon 66798186Sgordon # if the cmd failed and force 66898186Sgordon # isn't set, exit 66998186Sgordon # 67098186Sgordon debug "run_rc_command: _doit: $_doit" 671116097Smtm eval $_doit 672116097Smtm _return=$? 673116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 67498186Sgordon 67598186Sgordon # finally, run postcmd 67698186Sgordon # 677116097Smtm if [ -n "${_postcmd}" ]; then 678116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 679116097Smtm eval $_postcmd 680116097Smtm fi 68178344Sobrien ;; 68278344Sobrien 68378344Sobrien stop) 68498186Sgordon if [ -z "$rc_pid" ]; then 685153152Syar [ -n "$rc_fast" ] && return 0 68678344Sobrien if [ -n "$pidfile" ]; then 68778344Sobrien echo \ 68878344Sobrien "${name} not running? (check $pidfile)." 68978344Sobrien else 69078344Sobrien echo "${name} not running?" 69178344Sobrien fi 692153152Syar return 1 69378344Sobrien fi 69478344Sobrien 69598186Sgordon # if the precmd failed and force 69698186Sgordon # isn't set, exit 69798186Sgordon # 698117977Smtm if [ -n "$_precmd" ]; then 699116097Smtm eval $_precmd 700116097Smtm _return=$? 701116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 702116097Smtm return 1 70378344Sobrien fi 70498186Sgordon 70598186Sgordon # send the signal to stop 70698186Sgordon # 70778344Sobrien echo "Stopping ${name}." 70898186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 70998186Sgordon if [ -n "$_user" ]; then 71098186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 71198186Sgordon fi 71298186Sgordon 71398186Sgordon # if the stop cmd failed and force 71498186Sgordon # isn't set, exit 71598186Sgordon # 716116097Smtm eval $_doit 717116097Smtm _return=$? 718116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 71998186Sgordon 72098186Sgordon # wait for the command to exit, 72198186Sgordon # and run postcmd. 72298186Sgordon wait_for_pids $rc_pid 723116097Smtm if [ -n "$_postcmd" ]; then 724116097Smtm eval $_postcmd 725116097Smtm _return=$? 726116097Smtm fi 72778344Sobrien ;; 72878344Sobrien 72978344Sobrien reload) 73098186Sgordon if [ -z "$rc_pid" ]; then 73178344Sobrien if [ -n "$pidfile" ]; then 73278344Sobrien echo \ 73378344Sobrien "${name} not running? (check $pidfile)." 73478344Sobrien else 73578344Sobrien echo "${name} not running?" 73678344Sobrien fi 737153152Syar return 1 73878344Sobrien fi 73978344Sobrien echo "Reloading ${name} config files." 740116097Smtm if [ -n "$_precmd" ]; then 741116097Smtm eval $_precmd 742116097Smtm _return=$? 743116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 744116097Smtm return 1 74578344Sobrien fi 74698186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 74798186Sgordon if [ -n "$_user" ]; then 74898186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 74998186Sgordon fi 750116097Smtm eval $_doit 751116097Smtm _return=$? 752116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 753116097Smtm if [ -n "$_postcmd" ]; then 754116097Smtm eval $_postcmd 755116097Smtm _return=$? 75698186Sgordon fi 75778344Sobrien ;; 75878344Sobrien 75978344Sobrien restart) 760116097Smtm if [ -n "$_precmd" ]; then 761132892Smtm eval $_precmd $rc_extra_args 762116097Smtm _return=$? 763116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 764116097Smtm return 1 76578344Sobrien fi 76678344Sobrien # prevent restart being called more 76778344Sobrien # than once by any given script 76878344Sobrien # 769126285Smtm if ${_rc_restart_done:-false}; then 77078344Sobrien return 0 77178344Sobrien fi 772126285Smtm _rc_restart_done=true 77378344Sobrien 774152519Syar # run stop in a subshell to keep variables for start 775152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 776152519Syar run_rc_command ${_rc_prefix}start $rc_extra_args 77798186Sgordon 778116097Smtm if [ -n "$_postcmd" ]; then 779132892Smtm eval $_postcmd $rc_extra_args 780116097Smtm _return=$? 781116097Smtm fi 78278344Sobrien ;; 78378344Sobrien 78498186Sgordon poll) 78598186Sgordon if [ -n "$rc_pid" ]; then 78698186Sgordon wait_for_pids $rc_pid 78798186Sgordon fi 78898186Sgordon ;; 78998186Sgordon 79078344Sobrien rcvar) 79178344Sobrien echo "# $name" 79278344Sobrien if [ -n "$rcvar" ]; then 79378344Sobrien if checkyesno ${rcvar}; then 79478344Sobrien echo "\$${rcvar}=YES" 79578344Sobrien else 79678344Sobrien echo "\$${rcvar}=NO" 79778344Sobrien fi 79878344Sobrien fi 79978344Sobrien ;; 80078344Sobrien 80178344Sobrien *) 802150796Syar rc_usage $_keywords 80378344Sobrien ;; 80478344Sobrien 80578344Sobrien esac 806116097Smtm return $_return 80778344Sobrien done 80878344Sobrien 80998186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 810150796Syar rc_usage $_keywords 811153152Syar # not reached 81278344Sobrien} 81378344Sobrien 81478344Sobrien# 81578344Sobrien# run_rc_script file arg 81678344Sobrien# Start the script `file' with `arg', and correctly handle the 81778344Sobrien# return value from the script. If `file' ends with `.sh', it's 81898186Sgordon# sourced into the current environment. If `file' appears to be 81998186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 82098186Sgordon# executable run as a child process. 82178344Sobrien# 82278344Sobrienrun_rc_script() 82378344Sobrien{ 82478344Sobrien _file=$1 82578344Sobrien _arg=$2 82678344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 82778344Sobrien err 3 'USAGE: run_rc_script file arg' 82878344Sobrien fi 82978344Sobrien 83098186Sgordon unset name command command_args command_interpreter \ 83198186Sgordon extra_commands pidfile procname \ 83298186Sgordon rcvar required_dirs required_files required_vars 83398186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 83498186Sgordon 83578344Sobrien case "$_file" in 836153105Sdougb /etc/rc.d/*.sh) # run in current shell 837146490Sschweikh set $_arg; . $_file 83878344Sobrien ;; 839153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 84098186Sgordon warn "Ignoring scratch file $_file" 84198186Sgordon ;; 84278344Sobrien *) # run in subshell 84398186Sgordon if [ -x $_file ]; then 84498186Sgordon if [ -n "$rc_fast_and_loose" ]; then 845146490Sschweikh set $_arg; . $_file 84698186Sgordon else 847130161Smtm ( trap "echo Script $_file interrupted; kill -QUIT $$" 3 848130161Smtm trap "echo Script $_file interrupted; exit 1" 2 849146490Sschweikh set $_arg; . $_file ) 85098186Sgordon fi 85198186Sgordon fi 85278344Sobrien ;; 85378344Sobrien esac 85478344Sobrien} 85578344Sobrien 85678344Sobrien# 85778344Sobrien# load_rc_config 85878344Sobrien# Source in the configuration file for a given command. 85978344Sobrien# 86078344Sobrienload_rc_config() 86178344Sobrien{ 86278344Sobrien _command=$1 86378344Sobrien if [ -z "$_command" ]; then 86478344Sobrien err 3 'USAGE: load_rc_config command' 86578344Sobrien fi 86678344Sobrien 867126285Smtm if ${_rc_conf_loaded:-false}; then 868126285Smtm : 869126285Smtm else 87098186Sgordon if [ -r /etc/defaults/rc.conf ]; then 87198186Sgordon debug "Sourcing /etc/defaults/rc.conf" 87298186Sgordon . /etc/defaults/rc.conf 87398186Sgordon source_rc_confs 87498186Sgordon elif [ -r /etc/rc.conf ]; then 87598186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 87698186Sgordon . /etc/rc.conf 87798186Sgordon fi 878126285Smtm _rc_conf_loaded=true 87998186Sgordon fi 88078344Sobrien if [ -f /etc/rc.conf.d/"$_command" ]; then 88198186Sgordon debug "Sourcing /etc/rc.conf.d/${_command}" 88278344Sobrien . /etc/rc.conf.d/"$_command" 88378344Sobrien fi 884101850Sgordon 885101850Sgordon # XXX - Deprecated variable name support 886101850Sgordon # 887103018Sgordon case ${OSTYPE} in 888101850Sgordon FreeBSD) 889146490Sschweikh [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 890146490Sschweikh [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 891146490Sschweikh [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 892146490Sschweikh [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 893146490Sschweikh [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 894146490Sschweikh [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 895146490Sschweikh [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 896115950Smtm [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program" 897115950Smtm [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags" 898146490Sschweikh ;; 899101850Sgordon esac 90078344Sobrien} 90178344Sobrien 90278344Sobrien# 90378344Sobrien# rc_usage commands 90478344Sobrien# Print a usage string for $0, with `commands' being a list of 90578344Sobrien# valid commands. 90678344Sobrien# 90778344Sobrienrc_usage() 90878344Sobrien{ 909126303Smtm echo -n 1>&2 "Usage: $0 [fast|force|one](" 91078344Sobrien 91178344Sobrien _sep= 912126286Smtm for _elem; do 91378344Sobrien echo -n 1>&2 "$_sep$_elem" 91478344Sobrien _sep="|" 91578344Sobrien done 91678344Sobrien echo 1>&2 ")" 91778344Sobrien exit 1 91878344Sobrien} 91978344Sobrien 92078344Sobrien# 92178344Sobrien# err exitval message 92278344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 92378344Sobrien# 92478344Sobrienerr() 92578344Sobrien{ 92678344Sobrien exitval=$1 92778344Sobrien shift 92878344Sobrien 929106643Sgordon if [ -x /usr/bin/logger ]; then 930106643Sgordon logger "$0: ERROR: $*" 931106643Sgordon fi 932106643Sgordon echo 1>&2 "$0: ERROR: $*" 93378344Sobrien exit $exitval 93478344Sobrien} 93578344Sobrien 93678344Sobrien# 93778344Sobrien# warn message 93878344Sobrien# Display message to stderr and log to the syslog. 93978344Sobrien# 94078344Sobrienwarn() 94178344Sobrien{ 942106643Sgordon if [ -x /usr/bin/logger ]; then 943106643Sgordon logger "$0: WARNING: $*" 944106643Sgordon fi 945106643Sgordon echo 1>&2 "$0: WARNING: $*" 94678344Sobrien} 94798186Sgordon 94898186Sgordon# 94998186Sgordon# info message 95098186Sgordon# Display informational message to stdout and log to syslog. 95198186Sgordon# 95298186Sgordoninfo() 95398186Sgordon{ 954119170Smtm case ${rc_info} in 955119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 956119170Smtm if [ -x /usr/bin/logger ]; then 957119170Smtm logger "$0: INFO: $*" 958119170Smtm fi 959119170Smtm echo "$0: INFO: $*" 960119170Smtm ;; 961119170Smtm esac 96298186Sgordon} 96398186Sgordon 96498186Sgordon# 96598186Sgordon# debug message 966106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 96798186Sgordon# BEWARE that you don't call any subroutine that itself calls this 96898186Sgordon# function. 96998186Sgordon# 97098186Sgordondebug() 97198186Sgordon{ 97298186Sgordon case ${rc_debug} in 97398186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 974106700Sgordon if [ -x /usr/bin/logger ]; then 975106700Sgordon logger "$0: INFO: $*" 976106700Sgordon fi 977146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 97898186Sgordon ;; 97998186Sgordon esac 98098186Sgordon} 98198186Sgordon 98298186Sgordon# 98398186Sgordon# backup_file action file cur backup 98498186Sgordon# Make a backup copy of `file' into `cur', and save the previous 98598186Sgordon# version of `cur' as `backup' or use rcs for archiving. 98698186Sgordon# 98798186Sgordon# This routine checks the value of the backup_uses_rcs variable, 98898186Sgordon# which can be either YES or NO. 98998186Sgordon# 99098186Sgordon# The `action' keyword can be one of the following: 99198186Sgordon# 99298186Sgordon# add `file' is now being backed up (and is possibly 99398186Sgordon# being reentered into the backups system). `cur' 99498186Sgordon# is created and RCS files, if necessary, are 99598186Sgordon# created as well. 99698186Sgordon# 99798186Sgordon# update `file' has changed and needs to be backed up. 99898186Sgordon# If `cur' exists, it is copied to to `back' or 99998186Sgordon# checked into RCS (if the repository file is old), 100098186Sgordon# and then `file' is copied to `cur'. Another RCS 100198186Sgordon# check in done here if RCS is being used. 100298186Sgordon# 100398186Sgordon# remove `file' is no longer being tracked by the backups 100498186Sgordon# system. If RCS is not being used, `cur' is moved 100598186Sgordon# to `back', otherwise an empty file is checked in, 100698186Sgordon# and then `cur' is removed. 100798186Sgordon# 100898186Sgordon# 100998186Sgordonbackup_file() 101098186Sgordon{ 101198186Sgordon _action=$1 101298186Sgordon _file=$2 101398186Sgordon _cur=$3 101498186Sgordon _back=$4 101598186Sgordon 101698186Sgordon if checkyesno backup_uses_rcs; then 101798186Sgordon _msg0="backup archive" 101898186Sgordon _msg1="update" 101998186Sgordon 102098186Sgordon # ensure that history file is not locked 102198186Sgordon if [ -f $_cur,v ]; then 102298186Sgordon rcs -q -u -U -M $_cur 102398186Sgordon fi 102498186Sgordon 102598186Sgordon # ensure after switching to rcs that the 102698186Sgordon # current backup is not lost 102798186Sgordon if [ -f $_cur ]; then 102898186Sgordon # no archive, or current newer than archive 102998186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 103098186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 103198186Sgordon rcs -q -kb -U $_cur 103298186Sgordon co -q -f -u $_cur 103398186Sgordon fi 103498186Sgordon fi 103598186Sgordon 103698186Sgordon case $_action in 103798186Sgordon add|update) 103898186Sgordon cp -p $_file $_cur 103998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 104098186Sgordon rcs -q -kb -U $_cur 104198186Sgordon co -q -f -u $_cur 104298186Sgordon chown root:wheel $_cur $_cur,v 104398186Sgordon ;; 104498186Sgordon remove) 104598186Sgordon cp /dev/null $_cur 104698186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 104798186Sgordon rcs -q -kb -U $_cur 104898186Sgordon chown root:wheel $_cur $_cur,v 104998186Sgordon rm $_cur 105098186Sgordon ;; 105198186Sgordon esac 105298186Sgordon else 105398186Sgordon case $_action in 105498186Sgordon add|update) 105598186Sgordon if [ -f $_cur ]; then 105698186Sgordon cp -p $_cur $_back 105798186Sgordon fi 105898186Sgordon cp -p $_file $_cur 105998186Sgordon chown root:wheel $_cur 106098186Sgordon ;; 106198186Sgordon remove) 106298186Sgordon mv -f $_cur $_back 106398186Sgordon ;; 106498186Sgordon esac 106598186Sgordon fi 106698186Sgordon} 1067119166Smtm 1068123344Smtm# make_symlink src link 1069123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1070123344Smtm# directory in which link is to be created does not exist 1071123344Smtm# a warning will be displayed and an error will be returned. 1072123344Smtm# Returns 0 on sucess, 1 otherwise. 1073119166Smtm# 1074123344Smtmmake_symlink() 1075119166Smtm{ 1076123344Smtm local src link linkdir _me 1077123344Smtm src="$1" 1078123344Smtm link="$2" 1079123344Smtm linkdir="`dirname $link`" 1080123344Smtm _me="make_symlink()" 1081119166Smtm 1082123344Smtm if [ -z "$src" -o -z "$link" ]; then 1083123344Smtm warn "$_me: requires two arguments." 1084119166Smtm return 1 1085119166Smtm fi 1086123344Smtm if [ ! -d "$linkdir" ]; then 1087123344Smtm warn "$_me: the directory $linkdir does not exist" 1088119166Smtm return 1 1089119166Smtm fi 1090146490Sschweikh if ! ln -sf $src $link; then 1091123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1092119166Smtm return 1 1093119166Smtm fi 1094119166Smtm return 0 1095119166Smtm} 1096119166Smtm 1097119166Smtm# devfs_rulesets_from_file file 1098119166Smtm# Reads a set of devfs commands from file, and creates 1099119166Smtm# the specified rulesets with their rules. Returns non-zero 1100119166Smtm# if there was an error. 1101119166Smtm# 1102119166Smtmdevfs_rulesets_from_file() 1103119166Smtm{ 1104119166Smtm local file _err _me 1105119166Smtm file="$1" 1106119166Smtm _me="devfs_rulesets_from_file" 1107119166Smtm _err=0 1108119166Smtm 1109119166Smtm if [ -z "$file" ]; then 1110119166Smtm warn "$_me: you must specify a file" 1111119166Smtm return 1 1112119166Smtm fi 1113119166Smtm if [ ! -e "$file" ]; then 1114119166Smtm debug "$_me: no such file ($file)" 1115119166Smtm return 0 1116119166Smtm fi 1117119166Smtm debug "reading rulesets from file ($file)" 1118119166Smtm { while read line 1119119166Smtm do 1120119166Smtm case $line in 1121119166Smtm \#*) 1122119166Smtm continue 1123119166Smtm ;; 1124119166Smtm \[*\]*) 1125119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1126119166Smtm if [ -z "$rulenum" ]; then 1127119166Smtm warn "$_me: cannot extract rule number ($line)" 1128119166Smtm _err=1 1129119166Smtm break 1130119166Smtm fi 1131119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1132119166Smtm if [ -z "$rulename" ]; then 1133119166Smtm warn "$_me: cannot extract rule name ($line)" 1134119166Smtm _err=1 1135119166Smtm break; 1136119166Smtm fi 1137119166Smtm eval $rulename=\$rulenum 1138119166Smtm debug "found ruleset: $rulename=$rulenum" 1139146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1140119166Smtm _err=1 1141119166Smtm break 1142119166Smtm fi 1143119166Smtm ;; 1144119166Smtm *) 1145119166Smtm rulecmd="${line%%"\#*"}" 1146119166Smtm # evaluate the command incase it includes 1147119166Smtm # other rules 1148119166Smtm if [ -n "$rulecmd" ]; then 1149119166Smtm debug "adding rule ($rulecmd)" 1150119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1151119166Smtm then 1152119166Smtm _err=1 1153119166Smtm break 1154119166Smtm fi 1155119166Smtm fi 1156119166Smtm ;; 1157119166Smtm esac 1158119166Smtm if [ $_err -ne 0 ]; then 1159119166Smtm debug "error in $_me" 1160119166Smtm break 1161119166Smtm fi 1162119166Smtm done } < $file 1163119166Smtm return $_err 1164119166Smtm} 1165119166Smtm 1166119166Smtm# devfs_init_rulesets 1167119166Smtm# Initializes rulesets from configuration files. Returns 1168119166Smtm# non-zero if there was an error. 1169119166Smtm# 1170119166Smtmdevfs_init_rulesets() 1171119166Smtm{ 1172119166Smtm local file _me 1173119166Smtm _me="devfs_init_rulesets" 1174119166Smtm 1175119166Smtm # Go through this only once 1176119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1177119166Smtm debug "$_me: devfs rulesets already initialized" 1178119166Smtm return 1179119166Smtm fi 1180146490Sschweikh for file in $devfs_rulesets; do 1181119166Smtm devfs_rulesets_from_file $file || return 1 1182119166Smtm done 1183119166Smtm devfs_rulesets_init=1 1184119166Smtm debug "$_me: devfs rulesets initialized" 1185119166Smtm return 0 1186119166Smtm} 1187119166Smtm 1188119166Smtm# devfs_set_ruleset ruleset [dir] 1189151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1190119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1191119166Smtm# Returns non-zero if it could not set it successfully. 1192119166Smtm# 1193119166Smtmdevfs_set_ruleset() 1194119166Smtm{ 1195119166Smtm local devdir rs _me 1196119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1197119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1198119166Smtm _me="devfs_set_ruleset" 1199119166Smtm 1200119166Smtm if [ -z "$rs" ]; then 1201119166Smtm warn "$_me: you must specify a ruleset number" 1202119166Smtm return 1 1203119166Smtm fi 1204119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1205146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1206119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1207119166Smtm return 1 1208119166Smtm fi 1209119166Smtm return 0 1210119166Smtm} 1211119166Smtm 1212119166Smtm# devfs_apply_ruleset ruleset [dir] 1213119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1214119166Smtm# The ruleset argument must be a ruleset name as specified 1215119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1216119166Smtm# if it could not apply the ruleset. 1217119166Smtm# 1218119166Smtmdevfs_apply_ruleset() 1219119166Smtm{ 1220119166Smtm local devdir rs _me 1221119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1222119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1223119166Smtm _me="devfs_apply_ruleset" 1224119166Smtm 1225119166Smtm if [ -z "$rs" ]; then 1226119166Smtm warn "$_me: you must specify a ruleset" 1227119166Smtm return 1 1228119166Smtm fi 1229119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1230146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1231119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1232119166Smtm return 1 1233119166Smtm fi 1234119166Smtm return 0 1235119166Smtm} 1236119166Smtm 1237119166Smtm# devfs_domount dir [ruleset] 1238119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1239119166Smtm# on the mount-point. It must also be a ruleset name as specified 1240119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1241119166Smtm# 1242119166Smtmdevfs_domount() 1243119166Smtm{ 1244119166Smtm local devdir rs _me 1245119166Smtm devdir="$1" 1246119166Smtm [ -n "$2" ] && rs=$2 || rs= 1247119166Smtm _me="devfs_domount()" 1248119166Smtm 1249119166Smtm if [ -z "$devdir" ]; then 1250119166Smtm warn "$_me: you must specify a mount-point" 1251119166Smtm return 1 1252119166Smtm fi 1253119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1254146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1255119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1256119166Smtm return 1 1257119166Smtm fi 1258119166Smtm if [ -n "$rs" ]; then 1259119166Smtm devfs_init_rulesets 1260119166Smtm devfs_set_ruleset $rs $devdir 1261124797Scperciva devfs -m $devdir rule applyset 1262119166Smtm fi 1263119166Smtm return 0 1264119166Smtm} 1265119166Smtm 1266119166Smtm# devfs_mount_jail dir [ruleset] 1267119166Smtm# Mounts a devfs file system appropriate for jails 1268119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1269119166Smtm# it names will be used instead. If present, ruleset must 1270119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1271119166Smtm# This function returns non-zero if an error occurs. 1272119166Smtm# 1273119166Smtmdevfs_mount_jail() 1274119166Smtm{ 1275119166Smtm local jdev rs _me 1276119166Smtm jdev="$1" 1277119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1278119166Smtm _me="devfs_mount_jail" 1279119166Smtm 1280119166Smtm devfs_init_rulesets 1281146490Sschweikh if ! devfs_domount "$jdev" $rs; then 1282119166Smtm warn "$_me: devfs was not mounted on $jdev" 1283119166Smtm return 1 1284119166Smtm fi 1285119166Smtm return 0 1286119166Smtm} 1287127345Sbrooks 1288127345Sbrooks# Provide a function for normalizing the mounting of memory 1289127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1290127345Sbrooks# as close as possible between 5-current and 4-stable. 1291127345Sbrooks# $1 = size 1292127345Sbrooks# $2 = mount point 1293137451Skeramida# $3 = (optional) extra mdmfs flags 1294146490Sschweikhmount_md() 1295146490Sschweikh{ 1296127345Sbrooks if [ -n "$3" ]; then 1297137451Skeramida flags="$3" 1298127345Sbrooks fi 1299149421Syar /sbin/mdmfs $flags -s $1 md $2 1300127345Sbrooks} 1301131550Scperciva 1302149049Spjd# ltr str src dst 1303149049Spjd# Change every $src in $str to $dst. 1304149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1305149049Spjd# awk(1). 1306149049Spjdltr() 1307149049Spjd{ 1308149049Spjd local _str _src _dst _out _com 1309149049Spjd _str=$1 1310149049Spjd _src=$2 1311149049Spjd _dst=$3 1312149049Spjd _out="" 1313149049Spjd 1314149049Spjd IFS=${_src} 1315149049Spjd for _com in ${_str}; do 1316149049Spjd if [ -z "${_out}" ]; then 1317149049Spjd _out="${_com}" 1318149049Spjd else 1319149049Spjd _out="${_out}${_dst}${_com}" 1320149049Spjd fi 1321149049Spjd done 1322149049Spjd echo "${_out}" 1323149049Spjd} 1324149049Spjd 1325149050Spjd# Creates a list of providers for GELI encryption. 1326149050Spjdgeli_make_list() 1327149050Spjd{ 1328149050Spjd local devices devices2 1329149050Spjd local provider mountpoint type options rest 1330149050Spjd 1331149050Spjd # Create list of GELI providers from fstab. 1332149050Spjd while read provider mountpoint type options rest ; do 1333155570Sflz case ":${options}" in 1334155570Sflz :*noauto*) 1335155570Sflz noauto=yes 1336155570Sflz ;; 1337155570Sflz *) 1338155570Sflz noauto=no 1339155570Sflz ;; 1340155570Sflz esac 1341155570Sflz 1342149050Spjd case ":${provider}" in 1343149050Spjd :#*) 1344149050Spjd continue 1345149050Spjd ;; 1346149050Spjd *.eli) 1347149050Spjd # Skip swap devices. 1348155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1349149050Spjd continue 1350149050Spjd fi 1351149050Spjd devices="${devices} ${provider}" 1352149050Spjd ;; 1353149050Spjd esac 1354149050Spjd done < /etc/fstab 1355149050Spjd 1356149050Spjd # Append providers from geli_devices. 1357149050Spjd devices="${devices} ${geli_devices}" 1358149050Spjd 1359149050Spjd for provider in ${devices}; do 1360149050Spjd provider=${provider%.eli} 1361149050Spjd provider=${provider#/dev/} 1362149050Spjd devices2="${devices2} ${provider}" 1363149050Spjd done 1364149050Spjd 1365149050Spjd echo ${devices2} 1366149050Spjd} 1367149050Spjd 1368153027Sdougb# Find scripts in local_startup directories that use the old syntax 1369153027Sdougb# 1370153027Sdougbfind_local_scripts_old () { 1371153027Sdougb zlist='' 1372153027Sdougb slist='' 1373153027Sdougb for dir in ${local_startup}; do 1374153027Sdougb if [ -d "${dir}" ]; then 1375153027Sdougb for file in ${dir}/[0-9]*.sh; do 1376153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1377153027Sdougb continue 1378153027Sdougb zlist="$zlist $file" 1379153027Sdougb done 1380153027Sdougb for file in ${dir}/[^0-9]*.sh; do 1381153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1382153027Sdougb continue 1383153027Sdougb slist="$slist $file" 1384153027Sdougb done 1385153027Sdougb fi 1386153027Sdougb done 1387153027Sdougb} 1388153027Sdougb 1389153027Sdougbfind_local_scripts_new () { 1390153027Sdougb local_rc='' 1391153027Sdougb for dir in ${local_startup}; do 1392153027Sdougb if [ -d "${dir}" ]; then 1393153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1394153027Sdougb case "$file" in 1395153027Sdougb *.sample) ;; 1396153027Sdougb *) if [ -x "$file" ]; then 1397153027Sdougb local_rc="${local_rc} ${file}" 1398153027Sdougb fi 1399153027Sdougb ;; 1400153027Sdougb esac 1401153027Sdougb done 1402153027Sdougb fi 1403153027Sdougb done 1404153027Sdougb} 1405153027Sdougb 1406131550Scpercivafi 1407