static NMIfcfgConnection * find_by_path (SCPluginIfcfg *self, const char *path) { SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self); GHashTableIter iter; NMSettingsConnection *candidate = NULL; g_return_val_if_fail (path != NULL, NULL); g_hash_table_iter_init (&iter, priv->connections); while (g_hash_table_iter_next (&iter, NULL, (gpointer) &candidate)) { if (g_strcmp0 (path, nm_settings_connection_get_filename (candidate)) == 0) return NM_IFCFG_CONNECTION (candidate); } return NULL; }
static GHashTable * _paths_from_connections (GHashTable *connections) { GHashTableIter iter; NMIfcfgConnection *connection; GHashTable *paths = g_hash_table_new (g_str_hash, g_str_equal); g_hash_table_iter_init (&iter, connections); while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &connection)) { const char *path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection)); if (path) g_hash_table_add (paths, (void *) path); } return paths; }
static void connection_ifcfg_changed (NMIfcfgConnection *connection, gpointer user_data) { SCPluginIfcfg *self = SC_PLUGIN_IFCFG (user_data); SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self); const char *path; path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection)); g_return_if_fail (path != NULL); if (!priv->ifcfg_monitor) { _LOGD ("connection_ifcfg_changed("NM_IFCFG_CONNECTION_LOG_FMTD"): %s", NM_IFCFG_CONNECTION_LOG_ARGD (connection), "ignore event"); return; } _LOGD ("connection_ifcfg_changed("NM_IFCFG_CONNECTION_LOG_FMTD"): %s", NM_IFCFG_CONNECTION_LOG_ARGD (connection), "reload"); update_connection (self, NULL, path, connection, TRUE, NULL, NULL); }
static void read_connections (SCPluginIfcfg *plugin) { SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (plugin); GDir *dir; GError *err = NULL; const char *item; GHashTable *alive_connections; GHashTableIter iter; NMIfcfgConnection *connection; GPtrArray *dead_connections = NULL; guint i; GPtrArray *filenames; GHashTable *paths; dir = g_dir_open (IFCFG_DIR, 0, &err); if (!dir) { _LOGW ("Could not read directory '%s': %s", IFCFG_DIR, err->message); g_error_free (err); return; } alive_connections = g_hash_table_new (NULL, NULL); filenames = g_ptr_array_new_with_free_func (g_free); while ((item = g_dir_read_name (dir))) { char *full_path; if (utils_should_ignore_file (item, TRUE)) continue; if (utils_is_ifcfg_alias_file (item, NULL)) continue; full_path = g_build_filename (IFCFG_DIR, item, NULL); if (!utils_get_ifcfg_name (full_path, TRUE)) g_free (full_path); else g_ptr_array_add (filenames, full_path); } g_dir_close (dir); /* While reloading, we don't replace connections that we already loaded while * iterating over the files. * * To have sensible, reproducible behavior, sort the paths by last modification * time prefering older files. */ paths = _paths_from_connections (priv->connections); g_ptr_array_sort_with_data (filenames, (GCompareDataFunc) _sort_paths, paths); g_hash_table_destroy (paths); for (i = 0; i < filenames->len; i++) { connection = update_connection (plugin, NULL, filenames->pdata[i], NULL, FALSE, alive_connections, NULL); if (connection) g_hash_table_add (alive_connections, connection); } g_ptr_array_free (filenames, TRUE); g_hash_table_iter_init (&iter, priv->connections); while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &connection)) { if ( !g_hash_table_contains (alive_connections, connection) && nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection))) { if (!dead_connections) dead_connections = g_ptr_array_new (); g_ptr_array_add (dead_connections, connection); } } g_hash_table_destroy (alive_connections); if (dead_connections) { for (i = 0; i < dead_connections->len; i++) remove_connection (plugin, dead_connections->pdata[i]); g_ptr_array_free (dead_connections, TRUE); } }
static NMIfcfgConnection * update_connection (SCPluginIfcfg *self, NMConnection *source, const char *full_path, NMIfcfgConnection *connection, gboolean protect_existing_connection, GHashTable *protected_connections, GError **error) { SCPluginIfcfgPrivate *priv = SC_PLUGIN_IFCFG_GET_PRIVATE (self); NMIfcfgConnection *connection_new; NMIfcfgConnection *connection_by_uuid; GError *local = NULL; const char *new_unmanaged = NULL, *old_unmanaged = NULL; const char *new_unrecognized = NULL, *old_unrecognized = NULL; gboolean unmanaged_changed = FALSE, unrecognized_changed = FALSE; const char *uuid; g_return_val_if_fail (!source || NM_IS_CONNECTION (source), NULL); g_return_val_if_fail (full_path || source, NULL); if (full_path) _LOGD ("loading from file \"%s\"...", full_path); /* Create a NMIfcfgConnection instance, either by reading from @full_path or * based on @source. */ connection_new = nm_ifcfg_connection_new (source, full_path, error); if (!connection_new) { /* Unexpected failure. Probably the file is invalid? */ if ( connection && !protect_existing_connection && (!protected_connections || !g_hash_table_contains (protected_connections, connection))) remove_connection (self, connection); return NULL; } uuid = nm_connection_get_uuid (NM_CONNECTION (connection_new)); connection_by_uuid = g_hash_table_lookup (priv->connections, uuid); if ( connection && connection != connection_by_uuid) { if ( (protect_existing_connection && connection_by_uuid != NULL) || (protected_connections && g_hash_table_contains (protected_connections, connection))) { NMIfcfgConnection *conflicting = (protect_existing_connection && connection_by_uuid != NULL) ? connection_by_uuid : connection; if (source) _LOGW ("cannot update protected connection "NM_IFCFG_CONNECTION_LOG_FMT" due to conflicting UUID %s", NM_IFCFG_CONNECTION_LOG_ARG (conflicting), uuid); else _LOGW ("cannot load %s due to conflicting UUID for "NM_IFCFG_CONNECTION_LOG_FMT, full_path, NM_IFCFG_CONNECTION_LOG_ARG (conflicting)); g_object_unref (connection_new); g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Cannot update protected connection due to conflicting UUID"); return NULL; } /* The new connection has a different UUID then the original one that we * are about to update. Remove @connection. */ remove_connection (self, connection); } /* Check if the found connection with the same UUID is not protected from updating. */ if ( connection_by_uuid && ( (!connection && protect_existing_connection) || (protected_connections && g_hash_table_contains (protected_connections, connection_by_uuid)))) { if (source) _LOGW ("cannot update connection due to conflicting UUID for "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_by_uuid)); else _LOGW ("cannot load %s due to conflicting UUID for "NM_IFCFG_CONNECTION_LOG_FMT, full_path, NM_IFCFG_CONNECTION_LOG_ARG (connection_by_uuid)); g_object_unref (connection_new); g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Skip updating protected connection during reload"); return NULL; } /* Evaluate unmanaged/unrecognized flags. */ if (connection_by_uuid) old_unmanaged = nm_ifcfg_connection_get_unmanaged_spec (connection_by_uuid); new_unmanaged = nm_ifcfg_connection_get_unmanaged_spec (connection_new); unmanaged_changed = g_strcmp0 (old_unmanaged, new_unmanaged); if (connection_by_uuid) old_unrecognized = nm_ifcfg_connection_get_unrecognized_spec (connection_by_uuid); new_unrecognized = nm_ifcfg_connection_get_unrecognized_spec (connection_new); unrecognized_changed = g_strcmp0 (old_unrecognized, new_unrecognized); if (connection_by_uuid) { const char *old_path; old_path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid)); if ( !unmanaged_changed && !unrecognized_changed && nm_connection_compare (NM_CONNECTION (connection_by_uuid), NM_CONNECTION (connection_new), NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS | NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS)) { if (old_path && g_strcmp0 (old_path, full_path) != 0) _LOGI ("rename \"%s\" to "NM_IFCFG_CONNECTION_LOG_FMT" without other changes", nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid)), NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); } else { /******************************************************* * UPDATE *******************************************************/ if (source) _LOGI ("update "NM_IFCFG_CONNECTION_LOG_FMT" from %s", NM_IFCFG_CONNECTION_LOG_ARG (connection_new), NM_IFCFG_CONNECTION_LOG_PATH (old_path)); else if (!g_strcmp0 (old_path, nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_new)))) _LOGI ("update "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); else if (old_path) _LOGI ("rename \"%s\" to "NM_IFCFG_CONNECTION_LOG_FMT, old_path, NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); else _LOGI ("update and persist "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); g_object_set (connection_by_uuid, NM_IFCFG_CONNECTION_UNMANAGED_SPEC, new_unmanaged, NM_IFCFG_CONNECTION_UNRECOGNIZED_SPEC, new_unrecognized, NULL); if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (connection_by_uuid), NM_CONNECTION (connection_new), FALSE, /* don't set Unsaved */ "ifcfg-update", &local)) { /* Shouldn't ever get here as 'connection_new' was verified by the reader already * and the UUID did not change. */ g_assert_not_reached (); } g_assert_no_error (local); if (new_unmanaged || new_unrecognized) { if (!old_unmanaged && !old_unrecognized) { g_object_ref (connection_by_uuid); /* Unexport the connection by telling the settings service it's * been removed. */ nm_settings_connection_signal_remove (NM_SETTINGS_CONNECTION (connection_by_uuid)); /* Remove the path so that claim_connection() doesn't complain later when * interface gets managed and connection is re-added. */ nm_connection_set_path (NM_CONNECTION (connection_by_uuid), NULL); /* signal_remove() will end up removing the connection from our hash, * so add it back now. */ g_hash_table_insert (priv->connections, g_strdup (nm_connection_get_uuid (NM_CONNECTION (connection_by_uuid))), connection_by_uuid); } } else { if (old_unmanaged /* && !new_unmanaged */) { _LOGI ("Managing connection "NM_IFCFG_CONNECTION_LOG_FMT" and its device because NM_CONTROLLED was true.", NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_by_uuid); } else if (old_unrecognized /* && !new_unrecognized */) { _LOGI ("Managing connection "NM_IFCFG_CONNECTION_LOG_FMT" because it is now a recognized type.", NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_by_uuid); } } if (unmanaged_changed) g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED); if (unrecognized_changed) g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNRECOGNIZED_SPECS_CHANGED); } nm_settings_connection_set_filename (NM_SETTINGS_CONNECTION (connection_by_uuid), full_path); g_object_unref (connection_new); return connection_by_uuid; } else { /******************************************************* * ADD *******************************************************/ if (source) _LOGI ("add connection "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); else _LOGI ("new connection "NM_IFCFG_CONNECTION_LOG_FMT, NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); g_hash_table_insert (priv->connections, g_strdup (uuid), connection_new); g_signal_connect (connection_new, NM_SETTINGS_CONNECTION_REMOVED, G_CALLBACK (connection_removed_cb), self); if (nm_ifcfg_connection_get_unmanaged_spec (connection_new)) { const char *spec; const char *device_id; spec = nm_ifcfg_connection_get_unmanaged_spec (connection_new); device_id = strchr (spec, ':'); if (device_id) device_id++; else device_id = spec; _LOGW ("Ignoring connection "NM_IFCFG_CONNECTION_LOG_FMT" / device '%s' due to NM_CONTROLLED=no.", NM_IFCFG_CONNECTION_LOG_ARG (connection_new), device_id); } else if (nm_ifcfg_connection_get_unrecognized_spec (connection_new)) _LOGW ("Ignoring connection "NM_IFCFG_CONNECTION_LOG_FMT" of unrecognized type.", NM_IFCFG_CONNECTION_LOG_ARG (connection_new)); /* watch changes of ifcfg hardlinks */ g_signal_connect (G_OBJECT (connection_new), "ifcfg-changed", G_CALLBACK (connection_ifcfg_changed), self); if (!source) { /* Only raise the signal if we were called without source, i.e. if we read the connection from file. * Otherwise, we were called by add_connection() which does not expect the signal. */ if (nm_ifcfg_connection_get_unmanaged_spec (connection_new)) g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED); else if (nm_ifcfg_connection_get_unrecognized_spec (connection_new)) g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNRECOGNIZED_SPECS_CHANGED); else g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_new); } return connection_new; } }
static gboolean _dispatcher_call (DispatcherAction action, gboolean blocking, NMSettingsConnection *settings_connection, NMConnection *applied_connection, NMDevice *device, NMConnectivityState connectivity_state, const char *vpn_iface, NMProxyConfig *vpn_proxy_config, NMIP4Config *vpn_ip4_config, NMIP6Config *vpn_ip6_config, DispatcherFunc callback, gpointer user_data, guint *out_call_id) { GVariant *connection_dict; GVariantBuilder connection_props; GVariantBuilder device_props; GVariantBuilder device_proxy_props; GVariantBuilder device_ip4_props; GVariantBuilder device_ip6_props; GVariant *device_dhcp4_props = NULL; GVariant *device_dhcp6_props = NULL; GVariantBuilder vpn_proxy_props; GVariantBuilder vpn_ip4_props; GVariantBuilder vpn_ip6_props; DispatchInfo *info = NULL; gboolean success = FALSE; GError *error = NULL; static guint request_counter = 0; guint reqid = ++request_counter; if (!dispatcher_proxy) return FALSE; /* Wrapping protection */ if (G_UNLIKELY (!reqid)) reqid = ++request_counter; g_assert (!blocking || (!callback && !user_data)); _ensure_requests (); /* All actions except 'hostname' and 'connectivity-change' require a device */ if ( action == DISPATCHER_ACTION_HOSTNAME || action == DISPATCHER_ACTION_CONNECTIVITY_CHANGE) { _LOGD ("(%u) dispatching action '%s'%s", reqid, action_to_string (action), blocking ? " (blocking)" : (callback ? " (with callback)" : "")); } else { g_return_val_if_fail (NM_IS_DEVICE (device), FALSE); _LOGD ("(%u) (%s) dispatching action '%s'%s", reqid, vpn_iface ? vpn_iface : nm_device_get_iface (device), action_to_string (action), blocking ? " (blocking)" : (callback ? " (with callback)" : "")); } if (!_get_monitor_by_action(action)->has_scripts) { if (blocking == FALSE && (out_call_id || callback)) { info = g_malloc0 (sizeof (*info)); info->action = action; info->request_id = reqid; info->callback = callback; info->user_data = user_data; info->idle_id = g_idle_add (dispatcher_idle_cb, info); _LOGD ("(%u) simulate request; no scripts in %s", reqid, _get_monitor_by_action(action)->dir); } else _LOGD ("(%u) ignoring request; no scripts in %s", reqid, _get_monitor_by_action(action)->dir); success = TRUE; goto done; } if (applied_connection) connection_dict = nm_connection_to_dbus (applied_connection, NM_CONNECTION_SERIALIZE_NO_SECRETS); else connection_dict = g_variant_new_array (G_VARIANT_TYPE ("{sa{sv}}"), NULL, 0); g_variant_builder_init (&connection_props, G_VARIANT_TYPE_VARDICT); if (settings_connection) { const char *connection_path; const char *filename; connection_path = nm_connection_get_path (NM_CONNECTION (settings_connection)); if (connection_path) { g_variant_builder_add (&connection_props, "{sv}", NMD_CONNECTION_PROPS_PATH, g_variant_new_object_path (connection_path)); } filename = nm_settings_connection_get_filename (settings_connection); if (filename) { g_variant_builder_add (&connection_props, "{sv}", NMD_CONNECTION_PROPS_FILENAME, g_variant_new_string (filename)); } if (nm_settings_connection_get_nm_generated_assumed (settings_connection)) { g_variant_builder_add (&connection_props, "{sv}", NMD_CONNECTION_PROPS_EXTERNAL, g_variant_new_boolean (TRUE)); } } g_variant_builder_init (&device_props, G_VARIANT_TYPE_VARDICT); g_variant_builder_init (&device_proxy_props, G_VARIANT_TYPE_VARDICT); g_variant_builder_init (&device_ip4_props, G_VARIANT_TYPE_VARDICT); g_variant_builder_init (&device_ip6_props, G_VARIANT_TYPE_VARDICT); g_variant_builder_init (&vpn_proxy_props, G_VARIANT_TYPE_VARDICT); g_variant_builder_init (&vpn_ip4_props, G_VARIANT_TYPE_VARDICT); g_variant_builder_init (&vpn_ip6_props, G_VARIANT_TYPE_VARDICT); /* hostname and connectivity-change actions don't send device data */ if ( action != DISPATCHER_ACTION_HOSTNAME && action != DISPATCHER_ACTION_CONNECTIVITY_CHANGE) { fill_device_props (device, &device_props, &device_proxy_props, &device_ip4_props, &device_ip6_props, &device_dhcp4_props, &device_dhcp6_props); if (vpn_ip4_config || vpn_ip6_config) { fill_vpn_props (vpn_proxy_config, vpn_ip4_config, vpn_ip6_config, &vpn_proxy_props, &vpn_ip4_props, &vpn_ip6_props); } } if (!device_dhcp4_props) device_dhcp4_props = g_variant_ref_sink (g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0)); if (!device_dhcp6_props) device_dhcp6_props = g_variant_ref_sink (g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0)); /* Send the action to the dispatcher */ if (blocking) { GVariant *ret; GVariantIter *results; ret = _nm_dbus_proxy_call_sync (dispatcher_proxy, "Action", g_variant_new ("(s@a{sa{sv}}a{sv}a{sv}a{sv}a{sv}a{sv}@a{sv}@a{sv}ssa{sv}a{sv}a{sv}b)", action_to_string (action), connection_dict, &connection_props, &device_props, &device_proxy_props, &device_ip4_props, &device_ip6_props, device_dhcp4_props, device_dhcp6_props, nm_connectivity_state_to_string (connectivity_state), vpn_iface ? vpn_iface : "", &vpn_proxy_props, &vpn_ip4_props, &vpn_ip6_props, nm_logging_enabled (LOGL_DEBUG, LOGD_DISPATCH)), G_VARIANT_TYPE ("(a(sus))"), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT, NULL, &error); if (ret) { g_variant_get (ret, "(a(sus))", &results); dispatcher_results_process (reqid, action, results); g_variant_iter_free (results); g_variant_unref (ret); success = TRUE; } else { g_dbus_error_strip_remote_error (error); _LOGW ("(%u) failed: %s", reqid, error->message); g_clear_error (&error); success = FALSE; } } else { info = g_malloc0 (sizeof (*info)); info->action = action; info->request_id = reqid; info->callback = callback; info->user_data = user_data; g_dbus_proxy_call (dispatcher_proxy, "Action", g_variant_new ("(s@a{sa{sv}}a{sv}a{sv}a{sv}a{sv}a{sv}@a{sv}@a{sv}ssa{sv}a{sv}a{sv}b)", action_to_string (action), connection_dict, &connection_props, &device_props, &device_proxy_props, &device_ip4_props, &device_ip6_props, device_dhcp4_props, device_dhcp6_props, nm_connectivity_state_to_string (connectivity_state), vpn_iface ? vpn_iface : "", &vpn_proxy_props, &vpn_ip4_props, &vpn_ip6_props, nm_logging_enabled (LOGL_DEBUG, LOGD_DISPATCH)), G_DBUS_CALL_FLAGS_NONE, CALL_TIMEOUT, NULL, dispatcher_done_cb, info); success = TRUE; } g_variant_unref (device_dhcp4_props); g_variant_unref (device_dhcp6_props); done: if (success && info) { /* Track the request in case of cancelation */ g_hash_table_insert (requests, GUINT_TO_POINTER (info->request_id), info); if (out_call_id) *out_call_id = info->request_id; } else if (out_call_id) *out_call_id = 0; return success; }
static void read_connections (NMSystemConfigInterface *config) { SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (config); SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self); GDir *dir; GError *error = NULL; const char *item; GHashTable *alive_connections; GHashTableIter iter; NMKeyfileConnection *connection; GPtrArray *dead_connections = NULL; guint i; GPtrArray *filenames; GHashTable *paths; dir = g_dir_open (KEYFILE_DIR, 0, &error); if (!dir) { nm_log_warn (LOGD_SETTINGS, "keyfile: cannot read directory '%s': (%d) %s", KEYFILE_DIR, error ? error->code : -1, error && error->message ? error->message : "(unknown)"); g_clear_error (&error); return; } alive_connections = g_hash_table_new (NULL, NULL); filenames = g_ptr_array_new_with_free_func (g_free); while ((item = g_dir_read_name (dir))) { if (nm_keyfile_plugin_utils_should_ignore_file (item)) continue; g_ptr_array_add (filenames, g_build_filename (KEYFILE_DIR, item, NULL)); } g_dir_close (dir); /* While reloading, we don't replace connections that we already loaded while * iterating over the files. * * To have sensible, reproducible behavior, sort the paths by last modification * time prefering older files. */ paths = _paths_from_connections (priv->connections); g_ptr_array_sort_with_data (filenames, (GCompareDataFunc) _sort_paths, paths); g_hash_table_destroy (paths); for (i = 0; i < filenames->len; i++) { connection = update_connection (self, NULL, filenames->pdata[i], NULL, FALSE, alive_connections, NULL); if (connection) g_hash_table_add (alive_connections, connection); } g_ptr_array_free (filenames, TRUE); g_hash_table_iter_init (&iter, priv->connections); while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &connection)) { if ( !g_hash_table_contains (alive_connections, connection) && nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection))) { if (!dead_connections) dead_connections = g_ptr_array_new (); g_ptr_array_add (dead_connections, connection); } } g_hash_table_destroy (alive_connections); if (dead_connections) { for (i = 0; i < dead_connections->len; i++) remove_connection (self, dead_connections->pdata[i]); g_ptr_array_free (dead_connections, TRUE); } }
/* update_connection: * @self: the plugin instance * @source: if %NULL, this re-reads the connection from @full_path * and updates it. When passing @source, this adds a connection from * memory. * @full_path: the filename of the keyfile to be loaded * @connection: an existing connection that might be updated. * If given, @connection must be an existing connection that is currently * owned by the plugin. * @protect_existing_connection: if %TRUE, and !@connection, we don't allow updating * an existing connection with the same UUID. * If %TRUE and @connection, allow updating only if the reload would modify * @connection (without changing its UUID) or if we would create a new connection. * In other words, if this paramter is %TRUE, we only allow creating a * new connection (with an unseen UUID) or updating the passed in @connection * (whereas the UUID cannot change). * Note, that this allows for @connection to be replaced by a new connection. * @protected_connections: (allow-none): if given, we only update an * existing connection if it is not contained in this hash. * @error: error in case of failure * * Loads a connection from file @full_path. This can both be used to * load a connection initially or to update an existing connection. * * If you pass in an existing connection and the reloaded file happens * to have a different UUID, the connection is deleted. * Beware, that means that after the function, you have a dangling pointer * if the returned connection is different from @connection. * * Returns: the updated connection. * */ static NMKeyfileConnection * update_connection (SCPluginKeyfile *self, NMConnection *source, const char *full_path, NMKeyfileConnection *connection, gboolean protect_existing_connection, GHashTable *protected_connections, GError **error) { SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self); NMKeyfileConnection *connection_new; NMKeyfileConnection *connection_by_uuid; GError *local = NULL; const char *uuid; g_return_val_if_fail (!source || NM_IS_CONNECTION (source), NULL); g_return_val_if_fail (full_path || source, NULL); if (full_path) nm_log_dbg (LOGD_SETTINGS, "keyfile: loading from file \"%s\"...", full_path); connection_new = nm_keyfile_connection_new (source, full_path, &local); if (!connection_new) { /* Error; remove the connection */ if (source) nm_log_warn (LOGD_SETTINGS, "keyfile: error creating connection %s: %s", nm_connection_get_uuid (source), local->message); else nm_log_warn (LOGD_SETTINGS, "keyfile: error loading connection from file %s: %s", full_path, local->message); if ( connection && !protect_existing_connection && (!protected_connections || !g_hash_table_contains (protected_connections, connection))) remove_connection (self, connection); g_propagate_error (error, local); return NULL; } uuid = nm_connection_get_uuid (NM_CONNECTION (connection_new)); connection_by_uuid = g_hash_table_lookup (priv->connections, uuid); if ( connection && connection != connection_by_uuid) { if ( (protect_existing_connection && connection_by_uuid != NULL) || (protected_connections && g_hash_table_contains (protected_connections, connection))) { NMKeyfileConnection *conflicting = (protect_existing_connection && connection_by_uuid != NULL) ? connection_by_uuid : connection; if (source) nm_log_warn (LOGD_SETTINGS, "keyfile: cannot update protected "NM_KEYFILE_CONNECTION_LOG_FMT" connection due to conflicting UUID %s", NM_KEYFILE_CONNECTION_LOG_ARG (conflicting), uuid); else nm_log_warn (LOGD_SETTINGS, "keyfile: cannot load %s due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, full_path, NM_KEYFILE_CONNECTION_LOG_ARG (conflicting)); g_object_unref (connection_new); g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Cannot update protected connection due to conflicting UUID"); return NULL; } /* The new connection has a different UUID then the original one. * Remove @connection. */ remove_connection (self, connection); } if ( connection_by_uuid && ( (!connection && protect_existing_connection) || (protected_connections && g_hash_table_contains (protected_connections, connection_by_uuid)))) { if (source) nm_log_warn (LOGD_SETTINGS, "keyfile: cannot update connection due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_by_uuid)); else nm_log_warn (LOGD_SETTINGS, "keyfile: cannot load %s due to conflicting UUID for "NM_KEYFILE_CONNECTION_LOG_FMT, full_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_by_uuid)); g_object_unref (connection_new); g_set_error_literal (error, NM_SETTINGS_ERROR, NM_SETTINGS_ERROR_FAILED, "Skip updating protected connection during reload"); return NULL; } if (connection_by_uuid) { const char *old_path; old_path = nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_by_uuid)); if (nm_connection_compare (NM_CONNECTION (connection_by_uuid), NM_CONNECTION (connection_new), NM_SETTING_COMPARE_FLAG_IGNORE_AGENT_OWNED_SECRETS | NM_SETTING_COMPARE_FLAG_IGNORE_NOT_SAVED_SECRETS)) { /* Nothing to do... except updating the path. */ if (old_path && g_strcmp0 (old_path, full_path) != 0) nm_log_info (LOGD_SETTINGS, "keyfile: rename \"%s\" to "NM_KEYFILE_CONNECTION_LOG_FMT" without other changes", old_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); } else { /* An existing connection changed. */ if (source) nm_log_info (LOGD_SETTINGS, "keyfile: update "NM_KEYFILE_CONNECTION_LOG_FMT" from %s", NM_KEYFILE_CONNECTION_LOG_ARG (connection_new), NM_KEYFILE_CONNECTION_LOG_PATH (old_path)); else if (!g_strcmp0 (old_path, nm_settings_connection_get_filename (NM_SETTINGS_CONNECTION (connection_new)))) nm_log_info (LOGD_SETTINGS, "keyfile: update "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); else if (old_path) nm_log_info (LOGD_SETTINGS, "keyfile: rename \"%s\" to "NM_KEYFILE_CONNECTION_LOG_FMT, old_path, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); else nm_log_info (LOGD_SETTINGS, "keyfile: update and persist "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); if (!nm_settings_connection_replace_settings (NM_SETTINGS_CONNECTION (connection_by_uuid), NM_CONNECTION (connection_new), FALSE, /* don't set Unsaved */ "keyfile-update", &local)) { /* Shouldn't ever get here as 'connection_new' was verified by the reader already * and the UUID did not change. */ g_assert_not_reached (); } g_assert_no_error (local); } nm_settings_connection_set_filename (NM_SETTINGS_CONNECTION (connection_by_uuid), full_path); g_object_unref (connection_new); return connection_by_uuid; } else { if (source) nm_log_info (LOGD_SETTINGS, "keyfile: add connection "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); else nm_log_info (LOGD_SETTINGS, "keyfile: new connection "NM_KEYFILE_CONNECTION_LOG_FMT, NM_KEYFILE_CONNECTION_LOG_ARG (connection_new)); g_hash_table_insert (priv->connections, g_strdup (uuid), connection_new); g_signal_connect (connection_new, NM_SETTINGS_CONNECTION_REMOVED, G_CALLBACK (connection_removed_cb), self); if (!source) { /* Only raise the signal if we were called without source, i.e. if we read the connection from file. * Otherwise, we were called by add_connection() which does not expect the signal. */ g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_CONNECTION_ADDED, connection_new); } return connection_new; } }