diff options
author | Thomas Haller <thaller@redhat.com> | 2020-03-24 20:59:51 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2020-03-24 20:59:51 +0100 |
commit | 1704507c490fba2b54387e1b19a222bec91e715e (patch) | |
tree | d7a912020343c835f87a374705b5d724d1231d44 | |
parent | a30736fbd7d46b8aed0bf51da01f8e6e79130b9d (diff) | |
parent | f2fd1614f5881e93239befc97c17ea909cb7b5d6 (diff) | |
download | NetworkManager-1704507c490fba2b54387e1b19a222bec91e715e.tar.gz |
core: merge branch 'af/settings-add-matches' (part 1)
https://gitlab.freedesktop.org/afreof/NetworkManager/-/tree/af/settings-add-matches
-rw-r--r-- | shared/nm-glib-aux/nm-macros-internal.h | 7 | ||||
-rw-r--r-- | src/nm-core-utils.c | 53 | ||||
-rw-r--r-- | src/nm-core-utils.h | 2 |
3 files changed, 61 insertions, 1 deletions
diff --git a/shared/nm-glib-aux/nm-macros-internal.h b/shared/nm-glib-aux/nm-macros-internal.h index 84317567ef..d54bb3ddda 100644 --- a/shared/nm-glib-aux/nm-macros-internal.h +++ b/shared/nm-glib-aux/nm-macros-internal.h @@ -1479,7 +1479,12 @@ _NM_BACKPORT_SYMBOL_IMPL(version, return_type, func, _##func##_##version, args_t /*****************************************************************************/ /* mirrors g_ascii_isspace() and what we consider spaces in general. */ -#define NM_ASCII_SPACES "\t\n\f\r " +#define NM_ASCII_SPACES " \n\t\r\f" + +/* Like NM_ASCII_SPACES, but without "\f" (0x0c, Formfeed Page Break). + * This is what for example systemd calls WHITESPACE and what it uses to tokenize + * the kernel command line. */ +#define NM_ASCII_WHITESPACES " \n\t\r" #define nm_str_skip_leading_spaces(str) \ ({ \ diff --git a/src/nm-core-utils.c b/src/nm-core-utils.c index 075d9cb0e7..a9f50a0f7a 100644 --- a/src/nm-core-utils.c +++ b/src/nm-core-utils.c @@ -2741,6 +2741,59 @@ 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)) { + gs_free const char **split = NULL; + + /* TODO: support quotation, like systemd's proc_cmdline_extract_first(). + * For that, add a new NMUtilsStrsplitSetFlags flag. */ + split = nm_utils_strsplit_set_full (nm_utils_proc_cmdline (), + NM_ASCII_WHITESPACES, + NM_UTILS_STRSPLIT_SET_FLAGS_NONE); + proc_cmdline = split + ?: NM_PTRARRAY_EMPTY (const char *); + if (!g_atomic_pointer_compare_and_exchange (&proc_cmdline_cached, NULL, proc_cmdline)) + goto again; + + g_steal_pointer (&split); + } + + 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); |