#!/bin/bash
#
# start_udev
#
# script to initialize /dev by using udev.
#
# Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
#
# Released under the GPL v2 only.
#
# This needs to be run at the earliest possible point in the boot 
# process.
#
# Based on the udev init.d script
#
# Thanks go out to the Gentoo developers for proving 
# that this is possible to do.
#
# Yes, it's very verbose, feel free to turn off all of the echo calls,
# they were there to make me feel better that everything was working
# properly during development...
#
# don't use udev if sysfs is not mounted.

sysfs_dir=/sys

export TZ=/etc/localtime

[ -d $sysfs_dir/class ] || exit 1
[ -r /proc/mounts ] || exit 1
[ -x /sbin/udevd ] || exit 1
[ -f /etc/udev/udev.conf ] && . /etc/udev/udev.conf
udev_root=${udev_root-/dev}

if [ -f /dev/.in_sysinit ]; then
	exec >/dev/console 2>/dev/console </dev/console
fi

. /etc/init.d/functions
. /etc/sysconfig/udev

MAKEDEV="/sbin/MAKEDEV"

# Check SELinux status
selinuxfs="$(fstab_decode_str `LC_ALL=C awk '/ selinuxfs / { print $2 }' /proc/mounts`)"
SELINUX_STATE=
if [ -n "$selinuxfs" ] && [ "`cat /proc/self/attr/current`" != "kernel" ]; then
        if [ -r "$selinuxfs/enforce" ] ; then
                SELINUX_STATE=`cat "$selinuxfs/enforce"`
        else
                # assume enforcing if you can't read it
                SELINUX_STATE=1
        fi
fi



xargs_simple () {
	if [ "$1" = "-n" ]; then
		shift
		MAXNR="$1"
		shift
	else
		MAXNR=100
	fi
	NR=$MAXNR
	ARGS=""
	[ -z "$1" ] && set echo

	while read line; do
		if [ $NR -gt 0 ]; then
        		ARGS="$ARGS $line"
	        	NR=$[$NR - 1]
		else
        		"$@" $ARGS
	        	NR=$MAXNR
		        ARGS="$line"
		fi
	done
	if [ -n "$ARGS" ]; then
		"$@" $ARGS
	fi 
}

# 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,size=$tmpfs_size,mode=0755 -t devtmpfs devtmpfs /dev
    return
  fi

  if ! mount -n -o nosuid,size=$tmpfs_size,mode=0755 -t devtmpfs devtmpfs /dev; then
    echo 'udev requires devtmpfs support, udevd not started' >&2
  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 /bin/kmod ] || return 0

  /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
      d|d!)
			test $name || mkdir -p -m "${mode}" "${name}"
			;;
      c|c!) 
            if [ ! -c "${name}" ]; then
                    old_ifs="${IFS}"
                    IFS=:
                    set -- ${TARG}
                    IFS="${old_ifs}"
                    mknod -m "${mode}" "${name}" "$type" $(echo $arg | sed 's/:/ /')
            fi
            ;;
		b|b!)
            if [ ! -b "${name}" ]; then
                    old_ifs="${IFS}"
                    IFS=:
                    set -- ${TARG}
                    IFS="${old_ifs}"
                    mknod -m "${mode}" "${name}" "$type" $(echo $arg | sed 's/:/ /')
			fi
			;;
		*)
			echo "unparseable line ($type $name $mode $uid $gid $age $arg)" >&2
			;;
    esac
    if [ "${uid}" != "-" ]; then
            chown "${uid}" "${name}"
    fi
    if [ "${gid}" != "-" ]; then
            chgrp "${gid}" "${name}"
    fi
    
  done
}

