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>
This commit is contained in:
H. Peter Anvin 2008-01-16 13:53:56 -05:00 committed by Karel Zak
parent 30568eec59
commit f26873dc4d
1 changed files with 4 additions and 1 deletions

View File

@ -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 */