rc.subr revision 1.110
11.110Salnsn# $NetBSD: rc.subr,v 1.110 2022/02/06 16:23:12 alnsn 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.107Schristosexport PATH=/sbin:/bin:/usr/sbin:/usr/bin 501.11Slukem# 511.11Slukem# functions 521.11Slukem# --------- 531.1Scjs 541.5Slukem# 551.11Slukem# checkyesno var 561.95Sroy# Test $1 variable. 571.95Sroy# Return 0 if it's "yes" (et al), 1 if it's "no" (et al), 2 otherwise. 581.5Slukem# 591.95Sroycheckyesnox() 601.11Slukem{ 611.11Slukem eval _value=\$${1} 621.11Slukem case $_value in 631.4Slukem 641.4Slukem # "yes", "true", "on", or "1" 651.4Slukem [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 661.4Slukem return 0 671.3Slukem ;; 681.4Slukem 691.4Slukem # "no", "false", "off", or "0" 701.4Slukem [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 711.4Slukem return 1 721.3Slukem ;; 731.3Slukem *) 741.95Sroy return 2 751.3Slukem ;; 761.3Slukem esac 771.38Slukem} 781.38Slukem 791.65Slukem# 801.95Sroy# checkyesno var 811.95Sroy# Test $1 variable, and warn if not set to YES or NO. 821.95Sroy# Return 0 if it's "yes" (et al), nonzero otherwise. 831.95Sroy# 841.95Sroycheckyesno() 851.95Sroy{ 861.95Sroy local var 871.95Sroy 881.95Sroy checkyesnox $1 891.95Sroy var=$? 901.103Skre case "${var}" in 911.103Skre ( 0 | 1 ) return $var;; 921.103Skre esac 931.95Sroy warn "\$${1} is not set properly - see ${rcvar_manpage}." 941.95Sroy return 1 951.95Sroy} 961.95Sroy 971.95Sroy# 981.78Sapb# yesno_to_truefalse var 991.78Sapb# Convert the value of a variable from any of the values 1001.78Sapb# understood by checkyesno() to "true" or "false". 1011.78Sapb# 1021.78Sapbyesno_to_truefalse() 1031.78Sapb{ 1041.78Sapb local var=$1 1051.78Sapb if checkyesno $var; then 1061.78Sapb eval $var=true 1071.78Sapb return 0 1081.78Sapb else 1091.78Sapb eval $var=false 1101.78Sapb return 1 1111.78Sapb fi 1121.78Sapb} 1131.78Sapb 1141.78Sapb# 1151.38Slukem# reverse_list list 1161.38Slukem# print the list in reverse order 1171.38Slukem# 1181.38Slukemreverse_list() 1191.38Slukem{ 1201.38Slukem _revlist= 1211.56Schristos for _revfile; do 1221.38Slukem _revlist="$_revfile $_revlist" 1231.38Slukem done 1241.38Slukem echo $_revlist 1251.6Smellon} 1261.6Smellon 1271.6Smellon# 1281.69Sapb# If booting directly to multiuser, send SIGTERM to 1291.69Sapb# the parent (/etc/rc) to abort the boot. 1301.69Sapb# Otherwise just exit. 1311.69Sapb# 1321.69Sapbstop_boot() 1331.69Sapb{ 1341.69Sapb if [ "$autoboot" = yes ]; then 1351.69Sapb echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 1361.69Sapb kill -TERM ${RC_PID} 1371.69Sapb fi 1381.69Sapb exit 1 1391.69Sapb} 1401.69Sapb 1411.69Sapb# 1421.47Slukem# mount_critical_filesystems type 1431.93Swiz# Go through the list of critical file systems as provided in 1441.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 1451.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 1461.79Sapb# It's not an error if file systems prefixed with "OPTIONAL:" 1471.79Sapb# are not mentioned in /etc/fstab. 1481.6Smellon# 1491.11Slukemmount_critical_filesystems() 1501.11Slukem{ 1511.47Slukem eval _fslist=\$critical_filesystems_${1} 1521.79Sapb _mountcrit_es=0 1531.17Slukem for _fs in $_fslist; do 1541.79Sapb _optional=false 1551.79Sapb case "$_fs" in 1561.79Sapb OPTIONAL:*) 1571.79Sapb _optional=true 1581.79Sapb _fs="${_fs#*:}" 1591.79Sapb ;; 1601.79Sapb esac 1611.79Sapb _ismounted=false 1621.79Sapb # look for a line like "${fs} on * type *" 1631.79Sapb # or "* on ${fs} type *" in the output from mount. 1641.79Sapb case "${nl}$( mount )${nl}" in 1651.79Sapb *" on ${_fs} type "*) 1661.79Sapb _ismounted=true 1671.79Sapb ;; 1681.79Sapb *"${nl}${_fs} on "*) 1691.79Sapb _ismounted=true 1701.79Sapb ;; 1711.79Sapb esac 1721.79Sapb if $_ismounted; then 1731.79Sapb print_rc_metadata \ 1741.79Sapb "note:File system ${_fs} was already mounted" 1751.79Sapb else 1761.79Sapb _mount_output=$( mount $_fs 2>&1 ) 1771.79Sapb _mount_es=$? 1781.79Sapb case "$_mount_output" in 1791.79Sapb *"${nl}"*) 1801.79Sapb # multiple lines can't be good, 1811.79Sapb # not even if $_optional is true 1821.79Sapb ;; 1831.89Sapb *[uU]'nknown special file or file system'*) 1841.79Sapb if $_optional; then 1851.79Sapb # ignore this error 1861.79Sapb print_rc_metadata \ 1871.79Sapb "note:Optional file system ${_fs} is not present" 1881.79Sapb _mount_es=0 1891.79Sapb _mount_output="" 1901.6Smellon fi 1911.79Sapb ;; 1921.79Sapb esac 1931.79Sapb if [ -n "$_mount_output" ]; then 1941.79Sapb printf >&2 "%s\n" "$_mount_output" 1951.79Sapb fi 1961.79Sapb if [ "$_mount_es" != 0 ]; then 1971.79Sapb _mountcrit_es="$_mount_es" 1981.6Smellon fi 1991.79Sapb fi 2001.6Smellon done 2011.79Sapb return $_mountcrit_es 2021.7Scjs} 2031.7Scjs 2041.11Slukem# 2051.108Salnsn# mount_critical_filesystems_zfs 2061.108Salnsn# Go through the list of critical ZFS mountpoints as provided in 2071.108Salnsn# the rc.conf(5) variable $critical_filesystems_zfs, checking 2081.108Salnsn# each one to see if it is mounted, and if it is not, mounting it. 2091.108Salnsn# It's not an error if file systems prefixed with "OPTIONAL:" 2101.110Salnsn# aren't ZFS mountpoints. 2111.108Salnsnmount_critical_filesystems_zfs() 2121.108Salnsn{ 2131.110Salnsn _fslist=$critical_filesystems_zfs 2141.108Salnsn _tab=" " 2151.108Salnsn _mountcrit_es=0 2161.108Salnsn for _fs in $_fslist; do 2171.108Salnsn _optional=false 2181.108Salnsn case "$_fs" in 2191.108Salnsn OPTIONAL:*) 2201.108Salnsn _optional=true 2211.108Salnsn _fs="${_fs#*:}" 2221.108Salnsn ;; 2231.108Salnsn esac 2241.108Salnsn 2251.110Salnsn _dataset=$( 2261.108Salnsn zfs list -H -o mountpoint,name | 2271.108Salnsn while read _line ; do 2281.108Salnsn _dataset='' 2291.108Salnsn case "$_line" in 2301.108Salnsn "${_fs}${_tab}"*) 2311.108Salnsn _dataset="${_line#*${_tab}}" 2321.108Salnsn ;; 2331.108Salnsn esac 2341.108Salnsn if [ -n "$_dataset" ]; then 2351.108Salnsn case "$( zfs get -H -o value canmount $_dataset )" in 2361.108Salnsn on) 2371.108Salnsn echo -n "$_dataset" 2381.108Salnsn break ;; 2391.108Salnsn *) # noauto|off - dataset isn't supposed to be mounted 2401.108Salnsn ;; 2411.108Salnsn esac 2421.108Salnsn fi 2431.110Salnsn done) 2441.108Salnsn 2451.108Salnsn if [ -z "$_dataset" ]; then 2461.108Salnsn if $_optional; then 2471.108Salnsn # ignore this error 2481.108Salnsn print_rc_metadata \ 2491.108Salnsn "note:Optional file system $_fs is not present" 2501.108Salnsn else 2511.108Salnsn printf >&2 "%s\n" "No suitable ZFS dataset found for mountpoint $_fs" 2521.108Salnsn _mountcrit_es=1 2531.108Salnsn fi 2541.108Salnsn else 2551.108Salnsn _mount_es= 2561.108Salnsn case "$( zfs get -H -o value mounted $_dataset )" in 2571.108Salnsn yes) 2581.108Salnsn _mount_es=1 2591.108Salnsn print_rc_metadata \ 2601.108Salnsn "note:File system $_fs was already mounted" 2611.108Salnsn ;; 2621.110Salnsn *) # no 2631.108Salnsn zfs mount "$_dataset" >/dev/null 2641.108Salnsn _mount_es=$? 2651.110Salnsn ;; 2661.110Salnsn esac 2671.108Salnsn 2681.110Salnsn if [ $_mount_es -ne 0 ]; then 2691.108Salnsn _mountcrit_es="$_mount_es" 2701.108Salnsn fi 2711.108Salnsn fi 2721.108Salnsn done 2731.108Salnsn return $_mountcrit_es 2741.108Salnsn} 2751.108Salnsn 2761.108Salnsn# 2771.45Slukem# check_pidfile pidfile procname [interpreter] 2781.45Slukem# Parses the first line of pidfile for a PID, and ensures 2791.11Slukem# that the process is running and matches procname. 2801.45Slukem# Prints the matching PID upon success, nothing otherwise. 2811.45Slukem# interpreter is optional; see _find_processes() for details. 2821.11Slukem# 2831.11Slukemcheck_pidfile() 2841.11Slukem{ 2851.11Slukem _pidfile=$1 2861.11Slukem _procname=$2 2871.45Slukem _interpreter=$3 2881.103Skre if [ -z "$_pidfile" ] || [ -z "$_procname" ]; then 2891.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 2901.11Slukem fi 2911.11Slukem if [ ! -f $_pidfile ]; then 2921.11Slukem return 2931.11Slukem fi 2941.11Slukem read _pid _junk < $_pidfile 2951.11Slukem if [ -z "$_pid" ]; then 2961.11Slukem return 2971.11Slukem fi 2981.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 2991.11Slukem} 3001.11Slukem 3011.11Slukem# 3021.45Slukem# check_process procname [interpreter] 3031.11Slukem# Ensures that a process (or processes) named procname is running. 3041.45Slukem# Prints a list of matching PIDs. 3051.45Slukem# interpreter is optional; see _find_processes() for details. 3061.11Slukem# 3071.11Slukemcheck_process() 3081.11Slukem{ 3091.11Slukem _procname=$1 3101.45Slukem _interpreter=$2 3111.11Slukem if [ -z "$_procname" ]; then 3121.45Slukem err 3 'USAGE: check_process procname [interpreter]' 3131.45Slukem fi 3141.101Skre _find_processes $_procname ${_interpreter:-.} '-A' 3151.45Slukem} 3161.45Slukem 3171.46Slukem# 3181.45Slukem# _find_processes procname interpreter psargs 3191.45Slukem# Search for procname in the output of ps generated by psargs. 3201.45Slukem# Prints the PIDs of any matching processes, space separated. 3211.45Slukem# 3221.45Slukem# If interpreter == ".", check the following variations of procname 3231.45Slukem# against the first word of each command: 3241.45Slukem# procname 3251.45Slukem# `basename procname` 3261.45Slukem# `basename procname` + ":" 3271.45Slukem# "(" + `basename procname` + ")" 3281.45Slukem# 3291.45Slukem# If interpreter != ".", read the first line of procname, remove the 3301.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 3311.45Slukem# match that against each command, either as is, or with extra words 3321.73Sdholland# at the end. As an alternative, to deal with interpreted daemons 3331.66She# using perl, the basename of the interpreter plus a colon is also 3341.66She# tried as the prefix to procname. 3351.45Slukem# 3361.45Slukem_find_processes() 3371.45Slukem{ 3381.45Slukem if [ $# -ne 3 ]; then 3391.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 3401.11Slukem fi 3411.45Slukem _procname=$1 3421.45Slukem _interpreter=$2 3431.45Slukem _psargs=$3 3441.45Slukem 3451.11Slukem _pref= 3461.68Shubertf _procnamebn=${_procname##*/} 3471.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 3481.67Selad read _interp < ${_chroot:-}/$_procname # read interpreter name 3491.45Slukem _interp=${_interp#\#!} # strip #! 3501.45Slukem set -- $_interp 3511.87Schristos if [ $1 = "/usr/bin/env" ]; then 3521.87Schristos shift 3531.87Schristos set -- $(type $1) 3541.87Schristos shift $(($# - 1)) 3551.87Schristos _interp="${1##*/} $_procname" 3561.87Schristos else 3571.87Schristos _interp="$* $_procname" 3581.87Schristos fi 3591.45Slukem if [ $_interpreter != $1 ]; then 3601.45Slukem warn "\$command_interpreter $_interpreter != $1" 3611.45Slukem fi 3621.66She _interpbn=${1##*/} 3631.45Slukem _fp_args='_argv' 3641.45Slukem _fp_match='case "$_argv" in 3651.68Shubertf ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)' 3661.45Slukem else # a normal daemon 3671.45Slukem _fp_args='_arg0 _argv' 3681.45Slukem _fp_match='case "$_arg0" in 3691.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 3701.45Slukem fi 3711.45Slukem 3721.45Slukem _proccheck=' 3731.102Schristos ps -o "pid,args" '"$_psargs"' 2>&1 | 3741.45Slukem while read _npid '"$_fp_args"'; do 3751.45Slukem case "$_npid" in 3761.102Schristos ps:|PID) 3771.45Slukem continue ;; 3781.45Slukem esac ; '"$_fp_match"' 3791.45Slukem echo -n "$_pref$_npid" ; 3801.45Slukem _pref=" " 3811.45Slukem ;; 3821.45Slukem esac 3831.45Slukem done' 3841.45Slukem 3851.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 3861.45Slukem eval $_proccheck 3871.11Slukem} 3881.11Slukem 3891.11Slukem# 3901.104Schristos# kill_pids signal pid [pid ...] 3911.104Schristos# kills the given pids with signal. 3921.104Schristos# returns the list of pids killed successfully. 3931.104Schristos# 3941.104Schristoskill_pids() 3951.104Schristos{ 3961.104Schristos local signal=$1 3971.104Schristos shift 3981.105Sotis local list="$*" 3991.104Schristos local j= 4001.104Schristos local nlist= 4011.104Schristos for j in $list; do 4021.104Schristos if kill -$signal $j 2>/dev/null; then 4031.104Schristos nlist="${nlist}${nlist:+ }$j" 4041.104Schristos fi 4051.104Schristos done 4061.104Schristos echo $nlist 4071.104Schristos} 4081.104Schristos 4091.104Schristos# 4101.33Slukem# wait_for_pids pid [pid ...] 4111.35Slukem# spins until none of the pids exist 4121.104Schristos# if _rc_kill_ntries is set and exceeded, it SIGKILLS the remaining 4131.104Schristos# pids 4141.33Slukem# 4151.33Slukemwait_for_pids() 4161.33Slukem{ 4171.104Schristos local ntries=0 4181.104Schristos local prefix= 4191.105Sotis local nlist= 4201.105Sotis local list="$*" 4211.104Schristos 4221.104Schristos if [ -z "$list" ]; then 4231.33Slukem return 4241.33Slukem fi 4251.104Schristos 4261.35Slukem while true; do 4271.105Sotis nlist=$(kill_pids 0 $list) 4281.104Schristos if [ -z "$nlist" ]; then 4291.33Slukem break 4301.33Slukem fi 4311.104Schristos if [ "$list" != "$nlist" ]; then 4321.104Schristos list=$nlist 4331.104Schristos echo -n ${prefix:-"Waiting for PIDS: "}$list 4341.104Schristos prefix=", " 4351.96Sroy fi 4361.96Sroy # We want this to be a tight loop for a fast exit 4371.96Sroy sleep 0.05 4381.104Schristos ntries=$((ntries + 1)) 4391.104Schristos if [ -n "${_rc_kill_ntries}" ]; then 4401.104Schristos if [ ${ntries} -gt ${_rc_kill_ntries} ]; then 4411.104Schristos kill_pids 9 $list > /dev/null 4421.104Schristos fi 4431.104Schristos fi 4441.33Slukem done 4451.104Schristos if [ -n "$prefix" ]; then 4461.35Slukem echo "." 4471.35Slukem fi 4481.33Slukem} 4491.33Slukem 4501.33Slukem# 4511.81Sjmmv# run_rc_command argument [parameters] 4521.46Slukem# Search for argument in the list of supported commands, which is: 4531.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 4541.46Slukem# If there's a match, run ${argument}_cmd or the default method 4551.81Sjmmv# (see below), and pass the optional list of parameters to it. 4561.11Slukem# 4571.46Slukem# If argument has a given prefix, then change the operation as follows: 4581.46Slukem# Prefix Operation 4591.11Slukem# ------ --------- 4601.48Slukem# fast Skip the pid check, and set rc_fast=yes 4611.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 4621.61Slukem# one Set ${rcvar} to YES 4631.11Slukem# 4641.11Slukem# The following globals are used: 4651.24Slukem# 4661.46Slukem# Name Needed Purpose 4671.46Slukem# ---- ------ ------- 4681.11Slukem# name y Name of script. 4691.24Slukem# 4701.11Slukem# command n Full path to command. 4711.46Slukem# Not needed if ${rc_arg}_cmd is set for 4721.11Slukem# each keyword. 4731.24Slukem# 4741.11Slukem# command_args n Optional args/shell directives for command. 4751.24Slukem# 4761.45Slukem# command_interpreter n If not empty, command is interpreted, so 4771.45Slukem# call check_{pidfile,process}() appropriately. 4781.45Slukem# 4791.16Slukem# extra_commands n List of extra commands supported. 4801.24Slukem# 4811.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 4821.42Slukem# otherwise use check_process $command. 4831.42Slukem# In either case, only check if $command is set. 4841.42Slukem# 4851.42Slukem# procname n Process name to check for instead of $command. 4861.24Slukem# 4871.24Slukem# rcvar n This is checked with checkyesno to determine 4881.24Slukem# if the action should be run. 4891.24Slukem# 4901.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 4911.42Slukem# Requires /usr to be mounted. 4921.24Slukem# 4931.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 4941.22Slukem# (if not using ${name}_chroot). 4951.24Slukem# 4961.11Slukem# ${name}_flags n Arguments to call ${command} with. 4971.24Slukem# NOTE: $flags from the parent environment 4981.24Slukem# can be used to override this. 4991.24Slukem# 5001.72She# ${name}_env n Additional environment variable settings 5011.72She# for running ${command} 5021.72She# 5031.23Slukem# ${name}_nice n Nice level to run ${command} at. 5041.24Slukem# 5051.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 5061.22Slukem# using ${name}_chroot. 5071.42Slukem# Requires /usr to be mounted. 5081.24Slukem# 5091.22Slukem# ${name}_group n Group to run chrooted ${command} as. 5101.42Slukem# Requires /usr to be mounted. 5111.24Slukem# 5121.32Slukem# ${name}_groups n Comma separated list of supplementary groups 5131.32Slukem# to run the chrooted ${command} with. 5141.42Slukem# Requires /usr to be mounted. 5151.24Slukem# 5161.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 5171.11Slukem# Otherwise, use default command (see below) 5181.24Slukem# 5191.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 5201.46Slukem# ${rc_arg}_cmd method in the default 5211.46Slukem# operation (i.e, after checking for required 5221.46Slukem# bits and process (non)existence). 5231.11Slukem# If this completes with a non-zero exit code, 5241.46Slukem# don't run ${rc_arg}_cmd. 5251.24Slukem# 5261.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 5271.46Slukem# ${rc_arg}_cmd method, if that method 5281.43Slukem# returned a zero exit code. 5291.43Slukem# 5301.11Slukem# required_dirs n If set, check for the existence of the given 5311.11Slukem# directories before running the default 5321.11Slukem# (re)start command. 5331.24Slukem# 5341.11Slukem# required_files n If set, check for the readability of the given 5351.11Slukem# files before running the default (re)start 5361.11Slukem# command. 5371.24Slukem# 5381.11Slukem# required_vars n If set, perform checkyesno on each of the 5391.11Slukem# listed variables before running the default 5401.11Slukem# (re)start command. 5411.11Slukem# 5421.46Slukem# Default behaviour for a given argument, if no override method is 5431.46Slukem# provided: 5441.24Slukem# 5451.46Slukem# Argument Default behaviour 5461.46Slukem# -------- ----------------- 5471.11Slukem# start if !running && checkyesno ${rcvar} 5481.11Slukem# ${command} 5491.24Slukem# 5501.11Slukem# stop if ${pidfile} 5511.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 5521.11Slukem# else 5531.46Slukem# rc_pid=$(check_process $command) 5541.46Slukem# kill $sig_stop $rc_pid 5551.46Slukem# wait_for_pids $rc_pid 5561.35Slukem# ($sig_stop defaults to TERM.) 5571.24Slukem# 5581.35Slukem# reload Similar to stop, except use $sig_reload instead, 5591.35Slukem# and doesn't wait_for_pids. 5601.11Slukem# $sig_reload defaults to HUP. 5611.24Slukem# 5621.11Slukem# restart Run `stop' then `start'. 5631.11Slukem# 5641.33Slukem# status Show if ${command} is running, etc. 5651.33Slukem# 5661.33Slukem# poll Wait for ${command} to exit. 5671.33Slukem# 5681.33Slukem# rcvar Display what rc.conf variable is used (if any). 5691.33Slukem# 5701.46Slukem# Variables available to methods, and after run_rc_command() has 5711.46Slukem# completed: 5721.46Slukem# 5731.46Slukem# Variable Purpose 5741.46Slukem# -------- ------- 5751.61Slukem# rc_arg Argument to command, after fast/force/one processing 5761.46Slukem# performed 5771.46Slukem# 5781.46Slukem# rc_flags Flags to start the default command with. 5791.46Slukem# Defaults to ${name}_flags, unless overridden 5801.46Slukem# by $flags from the environment. 5811.46Slukem# This variable may be changed by the precmd method. 5821.46Slukem# 5831.46Slukem# rc_pid PID of command (if appropriate) 5841.46Slukem# 5851.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 5861.46Slukem# 5871.46Slukem# rc_force Not empty if "force" was provided (q.v.) 5881.33Slukem# 5891.24Slukem# 5901.11Slukemrun_rc_command() 5911.11Slukem{ 5921.46Slukem rc_arg=$1 5931.24Slukem if [ -z "$name" ]; then 5941.30Slukem err 3 'run_rc_command: $name is not set.' 5951.11Slukem fi 5961.11Slukem 5971.61Slukem _rc_prefix= 5981.46Slukem case "$rc_arg" in 5991.24Slukem fast*) # "fast" prefix; don't check pid 6001.46Slukem rc_arg=${rc_arg#fast} 6011.48Slukem rc_fast=yes 6021.11Slukem ;; 6031.61Slukem force*) # "force" prefix; always run 6041.48Slukem rc_force=yes 6051.61Slukem _rc_prefix=force 6061.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 6071.61Slukem if [ -n "${rcvar}" ]; then 6081.61Slukem eval ${rcvar}=YES 6091.61Slukem fi 6101.61Slukem ;; 6111.61Slukem one*) # "one" prefix; set ${rcvar}=yes 6121.61Slukem _rc_prefix=one 6131.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 6141.29Slukem if [ -n "${rcvar}" ]; then 6151.29Slukem eval ${rcvar}=YES 6161.29Slukem fi 6171.11Slukem ;; 6181.11Slukem esac 6191.11Slukem 6201.75Sreed _keywords="start stop restart rcvar" 6211.75Sreed if [ -n "$extra_commands" ]; then 6221.75Sreed _keywords="${_keywords} ${extra_commands}" 6231.75Sreed fi 6241.46Slukem rc_pid= 6251.11Slukem _pidcmd= 6261.42Slukem _procname=${procname:-${command}} 6271.42Slukem 6281.24Slukem # setup pid check command if not fast 6291.103Skre if [ -z "$rc_fast" ] && [ -n "$_procname" ]; then 6301.11Slukem if [ -n "$pidfile" ]; then 6311.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 6321.42Slukem else 6331.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 6341.11Slukem fi 6351.11Slukem if [ -n "$_pidcmd" ]; then 6361.33Slukem _keywords="${_keywords} status poll" 6371.11Slukem fi 6381.11Slukem fi 6391.11Slukem 6401.46Slukem if [ -z "$rc_arg" ]; then 6411.11Slukem rc_usage "$_keywords" 6421.11Slukem fi 6431.81Sjmmv shift # remove $rc_arg from the positional parameters 6441.11Slukem 6451.17Slukem if [ -n "$flags" ]; then # allow override from environment 6461.46Slukem rc_flags=$flags 6471.11Slukem else 6481.46Slukem eval rc_flags=\$${name}_flags 6491.11Slukem fi 6501.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 6511.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 6521.72She _group=\$${name}_group _groups=\$${name}_groups \ 6531.72She _env=\"\$${name}_env\" 6541.11Slukem 6551.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 6561.42Slukem if [ "$_user" = "$(id -un)" ]; then 6571.42Slukem unset _user 6581.42Slukem fi 6591.42Slukem fi 6601.42Slukem 6611.29Slukem # if ${rcvar} is set, and $1 is not 6621.40Slukem # "rcvar", then run 6631.29Slukem # checkyesno ${rcvar} 6641.74Ssalo # and return if that failed or warn 6651.74Ssalo # user and exit when interactive 6661.24Slukem # 6671.103Skre if [ -n "${rcvar}" ] && [ "$rc_arg" != "rcvar" ]; then 6681.24Slukem if ! checkyesno ${rcvar}; then 6691.74Ssalo # check whether interactive or not 6701.74Ssalo if [ -n "$_run_rc_script" ]; then 6711.74Ssalo return 0 6721.74Ssalo fi 6731.74Ssalo for _elem in $_keywords; do 6741.74Ssalo if [ "$_elem" = "$rc_arg" ]; then 6751.88Sapb cat 1>&2 <<EOF 6761.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}. 6771.88SapbUse the following if you wish to perform the operation: 6781.88Sapb $0 one${rc_arg} 6791.88SapbEOF 6801.74Ssalo exit 1 6811.74Ssalo fi 6821.74Ssalo done 6831.74Ssalo echo 1>&2 "$0: unknown directive '$rc_arg'." 6841.74Ssalo rc_usage "$_keywords" 6851.24Slukem fi 6861.24Slukem fi 6871.24Slukem 6881.24Slukem eval $_pidcmd # determine the pid if necessary 6891.11Slukem 6901.11Slukem for _elem in $_keywords; do 6911.46Slukem if [ "$_elem" != "$rc_arg" ]; then 6921.11Slukem continue 6931.11Slukem fi 6941.11Slukem 6951.24Slukem # if there's a custom ${XXX_cmd}, 6961.24Slukem # run that instead of the default 6971.24Slukem # 6981.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 6991.46Slukem _postcmd=\$${rc_arg}_postcmd 7001.11Slukem if [ -n "$_cmd" ]; then 7011.24Slukem # if the precmd failed and force 7021.24Slukem # isn't set, exit 7031.24Slukem # 7041.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7051.24Slukem return 1 7061.24Slukem fi 7071.24Slukem 7081.81Sjmmv if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then 7091.44Slukem return 1 7101.44Slukem fi 7111.44Slukem eval $_postcmd 7121.11Slukem return 0 7131.11Slukem fi 7141.11Slukem 7151.81Sjmmv if [ ${#} -gt 0 ]; then 7161.81Sjmmv err 1 "the $rc_arg command does not take any parameters" 7171.81Sjmmv fi 7181.81Sjmmv 7191.46Slukem case "$rc_arg" in # default operations... 7201.11Slukem 7211.11Slukem status) 7221.46Slukem if [ -n "$rc_pid" ]; then 7231.46Slukem echo "${name} is running as pid $rc_pid." 7241.11Slukem else 7251.11Slukem echo "${name} is not running." 7261.28Slukem return 1 7271.11Slukem fi 7281.11Slukem ;; 7291.11Slukem 7301.11Slukem start) 7311.46Slukem if [ -n "$rc_pid" ]; then 7321.63Slukem echo 1>&2 "${name} already running? (pid=$rc_pid)." 7331.11Slukem exit 1 7341.11Slukem fi 7351.11Slukem 7361.57Slukem if [ ! -x ${_chroot}${command} ]; then 7371.11Slukem return 0 7381.11Slukem fi 7391.11Slukem 7401.24Slukem # check for required variables, 7411.24Slukem # directories, and files 7421.24Slukem # 7431.11Slukem for _f in $required_vars; do 7441.11Slukem if ! checkyesno $_f; then 7451.65Slukem warn "\$${_f} is not enabled." 7461.46Slukem if [ -z "$rc_force" ]; then 7471.24Slukem return 1 7481.24Slukem fi 7491.11Slukem fi 7501.11Slukem done 7511.11Slukem for _f in $required_dirs; do 7521.11Slukem if [ ! -d "${_f}/." ]; then 7531.25Slukem warn "${_f} is not a directory." 7541.46Slukem if [ -z "$rc_force" ]; then 7551.24Slukem return 1 7561.24Slukem fi 7571.11Slukem fi 7581.11Slukem done 7591.11Slukem for _f in $required_files; do 7601.11Slukem if [ ! -r "${_f}" ]; then 7611.25Slukem warn "${_f} is not readable." 7621.46Slukem if [ -z "$rc_force" ]; then 7631.24Slukem return 1 7641.24Slukem fi 7651.11Slukem fi 7661.11Slukem done 7671.11Slukem 7681.24Slukem # if the precmd failed and force 7691.24Slukem # isn't set, exit 7701.24Slukem # 7711.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7721.24Slukem return 1 7731.24Slukem fi 7741.24Slukem 7751.24Slukem # setup the command to run, and run it 7761.29Slukem # 7771.11Slukem echo "Starting ${name}." 7781.22Slukem if [ -n "$_chroot" ]; then 7791.22Slukem _doit="\ 7801.100Schristos$_env_clear_rc_vars $_env \ 7811.23Slukem${_nice:+nice -n $_nice }\ 7821.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 7831.46Slukem$_chroot $command $rc_flags $command_args" 7841.22Slukem else 7851.22Slukem _doit="\ 7861.18Slukem${_chdir:+cd $_chdir; }\ 7871.100Schristos$_env_clear_rc_vars $_env \ 7881.18Slukem${_nice:+nice -n $_nice }\ 7891.46Slukem$command $rc_flags $command_args" 7901.34Slukem if [ -n "$_user" ]; then 7911.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 7921.34Slukem fi 7931.22Slukem fi 7941.43Slukem 7951.43Slukem # if the cmd failed and force 7961.43Slukem # isn't set, exit 7971.43Slukem # 7981.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 7991.43Slukem return 1 8001.43Slukem fi 8011.43Slukem 8021.43Slukem # finally, run postcmd 8031.43Slukem # 8041.43Slukem eval $_postcmd 8051.11Slukem ;; 8061.11Slukem 8071.11Slukem stop) 8081.46Slukem if [ -z "$rc_pid" ]; then 8091.24Slukem if [ -n "$pidfile" ]; then 8101.63Slukem echo 1>&2 \ 8111.24Slukem "${name} not running? (check $pidfile)." 8121.24Slukem else 8131.63Slukem echo 1>&2 "${name} not running?" 8141.11Slukem fi 8151.24Slukem exit 1 8161.11Slukem fi 8171.11Slukem 8181.43Slukem # if the precmd failed and force 8191.43Slukem # isn't set, exit 8201.43Slukem # 8211.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 8221.24Slukem return 1 8231.24Slukem fi 8241.43Slukem 8251.43Slukem # send the signal to stop 8261.43Slukem # 8271.11Slukem echo "Stopping ${name}." 8281.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 8291.34Slukem if [ -n "$_user" ]; then 8301.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 8311.34Slukem fi 8321.43Slukem 8331.43Slukem # if the stop cmd failed and force 8341.43Slukem # isn't set, exit 8351.43Slukem # 8361.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 8371.43Slukem return 1 8381.43Slukem fi 8391.43Slukem 8401.43Slukem # wait for the command to exit, 8411.43Slukem # and run postcmd. 8421.46Slukem wait_for_pids $rc_pid 8431.43Slukem eval $_postcmd 8441.11Slukem ;; 8451.11Slukem 8461.11Slukem reload) 8471.46Slukem if [ -z "$rc_pid" ]; then 8481.24Slukem if [ -n "$pidfile" ]; then 8491.63Slukem echo 1>&2 \ 8501.11Slukem "${name} not running? (check $pidfile)." 8511.24Slukem else 8521.63Slukem echo 1>&2 "${name} not running?" 8531.11Slukem fi 8541.24Slukem exit 1 8551.11Slukem fi 8561.11Slukem echo "Reloading ${name} config files." 8571.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 8581.24Slukem return 1 8591.24Slukem fi 8601.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 8611.34Slukem if [ -n "$_user" ]; then 8621.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 8631.34Slukem fi 8641.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 8651.43Slukem return 1 8661.43Slukem fi 8671.43Slukem eval $_postcmd 8681.11Slukem ;; 8691.11Slukem 8701.11Slukem restart) 8711.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 8721.24Slukem return 1 8731.11Slukem fi 8741.29Slukem # prevent restart being called more 8751.29Slukem # than once by any given script 8761.29Slukem # 8771.53Slukem if ${_rc_restart_done:-false}; then 8781.29Slukem return 0 8791.29Slukem fi 8801.53Slukem _rc_restart_done=true 8811.33Slukem 8821.61Slukem ( $0 ${_rc_prefix}stop ) 8831.61Slukem $0 ${_rc_prefix}start 8841.11Slukem 8851.43Slukem eval $_postcmd 8861.33Slukem ;; 8871.33Slukem 8881.33Slukem poll) 8891.46Slukem if [ -n "$rc_pid" ]; then 8901.46Slukem wait_for_pids $rc_pid 8911.33Slukem fi 8921.11Slukem ;; 8931.11Slukem 8941.11Slukem rcvar) 8951.11Slukem echo "# $name" 8961.24Slukem if [ -n "$rcvar" ]; then 8971.24Slukem if checkyesno ${rcvar}; then 8981.106Suwe echo "${rcvar}=YES" 8991.24Slukem else 9001.106Suwe echo "${rcvar}=NO" 9011.24Slukem fi 9021.11Slukem fi 9031.11Slukem ;; 9041.11Slukem 9051.11Slukem *) 9061.11Slukem rc_usage "$_keywords" 9071.11Slukem ;; 9081.11Slukem 9091.11Slukem esac 9101.11Slukem return 0 9111.11Slukem done 9121.11Slukem 9131.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 9141.11Slukem rc_usage "$_keywords" 9151.11Slukem exit 1 9161.11Slukem} 9171.11Slukem 9181.11Slukem# 9191.94Sapb# _have_rc_postprocessor 9201.94Sapb# Test whether the current script is running in a context that 9211.94Sapb# was invoked from /etc/rc with a postprocessor. 9221.94Sapb# 9231.94Sapb# If the test fails, some variables may be unset to make 9241.94Sapb# such tests more efficient in future. 9251.94Sapb# 9261.94Sapb_have_rc_postprocessor() 9271.94Sapb{ 9281.94Sapb # Cheap tests that fd and pid are set, fd is writable. 9291.97Sphx [ -n "${_rc_pid}" ] || { unset _rc_pid; return 1; } 9301.97Sphx [ -n "${_rc_postprocessor_fd}" ] || { unset _rc_pid; return 1; } 9311.97Sphx eval ": >&${_rc_postprocessor_fd}" 2>/dev/null \ 9321.94Sapb || { unset _rc_pid; return 1; } 9331.94Sapb 9341.94Sapb return 0 9351.94Sapb} 9361.94Sapb 9371.94Sapb# 9381.11Slukem# run_rc_script file arg 9391.11Slukem# Start the script `file' with `arg', and correctly handle the 9401.17Slukem# return value from the script. If `file' ends with `.sh', it's 9411.37Slukem# sourced into the current environment. If `file' appears to be 9421.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 9431.37Slukem# executable run as a child process. 9441.17Slukem# 9451.78Sapb# If `file' contains "KEYWORD: interactive" and if we are 9461.94Sapb# running inside /etc/rc with postprocessing, then the script's 9471.94Sapb# stdout and stderr are redirected to $_rc_original_stdout_fd and 9481.78Sapb# $_rc_original_stderr_fd, so the output will be displayed on the 9491.78Sapb# console but not intercepted by /etc/rc's postprocessor. 9501.78Sapb# 9511.11Slukemrun_rc_script() 9521.11Slukem{ 9531.11Slukem _file=$1 9541.11Slukem _arg=$2 9551.103Skre if [ -z "$_file" ] || [ -z "$_arg" ]; then 9561.11Slukem err 3 'USAGE: run_rc_script file arg' 9571.11Slukem fi 9581.11Slukem 9591.74Ssalo _run_rc_script=true 9601.74Ssalo 9611.45Slukem unset name command command_args command_interpreter \ 9621.45Slukem extra_commands pidfile procname \ 9631.42Slukem rcvar required_dirs required_files required_vars 9641.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 9651.39Slukem 9661.78Sapb _must_redirect=false 9671.94Sapb if _have_rc_postprocessor \ 9681.78Sapb && _has_rcorder_keyword interactive $_file 9691.78Sapb then 9701.78Sapb _must_redirect=true 9711.78Sapb fi 9721.78Sapb 9731.39Slukem case "$_file" in 9741.39Slukem *.sh) # run in current shell 9751.78Sapb if $_must_redirect; then 9761.78Sapb print_rc_metadata \ 9771.78Sapb "note:Output from ${_file} is not logged" 9781.80Sapb no_rc_postprocess eval \ 9791.80Sapb 'set $_arg ; . $_file' 9801.78Sapb else 9811.78Sapb set $_arg ; . $_file 9821.78Sapb fi 9831.39Slukem ;; 9841.60Slukem *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 9851.39Slukem warn "Ignoring scratch file $_file" 9861.39Slukem ;; 9871.39Slukem *) # run in subshell 9881.78Sapb if [ -x $_file ] && $_must_redirect; then 9891.78Sapb print_rc_metadata \ 9901.78Sapb "note:Output from ${_file} is not logged" 9911.78Sapb if [ -n "$rc_fast_and_loose" ]; then 9921.80Sapb no_rc_postprocess eval \ 9931.80Sapb 'set $_arg ; . $_file' 9941.78Sapb else 9951.80Sapb no_rc_postprocess eval \ 9961.80Sapb '( set $_arg ; . $_file )' 9971.78Sapb fi 9981.78Sapb elif [ -x $_file ]; then 9991.39Slukem if [ -n "$rc_fast_and_loose" ]; then 10001.39Slukem set $_arg ; . $_file 10011.39Slukem else 10021.37Slukem ( set $_arg ; . $_file ) 10031.37Slukem fi 10041.78Sapb else 10051.78Sapb warn "Ignoring non-executable file $_file" 10061.39Slukem fi 10071.39Slukem ;; 10081.39Slukem esac 10091.11Slukem} 10101.19Slukem 10111.19Slukem# 10121.65Slukem# load_rc_config command 10131.19Slukem# Source in the configuration file for a given command. 10141.19Slukem# 10151.19Slukemload_rc_config() 10161.19Slukem{ 10171.19Slukem _command=$1 10181.19Slukem if [ -z "$_command" ]; then 10191.19Slukem err 3 'USAGE: load_rc_config command' 10201.19Slukem fi 10211.19Slukem 10221.54Slukem if ${_rc_conf_loaded:-false}; then 10231.54Slukem : 10241.54Slukem else 10251.30Slukem . /etc/rc.conf 10261.53Slukem _rc_conf_loaded=true 10271.30Slukem fi 10281.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 10291.20Sfvdl . /etc/rc.conf.d/"$_command" 10301.20Sfvdl fi 10311.19Slukem} 10321.19Slukem 10331.65Slukem# 10341.65Slukem# load_rc_config_var cmd var 10351.65Slukem# Read the rc.conf(5) var for cmd and set in the 10361.65Slukem# current shell, using load_rc_config in a subshell to prevent 10371.65Slukem# unwanted side effects from other variable assignments. 10381.65Slukem# 10391.65Slukemload_rc_config_var() 10401.65Slukem{ 10411.65Slukem if [ $# -ne 2 ]; then 10421.65Slukem err 3 'USAGE: load_rc_config_var cmd var' 10431.65Slukem fi 10441.65Slukem eval $(eval '( 10451.65Slukem load_rc_config '$1' >/dev/null; 10461.103Skre if [ -n "${'$2'}" ] || [ "${'$2'-UNSET}" != "UNSET" ]; then 10471.65Slukem echo '$2'=\'\''${'$2'}\'\''; 10481.65Slukem fi 10491.65Slukem )' ) 10501.65Slukem} 10511.11Slukem 10521.11Slukem# 10531.11Slukem# rc_usage commands 10541.11Slukem# Print a usage string for $0, with `commands' being a list of 10551.11Slukem# valid commands. 10561.11Slukem# 10571.11Slukemrc_usage() 10581.11Slukem{ 10591.61Slukem echo -n 1>&2 "Usage: $0 [fast|force|one](" 10601.11Slukem 10611.11Slukem _sep= 10621.56Schristos for _elem; do 10631.11Slukem echo -n 1>&2 "$_sep$_elem" 10641.11Slukem _sep="|" 10651.11Slukem done 10661.11Slukem echo 1>&2 ")" 10671.11Slukem exit 1 10681.11Slukem} 10691.11Slukem 10701.11Slukem# 10711.11Slukem# err exitval message 10721.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 10731.11Slukem# 10741.11Slukemerr() 10751.11Slukem{ 10761.11Slukem exitval=$1 10771.11Slukem shift 10781.11Slukem 10791.51Sgrant if [ -x /usr/bin/logger ]; then 10801.51Sgrant logger "$0: ERROR: $*" 10811.51Sgrant fi 10821.21Slukem echo 1>&2 "$0: ERROR: $*" 10831.11Slukem exit $exitval 10841.11Slukem} 10851.11Slukem 10861.11Slukem# 10871.11Slukem# warn message 10881.11Slukem# Display message to stderr and log to the syslog. 10891.11Slukem# 10901.11Slukemwarn() 10911.11Slukem{ 10921.51Sgrant if [ -x /usr/bin/logger ]; then 10931.51Sgrant logger "$0: WARNING: $*" 10941.51Sgrant fi 10951.21Slukem echo 1>&2 "$0: WARNING: $*" 10961.31Satatat} 10971.31Satatat 10981.31Satatat# 10991.31Satatat# backup_file action file cur backup 11001.31Satatat# Make a backup copy of `file' into `cur', and save the previous 11011.31Satatat# version of `cur' as `backup' or use rcs for archiving. 11021.31Satatat# 11031.31Satatat# This routine checks the value of the backup_uses_rcs variable, 11041.31Satatat# which can be either YES or NO. 11051.31Satatat# 11061.31Satatat# The `action' keyword can be one of the following: 11071.31Satatat# 11081.31Satatat# add `file' is now being backed up (and is possibly 11091.31Satatat# being reentered into the backups system). `cur' 11101.31Satatat# is created and RCS files, if necessary, are 11111.31Satatat# created as well. 11121.31Satatat# 11131.31Satatat# update `file' has changed and needs to be backed up. 11141.31Satatat# If `cur' exists, it is copied to to `back' or 11151.31Satatat# checked into RCS (if the repository file is old), 11161.31Satatat# and then `file' is copied to `cur'. Another RCS 11171.31Satatat# check in done here if RCS is being used. 11181.31Satatat# 11191.31Satatat# remove `file' is no longer being tracked by the backups 11201.31Satatat# system. If RCS is not being used, `cur' is moved 11211.31Satatat# to `back', otherwise an empty file is checked in, 11221.31Satatat# and then `cur' is removed. 11231.31Satatat# 11241.31Satatat# 11251.31Satatatbackup_file() 11261.31Satatat{ 11271.31Satatat _action=$1 11281.31Satatat _file=$2 11291.31Satatat _cur=$3 11301.31Satatat _back=$4 11311.31Satatat 11321.31Satatat if checkyesno backup_uses_rcs; then 11331.31Satatat _msg0="backup archive" 11341.31Satatat _msg1="update" 11351.31Satatat 11361.36Satatat # ensure that history file is not locked 11371.36Satatat if [ -f $_cur,v ]; then 11381.36Satatat rcs -q -u -U -M $_cur 11391.36Satatat fi 11401.36Satatat 11411.31Satatat # ensure after switching to rcs that the 11421.31Satatat # current backup is not lost 11431.31Satatat if [ -f $_cur ]; then 11441.31Satatat # no archive, or current newer than archive 11451.103Skre if [ ! -f $_cur,v ] || [ $_cur -nt $_cur,v ]; then 11461.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 11471.36Satatat rcs -q -kb -U $_cur 11481.49Slukem co -q -f -u $_cur 11491.31Satatat fi 11501.31Satatat fi 11511.31Satatat 11521.31Satatat case $_action in 11531.31Satatat add|update) 11541.31Satatat cp -p $_file $_cur 11551.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 11561.36Satatat rcs -q -kb -U $_cur 11571.49Slukem co -q -f -u $_cur 11581.31Satatat chown root:wheel $_cur $_cur,v 11591.31Satatat ;; 11601.31Satatat remove) 11611.31Satatat cp /dev/null $_cur 11621.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 11631.36Satatat rcs -q -kb -U $_cur 11641.31Satatat chown root:wheel $_cur $_cur,v 11651.31Satatat rm $_cur 11661.31Satatat ;; 11671.31Satatat esac 11681.31Satatat else 11691.31Satatat case $_action in 11701.31Satatat add|update) 11711.31Satatat if [ -f $_cur ]; then 11721.31Satatat cp -p $_cur $_back 11731.31Satatat fi 11741.31Satatat cp -p $_file $_cur 11751.31Satatat chown root:wheel $_cur 11761.31Satatat ;; 11771.31Satatat remove) 11781.31Satatat mv -f $_cur $_back 11791.31Satatat ;; 11801.31Satatat esac 11811.31Satatat fi 11821.1Scjs} 11831.64Smycroft 11841.76Schristos# 11851.76Schristos# handle_fsck_error fsck_exit_code 11861.76Schristos# Take action depending on the return code from fsck. 11871.76Schristos# 11881.76Schristoshandle_fsck_error() 11891.76Schristos{ 11901.76Schristos case $1 in 11911.76Schristos 0) # OK 11921.76Schristos return 11931.76Schristos ;; 11941.76Schristos 2) # Needs re-run, still fs errors 11951.76Schristos echo "File system still has errors; re-run fsck manually!" 11961.76Schristos ;; 11971.76Schristos 4) # Root modified 11981.93Swiz echo "Root file system was modified, rebooting ..." 11991.76Schristos reboot -n 12001.76Schristos echo "Reboot failed; help!" 12011.76Schristos ;; 12021.76Schristos 8) # Check failed 12031.76Schristos echo "Automatic file system check failed; help!" 12041.76Schristos ;; 12051.76Schristos 12) # Got signal 12061.76Schristos echo "Boot interrupted." 12071.76Schristos ;; 12081.76Schristos *) 12091.76Schristos echo "Unknown error $1; help!" 12101.76Schristos ;; 12111.76Schristos esac 12121.76Schristos stop_boot 12131.76Schristos} 12141.76Schristos 12151.78Sapb# 12161.78Sapb# _has_rcorder_keyword word file 12171.78Sapb# Check whether a file contains a "# KEYWORD:" comment with a 12181.78Sapb# specified keyword in the style used by rcorder(8). 12191.78Sapb# 12201.78Sapb_has_rcorder_keyword() 12211.78Sapb{ 12221.78Sapb local word="$1" 12231.78Sapb local file="$2" 12241.78Sapb local line 12251.78Sapb 12261.78Sapb [ -r "$file" ] || return 1 12271.78Sapb while read line; do 12281.78Sapb case "${line} " in 12291.78Sapb "# KEYWORD:"*[\ \ ]"${word}"[\ \ ]*) 12301.78Sapb return 0 12311.78Sapb ;; 12321.78Sapb "#"*) 12331.78Sapb continue 12341.78Sapb ;; 12351.78Sapb *[A-Za-z0-9]*) 12361.78Sapb # give up at the first non-empty non-comment line 12371.78Sapb return 1 12381.78Sapb ;; 12391.78Sapb esac 12401.78Sapb done <"$file" 12411.78Sapb return 1 12421.78Sapb} 12431.78Sapb 12441.78Sapb# 12451.78Sapb# print_rc_metadata string 12461.78Sapb# Print the specified string in such a way that the post-processor 12471.78Sapb# inside /etc/rc will treat it as meta-data. 12481.78Sapb# 12491.78Sapb# If we are not running inside /etc/rc, do nothing. 12501.78Sapb# 12511.78Sapb# For public use by any rc.d script, the string must begin with 12521.78Sapb# "note:", followed by arbitrary text. The intent is that the text 12531.78Sapb# will appear in a log file but not on the console. 12541.78Sapb# 12551.78Sapb# For private use within /etc/rc, the string must contain a 12561.78Sapb# keyword recognised by the rc_postprocess_metadata() function 12571.78Sapb# defined in /etc/rc, followed by a colon, followed by one or more 12581.78Sapb# colon-separated arguments associated with the keyword. 12591.78Sapb# 12601.78Sapbprint_rc_metadata() 12611.78Sapb{ 12621.78Sapb # _rc_postprocessor fd, if defined, is the fd to which we must 12631.78Sapb # print, prefixing the output with $_rc_metadata_prefix. 12641.78Sapb # 12651.94Sapb if _have_rc_postprocessor; then 12661.88Sapb command printf "%s%s\n" "$rc_metadata_prefix" "$1" \ 12671.78Sapb >&${_rc_postprocessor_fd} 12681.78Sapb fi 12691.78Sapb} 12701.78Sapb 12711.78Sapb# 12721.88Sapb# _flush_rc_output 12731.88Sapb# Arrange for output to be flushed, if we are running 12741.88Sapb# inside /etc/rc with postprocessing. 12751.88Sapb# 12761.88Sapb_flush_rc_output() 12771.88Sapb{ 12781.88Sapb print_rc_metadata "nop" 12791.88Sapb} 12801.88Sapb 12811.88Sapb# 12821.88Sapb# print_rc_normal [-n] string 12831.78Sapb# Print the specified string in such way that it is treated as 12841.78Sapb# normal output, regardless of whether or not we are running 12851.78Sapb# inside /etc/rc with post-processing. 12861.78Sapb# 12871.88Sapb# If "-n" is specified in $1, then the string in $2 is printed 12881.88Sapb# without a newline; otherwise, the string in $1 is printed 12891.88Sapb# with a newline. 12901.88Sapb# 12911.88Sapb# Intended use cases include: 12921.88Sapb# 12931.88Sapb# o An rc.d script can use ``print_rc_normal -n'' to print a 12941.88Sapb# partial line in such a way that it appears immediately 12951.88Sapb# instead of being buffered by rc(8)'s post-processor. 12961.88Sapb# 12971.88Sapb# o An rc.d script that is run via the no_rc_postprocess 12981.88Sapb# function (so most of its output is invisible to rc(8)'s 12991.88Sapb# post-processor) can use print_rc_normal to force some of its 13001.88Sapb# output to be seen by the post-processor. 13011.88Sapb# 13021.78Sapb# 13031.78Sapbprint_rc_normal() 13041.78Sapb{ 13051.94Sapb # print to stdout or _rc_postprocessor_fd, depending on 13061.94Sapb # whether not we have an rc postprocessor. 13071.78Sapb # 13081.94Sapb local fd=1 13091.94Sapb _have_rc_postprocessor && fd="${_rc_postprocessor_fd}" 13101.88Sapb case "$1" in 13111.88Sapb "-n") 13121.88Sapb command printf "%s" "$2" >&${fd} 13131.88Sapb _flush_rc_output 13141.88Sapb ;; 13151.88Sapb *) 13161.88Sapb command printf "%s\n" "$1" >&${fd} 13171.88Sapb ;; 13181.88Sapb esac 13191.78Sapb} 13201.78Sapb 13211.78Sapb# 13221.78Sapb# no_rc_postprocess cmd... 13231.78Sapb# Execute the specified command in such a way that its output 13241.78Sapb# bypasses the post-processor that handles the output from 13251.78Sapb# most commands that are run inside /etc/rc. If we are not 13261.78Sapb# inside /etc/rc, then just execute the command without special 13271.78Sapb# treatment. 13281.78Sapb# 13291.78Sapb# The intent is that interactive commands can be run via 13301.78Sapb# no_rc_postprocess(), and their output will apear immediately 13311.78Sapb# on the console instead of being hidden or delayed by the 13321.78Sapb# post-processor. An unfortunate consequence of the output 13331.78Sapb# bypassing the post-processor is that the output will not be 13341.78Sapb# logged. 13351.78Sapb# 13361.78Sapbno_rc_postprocess() 13371.78Sapb{ 13381.94Sapb if _have_rc_postprocessor; then 13391.78Sapb "$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd} 13401.78Sapb else 13411.78Sapb "$@" 13421.78Sapb fi 13431.78Sapb} 13441.78Sapb 13451.78Sapb# 13461.78Sapb# twiddle 13471.78Sapb# On each call, print a different one of "/", "-", "\\", "|", 13481.78Sapb# followed by a backspace. The most recently printed value is 13491.78Sapb# saved in $_twiddle_state. 13501.78Sapb# 13511.78Sapb# Output is to /dev/tty, so this function may be useful even inside 13521.78Sapb# a script whose output is redirected. 13531.78Sapb# 13541.78Sapbtwiddle() 13551.78Sapb{ 13561.78Sapb case "$_twiddle_state" in 13571.78Sapb '/') _next='-' ;; 13581.78Sapb '-') _next='\' ;; 13591.78Sapb '\') _next='|' ;; 13601.78Sapb *) _next='/' ;; 13611.78Sapb esac 13621.88Sapb command printf "%s\b" "$_next" >/dev/tty 13631.78Sapb _twiddle_state="$_next" 13641.78Sapb} 13651.78Sapb 13661.82Schristos# 13671.82Schristos# human_exit_code 13681.82Schristos# Print the a human version of the exit code. 13691.82Schristos# 13701.82Schristoshuman_exit_code() 13711.82Schristos{ 13721.83Schristos if [ "$1" -lt 127 ] 13731.82Schristos then 13741.82Schristos echo "exited with code $1" 13751.85Schristos elif [ "$(expr $1 % 256)" -eq 127 ] 13761.82Schristos then 13771.84Schristos # This cannot really happen because the shell will not 13781.84Schristos # pass stopped job status out and the exit code is limited 13791.84Schristos # to 8 bits. This code is here just for completeness. 13801.82Schristos echo "stopped with signal $(expr $1 / 256)" 13811.82Schristos else 13821.82Schristos echo "terminated with signal $(expr $1 - 128)" 13831.82Schristos fi 13841.82Schristos} 13851.86Sapb 13861.86Sapb# 13871.86Sapb# collapse_backslash_newline 13881.86Sapb# Copy input to output, collapsing <backslash><newline> 13891.86Sapb# to nothing, but leaving other backslashes alone. 13901.86Sapb# 13911.86Sapbcollapse_backslash_newline() 13921.86Sapb{ 13931.86Sapb local line 13941.86Sapb while read -r line ; do 13951.86Sapb case "$line" in 13961.86Sapb *\\) 13971.86Sapb # print it, without the backslash or newline 13981.88Sapb command printf "%s" "${line%?}" 13991.86Sapb ;; 14001.86Sapb *) 14011.86Sapb # print it, with a newline 14021.88Sapb command printf "%s\n" "${line}" 14031.86Sapb ;; 14041.86Sapb esac 14051.86Sapb done 14061.86Sapb} 14071.82Schristos 14081.92Sapb# Shell implementations of basename and dirname, usable before 14091.92Sapb# the /usr file system is mounted. 14101.92Sapb# 14111.92Sapbbasename() 14121.92Sapb{ 14131.92Sapb local file="$1" 14141.92Sapb local suffix="$2" 14151.92Sapb local base 14161.92Sapb 14171.92Sapb base="${file##*/}" # remove up to and including last '/' 14181.92Sapb base="${base%${suffix}}" # remove suffix, if any 14191.92Sapb command printf "%s\n" "${base}" 14201.92Sapb} 14211.92Sapb 14221.92Sapbdirname() 14231.92Sapb{ 14241.92Sapb local file="$1" 14251.92Sapb local dir 14261.92Sapb 14271.92Sapb case "$file" in 14281.92Sapb /*/*) dir="${file%/*}" ;; # common case: absolute path 14291.92Sapb /*) dir="/" ;; # special case: name in root dir 14301.92Sapb */*) dir="${file%/*}" ;; # common case: relative path with '/' 14311.92Sapb *) dir="." ;; # special case: name without '/' 14321.92Sapb esac 14331.92Sapb command printf "%s\n" "${dir}" 14341.92Sapb} 14351.92Sapb 14361.88Sapb# Override the normal "echo" and "printf" commands, so that 14371.88Sapb# partial lines printed by rc.d scripts appear immediately, 14381.88Sapb# instead of being buffered by rc(8)'s post-processor. 14391.88Sapb# 14401.88Sapb# Naive use of the echo or printf commands from rc.d scripts, 14411.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr, 14421.88Sapb# will call these functions. To call the real echo and printf 14431.88Sapb# commands, use "command echo" or "command printf". 14441.88Sapb# 14451.103Skre# Avoid use of echo altogether as much as possible, printf works better 14461.103Skre# 14471.88Sapbecho() 14481.88Sapb{ 14491.103Skre local IFS=' ' NL='\n' # not a literal newline... 14501.103Skre 14511.88Sapb case "$1" in 14521.103Skre -n) NL=; shift;; 14531.88Sapb esac 14541.103Skre 14551.103Skre command printf "%s${NL}" "$*" 14561.103Skre 14571.103Skre if test -z "${NL}" 14581.103Skre then 14591.103Skre _flush_rc_output 14601.103Skre fi 14611.103Skre return 0 14621.88Sapb} 14631.103Skre 14641.88Sapbprintf() 14651.88Sapb{ 14661.88Sapb command printf "$@" 14671.88Sapb case "$1" in 14681.88Sapb *'\n') : ;; 14691.88Sapb *) _flush_rc_output ;; 14701.88Sapb esac 14711.103Skre return 0 14721.88Sapb} 14731.88Sapb 14741.98Schristoskat() { 14751.98Schristos local i 14761.98Schristos local v 14771.98Schristos for i; do 14781.98Schristos while read -r v; do 14791.98Schristos v="${v%%#*}" 14801.98Schristos if [ -z "$v" ]; then 14811.98Schristos continue 14821.98Schristos fi 14831.98Schristos echo "$v" 14841.98Schristos done < "$i" 14851.98Schristos done 14861.98Schristos} 14871.98Schristos 14881.64Smycroft_rc_subr_loaded=: 1489