[PATCH] Fix D-Bus restart for addon-cpufreq

Holger Macht hmacht at suse.de
Sat Oct 28 08:43:34 PDT 2006


Hi,

this patch fixes the D-Bus restart case for the CPUFreq addon. The D-Bus
connection you get with libhal_ctx_init_direct is just good for talking to
the HAL daemon, but not for talking to others like PolicyKit. So for other
D-Bus messages, I need to use another connection which I have to reconnect
properly after a D-Bus restart.

Ok to commit?

Regards,
	Holger

---
diff --git a/hald/linux/addons/Makefile.am b/hald/linux/addons/Makefile.am
diff --git a/hald/linux/addons/addon-cpufreq.c b/hald/linux/addons/addon-cpufreq.c
index e07ff69..2dc271b 100644
--- a/hald/linux/addons/addon-cpufreq.c
+++ b/hald/linux/addons/addon-cpufreq.c
@@ -815,7 +815,7 @@ static gboolean dbus_is_privileged(DBusC
 	gboolean		out_is_allowed;
 	gboolean		out_is_temporary;
 	LibPolKitResult		res;
-    
+
 	connection_new = dbus_bus_get(DBUS_BUS_SYSTEM, error);
 	if (dbus_error_is_set(error)) {
 		dbus_raise_error(connection, message, CPUFREQ_ERROR_GENERAL,
@@ -952,6 +952,19 @@ static gboolean dbus_get_argument(DBusCo
 	return TRUE;
 }
 
+static DBusHandlerResult dbus_filter_function_local(DBusConnection *connection,
+						    DBusMessage *message,
+						    void *user_data)
+{
+	if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
+				   "Disconnected")) {
+		HAL_DEBUG(("DBus daemon disconnected. Trying to reconnect..."));
+		dbus_connection_unref(connection);
+		g_timeout_add(5000, (GSourceFunc)dbus_init_local, NULL);
+	}
+	return DBUS_HANDLER_RESULT_HANDLED;
+}
+
 /** dbus filter function
  *
  * @raises UnknownMethod
@@ -1043,13 +1056,6 @@ #endif
 			dbus_send_reply_strlist(connection, message, governors);
 		g_strfreev(governors);
 
-	} else if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL,
-					  "Disconnected")) {
-		HAL_DEBUG(("DBus daemon disconnected. Trying to reconnect..."));
-		dbus_connection_close(connection);
-		dbus_connection_unref(connection);
-		g_timeout_add(5000, (GSourceFunc)dbus_init, NULL);
-
 	} else {
 		return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 	}
@@ -1071,6 +1077,26 @@ static gboolean is_supported(void)
 }
 
 /** returns FALSE on success because it's used as a callback */
+gboolean dbus_init_local(void)
+{
+	DBusConnection	*dbus_connection;
+	DBusError	dbus_error;
+
+	dbus_error_init(&dbus_error);
+
+	dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
+	if (dbus_error_is_set(&dbus_error)) {
+		HAL_WARNING(("Cannot get D-Bus connection"));
+		return TRUE;
+	}
+
+	dbus_connection_setup_with_g_main(dbus_connection, NULL);
+	dbus_connection_add_filter(dbus_connection, dbus_filter_function_local,
+				   NULL, NULL);
+	dbus_connection_set_exit_on_disconnect(dbus_connection, 0);
+	return FALSE;
+}
+
 gboolean dbus_init(void)
 {
 	DBusError	dbus_error;
@@ -1090,12 +1116,10 @@ gboolean dbus_init(void)
 		goto Error;
 	}
 
-	dbus_error_init (&dbus_error);
 	if (!libhal_device_addon_is_ready (halctx, udi, &dbus_error)) {
 		goto Error;
 	}
 
-
 	if (!libhal_device_claim_interface(halctx, udi,
 		"org.freedesktop.Hal.Device.CPUFreq", 
 		"    <method name=\"SetCPUFreqGovernor\">\n"
@@ -1137,11 +1161,11 @@ gboolean dbus_init(void)
 	dbus_connection_setup_with_g_main(dbus_connection, NULL);
 	dbus_connection_add_filter(dbus_connection, dbus_filter_function, NULL, NULL);
 	dbus_connection_set_exit_on_disconnect(dbus_connection, 0);
-	return FALSE;
+	return TRUE;
 
 Error:
 	dbus_error_free(&dbus_error);
-	return TRUE;
+	return FALSE;
 }
 /********************* DBus end *********************/
 
@@ -1179,7 +1203,7 @@ int main(int argc, char *argv[])
 		exit(EXIT_FAILURE);
 	}
 
-	if (dbus_init())
+	if (!dbus_init() || dbus_init_local())
 		exit(EXIT_FAILURE);
 
 	gmain = g_main_loop_new(NULL, FALSE);
diff --git a/hald/linux/addons/addon-cpufreq.h b/hald/linux/addons/addon-cpufreq.h
index d812219..f4141be 100644
--- a/hald/linux/addons/addon-cpufreq.h
+++ b/hald/linux/addons/addon-cpufreq.h
@@ -69,4 +69,6 @@ gboolean	write_governor		(char *new_gove
 
 gboolean	dbus_init		(void);
 
+gboolean	dbus_init_local		(void);
+
 #endif /* ADDON_CPUFREQ_H */
diff --git a/hald/linux/addons/addon-macbookpro-backlight.c b/hald/linux/addons/addon-macbookpro-backlight.c



More information about the hal mailing list