Skip to contentSkip to conversation

Fix bluetoothctl --help hanging if daemon isn't running

PATCH Superseded Bastien Nocera 3 participants 13 messages Oct 22, 2024 – Oct 28, 2024 Bluetooth drivers

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Fix bluetoothctl --help hanging if daemon isn't running

Changes from v2:
- rebased against latest upstream

Bastien Nocera (7):
configure.ac: Update requirement to glib 2.34
client: Use g_clear_pointer() to clean up menus
client: Split installing submenu and doing I/O
client: Install submenus before contacting bluez daemon
shell: Document "mode" variable
shared/shell: Add function to handle early help calls
client: Fix --help hanging if bluetoothd is not running

 acinclude.m4       |  4 ++--
 client/admin.c     |  8 +++++---
 client/admin.h     |  1 +
 client/assistant.c |  8 +++++---
 client/assistant.h |  1 +
 client/main.c      | 14 ++++++++++----
 client/mgmt.c      | 11 +++++++----
 client/mgmt.h      |  3 ++-
 client/player.c    |  7 +++++--
 client/player.h    |  1 +
 configure.ac       |  2 +-
 src/shared/shell.c | 12 +++++++++++-
 src/shared/shell.h |  2 ++
 tools/btmgmt.c     |  3 ++-
 14 files changed, 55 insertions(+), 22 deletions(-)
Signature · 1 line
2.47.0
#1 lore raw

PATCH v3 1/7 configure.ac: Update requirement to glib 2.34

Update build requirements from 2011's glib 2.28 all the way up to 2012's glib 2.34. This will allow us to use g_clear_pointer().

---
 acinclude.m4 | 4 ++--
 configure.ac | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
acinclude.m4 +2 -2
index 4b73a5bfc38f..d0bfe6ccb2fa 100644
@@ -62,8 +62,8 @@ AC_DEFUN([COMPILER_FLAGS], [
 		with_cflags="$with_cflags -Wswitch-enum"
 		with_cflags="$with_cflags -Wformat -Wformat-security"
 		with_cflags="$with_cflags -DG_DISABLE_DEPRECATED"
-		with_cflags="$with_cflags -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_28"
-		with_cflags="$with_cflags -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_32"
+		with_cflags="$with_cflags -DGLIB_VERSION_MIN_REQUIRED=GLIB_VERSION_2_34"
+		with_cflags="$with_cflags -DGLIB_VERSION_MAX_ALLOWED=GLIB_VERSION_2_34"
 	fi
 	AC_SUBST([WARNING_CFLAGS], $with_cflags)
 ])
configure.ac +1 -1
index 7093e41ff714..14bd15293918 100644
@@ -81,7 +81,7 @@ AC_CHECK_DECLS([basename], [],
 				 ])
 
 
-PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.28)
+PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.34)
 
 if (test "${enable_threads}" = "yes"); then
 	AC_DEFINE(NEED_THREADS, 1, [Define if threading support is required])
Signature · 1 line
2.47.0
#2 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=901846

---Test result---

Test Summary:

CheckPatch                    FAIL      3.54 seconds
GitLint                       PASS      2.41 seconds
BuildEll                      PASS      24.73 seconds
BluezMake                     PASS      1737.88 seconds
MakeCheck                     PASS      13.46 seconds
MakeDistcheck                 PASS      180.52 seconds
CheckValgrind                 PASS      254.65 seconds
CheckSmatch                   WARNING   358.75 seconds
bluezmakeextell               PASS      121.22 seconds
IncrementalBuild              PASS      11429.59 seconds
ScanBuild                     PASS      1028.62 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[BlueZ,v3,2/7] client: Use g_clear_pointer() to clean up menus ERROR:INITIALISED_STATIC: do not initialise statics to NULL #66: FILE: client/admin.c:194:
+static GDBusClient *client = NULL;

