]> git.ipfire.org Git - thirdparty/mdadm.git/commitdiff
Make sure to seed the random number generator for uuids
authorNeil Brown <neilb@suse.de>
Tue, 7 Jun 2005 23:03:48 +0000 (23:03 +0000)
committerNeil Brown <neilb@suse.de>
Tue, 7 Jun 2005 23:03:48 +0000 (23:03 +0000)
Signed-off-by: Neil Brown <neilb@cse.unsw.edu.au>
mdadm.c
super0.c
super1.c

diff --git a/mdadm.c b/mdadm.c
index d8d46ee835fd7fc1f464992cca9a0729806fcf3b..c8b8e4704d6b099c644181a80b87c22f9916d54b 100644 (file)
--- a/mdadm.c
+++ b/mdadm.c
@@ -93,6 +93,8 @@ int main(int argc, char *argv[])
 
        int mdfd = -1;
 
+       srandom(time(0) ^ getpid());
+
        ident.uuid_set=0;
        ident.level = UnSet;
        ident.raid_disks = UnSet;
index d6619d50711e9c750d68325b31be4b6db60aa7ca..2b571473c4cbc735e033416fc20b4785ba273250 100644 (file)
--- a/super0.c
+++ b/super0.c
@@ -326,6 +326,7 @@ static int init_super0(void **sbp, mdu_array_info_t *info)
 {
        mdp_super_t *sb = malloc(MD_SB_BYTES);
        int spares;
+       int rfd;
        memset(sb, 0, MD_SB_BYTES);
 
        if (info->major_version == -1) {
@@ -340,12 +341,14 @@ static int init_super0(void **sbp, mdu_array_info_t *info)
                return 0;
        }
 
+       rfd = open("/dev/urandom", O_RDONLY);
        sb->md_magic = MD_SB_MAGIC;
        sb->major_version = 0;
        sb->minor_version = 90;
        sb->patch_version = 0;
        sb->gvalid_words = 0; /* ignored */
-       sb->set_uuid0 = random();
+       if (rfd < 0 || read(rfd, &sb->set_uuid0, 4) != 4)
+               sb->set_uuid0 = random();
        sb->ctime = time(0);
        sb->level = info->level;
        sb->size = info->size;
@@ -353,9 +356,13 @@ static int init_super0(void **sbp, mdu_array_info_t *info)
        sb->raid_disks = info->raid_disks;
        sb->md_minor = info->md_minor;
        sb->not_persistent = 0;
-       sb->set_uuid1 = random();
-       sb->set_uuid2 = random();
-       sb->set_uuid3 = random();
+       if (rfd < 0 || read(rfd, &sb->set_uuid1, 12) != 12) {
+               sb->set_uuid1 = random();
+               sb->set_uuid2 = random();
+               sb->set_uuid3 = random();
+       }
+       if (rfd >= 0)
+               close(rfd);
 
        sb->utime = sb->ctime;
        sb->state = info->state;
index 751e28d59908aa9247daa349086762b14a56c70d..c5813b9c4e04542d6871c5e5bbe047241daf838d 100644 (file)
--- a/super1.c
+++ b/super1.c
@@ -466,6 +466,7 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
        struct mdp_superblock_1 *sb = sbv;
        struct mdp_superblock_1 *refsb = NULL;
        int fd = open(devname, O_RDWR | O_EXCL);
+       int rfd;
        int rv;
 
        long size;
@@ -480,10 +481,14 @@ static int write_init_super1(struct supertype *st, void *sbv, mdu_disk_info_t *d
 
        sb->dev_number = __cpu_to_le32(dinfo->number);
 
-       *(__u32*)(sb->device_uuid) = random();
-       *(__u32*)(sb->device_uuid+4) = random();
-       *(__u32*)(sb->device_uuid+8) = random();
-       *(__u32*)(sb->device_uuid+12) = random();
+       if ((rfd = open("/dev/urandom", O_RDONLY)) < 0 ||
+           read(rfd, sb->device_uuid, 16) != 16) {
+               *(__u32*)(sb->device_uuid) = random();
+               *(__u32*)(sb->device_uuid+4) = random();
+               *(__u32*)(sb->device_uuid+8) = random();
+               *(__u32*)(sb->device_uuid+12) = random();
+       }
+       if (rfd >= 0) close(rfd);
        sb->events = 0;
 
        if (load_super1(st, fd, (void**)&refsb, NULL)==0) {