rc.subr revision 160698
1157841Sflz# $NetBSD: rc.subr,v 1.66 2006/04/01 10:05:50 he Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 160698 2006-07-26 08:03:24Z yar $ 378344Sobrien# 4157473Sflz# Copyright (c) 1997-2004 The NetBSD Foundation, Inc. 578344Sobrien# All rights reserved. 678344Sobrien# 778344Sobrien# This code is derived from software contributed to The NetBSD Foundation 878344Sobrien# by Luke Mewburn. 978344Sobrien# 1078344Sobrien# Redistribution and use in source and binary forms, with or without 1178344Sobrien# modification, are permitted provided that the following conditions 1278344Sobrien# are met: 1378344Sobrien# 1. Redistributions of source code must retain the above copyright 1478344Sobrien# notice, this list of conditions and the following disclaimer. 1578344Sobrien# 2. Redistributions in binary form must reproduce the above copyright 1678344Sobrien# notice, this list of conditions and the following disclaimer in the 1778344Sobrien# documentation and/or other materials provided with the distribution. 1878344Sobrien# 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 42157473Sflz: ${rcvar_manpage:='rc.conf(5)'} 43157473Sflz 4478344Sobrien# 4598186Sgordon# Operating System dependent/independent variables 4698186Sgordon# 4798186Sgordon 48131550Scpercivaif [ -z "${_rc_subr_loaded}" ]; then 49131550Scperciva 50131550Scperciva_rc_subr_loaded="YES" 51131550Scperciva 5298186SgordonSYSCTL="/sbin/sysctl" 5398186SgordonSYSCTL_N="${SYSCTL} -n" 5498186SgordonCMD_OSTYPE="${SYSCTL_N} kern.ostype" 55103018SgordonOSTYPE=`${CMD_OSTYPE}` 56124832SmtmID="/usr/bin/id" 57157710SflzJID=`ps -p $$ -o jid=` 58124832SmtmIDCMD="if [ -x $ID ]; then $ID -un; fi" 5998186Sgordon 60103018Sgordoncase ${OSTYPE} in 6198186SgordonFreeBSD) 6298186Sgordon SYSCTL_W="${SYSCTL}" 6398186Sgordon ;; 6498186SgordonNetBSD) 6598186Sgordon SYSCTL_W="${SYSCTL} -w" 6698186Sgordon ;; 6798186Sgordonesac 6898186Sgordon 6998186Sgordon# 7078344Sobrien# functions 7178344Sobrien# --------- 7278344Sobrien 7378344Sobrien# 7498186Sgordon# set_rcvar base_var 7598186Sgordon# Set the variable name enabling a specific service. 7698186Sgordon# FreeBSD uses ${service}_enable, while NetBSD uses 7798186Sgordon# just the name of the service. For example: 7898186Sgordon# FreeBSD: sendmail_enable="YES" 7998186Sgordon# NetBSD : sendmail="YES" 8098186Sgordon# $1 - if $name is not the base to work of off, specify 8198186Sgordon# a different one 8298186Sgordon# 8398186Sgordonset_rcvar() 8498186Sgordon{ 8598186Sgordon if [ -z "$1" ]; then 8698186Sgordon base_var=${name} 8798186Sgordon else 8898186Sgordon base_var="$1" 8998186Sgordon fi 9098186Sgordon 91103018Sgordon case ${OSTYPE} in 9298186Sgordon FreeBSD) 9398186Sgordon echo ${base_var}_enable 9498186Sgordon ;; 9598186Sgordon NetBSD) 9698186Sgordon echo ${base_var} 9798186Sgordon ;; 9898186Sgordon *) 9998186Sgordon echo 'XXX' 10098186Sgordon ;; 10198186Sgordon esac 10298186Sgordon} 10398186Sgordon 10498186Sgordon# 10598186Sgordon# force_depend script 10698186Sgordon# Force a service to start. Intended for use by services 10798186Sgordon# to resolve dependency issues. It is assumed the caller 10898186Sgordon# has check to make sure this call is necessary 10998186Sgordon# $1 - filename of script, in /etc/rc.d, to run 11098186Sgordon# 11198186Sgordonforce_depend() 11298186Sgordon{ 11398186Sgordon _depend="$1" 11498186Sgordon 11598186Sgordon info "${name} depends on ${_depend}, which will be forced to start." 116146490Sschweikh if ! /etc/rc.d/${_depend} forcestart; then 11798186Sgordon warn "Unable to force ${_depend}. It may already be running." 11898186Sgordon return 1 11998186Sgordon fi 12098186Sgordon return 0 12198186Sgordon} 12298186Sgordon 12398186Sgordon# 12478344Sobrien# checkyesno var 12578344Sobrien# Test $1 variable, and warn if not set to YES or NO. 12678344Sobrien# Return 0 if it's "yes" (et al), nonzero otherwise. 12778344Sobrien# 12878344Sobriencheckyesno() 12978344Sobrien{ 13078344Sobrien eval _value=\$${1} 13198186Sgordon debug "checkyesno: $1 is set to $_value." 13278344Sobrien case $_value in 13378344Sobrien 13478344Sobrien # "yes", "true", "on", or "1" 13578344Sobrien [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 13678344Sobrien return 0 13778344Sobrien ;; 13878344Sobrien 13978344Sobrien # "no", "false", "off", or "0" 14078344Sobrien [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 14178344Sobrien return 1 14278344Sobrien ;; 14378344Sobrien *) 144157473Sflz warn "\$${1} is not set properly - see ${rcvar_manpage}." 14578344Sobrien return 1 14678344Sobrien ;; 14778344Sobrien esac 14878344Sobrien} 14978344Sobrien 150157473Sflz# 15198186Sgordon# reverse_list list 15298186Sgordon# print the list in reverse order 15378344Sobrien# 15498186Sgordonreverse_list() 15598186Sgordon{ 15698186Sgordon _revlist= 157126286Smtm for _revfile; do 15898186Sgordon _revlist="$_revfile $_revlist" 15998186Sgordon done 16098186Sgordon echo $_revlist 16198186Sgordon} 16298186Sgordon 16378344Sobrien# 16498186Sgordon# mount_critical_filesystems type 16598186Sgordon# Go through the list of critical filesystems as provided in 16698186Sgordon# the rc.conf(5) variable $critical_filesystems_${type}, checking 16798186Sgordon# each one to see if it is mounted, and if it is not, mounting it. 16898186Sgordon# 16978344Sobrienmount_critical_filesystems() 17078344Sobrien{ 17198186Sgordon eval _fslist=\$critical_filesystems_${1} 17278344Sobrien for _fs in $_fslist; do 17378344Sobrien mount | ( 174126285Smtm _ismounted=false 17578344Sobrien while read what _on on _type type; do 17678344Sobrien if [ $on = $_fs ]; then 177126285Smtm _ismounted=true 17878344Sobrien fi 17978344Sobrien done 180126285Smtm if $_ismounted; then 181126285Smtm : 182126285Smtm else 18378344Sobrien mount $_fs >/dev/null 2>&1 18478344Sobrien fi 18598186Sgordon ) 18678344Sobrien done 18778344Sobrien} 18878344Sobrien 18978344Sobrien# 19098186Sgordon# check_pidfile pidfile procname [interpreter] 19198186Sgordon# Parses the first line of pidfile for a PID, and ensures 19278344Sobrien# that the process is running and matches procname. 19398186Sgordon# Prints the matching PID upon success, nothing otherwise. 19498186Sgordon# interpreter is optional; see _find_processes() for details. 19578344Sobrien# 19678344Sobriencheck_pidfile() 19778344Sobrien{ 19878344Sobrien _pidfile=$1 19978344Sobrien _procname=$2 20098186Sgordon _interpreter=$3 20178344Sobrien if [ -z "$_pidfile" -o -z "$_procname" ]; then 20298186Sgordon err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 20378344Sobrien fi 20478344Sobrien if [ ! -f $_pidfile ]; then 205131061Smtm debug "pid file ($_pidfile): not readable." 20678344Sobrien return 20778344Sobrien fi 20878344Sobrien read _pid _junk < $_pidfile 20978344Sobrien if [ -z "$_pid" ]; then 210139949Skeramida debug "pid file ($_pidfile): no pid in file." 21178344Sobrien return 21278344Sobrien fi 21398186Sgordon _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 21478344Sobrien} 21578344Sobrien 21678344Sobrien# 21798186Sgordon# check_process procname [interpreter] 21878344Sobrien# Ensures that a process (or processes) named procname is running. 21998186Sgordon# Prints a list of matching PIDs. 22098186Sgordon# interpreter is optional; see _find_processes() for details. 22178344Sobrien# 22278344Sobriencheck_process() 22378344Sobrien{ 22478344Sobrien _procname=$1 22598186Sgordon _interpreter=$2 22678344Sobrien if [ -z "$_procname" ]; then 22798186Sgordon err 3 'USAGE: check_process procname [interpreter]' 22878344Sobrien fi 22998186Sgordon _find_processes $_procname ${_interpreter:-.} '-ax' 23098186Sgordon} 23198186Sgordon 23298186Sgordon# 23398186Sgordon# _find_processes procname interpreter psargs 23498186Sgordon# Search for procname in the output of ps generated by psargs. 23598186Sgordon# Prints the PIDs of any matching processes, space separated. 23698186Sgordon# 23798186Sgordon# If interpreter == ".", check the following variations of procname 23898186Sgordon# against the first word of each command: 23998186Sgordon# procname 24098186Sgordon# `basename procname` 24198186Sgordon# `basename procname` + ":" 24298186Sgordon# "(" + `basename procname` + ")" 243155719Sceri# "[" + `basename procname` + "]" 24498186Sgordon# 24598186Sgordon# If interpreter != ".", read the first line of procname, remove the 24698186Sgordon# leading #!, normalise whitespace, append procname, and attempt to 24798186Sgordon# match that against each command, either as is, or with extra words 248157841Sflz# at the end. As an alternative, to deal with interpreted daemons 249157841Sflz# using perl, the basename of the interpreter plus a colon is also 250157841Sflz# tried as the prefix to procname. 25198186Sgordon# 25298186Sgordon_find_processes() 25398186Sgordon{ 25498186Sgordon if [ $# -ne 3 ]; then 25598186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 25698186Sgordon fi 25798186Sgordon _procname=$1 25898186Sgordon _interpreter=$2 25998186Sgordon _psargs=$3 26098186Sgordon 26178344Sobrien _pref= 26298186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 26398186Sgordon read _interp < $_procname # read interpreter name 26498186Sgordon _interp=${_interp#\#!} # strip #! 26598186Sgordon set -- $_interp 26698186Sgordon if [ $_interpreter != $1 ]; then 26798186Sgordon warn "\$command_interpreter $_interpreter != $1" 26878344Sobrien fi 26998186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 270157841Sflz _interpbn=${1##*/} 27198186Sgordon _fp_args='_argv' 27298186Sgordon _fp_match='case "$_argv" in 273157841Sflz ${_interp}|"${_interp} "*|"${_interpbn}: ${_procname}"*)' 27498186Sgordon else # a normal daemon 27598186Sgordon _procnamebn=${_procname##*/} 27698186Sgordon _fp_args='_arg0 _argv' 27798186Sgordon _fp_match='case "$_arg0" in 278151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 27998186Sgordon fi 28098186Sgordon 28198186Sgordon _proccheck=' 282157657Sflz ps 2>/dev/null -o "pid,jid,command" '"$_psargs"' | 283157657Sflz while read _npid _jid '"$_fp_args"'; do 28498186Sgordon case "$_npid" in 285146490Sschweikh PID) 286146490Sschweikh continue;; 287146490Sschweikh esac; '"$_fp_match"' 288157657Sflz if [ "$JID" -eq "$_jid" ]; 289157657Sflz then echo -n "$_pref$_npid"; 290157657Sflz _pref=" "; 291157657Sflz fi 29298186Sgordon ;; 29398186Sgordon esac 29498186Sgordon done' 29598186Sgordon 296114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 29798186Sgordon eval $_proccheck 29898186Sgordon} 29998186Sgordon 30098186Sgordon# 30198186Sgordon# wait_for_pids pid [pid ...] 30298186Sgordon# spins until none of the pids exist 30398186Sgordon# 30498186Sgordonwait_for_pids() 30598186Sgordon{ 306126286Smtm _list="$@" 30798186Sgordon if [ -z "$_list" ]; then 30898186Sgordon return 30998186Sgordon fi 31098186Sgordon _prefix= 31198186Sgordon while true; do 31298186Sgordon _nlist=""; 31398186Sgordon for _j in $_list; do 31498186Sgordon if kill -0 $_j 2>/dev/null; then 31598186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 31698186Sgordon fi 31798186Sgordon done 31898186Sgordon if [ -z "$_nlist" ]; then 31998186Sgordon break 32078344Sobrien fi 32198186Sgordon _list=$_nlist 32298186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 32398186Sgordon _prefix=", " 32498186Sgordon sleep 2 32578344Sobrien done 32698186Sgordon if [ -n "$_prefix" ]; then 32798186Sgordon echo "." 32898186Sgordon fi 32978344Sobrien} 33078344Sobrien 33178344Sobrien# 33298186Sgordon# run_rc_command argument 33398186Sgordon# Search for argument in the list of supported commands, which is: 33498186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 33598186Sgordon# If there's a match, run ${argument}_cmd or the default method 33698186Sgordon# (see below). 33778344Sobrien# 33898186Sgordon# If argument has a given prefix, then change the operation as follows: 33998186Sgordon# Prefix Operation 34078344Sobrien# ------ --------- 34198186Sgordon# fast Skip the pid check, and set rc_fast=yes 34298186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 343126303Smtm# one Set ${rcvar} to YES 34478344Sobrien# 34578344Sobrien# The following globals are used: 34678344Sobrien# 34798186Sgordon# Name Needed Purpose 34898186Sgordon# ---- ------ ------- 34978344Sobrien# name y Name of script. 35078344Sobrien# 35178344Sobrien# command n Full path to command. 35298186Sgordon# Not needed if ${rc_arg}_cmd is set for 35378344Sobrien# each keyword. 35478344Sobrien# 35578344Sobrien# command_args n Optional args/shell directives for command. 35678344Sobrien# 35798186Sgordon# command_interpreter n If not empty, command is interpreted, so 35898186Sgordon# call check_{pidfile,process}() appropriately. 35998186Sgordon# 36078344Sobrien# extra_commands n List of extra commands supported. 36178344Sobrien# 36298186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 36398186Sgordon# otherwise use check_process $command. 36498186Sgordon# In either case, only check if $command is set. 36578344Sobrien# 36698186Sgordon# procname n Process name to check for instead of $command. 36798186Sgordon# 36878344Sobrien# rcvar n This is checked with checkyesno to determine 36978344Sobrien# if the action should be run. 37078344Sobrien# 371157653Sflz# ${name}_program n Full path to command. 372157653Sflz# Meant to be used in /etc/rc.conf to override 373157653Sflz# ${command}. 374157653Sflz# 37578344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 37698186Sgordon# Requires /usr to be mounted. 37778344Sobrien# 37878344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 37978344Sobrien# (if not using ${name}_chroot). 38078344Sobrien# 38178344Sobrien# ${name}_flags n Arguments to call ${command} with. 38278344Sobrien# NOTE: $flags from the parent environment 38378344Sobrien# can be used to override this. 38478344Sobrien# 38578344Sobrien# ${name}_nice n Nice level to run ${command} at. 38678344Sobrien# 38778344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 38878344Sobrien# using ${name}_chroot. 38998186Sgordon# Requires /usr to be mounted. 39078344Sobrien# 39178344Sobrien# ${name}_group n Group to run chrooted ${command} as. 39298186Sgordon# Requires /usr to be mounted. 39378344Sobrien# 39498186Sgordon# ${name}_groups n Comma separated list of supplementary groups 39598186Sgordon# to run the chrooted ${command} with. 39698186Sgordon# Requires /usr to be mounted. 39778344Sobrien# 39898186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 39978344Sobrien# Otherwise, use default command (see below) 40078344Sobrien# 40198186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 40298186Sgordon# ${rc_arg}_cmd method in the default 40398186Sgordon# operation (i.e, after checking for required 40498186Sgordon# bits and process (non)existence). 40578344Sobrien# If this completes with a non-zero exit code, 40698186Sgordon# don't run ${rc_arg}_cmd. 40778344Sobrien# 40898186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 40998186Sgordon# ${rc_arg}_cmd method, if that method 41098186Sgordon# returned a zero exit code. 41198186Sgordon# 41278344Sobrien# required_dirs n If set, check for the existence of the given 41378344Sobrien# directories before running the default 41478344Sobrien# (re)start command. 41578344Sobrien# 41678344Sobrien# required_files n If set, check for the readability of the given 41778344Sobrien# files before running the default (re)start 41878344Sobrien# command. 41978344Sobrien# 42078344Sobrien# required_vars n If set, perform checkyesno on each of the 42178344Sobrien# listed variables before running the default 42278344Sobrien# (re)start command. 42378344Sobrien# 42498186Sgordon# Default behaviour for a given argument, if no override method is 42598186Sgordon# provided: 42678344Sobrien# 42798186Sgordon# Argument Default behaviour 42898186Sgordon# -------- ----------------- 42978344Sobrien# start if !running && checkyesno ${rcvar} 43078344Sobrien# ${command} 43178344Sobrien# 43278344Sobrien# stop if ${pidfile} 43398186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 43478344Sobrien# else 43598186Sgordon# rc_pid=$(check_process $command) 43698186Sgordon# kill $sig_stop $rc_pid 43798186Sgordon# wait_for_pids $rc_pid 43898186Sgordon# ($sig_stop defaults to TERM.) 43978344Sobrien# 44098186Sgordon# reload Similar to stop, except use $sig_reload instead, 44198186Sgordon# and doesn't wait_for_pids. 44278344Sobrien# $sig_reload defaults to HUP. 443151685Syar# Note that `reload' isn't provided by default, 444151685Syar# it should be enabled via $extra_commands. 44578344Sobrien# 44678344Sobrien# restart Run `stop' then `start'. 44778344Sobrien# 44898186Sgordon# status Show if ${command} is running, etc. 44978344Sobrien# 45098186Sgordon# poll Wait for ${command} to exit. 45198186Sgordon# 45298186Sgordon# rcvar Display what rc.conf variable is used (if any). 45398186Sgordon# 45498186Sgordon# Variables available to methods, and after run_rc_command() has 45598186Sgordon# completed: 45698186Sgordon# 45798186Sgordon# Variable Purpose 45898186Sgordon# -------- ------- 459126303Smtm# rc_arg Argument to command, after fast/force/one processing 46098186Sgordon# performed 46198186Sgordon# 46298186Sgordon# rc_flags Flags to start the default command with. 46398186Sgordon# Defaults to ${name}_flags, unless overridden 46498186Sgordon# by $flags from the environment. 46598186Sgordon# This variable may be changed by the precmd method. 46698186Sgordon# 46798186Sgordon# rc_pid PID of command (if appropriate) 46898186Sgordon# 46998186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 47098186Sgordon# 47198186Sgordon# rc_force Not empty if "force" was provided (q.v.) 47298186Sgordon# 47398186Sgordon# 47478344Sobrienrun_rc_command() 47578344Sobrien{ 476116097Smtm _return=0 47798186Sgordon rc_arg=$1 47878344Sobrien if [ -z "$name" ]; then 47998186Sgordon err 3 'run_rc_command: $name is not set.' 48078344Sobrien fi 48178344Sobrien 482132892Smtm # Don't repeat the first argument when passing additional command- 483132892Smtm # line arguments to the command subroutines. 484132892Smtm # 485132892Smtm shift 1 486132892Smtm rc_extra_args="$*" 487132892Smtm 488126303Smtm _rc_prefix= 48998186Sgordon case "$rc_arg" in 49078344Sobrien fast*) # "fast" prefix; don't check pid 49198186Sgordon rc_arg=${rc_arg#fast} 49298186Sgordon rc_fast=yes 49378344Sobrien ;; 494126303Smtm force*) # "force prefix; always run 49598186Sgordon rc_force=yes 496126303Smtm _rc_prefix=force 497126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 49878344Sobrien if [ -n "${rcvar}" ]; then 49978344Sobrien eval ${rcvar}=YES 50078344Sobrien fi 50178344Sobrien ;; 502126303Smtm one*) # "one" prefix; set ${rcvar}=yes 503126303Smtm _rc_prefix=one 504126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 505126303Smtm if [ -n "${rcvar}" ]; then 506126303Smtm eval ${rcvar}=YES 507126303Smtm fi 508126303Smtm ;; 50978344Sobrien esac 51078344Sobrien 51178344Sobrien _keywords="start stop restart rcvar $extra_commands" 51298186Sgordon rc_pid= 51378344Sobrien _pidcmd= 51498186Sgordon _procname=${procname:-${command}} 51598186Sgordon 516131135Smtm # setup pid check command 517131135Smtm if [ -n "$_procname" ]; then 51878344Sobrien if [ -n "$pidfile" ]; then 51998186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 52098186Sgordon else 52198186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 52278344Sobrien fi 52378344Sobrien if [ -n "$_pidcmd" ]; then 52498186Sgordon _keywords="${_keywords} status poll" 52578344Sobrien fi 52678344Sobrien fi 52778344Sobrien 52898186Sgordon if [ -z "$rc_arg" ]; then 529150796Syar rc_usage $_keywords 53078344Sobrien fi 53178344Sobrien 53278344Sobrien if [ -n "$flags" ]; then # allow override from environment 53398186Sgordon rc_flags=$flags 53478344Sobrien else 53598186Sgordon eval rc_flags=\$${name}_flags 53678344Sobrien fi 53798186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 53898186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 53998186Sgordon _group=\$${name}_group _groups=\$${name}_groups 54078344Sobrien 54198186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 542124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 54398186Sgordon unset _user 54498186Sgordon fi 54598186Sgordon fi 54698186Sgordon 54778344Sobrien # if ${rcvar} is set, and $1 is not 54898186Sgordon # "rcvar", then run 54978344Sobrien # checkyesno ${rcvar} 55078344Sobrien # and return if that failed 55178344Sobrien # 55298186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 55378344Sobrien if ! checkyesno ${rcvar}; then 55478344Sobrien return 0 55578344Sobrien fi 55678344Sobrien fi 55778344Sobrien 55878344Sobrien eval $_pidcmd # determine the pid if necessary 55978344Sobrien 56078344Sobrien for _elem in $_keywords; do 56198186Sgordon if [ "$_elem" != "$rc_arg" ]; then 56278344Sobrien continue 56378344Sobrien fi 56478344Sobrien 56578344Sobrien # if there's a custom ${XXX_cmd}, 56678344Sobrien # run that instead of the default 56778344Sobrien # 56898186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 56998186Sgordon _postcmd=\$${rc_arg}_postcmd 57078344Sobrien if [ -n "$_cmd" ]; then 57178344Sobrien # if the precmd failed and force 57278344Sobrien # isn't set, exit 57378344Sobrien # 574116097Smtm if [ -n "$_precmd" ]; then 575116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 576132892Smtm eval $_precmd $rc_extra_args 577116097Smtm _return=$? 578160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && 579160698Syar return 1 58078344Sobrien fi 58178344Sobrien 582116097Smtm if [ -n "$_cmd" ]; then 583116097Smtm debug "run_rc_command: evaluating ${_cmd}()." 584132892Smtm eval $_cmd $rc_extra_args 585116097Smtm _return=$? 586160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && 587160698Syar return 1 58898186Sgordon fi 589109582Smtm 590116097Smtm if [ -n "$_postcmd" ]; then 591116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 592132892Smtm eval $_postcmd $rc_extra_args 593116097Smtm _return=$? 594116097Smtm fi 595116097Smtm return $_return 59678344Sobrien fi 59778344Sobrien 59898186Sgordon case "$rc_arg" in # default operations... 59978344Sobrien 60078344Sobrien status) 60198186Sgordon if [ -n "$rc_pid" ]; then 60298186Sgordon echo "${name} is running as pid $rc_pid." 60378344Sobrien else 60478344Sobrien echo "${name} is not running." 60578344Sobrien return 1 60678344Sobrien fi 60778344Sobrien ;; 60878344Sobrien 60978344Sobrien start) 610131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 611157473Sflz echo 1>&2 "${name} already running? (pid=$rc_pid)." 612153152Syar return 1 61378344Sobrien fi 61478344Sobrien 615126287Smtm if [ ! -x ${_chroot}${command} ]; then 616160667Syar warn "run_rc_command: cannot run $command" 617153152Syar return 1 61878344Sobrien fi 61978344Sobrien 62078344Sobrien # check for required variables, 62178344Sobrien # directories, and files 62278344Sobrien # 62378344Sobrien for _f in $required_vars; do 62478344Sobrien if ! checkyesno $_f; then 625157473Sflz warn "\$${_f} is not enabled." 62698186Sgordon if [ -z "$rc_force" ]; then 62778344Sobrien return 1 62878344Sobrien fi 62978344Sobrien fi 63078344Sobrien done 63178344Sobrien for _f in $required_dirs; do 63278344Sobrien if [ ! -d "${_f}/." ]; then 63378344Sobrien warn "${_f} is not a directory." 63498186Sgordon if [ -z "$rc_force" ]; then 63578344Sobrien return 1 63678344Sobrien fi 63778344Sobrien fi 63878344Sobrien done 63978344Sobrien for _f in $required_files; do 64078344Sobrien if [ ! -r "${_f}" ]; then 64178344Sobrien warn "${_f} is not readable." 64298186Sgordon if [ -z "$rc_force" ]; then 64378344Sobrien return 1 64478344Sobrien fi 64578344Sobrien fi 64678344Sobrien done 64778344Sobrien 64878344Sobrien # if the precmd failed and force 64978344Sobrien # isn't set, exit 65078344Sobrien # 651116097Smtm if [ -n "${_precmd}" ]; then 652116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 653116097Smtm eval $_precmd 654116097Smtm _return=$? 655160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && 656160698Syar return 1 65778344Sobrien fi 65878344Sobrien 659160668Syar # setup the full command to run 66078344Sobrien # 66178344Sobrien echo "Starting ${name}." 66278344Sobrien if [ -n "$_chroot" ]; then 66378344Sobrien _doit="\ 66478344Sobrien${_nice:+nice -n $_nice }\ 66578344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 66698186Sgordon$_chroot $command $rc_flags $command_args" 66778344Sobrien else 66878344Sobrien _doit="\ 66978344Sobrien${_chdir:+cd $_chdir; }\ 67078344Sobrien${_nice:+nice -n $_nice }\ 67198186Sgordon$command $rc_flags $command_args" 67298186Sgordon if [ -n "$_user" ]; then 67398186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 67498186Sgordon fi 67578344Sobrien fi 67698186Sgordon 677160668Syar # run the full command; 67898186Sgordon # if the cmd failed and force 67998186Sgordon # isn't set, exit 68098186Sgordon # 68198186Sgordon debug "run_rc_command: _doit: $_doit" 682116097Smtm eval $_doit 683116097Smtm _return=$? 684160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 68598186Sgordon 68698186Sgordon # finally, run postcmd 68798186Sgordon # 688116097Smtm if [ -n "${_postcmd}" ]; then 689116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 690116097Smtm eval $_postcmd 691116097Smtm fi 69278344Sobrien ;; 69378344Sobrien 69478344Sobrien stop) 69598186Sgordon if [ -z "$rc_pid" ]; then 696153152Syar [ -n "$rc_fast" ] && return 0 69778344Sobrien if [ -n "$pidfile" ]; then 698157473Sflz echo 1>&2 \ 69978344Sobrien "${name} not running? (check $pidfile)." 70078344Sobrien else 701157473Sflz echo 1>&2 "${name} not running?" 70278344Sobrien fi 703153152Syar return 1 70478344Sobrien fi 70578344Sobrien 70698186Sgordon # if the precmd failed and force 70798186Sgordon # isn't set, exit 70898186Sgordon # 709117977Smtm if [ -n "$_precmd" ]; then 710116097Smtm eval $_precmd 711116097Smtm _return=$? 712160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && 713160698Syar return 1 71478344Sobrien fi 71598186Sgordon 71698186Sgordon # send the signal to stop 71798186Sgordon # 71878344Sobrien echo "Stopping ${name}." 71998186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 72098186Sgordon if [ -n "$_user" ]; then 72198186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 72298186Sgordon fi 72398186Sgordon 72498186Sgordon # if the stop cmd failed and force 72598186Sgordon # isn't set, exit 72698186Sgordon # 727116097Smtm eval $_doit 728116097Smtm _return=$? 729160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 73098186Sgordon 73198186Sgordon # wait for the command to exit, 73298186Sgordon # and run postcmd. 73398186Sgordon wait_for_pids $rc_pid 734116097Smtm if [ -n "$_postcmd" ]; then 735116097Smtm eval $_postcmd 736116097Smtm _return=$? 737116097Smtm fi 73878344Sobrien ;; 73978344Sobrien 74078344Sobrien reload) 74198186Sgordon if [ -z "$rc_pid" ]; then 74278344Sobrien if [ -n "$pidfile" ]; then 743157473Sflz echo 1>&2 \ 74478344Sobrien "${name} not running? (check $pidfile)." 74578344Sobrien else 746157473Sflz echo 1>&2 "${name} not running?" 74778344Sobrien fi 748153152Syar return 1 74978344Sobrien fi 75078344Sobrien echo "Reloading ${name} config files." 751116097Smtm if [ -n "$_precmd" ]; then 752116097Smtm eval $_precmd 753116097Smtm _return=$? 754160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && 755160698Syar return 1 75678344Sobrien fi 75798186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 75898186Sgordon if [ -n "$_user" ]; then 75998186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 76098186Sgordon fi 761116097Smtm eval $_doit 762116097Smtm _return=$? 763160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 764116097Smtm if [ -n "$_postcmd" ]; then 765116097Smtm eval $_postcmd 766116097Smtm _return=$? 76798186Sgordon fi 76878344Sobrien ;; 76978344Sobrien 77078344Sobrien restart) 771116097Smtm if [ -n "$_precmd" ]; then 772132892Smtm eval $_precmd $rc_extra_args 773116097Smtm _return=$? 774160698Syar [ $_return -ne 0 ] && [ -z "$rc_force" ] && 775160698Syar return 1 77678344Sobrien fi 77778344Sobrien # prevent restart being called more 77878344Sobrien # than once by any given script 77978344Sobrien # 780126285Smtm if ${_rc_restart_done:-false}; then 78178344Sobrien return 0 78278344Sobrien fi 783126285Smtm _rc_restart_done=true 78478344Sobrien 785152519Syar # run stop in a subshell to keep variables for start 786152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 787152519Syar run_rc_command ${_rc_prefix}start $rc_extra_args 78898186Sgordon 789116097Smtm if [ -n "$_postcmd" ]; then 790132892Smtm eval $_postcmd $rc_extra_args 791116097Smtm _return=$? 792116097Smtm fi 79378344Sobrien ;; 79478344Sobrien 79598186Sgordon poll) 79698186Sgordon if [ -n "$rc_pid" ]; then 79798186Sgordon wait_for_pids $rc_pid 79898186Sgordon fi 79998186Sgordon ;; 80098186Sgordon 80178344Sobrien rcvar) 80278344Sobrien echo "# $name" 80378344Sobrien if [ -n "$rcvar" ]; then 80478344Sobrien if checkyesno ${rcvar}; then 80578344Sobrien echo "\$${rcvar}=YES" 80678344Sobrien else 80778344Sobrien echo "\$${rcvar}=NO" 80878344Sobrien fi 80978344Sobrien fi 81078344Sobrien ;; 81178344Sobrien 81278344Sobrien *) 813150796Syar rc_usage $_keywords 81478344Sobrien ;; 81578344Sobrien 81678344Sobrien esac 817116097Smtm return $_return 81878344Sobrien done 81978344Sobrien 82098186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 821150796Syar rc_usage $_keywords 822153152Syar # not reached 82378344Sobrien} 82478344Sobrien 82578344Sobrien# 82678344Sobrien# run_rc_script file arg 82778344Sobrien# Start the script `file' with `arg', and correctly handle the 82878344Sobrien# return value from the script. If `file' ends with `.sh', it's 82998186Sgordon# sourced into the current environment. If `file' appears to be 83098186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 83198186Sgordon# executable run as a child process. 83278344Sobrien# 83378344Sobrienrun_rc_script() 83478344Sobrien{ 83578344Sobrien _file=$1 83678344Sobrien _arg=$2 83778344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 83878344Sobrien err 3 'USAGE: run_rc_script file arg' 83978344Sobrien fi 84078344Sobrien 84198186Sgordon unset name command command_args command_interpreter \ 84298186Sgordon extra_commands pidfile procname \ 84398186Sgordon rcvar required_dirs required_files required_vars 84498186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 84598186Sgordon 84678344Sobrien case "$_file" in 847153105Sdougb /etc/rc.d/*.sh) # run in current shell 848146490Sschweikh set $_arg; . $_file 84978344Sobrien ;; 850153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 85198186Sgordon warn "Ignoring scratch file $_file" 85298186Sgordon ;; 85378344Sobrien *) # run in subshell 85498186Sgordon if [ -x $_file ]; then 85598186Sgordon if [ -n "$rc_fast_and_loose" ]; then 856146490Sschweikh set $_arg; . $_file 85798186Sgordon else 858130161Smtm ( trap "echo Script $_file interrupted; kill -QUIT $$" 3 859130161Smtm trap "echo Script $_file interrupted; exit 1" 2 860146490Sschweikh set $_arg; . $_file ) 86198186Sgordon fi 86298186Sgordon fi 86378344Sobrien ;; 86478344Sobrien esac 86578344Sobrien} 86678344Sobrien 86778344Sobrien# 868157653Sflz# load_rc_config name 869157653Sflz# Source in the configuration file for a given name. 87078344Sobrien# 87178344Sobrienload_rc_config() 87278344Sobrien{ 873157653Sflz local _tmp 874157653Sflz 875157653Sflz _name=$1 876157653Sflz if [ -z "$_name" ]; then 877157653Sflz err 3 'USAGE: load_rc_config name' 87878344Sobrien fi 87978344Sobrien 880126285Smtm if ${_rc_conf_loaded:-false}; then 881126285Smtm : 882126285Smtm else 88398186Sgordon if [ -r /etc/defaults/rc.conf ]; then 88498186Sgordon debug "Sourcing /etc/defaults/rc.conf" 88598186Sgordon . /etc/defaults/rc.conf 88698186Sgordon source_rc_confs 88798186Sgordon elif [ -r /etc/rc.conf ]; then 88898186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 88998186Sgordon . /etc/rc.conf 89098186Sgordon fi 891126285Smtm _rc_conf_loaded=true 89298186Sgordon fi 893157653Sflz 894157653Sflz eval _override_command=\$${name}_program 895157653Sflz command=${command:+${_override_command:-$command}} 896157653Sflz 897157653Sflz if [ -z "${command}" ]; then 898157653Sflz _tmp=`/bin/realpath $0` 899157653Sflz prefix=${_tmp%/etc/rc.d/*}/ 900157653Sflz else 901157653Sflz prefix=${command%/*bin/*}/ 90278344Sobrien fi 903157653Sflz if [ "${prefix}" = "/" -o "${prefix}" = "/usr/" ] ; then 904157653Sflz etcdir="/etc" 905157653Sflz else 906157653Sflz etcdir="${prefix}etc" 907157653Sflz fi 908101850Sgordon 909157653Sflz # XXX - Deprecated 910157653Sflz if [ -f /etc/rc.conf.d/${_name} -a ${etcdir} != "/etc" ]; then 911157653Sflz debug "Sourcing /etc/rc.conf.d/${_name}" 912157653Sflz warn "Warning: /etc/rc.conf.d/${_name} is deprecated, please use ${etcdir}/rc.conf.d/${_name} instead." 913157656Sflz if [ -f ${etcdir}/rc.conf.d/${_name} ]; then 914157653Sflz warn "Warning: Both /etc/rc.conf.d/${_name} and ${etcdir}/rc.conf.d/${_name} exist." 915157653Sflz fi 916157653Sflz . /etc/rc.conf.d/${_name} 917157653Sflz fi 918157653Sflz 919157653Sflz if [ -f ${etcdir}/rc.conf.d/${_name} ]; then 920157653Sflz debug "Sourcing ${etcdir}/rc.conf.d/${_name}" 921157653Sflz . ${etcdir}/rc.conf.d/${_name} 922157653Sflz fi 923157653Sflz 924101850Sgordon # XXX - Deprecated variable name support 925101850Sgordon # 926103018Sgordon case ${OSTYPE} in 927101850Sgordon FreeBSD) 928146490Sschweikh [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 929146490Sschweikh [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 930146490Sschweikh [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 931146490Sschweikh [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 932146490Sschweikh [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 933146490Sschweikh [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 934146490Sschweikh [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 935115950Smtm [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program" 936115950Smtm [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags" 937146490Sschweikh ;; 938101850Sgordon esac 93978344Sobrien} 940157473Sflz 941157473Sflz# 942157653Sflz# load_rc_config_var name var 943157653Sflz# Read the rc.conf(5) var for name and set in the 944157473Sflz# current shell, using load_rc_config in a subshell to prevent 945157473Sflz# unwanted side effects from other variable assignments. 946157473Sflz# 947157473Sflzload_rc_config_var() 948157473Sflz{ 949157473Sflz if [ $# -ne 2 ]; then 950157653Sflz err 3 'USAGE: load_rc_config_var name var' 951157473Sflz fi 952157473Sflz eval $(eval '( 953157473Sflz load_rc_config '$1' >/dev/null; 954157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 955157473Sflz echo '$2'=\'\''${'$2'}\'\''; 956157473Sflz fi 957157473Sflz )' ) 958157473Sflz} 95978344Sobrien 96078344Sobrien# 96178344Sobrien# rc_usage commands 96278344Sobrien# Print a usage string for $0, with `commands' being a list of 96378344Sobrien# valid commands. 96478344Sobrien# 96578344Sobrienrc_usage() 96678344Sobrien{ 967126303Smtm echo -n 1>&2 "Usage: $0 [fast|force|one](" 96878344Sobrien 96978344Sobrien _sep= 970126286Smtm for _elem; do 97178344Sobrien echo -n 1>&2 "$_sep$_elem" 97278344Sobrien _sep="|" 97378344Sobrien done 97478344Sobrien echo 1>&2 ")" 97578344Sobrien exit 1 97678344Sobrien} 97778344Sobrien 97878344Sobrien# 97978344Sobrien# err exitval message 98078344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 98178344Sobrien# 98278344Sobrienerr() 98378344Sobrien{ 98478344Sobrien exitval=$1 98578344Sobrien shift 98678344Sobrien 987106643Sgordon if [ -x /usr/bin/logger ]; then 988106643Sgordon logger "$0: ERROR: $*" 989106643Sgordon fi 990106643Sgordon echo 1>&2 "$0: ERROR: $*" 99178344Sobrien exit $exitval 99278344Sobrien} 99378344Sobrien 99478344Sobrien# 99578344Sobrien# warn message 99678344Sobrien# Display message to stderr and log to the syslog. 99778344Sobrien# 99878344Sobrienwarn() 99978344Sobrien{ 1000106643Sgordon if [ -x /usr/bin/logger ]; then 1001106643Sgordon logger "$0: WARNING: $*" 1002106643Sgordon fi 1003106643Sgordon echo 1>&2 "$0: WARNING: $*" 100478344Sobrien} 100598186Sgordon 100698186Sgordon# 100798186Sgordon# info message 100898186Sgordon# Display informational message to stdout and log to syslog. 100998186Sgordon# 101098186Sgordoninfo() 101198186Sgordon{ 1012119170Smtm case ${rc_info} in 1013119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1014119170Smtm if [ -x /usr/bin/logger ]; then 1015119170Smtm logger "$0: INFO: $*" 1016119170Smtm fi 1017119170Smtm echo "$0: INFO: $*" 1018119170Smtm ;; 1019119170Smtm esac 102098186Sgordon} 102198186Sgordon 102298186Sgordon# 102398186Sgordon# debug message 1024106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 102598186Sgordon# BEWARE that you don't call any subroutine that itself calls this 102698186Sgordon# function. 102798186Sgordon# 102898186Sgordondebug() 102998186Sgordon{ 103098186Sgordon case ${rc_debug} in 103198186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1032106700Sgordon if [ -x /usr/bin/logger ]; then 1033106700Sgordon logger "$0: INFO: $*" 1034106700Sgordon fi 1035146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 103698186Sgordon ;; 103798186Sgordon esac 103898186Sgordon} 103998186Sgordon 104098186Sgordon# 104198186Sgordon# backup_file action file cur backup 104298186Sgordon# Make a backup copy of `file' into `cur', and save the previous 104398186Sgordon# version of `cur' as `backup' or use rcs for archiving. 104498186Sgordon# 104598186Sgordon# This routine checks the value of the backup_uses_rcs variable, 104698186Sgordon# which can be either YES or NO. 104798186Sgordon# 104898186Sgordon# The `action' keyword can be one of the following: 104998186Sgordon# 105098186Sgordon# add `file' is now being backed up (and is possibly 105198186Sgordon# being reentered into the backups system). `cur' 105298186Sgordon# is created and RCS files, if necessary, are 105398186Sgordon# created as well. 105498186Sgordon# 105598186Sgordon# update `file' has changed and needs to be backed up. 105698186Sgordon# If `cur' exists, it is copied to to `back' or 105798186Sgordon# checked into RCS (if the repository file is old), 105898186Sgordon# and then `file' is copied to `cur'. Another RCS 105998186Sgordon# check in done here if RCS is being used. 106098186Sgordon# 106198186Sgordon# remove `file' is no longer being tracked by the backups 106298186Sgordon# system. If RCS is not being used, `cur' is moved 106398186Sgordon# to `back', otherwise an empty file is checked in, 106498186Sgordon# and then `cur' is removed. 106598186Sgordon# 106698186Sgordon# 106798186Sgordonbackup_file() 106898186Sgordon{ 106998186Sgordon _action=$1 107098186Sgordon _file=$2 107198186Sgordon _cur=$3 107298186Sgordon _back=$4 107398186Sgordon 107498186Sgordon if checkyesno backup_uses_rcs; then 107598186Sgordon _msg0="backup archive" 107698186Sgordon _msg1="update" 107798186Sgordon 107898186Sgordon # ensure that history file is not locked 107998186Sgordon if [ -f $_cur,v ]; then 108098186Sgordon rcs -q -u -U -M $_cur 108198186Sgordon fi 108298186Sgordon 108398186Sgordon # ensure after switching to rcs that the 108498186Sgordon # current backup is not lost 108598186Sgordon if [ -f $_cur ]; then 108698186Sgordon # no archive, or current newer than archive 108798186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 108898186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 108998186Sgordon rcs -q -kb -U $_cur 109098186Sgordon co -q -f -u $_cur 109198186Sgordon fi 109298186Sgordon fi 109398186Sgordon 109498186Sgordon case $_action in 109598186Sgordon add|update) 109698186Sgordon cp -p $_file $_cur 109798186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 109898186Sgordon rcs -q -kb -U $_cur 109998186Sgordon co -q -f -u $_cur 110098186Sgordon chown root:wheel $_cur $_cur,v 110198186Sgordon ;; 110298186Sgordon remove) 110398186Sgordon cp /dev/null $_cur 110498186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 110598186Sgordon rcs -q -kb -U $_cur 110698186Sgordon chown root:wheel $_cur $_cur,v 110798186Sgordon rm $_cur 110898186Sgordon ;; 110998186Sgordon esac 111098186Sgordon else 111198186Sgordon case $_action in 111298186Sgordon add|update) 111398186Sgordon if [ -f $_cur ]; then 111498186Sgordon cp -p $_cur $_back 111598186Sgordon fi 111698186Sgordon cp -p $_file $_cur 111798186Sgordon chown root:wheel $_cur 111898186Sgordon ;; 111998186Sgordon remove) 112098186Sgordon mv -f $_cur $_back 112198186Sgordon ;; 112298186Sgordon esac 112398186Sgordon fi 112498186Sgordon} 1125119166Smtm 1126123344Smtm# make_symlink src link 1127123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1128123344Smtm# directory in which link is to be created does not exist 1129123344Smtm# a warning will be displayed and an error will be returned. 1130123344Smtm# Returns 0 on sucess, 1 otherwise. 1131119166Smtm# 1132123344Smtmmake_symlink() 1133119166Smtm{ 1134123344Smtm local src link linkdir _me 1135123344Smtm src="$1" 1136123344Smtm link="$2" 1137123344Smtm linkdir="`dirname $link`" 1138123344Smtm _me="make_symlink()" 1139119166Smtm 1140123344Smtm if [ -z "$src" -o -z "$link" ]; then 1141123344Smtm warn "$_me: requires two arguments." 1142119166Smtm return 1 1143119166Smtm fi 1144123344Smtm if [ ! -d "$linkdir" ]; then 1145160667Syar warn "$_me: the directory $linkdir does not exist." 1146119166Smtm return 1 1147119166Smtm fi 1148146490Sschweikh if ! ln -sf $src $link; then 1149123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1150119166Smtm return 1 1151119166Smtm fi 1152119166Smtm return 0 1153119166Smtm} 1154119166Smtm 1155119166Smtm# devfs_rulesets_from_file file 1156119166Smtm# Reads a set of devfs commands from file, and creates 1157119166Smtm# the specified rulesets with their rules. Returns non-zero 1158119166Smtm# if there was an error. 1159119166Smtm# 1160119166Smtmdevfs_rulesets_from_file() 1161119166Smtm{ 1162119166Smtm local file _err _me 1163119166Smtm file="$1" 1164119166Smtm _me="devfs_rulesets_from_file" 1165119166Smtm _err=0 1166119166Smtm 1167119166Smtm if [ -z "$file" ]; then 1168119166Smtm warn "$_me: you must specify a file" 1169119166Smtm return 1 1170119166Smtm fi 1171119166Smtm if [ ! -e "$file" ]; then 1172119166Smtm debug "$_me: no such file ($file)" 1173119166Smtm return 0 1174119166Smtm fi 1175119166Smtm debug "reading rulesets from file ($file)" 1176119166Smtm { while read line 1177119166Smtm do 1178119166Smtm case $line in 1179119166Smtm \#*) 1180119166Smtm continue 1181119166Smtm ;; 1182119166Smtm \[*\]*) 1183119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1184119166Smtm if [ -z "$rulenum" ]; then 1185119166Smtm warn "$_me: cannot extract rule number ($line)" 1186119166Smtm _err=1 1187119166Smtm break 1188119166Smtm fi 1189119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1190119166Smtm if [ -z "$rulename" ]; then 1191119166Smtm warn "$_me: cannot extract rule name ($line)" 1192119166Smtm _err=1 1193119166Smtm break; 1194119166Smtm fi 1195119166Smtm eval $rulename=\$rulenum 1196119166Smtm debug "found ruleset: $rulename=$rulenum" 1197146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1198119166Smtm _err=1 1199119166Smtm break 1200119166Smtm fi 1201119166Smtm ;; 1202119166Smtm *) 1203119166Smtm rulecmd="${line%%"\#*"}" 1204119166Smtm # evaluate the command incase it includes 1205119166Smtm # other rules 1206119166Smtm if [ -n "$rulecmd" ]; then 1207119166Smtm debug "adding rule ($rulecmd)" 1208119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1209119166Smtm then 1210119166Smtm _err=1 1211119166Smtm break 1212119166Smtm fi 1213119166Smtm fi 1214119166Smtm ;; 1215119166Smtm esac 1216119166Smtm if [ $_err -ne 0 ]; then 1217119166Smtm debug "error in $_me" 1218119166Smtm break 1219119166Smtm fi 1220119166Smtm done } < $file 1221119166Smtm return $_err 1222119166Smtm} 1223119166Smtm 1224119166Smtm# devfs_init_rulesets 1225119166Smtm# Initializes rulesets from configuration files. Returns 1226119166Smtm# non-zero if there was an error. 1227119166Smtm# 1228119166Smtmdevfs_init_rulesets() 1229119166Smtm{ 1230119166Smtm local file _me 1231119166Smtm _me="devfs_init_rulesets" 1232119166Smtm 1233119166Smtm # Go through this only once 1234119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1235119166Smtm debug "$_me: devfs rulesets already initialized" 1236119166Smtm return 1237119166Smtm fi 1238146490Sschweikh for file in $devfs_rulesets; do 1239119166Smtm devfs_rulesets_from_file $file || return 1 1240119166Smtm done 1241119166Smtm devfs_rulesets_init=1 1242119166Smtm debug "$_me: devfs rulesets initialized" 1243119166Smtm return 0 1244119166Smtm} 1245119166Smtm 1246119166Smtm# devfs_set_ruleset ruleset [dir] 1247151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1248119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1249119166Smtm# Returns non-zero if it could not set it successfully. 1250119166Smtm# 1251119166Smtmdevfs_set_ruleset() 1252119166Smtm{ 1253119166Smtm local devdir rs _me 1254119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1255119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1256119166Smtm _me="devfs_set_ruleset" 1257119166Smtm 1258119166Smtm if [ -z "$rs" ]; then 1259119166Smtm warn "$_me: you must specify a ruleset number" 1260119166Smtm return 1 1261119166Smtm fi 1262119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1263146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1264119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1265119166Smtm return 1 1266119166Smtm fi 1267119166Smtm return 0 1268119166Smtm} 1269119166Smtm 1270119166Smtm# devfs_apply_ruleset ruleset [dir] 1271119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1272119166Smtm# The ruleset argument must be a ruleset name as specified 1273119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1274119166Smtm# if it could not apply the ruleset. 1275119166Smtm# 1276119166Smtmdevfs_apply_ruleset() 1277119166Smtm{ 1278119166Smtm local devdir rs _me 1279119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1280119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1281119166Smtm _me="devfs_apply_ruleset" 1282119166Smtm 1283119166Smtm if [ -z "$rs" ]; then 1284119166Smtm warn "$_me: you must specify a ruleset" 1285119166Smtm return 1 1286119166Smtm fi 1287119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1288146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1289119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1290119166Smtm return 1 1291119166Smtm fi 1292119166Smtm return 0 1293119166Smtm} 1294119166Smtm 1295119166Smtm# devfs_domount dir [ruleset] 1296119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1297119166Smtm# on the mount-point. It must also be a ruleset name as specified 1298119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1299119166Smtm# 1300119166Smtmdevfs_domount() 1301119166Smtm{ 1302119166Smtm local devdir rs _me 1303119166Smtm devdir="$1" 1304119166Smtm [ -n "$2" ] && rs=$2 || rs= 1305119166Smtm _me="devfs_domount()" 1306119166Smtm 1307119166Smtm if [ -z "$devdir" ]; then 1308119166Smtm warn "$_me: you must specify a mount-point" 1309119166Smtm return 1 1310119166Smtm fi 1311119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1312146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1313119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1314119166Smtm return 1 1315119166Smtm fi 1316119166Smtm if [ -n "$rs" ]; then 1317119166Smtm devfs_init_rulesets 1318119166Smtm devfs_set_ruleset $rs $devdir 1319124797Scperciva devfs -m $devdir rule applyset 1320119166Smtm fi 1321119166Smtm return 0 1322119166Smtm} 1323119166Smtm 1324119166Smtm# devfs_mount_jail dir [ruleset] 1325119166Smtm# Mounts a devfs file system appropriate for jails 1326119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1327119166Smtm# it names will be used instead. If present, ruleset must 1328119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1329119166Smtm# This function returns non-zero if an error occurs. 1330119166Smtm# 1331119166Smtmdevfs_mount_jail() 1332119166Smtm{ 1333119166Smtm local jdev rs _me 1334119166Smtm jdev="$1" 1335119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1336119166Smtm _me="devfs_mount_jail" 1337119166Smtm 1338119166Smtm devfs_init_rulesets 1339146490Sschweikh if ! devfs_domount "$jdev" $rs; then 1340119166Smtm warn "$_me: devfs was not mounted on $jdev" 1341119166Smtm return 1 1342119166Smtm fi 1343119166Smtm return 0 1344119166Smtm} 1345127345Sbrooks 1346127345Sbrooks# Provide a function for normalizing the mounting of memory 1347127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1348127345Sbrooks# as close as possible between 5-current and 4-stable. 1349127345Sbrooks# $1 = size 1350127345Sbrooks# $2 = mount point 1351137451Skeramida# $3 = (optional) extra mdmfs flags 1352146490Sschweikhmount_md() 1353146490Sschweikh{ 1354127345Sbrooks if [ -n "$3" ]; then 1355137451Skeramida flags="$3" 1356127345Sbrooks fi 1357149421Syar /sbin/mdmfs $flags -s $1 md $2 1358127345Sbrooks} 1359131550Scperciva 1360159828Syar# Code common to scripts that need to load a kernel module 1361159828Syar# if it isn't in the kernel yet. Syntax: 1362160666Syar# load_kld [-e regex] [-m module] file 1363159828Syar# where -e or -m chooses the way to check if the module 1364159828Syar# is already loaded: 1365160666Syar# regex is egrep'd in the output from `kldstat -v', 1366160666Syar# module is passed to `kldstat -m'. 1367160666Syar# The default way is as though `-m file' were specified. 1368159828Syarload_kld() 1369159828Syar{ 1370159828Syar local _loaded _mod _opt _re 1371159828Syar 1372159828Syar while getopts "e:m:" _opt; do 1373159828Syar case "$_opt" in 1374159828Syar e) _re="$OPTARG" ;; 1375159828Syar m) _mod="$OPTARG" ;; 1376160666Syar *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; 1377159828Syar esac 1378159828Syar done 1379159828Syar shift $(($OPTIND - 1)) 1380160666Syar if [ $# -ne 1 ]; then 1381160666Syar err 3 'USAGE: load_kld [-e regex] [-m module] file' 1382160666Syar fi 1383159828Syar _mod=${_mod:-$1} 1384159828Syar _loaded=false 1385159828Syar if [ -n "$_re" ]; then 1386159828Syar if kldstat -v | egrep -q -e "$_re"; then 1387159828Syar _loaded=true 1388159828Syar fi 1389159828Syar else 1390159828Syar if kldstat -q -m "$_mod"; then 1391159828Syar _loaded=true 1392159828Syar fi 1393159828Syar fi 1394159828Syar if ! $_loaded; then 1395159828Syar if ! kldload "$1"; then 1396159828Syar warn "Unable to load kernel module $1" 1397159828Syar return 1 1398160666Syar else 1399160666Syar info "$1 kernel module loaded." 1400159828Syar fi 1401160666Syar else 1402160666Syar debug "load_kld: $1 kernel module already loaded." 1403159828Syar fi 1404159828Syar return 0 1405159828Syar} 1406159828Syar 1407149049Spjd# ltr str src dst 1408149049Spjd# Change every $src in $str to $dst. 1409149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1410149049Spjd# awk(1). 1411149049Spjdltr() 1412149049Spjd{ 1413149049Spjd local _str _src _dst _out _com 1414149049Spjd _str=$1 1415149049Spjd _src=$2 1416149049Spjd _dst=$3 1417149049Spjd _out="" 1418149049Spjd 1419149049Spjd IFS=${_src} 1420149049Spjd for _com in ${_str}; do 1421149049Spjd if [ -z "${_out}" ]; then 1422149049Spjd _out="${_com}" 1423149049Spjd else 1424149049Spjd _out="${_out}${_dst}${_com}" 1425149049Spjd fi 1426149049Spjd done 1427149049Spjd echo "${_out}" 1428149049Spjd} 1429149049Spjd 1430149050Spjd# Creates a list of providers for GELI encryption. 1431149050Spjdgeli_make_list() 1432149050Spjd{ 1433149050Spjd local devices devices2 1434149050Spjd local provider mountpoint type options rest 1435149050Spjd 1436149050Spjd # Create list of GELI providers from fstab. 1437149050Spjd while read provider mountpoint type options rest ; do 1438155570Sflz case ":${options}" in 1439155570Sflz :*noauto*) 1440155570Sflz noauto=yes 1441155570Sflz ;; 1442155570Sflz *) 1443155570Sflz noauto=no 1444155570Sflz ;; 1445155570Sflz esac 1446155570Sflz 1447149050Spjd case ":${provider}" in 1448149050Spjd :#*) 1449149050Spjd continue 1450149050Spjd ;; 1451149050Spjd *.eli) 1452149050Spjd # Skip swap devices. 1453155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1454149050Spjd continue 1455149050Spjd fi 1456149050Spjd devices="${devices} ${provider}" 1457149050Spjd ;; 1458149050Spjd esac 1459149050Spjd done < /etc/fstab 1460149050Spjd 1461149050Spjd # Append providers from geli_devices. 1462149050Spjd devices="${devices} ${geli_devices}" 1463149050Spjd 1464149050Spjd for provider in ${devices}; do 1465149050Spjd provider=${provider%.eli} 1466149050Spjd provider=${provider#/dev/} 1467149050Spjd devices2="${devices2} ${provider}" 1468149050Spjd done 1469149050Spjd 1470149050Spjd echo ${devices2} 1471149050Spjd} 1472149050Spjd 1473153027Sdougb# Find scripts in local_startup directories that use the old syntax 1474153027Sdougb# 1475153027Sdougbfind_local_scripts_old () { 1476153027Sdougb zlist='' 1477153027Sdougb slist='' 1478153027Sdougb for dir in ${local_startup}; do 1479153027Sdougb if [ -d "${dir}" ]; then 1480153027Sdougb for file in ${dir}/[0-9]*.sh; do 1481153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1482153027Sdougb continue 1483153027Sdougb zlist="$zlist $file" 1484153027Sdougb done 1485153027Sdougb for file in ${dir}/[^0-9]*.sh; do 1486153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1487153027Sdougb continue 1488153027Sdougb slist="$slist $file" 1489153027Sdougb done 1490153027Sdougb fi 1491153027Sdougb done 1492153027Sdougb} 1493153027Sdougb 1494153027Sdougbfind_local_scripts_new () { 1495153027Sdougb local_rc='' 1496153027Sdougb for dir in ${local_startup}; do 1497153027Sdougb if [ -d "${dir}" ]; then 1498153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1499153027Sdougb case "$file" in 1500153027Sdougb *.sample) ;; 1501153027Sdougb *) if [ -x "$file" ]; then 1502153027Sdougb local_rc="${local_rc} ${file}" 1503153027Sdougb fi 1504153027Sdougb ;; 1505153027Sdougb esac 1506153027Sdougb done 1507153027Sdougb fi 1508153027Sdougb done 1509153027Sdougb} 1510153027Sdougb 1511131550Scpercivafi 1512157841Sflz 1513157841Sflz_rc_subr_loaded=: 1514