rc.subr revision 151685
1126288Smtm# $NetBSD: rc.subr,v 1.60 2003/07/26 05:13:47 lukem Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 151685 2005-10-26 04:12:34Z yar $ 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` + ")" 23998186Sgordon# 24098186Sgordon# If interpreter != ".", read the first line of procname, remove the 24198186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 24298186Sgordon# match that against each command, either as is, or with extra words 24398186Sgordon# at the end. 24498186Sgordon# 24598186Sgordon_find_processes() 24698186Sgordon{ 24798186Sgordon if [ $# -ne 3 ]; then 24898186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 24998186Sgordon fi 25098186Sgordon _procname=$1 25198186Sgordon _interpreter=$2 25298186Sgordon _psargs=$3 25398186Sgordon 25478344Sobrien _pref= 25598186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 25698186Sgordon read _interp < $_procname # read interpreter name 25798186Sgordon _interp=${_interp#\#!} # strip #! 25898186Sgordon set -- $_interp 25998186Sgordon if [ $_interpreter != $1 ]; then 26098186Sgordon warn "\$command_interpreter $_interpreter != $1" 26178344Sobrien fi 26298186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 26398186Sgordon _fp_args='_argv' 26498186Sgordon _fp_match='case "$_argv" in 26598186Sgordon ${_interp}|"${_interp} "*)' 26698186Sgordon else # a normal daemon 26798186Sgordon _procnamebn=${_procname##*/} 26898186Sgordon _fp_args='_arg0 _argv' 26998186Sgordon _fp_match='case "$_arg0" in 270151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 27198186Sgordon fi 27298186Sgordon 27398186Sgordon _proccheck=' 274126556Smtm ps 2>/dev/null -o "pid,command" '"$_psargs"' | 27598186Sgordon while read _npid '"$_fp_args"'; do 27698186Sgordon case "$_npid" in 277146490Sschweikh PID) 278146490Sschweikh continue;; 279146490Sschweikh esac; '"$_fp_match"' 280146490Sschweikh echo -n "$_pref$_npid"; 28198186Sgordon _pref=" " 28298186Sgordon ;; 28398186Sgordon esac 28498186Sgordon done' 28598186Sgordon 286114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 28798186Sgordon eval $_proccheck 28898186Sgordon} 28998186Sgordon 29098186Sgordon# 29198186Sgordon# wait_for_pids pid [pid ...] 29298186Sgordon# spins until none of the pids exist 29398186Sgordon# 29498186Sgordonwait_for_pids() 29598186Sgordon{ 296126286Smtm _list="$@" 29798186Sgordon if [ -z "$_list" ]; then 29898186Sgordon return 29998186Sgordon fi 30098186Sgordon _prefix= 30198186Sgordon while true; do 30298186Sgordon _nlist=""; 30398186Sgordon for _j in $_list; do 30498186Sgordon if kill -0 $_j 2>/dev/null; then 30598186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 30698186Sgordon fi 30798186Sgordon done 30898186Sgordon if [ -z "$_nlist" ]; then 30998186Sgordon break 31078344Sobrien fi 31198186Sgordon _list=$_nlist 31298186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 31398186Sgordon _prefix=", " 31498186Sgordon sleep 2 31578344Sobrien done 31698186Sgordon if [ -n "$_prefix" ]; then 31798186Sgordon echo "." 31898186Sgordon fi 31978344Sobrien} 32078344Sobrien 32178344Sobrien# 32298186Sgordon# run_rc_command argument 32398186Sgordon# Search for argument in the list of supported commands, which is: 32498186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 32598186Sgordon# If there's a match, run ${argument}_cmd or the default method 32698186Sgordon# (see below). 32778344Sobrien# 32898186Sgordon# If argument has a given prefix, then change the operation as follows: 32998186Sgordon# Prefix Operation 33078344Sobrien# ------ --------- 33198186Sgordon# fast Skip the pid check, and set rc_fast=yes 33298186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 333126303Smtm# one Set ${rcvar} to YES 33478344Sobrien# 33578344Sobrien# The following globals are used: 33678344Sobrien# 33798186Sgordon# Name Needed Purpose 33898186Sgordon# ---- ------ ------- 33978344Sobrien# name y Name of script. 34078344Sobrien# 34178344Sobrien# command n Full path to command. 34298186Sgordon# Not needed if ${rc_arg}_cmd is set for 34378344Sobrien# each keyword. 34478344Sobrien# 34578344Sobrien# command_args n Optional args/shell directives for command. 34678344Sobrien# 34798186Sgordon# command_interpreter n If not empty, command is interpreted, so 34898186Sgordon# call check_{pidfile,process}() appropriately. 34998186Sgordon# 35078344Sobrien# extra_commands n List of extra commands supported. 35178344Sobrien# 35298186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 35398186Sgordon# otherwise use check_process $command. 35498186Sgordon# In either case, only check if $command is set. 35578344Sobrien# 35698186Sgordon# procname n Process name to check for instead of $command. 35798186Sgordon# 35878344Sobrien# rcvar n This is checked with checkyesno to determine 35978344Sobrien# if the action should be run. 36078344Sobrien# 36178344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 36298186Sgordon# Requires /usr to be mounted. 36378344Sobrien# 36478344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 36578344Sobrien# (if not using ${name}_chroot). 36678344Sobrien# 36778344Sobrien# ${name}_flags n Arguments to call ${command} with. 36878344Sobrien# NOTE: $flags from the parent environment 36978344Sobrien# can be used to override this. 37078344Sobrien# 37178344Sobrien# ${name}_nice n Nice level to run ${command} at. 37278344Sobrien# 37378344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 37478344Sobrien# using ${name}_chroot. 37598186Sgordon# Requires /usr to be mounted. 37678344Sobrien# 37778344Sobrien# ${name}_group n Group to run chrooted ${command} as. 37898186Sgordon# Requires /usr to be mounted. 37978344Sobrien# 38098186Sgordon# ${name}_groups n Comma separated list of supplementary groups 38198186Sgordon# to run the chrooted ${command} with. 38298186Sgordon# Requires /usr to be mounted. 38378344Sobrien# 38498186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 38578344Sobrien# Otherwise, use default command (see below) 38678344Sobrien# 38798186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 38898186Sgordon# ${rc_arg}_cmd method in the default 38998186Sgordon# operation (i.e, after checking for required 39098186Sgordon# bits and process (non)existence). 39178344Sobrien# If this completes with a non-zero exit code, 39298186Sgordon# don't run ${rc_arg}_cmd. 39378344Sobrien# 39498186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 39598186Sgordon# ${rc_arg}_cmd method, if that method 39698186Sgordon# returned a zero exit code. 39798186Sgordon# 39878344Sobrien# required_dirs n If set, check for the existence of the given 39978344Sobrien# directories before running the default 40078344Sobrien# (re)start command. 40178344Sobrien# 40278344Sobrien# required_files n If set, check for the readability of the given 40378344Sobrien# files before running the default (re)start 40478344Sobrien# command. 40578344Sobrien# 40678344Sobrien# required_vars n If set, perform checkyesno on each of the 40778344Sobrien# listed variables before running the default 40878344Sobrien# (re)start command. 40978344Sobrien# 41098186Sgordon# Default behaviour for a given argument, if no override method is 41198186Sgordon# provided: 41278344Sobrien# 41398186Sgordon# Argument Default behaviour 41498186Sgordon# -------- ----------------- 41578344Sobrien# start if !running && checkyesno ${rcvar} 41678344Sobrien# ${command} 41778344Sobrien# 41878344Sobrien# stop if ${pidfile} 41998186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 42078344Sobrien# else 42198186Sgordon# rc_pid=$(check_process $command) 42298186Sgordon# kill $sig_stop $rc_pid 42398186Sgordon# wait_for_pids $rc_pid 42498186Sgordon# ($sig_stop defaults to TERM.) 42578344Sobrien# 42698186Sgordon# reload Similar to stop, except use $sig_reload instead, 42798186Sgordon# and doesn't wait_for_pids. 42878344Sobrien# $sig_reload defaults to HUP. 429151685Syar# Note that `reload' isn't provided by default, 430151685Syar# it should be enabled via $extra_commands. 43178344Sobrien# 43278344Sobrien# restart Run `stop' then `start'. 43378344Sobrien# 43498186Sgordon# status Show if ${command} is running, etc. 43578344Sobrien# 43698186Sgordon# poll Wait for ${command} to exit. 43798186Sgordon# 43898186Sgordon# rcvar Display what rc.conf variable is used (if any). 43998186Sgordon# 44098186Sgordon# Variables available to methods, and after run_rc_command() has 44198186Sgordon# completed: 44298186Sgordon# 44398186Sgordon# Variable Purpose 44498186Sgordon# -------- ------- 445126303Smtm# rc_arg Argument to command, after fast/force/one processing 44698186Sgordon# performed 44798186Sgordon# 44898186Sgordon# rc_flags Flags to start the default command with. 44998186Sgordon# Defaults to ${name}_flags, unless overridden 45098186Sgordon# by $flags from the environment. 45198186Sgordon# This variable may be changed by the precmd method. 45298186Sgordon# 45398186Sgordon# rc_pid PID of command (if appropriate) 45498186Sgordon# 45598186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 45698186Sgordon# 45798186Sgordon# rc_force Not empty if "force" was provided (q.v.) 45898186Sgordon# 45998186Sgordon# 46078344Sobrienrun_rc_command() 46178344Sobrien{ 462116097Smtm _return=0 46398186Sgordon rc_arg=$1 46478344Sobrien if [ -z "$name" ]; then 46598186Sgordon err 3 'run_rc_command: $name is not set.' 46678344Sobrien fi 46778344Sobrien 468132892Smtm # Don't repeat the first argument when passing additional command- 469132892Smtm # line arguments to the command subroutines. 470132892Smtm # 471132892Smtm shift 1 472132892Smtm rc_extra_args="$*" 473132892Smtm 474126303Smtm _rc_prefix= 47598186Sgordon case "$rc_arg" in 47678344Sobrien fast*) # "fast" prefix; don't check pid 47798186Sgordon rc_arg=${rc_arg#fast} 47898186Sgordon rc_fast=yes 47978344Sobrien ;; 480126303Smtm force*) # "force prefix; always run 48198186Sgordon rc_force=yes 482126303Smtm _rc_prefix=force 483126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 48478344Sobrien if [ -n "${rcvar}" ]; then 48578344Sobrien eval ${rcvar}=YES 48678344Sobrien fi 48778344Sobrien ;; 488126303Smtm one*) # "one" prefix; set ${rcvar}=yes 489126303Smtm _rc_prefix=one 490126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 491126303Smtm if [ -n "${rcvar}" ]; then 492126303Smtm eval ${rcvar}=YES 493126303Smtm fi 494126303Smtm ;; 49578344Sobrien esac 49678344Sobrien 49798186Sgordon eval _overide_command=\$${name}_program 49898186Sgordon if [ -n "$_overide_command" ]; then 49998186Sgordon command=$_overide_command 50098186Sgordon fi 50198186Sgordon 50278344Sobrien _keywords="start stop restart rcvar $extra_commands" 50398186Sgordon rc_pid= 50478344Sobrien _pidcmd= 50598186Sgordon _procname=${procname:-${command}} 50698186Sgordon 507131135Smtm # setup pid check command 508131135Smtm if [ -n "$_procname" ]; then 50978344Sobrien if [ -n "$pidfile" ]; then 51098186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 51198186Sgordon else 51298186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 51378344Sobrien fi 51478344Sobrien if [ -n "$_pidcmd" ]; then 51598186Sgordon _keywords="${_keywords} status poll" 51678344Sobrien fi 51778344Sobrien fi 51878344Sobrien 51998186Sgordon if [ -z "$rc_arg" ]; then 520150796Syar rc_usage $_keywords 52178344Sobrien fi 52278344Sobrien 52378344Sobrien if [ -n "$flags" ]; then # allow override from environment 52498186Sgordon rc_flags=$flags 52578344Sobrien else 52698186Sgordon eval rc_flags=\$${name}_flags 52778344Sobrien fi 52898186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 52998186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 53098186Sgordon _group=\$${name}_group _groups=\$${name}_groups 53178344Sobrien 53298186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 533124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 53498186Sgordon unset _user 53598186Sgordon fi 53698186Sgordon fi 53798186Sgordon 53878344Sobrien # if ${rcvar} is set, and $1 is not 53998186Sgordon # "rcvar", then run 54078344Sobrien # checkyesno ${rcvar} 54178344Sobrien # and return if that failed 54278344Sobrien # 54398186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 54478344Sobrien if ! checkyesno ${rcvar}; then 54578344Sobrien return 0 54678344Sobrien fi 54778344Sobrien fi 54878344Sobrien 54978344Sobrien eval $_pidcmd # determine the pid if necessary 55078344Sobrien 55178344Sobrien for _elem in $_keywords; do 55298186Sgordon if [ "$_elem" != "$rc_arg" ]; then 55378344Sobrien continue 55478344Sobrien fi 55578344Sobrien 55678344Sobrien # if there's a custom ${XXX_cmd}, 55778344Sobrien # run that instead of the default 55878344Sobrien # 55998186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 56098186Sgordon _postcmd=\$${rc_arg}_postcmd 56178344Sobrien if [ -n "$_cmd" ]; then 56278344Sobrien # if the precmd failed and force 56378344Sobrien # isn't set, exit 56478344Sobrien # 565116097Smtm if [ -n "$_precmd" ]; then 566116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 567132892Smtm eval $_precmd $rc_extra_args 568116097Smtm _return=$? 569116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 570116097Smtm return 1 57178344Sobrien fi 57278344Sobrien 573116097Smtm if [ -n "$_cmd" ]; then 574116097Smtm debug "run_rc_command: evaluating ${_cmd}()." 575132892Smtm eval $_cmd $rc_extra_args 576116097Smtm _return=$? 577116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 578116097Smtm return 1 57998186Sgordon fi 580109582Smtm 581116097Smtm if [ -n "$_postcmd" ]; then 582116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 583132892Smtm eval $_postcmd $rc_extra_args 584116097Smtm _return=$? 585116097Smtm fi 586116097Smtm return $_return 58778344Sobrien fi 58878344Sobrien 58998186Sgordon case "$rc_arg" in # default operations... 59078344Sobrien 59178344Sobrien status) 59298186Sgordon if [ -n "$rc_pid" ]; then 59398186Sgordon echo "${name} is running as pid $rc_pid." 59478344Sobrien else 59578344Sobrien echo "${name} is not running." 59678344Sobrien return 1 59778344Sobrien fi 59878344Sobrien ;; 59978344Sobrien 60078344Sobrien start) 601131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 60298186Sgordon echo "${name} already running? (pid=$rc_pid)." 60378344Sobrien exit 1 60478344Sobrien fi 60578344Sobrien 606126287Smtm if [ ! -x ${_chroot}${command} ]; then 60798186Sgordon info "run_rc_command: cannot run ($command)." 60878344Sobrien return 0 60978344Sobrien fi 61078344Sobrien 61178344Sobrien # check for required variables, 61278344Sobrien # directories, and files 61378344Sobrien # 61478344Sobrien for _f in $required_vars; do 61578344Sobrien if ! checkyesno $_f; then 61678344Sobrien warn "\$${_f} is not set." 61798186Sgordon if [ -z "$rc_force" ]; then 61878344Sobrien return 1 61978344Sobrien fi 62078344Sobrien fi 62178344Sobrien done 62278344Sobrien for _f in $required_dirs; do 62378344Sobrien if [ ! -d "${_f}/." ]; then 62478344Sobrien warn "${_f} is not a directory." 62598186Sgordon if [ -z "$rc_force" ]; then 62678344Sobrien return 1 62778344Sobrien fi 62878344Sobrien fi 62978344Sobrien done 63078344Sobrien for _f in $required_files; do 63178344Sobrien if [ ! -r "${_f}" ]; then 63278344Sobrien warn "${_f} is not readable." 63398186Sgordon if [ -z "$rc_force" ]; then 63478344Sobrien return 1 63578344Sobrien fi 63678344Sobrien fi 63778344Sobrien done 63878344Sobrien 63978344Sobrien # if the precmd failed and force 64078344Sobrien # isn't set, exit 64178344Sobrien # 642116097Smtm if [ -n "${_precmd}" ]; then 643116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 644116097Smtm eval $_precmd 645116097Smtm _return=$? 646116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 647116097Smtm return 1 64878344Sobrien fi 64978344Sobrien 65078344Sobrien # setup the command to run, and run it 65178344Sobrien # 65278344Sobrien echo "Starting ${name}." 65378344Sobrien if [ -n "$_chroot" ]; then 65478344Sobrien _doit="\ 65578344Sobrien${_nice:+nice -n $_nice }\ 65678344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 65798186Sgordon$_chroot $command $rc_flags $command_args" 65878344Sobrien else 65978344Sobrien _doit="\ 66078344Sobrien${_chdir:+cd $_chdir; }\ 66178344Sobrien${_nice:+nice -n $_nice }\ 66298186Sgordon$command $rc_flags $command_args" 66398186Sgordon if [ -n "$_user" ]; then 66498186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 66598186Sgordon fi 66678344Sobrien fi 66798186Sgordon 66898186Sgordon # if the cmd failed and force 66998186Sgordon # isn't set, exit 67098186Sgordon # 67198186Sgordon debug "run_rc_command: _doit: $_doit" 672116097Smtm eval $_doit 673116097Smtm _return=$? 674116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 67598186Sgordon 67698186Sgordon # finally, run postcmd 67798186Sgordon # 678116097Smtm if [ -n "${_postcmd}" ]; then 679116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 680116097Smtm eval $_postcmd 681116097Smtm fi 68278344Sobrien ;; 68378344Sobrien 68478344Sobrien stop) 68598186Sgordon if [ -z "$rc_pid" ]; then 686131135Smtm [ -n "$rc_fast" ] && exit 0 68778344Sobrien if [ -n "$pidfile" ]; then 68878344Sobrien echo \ 68978344Sobrien "${name} not running? (check $pidfile)." 69078344Sobrien else 69178344Sobrien echo "${name} not running?" 69278344Sobrien fi 69378344Sobrien exit 1 69478344Sobrien fi 69578344Sobrien 69698186Sgordon # if the precmd failed and force 69798186Sgordon # isn't set, exit 69898186Sgordon # 699117977Smtm if [ -n "$_precmd" ]; then 700116097Smtm eval $_precmd 701116097Smtm _return=$? 702116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 703116097Smtm return 1 70478344Sobrien fi 70598186Sgordon 70698186Sgordon # send the signal to stop 70798186Sgordon # 70878344Sobrien echo "Stopping ${name}." 70998186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 71098186Sgordon if [ -n "$_user" ]; then 71198186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 71298186Sgordon fi 71398186Sgordon 71498186Sgordon # if the stop cmd failed and force 71598186Sgordon # isn't set, exit 71698186Sgordon # 717116097Smtm eval $_doit 718116097Smtm _return=$? 719116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 72098186Sgordon 72198186Sgordon # wait for the command to exit, 72298186Sgordon # and run postcmd. 72398186Sgordon wait_for_pids $rc_pid 724116097Smtm if [ -n "$_postcmd" ]; then 725116097Smtm eval $_postcmd 726116097Smtm _return=$? 727116097Smtm fi 72878344Sobrien ;; 72978344Sobrien 73078344Sobrien reload) 73198186Sgordon if [ -z "$rc_pid" ]; then 73278344Sobrien if [ -n "$pidfile" ]; then 73378344Sobrien echo \ 73478344Sobrien "${name} not running? (check $pidfile)." 73578344Sobrien else 73678344Sobrien echo "${name} not running?" 73778344Sobrien fi 73878344Sobrien exit 1 73978344Sobrien fi 74078344Sobrien echo "Reloading ${name} config files." 741116097Smtm if [ -n "$_precmd" ]; then 742116097Smtm eval $_precmd 743116097Smtm _return=$? 744116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 745116097Smtm return 1 74678344Sobrien fi 74798186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 74898186Sgordon if [ -n "$_user" ]; then 74998186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 75098186Sgordon fi 751116097Smtm eval $_doit 752116097Smtm _return=$? 753116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 754116097Smtm if [ -n "$_postcmd" ]; then 755116097Smtm eval $_postcmd 756116097Smtm _return=$? 75798186Sgordon fi 75878344Sobrien ;; 75978344Sobrien 76078344Sobrien restart) 761116097Smtm if [ -n "$_precmd" ]; then 762132892Smtm eval $_precmd $rc_extra_args 763116097Smtm _return=$? 764116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 765116097Smtm return 1 76678344Sobrien fi 76778344Sobrien # prevent restart being called more 76878344Sobrien # than once by any given script 76978344Sobrien # 770126285Smtm if ${_rc_restart_done:-false}; then 77178344Sobrien return 0 77278344Sobrien fi 773126285Smtm _rc_restart_done=true 77478344Sobrien 775132892Smtm ( $0 ${_rc_prefix}stop $rc_extra_args ) 776132892Smtm $0 ${_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 81178344Sobrien exit 1 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 83678344Sobrien *.sh) # run in current shell 837146490Sschweikh set $_arg; . $_file 83878344Sobrien ;; 839126288Smtm *[~#]|*.OLD|*.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 1333149050Spjd case ":${provider}" in 1334149050Spjd :#*) 1335149050Spjd continue 1336149050Spjd ;; 1337149050Spjd *.eli) 1338149050Spjd # Skip swap devices. 1339149050Spjd if [ "${type}" = "swap" -o "${options}" = "sw" ]; then 1340149050Spjd continue 1341149050Spjd fi 1342149050Spjd devices="${devices} ${provider}" 1343149050Spjd ;; 1344149050Spjd esac 1345149050Spjd done < /etc/fstab 1346149050Spjd 1347149050Spjd # Append providers from geli_devices. 1348149050Spjd devices="${devices} ${geli_devices}" 1349149050Spjd 1350149050Spjd for provider in ${devices}; do 1351149050Spjd provider=${provider%.eli} 1352149050Spjd provider=${provider#/dev/} 1353149050Spjd devices2="${devices2} ${provider}" 1354149050Spjd done 1355149050Spjd 1356149050Spjd echo ${devices2} 1357149050Spjd} 1358149050Spjd 1359131550Scpercivafi 1360