NetworkManager r4151 - in trunk: . system-settings/plugins/ifupdown



Author: dcbw
Date: Mon Oct  6 18:07:03 2008
New Revision: 4151
URL: http://svn.gnome.org/viewvc/NetworkManager?rev=4151&view=rev

Log:
2008-10-03  Alexander Sack  <asac ubuntu com>

	Implement system hostname support for debian/ubuntu

	* system-settings/plugins/ifupdown/plugin.c
		- (GObject__get_property): extend announced capabilities; add
			NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME support
		- (GObject__set_property,write_system_hostname): implement
			NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME capability.
		- (GObject__set_property, GObject__get_property, SCPluginIfupdown_init,
		   update_system_hostname, get_hostname):
			implement hostname property that watches and
			parses /etc/hostname



Modified:
   trunk/ChangeLog
   trunk/system-settings/plugins/ifupdown/plugin.c

Modified: trunk/system-settings/plugins/ifupdown/plugin.c
==============================================================================
--- trunk/system-settings/plugins/ifupdown/plugin.c	(original)
+++ trunk/system-settings/plugins/ifupdown/plugin.c	Mon Oct  6 18:07:03 2008
@@ -41,6 +41,7 @@
 #include "nm-ifupdown-connection.h"
 #include "plugin.h"
 #include "parser.h"
+#include "nm-inotify-helper.h"
 
 #include <nm-utils.h>
 #include <sha1.h>
@@ -49,6 +50,7 @@
 
 #define IFUPDOWN_PLUGIN_NAME "ifupdown"
 #define IFUPDOWN_PLUGIN_INFO "(C) 2008 Canonical Ltd.  To report bugs please use the NetworkManager mailing list."
+#define IFUPDOWN_SYSTEM_HOSTNAME_FILE "/etc/hostname"
 
 typedef struct {
 
@@ -56,7 +58,10 @@
 	NMSystemConfigHalManager *hal_mgr;
 
 	GHashTable *iface_connections;
-	
+	gchar* hostname;
+
+	gulong inotify_event_id;
+	int inotify_system_hostname_wd;
 } SCPluginIfupdownPrivate;
 
 static void
@@ -105,11 +110,26 @@
 				   GValue *value, GParamSpec *pspec);
 
 static void
+GObject__set_property (GObject *object, guint prop_id,
+				   const GValue *value, GParamSpec *pspec);
+
+static void
 GObject__dispose (GObject *object);
 
 static void
 GObject__finalize (GObject *object);
 
+/* other helpers */
+static const char *
+get_hostname (NMSystemConfigInterface *config);
+
+
+static void
+update_system_hostname(NMInotifyHelper *inotify_helper,
+                       struct inotify_event *evt,
+                       const char *path,
+                       NMSystemConfigInterface *config);
+
 
 static void
 system_config_interface_init (NMSystemConfigInterface *system_config_interface_class)
@@ -131,6 +151,7 @@
 	object_class->dispose = GObject__dispose;
 	object_class->finalize = GObject__finalize;
 	object_class->get_property = GObject__get_property;
+	object_class->set_property = GObject__set_property;
 
 	g_object_class_override_property (object_class,
 	                                  NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME,
@@ -156,6 +177,7 @@
 	SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
 	GHashTable *auto_ifaces = g_hash_table_new (g_str_hash, g_str_equal);
 	if_block *block = NULL;
+	NMInotifyHelper *inotify_helper;
 
 	if(!priv->iface_connections)
 		priv->iface_connections = g_hash_table_new (g_str_hash, g_str_equal);
@@ -163,6 +185,17 @@
 	PLUGIN_PRINT("SCPlugin-Ifupdown", "init!");
 	priv->hal_mgr = g_object_ref (hal_manager);
 
+	inotify_helper = nm_inotify_helper_get ();
+	priv->inotify_event_id = g_signal_connect (inotify_helper,
+									   "event",
+									   G_CALLBACK (update_system_hostname),
+									   config);
+
+	priv->inotify_system_hostname_wd =
+		nm_inotify_helper_add_watch (inotify_helper, IFUPDOWN_SYSTEM_HOSTNAME_FILE);
+
+	update_system_hostname(inotify_helper, NULL, NULL, config);
+
 	ifparser_init();
 	block = ifparser_getfirst();
 
@@ -265,6 +298,71 @@
 	PLUGIN_PRINT("SCPlugin-Ifdown", "unmanaged_devices_changed ... ended");
 }
 
