Skip to contentSkip to conversation

Add udev mode to bluetoothd

PATCH In review Bastien Nocera 3 participants 9 messages Jun 11, 2009 – Jun 13, 2009 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add udev mode to bluetoothd

As discussed on IRC.

Still needed:
- patch to wait for the bus to startup in udev mode
- udev rules
- unleash for testing

Cheers

Quoted 1 line

From 3e9257b31efcffbb8583f99770604b40a04410b2 Mon Sep 17 00:00:00 2001

From: Bastien Nocera <[email protected]>
Date: Thu, 11 Jun 2009 18:33:35 +0100
Subject: [PATCH] Add udev mode to bluetoothd

Add --udev option to bluetoothd, to allow it to be started on-demand from udev.

When a new adapter appears, udev would launch bluetoothd --udev.

To avoid problems with udev, bluetoothd --udev would only return an error exit code if it wasn't already running and a real error occurred.

When no more Bluetooth adapter are present on the system, bluetoothd will exit after a 30 second timeout.

---
 src/dbus-common.c |   18 +++++++++++----
 src/hcid.h        |    3 ++
 src/main.c        |   63 +++++++++++++++++++++++++++++++++++++++++++++++++---
 src/manager.c     |    6 +++++
 4 files changed, 81 insertions(+), 9 deletions(-)
src/dbus-common.c +13 -5
index b596909..d06d8e5 100644
@@ -165,19 +165,27 @@ void hcid_dbus_exit(void)
 int hcid_dbus_init(void)
 {
 	DBusConnection *conn;
+	DBusError err;
 
-	conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, NULL);
-	if (!conn)
-		return -1;
+	dbus_error_init(&err);
+
+	conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, &err);
+	if (!conn) {
+		if (error != NULL && dbus_error_is_set(&err)) {
+			dbus_error_free(&err);
+			return -EIO;
+		}
+		return -EALREADY;
+	}
 
 	if (g_dbus_set_disconnect_function(conn, disconnect_callback,
 							NULL, NULL) == FALSE) {
 		dbus_connection_unref(conn);
-		return -1;
+		return -EIO;
 	}
 
 	if (!manager_init(conn, "/"))
-		return -1;
+		return -EIO;
 
 	set_dbus_connection(conn);
 
src/hcid.h +3 -0
index 4fbbef1..605dc06 100644
@@ -85,6 +85,9 @@ void hci_req_queue_remove(int dev_id, bdaddr_t *dba);
 void start_security_manager(int hdev);
 void stop_security_manager(int hdev);
 
+void btd_start_exit_timer(void);
+void btd_stop_exit_timer(void);
+
 void set_pin_length(bdaddr_t *sba, int length);
 
 gboolean plugin_init(GKeyFile *config);
src/main.c +59 -4

101 lines not on this page · raw message

src/manager.c +6 -0
index db6d251..ab69e4e 100644
@@ -43,6 +43,7 @@
 
 #include <gdbus.h>
 
+#include "hcid.h"
 #include "dbus-common.h"
 #include "logging.h"
 #include "adapter.h"
@@ -312,6 +313,9 @@ static void manager_remove_adapter(struct btd_adapter *adapter)
 			DBUS_TYPE_INVALID);
 
 	adapter_remove(adapter);
+
+	if (adapters == NULL)
+		btd_start_exit_timer();
 }
 
 void manager_cleanup(DBusConnection *conn, const char *path)
@@ -421,6 +425,8 @@ void manager_add_adapter(const char *path)
 			DBUS_TYPE_INVALID);
 
 	manager_update_adapters();
+
+	btd_stop_exit_timer();
 }
 
 int manager_register_adapter(int id, gboolean devup)
Signature · 1 line
1.6.2.2
maintainer
#1 lore raw

Hi Bastien,

Quoted 6 lines

As discussed on IRC.

Still needed:
- patch to wait for the bus to startup in udev mode
- udev rules
- unleash for testing

patch has been applied. Thanks.

Regards

Marcel

#2 lore raw
Quoted 6 lines · Marcel Holtmann

Hi Bastien,

Quoted 4 lines

As discussed on IRC.

Still needed:
- patch to wait for the bus to startup in udev mode

Should we really do that? (What if the bus never appears?, how long should we wait?)
The alternative (not overwhelmingly nice, I admit) would be to record somewhere in the system that bluetoothd tried to start but could not due to the missing bus and then start it "manually" by an init script that is guaranteed to run after DBus is started.

