1
=== modified file 'ChangeLog'
2
--- a/ChangeLog 2008-10-03 21:51:57 +0000
3
+++ b/ChangeLog 2008-10-06 08:22:23 +0000
5
+2008-10-03 Alexander Sack <asac@ubuntu.com>
7
+ Implement system hostname support for debian/ubuntu
9
+ * system-settings/plugins/ifupdown/plugin.c
10
+ - (GObject__set_property, GObject__get_property, SCPluginIfupdown_init, update_system_hostname, get_hostname):
11
+ implement hostname property that watches and
12
+ parses /etc/hostname
15
+ Make nm-inotify-helper from ifcfg-fedora plugin usable
16
+ for other plugins too
18
+ * system-settings/plugins/ifcfg-fedora/nm-inotify-helper.c (deleted)
19
+ system-settings/plugins/ifcfg-fedora/nm-inotify-helper.h (deleted)
20
+ system-settings/src/nm-inotify-helper.c (NEW)
21
+ system-settings/src/nm-inotify-helper.h (NEW)
22
+ system-settings/plugins/ifcfg-fedora/Makefile.am
23
+ system-settings/src/Makefile.am
24
+ - make inotify available for all system plugins and
25
+ adjust ifcfg-fedora build accordingly
27
+ * system-settings/plugins/ifupdown/plugin.c
28
+ - (GObject__get_property): extend announced capabilities; add
29
+ NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME support
30
+ - (GObject__set_property,write_system_hostname): implement
31
+ NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME capability.
33
2008-10-02 Alexander Sack <asac@ubuntu.com>
35
Implement support for wep-tx-keyidx in ifupdown system
38
* system-settings/plugins/ifupdown/parser.c
39
- (update_wireless_security_setting_from_if_block): introduce
40
free_type_mapping func table; rename a few local
42
=== modified file 'system-settings/plugins/ifcfg-fedora/Makefile.am'
43
--- a/system-settings/plugins/ifcfg-fedora/Makefile.am 2008-05-13 16:53:50 +0000
44
+++ b/system-settings/plugins/ifcfg-fedora/Makefile.am 2008-10-02 22:32:18 +0000
45
@@ -5,19 +5,17 @@ libnm_settings_plugin_ifcfg_fedora_la_SO
50
nm-ifcfg-connection.c \
51
nm-ifcfg-connection.h \
55
- nm-inotify-helper.c \
59
libnm_settings_plugin_ifcfg_fedora_la_CPPFLAGS = \
63
-DG_DISABLE_DEPRECATED \
64
-I${top_srcdir}/system-settings/src \
65
-I$(top_srcdir)/include \
67
=== removed file 'system-settings/plugins/ifcfg-fedora/nm-inotify-helper.c'
68
--- a/system-settings/plugins/ifcfg-fedora/nm-inotify-helper.c 2008-08-26 09:34:31 +0000
69
+++ b/system-settings/plugins/ifcfg-fedora/nm-inotify-helper.c 1970-01-01 00:00:00 +0000
71
-/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
72
-/* NetworkManager system settings service
74
- * This program is free software; you can redistribute it and/or modify
75
- * it under the terms of the GNU General Public License as published by
76
- * the Free Software Foundation; either version 2 of the License, or
77
- * (at your option) any later version.
79
- * This program is distributed in the hope that it will be useful,
80
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
81
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
82
- * GNU General Public License for more details.
84
- * You should have received a copy of the GNU General Public License along
85
- * with this program; if not, write to the Free Software Foundation, Inc.,
86
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
88
- * (C) Copyright 2008 Red Hat, Inc.
93
-#include <sys/inotify.h>
96
-#include "nm-marshal.h"
97
-#include "nm-inotify-helper.h"
99
-G_DEFINE_TYPE (NMInotifyHelper, nm_inotify_helper, G_TYPE_OBJECT)
101
-#define NM_INOTIFY_HELPER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_INOTIFY_HELPER, NMInotifyHelperPrivate))
106
- GHashTable *wd_refs;
107
-} NMInotifyHelperPrivate;
115
-static guint signals[LAST_SIGNAL] = { 0 };
118
-nm_inotify_helper_add_watch (NMInotifyHelper *self, const char *path)
120
- NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
124
- g_return_val_if_fail (priv->ifd >= 0, -1);
126
- /* We only care about modifications since we're just trying to get change
127
- * notifications on hardlinks.
130
- wd = inotify_add_watch (priv->ifd, path, IN_CLOSE_WRITE);
134
- refcount = GPOINTER_TO_UINT (g_hash_table_lookup (priv->wd_refs, GINT_TO_POINTER (wd)));
136
- g_hash_table_replace (priv->wd_refs, GINT_TO_POINTER (wd), GUINT_TO_POINTER (refcount));
142
-nm_inotify_helper_remove_watch (NMInotifyHelper *self, int wd)
144
- NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
147
- g_return_if_fail (priv->ifd >= 0);
149
- refcount = GPOINTER_TO_UINT (g_hash_table_lookup (priv->wd_refs, GINT_TO_POINTER (wd)));
155
- g_hash_table_remove (priv->wd_refs, GINT_TO_POINTER (wd));
156
- inotify_rm_watch (priv->ifd, wd);
158
- g_hash_table_replace (priv->wd_refs, GINT_TO_POINTER (wd), GUINT_TO_POINTER (refcount));
162
-inotify_event_handler (GIOChannel *channel, GIOCondition cond, gpointer user_data)
164
- NMInotifyHelper *self = NM_INOTIFY_HELPER (user_data);
165
- struct inotify_event evt;
167
- /* read the notifications from the watch descriptor */
168
- while (g_io_channel_read_chars (channel, (gchar *) &evt, sizeof (struct inotify_event), NULL, NULL) == G_IO_STATUS_NORMAL) {
169
- gchar filename[PATH_MAX + 1];
171
- filename[0] = '\0';
173
- g_io_channel_read_chars (channel,
175
- evt.len > PATH_MAX ? PATH_MAX : evt.len,
179
- if (!(evt.mask & IN_IGNORED))
180
- g_signal_emit (self, signals[EVENT], 0, &evt, &filename[0]);
187
-init_inotify (NMInotifyHelper *self)
189
- NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
190
- GIOChannel *channel;
193
- priv->ifd = inotify_init ();
194
- if (priv->ifd == -1) {
195
- g_warning ("%s: couldn't initialize inotify", __func__);
199
- /* Watch the inotify descriptor for file/directory change events */
200
- channel = g_io_channel_unix_new (priv->ifd);
202
- g_warning ("%s: couldn't create new GIOChannel", __func__);
208
- g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
209
- g_io_channel_set_encoding (channel, NULL, NULL);
211
- source_id = g_io_add_watch (channel,
212
- G_IO_IN | G_IO_ERR,
213
- (GIOFunc) inotify_event_handler,
215
- g_io_channel_unref (channel);
220
-nm_inotify_helper_get (void)
222
- static NMInotifyHelper *singleton = NULL;
225
- singleton = (NMInotifyHelper *) g_object_new (NM_TYPE_INOTIFY_HELPER, NULL);
229
- if (!init_inotify (singleton)) {
230
- g_object_unref (singleton);
234
- g_object_ref (singleton);
236
- g_assert (singleton);
241
-nm_inotify_helper_init (NMInotifyHelper *self)
243
- NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
245
- priv->wd_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
249
-finalize (GObject *object)
251
- NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (object);
253
- if (priv->ifd >= 0)
256
- g_hash_table_destroy (priv->wd_refs);
258
- G_OBJECT_CLASS (nm_inotify_helper_parent_class)->finalize (object);
262
-nm_inotify_helper_class_init (NMInotifyHelperClass *klass)
264
- GObjectClass *object_class = G_OBJECT_CLASS (klass);
266
- g_type_class_add_private (klass, sizeof (NMInotifyHelperPrivate));
268
- /* Virtual methods */
269
- object_class->finalize = finalize;
273
- g_signal_new ("event",
274
- G_OBJECT_CLASS_TYPE (object_class),
276
- G_STRUCT_OFFSET (NMInotifyHelperClass, event),
278
- _nm_marshal_VOID__POINTER_STRING,
279
- G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_STRING);
283
=== removed file 'system-settings/plugins/ifcfg-fedora/nm-inotify-helper.h'
284
--- a/system-settings/plugins/ifcfg-fedora/nm-inotify-helper.h 2008-06-26 18:31:52 +0000
285
+++ b/system-settings/plugins/ifcfg-fedora/nm-inotify-helper.h 1970-01-01 00:00:00 +0000
287
-/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
288
-/* NetworkManager system settings service
290
- * This program is free software; you can redistribute it and/or modify
291
- * it under the terms of the GNU General Public License as published by
292
- * the Free Software Foundation; either version 2 of the License, or
293
- * (at your option) any later version.
295
- * This program is distributed in the hope that it will be useful,
296
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
297
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
298
- * GNU General Public License for more details.
300
- * You should have received a copy of the GNU General Public License along
301
- * with this program; if not, write to the Free Software Foundation, Inc.,
302
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
304
- * (C) Copyright 2008 Red Hat, Inc.
307
-#ifndef __INOTIFY_HELPER_H__
308
-#define __INOTIFY_HELPER_H__
311
-#include <glib-object.h>
312
-#include <sys/inotify.h>
314
-#define NM_TYPE_INOTIFY_HELPER (nm_inotify_helper_get_type ())
315
-#define NM_INOTIFY_HELPER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_INOTIFY_HELPER, NMInotifyHelper))
316
-#define NM_INOTIFY_HELPER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_INOTIFY_HELPER, NMInotifyHelperClass))
317
-#define NM_IS_INOTIFY_HELPER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_INOTIFY_HELPER))
318
-#define NM_IS_INOTIFY_HELPER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_INOTIFY_HELPER))
319
-#define NM_INOTIFY_HELPER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_INOTIFY_HELPER, NMInotifyHelperClass))
326
- GObjectClass parent;
329
- void (* event) (NMInotifyHelper *helper, struct inotify_event *evt, const char *filename);
330
-} NMInotifyHelperClass;
332
-GType nm_inotify_helper_get_type (void);
334
-NMInotifyHelper * nm_inotify_helper_get (void);
336
-int nm_inotify_helper_add_watch (NMInotifyHelper *helper, const char *path);
338
-void nm_inotify_helper_remove_watch (NMInotifyHelper *helper, int wd);
340
-#endif /* __INOTIFY_HELPER_H__ */
342
=== modified file 'system-settings/plugins/ifupdown/plugin.c'
343
--- a/system-settings/plugins/ifupdown/plugin.c 2008-09-18 15:29:59 +0000
344
+++ b/system-settings/plugins/ifupdown/plugin.c 2008-10-04 17:09:53 +0000
346
#include "nm-setting-ip4-config.h"
347
#include "nm-setting-wireless.h"
348
#include "nm-setting-wired.h"
349
#include "nm-setting-ppp.h"
351
#include "nm-ifupdown-connection.h"
354
+#include "nm-inotify-helper.h"
356
#include <nm-utils.h>
359
#include <arpa/inet.h>
361
#define IFUPDOWN_PLUGIN_NAME "ifupdown"
362
#define IFUPDOWN_PLUGIN_INFO "(C) 2008 Canonical Ltd. To report bugs please use the NetworkManager mailing list."
363
+#define IFUPDOWN_SYSTEM_HOSTNAME_FILE "/etc/hostname"
367
DBusGConnection *g_connection;
368
NMSystemConfigHalManager *hal_mgr;
370
GHashTable *iface_connections;
374
+ gulong inotify_event_id;
375
+ int inotify_system_hostname_wd;
376
} SCPluginIfupdownPrivate;
379
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class);
381
G_DEFINE_TYPE_EXTENDED (SCPluginIfupdown, sc_plugin_ifupdown, G_TYPE_OBJECT, 0,
382
G_IMPLEMENT_INTERFACE (NM_TYPE_SYSTEM_CONFIG_INTERFACE,
383
system_config_interface_init))
384
@@ -100,21 +105,36 @@ SCPluginIfupdown_connection_added (NMSys
386
SCPluginIfupdown_unmanaged_devices_changed (NMSystemConfigInterface *config);
389
GObject__get_property (GObject *object, guint prop_id,
390
GValue *value, GParamSpec *pspec);
393
+GObject__set_property (GObject *object, guint prop_id,
394
+ const GValue *value, GParamSpec *pspec);
397
GObject__dispose (GObject *object);
400
GObject__finalize (GObject *object);
404
+get_hostname (NMSystemConfigInterface *config);
408
+update_system_hostname(NMInotifyHelper *inotify_helper,
409
+ struct inotify_event *evt,
411
+ NMSystemConfigInterface *config);
415
system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
417
system_config_interface_class->init = SCPluginIfupdown_init;
418
system_config_interface_class->get_connections = SCPluginIfupdown_get_connections;
419
system_config_interface_class->get_unmanaged_devices = SCPluginIfupdown_get_unmanaged_devices;
420
system_config_interface_class->connection_added = SCPluginIfupdown_connection_added;
421
@@ -126,16 +146,17 @@ sc_plugin_ifupdown_class_init (SCPluginI
423
GObjectClass *object_class = G_OBJECT_CLASS (req_class);
425
g_type_class_add_private (req_class, sizeof (SCPluginIfupdownPrivate));
427
object_class->dispose = GObject__dispose;
428
object_class->finalize = GObject__finalize;
429
object_class->get_property = GObject__get_property;
430
+ object_class->set_property = GObject__set_property;
432
g_object_class_override_property (object_class,
433
NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
434
NM_SYSTEM_CONFIG_INTERFACE_NAME);
436
g_object_class_override_property (object_class,
437
NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO,
438
NM_SYSTEM_CONFIG_INTERFACE_INFO);
439
@@ -151,23 +172,35 @@ sc_plugin_ifupdown_class_init (SCPluginI
442
SCPluginIfupdown_init (NMSystemConfigInterface *config,
443
NMSystemConfigHalManager *hal_manager)
445
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
446
GHashTable *auto_ifaces = g_hash_table_new (g_str_hash, g_str_equal);
447
if_block *block = NULL;
448
+ NMInotifyHelper *inotify_helper;
450
if(!priv->iface_connections)
451
priv->iface_connections = g_hash_table_new (g_str_hash, g_str_equal);
453
PLUGIN_PRINT("SCPlugin-Ifupdown", "init!");
454
priv->hal_mgr = g_object_ref (hal_manager);
456
+ inotify_helper = nm_inotify_helper_get ();
457
+ priv->inotify_event_id = g_signal_connect (inotify_helper,
459
+ G_CALLBACK (update_system_hostname),
462
+ priv->inotify_system_hostname_wd =
463
+ nm_inotify_helper_add_watch (inotify_helper, IFUPDOWN_SYSTEM_HOSTNAME_FILE);
465
+ update_system_hostname(inotify_helper, NULL, NULL, config);
468
block = ifparser_getfirst();
471
if(!strcmp("auto", block->type)) {
472
g_hash_table_insert (auto_ifaces, block->name, "auto");
473
} else if (!strcmp ("iface", block->type) && strcmp ("lo", block->name)) {
474
NMExportedConnection *connection = g_hash_table_lookup(priv->iface_connections, block->name);
475
@@ -260,58 +293,153 @@ static void
476
SCPluginIfupdown_unmanaged_devices_changed (NMSystemConfigInterface *config)
478
PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... started");
479
g_return_if_fail (config != NULL);
481
PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... ended");
485
+get_hostname (NMSystemConfigInterface *config)
487
+ SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
488
+ return priv->hostname;
492
+update_system_hostname(NMInotifyHelper *inotify_helper,
493
+ struct inotify_event *evt,
495
+ NMSystemConfigInterface *config)
497
+ SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
498
+ gchar *hostname_file = NULL;
499
+ gsize hostname_file_len = 0;
500
+ GError *error = NULL;
502
+ PLUGIN_PRINT ("SCPlugin-Ifupdown", "update_system_hostname");
504
+ if (evt && evt->wd != priv->inotify_system_hostname_wd)
507
+ if(!g_file_get_contents ( IFUPDOWN_SYSTEM_HOSTNAME_FILE,
509
+ &hostname_file_len,
511
+ nm_warning ("update_system_hostname() - couldn't read "
512
+ IFUPDOWN_SYSTEM_HOSTNAME_FILE " (%d/%s)",
513
+ error->code, error->message);
517
+ if (priv->hostname)
518
+ g_free(priv->hostname);
520
+ priv->hostname = g_strstrip(hostname_file);
522
+ g_object_notify (G_OBJECT (config), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
526
+write_system_hostname(NMSystemConfigInterface *config,
527
+ const char *newhostname)
529
+ GError *error = NULL;
530
+ SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
531
+ PLUGIN_PRINT ("SCPlugin-Ifupdown", "write_system_hostname: %s", newhostname);
533
+ g_return_if_fail (newhostname);
535
+ if(!g_file_set_contents ( IFUPDOWN_SYSTEM_HOSTNAME_FILE,
539
+ nm_warning ("update_system_hostname() - couldn't write hostname (%s) to "
540
+ IFUPDOWN_SYSTEM_HOSTNAME_FILE " (%d/%s)",
541
+ newhostname, error->code, error->message);
543
+ priv->hostname = g_strdup (newhostname);
545
+ g_object_notify (G_OBJECT (config), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
550
sc_plugin_ifupdown_init (SCPluginIfupdown *plugin)
552
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
553
GError *error = NULL;
555
priv->g_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
556
if (!priv->g_connection) {
557
- PLUGIN_PRINT (IFUPDOWN_PLUGIN_NAME, " dbus-glib error: %s",
558
+ PLUGIN_PRINT ("SCPlugin-Ifupdown", " dbus-glib error: %s",
559
error->message ? error->message : "(unknown)");
560
g_error_free (error);
565
GObject__get_property (GObject *object, guint prop_id,
566
GValue *value, GParamSpec *pspec)
568
+ NMSystemConfigInterface *self = NM_SYSTEM_CONFIG_INTERFACE (object);
571
case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
572
g_value_set_string (value, IFUPDOWN_PLUGIN_NAME);
574
case NM_SYSTEM_CONFIG_INTERFACE_PROP_INFO:
575
g_value_set_string (value, IFUPDOWN_PLUGIN_INFO);
577
case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
578
- g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
579
+ g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
581
case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
582
- g_value_set_string (value, "");
584
+ g_value_set_string (value, get_hostname(self));
588
+ G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
594
+GObject__set_property (GObject *object, guint prop_id,
595
+ const GValue *value, GParamSpec *pspec)
598
+ case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
600
+ const gchar *hostname = g_value_get_string (value);
601
+ if (hostname && strlen (hostname) < 1)
603
+ write_system_hostname(NM_SYSTEM_CONFIG_INTERFACE(object),
608
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
614
GObject__dispose (GObject *object)
616
SCPluginIfupdown *plugin = SC_PLUGIN_IFUPDOWN (object);
617
SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
618
+ NMInotifyHelper *inotify_helper = nm_inotify_helper_get ();
620
+ g_signal_handler_disconnect (inotify_helper, priv->inotify_event_id);
622
+ if (priv->inotify_system_hostname_wd >= 0)
623
+ nm_inotify_helper_remove_watch (inotify_helper, priv->inotify_system_hostname_wd);
625
g_object_unref (priv->hal_mgr);
626
G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->dispose (object);
630
GObject__finalize (GObject *object)
633
=== modified file 'system-settings/src/Makefile.am'
634
--- a/system-settings/src/Makefile.am 2008-07-16 07:37:10 +0000
635
+++ b/system-settings/src/Makefile.am 2008-10-02 22:32:18 +0000
636
@@ -8,16 +8,18 @@ sbin_PROGRAMS = nm-system-settings
639
nm-settings-system-glue.h
641
nm_system_settings_SOURCES = \
645
+ nm-inotify-helper.c \
646
+ nm-inotify-helper.h \
647
nm-polkit-helpers.c \
648
nm-polkit-helpers.h \
649
nm-system-config-error.c \
650
nm-system-config-error.h \
651
nm-system-config-interface.c \
652
nm-system-config-interface.h \
653
nm-system-config-hal-manager.c \
654
nm-system-config-hal-manager.h \
656
=== added file 'system-settings/src/nm-inotify-helper.c'
657
--- a/system-settings/src/nm-inotify-helper.c 1970-01-01 00:00:00 +0000
658
+++ b/system-settings/src/nm-inotify-helper.c 2008-10-02 22:32:18 +0000
660
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
661
+/* NetworkManager system settings service
663
+ * This program is free software; you can redistribute it and/or modify
664
+ * it under the terms of the GNU General Public License as published by
665
+ * the Free Software Foundation; either version 2 of the License, or
666
+ * (at your option) any later version.
668
+ * This program is distributed in the hope that it will be useful,
669
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
670
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
671
+ * GNU General Public License for more details.
673
+ * You should have received a copy of the GNU General Public License along
674
+ * with this program; if not, write to the Free Software Foundation, Inc.,
675
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
677
+ * (C) Copyright 2008 Red Hat, Inc.
682
+#include <sys/inotify.h>
685
+#include "nm-marshal.h"
686
+#include "nm-inotify-helper.h"
688
+G_DEFINE_TYPE (NMInotifyHelper, nm_inotify_helper, G_TYPE_OBJECT)
690
+#define NM_INOTIFY_HELPER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NM_TYPE_INOTIFY_HELPER, NMInotifyHelperPrivate))
695
+ GHashTable *wd_refs;
696
+} NMInotifyHelperPrivate;
704
+static guint signals[LAST_SIGNAL] = { 0 };
707
+nm_inotify_helper_add_watch (NMInotifyHelper *self, const char *path)
709
+ NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
713
+ g_return_val_if_fail (priv->ifd >= 0, -1);
715
+ /* We only care about modifications since we're just trying to get change
716
+ * notifications on hardlinks.
719
+ wd = inotify_add_watch (priv->ifd, path, IN_CLOSE_WRITE);
723
+ refcount = GPOINTER_TO_UINT (g_hash_table_lookup (priv->wd_refs, GINT_TO_POINTER (wd)));
725
+ g_hash_table_replace (priv->wd_refs, GINT_TO_POINTER (wd), GUINT_TO_POINTER (refcount));
731
+nm_inotify_helper_remove_watch (NMInotifyHelper *self, int wd)
733
+ NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
736
+ g_return_if_fail (priv->ifd >= 0);
738
+ refcount = GPOINTER_TO_UINT (g_hash_table_lookup (priv->wd_refs, GINT_TO_POINTER (wd)));
744
+ g_hash_table_remove (priv->wd_refs, GINT_TO_POINTER (wd));
745
+ inotify_rm_watch (priv->ifd, wd);
747
+ g_hash_table_replace (priv->wd_refs, GINT_TO_POINTER (wd), GUINT_TO_POINTER (refcount));
751
+inotify_event_handler (GIOChannel *channel, GIOCondition cond, gpointer user_data)
753
+ NMInotifyHelper *self = NM_INOTIFY_HELPER (user_data);
754
+ struct inotify_event evt;
756
+ /* read the notifications from the watch descriptor */
757
+ while (g_io_channel_read_chars (channel, (gchar *) &evt, sizeof (struct inotify_event), NULL, NULL) == G_IO_STATUS_NORMAL) {
758
+ gchar filename[PATH_MAX + 1];
760
+ filename[0] = '\0';
762
+ g_io_channel_read_chars (channel,
764
+ evt.len > PATH_MAX ? PATH_MAX : evt.len,
768
+ if (!(evt.mask & IN_IGNORED))
769
+ g_signal_emit (self, signals[EVENT], 0, &evt, &filename[0]);
776
+init_inotify (NMInotifyHelper *self)
778
+ NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
779
+ GIOChannel *channel;
782
+ priv->ifd = inotify_init ();
783
+ if (priv->ifd == -1) {
784
+ g_warning ("%s: couldn't initialize inotify", __func__);
788
+ /* Watch the inotify descriptor for file/directory change events */
789
+ channel = g_io_channel_unix_new (priv->ifd);
791
+ g_warning ("%s: couldn't create new GIOChannel", __func__);
797
+ g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL);
798
+ g_io_channel_set_encoding (channel, NULL, NULL);
800
+ source_id = g_io_add_watch (channel,
801
+ G_IO_IN | G_IO_ERR,
802
+ (GIOFunc) inotify_event_handler,
804
+ g_io_channel_unref (channel);
809
+nm_inotify_helper_get (void)
811
+ static NMInotifyHelper *singleton = NULL;
814
+ singleton = (NMInotifyHelper *) g_object_new (NM_TYPE_INOTIFY_HELPER, NULL);
818
+ if (!init_inotify (singleton)) {
819
+ g_object_unref (singleton);
823
+ g_object_ref (singleton);
825
+ g_assert (singleton);
830
+nm_inotify_helper_init (NMInotifyHelper *self)
832
+ NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self);
834
+ priv->wd_refs = g_hash_table_new (g_direct_hash, g_direct_equal);
838
+finalize (GObject *object)
840
+ NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (object);
842
+ if (priv->ifd >= 0)
845
+ g_hash_table_destroy (priv->wd_refs);
847
+ G_OBJECT_CLASS (nm_inotify_helper_parent_class)->finalize (object);
851
+nm_inotify_helper_class_init (NMInotifyHelperClass *klass)
853
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
855
+ g_type_class_add_private (klass, sizeof (NMInotifyHelperPrivate));
857
+ /* Virtual methods */
858
+ object_class->finalize = finalize;
862
+ g_signal_new ("event",
863
+ G_OBJECT_CLASS_TYPE (object_class),
865
+ G_STRUCT_OFFSET (NMInotifyHelperClass, event),
867
+ _nm_marshal_VOID__POINTER_STRING,
868
+ G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_STRING);
872
=== added file 'system-settings/src/nm-inotify-helper.h'
873
--- a/system-settings/src/nm-inotify-helper.h 1970-01-01 00:00:00 +0000
874
+++ b/system-settings/src/nm-inotify-helper.h 2008-10-02 22:32:18 +0000
876
+/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
877
+/* NetworkManager system settings service
879
+ * This program is free software; you can redistribute it and/or modify
880
+ * it under the terms of the GNU General Public License as published by
881
+ * the Free Software Foundation; either version 2 of the License, or
882
+ * (at your option) any later version.
884
+ * This program is distributed in the hope that it will be useful,
885
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
886
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
887
+ * GNU General Public License for more details.
889
+ * You should have received a copy of the GNU General Public License along
890
+ * with this program; if not, write to the Free Software Foundation, Inc.,
891
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
893
+ * (C) Copyright 2008 Red Hat, Inc.
896
+#ifndef __INOTIFY_HELPER_H__
897
+#define __INOTIFY_HELPER_H__
900
+#include <glib-object.h>
901
+#include <sys/inotify.h>
903
+#define NM_TYPE_INOTIFY_HELPER (nm_inotify_helper_get_type ())
904
+#define NM_INOTIFY_HELPER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), NM_TYPE_INOTIFY_HELPER, NMInotifyHelper))
905
+#define NM_INOTIFY_HELPER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), NM_TYPE_INOTIFY_HELPER, NMInotifyHelperClass))
906
+#define NM_IS_INOTIFY_HELPER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), NM_TYPE_INOTIFY_HELPER))
907
+#define NM_IS_INOTIFY_HELPER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((obj), NM_TYPE_INOTIFY_HELPER))
908
+#define NM_INOTIFY_HELPER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), NM_TYPE_INOTIFY_HELPER, NMInotifyHelperClass))
915
+ GObjectClass parent;
918
+ void (* event) (NMInotifyHelper *helper, struct inotify_event *evt, const char *filename);
919
+} NMInotifyHelperClass;
921
+GType nm_inotify_helper_get_type (void);
923
+NMInotifyHelper * nm_inotify_helper_get (void);
925
+int nm_inotify_helper_add_watch (NMInotifyHelper *helper, const char *path);
927
+void nm_inotify_helper_remove_watch (NMInotifyHelper *helper, int wd);
929
+#endif /* __INOTIFY_HELPER_H__ */