static void name_owner_changed (DBusGProxy *proxy, const char *name, const char *old_owner, const char *new_owner, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); const char *sname = NM_DBUS_SERVICE; if (!strcmp (name, sname)) { if (priv->fetch_id) g_source_remove (priv->fetch_id); if (new_owner && strlen (new_owner) > 0) { priv->fetch_id = g_idle_add (fetch_connections, self); priv->service_running = TRUE; } else { priv->fetch_id = g_idle_add (remove_connections, self); priv->service_running = FALSE; } g_object_notify (G_OBJECT (self), NM_REMOTE_SETTINGS_SERVICE_RUNNING); } }
static void properties_changed_cb (DBusGProxy *proxy, GHashTable *properties, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); GHashTableIter iter; gpointer key, tmp; g_hash_table_iter_init (&iter, properties); while (g_hash_table_iter_next (&iter, &key, &tmp)) { GValue *value = tmp; if (!strcmp ((const char *) key, "Hostname")) { g_free (priv->hostname); priv->hostname = g_value_dup_string (value); g_object_notify (G_OBJECT (self), NM_REMOTE_SETTINGS_HOSTNAME); } if (!strcmp ((const char *) key, "CanModify")) { priv->can_modify = g_value_get_boolean (value); g_object_notify (G_OBJECT (self), NM_REMOTE_SETTINGS_CAN_MODIFY); } } }
static void get_all_cb (DBusGProxy *proxy, DBusGProxyCall *call, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); GHashTable *props = NULL; GError *error = NULL; if (!dbus_g_proxy_end_call (proxy, call, &error, DBUS_TYPE_G_MAP_OF_VARIANT, &props, G_TYPE_INVALID)) { /* Don't warn when the call times out because the settings service can't * be activated or whatever. */ if (!(error->domain == DBUS_GERROR && error->code == DBUS_GERROR_NO_REPLY)) { g_warning ("%s: couldn't retrieve system settings properties: (%d) %s.", __func__, error ? error->code : -1, (error && error->message) ? error->message : "(unknown)"); } g_clear_error (&error); return; } properties_changed_cb (NULL, props, self); g_hash_table_destroy (props); }
static void fetch_connections_done (DBusGProxy *proxy, GPtrArray *connections, GError *error, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); int i; if (error) { g_warning ("%s: error fetching %s connections: (%d) %s.", __func__, priv->scope == NM_CONNECTION_SCOPE_USER ? "user" : "system", error->code, error->message ? error->message : "(unknown)"); g_clear_error (&error); return; } /* Let listeners know we are done getting connections */ if (connections->len == 0) { g_signal_emit_by_name (self, NM_SETTINGS_INTERFACE_CONNECTIONS_READ); return; } for (i = 0; connections && (i < connections->len); i++) { char *path = g_ptr_array_index (connections, i); new_connection_cb (proxy, path, user_data); g_free (path); } g_ptr_array_free (connections, TRUE); }
static void dispose (GObject *object) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (object); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); if (priv->disposed) return; priv->disposed = TRUE; if (priv->fetch_id) g_source_remove (priv->fetch_id); while (g_slist_length (priv->add_list)) add_connection_info_dispose (self, (AddConnectionInfo *) priv->add_list->data); if (priv->connections) g_hash_table_destroy (priv->connections); if (priv->pending) g_hash_table_destroy (priv->pending); g_free (priv->hostname); g_object_unref (priv->dbus_proxy); g_object_unref (priv->proxy); g_object_unref (priv->props_proxy); dbus_g_connection_unref (priv->bus); G_OBJECT_CLASS (nm_remote_settings_parent_class)->dispose (object); }
static gboolean remove_connections (gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); clear_one_hash (priv->pending); clear_one_hash (priv->connections); return FALSE; }
static void connection_removed_cb (NMRemoteConnection *remote, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); const char *path; path = nm_connection_get_path (NM_CONNECTION (remote)); g_hash_table_remove (priv->connections, path); g_hash_table_remove (priv->pending, path); }
static gboolean fetch_connections (gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); priv->fetch_id = 0; org_freedesktop_NetworkManager_Settings_list_connections_async (priv->proxy, fetch_connections_done, self); return FALSE; }
static gboolean remove_connections (gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); GHashTableIter iter; gpointer value; g_hash_table_iter_init (&iter, priv->connections); while (g_hash_table_iter_next (&iter, NULL, &value)) g_signal_emit_by_name (NM_REMOTE_CONNECTION (value), "removed"); g_hash_table_remove_all (priv->connections); return FALSE; }
static void connection_init_result_cb (NMRemoteConnection *remote, GParamSpec *pspec, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); guint32 init_result = NM_REMOTE_CONNECTION_INIT_RESULT_UNKNOWN; const char *path; /* Disconnect from the init-result signal just to be safe */ g_signal_handlers_disconnect_matched (remote, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL, G_CALLBACK (connection_init_result_cb), self); path = nm_connection_get_path (NM_CONNECTION (remote)); g_object_get (G_OBJECT (remote), NM_REMOTE_CONNECTION_INIT_RESULT, &init_result, NULL); switch (init_result) { case NM_REMOTE_CONNECTION_INIT_RESULT_SUCCESS: /* ref it when adding to ->connections, since removing it from ->pending * will unref it. */ g_hash_table_insert (priv->connections, g_strdup (path), g_object_ref (remote)); /* Finally, let users know of the new connection now that it has all * its settings and is valid. */ g_signal_emit_by_name (self, "new-connection", remote); break; case NM_REMOTE_CONNECTION_INIT_RESULT_ERROR: default: break; } g_hash_table_remove (priv->pending, path); /* Let listeners know that all connections have been found */ if (!g_hash_table_size (priv->pending)) g_signal_emit_by_name (self, NM_SETTINGS_INTERFACE_CONNECTIONS_READ); }
static void fetch_connections_done (DBusGProxy *proxy, GPtrArray *connections, GError *error, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); int i; if (error) { /* Ignore settings service spawn errors */ if ( !g_error_matches (error, DBUS_GERROR, DBUS_GERROR_SERVICE_UNKNOWN) && !g_error_matches (error, DBUS_GERROR, DBUS_GERROR_NAME_HAS_NO_OWNER)) { g_warning ("%s: error fetching connections: (%d) %s.", __func__, error->code, error->message ? error->message : "(unknown)"); } g_clear_error (&error); /* We tried to read connections and failed */ g_signal_emit (self, signals[CONNECTIONS_READ], 0); return; } /* Let listeners know we are done getting connections */ if (connections->len == 0) g_signal_emit (self, signals[CONNECTIONS_READ], 0); else { priv->init_left = connections->len; for (i = 0; i < connections->len; i++) { char *path = g_ptr_array_index (connections, i); new_connection_cb (proxy, path, user_data); g_free (path); } } g_ptr_array_free (connections, TRUE); }
static void connection_visible_cb (NMRemoteConnection *remote, gboolean visible, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); const char *path; path = nm_connection_get_path (NM_CONNECTION (remote)); g_assert (path); /* When a connection becomes invisible, we put it back in the pending * hash until it becomes visible again. When it does, we move it back to * the normal connections hash. */ if (visible) { /* Connection visible to this user again */ if (g_hash_table_lookup (priv->pending, path)) { /* Move connection from pending to visible hash; emit for clients */ g_hash_table_insert (priv->connections, g_strdup (path), g_object_ref (remote)); g_hash_table_remove (priv->pending, path); g_signal_emit (self, signals[NEW_CONNECTION], 0, remote); } } else { /* Connection now invisible to this user */ if (g_hash_table_lookup (priv->connections, path)) { /* Move connection to pending hash and wait for it to become visible again */ g_hash_table_insert (priv->pending, g_strdup (path), g_object_ref (remote)); g_hash_table_remove (priv->connections, path); /* Signal to clients that the connection is gone; but we have to * block our connection removed handler so we don't destroy * the connection when the signal is emitted. */ g_signal_handlers_block_by_func (remote, connection_removed_cb, self); g_signal_emit_by_name (remote, NM_REMOTE_CONNECTION_REMOVED); g_signal_handlers_unblock_by_func (remote, connection_removed_cb, self); } } }
static NMRemoteConnection * new_connection_cb (DBusGProxy *proxy, const char *path, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); NMRemoteConnection *connection = NULL; /* Make double-sure we don't already have it */ connection = g_hash_table_lookup (priv->pending, path); if (connection) return connection; connection = g_hash_table_lookup (priv->connections, path); if (connection) return connection; /* Create a new connection object for it */ connection = nm_remote_connection_new (priv->bus, path); if (connection) { g_signal_connect (connection, NM_REMOTE_CONNECTION_REMOVED, G_CALLBACK (connection_removed_cb), self); g_signal_connect (connection, "visible", G_CALLBACK (connection_visible_cb), self); g_signal_connect (connection, "notify::" NM_REMOTE_CONNECTION_INIT_RESULT, G_CALLBACK (connection_init_result_cb), self); /* Add the connection to the pending table to wait for it to retrieve * it's settings asynchronously over D-Bus. The connection isn't * really valid until it has all its settings, so hide it until it does. */ g_hash_table_insert (priv->pending, g_strdup (path), connection); } return connection; }
static void connection_removed_cb (NMRemoteConnection *remote, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); AddConnectionInfo *addinfo; GError *add_error; const char *path; /* Might have been removed while it was waiting to be initialized */ addinfo = add_connection_info_find (self, remote); if (addinfo) { add_error = g_error_new_literal (NM_REMOTE_SETTINGS_ERROR, NM_REMOTE_SETTINGS_ERROR_CONNECTION_REMOVED, "Connection removed before it was initialized"); add_connection_info_complete (self, addinfo, add_error); g_error_free (add_error); } path = nm_connection_get_path (NM_CONNECTION (remote)); g_hash_table_remove (priv->connections, path); g_hash_table_remove (priv->pending, path); }
static gboolean add_connection (NMSettingsInterface *settings, NMConnection *connection, NMSettingsAddConnectionFunc callback, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (settings); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); AddConnectionInfo *info; GHashTable *new_settings; info = g_malloc0 (sizeof (AddConnectionInfo)); info->self = settings; info->callback = callback; info->callback_data = user_data; new_settings = nm_connection_to_hash (connection); org_freedesktop_NetworkManagerSettings_add_connection_async (priv->proxy, new_settings, add_connection_done, info); g_hash_table_destroy (new_settings); return TRUE; }
static void new_connection_cb (DBusGProxy *proxy, const char *path, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); NMRemoteConnection *connection; connection = nm_remote_connection_new (priv->bus, priv->scope, path); if (connection) { g_signal_connect (connection, "removed", G_CALLBACK (connection_removed_cb), self); g_signal_connect (connection, "notify::" NM_REMOTE_CONNECTION_INIT_RESULT, G_CALLBACK (connection_init_result_cb), self); /* Add the connection to the pending table to wait for it to retrieve * it's settings asynchronously over D-Bus. The connection isn't * really valid until it has all its settings, so hide it until it does. */ g_hash_table_insert (priv->pending, g_strdup (path), connection); } }
static void connection_init_result_cb (NMRemoteConnection *remote, GParamSpec *pspec, gpointer user_data) { NMRemoteSettings *self = NM_REMOTE_SETTINGS (user_data); NMRemoteSettingsPrivate *priv = NM_REMOTE_SETTINGS_GET_PRIVATE (self); guint32 init_result = NM_REMOTE_CONNECTION_INIT_RESULT_UNKNOWN; AddConnectionInfo *addinfo; const char *path; GError *add_error = NULL; gboolean remove_from_pending = TRUE; /* Disconnect from the init-result signal just to be safe */ g_signal_handlers_disconnect_matched (remote, G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL, G_CALLBACK (connection_init_result_cb), self); path = nm_connection_get_path (NM_CONNECTION (remote)); g_object_get (G_OBJECT (remote), NM_REMOTE_CONNECTION_INIT_RESULT, &init_result, NULL); addinfo = add_connection_info_find (self, remote); switch (init_result) { case NM_REMOTE_CONNECTION_INIT_RESULT_SUCCESS: /* ref it when adding to ->connections, since removing it from ->pending * will unref it. */ g_hash_table_insert (priv->connections, g_strdup (path), g_object_ref (remote)); /* If there's a pending AddConnection request, complete that here before * signaling new-connection. */ if (addinfo) add_connection_info_complete (self, addinfo, NULL); /* Finally, let users know of the new connection now that it has all * its settings and is valid. */ g_signal_emit (self, signals[NEW_CONNECTION], 0, remote); break; case NM_REMOTE_CONNECTION_INIT_RESULT_INVISIBLE: remove_from_pending = FALSE; /* fall through */ case NM_REMOTE_CONNECTION_INIT_RESULT_ERROR: /* Complete pending AddConnection callbacks */ if (addinfo) { add_error = g_error_new_literal (NM_REMOTE_SETTINGS_ERROR, NM_REMOTE_SETTINGS_ERROR_CONNECTION_UNAVAILABLE, "Connection not visible or not available"); add_connection_info_complete (self, addinfo, add_error); g_error_free (add_error); } break; default: break; } if (remove_from_pending) g_hash_table_remove (priv->pending, path); /* Let listeners know that all connections have been found */ priv->init_left--; if (priv->init_left == 0) g_signal_emit (self, signals[CONNECTIONS_READ], 0); }