From 8069e5fd208197f1f4aa6ffb5062f2d66416d9bd Mon Sep 17 00:00:00 2001 From: Adrian Freihofer Date: Sun, 15 Mar 2020 11:25:33 +0100 Subject: [PATCH] nm-core-utils: add nm_utils_proc_cmdline Add a new function to read /proc/cmdline. The function caches the content. --- src/nm-core-utils.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/nm-core-utils.h | 2 ++ 2 files changed, 47 insertions(+) diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 075d9cb0e7..73601bbff9 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2741,6 +2741,51 @@ nm_utils_boot_id_bin (void) /*****************************************************************************/ +const char * +nm_utils_proc_cmdline (void) +{ + static const char *volatile proc_cmdline_cached = NULL; + const char *proc_cmdline; + +again: + proc_cmdline = g_atomic_pointer_get (&proc_cmdline_cached); + if (G_UNLIKELY (!proc_cmdline)) { + gs_free char *str = NULL; + + g_file_get_contents ("/proc/cmdline", &str, NULL, NULL); + str = nm_str_realloc (str); + + proc_cmdline = str ?: ""; + if (!g_atomic_pointer_compare_and_exchange (&proc_cmdline_cached, NULL, proc_cmdline)) + goto again; + + g_steal_pointer (&str); + } + + return proc_cmdline; +} + +const char *const* +nm_utils_proc_cmdline_split (void) +{ + static const char *const* volatile proc_cmdline_cached = NULL; + const char *const* proc_cmdline; + +again: + proc_cmdline = g_atomic_pointer_get (&proc_cmdline_cached); + if (G_UNLIKELY (!proc_cmdline)) { + const char *proc_cl_str = nm_utils_proc_cmdline(); + proc_cmdline = (const char *const*) g_strsplit (proc_cl_str, " ", -1); + + if (!g_atomic_pointer_compare_and_exchange (&proc_cmdline_cached, NULL, proc_cmdline)) + goto again; + } + + return proc_cmdline; +} + +/*****************************************************************************/ + /** * nm_utils_arp_type_detect_from_hwaddrlen: * @hwaddr_len: the length of the hardware address in bytes. diff --git a/src/nm-core-utils.h b/src/nm-core-utils.h index 050c2fb5ec..e30d7b3651 100644 --- a/src/nm-core-utils.h +++ b/src/nm-core-utils.h @@ -267,6 +267,8 @@ gboolean nm_utils_machine_id_is_fake (void); const char *nm_utils_boot_id_str (void); const struct _NMUuid *nm_utils_boot_id_bin (void); +const char *nm_utils_proc_cmdline (void); +const char *const*nm_utils_proc_cmdline_split (void); gboolean nm_utils_host_id_get (const guint8 **out_host_id, gsize *out_host_id_len);