summaryrefslogtreecommitdiffstats
path: root/lib/randutils.c
diff options
context:
space:
mode:
authorPetr Uzel2012-05-06 21:55:53 +0200
committerKarel Zak2012-05-10 11:43:49 +0200
commitc544aa2c25095e6a4d4fca761eb15c46435086fc (patch)
treec188e1c320037328b009cf9cb6fa252fdeb1a2ad /lib/randutils.c
parentlslocks: fix bracket indentation (diff)
downloadkernel-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 'lib/randutils.c')
-rw-r--r--lib/randutils.c8
1 files changed, 3 insertions, 5 deletions
diff --git a/lib/randutils.c b/lib/randutils.c
index 0513798a0..b90c88691 100644
--- a/lib/randutils.c
+++ b/lib/randutils.c
@@ -58,15 +58,13 @@ int random_get_fd(void)
* Use /dev/urandom if possible, and if not,
* use glibc pseudo-random functions.
*/
-void random_get_bytes(void *buf, size_t nbytes, int fd)
+void random_get_bytes(void *buf, size_t nbytes)
{
size_t i, n = nbytes;
+ int fd = random_get_fd();
int lose_counter = 0;
unsigned char *cp = (unsigned char *) buf;
- if (fd < 0)
- fd = random_get_fd();
-
if (fd >= 0) {
while (n > 0) {
ssize_t x = read(fd, cp, n);
@@ -113,7 +111,7 @@ int main(int argc, char *argv[])
/* generate and print 10 random numbers */
for (i = 0; i < 10; i++) {
- random_get_bytes(&v, sizeof(v), -1);
+ random_get_bytes(&v, sizeof(v));
printf("%d\n", v);
}