Whamcloud - gitweb
ChangeLog, gen_uuid.c:
authorTheodore Ts'o <tytso@mit.edu>
Mon, 12 Jun 2000 17:35:13 +0000 (17:35 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 12 Jun 2000 17:35:13 +0000 (17:35 +0000)
  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().

lib/uuid/ChangeLog
lib/uuid/gen_uuid.c

index 6491656..b20ac32 100644 (file)
@@ -1,3 +1,11 @@
+2000-06-12  Theodore Ts'o  <tytso@valinux.com>
+
+       * 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    <tytso@snap.thunk.org>
 
        * Makefile: Add hack dependency rule so that parallel makes work
index 5fbb300..be1dbbb 100644 (file)
 #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);