I had patches for waiting for the bus long time ago and can dig them out if wanted.

Best regards,

Stefan

Signature · 6 lines
Stefan Seyfried
R&D Team Mobile Devices            |              "Any ideas, John?"
SUSE LINUX Products GmbH, Nürnberg | "Well, surrounding them's out."

This footer brought to you by insane German lawmakers:
SUSE Linux Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
#3 lore raw
Quoted 2 lines · Stefan Seyfried

I had patches for waiting for the bus long time ago and can dig them out if wanted.

Actually I quickly dug them out and adapted them to the current code.

I arbitrarily chose to stop waiting after 5 minutes. This timeout is certainly
a topic that might need to be discussed.

Best regards,

Stefan

Signature · 6 lines
Stefan Seyfried
R&D Team Mobile Devices            |              "Any ideas, John?"
SUSE LINUX Products GmbH, Nürnberg | "Well, surrounding them's out."

This footer brought to you by insane German lawmakers:
SUSE Linux Products GmbH, GF: Markus Rex, HRB 16746 (AG Nürnberg)
Quoted 1 line

From 8b41af8ac12e2cf0824af4f65e0ef47ffac3dcb8 Mon Sep 17 00:00:00 2001

From: Stefan Seyfried <[email protected]>
Date: Fri, 12 Jun 2009 10:22:29 +0200
Subject: [PATCH] Wait for system bus to appear

If the system bus is not there when starting bluetoothd, wait up to five minutes for it to appear and retry the connection every five seconds.

---
 src/dbus-common.c |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)
src/dbus-common.c +16 -5
index d06d8e5..dc648be 100644
@@ -162,21 +162,32 @@ void hcid_dbus_exit(void)
 	dbus_connection_unref(conn);
 }
 
