summaryrefslogtreecommitdiff
blob: 8d8cb2c07f3f87d486c4cdf4317819fa0a781529 (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
#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/sys-apps/rng-tools/files/2-r2/rngd,v 1.1 2009/11/10 03:15:06 robbat2 Exp $

depend() {
	need localmount
	after random
}

BINARY=/usr/sbin/rngd
# Do NOT add /dev/tpm to this.
DEFAULT_DEVICE="/dev/hw_random* /dev/hwrandom* /dev/i810_rng /dev/hwrng* /dev/urandom"
DEFAULT_TPM_DEVICE="/dev/tpm*"

find_device() {
	# The echo is to cause globbing
	for d in $*; do
		[ -e "$d" ] && break
	done
	echo "$d"
}

find_rng_device() {
	local d="$(find_device $(echo ${DEVICE-${DEFAULT_DEVICE}}))"
	[ -z "${d}" ] && d=/dev/null
	echo "$d"
}

find_tpm_device() {
	local d="$(find_device $(echo ${TPM_DEVICE-${DEFAULT_TPM_DEVICE}}))"
	[ -z "${d}" ] && d=/dev/null
	echo "$d"
}

start() {
	local d t
	d="$(find_rng_device)"
	[ "${NO_TPM}" == "1" ] || t="$(find_tpm_device)"
	[ -z "${d}" ] && d=/dev/null
	[ -z "${t}" ] && t=/dev/null
	# TODO: find a better way to handle multiple feedback devices.
	# Notice that we only warn if TPM is not available.
	[ "$d" == "/dev/urandom" -a "$t" == "/dev/null" ] && ewarn "No hardware RNG found, using $d as feedback input"
	# Now sanity check and start.
	if [ "$d" != "/dev/null" -o "$t" != "/dev/null" ]; then
		ebegin "Starting hardware RNG daemon"
		start-stop-daemon --start --quiet --exec ${BINARY} -- -b -s ${STEP-64} -t ${TIMEOUT-60} --no-tpm=${NO_TPM-0} -W ${WATERMARK-2048} -r $d
		eend $? "Failed to start hardware RNG daemon"
	else
		eerror "No hardware RNG or feedback source available."
		return 1
	fi
}

stop() {
	ebegin "Stopping hardware RNG daemon"
	start-stop-daemon --stop --quiet --exec ${BINARY}
	eend $? "Failed to stop hardware RNG daemon"
}