ERROR:SPACING: need consistent spacing around '*' (ctx:WxV) #66: FILE: client/admin.c:194:

+static GDBusClient *client = NULL;
                    ^

ERROR:INITIALISED_STATIC: do not initialise statics to NULL #87: FILE: client/assistant.c:393:
+static GDBusClient * client = NULL;

ERROR:POINTER_LOCATION: "foo * bar" should be "foo *bar" #87: FILE: client/assistant.c:393:
+static GDBusClient * client = NULL;

ERROR:INITIALISED_STATIC: do not initialise statics to NULL #109: FILE: client/player.c:5697:
+static GDBusClient *client = NULL;

/github/workspace/src/src/13845731.patch total: 5 errors, 0 warnings, 46 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/github/workspace/src/src/13845731.patch has style problems, please review.

NOTE: Ignored message types: COMMIT_MESSAGE COMPLEX_MACRO CONST_STRUCT FILE_PATH_CHANGES MISSING_SIGN_OFF PREFER_PACKED SPDX_LICENSE_TAG SPLIT_STRING SSCANF_TO_KSTRTO

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):

---
Regards,
Linux Bluetooth

#3 lore raw

PATCH v3 2/7 client: Use g_clear_pointer() to clean up menus

In reply to Bastien Nocera

This would avoid warnings should the client be NULL.

---
 client/admin.c     | 5 ++---
 client/assistant.c | 5 ++---
 client/player.c    | 4 ++--
 3 files changed, 6 insertions(+), 8 deletions(-)
client/admin.c +2 -3
index cd9af6f955da..9d48867bc1d7 100644
@@ -191,7 +191,7 @@ static void proxy_removed(GDBusProxy *proxy, void *user_data)
 		admin_policy_status_removed(proxy);
 }
 
-static GDBusClient *client;
+static GDBusClient *client = NULL;
 
 static void disconnect_handler(DBusConnection *connection, void *user_data)
 {
@@ -215,6 +215,5 @@ void admin_add_submenu(void)
 
 void admin_remove_submenu(void)
 {
-	g_dbus_client_unref(client);
-	client = NULL;
+	g_clear_pointer(&client, g_dbus_client_unref);
 }
client/assistant.c +2 -3
index 16e94664a5c3..94052e26fd59 100644
@@ -390,7 +390,7 @@ static const struct bt_shell_menu assistant_menu = {
 	{} },
 };
 
-static GDBusClient * client;
+static GDBusClient * client = NULL;
 
 void assistant_add_submenu(void)
 {
@@ -409,7 +409,6 @@ void assistant_add_submenu(void)
 
 void assistant_remove_submenu(void)
 {
-	g_dbus_client_unref(client);
-	client = NULL;
+	g_clear_pointer(&client, g_dbus_client_unref);
 }
 
client/player.c +2 -2
index 188378175486..dea5922d56db 100644
@@ -5694,7 +5694,7 @@ static const struct bt_shell_menu transport_menu = {
 	{} },
 };
 
