From bec26c732889b24fb6725e949e181bcf56d23c76 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 27 Jan 2025 14:28:36 +0100 Subject: [PATCH] libuuid: support non-cached scenarios (when -lpthread is unavailable) This patch makes the dependence on pthread optional for libuuid. In certain cases, such as Buildroot Linux, uClibc-ng, and very low resource systems, libpthread may be unavailable. If libuuid is compiled without pthread, it will not use a local cache and will instead request a UUID from uuidd for each call. This may result in less efficient performance, but the UUIDs generated will still be unique and reliable. On minimalistic systems, it is highly likely that uuidd will not be installed, making this change important for portability and robust code. Addresses: https://github.com/util-linux/util-linux/pull/3375 Signed-off-by: Karel Zak --- libuuid/src/gen_uuid.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c index 106af95f6..763abeb9e 100644 --- a/libuuid/src/gen_uuid.c +++ b/libuuid/src/gen_uuid.c @@ -80,7 +80,10 @@ #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) #include #endif -#include +#ifdef HAVE_LIBPTHREAD +# include +#endif + #include #include "all-io.h" @@ -599,8 +602,7 @@ static void __uuid_set_variant_and_version(uuid_t uuid, int version) * If neither of these is possible (e.g. because of insufficient permissions), it generates * the UUID anyway, but returns -1. Otherwise, returns 0. */ - -/* thread local cache for uuidd based requests */ +#ifdef HAVE_LIBPTHREAD THREAD_LOCAL struct { int num; int cache_size; @@ -616,8 +618,10 @@ static void reset_uuidd_cache(void) memset(&uuidd_cache, 0, sizeof(uuidd_cache)); uuidd_cache.cache_size = CS_MIN; } +#endif /* HAVE_LIBPTHREAD */ static int uuid_generate_time_generic(uuid_t out) { +#ifdef HAVE_LIBPTHREAD static volatile sig_atomic_t atfork_registered; time_t now; @@ -670,6 +674,14 @@ static int uuid_generate_time_generic(uuid_t out) { return 0; } +#else /* !HAVE_LIBPTHREAD */ + { + int num = 1; + if (get_uuid_via_daemon(UUIDD_OP_TIME_UUID, out, &num) == 0) + return 0; + } +#endif /* HAVE_LIBPTHREAD */ + return __uuid_generate_time(out, NULL); } -- 2.47.3