1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

Add generic function to read /dev/urandom, used in uuid calculation.

This commit is contained in:
Dave Wysochanski 2008-12-07 04:23:37 +00:00
parent 8d2f8d7b6b
commit b721056e04
4 changed files with 34 additions and 12 deletions

View File

@ -1,5 +1,6 @@
Version 2.02.44 - Version 2.02.44 -
==================================== ====================================
Add generic function to read /dev/urandom, used in uuid calculation.
Use displayable_lvs_in_vg and lv_is_displayable for consistency throughout. Use displayable_lvs_in_vg and lv_is_displayable for consistency throughout.
Fix race in vgcreate that would result in second caller overwriting first. Fix race in vgcreate that would result in second caller overwriting first.
Fix uninitialised lv_count in vgdisplay -c. Fix uninitialised lv_count in vgdisplay -c.

View File

@ -15,8 +15,34 @@
#include "lib.h" #include "lib.h"
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
int lvm_getpagesize(void) int lvm_getpagesize(void)
{ {
return getpagesize(); return getpagesize();
} }
int read_urandom(void *buf, size_t len)
{
int fd;
/* FIXME: we should stat here, and handle other cases */
/* FIXME: use common _io() routine's open/read/close */
if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
log_sys_error("open", "read_urandom: /dev/urandom");
return 0;
}
if (read(fd, buf, len) != (ssize_t) len) {
log_sys_error("read", "read_urandom: /dev/urandom");
if (close(fd))
stack;
return 0;
}
if (close(fd))
stack;
return 1;
}

View File

@ -18,4 +18,9 @@
int lvm_getpagesize(void); int lvm_getpagesize(void);
/*
* Read 'len' bytes of entropy from /dev/urandom and store in 'buf'.
*/
int read_urandom(void *buf, size_t len);
#endif #endif

View File

@ -15,6 +15,7 @@
#include "lib.h" #include "lib.h"
#include "uuid.h" #include "uuid.h"
#include "lvm-wrappers.h"
#include <assert.h> #include <assert.h>
#include <sys/stat.h> #include <sys/stat.h>
@ -94,25 +95,14 @@ int lvid_in_restricted_range(union lvid *lvid)
int id_create(struct id *id) int id_create(struct id *id)
{ {
int randomfile;
unsigned i; unsigned i;
size_t len = sizeof(id->uuid); size_t len = sizeof(id->uuid);
memset(id->uuid, 0, len); memset(id->uuid, 0, len);
if ((randomfile = open("/dev/urandom", O_RDONLY)) < 0) { if (!read_urandom(&id->uuid, len)) {
log_sys_error("open", "id_create: /dev/urandom");
return 0; return 0;
} }
if (read(randomfile, id->uuid, len) != (ssize_t) len) {
log_sys_error("read", "id_create: /dev/urandom");
if (close(randomfile))
stack;
return 0;
}
if (close(randomfile))
stack;
/* /*
* Skip out the last 2 chars in randomized creation for LVM1 * Skip out the last 2 chars in randomized creation for LVM1
* backwards compatibility. * backwards compatibility.