1#!/bin/sh
2#
3# $FreeBSD$
4#
5
6# PROVIDE: random
7# REQUIRE: initrandom FILESYSTEMS
8# BEFORE: netif
9# KEYWORD: nojail 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			feed_dev_random /var/db/entropy-file
49		fi
50		;;
51	esac
52}
53
54random_stop()
55{
56	# Write some entropy so when the machine reboots /dev/random
57	# can be reseeded
58	#
59	case ${entropy_file} in
60	[Nn][Oo] | '')
61		;;
62	*)
63		echo -n 'Writing entropy file:'
64		rm -f ${entropy_file} 2> /dev/null
65		oumask=`umask`
66		umask 077
67		if touch ${entropy_file} 2> /dev/null; then
68			entropy_file_confirmed="${entropy_file}"
69		else
70			# Try this as a reasonable alternative for read-only
71			# roots, diskless workstations, etc.
72			rm -f /var/db/entropy-file 2> /dev/null
73			if touch /var/db/entropy-file 2> /dev/null; then
74				entropy_file_confirmed=/var/db/entropy-file
75			fi
76		fi
77		case ${entropy_file_confirmed} in
78		'')
79			warn 'write failed (read-only fs?)'
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