-static GDBusClient *client;
+static GDBusClient *client = NULL;
 
 void player_add_submenu(void)
 {
@@ -5715,6 +5715,6 @@ void player_add_submenu(void)
 
 void player_remove_submenu(void)
 {
-	g_dbus_client_unref(client);
+	g_clear_pointer(&client, g_dbus_client_unref);
 	queue_destroy(ios, transport_free);
 }
Signature · 1 line
2.47.0
#4 lore raw

PATCH v3 3/7 client: Split installing submenu and doing I/O

In reply to Bastien Nocera

Split off installing the command's submenu and contacting the management socket or the bluez daemon.

---
 client/admin.c     |  3 +++
 client/admin.h     |  1 +
 client/assistant.c |  3 +++
 client/assistant.h |  1 +
 client/main.c      |  4 ++++
 client/mgmt.c      | 11 +++++++----
 client/mgmt.h      |  3 ++-
 client/player.c    |  3 +++
 client/player.h    |  1 +
 tools/btmgmt.c     |  3 ++-
 10 files changed, 27 insertions(+), 6 deletions(-)
client/admin.c +3 -0
index 9d48867bc1d7..15759ba0291a 100644
@@ -202,7 +202,10 @@ static void disconnect_handler(DBusConnection *connection, void *user_data)
 void admin_add_submenu(void)
 {
 	bt_shell_add_submenu(&admin_menu);
+}
 
+void admin_enable_submenu(void)
+{
 	dbus_conn = bt_shell_get_env("DBUS_CONNECTION");
 	if (!dbus_conn || client)
 		return;
client/admin.h +1 -0
index 4da83e4e36ba..68a497971f66 100644
@@ -18,4 +18,5 @@
  */
 
 void admin_add_submenu(void);
+void admin_enable_submenu(void);
 void admin_remove_submenu(void);
client/assistant.c +3 -0
index 94052e26fd59..ceade046be68 100644
@@ -395,7 +395,10 @@ static GDBusClient * client = NULL;
 void assistant_add_submenu(void)
 {
 	bt_shell_add_submenu(&assistant_menu);
+}
 
+void assistant_enable_submenu(void)
+{
 	dbus_conn = bt_shell_get_env("DBUS_CONNECTION");
 	if (!dbus_conn || client)
 		return;
client/assistant.h +1 -0
index 418b0b84031f..c304abea0515 100644
@@ -9,5 +9,6 @@
  */
 
 void assistant_add_submenu(void);
+void assistant_enable_submenu(void);
 void assistant_remove_submenu(void);
 
client/main.c +4 -0
index 3f8143dde4b8..17af62000f6d 100644
@@ -3204,9 +3204,13 @@ int main(int argc, char *argv[])
 					(void *)endpoint_option);
 
 	admin_add_submenu();
+	admin_enable_submenu();
 	player_add_submenu();
+	player_enable_submenu();
 	mgmt_add_submenu();
+	mgmt_enable_submenu();
 	assistant_add_submenu();
+	assistant_enable_submenu();
 
 	client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
 
client/mgmt.c +7 -4
index 602b92228ab8..aea3d95597c9 100644
@@ -6026,7 +6026,13 @@ static void mgmt_debug(const char *str, void *user_data)
 	print("%s%s", prefix, str);
 }
 
-bool mgmt_add_submenu(void)
+void mgmt_add_submenu(void)
+{
+	bt_shell_add_submenu(&mgmt_menu);
+	bt_shell_add_submenu(&monitor_menu);
+}
+
+bool mgmt_enable_submenu(void)
 {
 	mgmt = mgmt_new_default();
 	if (!mgmt) {
@@ -6034,9 +6040,6 @@ bool mgmt_add_submenu(void)
 		return false;
 	}
 
-	bt_shell_add_submenu(&mgmt_menu);
-	bt_shell_add_submenu(&monitor_menu);
-
 	if (getenv("MGMT_DEBUG"))
 		mgmt_set_debug(mgmt, mgmt_debug, "mgmt: ", NULL);
 
client/mgmt.h +2 -1
index 5a2026eab6a2..b0f3cafd0777 100644
@@ -8,6 +8,7 @@
  *
  */
 
-bool mgmt_add_submenu(void);
+void mgmt_add_submenu(void);
+bool mgmt_enable_submenu(void);
 void mgmt_remove_submenu(void);
 void mgmt_set_index(const char *arg);
client/player.c +3 -0
index dea5922d56db..3189e0575798 100644
@@ -5701,7 +5701,10 @@ void player_add_submenu(void)
 	bt_shell_add_submenu(&player_menu);
 	bt_shell_add_submenu(&endpoint_menu);
 	bt_shell_add_submenu(&transport_menu);
+}
 
+void player_enable_submenu(void)
+{
 	dbus_conn = bt_shell_get_env("DBUS_CONNECTION");
 	if (!dbus_conn || client)
 		return;
client/player.h +1 -0
index e7778cb1efd9..c09ffa94cbff 100644
@@ -9,4 +9,5 @@
  */
 
 void player_add_submenu(void);
+void player_enable_submenu(void);
 void player_remove_submenu(void);
tools/btmgmt.c +2 -1
index 436c2bb21f10..0f6051d5befe 100644
@@ -46,7 +46,8 @@ int main(int argc, char *argv[])
 
 	bt_shell_init(argc, argv, &opt);
 
-	if (!mgmt_add_submenu()) {
+	mgmt_add_submenu();
+	if (!mgmt_enable_submenu()) {
 		fprintf(stderr, "Unable to open mgmt_socket\n");
 		return EXIT_FAILURE;
 	}
Signature · 1 line
2.47.0
#5 lore raw

PATCH v3 4/7 client: Install submenus before contacting bluez daemon

In reply to Bastien Nocera

So that the submenus are installed even if bluez isn't available.

---
 client/main.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
client/main.c +4 -4
index 17af62000f6d..f60bef1a6d3a 100644
@@ -3187,6 +3187,10 @@ int main(int argc, char *argv[])
 	bt_shell_add_submenu(&advertise_monitor_menu);
 	bt_shell_add_submenu(&scan_menu);
 	bt_shell_add_submenu(&gatt_menu);
+	admin_add_submenu();
+	player_add_submenu();
+	mgmt_add_submenu();
+	assistant_add_submenu();
 	bt_shell_set_prompt(PROMPT_OFF, NULL);
 
 	if (agent_option)
@@ -3203,13 +3207,9 @@ int main(int argc, char *argv[])
 		bt_shell_set_env("AUTO_REGISTER_ENDPOINT",
 					(void *)endpoint_option);
 
-	admin_add_submenu();
 	admin_enable_submenu();
-	player_add_submenu();
 	player_enable_submenu();
-	mgmt_add_submenu();
 	mgmt_enable_submenu();
-	assistant_add_submenu();
 	assistant_enable_submenu();
 
 	client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
Signature · 1 line
2.47.0
#6 lore raw

PATCH v3 5/7 shell: Document "mode" variable

In reply to Bastien Nocera

It's unclear what it does, could probably do with being replaced with an enum to make its possible values and meaning clearer.

---
 src/shared/shell.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/shared/shell.c +1 -1
index a8ad956c7948..2f83717b2d39 100644
@@ -71,7 +71,7 @@ static struct {
 	char history[256];
 	int argc;
 	char **argv;
-	bool mode;
+	bool mode; /* whether non-interactive */
 	bool zsh;
 	bool monitor;
 	int timeout;
Signature · 1 line
2.47.0
#7 lore raw

PATCH v3 6/7 shared/shell: Add function to handle early help calls

In reply to Bastien Nocera

Add a function that would allow tools to exit after handling --help, so as to avoid the daemon waiting to communicate with a D-Bus service that might not be running.

---
 src/shared/shell.c | 10 ++++++++++
 src/shared/shell.h |  2 ++
 2 files changed, 12 insertions(+)
src/shared/shell.c +10 -0
index 2f83717b2d39..88cc7df2d565 100644
@@ -1697,3 +1697,13 @@ int bt_shell_get_timeout(void)
 {
 	return data.timeout;
 }
+
+void bt_shell_handle_non_interactive_help(void)
+{
+	if (!data.mode)
+		return;
+	if (data.argv[0] != cmplt)
+		return;
+	print_cmds();
+	exit(EXIT_SUCCESS);
+}
src/shared/shell.h +2 -0
index e431db9f5821..eebbc71faffb 100644
@@ -60,6 +60,8 @@ int bt_shell_exec(const char *input);
 void bt_shell_quit(int status);
 void bt_shell_noninteractive_quit(int status);
 
+void bt_shell_handle_non_interactive_help(void);
+
 bool bt_shell_set_menu(const struct bt_shell_menu *menu);
 
 bool bt_shell_add_submenu(const struct bt_shell_menu *menu);
Signature · 1 line
2.47.0
#8 lore raw

PATCH v3 7/7 client: Fix --help hanging if bluetoothd is not running

In reply to Bastien Nocera

Exit after printing all the main and submenu commands.

---
 client/main.c | 2 ++
 1 file changed, 2 insertions(+)
client/main.c +2 -0
index f60bef1a6d3a..f5ed9f9f5297 100644
@@ -3193,6 +3193,8 @@ int main(int argc, char *argv[])
 	assistant_add_submenu();
 	bt_shell_set_prompt(PROMPT_OFF, NULL);
 
+	bt_shell_handle_non_interactive_help();
+
 	if (agent_option)
 		auto_register_agent = g_strdup(agent_option);
 	else
Signature · 1 line
2.47.0
#9 lore raw

Re: [BlueZ v3 7/7] client: Fix --help hanging if bluetoothd is not running

Hi Bastien,

Quoted 20 lines · Bastien Nocera

Exit after printing all the main and submenu commands.

---
 client/main.c | 2 ++
 1 file changed, 2 insertions(+)
client/main.c +2 -1
index f60bef1a6d3a..f5ed9f9f5297 100644
@@ -3193,6 +3193,8 @@ int main(int argc, char *argv[])
        assistant_add_submenu();
        bt_shell_set_prompt(PROMPT_OFF, NULL);
 
+       bt_shell_handle_non_interactive_help();
+
        if (agent_option)
                auto_register_agent = g_strdup(agent_option);
        else
--
Quoted 2 lines

2.47.0

Having some thoughts about how to do this is more clean way, perhaps we should do this as part of bt_shell_run and then introduce .run callback to bt_shell_menu so it is called as part of bt_shell_run, under the .run callback the menu can place e.g. DBUS connection setup, etc, but before it reaches that we can check if it just a help pending.

Signature · 1 line
Luiz Augusto von Dentz
#10 lore raw

Re: [BlueZ v3 7/7] client: Fix --help hanging if bluetoothd is not running

Quoted 18 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 14 lines · Bastien Nocera

Exit after printing all the main and submenu commands.
---
 client/main.c | 2 ++

 1 file changed, 2 insertions(+)
client/main.c +0 -0
index f60bef1a6d3a..f5ed9f9f5297 100644
@@ -3193,6 +3193,8 @@ int main(int argc, char *argv[])
         assistant_add_submenu();
         bt_shell_set_prompt(PROMPT_OFF, NULL);
 
Quoted 16 lines
Quoted 8 lines
client/main.c +2 -0
+       bt_shell_handle_non_interactive_help();
+
         if (agent_option)
                 auto_register_agent = g_strdup(agent_option);

        else
--
2.47.0

Having some thoughts about how to do this is more clean way, perhaps we should do this as part of bt_shell_run and then introduce .run callback to bt_shell_menu so it is called as part of bt_shell_run, under the .run callback the menu can place e.g. DBUS connection setup,
etc, but before it reaches that we can check if it just a help pending.

We need to:
1) set the top menu
2) populate the submenus without any D-Bus IO
3) turn off the prompt
4) print the help and exit if that's what was requested
5) setup IO for submenus
6) run the mainloop

