summaryrefslogtreecommitdiffstats
path: root/super-ddf.c
diff options
context:
space:
mode:
authorNeilBrown <neilb@suse.de>2009-03-10 16:28:18 +1100
committerNeilBrown <neilb@suse.de>2009-03-10 16:28:18 +1100
commitbfb7ea78e49bd851ee9fb73cac813123cb78b1de (patch)
tree617e54cda3b12d8eca51d958d0915459be52721e /super-ddf.c
parentbceedeec55eeb5fc37065fed39159a04cffc1307 (diff)
downloadmdadm-bfb7ea78e49bd851ee9fb73cac813123cb78b1de.tar.gz
mdadm-bfb7ea78e49bd851ee9fb73cac813123cb78b1de.tar.xz
mdadm-bfb7ea78e49bd851ee9fb73cac813123cb78b1de.zip
ddf: use better random number generation.
use /dev/urandom rather than calls to 'random()' if possible. Signed-off-by: NeilBrown <neilb@suse.de>
Diffstat (limited to 'super-ddf.c')
-rw-r--r--super-ddf.c23
1 files changed, 15 insertions, 8 deletions
diff --git a/super-ddf.c b/super-ddf.c
index 8725eab..009fbfb 100644
--- a/super-ddf.c
+++ b/super-ddf.c
@@ -1493,10 +1493,20 @@ static int update_super_ddf(struct supertype *st, struct mdinfo *info,
return rv;
}
+__u32 random32(void)
+{
+ __u32 rv;
+ int rfd = open("/dev/urandom", O_RDONLY);
+ if (rfd < 0 || read(rfd, &rv, 4) != 4)
+ rv = random();
+ if (rfd >= 0)
+ close(rfd);
+ return rv;
+}
+
static void make_header_guid(char *guid)
{
__u32 stamp;
- int rfd;
/* Create a DDF Header of Virtual Disk GUID */
/* 24 bytes of fiction required.
@@ -1511,11 +1521,8 @@ static void make_header_guid(char *guid)
memcpy(guid+12, &stamp, 4);
stamp = __cpu_to_be32(time(0) - DECADE);
memcpy(guid+16, &stamp, 4);
- rfd = open("/dev/urandom", O_RDONLY);
- if (rfd < 0 || read(rfd, &stamp, 4) != 4)
- stamp = random();
+ stamp = random32();
memcpy(guid+20, &stamp, 4);
- if (rfd >= 0) close(rfd);
}
static int init_super_ddf_bvd(struct supertype *st,
@@ -2148,12 +2155,12 @@ static int add_to_super_ddf(struct supertype *st,
tm = localtime(&now);
sprintf(dd->disk.guid, "%8s%04d%02d%02d",
T10, tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday);
- *(__u32*)(dd->disk.guid + 16) = random();
- *(__u32*)(dd->disk.guid + 20) = random();
+ *(__u32*)(dd->disk.guid + 16) = random32();
+ *(__u32*)(dd->disk.guid + 20) = random32();
do {
/* Cannot be bothered finding a CRC of some irrelevant details*/
- dd->disk.refnum = random();
+ dd->disk.refnum = random32();
for (i = __be16_to_cpu(ddf->active->max_pd_entries) - 1;
i >= 0; i--)
if (ddf->phys->entries[i].refnum == dd->disk.refnum)