summaryrefslogtreecommitdiffstats
path: root/etc/rc.d/random
blob: 1450b93bedaf627746e18bbfe9eda04d82a9ce28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: random
# REQUIRE: diskless mountcritlocal initrandom
# BEFORE: network1
# KEYWORD: FreeBSD shutdown

. /etc/rc.subr

name="random"
start_cmd="random_start"
stop_cmd="random_stop"

feed_dev_random()
{
	if [ -f "${1}" -a -r "${1}" -a -s "${1}" ]; then
		cat "${1}" | dd of=/dev/random bs=8k 2>/dev/null
	fi
}

random_start()
{
	# Reseed /dev/random with previously stored entropy.
	case ${entropy_dir} in
	[Nn][Oo])
		;;
	*)
		entropy_dir=${entropy_dir:-/var/db/entropy}
		if [ -d "${entropy_dir}" ]; then
			if [ -w /dev/random ]; then
				for seedfile in ${entropy_dir}/*; do
					feed_dev_random "${seedfile}"
				done
			fi
		fi
		;;
	esac

	case ${entropy_file} in
	[Nn][Oo] | '')
		;;
	*)
		if [ -w /dev/random ]; then
			feed_dev_random "${entropy_file}"
		fi
		;;
	esac
}

random_stop()
{
	# Write some entropy so when the machine reebots /dev/random
	# can be reseeded
	#
	case ${entropy_file} in
	[Nn][Oo] | '')
		;;
	*)
		echo -n 'Writing entropy file:'
		rm -f ${entropy_file}
		oumask=`umask`
		umask 077
		if touch ${entropy_file} ; then
			entropy_file_confirmed="${entropy_file}"
		else
			# Try this as a reasonable alternative for read-only
			# roots, diskless workstations, etc.
			rm -f /var/db/entropy
			if touch /var/db/entropy ; then
				entropy_file_confirmed=/var/db/entropy
			fi
		fi
		case ${entropy_file_confirmed} in
		'')
			err 1 '${entropy_file_confirmed}:' \
			    ' entropy file write failed.'
			;;
		*)
			dd if=/dev/random of=${entropy_file_confirmed} \
			   bs=4096 count=1 2> /dev/null
			echo '.'
			;;
		esac
		umask ${oumask}
		;;
	esac
}

load_rc_config $name
run_rc_command "$1"
OpenPOWER on IntegriCloud