Skip to contentSkip to conversation

client: Clean up agent if bluetoothd exited without releasing it

PATCH In review Szymon Janc 2 participants 3 messages Dec 22, 2013 – Dec 23, 2013 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 client: Clean up agent if bluetoothd exited without releasing it

If bluetoothd exited without releasing agent (eg. due to crash) it was not possible to register client as agent after daemon was restarted.

---
 client/agent.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
client/agent.c +19 -2
index 2d9dffd..b34ad77 100644
@@ -35,6 +35,7 @@
 
 #define AGENT_PATH "/org/bluez/agent"
 #define AGENT_INTERFACE "org.bluez.Agent1"
+#define BLUEZ_NAME "org.bluez"
 
 #define AGENT_PROMPT	COLOR_RED "[agent]" COLOR_OFF " "
 
@@ -43,6 +44,7 @@ static const char *agent_capability = NULL;
 static DBusMessage *pending_message = NULL;
 static char *agent_saved_prompt = NULL;
 static int agent_saved_point = 0;
+static guint watch_id = 0;
 
 static void agent_prompt(const char *msg)
 {
@@ -154,8 +156,7 @@ dbus_bool_t agent_input(DBusConnection *conn, const char *input)
 	return TRUE;
 }
 
-static DBusMessage *release_agent(DBusConnection *conn,
-					DBusMessage *msg, void *user_data)
+static void clean_agent(DBusConnection *conn, void *user_data)
 {
 	agent_registered = FALSE;
 	agent_capability = NULL;
@@ -171,6 +172,15 @@ static DBusMessage *release_agent(DBusConnection *conn,
 
 	g_dbus_unregister_interface(conn, AGENT_PATH, AGENT_INTERFACE);
 
+	g_dbus_remove_watch(conn, watch_id);
+	watch_id = 0;
+}
+
+static DBusMessage *release_agent(DBusConnection *conn, DBusMessage *msg,
+								void *user_data)
+{
+	clean_agent(conn, NULL);
+
 	return dbus_message_new_method_return(msg);
 }
 
@@ -364,6 +374,10 @@ static void register_agent_reply(DBusMessage *message, void *user_data)
 	if (dbus_set_error_from_message(&error, message) == FALSE) {
 		agent_registered = TRUE;
 		rl_printf("Agent registered\n");
+
+		watch_id = g_dbus_add_service_watch(conn, BLUEZ_NAME, NULL,
+							clean_agent, NULL,
+							NULL);
 	} else {
 		rl_printf("Failed to register agent: %s\n", error.name);
 		dbus_error_free(&error);
@@ -425,6 +439,9 @@ static void unregister_agent_reply(DBusMessage *message, void *user_data)
 		if (g_dbus_unregister_interface(conn, AGENT_PATH,
 						AGENT_INTERFACE) == FALSE)
 			rl_printf("Failed to unregister agent object\n");
+
+		g_dbus_remove_watch(conn, watch_id);
+		watch_id = 0;
 	} else {
 		rl_printf("Failed to unregister agent: %s\n", error.name);
 		dbus_error_free(&error);
Signature · 1 line
1.8.5.2
#1 lore raw

PATCH 2/2 client: Minor code style fix

---
 client/agent.c | 1 -
 1 file changed, 1 deletion(-)
client/agent.c +0 -1
index b34ad77..20787bc 100644
@@ -390,7 +390,6 @@ static void register_agent_reply(DBusMessage *message, void *user_data)
 
 void agent_register(DBusConnection *conn, GDBusProxy *manager,
 						const char *capability)
-
 {
 	if (agent_registered == TRUE) {
 		rl_printf("Agent is already registered\n");
Signature · 1 line
1.8.5.2
#2 lore raw
In reply to Szymon Janc

Hi Szymon,

Quoted 74 lines · Szymon Janc

If bluetoothd exited without releasing agent (eg. due to crash) it was not possible to register client as agent after daemon was restarted.

---
 client/agent.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)
client/agent.c +19 -3
index 2d9dffd..b34ad77 100644
@@ -35,6 +35,7 @@
 
 #define AGENT_PATH "/org/bluez/agent"
 #define AGENT_INTERFACE "org.bluez.Agent1"
+#define BLUEZ_NAME "org.bluez"
 
 #define AGENT_PROMPT   COLOR_RED "[agent]" COLOR_OFF " "
 
@@ -43,6 +44,7 @@ static const char *agent_capability = NULL;
 static DBusMessage *pending_message = NULL;
 static char *agent_saved_prompt = NULL;
 static int agent_saved_point = 0;
+static guint watch_id = 0;
 
 static void agent_prompt(const char *msg)
 {
@@ -154,8 +156,7 @@ dbus_bool_t agent_input(DBusConnection *conn, const char *input)
        return TRUE;
 }
 
-static DBusMessage *release_agent(DBusConnection *conn,
-                                       DBusMessage *msg, void *user_data)
+static void clean_agent(DBusConnection *conn, void *user_data)
 {
        agent_registered = FALSE;
        agent_capability = NULL;
@@ -171,6 +172,15 @@ static DBusMessage *release_agent(DBusConnection *conn,
 
        g_dbus_unregister_interface(conn, AGENT_PATH, AGENT_INTERFACE);
 
+       g_dbus_remove_watch(conn, watch_id);
+       watch_id = 0;
+}
+
+static DBusMessage *release_agent(DBusConnection *conn, DBusMessage *msg,
+                                                               void *user_data)
+{
+       clean_agent(conn, NULL);
+
        return dbus_message_new_method_return(msg);
 }
 
@@ -364,6 +374,10 @@ static void register_agent_reply(DBusMessage *message, void *user_data)
        if (dbus_set_error_from_message(&error, message) == FALSE) {
                agent_registered = TRUE;
                rl_printf("Agent registered\n");
+
+               watch_id = g_dbus_add_service_watch(conn, BLUEZ_NAME, NULL,
+                                                       clean_agent, NULL,
+                                                       NULL);
        } else {
                rl_printf("Failed to register agent: %s\n", error.name);
                dbus_error_free(&error);
@@ -425,6 +439,9 @@ static void unregister_agent_reply(DBusMessage *message, void *user_data)
                if (g_dbus_unregister_interface(conn, AGENT_PATH,
                                                AGENT_INTERFACE) == FALSE)
                        rl_printf("Failed to unregister agent object\n");
+
+               g_dbus_remove_watch(conn, watch_id);
+               watch_id = 0;
        } else {
                rl_printf("Failed to unregister agent: %s\n", error.name);
                dbus_error_free(&error);
--
Quoted 1 line

1.8.5.2

Ive send a patch for it not long time ago, but instead of adding a new watch I just unregister the agent whenever org.bluez.AgentManager1 proxy is removed which should treat the case where the daemon crash or exit without sending a proper unregister.

Signature · 1 line
Luiz Augusto von Dentz