The existing generate_random_id() in libparted only produces numbers in
the range 0..999999, less than 20 bits.  This patch tries to get a
random number from /dev/urandom if available, and otherwise produces a
32-bit value from gettimeofday().

Signed-off-by: H. Peter Anvin <[EMAIL PROTECTED]>

diff --git a/libparted/labels/dos.c b/libparted/labels/dos.c
index 793744e..f085574 100644
--- a/libparted/labels/dos.c
+++ b/libparted/labels/dos.c
@@ -20,6 +20,8 @@
 
 #include <config.h>
 
+#include <fcntl.h>
+#include <unistd.h>
 #include <sys/time.h>
 #include <stdbool.h>
 #include <parted/parted.h>
@@ -1116,14 +1118,27 @@ write_extended_partitions (const PedDisk* disk)
 		return write_empty_table (disk, ext_part->geom.start);
 }
 
-static inline uint32_t generate_random_id (void)
+static uint32_t
+generate_random_id (void)
 {
+	int fd;
 	struct timeval tv;
 	int rc;
+	uint32_t v;
+	
+	fd = open("/dev/urandom", O_RDONLY);
+	if (fd >= 0) {
+		rc = read(fd, &v, sizeof v);
+		close(fd);
+
+		if (rc == sizeof v)
+			return v;
+	}
+
 	rc = gettimeofday(&tv, NULL);
 	if (rc == -1)
-		return 0;
-	return (uint32_t)(tv.tv_usec & 0xFFFFFFFFUL);
+		return getpid(); /* Better than nothing... */
+	return (uint32_t)(tv.tv_sec*1000000+tv.tv_usec);
 }
 
 static int

_______________________________________________
parted-devel mailing list
[email protected]
http://lists.alioth.debian.org/mailman/listinfo/parted-devel

Reply via email to