PageRenderTime 105ms CodeModel.GetById 99ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/init.d/urandom

http://github.com/brinkman83/bashrc
Shell | 78 lines | 55 code | 6 blank | 17 comment | 10 complexity | d0385e199d51b19181b77dc55211ac02 MD5 | raw file
 1#! /bin/sh
 2### BEGIN INIT INFO
 3# Provides:          urandom
 4# Required-Start:    $remote_fs
 5# Required-Stop:     $remote_fs
 6# Default-Start:     S
 7# Default-Stop:      0 6
 8# Short-Description: Save and restore random seed between restarts.
 9# Description:       This script saves the random seed between restarts.
10#                    It is called from the boot, halt and reboot scripts.
11### END INIT INFO
12
13[ -c /dev/urandom ] || exit 0
14
15PATH=/sbin:/usr/sbin:/bin:/usr/bin
16SAVEDFILE=/var/lib/urandom/random-seed
17POOLSIZE=512
18[ -f /proc/sys/kernel/random/poolsize ] && POOLSIZE="$(cat /proc/sys/kernel/random/poolsize)"
19. /lib/init/vars.sh
20
21. /lib/lsb/init-functions
22
23do_status () {
24	if [ -f $SAVEDFILE ] ; then
25		return 0
26	else
27		return 4
28	fi
29}
30
31case "$1" in
32  start|"")
33	[ "$VERBOSE" = no ] || log_action_begin_msg "Initializing random number generator"
34	# Load and then save $POOLSIZE bytes,
35	# which is the size of the entropy pool
36	if [ -f "$SAVEDFILE" ]
37	then
38		# Handle locally increased pool size
39		SAVEDSIZE="$(find "$SAVEDFILE" -printf "%s")"
40		if [ "$SAVEDSIZE" -gt "$POOLSIZE" ]
41		then
42			[ -w /proc/sys/kernel/random/poolsize ] && echo $POOLSIZE > /proc/sys/kernel/random/poolsize
43			POOLSIZE=$SAVEDSIZE
44		fi
45		cat "$SAVEDFILE" >/dev/urandom
46	fi
47	rm -f $SAVEDFILE
48	# Hm, why is the saved pool re-created at boot? [pere 2009-09-03]
49	umask 077
50	dd if=/dev/urandom of=$SAVEDFILE bs=$POOLSIZE count=1 >/dev/null 2>&1
51	ES=$?
52	umask 022
53	[ "$VERBOSE" = no ] || log_action_end_msg $ES
54	;;
55  stop)
56	# Carry a random seed from shut-down to start-up;
57	# see documentation in linux/drivers/char/random.c
58	[ "$VERBOSE" = no ] || log_action_begin_msg "Saving random seed"
59	umask 077
60	dd if=/dev/urandom of=$SAVEDFILE bs=$POOLSIZE count=1 >/dev/null 2>&1
61	ES=$?
62	[ "$VERBOSE" = no ] || log_action_end_msg $ES
63	;;
64  status)
65	do_status
66	exit $?
67	;;
68  restart|reload|force-reload)
69	echo "Error: argument '$1' not supported" >&2
70	exit 3
71	;;
72  *)
73	echo "Usage: urandom start|stop" >&2
74	exit 3
75	;;
76esac
77
78: