[Midnightbsd-cvs] src [11629] trunk/etc/rc.subr: merge fixes from freebsd

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Jul 8 12:55:36 EDT 2018


Revision: 11629
          http://svnweb.midnightbsd.org/src/?rev=11629
Author:   laffer1
Date:     2018-07-08 12:55:35 -0400 (Sun, 08 Jul 2018)
Log Message:
-----------
merge fixes from freebsd

Modified Paths:
--------------
    trunk/etc/rc.subr

Property Changed:
----------------
    trunk/etc/rc.subr

Modified: trunk/etc/rc.subr
===================================================================
--- trunk/etc/rc.subr	2018-07-08 16:53:43 UTC (rev 11628)
+++ trunk/etc/rc.subr	2018-07-08 16:55:35 UTC (rev 11629)
@@ -1,5 +1,6 @@
-# $NetBSD: rc.subr,v 1.66 2006/04/01 10:05:50 he Exp $
-# $MidnightBSD: src/etc/rc.subr,v 1.12 2013/01/09 04:48:14 laffer1 Exp $
+# $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $
+# $FreeBSD: stable/10/etc/rc.subr 292450 2015-12-18 19:58:34Z jilles $
+# $MidnightBSD$
 #
 # Copyright (c) 1997-2004 The NetBSD Foundation, Inc.
 # All rights reserved.
@@ -54,6 +55,20 @@
 #	functions
 #	---------
 
+# list_vars pattern
+#	List vars matching pattern.
+# 
+list_vars()
+{
+	set | { while read LINE; do
+		var="${LINE%%=*}"
+		case "$var" in
+		"$LINE"|*[!a-zA-Z0-9_]*) continue ;;
+		$1) echo $var
+		esac
+	done; }
+}
+
 # set_rcvar [var] [defval] [desc]
 #
 #	Echo or define a rc.conf(5) variable name.  Global variable
@@ -70,28 +85,20 @@
 #
 set_rcvar()
 {
+	local _var
+
 	case $# in
-	0)
-		echo ${name}_enable
-		;;
-	1)
-		echo ${1}_enable
-		;;
+	0)	echo ${name}_enable ;;
+	1)	echo ${1}_enable ;;
 	*)
-		debug "rcvar_define: \$$1=$2 is added" \
+		debug "set_rcvar: \$$1=$2 is added" \
 		    " as a rc.conf(5) variable."
-
-		local _var
 		_var=$1
 		rcvars="${rcvars# } $_var"
 		eval ${_var}_defval=\"$2\"
 		shift 2
-		# encode multiple lines of _desc
-		for l in "$@"; do
-			eval ${_var}_desc=\"\${${_var}_desc#^^}^^$l\"
-		done
-		eval ${_var}_desc=\"\${${_var}_desc#^^}\"
-		;;
+		eval ${_var}_desc=\"$*\"
+	;;
 	esac
 }
 
@@ -103,7 +110,7 @@
 {
 	local _var
 	_var=$1
-	debug "rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
+	debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined"
 
 	rcvars_obsolete="${rcvars_obsolete# } $1"
 	eval ${1}_newvar=\"$2\"
@@ -355,7 +362,247 @@
 	eval $_proccheck
 }
 
+# sort_lite [-b] [-n] [-k POS] [-t SEP]
+#	A lite version of sort(1) (supporting a few options) that can be used
+#	before the real sort(1) is available (e.g., in scripts that run prior
+#	to mountcritremote). Requires only shell built-in functionality.
 #
