[util-linux-ng] fdisk: better fallback for get_random_id()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When /dev/urandom is not available, we have to use some kind of a hack
to generate a random MBR identifier.  Use a better fallback that
incorporates the clock down to microsecond granularity.
---
 fdisk/fdisk.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)

diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index cc71c5c..ede41b3 100644
--- a/fdisk/fdisk.c
+++ b/fdisk/fdisk.c
@@ -18,6 +18,7 @@
 #include <errno.h>
 #include <getopt.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 #include <time.h>
 
 #include "nls.h"
@@ -148,6 +149,7 @@ get_random_id(void) {
 	int fd;
 	unsigned int v;
 	ssize_t rv = -1;
+	struct timeval tv;
 
 	fd = open("/dev/urandom", O_RDONLY);
 	if (fd >= 0) {
@@ -159,7 +161,8 @@ get_random_id(void) {
 		return v;
 
 	/* Fallback: sucks, but better than nothing */
-	return (unsigned int)(getpid() + time(NULL));
+	gettimeofday(&tv, NULL);
+	return (unsigned int)(tv.tv_sec + (tv.tv_usec << 12) + getpid());
 }
 
 /* normally O_RDWR, -l option gives O_RDONLY */
-- 
1.5.3.7

-
To unsubscribe from this list: send the line "unsubscribe util-linux-ng" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux