#!/bin/sh

udev_root="/dev"
udev_rules="/lib/udev/rules.d"
udev_log="err"

make_extra_nodes () {
	# there are a few things that sysfs does not export for us.
	# these things go here (and remember to remove them in
	# remove_extra_nodes()
	#
	# Thanks to Gentoo for the initial list of these.
	ln -snf /proc/self/fd /dev/fd
	ln -snf /proc/self/fd/0 /dev/stdin
	ln -snf /proc/self/fd/1 /dev/stdout
	ln -snf /proc/self/fd/2 /dev/stderr
	ln -snf /proc/kcore /dev/core

	mkdir /dev/pts
	mkdir /dev/shm

	mknod /dev/null c 1 3
	mknod /dev/console c 5 1
	mknod /dev/zero c 1 5
}

case $1 in
start)
	echo "starting udev"

	# don't use udev if sysfs is not mounted.
	if [ ! -d /sys/kernel ]; then
		echo "failed"
		echo "error: sysfs not mounted"
		exit 1
	fi

	# The reason we don't write to mtab is because we don't ever
	# want /dev to be unavailable (such as by `umount -a').
	echo "mounting tmpfs at $udev_root"
	mount -n -t tmpfs tmpfs $udev_root -o mode=755

	# udev handles uevents itself, so we don't need to have
	# the kernel call out to any binary in response to them
	echo > /proc/sys/kernel/hotplug

	echo "creating static nodes"
	make_extra_nodes

	# Start the udev daemon to continually
	# watch for, and act on, uevents
	echo -n "starting udevd..."
	/sbin/udevd --daemon
	if [ "$?" = "0" ]; then
		echo "done"
	else
		echo "failed"
	fi

	# Now traverse sys/ in order to "coldplug"
	# devices that have already been discovered
	/sbin/udevadm trigger
	# Now wait for udevd to process
	# the uevents we triggered
	echo -n "waiting for devices..."
	/sbin/udevadm settle --timeout=10
	if [ "$?" = "0" ]; then
		echo "done"
	else
		echo "failed"
	fi

	# We can only mount /dev/pts after initialising udev
	if [ -d "/dev/pts" ]; then
		mount /dev/pts
	fi

	exit 0

	;;
stop)
	echo "stopping udevd... error, udevd cannot be stopped, aborting"
	;;
*)
	echo "usage: $0 [start|stop]"
	;;
esac

