rc.subr revision 1.104
11.104Schristos# $NetBSD: rc.subr,v 1.104 2020/04/05 21:03:08 christos Exp $ 21.11Slukem# 31.88Sapb# Copyright (c) 1997-2011 The NetBSD Foundation, Inc. 41.11Slukem# All rights reserved. 51.11Slukem# 61.11Slukem# This code is derived from software contributed to The NetBSD Foundation 71.11Slukem# by Luke Mewburn. 81.11Slukem# 91.11Slukem# Redistribution and use in source and binary forms, with or without 101.11Slukem# modification, are permitted provided that the following conditions 111.11Slukem# are met: 121.11Slukem# 1. Redistributions of source code must retain the above copyright 131.11Slukem# notice, this list of conditions and the following disclaimer. 141.11Slukem# 2. Redistributions in binary form must reproduce the above copyright 151.11Slukem# notice, this list of conditions and the following disclaimer in the 161.11Slukem# documentation and/or other materials provided with the distribution. 171.11Slukem# 181.11Slukem# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 191.11Slukem# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 201.11Slukem# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 211.11Slukem# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 221.11Slukem# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 231.11Slukem# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 241.11Slukem# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 251.11Slukem# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 261.11Slukem# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 271.11Slukem# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 281.11Slukem# POSSIBILITY OF SUCH DAMAGE. 291.11Slukem# 301.11Slukem# rc.subr 311.11Slukem# functions used by various rc scripts 321.11Slukem# 331.11Slukem 341.62Sjmmv: ${rcvar_manpage:='rc.conf(5)'} 351.69Sapb: ${RC_PID:=$$} ; export RC_PID 361.78Sapbnl=' 371.78Sapb' # a literal newline 381.62Sjmmv 391.99Schristos# RC variables to clear on start. 401.99Schristos_env_clear_rc_vars=" 411.99SchristosRC_PID= 421.99Schristos_rc_pid= 431.99Schristos_rc_original_stdout_fd= 441.99Schristos_rc_original_stderr_fd= 451.99Schristos_rc_postprocessor_fd= 461.104Schristos_rc_kill_ntries= 471.99Schristos" 481.99Schristos 491.11Slukem# 501.11Slukem# functions 511.11Slukem# --------- 521.1Scjs 531.5Slukem# 541.11Slukem# checkyesno var 551.95Sroy# Test $1 variable. 561.95Sroy# Return 0 if it's "yes" (et al), 1 if it's "no" (et al), 2 otherwise. 571.5Slukem# 581.95Sroycheckyesnox() 591.11Slukem{ 601.11Slukem eval _value=\$${1} 611.11Slukem case $_value in 621.4Slukem 631.4Slukem # "yes", "true", "on", or "1" 641.4Slukem [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 651.4Slukem return 0 661.3Slukem ;; 671.4Slukem 681.4Slukem # "no", "false", "off", or "0" 691.4Slukem [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 701.4Slukem return 1 711.3Slukem ;; 721.3Slukem *) 731.95Sroy return 2 741.3Slukem ;; 751.3Slukem esac 761.38Slukem} 771.38Slukem 781.65Slukem# 791.95Sroy# checkyesno var 801.95Sroy# Test $1 variable, and warn if not set to YES or NO. 811.95Sroy# Return 0 if it's "yes" (et al), nonzero otherwise. 821.95Sroy# 831.95Sroycheckyesno() 841.95Sroy{ 851.95Sroy local var 861.95Sroy 871.95Sroy checkyesnox $1 881.95Sroy var=$? 891.103Skre case "${var}" in 901.103Skre ( 0 | 1 ) return $var;; 911.103Skre esac 921.95Sroy warn "\$${1} is not set properly - see ${rcvar_manpage}." 931.95Sroy return 1 941.95Sroy} 951.95Sroy 961.95Sroy# 971.78Sapb# yesno_to_truefalse var 981.78Sapb# Convert the value of a variable from any of the values 991.78Sapb# understood by checkyesno() to "true" or "false". 1001.78Sapb# 1011.78Sapbyesno_to_truefalse() 1021.78Sapb{ 1031.78Sapb local var=$1 1041.78Sapb if checkyesno $var; then 1051.78Sapb eval $var=true 1061.78Sapb return 0 1071.78Sapb else 1081.78Sapb eval $var=false 1091.78Sapb return 1 1101.78Sapb fi 1111.78Sapb} 1121.78Sapb 1131.78Sapb# 1141.38Slukem# reverse_list list 1151.38Slukem# print the list in reverse order 1161.38Slukem# 1171.38Slukemreverse_list() 1181.38Slukem{ 1191.38Slukem _revlist= 1201.56Schristos for _revfile; do 1211.38Slukem _revlist="$_revfile $_revlist" 1221.38Slukem done 1231.38Slukem echo $_revlist 1241.6Smellon} 1251.6Smellon 1261.6Smellon# 1271.69Sapb# If booting directly to multiuser, send SIGTERM to 1281.69Sapb# the parent (/etc/rc) to abort the boot. 1291.69Sapb# Otherwise just exit. 1301.69Sapb# 1311.69Sapbstop_boot() 1321.69Sapb{ 1331.69Sapb if [ "$autoboot" = yes ]; then 1341.69Sapb echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 1351.69Sapb kill -TERM ${RC_PID} 1361.69Sapb fi 1371.69Sapb exit 1 1381.69Sapb} 1391.69Sapb 1401.69Sapb# 1411.47Slukem# mount_critical_filesystems type 1421.93Swiz# Go through the list of critical file systems as provided in 1431.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 1441.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 1451.79Sapb# It's not an error if file systems prefixed with "OPTIONAL:" 1461.79Sapb# are not mentioned in /etc/fstab. 1471.6Smellon# 1481.11Slukemmount_critical_filesystems() 1491.11Slukem{ 1501.47Slukem eval _fslist=\$critical_filesystems_${1} 1511.79Sapb _mountcrit_es=0 1521.17Slukem for _fs in $_fslist; do 1531.79Sapb _optional=false 1541.79Sapb case "$_fs" in 1551.79Sapb OPTIONAL:*) 1561.79Sapb _optional=true 1571.79Sapb _fs="${_fs#*:}" 1581.79Sapb ;; 1591.79Sapb esac 1601.79Sapb _ismounted=false 1611.79Sapb # look for a line like "${fs} on * type *" 1621.79Sapb # or "* on ${fs} type *" in the output from mount. 1631.79Sapb case "${nl}$( mount )${nl}" in 1641.79Sapb *" on ${_fs} type "*) 1651.79Sapb _ismounted=true 1661.79Sapb ;; 1671.79Sapb *"${nl}${_fs} on "*) 1681.79Sapb _ismounted=true 1691.79Sapb ;; 1701.79Sapb esac 1711.79Sapb if $_ismounted; then 1721.79Sapb print_rc_metadata \ 1731.79Sapb "note:File system ${_fs} was already mounted" 1741.79Sapb else 1751.79Sapb _mount_output=$( mount $_fs 2>&1 ) 1761.79Sapb _mount_es=$? 1771.79Sapb case "$_mount_output" in 1781.79Sapb *"${nl}"*) 1791.79Sapb # multiple lines can't be good, 1801.79Sapb # not even if $_optional is true 1811.79Sapb ;; 1821.89Sapb *[uU]'nknown special file or file system'*) 1831.79Sapb if $_optional; then 1841.79Sapb # ignore this error 1851.79Sapb print_rc_metadata \ 1861.79Sapb "note:Optional file system ${_fs} is not present" 1871.79Sapb _mount_es=0 1881.79Sapb _mount_output="" 1891.6Smellon fi 1901.79Sapb ;; 1911.79Sapb esac 1921.79Sapb if [ -n "$_mount_output" ]; then 1931.79Sapb printf >&2 "%s\n" "$_mount_output" 1941.79Sapb fi 1951.79Sapb if [ "$_mount_es" != 0 ]; then 1961.79Sapb _mountcrit_es="$_mount_es" 1971.6Smellon fi 1981.79Sapb fi 1991.6Smellon done 2001.79Sapb return $_mountcrit_es 2011.7Scjs} 2021.7Scjs 2031.11Slukem# 2041.45Slukem# check_pidfile pidfile procname [interpreter] 2051.45Slukem# Parses the first line of pidfile for a PID, and ensures 2061.11Slukem# that the process is running and matches procname. 2071.45Slukem# Prints the matching PID upon success, nothing otherwise. 2081.45Slukem# interpreter is optional; see _find_processes() for details. 2091.11Slukem# 2101.11Slukemcheck_pidfile() 2111.11Slukem{ 2121.11Slukem _pidfile=$1 2131.11Slukem _procname=$2 2141.45Slukem _interpreter=$3 2151.103Skre if [ -z "$_pidfile" ] || [ -z "$_procname" ]; then 2161.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 2171.11Slukem fi 2181.11Slukem if [ ! -f $_pidfile ]; then 2191.11Slukem return 2201.11Slukem fi 2211.11Slukem read _pid _junk < $_pidfile 2221.11Slukem if [ -z "$_pid" ]; then 2231.11Slukem return 2241.11Slukem fi 2251.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 2261.11Slukem} 2271.11Slukem 2281.11Slukem# 2291.45Slukem# check_process procname [interpreter] 2301.11Slukem# Ensures that a process (or processes) named procname is running. 2311.45Slukem# Prints a list of matching PIDs. 2321.45Slukem# interpreter is optional; see _find_processes() for details. 2331.11Slukem# 2341.11Slukemcheck_process() 2351.11Slukem{ 2361.11Slukem _procname=$1 2371.45Slukem _interpreter=$2 2381.11Slukem if [ -z "$_procname" ]; then 2391.45Slukem err 3 'USAGE: check_process procname [interpreter]' 2401.45Slukem fi 2411.101Skre _find_processes $_procname ${_interpreter:-.} '-A' 2421.45Slukem} 2431.45Slukem 2441.46Slukem# 2451.45Slukem# _find_processes procname interpreter psargs 2461.45Slukem# Search for procname in the output of ps generated by psargs. 2471.45Slukem# Prints the PIDs of any matching processes, space separated. 2481.45Slukem# 2491.45Slukem# If interpreter == ".", check the following variations of procname 2501.45Slukem# against the first word of each command: 2511.45Slukem# procname 2521.45Slukem# `basename procname` 2531.45Slukem# `basename procname` + ":" 2541.45Slukem# "(" + `basename procname` + ")" 2551.45Slukem# 2561.45Slukem# If interpreter != ".", read the first line of procname, remove the 2571.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 2581.45Slukem# match that against each command, either as is, or with extra words 2591.73Sdholland# at the end. As an alternative, to deal with interpreted daemons 2601.66She# using perl, the basename of the interpreter plus a colon is also 2611.66She# tried as the prefix to procname. 2621.45Slukem# 2631.45Slukem_find_processes() 2641.45Slukem{ 2651.45Slukem if [ $# -ne 3 ]; then 2661.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 2671.11Slukem fi 2681.45Slukem _procname=$1 2691.45Slukem _interpreter=$2 2701.45Slukem _psargs=$3 2711.45Slukem 2721.11Slukem _pref= 2731.68Shubertf _procnamebn=${_procname##*/} 2741.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 2751.67Selad read _interp < ${_chroot:-}/$_procname # read interpreter name 2761.45Slukem _interp=${_interp#\#!} # strip #! 2771.45Slukem set -- $_interp 2781.87Schristos if [ $1 = "/usr/bin/env" ]; then 2791.87Schristos shift 2801.87Schristos set -- $(type $1) 2811.87Schristos shift $(($# - 1)) 2821.87Schristos _interp="${1##*/} $_procname" 2831.87Schristos else 2841.87Schristos _interp="$* $_procname" 2851.87Schristos fi 2861.45Slukem if [ $_interpreter != $1 ]; then 2871.45Slukem warn "\$command_interpreter $_interpreter != $1" 2881.45Slukem fi 2891.66She _interpbn=${1##*/} 2901.45Slukem _fp_args='_argv' 2911.45Slukem _fp_match='case "$_argv" in 2921.68Shubertf ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)' 2931.45Slukem else # a normal daemon 2941.45Slukem _fp_args='_arg0 _argv' 2951.45Slukem _fp_match='case "$_arg0" in 2961.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 2971.45Slukem fi 2981.45Slukem 2991.45Slukem _proccheck=' 3001.102Schristos ps -o "pid,args" '"$_psargs"' 2>&1 | 3011.45Slukem while read _npid '"$_fp_args"'; do 3021.45Slukem case "$_npid" in 3031.102Schristos ps:|PID) 3041.45Slukem continue ;; 3051.45Slukem esac ; '"$_fp_match"' 3061.45Slukem echo -n "$_pref$_npid" ; 3071.45Slukem _pref=" " 3081.45Slukem ;; 3091.45Slukem esac 3101.45Slukem done' 3111.45Slukem 3121.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 3131.45Slukem eval $_proccheck 3141.11Slukem} 3151.11Slukem 3161.11Slukem# 3171.104Schristos# kill_pids signal pid [pid ...] 3181.104Schristos# kills the given pids with signal. 3191.104Schristos# returns the list of pids killed successfully. 3201.104Schristos# 3211.104Schristoskill_pids() 3221.104Schristos{ 3231.104Schristos local signal=$1 3241.104Schristos shift 3251.104Schristos local list="$@" 3261.104Schristos local j= 3271.104Schristos local nlist= 3281.104Schristos for j in $list; do 3291.104Schristos if kill -$signal $j 2>/dev/null; then 3301.104Schristos nlist="${nlist}${nlist:+ }$j" 3311.104Schristos fi 3321.104Schristos done 3331.104Schristos echo $nlist 3341.104Schristos} 3351.104Schristos 3361.104Schristos# 3371.33Slukem# wait_for_pids pid [pid ...] 3381.35Slukem# spins until none of the pids exist 3391.104Schristos# if _rc_kill_ntries is set and exceeded, it SIGKILLS the remaining 3401.104Schristos# pids 3411.33Slukem# 3421.33Slukemwait_for_pids() 3431.33Slukem{ 3441.104Schristos local ntries=0 3451.104Schristos local prefix= 3461.104Schristos local list="$@" 3471.104Schristos 3481.104Schristos if [ -z "$list" ]; then 3491.33Slukem return 3501.33Slukem fi 3511.104Schristos 3521.35Slukem while true; do 3531.104Schristos local nlist=$(kill_pids 0 $list) 3541.104Schristos if [ -z "$nlist" ]; then 3551.33Slukem break 3561.33Slukem fi 3571.104Schristos if [ "$list" != "$nlist" ]; then 3581.104Schristos list=$nlist 3591.104Schristos echo -n ${prefix:-"Waiting for PIDS: "}$list 3601.104Schristos prefix=", " 3611.96Sroy fi 3621.96Sroy # We want this to be a tight loop for a fast exit 3631.96Sroy sleep 0.05 3641.104Schristos ntries=$((ntries + 1)) 3651.104Schristos if [ -n "${_rc_kill_ntries}" ]; then 3661.104Schristos if [ ${ntries} -gt ${_rc_kill_ntries} ]; then 3671.104Schristos kill_pids 9 $list > /dev/null 3681.104Schristos fi 3691.104Schristos fi 3701.33Slukem done 3711.104Schristos if [ -n "$prefix" ]; then 3721.35Slukem echo "." 3731.35Slukem fi 3741.33Slukem} 3751.33Slukem 3761.33Slukem# 3771.81Sjmmv# run_rc_command argument [parameters] 3781.46Slukem# Search for argument in the list of supported commands, which is: 3791.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 3801.46Slukem# If there's a match, run ${argument}_cmd or the default method 3811.81Sjmmv# (see below), and pass the optional list of parameters to it. 3821.11Slukem# 3831.46Slukem# If argument has a given prefix, then change the operation as follows: 3841.46Slukem# Prefix Operation 3851.11Slukem# ------ --------- 3861.48Slukem# fast Skip the pid check, and set rc_fast=yes 3871.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 3881.61Slukem# one Set ${rcvar} to YES 3891.11Slukem# 3901.11Slukem# The following globals are used: 3911.24Slukem# 3921.46Slukem# Name Needed Purpose 3931.46Slukem# ---- ------ ------- 3941.11Slukem# name y Name of script. 3951.24Slukem# 3961.11Slukem# command n Full path to command. 3971.46Slukem# Not needed if ${rc_arg}_cmd is set for 3981.11Slukem# each keyword. 3991.24Slukem# 4001.11Slukem# command_args n Optional args/shell directives for command. 4011.24Slukem# 4021.45Slukem# command_interpreter n If not empty, command is interpreted, so 4031.45Slukem# call check_{pidfile,process}() appropriately. 4041.45Slukem# 4051.16Slukem# extra_commands n List of extra commands supported. 4061.24Slukem# 4071.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 4081.42Slukem# otherwise use check_process $command. 4091.42Slukem# In either case, only check if $command is set. 4101.42Slukem# 4111.42Slukem# procname n Process name to check for instead of $command. 4121.24Slukem# 4131.24Slukem# rcvar n This is checked with checkyesno to determine 4141.24Slukem# if the action should be run. 4151.24Slukem# 4161.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 4171.42Slukem# Requires /usr to be mounted. 4181.24Slukem# 4191.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 4201.22Slukem# (if not using ${name}_chroot). 4211.24Slukem# 4221.11Slukem# ${name}_flags n Arguments to call ${command} with. 4231.24Slukem# NOTE: $flags from the parent environment 4241.24Slukem# can be used to override this. 4251.24Slukem# 4261.72She# ${name}_env n Additional environment variable settings 4271.72She# for running ${command} 4281.72She# 4291.23Slukem# ${name}_nice n Nice level to run ${command} at. 4301.24Slukem# 4311.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 4321.22Slukem# using ${name}_chroot. 4331.42Slukem# Requires /usr to be mounted. 4341.24Slukem# 4351.22Slukem# ${name}_group n Group to run chrooted ${command} as. 4361.42Slukem# Requires /usr to be mounted. 4371.24Slukem# 4381.32Slukem# ${name}_groups n Comma separated list of supplementary groups 4391.32Slukem# to run the chrooted ${command} with. 4401.42Slukem# Requires /usr to be mounted. 4411.24Slukem# 4421.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 4431.11Slukem# Otherwise, use default command (see below) 4441.24Slukem# 4451.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 4461.46Slukem# ${rc_arg}_cmd method in the default 4471.46Slukem# operation (i.e, after checking for required 4481.46Slukem# bits and process (non)existence). 4491.11Slukem# If this completes with a non-zero exit code, 4501.46Slukem# don't run ${rc_arg}_cmd. 4511.24Slukem# 4521.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 4531.46Slukem# ${rc_arg}_cmd method, if that method 4541.43Slukem# returned a zero exit code. 4551.43Slukem# 4561.11Slukem# required_dirs n If set, check for the existence of the given 4571.11Slukem# directories before running the default 4581.11Slukem# (re)start command. 4591.24Slukem# 4601.11Slukem# required_files n If set, check for the readability of the given 4611.11Slukem# files before running the default (re)start 4621.11Slukem# command. 4631.24Slukem# 4641.11Slukem# required_vars n If set, perform checkyesno on each of the 4651.11Slukem# listed variables before running the default 4661.11Slukem# (re)start command. 4671.11Slukem# 4681.46Slukem# Default behaviour for a given argument, if no override method is 4691.46Slukem# provided: 4701.24Slukem# 4711.46Slukem# Argument Default behaviour 4721.46Slukem# -------- ----------------- 4731.11Slukem# start if !running && checkyesno ${rcvar} 4741.11Slukem# ${command} 4751.24Slukem# 4761.11Slukem# stop if ${pidfile} 4771.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 4781.11Slukem# else 4791.46Slukem# rc_pid=$(check_process $command) 4801.46Slukem# kill $sig_stop $rc_pid 4811.46Slukem# wait_for_pids $rc_pid 4821.35Slukem# ($sig_stop defaults to TERM.) 4831.24Slukem# 4841.35Slukem# reload Similar to stop, except use $sig_reload instead, 4851.35Slukem# and doesn't wait_for_pids. 4861.11Slukem# $sig_reload defaults to HUP. 4871.24Slukem# 4881.11Slukem# restart Run `stop' then `start'. 4891.11Slukem# 4901.33Slukem# status Show if ${command} is running, etc. 4911.33Slukem# 4921.33Slukem# poll Wait for ${command} to exit. 4931.33Slukem# 4941.33Slukem# rcvar Display what rc.conf variable is used (if any). 4951.33Slukem# 4961.46Slukem# Variables available to methods, and after run_rc_command() has 4971.46Slukem# completed: 4981.46Slukem# 4991.46Slukem# Variable Purpose 5001.46Slukem# -------- ------- 5011.61Slukem# rc_arg Argument to command, after fast/force/one processing 5021.46Slukem# performed 5031.46Slukem# 5041.46Slukem# rc_flags Flags to start the default command with. 5051.46Slukem# Defaults to ${name}_flags, unless overridden 5061.46Slukem# by $flags from the environment. 5071.46Slukem# This variable may be changed by the precmd method. 5081.46Slukem# 5091.46Slukem# rc_pid PID of command (if appropriate) 5101.46Slukem# 5111.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 5121.46Slukem# 5131.46Slukem# rc_force Not empty if "force" was provided (q.v.) 5141.33Slukem# 5151.24Slukem# 5161.11Slukemrun_rc_command() 5171.11Slukem{ 5181.46Slukem rc_arg=$1 5191.24Slukem if [ -z "$name" ]; then 5201.30Slukem err 3 'run_rc_command: $name is not set.' 5211.11Slukem fi 5221.11Slukem 5231.61Slukem _rc_prefix= 5241.46Slukem case "$rc_arg" in 5251.24Slukem fast*) # "fast" prefix; don't check pid 5261.46Slukem rc_arg=${rc_arg#fast} 5271.48Slukem rc_fast=yes 5281.11Slukem ;; 5291.61Slukem force*) # "force" prefix; always run 5301.48Slukem rc_force=yes 5311.61Slukem _rc_prefix=force 5321.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 5331.61Slukem if [ -n "${rcvar}" ]; then 5341.61Slukem eval ${rcvar}=YES 5351.61Slukem fi 5361.61Slukem ;; 5371.61Slukem one*) # "one" prefix; set ${rcvar}=yes 5381.61Slukem _rc_prefix=one 5391.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 5401.29Slukem if [ -n "${rcvar}" ]; then 5411.29Slukem eval ${rcvar}=YES 5421.29Slukem fi 5431.11Slukem ;; 5441.11Slukem esac 5451.11Slukem 5461.75Sreed _keywords="start stop restart rcvar" 5471.75Sreed if [ -n "$extra_commands" ]; then 5481.75Sreed _keywords="${_keywords} ${extra_commands}" 5491.75Sreed fi 5501.46Slukem rc_pid= 5511.11Slukem _pidcmd= 5521.42Slukem _procname=${procname:-${command}} 5531.42Slukem 5541.24Slukem # setup pid check command if not fast 5551.103Skre if [ -z "$rc_fast" ] && [ -n "$_procname" ]; then 5561.11Slukem if [ -n "$pidfile" ]; then 5571.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 5581.42Slukem else 5591.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 5601.11Slukem fi 5611.11Slukem if [ -n "$_pidcmd" ]; then 5621.33Slukem _keywords="${_keywords} status poll" 5631.11Slukem fi 5641.11Slukem fi 5651.11Slukem 5661.46Slukem if [ -z "$rc_arg" ]; then 5671.11Slukem rc_usage "$_keywords" 5681.11Slukem fi 5691.81Sjmmv shift # remove $rc_arg from the positional parameters 5701.11Slukem 5711.17Slukem if [ -n "$flags" ]; then # allow override from environment 5721.46Slukem rc_flags=$flags 5731.11Slukem else 5741.46Slukem eval rc_flags=\$${name}_flags 5751.11Slukem fi 5761.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 5771.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 5781.72She _group=\$${name}_group _groups=\$${name}_groups \ 5791.72She _env=\"\$${name}_env\" 5801.11Slukem 5811.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 5821.42Slukem if [ "$_user" = "$(id -un)" ]; then 5831.42Slukem unset _user 5841.42Slukem fi 5851.42Slukem fi 5861.42Slukem 5871.29Slukem # if ${rcvar} is set, and $1 is not 5881.40Slukem # "rcvar", then run 5891.29Slukem # checkyesno ${rcvar} 5901.74Ssalo # and return if that failed or warn 5911.74Ssalo # user and exit when interactive 5921.24Slukem # 5931.103Skre if [ -n "${rcvar}" ] && [ "$rc_arg" != "rcvar" ]; then 5941.24Slukem if ! checkyesno ${rcvar}; then 5951.74Ssalo # check whether interactive or not 5961.74Ssalo if [ -n "$_run_rc_script" ]; then 5971.74Ssalo return 0 5981.74Ssalo fi 5991.74Ssalo for _elem in $_keywords; do 6001.74Ssalo if [ "$_elem" = "$rc_arg" ]; then 6011.88Sapb cat 1>&2 <<EOF 6021.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}. 6031.88SapbUse the following if you wish to perform the operation: 6041.88Sapb $0 one${rc_arg} 6051.88SapbEOF 6061.74Ssalo exit 1 6071.74Ssalo fi 6081.74Ssalo done 6091.74Ssalo echo 1>&2 "$0: unknown directive '$rc_arg'." 6101.74Ssalo rc_usage "$_keywords" 6111.24Slukem fi 6121.24Slukem fi 6131.24Slukem 6141.24Slukem eval $_pidcmd # determine the pid if necessary 6151.11Slukem 6161.11Slukem for _elem in $_keywords; do 6171.46Slukem if [ "$_elem" != "$rc_arg" ]; then 6181.11Slukem continue 6191.11Slukem fi 6201.11Slukem 6211.24Slukem # if there's a custom ${XXX_cmd}, 6221.24Slukem # run that instead of the default 6231.24Slukem # 6241.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 6251.46Slukem _postcmd=\$${rc_arg}_postcmd 6261.11Slukem if [ -n "$_cmd" ]; then 6271.24Slukem # if the precmd failed and force 6281.24Slukem # isn't set, exit 6291.24Slukem # 6301.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6311.24Slukem return 1 6321.24Slukem fi 6331.24Slukem 6341.81Sjmmv if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then 6351.44Slukem return 1 6361.44Slukem fi 6371.44Slukem eval $_postcmd 6381.11Slukem return 0 6391.11Slukem fi 6401.11Slukem 6411.81Sjmmv if [ ${#} -gt 0 ]; then 6421.81Sjmmv err 1 "the $rc_arg command does not take any parameters" 6431.81Sjmmv fi 6441.81Sjmmv 6451.46Slukem case "$rc_arg" in # default operations... 6461.11Slukem 6471.11Slukem status) 6481.46Slukem if [ -n "$rc_pid" ]; then 6491.46Slukem echo "${name} is running as pid $rc_pid." 6501.11Slukem else 6511.11Slukem echo "${name} is not running." 6521.28Slukem return 1 6531.11Slukem fi 6541.11Slukem ;; 6551.11Slukem 6561.11Slukem start) 6571.46Slukem if [ -n "$rc_pid" ]; then 6581.63Slukem echo 1>&2 "${name} already running? (pid=$rc_pid)." 6591.11Slukem exit 1 6601.11Slukem fi 6611.11Slukem 6621.57Slukem if [ ! -x ${_chroot}${command} ]; then 6631.11Slukem return 0 6641.11Slukem fi 6651.11Slukem 6661.24Slukem # check for required variables, 6671.24Slukem # directories, and files 6681.24Slukem # 6691.11Slukem for _f in $required_vars; do 6701.11Slukem if ! checkyesno $_f; then 6711.65Slukem warn "\$${_f} is not enabled." 6721.46Slukem if [ -z "$rc_force" ]; then 6731.24Slukem return 1 6741.24Slukem fi 6751.11Slukem fi 6761.11Slukem done 6771.11Slukem for _f in $required_dirs; do 6781.11Slukem if [ ! -d "${_f}/." ]; then 6791.25Slukem warn "${_f} is not a directory." 6801.46Slukem if [ -z "$rc_force" ]; then 6811.24Slukem return 1 6821.24Slukem fi 6831.11Slukem fi 6841.11Slukem done 6851.11Slukem for _f in $required_files; do 6861.11Slukem if [ ! -r "${_f}" ]; then 6871.25Slukem warn "${_f} is not readable." 6881.46Slukem if [ -z "$rc_force" ]; then 6891.24Slukem return 1 6901.24Slukem fi 6911.11Slukem fi 6921.11Slukem done 6931.11Slukem 6941.24Slukem # if the precmd failed and force 6951.24Slukem # isn't set, exit 6961.24Slukem # 6971.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6981.24Slukem return 1 6991.24Slukem fi 7001.24Slukem 7011.24Slukem # setup the command to run, and run it 7021.29Slukem # 7031.11Slukem echo "Starting ${name}." 7041.22Slukem if [ -n "$_chroot" ]; then 7051.22Slukem _doit="\ 7061.100Schristos$_env_clear_rc_vars $_env \ 7071.23Slukem${_nice:+nice -n $_nice }\ 7081.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 7091.46Slukem$_chroot $command $rc_flags $command_args" 7101.22Slukem else 7111.22Slukem _doit="\ 7121.18Slukem${_chdir:+cd $_chdir; }\ 7131.100Schristos$_env_clear_rc_vars $_env \ 7141.18Slukem${_nice:+nice -n $_nice }\ 7151.46Slukem$command $rc_flags $command_args" 7161.34Slukem if [ -n "$_user" ]; then 7171.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 7181.34Slukem fi 7191.22Slukem fi 7201.43Slukem 7211.43Slukem # if the cmd failed and force 7221.43Slukem # isn't set, exit 7231.43Slukem # 7241.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 7251.43Slukem return 1 7261.43Slukem fi 7271.43Slukem 7281.43Slukem # finally, run postcmd 7291.43Slukem # 7301.43Slukem eval $_postcmd 7311.11Slukem ;; 7321.11Slukem 7331.11Slukem stop) 7341.46Slukem if [ -z "$rc_pid" ]; then 7351.24Slukem if [ -n "$pidfile" ]; then 7361.63Slukem echo 1>&2 \ 7371.24Slukem "${name} not running? (check $pidfile)." 7381.24Slukem else 7391.63Slukem echo 1>&2 "${name} not running?" 7401.11Slukem fi 7411.24Slukem exit 1 7421.11Slukem fi 7431.11Slukem 7441.43Slukem # if the precmd failed and force 7451.43Slukem # isn't set, exit 7461.43Slukem # 7471.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7481.24Slukem return 1 7491.24Slukem fi 7501.43Slukem 7511.43Slukem # send the signal to stop 7521.43Slukem # 7531.11Slukem echo "Stopping ${name}." 7541.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 7551.34Slukem if [ -n "$_user" ]; then 7561.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 7571.34Slukem fi 7581.43Slukem 7591.43Slukem # if the stop cmd failed and force 7601.43Slukem # isn't set, exit 7611.43Slukem # 7621.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 7631.43Slukem return 1 7641.43Slukem fi 7651.43Slukem 7661.43Slukem # wait for the command to exit, 7671.43Slukem # and run postcmd. 7681.46Slukem wait_for_pids $rc_pid 7691.43Slukem eval $_postcmd 7701.11Slukem ;; 7711.11Slukem 7721.11Slukem reload) 7731.46Slukem if [ -z "$rc_pid" ]; then 7741.24Slukem if [ -n "$pidfile" ]; then 7751.63Slukem echo 1>&2 \ 7761.11Slukem "${name} not running? (check $pidfile)." 7771.24Slukem else 7781.63Slukem echo 1>&2 "${name} not running?" 7791.11Slukem fi 7801.24Slukem exit 1 7811.11Slukem fi 7821.11Slukem echo "Reloading ${name} config files." 7831.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7841.24Slukem return 1 7851.24Slukem fi 7861.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 7871.34Slukem if [ -n "$_user" ]; then 7881.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 7891.34Slukem fi 7901.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 7911.43Slukem return 1 7921.43Slukem fi 7931.43Slukem eval $_postcmd 7941.11Slukem ;; 7951.11Slukem 7961.11Slukem restart) 7971.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7981.24Slukem return 1 7991.11Slukem fi 8001.29Slukem # prevent restart being called more 8011.29Slukem # than once by any given script 8021.29Slukem # 8031.53Slukem if ${_rc_restart_done:-false}; then 8041.29Slukem return 0 8051.29Slukem fi 8061.53Slukem _rc_restart_done=true 8071.33Slukem 8081.61Slukem ( $0 ${_rc_prefix}stop ) 8091.61Slukem $0 ${_rc_prefix}start 8101.11Slukem 8111.43Slukem eval $_postcmd 8121.33Slukem ;; 8131.33Slukem 8141.33Slukem poll) 8151.46Slukem if [ -n "$rc_pid" ]; then 8161.46Slukem wait_for_pids $rc_pid 8171.33Slukem fi 8181.11Slukem ;; 8191.11Slukem 8201.11Slukem rcvar) 8211.11Slukem echo "# $name" 8221.24Slukem if [ -n "$rcvar" ]; then 8231.24Slukem if checkyesno ${rcvar}; then 8241.24Slukem echo "\$${rcvar}=YES" 8251.24Slukem else 8261.24Slukem echo "\$${rcvar}=NO" 8271.24Slukem fi 8281.11Slukem fi 8291.11Slukem ;; 8301.11Slukem 8311.11Slukem *) 8321.11Slukem rc_usage "$_keywords" 8331.11Slukem ;; 8341.11Slukem 8351.11Slukem esac 8361.11Slukem return 0 8371.11Slukem done 8381.11Slukem 8391.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 8401.11Slukem rc_usage "$_keywords" 8411.11Slukem exit 1 8421.11Slukem} 8431.11Slukem 8441.11Slukem# 8451.94Sapb# _have_rc_postprocessor 8461.94Sapb# Test whether the current script is running in a context that 8471.94Sapb# was invoked from /etc/rc with a postprocessor. 8481.94Sapb# 8491.94Sapb# If the test fails, some variables may be unset to make 8501.94Sapb# such tests more efficient in future. 8511.94Sapb# 8521.94Sapb_have_rc_postprocessor() 8531.94Sapb{ 8541.94Sapb # Cheap tests that fd and pid are set, fd is writable. 8551.97Sphx [ -n "${_rc_pid}" ] || { unset _rc_pid; return 1; } 8561.97Sphx [ -n "${_rc_postprocessor_fd}" ] || { unset _rc_pid; return 1; } 8571.97Sphx eval ": >&${_rc_postprocessor_fd}" 2>/dev/null \ 8581.94Sapb || { unset _rc_pid; return 1; } 8591.94Sapb 8601.94Sapb return 0 8611.94Sapb} 8621.94Sapb 8631.94Sapb# 8641.11Slukem# run_rc_script file arg 8651.11Slukem# Start the script `file' with `arg', and correctly handle the 8661.17Slukem# return value from the script. If `file' ends with `.sh', it's 8671.37Slukem# sourced into the current environment. If `file' appears to be 8681.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 8691.37Slukem# executable run as a child process. 8701.17Slukem# 8711.78Sapb# If `file' contains "KEYWORD: interactive" and if we are 8721.94Sapb# running inside /etc/rc with postprocessing, then the script's 8731.94Sapb# stdout and stderr are redirected to $_rc_original_stdout_fd and 8741.78Sapb# $_rc_original_stderr_fd, so the output will be displayed on the 8751.78Sapb# console but not intercepted by /etc/rc's postprocessor. 8761.78Sapb# 8771.11Slukemrun_rc_script() 8781.11Slukem{ 8791.11Slukem _file=$1 8801.11Slukem _arg=$2 8811.103Skre if [ -z "$_file" ] || [ -z "$_arg" ]; then 8821.11Slukem err 3 'USAGE: run_rc_script file arg' 8831.11Slukem fi 8841.11Slukem 8851.74Ssalo _run_rc_script=true 8861.74Ssalo 8871.45Slukem unset name command command_args command_interpreter \ 8881.45Slukem extra_commands pidfile procname \ 8891.42Slukem rcvar required_dirs required_files required_vars 8901.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 8911.39Slukem 8921.78Sapb _must_redirect=false 8931.94Sapb if _have_rc_postprocessor \ 8941.78Sapb && _has_rcorder_keyword interactive $_file 8951.78Sapb then 8961.78Sapb _must_redirect=true 8971.78Sapb fi 8981.78Sapb 8991.39Slukem case "$_file" in 9001.39Slukem *.sh) # run in current shell 9011.78Sapb if $_must_redirect; then 9021.78Sapb print_rc_metadata \ 9031.78Sapb "note:Output from ${_file} is not logged" 9041.80Sapb no_rc_postprocess eval \ 9051.80Sapb 'set $_arg ; . $_file' 9061.78Sapb else 9071.78Sapb set $_arg ; . $_file 9081.78Sapb fi 9091.39Slukem ;; 9101.60Slukem *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 9111.39Slukem warn "Ignoring scratch file $_file" 9121.39Slukem ;; 9131.39Slukem *) # run in subshell 9141.78Sapb if [ -x $_file ] && $_must_redirect; then 9151.78Sapb print_rc_metadata \ 9161.78Sapb "note:Output from ${_file} is not logged" 9171.78Sapb if [ -n "$rc_fast_and_loose" ]; then 9181.80Sapb no_rc_postprocess eval \ 9191.80Sapb 'set $_arg ; . $_file' 9201.78Sapb else 9211.80Sapb no_rc_postprocess eval \ 9221.80Sapb '( set $_arg ; . $_file )' 9231.78Sapb fi 9241.78Sapb elif [ -x $_file ]; then 9251.39Slukem if [ -n "$rc_fast_and_loose" ]; then 9261.39Slukem set $_arg ; . $_file 9271.39Slukem else 9281.37Slukem ( set $_arg ; . $_file ) 9291.37Slukem fi 9301.78Sapb else 9311.78Sapb warn "Ignoring non-executable file $_file" 9321.39Slukem fi 9331.39Slukem ;; 9341.39Slukem esac 9351.11Slukem} 9361.19Slukem 9371.19Slukem# 9381.65Slukem# load_rc_config command 9391.19Slukem# Source in the configuration file for a given command. 9401.19Slukem# 9411.19Slukemload_rc_config() 9421.19Slukem{ 9431.19Slukem _command=$1 9441.19Slukem if [ -z "$_command" ]; then 9451.19Slukem err 3 'USAGE: load_rc_config command' 9461.19Slukem fi 9471.19Slukem 9481.54Slukem if ${_rc_conf_loaded:-false}; then 9491.54Slukem : 9501.54Slukem else 9511.30Slukem . /etc/rc.conf 9521.53Slukem _rc_conf_loaded=true 9531.30Slukem fi 9541.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 9551.20Sfvdl . /etc/rc.conf.d/"$_command" 9561.20Sfvdl fi 9571.19Slukem} 9581.19Slukem 9591.65Slukem# 9601.65Slukem# load_rc_config_var cmd var 9611.65Slukem# Read the rc.conf(5) var for cmd and set in the 9621.65Slukem# current shell, using load_rc_config in a subshell to prevent 9631.65Slukem# unwanted side effects from other variable assignments. 9641.65Slukem# 9651.65Slukemload_rc_config_var() 9661.65Slukem{ 9671.65Slukem if [ $# -ne 2 ]; then 9681.65Slukem err 3 'USAGE: load_rc_config_var cmd var' 9691.65Slukem fi 9701.65Slukem eval $(eval '( 9711.65Slukem load_rc_config '$1' >/dev/null; 9721.103Skre if [ -n "${'$2'}" ] || [ "${'$2'-UNSET}" != "UNSET" ]; then 9731.65Slukem echo '$2'=\'\''${'$2'}\'\''; 9741.65Slukem fi 9751.65Slukem )' ) 9761.65Slukem} 9771.11Slukem 9781.11Slukem# 9791.11Slukem# rc_usage commands 9801.11Slukem# Print a usage string for $0, with `commands' being a list of 9811.11Slukem# valid commands. 9821.11Slukem# 9831.11Slukemrc_usage() 9841.11Slukem{ 9851.61Slukem echo -n 1>&2 "Usage: $0 [fast|force|one](" 9861.11Slukem 9871.11Slukem _sep= 9881.56Schristos for _elem; do 9891.11Slukem echo -n 1>&2 "$_sep$_elem" 9901.11Slukem _sep="|" 9911.11Slukem done 9921.11Slukem echo 1>&2 ")" 9931.11Slukem exit 1 9941.11Slukem} 9951.11Slukem 9961.11Slukem# 9971.11Slukem# err exitval message 9981.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 9991.11Slukem# 10001.11Slukemerr() 10011.11Slukem{ 10021.11Slukem exitval=$1 10031.11Slukem shift 10041.11Slukem 10051.51Sgrant if [ -x /usr/bin/logger ]; then 10061.51Sgrant logger "$0: ERROR: $*" 10071.51Sgrant fi 10081.21Slukem echo 1>&2 "$0: ERROR: $*" 10091.11Slukem exit $exitval 10101.11Slukem} 10111.11Slukem 10121.11Slukem# 10131.11Slukem# warn message 10141.11Slukem# Display message to stderr and log to the syslog. 10151.11Slukem# 10161.11Slukemwarn() 10171.11Slukem{ 10181.51Sgrant if [ -x /usr/bin/logger ]; then 10191.51Sgrant logger "$0: WARNING: $*" 10201.51Sgrant fi 10211.21Slukem echo 1>&2 "$0: WARNING: $*" 10221.31Satatat} 10231.31Satatat 10241.31Satatat# 10251.31Satatat# backup_file action file cur backup 10261.31Satatat# Make a backup copy of `file' into `cur', and save the previous 10271.31Satatat# version of `cur' as `backup' or use rcs for archiving. 10281.31Satatat# 10291.31Satatat# This routine checks the value of the backup_uses_rcs variable, 10301.31Satatat# which can be either YES or NO. 10311.31Satatat# 10321.31Satatat# The `action' keyword can be one of the following: 10331.31Satatat# 10341.31Satatat# add `file' is now being backed up (and is possibly 10351.31Satatat# being reentered into the backups system). `cur' 10361.31Satatat# is created and RCS files, if necessary, are 10371.31Satatat# created as well. 10381.31Satatat# 10391.31Satatat# update `file' has changed and needs to be backed up. 10401.31Satatat# If `cur' exists, it is copied to to `back' or 10411.31Satatat# checked into RCS (if the repository file is old), 10421.31Satatat# and then `file' is copied to `cur'. Another RCS 10431.31Satatat# check in done here if RCS is being used. 10441.31Satatat# 10451.31Satatat# remove `file' is no longer being tracked by the backups 10461.31Satatat# system. If RCS is not being used, `cur' is moved 10471.31Satatat# to `back', otherwise an empty file is checked in, 10481.31Satatat# and then `cur' is removed. 10491.31Satatat# 10501.31Satatat# 10511.31Satatatbackup_file() 10521.31Satatat{ 10531.31Satatat _action=$1 10541.31Satatat _file=$2 10551.31Satatat _cur=$3 10561.31Satatat _back=$4 10571.31Satatat 10581.31Satatat if checkyesno backup_uses_rcs; then 10591.31Satatat _msg0="backup archive" 10601.31Satatat _msg1="update" 10611.31Satatat 10621.36Satatat # ensure that history file is not locked 10631.36Satatat if [ -f $_cur,v ]; then 10641.36Satatat rcs -q -u -U -M $_cur 10651.36Satatat fi 10661.36Satatat 10671.31Satatat # ensure after switching to rcs that the 10681.31Satatat # current backup is not lost 10691.31Satatat if [ -f $_cur ]; then 10701.31Satatat # no archive, or current newer than archive 10711.103Skre if [ ! -f $_cur,v ] || [ $_cur -nt $_cur,v ]; then 10721.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 10731.36Satatat rcs -q -kb -U $_cur 10741.49Slukem co -q -f -u $_cur 10751.31Satatat fi 10761.31Satatat fi 10771.31Satatat 10781.31Satatat case $_action in 10791.31Satatat add|update) 10801.31Satatat cp -p $_file $_cur 10811.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 10821.36Satatat rcs -q -kb -U $_cur 10831.49Slukem co -q -f -u $_cur 10841.31Satatat chown root:wheel $_cur $_cur,v 10851.31Satatat ;; 10861.31Satatat remove) 10871.31Satatat cp /dev/null $_cur 10881.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 10891.36Satatat rcs -q -kb -U $_cur 10901.31Satatat chown root:wheel $_cur $_cur,v 10911.31Satatat rm $_cur 10921.31Satatat ;; 10931.31Satatat esac 10941.31Satatat else 10951.31Satatat case $_action in 10961.31Satatat add|update) 10971.31Satatat if [ -f $_cur ]; then 10981.31Satatat cp -p $_cur $_back 10991.31Satatat fi 11001.31Satatat cp -p $_file $_cur 11011.31Satatat chown root:wheel $_cur 11021.31Satatat ;; 11031.31Satatat remove) 11041.31Satatat mv -f $_cur $_back 11051.31Satatat ;; 11061.31Satatat esac 11071.31Satatat fi 11081.1Scjs} 11091.64Smycroft 11101.76Schristos# 11111.76Schristos# handle_fsck_error fsck_exit_code 11121.76Schristos# Take action depending on the return code from fsck. 11131.76Schristos# 11141.76Schristoshandle_fsck_error() 11151.76Schristos{ 11161.76Schristos case $1 in 11171.76Schristos 0) # OK 11181.76Schristos return 11191.76Schristos ;; 11201.76Schristos 2) # Needs re-run, still fs errors 11211.76Schristos echo "File system still has errors; re-run fsck manually!" 11221.76Schristos ;; 11231.76Schristos 4) # Root modified 11241.93Swiz echo "Root file system was modified, rebooting ..." 11251.76Schristos reboot -n 11261.76Schristos echo "Reboot failed; help!" 11271.76Schristos ;; 11281.76Schristos 8) # Check failed 11291.76Schristos echo "Automatic file system check failed; help!" 11301.76Schristos ;; 11311.76Schristos 12) # Got signal 11321.76Schristos echo "Boot interrupted." 11331.76Schristos ;; 11341.76Schristos *) 11351.76Schristos echo "Unknown error $1; help!" 11361.76Schristos ;; 11371.76Schristos esac 11381.76Schristos stop_boot 11391.76Schristos} 11401.76Schristos 11411.78Sapb# 11421.78Sapb# _has_rcorder_keyword word file 11431.78Sapb# Check whether a file contains a "# KEYWORD:" comment with a 11441.78Sapb# specified keyword in the style used by rcorder(8). 11451.78Sapb# 11461.78Sapb_has_rcorder_keyword() 11471.78Sapb{ 11481.78Sapb local word="$1" 11491.78Sapb local file="$2" 11501.78Sapb local line 11511.78Sapb 11521.78Sapb [ -r "$file" ] || return 1 11531.78Sapb while read line; do 11541.78Sapb case "${line} " in 11551.78Sapb "# KEYWORD:"*[\ \ ]"${word}"[\ \ ]*) 11561.78Sapb return 0 11571.78Sapb ;; 11581.78Sapb "#"*) 11591.78Sapb continue 11601.78Sapb ;; 11611.78Sapb *[A-Za-z0-9]*) 11621.78Sapb # give up at the first non-empty non-comment line 11631.78Sapb return 1 11641.78Sapb ;; 11651.78Sapb esac 11661.78Sapb done <"$file" 11671.78Sapb return 1 11681.78Sapb} 11691.78Sapb 11701.78Sapb# 11711.78Sapb# print_rc_metadata string 11721.78Sapb# Print the specified string in such a way that the post-processor 11731.78Sapb# inside /etc/rc will treat it as meta-data. 11741.78Sapb# 11751.78Sapb# If we are not running inside /etc/rc, do nothing. 11761.78Sapb# 11771.78Sapb# For public use by any rc.d script, the string must begin with 11781.78Sapb# "note:", followed by arbitrary text. The intent is that the text 11791.78Sapb# will appear in a log file but not on the console. 11801.78Sapb# 11811.78Sapb# For private use within /etc/rc, the string must contain a 11821.78Sapb# keyword recognised by the rc_postprocess_metadata() function 11831.78Sapb# defined in /etc/rc, followed by a colon, followed by one or more 11841.78Sapb# colon-separated arguments associated with the keyword. 11851.78Sapb# 11861.78Sapbprint_rc_metadata() 11871.78Sapb{ 11881.78Sapb # _rc_postprocessor fd, if defined, is the fd to which we must 11891.78Sapb # print, prefixing the output with $_rc_metadata_prefix. 11901.78Sapb # 11911.94Sapb if _have_rc_postprocessor; then 11921.88Sapb command printf "%s%s\n" "$rc_metadata_prefix" "$1" \ 11931.78Sapb >&${_rc_postprocessor_fd} 11941.78Sapb fi 11951.78Sapb} 11961.78Sapb 11971.78Sapb# 11981.88Sapb# _flush_rc_output 11991.88Sapb# Arrange for output to be flushed, if we are running 12001.88Sapb# inside /etc/rc with postprocessing. 12011.88Sapb# 12021.88Sapb_flush_rc_output() 12031.88Sapb{ 12041.88Sapb print_rc_metadata "nop" 12051.88Sapb} 12061.88Sapb 12071.88Sapb# 12081.88Sapb# print_rc_normal [-n] string 12091.78Sapb# Print the specified string in such way that it is treated as 12101.78Sapb# normal output, regardless of whether or not we are running 12111.78Sapb# inside /etc/rc with post-processing. 12121.78Sapb# 12131.88Sapb# If "-n" is specified in $1, then the string in $2 is printed 12141.88Sapb# without a newline; otherwise, the string in $1 is printed 12151.88Sapb# with a newline. 12161.88Sapb# 12171.88Sapb# Intended use cases include: 12181.88Sapb# 12191.88Sapb# o An rc.d script can use ``print_rc_normal -n'' to print a 12201.88Sapb# partial line in such a way that it appears immediately 12211.88Sapb# instead of being buffered by rc(8)'s post-processor. 12221.88Sapb# 12231.88Sapb# o An rc.d script that is run via the no_rc_postprocess 12241.88Sapb# function (so most of its output is invisible to rc(8)'s 12251.88Sapb# post-processor) can use print_rc_normal to force some of its 12261.88Sapb# output to be seen by the post-processor. 12271.88Sapb# 12281.78Sapb# 12291.78Sapbprint_rc_normal() 12301.78Sapb{ 12311.94Sapb # print to stdout or _rc_postprocessor_fd, depending on 12321.94Sapb # whether not we have an rc postprocessor. 12331.78Sapb # 12341.94Sapb local fd=1 12351.94Sapb _have_rc_postprocessor && fd="${_rc_postprocessor_fd}" 12361.88Sapb case "$1" in 12371.88Sapb "-n") 12381.88Sapb command printf "%s" "$2" >&${fd} 12391.88Sapb _flush_rc_output 12401.88Sapb ;; 12411.88Sapb *) 12421.88Sapb command printf "%s\n" "$1" >&${fd} 12431.88Sapb ;; 12441.88Sapb esac 12451.78Sapb} 12461.78Sapb 12471.78Sapb# 12481.78Sapb# no_rc_postprocess cmd... 12491.78Sapb# Execute the specified command in such a way that its output 12501.78Sapb# bypasses the post-processor that handles the output from 12511.78Sapb# most commands that are run inside /etc/rc. If we are not 12521.78Sapb# inside /etc/rc, then just execute the command without special 12531.78Sapb# treatment. 12541.78Sapb# 12551.78Sapb# The intent is that interactive commands can be run via 12561.78Sapb# no_rc_postprocess(), and their output will apear immediately 12571.78Sapb# on the console instead of being hidden or delayed by the 12581.78Sapb# post-processor. An unfortunate consequence of the output 12591.78Sapb# bypassing the post-processor is that the output will not be 12601.78Sapb# logged. 12611.78Sapb# 12621.78Sapbno_rc_postprocess() 12631.78Sapb{ 12641.94Sapb if _have_rc_postprocessor; then 12651.78Sapb "$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd} 12661.78Sapb else 12671.78Sapb "$@" 12681.78Sapb fi 12691.78Sapb} 12701.78Sapb 12711.78Sapb# 12721.78Sapb# twiddle 12731.78Sapb# On each call, print a different one of "/", "-", "\\", "|", 12741.78Sapb# followed by a backspace. The most recently printed value is 12751.78Sapb# saved in $_twiddle_state. 12761.78Sapb# 12771.78Sapb# Output is to /dev/tty, so this function may be useful even inside 12781.78Sapb# a script whose output is redirected. 12791.78Sapb# 12801.78Sapbtwiddle() 12811.78Sapb{ 12821.78Sapb case "$_twiddle_state" in 12831.78Sapb '/') _next='-' ;; 12841.78Sapb '-') _next='\' ;; 12851.78Sapb '\') _next='|' ;; 12861.78Sapb *) _next='/' ;; 12871.78Sapb esac 12881.88Sapb command printf "%s\b" "$_next" >/dev/tty 12891.78Sapb _twiddle_state="$_next" 12901.78Sapb} 12911.78Sapb 12921.82Schristos# 12931.82Schristos# human_exit_code 12941.82Schristos# Print the a human version of the exit code. 12951.82Schristos# 12961.82Schristoshuman_exit_code() 12971.82Schristos{ 12981.83Schristos if [ "$1" -lt 127 ] 12991.82Schristos then 13001.82Schristos echo "exited with code $1" 13011.85Schristos elif [ "$(expr $1 % 256)" -eq 127 ] 13021.82Schristos then 13031.84Schristos # This cannot really happen because the shell will not 13041.84Schristos # pass stopped job status out and the exit code is limited 13051.84Schristos # to 8 bits. This code is here just for completeness. 13061.82Schristos echo "stopped with signal $(expr $1 / 256)" 13071.82Schristos else 13081.82Schristos echo "terminated with signal $(expr $1 - 128)" 13091.82Schristos fi 13101.82Schristos} 13111.86Sapb 13121.86Sapb# 13131.86Sapb# collapse_backslash_newline 13141.86Sapb# Copy input to output, collapsing <backslash><newline> 13151.86Sapb# to nothing, but leaving other backslashes alone. 13161.86Sapb# 13171.86Sapbcollapse_backslash_newline() 13181.86Sapb{ 13191.86Sapb local line 13201.86Sapb while read -r line ; do 13211.86Sapb case "$line" in 13221.86Sapb *\\) 13231.86Sapb # print it, without the backslash or newline 13241.88Sapb command printf "%s" "${line%?}" 13251.86Sapb ;; 13261.86Sapb *) 13271.86Sapb # print it, with a newline 13281.88Sapb command printf "%s\n" "${line}" 13291.86Sapb ;; 13301.86Sapb esac 13311.86Sapb done 13321.86Sapb} 13331.82Schristos 13341.92Sapb# Shell implementations of basename and dirname, usable before 13351.92Sapb# the /usr file system is mounted. 13361.92Sapb# 13371.92Sapbbasename() 13381.92Sapb{ 13391.92Sapb local file="$1" 13401.92Sapb local suffix="$2" 13411.92Sapb local base 13421.92Sapb 13431.92Sapb base="${file##*/}" # remove up to and including last '/' 13441.92Sapb base="${base%${suffix}}" # remove suffix, if any 13451.92Sapb command printf "%s\n" "${base}" 13461.92Sapb} 13471.92Sapb 13481.92Sapbdirname() 13491.92Sapb{ 13501.92Sapb local file="$1" 13511.92Sapb local dir 13521.92Sapb 13531.92Sapb case "$file" in 13541.92Sapb /*/*) dir="${file%/*}" ;; # common case: absolute path 13551.92Sapb /*) dir="/" ;; # special case: name in root dir 13561.92Sapb */*) dir="${file%/*}" ;; # common case: relative path with '/' 13571.92Sapb *) dir="." ;; # special case: name without '/' 13581.92Sapb esac 13591.92Sapb command printf "%s\n" "${dir}" 13601.92Sapb} 13611.92Sapb 13621.88Sapb# Override the normal "echo" and "printf" commands, so that 13631.88Sapb# partial lines printed by rc.d scripts appear immediately, 13641.88Sapb# instead of being buffered by rc(8)'s post-processor. 13651.88Sapb# 13661.88Sapb# Naive use of the echo or printf commands from rc.d scripts, 13671.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr, 13681.88Sapb# will call these functions. To call the real echo and printf 13691.88Sapb# commands, use "command echo" or "command printf". 13701.88Sapb# 13711.103Skre# Avoid use of echo altogether as much as possible, printf works better 13721.103Skre# 13731.88Sapbecho() 13741.88Sapb{ 13751.103Skre local IFS=' ' NL='\n' # not a literal newline... 13761.103Skre 13771.88Sapb case "$1" in 13781.103Skre -n) NL=; shift;; 13791.88Sapb esac 13801.103Skre 13811.103Skre command printf "%s${NL}" "$*" 13821.103Skre 13831.103Skre if test -z "${NL}" 13841.103Skre then 13851.103Skre _flush_rc_output 13861.103Skre fi 13871.103Skre return 0 13881.88Sapb} 13891.103Skre 13901.88Sapbprintf() 13911.88Sapb{ 13921.88Sapb command printf "$@" 13931.88Sapb case "$1" in 13941.88Sapb *'\n') : ;; 13951.88Sapb *) _flush_rc_output ;; 13961.88Sapb esac 13971.103Skre return 0 13981.88Sapb} 13991.88Sapb 14001.98Schristoskat() { 14011.98Schristos local i 14021.98Schristos local v 14031.98Schristos for i; do 14041.98Schristos while read -r v; do 14051.98Schristos v="${v%%#*}" 14061.98Schristos if [ -z "$v" ]; then 14071.98Schristos continue 14081.98Schristos fi 14091.98Schristos echo "$v" 14101.98Schristos done < "$i" 14111.98Schristos done 14121.98Schristos} 14131.98Schristos 14141.64Smycroft_rc_subr_loaded=: 1415