I guess I could do 2) with a callback from bt_shell_set_menu() and then do 4) and 5) from a bt_shell_run() callback.

Is that what you expected?
Do you have preferred names for the callback functions?

I think that we can still use the function split from 3/7, did you have a better name for the functions?

Cheers

#11 lore raw

Re: [BlueZ v3 7/7] client: Fix --help hanging if bluetoothd is not running

Hi Bastien,

Quoted 45 lines · Bastien Nocera
Quoted 18 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 14 lines · Bastien Nocera

Exit after printing all the main and submenu commands.

---
 client/main.c | 2 ++
 1 file changed, 2 insertions(+)
client/main.c +0 -0
index f60bef1a6d3a..f5ed9f9f5297 100644
@@ -3193,6 +3193,8 @@ int main(int argc, char *argv[])
        assistant_add_submenu();
        bt_shell_set_prompt(PROMPT_OFF, NULL);
 
Quoted 16 lines
Quoted 8 lines
client/main.c +2 -1
+       bt_shell_handle_non_interactive_help();
+
        if (agent_option)
                auto_register_agent = g_strdup(agent_option);
        else
--

2.47.0

Having some thoughts about how to do this is more clean way, perhaps we should do this as part of bt_shell_run and then introduce .run callback to bt_shell_menu so it is called as part of bt_shell_run, under the .run callback the menu can place e.g. DBUS connection setup,
etc, but before it reaches that we can check if it just a help pending.

