#!/bin/sh
########################################################################
# Begin $rc_base/init.d/udev
#
# Description : Udev cold-plugging script
#
# Authors     : Zack Winkles
#
# Version     : 00.01
#
# Notes       :
#
########################################################################

. /etc/sysconfig/rc
. ${rc_functions}

# Create some things that sysfs does not, and should not export for us.  Feel
# free to add devices to this list.
make_extra_nodes() {
	ln -s /proc/self/fd /dev/fd
	ln -s /proc/self/fd/0 /dev/stdin
	ln -s /proc/self/fd/1 /dev/stdout
	ln -s /proc/self/fd/2 /dev/stderr
	ln -s /proc/kcore /dev/core
	mkdir /dev/pts
	mkdir /dev/shm
}

case "${1}" in
	start)
		boot_mesg "Populating /dev with device nodes..."
		if ! grep -q '[[:space:]]sysfs' /proc/mounts; then
			boot_log " The SysFS filesystem could not be found.  Unable to continue."
			echo_failure
			boot_mesg -n "FAILURE:\n\nUnable to create" ${FAILURE}
			boot_mesg -n " devices without a SysFS filesystem"
			boot_mesg -n "\n\nAfter you press Enter, this system"
			boot_mesg -n " will be halted and powered off."
			boot_mesg -n "\n\nPress Enter to continue..." ${INFO}
			boot_mesg "" ${NORMAL}
			read ENTER
			/etc/rc.d/init.d/halt stop
		fi

		# Mount a temporary file system over /dev, so that any devices
		# made or removed during this boot don't affect the next one.
		# The reason we don't write to mtab is because we don't ever
		# want /dev to be unavailable (such as by `umount -a').
		mount -n -t tmpfs tmpfs /dev -o mode=755
		if [ ${?} != 0 ]; then
			boot_log "Unable to mount a tmpfs onto /dev."
			echo_failure
			boot_mesg -n "FAILURE:\n\nCannot mount a tmpfs" ${FAILURE}
			boot_mesg -n " onto /dev, this system will be halted."
			boot_mesg -n "\n\nAfter you press Enter, this system"
			boot_mesg -n " will be halted and powered off."
			boot_mesg -n "\n\nPress Enter to continue..." ${INFO}
			boot_mesg "" ${NORMAL}
			read ENTER
			/etc/rc.d/init.d/halt stop
		fi

		# Assign udevsend to get hotplug events. udevsend can manage the whole
		# hotplug handling by taking over the kernel spawned event process
		echo /sbin/udevsend > /proc/sys/kernel/hotplug

		# Populate /dev with all the devices that are already available,
		# and save it's status so we can report failures.
		udevstart || failed=1

		# Now, create some required files/directories/devices that sysfs
		# doesn't export for us.
		make_extra_nodes

		# When reporting the status, base it on the success or failure
		# of the `udevstart' command, since that's the most important.
		(exit ${failed})
		evaluate_retval
		;;

	*)
		echo "Usage ${0} {start}"
		exit 1
		;;
esac

# End $rc_base/init.d/udev
