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.1 2003/07/24 06:35:37 dillon Exp $ 5# 6 7# PROVIDE: random 8# REQUIRE: diskless mountcritlocal initrandom 9# BEFORE: netif 10# KEYWORD: DragonFly FreeBSD 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 cat "${1}" | dd of=/dev/random bs=8k 2>/dev/null 22 fi 23} 24 25random_start() 26{ 27 # Reseed /dev/random with previously stored entropy. 28 case ${entropy_dir} in 29 [Nn][Oo]) 30 ;; 31 *) 32 entropy_dir=${entropy_dir:-/var/db/entropy} 33 if [ -d "${entropy_dir}" ]; then 34 if [ -w /dev/random ]; then 35 for seedfile in ${entropy_dir}/*; do 36 feed_dev_random "${seedfile}" 37 done 38 fi 39 fi 40 ;; 41 esac 42 43 case ${entropy_file} in 44 [Nn][Oo] | '') 45 ;; 46 *) 47 if [ -w /dev/random ]; then 48 feed_dev_random "${entropy_file}" 49 fi 50 ;; 51 esac 52} 53 54random_stop() 55{ 56 # Write some entropy so when the machine reebots /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} 65 oumask=`umask` 66 umask 077 67 if touch ${entropy_file}; 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 73 if touch /var/db/entropy; then 74 entropy_file_confirmed=/var/db/entropy 75 fi 76 fi 77 case ${entropy_file_confirmed} in 78 '') 79 err 1 '${entropy_file_confirmed}:' \ 80 ' entropy file write failed.' 81 ;; 82 *) 83 dd if=/dev/random of=${entropy_file_confirmed} \ 84 bs=4096 count=1 2> /dev/null 85 echo '.' 86 ;; 87 esac 88 umask ${oumask} 89 ;; 90 esac 91} 92 93load_rc_config $name 94run_rc_command "$1" 95