+static const char *
+get_hostname (NMSystemConfigInterface *config)
+{
+	SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
+	return priv->hostname;
+}
+
+static void
+update_system_hostname(NMInotifyHelper *inotify_helper,
+                       struct inotify_event *evt,
+                       const char *path,
+                       NMSystemConfigInterface *config)
+{
+	SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
+	gchar *hostname_file = NULL;
+	gsize hostname_file_len = 0;
+	GError *error = NULL;
+
+	PLUGIN_PRINT ("SCPlugin-Ifupdown", "update_system_hostname");
+
+	if (evt && evt->wd != priv->inotify_system_hostname_wd)
+		return;
+
+	if(!g_file_get_contents ( IFUPDOWN_SYSTEM_HOSTNAME_FILE,
+						 &hostname_file,
+						 &hostname_file_len,
+						 &error)) {
+		nm_warning ("update_system_hostname() - couldn't read "
+				  IFUPDOWN_SYSTEM_HOSTNAME_FILE " (%d/%s)",
+				  error->code, error->message);
+		return;
+	}
+
+	if (priv->hostname)
+		g_free(priv->hostname);
+
+	priv->hostname = g_strstrip(hostname_file);
+
+	g_object_notify (G_OBJECT (config), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
+}
+
+static void
+write_system_hostname(NMSystemConfigInterface *config,
+				  const char *newhostname)
+{
+	GError *error = NULL;
+	SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (config);
+	PLUGIN_PRINT ("SCPlugin-Ifupdown", "write_system_hostname: %s", newhostname);
+
+	g_return_if_fail (newhostname);
+
+	if(!g_file_set_contents ( IFUPDOWN_SYSTEM_HOSTNAME_FILE,
+						 newhostname,
+						 -1,
+						 &error)) {
+		nm_warning ("update_system_hostname() - couldn't write hostname (%s) to "
+				  IFUPDOWN_SYSTEM_HOSTNAME_FILE " (%d/%s)",
+				  newhostname, error->code, error->message);	
+	} else {
+		priv->hostname = g_strdup (newhostname);
+	}
+	g_object_notify (G_OBJECT (config), NM_SYSTEM_CONFIG_INTERFACE_HOSTNAME);
+}
+
+
 static void
 sc_plugin_ifupdown_init (SCPluginIfupdown *plugin)
 {
@@ -273,7 +371,7 @@
 
 	priv->g_connection = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error);
 	if (!priv->g_connection) {
-		PLUGIN_PRINT (IFUPDOWN_PLUGIN_NAME, "    dbus-glib error: %s",
+		PLUGIN_PRINT ("SCPlugin-Ifupdown", "    dbus-glib error: %s",
 		              error->message ? error->message : "(unknown)");
 		g_error_free (error);
 	}
@@ -283,6 +381,8 @@
 GObject__get_property (GObject *object, guint prop_id,
 				   GValue *value, GParamSpec *pspec)
 {
+	NMSystemConfigInterface *self = NM_SYSTEM_CONFIG_INTERFACE (object);
+
 	switch (prop_id) {
 	case NM_SYSTEM_CONFIG_INTERFACE_PROP_NAME:
 		g_value_set_string (value, IFUPDOWN_PLUGIN_NAME);
@@ -291,11 +391,33 @@
 		g_value_set_string (value, IFUPDOWN_PLUGIN_INFO);
 		break;
 	case NM_SYSTEM_CONFIG_INTERFACE_PROP_CAPABILITIES:
-		g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_NONE);
+		g_value_set_uint (value, NM_SYSTEM_CONFIG_INTERFACE_CAP_MODIFY_HOSTNAME);
 		break;
 	case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
-		g_value_set_string (value, "");
+		{
+			g_value_set_string (value, get_hostname(self));
+			break;
+		}
+	default:
+		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 		break;
+	}
+}
+
+static void
+GObject__set_property (GObject *object, guint prop_id,
+				   const GValue *value, GParamSpec *pspec)
+{
+	switch (prop_id) {
+	case NM_SYSTEM_CONFIG_INTERFACE_PROP_HOSTNAME:
+		{
+			const gchar *hostname = g_value_get_string (value);
+			if (hostname && strlen (hostname) < 1)
+				hostname = NULL;
+			write_system_hostname(NM_SYSTEM_CONFIG_INTERFACE(object),
+							  hostname);
+			break;
+		}
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 		break;
@@ -307,6 +429,12 @@
 {
 	SCPluginIfupdown *plugin = SC_PLUGIN_IFUPDOWN (object);
 	SCPluginIfupdownPrivate *priv = SC_PLUGIN_IFUPDOWN_GET_PRIVATE (plugin);
+	NMInotifyHelper *inotify_helper = nm_inotify_helper_get ();
+
+	g_signal_handler_disconnect (inotify_helper, priv->inotify_event_id);
+
+	if (priv->inotify_system_hostname_wd >= 0)
+		nm_inotify_helper_remove_watch (inotify_helper, priv->inotify_system_hostname_wd);
 
 	g_object_unref (priv->hal_mgr);
 	G_OBJECT_CLASS (sc_plugin_ifupdown_parent_class)->dispose (object);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]