diff options
author | Thomas Haller <thaller@redhat.com> | 2021-06-21 17:24:22 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-06-21 17:24:22 +0200 |
commit | 3e7a589972150ca0574fe8cb8080c5db14ab9051 (patch) | |
tree | 2e23cac894d140ce84044967303cc12c30b4ec57 | |
parent | 1441b04bbad677caf4a58c293ff6730b319a91df (diff) | |
parent | 9452d6946527eb4ae36f3c405d153e1fb4de7fbe (diff) |
config: merge branch 'th/config-match-lookup'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/894
-rw-r--r-- | src/core/devices/nm-device.c | 169 | ||||
-rw-r--r-- | src/core/devices/wifi/nm-device-wifi.c | 4 | ||||
-rw-r--r-- | src/core/devices/wifi/nm-wifi-factory.c | 5 | ||||
-rw-r--r-- | src/core/nm-config-data.c | 154 | ||||
-rw-r--r-- | src/core/nm-config-data.h | 32 | ||||
-rw-r--r-- | src/core/tests/config/test-config.c | 100 | ||||
-rw-r--r-- | src/libnm-glib-aux/nm-shared-utils.c | 7 |
7 files changed, 270 insertions, 201 deletions
diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 269418df40..1c70ecb328 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -820,8 +820,7 @@ _prop_get_connection_stable_id(NMDevice * self, * Especially with ${RANDOM} stable-id we want to generate *one* configuration * for each activation. */ if (G_UNLIKELY(!priv->current_stable_id)) { - gs_free char * default_id = NULL; - gs_free char * generated = NULL; + gs_free char * generated = NULL; NMUtilsStableType stable_type; NMSettingConnection *s_con; gboolean hwaddr_is_fake; @@ -834,11 +833,10 @@ _prop_get_connection_stable_id(NMDevice * self, stable_id = nm_setting_connection_get_stable_id(s_con); if (!stable_id) { - default_id = + stable_id = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, NM_CON_DEFAULT("connection.stable-id"), self); - stable_id = default_id; } uuid = nm_connection_get_uuid(connection); @@ -897,7 +895,6 @@ _prop_get_ipv6_dhcp_duid(NMDevice * self, { NMSettingIPConfig *s_ip6; const char * duid; - gs_free char * duid_default = NULL; const char * duid_error; GBytes * duid_out; gboolean duid_enforce = TRUE; @@ -910,10 +907,9 @@ _prop_get_ipv6_dhcp_duid(NMDevice * self, duid = nm_setting_ip6_config_get_dhcp_duid(NM_SETTING_IP6_CONFIG(s_ip6)); if (!duid) { - duid_default = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, - NM_CON_DEFAULT("ipv6.dhcp-duid"), - self); - duid = duid_default; + duid = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, + NM_CON_DEFAULT("ipv6.dhcp-duid"), + self); if (!duid) duid = "lease"; } @@ -1390,7 +1386,6 @@ _prop_get_ipvx_dhcp_iaid(NMDevice * self, const int IS_IPv4 = NM_IS_IPv4(addr_family); NMSettingIPConfig *s_ip; const char * iaid_str; - gs_free char * iaid_str_free = NULL; guint32 iaid; const char * iface; const char * fail_reason; @@ -1399,11 +1394,10 @@ _prop_get_ipvx_dhcp_iaid(NMDevice * self, s_ip = nm_connection_get_setting_ip_config(connection, addr_family); iaid_str = nm_setting_ip_config_get_dhcp_iaid(s_ip); if (!iaid_str) { - iaid_str_free = nm_config_data_get_connection_default( - NM_CONFIG_GET_DATA, - IS_IPv4 ? NM_CON_DEFAULT("ipv4.dhcp-iaid") : NM_CON_DEFAULT("ipv6.dhcp-iaid"), - self); - iaid_str = iaid_str_free; + iaid_str = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, + IS_IPv4 ? NM_CON_DEFAULT("ipv4.dhcp-iaid") + : NM_CON_DEFAULT("ipv6.dhcp-iaid"), + self); if (!iaid_str) { iaid_str = NM_IAID_IFNAME; is_explicit = FALSE; @@ -1560,12 +1554,10 @@ _prop_get_ipvx_dhcp_hostname_flags(NMDevice *self, int addr_family) } static const char * -_prop_get_connection_mud_url(NMDevice *self, NMSettingConnection *s_con, char **out_mud_url) +_prop_get_connection_mud_url(NMDevice *self, NMSettingConnection *s_con) { - const char * mud_url; - gs_free char *s = NULL; - - nm_assert(out_mud_url && !*out_mud_url); + const char *mud_url; + const char *s; mud_url = nm_setting_connection_get_mud_url(s_con); @@ -1582,7 +1574,7 @@ _prop_get_connection_mud_url(NMDevice *self, NMSettingConnection *s_con, char ** if (nm_streq(s, NM_CONNECTION_MUD_URL_NONE)) return NULL; if (nm_sd_http_url_is_valid_https(s)) - return (*out_mud_url = g_steal_pointer(&s)); + return s; } return NULL; @@ -1593,7 +1585,6 @@ _prop_get_ipv4_dhcp_client_id(NMDevice *self, NMConnection *connection, GBytes * { NMSettingIPConfig *s_ip4; const char * client_id; - gs_free char * client_id_default = NULL; guint8 * client_id_buf; const char * fail_reason; guint8 hwaddr_bin_buf[_NM_UTILS_HWADDR_LEN_MAX]; @@ -1607,13 +1598,12 @@ _prop_get_ipv4_dhcp_client_id(NMDevice *self, NMConnection *connection, GBytes * client_id = nm_setting_ip4_config_get_dhcp_client_id(NM_SETTING_IP4_CONFIG(s_ip4)); if (!client_id) { - client_id_default = - nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, - NM_CON_DEFAULT("ipv4.dhcp-client-id"), - self); - if (client_id_default && client_id_default[0]) { + client_id = nm_config_data_get_connection_default(NM_CONFIG_GET_DATA, + NM_CON_DEFAULT("ipv4.dhcp-client-id"), + self); + if (client_id && !client_id[0]) { /* a non-empty client-id is always valid, see nm_dhcp_utils_client_id_string_to_bytes(). */ - client_id = client_id_default; + client_id = NULL; } } @@ -1745,8 +1735,7 @@ out_good: static GBytes * _prop_get_ipv4_dhcp_vendor_class_identifier(NMDevice *self, NMSettingIP4Config *s_ip4) { - gs_free char *config_data_prop = NULL; - gs_free char *to_free = NULL; + gs_free char *to_free = NULL; const char * conn_prop; GBytes * bytes = NULL; const char * bin; @@ -1756,13 +1745,13 @@ _prop_get_ipv4_dhcp_vendor_class_identifier(NMDevice *self, NMSettingIP4Config * if (!conn_prop) { /* set in NetworkManager.conf ? */ - config_data_prop = nm_config_data_get_connection_default( + conn_prop = nm_config_data_get_connection_default( NM_CONFIG_GET_DATA, NM_CON_DEFAULT("ipv4.dhcp-vendor-class-identifier"), self); - if (config_data_prop && nm_utils_validate_dhcp4_vendor_class_id(config_data_prop, NULL)) - conn_prop = config_data_prop; + if (conn_prop && !nm_utils_validate_dhcp4_vendor_class_id(conn_prop, NULL)) + conn_prop = NULL; } if (conn_prop) { @@ -1828,16 +1817,11 @@ _prop_get_ipv6_ip6_privacy(NMDevice *self) } static const char * -_prop_get_x_cloned_mac_address(NMDevice * self, - NMConnection *connection, - gboolean is_wifi, - char ** out_addr) +_prop_get_x_cloned_mac_address(NMDevice *self, NMConnection *connection, gboolean is_wifi) { NMSetting * setting; const char *addr = NULL; - nm_assert(out_addr && !*out_addr); - setting = nm_connection_get_setting(connection, is_wifi ? NM_TYPE_SETTING_WIRELESS : NM_TYPE_SETTING_WIRED); if (setting) { @@ -1846,7 +1830,7 @@ _prop_get_x_cloned_mac_address(NMDevice * self, } if (!addr) { - gs_free char *a = NULL; + const char *a; a = nm_config_data_get_connection_default( NM_CONFIG_GET_DATA, @@ -1861,36 +1845,28 @@ _prop_get_x_cloned_mac_address(NMDevice * self, NMSettingMacRandomization v; /* for backward compatibility, read the deprecated wifi.mac-address-randomization setting. */ - a = nm_config_data_get_connection_default( + v = nm_config_data_get_connection_default_int64( NM_CONFIG_GET_DATA, NM_CON_DEFAULT("wifi.mac-address-randomization"), - self); - v = _nm_utils_ascii_str_to_int64(a, - 10, - NM_SETTING_MAC_RANDOMIZATION_DEFAULT, - NM_SETTING_MAC_RANDOMIZATION_ALWAYS, - NM_SETTING_MAC_RANDOMIZATION_DEFAULT); + self, + NM_SETTING_MAC_RANDOMIZATION_DEFAULT, + NM_SETTING_MAC_RANDOMIZATION_ALWAYS, + NM_SETTING_MAC_RANDOMIZATION_DEFAULT); if (v == NM_SETTING_MAC_RANDOMIZATION_ALWAYS) addr = NM_CLONED_MAC_RANDOM; } } else if (NM_CLONED_MAC_IS_SPECIAL(a) || nm_utils_hwaddr_valid(a, ETH_ALEN)) - addr = *out_addr = g_steal_pointer(&a); + addr = a; } return addr; } static const char * -_prop_get_x_generate_mac_address_mask(NMDevice * self, - NMConnection *connection, - gboolean is_wifi, - char ** out_value) +_prop_get_x_generate_mac_address_mask(NMDevice *self, NMConnection *connection, gboolean is_wifi) { NMSetting * setting; - const char *value = NULL; - char * a; - - nm_assert(out_value && !*out_value); + const char *value; setting = nm_connection_get_setting(connection, is_wifi ? NM_TYPE_SETTING_WIRELESS : NM_TYPE_SETTING_WIRED); @@ -1903,15 +1879,11 @@ _prop_get_x_generate_mac_address_mask(NMDevice * self, return value; } - a = nm_config_data_get_connection_default( + return nm_config_data_get_connection_default( NM_CONFIG_GET_DATA, is_wifi ? NM_CON_DEFAULT("wifi.generate-mac-address-mask") : NM_CON_DEFAULT("ethernet.generate-mac-address-mask"), self); - if (!a) - return NULL; - *out_value = a; - return a; } /*****************************************************************************/ @@ -5874,16 +5846,19 @@ sriov_op_queue(NMDevice * self, static void device_init_static_sriov_num_vfs(NMDevice *self) { - NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE(self); - gs_free char * value = NULL; - int num_vfs; + NMDevicePrivate *priv = NM_DEVICE_GET_PRIVATE(self); if (priv->ifindex > 0 && nm_device_has_capability(self, NM_DEVICE_CAP_SRIOV)) { - value = nm_config_data_get_device_config(NM_CONFIG_GET_DATA, - NM_CONFIG_KEYFILE_KEY_DEVICE_SRIOV_NUM_VFS, - self, - NULL); - num_vfs = _nm_utils_ascii_str_to_int64(value, 10, 0, G_MAXINT32, -1); + int num_vfs; + + num_vfs = nm_config_data_get_device_config_int64(NM_CONFIG_GET_DATA, + NM_CONFIG_KEYFILE_KEY_DEVICE_SRIOV_NUM_VFS, + self, + 10, + 0, + G_MAXINT32, + -1, + -1); if (num_vfs >= 0) sriov_op_queue(self, num_vfs, NM_OPTION_BOOL_DEFAULT, NULL, NULL); } @@ -9414,7 +9389,6 @@ dhcp4_start(NMDevice *self) gs_unref_bytes GBytes *hwaddr = NULL; gs_unref_bytes GBytes *bcast_hwaddr = NULL; gs_unref_bytes GBytes *client_id = NULL; - gs_free char * mud_url_free = NULL; NMConnection * connection; NMSettingConnection * s_con; GError * error = NULL; @@ -9474,7 +9448,7 @@ dhcp4_start(NMDevice *self) nm_setting_ip_config_get_dhcp_hostname(s_ip4), nm_setting_ip4_config_get_dhcp_fqdn(NM_SETTING_IP4_CONFIG(s_ip4)), _prop_get_ipvx_dhcp_hostname_flags(self, AF_INET), - _prop_get_connection_mud_url(self, s_con, &mud_url_free), + _prop_get_connection_mud_url(self, s_con), client_id, _prop_get_ipvx_dhcp_timeout(self, AF_INET), _device_get_dhcp_anycast_address(self), @@ -9878,8 +9852,7 @@ dhcp6_start_with_link_ready(NMDevice *self, NMConnection *connection) gs_unref_bytes GBytes * duid = NULL; gboolean enforce_duid = FALSE; const NMPlatformLink * pllink; - gs_free char * mud_url_free = NULL; - GError * error = NULL; + GError * error = NULL; guint32 iaid; gboolean iaid_explicit; NMSettingConnection * s_con; @@ -9924,7 +9897,7 @@ dhcp6_start_with_link_ready(NMDevice *self, NMConnection *connection) nm_setting_ip_config_get_dhcp_send_hostname(s_ip6), nm_setting_ip_config_get_dhcp_hostname(s_ip6), _prop_get_ipvx_dhcp_hostname_flags(self, AF_INET6), - _prop_get_connection_mud_url(self, s_con, &mud_url_free), + _prop_get_connection_mud_url(self, s_con), duid, enforce_duid, iaid, @@ -14043,13 +14016,14 @@ nm_device_is_up(NMDevice *self) static gint64 _get_carrier_wait_ms(NMDevice *self) { - gs_free char *value = NULL; - - value = nm_config_data_get_device_config(NM_CONFIG_GET_DATA, - NM_CONFIG_KEYFILE_KEY_DEVICE_CARRIER_WAIT_TIMEOUT, - self, - NULL); - return _nm_utils_ascii_str_to_int64(value, 10, 0, G_MAXINT32, CARRIER_WAIT_TIME_MS); + return nm_config_data_get_device_config_int64(NM_CONFIG_GET_DATA, + NM_CONFIG_KEYFILE_KEY_DEVICE_CARRIER_WAIT_TIMEOUT, + self, + 10, + 0, + G_MAXINT32, + CARRIER_WAIT_TIME_MS, + CARRIER_WAIT_TIME_MS); } gboolean @@ -17273,14 +17247,13 @@ _hw_addr_get_cloned(NMDevice * self, gboolean * preserve, char ** hwaddr, HwAddrType * hwaddr_type, - char ** hwaddr_detail, + const char ** hwaddr_detail, GError ** error) { NMDevicePrivate *priv; - gs_free char * addr_setting_free = NULL; - gs_free char * hw_addr_generated = NULL; - gs_free char * generate_mac_address_mask_tmp = NULL; - const char * addr, *addr_setting; + gs_free char * hw_addr_generated = NULL; + const char * addr; + const char * addr_setting; char * addr_out; HwAddrType type_out; @@ -17293,15 +17266,16 @@ _hw_addr_get_cloned(NMDevice * self, if (!connection) g_return_val_if_reached(FALSE); - addr = addr_setting = - _prop_get_x_cloned_mac_address(self, connection, is_wifi, &addr_setting_free); + addr_setting = _prop_get_x_cloned_mac_address(self, connection, is_wifi); + + addr = addr_setting; if (nm_streq(addr, NM_CLONED_MAC_PRESERVE)) { /* "preserve" means to reset the initial MAC address. */ NM_SET_OUT(preserve, TRUE); NM_SET_OUT(hwaddr, NULL); NM_SET_OUT(hwaddr_type, HW_ADDR_TYPE_UNSET); - NM_SET_OUT(hwaddr_detail, g_steal_pointer(&addr_setting_free) ?: g_strdup(addr_setting)); + NM_SET_OUT(hwaddr_detail, addr_setting); return TRUE; } @@ -17314,8 +17288,7 @@ _hw_addr_get_cloned(NMDevice * self, NM_SET_OUT(preserve, TRUE); NM_SET_OUT(hwaddr, NULL); NM_SET_OUT(hwaddr_type, HW_ADDR_TYPE_UNSET); - NM_SET_OUT(hwaddr_detail, - g_steal_pointer(&addr_setting_free) ?: g_strdup(addr_setting)); + NM_SET_OUT(hwaddr_detail, addr_setting); return TRUE; } else if (!addr) { g_set_error_literal(error, @@ -17335,10 +17308,7 @@ _hw_addr_get_cloned(NMDevice * self, } hw_addr_generated = nm_utils_hw_addr_gen_random_eth( nm_device_get_initial_hw_address(self), - _prop_get_x_generate_mac_address_mask(self, - connection, - is_wifi, - &generate_mac_address_mask_tmp)); + _prop_get_x_generate_mac_address_mask(self, connection, is_wifi)); if (!hw_addr_generated) { g_set_error(error, NM_DEVICE_ERROR, @@ -17366,10 +17336,7 @@ _hw_addr_get_cloned(NMDevice * self, stable_id, nm_device_get_ip_iface(self), nm_device_get_initial_hw_address(self), - _prop_get_x_generate_mac_address_mask(self, - connection, - is_wifi, - &generate_mac_address_mask_tmp)); + _prop_get_x_generate_mac_address_mask(self, connection, is_wifi)); if (!hw_addr_generated) { g_set_error(error, NM_DEVICE_ERROR, @@ -17393,7 +17360,7 @@ _hw_addr_get_cloned(NMDevice * self, NM_SET_OUT(preserve, FALSE); NM_SET_OUT(hwaddr, addr_out); NM_SET_OUT(hwaddr_type, type_out); - NM_SET_OUT(hwaddr_detail, g_steal_pointer(&addr_setting_free) ?: g_strdup(addr_setting)); + NM_SET_OUT(hwaddr_detail, addr_setting); return TRUE; out_no_action: NM_SET_OUT(preserve, FALSE); @@ -17423,7 +17390,7 @@ nm_device_hw_addr_set_cloned(NMDevice *self, NMConnection *connection, gboolean NMDevicePrivate *priv; gboolean preserve = FALSE; gs_free char * hwaddr = NULL; - gs_free char * detail = NULL; + const char * detail = NULL; HwAddrType type = HW_ADDR_TYPE_UNSET; gs_free_error GError *error = NULL; diff --git a/src/core/devices/wifi/nm-device-wifi.c b/src/core/devices/wifi/nm-device-wifi.c index dd52186d82..fca2fde515 100644 --- a/src/core/devices/wifi/nm-device-wifi.c +++ b/src/core/devices/wifi/nm-device-wifi.c @@ -1382,8 +1382,8 @@ _hw_addr_set_scanning(NMDeviceWifi *self, gboolean do_reset) now = nm_utils_get_monotonic_timestamp_sec(); if (now >= priv->hw_addr_scan_expire) { - gs_free char *generate_mac_address_mask = NULL; - gs_free char *hw_addr_scan = NULL; + gs_free char *hw_addr_scan = NULL; + const char * generate_mac_address_mask; /* the random MAC address for scanning expires after a while. * diff --git a/src/core/devices/wifi/nm-wifi-factory.c b/src/core/devices/wifi/nm-wifi-factory.c index c7be7ab8cb..d2269c74ef 100644 --- a/src/core/devices/wifi/nm-wifi-factory.c +++ b/src/core/devices/wifi/nm-wifi-factory.c @@ -74,7 +74,8 @@ create_device(NMDeviceFactory * factory, NMConnection * connection, gboolean * out_ignore) { - gs_free char *backend = NULL; + gs_free char *backend_free = NULL; + const char * backend; g_return_val_if_fail(iface != NULL, NULL); g_return_val_if_fail(plink != NULL, NULL); @@ -89,7 +90,7 @@ create_device(NMDeviceFactory * factory, plink, "wifi", NULL); - nm_strstrip(backend); + backend = nm_strstrip_avoid_copy_a(300, backend, &backend_free); nm_log_dbg(LOGD_PLATFORM | LOGD_WIFI, "(%s) config: backend is %s%s%s%s", diff --git a/src/core/nm-config-data.c b/src/core/nm-config-data.c index f31c900e27..e127ea23bb 100644 --- a/src/core/nm-config-data.c +++ b/src/core/nm-config-data.c @@ -26,6 +26,8 @@ typedef struct { gboolean has; GSList * spec; } match_device; + gsize lookup_len; + const NMUtilsNamedValue *lookup_idx; } MatchSectionInfo; struct _NMGlobalDnsDomain { @@ -116,6 +118,11 @@ G_DEFINE_TYPE(NMConfigData, nm_config_data, G_TYPE_OBJECT) /*****************************************************************************/ +static const char * +_match_section_info_get_str(const MatchSectionInfo *m, GKeyFile *keyfile, const char *property); + +/*****************************************************************************/ + const char * nm_config_data_get_config_main_file(const NMConfigData *self) { @@ -352,9 +359,9 @@ nm_config_data_get_iwd_config_path(const NMConfigData *self) gboolean nm_config_data_get_ignore_carrier(const NMConfigData *self, NMDevice *device) { - gs_free char *value = NULL; - gboolean has_match; - int m; + const char *value; + gboolean has_match; + int m; g_return_val_if_fail(NM_IS_CONFIG_DATA(self), FALSE); g_return_val_if_fail(NM_IS_DEVICE(device), FALSE); @@ -1390,18 +1397,18 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, NMDevice * device, const NMPlatformLink * pllink, const char * match_device_type, - char ** out_value) + const char ** out_value) { const char *match_dhcp_plugin; if (!match_section_infos) - return NULL; + goto out; match_dhcp_plugin = nm_dhcp_manager_get_config(nm_dhcp_manager_get()); for (; match_section_infos->group_name; match_section_infos++) { - char * value = NULL; - gboolean match; + const char *value; + gboolean match; /* FIXME: Here we use g_key_file_get_string(). This should be in sync with what keyfile-reader * does. @@ -1410,7 +1417,7 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, * string_to_value(keyfile_to_string(keyfile)) in one. Optimally, keyfile library would * expose both functions, and we would return here keyfile_to_string(keyfile). * The caller then could convert the string to the proper value via string_to_value(value). */ - value = g_key_file_get_string(keyfile, match_section_infos->group_name, property, NULL); + value = _match_section_info_get_str(match_section_infos, keyfile, property); if (!value && !match_section_infos->stop_match) continue; @@ -1432,12 +1439,14 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, *out_value = value; return match_section_infos; } - g_free(value); } + +out: + *out_value = NULL; return NULL; } -char * +const char * nm_config_data_get_device_config(const NMConfigData *self, const char * property, NMDevice * device, @@ -1445,7 +1454,7 @@ nm_config_data_get_device_config(const NMConfigData *self, { const NMConfigDataPrivate *priv; const MatchSectionInfo * connection_info; - char * value = NULL; + const char * value; NM_SET_OUT(has_match, FALSE); @@ -1465,7 +1474,7 @@ nm_config_data_get_device_config(const NMConfigData *self, return value; } -char * +const char * nm_config_data_get_device_config_by_pllink(const NMConfigData * self, const char * property, const NMPlatformLink *pllink, @@ -1474,7 +1483,7 @@ nm_config_data_get_device_config_by_pllink(const NMConfigData * self, { const NMConfigDataPrivate *priv; const MatchSectionInfo * connection_info; - char * value = NULL; + const char * value; g_return_val_if_fail(self, NULL); g_return_val_if_fail(property && *property, NULL); @@ -1499,8 +1508,8 @@ nm_config_data_get_device_config_boolean(const NMConfigData *self, int val_no_match, int val_invalid) { - gs_free char *value = NULL; - gboolean has_match; + const char *value; + gboolean has_match; value = nm_config_data_get_device_config(self, property, device, &has_match); if (!has_match) @@ -1508,13 +1517,34 @@ nm_config_data_get_device_config_boolean(const NMConfigData *self, return nm_config_parse_boolean(value, val_invalid); } -char * +gint64 +nm_config_data_get_device_config_int64(const NMConfigData *self, + const char * property, + NMDevice * device, + int base, + gint64 min, + gint64 max, + gint64 val_no_match, + gint64 val_invalid) +{ + const char *value; + gboolean has_match; + + value = nm_config_data_get_device_config(self, property, device, &has_match); + if (!has_match) { + errno = ENOENT; + return val_no_match; + } + return _nm_utils_ascii_str_to_int64(value, base, min, max, val_invalid); +} + +const char * nm_config_data_get_connection_default(const NMConfigData *self, const char * property, NMDevice * device) { const NMConfigDataPrivate *priv; - char * value = NULL; + const char * value; g_return_val_if_fail(self, NULL); g_return_val_if_fail(property && *property, NULL); @@ -1553,15 +1583,41 @@ nm_config_data_get_connection_default_int64(const NMConfigData *self, gint64 max, gint64 fallback) { - gs_free char *value = NULL; + const char *value; value = nm_config_data_get_connection_default(self, property, device); return _nm_utils_ascii_str_to_int64(value, 10, min, max, fallback); } +static const char * +_match_section_info_get_str(const MatchSectionInfo *m, GKeyFile *keyfile, const char *property) +{ + gssize idx; + const char *value; + + idx = nm_utils_named_value_list_find(m->lookup_idx, m->lookup_len, property, TRUE); + value = idx >= 0 ? m->lookup_idx[idx].value_str : NULL; + +#if NM_MORE_ASSERTS > 10 + { + gs_free char *value2 = g_key_file_get_string(keyfile, m->group_name, property, NULL); + + nm_assert(nm_streq0(value2, value)); + } +#endif + + return value; +} + static void -_get_connection_info_init(MatchSectionInfo *connection_info, GKeyFile *keyfile, char *group) +_match_section_info_init(MatchSectionInfo *connection_info, GKeyFile *keyfile, char *group) { + char ** keys = NULL; + gsize n_keys; + gsize i; + gsize j; + NMUtilsNamedValue *vals; + /* pass ownership of @group on... */ connection_info->group_name = group; @@ -1572,18 +1628,66 @@ _get_connection_info_init(MatchSectionInfo *connection_info, GKeyFile *keyfile, &connection_info->match_device.has); connection_info->stop_match = nm_config_keyfile_get_boolean(keyfile, group, NM_CONFIG_KEYFILE_KEY_STOP_MATCH, FALSE); + + keys = g_key_file_get_keys(keyfile, group, &n_keys, NULL); + nm_utils_strv_sort(keys, n_keys); + + vals = g_new(NMUtilsNamedValue, n_keys); + + for (i = 0, j = 0; i < n_keys; i++) { + gs_free char *key = g_steal_pointer(&keys[i]); + char * value; + + if (NM_IN_STRSET(key, NM_CONFIG_KEYFILE_KEY_STOP_MATCH, NM_CONFIG_KEYFILE_KEY_MATCH_DEVICE)) + continue; + + if (j > 0 && nm_streq(vals[j - 1].name, key)) + continue; + + value = g_key_file_get_string(keyfile, group, key, NULL); + if (!value) + continue; + + vals[j++] = (NMUtilsNamedValue){ + .name = g_steal_pointer(&key), + .value_str = value, + }; + } + + g_free(keys); + + if (n_keys != j) { + gs_free NMUtilsNamedValue *vals2 = vals; + + /* since this buffer will be kept around for a long time, + * get rid of the excess allocation. */ + vals = nm_memdup(vals2, sizeof(NMUtilsNamedValue) * j); + n_keys = j; + } + + if (n_keys == 0) + nm_clear_g_free(&vals); + + connection_info->lookup_idx = vals; + connection_info->lookup_len = n_keys; } static void _match_section_infos_free(MatchSectionInfo *match_section_infos) { - guint i; + MatchSectionInfo *m; + gsize i; if (!match_section_infos) return; - for (i = 0; match_section_infos[i].group_name; i++) { - g_free(match_section_infos[i].group_name); - g_slist_free_full(match_section_infos[i].match_device.spec, g_free); + for (m = match_section_infos; m->group_name; m++) { + g_free(m->group_name); + g_slist_free_full(m->match_device.spec, g_free); + for (i = 0; i < m->lookup_len; i++) { + g_free(m->lookup_idx[i].name_mutable); + g_free(m->lookup_idx[i].value_str_mutable); + } + g_free((gpointer) m->lookup_idx); } g_free(match_section_infos); } @@ -1628,11 +1732,11 @@ _match_section_infos_construct(GKeyFile *keyfile, const char *prefix) match_section_infos = g_new0(MatchSectionInfo, ngroups + 1 + (connection_tag ? 1 : 0)); for (i = 0; i < ngroups; i++) { /* pass ownership of @group on... */ - _get_connection_info_init(&match_section_infos[i], keyfile, groups[ngroups - i - 1]); + _match_section_info_init(&match_section_infos[i], keyfile, groups[ngroups - i - 1]); } if (connection_tag) { /* pass ownership of @connection_tag on... */ - _get_connection_info_init(&match_section_infos[i], keyfile, connection_tag); + _match_section_info_init(&match_section_infos[i], keyfile, connection_tag); } g_free(groups); diff --git a/src/core/nm-config-data.h b/src/core/nm-config-data.h index d1b2c3744a..42d69a8b92 100644 --- a/src/core/nm-config-data.h +++ b/src/core/nm-config-data.h @@ -207,9 +207,9 @@ extern const char *__stop_connection_defaults[]; name; \ }) -char *nm_config_data_get_connection_default(const NMConfigData *self, - const char * property, - NMDevice * device); +const char *nm_config_data_get_connection_default(const NMConfigData *self, + const char * property, + NMDevice * device); gint64 nm_config_data_get_connection_default_int64(const NMConfigData *self, const char * property, @@ -218,22 +218,30 @@ gint64 nm_config_data_get_connection_default_int64(const NMConfigData *self, gint64 max, gint64 fallback); -char *nm_config_data_get_device_config(const NMConfigData *self, - const char * property, - NMDevice * device, - gboolean * has_match); +const char *nm_config_data_get_device_config(const NMConfigData *self, + const char * property, + NMDevice * device, + gboolean * has_match); -char *nm_config_data_get_device_config_by_pllink(const NMConfigData * self, - const char * property, - const NMPlatformLink *pllink, - const char * match_device_type, - gboolean * has_match); +const char *nm_config_data_get_device_config_by_pllink(const NMConfigData * self, + const char * property, + const NMPlatformLink *pllink, + const char * match_device_type, + gboolean * has_match); gboolean nm_config_data_get_device_config_boolean(const NMConfigData *self, const char * property, NMDevice * device, int val_no_match, int val_invalid); +gint64 nm_config_data_get_device_config_int64(const NMConfigData *self, + const char * property, + NMDevice * device, + int base, + gint64 min, + gint64 max, + gint64 val_no_match, + gint64 val_invalid); char ** nm_config_data_get_groups(const NMConfigData *self); char ** nm_config_data_get_keys(const NMConfigData *self, const char *group); diff --git a/src/core/tests/config/test-config.c b/src/core/tests/config/test-config.c index 3bb148152b..6794fbf6cb 100644 --- a/src/core/tests/config/test-config.c +++ b/src/core/tests/config/test-config.c @@ -151,6 +151,7 @@ test_config_simple(void) gs_unref_object NMConfig *config = NULL; gs_strfreev char ** plugins = NULL; char * value; + const char * cvalue; gs_unref_object NMDevice *dev50 = nm_test_device_new("00:00:00:00:00:50"); gs_unref_object NMDevice *dev51 = nm_test_device_new("00:00:00:00:00:51"); gs_unref_object NMDevice *dev52 = nm_test_device_new("00:00:00:00:00:52"); @@ -206,59 +207,50 @@ test_config_simple(void) g_assert_cmpstr(value, ==, "51"); g_free(value); - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv6.route-metric", - NULL); - g_assert_cmpstr(value, ==, NULL); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - NULL); - g_assert_cmpstr(value, ==, "50"); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - dev50); - g_assert_cmpstr(value, ==, "50"); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - dev51); - g_assert_cmpstr(value, ==, "51"); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.route-metric", - dev52); - g_assert_cmpstr(value, ==, "52"); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ethernet.mtu", - dev51); - g_assert_cmpstr(value, ==, "9000"); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ethernet.mtu", - dev50); - g_assert_cmpstr(value, ==, "1400"); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.dns-priority", - dev51); - g_assert_cmpstr(value, ==, NULL); - g_free(value); - - value = nm_config_data_get_connection_default(nm_config_get_data_orig(config), - "ipv4.dns-priority", - dev50); - g_assert_cmpstr(value, ==, "60"); - g_free(value); + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv6.route-metric", + NULL); + g_assert_cmpstr(cvalue, ==, NULL); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + NULL); + g_assert_cmpstr(cvalue, ==, "50"); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + dev50); + g_assert_cmpstr(cvalue, ==, "50"); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + dev51); + g_assert_cmpstr(cvalue, ==, "51"); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.route-metric", + dev52); + g_assert_cmpstr(cvalue, ==, "52"); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ethernet.mtu", + dev51); + g_assert_cmpstr(cvalue, ==, "9000"); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ethernet.mtu", + dev50); + g_assert_cmpstr(cvalue, ==, "1400"); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.dns-priority", + dev51); + g_assert_cmpstr(cvalue, ==, NULL); + + cvalue = nm_config_data_get_connection_default(nm_config_get_data_orig(config), + "ipv4.dns-priority", + dev50); + g_assert_cmpstr(cvalue, ==, "60"); } static void @@ -616,7 +608,7 @@ test_config_confdir(void) #define ASSERT_GET_CONN_DEFAULT(xconfig, xname, xvalue) \ G_STMT_START \ { \ - gs_free char *_value = \ + const char *_value = \ nm_config_data_get_connection_default(nm_config_get_data_orig(xconfig), \ (xname), \ NULL); \ diff --git a/src/libnm-glib-aux/nm-shared-utils.c b/src/libnm-glib-aux/nm-shared-utils.c index ff1b705006..936d900618 100644 --- a/src/libnm-glib-aux/nm-shared-utils.c +++ b/src/libnm-glib-aux/nm-shared-utils.c @@ -3244,11 +3244,8 @@ void nm_utils_named_value_clear_with_g_free(NMUtilsNamedValue *val) { if (val) { - gs_free gpointer x_name = NULL; - gs_free gpointer x_value = NULL; - - x_name = (gpointer) g_steal_pointer(&val->name); - x_value = g_steal_pointer(&val->value_ptr); + nm_clear_g_free(&val->name_mutable); + nm_clear_g_free(&val->value_ptr); } } |