From 075e80b358b3ea10c9a66caf6dd38c4ddc89f82c Mon Sep 17 00:00:00 2001
From: Roy Marples <roy@marples.name>
Date: Wed, 25 Jul 2007 20:58:23 +0000
Subject: [PATCH] Merge some of reb's OpenBSD fixes in

---
 init.d/bootmisc     |  9 ++++----
 init.d/checkroot    |  7 +++++--
 init.d/localmount   | 22 +++++++++++--------
 net.BSD/ifconfig.sh | 51 ++++++++++++++++++++++++++++++++++++---------
 sh.BSD/init.sh      |  2 +-
 src/mountinfo.c     |  7 +++++--
 src/rc.c            |  2 ++
 7 files changed, 72 insertions(+), 28 deletions(-)

diff --git a/init.d/bootmisc b/init.d/bootmisc
index 6be844e1..f0508f59 100755
--- a/init.d/bootmisc
+++ b/init.d/bootmisc
@@ -53,7 +53,8 @@ start() {
 	find /var/lock -type f -print0 | xargs -0 rm -f --
 		
 	# Clean up /var/run and create /var/run/utmp so we can login.
-	for x in $(find /var/run ! -type d ! -name utmp ! -name innd.pid ! -name random-seed ! -name ld-elf.so.hints); do
+	for x in $(find /var/run ! -type d ! -name utmp ! -name innd.pid \
+		! -name random-seed ! -name ld-elf.so.hints ! -name ld.so.hints); do
 		[ ! -f "${x}" ] && continue
 		# Do not remove pidfiles of already running daemons
 		case "${x}" in
@@ -66,9 +67,9 @@ start() {
 	done
 
 	# Reset pam_console permissions if we are actually using it
-	if [ -x /sbin/pam_console_apply -a ! -c /dev/.devfsd ] ; then
-		if [ -n $(grep -v -e '^[[:space:]]*#' /etc/pam.d/* | grep 'pam_console') ] ; then
-			/sbin/pam_console_apply -r
+	if [ -x /sbin/pam_console_apply ] && ! [ -c /dev/.devfsd ] ; then
+		if grep -v -e '^[[:space:]]*#' /etc/pam.d/* | grep -q 'pam_console.so' ; then
+			pam_console_apply -r
 		fi
 	fi
 
diff --git a/init.d/checkroot b/init.d/checkroot
index a35d4586..72a59388 100755
--- a/init.d/checkroot
+++ b/init.d/checkroot
@@ -32,8 +32,11 @@ do_mtab() {
 }
 
 do_fsck() {
-	local retval=0 opts="-F"
-	[ "${RC_UNAME}" = "Linux" ] && opts="-T -C0"
+	local retval=0 opts=
+	case "${RC_UNAME}" in
+		FreeBSD) opts="-F";;
+		Linux) opts="-T -C0";;
+	esac
 
 	# Don't bother doing a fsck on these
 	if [ -n "${CDBOOT}" ] || is_net_fs / || is_union_fs / ; then
diff --git a/init.d/localmount b/init.d/localmount
index ff841a2e..7e20c3df 100755
--- a/init.d/localmount
+++ b/init.d/localmount
@@ -21,9 +21,9 @@ start() {
 	mount -at "${types}"
 	eend $? "Some local filesystem failed to mount"
 
-	# Change the mount options of already mounted paritions
+	# Change the mount options of already mounted partitions
 	# This is needed when /usr is separate and coming back from single user
-	if [ "${RC_UNAME}" != "Linux" ] ; then
+	if [ "${RC_UNAME}" = "FreeBSD" ] ; then
 		mount -uao fstab -t "${types},linprocfs"
 	fi
 
@@ -34,13 +34,17 @@ start() {
 			chmod 700 "${dumpdir}"
 		fi
 
-		# Don't quote ${KERNEL_DUMP_DEVICE}, so that if it's unset, savecore
-		# will check on the partitions listed in fstab without errors in the
-		# output
-		if savecore -C "${dumpdir}" ${KERNEL_DUMP_DEVICE} >/dev/null ; then
-			local savecoreopts="${dumpdir} ${KERNEL_DUMP_DEVICE}"
-			[ "${KERNEL_DUMP_COMPRESS}" = "yes" ] \
-				&& savecoreopts="-z ${savecoreopts}"
+		if [ "${RC_UNAME}" = "FreeBSD" ] ; then
+			# Don't quote ${KERNEL_DUMP_DEVICE}, so that if it's unset,
+			# savecore will check on the partitions listed in fstab
+			# without errors in the output
+			savecore -C "${dumpdir}" ${KERNEL_DUMP_DEVICE} >/dev/null
+		else
+			ls "${dumpdir}"/bsd* > /dev/null 2>&1
+		fi
+		if [ $? = 0 ] ; then
+			local sopts="${dumpdir} ${KERNEL_DUMP_DEVICE}"
+			[ "${KERNEL_DUMP_COMPRESS}" = "yes" ] && sopts="-z ${sopts}"
 			ebegin "Saving kernel core dump in" "${dumpdir}"
 			savecore ${savecoreopts} >/dev/null
 			eend $?
diff --git a/net.BSD/ifconfig.sh b/net.BSD/ifconfig.sh
index 68a3875f..06be70cf 100644
--- a/net.BSD/ifconfig.sh
+++ b/net.BSD/ifconfig.sh
@@ -7,7 +7,13 @@ ifconfig_depend() {
 }
 
 _exists() {
-	[ -e /dev/net/"${IFACE}" ]
+	# Only FreeBSD sees to have /dev/net .... is there something
+	# other than ifconfig we can use for the others?
+	if [ -d /dev/net ] ; then
+		[ -e /dev/net/"${IFACE}" ]
+	else
+		ifconfig "${IFACE}" >/dev/null 2>&1
+	fi
 }
 
 _get_mac_address() {
@@ -33,13 +39,26 @@ _down () {
 }
 
 _ifindex() {
-	local x=
-	for x in /dev/net[0-9]* ; do
-		if [ "${x}" -ef /dev/net/"${IFACE}" ] ; then
-			echo "${x#/dev/net}"
-			return 0
-		fi
-	done
+	local x= i=1
+	case "${RC_UNAME}" in
+		FreeBSD|DragonFly)
+			for x in /dev/net[0-9]* ; do
+				if [ "${x}" -ef /dev/net/"${IFACE}" ] ; then
+					echo "${x#/dev/net}"
+					return 0
+				fi
+			done
+			;;
+		default)
+			for x in $(ifconfig -a | sed -n -e 's/^\([^[:space:]]*\):.*/\1/p') ; do
+				if [ "${x}" = "${IFACE}" ] ; then
+					echo "${i}"
+					return 0
+				fi
+				i=$((${i} + 1))
+			done
+			;;
+	esac
 	return 1
 }
 
@@ -63,9 +82,21 @@ _add_address() {
 		set -- "$@" metric ${metric}
 	fi
 
+	# ifconfig doesn't like CIDR addresses
+	case "${RC_UNAME}" in
+		NetBSD|OpenBSD)
+			local ip="${1%%/*}" cidr="${1##*/}" netmask=
+			if [ -n "${cidr}" -a "${cidr}" != "${ip}" ]; then
+				netmask="$(_cidr2netmask "${cidr}")"
+				shift
+				set -- "${ip}" netmask "${netmask}" "$@"
+			fi
+			;;
+	esac
+
 	case "$@" in
-		*:*) ifconfig "${IFACE}" inet6 add "$@" ;;
-		*)   ifconfig "${IFACE}"       add "$@" ;;
+		*:*) ifconfig "${IFACE}" inet6 "$@" ;;
+		*)   ifconfig "${IFACE}"       "$@" ;;
 	esac
 }
 
