]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
fdisk: use randutils for mbr signature creation
authorDavidlohr Bueso <dave@gnu.org>
Thu, 5 Apr 2012 21:52:07 +0000 (23:52 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 10 Apr 2012 11:15:32 +0000 (13:15 +0200)
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
fdisk/Makefile.am
fdisk/fdisk.c

index 9fbfc79bb252802492e72c52c5d9482a1f3d0d10..f6e9a4b2a51ffad4cca082e02c610d297d681ebd 100644 (file)
@@ -10,6 +10,7 @@ fdisk_common = \
        $(top_srcdir)/lib/blkdev.c \
        $(top_srcdir)/lib/mbsalign.c \
        $(top_srcdir)/lib/strutils.c \
+       $(top_srcdir)/lib/randutils.c \
        $(top_srcdir)/lib/wholedisk.c
 
 if LINUX
index 126efb37821b8e167e90d5e0770fc7d3de53b512..7c1486184ab856992b664008968da6f9095ac712 100644 (file)
@@ -33,6 +33,7 @@
 #include "pathnames.h"
 #include "canonicalize.h"
 #include "strutils.h"
+#include "randutils.h"
 
 #include "fdisksunlabel.h"
 #include "fdisksgilabel.h"
@@ -186,51 +187,6 @@ get_nr_sects(struct partition *p) {
        return read4_little_endian(p->size4);
 }
 
-static ssize_t
-xread(int fd, void *buf, size_t count) {
-        char *p = buf;
-        ssize_t out = 0;
-        ssize_t rv;
-
-        while (count) {
-                rv = read(fd, p, count);
-                if (rv == -1) {
-                        if (errno == EINTR || errno == EAGAIN)
-                                continue;
-                        return out ? out : -1; /* Error */
-                } else if (rv == 0) {
-                        return out; /* EOF */
-                }
-
-                p += rv;
-                out += rv;
-                count -= rv;
-        }
-
-        return out;
-}
-
-static unsigned int
-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) {
-               rv = xread(fd, &v, sizeof v);
-               close(fd);
-       }
-
-       if (rv == sizeof v)
-               return v;
-
-       /* Fallback: sucks, but better than nothing */
-       gettimeofday(&tv, NULL);
-       return (unsigned int)(tv.tv_sec + (tv.tv_usec << 12) + getpid());
-}
-
 /*
  * Raw disk label. For DOS-type partition tables the MBR,
  * with descriptions of the primary partitions.
@@ -876,7 +832,10 @@ static void dos_init(void)
 
 static void
 create_doslabel(void) {
-       unsigned int id = get_random_id();
+       unsigned int id;
+
+       /* random disk signature */
+       random_get_bytes(&id, sizeof(id));
 
        fprintf(stderr, _("Building a new DOS disklabel with disk identifier 0x%08x.\n"), id);
        sun_nolabel();  /* otherwise always recognised as sun */