#! /bin/sh
#
# urandom	This script saves the random seed between reboots.
#		It is called from the boot, halt and reboot scripts.
#
# Version:	@(#)urandom  2.85-14  31-Mar-2004  miquels@cistron.nl
#

[ -c /dev/urandom ] || exit 0

VERBOSE=yes
[ -f /etc/default/rcS ] && . /etc/default/rcS

POOLSIZE=512
if [ -f /proc/sys/kernel/random/poolsize ]
then
	POOLSIZE="`cat /proc/sys/kernel/random/poolsize`"
fi

case "$1" in
	start|"")
		if [ "$VERBOSE" != no ]
		then
			echo -n "Initializing random number generator..."
		fi
		# Load and then save $POOLSIZE (512) bytes,
		# which is the size of the entropy pool
		if [ -f /var/lib/urandom/random-seed ]
		then
			cat /var/lib/urandom/random-seed >/dev/urandom
		fi
		rm -f /var/lib/urandom/random-seed
		umask 077
		dd if=/dev/urandom of=/var/lib/urandom/random-seed \
			bs=$POOLSIZE count=1 >/dev/null 2>&1 \
			|| echo "urandom start: failed."
		umask 022
		[ "$VERBOSE" != no ] && echo "done."
		;;
	stop)
		# Carry a random seed from shut-down to start-up;
		# see documentation in linux/drivers/char/random.c
		[ "$VERBOSE" != no ] && echo -n "Saving random seed..."
		umask 077
		dd if=/dev/urandom of=/var/lib/urandom/random-seed \
			bs=$POOLSIZE count=1 >/dev/null 2>&1 \
			|| echo "urandom stop: failed."
		[ "$VERBOSE" != no ] && echo "done."
		;;
	*)
		echo "Usage: urandom {start|stop}" >&2
		exit 1
		;;
esac

exit 0