+sort_lite()
+{
+	local funcname=sort_lite
+	local sort_sep="$IFS" sort_ignore_leading_space=
+	local sort_field=0 sort_strict_fields= sort_numeric=
+	local nitems=0 skip_leading=0 trim=
+
+	local OPTIND flag
+	while getopts bnk:t: flag; do
+		case "$flag" in
+		b) sort_ignore_leading_space=1 ;;
+		n) sort_numeric=1 sort_ignore_leading_space=1 ;;
+		k) sort_field="${OPTARG%%,*}" ;; # only up to first comma
+			# NB: Unlike sort(1) only one POS allowed
+		t) sort_sep="$OPTARG"
+		   if [ ${#sort_sep} -gt 1 ]; then
+		   	echo "$funcname: multi-character tab \`$sort_sep'" >&2
+		   	return 1
+		   fi
+		   sort_strict_fields=1
+		   ;;
+		\?) return 1 ;;
+		esac
+	done
+	shift $(( $OPTIND - 1 ))
+
+	# Create transformation pattern to trim leading text if desired
+	case "$sort_field" in
+	""|[!0-9]*|*[!0-9.]*)
+		echo "$funcname: invalid sort field \`$sort_field'" >&2
+		return 1
+		;;
+	*.*)
+		skip_leading=${sort_field#*.} sort_field=${sort_field%%.*}
+		while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do
+			trim="$trim?" skip_leading=$(( $skip_leading - 1 ))
+		done
+	esac
+
+	# Copy input to series of local numbered variables
+	# NB: IFS of NULL preserves leading whitespace
+	local LINE
+	while IFS= read -r LINE || [ "$LINE" ]; do
+		nitems=$(( $nitems + 1 ))
+		local src_$nitems="$LINE"
+	done
+
+	#
+	# Sort numbered locals using insertion sort
+	#
+	local curitem curitem_orig curitem_mod curitem_haskey
+	local dest dest_orig dest_mod dest_haskey
+	local d gt n
+	local i=1 
+	while [ $i -le $nitems ]; do
+		curitem_haskey=1 # Assume sort field (-k POS) exists
+		eval curitem=\"\$src_$i\"
+		curitem_mod="$curitem" # for modified comparison
+		curitem_orig="$curitem" # for original comparison
+
+		# Trim leading whitespace if desired
+		if [ "$sort_ignore_leading_space" ]; then
+			while case "$curitem_orig" in
+				[$IFS]*) : ;; *) false; esac
+			do
+				curitem_orig="${curitem_orig#?}"
+			done
+			curitem_mod="$curitem_orig"
+		fi
+
+		# Shift modified comparison value if sort field (-k POS) is > 1
+		n=$sort_field
+		while [ $n -gt 1 ]; do
+			case "$curitem_mod" in
+			*[$sort_sep]*)
+				# Cut text up-to (and incl.) first separator
+				curitem_mod="${curitem_mod#*[$sort_sep]}"
+
+				# Skip NULLs unless strict field splitting
+				[ "$sort_strict_fields" ] ||
+					[ "${curitem_mod%%[$sort_sep]*}" ] ||
+					[ $n -eq 2 ] ||
+					continue
+				;;
+			*)
+				# Asked for a field that doesn't exist
+				curitem_haskey= break
+			esac
+			n=$(( $n - 1 ))
+		done
+
+		# Trim trailing words if sort field >= 1
+		[ $sort_field -ge 1 -a "$sort_numeric" ] &&
+			curitem_mod="${curitem_mod%%[$sort_sep]*}"
+
+		# Apply optional trim (-k POS.TRIM) to cut leading characters
+		curitem_mod="${curitem_mod#$trim}"
+
+		# Determine the type of modified comparison to use initially
+		# NB: Prefer numerical if requested but fallback to standard
+		case "$curitem_mod" in
+		""|[!0-9]*) # NULL or begins with non-number
+			gt=">"
+			[ "$sort_numeric" ] && curitem_mod=0
+			;;
+		*)
+			if [ "$sort_numeric" ]; then
+				gt="-gt"
+				curitem_mod="${curitem_mod%%[!0-9]*}"
+					# NB: trailing non-digits removed
+					# otherwise numeric comparison fails
+			else
+				gt=">"
+			fi
+		esac
+
+		# If first time through, short-circuit below position-search
+		if [ $i -le 1 ]; then
+			d=0
+		else
+			d=1
+		fi
+
+		#
+		# Find appropriate element position
+		#
+		while [ $d -gt 0 ]
+		do
+			dest_haskey=$curitem_haskey
+			eval dest=\"\$dest_$d\"
+			dest_mod="$dest" # for modified comparison
+			dest_orig="$dest" # for original comparison
+
+			# Trim leading whitespace if desired
+			if [ "$sort_ignore_leading_space" ]; then
+				while case "$dest_orig" in
+					[$IFS]*) : ;; *) false; esac
+				do
+					dest_orig="${dest_orig#?}"
+				done
+				dest_mod="$dest_orig"
+			fi
+
+			# Shift modified value if sort field (-k POS) is > 1
+			n=$sort_field
+			while [ $n -gt 1 ]; do
+				case "$dest_mod" in
+				*[$sort_sep]*)
+					# Cut text up-to (and incl.) 1st sep
+					dest_mod="${dest_mod#*[$sort_sep]}"
+
+					# Skip NULLs unless strict fields
+					[ "$sort_strict_fields" ] ||
+					    [ "${dest_mod%%[$sort_sep]*}" ] ||
+					    [ $n -eq 2 ] ||
+					    continue
+					;;
+				*)
+					# Asked for a field that doesn't exist
+					dest_haskey= break
+				esac
+				n=$(( $n - 1 ))
+			done
+
+			# Trim trailing words if sort field >= 1
+			[ $sort_field -ge 1 -a "$sort_numeric" ] &&
+				dest_mod="${dest_mod%%[$sort_sep]*}"
+
+			# Apply optional trim (-k POS.TRIM), cut leading chars
+			dest_mod="${dest_mod#$trim}"
+
+			# Determine type of modified comparison to use
+			# NB: Prefer numerical if requested, fallback to std
+			case "$dest_mod" in
+			""|[!0-9]*) # NULL or begins with non-number
+				gt=">"
+				[ "$sort_numeric" ] && dest_mod=0
+				;;
+			*)
+				if [ "$sort_numeric" ]; then
+					gt="-gt"
+					dest_mod="${dest_mod%%[!0-9]*}"
+						# NB: kill trailing non-digits
+						# for numeric comparison safety
+				else
+					gt=">"
+				fi
+			esac
+
+			# Break if we've found the proper element position
+			if [ "$curitem_haskey" -a "$dest_haskey" ]; then
+				if [ "$dest_mod" = "$curitem_mod" ]; then
+					[ "$dest_orig" ">" "$curitem_orig" ] &&
+						break
+				elif [ "$dest_mod" $gt "$curitem_mod" ] \
+					2> /dev/null
+				then
+					break
+				fi
+			else
+				[ "$dest_orig" ">" "$curitem_orig" ] && break
+			fi
+
+			# Break if we've hit the end
+			[ $d -ge $i ] && break
+
+			d=$(( $d + 1 ))
+		done
+
+		# Shift remaining positions forward, making room for new item
+		n=$i
+		while [ $n -ge $d ]; do
+			# Shift destination item forward one placement
+			eval dest_$(( $n + 1 ))=\"\$dest_$n\"
+			n=$(( $n - 1 ))
+		done
+
+		# Place the element
+		if [ $i -eq 1 ]; then
+			local dest_1="$curitem"
+		else
+			local dest_$d="$curitem"
+		fi
+
+		i=$(( $i + 1 ))
+	done
+
+	# Print sorted results
+	d=1
+	while [ $d -le $nitems ]; do
+		eval echo \"\$dest_$d\"
+		d=$(( $d + 1 ))
+	done
+}
+
+#
 # wait_for_pids pid [pid ...]
 #	spins until none of the pids exist
 #
