From: Theodore Ts'o Date: Mon, 12 Jun 2000 17:35:13 +0000 (+0000) Subject: ChangeLog, gen_uuid.c: X-Git-Tag: E2FSPROGS-1_19~26 X-Git-Url: https://git.whamcloud.com/?a=commitdiff_plain;h=5dd7ff07d032df32b018c0dfd1cbdf30bd7bf29f;p=tools%2Fe2fsprogs.git ChangeLog, gen_uuid.c: gen_uuid.c (get_random_bytes): Use O_NONBLOCK when trying to open /dev/random. Break out the /dev/random initialization code into a get_random_fd() function, and use that function in uuid_generate() to determine whether to use uuid_generate_random() or uuid_generate_time(). --- diff --git a/lib/uuid/ChangeLog b/lib/uuid/ChangeLog index 6491656..b20ac32 100644 --- a/lib/uuid/ChangeLog +++ b/lib/uuid/ChangeLog @@ -1,3 +1,11 @@ +2000-06-12 Theodore Ts'o + + * gen_uuid.c (get_random_bytes): Use O_NONBLOCK when trying to + open /dev/random. Break out the /dev/random + initialization code into a get_random_fd() function, and + use that function in uuid_generate() to determine whether + to use uuid_generate_random() or uuid_generate_time(). + 2000-05-25 * Makefile: Add hack dependency rule so that parallel makes work diff --git a/lib/uuid/gen_uuid.c b/lib/uuid/gen_uuid.c index 5fbb300..be1dbbb 100644 --- a/lib/uuid/gen_uuid.c +++ b/lib/uuid/gen_uuid.c @@ -41,23 +41,34 @@ #define rand() random() #endif +static int get_random_fd() +{ + static int fd = -2; + int i; + + if (fd == -2) { + fd = open("/dev/urandom", O_RDONLY); + if (fd == -1) + fd = open("/dev/random", O_RDONLY | O_NONBLOCK); + srand((getpid() << 16) ^ getuid() ^ time(0)); + } + /* Crank the random number generator a few times */ + for (i = time(0) & 0x1F; i > 0; i--) + rand(); + return fd; +} + + /* * Generate a series of random bytes. Use /dev/urandom if possible, * and if not, use srandom/random. */ static void get_random_bytes(void *buf, int nbytes) { - static int fd = -2; - int i; + int i, fd = get_random_fd(); int lose_counter = 0; char *cp = (char *) buf; - if (fd == -2) { - fd = open("/dev/urandom", O_RDONLY); - if (fd == -1) - fd = open("/dev/random", O_RDONLY); - srand((getpid() << 16) ^ getuid() ^ time(0)); - } if (fd >= 0) { while (nbytes > 0) { i = read(fd, cp, nbytes); @@ -252,15 +263,7 @@ void uuid_generate_random(uuid_t out) */ void uuid_generate(uuid_t out) { - static int has_random = -1; - - if (has_random < 0) { - if (access("/dev/urandom", R_OK) == 0) - has_random = 1; - else - has_random = 0; - } - if (has_random) + if (get_random_fd() >= 0) uuid_generate_random(out); else uuid_generate_time(out);