[Pkg-wmaker-commits] [wmbattery] 104/241: * Support reconnecting to hal if the connection is closed, as happens if the hal is restarted.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Mon Aug 24 23:37:41 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository wmbattery.

commit e48d2d48acbaa547052f2b90e332752445802bd4
Author: Joey Hess <joey at kodama.kitenet.net>
Date:   Wed Feb 20 22:30:04 2008 -0500

    * Support reconnecting to hal if the connection is closed, as happens
      if the hal is restarted.
---
 debian/changelog |   7 +++
 simplehal.c      | 129 +++++++++++++++++++++++++++++++++----------------------
 2 files changed, 85 insertions(+), 51 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index b4f4d99..34805e7 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,3 +1,10 @@
+wmbattery (2.36) unstable; urgency=low
+
+  * Support reconnecting to hal if the connection is closed, as happens
+    if the hal is restarted.
+
+ -- Joey Hess <joeyh at debian.org>  Wed, 20 Feb 2008 22:29:00 -0500
+
 wmbattery (2.35) unstable; urgency=low
 
   * Don't spew error messages if optional hal properties are not available.
diff --git a/simplehal.c b/simplehal.c
index e42ca46..f345b9a 100644
--- a/simplehal.c
+++ b/simplehal.c
@@ -16,38 +16,94 @@ int num_batteries = 0;
 char **ac_adapters = NULL;
 char **batteries = NULL;
 
+int connect_hal (void) {
+	DBusError error;
+	DBusConnection *conn;
+
+	dbus_error_init(&error);
+	conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
+	if (conn == NULL) {
+		fprintf(stderr, "error: dbus_bus_get: %s: %s\n",
+			 error.name, error.message);
+		LIBHAL_FREE_DBUS_ERROR(&error);
+		return 0;
+	}
+	if ((hal_ctx = libhal_ctx_new()) == NULL) {
+		fprintf(stderr, "error: libhal_ctx_new\n");
+		LIBHAL_FREE_DBUS_ERROR(&error);
+		return 0;
+	}
+	if (!libhal_ctx_set_dbus_connection (hal_ctx, conn)) {
+		fprintf(stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n",
+			 error.name, error.message);
+		LIBHAL_FREE_DBUS_ERROR(&error);
+		return 0;
+	}
+	if (!libhal_ctx_init(hal_ctx, &error)) {
+		if (dbus_error_is_set(&error)) {
+			fprintf(stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
+			LIBHAL_FREE_DBUS_ERROR (&error);
+		}
+		fprintf(stderr, "Could not initialise connection to hald.\n"
+				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
+		return 0;
+	}
+
+	return 1;
+}
+
 signed int get_hal_int (const char *udi, const char *key, int optional) {
 	int ret;
 	DBusError error;
-
+	
 	dbus_error_init(&error);
-	ret = libhal_device_get_property_int (hal_ctx, udi, key, &error);
+
+	for (;;) {
+		ret = libhal_device_get_property_int (hal_ctx, udi, key, &error);
 	
-	if (dbus_error_is_set (&error)) {
-		if (! optional)
-			fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n",
-				 error.name, error.message);
-		dbus_error_free (&error);
-		return -1;
+		if (! dbus_error_is_set (&error)) {
+			return ret;
+		}
+		else {
+			if (strcmp(error.name, "org.freedesktop.dbus.error.disconnected") == 0 &&
+			    connect_hal()) {
+				continue; /* retry */
+			}
+			else if (! optional) {
+				fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n",
+					 error.name, error.message);
+			}
+			dbus_error_free (&error);
+			return -1;
+		}
 	}
-	return ret;
 }
 
 signed int get_hal_bool (const char *udi, const char *key, int optional) {
 	int ret;
 	DBusError error;
-
+	
 	dbus_error_init(&error);
-	ret = libhal_device_get_property_bool (hal_ctx, udi, key, &error);
+
+	for (;;) {
+		ret = libhal_device_get_property_bool (hal_ctx, udi, key, &error);
 	
-	if (dbus_error_is_set (&error)) {
-		if (! optional)
-			fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n",
-				 error.name, error.message);
-		dbus_error_free (&error);
-		return -1;
+		if (! dbus_error_is_set (&error)) {
+			return ret;
+		}
+		else {
+			if (strcmp(error.name, "org.freedesktop.dbus.error.disconnected") == 0 &&
+			    connect_hal()) {
+				continue; /* retry */
+			}
+			else if (! optional) {
+				fprintf(stderr, "error: libhal_device_get_property_int: %s: %s\n",
+					 error.name, error.message);
+			}
+			dbus_error_free (&error);
+			return -1;
+		}
 	}
-	return ret;
 }
 
 void find_devices (void) {
@@ -74,43 +130,14 @@ void find_devices (void) {
 	}
 }
 
-
 int simplehal_supported (void) {
-	DBusError error;
-	DBusConnection *conn;
-
-	dbus_error_init(&error);
-	conn = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
-	if (conn == NULL) {
-		fprintf(stderr, "error: dbus_bus_get: %s: %s\n",
-			 error.name, error.message);
-		LIBHAL_FREE_DBUS_ERROR(&error);
-		return 0;
-	}
-	if ((hal_ctx = libhal_ctx_new()) == NULL) {
-		fprintf(stderr, "error: libhal_ctx_new\n");
-		LIBHAL_FREE_DBUS_ERROR(&error);
-		return 0;
-	}
-	if (!libhal_ctx_set_dbus_connection (hal_ctx, conn)) {
-		fprintf(stderr, "error: libhal_ctx_set_dbus_connection: %s: %s\n",
-			 error.name, error.message);
-		LIBHAL_FREE_DBUS_ERROR(&error);
+	if (! connect_hal()) {
 		return 0;
 	}
-	if (!libhal_ctx_init(hal_ctx, &error)) {
-		if (dbus_error_is_set(&error)) {
-			fprintf(stderr, "error: libhal_ctx_init: %s: %s\n", error.name, error.message);
-			LIBHAL_FREE_DBUS_ERROR (&error);
-		}
-		fprintf(stderr, "Could not initialise connection to hald.\n"
-				 "Normally this means the HAL daemon (hald) is not running or not ready.\n");
-		return 0;
+	else {
+		find_devices();
+		return 1;
 	}
-
-	find_devices();
-
-	return 1;
 }
 
 /* Fill the passed apm_info struct. */

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmbattery.git



More information about the Pkg-wmaker-commits mailing list