summaryrefslogtreecommitdiffstats
path: root/fdisk/fdisk.c
diff options
context:
space:
mode:
authorH. Peter Anvin2008-01-16 19:53:56 +0100
committerKarel Zak2008-01-28 14:55:04 +0100
commitf26873dc4ded29660c89e53e303d39555d3569b5 (patch)
tree23b47786acefda9a2b360b7b5d35ce0401169e39 /fdisk/fdisk.c
parentlogin: fix a small memory leak and remove unnecessary zeroing (diff)
downloadkernel-qcow2-util-linux-f26873dc4ded29660c89e53e303d39555d3569b5.tar.gz
kernel-qcow2-util-linux-f26873dc4ded29660c89e53e303d39555d3569b5.tar.xz
kernel-qcow2-util-linux-f26873dc4ded29660c89e53e303d39555d3569b5.zip
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" <hpa@zytor.com>
Diffstat (limited to 'fdisk/fdisk.c')
-rw-r--r--fdisk/fdisk.c5
1 files changed, 4 insertions, 1 deletions
diff --git a/fdisk/fdisk.c b/fdisk/fdisk.c
index cc71c5c49..ede41b3e6 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 */