diff --git a/sh.BSD/init.sh b/sh.BSD/init.sh
index 63f26927..3da44a33 100755
--- a/sh.BSD/init.sh
+++ b/sh.BSD/init.sh
@@ -40,7 +40,7 @@ mount_svcdir() {
 . "${RC_LIBDIR}"/sh/functions.sh
 
 # Disable devd until we need it
-sysctl hw.bus.devctl_disable=1 >/dev/null
+[ "${RC_UNAME}" = "FreeBSD" ] && sysctl hw.bus.devctl_disable=1 >/dev/null
 
 . "${RC_LIBDIR}"/sh/init-common-post.sh
 
diff --git a/src/mountinfo.c b/src/mountinfo.c
index 5497875b..77fe9111 100644
--- a/src/mountinfo.c
+++ b/src/mountinfo.c
@@ -7,8 +7,11 @@
 
 #define APPLET "mountinfo"
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || \
+	defined(__NetBSD__) || defined(__OpenBSD__)
+#define BSD
+
 #include <sys/types.h>
-#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
 #include <sys/param.h>
 #include <sys/ucred.h>
 #include <sys/mount.h>
@@ -30,7 +33,7 @@
 #include "strlist.h"
 
 
-#if defined(__FreeBSD__) || defined(__NetBSD__) || defined (__OpenBSD__)
+#ifdef BSD 
 static char **find_mounts (regex_t *node_regex, regex_t *skip_node_regex,
 						   regex_t *fstype_regex, regex_t *skip_fstype_regex,
 						   char **mounts, bool node, bool fstype)
diff --git a/src/rc.c b/src/rc.c
index 984ac19c..9c9423cc 100644
--- a/src/rc.c
+++ b/src/rc.c
@@ -990,6 +990,7 @@ int main (int argc, char **argv)
 		 strcmp (runlevel, RC_LEVEL_SYSINIT) == 0) &&
 		rc_is_env ("RC_COLDPLUG", "yes"))
 	{
+#if defined(__DragonFly__) || defined(__FreeBSD__)
 		/* The net interfaces are easy - they're all in net /dev/net :) */
 		start_services = rc_ls_dir (NULL, "/dev/net", 0);
 		STRLIST_FOREACH (start_services, service, i) {
@@ -1001,6 +1002,7 @@ int main (int argc, char **argv)
 			CHAR_FREE (tmp);
 		}
 		rc_strlist_free (start_services);
+#endif
 
 		/* The mice are a little more tricky.
 		   If we coldplug anything else, we'll probably do it here. */