From f26873dc4ded29660c89e53e303d39555d3569b5 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Wed, 16 Jan 2008 13:53:56 -0500 Subject: [PATCH] fdisk: better fallback for get_random_id() 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. Signed-off-by: H. Peter Anvin" --- fdisk/fdisk.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c index cc71c5c4..ede41b3e 100644 --- a/fdisk/fdisk.c +++ b/fdisk/fdisk.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #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 */ -- 2.39.5