diff --git a/src/utils.c b/src/utils.c index 55450d3c413676a4c2ebceb65a1f8b82c7d2f5c2..2fc26db88dbe2717f1d7eb7cf03f2b0630c6e9f4 100644 --- a/src/utils.c +++ b/src/utils.c @@ -544,3 +544,34 @@ pv_async_signal_safe_error (const char *message, _exit (exit_status); } + +#define PROC_SYS_KERNEL_RANDOM_UUID "/proc/sys/kernel/random/uuid" + +/** + * pv_get_random_uuid: + * @error: Used to raise an error on failure + * + * Return a random UUID (RFC 4122 version 4) as a string. + * It is a 128-bit quantity, with 122 bits of entropy, and 6 fixed bits + * indicating the "variant" (type, 0b10) and "version" (subtype, 0b0100). + * + * Returns: (transfer full): A random UUID, or %NULL on error + */ +gchar * +pv_get_random_uuid (GError **error) +{ + g_autofree gchar *contents = NULL; + + if (!g_file_get_contents (PROC_SYS_KERNEL_RANDOM_UUID, + &contents, NULL, error)) + return NULL; + + g_strchomp (contents); /* delete trailing newline */ + + /* Format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx */ + if (strlen (contents) != 36) + return glnx_null_throw (error, "%s not in expected format", + PROC_SYS_KERNEL_RANDOM_UUID); + + return g_steal_pointer (&contents); +} diff --git a/src/utils.h b/src/utils.h index 49c065ef3fb386195bf2401745b136272eb10926..d4215d0f6688dac03a78a2caa80840764ccc69b7 100644 --- a/src/utils.h +++ b/src/utils.h @@ -58,3 +58,5 @@ FILE *pv_divert_stdout_to_stderr (GError **error); void pv_async_signal_safe_error (const char *message, int exit_status) G_GNUC_NORETURN; + +gchar *pv_get_random_uuid (GError **error);