rc.subr revision 126556
1126288Smtm# $NetBSD: rc.subr,v 1.60 2003/07/26 05:13:47 lukem Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 126556 2004-03-03 15:28:44Z mtm $ 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 4698186SgordonSYSCTL="/sbin/sysctl" 4798186SgordonSYSCTL_N="${SYSCTL} -n" 4898186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype" 49103018SgordonOSTYPE=`${CMD_OSTYPE}` 50124832SmtmID="/usr/bin/id" 51124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 5298186Sgordon 53103018Sgordoncase ${OSTYPE} in 5498186SgordonFreeBSD) 5598186Sgordon SYSCTL_W="${SYSCTL}" 5698186Sgordon ;; 5798186SgordonNetBSD) 5898186Sgordon SYSCTL_W="${SYSCTL} -w" 5998186Sgordon ;; 6098186Sgordonesac 6198186Sgordon 6298186Sgordon# 6378344Sobrien# functions 6478344Sobrien# --------- 6578344Sobrien 6678344Sobrien# 6798186Sgordon# set_rcvar base_var 6898186Sgordon# Set the variable name enabling a specific service. 6998186Sgordon# FreeBSD uses ${service}_enable, while NetBSD uses 7098186Sgordon# just the name of the service. For example: 7198186Sgordon# FreeBSD: sendmail_enable="YES" 7298186Sgordon# NetBSD : sendmail="YES" 7398186Sgordon# $1 - if $name is not the base to work of off, specify 7498186Sgordon# a different one 7598186Sgordon# 7698186Sgordonset_rcvar() 7798186Sgordon{ 7898186Sgordon if [ -z "$1" ]; then 7998186Sgordon base_var=${name} 8098186Sgordon else 8198186Sgordon base_var="$1" 8298186Sgordon fi 8398186Sgordon 84103018Sgordon case ${OSTYPE} in 8598186Sgordon FreeBSD) 8698186Sgordon echo ${base_var}_enable 8798186Sgordon ;; 8898186Sgordon NetBSD) 8998186Sgordon echo ${base_var} 9098186Sgordon ;; 9198186Sgordon *) 9298186Sgordon echo 'XXX' 9398186Sgordon ;; 9498186Sgordon esac 9598186Sgordon} 9698186Sgordon 9798186Sgordon# 9898186Sgordon# force_depend script 9998186Sgordon# Force a service to start. Intended for use by services 10098186Sgordon# to resolve dependency issues. It is assumed the caller 10198186Sgordon# has check to make sure this call is necessary 10298186Sgordon# $1 - filename of script, in /etc/rc.d, to run 10398186Sgordon# 10498186Sgordonforce_depend() 10598186Sgordon{ 10698186Sgordon _depend="$1" 10798186Sgordon 10898186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 10998186Sgordon if ! /etc/rc.d/${_depend} forcestart ; then 11098186Sgordon warn "Unable to force ${_depend}. It may already be running." 11198186Sgordon return 1 11298186Sgordon fi 11398186Sgordon return 0 11498186Sgordon} 11598186Sgordon 11698186Sgordon# 11778344Sobrien# checkyesno var 11878344Sobrien# Test $1 variable, and warn if not set to YES or NO. 11978344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 12078344Sobrien# 12178344Sobriencheckyesno() 12278344Sobrien{ 12378344Sobrien eval _value=\$${1} 12498186Sgordon debug "checkyesno: $1 is set to $_value." 12578344Sobrien case $_value in 12678344Sobrien 12778344Sobrien # "yes", "true", "on", or "1" 12878344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 12978344Sobrien return 0 13078344Sobrien ;; 13178344Sobrien 13278344Sobrien # "no", "false", "off", or "0" 13378344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 13478344Sobrien return 1 13578344Sobrien ;; 13678344Sobrien *) 137106643Sgordon warn "\$${1} is not set properly - see rc.conf(5)." 13878344Sobrien return 1 13978344Sobrien ;; 14078344Sobrien esac 14178344Sobrien} 14278344Sobrien 14398186Sgordon# reverse_list list 14498186Sgordon# print the list in reverse order 14578344Sobrien# 14698186Sgordonreverse_list() 14798186Sgordon{ 14898186Sgordon _revlist= 149126286Smtm for _revfile; do 15098186Sgordon _revlist="$_revfile $_revlist" 15198186Sgordon done 15298186Sgordon echo $_revlist 15398186Sgordon} 15498186Sgordon 15578344Sobrien# 15698186Sgordon# mount_critical_filesystems type 15798186Sgordon# Go through the list of critical filesystems as provided in 15898186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 15998186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 16098186Sgordon# 16178344Sobrienmount_critical_filesystems() 16278344Sobrien{ 16398186Sgordon eval _fslist=\$critical_filesystems_${1} 16478344Sobrien for _fs in $_fslist; do 16578344Sobrien mount | ( 166126285Smtm _ismounted=false 16778344Sobrien while read what _on on _type type; do 16878344Sobrien if [ $on = $_fs ]; then 169126285Smtm _ismounted=true 17078344Sobrien fi 17178344Sobrien done 172126285Smtm if $_ismounted; then 173126285Smtm : 174126285Smtm else 17578344Sobrien mount $_fs >/dev/null 2>&1 17678344Sobrien fi 17798186Sgordon ) 17878344Sobrien done 17978344Sobrien} 18078344Sobrien 18178344Sobrien# 18298186Sgordon# check_pidfile pidfile procname [interpreter] 18398186Sgordon# Parses the first line of pidfile for a PID, and ensures 18478344Sobrien# that the process is running and matches procname. 18598186Sgordon# Prints the matching PID upon success, nothing otherwise. 18698186Sgordon# interpreter is optional; see _find_processes() for details. 18778344Sobrien# 18878344Sobriencheck_pidfile() 18978344Sobrien{ 19078344Sobrien _pidfile=$1 19178344Sobrien _procname=$2 19298186Sgordon _interpreter=$3 19378344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 19498186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 19578344Sobrien fi 19678344Sobrien if [ ! -f $_pidfile ]; then 19798186Sgordon debug "pid file {$_pidfile): not readable." 19878344Sobrien return 19978344Sobrien fi 20078344Sobrien read _pid _junk < $_pidfile 20178344Sobrien if [ -z "$_pid" ]; then 20298186Sgordon debug "pid file {$_pidfile): no pid in file." 20378344Sobrien return 20478344Sobrien fi 20598186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 20678344Sobrien} 20778344Sobrien 20878344Sobrien# 20998186Sgordon# check_process procname [interpreter] 21078344Sobrien# Ensures that a process (or processes) named procname is running. 21198186Sgordon# Prints a list of matching PIDs. 21298186Sgordon# interpreter is optional; see _find_processes() for details. 21378344Sobrien# 21478344Sobriencheck_process() 21578344Sobrien{ 21678344Sobrien _procname=$1 21798186Sgordon _interpreter=$2 21878344Sobrien if [ -z "$_procname" ]; then 21998186Sgordon err 3 'USAGE: check_process procname [interpreter]' 22078344Sobrien fi 22198186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 22298186Sgordon} 22398186Sgordon 22498186Sgordon# 22598186Sgordon# _find_processes procname interpreter psargs 22698186Sgordon# Search for procname in the output of ps generated by psargs. 22798186Sgordon# Prints the PIDs of any matching processes, space separated. 22898186Sgordon# 22998186Sgordon# If interpreter == ".", check the following variations of procname 23098186Sgordon# against the first word of each command: 23198186Sgordon# procname 23298186Sgordon# `basename procname` 23398186Sgordon# `basename procname` + ":" 23498186Sgordon# "(" + `basename procname` + ")" 23598186Sgordon# 23698186Sgordon# If interpreter != ".", read the first line of procname, remove the 23798186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 23898186Sgordon# match that against each command, either as is, or with extra words 23998186Sgordon# at the end. 24098186Sgordon# 24198186Sgordon_find_processes() 24298186Sgordon{ 24398186Sgordon if [ $# -ne 3 ]; then 24498186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 24598186Sgordon fi 24698186Sgordon _procname=$1 24798186Sgordon _interpreter=$2 24898186Sgordon _psargs=$3 24998186Sgordon 25078344Sobrien _pref= 25198186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 25298186Sgordon read _interp < $_procname # read interpreter name 25398186Sgordon _interp=${_interp#\#!} # strip #! 25498186Sgordon set -- $_interp 25598186Sgordon if [ $_interpreter != $1 ]; then 25698186Sgordon warn "\$command_interpreter $_interpreter != $1" 25778344Sobrien fi 25898186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 25998186Sgordon _fp_args='_argv' 26098186Sgordon _fp_match='case "$_argv" in 26198186Sgordon ${_interp}|"${_interp} "*)' 26298186Sgordon else # a normal daemon 26398186Sgordon _procnamebn=${_procname##*/} 26498186Sgordon _fp_args='_arg0 _argv' 26598186Sgordon _fp_match='case "$_arg0" in 26698186Sgordon $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 26798186Sgordon fi 26898186Sgordon 26998186Sgordon _proccheck=' 270126556Smtm ps 2>/dev/null -o "pid,command" '"$_psargs"' | 27198186Sgordon while read _npid '"$_fp_args"'; do 27298186Sgordon case "$_npid" in 27398186Sgordon PID) 27498186Sgordon continue ;; 27598186Sgordon esac ; '"$_fp_match"' 27698186Sgordon echo -n "$_pref$_npid" ; 27798186Sgordon _pref=" " 27898186Sgordon ;; 27998186Sgordon esac 28098186Sgordon done' 28198186Sgordon 282114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 28398186Sgordon eval $_proccheck 28498186Sgordon} 28598186Sgordon 28698186Sgordon# 28798186Sgordon# wait_for_pids pid [pid ...] 28898186Sgordon# spins until none of the pids exist 28998186Sgordon# 29098186Sgordonwait_for_pids() 29198186Sgordon{ 292126286Smtm _list="$@" 29398186Sgordon if [ -z "$_list" ]; then 29498186Sgordon return 29598186Sgordon fi 29698186Sgordon _prefix= 29798186Sgordon while true; do 29898186Sgordon _nlist=""; 29998186Sgordon for _j in $_list; do 30098186Sgordon if kill -0 $_j 2>/dev/null; then 30198186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 30298186Sgordon fi 30398186Sgordon done 30498186Sgordon if [ -z "$_nlist" ]; then 30598186Sgordon break 30678344Sobrien fi 30798186Sgordon _list=$_nlist 30898186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 30998186Sgordon _prefix=", " 31098186Sgordon sleep 2 31178344Sobrien done 31298186Sgordon if [ -n "$_prefix" ]; then 31398186Sgordon echo "." 31498186Sgordon fi 31578344Sobrien} 31678344Sobrien 31778344Sobrien# 31898186Sgordon# run_rc_command argument 31998186Sgordon# Search for argument in the list of supported commands, which is: 32098186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 32198186Sgordon# If there's a match, run ${argument}_cmd or the default method 32298186Sgordon# (see below). 32378344Sobrien# 32498186Sgordon# If argument has a given prefix, then change the operation as follows: 32598186Sgordon# Prefix Operation 32678344Sobrien# ------ --------- 32798186Sgordon# fast Skip the pid check, and set rc_fast=yes 32898186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 329126303Smtm# one Set ${rcvar} to YES 33078344Sobrien# 33178344Sobrien# The following globals are used: 33278344Sobrien# 33398186Sgordon# Name Needed Purpose 33498186Sgordon# ---- ------ ------- 33578344Sobrien# name y Name of script. 33678344Sobrien# 33778344Sobrien# command n Full path to command. 33898186Sgordon# Not needed if ${rc_arg}_cmd is set for 33978344Sobrien# each keyword. 34078344Sobrien# 34178344Sobrien# command_args n Optional args/shell directives for command. 34278344Sobrien# 34398186Sgordon# command_interpreter n If not empty, command is interpreted, so 34498186Sgordon# call check_{pidfile,process}() appropriately. 34598186Sgordon# 34678344Sobrien# extra_commands n List of extra commands supported. 34778344Sobrien# 34898186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 34998186Sgordon# otherwise use check_process $command. 35098186Sgordon# In either case, only check if $command is set. 35178344Sobrien# 35298186Sgordon# procname n Process name to check for instead of $command. 35398186Sgordon# 35478344Sobrien# rcvar n This is checked with checkyesno to determine 35578344Sobrien# if the action should be run. 35678344Sobrien# 35778344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 35898186Sgordon# Requires /usr to be mounted. 35978344Sobrien# 36078344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 36178344Sobrien# (if not using ${name}_chroot). 36278344Sobrien# 36378344Sobrien# ${name}_flags n Arguments to call ${command} with. 36478344Sobrien# NOTE: $flags from the parent environment 36578344Sobrien# can be used to override this. 36678344Sobrien# 36778344Sobrien# ${name}_nice n Nice level to run ${command} at. 36878344Sobrien# 36978344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 37078344Sobrien# using ${name}_chroot. 37198186Sgordon# Requires /usr to be mounted. 37278344Sobrien# 37378344Sobrien# ${name}_group n Group to run chrooted ${command} as. 37498186Sgordon# Requires /usr to be mounted. 37578344Sobrien# 37698186Sgordon# ${name}_groups n Comma separated list of supplementary groups 37798186Sgordon# to run the chrooted ${command} with. 37898186Sgordon# Requires /usr to be mounted. 37978344Sobrien# 38098186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 38178344Sobrien# Otherwise, use default command (see below) 38278344Sobrien# 38398186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 38498186Sgordon# ${rc_arg}_cmd method in the default 38598186Sgordon# operation (i.e, after checking for required 38698186Sgordon# bits and process (non)existence). 38778344Sobrien# If this completes with a non-zero exit code, 38898186Sgordon# don't run ${rc_arg}_cmd. 38978344Sobrien# 39098186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 39198186Sgordon# ${rc_arg}_cmd method, if that method 39298186Sgordon# returned a zero exit code. 39398186Sgordon# 39478344Sobrien# required_dirs n If set, check for the existence of the given 39578344Sobrien# directories before running the default 39678344Sobrien# (re)start command. 39778344Sobrien# 39878344Sobrien# required_files n If set, check for the readability of the given 39978344Sobrien# files before running the default (re)start 40078344Sobrien# command. 40178344Sobrien# 40278344Sobrien# required_vars n If set, perform checkyesno on each of the 40378344Sobrien# listed variables before running the default 40478344Sobrien# (re)start command. 40578344Sobrien# 40698186Sgordon# Default behaviour for a given argument, if no override method is 40798186Sgordon# provided: 40878344Sobrien# 40998186Sgordon# Argument Default behaviour 41098186Sgordon# -------- ----------------- 41178344Sobrien# start if !running && checkyesno ${rcvar} 41278344Sobrien# ${command} 41378344Sobrien# 41478344Sobrien# stop if ${pidfile} 41598186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 41678344Sobrien# else 41798186Sgordon# rc_pid=$(check_process $command) 41898186Sgordon# kill $sig_stop $rc_pid 41998186Sgordon# wait_for_pids $rc_pid 42098186Sgordon# ($sig_stop defaults to TERM.) 42178344Sobrien# 42298186Sgordon# reload Similar to stop, except use $sig_reload instead, 42398186Sgordon# and doesn't wait_for_pids. 42478344Sobrien# $sig_reload defaults to HUP. 42578344Sobrien# 42678344Sobrien# restart Run `stop' then `start'. 42778344Sobrien# 42898186Sgordon# status Show if ${command} is running, etc. 42978344Sobrien# 43098186Sgordon# poll Wait for ${command} to exit. 43198186Sgordon# 43298186Sgordon# rcvar Display what rc.conf variable is used (if any). 43398186Sgordon# 43498186Sgordon# Variables available to methods, and after run_rc_command() has 43598186Sgordon# completed: 43698186Sgordon# 43798186Sgordon# Variable Purpose 43898186Sgordon# -------- ------- 439126303Smtm# rc_arg Argument to command, after fast/force/one processing 44098186Sgordon# performed 44198186Sgordon# 44298186Sgordon# rc_flags Flags to start the default command with. 44398186Sgordon# Defaults to ${name}_flags, unless overridden 44498186Sgordon# by $flags from the environment. 44598186Sgordon# This variable may be changed by the precmd method. 44698186Sgordon# 44798186Sgordon# rc_pid PID of command (if appropriate) 44898186Sgordon# 44998186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 45098186Sgordon# 45198186Sgordon# rc_force Not empty if "force" was provided (q.v.) 45298186Sgordon# 45398186Sgordon# 45478344Sobrienrun_rc_command() 45578344Sobrien{ 456116097Smtm _return=0 45798186Sgordon rc_arg=$1 45878344Sobrien if [ -z "$name" ]; then 45998186Sgordon err 3 'run_rc_command: $name is not set.' 46078344Sobrien fi 46178344Sobrien 462126303Smtm _rc_prefix= 46398186Sgordon case "$rc_arg" in 46478344Sobrien fast*) # "fast" prefix; don't check pid 46598186Sgordon rc_arg=${rc_arg#fast} 46698186Sgordon rc_fast=yes 46778344Sobrien ;; 468126303Smtm force*) # "force prefix; always run 46998186Sgordon rc_force=yes 470126303Smtm _rc_prefix=force 471126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 47278344Sobrien if [ -n "${rcvar}" ]; then 47378344Sobrien eval ${rcvar}=YES 47478344Sobrien fi 47578344Sobrien ;; 476126303Smtm one*) # "one" prefix; set ${rcvar}=yes 477126303Smtm _rc_prefix=one 478126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 479126303Smtm if [ -n "${rcvar}" ]; then 480126303Smtm eval ${rcvar}=YES 481126303Smtm fi 482126303Smtm ;; 48378344Sobrien esac 48478344Sobrien 48598186Sgordon eval _overide_command=\$${name}_program 48698186Sgordon if [ -n "$_overide_command" ]; then 48798186Sgordon command=$_overide_command 48898186Sgordon fi 48998186Sgordon 49078344Sobrien _keywords="start stop restart rcvar $extra_commands" 49198186Sgordon rc_pid= 49278344Sobrien _pidcmd= 49398186Sgordon _procname=${procname:-${command}} 49498186Sgordon 49578344Sobrien # setup pid check command if not fast 49698186Sgordon if [ -z "$rc_fast" -a -n "$_procname" ]; then 49778344Sobrien if [ -n "$pidfile" ]; then 49898186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 49998186Sgordon else 50098186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 50178344Sobrien fi 50278344Sobrien if [ -n "$_pidcmd" ]; then 50398186Sgordon _keywords="${_keywords} status poll" 50478344Sobrien fi 50578344Sobrien fi 50678344Sobrien 50798186Sgordon if [ -z "$rc_arg" ]; then 50878344Sobrien rc_usage "$_keywords" 50978344Sobrien fi 51078344Sobrien 51178344Sobrien if [ -n "$flags" ]; then # allow override from environment 51298186Sgordon rc_flags=$flags 51378344Sobrien else 51498186Sgordon eval rc_flags=\$${name}_flags 51578344Sobrien fi 51698186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 51798186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 51898186Sgordon _group=\$${name}_group _groups=\$${name}_groups 51978344Sobrien 52098186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 521124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 52298186Sgordon unset _user 52398186Sgordon fi 52498186Sgordon fi 52598186Sgordon 52678344Sobrien # if ${rcvar} is set, and $1 is not 52798186Sgordon # "rcvar", then run 52878344Sobrien # checkyesno ${rcvar} 52978344Sobrien # and return if that failed 53078344Sobrien # 53198186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 53278344Sobrien if ! checkyesno ${rcvar}; then 53378344Sobrien return 0 53478344Sobrien fi 53578344Sobrien fi 53678344Sobrien 53778344Sobrien eval $_pidcmd # determine the pid if necessary 53878344Sobrien 53978344Sobrien for _elem in $_keywords; do 54098186Sgordon if [ "$_elem" != "$rc_arg" ]; then 54178344Sobrien continue 54278344Sobrien fi 54378344Sobrien 54478344Sobrien # if there's a custom ${XXX_cmd}, 54578344Sobrien # run that instead of the default 54678344Sobrien # 54798186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 54898186Sgordon _postcmd=\$${rc_arg}_postcmd 54978344Sobrien if [ -n "$_cmd" ]; then 55078344Sobrien # if the precmd failed and force 55178344Sobrien # isn't set, exit 55278344Sobrien # 553116097Smtm if [ -n "$_precmd" ]; then 554116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 555116097Smtm eval $_precmd 556116097Smtm _return=$? 557116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 558116097Smtm return 1 55978344Sobrien fi 56078344Sobrien 561116097Smtm if [ -n "$_cmd" ]; then 562116097Smtm debug "run_rc_command: evaluating ${_cmd}()." 563116097Smtm eval $_cmd 564116097Smtm _return=$? 565116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 566116097Smtm return 1 56798186Sgordon fi 568109582Smtm 569116097Smtm if [ -n "$_postcmd" ]; then 570116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 571116097Smtm eval $_postcmd 572116097Smtm _return=$? 573116097Smtm fi 574116097Smtm return $_return 57578344Sobrien fi 57678344Sobrien 57798186Sgordon case "$rc_arg" in # default operations... 57878344Sobrien 57978344Sobrien status) 58098186Sgordon if [ -n "$rc_pid" ]; then 58198186Sgordon echo "${name} is running as pid $rc_pid." 58278344Sobrien else 58378344Sobrien echo "${name} is not running." 58478344Sobrien return 1 58578344Sobrien fi 58678344Sobrien ;; 58778344Sobrien 58878344Sobrien start) 58998186Sgordon if [ -n "$rc_pid" ]; then 59098186Sgordon echo "${name} already running? (pid=$rc_pid)." 59178344Sobrien exit 1 59278344Sobrien fi 59378344Sobrien 594126287Smtm if [ ! -x ${_chroot}${command} ]; then 59598186Sgordon info "run_rc_command: cannot run ($command)." 59678344Sobrien return 0 59778344Sobrien fi 59878344Sobrien 59978344Sobrien # check for required variables, 60078344Sobrien # directories, and files 60178344Sobrien # 60278344Sobrien for _f in $required_vars; do 60378344Sobrien if ! checkyesno $_f; then 60478344Sobrien warn "\$${_f} is not set." 60598186Sgordon if [ -z "$rc_force" ]; then 60678344Sobrien return 1 60778344Sobrien fi 60878344Sobrien fi 60978344Sobrien done 61078344Sobrien for _f in $required_dirs; do 61178344Sobrien if [ ! -d "${_f}/." ]; then 61278344Sobrien warn "${_f} is not a directory." 61398186Sgordon if [ -z "$rc_force" ]; then 61478344Sobrien return 1 61578344Sobrien fi 61678344Sobrien fi 61778344Sobrien done 61878344Sobrien for _f in $required_files; do 61978344Sobrien if [ ! -r "${_f}" ]; then 62078344Sobrien warn "${_f} is not readable." 62198186Sgordon if [ -z "$rc_force" ]; then 62278344Sobrien return 1 62378344Sobrien fi 62478344Sobrien fi 62578344Sobrien done 62678344Sobrien 62778344Sobrien # if the precmd failed and force 62878344Sobrien # isn't set, exit 62978344Sobrien # 630116097Smtm if [ -n "${_precmd}" ]; then 631116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 632116097Smtm eval $_precmd 633116097Smtm _return=$? 634116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 635116097Smtm return 1 63678344Sobrien fi 63778344Sobrien 63878344Sobrien # setup the command to run, and run it 63978344Sobrien # 64078344Sobrien echo "Starting ${name}." 64178344Sobrien if [ -n "$_chroot" ]; then 64278344Sobrien _doit="\ 64378344Sobrien${_nice:+nice -n $_nice }\ 64478344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 64598186Sgordon$_chroot $command $rc_flags $command_args" 64678344Sobrien else 64778344Sobrien _doit="\ 64878344Sobrien${_chdir:+cd $_chdir; }\ 64978344Sobrien${_nice:+nice -n $_nice }\ 65098186Sgordon$command $rc_flags $command_args" 65198186Sgordon if [ -n "$_user" ]; then 65298186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 65398186Sgordon fi 65478344Sobrien fi 65598186Sgordon 65698186Sgordon # if the cmd failed and force 65798186Sgordon # isn't set, exit 65898186Sgordon # 65998186Sgordon debug "run_rc_command: _doit: $_doit" 660116097Smtm eval $_doit 661116097Smtm _return=$? 662116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 66398186Sgordon 66498186Sgordon # finally, run postcmd 66598186Sgordon # 666116097Smtm if [ -n "${_postcmd}" ]; then 667116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 668116097Smtm eval $_postcmd 669116097Smtm fi 67078344Sobrien ;; 67178344Sobrien 67278344Sobrien stop) 67398186Sgordon if [ -z "$rc_pid" ]; then 67478344Sobrien if [ -n "$pidfile" ]; then 67578344Sobrien echo \ 67678344Sobrien "${name} not running? (check $pidfile)." 67778344Sobrien else 67878344Sobrien echo "${name} not running?" 67978344Sobrien fi 68078344Sobrien exit 1 68178344Sobrien fi 68278344Sobrien 68398186Sgordon # if the precmd failed and force 68498186Sgordon # isn't set, exit 68598186Sgordon # 686117977Smtm if [ -n "$_precmd" ]; then 687116097Smtm eval $_precmd 688116097Smtm _return=$? 689116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 690116097Smtm return 1 69178344Sobrien fi 69298186Sgordon 69398186Sgordon # send the signal to stop 69498186Sgordon # 69578344Sobrien echo "Stopping ${name}." 69698186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 69798186Sgordon if [ -n "$_user" ]; then 69898186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 69998186Sgordon fi 70098186Sgordon 70198186Sgordon # if the stop cmd failed and force 70298186Sgordon # isn't set, exit 70398186Sgordon # 704116097Smtm eval $_doit 705116097Smtm _return=$? 706116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 70798186Sgordon 70898186Sgordon # wait for the command to exit, 70998186Sgordon # and run postcmd. 71098186Sgordon wait_for_pids $rc_pid 711116097Smtm if [ -n "$_postcmd" ]; then 712116097Smtm eval $_postcmd 713116097Smtm _return=$? 714116097Smtm fi 71578344Sobrien ;; 71678344Sobrien 71778344Sobrien reload) 71898186Sgordon if [ -z "$rc_pid" ]; then 71978344Sobrien if [ -n "$pidfile" ]; then 72078344Sobrien echo \ 72178344Sobrien "${name} not running? (check $pidfile)." 72278344Sobrien else 72378344Sobrien echo "${name} not running?" 72478344Sobrien fi 72578344Sobrien exit 1 72678344Sobrien fi 72778344Sobrien echo "Reloading ${name} config files." 728116097Smtm if [ -n "$_precmd" ]; then 729116097Smtm eval $_precmd 730116097Smtm _return=$? 731116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 732116097Smtm return 1 73378344Sobrien fi 73498186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 73598186Sgordon if [ -n "$_user" ]; then 73698186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 73798186Sgordon fi 738116097Smtm eval $_doit 739116097Smtm _return=$? 740116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 741116097Smtm if [ -n "$_postcmd" ]; then 742116097Smtm eval $_postcmd 743116097Smtm _return=$? 74498186Sgordon fi 74578344Sobrien ;; 74678344Sobrien 74778344Sobrien restart) 748116097Smtm if [ -n "$_precmd" ]; then 749116097Smtm eval $_precmd 750116097Smtm _return=$? 751116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 752116097Smtm return 1 75378344Sobrien fi 75478344Sobrien # prevent restart being called more 75578344Sobrien # than once by any given script 75678344Sobrien # 757126285Smtm if ${_rc_restart_done:-false}; then 75878344Sobrien return 0 75978344Sobrien fi 760126285Smtm _rc_restart_done=true 76178344Sobrien 762126303Smtm ( $0 ${_rc_prefix}stop ) 763126303Smtm $0 ${_rc_prefix}start 76498186Sgordon 765116097Smtm if [ -n "$_postcmd" ]; then 766116097Smtm eval $_postcmd 767116097Smtm _return=$? 768116097Smtm fi 76978344Sobrien ;; 77078344Sobrien 77198186Sgordon poll) 77298186Sgordon if [ -n "$rc_pid" ]; then 77398186Sgordon wait_for_pids $rc_pid 77498186Sgordon fi 77598186Sgordon ;; 77698186Sgordon 77778344Sobrien rcvar) 77878344Sobrien echo "# $name" 77978344Sobrien if [ -n "$rcvar" ]; then 78078344Sobrien if checkyesno ${rcvar}; then 78178344Sobrien echo "\$${rcvar}=YES" 78278344Sobrien else 78378344Sobrien echo "\$${rcvar}=NO" 78478344Sobrien fi 78578344Sobrien fi 78678344Sobrien ;; 78778344Sobrien 78878344Sobrien *) 78978344Sobrien rc_usage "$_keywords" 79078344Sobrien ;; 79178344Sobrien 79278344Sobrien esac 793116097Smtm return $_return 79478344Sobrien done 79578344Sobrien 79698186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 79778344Sobrien rc_usage "$_keywords" 79878344Sobrien exit 1 79978344Sobrien} 80078344Sobrien 80178344Sobrien# 80278344Sobrien# run_rc_script file arg 80378344Sobrien# Start the script `file' with `arg', and correctly handle the 80478344Sobrien# return value from the script. If `file' ends with `.sh', it's 80598186Sgordon# sourced into the current environment. If `file' appears to be 80698186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 80798186Sgordon# executable run as a child process. 80878344Sobrien# 80978344Sobrienrun_rc_script() 81078344Sobrien{ 81178344Sobrien _file=$1 81278344Sobrien _arg=$2 81378344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 81478344Sobrien err 3 'USAGE: run_rc_script file arg' 81578344Sobrien fi 81678344Sobrien 81798186Sgordon trap "echo 'Reboot interrupted'; exit 1" 3 81898186Sgordon 81998186Sgordon unset name command command_args command_interpreter \ 82098186Sgordon extra_commands pidfile procname \ 82198186Sgordon rcvar required_dirs required_files required_vars 82298186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 82398186Sgordon 82478344Sobrien case "$_file" in 82578344Sobrien *.sh) # run in current shell 82678344Sobrien set $_arg ; . $_file 82778344Sobrien ;; 828126288Smtm *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 82998186Sgordon warn "Ignoring scratch file $_file" 83098186Sgordon ;; 83178344Sobrien *) # run in subshell 83298186Sgordon if [ -x $_file ]; then 83398186Sgordon if [ -n "$rc_fast_and_loose" ]; then 83498186Sgordon set $_arg ; . $_file 83598186Sgordon else 83698186Sgordon ( trap "echo 'Reboot interrupted'; exit 1" 3 83798186Sgordon set $_arg ; . $_file ) 83898186Sgordon fi 83998186Sgordon fi 84078344Sobrien ;; 84178344Sobrien esac 84278344Sobrien} 84378344Sobrien 84478344Sobrien# 84578344Sobrien# load_rc_config 84678344Sobrien# Source in the configuration file for a given command. 84778344Sobrien# 84878344Sobrienload_rc_config() 84978344Sobrien{ 85078344Sobrien _command=$1 85178344Sobrien if [ -z "$_command" ]; then 85278344Sobrien err 3 'USAGE: load_rc_config command' 85378344Sobrien fi 85478344Sobrien 855126285Smtm if ${_rc_conf_loaded:-false}; then 856126285Smtm : 857126285Smtm else 85898186Sgordon if [ -r /etc/defaults/rc.conf ]; then 85998186Sgordon debug "Sourcing /etc/defaults/rc.conf" 86098186Sgordon . /etc/defaults/rc.conf 86198186Sgordon source_rc_confs 86298186Sgordon elif [ -r /etc/rc.conf ]; then 86398186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 86498186Sgordon . /etc/rc.conf 86598186Sgordon fi 866126285Smtm _rc_conf_loaded=true 86798186Sgordon fi 86878344Sobrien if [ -f /etc/rc.conf.d/"$_command" ]; then 86998186Sgordon debug "Sourcing /etc/rc.conf.d/${_command}" 87078344Sobrien . /etc/rc.conf.d/"$_command" 87178344Sobrien fi 872101850Sgordon 873101850Sgordon # XXX - Deprecated variable name support 874101850Sgordon # 875103018Sgordon case ${OSTYPE} in 876101850Sgordon FreeBSD) 877101850Sgordon [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 878101850Sgordon [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 879101850Sgordon [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 880101850Sgordon [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 881101850Sgordon [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 882101850Sgordon [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 883101850Sgordon [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 884115950Smtm [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program" 885115950Smtm [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags" 886101850Sgordon ;; 887101850Sgordon esac 888101850Sgordon 88978344Sobrien} 89078344Sobrien 89178344Sobrien# 89278344Sobrien# rc_usage commands 89378344Sobrien# Print a usage string for $0, with `commands' being a list of 89478344Sobrien# valid commands. 89578344Sobrien# 89678344Sobrienrc_usage() 89778344Sobrien{ 898126303Smtm echo -n 1>&2 "Usage: $0 [fast|force|one](" 89978344Sobrien 90078344Sobrien _sep= 901126286Smtm for _elem; do 90278344Sobrien echo -n 1>&2 "$_sep$_elem" 90378344Sobrien _sep="|" 90478344Sobrien done 90578344Sobrien echo 1>&2 ")" 90678344Sobrien exit 1 90778344Sobrien} 90878344Sobrien 90978344Sobrien# 91078344Sobrien# err exitval message 91178344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 91278344Sobrien# 91378344Sobrienerr() 91478344Sobrien{ 91578344Sobrien exitval=$1 91678344Sobrien shift 91778344Sobrien 918106643Sgordon if [ -x /usr/bin/logger ]; then 919106643Sgordon logger "$0: ERROR: $*" 920106643Sgordon fi 921106643Sgordon echo 1>&2 "$0: ERROR: $*" 92278344Sobrien exit $exitval 92378344Sobrien} 92478344Sobrien 92578344Sobrien# 92678344Sobrien# warn message 92778344Sobrien# Display message to stderr and log to the syslog. 92878344Sobrien# 92978344Sobrienwarn() 93078344Sobrien{ 931106643Sgordon if [ -x /usr/bin/logger ]; then 932106643Sgordon logger "$0: WARNING: $*" 933106643Sgordon fi 934106643Sgordon echo 1>&2 "$0: WARNING: $*" 93578344Sobrien} 93698186Sgordon 93798186Sgordon# 93898186Sgordon# info message 93998186Sgordon# Display informational message to stdout and log to syslog. 94098186Sgordon# 94198186Sgordoninfo() 94298186Sgordon{ 943119170Smtm case ${rc_info} in 944119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 945119170Smtm if [ -x /usr/bin/logger ]; then 946119170Smtm logger "$0: INFO: $*" 947119170Smtm fi 948119170Smtm echo "$0: INFO: $*" 949119170Smtm ;; 950119170Smtm esac 95198186Sgordon} 95298186Sgordon 95398186Sgordon# 95498186Sgordon# debug message 955106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 95698186Sgordon# BEWARE that you don't call any subroutine that itself calls this 95798186Sgordon# function. 95898186Sgordon# 95998186Sgordondebug() 96098186Sgordon{ 96198186Sgordon case ${rc_debug} in 96298186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 963106700Sgordon if [ -x /usr/bin/logger ]; then 964106700Sgordon logger "$0: INFO: $*" 965106700Sgordon fi 966106643Sgordon echo 1>&2 "$0: DEBUG: $*" 96798186Sgordon ;; 96898186Sgordon esac 96998186Sgordon} 97098186Sgordon 97198186Sgordon# 97298186Sgordon# backup_file action file cur backup 97398186Sgordon# Make a backup copy of `file' into `cur', and save the previous 97498186Sgordon# version of `cur' as `backup' or use rcs for archiving. 97598186Sgordon# 97698186Sgordon# This routine checks the value of the backup_uses_rcs variable, 97798186Sgordon# which can be either YES or NO. 97898186Sgordon# 97998186Sgordon# The `action' keyword can be one of the following: 98098186Sgordon# 98198186Sgordon# add `file' is now being backed up (and is possibly 98298186Sgordon# being reentered into the backups system). `cur' 98398186Sgordon# is created and RCS files, if necessary, are 98498186Sgordon# created as well. 98598186Sgordon# 98698186Sgordon# update `file' has changed and needs to be backed up. 98798186Sgordon# If `cur' exists, it is copied to to `back' or 98898186Sgordon# checked into RCS (if the repository file is old), 98998186Sgordon# and then `file' is copied to `cur'. Another RCS 99098186Sgordon# check in done here if RCS is being used. 99198186Sgordon# 99298186Sgordon# remove `file' is no longer being tracked by the backups 99398186Sgordon# system. If RCS is not being used, `cur' is moved 99498186Sgordon# to `back', otherwise an empty file is checked in, 99598186Sgordon# and then `cur' is removed. 99698186Sgordon# 99798186Sgordon# 99898186Sgordonbackup_file() 99998186Sgordon{ 100098186Sgordon _action=$1 100198186Sgordon _file=$2 100298186Sgordon _cur=$3 100398186Sgordon _back=$4 100498186Sgordon 100598186Sgordon if checkyesno backup_uses_rcs; then 100698186Sgordon _msg0="backup archive" 100798186Sgordon _msg1="update" 100898186Sgordon 100998186Sgordon # ensure that history file is not locked 101098186Sgordon if [ -f $_cur,v ]; then 101198186Sgordon rcs -q -u -U -M $_cur 101298186Sgordon fi 101398186Sgordon 101498186Sgordon # ensure after switching to rcs that the 101598186Sgordon # current backup is not lost 101698186Sgordon if [ -f $_cur ]; then 101798186Sgordon # no archive, or current newer than archive 101898186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 101998186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 102098186Sgordon rcs -q -kb -U $_cur 102198186Sgordon co -q -f -u $_cur 102298186Sgordon fi 102398186Sgordon fi 102498186Sgordon 102598186Sgordon case $_action in 102698186Sgordon add|update) 102798186Sgordon cp -p $_file $_cur 102898186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 102998186Sgordon rcs -q -kb -U $_cur 103098186Sgordon co -q -f -u $_cur 103198186Sgordon chown root:wheel $_cur $_cur,v 103298186Sgordon ;; 103398186Sgordon remove) 103498186Sgordon cp /dev/null $_cur 103598186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 103698186Sgordon rcs -q -kb -U $_cur 103798186Sgordon chown root:wheel $_cur $_cur,v 103898186Sgordon rm $_cur 103998186Sgordon ;; 104098186Sgordon esac 104198186Sgordon else 104298186Sgordon case $_action in 104398186Sgordon add|update) 104498186Sgordon if [ -f $_cur ]; then 104598186Sgordon cp -p $_cur $_back 104698186Sgordon fi 104798186Sgordon cp -p $_file $_cur 104898186Sgordon chown root:wheel $_cur 104998186Sgordon ;; 105098186Sgordon remove) 105198186Sgordon mv -f $_cur $_back 105298186Sgordon ;; 105398186Sgordon esac 105498186Sgordon fi 105598186Sgordon} 1056119166Smtm 1057123344Smtm# make_symlink src link 1058123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1059123344Smtm# directory in which link is to be created does not exist 1060123344Smtm# a warning will be displayed and an error will be returned. 1061123344Smtm# Returns 0 on sucess, 1 otherwise. 1062119166Smtm# 1063123344Smtmmake_symlink() 1064119166Smtm{ 1065123344Smtm local src link linkdir _me 1066123344Smtm src="$1" 1067123344Smtm link="$2" 1068123344Smtm linkdir="`dirname $link`" 1069123344Smtm _me="make_symlink()" 1070119166Smtm 1071123344Smtm if [ -z "$src" -o -z "$link" ]; then 1072123344Smtm warn "$_me: requires two arguments." 1073119166Smtm return 1 1074119166Smtm fi 1075123344Smtm if [ ! -d "$linkdir" ]; then 1076123344Smtm warn "$_me: the directory $linkdir does not exist" 1077119166Smtm return 1 1078119166Smtm fi 1079119166Smtm if ! ln -sf $src $link ; then 1080123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1081119166Smtm return 1 1082119166Smtm fi 1083119166Smtm return 0 1084119166Smtm} 1085119166Smtm 1086119166Smtm# devfs_rulesets_from_file file 1087119166Smtm# Reads a set of devfs commands from file, and creates 1088119166Smtm# the specified rulesets with their rules. Returns non-zero 1089119166Smtm# if there was an error. 1090119166Smtm# 1091119166Smtmdevfs_rulesets_from_file() 1092119166Smtm{ 1093119166Smtm local file _err _me 1094119166Smtm file="$1" 1095119166Smtm _me="devfs_rulesets_from_file" 1096119166Smtm _err=0 1097119166Smtm 1098119166Smtm if [ -z "$file" ]; then 1099119166Smtm warn "$_me: you must specify a file" 1100119166Smtm return 1 1101119166Smtm fi 1102119166Smtm if [ ! -e "$file" ]; then 1103119166Smtm debug "$_me: no such file ($file)" 1104119166Smtm return 0 1105119166Smtm fi 1106119166Smtm debug "reading rulesets from file ($file)" 1107119166Smtm { while read line 1108119166Smtm do 1109119166Smtm case $line in 1110119166Smtm \#*) 1111119166Smtm continue 1112119166Smtm ;; 1113119166Smtm \[*\]*) 1114119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1115119166Smtm if [ -z "$rulenum" ]; then 1116119166Smtm warn "$_me: cannot extract rule number ($line)" 1117119166Smtm _err=1 1118119166Smtm break 1119119166Smtm fi 1120119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1121119166Smtm if [ -z "$rulename" ]; then 1122119166Smtm warn "$_me: cannot extract rule name ($line)" 1123119166Smtm _err=1 1124119166Smtm break; 1125119166Smtm fi 1126119166Smtm eval $rulename=\$rulenum 1127119166Smtm debug "found ruleset: $rulename=$rulenum" 1128119166Smtm if ! /sbin/devfs rule -s $rulenum delset ; then 1129119166Smtm _err=1 1130119166Smtm break 1131119166Smtm fi 1132119166Smtm ;; 1133119166Smtm *) 1134119166Smtm rulecmd="${line%%"\#*"}" 1135119166Smtm # evaluate the command incase it includes 1136119166Smtm # other rules 1137119166Smtm if [ -n "$rulecmd" ]; then 1138119166Smtm debug "adding rule ($rulecmd)" 1139119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1140119166Smtm then 1141119166Smtm _err=1 1142119166Smtm break 1143119166Smtm fi 1144119166Smtm fi 1145119166Smtm ;; 1146119166Smtm esac 1147119166Smtm if [ $_err -ne 0 ]; then 1148119166Smtm debug "error in $_me" 1149119166Smtm break 1150119166Smtm fi 1151119166Smtm done } < $file 1152119166Smtm return $_err 1153119166Smtm} 1154119166Smtm 1155119166Smtm# devfs_init_rulesets 1156119166Smtm# Initializes rulesets from configuration files. Returns 1157119166Smtm# non-zero if there was an error. 1158119166Smtm# 1159119166Smtmdevfs_init_rulesets() 1160119166Smtm{ 1161119166Smtm local file _me 1162119166Smtm _me="devfs_init_rulesets" 1163119166Smtm 1164119166Smtm # Go through this only once 1165119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1166119166Smtm debug "$_me: devfs rulesets already initialized" 1167119166Smtm return 1168119166Smtm fi 1169119166Smtm for file in $devfs_rulesets ; do 1170119166Smtm devfs_rulesets_from_file $file || return 1 1171119166Smtm done 1172119166Smtm devfs_rulesets_init=1 1173119166Smtm debug "$_me: devfs rulesets initialized" 1174119166Smtm return 0 1175119166Smtm} 1176119166Smtm 1177119166Smtm# devfs_set_ruleset ruleset [dir] 1178119166Smtm# Sets the default ruleset of dir to ruleset. The ruleset arguement 1179119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1180119166Smtm# Returns non-zero if it could not set it successfully. 1181119166Smtm# 1182119166Smtmdevfs_set_ruleset() 1183119166Smtm{ 1184119166Smtm local devdir rs _me 1185119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1186119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1187119166Smtm _me="devfs_set_ruleset" 1188119166Smtm 1189119166Smtm if [ -z "$rs" ]; then 1190119166Smtm warn "$_me: you must specify a ruleset number" 1191119166Smtm return 1 1192119166Smtm fi 1193119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1194119166Smtm if ! /sbin/devfs $devdir ruleset $rs ; then 1195119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1196119166Smtm return 1 1197119166Smtm fi 1198119166Smtm return 0 1199119166Smtm} 1200119166Smtm 1201119166Smtm# devfs_apply_ruleset ruleset [dir] 1202119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1203119166Smtm# The ruleset argument must be a ruleset name as specified 1204119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1205119166Smtm# if it could not apply the ruleset. 1206119166Smtm# 1207119166Smtmdevfs_apply_ruleset() 1208119166Smtm{ 1209119166Smtm local devdir rs _me 1210119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1211119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1212119166Smtm _me="devfs_apply_ruleset" 1213119166Smtm 1214119166Smtm if [ -z "$rs" ]; then 1215119166Smtm warn "$_me: you must specify a ruleset" 1216119166Smtm return 1 1217119166Smtm fi 1218119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1219119166Smtm if ! /sbin/devfs $devdir rule -s $rs applyset ; then 1220119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1221119166Smtm return 1 1222119166Smtm fi 1223119166Smtm return 0 1224119166Smtm} 1225119166Smtm 1226119166Smtm# devfs_domount dir [ruleset] 1227119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1228119166Smtm# on the mount-point. It must also be a ruleset name as specified 1229119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1230119166Smtm# 1231119166Smtmdevfs_domount() 1232119166Smtm{ 1233119166Smtm local devdir rs _me 1234119166Smtm devdir="$1" 1235119166Smtm [ -n "$2" ] && rs=$2 || rs= 1236119166Smtm _me="devfs_domount()" 1237119166Smtm 1238119166Smtm if [ -z "$devdir" ]; then 1239119166Smtm warn "$_me: you must specify a mount-point" 1240119166Smtm return 1 1241119166Smtm fi 1242119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1243119166Smtm if ! mount -t devfs dev "$devdir" ; then 1244119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1245119166Smtm return 1 1246119166Smtm fi 1247119166Smtm if [ -n "$rs" ]; then 1248119166Smtm devfs_init_rulesets 1249119166Smtm devfs_set_ruleset $rs $devdir 1250124797Scperciva devfs -m $devdir rule applyset 1251119166Smtm fi 1252119166Smtm return 0 1253119166Smtm} 1254119166Smtm 1255119166Smtm# devfs_mount_jail dir [ruleset] 1256119166Smtm# Mounts a devfs file system appropriate for jails 1257119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1258119166Smtm# it names will be used instead. If present, ruleset must 1259119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1260119166Smtm# This function returns non-zero if an error occurs. 1261119166Smtm# 1262119166Smtmdevfs_mount_jail() 1263119166Smtm{ 1264119166Smtm local jdev rs _me 1265119166Smtm jdev="$1" 1266119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1267119166Smtm _me="devfs_mount_jail" 1268119166Smtm 1269119166Smtm devfs_init_rulesets 1270119166Smtm if ! devfs_domount "$jdev" $rs ; then 1271119166Smtm warn "$_me: devfs was not mounted on $jdev" 1272119166Smtm return 1 1273119166Smtm fi 1274119166Smtm return 0 1275119166Smtm} 1276