#!/bin/sh
#
# random	Script to snapshot random state and reload it at boot time.
#
# chkconfig:	12345 20 80
#
# description:	Saves and restores system entropy pool for higher quality \
#		random number generation.
#
# $Id: random 9020 2007-11-09 00:21:42Z glen $

# Source function library.
. /etc/rc.d/init.d/functions

random_seed=/var/run/random-seed

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/random ]; then
		show "Initializing random number generator"
		busy
		# Carry a random seed from start-up to start-up
		# Load and then save 512 bytes, which is the size of the entropy pool
		if [ -f $random_seed ]; then
			cat $random_seed >/dev/urandom
		else
			touch $random_seed
		fi
		chmod 600 $random_seed
		dd if=/dev/urandom of=$random_seed count=1 bs=512 2>/dev/null
		touch /var/lock/subsys/random
		deltext
		ok
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/random ]; then
		# Carry a random seed from shut-down to start-up
		# Save 512 bytes, which is the size of the entropy pool
		show "Saving random seed"
		busy
		touch $random_seed
		chmod 600 $random_seed
		dd if=/dev/urandom of=$random_seed count=1 bs=512 2>/dev/null

		rm -f /var/lock/subsys/random
		deltext
		ok
	fi
	;;
  status)
	# this is way overkill, but at least we have some status output...
	if [ -c /dev/random ] ; then
		nls "The random data source exists"
	else
		nls "The random data source is missing"
	fi
	;;
  *)
	msg_usage "$0 {start|stop|status}"
	exit 3
esac

exit 0
