diff options
author | Petr Uzel | 2012-05-06 21:55:53 +0200 |
---|---|---|
committer | Karel Zak | 2012-05-10 11:43:49 +0200 |
commit | c544aa2c25095e6a4d4fca761eb15c46435086fc (patch) | |
tree | c188e1c320037328b009cf9cb6fa252fdeb1a2ad /libuuid/src | |
parent | lslocks: fix bracket indentation (diff) | |
download | kernel-qcow2-util-linux-c544aa2c25095e6a4d4fca761eb15c46435086fc.tar.gz kernel-qcow2-util-linux-c544aa2c25095e6a4d4fca761eb15c46435086fc.tar.xz kernel-qcow2-util-linux-c544aa2c25095e6a4d4fca761eb15c46435086fc.zip |
libuuid: avoid double open and leaking fd (reworked)
This reverts commit 6126f7a53c57485a9a29ddd772765695f23c92e6
and fixes the double open and leaking descriptor in a different way,
that is by using newly introduced function 'have_random_source()'
to check whether good random source is available while deciding
which uuid type to generate (random/time).
This is better than calling random_get_fd() twice, passing the file
descriptor down the stack and reusing it in next call to
random_get_fd().
Signed-off-by: Petr Uzel <petr.uzel@suse.cz>
Diffstat (limited to 'libuuid/src')
-rw-r--r-- | libuuid/src/gen_uuid.c | 28 | ||||
-rw-r--r-- | libuuid/src/uuidd.h | 2 |
2 files changed, 19 insertions, 11 deletions
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c index 76f1bbdad..93d292a22 100644 --- a/libuuid/src/gen_uuid.c +++ b/libuuid/src/gen_uuid.c @@ -277,7 +277,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low, } if ((last.tv_sec == 0) && (last.tv_usec == 0)) { - random_get_bytes(&clock_seq, sizeof(clock_seq), -1); + random_get_bytes(&clock_seq, sizeof(clock_seq)); clock_seq &= 0x3FFF; gettimeofday(&last, 0); last.tv_sec--; @@ -434,7 +434,7 @@ int __uuid_generate_time(uuid_t out, int *num) if (!has_init) { if (get_node_id(node_id) <= 0) { - random_get_bytes(node_id, 6, -1); + random_get_bytes(node_id, 6); /* * Set multicast bit, to prevent conflicts * with IEEE 802 addresses obtained from @@ -520,7 +520,7 @@ int uuid_generate_time_safe(uuid_t out) } -void __uuid_generate_random(uuid_t out, int *num, int fd) +void __uuid_generate_random(uuid_t out, int *num) { uuid_t buf; struct uuid uu; @@ -532,7 +532,7 @@ void __uuid_generate_random(uuid_t out, int *num, int fd) n = *num; for (i = 0; i < n; i++) { - random_get_bytes(buf, sizeof(buf), fd); + random_get_bytes(buf, sizeof(buf)); uuid_unpack(buf, &uu); uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000; @@ -548,7 +548,18 @@ void uuid_generate_random(uuid_t out) int num = 1; /* No real reason to use the daemon for random uuid's -- yet */ - __uuid_generate_random(out, &num, -1); + __uuid_generate_random(out, &num); +} + +/* + * Check whether good random source (/dev/random or /dev/urandom) + * is available. + */ +static int have_random_source(void) +{ + struct stat s; + + return (!stat("/dev/random", &s) || !stat("/dev/urandom", &s)); } @@ -560,11 +571,8 @@ void uuid_generate_random(uuid_t out) */ void uuid_generate(uuid_t out) { - int fd; - int num = 1; - - if ((fd = random_get_fd()) >= 0) - __uuid_generate_random(out, &num, fd); + if (have_random_source()) + uuid_generate_random(out); else uuid_generate_time(out); } diff --git a/libuuid/src/uuidd.h b/libuuid/src/uuidd.h index 2e19522df..27b79c216 100644 --- a/libuuid/src/uuidd.h +++ b/libuuid/src/uuidd.h @@ -49,6 +49,6 @@ #define UUIDD_MAX_OP UUIDD_OP_BULK_RANDOM_UUID extern int __uuid_generate_time(uuid_t out, int *num); -extern void __uuid_generate_random(uuid_t out, int *num, int fd); +extern void __uuid_generate_random(uuid_t out, int *num); #endif /* _UUID_UUID_H */ |