urandom 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: urandom
  4. # Required-Start: $local_fs mountvirtfs
  5. # Required-Stop: $local_fs
  6. # Default-Start: S
  7. # Default-Stop: 0 6
  8. # Short-Description: Save and restore the random seed
  9. # Description: Save the random seed on shutdown and restore it on boot,
  10. # to ensure that the seed isn't predicable on startup
  11. # (because the boot process is predictable)
  12. ### END INIT INFO
  13. test -c /dev/urandom || exit 0
  14. RANDOM_SEED_FILE=/var/lib/urandom/random-seed
  15. . /etc/default/rcS
  16. [ -f /etc/default/urandom ] && . /etc/default/urandom
  17. case "$1" in
  18. start|"")
  19. test "$VERBOSE" != no && echo "Initializing random number generator..."
  20. # Load and then save 512 bytes, which is the size of the entropy
  21. # pool. Also load the current date, in case the seed file is
  22. # empty.
  23. ( date +%s.%N; [ -f "$RANDOM_SEED_FILE" ] && cat "$RANDOM_SEED_FILE" ) \
  24. >/dev/urandom
  25. rm -f "$RANDOM_SEED_FILE"
  26. umask 077
  27. dd if=/dev/urandom of=$RANDOM_SEED_FILE count=1 \
  28. >/dev/null 2>&1 || echo "urandom start: failed."
  29. umask 022
  30. ;;
  31. stop)
  32. # Carry a random seed from shut-down to start-up;
  33. # see documentation in linux/drivers/char/random.c
  34. test "$VERBOSE" != no && echo "Saving random seed..."
  35. umask 077
  36. dd if=/dev/urandom of=$RANDOM_SEED_FILE count=1 \
  37. >/dev/null 2>&1 || echo "urandom stop: failed."
  38. ;;
  39. *)
  40. echo "Usage: urandom {start|stop}" >&2
  41. exit 1
  42. ;;
  43. esac
  44. exit 0