From 650cbcc10849af93a5a308fb8d4073e7455a8140 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Wed, 12 Aug 2009 17:13:24 -0500 Subject: [PATCH] ifupdown: port to new settings API --- .../plugins/ifupdown/nm-ifupdown-connection.c | 67 ++++------- system-settings/plugins/ifupdown/parser.c | 51 ++++---- system-settings/plugins/ifupdown/parser.h | 7 +- system-settings/plugins/ifupdown/plugin.c | 111 +++++++++--------- 4 files changed, 103 insertions(+), 133 deletions(-) diff --git a/system-settings/plugins/ifupdown/nm-ifupdown-connection.c b/system-settings/plugins/ifupdown/nm-ifupdown-connection.c index 275771f34..ad9a479a6 100644 --- a/system-settings/plugins/ifupdown/nm-ifupdown-connection.c +++ b/system-settings/plugins/ifupdown/nm-ifupdown-connection.c @@ -29,13 +29,10 @@ #include #include #include -#include #include "nm-ifupdown-connection.h" #include "parser.h" -G_DEFINE_TYPE (NMIfupdownConnection, - nm_ifupdown_connection, - NM_TYPE_SYSCONFIG_CONNECTION) +G_DEFINE_TYPE (NMIfupdownConnection, nm_ifupdown_connection, NM_TYPE_SYSCONFIG_CONNECTION) #define NM_IFUPDOWN_CONNECTION_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_IFUPDOWN_CONNECTION, NMIfupdownConnectionPrivate)) @@ -60,36 +57,16 @@ nm_ifupdown_connection_new (if_block *block) NULL); } -static gboolean -update (NMExportedConnection *exported, - GHashTable *new_settings, - GError **err) -{ - g_set_error (err, NM_SYSCONFIG_SETTINGS_ERROR, - NM_SYSCONFIG_SETTINGS_ERROR_UPDATE_NOT_SUPPORTED, - "%s.%d - %s", __FILE__, __LINE__, "connection update not supported (read-only)."); - return FALSE; -} - -static gboolean -do_delete (NMExportedConnection *exported, GError **err) -{ - g_set_error (err, NM_SYSCONFIG_SETTINGS_ERROR, - NM_SYSCONFIG_SETTINGS_ERROR_DELETE_NOT_SUPPORTED, - "%s", "ifupdown - connection delete not supported (read-only)."); - return FALSE; -} - static void -service_get_secrets (NMExportedConnection *exported, - const gchar *setting_name, - const gchar **hints, - gboolean request_new, - DBusGMethodInvocation *context) +get_secrets (NMExportedConnection *exported, + const gchar *setting_name, + const gchar **hints, + gboolean request_new, + DBusGMethodInvocation *context) { GError *error = NULL; - PLUGIN_PRINT ("SCPlugin-Ifupdown", "get_secrets for setting_name:'%s')", setting_name); + PLUGIN_PRINT ("SCPlugin-Ifupdown", "get_secrets() for setting_name:'%s'", setting_name); /* FIXME: Only wifi secrets are supported for now */ if (strcmp (setting_name, NM_SETTING_WIRELESS_SECURITY_SETTING_NAME)) { @@ -104,10 +81,9 @@ service_get_secrets (NMExportedConnection *exported, return; } - NM_EXPORTED_CONNECTION_CLASS (nm_ifupdown_connection_parent_class)->service_get_secrets (exported, setting_name, hints, request_new, context); + NM_EXPORTED_CONNECTION_CLASS (nm_ifupdown_connection_parent_class)->get_secrets (exported, setting_name, hints, request_new, context); } -/* GObject */ static void nm_ifupdown_connection_init (NMIfupdownConnection *connection) { @@ -120,23 +96,29 @@ constructor (GType type, { GObject *object; NMIfupdownConnectionPrivate *priv; - NMConnection *wrapped = nm_connection_new(); + GError *error = NULL; object = G_OBJECT_CLASS (nm_ifupdown_connection_parent_class)->constructor (type, n_construct_params, construct_params); g_return_val_if_fail (object, NULL); priv = NM_IFUPDOWN_CONNECTION_GET_PRIVATE (object); if (!priv) { - nm_warning ("%s.%d - no private instance.", __FILE__, __LINE__); + g_warning ("%s.%d - no private instance.", __FILE__, __LINE__); goto err; } if (!priv->ifblock) { - nm_warning ("(ifupdown) ifblock not provided to constructor."); + g_warning ("(ifupdown) ifblock not provided to constructor."); goto err; } - g_object_set (object, NM_EXPORTED_CONNECTION_CONNECTION, wrapped, NULL); - g_object_unref (wrapped); + if (!ifupdown_update_connection_from_if_block (NM_CONNECTION (object), priv->ifblock, &error)) { + g_warning ("%s.%d - invalid connection read from /etc/network/interfaces: (%d) %s", + __FILE__, + __LINE__, + error ? error->code : -1, + error && error->message ? error->message : "(unknown)"); + goto err; + } return object; @@ -145,12 +127,6 @@ constructor (GType type, return NULL; } -static void -finalize (GObject *object) -{ - G_OBJECT_CLASS (nm_ifupdown_connection_parent_class)->finalize (object); -} - static void set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) @@ -197,11 +173,8 @@ nm_ifupdown_connection_class_init (NMIfupdownConnectionClass *ifupdown_connectio object_class->constructor = constructor; object_class->set_property = set_property; object_class->get_property = get_property; - object_class->finalize = finalize; - connection_class->update = update; - connection_class->do_delete = do_delete; - connection_class->service_get_secrets = service_get_secrets; + connection_class->get_secrets = get_secrets; /* Properties */ g_object_class_install_property diff --git a/system-settings/plugins/ifupdown/parser.c b/system-settings/plugins/ifupdown/parser.c index 81da0aea9..dc2f8abf6 100644 --- a/system-settings/plugins/ifupdown/parser.c +++ b/system-settings/plugins/ifupdown/parser.c @@ -525,58 +525,53 @@ update_ip4_setting_from_if_block(NMConnection *connection, nm_connection_add_setting(connection, NM_SETTING(ip4_setting)); } -void -ifupdown_update_connection_from_if_block(NMConnection *connection, - if_block *block) +gboolean +ifupdown_update_connection_from_if_block (NMConnection *connection, + if_block *block, + GError **error) { const char *type = NULL; char *idstr = NULL; char *uuid_base = NULL; - GError *verify_error = NULL; char *uuid = NULL; + NMSettingConnection *s_con; + gboolean success = FALSE; - NMSettingConnection *connection_setting = - NM_SETTING_CONNECTION(nm_connection_get_setting - (connection, NM_TYPE_SETTING_CONNECTION)); - - if(!connection_setting) { - connection_setting = NM_SETTING_CONNECTION(nm_setting_connection_new()); - nm_connection_add_setting(connection, NM_SETTING(connection_setting)); + s_con = (NMSettingConnection *) nm_connection_get_setting (connection, NM_TYPE_SETTING_CONNECTION); + if(!s_con) { + s_con = NM_SETTING_CONNECTION (nm_setting_connection_new()); + g_assert (s_con); + nm_connection_add_setting (connection, NM_SETTING (s_con)); } type = _ifupdownplugin_guess_connection_type (block); - idstr = g_strconcat("Ifupdown (", block->name,")", NULL); + idstr = g_strconcat ("Ifupdown (", block->name, ")", NULL); uuid_base = idstr; - uuid = nm_utils_uuid_generate_from_string(uuid_base); - g_object_set (connection_setting, + uuid = nm_utils_uuid_generate_from_string (uuid_base); + g_object_set (s_con, NM_SETTING_CONNECTION_TYPE, type, NM_SETTING_CONNECTION_ID, idstr, NM_SETTING_CONNECTION_UUID, uuid, NM_SETTING_CONNECTION_READ_ONLY, TRUE, + NM_SETTING_CONNECTION_AUTOCONNECT, FALSE, NULL); g_free (uuid); - PLUGIN_PRINT("SCPlugin-Ifupdown", "update_connection_setting_from_if_block: name:%s, type:%s, autoconnect:%d, id:%s, uuid: %s", - block->name, type, - ((gboolean) strcmp("dhcp", type) == 0), - idstr, - nm_setting_connection_get_uuid (connection_setting)); + PLUGIN_PRINT("SCPlugin-Ifupdown", "update_connection_setting_from_if_block: name:%s, type:%s, id:%s, uuid: %s", + block->name, type, idstr, nm_setting_connection_get_uuid (s_con)); - if(!strcmp (NM_SETTING_WIRED_SETTING_NAME, type)) { + if (!strcmp (NM_SETTING_WIRED_SETTING_NAME, type)) update_wired_setting_from_if_block (connection, block); - } - else if(!strcmp (NM_SETTING_WIRELESS_SETTING_NAME, type)) { + else if (!strcmp (NM_SETTING_WIRELESS_SETTING_NAME, type)) { update_wireless_setting_from_if_block (connection, block); update_wireless_security_setting_from_if_block (connection, block); } - update_ip4_setting_from_if_block(connection, block); + update_ip4_setting_from_if_block (connection, block); - if(!nm_connection_verify(connection, &verify_error)) { - nm_warning("connection broken: %s (%d)", - verify_error->message, verify_error->code); - } + success = nm_connection_verify (connection, error); - g_free(idstr); + g_free (idstr); + return success; } diff --git a/system-settings/plugins/ifupdown/parser.h b/system-settings/plugins/ifupdown/parser.h index 5e7ddd605..6aebb8c71 100644 --- a/system-settings/plugins/ifupdown/parser.h +++ b/system-settings/plugins/ifupdown/parser.h @@ -26,8 +26,9 @@ G_BEGIN_DECLS -void -ifupdown_update_connection_from_if_block(NMConnection *connection, - if_block *block); +gboolean +ifupdown_update_connection_from_if_block (NMConnection *connection, + if_block *block, + GError **error); G_END_DECLS diff --git a/system-settings/plugins/ifupdown/plugin.c b/system-settings/plugins/ifupdown/plugin.c index a38f40cfc..e8e4e25e9 100644 --- a/system-settings/plugins/ifupdown/plugin.c +++ b/system-settings/plugins/ifupdown/plugin.c @@ -86,8 +86,8 @@ static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class); G_DEFINE_TYPE_EXTENDED (SCPluginIfupdown, sc_plugin_ifupdown, G_TYPE_OBJECT, 0, - G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE, - system_config_interface_init)) + G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE, + system_config_interface_init)) #define SC_PLUGIN_IFUPDOWN_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SC_TYPE_PLUGIN_IFUPDOWN, SCPluginIfupdownPrivate)) @@ -124,9 +124,6 @@ GObject__set_property (GObject *object, guint prop_id, static void GObject__dispose (GObject *object); -static void -GObject__finalize (GObject *object); - /* other helpers */ static const char * get_hostname (NMSystemConfigInterface *config); @@ -155,7 +152,6 @@ sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class) g_type_class_add_private (req_class, sizeof (SCPluginIfupdownPrivate)); object_class->dispose = GObject__dispose; - object_class->finalize = GObject__finalize; object_class->get_property = GObject__get_property; object_class->set_property = GObject__set_property; @@ -176,13 +172,19 @@ sc_plugin_ifupdown_class_init (SCPluginIfupdownClass *req_class) NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME); } +static void +ignore_cb (NMSettingsConnectionInterface *connection, + GError *error, + gpointer user_data) +{ +} + static void bind_device_to_connection (SCPluginIfupdown *self, GUdevDevice *device, - NMExportedConnection *exported) + NMIfupdownConnection *exported) { GByteArray *mac_address; - NMConnection *connection; NMSetting *s_wired = NULL; NMSetting *s_wifi = NULL; const char *iface, *address; @@ -194,13 +196,6 @@ bind_device_to_connection (SCPluginIfupdown *self, return; } - connection = nm_exported_connection_get_connection (exported); - if (!connection) { - PLUGIN_WARN ("SCPluginIfupdown", "no device locking possible. " - "NMExportedConnection doesnt have a real connection."); - return; - } - address = g_udev_device_get_sysfs_attr (device, "address"); if (!address || !strlen (address)) { PLUGIN_WARN ("SCPluginIfupdown", "failed to get MAC address for %s", iface); @@ -217,8 +212,8 @@ bind_device_to_connection (SCPluginIfupdown *self, mac_address = g_byte_array_sized_new (ETH_ALEN); g_byte_array_append (mac_address, &(tmp_mac->ether_addr_octet[0]), ETH_ALEN); - s_wired = nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRED); - s_wifi = nm_connection_get_setting (connection, NM_TYPE_SETTING_WIRELESS); + s_wired = nm_connection_get_setting (NM_CONNECTION (exported), NM_TYPE_SETTING_WIRED); + s_wifi = nm_connection_get_setting (NM_CONNECTION (exported), NM_TYPE_SETTING_WIRELESS); if (s_wifi) { PLUGIN_PRINT ("SCPluginIfupdown", "locking wired connection setting"); g_object_set (s_wired, NM_SETTING_WIRED_MAC_ADDRESS, mac_address, NULL); @@ -226,8 +221,11 @@ bind_device_to_connection (SCPluginIfupdown *self, PLUGIN_PRINT ("SCPluginIfupdown", "locking wireless connection setting"); g_object_set (s_wifi, NM_SETTING_WIRELESS_MAC_ADDRESS, mac_address, NULL); } - g_byte_array_free (mac_address, TRUE); + + nm_settings_connection_interface_update (NM_SETTINGS_CONNECTION_INTERFACE (exported), + ignore_cb, + NULL); } static void @@ -235,7 +233,7 @@ udev_device_added (SCPluginIfupdown *self, GUdevDevice *device) { SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (self); const char *iface, *path; - NMExportedConnection *exported; + NMIfupdownConnection *exported; iface = g_udev_device_get_name (device); path = g_udev_device_get_sysfs_path (device); @@ -248,7 +246,7 @@ udev_device_added (SCPluginIfupdown *self, GUdevDevice *device) /* if we have a configured connection for this particular iface * we want to either unmanage the device or lock it */ - exported = (NMExportedConnection *) g_hash_table_lookup (priv->iface_connections, iface); + exported = (NMIfupdownConnection *) g_hash_table_lookup (priv->iface_connections, iface); if (!exported) return; @@ -352,14 +350,23 @@ SCPluginIfupdown_init (NMSystemConfigInterface *config) if(!strcmp ("auto", block->type)) g_hash_table_insert (auto_ifaces, block->name, GUINT_TO_POINTER (1)); else if (!strcmp ("iface", block->type) && strcmp ("lo", block->name)) { - NMExportedConnection *exported; + NMIfupdownConnection *exported; - g_hash_table_remove (priv->iface_connections, block->name); + /* Remove any connection for this block that was previously found */ + exported = g_hash_table_lookup (priv->iface_connections, block->name); + if (exported) { + nm_settings_connection_interface_delete (NM_SETTINGS_CONNECTION_INTERFACE (exported), + ignore_cb, + NULL); + g_hash_table_remove (priv->iface_connections, block->name); + } - exported = NM_EXPORTED_CONNECTION (nm_ifupdown_connection_new (block)); - ifupdown_update_connection_from_if_block (nm_exported_connection_get_connection (exported), - block); - g_hash_table_insert (priv->iface_connections, block->name, exported); + /* add the new connection */ + exported = nm_ifupdown_connection_new (block); + if (exported) { + g_hash_table_insert (priv->iface_connections, block->name, exported); + g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, exported); + } } block = block->next; } @@ -367,17 +374,20 @@ SCPluginIfupdown_init (NMSystemConfigInterface *config) /* Make 'auto' interfaces autoconnect=TRUE */ keys = g_hash_table_get_keys (priv->iface_connections); for (iter = keys; iter; iter = g_list_next (iter)) { - NMExportedConnection *exported; - NMConnection *wrapped; + NMIfupdownConnection *exported; NMSetting *setting; if (!g_hash_table_lookup (auto_ifaces, iter->data)) continue; exported = g_hash_table_lookup (priv->iface_connections, iter->data); - wrapped = nm_exported_connection_get_connection (exported); - setting = NM_SETTING (nm_connection_get_setting (wrapped, NM_TYPE_SETTING_CONNECTION)); + setting = NM_SETTING (nm_connection_get_setting (NM_CONNECTION (exported), NM_TYPE_SETTING_CONNECTION)); g_object_set (setting, NM_SETTING_CONNECTION_AUTOCONNECT, TRUE, NULL); + + nm_settings_connection_interface_update (NM_SETTINGS_CONNECTION_INTERFACE (exported), + ignore_cb, + NULL); + PLUGIN_PRINT("SCPlugin-Ifupdown", "autoconnect"); } g_list_free (keys); @@ -433,15 +443,15 @@ SCPluginIfupdown_get_connections (NMSystemConfigInterface *config) { SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config); GSList *connections = NULL; - GList *priv_list = g_hash_table_get_values(priv->iface_connections); - GList *it = priv_list; + GHashTableIter iter; + gpointer value; + PLUGIN_PRINT("SCPlugin-Ifupdown", "(%d) ... get_connections.", GPOINTER_TO_UINT(config)); - while(it) { - NMExportedConnection *conn = it->data; - connections = g_slist_append(connections, conn); - it = it->next; - } + g_hash_table_iter_init (&iter, priv->iface_connections); + while (g_hash_table_iter_next (&iter, NULL, &value)) + connections = g_slist_prepend (connections, value); + PLUGIN_PRINT("SCPlugin-Ifupdown", "(%d) connections count: %d", GPOINTER_TO_UINT(config), g_slist_length(connections)); return connections; } @@ -455,28 +465,25 @@ static GSList* SCPluginIfupdown_get_unmanaged_specs (NMSystemConfigInterface *config) { SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config); - GList *devs, *iter; GSList *specs = NULL; + GHashTableIter iter; + gpointer value; if (!ALWAYS_UNMANAGE && !priv->unmanage_well_known) return NULL; - devs = g_hash_table_get_values (priv->well_known_ifaces); - PLUGIN_PRINT("Ifupdown", "get unmanaged devices count: %d", g_list_length (devs)); + PLUGIN_PRINT("Ifupdown", "get unmanaged devices count: %d", + g_hash_table_size (priv->well_known_ifaces)); - for (iter = devs; iter; iter = g_list_next (iter)) { - GUdevDevice *device = G_UDEV_DEVICE (iter->data); + g_hash_table_iter_init (&iter, priv->well_known_ifaces); + while (g_hash_table_iter_next (&iter, NULL, &value)) { + GUdevDevice *device = G_UDEV_DEVICE (value); const char *address; - char *spec; address = g_udev_device_get_sysfs_attr (device, "address"); - if (!address) - continue; - - spec = g_strdup_printf ("mac:%s", address); - specs = g_slist_append (specs, spec); + if (address) + specs = g_slist_append (specs, g_strdup_printf ("mac:%s", address)); } - g_list_free (devs); return specs; } @@ -619,12 +626,6 @@ GObject__dispose (GObject *object) G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->dispose (object); } -static void -GObject__finalize (GObject *object) -{ - G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->finalize (object); -} - G_MODULE_EXPORT GObject * nm_system_config_factory (void) {