26
26
#define KEYFILE_PLUGIN_NAME "keyfile"
27
27
#define KEYFILE_PLUGIN_INFO "(c) 2007 - 2008 Red Hat, Inc. To report bugs please use the NetworkManager mailing list."
29
#define CONF_FILE SYSCONFDIR "/NetworkManager/nm-system-settings.conf"
31
static char *plugin_get_hostname (SCPluginKeyfile *plugin);
29
32
static void system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
31
34
G_DEFINE_TYPE_EXTENDED (SCPluginKeyfile, sc_plugin_keyfile, G_TYPE_OBJECT, 0,
84
92
find_by_uuid (gpointer key, gpointer data, gpointer user_data)
86
NMKeyfileConnection *keyfile = NM_KEYFILE_CONNECTION (key);
94
NMKeyfileConnection *keyfile = NM_KEYFILE_CONNECTION (data);
87
95
FindByUUIDInfo *info = user_data;
88
96
NMConnection *connection;
89
97
NMSettingConnection *s_con;
103
111
update_connection_settings (NMExportedConnection *orig,
104
NMExportedConnection *new,
112
NMExportedConnection *new)
107
GHashTable *settings;
110
settings = nm_connection_to_hash (nm_exported_connection_get_connection (new));
111
success = nm_exported_connection_update (orig, settings, error);
112
g_hash_table_destroy (settings);
114
NMConnection *wrapped;
115
GHashTable *new_settings;
117
new_settings = nm_connection_to_hash (nm_exported_connection_get_connection (new));
118
wrapped = nm_exported_connection_get_connection (orig);
119
nm_connection_replace_settings (wrapped, new_settings);
120
nm_exported_connection_signal_updated (orig, new_settings);
121
g_hash_table_destroy (new_settings);
150
157
tmp = (NMExportedConnection *) nm_keyfile_connection_new (name);
152
GError *error = NULL;
154
if (!update_connection_settings (NM_EXPORTED_CONNECTION (connection), tmp, &error)) {
155
g_warning ("%s: couldn't update connection settings: (%d) %s",
156
__func__, error ? error->code : 0,
157
error ? error->message : "unknown");
159
g_error_free (error);
159
update_connection_settings (NM_EXPORTED_CONNECTION (connection), tmp);
161
160
g_object_unref (tmp);
196
194
/* Updating settings should update the NMKeyfileConnection's
197
195
* filename property too.
199
if (!update_connection_settings (NM_EXPORTED_CONNECTION (found),
200
NM_EXPORTED_CONNECTION (connection),
202
g_warning ("%s: couldn't update connection settings: (%d) %s",
203
__func__, error ? error->code : 0,
204
error ? error->message : "unknown");
205
g_error_free (error);
197
update_connection_settings (NM_EXPORTED_CONNECTION (found),
198
NM_EXPORTED_CONNECTION (connection));
208
200
/* Re-insert the connection back into the hash with the new filename */
209
201
g_hash_table_insert (priv->hash,
224
conf_file_changed (GFileMonitor *monitor,
227
GFileMonitorEvent event_type,
230
SCPluginKeyfile *self = SC_PLUGIN_KEYFILE (data);
231
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (self);
234
switch (event_type) {
235
case G_FILE_MONITOR_EVENT_DELETED:
236
case G_FILE_MONITOR_EVENT_CREATED:
237
case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
238
g_signal_emit_by_name (self, "unmanaged-devices-changed");
241
tmp = plugin_get_hostname (self);
242
if ((tmp && !priv->hostname)
243
|| (!tmp && priv->hostname)
244
|| (priv->hostname && tmp && strcmp (priv->hostname, tmp))) {
246
g_free (priv->hostname);
247
priv->hostname = tmp;
249
g_object_notify (G_OBJECT (self), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
232
261
setup_monitoring (NMSystemConfigInterface *config)
234
263
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (config);
245
274
priv->monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (dir_changed), config);
246
275
priv->monitor = monitor;
278
file = g_file_new_for_path (CONF_FILE);
279
monitor = g_file_monitor_file (file, G_FILE_MONITOR_NONE, NULL, NULL);
280
g_object_unref (file);
283
priv->conf_file_monitor_id = g_signal_connect (monitor, "changed", G_CALLBACK (conf_file_changed), config);
284
priv->conf_file_monitor = monitor;
281
319
return write_connection (connection, NULL, error);
323
get_unmanaged_devices (NMSystemConfigInterface *config)
326
GSList *unmanaged_devices = NULL;
327
GError *error = NULL;
329
key_file = g_key_file_new ();
330
if (g_key_file_load_from_file (key_file, CONF_FILE, G_KEY_FILE_NONE, &error)) {
333
str = g_key_file_get_value (key_file, "keyfile", "unmanaged-devices", NULL);
338
udis = g_strsplit (str, ";", -1);
341
for (i = 0; udis[i] != NULL; i++)
342
unmanaged_devices = g_slist_append (unmanaged_devices, udis[i]);
344
g_free (udis); /* Yes, g_free, not g_strfreev because we need the strings in the list */
347
g_warning ("Error parsing file '%s': %s", CONF_FILE, error->message);
348
g_error_free (error);
351
g_key_file_free (key_file);
353
return unmanaged_devices;
357
plugin_get_hostname (SCPluginKeyfile *plugin)
360
char *hostname = NULL;
361
GError *error = NULL;
363
key_file = g_key_file_new ();
364
if (g_key_file_load_from_file (key_file, CONF_FILE, G_KEY_FILE_NONE, &error))
365
hostname = g_key_file_get_value (key_file, "keyfile", "hostname", NULL);
367
g_warning ("Error parsing file '%s': %s", CONF_FILE, error->message);
368
g_error_free (error);
371
g_key_file_free (key_file);
377
plugin_set_hostname (SCPluginKeyfile *plugin, const char *hostname)
379
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (plugin);
381
GError *error = NULL;
382
gboolean result = FALSE;
384
key_file = g_key_file_new ();
385
if (g_key_file_load_from_file (key_file, CONF_FILE, G_KEY_FILE_NONE, &error)) {
389
g_key_file_set_string (key_file, "keyfile", "hostname", hostname);
391
data = g_key_file_to_data (key_file, &len, &error);
393
g_file_set_contents (CONF_FILE, data, len, &error);
396
g_free (priv->hostname);
397
priv->hostname = hostname ? g_strdup (hostname) : NULL;
402
g_warning ("Error saving hostname: %s", error->message);
403
g_error_free (error);
406
g_warning ("Error parsing file '%s': %s", CONF_FILE, error->message);
407
g_error_free (error);
410
g_key_file_free (key_file);
287
418
sc_plugin_keyfile_init (SCPluginKeyfile *plugin)
420
SCPluginKeyfilePrivate *priv = SC_PLUGIN_KEYFILE_GET_PRIVATE (plugin);
422
priv->hostname = plugin_get_hostname (plugin);
299
433
case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
300
434
g_value_set_string (value, KEYFILE_PLUGIN_INFO);
436
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
437
g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_CONNECTIONS |
438
NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
440
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
441
g_value_set_string (value, SC_PLUGIN_KEYFILE_GET_PRIVATE (object)->hostname);
444
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
450
set_property (GObject *object, guint prop_id,
451
const GValue *value, GParamSpec *pspec)
453
const char *hostname;
456
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
457
hostname = g_value_get_string (value);
458
if (hostname && strlen (hostname) < 1)
460
plugin_set_hostname (SC_PLUGIN_KEYFILE (object), hostname);
303
463
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
323
483
g_object_unref (priv->monitor);
486
if (priv->conf_file_monitor) {
487
if (priv->conf_file_monitor_id)
488
g_signal_handler_disconnect (priv->conf_file_monitor, priv->conf_file_monitor_id);
490
g_file_monitor_cancel (priv->conf_file_monitor);
491
g_object_unref (priv->conf_file_monitor);
494
g_free (priv->hostname);
327
497
g_hash_table_destroy (priv->hash);
339
509
object_class->dispose = dispose;
340
510
object_class->get_property = get_property;
342
g_object_class_override_property (object_class,
343
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
344
NM_SYSTEM_CONFIG_INTERFACE_NAME);
346
g_object_class_override_property (object_class,
347
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
348
NM_SYSTEM_CONFIG_INTERFACE_INFO);
511
object_class->set_property = set_property;
513
g_object_class_override_property (object_class,
514
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
515
NM_SYSTEM_CONFIG_INTERFACE_NAME);
517
g_object_class_override_property (object_class,
518
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
519
NM_SYSTEM_CONFIG_INTERFACE_INFO);
521
g_object_class_override_property (object_class,
522
NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES,
523
NM_SYSTEM_CONFIG_INTERFACE_CAPABILITIES);
525
g_object_class_override_property (object_class,
526
NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME,
527
NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);