@@ -503,6 +750,8 @@
 #				NOTE:	$flags from the parent environment
 #					can be used to override this.
 #
+#	${name}_env	n	Environment variables to run ${command} with.
+#
 #	${name}_fib	n	Routing table number to run ${command} with.
 #
 #	${name}_nice	n	Nice level to run ${command} at.
@@ -518,6 +767,8 @@
 #				to run the chrooted ${command} with.
 #				Requires /usr to be mounted.
 #
+#	${name}_prepend	n	Command added before ${command}.
+#
 #	${rc_arg}_cmd	n	If set, use this as the method when invoked;
 #				Otherwise, use default command (see below)
 #
@@ -587,6 +838,8 @@
 #
 #	rcvar		Display what rc.conf variable is used (if any).
 #
+#	enabled		Return true if the service is enabled.
+#
 #	Variables available to methods, and after run_rc_command() has
 #	completed:
 #
@@ -655,7 +908,7 @@
 	eval _override_command=\$${name}_program
 	command=${_override_command:-$command}
 
-	_keywords="start stop restart rcvar $extra_commands"
+	_keywords="start stop restart rcvar enabled $extra_commands"
 	rc_pid=
 	_pidcmd=
 	_procname=${procname:-${command}}
@@ -676,6 +929,11 @@
 		rc_usage $_keywords
 	fi
 
