Skip to contentSkip to conversation

Invalid file descriptor gatttool of bluez 5.32

DISCUSSION Tobias Preclik 2 participants 3 messages Jul 27, 2015 – Jul 7, 2016 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

Hello,

it seems the cleanup process when disconnecting from a bluetooth LE device changed in bluez 5.32 but gatttool (attrib/interactive.c:disconnect_io) wasn't updated.

$ ./gatttool -I
[                 ][LE]> connect FF:A7:8C:98:46:B6 random
Attempting to connect to FF:A7:8C:98:46:B6
Connection successful
[FF:A7:8C:98:46:B6][LE]> disconnect

(gatttool:20949): GLib-WARNING **: Invalid file descriptor.

[FF:A7:8C:98:46:B6][LE]> exit

What's the right way to do it in gatttool/bluez 5.32?

Best regards,
Tobias

#1 lore raw

Hi Tobias,

Quoted 12 lines · Tobias Preclik

Hello,

it seems the cleanup process when disconnecting from a bluetooth LE device changed in bluez 5.32 but gatttool (attrib/interactive.c:disconnect_io) wasn't updated.

$ ./gatttool -I
[                 ][LE]> connect FF:A7:8C:98:46:B6 random
Attempting to connect to FF:A7:8C:98:46:B6
Connection successful
[FF:A7:8C:98:46:B6][LE]> disconnect
Quoted 5 lines

(gatttool:20949): GLib-WARNING **: Invalid file descriptor.

[FF:A7:8C:98:46:B6][LE]> exit

What's the right way to do it in gatttool/bluez 5.32?

There are new tools to use with GATT, bluetoothctl/bluetoothd is the preferred since with that you have GAP, etc, but if want to use a stand alone tool then I suggest you use btgatt-client.

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

Hello Luiz et al.,

thanks for your answer and sorry for digging that thread up again. At least for the reference:

The invalid file descriptor warning stems from the g_io_channel_shutdown call in attrib/interactive.c:disconnect_io. Afaict the g_io_channel_shutdown call is redundant and thus causes the glib warning. When establishing the connection gatttool uses approximately the following call sequence.

attrib/utils.c:gatt_connect

Quoted 2 lines

btio/btio.c:bt_io_connect
btio/btio.c:create_io

       > **socket**
    > btio/btio.c:l2cap_connect
       > **connect**

In btio/btio.c:create_io the socket is created, a corresponding glib IO channel is created in a call to g_io_channel_unix_new and the IO channel is instructed to close the socket as soon as the last IO channel reference is dropped in a call to g_io_channel_set_close_on_unref.

When disconnecting in gatttool, the last reference to the glib IO channel seems to be dropped somewhere at the end of the following call sequence (after the last reference to the ATT object is dropped in bt_att_unref):

attrib/interactive.c:disconnect_io

Quoted 2 lines

attrib/gattrib.c:g_attrib_unref
src/shared/att.c:bt_att_unref

       > src/shared/att.c:bt_att_free
          > ...

In a nutshell: Afaict the g_io_channel_unref call in disconnect_io is redundant since the last IO channel reference is dropped in g_attrib_unref before this call. Since the IO channel is instructed to close the socket when the last reference is dropped, the call to g_io_channel_shutdown is also redundant (see attached patch). Applying the patch will indeed get rid of the glib warning. Can somebody second that this the correct way of fixing the cleanup sequence?

Regards,
Tobias

Quoted 18 lines · Luiz Augusto von Dentz

Hi Tobias,

Quoted 14 lines · Tobias Preclik

Hello,

it seems the cleanup process when disconnecting from a bluetooth LE device changed in bluez 5.32 but gatttool (attrib/interactive.c:disconnect_io) wasn't updated.

$ ./gatttool -I
[                 ][LE]> connect FF:A7:8C:98:46:B6 random
Attempting to connect to FF:A7:8C:98:46:B6
Connection successful
[FF:A7:8C:98:46:B6][LE]> disconnect

(gatttool:20949): GLib-WARNING **: Invalid file descriptor.

Quoted 9 lines
Quoted 3 lines

[FF:A7:8C:98:46:B6][LE]> exit

What's the right way to do it in gatttool/bluez 5.32?

There are new tools to use with GATT, bluetoothctl/bluetoothd is the preferred since with that you have GAP, etc, but if want to use a stand alone tool then I suggest you use btgatt-client.

diff -crB bluez-5.40-orig/attrib/interactive.c bluez-5.40/attrib/interactive.c
*** bluez-5.40-orig/attrib/interactive.c 2016-07-07 15:20:13.955103280 +0200
--- bluez-5.40/attrib/interactive.c 2016-07-07 15:20:23.170926414 +0200
***************
*** 191,199 ****
g_attrib_unref(attrib);
attrib = NULL;
opt_mtu = 0;
-
- g_io_channel_shutdown(iochannel, FALSE, NULL);
- g_io_channel_unref(iochannel);
iochannel = NULL;

set_state(STATE_DISCONNECTED);
--- 191,196 ----