summaryrefslogtreecommitdiff
path: root/src/settings/plugins/keyfile/plugin.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/settings/plugins/keyfile/plugin.c')
-rw-r--r--src/settings/plugins/keyfile/plugin.c241
1 files changed, 36 insertions, 205 deletions
diff --git a/src/settings/plugins/keyfile/plugin.c b/src/settings/plugins/keyfile/plugin.c
index 611192baa5..ee7da79630 100644
--- a/src/settings/plugins/keyfile/plugin.c
+++ b/src/settings/plugins/keyfile/plugin.c
@@ -47,7 +47,6 @@
#include "utils.h"
#include "gsystem-local-alloc.h"
-static char *plugin_get_hostname (SCPluginKeyfile *plugin);
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
G_DEFINE_TYPE_EXTENDED (SCPluginKeyfile, sc_plugin_keyfile, G_TYPE_OBJECT, 0,
@@ -63,13 +62,7 @@ typedef struct {
GFileMonitor *monitor;
guint monitor_id;
- const char *conf_file;
- GFileMonitor *conf_file_monitor;
- guint conf_file_monitor_id;
-
- char *hostname;
-
- gboolean disposed;
+ NMConfig *config;
} SCPluginKeyfilePrivate;
static void
@@ -322,40 +315,19 @@ dir_changed (GFileMonitor *monitor,
}
static void
-conf_file_changed (GFileMonitor *monitor,
- GFile *file,
- GFile *other_file,
- GFileMonitorEvent event_type,
- gpointer data)
+config_changed_cb (NMConfig *config,
+ NMConfigData *config_data,
+ NMConfigChangeFlags changes,
+ NMConfigData *old_data,
+ SCPluginKeyfile *self)
{
- SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (data);
- SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
- char *tmp;
+ gs_free char *old_value = NULL, *new_value = NULL;
- switch (event_type) {
- case G_FILE_MONITOR_EVENT_DELETED:
- case G_FILE_MONITOR_EVENT_CREATED:
- case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
- g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
-
- /* hostname */
- tmp = plugin_get_hostname (self);
- if ((tmp && !priv->hostname)
- || (!tmp && priv->hostname)
- || (priv->hostname && tmp && strcmp (priv->hostname, tmp))) {
-
- g_free (priv->hostname);
- priv->hostname = tmp;
- tmp = NULL;
- g_object_notify (G_OBJECT (self), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
- }
+ old_value = nm_config_data_get_value (old_data, "keyfile", "unmanaged-devices", NULL);
+ new_value = nm_config_data_get_value (config_data, "keyfile", "unmanaged-devices", NULL);
- g_free (tmp);
-
- break;
- default:
- break;
- }
+ if (g_strcmp0 (old_value, new_value) != 0)
+ g_signal_emit_by_name (self, NM_SYSTEM_CONFIG_INTERFACE_UNMANAGED_SPECS_CHANGED);
}
static void
@@ -376,16 +348,10 @@ setup_monitoring (NMSystemConfigInterface *config)
}
}
- if (priv->conf_file) {
- file = g_file_new_for_path (priv->conf_file);
- monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
- g_object_unref (file);
-
- if (monitor) {
- priv->conf_file_monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (conf_file_changed), config);
- priv->conf_file_monitor = monitor;
- }
- }
+ g_signal_connect (G_OBJECT (priv->config),
+ NM_CONFIG_SIGNAL_CONFIG_CHANGED,
+ G_CALLBACK (config_changed_cb),
+ config);
}
static GHashTable *
@@ -554,128 +520,14 @@ add_connection (NMSystemConfigInterface *config,
return NM_SETTINGS_CONNECTION (update_connection (self, connection, path, NULL, FALSE, NULL, error));
}
-static gboolean
-parse_key_file_allow_none (SCPluginKeyfilePrivate *priv,
- GKeyFile *key_file,
- GError **error)
-{
- gboolean ret = FALSE;
- GError *local_error = NULL;
-
- if (!g_key_file_load_from_file (key_file, priv->conf_file, G_KEY_FILE_NONE, &local_error)) {
- if (g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
- g_clear_error (&local_error);
- else {
- g_propagate_prefixed_error (error, local_error,
- "Error parsing file '%s': ",
- priv->conf_file);
- goto out;
- }
- }
- ret = TRUE;
-
- out:
- return ret;
-}
-
static GSList *
get_unmanaged_specs (NMSystemConfigInterface *config)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
- GKeyFile *key_file;
- GSList *specs = NULL;
- GError *error = NULL;
-
- if (!priv->conf_file)
- return NULL;
-
- key_file = nm_config_create_keyfile ();
- if (parse_key_file_allow_none (priv, key_file, &error))
- specs = nm_config_get_device_match_spec (key_file, "keyfile", "unmanaged-devices");
+ gs_free char *value = NULL;
- if (error) {
- nm_log_warn (LOGD_SETTINGS, "keyfile: error getting unmanaged specs: %s", error->message);
- g_error_free (error);
- }
- g_key_file_free (key_file);
-
- return specs;
-}
-
-static char *
-plugin_get_hostname (SCPluginKeyfile *plugin)
-{
- SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (plugin);
- GKeyFile *key_file;
- char *hostname = NULL;
- GError *error = NULL;
-
- if (!priv->conf_file)
- return NULL;
-
- key_file = g_key_file_new ();
- if (!parse_key_file_allow_none (priv, key_file, &error))
- goto out;
-
- hostname = g_key_file_get_value (key_file, "keyfile", "hostname", NULL);
-
- out:
- if (error) {
- nm_log_warn (LOGD_SETTINGS, "keyfile: error getting hostname: %s", error->message);
- g_error_free (error);
- }
- if (key_file)
- g_key_file_free (key_file);
-
- return hostname;
-}
-
-static gboolean
-plugin_set_hostname (SCPluginKeyfile *plugin, const char *hostname)
-{
- gboolean ret = FALSE;
- SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (plugin);
- GKeyFile *key_file = NULL;
- GError *error = NULL;
- char *data = NULL;
- gsize len;
-
- if (!priv->conf_file) {
- g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
- "Error saving hostname: no config file");
- goto out;
- }
-
- g_free (priv->hostname);
- priv->hostname = g_strdup (hostname);
-
- key_file = g_key_file_new ();
- if (!parse_key_file_allow_none (priv, key_file, &error))
- goto out;
-
- g_key_file_set_string (key_file, "keyfile", "hostname", hostname);
-
- data = g_key_file_to_data (key_file, &len, &error);
- if (!data)
- goto out;
-
- if (!g_file_set_contents (priv->conf_file, data, len, &error)) {
- g_prefix_error (&error, "Error saving hostname: ");
- goto out;
- }
-
- ret = TRUE;
-
- out:
- if (error) {
- nm_log_warn (LOGD_SETTINGS, "keyfile: error setting hostname: %s", error->message);
- g_error_free (error);
- }
- g_free (data);
- if (key_file)
- g_key_file_free (key_file);
-
- return ret;
+ value = nm_config_data_get_value (nm_config_get_data (priv->config), "keyfile", "unmanaged-devices", NULL);
+ return nm_match_spec_split (value);
}
/* GObject */
@@ -700,11 +552,7 @@ get_property (GObject *object, guint prop_id,
g_value_set_string (value, KEYFILE_PLUGIN_INFO);
break;
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
- g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS |
- NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
- break;
- case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
- g_value_set_string (value, SC_PLUGIN_KEYFILE_GET_PRIVATE (object)->hostname);
+ g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -716,15 +564,7 @@ static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
- const char *hostname;
-
switch (prop_id) {
- case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
- hostname = g_value_get_string (value);
- if (hostname && strlen (hostname) < 1)
- hostname = NULL;
- plugin_set_hostname (SC_PLUGIN_KEYFILE (object), hostname);
- break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -736,35 +576,26 @@ dispose (GObject *object)
{
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (object);
- if (priv->disposed)
- goto out;
-
- priv->disposed = TRUE;
-
if (priv->monitor) {
- if (priv->monitor_id)
+ if (priv->monitor_id) {
g_signal_handler_disconnect (priv->monitor, priv->monitor_id);
+ priv->monitor_id = 0;
+ }
g_file_monitor_cancel (priv->monitor);
- g_object_unref (priv->monitor);
+ g_clear_object (&priv->monitor);
}
- if (priv->conf_file_monitor) {
- if (priv->conf_file_monitor_id)
- g_signal_handler_disconnect (priv->conf_file_monitor, priv->conf_file_monitor_id);
-
- g_file_monitor_cancel (priv->conf_file_monitor);
- g_object_unref (priv->conf_file_monitor);
- }
-
- g_free (priv->hostname);
-
if (priv->connections) {
g_hash_table_destroy (priv->connections);
priv->connections = NULL;
}
-out:
+ if (priv->config) {
+ g_signal_handlers_disconnect_by_func (priv->config, config_changed_cb, object);
+ g_clear_object (&priv->config);
+ }
+
G_OBJECT_CLASS (sc_plugin_keyfile_parent_class)->dispose (object);
}
@@ -790,10 +621,6 @@ sc_plugin_keyfile_class_init (SCPluginKeyfileClass *req_class)
g_object_class_override_property (object_class,
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
-
- g_object_class_override_property (object_class,
- NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
- NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
}
static void
@@ -812,15 +639,19 @@ nm_settings_keyfile_plugin_new (void)
{
static SCPluginKeyfile *singleton = NULL;
SCPluginKeyfilePrivate *priv;
+ char *value;
if (!singleton) {
singleton = SC_PLUGIN_KEYFILE (g_object_new (SC_TYPE_PLUGIN_KEYFILE, NULL));
priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (singleton);
- priv->conf_file = nm_config_data_get_config_main_file (nm_config_get_data (nm_config_get ()));
-
- /* plugin_set_hostname() has to be called *after* priv->conf_file is set */
- priv->hostname = plugin_get_hostname (singleton);
+ priv->config = g_object_ref (nm_config_get ());
+ value = nm_config_data_get_value (nm_config_get_data (priv->config),
+ "keyfile", "hostname", NULL);
+ if (value) {
+ nm_log_warn (LOGD_SETTINGS, "keyfile: 'hostname' option is deprecated and has no effect");
+ g_free (value);
+ }
} else
g_object_ref (singleton);