+	if [ "$rc_arg" = "enabled" ] ; then
+		checkyesno ${rcvar}
+		return $?
+	fi
+
 	if [ -n "$flags" ]; then	# allow override from environment
 		rc_flags=$flags
 	else
@@ -684,7 +942,8 @@
 	eval _chdir=\$${name}_chdir	_chroot=\$${name}_chroot \
 	    _nice=\$${name}_nice	_user=\$${name}_user \
 	    _group=\$${name}_group	_groups=\$${name}_groups \
-	    _fib=\$${name}_fib
+	    _fib=\$${name}_fib		_env=\$${name}_env \
+	    _prepend=\$${name}_prepend
 
 	if [ -n "$_user" ]; then	# unset $_user if running as that user
 		if [ "$_user" = "$(eval $IDCMD)" ]; then
@@ -716,6 +975,14 @@
 			fi
 		fi
 
+		if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then
+			if [ -z "$rc_quiet" ]; then
+				echo 1>&2 "${name} already running? " \
+				    "(pid=$rc_pid)."
+			fi
+			return 1
+		fi
+
 					# if there's a custom ${XXX_cmd},
 					# run that instead of the default
 					#
@@ -744,14 +1011,6 @@
 			;;
 
 		start)
-			if [ -z "$rc_fast" -a -n "$rc_pid" ]; then
-				if [ -z "$rc_quiet" ]; then
-					echo 1>&2 "${name} already running? " \
-					    "(pid=$rc_pid)."
-				fi
-				return 1
-			fi
-
 			if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then
 				warn "run_rc_command: cannot run $command"
 				return 1
@@ -769,6 +1028,7 @@
 				_doit="\
 ${_nice:+nice -n $_nice }\
 ${_fib:+setfib -F $_fib }\
+${_env:+env $_env }\
 chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\
 $_chroot $command $rc_flags $command_args"
 			else
@@ -775,6 +1035,7 @@
 				_doit="\
 ${_chdir:+cd $_chdir && }\
 ${_fib:+setfib -F $_fib }\
+${_env:+env $_env }\
 $command $rc_flags $command_args"
 				if [ -n "$_user" ]; then
 				    _doit="su -m $_user -c 'sh -c \"$_doit\"'"
@@ -785,6 +1046,9 @@
 					fi
 					_doit="nice -n $_nice $_doit"
 				fi
+				if [ -n "$_prepend" ]; then
+					_doit="$_prepend $_doit"
+				fi
 			fi
 
 					# run the full command
@@ -1045,16 +1309,15 @@
 }
 
 #
