uuid_generate_random_safe() cannot really fail but it may use a weak random number generator as fallback. --- libuuid/src/gen_uuid.c | 32 +++++++++++++++++++++++++++++--- libuuid/src/uuid.h | 4 +++- libuuid/src/uuid.sym | 2 ++ misc-utils/uuidgen.c | 10 ++++++++-- 4 files changed, 42 insertions(+), 6 deletions(-) diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c index f4c8997..0c81a5c 100644 --- a/libuuid/src/gen_uuid.c +++ b/libuuid/src/gen_uuid.c @@ -693,6 +693,18 @@ void uuid_generate_random(uuid_t out) __uuid_generate_random(out, &num); } +int uuid_generate_random_safe(uuid_t out) +{ + int num = 1; + /* No real reason to use the daemon for random uuid's -- yet */ + + if (get_random_fd() < 0) + return -1; + + __uuid_generate_random(out, &num); + return 0; +} + /* * This is the generic front-end to uuid_generate_random and @@ -700,10 +712,24 @@ void uuid_generate_random(uuid_t out) * /dev/urandom is available, since otherwise we won't have * high-quality randomness. */ -void uuid_generate(uuid_t out) +static int _uuid_generate(uuid_t out) { if (get_random_fd() >= 0) - uuid_generate_random(out); + { + int num = 1; + __uuid_generate_random(out, &num); + return 0; + } else - uuid_generate_time(out); + return uuid_generate_time_generic(out); +} + +void uuid_generate(uuid_t out) +{ + (void)_uuid_generate(out); +} + +int uuid_generate_safe(uuid_t out) +{ + return _uuid_generate(out); } diff --git a/libuuid/src/uuid.h b/libuuid/src/uuid.h index 874d65a..2771255 100644 --- a/libuuid/src/uuid.h +++ b/libuuid/src/uuid.h @@ -79,7 +79,9 @@ void uuid_copy(uuid_t dst, const uuid_t src); void uuid_generate(uuid_t out); void uuid_generate_random(uuid_t out); void uuid_generate_time(uuid_t out); -int uuid_generate_time_safe(uuid_t out); +int uuid_generate_time_safe(uuid_t out) __attribute__((warn_unused_result)); +int uuid_generate_random_safe(uuid_t out) __attribute__((warn_unused_result)); +int uuid_generate_safe(uuid_t out) __attribute__((warn_unused_result)); /* isnull.c */ int uuid_is_null(const uuid_t uu); diff --git a/libuuid/src/uuid.sym b/libuuid/src/uuid.sym index 2cad51b..aec79e7 100644 --- a/libuuid/src/uuid.sym +++ b/libuuid/src/uuid.sym @@ -30,6 +30,8 @@ global: UUID_2.20 { global: uuid_generate_time_safe; + uuid_generate_random_safe; + uuid_generate_safe; } UUID_1.0; diff --git a/misc-utils/uuidgen.c b/misc-utils/uuidgen.c index b83b124..e81e065 100644 --- a/misc-utils/uuidgen.c +++ b/misc-utils/uuidgen.c @@ -81,10 +81,16 @@ main (int argc, char *argv[]) } break; case DO_TYPE_RANDOM: - uuid_generate_random(uu); + if (uuid_generate_random_safe(uu) && !force) { + fprintf(stderr, _("/dev/[u]random unavailable.\n")); + exit(1); + } break; default: - uuid_generate(uu); + if (uuid_generate_safe(uu) && !force) { + fprintf(stderr, _("/dev/[u]random unavailable and uuidd not running.\n")); + exit(1); + } break; } -- 1.7.3.4 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html