make_extra_nodes () {
	ln -snf /proc/self/fd $udev_root/fd
	ln -snf /proc/self/fd/0 $udev_root/stdin
	ln -snf /proc/self/fd/1 $udev_root/stdout
	ln -snf /proc/self/fd/2 $udev_root/stderr
	ln -snf /proc/kcore $udev_root/core

	[ -d $udev_root/pts ] || mkdir -m 0755 $udev_root/pts
	[ -d $udev_root/shm ] || mkdir -m 0755 $udev_root/shm
	[ -d $udev_root/hugepages ] || mkdir -m 0755 $udev_root/hugepages
	create_dev_makedev

	USE_MD5="false"
	[ -x /usr/bin/md5sum -a "$UDEV_USE_MAKEDEV_CACHE" == "yes" ] && USE_MD5="true"

	for i in 0 1 2 3 4 5 6 7; do
		[ -b /dev/loop$i ] || /bin/mknod -m 0640 /dev/loop$i b 7 $i
		/bin/chown root:disk /dev/loop$i
	done

	for i in 0 1 2 3; do
		[ -c /dev/lp$i ] || /bin/mknod -m 0660 /dev/lp$i c 6 $i
		/bin/chown root:lp /dev/lp$i
	done

	if [ -x /sbin/restorecon ]; then
		/sbin/restorecon -R /dev
	fi

	if [ -x "$MAKEDEV" ]; then
		for i in /etc/udev/makedev.d/*.nodes; do
			if [ -f "$i" ]; then 			   
				# use a little caching to speedup things
				if [ "$USE_MD5" == "true" ]; then
					# fix for MAKEDEV shell scripts
					[ -d /dev/.udev/makedev.d ] || mkdir -p /dev/.udev/makedev.d 
					md5=$(/usr/bin/md5sum "$i"|(read a b; echo $a))-se$SELINUX_STATE
					if [ -f "/var/lib/udev/makedev.d/${md5}.sh" ];then
						md5file="/var/lib/udev/makedev.d/${md5}.sh"
					else
						md5file="/dev/.udev/makedev.d/${md5}.sh"
					fi
					if [ ! -f "$md5file" ]; then
						( sed -e 's,#.*,,g' "$i" | \
							xargs_simple -n 100 $MAKEDEV -x -a -S ) \
							> "$md5file"
					fi
					. "$md5file" >$udev_root/null 2>&1
				else
						( sed -e 's,#.*,,g' "$i" | \
							xargs_simple -n 100 $MAKEDEV -x )
				fi
			fi
		done 
	fi

	for devdir in /etc/udev/devices /lib/udev/devices; do
		[ -d "$devdir" ] || continue
		pushd $devdir &> "$udev_root/null"
		set *
		if [ "$1" != "*" ]; then
			#echo "Warning: $devdir is deprecated. Please use /etc/udev/makedev.d/."
        		cp -ar "$@" $udev_root/ 
			pushd "$udev_root" &> "$udev_root/null"
			[ -x /sbin/restorecon ] && /sbin/restorecon "$@" 
			popd &> "$udev_root/null"
		fi
		popd &> "$udev_root/null"
	done
}

kill_udevd() {
	if [ -x /sbin/pidof ]; then
		pid=`/sbin/pidof -x udevd`
        	[ -n "$pid" ] && kill $pid
	fi
}

findalias () {
	local n
	for n in "$1"/* ; do
		[ -h "$n" ] && continue
		[ -d "$n" ] && { findalias "$n" ; continue; }
		[ "${n##*/}" == "modalias" ] && echo $(cat $n)
	done
}

# returns OK if $1 contains $2
strstr() {
  [ "${1#*$2*}" = "$1" ] && return 1
  return 0
}

