Skip to contentSkip to conversation

shared/mgmt: Make mgmt_new_default return the same instance

PATCH In review Luiz Augusto von Dentz 2 participants 2 messages Feb 27, 2018 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH v2 1/1 shared/mgmt: Make mgmt_new_default return the same instance

From: Luiz Augusto von Dentz <[email protected]>

If an instance already exists there is no reason to create a second one as they both will bind to HCI_CHANNEL_CONTROL.

---
 src/shared/mgmt.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
src/shared/mgmt.c +10 -0
index 277e361a6..716c44289 100644
@@ -81,6 +81,8 @@ struct mgmt_notify {
 	void *user_data;
 };
 
+static struct mgmt *default_mgmt;
+
 static void destroy_request(void *data)
 {
 	struct mgmt_request *request = data;
@@ -432,6 +434,9 @@ struct mgmt *mgmt_new_default(void)
 	} addr;
 	int fd;
 
+	if (default_mgmt)
+		return mgmt_ref(default_mgmt);
+
 	fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
 								BTPROTO_HCI);
 	if (fd < 0)
@@ -455,6 +460,8 @@ struct mgmt *mgmt_new_default(void)
 
 	mgmt->close_on_unref = true;
 
+	default_mgmt = mgmt;
+
 	return mgmt;
 }
 
@@ -476,6 +483,9 @@ void mgmt_unref(struct mgmt *mgmt)
 	if (__sync_sub_and_fetch(&mgmt->ref_count, 1))
 		return;
 
+	if (default_mgmt == mgmt)
+		default_mgmt = NULL;
+
 	mgmt_unregister_all(mgmt);
 	mgmt_cancel_all(mgmt);
 
Signature · 1 line
2.14.3
maintainer
#1 lore raw

Hi Luiz,

Quoted 39 lines

If an instance already exists there is no reason to create a second one as they both will bind to HCI_CHANNEL_CONTROL.
---
src/shared/mgmt.c | 10 ++++++++++

1 file changed, 10 insertions(+)
src/shared/mgmt.c +7 -0
index 277e361a6..716c44289 100644
@@ -81,6 +81,8 @@ struct mgmt_notify {
 	void *user_data;
 };
 
+static struct mgmt *default_mgmt;
+
 static void destroy_request(void *data)
 {
 	struct mgmt_request *request = data;
@@ -432,6 +434,9 @@ struct mgmt *mgmt_new_default(void)
 	} addr;
 	int fd;
 
+	if (default_mgmt)
+		return mgmt_ref(default_mgmt);
+
 	fd = socket(PF_BLUETOOTH, SOCK_RAW | SOCK_CLOEXEC | SOCK_NONBLOCK,
 								BTPROTO_HCI);
 	if (fd < 0)
@@ -455,6 +460,8 @@ struct mgmt *mgmt_new_default(void)
 
 	mgmt->close_on_unref = true;
 
+	default_mgmt = mgmt;
+
 	return mgmt;
 }
 

src/shared/mgmt.c void mgmt_unref(struct mgmt *mgmt) · line 490

Quoted 8 lines
src/shared/mgmt.c +3 -0
@@ -476,6 +483,9 @@ void mgmt_unref(struct mgmt *mgmt)
 	if (__sync_sub_and_fetch(&mgmt->ref_count, 1))
 		return;
 
+	if (default_mgmt == mgmt)
+		default_mgmt = NULL;
+
 	mgmt_unregister_all(mgmt);
 	mgmt_cancel_all(mgmt);

this is a poor mans fix for the issue at hand. It is not the job of shared/mgmt.c to fix this. So NAK.

Lets make sure that bluetoothd internally does this correctly.

Regards

Marcel