]> Frank Brehm's Git Trees - config/weimar/etc.git/blob - init.d/urandom
saving uncommitted changes in /etc prior to emerge run
[config/weimar/etc.git] / init.d / urandom
1 #!/sbin/openrc-run
2 # Copyright (c) 2007-2015 The OpenRC Authors.
3 # See the Authors file at the top-level directory of this distribution and
4 # https://github.com/OpenRC/openrc/blob/master/AUTHORS
5 #
6 # This file is part of OpenRC. It is subject to the license terms in
7 # the LICENSE file found in the top-level directory of this
8 # distribution and at https://github.com/OpenRC/openrc/blob/master/LICENSE
9 # This file may not be copied, modified, propagated, or distributed
10 # except according to the terms contained in the LICENSE file.
11
12 : ${urandom_seed:=${URANDOM_SEED:-/var/lib/misc/random-seed}}
13 description="Initializes the random number generator."
14
15 depend()
16 {
17         need localmount
18         keyword -docker -jail -lxc -openvz -prefix -systemd-nspawn
19 }
20
21 save_seed()
22 {
23         local psz=1
24
25         if [ -e /proc/sys/kernel/random/poolsize ]; then
26                 : $(( psz = $(cat /proc/sys/kernel/random/poolsize) / 4096 ))
27         fi
28
29         (       # sub shell to prevent umask pollution
30                 umask 077
31                 dd if=/dev/urandom of="$urandom_seed" count=${psz} 2>/dev/null
32         )
33 }
34
35 start()
36 {
37         [ -c /dev/urandom ] || return
38         if [ -f "$urandom_seed" ]; then
39                 ebegin "Initializing random number generator"
40                 cat "$urandom_seed" > /dev/urandom
41                 eend $? "Error initializing random number generator"
42         fi
43         rm -f "$urandom_seed" && save_seed
44         return 0
45 }
46
47 stop()
48 {
49         ebegin "Saving random seed"
50         save_seed
51         eend $? "Failed to save random seed"
52 }