+#define MAX_DBUS_RETRY		60
 int hcid_dbus_init(void)
 {
 	DBusConnection *conn;
 	DBusError err;
+	int retry = 0;
 
-	dbus_error_init(&err);
+	do {
+		dbus_error_init(&err);
+		conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, &err);
+		if (conn)
+			break;
 
-	conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, BLUEZ_NAME, &err);
-	if (!conn) {
 		if (error != NULL && dbus_error_is_set(&err)) {
 			dbus_error_free(&err);
-			return -EIO;
+			if (retry >= MAX_DBUS_RETRY)
+				return -EIO;
+			if (!retry)
+				info("Can't connect to system bus, will retry "
+					"%i times.", MAX_DBUS_RETRY);
+			sleep(5);
+			retry++;
+			continue;
 		}
 		return -EALREADY;
-	}
+	} while (1);
 
 	if (g_dbus_set_disconnect_function(conn, disconnect_callback,
 							NULL, NULL) == FALSE) {
Signature · 1 line
1.6.3.2
#4 lore raw
Quoted 9 lines · Stefan Seyfried
Quoted 2 lines · Stefan Seyfried

I had patches for waiting for the bus long time ago and can dig them out if wanted.

Actually I quickly dug them out and adapted them to the current code.

I arbitrarily chose to stop waiting after 5 minutes. This timeout is certainly
a topic that might need to be discussed.

This patch won't work as part of the udev mode, as we don't daemonise until we have a D-Bus connection. That would make udev hang for 5 minutes waiting for the helper to come back.

#5 lore raw
In reply to Stefan Seyfried
Quoted 8 lines · Stefan Seyfried
Quoted 6 lines · Marcel Holtmann

Hi Bastien,

Quoted 4 lines

As discussed on IRC.

Still needed:
- patch to wait for the bus to startup in udev mode

Quoted 6 lines

Should we really do that? (What if the bus never appears?, how long should we wait?)
The alternative (not overwhelmingly nice, I admit) would be to record somewhere in the system that bluetoothd tried to start but could not due to the missing bus and then start it "manually" by an init script that is guaranteed to run after DBus is started.

Under normal conditions, we'd exit(1) if started and the bus isn't available, and udev would pick that up, marking our job as failed, and relaunching us later in the boot process, under coldplug.

Marcel didn't like the idea though.

Quoted 2 lines

I had patches for waiting for the bus long time ago and can dig them out if wanted.

Sure, as long as it's not bluez 3.x stuff :)

maintainer
#6 lore raw

Hi Bastien,

Quoted 12 lines
Quoted 11 lines
Quoted 4 lines
Quoted 4 lines

As discussed on IRC.

Still needed:
- patch to wait for the bus to startup in udev mode

Should we really do that? (What if the bus never appears?, how long should we wait?)
The alternative (not overwhelmingly nice, I admit) would be to record somewhere in the system that bluetoothd tried to start but could not due to the missing bus and then start it "manually" by an init script that is guaranteed to run after DBus is started.

Quoted 5 lines

Under normal conditions, we'd exit(1) if started and the bus isn't available, and udev would pick that up, marking our job as failed, and relaunching us later in the boot process, under coldplug.

Marcel didn't like the idea though.

so how does udev handle this exactly. We try bluetoothd and it fails, then it tries again later? What time exactly? How often? Does this affect the fast-boot effort?

Regards

Marcel

#7 lore raw
Quoted 18 lines · Marcel Holtmann

Hi Bastien,

Quoted 16 lines
Quoted 11 lines
Quoted 4 lines
Quoted 4 lines

As discussed on IRC.

Still needed:
- patch to wait for the bus to startup in udev mode

Should we really do that? (What if the bus never appears?, how long should we wait?)
The alternative (not overwhelmingly nice, I admit) would be to record somewhere in the system that bluetoothd tried to start but could not due to the missing bus and then start it "manually" by an init script that is guaranteed to run after DBus is started.

Under normal conditions, we'd exit(1) if started and the bus isn't available, and udev would pick that up, marking our job as failed, and relaunching us later in the boot process, under coldplug.

Quoted 5 lines
Quoted 1 line

Marcel didn't like the idea though.

so how does udev handle this exactly. We try bluetoothd and it fails, then it tries again later? What time exactly? How often? Does this affect the fast-boot effort?

It will try to start up bluetooth as soon as it sees the device on startup. bluetoothd will fail to start, as D-Bus isn't started, with an exit code of 1.

udev notes the failure, and saves the rules to /dev/.udev/*.

The initscripts carry on, filesystems are mounted rw, D-Bus is started, then a udev "coldplug" is started (still part of the initscripts), and bluetoothd is started as expected.

I built some test packages yesterday, and Petr tested those, and it seems to work as expected.

If you fancy trying out on an F-12 system (the SRPM can be re-used on F-11, just remove the libgudev-devel BuildRequires line):
http://koji.fedoraproject.org/koji/buildinfo?buildID=105952

So as far as I'm concerned, the only thing missing is getting the rules into bluez.

Marcel, do you want the udev rules installed by default? The cost would be an attempted run at bluetoothd on each adapter insertion, but it would still work as expected if bluetoothd was started from an initscript.

Cheers

maintainer
#8 lore raw

Hi Bastien,

Quoted 27 lines
Quoted 9 lines
Quoted 5 lines

Under normal conditions, we'd exit(1) if started and the bus isn't available, and udev would pick that up, marking our job as failed, and relaunching us later in the boot process, under coldplug.

Marcel didn't like the idea though.

so how does udev handle this exactly. We try bluetoothd and it fails, then it tries again later? What time exactly? How often? Does this affect the fast-boot effort?

It will try to start up bluetooth as soon as it sees the device on startup. bluetoothd will fail to start, as D-Bus isn't started, with an exit code of 1.

udev notes the failure, and saves the rules to /dev/.udev/*.

The initscripts carry on, filesystems are mounted rw, D-Bus is started, then a udev "coldplug" is started (still part of the initscripts), and bluetoothd is started as expected.

I built some test packages yesterday, and Petr tested those, and it seems to work as expected.

If you fancy trying out on an F-12 system (the SRPM can be re-used on F-11, just remove the libgudev-devel BuildRequires line):
http://koji.fedoraproject.org/koji/buildinfo?buildID=105952

Quoted 7 lines

So as far as I'm concerned, the only thing missing is getting the rules into bluez.

Marcel, do you want the udev rules installed by default? The cost would be an attempted run at bluetoothd on each adapter insertion, but it would still work as expected if bluetoothd was started from an initscript.

I have no problem triggering bluetoothd from udev every time we add a new adapter. And unless we try it, we never know. I am a little bit concerned about the boot time implications of this.

Send me a patch with the rules installation and lets make it default. We see how far we get. I do hate init scripts anyway and if they get too complex it costs us actually more time at boot than just starting the daemon.

Regards

Marcel