We need to:
1) set the top menu
2) populate the submenus without any D-Bus IO
3) turn off the prompt
4) print the help and exit if that's what was requested
5) setup IO for submenus
6) run the mainloop

Quoted 2 lines

I guess I could do 2) with a callback from bt_shell_set_menu() and then do 4) and 5) from a bt_shell_run() callback.

Yep.

Quoted 2 lines

Is that what you expected?
Do you have preferred names for the callback functions?

Id call it .run since it should be the result of bt_shell_run

Quoted 2 lines

I think that we can still use the function split from 3/7, did you have a better name for the functions?

Not sure I follow, there will be a split between adding submenus and .run to achieve the 2 stages, or are you talking about some other split?

Quoted 2 lines

Cheers

Signature · 1 line
Luiz Augusto von Dentz
#12 lore raw

Re: [BlueZ v3 7/7] client: Fix --help hanging if bluetoothd is not running

Quoted 62 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 48 lines · Bastien Nocera
Quoted 29 lines · Luiz Augusto von Dentz

Hi Bastien,

Quoted 18 lines · Bastien Nocera

Exit after printing all the main and submenu commands.
---
 client/main.c | 2 ++

 1 file changed, 2 insertions(+)
client/main.c +2 -0
index f60bef1a6d3a..f5ed9f9f5297 100644
@@ -3193,6 +3193,8 @@ int main(int argc, char *argv[])
         assistant_add_submenu();
         bt_shell_set_prompt(PROMPT_OFF, NULL);
 
