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