#!/bin/sh
#
# udev	This is a minimal non-LSB version of a UDEV startup script.  It
#	was derived by stripping down the udev-058 LSB version for use
#	with buildroot on embedded hardware using Linux 2.6.34+ kernels.
#
#	You may need to customize this for your system's resource limits
#	(including startup time!) and administration.  For example, if
#	your early userspace has a custom initramfs or initrd you might
#	need /dev much earlier; or without hotpluggable busses (like USB,
#	PCMCIA, MMC/SD, and so on) your /dev might be static after boot.
#
#	This script assumes your system boots right into the eventual root
#	filesystem, and that init runs this udev script before any programs
#	needing more device nodes than the bare-bones set -- /dev/console,
#	/dev/zero, /dev/null -- that's needed to boot and run this script.
#

# Check for config file and read it
UDEV_CONFIG=/etc/udev/udev.conf
test -r $UDEV_CONFIG || exit 6
. $UDEV_CONFIG

# we need to unmount /dev/pts/ and remount it later over the devtmpfs
unmount_devpts() {
    if mountpoint -q /dev/pts/; then
        umount -n -l /dev/pts/
    fi

    if mountpoint -q /dev/shm/; then
        umount -n -l /dev/shm/
    fi
}

# mount a devtmpfs over /dev, if somebody did not already do it
mount_devtmpfs() {
    if grep -E -q "^[^[:space:]]+ /dev devtmpfs" /proc/mounts; then
        mount -n -o remount,nosuid,mode=0755 -t devtmpfs devtmpfs /dev
        return
    fi

    if ! mount -n -o nosuid,mode=0755 -t devtmpfs devtmpfs /dev; then
        echo "udev requires devtmpfs support, not started"
    fi

    return 0
}

create_dev_makedev() {
    if [ -e /sbin/MAKEDEV ]; then
        ln -sf /sbin/MAKEDEV /dev/MAKEDEV
    else
        ln -sf /bin/true /dev/MAKEDEV
    fi
}

make_static_nodes() {
    [ -e /lib/modules/$(uname -r)/modules.devname ] || return 0
    [ -x /usr/bin/kmod ] || return 0

    /usr/bin/kmod static-nodes --format=tmpfiles --output=/proc/self/fd/1 | \
    while read type name mode uid gid age arg; do
        [ -e $name ] && continue
        case "$type" in
            c|b|c!|b!) mknod -m $mode $name $type $(echo $arg | sed 's/:/ /') ;;
            d|d!) mkdir $name ;;
            *) echo "unparseable line ($type $name $mode $uid $gid $age $arg)" >&2 ;;
        esac
    done
}

case "$1" in
    start)
        if ! mountpoint -q /dev/; then
            unmount_devpts
            mount_devtmpfs
        fi

        make_static_nodes

        # clean up parts of the database created by the initramfs udev
        udevadm info --cleanup-db

        printf "Populating %s using udev: " "${udev_root:-/dev}"
        [ -e /proc/sys/kernel/hotplug ] && printf '\000\000\000\000' > /proc/sys/kernel/hotplug
        /sbin/udevd -d || { echo "FAIL"; exit 1; }
        udevadm trigger --type=subsystems --action=add
        udevadm trigger --type=devices --action=add
        create_dev_makedev
        udevadm settle --timeout=30 || echo "udevadm settle failed"
        echo "done"
        ;;
    stop)
        # Stop execution of events
        udevadm control --stop-exec-queue
        killall udevd
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac


exit 0
