From dea3c49ce2567ee823c3fda92c7b882a4572cab9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 18 Dec 2016 17:58:30 +0100 Subject: [PATCH] utils: add nm_utils_get_boot_id() util to read "/proc/sys/kernel/random/boot_id" --- src/nm-core-utils.c | 33 +++++++++++++++++++++++++++++++++ src/nm-core-utils.h | 2 ++ 2 files changed, 35 insertions(+) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index f2d55652eb..b5fbfe34f8 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -3040,6 +3040,39 @@ out: return NULL; } +/*****************************************************************************/ + +const char * +nm_utils_get_boot_id (void) +{ + static const char *boot_id; + + if (G_UNLIKELY (!boot_id)) { + gs_free char *contents = NULL; + + nm_utils_file_get_contents (-1, "/proc/sys/kernel/random/boot_id", 0, + &contents, NULL, NULL); + if (contents) { + g_strstrip (contents); + if (contents[0]) { + /* clone @contents because we keep @boot_id until the program + * ends. + * nm_utils_file_get_contents() likely allocated a larger + * buffer chunk initially and (although using realloc to shrink + * the buffer) it might not be best to keep this memory + * around. */ + boot_id = g_strdup (contents); + } + } + if (!boot_id) + boot_id = nm_utils_uuid_generate (); + } + + return boot_id; +} + +/*****************************************************************************/ + /* Returns the "u" (universal/local) bit value for a Modified EUI-64 */ static gboolean get_gre_eui64_u_bit (guint32 addr) diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 66bcdc29e1..dc5f6a8f4d 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -322,6 +322,8 @@ gboolean nm_utils_machine_id_parse (const char *id_str, /*uuid_t*/ guchar *out_u guint8 *nm_utils_secret_key_read (gsize *out_key_len, GError **error); +const char *nm_utils_get_boot_id (void); + /* IPv6 Interface Identifer helpers */ /**