* [PATCH 3/3] libuuid: use randutils
@ 2012-04-05 21:52 Davidlohr Bueso
2012-04-10 11:31 ` Karel Zak
0 siblings, 1 reply; 4+ messages in thread
From: Davidlohr Bueso @ 2012-04-05 21:52 UTC (permalink / raw)
To: Karel Zak; +Cc: util-linux
From: Davidlohr Bueso <dave@gnu.org>
Signed-off-by: Davidlohr Bueso <dave@gnu.org>
---
libuuid/src/Makefile.am | 3 +-
libuuid/src/gen_uuid.c | 99 ++--------------------------------------------
2 files changed, 7 insertions(+), 95 deletions(-)
diff --git a/libuuid/src/Makefile.am b/libuuid/src/Makefile.am
index 66ab2d4..8e87210 100644
--- a/libuuid/src/Makefile.am
+++ b/libuuid/src/Makefile.am
@@ -25,7 +25,8 @@ libuuid_la_SOURCES = \
uuidd.h \
uuidP.h \
uuid_time.c \
- $(uuidinc_HEADERS)
+ $(uuidinc_HEADERS) \
+ $(top_srcdir)/lib/randutils.c
libuuid_la_DEPENDENCIES = uuid.sym
diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c
index 492bb75..9bcd62c 100644
--- a/libuuid/src/gen_uuid.c
+++ b/libuuid/src/gen_uuid.c
@@ -93,11 +93,7 @@
#include "uuidP.h"
#include "uuidd.h"
-
-#ifdef HAVE_SRANDOM
-#define srand(x) srandom(x)
-#define rand() random()
-#endif
+#include "randutils.h"
#ifdef HAVE_TLS
#define THREAD_LOCAL static __thread
@@ -105,11 +101,6 @@
#define THREAD_LOCAL static
#endif
-#if defined(__linux__) && defined(__NR_gettid) && defined(HAVE_JRAND48)
-#define DO_JRAND_MIX
-THREAD_LOCAL unsigned short jrand_seed[3];
-#endif
-
#ifdef _WIN32
static void gettimeofday (struct timeval *tv, void *dummy)
{
@@ -134,86 +125,6 @@ static int getuid (void)
}
#endif
-static int get_random_fd(void)
-{
- struct timeval tv;
- static int fd = -2;
- int i;
-
- if (fd == -2) {
- gettimeofday(&tv, 0);
-#ifndef _WIN32
- fd = open("/dev/urandom", O_RDONLY);
- if (fd == -1)
- fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
- if (fd >= 0) {
- i = fcntl(fd, F_GETFD);
- if (i >= 0)
- fcntl(fd, F_SETFD, i | FD_CLOEXEC);
- }
-#endif
- srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
-#ifdef DO_JRAND_MIX
- jrand_seed[0] = getpid() ^ (tv.tv_sec & 0xFFFF);
- jrand_seed[1] = getppid() ^ (tv.tv_usec & 0xFFFF);
- jrand_seed[2] = (tv.tv_sec ^ tv.tv_usec) >> 16;
-#endif
- }
- /* Crank the random number generator a few times */
- gettimeofday(&tv, 0);
- for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
- rand();
- return fd;
-}
-
-
-/*
- * Generate a series of random bytes. Use /dev/urandom if possible,
- * and if not, use srandom/random.
- */
-static void get_random_bytes(void *buf, int nbytes)
-{
- int i, n = nbytes, fd = get_random_fd();
- int lose_counter = 0;
- unsigned char *cp = (unsigned char *) buf;
-
- if (fd >= 0) {
- while (n > 0) {
- i = read(fd, cp, n);
- if (i <= 0) {
- if (lose_counter++ > 16)
- break;
- continue;
- }
- n -= i;
- cp += i;
- lose_counter = 0;
- }
- }
-
- /*
- * We do this all the time, but this is the only source of
- * randomness if /dev/random/urandom is out to lunch.
- */
- for (cp = buf, i = 0; i < nbytes; i++)
- *cp++ ^= (rand() >> 7) & 0xFF;
-
-#ifdef DO_JRAND_MIX
- {
- unsigned short tmp_seed[3];
-
- memcpy(tmp_seed, jrand_seed, sizeof(tmp_seed));
- jrand_seed[2] = jrand_seed[2] ^ syscall(__NR_gettid);
- for (cp = buf, i = 0; i < nbytes; i++)
- *cp++ ^= (jrand48(tmp_seed) >> 7) & 0xFF;
- memcpy(jrand_seed, tmp_seed,
- sizeof(jrand_seed)-sizeof(unsigned short));
- }
-#endif
-
- return;
-}
-
/*
* Get the ethernet hardware address, if we can find it...
*
@@ -370,7 +281,7 @@ static int get_clock(uint32_t *clock_high, uint32_t *clock_low,
}
if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
- get_random_bytes(&clock_seq, sizeof(clock_seq));
+ random_get_bytes(&clock_seq, sizeof(clock_seq));
clock_seq &= 0x3FFF;
gettimeofday(&last, 0);
last.tv_sec--;
@@ -577,7 +488,7 @@ int __uuid_generate_time(uuid_t out, int *num)
if (!has_init) {
if (get_node_id(node_id) <= 0) {
- get_random_bytes(node_id, 6);
+ random_get_bytes(node_id, 6);
/*
* Set multicast bit, to prevent conflicts
* with IEEE 802 addresses obtained from
@@ -675,7 +586,7 @@ void __uuid_generate_random(uuid_t out, int *num)
n = *num;
for (i = 0; i < n; i++) {
- get_random_bytes(buf, sizeof(buf));
+ random_get_bytes(buf, sizeof(buf));
uuid_unpack(buf, &uu);
uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
@@ -703,7 +614,7 @@ void uuid_generate_random(uuid_t out)
*/
void uuid_generate(uuid_t out)
{
- if (get_random_fd() >= 0)
+ if (random_get_fd() >= 0)
uuid_generate_random(out);
else
uuid_generate_time(out);
--
1.7.4.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 3/3] libuuid: use randutils
2012-04-05 21:52 [PATCH 3/3] libuuid: use randutils Davidlohr Bueso
@ 2012-04-10 11:31 ` Karel Zak
2012-04-10 12:20 ` Petr Uzel
2012-05-03 19:04 ` Petr Uzel
0 siblings, 2 replies; 4+ messages in thread
From: Karel Zak @ 2012-04-10 11:31 UTC (permalink / raw)
To: Davidlohr Bueso; +Cc: util-linux, Petr Uzel
On Thu, Apr 05, 2012 at 11:52:10PM +0200, Davidlohr Bueso wrote:
> libuuid/src/Makefile.am | 3 +-
> libuuid/src/gen_uuid.c | 99 ++--------------------------------------------
> 2 files changed, 7 insertions(+), 95 deletions(-)
Applied, thanks.
> @@ -703,7 +614,7 @@ void uuid_generate_random(uuid_t out)
> */
> void uuid_generate(uuid_t out)
> {
> - if (get_random_fd() >= 0)
> + if (random_get_fd() >= 0)
> uuid_generate_random(out);
This sucks:
- because we call random_get_fd() in __uuid_generate_random again:
$ strace -e open ./misc-utils/uuidgen
open("/dev/urandom", O_RDONLY) = 3
open("/dev/urandom", O_RDONLY) = 4
it would be better to add 'fd' argument to __uuid_generate_random(),
use it in uuid_generate() and reuse the file descriptor if >= 0.
We use __uuid_generate_random in the library and in uuidd, so we
don't have to care about API there.
- it's file descriptor leak in shared library...
Volunteers? ;-)
Karel
--
Karel Zak <kzak@redhat.com>
http://karelzak.blogspot.com
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 3/3] libuuid: use randutils
2012-04-10 11:31 ` Karel Zak
@ 2012-04-10 12:20 ` Petr Uzel
2012-05-03 19:04 ` Petr Uzel
1 sibling, 0 replies; 4+ messages in thread
From: Petr Uzel @ 2012-04-10 12:20 UTC (permalink / raw)
To: util-linux; +Cc: Davidlohr Bueso
On Tue, Apr 10, 2012 at 01:31:50PM +0200, Karel Zak wrote:
> On Thu, Apr 05, 2012 at 11:52:10PM +0200, Davidlohr Bueso wrote:
> > libuuid/src/Makefile.am | 3 +-
> > libuuid/src/gen_uuid.c | 99 ++--------------------------------------------
> > 2 files changed, 7 insertions(+), 95 deletions(-)
>
> Applied, thanks.
>
> > @@ -703,7 +614,7 @@ void uuid_generate_random(uuid_t out)
> > */
> > void uuid_generate(uuid_t out)
> > {
> > - if (get_random_fd() >= 0)
> > + if (random_get_fd() >= 0)
> > uuid_generate_random(out);
>
> This sucks:
>
> - because we call random_get_fd() in __uuid_generate_random again:
>
> $ strace -e open ./misc-utils/uuidgen
> open("/dev/urandom", O_RDONLY) = 3
> open("/dev/urandom", O_RDONLY) = 4
>
> it would be better to add 'fd' argument to __uuid_generate_random(),
> use it in uuid_generate() and reuse the file descriptor if >= 0.
>
> We use __uuid_generate_random in the library and in uuidd, so we
> don't have to care about API there.
>
> - it's file descriptor leak in shared library...
>
> Volunteers? ;-)
If nobody steps out, I can include this in my uuidd systemd-ization
series (which I'll hopefully finish this week).
Petr
--
Petr Uzel
IRC: ptr_uzl @ freenode
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 3/3] libuuid: use randutils
2012-04-10 11:31 ` Karel Zak
2012-04-10 12:20 ` Petr Uzel
@ 2012-05-03 19:04 ` Petr Uzel
1 sibling, 0 replies; 4+ messages in thread
From: Petr Uzel @ 2012-05-03 19:04 UTC (permalink / raw)
To: util-linux
[-- Attachment #1: Type: text/plain, Size: 1353 bytes --]
On Tue, Apr 10, 2012 at 01:31:50PM +0200, Karel Zak wrote:
> On Thu, Apr 05, 2012 at 11:52:10PM +0200, Davidlohr Bueso wrote:
> > libuuid/src/Makefile.am | 3 +-
> > libuuid/src/gen_uuid.c | 99 ++--------------------------------------------
> > 2 files changed, 7 insertions(+), 95 deletions(-)
>
> Applied, thanks.
>
> > @@ -703,7 +614,7 @@ void uuid_generate_random(uuid_t out)
> > */
> > void uuid_generate(uuid_t out)
> > {
> > - if (get_random_fd() >= 0)
> > + if (random_get_fd() >= 0)
> > uuid_generate_random(out);
>
> This sucks:
>
> - because we call random_get_fd() in __uuid_generate_random again:
>
> $ strace -e open ./misc-utils/uuidgen
> open("/dev/urandom", O_RDONLY) = 3
> open("/dev/urandom", O_RDONLY) = 4
>
> it would be better to add 'fd' argument to __uuid_generate_random(),
> use it in uuid_generate() and reuse the file descriptor if >= 0.
>
> We use __uuid_generate_random in the library and in uuidd, so we
> don't have to care about API there.
>
> - it's file descriptor leak in shared library...
>
> Volunteers? ;-)
Patch addressing this is included in the uuidd series I sent shortly
before. But please check the note about it in the series cover letter.
Thanks,
Petr
--
Petr Uzel
IRC: ptr_uzl @ freenode
[-- Attachment #2: Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2012-05-03 19:04 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-04-05 21:52 [PATCH 3/3] libuuid: use randutils Davidlohr Bueso
2012-04-10 11:31 ` Karel Zak
2012-04-10 12:20 ` Petr Uzel
2012-05-03 19:04 ` Petr Uzel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox