diff options
author | Adrian Freihofer <adrian.freihofer@gmail.com> | 2020-03-15 11:25:33 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-03-24 16:56:06 +0100 |
commit | 8069e5fd208197f1f4aa6ffb5062f2d66416d9bd (patch) | |
tree | e91696c8c09d9a7d368afe578b842040ff97416e | |
parent | a30736fbd7d46b8aed0bf51da01f8e6e79130b9d (diff) | |
download | NetworkManager-8069e5fd208197f1f4aa6ffb5062f2d66416d9bd.tar.gz |
nm-core-utils: add nm_utils_proc_cmdline
Add a new function to read /proc/cmdline. The function caches the content.
-rw-r--r-- | src/nm-core-utils.c | 45 | ||||
-rw-r--r-- | src/nm-core-utils.h | 2 |
2 files changed, 47 insertions, 0 deletions
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); |