rc.subr revision 157710
1157473Sflz# $NetBSD: rc.subr,v 1.65 2004/10/12 14:45:29 lukem Exp $ 298186Sgordon# $FreeBSD: head/etc/rc.subr 157710 2006-04-13 08:30:43Z flz $ 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 24898186Sgordon# at the end. 24998186Sgordon# 25098186Sgordon_find_processes() 25198186Sgordon{ 25298186Sgordon if [ $# -ne 3 ]; then 25398186Sgordon err 3 'USAGE: _find_processes procname interpreter psargs' 25498186Sgordon fi 25598186Sgordon _procname=$1 25698186Sgordon _interpreter=$2 25798186Sgordon _psargs=$3 25898186Sgordon 25978344Sobrien _pref= 26098186Sgordon if [ $_interpreter != "." ]; then # an interpreted script 26198186Sgordon read _interp < $_procname # read interpreter name 26298186Sgordon _interp=${_interp#\#!} # strip #! 26398186Sgordon set -- $_interp 26498186Sgordon if [ $_interpreter != $1 ]; then 26598186Sgordon warn "\$command_interpreter $_interpreter != $1" 26678344Sobrien fi 26798186Sgordon _interp="$* $_procname" # cleanup spaces, add _procname 26898186Sgordon _fp_args='_argv' 26998186Sgordon _fp_match='case "$_argv" in 27098186Sgordon ${_interp}|"${_interp} "*)' 27198186Sgordon else # a normal daemon 27298186Sgordon _procnamebn=${_procname##*/} 27398186Sgordon _fp_args='_arg0 _argv' 27498186Sgordon _fp_match='case "$_arg0" in 275151426Sjhb $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' 27698186Sgordon fi 27798186Sgordon 27898186Sgordon _proccheck=' 279157657Sflz ps 2>/dev/null -o "pid,jid,command" '"$_psargs"' | 280157657Sflz while read _npid _jid '"$_fp_args"'; do 28198186Sgordon case "$_npid" in 282146490Sschweikh PID) 283146490Sschweikh continue;; 284146490Sschweikh esac; '"$_fp_match"' 285157657Sflz if [ "$JID" -eq "$_jid" ]; 286157657Sflz then echo -n "$_pref$_npid"; 287157657Sflz _pref=" "; 288157657Sflz fi 28998186Sgordon ;; 29098186Sgordon esac 29198186Sgordon done' 29298186Sgordon 293114272Smtm# debug "in _find_processes: proccheck is ($_proccheck)." 29498186Sgordon eval $_proccheck 29598186Sgordon} 29698186Sgordon 29798186Sgordon# 29898186Sgordon# wait_for_pids pid [pid ...] 29998186Sgordon# spins until none of the pids exist 30098186Sgordon# 30198186Sgordonwait_for_pids() 30298186Sgordon{ 303126286Smtm _list="$@" 30498186Sgordon if [ -z "$_list" ]; then 30598186Sgordon return 30698186Sgordon fi 30798186Sgordon _prefix= 30898186Sgordon while true; do 30998186Sgordon _nlist=""; 31098186Sgordon for _j in $_list; do 31198186Sgordon if kill -0 $_j 2>/dev/null; then 31298186Sgordon _nlist="${_nlist}${_nlist:+ }$_j" 31398186Sgordon fi 31498186Sgordon done 31598186Sgordon if [ -z "$_nlist" ]; then 31698186Sgordon break 31778344Sobrien fi 31898186Sgordon _list=$_nlist 31998186Sgordon echo -n ${_prefix:-"Waiting for PIDS: "}$_list 32098186Sgordon _prefix=", " 32198186Sgordon sleep 2 32278344Sobrien done 32398186Sgordon if [ -n "$_prefix" ]; then 32498186Sgordon echo "." 32598186Sgordon fi 32678344Sobrien} 32778344Sobrien 32878344Sobrien# 32998186Sgordon# run_rc_command argument 33098186Sgordon# Search for argument in the list of supported commands, which is: 33198186Sgordon# "start stop restart rcvar status poll ${extra_commands}" 33298186Sgordon# If there's a match, run ${argument}_cmd or the default method 33398186Sgordon# (see below). 33478344Sobrien# 33598186Sgordon# If argument has a given prefix, then change the operation as follows: 33698186Sgordon# Prefix Operation 33778344Sobrien# ------ --------- 33898186Sgordon# fast Skip the pid check, and set rc_fast=yes 33998186Sgordon# force Set ${rcvar} to YES, and set rc_force=yes 340126303Smtm# one Set ${rcvar} to YES 34178344Sobrien# 34278344Sobrien# The following globals are used: 34378344Sobrien# 34498186Sgordon# Name Needed Purpose 34598186Sgordon# ---- ------ ------- 34678344Sobrien# name y Name of script. 34778344Sobrien# 34878344Sobrien# command n Full path to command. 34998186Sgordon# Not needed if ${rc_arg}_cmd is set for 35078344Sobrien# each keyword. 35178344Sobrien# 35278344Sobrien# command_args n Optional args/shell directives for command. 35378344Sobrien# 35498186Sgordon# command_interpreter n If not empty, command is interpreted, so 35598186Sgordon# call check_{pidfile,process}() appropriately. 35698186Sgordon# 35778344Sobrien# extra_commands n List of extra commands supported. 35878344Sobrien# 35998186Sgordon# pidfile n If set, use check_pidfile $pidfile $command, 36098186Sgordon# otherwise use check_process $command. 36198186Sgordon# In either case, only check if $command is set. 36278344Sobrien# 36398186Sgordon# procname n Process name to check for instead of $command. 36498186Sgordon# 36578344Sobrien# rcvar n This is checked with checkyesno to determine 36678344Sobrien# if the action should be run. 36778344Sobrien# 368157653Sflz# ${name}_program n Full path to command. 369157653Sflz# Meant to be used in /etc/rc.conf to override 370157653Sflz# ${command}. 371157653Sflz# 37278344Sobrien# ${name}_chroot n Directory to chroot to before running ${command} 37398186Sgordon# Requires /usr to be mounted. 37478344Sobrien# 37578344Sobrien# ${name}_chdir n Directory to cd to before running ${command} 37678344Sobrien# (if not using ${name}_chroot). 37778344Sobrien# 37878344Sobrien# ${name}_flags n Arguments to call ${command} with. 37978344Sobrien# NOTE: $flags from the parent environment 38078344Sobrien# can be used to override this. 38178344Sobrien# 38278344Sobrien# ${name}_nice n Nice level to run ${command} at. 38378344Sobrien# 38478344Sobrien# ${name}_user n User to run ${command} as, using su(1) if not 38578344Sobrien# using ${name}_chroot. 38698186Sgordon# Requires /usr to be mounted. 38778344Sobrien# 38878344Sobrien# ${name}_group n Group to run chrooted ${command} as. 38998186Sgordon# Requires /usr to be mounted. 39078344Sobrien# 39198186Sgordon# ${name}_groups n Comma separated list of supplementary groups 39298186Sgordon# to run the chrooted ${command} with. 39398186Sgordon# Requires /usr to be mounted. 39478344Sobrien# 39598186Sgordon# ${rc_arg}_cmd n If set, use this as the method when invoked; 39678344Sobrien# Otherwise, use default command (see below) 39778344Sobrien# 39898186Sgordon# ${rc_arg}_precmd n If set, run just before performing the 39998186Sgordon# ${rc_arg}_cmd method in the default 40098186Sgordon# operation (i.e, after checking for required 40198186Sgordon# bits and process (non)existence). 40278344Sobrien# If this completes with a non-zero exit code, 40398186Sgordon# don't run ${rc_arg}_cmd. 40478344Sobrien# 40598186Sgordon# ${rc_arg}_postcmd n If set, run just after performing the 40698186Sgordon# ${rc_arg}_cmd method, if that method 40798186Sgordon# returned a zero exit code. 40898186Sgordon# 40978344Sobrien# required_dirs n If set, check for the existence of the given 41078344Sobrien# directories before running the default 41178344Sobrien# (re)start command. 41278344Sobrien# 41378344Sobrien# required_files n If set, check for the readability of the given 41478344Sobrien# files before running the default (re)start 41578344Sobrien# command. 41678344Sobrien# 41778344Sobrien# required_vars n If set, perform checkyesno on each of the 41878344Sobrien# listed variables before running the default 41978344Sobrien# (re)start command. 42078344Sobrien# 42198186Sgordon# Default behaviour for a given argument, if no override method is 42298186Sgordon# provided: 42378344Sobrien# 42498186Sgordon# Argument Default behaviour 42598186Sgordon# -------- ----------------- 42678344Sobrien# start if !running && checkyesno ${rcvar} 42778344Sobrien# ${command} 42878344Sobrien# 42978344Sobrien# stop if ${pidfile} 43098186Sgordon# rc_pid=$(check_pidfile $pidfile $command) 43178344Sobrien# else 43298186Sgordon# rc_pid=$(check_process $command) 43398186Sgordon# kill $sig_stop $rc_pid 43498186Sgordon# wait_for_pids $rc_pid 43598186Sgordon# ($sig_stop defaults to TERM.) 43678344Sobrien# 43798186Sgordon# reload Similar to stop, except use $sig_reload instead, 43898186Sgordon# and doesn't wait_for_pids. 43978344Sobrien# $sig_reload defaults to HUP. 440151685Syar# Note that `reload' isn't provided by default, 441151685Syar# it should be enabled via $extra_commands. 44278344Sobrien# 44378344Sobrien# restart Run `stop' then `start'. 44478344Sobrien# 44598186Sgordon# status Show if ${command} is running, etc. 44678344Sobrien# 44798186Sgordon# poll Wait for ${command} to exit. 44898186Sgordon# 44998186Sgordon# rcvar Display what rc.conf variable is used (if any). 45098186Sgordon# 45198186Sgordon# Variables available to methods, and after run_rc_command() has 45298186Sgordon# completed: 45398186Sgordon# 45498186Sgordon# Variable Purpose 45598186Sgordon# -------- ------- 456126303Smtm# rc_arg Argument to command, after fast/force/one processing 45798186Sgordon# performed 45898186Sgordon# 45998186Sgordon# rc_flags Flags to start the default command with. 46098186Sgordon# Defaults to ${name}_flags, unless overridden 46198186Sgordon# by $flags from the environment. 46298186Sgordon# This variable may be changed by the precmd method. 46398186Sgordon# 46498186Sgordon# rc_pid PID of command (if appropriate) 46598186Sgordon# 46698186Sgordon# rc_fast Not empty if "fast" was provided (q.v.) 46798186Sgordon# 46898186Sgordon# rc_force Not empty if "force" was provided (q.v.) 46998186Sgordon# 47098186Sgordon# 47178344Sobrienrun_rc_command() 47278344Sobrien{ 473116097Smtm _return=0 47498186Sgordon rc_arg=$1 47578344Sobrien if [ -z "$name" ]; then 47698186Sgordon err 3 'run_rc_command: $name is not set.' 47778344Sobrien fi 47878344Sobrien 479132892Smtm # Don't repeat the first argument when passing additional command- 480132892Smtm # line arguments to the command subroutines. 481132892Smtm # 482132892Smtm shift 1 483132892Smtm rc_extra_args="$*" 484132892Smtm 485126303Smtm _rc_prefix= 48698186Sgordon case "$rc_arg" in 48778344Sobrien fast*) # "fast" prefix; don't check pid 48898186Sgordon rc_arg=${rc_arg#fast} 48998186Sgordon rc_fast=yes 49078344Sobrien ;; 491126303Smtm force*) # "force prefix; always run 49298186Sgordon rc_force=yes 493126303Smtm _rc_prefix=force 494126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 49578344Sobrien if [ -n "${rcvar}" ]; then 49678344Sobrien eval ${rcvar}=YES 49778344Sobrien fi 49878344Sobrien ;; 499126303Smtm one*) # "one" prefix; set ${rcvar}=yes 500126303Smtm _rc_prefix=one 501126303Smtm rc_arg=${rc_arg#${_rc_prefix}} 502126303Smtm if [ -n "${rcvar}" ]; then 503126303Smtm eval ${rcvar}=YES 504126303Smtm fi 505126303Smtm ;; 50678344Sobrien esac 50778344Sobrien 50878344Sobrien _keywords="start stop restart rcvar $extra_commands" 50998186Sgordon rc_pid= 51078344Sobrien _pidcmd= 51198186Sgordon _procname=${procname:-${command}} 51298186Sgordon 513131135Smtm # setup pid check command 514131135Smtm if [ -n "$_procname" ]; then 51578344Sobrien if [ -n "$pidfile" ]; then 51698186Sgordon _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 51798186Sgordon else 51898186Sgordon _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 51978344Sobrien fi 52078344Sobrien if [ -n "$_pidcmd" ]; then 52198186Sgordon _keywords="${_keywords} status poll" 52278344Sobrien fi 52378344Sobrien fi 52478344Sobrien 52598186Sgordon if [ -z "$rc_arg" ]; then 526150796Syar rc_usage $_keywords 52778344Sobrien fi 52878344Sobrien 52978344Sobrien if [ -n "$flags" ]; then # allow override from environment 53098186Sgordon rc_flags=$flags 53178344Sobrien else 53298186Sgordon eval rc_flags=\$${name}_flags 53378344Sobrien fi 53498186Sgordon eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 53598186Sgordon _nice=\$${name}_nice _user=\$${name}_user \ 53698186Sgordon _group=\$${name}_group _groups=\$${name}_groups 53778344Sobrien 53898186Sgordon if [ -n "$_user" ]; then # unset $_user if running as that user 539124832Smtm if [ "$_user" = "$(eval $IDCMD)" ]; then 54098186Sgordon unset _user 54198186Sgordon fi 54298186Sgordon fi 54398186Sgordon 54478344Sobrien # if ${rcvar} is set, and $1 is not 54598186Sgordon # "rcvar", then run 54678344Sobrien # checkyesno ${rcvar} 54778344Sobrien # and return if that failed 54878344Sobrien # 54998186Sgordon if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 55078344Sobrien if ! checkyesno ${rcvar}; then 55178344Sobrien return 0 55278344Sobrien fi 55378344Sobrien fi 55478344Sobrien 55578344Sobrien eval $_pidcmd # determine the pid if necessary 55678344Sobrien 55778344Sobrien for _elem in $_keywords; do 55898186Sgordon if [ "$_elem" != "$rc_arg" ]; then 55978344Sobrien continue 56078344Sobrien fi 56178344Sobrien 56278344Sobrien # if there's a custom ${XXX_cmd}, 56378344Sobrien # run that instead of the default 56478344Sobrien # 56598186Sgordon eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 56698186Sgordon _postcmd=\$${rc_arg}_postcmd 56778344Sobrien if [ -n "$_cmd" ]; then 56878344Sobrien # if the precmd failed and force 56978344Sobrien # isn't set, exit 57078344Sobrien # 571116097Smtm if [ -n "$_precmd" ]; then 572116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 573132892Smtm eval $_precmd $rc_extra_args 574116097Smtm _return=$? 575116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 576116097Smtm return 1 57778344Sobrien fi 57878344Sobrien 579116097Smtm if [ -n "$_cmd" ]; then 580116097Smtm debug "run_rc_command: evaluating ${_cmd}()." 581132892Smtm eval $_cmd $rc_extra_args 582116097Smtm _return=$? 583116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 584116097Smtm return 1 58598186Sgordon fi 586109582Smtm 587116097Smtm if [ -n "$_postcmd" ]; then 588116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 589132892Smtm eval $_postcmd $rc_extra_args 590116097Smtm _return=$? 591116097Smtm fi 592116097Smtm return $_return 59378344Sobrien fi 59478344Sobrien 59598186Sgordon case "$rc_arg" in # default operations... 59678344Sobrien 59778344Sobrien status) 59898186Sgordon if [ -n "$rc_pid" ]; then 59998186Sgordon echo "${name} is running as pid $rc_pid." 60078344Sobrien else 60178344Sobrien echo "${name} is not running." 60278344Sobrien return 1 60378344Sobrien fi 60478344Sobrien ;; 60578344Sobrien 60678344Sobrien start) 607131135Smtm if [ -z "$rc_fast" -a -n "$rc_pid" ]; then 608157473Sflz echo 1>&2 "${name} already running? (pid=$rc_pid)." 609153152Syar return 1 61078344Sobrien fi 61178344Sobrien 612126287Smtm if [ ! -x ${_chroot}${command} ]; then 61398186Sgordon info "run_rc_command: cannot run ($command)." 614153152Syar return 1 61578344Sobrien fi 61678344Sobrien 61778344Sobrien # check for required variables, 61878344Sobrien # directories, and files 61978344Sobrien # 62078344Sobrien for _f in $required_vars; do 62178344Sobrien if ! checkyesno $_f; then 622157473Sflz warn "\$${_f} is not enabled." 62398186Sgordon if [ -z "$rc_force" ]; then 62478344Sobrien return 1 62578344Sobrien fi 62678344Sobrien fi 62778344Sobrien done 62878344Sobrien for _f in $required_dirs; do 62978344Sobrien if [ ! -d "${_f}/." ]; then 63078344Sobrien warn "${_f} is not a directory." 63198186Sgordon if [ -z "$rc_force" ]; then 63278344Sobrien return 1 63378344Sobrien fi 63478344Sobrien fi 63578344Sobrien done 63678344Sobrien for _f in $required_files; do 63778344Sobrien if [ ! -r "${_f}" ]; then 63878344Sobrien warn "${_f} is not readable." 63998186Sgordon if [ -z "$rc_force" ]; then 64078344Sobrien return 1 64178344Sobrien fi 64278344Sobrien fi 64378344Sobrien done 64478344Sobrien 64578344Sobrien # if the precmd failed and force 64678344Sobrien # isn't set, exit 64778344Sobrien # 648116097Smtm if [ -n "${_precmd}" ]; then 649116097Smtm debug "run_rc_command: evaluating ${_precmd}()." 650116097Smtm eval $_precmd 651116097Smtm _return=$? 652116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 653116097Smtm return 1 65478344Sobrien fi 65578344Sobrien 65678344Sobrien # setup the command to run, and run it 65778344Sobrien # 65878344Sobrien echo "Starting ${name}." 65978344Sobrien if [ -n "$_chroot" ]; then 66078344Sobrien _doit="\ 66178344Sobrien${_nice:+nice -n $_nice }\ 66278344Sobrienchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 66398186Sgordon$_chroot $command $rc_flags $command_args" 66478344Sobrien else 66578344Sobrien _doit="\ 66678344Sobrien${_chdir:+cd $_chdir; }\ 66778344Sobrien${_nice:+nice -n $_nice }\ 66898186Sgordon$command $rc_flags $command_args" 66998186Sgordon if [ -n "$_user" ]; then 67098186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 67198186Sgordon fi 67278344Sobrien fi 67398186Sgordon 67498186Sgordon # if the cmd failed and force 67598186Sgordon # isn't set, exit 67698186Sgordon # 67798186Sgordon debug "run_rc_command: _doit: $_doit" 678116097Smtm eval $_doit 679116097Smtm _return=$? 680116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 68198186Sgordon 68298186Sgordon # finally, run postcmd 68398186Sgordon # 684116097Smtm if [ -n "${_postcmd}" ]; then 685116097Smtm debug "run_rc_command: evaluating ${_postcmd}()." 686116097Smtm eval $_postcmd 687116097Smtm fi 68878344Sobrien ;; 68978344Sobrien 69078344Sobrien stop) 69198186Sgordon if [ -z "$rc_pid" ]; then 692153152Syar [ -n "$rc_fast" ] && return 0 69378344Sobrien if [ -n "$pidfile" ]; then 694157473Sflz echo 1>&2 \ 69578344Sobrien "${name} not running? (check $pidfile)." 69678344Sobrien else 697157473Sflz echo 1>&2 "${name} not running?" 69878344Sobrien fi 699153152Syar return 1 70078344Sobrien fi 70178344Sobrien 70298186Sgordon # if the precmd failed and force 70398186Sgordon # isn't set, exit 70498186Sgordon # 705117977Smtm if [ -n "$_precmd" ]; then 706116097Smtm eval $_precmd 707116097Smtm _return=$? 708116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 709116097Smtm return 1 71078344Sobrien fi 71198186Sgordon 71298186Sgordon # send the signal to stop 71398186Sgordon # 71478344Sobrien echo "Stopping ${name}." 71598186Sgordon _doit="kill -${sig_stop:-TERM} $rc_pid" 71698186Sgordon if [ -n "$_user" ]; then 71798186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 71898186Sgordon fi 71998186Sgordon 72098186Sgordon # if the stop cmd failed and force 72198186Sgordon # isn't set, exit 72298186Sgordon # 723116097Smtm eval $_doit 724116097Smtm _return=$? 725116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 72698186Sgordon 72798186Sgordon # wait for the command to exit, 72898186Sgordon # and run postcmd. 72998186Sgordon wait_for_pids $rc_pid 730116097Smtm if [ -n "$_postcmd" ]; then 731116097Smtm eval $_postcmd 732116097Smtm _return=$? 733116097Smtm fi 73478344Sobrien ;; 73578344Sobrien 73678344Sobrien reload) 73798186Sgordon if [ -z "$rc_pid" ]; then 73878344Sobrien if [ -n "$pidfile" ]; then 739157473Sflz echo 1>&2 \ 74078344Sobrien "${name} not running? (check $pidfile)." 74178344Sobrien else 742157473Sflz echo 1>&2 "${name} not running?" 74378344Sobrien fi 744153152Syar return 1 74578344Sobrien fi 74678344Sobrien echo "Reloading ${name} config files." 747116097Smtm if [ -n "$_precmd" ]; then 748116097Smtm eval $_precmd 749116097Smtm _return=$? 750116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 751116097Smtm return 1 75278344Sobrien fi 75398186Sgordon _doit="kill -${sig_reload:-HUP} $rc_pid" 75498186Sgordon if [ -n "$_user" ]; then 75598186Sgordon _doit="su -m $_user -c 'sh -c \"$_doit\"'" 75698186Sgordon fi 757116097Smtm eval $_doit 758116097Smtm _return=$? 759116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 760116097Smtm if [ -n "$_postcmd" ]; then 761116097Smtm eval $_postcmd 762116097Smtm _return=$? 76398186Sgordon fi 76478344Sobrien ;; 76578344Sobrien 76678344Sobrien restart) 767116097Smtm if [ -n "$_precmd" ]; then 768132892Smtm eval $_precmd $rc_extra_args 769116097Smtm _return=$? 770116097Smtm [ $_return -ne 0 ] && [ -z "$rc_force" ] && 771116097Smtm return 1 77278344Sobrien fi 77378344Sobrien # prevent restart being called more 77478344Sobrien # than once by any given script 77578344Sobrien # 776126285Smtm if ${_rc_restart_done:-false}; then 77778344Sobrien return 0 77878344Sobrien fi 779126285Smtm _rc_restart_done=true 78078344Sobrien 781152519Syar # run stop in a subshell to keep variables for start 782152519Syar ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) 783152519Syar run_rc_command ${_rc_prefix}start $rc_extra_args 78498186Sgordon 785116097Smtm if [ -n "$_postcmd" ]; then 786132892Smtm eval $_postcmd $rc_extra_args 787116097Smtm _return=$? 788116097Smtm fi 78978344Sobrien ;; 79078344Sobrien 79198186Sgordon poll) 79298186Sgordon if [ -n "$rc_pid" ]; then 79398186Sgordon wait_for_pids $rc_pid 79498186Sgordon fi 79598186Sgordon ;; 79698186Sgordon 79778344Sobrien rcvar) 79878344Sobrien echo "# $name" 79978344Sobrien if [ -n "$rcvar" ]; then 80078344Sobrien if checkyesno ${rcvar}; then 80178344Sobrien echo "\$${rcvar}=YES" 80278344Sobrien else 80378344Sobrien echo "\$${rcvar}=NO" 80478344Sobrien fi 80578344Sobrien fi 80678344Sobrien ;; 80778344Sobrien 80878344Sobrien *) 809150796Syar rc_usage $_keywords 81078344Sobrien ;; 81178344Sobrien 81278344Sobrien esac 813116097Smtm return $_return 81478344Sobrien done 81578344Sobrien 81698186Sgordon echo 1>&2 "$0: unknown directive '$rc_arg'." 817150796Syar rc_usage $_keywords 818153152Syar # not reached 81978344Sobrien} 82078344Sobrien 82178344Sobrien# 82278344Sobrien# run_rc_script file arg 82378344Sobrien# Start the script `file' with `arg', and correctly handle the 82478344Sobrien# return value from the script. If `file' ends with `.sh', it's 82598186Sgordon# sourced into the current environment. If `file' appears to be 82698186Sgordon# a backup or scratch file, ignore it. Otherwise if it's 82798186Sgordon# executable run as a child process. 82878344Sobrien# 82978344Sobrienrun_rc_script() 83078344Sobrien{ 83178344Sobrien _file=$1 83278344Sobrien _arg=$2 83378344Sobrien if [ -z "$_file" -o -z "$_arg" ]; then 83478344Sobrien err 3 'USAGE: run_rc_script file arg' 83578344Sobrien fi 83678344Sobrien 83798186Sgordon unset name command command_args command_interpreter \ 83898186Sgordon extra_commands pidfile procname \ 83998186Sgordon rcvar required_dirs required_files required_vars 84098186Sgordon eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 84198186Sgordon 84278344Sobrien case "$_file" in 843153105Sdougb /etc/rc.d/*.sh) # run in current shell 844146490Sschweikh set $_arg; . $_file 84578344Sobrien ;; 846153105Sdougb *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip 84798186Sgordon warn "Ignoring scratch file $_file" 84898186Sgordon ;; 84978344Sobrien *) # run in subshell 85098186Sgordon if [ -x $_file ]; then 85198186Sgordon if [ -n "$rc_fast_and_loose" ]; then 852146490Sschweikh set $_arg; . $_file 85398186Sgordon else 854130161Smtm ( trap "echo Script $_file interrupted; kill -QUIT $$" 3 855130161Smtm trap "echo Script $_file interrupted; exit 1" 2 856146490Sschweikh set $_arg; . $_file ) 85798186Sgordon fi 85898186Sgordon fi 85978344Sobrien ;; 86078344Sobrien esac 86178344Sobrien} 86278344Sobrien 86378344Sobrien# 864157653Sflz# load_rc_config name 865157653Sflz# Source in the configuration file for a given name. 86678344Sobrien# 86778344Sobrienload_rc_config() 86878344Sobrien{ 869157653Sflz local _tmp 870157653Sflz 871157653Sflz _name=$1 872157653Sflz if [ -z "$_name" ]; then 873157653Sflz err 3 'USAGE: load_rc_config name' 87478344Sobrien fi 87578344Sobrien 876126285Smtm if ${_rc_conf_loaded:-false}; then 877126285Smtm : 878126285Smtm else 87998186Sgordon if [ -r /etc/defaults/rc.conf ]; then 88098186Sgordon debug "Sourcing /etc/defaults/rc.conf" 88198186Sgordon . /etc/defaults/rc.conf 88298186Sgordon source_rc_confs 88398186Sgordon elif [ -r /etc/rc.conf ]; then 88498186Sgordon debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." 88598186Sgordon . /etc/rc.conf 88698186Sgordon fi 887126285Smtm _rc_conf_loaded=true 88898186Sgordon fi 889157653Sflz 890157653Sflz eval _override_command=\$${name}_program 891157653Sflz command=${command:+${_override_command:-$command}} 892157653Sflz 893157653Sflz if [ -z "${command}" ]; then 894157653Sflz _tmp=`/bin/realpath $0` 895157653Sflz prefix=${_tmp%/etc/rc.d/*}/ 896157653Sflz else 897157653Sflz prefix=${command%/*bin/*}/ 89878344Sobrien fi 899157653Sflz if [ "${prefix}" = "/" -o "${prefix}" = "/usr/" ] ; then 900157653Sflz etcdir="/etc" 901157653Sflz else 902157653Sflz etcdir="${prefix}etc" 903157653Sflz fi 904101850Sgordon 905157653Sflz # XXX - Deprecated 906157653Sflz if [ -f /etc/rc.conf.d/${_name} -a ${etcdir} != "/etc" ]; then 907157653Sflz debug "Sourcing /etc/rc.conf.d/${_name}" 908157653Sflz warn "Warning: /etc/rc.conf.d/${_name} is deprecated, please use ${etcdir}/rc.conf.d/${_name} instead." 909157656Sflz if [ -f ${etcdir}/rc.conf.d/${_name} ]; then 910157653Sflz warn "Warning: Both /etc/rc.conf.d/${_name} and ${etcdir}/rc.conf.d/${_name} exist." 911157653Sflz fi 912157653Sflz . /etc/rc.conf.d/${_name} 913157653Sflz fi 914157653Sflz 915157653Sflz if [ -f ${etcdir}/rc.conf.d/${_name} ]; then 916157653Sflz debug "Sourcing ${etcdir}/rc.conf.d/${_name}" 917157653Sflz . ${etcdir}/rc.conf.d/${_name} 918157653Sflz fi 919157653Sflz 920101850Sgordon # XXX - Deprecated variable name support 921101850Sgordon # 922103018Sgordon case ${OSTYPE} in 923101850Sgordon FreeBSD) 924146490Sschweikh [ -n "$portmap_enable" ] && rpcbind_enable="$portmap_enable" 925146490Sschweikh [ -n "$portmap_program" ] && rpcbind_program="$portmap_program" 926146490Sschweikh [ -n "$portmap_flags" ] && rpcbind_flags="$portmap_flags" 927146490Sschweikh [ -n "$single_mountd_enable" ] && mountd_enable="$single_mountd_enable" 928146490Sschweikh [ -n "$xntpd_enable" ] && ntpd_enable="$xntpd_enable" 929146490Sschweikh [ -n "$xntpd_program" ] && ntpd_program="$xntpd_program" 930146490Sschweikh [ -n "$xntpd_flags" ] && ntpd_flags="$xntpd_flags" 931115950Smtm [ -n "$dhcp_program" ] && dhclient_program="$dhcp_program" 932115950Smtm [ -n "$dhcp_flags" ] && dhclient_flags="$dhcp_flags" 933146490Sschweikh ;; 934101850Sgordon esac 93578344Sobrien} 936157473Sflz 937157473Sflz# 938157653Sflz# load_rc_config_var name var 939157653Sflz# Read the rc.conf(5) var for name and set in the 940157473Sflz# current shell, using load_rc_config in a subshell to prevent 941157473Sflz# unwanted side effects from other variable assignments. 942157473Sflz# 943157473Sflzload_rc_config_var() 944157473Sflz{ 945157473Sflz if [ $# -ne 2 ]; then 946157653Sflz err 3 'USAGE: load_rc_config_var name var' 947157473Sflz fi 948157473Sflz eval $(eval '( 949157473Sflz load_rc_config '$1' >/dev/null; 950157473Sflz if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 951157473Sflz echo '$2'=\'\''${'$2'}\'\''; 952157473Sflz fi 953157473Sflz )' ) 954157473Sflz} 95578344Sobrien 95678344Sobrien# 95778344Sobrien# rc_usage commands 95878344Sobrien# Print a usage string for $0, with `commands' being a list of 95978344Sobrien# valid commands. 96078344Sobrien# 96178344Sobrienrc_usage() 96278344Sobrien{ 963126303Smtm echo -n 1>&2 "Usage: $0 [fast|force|one](" 96478344Sobrien 96578344Sobrien _sep= 966126286Smtm for _elem; do 96778344Sobrien echo -n 1>&2 "$_sep$_elem" 96878344Sobrien _sep="|" 96978344Sobrien done 97078344Sobrien echo 1>&2 ")" 97178344Sobrien exit 1 97278344Sobrien} 97378344Sobrien 97478344Sobrien# 97578344Sobrien# err exitval message 97678344Sobrien# Display message to stderr and log to the syslog, and exit with exitval. 97778344Sobrien# 97878344Sobrienerr() 97978344Sobrien{ 98078344Sobrien exitval=$1 98178344Sobrien shift 98278344Sobrien 983106643Sgordon if [ -x /usr/bin/logger ]; then 984106643Sgordon logger "$0: ERROR: $*" 985106643Sgordon fi 986106643Sgordon echo 1>&2 "$0: ERROR: $*" 98778344Sobrien exit $exitval 98878344Sobrien} 98978344Sobrien 99078344Sobrien# 99178344Sobrien# warn message 99278344Sobrien# Display message to stderr and log to the syslog. 99378344Sobrien# 99478344Sobrienwarn() 99578344Sobrien{ 996106643Sgordon if [ -x /usr/bin/logger ]; then 997106643Sgordon logger "$0: WARNING: $*" 998106643Sgordon fi 999106643Sgordon echo 1>&2 "$0: WARNING: $*" 100078344Sobrien} 100198186Sgordon 100298186Sgordon# 100398186Sgordon# info message 100498186Sgordon# Display informational message to stdout and log to syslog. 100598186Sgordon# 100698186Sgordoninfo() 100798186Sgordon{ 1008119170Smtm case ${rc_info} in 1009119170Smtm [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1010119170Smtm if [ -x /usr/bin/logger ]; then 1011119170Smtm logger "$0: INFO: $*" 1012119170Smtm fi 1013119170Smtm echo "$0: INFO: $*" 1014119170Smtm ;; 1015119170Smtm esac 101698186Sgordon} 101798186Sgordon 101898186Sgordon# 101998186Sgordon# debug message 1020106643Sgordon# If debugging is enabled in rc.conf output message to stderr. 102198186Sgordon# BEWARE that you don't call any subroutine that itself calls this 102298186Sgordon# function. 102398186Sgordon# 102498186Sgordondebug() 102598186Sgordon{ 102698186Sgordon case ${rc_debug} in 102798186Sgordon [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 1028106700Sgordon if [ -x /usr/bin/logger ]; then 1029106700Sgordon logger "$0: INFO: $*" 1030106700Sgordon fi 1031146490Sschweikh echo 1>&2 "$0: DEBUG: $*" 103298186Sgordon ;; 103398186Sgordon esac 103498186Sgordon} 103598186Sgordon 103698186Sgordon# 103798186Sgordon# backup_file action file cur backup 103898186Sgordon# Make a backup copy of `file' into `cur', and save the previous 103998186Sgordon# version of `cur' as `backup' or use rcs for archiving. 104098186Sgordon# 104198186Sgordon# This routine checks the value of the backup_uses_rcs variable, 104298186Sgordon# which can be either YES or NO. 104398186Sgordon# 104498186Sgordon# The `action' keyword can be one of the following: 104598186Sgordon# 104698186Sgordon# add `file' is now being backed up (and is possibly 104798186Sgordon# being reentered into the backups system). `cur' 104898186Sgordon# is created and RCS files, if necessary, are 104998186Sgordon# created as well. 105098186Sgordon# 105198186Sgordon# update `file' has changed and needs to be backed up. 105298186Sgordon# If `cur' exists, it is copied to to `back' or 105398186Sgordon# checked into RCS (if the repository file is old), 105498186Sgordon# and then `file' is copied to `cur'. Another RCS 105598186Sgordon# check in done here if RCS is being used. 105698186Sgordon# 105798186Sgordon# remove `file' is no longer being tracked by the backups 105898186Sgordon# system. If RCS is not being used, `cur' is moved 105998186Sgordon# to `back', otherwise an empty file is checked in, 106098186Sgordon# and then `cur' is removed. 106198186Sgordon# 106298186Sgordon# 106398186Sgordonbackup_file() 106498186Sgordon{ 106598186Sgordon _action=$1 106698186Sgordon _file=$2 106798186Sgordon _cur=$3 106898186Sgordon _back=$4 106998186Sgordon 107098186Sgordon if checkyesno backup_uses_rcs; then 107198186Sgordon _msg0="backup archive" 107298186Sgordon _msg1="update" 107398186Sgordon 107498186Sgordon # ensure that history file is not locked 107598186Sgordon if [ -f $_cur,v ]; then 107698186Sgordon rcs -q -u -U -M $_cur 107798186Sgordon fi 107898186Sgordon 107998186Sgordon # ensure after switching to rcs that the 108098186Sgordon # current backup is not lost 108198186Sgordon if [ -f $_cur ]; then 108298186Sgordon # no archive, or current newer than archive 108398186Sgordon if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 108498186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 108598186Sgordon rcs -q -kb -U $_cur 108698186Sgordon co -q -f -u $_cur 108798186Sgordon fi 108898186Sgordon fi 108998186Sgordon 109098186Sgordon case $_action in 109198186Sgordon add|update) 109298186Sgordon cp -p $_file $_cur 109398186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 109498186Sgordon rcs -q -kb -U $_cur 109598186Sgordon co -q -f -u $_cur 109698186Sgordon chown root:wheel $_cur $_cur,v 109798186Sgordon ;; 109898186Sgordon remove) 109998186Sgordon cp /dev/null $_cur 110098186Sgordon ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 110198186Sgordon rcs -q -kb -U $_cur 110298186Sgordon chown root:wheel $_cur $_cur,v 110398186Sgordon rm $_cur 110498186Sgordon ;; 110598186Sgordon esac 110698186Sgordon else 110798186Sgordon case $_action in 110898186Sgordon add|update) 110998186Sgordon if [ -f $_cur ]; then 111098186Sgordon cp -p $_cur $_back 111198186Sgordon fi 111298186Sgordon cp -p $_file $_cur 111398186Sgordon chown root:wheel $_cur 111498186Sgordon ;; 111598186Sgordon remove) 111698186Sgordon mv -f $_cur $_back 111798186Sgordon ;; 111898186Sgordon esac 111998186Sgordon fi 112098186Sgordon} 1121119166Smtm 1122157473Sflz_rc_subr_loaded=: 1123157473Sflz 1124123344Smtm# make_symlink src link 1125123344Smtm# Make a symbolic link 'link' to src from basedir. If the 1126123344Smtm# directory in which link is to be created does not exist 1127123344Smtm# a warning will be displayed and an error will be returned. 1128123344Smtm# Returns 0 on sucess, 1 otherwise. 1129119166Smtm# 1130123344Smtmmake_symlink() 1131119166Smtm{ 1132123344Smtm local src link linkdir _me 1133123344Smtm src="$1" 1134123344Smtm link="$2" 1135123344Smtm linkdir="`dirname $link`" 1136123344Smtm _me="make_symlink()" 1137119166Smtm 1138123344Smtm if [ -z "$src" -o -z "$link" ]; then 1139123344Smtm warn "$_me: requires two arguments." 1140119166Smtm return 1 1141119166Smtm fi 1142123344Smtm if [ ! -d "$linkdir" ]; then 1143123344Smtm warn "$_me: the directory $linkdir does not exist" 1144119166Smtm return 1 1145119166Smtm fi 1146146490Sschweikh if ! ln -sf $src $link; then 1147123344Smtm warn "$_me: unable to make a symbolic link from $link to $src" 1148119166Smtm return 1 1149119166Smtm fi 1150119166Smtm return 0 1151119166Smtm} 1152119166Smtm 1153119166Smtm# devfs_rulesets_from_file file 1154119166Smtm# Reads a set of devfs commands from file, and creates 1155119166Smtm# the specified rulesets with their rules. Returns non-zero 1156119166Smtm# if there was an error. 1157119166Smtm# 1158119166Smtmdevfs_rulesets_from_file() 1159119166Smtm{ 1160119166Smtm local file _err _me 1161119166Smtm file="$1" 1162119166Smtm _me="devfs_rulesets_from_file" 1163119166Smtm _err=0 1164119166Smtm 1165119166Smtm if [ -z "$file" ]; then 1166119166Smtm warn "$_me: you must specify a file" 1167119166Smtm return 1 1168119166Smtm fi 1169119166Smtm if [ ! -e "$file" ]; then 1170119166Smtm debug "$_me: no such file ($file)" 1171119166Smtm return 0 1172119166Smtm fi 1173119166Smtm debug "reading rulesets from file ($file)" 1174119166Smtm { while read line 1175119166Smtm do 1176119166Smtm case $line in 1177119166Smtm \#*) 1178119166Smtm continue 1179119166Smtm ;; 1180119166Smtm \[*\]*) 1181119166Smtm rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` 1182119166Smtm if [ -z "$rulenum" ]; then 1183119166Smtm warn "$_me: cannot extract rule number ($line)" 1184119166Smtm _err=1 1185119166Smtm break 1186119166Smtm fi 1187119166Smtm rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` 1188119166Smtm if [ -z "$rulename" ]; then 1189119166Smtm warn "$_me: cannot extract rule name ($line)" 1190119166Smtm _err=1 1191119166Smtm break; 1192119166Smtm fi 1193119166Smtm eval $rulename=\$rulenum 1194119166Smtm debug "found ruleset: $rulename=$rulenum" 1195146490Sschweikh if ! /sbin/devfs rule -s $rulenum delset; then 1196119166Smtm _err=1 1197119166Smtm break 1198119166Smtm fi 1199119166Smtm ;; 1200119166Smtm *) 1201119166Smtm rulecmd="${line%%"\#*"}" 1202119166Smtm # evaluate the command incase it includes 1203119166Smtm # other rules 1204119166Smtm if [ -n "$rulecmd" ]; then 1205119166Smtm debug "adding rule ($rulecmd)" 1206119166Smtm if ! eval /sbin/devfs rule -s $rulenum $rulecmd 1207119166Smtm then 1208119166Smtm _err=1 1209119166Smtm break 1210119166Smtm fi 1211119166Smtm fi 1212119166Smtm ;; 1213119166Smtm esac 1214119166Smtm if [ $_err -ne 0 ]; then 1215119166Smtm debug "error in $_me" 1216119166Smtm break 1217119166Smtm fi 1218119166Smtm done } < $file 1219119166Smtm return $_err 1220119166Smtm} 1221119166Smtm 1222119166Smtm# devfs_init_rulesets 1223119166Smtm# Initializes rulesets from configuration files. Returns 1224119166Smtm# non-zero if there was an error. 1225119166Smtm# 1226119166Smtmdevfs_init_rulesets() 1227119166Smtm{ 1228119166Smtm local file _me 1229119166Smtm _me="devfs_init_rulesets" 1230119166Smtm 1231119166Smtm # Go through this only once 1232119166Smtm if [ -n "$devfs_rulesets_init" ]; then 1233119166Smtm debug "$_me: devfs rulesets already initialized" 1234119166Smtm return 1235119166Smtm fi 1236146490Sschweikh for file in $devfs_rulesets; do 1237119166Smtm devfs_rulesets_from_file $file || return 1 1238119166Smtm done 1239119166Smtm devfs_rulesets_init=1 1240119166Smtm debug "$_me: devfs rulesets initialized" 1241119166Smtm return 0 1242119166Smtm} 1243119166Smtm 1244119166Smtm# devfs_set_ruleset ruleset [dir] 1245151619Smaxim# Sets the default ruleset of dir to ruleset. The ruleset argument 1246119166Smtm# must be a ruleset name as specified in devfs.rules(5) file. 1247119166Smtm# Returns non-zero if it could not set it successfully. 1248119166Smtm# 1249119166Smtmdevfs_set_ruleset() 1250119166Smtm{ 1251119166Smtm local devdir rs _me 1252119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1253119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1254119166Smtm _me="devfs_set_ruleset" 1255119166Smtm 1256119166Smtm if [ -z "$rs" ]; then 1257119166Smtm warn "$_me: you must specify a ruleset number" 1258119166Smtm return 1 1259119166Smtm fi 1260119166Smtm debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" 1261146490Sschweikh if ! /sbin/devfs $devdir ruleset $rs; then 1262119166Smtm warn "$_me: unable to set ruleset $rs to ${devdir#-m }" 1263119166Smtm return 1 1264119166Smtm fi 1265119166Smtm return 0 1266119166Smtm} 1267119166Smtm 1268119166Smtm# devfs_apply_ruleset ruleset [dir] 1269119166Smtm# Apply ruleset number $ruleset to the devfs mountpoint $dir. 1270119166Smtm# The ruleset argument must be a ruleset name as specified 1271119166Smtm# in a devfs.rules(5) file. Returns 0 on success or non-zero 1272119166Smtm# if it could not apply the ruleset. 1273119166Smtm# 1274119166Smtmdevfs_apply_ruleset() 1275119166Smtm{ 1276119166Smtm local devdir rs _me 1277119166Smtm [ -n "$1" ] && eval rs=\$$1 || rs= 1278119166Smtm [ -n "$2" ] && devdir="-m "$2"" || devdir= 1279119166Smtm _me="devfs_apply_ruleset" 1280119166Smtm 1281119166Smtm if [ -z "$rs" ]; then 1282119166Smtm warn "$_me: you must specify a ruleset" 1283119166Smtm return 1 1284119166Smtm fi 1285119166Smtm debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" 1286146490Sschweikh if ! /sbin/devfs $devdir rule -s $rs applyset; then 1287119166Smtm warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" 1288119166Smtm return 1 1289119166Smtm fi 1290119166Smtm return 0 1291119166Smtm} 1292119166Smtm 1293119166Smtm# devfs_domount dir [ruleset] 1294119166Smtm# Mount devfs on dir. If ruleset is specified it is set 1295119166Smtm# on the mount-point. It must also be a ruleset name as specified 1296119166Smtm# in a devfs.rules(5) file. Returns 0 on success. 1297119166Smtm# 1298119166Smtmdevfs_domount() 1299119166Smtm{ 1300119166Smtm local devdir rs _me 1301119166Smtm devdir="$1" 1302119166Smtm [ -n "$2" ] && rs=$2 || rs= 1303119166Smtm _me="devfs_domount()" 1304119166Smtm 1305119166Smtm if [ -z "$devdir" ]; then 1306119166Smtm warn "$_me: you must specify a mount-point" 1307119166Smtm return 1 1308119166Smtm fi 1309119166Smtm debug "$_me: mount-point is ($devdir), ruleset is ($rs)" 1310146490Sschweikh if ! mount -t devfs dev "$devdir"; then 1311119166Smtm warn "$_me: Unable to mount devfs on $devdir" 1312119166Smtm return 1 1313119166Smtm fi 1314119166Smtm if [ -n "$rs" ]; then 1315119166Smtm devfs_init_rulesets 1316119166Smtm devfs_set_ruleset $rs $devdir 1317124797Scperciva devfs -m $devdir rule applyset 1318119166Smtm fi 1319119166Smtm return 0 1320119166Smtm} 1321119166Smtm 1322119166Smtm# devfs_mount_jail dir [ruleset] 1323119166Smtm# Mounts a devfs file system appropriate for jails 1324119166Smtm# on the directory dir. If ruleset is specified, the ruleset 1325119166Smtm# it names will be used instead. If present, ruleset must 1326119166Smtm# be the name of a ruleset as defined in a devfs.rules(5) file. 1327119166Smtm# This function returns non-zero if an error occurs. 1328119166Smtm# 1329119166Smtmdevfs_mount_jail() 1330119166Smtm{ 1331119166Smtm local jdev rs _me 1332119166Smtm jdev="$1" 1333119166Smtm [ -n "$2" ] && rs=$2 || rs="devfsrules_jail" 1334119166Smtm _me="devfs_mount_jail" 1335119166Smtm 1336119166Smtm devfs_init_rulesets 1337146490Sschweikh if ! devfs_domount "$jdev" $rs; then 1338119166Smtm warn "$_me: devfs was not mounted on $jdev" 1339119166Smtm return 1 1340119166Smtm fi 1341119166Smtm return 0 1342119166Smtm} 1343127345Sbrooks 1344127345Sbrooks# Provide a function for normalizing the mounting of memory 1345127345Sbrooks# filesystems. This should allow the rest of the code here to remain 1346127345Sbrooks# as close as possible between 5-current and 4-stable. 1347127345Sbrooks# $1 = size 1348127345Sbrooks# $2 = mount point 1349137451Skeramida# $3 = (optional) extra mdmfs flags 1350146490Sschweikhmount_md() 1351146490Sschweikh{ 1352127345Sbrooks if [ -n "$3" ]; then 1353137451Skeramida flags="$3" 1354127345Sbrooks fi 1355149421Syar /sbin/mdmfs $flags -s $1 md $2 1356127345Sbrooks} 1357131550Scperciva 1358149049Spjd# ltr str src dst 1359149049Spjd# Change every $src in $str to $dst. 1360149049Spjd# Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor 1361149049Spjd# awk(1). 1362149049Spjdltr() 1363149049Spjd{ 1364149049Spjd local _str _src _dst _out _com 1365149049Spjd _str=$1 1366149049Spjd _src=$2 1367149049Spjd _dst=$3 1368149049Spjd _out="" 1369149049Spjd 1370149049Spjd IFS=${_src} 1371149049Spjd for _com in ${_str}; do 1372149049Spjd if [ -z "${_out}" ]; then 1373149049Spjd _out="${_com}" 1374149049Spjd else 1375149049Spjd _out="${_out}${_dst}${_com}" 1376149049Spjd fi 1377149049Spjd done 1378149049Spjd echo "${_out}" 1379149049Spjd} 1380149049Spjd 1381149050Spjd# Creates a list of providers for GELI encryption. 1382149050Spjdgeli_make_list() 1383149050Spjd{ 1384149050Spjd local devices devices2 1385149050Spjd local provider mountpoint type options rest 1386149050Spjd 1387149050Spjd # Create list of GELI providers from fstab. 1388149050Spjd while read provider mountpoint type options rest ; do 1389155570Sflz case ":${options}" in 1390155570Sflz :*noauto*) 1391155570Sflz noauto=yes 1392155570Sflz ;; 1393155570Sflz *) 1394155570Sflz noauto=no 1395155570Sflz ;; 1396155570Sflz esac 1397155570Sflz 1398149050Spjd case ":${provider}" in 1399149050Spjd :#*) 1400149050Spjd continue 1401149050Spjd ;; 1402149050Spjd *.eli) 1403149050Spjd # Skip swap devices. 1404155570Sflz if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then 1405149050Spjd continue 1406149050Spjd fi 1407149050Spjd devices="${devices} ${provider}" 1408149050Spjd ;; 1409149050Spjd esac 1410149050Spjd done < /etc/fstab 1411149050Spjd 1412149050Spjd # Append providers from geli_devices. 1413149050Spjd devices="${devices} ${geli_devices}" 1414149050Spjd 1415149050Spjd for provider in ${devices}; do 1416149050Spjd provider=${provider%.eli} 1417149050Spjd provider=${provider#/dev/} 1418149050Spjd devices2="${devices2} ${provider}" 1419149050Spjd done 1420149050Spjd 1421149050Spjd echo ${devices2} 1422149050Spjd} 1423149050Spjd 1424153027Sdougb# Find scripts in local_startup directories that use the old syntax 1425153027Sdougb# 1426153027Sdougbfind_local_scripts_old () { 1427153027Sdougb zlist='' 1428153027Sdougb slist='' 1429153027Sdougb for dir in ${local_startup}; do 1430153027Sdougb if [ -d "${dir}" ]; then 1431153027Sdougb for file in ${dir}/[0-9]*.sh; do 1432153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1433153027Sdougb continue 1434153027Sdougb zlist="$zlist $file" 1435153027Sdougb done 1436153027Sdougb for file in ${dir}/[^0-9]*.sh; do 1437153027Sdougb grep '^# PROVIDE:' $file >/dev/null 2>&1 && 1438153027Sdougb continue 1439153027Sdougb slist="$slist $file" 1440153027Sdougb done 1441153027Sdougb fi 1442153027Sdougb done 1443153027Sdougb} 1444153027Sdougb 1445153027Sdougbfind_local_scripts_new () { 1446153027Sdougb local_rc='' 1447153027Sdougb for dir in ${local_startup}; do 1448153027Sdougb if [ -d "${dir}" ]; then 1449153297Sdougb for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do 1450153027Sdougb case "$file" in 1451153027Sdougb *.sample) ;; 1452153027Sdougb *) if [ -x "$file" ]; then 1453153027Sdougb local_rc="${local_rc} ${file}" 1454153027Sdougb fi 1455153027Sdougb ;; 1456153027Sdougb esac 1457153027Sdougb done 1458153027Sdougb fi 1459153027Sdougb done 1460153027Sdougb} 1461153027Sdougb 1462131550Scpercivafi 1463