From e152ec7bf4ba252ff9d3eb13eabd417b931dac9a Mon Sep 17 00:00:00 2001 From: Iain Lane Date: Mon, 24 Sep 2018 09:29:55 +0100 Subject: Import Upstream version 1.12.2 --- clients/common/nm-meta-setting-desc.c | 746 +++++++++++++++++----------------- 1 file changed, 380 insertions(+), 366 deletions(-) (limited to 'clients/common/nm-meta-setting-desc.c') diff --git a/clients/common/nm-meta-setting-desc.c b/clients/common/nm-meta-setting-desc.c index 94404e90..3e4bd1bb 100644 --- a/clients/common/nm-meta-setting-desc.c +++ b/clients/common/nm-meta-setting-desc.c @@ -25,10 +25,8 @@ #include #include "nm-common-macros.h" -#include "nm-utils/nm-hash-utils.h" #include "nm-utils/nm-enum-utils.h" -#include "NetworkManager.h" #include "nm-vpn-helpers.h" #include "nm-client-utils.h" #include "nm-meta-setting-access.h" @@ -118,7 +116,6 @@ _parse_ip_route (int family, GError **error) { const int MAX_PREFIX = (family == AF_INET) ? 32 : 128; - char *plen = NULL; const char *next_hop = NULL; const char *canon_dest; int prefix; @@ -126,9 +123,11 @@ _parse_ip_route (int family, GError *local = NULL; gint64 metric = -1; guint i; - gs_strfreev char **routev = NULL; + gs_free const char **routev = NULL; gs_free char *str_clean = NULL; - char *dest; + gs_free char *dest_clone = NULL; + const char *dest; + const char *plen; gs_unref_hashtable GHashTable *attrs = NULL; GHashTable *tmp_attrs; #define ROUTE_SYNTAX _("The valid syntax is: 'ip[/prefix] [next-hop] [metric] [attribute=val]... [,ip[/prefix] ...]'") @@ -138,8 +137,8 @@ _parse_ip_route (int family, nm_assert (!error || !*error); str_clean = g_strstrip (g_strdup (str)); - routev = nmc_strsplit_set (str_clean, " \t", 0); - if (!routev || !routev[0]) { + routev = nm_utils_strsplit_set (str_clean, " \t"); + if (!routev) { g_set_error (error, 1, 0, "'%s' is not valid. %s", str, ROUTE_SYNTAX); @@ -148,8 +147,13 @@ _parse_ip_route (int family, dest = routev[0]; plen = strchr (dest, '/'); /* prefix delimiter */ - if (plen) - *plen++ = '\0'; + if (plen) { + dest_clone = g_strdup (dest); + plen = &dest_clone[plen - dest]; + dest = dest_clone; + *((char *) plen) = '\0'; + plen++; + } prefix = MAX_PREFIX; if (plen) { if ((prefix = _nm_utils_ascii_str_to_int64 (plen, 10, 1, MAX_PREFIX, -1)) == -1) { @@ -296,7 +300,7 @@ static NMTeamLinkWatcher * _parse_team_link_watcher (const char *str, GError **error) { - gs_strfreev char **watcherv = NULL; + gs_free const char **watcherv = NULL; gs_free char *str_clean = NULL; guint i; gs_free const char *name = NULL; @@ -309,17 +313,17 @@ _parse_team_link_watcher (const char *str, nm_assert (!error || !*error); str_clean = g_strstrip (g_strdup (str)); - watcherv = nmc_strsplit_set (str_clean, " \t", 0); - if (!watcherv || !watcherv[0]) { + watcherv = nm_utils_strsplit_set (str_clean, " \t"); + if (!watcherv) { g_set_error (error, 1, 0, "'%s' is not valid", str); return NULL; } for (i = 0; watcherv[i]; i++) { - gs_strfreev char **pair = NULL; + gs_free const char **pair = NULL; - pair = nmc_strsplit_set (watcherv[i], "=", 0); - if (!pair[0]) { + pair = nm_utils_strsplit_set (watcherv[i], "="); + if (!pair) { g_set_error (error, 1, 0, "'%s' is not valid: %s", watcherv[i], "properties should be specified as 'key=value'"); return NULL; @@ -605,7 +609,6 @@ _get_text_hidden (NMMetaAccessorGetType get_type) return NM_META_TEXT_HIDDEN; } - /*****************************************************************************/ G_GNUC_PRINTF (4, 5) @@ -636,7 +639,7 @@ _env_warn_fcn (const NMMetaEnvironment *environment, const NMMetaPropertyInfo *property_info, char **out_to_free #define ARGS_GET_FCN \ - const NMMetaPropertyInfo *property_info, const NMMetaEnvironment *environment, gpointer environment_user_data, NMSetting *setting, NMMetaAccessorGetType get_type, NMMetaAccessorGetFlags get_flags, NMMetaAccessorGetOutFlags *out_flags, gpointer *out_to_free + const NMMetaPropertyInfo *property_info, const NMMetaEnvironment *environment, gpointer environment_user_data, NMSetting *setting, NMMetaAccessorGetType get_type, NMMetaAccessorGetFlags get_flags, NMMetaAccessorGetOutFlags *out_flags, gboolean *out_is_default, gpointer *out_to_free #define ARGS_SET_FCN \ const NMMetaPropertyInfo *property_info, const NMMetaEnvironment *environment, gpointer environment_user_data, NMSetting *setting, const char *value, GError **error @@ -669,6 +672,33 @@ _env_warn_fcn (const NMMetaEnvironment *environment, return ((*(out_to_free)) = _val); \ } G_STMT_END +static gboolean +property_is_default (NMSetting *setting, const char *prop_name) +{ + nm_auto_unset_gvalue GValue v = G_VALUE_INIT; + GParamSpec *pspec; + GHashTable *ht; + char **strv; + + pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (G_OBJECT (setting)), + prop_name); + if (!G_IS_PARAM_SPEC (pspec)) + g_return_val_if_reached (FALSE); + + g_value_init (&v, pspec->value_type); + g_object_get_property (G_OBJECT (setting), prop_name, &v); + + if (pspec->value_type == G_TYPE_STRV) { + strv = g_value_get_boxed (&v); + return !strv || !strv[0]; + } else if (pspec->value_type == G_TYPE_HASH_TABLE) { + ht = g_value_get_boxed (&v); + return !ht || !g_hash_table_size (ht); + } + + return g_param_value_defaults (pspec, &v); +} + static gconstpointer _get_fcn_nmc_with_default (ARGS_GET_FCN) { @@ -677,6 +707,7 @@ _get_fcn_nmc_with_default (ARGS_GET_FCN) GValue val = G_VALUE_INIT; RETURN_UNSUPPORTED_GET_TYPE (); + NM_SET_OUT (out_is_default, property_is_default (setting, property_info->property_name)); if (property_info->property_typ_data->subtype.get_with_default.fcn (setting)) { if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) @@ -692,6 +723,7 @@ _get_fcn_nmc_with_default (ARGS_GET_FCN) else s_full = g_strdup (s && *s ? s : " "); g_value_unset (&val); + RETURN_STR_TO_FREE (s_full); } @@ -699,6 +731,7 @@ static gconstpointer _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info, NMSetting *setting, NMMetaAccessorGetType get_type, + gboolean *out_is_default, gpointer *out_to_free) { char *s; @@ -707,6 +740,7 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info, nm_auto_unset_gvalue GValue val = G_VALUE_INIT; RETURN_UNSUPPORTED_GET_TYPE (); + NM_SET_OUT (out_is_default, property_is_default (setting, property_info->property_name)); gtype_prop = _gobject_property_get_gtype (G_OBJECT (setting), property_info->property_name); @@ -732,13 +766,13 @@ _get_fcn_gobject_impl (const NMMetaPropertyInfo *property_info, static gconstpointer _get_fcn_gobject (ARGS_GET_FCN) { - return _get_fcn_gobject_impl (property_info, setting, get_type, out_to_free); + return _get_fcn_gobject_impl (property_info, setting, get_type, out_is_default, out_to_free); } static gconstpointer _get_fcn_gobject_int (ARGS_GET_FCN) { - const GParamSpec *pspec; + GParamSpec *pspec; nm_auto_unset_gvalue GValue gval = G_VALUE_INIT; gint64 v; const NMMetaUtilsIntValueInfo *value_infos; @@ -751,6 +785,7 @@ _get_fcn_gobject_int (ARGS_GET_FCN) g_value_init (&gval, pspec->value_type); g_object_get_property (G_OBJECT (setting), property_info->property_name, &gval); + NM_SET_OUT (out_is_default, g_param_value_defaults (pspec, &gval)); switch (pspec->value_type) { case G_TYPE_INT: v = g_value_get_int (&gval); @@ -790,10 +825,11 @@ _get_fcn_gobject_mtu (ARGS_GET_FCN) if ( !property_info->property_typ_data || !property_info->property_typ_data->subtype.mtu.get_fcn) - return _get_fcn_gobject_impl (property_info, setting, get_type, out_to_free); + return _get_fcn_gobject_impl (property_info, setting, get_type, out_is_default, out_to_free); mtu = property_info->property_typ_data->subtype.mtu.get_fcn (setting); if (mtu == 0) { + NM_SET_OUT (out_is_default, TRUE); if (get_type == NM_META_ACCESSOR_GET_TYPE_PRETTY) return _("auto"); return "auto"; @@ -820,9 +856,9 @@ static gconstpointer _get_fcn_gobject_enum (ARGS_GET_FCN) { GType gtype = 0; - GType gtype_prop; nm_auto_unref_gtypeclass GTypeClass *gtype_class = NULL; nm_auto_unref_gtypeclass GTypeClass *gtype_prop_class = NULL; + const struct _NMUtilsEnumValueInfo *value_infos = NULL; gboolean has_gtype = FALSE; nm_auto_unset_gvalue GValue gval = G_VALUE_INIT; gint64 v; @@ -833,6 +869,7 @@ _get_fcn_gobject_enum (ARGS_GET_FCN) gboolean format_text_l10n = FALSE; gs_free char *s = NULL; char s_numeric[64]; + GParamSpec *pspec; RETURN_UNSUPPORTED_GET_TYPE (); @@ -875,25 +912,26 @@ _get_fcn_gobject_enum (ARGS_GET_FCN) nm_assert (format_text || format_numeric); - gtype_prop = _gobject_property_get_gtype (G_OBJECT (setting), property_info->property_name); - - g_value_init (&gval, gtype_prop); + pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (setting), property_info->property_name); + g_return_val_if_fail (pspec, NULL); + g_value_init (&gval, pspec->value_type); g_object_get_property (G_OBJECT (setting), property_info->property_name, &gval); + NM_SET_OUT (out_is_default, g_param_value_defaults (pspec, &gval)); - if ( gtype_prop == G_TYPE_INT - || ( G_TYPE_IS_CLASSED (gtype_prop) - && G_IS_ENUM_CLASS ((gtype_prop_class ?: (gtype_prop_class = g_type_class_ref (gtype_prop)))))) { - if (gtype_prop == G_TYPE_INT) { + if ( pspec->value_type == G_TYPE_INT + || ( G_TYPE_IS_CLASSED (pspec->value_type) + && G_IS_ENUM_CLASS ((gtype_prop_class ?: (gtype_prop_class = g_type_class_ref (pspec->value_type)))))) { + if (pspec->value_type == G_TYPE_INT) { if (!has_gtype) g_return_val_if_reached (NULL); v = g_value_get_int (&gval); } else v = g_value_get_enum (&gval); - } else if ( gtype_prop == G_TYPE_UINT - || ( G_TYPE_IS_CLASSED (gtype_prop) - && G_IS_FLAGS_CLASS ((gtype_prop_class ?: (gtype_prop_class = g_type_class_ref (gtype_prop)))))) { - if (gtype_prop == G_TYPE_UINT) { + } else if ( pspec->value_type == G_TYPE_UINT + || ( G_TYPE_IS_CLASSED (pspec->value_type) + && G_IS_FLAGS_CLASS ((gtype_prop_class ?: (gtype_prop_class = g_type_class_ref (pspec->value_type)))))) { + if (pspec->value_type == G_TYPE_UINT) { if (!has_gtype) g_return_val_if_reached (NULL); v = g_value_get_uint (&gval); @@ -903,7 +941,7 @@ _get_fcn_gobject_enum (ARGS_GET_FCN) g_return_val_if_reached (NULL); if (!has_gtype) { - gtype = gtype_prop; + gtype = pspec->value_type; gtype_class = g_steal_pointer (>ype_prop_class); } @@ -927,7 +965,9 @@ _get_fcn_gobject_enum (ARGS_GET_FCN) /* the gobject_enum.value_infos are currently ignored for the getter. They * only declare additional aliases for the setter. */ - s = nm_utils_enum_to_str (gtype, (int) v); + if (property_info->property_typ_data) + value_infos = property_info->property_typ_data->subtype.gobject_enum.value_infos_get; + s = _nm_utils_enum_to_str_full (gtype, (int) v, ", ", value_infos); if (!format_numeric) RETURN_STR_TO_FREE (g_steal_pointer (&s)); @@ -1150,7 +1190,6 @@ _set_fcn_gobject_mac (ARGS_SET_FCN) else mode = NM_META_PROPERTY_TYPE_MAC_MODE_DEFAULT; - if (mode == NM_META_PROPERTY_TYPE_MAC_MODE_INFINIBAND) valid = nm_utils_hwaddr_valid (value, INFINIBAND_ALEN); else { @@ -1168,39 +1207,6 @@ _set_fcn_gobject_mac (ARGS_SET_FCN) return TRUE; } -static gboolean -_set_fcn_gobject_secret_flags (ARGS_SET_FCN) -{ - char **strv = NULL, **iter; - unsigned long flags = 0, val_int; - - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); - - strv = nmc_strsplit_set (value, " \t,", 0); - for (iter = strv; iter && *iter; iter++) { - if (!nmc_string_to_uint (*iter, TRUE, 0, ALL_SECRET_FLAGS, &val_int)) { - g_set_error (error, 1, 0, _("'%s' is not a valid flag number; use <0-%d>"), - *iter, ALL_SECRET_FLAGS); - g_strfreev (strv); - return FALSE; - } - flags += val_int; - } - g_strfreev (strv); - - /* Validate the flags number */ - if (flags > ALL_SECRET_FLAGS) { - flags = ALL_SECRET_FLAGS; - _env_warn_fcn (environment, environment_user_data, - NM_META_ENV_WARN_LEVEL_WARN, - N_("'%s' sum is higher than all flags => all flags set"), - value); - } - - g_object_set (setting, property_info->property_name, (guint) flags, NULL); - return TRUE; -} - static gboolean _set_fcn_gobject_enum (ARGS_SET_FCN) { @@ -1320,7 +1326,7 @@ _values_fcn_gobject_enum (ARGS_VALUES_FCN) gboolean has_minmax = FALSE; int min = G_MININT; int max = G_MAXINT; - char **v, **w; + char **v; if (property_info->property_typ_data) { if ( property_info->property_typ_data->subtype.gobject_enum.min @@ -1354,11 +1360,7 @@ _values_fcn_gobject_enum (ARGS_VALUES_FCN) /* the gobject_enum.value_infos are currently ignored for the list of * values. They only declare additional (hidden) aliases for the setter. */ - v = (char **) nm_utils_enum_get_values (gtype, min, max); - if (v) { - for (w = v; *w; w++) - *w = g_strdup (*w); - } + v = nm_utils_strv_make_deep_copied (nm_utils_enum_get_values (gtype, min, max)); return (const char *const*) (*out_to_free = v); } @@ -1505,24 +1507,6 @@ vlan_priorities_to_string (NMSettingVlan *s_vlan, NMVlanPriorityMap map) return g_string_free (priorities, FALSE); } -static char * -ip6_privacy_to_string (NMSettingIP6ConfigPrivacy ip6_privacy, NMMetaAccessorGetType get_type) -{ - if (get_type != NM_META_ACCESSOR_GET_TYPE_PRETTY) - return g_strdup_printf ("%d", ip6_privacy); - - switch (ip6_privacy) { - case NM_SETTING_IP6_CONFIG_PRIVACY_DISABLED: - return g_strdup_printf (_("%d (disabled)"), ip6_privacy); - case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR: - return g_strdup_printf (_("%d (enabled, prefer public IP)"), ip6_privacy); - case NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR: - return g_strdup_printf (_("%d (enabled, prefer temporary IP)"), ip6_privacy); - default: - return g_strdup_printf (_("%d (unknown)"), ip6_privacy); - } -} - static char * secret_flags_to_string (guint32 flags, NMMetaAccessorGetType get_type) { @@ -1573,18 +1557,19 @@ vpn_data_item (const char *key, const char *value, gpointer user_data) const char **valid_strv, \ GError **error) \ { \ - char **strv = NULL, **iter; \ + gs_free const char **strv = NULL; \ + gsize i; \ const char *item; \ - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); \ - strv = nmc_strsplit_set (value, " \t,", 0); \ - for (iter = strv; iter && *iter; iter++) { \ - if (!(item = nmc_string_is_valid (g_strstrip (*iter), valid_strv, error))) { \ - g_strfreev (strv); \ - return FALSE; \ + nm_assert (!error || !*error); \ + strv = nm_utils_strsplit_set (value, " \t,"); \ + if (strv) { \ + for (i = 0; strv[i]; i++) { \ + if (!(item = nmc_string_is_valid (strv[i], valid_strv, error))) { \ + return FALSE; \ + } \ + set_func (s_macro (setting), item); \ } \ - set_func (s_macro (setting), item); \ } \ - g_strfreev (strv); \ return TRUE; \ } @@ -1592,43 +1577,42 @@ vpn_data_item (const char *key, const char *value, gpointer user_data) static gboolean \ def_func (ARGS_SET_FCN) \ { \ - char **strv = NULL, **iter; \ + gs_free const char **strv = NULL; \ + const char **iter; \ const char **(*valid_func1_p) (s_type *) = valid_func1; \ const char * (*valid_func2_p) (const char *, const char *, GError **) = valid_func2; \ const char *opt_name, *opt_val; \ \ - g_return_val_if_fail (error == NULL || *error == NULL, FALSE); \ + nm_assert (!error || !*error); \ \ - strv = nmc_strsplit_set (value, ",", 0); \ + strv = nm_utils_strsplit_set (value, ","); \ for (iter = strv; iter && *iter; iter++) { \ - char *left = g_strstrip (*iter); \ + gs_free char *left_clone = g_strstrip (g_strdup (*iter)); \ + char *left = left_clone; \ char *right = strchr (left, '='); \ if (!right) { \ g_set_error (error, 1, 0, _("'%s' is not valid; use