#!/bin/sh

### BEGIN INIT INFO
# Provides:          udev
# Required-Start:    mountvirtfs
# Required-Stop:
# Default-Start:     S
# Default-Stop:
# Short-Description: Start udevd, populate /dev and load drivers.
### END INIT INFO

export TZ=/etc/localtime

[ -d /sys/class ] || exit 1
[ -r /proc/mounts ] || exit 1
[ -x /lib/udev/udevd ] || exit 1
[ -f /etc/default/udev-cache ] && . /etc/default/udev-cache
[ -f /etc/udev/udev.conf ] && . /etc/udev/udev.conf
[ -f /etc/default/rcS ] && . /etc/default/rcS

readfiles () {
   READDATA=""
   for filename in $@; do
           if [ -r $filename ]; then
                   while read line; do
                           READDATA="$READDATA$line"
                   done < $filename
           fi
   done
}

kill_udevd () {
    pid=`pidof -x udevd`
    [ -n "$pid" ] && kill $pid
}

case "$1" in
  start)
    export ACTION=add
    # propagate /dev from /sys
    printf "Starting udev\\n"

    # Check for requireed devtmpfs before trying to start udev and
    # mount a no-existant fs.
    if ! grep -q devtmpfs /proc/filesystems
    then
        printf "Missing devtmpfs, which is required for udev to run\\n";
        printf "Halting...\\n"
        halt
    fi
    # mount the devtmpfs on /dev, if not already done
    LANG=C awk '$2 == "/dev" && ($3 == "devtmpfs") { exit 1 }' /proc/mounts && {
            mount -n -o mode=0755 -t devtmpfs none "/dev"
    }
    [ -e /dev/pts ] || mkdir -m 0755 /dev/pts
    [ -e /dev/shm ] || mkdir -m 1777 /dev/shm
    # the automount rule for udev needs /tmp directory available, as /tmp is a symlink
    # to /var/tmp which in turn is a symlink to /var/volatile/tmp, we need to make sure
    # /var/volatile/tmp directory to be available.
    mkdir -p /var/volatile/tmp
    chmod 1777 /var/volatile/tmp

    # Cache handling.
    # A list of files which are used as a criteria to judge whether the udev cache could be reused.
    CMP_FILE_LIST="/proc/version /proc/cmdline /proc/devices /proc/atags"
    if [ "$DEVCACHE" != "" ]; then
            if [ -e $DEVCACHE ]; then
                    readfiles $CMP_FILE_LIST
                    NEWDATA="$READDATA"
                    readfiles /etc/udev/cache.data
                    OLDDATA="$READDATA"
                    if [ "$OLDDATA" = "$NEWDATA" ]; then
                            (cd /; tar xf $DEVCACHE > /dev/null 2>&1)
                            not_first_boot=1
                            [ "$VERBOSE" != "no" ] && printf "udev: using cache file %s\\n" "$DEVCACHE"
                            [ -e /dev/shm/udev.cache ] && rm -f /dev/shm/udev.cache
                    else
                            # Output detailed reason why the cached /dev is not used
                            if [ "$VERBOSE" != "no" ]; then
                                    printf "udev: udev cache not used\\n"
                                    printf "udev: we use %s as criteria to judge whether the cache /dev could be resued\\n" "$CMP_FILE_LIST"
                                    printf "udev: olddata: %s\\n" "$OLDDATA"
                                    printf "udev: newdata: %s\\n" "$NEWDATA"
                            fi
                            printf "%s\\n" "$NEWDATA" > /dev/shm/udev.cache
                    fi
            else
                    if [ "$ROOTFS_READ_ONLY" != "yes" ]; then
                            # If rootfs is not read-only, it's possible that a new udev cache would be generated;
                            # otherwise, we do not bother to read files.
                            readfiles $CMP_FILE_LIST
                            printf "%s\\n" "$READDATA" > /dev/shm/udev.cache
                    fi
            fi
    fi

    # make_extra_nodes
    kill_udevd > "/dev/null" 2>&1

    # trigger the sorted events
    [ -e /proc/sys/kernel/hotplug ] && printf "%b" "\000" >/proc/sys/kernel/hotplug
    /lib/udev/udevd -d

    udevadm control --env=STARTUP=1
    if [ "$not_first_boot" != "" ];then
            udevadm trigger --action=add --subsystem-nomatch=tty --subsystem-nomatch=mem --subsystem-nomatch=vc --subsystem-nomatch=vtconsole --subsystem-nomatch=misc --subsystem-nomatch=dcon --subsystem-nomatch=pci_bus  --subsystem-nomatch=graphics    --subsystem-nomatch=backlight --subsystem-nomatch=video4linux  --subsystem-nomatch=platform
            (udevadm settle --timeout=3; udevadm control --env=STARTUP=)&
    else
            udevadm trigger --action=add
            udevadm settle
    fi
    ;;
  stop)
    printf "Stopping udevd\\n"
    start-stop-daemon --stop --name udevd --quiet
    ;;
  restart)
    $0 stop
    sleep 1
    $0 start
    ;;
  status)
    pid=`pidof -x udevd`
    if [ -n "$pid" ]; then
        printf "udevd (pid %s) is running ...\\n" "$pid"
    else
        printf "udevd is stopped\\n"
    fi
    ;;
  *)
    printf "Usage: %s {start|stop|status|restart}\\n" "$0"
    exit 1
esac
exit 0