+       bt_shell_handle_non_interactive_help();
+
         if (agent_option)
                 auto_register_agent = g_strdup(agent_option);
Quoted 4 lines

        else
--
2.47.0

Having some thoughts about how to do this is more clean way,

Quoted 8 lines

perhaps
we should do this as part of bt_shell_run and then introduce .run callback to bt_shell_menu so it is called as part of bt_shell_run,
under the .run callback the menu can place e.g. DBUS connection setup,
etc, but before it reaches that we can check if it just a help pending.

We need to:
1) set the top menu
2) populate the submenus without any D-Bus IO
3) turn off the prompt
4) print the help and exit if that's what was requested
5) setup IO for submenus
6) run the mainloop

Quoted 2 lines

I guess I could do 2) with a callback from bt_shell_set_menu() and then do 4) and 5) from a bt_shell_run() callback.

Yep.

Quoted 2 lines

Is that what you expected?
Do you have preferred names for the callback functions?

Id call it .run since it should be the result of bt_shell_run

Quoted 7 lines
Quoted 3 lines

I think that we can still use the function split from 3/7, did you have
a better name for the functions?

Not sure I follow, there will be a split between adding submenus and .run to achieve the 2 stages, or are you talking about some other split?

We still need to split the functions called in .run between the bits that populate the submenu, and the bits that will make D-Bus I/O, so patch number 3 still needs to be exist.

Is assistant_add_submenu() and assistant_enable_submenu() OK, or did you want a different name?