nm-core-utils: add nm_utils_proc_cmdline

Add a new function to read /proc/cmdline. The function caches the content.
This commit is contained in:
Adrian Freihofer 2020-03-15 11:25:33 +01:00 committed by Thomas Haller
parent a30736fbd7
commit 8069e5fd20
2 changed files with 47 additions and 0 deletions

View file

@ -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.

View file

@ -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);