]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libuuid: support non-cached scenarios (when -lpthread is unavailable)
authorKarel Zak <kzak@redhat.com>
Mon, 27 Jan 2025 13:28:36 +0000 (14:28 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 27 Jan 2025 13:28:36 +0000 (14:28 +0100)
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 <kzak@redhat.com>
libuuid/src/gen_uuid.c

index 106af95f6c7ca64ae5a7795a67fc572764c390d2..763abeb9eafa6480d9f26f9b8c532182beb7ec34 100644 (file)
 #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H)
 #include <sys/syscall.h>
 #endif
-#include <pthread.h>
+#ifdef HAVE_LIBPTHREAD
+# include <pthread.h>
+#endif
+
 #include <signal.h>
 
 #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);
 }