-# load_rc_config name
-#	Source in the configuration file for a given name.
+# load_rc_config [service]
+#	Source in the configuration file(s) for a given service.
+#	If no service is specified, only the global configuration
+#	file(s) will be loaded.
 #
 load_rc_config()
 {
-	local _name _var _defval _v _msg _new
+	local _name _rcvar_val _var _defval _v _msg _new _d
 	_name=$1
-	if [ -z "$_name" ]; then
-		err 3 'USAGE: load_rc_config name'
-	fi
 
 	if ${_rc_conf_loaded:-false}; then
 		:
@@ -1069,9 +1332,25 @@
 		fi
 		_rc_conf_loaded=true
 	fi
-	if [ -f /etc/rc.conf.d/"$_name" ]; then
-		debug "Sourcing /etc/rc.conf.d/${_name}"
-		. /etc/rc.conf.d/"$_name"
+
+	# If a service name was specified, attempt to load
+	# service-specific configuration
+	if [ -n "$_name" ] ; then
+		for _d in /etc ${local_startup}; do
+			_d=${_d%/rc.d}
+			if [ -f ${_d}/rc.conf.d/"$_name" ]; then
+				debug "Sourcing ${_d}/rc.conf.d/$_name"
+				. ${_d}/rc.conf.d/"$_name"
+			elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then
+				local _rc
+				for _rc in ${_d}/rc.conf.d/"$_name"/* ; do
+					if [ -f "$_rc" ] ; then
+						debug "Sourcing $_rc"
+						. "$_rc"
+					fi
+				done
+			fi
+		done
 	fi
 
 	# Set defaults if defined.
@@ -1294,7 +1573,7 @@
 #	Make a symbolic link 'link' to src from basedir. If the
 #	directory in which link is to be created does not exist
 #	a warning will be displayed and an error will be returned.
-#	Returns 0 on sucess, 1 otherwise.
+#	Returns 0 on success, 1 otherwise.
 #
 make_symlink()
 {
@@ -1497,28 +1776,6 @@
 	return 0
 }
 
-# devfs_mount_jail dir [ruleset]
-#	Mounts a devfs file system appropriate for jails
-#	on the directory dir. If ruleset is specified, the ruleset
-#	it names will be used instead.  If present, ruleset must
-#	be the name of a ruleset as defined in a devfs.rules(5) file.
-#	This function returns non-zero if an error occurs.
-#
-devfs_mount_jail()
-{
-	local jdev rs _me
-	jdev="$1"
-	[ -n "$2" ] && rs=$2 || rs="devfsrules_jail"
-	_me="devfs_mount_jail"
-
-	devfs_init_rulesets
-	if ! devfs_domount "$jdev" $rs; then
-		warn "$_me: devfs was not mounted on $jdev"
-		return 1
-	fi
-	return 0
-}
-
 # Provide a function for normalizing the mounting of memory
 # filesystems.  This should allow the rest of the code here to remain
 # as close as possible between 5-current and 4-stable.
@@ -1580,19 +1837,20 @@
 	return 0
 }
 
-# ltr str src dst
+# ltr str src dst [var]
 #	Change every $src in $str to $dst.
 #	Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor
-#	awk(1).
+#	awk(1). If var is non-NULL, set it to the result.
 ltr()
 {
-	local _str _src _dst _out _com
-	_str=$1
-	_src=$2
-	_dst=$3
+	local _str _src _dst _out _com _var
+	_str="$1"
+	_src="$2"
+	_dst="$3"
+	_var="$4"
 	_out=""
 
-	IFS=${_src}
+	local IFS="${_src}"
 	for _com in ${_str}; do
 		if [ -z "${_out}" ]; then
 			_out="${_com}"
@@ -1600,7 +1858,11 @@
 			_out="${_out}${_dst}${_com}"
 		fi
 	done
-	echo "${_out}"
+	if [ -n "${_var}" ]; then
+		setvar "${_var}" "${_out}"
+	else
+		echo "${_out}"
+	fi
 }
 
 # Creates a list of providers for GELI encryption.
@@ -1648,7 +1910,7 @@
 
 # Find scripts in local_startup directories that use the old syntax
 #
-find_local_scripts_old () {
+find_local_scripts_old() {
 	zlist=''
 	slist=''
 	for dir in ${local_startup}; do
@@ -1658,7 +1920,7 @@
 				    continue
 				zlist="$zlist $file"
 			done
-			for file in ${dir}/[^0-9]*.sh; do
+			for file in ${dir}/[!0-9]*.sh; do
 				grep '^# PROVIDE:' $file >/dev/null 2>&1 &&
 				    continue
 				slist="$slist $file"
@@ -1667,7 +1929,7 @@
 	done
 }
 
-find_local_scripts_new () {
+find_local_scripts_new() {
 	local_rc=''
 	for dir in ${local_startup}; do
 		if [ -d "${dir}" ]; then
@@ -1752,6 +2014,22 @@
 	return 0
 }
 
+# check_jail mib
+#	Return true if security.jail.$mib exists and set to 1.
+
+check_jail()
+{
+	local _mib _v
+
+	_mib=$1
+	if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then
+		case $_v in
+		1)	return 0;;
+		esac
+	fi
+	return 1
+}
+
 # check_kern_features mib
 #	Return existence of kern.features.* sysctl MIB as true or
 #	false.  The result will be cached in $_rc_cache_kern_features_
@@ -1777,7 +2055,7 @@
 # check_namevarlist var
 #	Return "0" if ${name}_var is reserved in rc.subr.
 
-_rc_namevarlist="program chroot chdir flags fib nice user group groups"
+_rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend"
 check_namevarlist()
 {
 	local _v


Property changes on: trunk/etc/rc.subr
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property


More information about the Midnightbsd-cvs mailing list