random revision 98184
1#!/bin/sh
2#
3# $FreeBSD: head/etc/rc.d/random 98184 2002-06-13 22:14:37Z gordon $
4#
5
6# PROVIDE: random
7# REQUIRE: diskless mountcritlocal initrandom
8# BEFORE: network1
9# KEYWORD: FreeBSD shutdown
10
11. /etc/rc.subr
12
13name="random"
14start_cmd="random_start"
15stop_cmd="random_stop"
16
17feed_dev_random()
18{
19	if [ -f "${1}" -a -r "${1}" -a -s "${1}" ]; then
20		cat "${1}" | dd of=/dev/random bs=8k 2>/dev/null
21	fi
22}
23
24random_start()
25{
26	# Reseed /dev/random with previously stored entropy.
27	case ${entropy_dir} in
28	[Nn][Oo])
29		;;
30	*)
31		entropy_dir=${entropy_dir:-/var/db/entropy}
32		if [ -d "${entropy_dir}" ]; then
33			if [ -w /dev/random ]; then
34				for seedfile in ${entropy_dir}/*; do
35					feed_dev_random "${seedfile}"
36				done
37			fi
38		fi
39		;;
40	esac
41
42	case ${entropy_file} in
43	[Nn][Oo] | '')
44		;;
45	*)
46		if [ -w /dev/random ]; then
47			feed_dev_random "${entropy_file}"
48		fi
49		;;
50	esac
51}
52
53random_stop()
54{
55	# Write some entropy so when the machine reebots /dev/random
56	# can be reseeded
57	#
58	case ${entropy_file} in
59	[Nn][Oo] | '')
60		;;
61	*)
62		echo -n 'Writing entropy file:'
63		rm -f ${entropy_file}
64		oumask=`umask`
65		umask 077
66		if touch ${entropy_file} ; then
67			entropy_file_confirmed="${entropy_file}"
68		else
69			# Try this as a reasonable alternative for read-only
70			# roots, diskless workstations, etc.
71			rm -f /var/db/entropy
72			if touch /var/db/entropy ; then
73				entropy_file_confirmed=/var/db/entropy
74			fi
75		fi
76		case ${entropy_file_confirmed} in
77		'')
78			err 1 '${entropy_file_confirmed}:' \
79			    ' entropy file write failed.'
80			;;
81		*)
82			dd if=/dev/random of=${entropy_file_confirmed} \
83			   bs=4096 count=1 2> /dev/null
84			echo '.'
85			;;
86		esac
87		umask ${oumask}
88		;;
89	esac
90}
91
92load_rc_config $name
93run_rc_command "$1"
94