#! /bin/sh
# $Id: udev.boot.udev 265 2005-03-21 11:42:55Z hare $
# Copyright (c) 2004 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# /etc/init.d/boot.udev

### BEGIN INIT INFO
# Provides:          boot.udev
# Required-Start:    boot.rootfsck
# Default-Start:     B 2 3 5
# Default-Stop:      
# Short-Description: recreate /dev contents and udev device node database
# Description:       This script creates a /dev tree if /dev is
# mounted as a tmpfs. In comparison to the old /dev/ with devices
# populated by the devs RPM this script will only create devices that
# are actually present in the kernel at boot time. The hotplugging
# will later fill in new device entries if devices are attached and
# remove them if devices are detached.
### END INIT INFO
# 

. /etc/rc.status
. /etc/udev/udev.conf
. /etc/sysconfig/hotplug

if [ -n "$HOTPLUG_MAX_EVENTS" ]; then
    export UDEVD_MAX_CHILDS="$HOTPLUG_MAX_EVENTS"
    export UDEVD_THROTTLE=0
fi

export UDEVD_PIDFILE=${udev_db}/udevd.pid

case "$1" in
    start)
	# Delete invalid PID files
	if [ -f ${UDEVD_PIDFILE} ]; then
	    read udevd_pid < ${UDEVD_PIDFILE}
	    if [ "$udevd_pid" = "0" ]; then
		rm ${UDEVD_PIDFILE}
	    fi
	fi
	checkproc -p ${UDEVD_PIDFILE} /sbin/udevd
	if [ $? -eq 0 ]; then
		if [ -z "$RUNLEVEL" ] ; then
			echo -n "Starting udevd (already running)"
			rc_status -v
		fi
		rc_exit
	fi
	if [ -x /sbin/udev -a -x /sbin/udevstart ] ; then
		dev=
		while read n m t r ; do
			if [ "$m" = "/dev" -a "$t" = "tmpfs" ] ; then
				dev=tmpfs
			fi
		done < /proc/self/mounts
		# check for /sys
		if [ ! -d /sys/class ]; then
		    mount -t sysfs none /sys
		fi
		# if [ "$dev" = "tmpfs" ] ; then
		#  	echo -n "Recreating udev db in the background ... "
		# 	# udevdb format in initramfs and installed system may differ
		# 	rm -rf "$udev_db"
		# else
		# 	echo -n "Creating device nodes "
		# 	# Remove stale device links
		# 	rm -rf $udev_db /dev/disk /dev/by-id /dev/by-path
		# fi
		echo -n "Starting udevd "
		[ -d $udev_db ] || mkdir $udev_db
		# Create permissions device
		[ -n "$udev_devperms" ] && touch $udev_devperms
		#
		# Check whether we can use uevent messages
		#
		if zcat /proc/config.gz | grep -q KOBJECT_UEVENT ; then
		    # Yes, just use udevd
		    export UDEVD_EXPECTED_SEQNUM=$(cat /sys/kernel/hotplug_seqnum)
		    export UDEVD_EVENT_TIMEOUT=1
		    startproc -p $UDEVD_PIDFILE /sbin/udevd -d
		    echo "" > /proc/sys/kernel/hotplug
		else
		    # No, use the udevsend/udevd combo
		    echo "/sbin/udevsend" > /proc/sys/kernel/hotplug
		fi
		# Remember status and be verbose
		rc_status $status -v
	fi
    
	;;
    force-reload)
	if [ -x /sbin/udev -a -x /sbin/udevstart ] ; then
		echo -n "recreate all available device nodes in /dev: "
		rm -rf $udev_db /dev/disk
		export UDEV_NO_DEVD=1
		export UDEV_NO_HOTPLUGD=1
		/sbin/udevstart &
		unset UDEV_NO_HOTPLUGD
		unset UDEV_NO_DEVD
		rc_status -v
	fi
	;;
    stop)
	if [ -n "$PREVLEVEL" ]; then
		case "$PREVLEVEL" in
		2|3|5)
			rc_exit
			;;
		esac
	fi	
	echo -n "Stopping udevd:"
	echo "/sbin/udevsend" > /proc/sys/kernel/hotplug
	if zcat /proc/config.gz | grep -q KOBJECT_UEVENT ; then
	    killproc -p $UDEVD_PIDFILE /sbin/udevd
	fi
	rc_status -v
	;;
    status)
	echo -n "Checking for udevd:"
	checkproc -p $UDEVD_PIDFILE /sbin/udevd
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|force-reload}"
	exit 1
	;;
esac
rc_exit
