rc.subr revision 1.88
11.88Sapb# $NetBSD: rc.subr,v 1.88 2011/08/11 22:52:47 apb 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.11Slukem# 401.11Slukem# functions 411.11Slukem# --------- 421.1Scjs 431.5Slukem# 441.11Slukem# checkyesno var 451.5Slukem# Test $1 variable, and warn if not set to YES or NO. 461.11Slukem# Return 0 if it's "yes" (et al), nonzero otherwise. 471.5Slukem# 481.11Slukemcheckyesno() 491.11Slukem{ 501.11Slukem eval _value=\$${1} 511.11Slukem case $_value in 521.4Slukem 531.4Slukem # "yes", "true", "on", or "1" 541.4Slukem [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) 551.4Slukem return 0 561.3Slukem ;; 571.4Slukem 581.4Slukem # "no", "false", "off", or "0" 591.4Slukem [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) 601.4Slukem return 1 611.3Slukem ;; 621.3Slukem *) 631.62Sjmmv warn "\$${1} is not set properly - see ${rcvar_manpage}." 641.4Slukem return 1 651.3Slukem ;; 661.3Slukem esac 671.38Slukem} 681.38Slukem 691.65Slukem# 701.78Sapb# yesno_to_truefalse var 711.78Sapb# Convert the value of a variable from any of the values 721.78Sapb# understood by checkyesno() to "true" or "false". 731.78Sapb# 741.78Sapbyesno_to_truefalse() 751.78Sapb{ 761.78Sapb local var=$1 771.78Sapb if checkyesno $var; then 781.78Sapb eval $var=true 791.78Sapb return 0 801.78Sapb else 811.78Sapb eval $var=false 821.78Sapb return 1 831.78Sapb fi 841.78Sapb} 851.78Sapb 861.78Sapb# 871.38Slukem# reverse_list list 881.38Slukem# print the list in reverse order 891.38Slukem# 901.38Slukemreverse_list() 911.38Slukem{ 921.38Slukem _revlist= 931.56Schristos for _revfile; do 941.38Slukem _revlist="$_revfile $_revlist" 951.38Slukem done 961.38Slukem echo $_revlist 971.6Smellon} 981.6Smellon 991.6Smellon# 1001.69Sapb# If booting directly to multiuser, send SIGTERM to 1011.69Sapb# the parent (/etc/rc) to abort the boot. 1021.69Sapb# Otherwise just exit. 1031.69Sapb# 1041.69Sapbstop_boot() 1051.69Sapb{ 1061.69Sapb if [ "$autoboot" = yes ]; then 1071.69Sapb echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" 1081.69Sapb kill -TERM ${RC_PID} 1091.69Sapb fi 1101.69Sapb exit 1 1111.69Sapb} 1121.69Sapb 1131.69Sapb# 1141.47Slukem# mount_critical_filesystems type 1151.47Slukem# Go through the list of critical filesystems as provided in 1161.47Slukem# the rc.conf(5) variable $critical_filesystems_${type}, checking 1171.47Slukem# each one to see if it is mounted, and if it is not, mounting it. 1181.79Sapb# It's not an error if file systems prefixed with "OPTIONAL:" 1191.79Sapb# are not mentioned in /etc/fstab. 1201.6Smellon# 1211.11Slukemmount_critical_filesystems() 1221.11Slukem{ 1231.47Slukem eval _fslist=\$critical_filesystems_${1} 1241.79Sapb _mountcrit_es=0 1251.17Slukem for _fs in $_fslist; do 1261.79Sapb _optional=false 1271.79Sapb case "$_fs" in 1281.79Sapb OPTIONAL:*) 1291.79Sapb _optional=true 1301.79Sapb _fs="${_fs#*:}" 1311.79Sapb ;; 1321.79Sapb esac 1331.79Sapb _ismounted=false 1341.79Sapb # look for a line like "${fs} on * type *" 1351.79Sapb # or "* on ${fs} type *" in the output from mount. 1361.79Sapb case "${nl}$( mount )${nl}" in 1371.79Sapb *" on ${_fs} type "*) 1381.79Sapb _ismounted=true 1391.79Sapb ;; 1401.79Sapb *"${nl}${_fs} on "*) 1411.79Sapb _ismounted=true 1421.79Sapb ;; 1431.79Sapb esac 1441.79Sapb if $_ismounted; then 1451.79Sapb print_rc_metadata \ 1461.79Sapb "note:File system ${_fs} was already mounted" 1471.79Sapb else 1481.79Sapb _mount_output=$( mount $_fs 2>&1 ) 1491.79Sapb _mount_es=$? 1501.79Sapb case "$_mount_output" in 1511.79Sapb *"${nl}"*) 1521.79Sapb # multiple lines can't be good, 1531.79Sapb # not even if $_optional is true 1541.79Sapb ;; 1551.79Sapb *'unknown special file or file system'*) 1561.79Sapb if $_optional; then 1571.79Sapb # ignore this error 1581.79Sapb print_rc_metadata \ 1591.79Sapb "note:Optional file system ${_fs} is not present" 1601.79Sapb _mount_es=0 1611.79Sapb _mount_output="" 1621.6Smellon fi 1631.79Sapb ;; 1641.79Sapb esac 1651.79Sapb if [ -n "$_mount_output" ]; then 1661.79Sapb printf >&2 "%s\n" "$_mount_output" 1671.79Sapb fi 1681.79Sapb if [ "$_mount_es" != 0 ]; then 1691.79Sapb _mountcrit_es="$_mount_es" 1701.6Smellon fi 1711.79Sapb fi 1721.6Smellon done 1731.79Sapb return $_mountcrit_es 1741.7Scjs} 1751.7Scjs 1761.11Slukem# 1771.45Slukem# check_pidfile pidfile procname [interpreter] 1781.45Slukem# Parses the first line of pidfile for a PID, and ensures 1791.11Slukem# that the process is running and matches procname. 1801.45Slukem# Prints the matching PID upon success, nothing otherwise. 1811.45Slukem# interpreter is optional; see _find_processes() for details. 1821.11Slukem# 1831.11Slukemcheck_pidfile() 1841.11Slukem{ 1851.11Slukem _pidfile=$1 1861.11Slukem _procname=$2 1871.45Slukem _interpreter=$3 1881.11Slukem if [ -z "$_pidfile" -o -z "$_procname" ]; then 1891.45Slukem err 3 'USAGE: check_pidfile pidfile procname [interpreter]' 1901.11Slukem fi 1911.11Slukem if [ ! -f $_pidfile ]; then 1921.11Slukem return 1931.11Slukem fi 1941.11Slukem read _pid _junk < $_pidfile 1951.11Slukem if [ -z "$_pid" ]; then 1961.11Slukem return 1971.11Slukem fi 1981.45Slukem _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" 1991.11Slukem} 2001.11Slukem 2011.11Slukem# 2021.45Slukem# check_process procname [interpreter] 2031.11Slukem# Ensures that a process (or processes) named procname is running. 2041.45Slukem# Prints a list of matching PIDs. 2051.45Slukem# interpreter is optional; see _find_processes() for details. 2061.11Slukem# 2071.11Slukemcheck_process() 2081.11Slukem{ 2091.11Slukem _procname=$1 2101.45Slukem _interpreter=$2 2111.11Slukem if [ -z "$_procname" ]; then 2121.45Slukem err 3 'USAGE: check_process procname [interpreter]' 2131.45Slukem fi 2141.45Slukem _find_processes $_procname ${_interpreter:-.} '-ax' 2151.45Slukem} 2161.45Slukem 2171.46Slukem# 2181.45Slukem# _find_processes procname interpreter psargs 2191.45Slukem# Search for procname in the output of ps generated by psargs. 2201.45Slukem# Prints the PIDs of any matching processes, space separated. 2211.45Slukem# 2221.45Slukem# If interpreter == ".", check the following variations of procname 2231.45Slukem# against the first word of each command: 2241.45Slukem# procname 2251.45Slukem# `basename procname` 2261.45Slukem# `basename procname` + ":" 2271.45Slukem# "(" + `basename procname` + ")" 2281.45Slukem# 2291.45Slukem# If interpreter != ".", read the first line of procname, remove the 2301.45Slukem# leading #!, normalise whitespace, append procname, and attempt to 2311.45Slukem# match that against each command, either as is, or with extra words 2321.73Sdholland# at the end. As an alternative, to deal with interpreted daemons 2331.66She# using perl, the basename of the interpreter plus a colon is also 2341.66She# tried as the prefix to procname. 2351.45Slukem# 2361.45Slukem_find_processes() 2371.45Slukem{ 2381.45Slukem if [ $# -ne 3 ]; then 2391.45Slukem err 3 'USAGE: _find_processes procname interpreter psargs' 2401.11Slukem fi 2411.45Slukem _procname=$1 2421.45Slukem _interpreter=$2 2431.45Slukem _psargs=$3 2441.45Slukem 2451.11Slukem _pref= 2461.68Shubertf _procnamebn=${_procname##*/} 2471.45Slukem if [ $_interpreter != "." ]; then # an interpreted script 2481.67Selad read _interp < ${_chroot:-}/$_procname # read interpreter name 2491.45Slukem _interp=${_interp#\#!} # strip #! 2501.45Slukem set -- $_interp 2511.87Schristos if [ $1 = "/usr/bin/env" ]; then 2521.87Schristos shift 2531.87Schristos set -- $(type $1) 2541.87Schristos shift $(($# - 1)) 2551.87Schristos _interp="${1##*/} $_procname" 2561.87Schristos else 2571.87Schristos _interp="$* $_procname" 2581.87Schristos fi 2591.45Slukem if [ $_interpreter != $1 ]; then 2601.45Slukem warn "\$command_interpreter $_interpreter != $1" 2611.45Slukem fi 2621.66She _interpbn=${1##*/} 2631.45Slukem _fp_args='_argv' 2641.45Slukem _fp_match='case "$_argv" in 2651.68Shubertf ${_interp}|"${_interp} "*|"${_interpbn}: "*${_procnamebn}*)' 2661.45Slukem else # a normal daemon 2671.45Slukem _fp_args='_arg0 _argv' 2681.45Slukem _fp_match='case "$_arg0" in 2691.45Slukem $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})")' 2701.45Slukem fi 2711.45Slukem 2721.45Slukem _proccheck=' 2731.46Slukem ps -o "pid,command" '"$_psargs"' | 2741.45Slukem while read _npid '"$_fp_args"'; do 2751.45Slukem case "$_npid" in 2761.45Slukem PID) 2771.45Slukem continue ;; 2781.45Slukem esac ; '"$_fp_match"' 2791.45Slukem echo -n "$_pref$_npid" ; 2801.45Slukem _pref=" " 2811.45Slukem ;; 2821.45Slukem esac 2831.45Slukem done' 2841.45Slukem 2851.45Slukem#echo 1>&2 "proccheck is :$_proccheck:" 2861.45Slukem eval $_proccheck 2871.11Slukem} 2881.11Slukem 2891.11Slukem# 2901.33Slukem# wait_for_pids pid [pid ...] 2911.35Slukem# spins until none of the pids exist 2921.33Slukem# 2931.33Slukemwait_for_pids() 2941.33Slukem{ 2951.56Schristos _list="$@" 2961.33Slukem if [ -z "$_list" ]; then 2971.33Slukem return 2981.33Slukem fi 2991.35Slukem _prefix= 3001.35Slukem while true; do 3011.33Slukem _nlist=""; 3021.33Slukem for _j in $_list; do 3031.33Slukem if kill -0 $_j 2>/dev/null; then 3041.33Slukem _nlist="${_nlist}${_nlist:+ }$_j" 3051.33Slukem fi 3061.33Slukem done 3071.33Slukem if [ -z "$_nlist" ]; then 3081.33Slukem break 3091.33Slukem fi 3101.33Slukem _list=$_nlist 3111.35Slukem echo -n ${_prefix:-"Waiting for PIDS: "}$_list 3121.35Slukem _prefix=", " 3131.35Slukem sleep 2 3141.33Slukem done 3151.35Slukem if [ -n "$_prefix" ]; then 3161.35Slukem echo "." 3171.35Slukem fi 3181.33Slukem} 3191.33Slukem 3201.33Slukem# 3211.81Sjmmv# run_rc_command argument [parameters] 3221.46Slukem# Search for argument in the list of supported commands, which is: 3231.33Slukem# "start stop restart rcvar status poll ${extra_commands}" 3241.46Slukem# If there's a match, run ${argument}_cmd or the default method 3251.81Sjmmv# (see below), and pass the optional list of parameters to it. 3261.11Slukem# 3271.46Slukem# If argument has a given prefix, then change the operation as follows: 3281.46Slukem# Prefix Operation 3291.11Slukem# ------ --------- 3301.48Slukem# fast Skip the pid check, and set rc_fast=yes 3311.48Slukem# force Set ${rcvar} to YES, and set rc_force=yes 3321.61Slukem# one Set ${rcvar} to YES 3331.11Slukem# 3341.11Slukem# The following globals are used: 3351.24Slukem# 3361.46Slukem# Name Needed Purpose 3371.46Slukem# ---- ------ ------- 3381.11Slukem# name y Name of script. 3391.24Slukem# 3401.11Slukem# command n Full path to command. 3411.46Slukem# Not needed if ${rc_arg}_cmd is set for 3421.11Slukem# each keyword. 3431.24Slukem# 3441.11Slukem# command_args n Optional args/shell directives for command. 3451.24Slukem# 3461.45Slukem# command_interpreter n If not empty, command is interpreted, so 3471.45Slukem# call check_{pidfile,process}() appropriately. 3481.45Slukem# 3491.16Slukem# extra_commands n List of extra commands supported. 3501.24Slukem# 3511.42Slukem# pidfile n If set, use check_pidfile $pidfile $command, 3521.42Slukem# otherwise use check_process $command. 3531.42Slukem# In either case, only check if $command is set. 3541.42Slukem# 3551.42Slukem# procname n Process name to check for instead of $command. 3561.24Slukem# 3571.24Slukem# rcvar n This is checked with checkyesno to determine 3581.24Slukem# if the action should be run. 3591.24Slukem# 3601.22Slukem# ${name}_chroot n Directory to chroot to before running ${command} 3611.42Slukem# Requires /usr to be mounted. 3621.24Slukem# 3631.22Slukem# ${name}_chdir n Directory to cd to before running ${command} 3641.22Slukem# (if not using ${name}_chroot). 3651.24Slukem# 3661.11Slukem# ${name}_flags n Arguments to call ${command} with. 3671.24Slukem# NOTE: $flags from the parent environment 3681.24Slukem# can be used to override this. 3691.24Slukem# 3701.72She# ${name}_env n Additional environment variable settings 3711.72She# for running ${command} 3721.72She# 3731.23Slukem# ${name}_nice n Nice level to run ${command} at. 3741.24Slukem# 3751.22Slukem# ${name}_user n User to run ${command} as, using su(1) if not 3761.22Slukem# using ${name}_chroot. 3771.42Slukem# Requires /usr to be mounted. 3781.24Slukem# 3791.22Slukem# ${name}_group n Group to run chrooted ${command} as. 3801.42Slukem# Requires /usr to be mounted. 3811.24Slukem# 3821.32Slukem# ${name}_groups n Comma separated list of supplementary groups 3831.32Slukem# to run the chrooted ${command} with. 3841.42Slukem# Requires /usr to be mounted. 3851.24Slukem# 3861.46Slukem# ${rc_arg}_cmd n If set, use this as the method when invoked; 3871.11Slukem# Otherwise, use default command (see below) 3881.24Slukem# 3891.46Slukem# ${rc_arg}_precmd n If set, run just before performing the 3901.46Slukem# ${rc_arg}_cmd method in the default 3911.46Slukem# operation (i.e, after checking for required 3921.46Slukem# bits and process (non)existence). 3931.11Slukem# If this completes with a non-zero exit code, 3941.46Slukem# don't run ${rc_arg}_cmd. 3951.24Slukem# 3961.46Slukem# ${rc_arg}_postcmd n If set, run just after performing the 3971.46Slukem# ${rc_arg}_cmd method, if that method 3981.43Slukem# returned a zero exit code. 3991.43Slukem# 4001.11Slukem# required_dirs n If set, check for the existence of the given 4011.11Slukem# directories before running the default 4021.11Slukem# (re)start command. 4031.24Slukem# 4041.11Slukem# required_files n If set, check for the readability of the given 4051.11Slukem# files before running the default (re)start 4061.11Slukem# command. 4071.24Slukem# 4081.11Slukem# required_vars n If set, perform checkyesno on each of the 4091.11Slukem# listed variables before running the default 4101.11Slukem# (re)start command. 4111.11Slukem# 4121.46Slukem# Default behaviour for a given argument, if no override method is 4131.46Slukem# provided: 4141.24Slukem# 4151.46Slukem# Argument Default behaviour 4161.46Slukem# -------- ----------------- 4171.11Slukem# start if !running && checkyesno ${rcvar} 4181.11Slukem# ${command} 4191.24Slukem# 4201.11Slukem# stop if ${pidfile} 4211.46Slukem# rc_pid=$(check_pidfile $pidfile $command) 4221.11Slukem# else 4231.46Slukem# rc_pid=$(check_process $command) 4241.46Slukem# kill $sig_stop $rc_pid 4251.46Slukem# wait_for_pids $rc_pid 4261.35Slukem# ($sig_stop defaults to TERM.) 4271.24Slukem# 4281.35Slukem# reload Similar to stop, except use $sig_reload instead, 4291.35Slukem# and doesn't wait_for_pids. 4301.11Slukem# $sig_reload defaults to HUP. 4311.24Slukem# 4321.11Slukem# restart Run `stop' then `start'. 4331.11Slukem# 4341.33Slukem# status Show if ${command} is running, etc. 4351.33Slukem# 4361.33Slukem# poll Wait for ${command} to exit. 4371.33Slukem# 4381.33Slukem# rcvar Display what rc.conf variable is used (if any). 4391.33Slukem# 4401.46Slukem# Variables available to methods, and after run_rc_command() has 4411.46Slukem# completed: 4421.46Slukem# 4431.46Slukem# Variable Purpose 4441.46Slukem# -------- ------- 4451.61Slukem# rc_arg Argument to command, after fast/force/one processing 4461.46Slukem# performed 4471.46Slukem# 4481.46Slukem# rc_flags Flags to start the default command with. 4491.46Slukem# Defaults to ${name}_flags, unless overridden 4501.46Slukem# by $flags from the environment. 4511.46Slukem# This variable may be changed by the precmd method. 4521.46Slukem# 4531.46Slukem# rc_pid PID of command (if appropriate) 4541.46Slukem# 4551.46Slukem# rc_fast Not empty if "fast" was provided (q.v.) 4561.46Slukem# 4571.46Slukem# rc_force Not empty if "force" was provided (q.v.) 4581.33Slukem# 4591.24Slukem# 4601.11Slukemrun_rc_command() 4611.11Slukem{ 4621.46Slukem rc_arg=$1 4631.24Slukem if [ -z "$name" ]; then 4641.30Slukem err 3 'run_rc_command: $name is not set.' 4651.11Slukem fi 4661.11Slukem 4671.61Slukem _rc_prefix= 4681.46Slukem case "$rc_arg" in 4691.24Slukem fast*) # "fast" prefix; don't check pid 4701.46Slukem rc_arg=${rc_arg#fast} 4711.48Slukem rc_fast=yes 4721.11Slukem ;; 4731.61Slukem force*) # "force" prefix; always run 4741.48Slukem rc_force=yes 4751.61Slukem _rc_prefix=force 4761.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4771.61Slukem if [ -n "${rcvar}" ]; then 4781.61Slukem eval ${rcvar}=YES 4791.61Slukem fi 4801.61Slukem ;; 4811.61Slukem one*) # "one" prefix; set ${rcvar}=yes 4821.61Slukem _rc_prefix=one 4831.61Slukem rc_arg=${rc_arg#${_rc_prefix}} 4841.29Slukem if [ -n "${rcvar}" ]; then 4851.29Slukem eval ${rcvar}=YES 4861.29Slukem fi 4871.11Slukem ;; 4881.11Slukem esac 4891.11Slukem 4901.75Sreed _keywords="start stop restart rcvar" 4911.75Sreed if [ -n "$extra_commands" ]; then 4921.75Sreed _keywords="${_keywords} ${extra_commands}" 4931.75Sreed fi 4941.46Slukem rc_pid= 4951.11Slukem _pidcmd= 4961.42Slukem _procname=${procname:-${command}} 4971.42Slukem 4981.24Slukem # setup pid check command if not fast 4991.46Slukem if [ -z "$rc_fast" -a -n "$_procname" ]; then 5001.11Slukem if [ -n "$pidfile" ]; then 5011.46Slukem _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' 5021.42Slukem else 5031.46Slukem _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' 5041.11Slukem fi 5051.11Slukem if [ -n "$_pidcmd" ]; then 5061.33Slukem _keywords="${_keywords} status poll" 5071.11Slukem fi 5081.11Slukem fi 5091.11Slukem 5101.46Slukem if [ -z "$rc_arg" ]; then 5111.11Slukem rc_usage "$_keywords" 5121.11Slukem fi 5131.81Sjmmv shift # remove $rc_arg from the positional parameters 5141.11Slukem 5151.17Slukem if [ -n "$flags" ]; then # allow override from environment 5161.46Slukem rc_flags=$flags 5171.11Slukem else 5181.46Slukem eval rc_flags=\$${name}_flags 5191.11Slukem fi 5201.30Slukem eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ 5211.30Slukem _nice=\$${name}_nice _user=\$${name}_user \ 5221.72She _group=\$${name}_group _groups=\$${name}_groups \ 5231.72She _env=\"\$${name}_env\" 5241.11Slukem 5251.42Slukem if [ -n "$_user" ]; then # unset $_user if running as that user 5261.42Slukem if [ "$_user" = "$(id -un)" ]; then 5271.42Slukem unset _user 5281.42Slukem fi 5291.42Slukem fi 5301.42Slukem 5311.29Slukem # if ${rcvar} is set, and $1 is not 5321.40Slukem # "rcvar", then run 5331.29Slukem # checkyesno ${rcvar} 5341.74Ssalo # and return if that failed or warn 5351.74Ssalo # user and exit when interactive 5361.24Slukem # 5371.46Slukem if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" ]; then 5381.24Slukem if ! checkyesno ${rcvar}; then 5391.74Ssalo # check whether interactive or not 5401.74Ssalo if [ -n "$_run_rc_script" ]; then 5411.74Ssalo return 0 5421.74Ssalo fi 5431.74Ssalo for _elem in $_keywords; do 5441.74Ssalo if [ "$_elem" = "$rc_arg" ]; then 5451.88Sapb cat 1>&2 <<EOF 5461.88Sapb\$${rcvar} is not enabled - see ${rcvar_manpage}. 5471.88SapbUse the following if you wish to perform the operation: 5481.88Sapb $0 one${rc_arg} 5491.88SapbEOF 5501.74Ssalo exit 1 5511.74Ssalo fi 5521.74Ssalo done 5531.74Ssalo echo 1>&2 "$0: unknown directive '$rc_arg'." 5541.74Ssalo rc_usage "$_keywords" 5551.24Slukem fi 5561.24Slukem fi 5571.24Slukem 5581.24Slukem eval $_pidcmd # determine the pid if necessary 5591.11Slukem 5601.11Slukem for _elem in $_keywords; do 5611.46Slukem if [ "$_elem" != "$rc_arg" ]; then 5621.11Slukem continue 5631.11Slukem fi 5641.11Slukem 5651.24Slukem # if there's a custom ${XXX_cmd}, 5661.24Slukem # run that instead of the default 5671.24Slukem # 5681.46Slukem eval _cmd=\$${rc_arg}_cmd _precmd=\$${rc_arg}_precmd \ 5691.46Slukem _postcmd=\$${rc_arg}_postcmd 5701.11Slukem if [ -n "$_cmd" ]; then 5711.24Slukem # if the precmd failed and force 5721.24Slukem # isn't set, exit 5731.24Slukem # 5741.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 5751.24Slukem return 1 5761.24Slukem fi 5771.24Slukem 5781.81Sjmmv if ! eval $_cmd \"\${@}\" && [ -z "$rc_force" ]; then 5791.44Slukem return 1 5801.44Slukem fi 5811.44Slukem eval $_postcmd 5821.11Slukem return 0 5831.11Slukem fi 5841.11Slukem 5851.81Sjmmv if [ ${#} -gt 0 ]; then 5861.81Sjmmv err 1 "the $rc_arg command does not take any parameters" 5871.81Sjmmv fi 5881.81Sjmmv 5891.46Slukem case "$rc_arg" in # default operations... 5901.11Slukem 5911.11Slukem status) 5921.46Slukem if [ -n "$rc_pid" ]; then 5931.46Slukem echo "${name} is running as pid $rc_pid." 5941.11Slukem else 5951.11Slukem echo "${name} is not running." 5961.28Slukem return 1 5971.11Slukem fi 5981.11Slukem ;; 5991.11Slukem 6001.11Slukem start) 6011.46Slukem if [ -n "$rc_pid" ]; then 6021.63Slukem echo 1>&2 "${name} already running? (pid=$rc_pid)." 6031.11Slukem exit 1 6041.11Slukem fi 6051.11Slukem 6061.57Slukem if [ ! -x ${_chroot}${command} ]; then 6071.11Slukem return 0 6081.11Slukem fi 6091.11Slukem 6101.24Slukem # check for required variables, 6111.24Slukem # directories, and files 6121.24Slukem # 6131.11Slukem for _f in $required_vars; do 6141.11Slukem if ! checkyesno $_f; then 6151.65Slukem warn "\$${_f} is not enabled." 6161.46Slukem if [ -z "$rc_force" ]; then 6171.24Slukem return 1 6181.24Slukem fi 6191.11Slukem fi 6201.11Slukem done 6211.11Slukem for _f in $required_dirs; do 6221.11Slukem if [ ! -d "${_f}/." ]; then 6231.25Slukem warn "${_f} is not a directory." 6241.46Slukem if [ -z "$rc_force" ]; then 6251.24Slukem return 1 6261.24Slukem fi 6271.11Slukem fi 6281.11Slukem done 6291.11Slukem for _f in $required_files; do 6301.11Slukem if [ ! -r "${_f}" ]; then 6311.25Slukem warn "${_f} is not readable." 6321.46Slukem if [ -z "$rc_force" ]; then 6331.24Slukem return 1 6341.24Slukem fi 6351.11Slukem fi 6361.11Slukem done 6371.11Slukem 6381.24Slukem # if the precmd failed and force 6391.24Slukem # isn't set, exit 6401.24Slukem # 6411.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6421.24Slukem return 1 6431.24Slukem fi 6441.24Slukem 6451.24Slukem # setup the command to run, and run it 6461.29Slukem # 6471.11Slukem echo "Starting ${name}." 6481.22Slukem if [ -n "$_chroot" ]; then 6491.22Slukem _doit="\ 6501.72She${_env:+env $_env }\ 6511.23Slukem${_nice:+nice -n $_nice }\ 6521.22Slukemchroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ 6531.46Slukem$_chroot $command $rc_flags $command_args" 6541.22Slukem else 6551.22Slukem _doit="\ 6561.18Slukem${_chdir:+cd $_chdir; }\ 6571.72She${_env:+env $_env }\ 6581.18Slukem${_nice:+nice -n $_nice }\ 6591.46Slukem$command $rc_flags $command_args" 6601.34Slukem if [ -n "$_user" ]; then 6611.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 6621.34Slukem fi 6631.22Slukem fi 6641.43Slukem 6651.43Slukem # if the cmd failed and force 6661.43Slukem # isn't set, exit 6671.43Slukem # 6681.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 6691.43Slukem return 1 6701.43Slukem fi 6711.43Slukem 6721.43Slukem # finally, run postcmd 6731.43Slukem # 6741.43Slukem eval $_postcmd 6751.11Slukem ;; 6761.11Slukem 6771.11Slukem stop) 6781.46Slukem if [ -z "$rc_pid" ]; then 6791.24Slukem if [ -n "$pidfile" ]; then 6801.63Slukem echo 1>&2 \ 6811.24Slukem "${name} not running? (check $pidfile)." 6821.24Slukem else 6831.63Slukem echo 1>&2 "${name} not running?" 6841.11Slukem fi 6851.24Slukem exit 1 6861.11Slukem fi 6871.11Slukem 6881.43Slukem # if the precmd failed and force 6891.43Slukem # isn't set, exit 6901.43Slukem # 6911.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 6921.24Slukem return 1 6931.24Slukem fi 6941.43Slukem 6951.43Slukem # send the signal to stop 6961.43Slukem # 6971.11Slukem echo "Stopping ${name}." 6981.46Slukem _doit="kill -${sig_stop:-TERM} $rc_pid" 6991.34Slukem if [ -n "$_user" ]; then 7001.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 7011.34Slukem fi 7021.43Slukem 7031.43Slukem # if the stop cmd failed and force 7041.43Slukem # isn't set, exit 7051.43Slukem # 7061.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 7071.43Slukem return 1 7081.43Slukem fi 7091.43Slukem 7101.43Slukem # wait for the command to exit, 7111.43Slukem # and run postcmd. 7121.46Slukem wait_for_pids $rc_pid 7131.43Slukem eval $_postcmd 7141.11Slukem ;; 7151.11Slukem 7161.11Slukem reload) 7171.46Slukem if [ -z "$rc_pid" ]; then 7181.24Slukem if [ -n "$pidfile" ]; then 7191.63Slukem echo 1>&2 \ 7201.11Slukem "${name} not running? (check $pidfile)." 7211.24Slukem else 7221.63Slukem echo 1>&2 "${name} not running?" 7231.11Slukem fi 7241.24Slukem exit 1 7251.11Slukem fi 7261.11Slukem echo "Reloading ${name} config files." 7271.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7281.24Slukem return 1 7291.24Slukem fi 7301.46Slukem _doit="kill -${sig_reload:-HUP} $rc_pid" 7311.34Slukem if [ -n "$_user" ]; then 7321.34Slukem _doit="su -m $_user -c 'sh -c \"$_doit\"'" 7331.34Slukem fi 7341.46Slukem if ! eval $_doit && [ -z "$rc_force" ]; then 7351.43Slukem return 1 7361.43Slukem fi 7371.43Slukem eval $_postcmd 7381.11Slukem ;; 7391.11Slukem 7401.11Slukem restart) 7411.46Slukem if ! eval $_precmd && [ -z "$rc_force" ]; then 7421.24Slukem return 1 7431.11Slukem fi 7441.29Slukem # prevent restart being called more 7451.29Slukem # than once by any given script 7461.29Slukem # 7471.53Slukem if ${_rc_restart_done:-false}; then 7481.29Slukem return 0 7491.29Slukem fi 7501.53Slukem _rc_restart_done=true 7511.33Slukem 7521.61Slukem ( $0 ${_rc_prefix}stop ) 7531.61Slukem $0 ${_rc_prefix}start 7541.11Slukem 7551.43Slukem eval $_postcmd 7561.33Slukem ;; 7571.33Slukem 7581.33Slukem poll) 7591.46Slukem if [ -n "$rc_pid" ]; then 7601.46Slukem wait_for_pids $rc_pid 7611.33Slukem fi 7621.11Slukem ;; 7631.11Slukem 7641.11Slukem rcvar) 7651.11Slukem echo "# $name" 7661.24Slukem if [ -n "$rcvar" ]; then 7671.24Slukem if checkyesno ${rcvar}; then 7681.24Slukem echo "\$${rcvar}=YES" 7691.24Slukem else 7701.24Slukem echo "\$${rcvar}=NO" 7711.24Slukem fi 7721.11Slukem fi 7731.11Slukem ;; 7741.11Slukem 7751.11Slukem *) 7761.11Slukem rc_usage "$_keywords" 7771.11Slukem ;; 7781.11Slukem 7791.11Slukem esac 7801.11Slukem return 0 7811.11Slukem done 7821.11Slukem 7831.46Slukem echo 1>&2 "$0: unknown directive '$rc_arg'." 7841.11Slukem rc_usage "$_keywords" 7851.11Slukem exit 1 7861.11Slukem} 7871.11Slukem 7881.11Slukem# 7891.11Slukem# run_rc_script file arg 7901.11Slukem# Start the script `file' with `arg', and correctly handle the 7911.17Slukem# return value from the script. If `file' ends with `.sh', it's 7921.37Slukem# sourced into the current environment. If `file' appears to be 7931.37Slukem# a backup or scratch file, ignore it. Otherwise if it's 7941.37Slukem# executable run as a child process. 7951.17Slukem# 7961.78Sapb# If `file' contains "KEYWORD: interactive" and if we are 7971.78Sapb# running inside /etc/rc with postprocessing (as signified by 7981.78Sapb# _rc_postprocessor_fd being defined) then the script's stdout 7991.78Sapb# and stderr are redirected to $_rc_original_stdout_fd and 8001.78Sapb# $_rc_original_stderr_fd, so the output will be displayed on the 8011.78Sapb# console but not intercepted by /etc/rc's postprocessor. 8021.78Sapb# 8031.11Slukemrun_rc_script() 8041.11Slukem{ 8051.11Slukem _file=$1 8061.11Slukem _arg=$2 8071.11Slukem if [ -z "$_file" -o -z "$_arg" ]; then 8081.11Slukem err 3 'USAGE: run_rc_script file arg' 8091.11Slukem fi 8101.11Slukem 8111.74Ssalo _run_rc_script=true 8121.74Ssalo 8131.45Slukem unset name command command_args command_interpreter \ 8141.45Slukem extra_commands pidfile procname \ 8151.42Slukem rcvar required_dirs required_files required_vars 8161.43Slukem eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd 8171.39Slukem 8181.78Sapb _must_redirect=false 8191.80Sapb if [ -n "${_rc_postprocessor_fd}" ] \ 8201.78Sapb && _has_rcorder_keyword interactive $_file 8211.78Sapb then 8221.78Sapb _must_redirect=true 8231.78Sapb fi 8241.78Sapb 8251.39Slukem case "$_file" in 8261.39Slukem *.sh) # run in current shell 8271.78Sapb if $_must_redirect; then 8281.78Sapb print_rc_metadata \ 8291.78Sapb "note:Output from ${_file} is not logged" 8301.80Sapb no_rc_postprocess eval \ 8311.80Sapb 'set $_arg ; . $_file' 8321.78Sapb else 8331.78Sapb set $_arg ; . $_file 8341.78Sapb fi 8351.39Slukem ;; 8361.60Slukem *[~#]|*.OLD|*.orig|*,v) # scratch file; skip 8371.39Slukem warn "Ignoring scratch file $_file" 8381.39Slukem ;; 8391.39Slukem *) # run in subshell 8401.78Sapb if [ -x $_file ] && $_must_redirect; then 8411.78Sapb print_rc_metadata \ 8421.78Sapb "note:Output from ${_file} is not logged" 8431.78Sapb if [ -n "$rc_fast_and_loose" ]; then 8441.80Sapb no_rc_postprocess eval \ 8451.80Sapb 'set $_arg ; . $_file' 8461.78Sapb else 8471.80Sapb no_rc_postprocess eval \ 8481.80Sapb '( set $_arg ; . $_file )' 8491.78Sapb fi 8501.78Sapb elif [ -x $_file ]; then 8511.39Slukem if [ -n "$rc_fast_and_loose" ]; then 8521.39Slukem set $_arg ; . $_file 8531.39Slukem else 8541.37Slukem ( set $_arg ; . $_file ) 8551.37Slukem fi 8561.78Sapb else 8571.78Sapb warn "Ignoring non-executable file $_file" 8581.39Slukem fi 8591.39Slukem ;; 8601.39Slukem esac 8611.11Slukem} 8621.19Slukem 8631.19Slukem# 8641.65Slukem# load_rc_config command 8651.19Slukem# Source in the configuration file for a given command. 8661.19Slukem# 8671.19Slukemload_rc_config() 8681.19Slukem{ 8691.19Slukem _command=$1 8701.19Slukem if [ -z "$_command" ]; then 8711.19Slukem err 3 'USAGE: load_rc_config command' 8721.19Slukem fi 8731.19Slukem 8741.54Slukem if ${_rc_conf_loaded:-false}; then 8751.54Slukem : 8761.54Slukem else 8771.30Slukem . /etc/rc.conf 8781.53Slukem _rc_conf_loaded=true 8791.30Slukem fi 8801.20Sfvdl if [ -f /etc/rc.conf.d/"$_command" ]; then 8811.20Sfvdl . /etc/rc.conf.d/"$_command" 8821.20Sfvdl fi 8831.19Slukem} 8841.19Slukem 8851.65Slukem# 8861.65Slukem# load_rc_config_var cmd var 8871.65Slukem# Read the rc.conf(5) var for cmd and set in the 8881.65Slukem# current shell, using load_rc_config in a subshell to prevent 8891.65Slukem# unwanted side effects from other variable assignments. 8901.65Slukem# 8911.65Slukemload_rc_config_var() 8921.65Slukem{ 8931.65Slukem if [ $# -ne 2 ]; then 8941.65Slukem err 3 'USAGE: load_rc_config_var cmd var' 8951.65Slukem fi 8961.65Slukem eval $(eval '( 8971.65Slukem load_rc_config '$1' >/dev/null; 8981.77Sapb if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then 8991.65Slukem echo '$2'=\'\''${'$2'}\'\''; 9001.65Slukem fi 9011.65Slukem )' ) 9021.65Slukem} 9031.11Slukem 9041.11Slukem# 9051.11Slukem# rc_usage commands 9061.11Slukem# Print a usage string for $0, with `commands' being a list of 9071.11Slukem# valid commands. 9081.11Slukem# 9091.11Slukemrc_usage() 9101.11Slukem{ 9111.61Slukem echo -n 1>&2 "Usage: $0 [fast|force|one](" 9121.11Slukem 9131.11Slukem _sep= 9141.56Schristos for _elem; do 9151.11Slukem echo -n 1>&2 "$_sep$_elem" 9161.11Slukem _sep="|" 9171.11Slukem done 9181.11Slukem echo 1>&2 ")" 9191.11Slukem exit 1 9201.11Slukem} 9211.11Slukem 9221.11Slukem# 9231.11Slukem# err exitval message 9241.11Slukem# Display message to stderr and log to the syslog, and exit with exitval. 9251.11Slukem# 9261.11Slukemerr() 9271.11Slukem{ 9281.11Slukem exitval=$1 9291.11Slukem shift 9301.11Slukem 9311.51Sgrant if [ -x /usr/bin/logger ]; then 9321.51Sgrant logger "$0: ERROR: $*" 9331.51Sgrant fi 9341.21Slukem echo 1>&2 "$0: ERROR: $*" 9351.11Slukem exit $exitval 9361.11Slukem} 9371.11Slukem 9381.11Slukem# 9391.11Slukem# warn message 9401.11Slukem# Display message to stderr and log to the syslog. 9411.11Slukem# 9421.11Slukemwarn() 9431.11Slukem{ 9441.51Sgrant if [ -x /usr/bin/logger ]; then 9451.51Sgrant logger "$0: WARNING: $*" 9461.51Sgrant fi 9471.21Slukem echo 1>&2 "$0: WARNING: $*" 9481.31Satatat} 9491.31Satatat 9501.31Satatat# 9511.31Satatat# backup_file action file cur backup 9521.31Satatat# Make a backup copy of `file' into `cur', and save the previous 9531.31Satatat# version of `cur' as `backup' or use rcs for archiving. 9541.31Satatat# 9551.31Satatat# This routine checks the value of the backup_uses_rcs variable, 9561.31Satatat# which can be either YES or NO. 9571.31Satatat# 9581.31Satatat# The `action' keyword can be one of the following: 9591.31Satatat# 9601.31Satatat# add `file' is now being backed up (and is possibly 9611.31Satatat# being reentered into the backups system). `cur' 9621.31Satatat# is created and RCS files, if necessary, are 9631.31Satatat# created as well. 9641.31Satatat# 9651.31Satatat# update `file' has changed and needs to be backed up. 9661.31Satatat# If `cur' exists, it is copied to to `back' or 9671.31Satatat# checked into RCS (if the repository file is old), 9681.31Satatat# and then `file' is copied to `cur'. Another RCS 9691.31Satatat# check in done here if RCS is being used. 9701.31Satatat# 9711.31Satatat# remove `file' is no longer being tracked by the backups 9721.31Satatat# system. If RCS is not being used, `cur' is moved 9731.31Satatat# to `back', otherwise an empty file is checked in, 9741.31Satatat# and then `cur' is removed. 9751.31Satatat# 9761.31Satatat# 9771.31Satatatbackup_file() 9781.31Satatat{ 9791.31Satatat _action=$1 9801.31Satatat _file=$2 9811.31Satatat _cur=$3 9821.31Satatat _back=$4 9831.31Satatat 9841.31Satatat if checkyesno backup_uses_rcs; then 9851.31Satatat _msg0="backup archive" 9861.31Satatat _msg1="update" 9871.31Satatat 9881.36Satatat # ensure that history file is not locked 9891.36Satatat if [ -f $_cur,v ]; then 9901.36Satatat rcs -q -u -U -M $_cur 9911.36Satatat fi 9921.36Satatat 9931.31Satatat # ensure after switching to rcs that the 9941.31Satatat # current backup is not lost 9951.31Satatat if [ -f $_cur ]; then 9961.31Satatat # no archive, or current newer than archive 9971.31Satatat if [ ! -f $_cur,v -o $_cur -nt $_cur,v ]; then 9981.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 9991.36Satatat rcs -q -kb -U $_cur 10001.49Slukem co -q -f -u $_cur 10011.31Satatat fi 10021.31Satatat fi 10031.31Satatat 10041.31Satatat case $_action in 10051.31Satatat add|update) 10061.31Satatat cp -p $_file $_cur 10071.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 10081.36Satatat rcs -q -kb -U $_cur 10091.49Slukem co -q -f -u $_cur 10101.31Satatat chown root:wheel $_cur $_cur,v 10111.31Satatat ;; 10121.31Satatat remove) 10131.31Satatat cp /dev/null $_cur 10141.36Satatat ci -q -f -u -t-"$_msg0" -m"$_msg1" $_cur 10151.36Satatat rcs -q -kb -U $_cur 10161.31Satatat chown root:wheel $_cur $_cur,v 10171.31Satatat rm $_cur 10181.31Satatat ;; 10191.31Satatat esac 10201.31Satatat else 10211.31Satatat case $_action in 10221.31Satatat add|update) 10231.31Satatat if [ -f $_cur ]; then 10241.31Satatat cp -p $_cur $_back 10251.31Satatat fi 10261.31Satatat cp -p $_file $_cur 10271.31Satatat chown root:wheel $_cur 10281.31Satatat ;; 10291.31Satatat remove) 10301.31Satatat mv -f $_cur $_back 10311.31Satatat ;; 10321.31Satatat esac 10331.31Satatat fi 10341.1Scjs} 10351.64Smycroft 10361.76Schristos# 10371.76Schristos# handle_fsck_error fsck_exit_code 10381.76Schristos# Take action depending on the return code from fsck. 10391.76Schristos# 10401.76Schristoshandle_fsck_error() 10411.76Schristos{ 10421.76Schristos case $1 in 10431.76Schristos 0) # OK 10441.76Schristos return 10451.76Schristos ;; 10461.76Schristos 2) # Needs re-run, still fs errors 10471.76Schristos echo "File system still has errors; re-run fsck manually!" 10481.76Schristos ;; 10491.76Schristos 4) # Root modified 10501.76Schristos echo "Root filesystem was modified, rebooting ..." 10511.76Schristos reboot -n 10521.76Schristos echo "Reboot failed; help!" 10531.76Schristos ;; 10541.76Schristos 8) # Check failed 10551.76Schristos echo "Automatic file system check failed; help!" 10561.76Schristos ;; 10571.76Schristos 12) # Got signal 10581.76Schristos echo "Boot interrupted." 10591.76Schristos ;; 10601.76Schristos *) 10611.76Schristos echo "Unknown error $1; help!" 10621.76Schristos ;; 10631.76Schristos esac 10641.76Schristos stop_boot 10651.76Schristos} 10661.76Schristos 10671.78Sapb# 10681.78Sapb# _has_rcorder_keyword word file 10691.78Sapb# Check whether a file contains a "# KEYWORD:" comment with a 10701.78Sapb# specified keyword in the style used by rcorder(8). 10711.78Sapb# 10721.78Sapb_has_rcorder_keyword() 10731.78Sapb{ 10741.78Sapb local word="$1" 10751.78Sapb local file="$2" 10761.78Sapb local line 10771.78Sapb 10781.78Sapb [ -r "$file" ] || return 1 10791.78Sapb while read line; do 10801.78Sapb case "${line} " in 10811.78Sapb "# KEYWORD:"*[\ \ ]"${word}"[\ \ ]*) 10821.78Sapb return 0 10831.78Sapb ;; 10841.78Sapb "#"*) 10851.78Sapb continue 10861.78Sapb ;; 10871.78Sapb *[A-Za-z0-9]*) 10881.78Sapb # give up at the first non-empty non-comment line 10891.78Sapb return 1 10901.78Sapb ;; 10911.78Sapb esac 10921.78Sapb done <"$file" 10931.78Sapb return 1 10941.78Sapb} 10951.78Sapb 10961.78Sapb# 10971.78Sapb# print_rc_metadata string 10981.78Sapb# Print the specified string in such a way that the post-processor 10991.78Sapb# inside /etc/rc will treat it as meta-data. 11001.78Sapb# 11011.78Sapb# If we are not running inside /etc/rc, do nothing. 11021.78Sapb# 11031.78Sapb# For public use by any rc.d script, the string must begin with 11041.78Sapb# "note:", followed by arbitrary text. The intent is that the text 11051.78Sapb# will appear in a log file but not on the console. 11061.78Sapb# 11071.78Sapb# For private use within /etc/rc, the string must contain a 11081.78Sapb# keyword recognised by the rc_postprocess_metadata() function 11091.78Sapb# defined in /etc/rc, followed by a colon, followed by one or more 11101.78Sapb# colon-separated arguments associated with the keyword. 11111.78Sapb# 11121.78Sapbprint_rc_metadata() 11131.78Sapb{ 11141.78Sapb # _rc_postprocessor fd, if defined, is the fd to which we must 11151.78Sapb # print, prefixing the output with $_rc_metadata_prefix. 11161.78Sapb # 11171.78Sapb if [ -n "$_rc_postprocessor_fd" ]; then 11181.88Sapb command printf "%s%s\n" "$rc_metadata_prefix" "$1" \ 11191.78Sapb >&${_rc_postprocessor_fd} 11201.78Sapb fi 11211.78Sapb} 11221.78Sapb 11231.78Sapb# 11241.88Sapb# _flush_rc_output 11251.88Sapb# Arrange for output to be flushed, if we are running 11261.88Sapb# inside /etc/rc with postprocessing. 11271.88Sapb# 11281.88Sapb_flush_rc_output() 11291.88Sapb{ 11301.88Sapb print_rc_metadata "nop" 11311.88Sapb} 11321.88Sapb 11331.88Sapb# 11341.88Sapb# print_rc_normal [-n] string 11351.78Sapb# Print the specified string in such way that it is treated as 11361.78Sapb# normal output, regardless of whether or not we are running 11371.78Sapb# inside /etc/rc with post-processing. 11381.78Sapb# 11391.88Sapb# If "-n" is specified in $1, then the string in $2 is printed 11401.88Sapb# without a newline; otherwise, the string in $1 is printed 11411.88Sapb# with a newline. 11421.88Sapb# 11431.88Sapb# Intended use cases include: 11441.88Sapb# 11451.88Sapb# o An rc.d script can use ``print_rc_normal -n'' to print a 11461.88Sapb# partial line in such a way that it appears immediately 11471.88Sapb# instead of being buffered by rc(8)'s post-processor. 11481.88Sapb# 11491.88Sapb# o An rc.d script that is run via the no_rc_postprocess 11501.88Sapb# function (so most of its output is invisible to rc(8)'s 11511.88Sapb# post-processor) can use print_rc_normal to force some of its 11521.88Sapb# output to be seen by the post-processor. 11531.88Sapb# 11541.78Sapb# 11551.78Sapbprint_rc_normal() 11561.78Sapb{ 11571.78Sapb # If _rc_postprocessor_fd is defined, then it is the fd 11581.88Sapb # to which we must print; otherwise print to stdout. 11591.78Sapb # 11601.88Sapb local fd="${_rc_postprocessor_fd:-1}" 11611.88Sapb case "$1" in 11621.88Sapb "-n") 11631.88Sapb command printf "%s" "$2" >&${fd} 11641.88Sapb _flush_rc_output 11651.88Sapb ;; 11661.88Sapb *) 11671.88Sapb command printf "%s\n" "$1" >&${fd} 11681.88Sapb ;; 11691.88Sapb esac 11701.78Sapb} 11711.78Sapb 11721.78Sapb# 11731.78Sapb# no_rc_postprocess cmd... 11741.78Sapb# Execute the specified command in such a way that its output 11751.78Sapb# bypasses the post-processor that handles the output from 11761.78Sapb# most commands that are run inside /etc/rc. If we are not 11771.78Sapb# inside /etc/rc, then just execute the command without special 11781.78Sapb# treatment. 11791.78Sapb# 11801.78Sapb# The intent is that interactive commands can be run via 11811.78Sapb# no_rc_postprocess(), and their output will apear immediately 11821.78Sapb# on the console instead of being hidden or delayed by the 11831.78Sapb# post-processor. An unfortunate consequence of the output 11841.78Sapb# bypassing the post-processor is that the output will not be 11851.78Sapb# logged. 11861.78Sapb# 11871.78Sapbno_rc_postprocess() 11881.78Sapb{ 11891.78Sapb if [ -n "${_rc_postprocessor_fd}" ]; then 11901.78Sapb "$@" >&${_rc_original_stdout_fd} 2>&${_rc_original_stderr_fd} 11911.78Sapb else 11921.78Sapb "$@" 11931.78Sapb fi 11941.78Sapb} 11951.78Sapb 11961.78Sapb# 11971.78Sapb# twiddle 11981.78Sapb# On each call, print a different one of "/", "-", "\\", "|", 11991.78Sapb# followed by a backspace. The most recently printed value is 12001.78Sapb# saved in $_twiddle_state. 12011.78Sapb# 12021.78Sapb# Output is to /dev/tty, so this function may be useful even inside 12031.78Sapb# a script whose output is redirected. 12041.78Sapb# 12051.78Sapbtwiddle() 12061.78Sapb{ 12071.78Sapb case "$_twiddle_state" in 12081.78Sapb '/') _next='-' ;; 12091.78Sapb '-') _next='\' ;; 12101.78Sapb '\') _next='|' ;; 12111.78Sapb *) _next='/' ;; 12121.78Sapb esac 12131.88Sapb command printf "%s\b" "$_next" >/dev/tty 12141.78Sapb _twiddle_state="$_next" 12151.78Sapb} 12161.78Sapb 12171.82Schristos# 12181.82Schristos# human_exit_code 12191.82Schristos# Print the a human version of the exit code. 12201.82Schristos# 12211.82Schristoshuman_exit_code() 12221.82Schristos{ 12231.83Schristos if [ "$1" -lt 127 ] 12241.82Schristos then 12251.82Schristos echo "exited with code $1" 12261.85Schristos elif [ "$(expr $1 % 256)" -eq 127 ] 12271.82Schristos then 12281.84Schristos # This cannot really happen because the shell will not 12291.84Schristos # pass stopped job status out and the exit code is limited 12301.84Schristos # to 8 bits. This code is here just for completeness. 12311.82Schristos echo "stopped with signal $(expr $1 / 256)" 12321.82Schristos else 12331.82Schristos echo "terminated with signal $(expr $1 - 128)" 12341.82Schristos fi 12351.82Schristos} 12361.86Sapb 12371.86Sapb# 12381.86Sapb# collapse_backslash_newline 12391.86Sapb# Copy input to output, collapsing <backslash><newline> 12401.86Sapb# to nothing, but leaving other backslashes alone. 12411.86Sapb# 12421.86Sapbcollapse_backslash_newline() 12431.86Sapb{ 12441.86Sapb local line 12451.86Sapb while read -r line ; do 12461.86Sapb case "$line" in 12471.86Sapb *\\) 12481.86Sapb # print it, without the backslash or newline 12491.88Sapb command printf "%s" "${line%?}" 12501.86Sapb ;; 12511.86Sapb *) 12521.86Sapb # print it, with a newline 12531.88Sapb command printf "%s\n" "${line}" 12541.86Sapb ;; 12551.86Sapb esac 12561.86Sapb done 12571.86Sapb} 12581.82Schristos 12591.88Sapb# Override the normal "echo" and "printf" commands, so that 12601.88Sapb# partial lines printed by rc.d scripts appear immediately, 12611.88Sapb# instead of being buffered by rc(8)'s post-processor. 12621.88Sapb# 12631.88Sapb# Naive use of the echo or printf commands from rc.d scripts, 12641.88Sapb# elsewhere in rc.subr, or anything else that sources rc.subr, 12651.88Sapb# will call these functions. To call the real echo and printf 12661.88Sapb# commands, use "command echo" or "command printf". 12671.88Sapb# 12681.88Sapbecho() 12691.88Sapb{ 12701.88Sapb command echo "$@" 12711.88Sapb case "$1" in 12721.88Sapb '-n') _flush_rc_output ;; 12731.88Sapb esac 12741.88Sapb} 12751.88Sapbprintf() 12761.88Sapb{ 12771.88Sapb command printf "$@" 12781.88Sapb case "$1" in 12791.88Sapb *'\n') : ;; 12801.88Sapb *) _flush_rc_output ;; 12811.88Sapb esac 12821.88Sapb} 12831.88Sapb 12841.64Smycroft_rc_subr_loaded=: 1285