getval() {
    what=$1
    shift
    for arg; do 
	if strstr "$arg" "$what="; then
	    val=${arg#${what}=*}
	    echo $val
	    return 0
	fi
    done
    return 1
}

wait_for_queue() {
        local timeout=${1:-0}
	local ret=0
        modprobe scsi_wait_scan &>/dev/null && rmmod scsi_wait_scan
	if [ $timeout -gt 0 ]; then
	    /sbin/udevadm settle --timeout=$timeout
        else
            # by default wait indefinitely
	    /sbin/udevadm settle
	    modprobe scsi_wait_scan &>/dev/null && rmmod scsi_wait_scan
            while ! /sbin/udevadm settle --timeout=0; do
                echo
                echo -n "udev still not settled. Waiting."
		/sbin/udevadm settle
		modprobe scsi_wait_scan &>/dev/null && rmmod scsi_wait_scan
	    done
	fi
	ret=$?
	if [ $ret -ne 0 ]; then
		echo -n "Wait timeout. Will continue in the background."
	fi
	return $ret;
}

export ACTION=add

touch_recursive() {
	( cd $1;
	for i in *; do 
		[[ -c "$i" || -b "$i" ]] && touch --no-create "$i"
		[ -d "$i" ] && touch_recursive "$i"
	done )
	return 0
}

prog=udev
ret=0
STRING=$"Starting $prog: "
# propagate $udev_root from /sys
echo -n "$STRING"

# mount the tmpfs on ${udev_root%/}, if not already done
LANG=C awk "\$2 == \"${udev_root%/}\" && ( \$3 == \"devtmpfs\" || \$3 == \"tmpfs\" ) { exit 1 }" /proc/mounts && {
	if LANG=C fgrep -q "none ${udev_root%/}/pts " /proc/mounts; then
		PTSDIR=$(mktemp -d)
		mount --move $udev_root/pts "$PTSDIR"
	fi
	if LANG=C fgrep -q "none ${udev_root%/}/shm " /proc/mounts; then
		SHMDIR=$(mktemp -d)
		mount --move $udev_root/shm "$SHMDIR"
	fi
	# First try to mount a devtmpfs on $udev_root
	mount -n -o mode=0755 -t devtmpfs none "$udev_root" 2>/dev/null \
	|| mount -n -o mode=0755 -t tmpfs none "$udev_root" 
	mkdir -m 0755 $udev_root/pts
	mkdir -m 0755 $udev_root/shm
	if [ -n "$PTSDIR" ]; then
		mount --move "$PTSDIR" $udev_root/pts
		rmdir "$PTSDIR"
	fi
	if [ -n "$SHMDIR" ]; then
		mount --move "$SHMDIR" $udev_root/shm
		rmdir "$SHMDIR"
	fi
	
	ret=$[$ret + $?]
}
#before trying to make initial nodes,
# make sure udevd is not running
# and /run is already mounted (= make udev happy) 
kill_udevd > "$udev_root/null" 2>&1

# Mount /run (check if nitro is PID1)
if [ ! "$(cat /proc/1/comm)" = "nitro" ]; then
	if [ ! -d /run ]; then
		mkdir -p /run
	fi
	mount -n -t tmpfs none /run > "$udev_root/null" 2>&1
fi

make_static_nodes
make_extra_nodes 

# clean up parts of the database created by the initrd udev
/sbin/udevadm info --cleanup-db > "$udev_root/null" 2>&1

cmdline=$(cat /proc/cmdline)
[ -d $udev_root/.udev ] || mkdir -p $udev_root/.udev > "$udev_root/null" 2>&1
[ -d /run/udev ] || mkdir -p /run/udev > "$udev_root/null" 2>&1
UDEV_OPTS=""

# turn off hotplug binary calling / trigger the sorted events
if ! [ -x /sbin/hotplug ]; then
	if [ -e /sys/kernel/uevent_helper ]; then
		echo -e '\000\000\000\000' > /sys/kernel/uevent_helper
	elif [ -e /proc/sys/kernel/hotplug ]; then
		echo -e '\000\000\000\000' > /proc/sys/kernel/hotplug
	fi

	if strstr "$cmdline" udevdebug; then
		UDEV_OPTS="$UDEV_OPTS --debug"
	fi

	if strstr "$cmdline" udevlog; then
                echo 'WARNING: udevlog is active!' >&2
                echo "$udev_root/.udev/udev.log can grow big very quick and resides in RAM" >&2
                echo 'Turn off udev logging as soon as you booted!' >&2
		/sbin/udevd --daemon $UDEV_OPTS 0<>$udev_root/.udev/udev.log 1<>$udev_root/.udev/udev.log 2<>$udev_root/.udev/udev.log
	else
		/sbin/udevd --daemon $UDEV_OPTS
	fi

	wait
	ret=$[$ret + $?]

	udevtimeout=$(getval udevtimeout $cmdline)

	if strstr "$cmdline" udevdebug; then
		/sbin/udevadm control --log-priority=debug
	fi
	if strstr "$cmdline" udevinfo; then
		/sbin/udevadm control --log-priority=info
	fi
	if strstr "$cmdline" udevchildren; then
		/sbin/udevadm control --children-max=$(getval udevchildren $cmdline)
	fi
    if strstr "$cmdline" noiswmd; then
        /sbin/udevadm control --env=rd_NO_MDIMSM=1
	fi

	/sbin/udevadm control --env=STARTUP=1

	if strstr "$cmdline" modprobedebug; then
		/sbin/udevadm control --env=MODPROBE_OPTIONS="-s -v -q"
		echo
		findalias /sys | while read modules ; do
			if [ -n "$modules" ]; then
				echo "Loading modules for $modules in 1 seconds"
				sleep 1
				/sbin/modprobe -a -v -q $modules
				wait_for_queue $udevtimeout
			 fi
		done
		echo "Triggering Rest"
	fi
        if [ "x$SPEEDBOOT" != "xyes" ]; then
	/sbin/udevadm trigger --type=subsystems --action=add
	/sbin/udevadm trigger --type=devices --action=add

	ret=$[$ret + $?]
	wait_for_queue $udevtimeout
	ret=$[$ret + $?]
	wait
	/sbin/udevadm control --env=STARTUP=
        fi # not in SPEEDBOOT mode
else
	echo -n " kernel too old for this udev version "
	/sbin/udevd --daemon $UDEV_OPTS
	ret=10

fi

ret=$[$ret + $?]

# touch all device files for timezone glitches
# "find" is in /usr/bin and might not be available
if [ -f /etc/sysconfig/clock ]; then
	. /etc/sysconfig/clock
	[[ "$UTC" == "false" || "$UTC" == "no" ]] \
		&& touch_recursive /dev > "/dev/null" 2>&1 
fi

[ $ret -eq 0 ] && success $"$STRING" || failure $"$STRING"
echo
exit 0
