Skip to contentSkip to conversation

obexd: unregister profiles when the user is inactive

PATCH Applied Andrew Sayers 4 participants 12 messages Apr 30, 2025 – May 26, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v6 cover obexd: unregister profiles when the user is inactive

This is a follow-up to "obexd: only run one instance at once". Instead of refusing to run parallel services, it unregisters profiles when the user is inactive. This avoids the need for tmpfiles, and avoids issues where the user with the obex service logs out, leaving obex disabled altogether.

Luiz previously suggested moving this to systemd, but I haven't had much luck getting the systemd devs to accept changes, and Pauli's mention of elogind (i.e. logind without systemd) suggests it's probably better to avoid the dependency anyway.

I considered writing a separate D-Bus service that would notify you when the session became (in)active, but D-Bus doesn't have particularly strong guarantees about how long messages take to deliver, which could lead to race conditions between instances on overloaded systems.

I also considered writing some kind of library, but there's not much code to deduplicate, and most of it would need to be reworked for every service that uses it. So I wrote a gist for people to copy/paste:

https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

---

v1 -> v2 open/close private DBus connections instead of
calling UnregisterProfile (thanks Pauli Virtanen)
Add obex_setup_dbus_connection_private() (to enable the above) Fix CI errors
Minor issues I missed last time...
s/regster/register/ in e-mail subject lines s/login_.*_cb/logind_$1_cb/g in logind.[ch]
remove watches on exit in pbap.c and bluetooth.c
V2 -> V3 Fix checkpatch.pl errors - thanks Luiz
Note: false positive warning about commit #4 is still present - this URL can't be split or shortened without causing issues.
V3 -> V4 Fix various bugs - thanks Pauli
Split timeout_handler() into a separate patch - see above
V4 -> V5 Add #include <stdlib.h> based on CI tests - thanks Luiz
Update the timeout on event_handler calls (and handle a couple of
edge cases that could have misbehaved before) - thanks Pauli
Discuss how the API assumes poll() in the penultimate
commit message - thanks Pauli
V5 -> V6 Remove commits that were upstreamed in v5 (thanks Luiz)
Replace dummy macros with inline functions in logind.h
(silences some checkpatch.pl warnings, function solution recommended by Luiz)

Andrew Sayers (3):
obexd: Unregister profiles when the user is inactive
obexd: Support sd_login_monitor_get_timeout() Revert "obexd: only run one instance at once"

 Makefile.obexd            |  10 ++
 obexd/client/pbap.c       |  17 ++-
 obexd/plugins/bluetooth.c |  14 ++-
 obexd/src/logind.c        | 282 ++++++++++++++++++++++++++++++++++++++++++++++
 obexd/src/logind.h        |  37 ++++++
 obexd/src/main.c          |   4 +
 obexd/src/obex.service.in |   9 --
 7 files changed, 359 insertions(+), 14 deletions(-)
#1 lore raw

PATCH v6 1/3 obexd: Unregister profiles when the user is inactive

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

---
 Makefile.obexd            |  10 ++
 obexd/client/pbap.c       |  17 ++-
 obexd/plugins/bluetooth.c |  14 ++-
 obexd/src/logind.c        | 239 ++++++++++++++++++++++++++++++++++++++
 obexd/src/logind.h        |  37 ++++++
 obexd/src/main.c          |   4 +
 6 files changed, 316 insertions(+), 5 deletions(-)
 create mode 100644 obexd/src/logind.c
 create mode 100644 obexd/src/logind.h
Makefile.obexd +10 -0
index 74dd977a0..b59cfaf8f 100644
@@ -67,6 +67,7 @@ obexd_src_obexd_SOURCES = $(btio_sources) $(gobex_sources) \
 			obexd/src/main.c obexd/src/obexd.h \
 			obexd/src/plugin.h obexd/src/plugin.c \
 			obexd/src/log.h obexd/src/log.c \
+			obexd/src/logind.h obexd/src/logind.c \
 			obexd/src/manager.h obexd/src/manager.c \
 			obexd/src/obex.h obexd/src/obex.c obexd/src/obex-priv.h \
 			obexd/src/mimetype.h obexd/src/mimetype.c \
@@ -96,6 +97,8 @@ obexd_src_obexd_LDADD = lib/libbluetooth-internal.la \
 
 if EXTERNAL_PLUGINS
 obexd_src_obexd_LDFLAGS = $(AM_LDFLAGS) -Wl,--export-dynamic
+else
+obexd_src_obexd_LDFLAGS =
 endif
 
 obexd_src_obexd_CPPFLAGS = $(AM_CPPFLAGS) $(GLIB_CFLAGS) $(DBUS_CFLAGS) \
@@ -109,6 +112,13 @@ obexd-add-service-symlink:
 obexd-remove-service-symlink:
 endif
 
+if OBEX
+if SYSTEMD
+obexd_src_obexd_CPPFLAGS += -DSYSTEMD
+obexd_src_obexd_LDFLAGS += -lsystemd
+endif
+endif
+
 obexd_src_obexd_SHORTNAME = obexd
 
 obexd_builtin_files = obexd/src/builtin.h $(obexd_builtin_nodist)
obexd/client/pbap.c +14 -3
index 90f8bdc02..51b523592 100644
@@ -27,6 +27,7 @@
 #include "gdbus/gdbus.h"
 
 #include "obexd/src/log.h"
+#include "obexd/src/logind.h"
 #include "obexd/src/obexd.h"
 
 #include "transfer.h"
@@ -1454,13 +1455,13 @@ static struct obc_driver pbap = {
 	.remove = pbap_remove
 };
 
-int pbap_init(void)
+static int pbap_init_cb(void)
 {
 	int err;
 
 	DBG("");
 
-	conn = obex_get_dbus_connection();
+	conn = obex_setup_dbus_connection_private(NULL, NULL);
 	if (!conn)
 		return -EIO;
 
@@ -1481,7 +1482,7 @@ int pbap_init(void)
 	return 0;
 }
 
-void pbap_exit(void)
+static void pbap_exit_cb(void)
 {
 	DBG("");
 
@@ -1496,9 +1497,19 @@ void pbap_exit(void)
 	}
 
 	if (conn) {
+		dbus_connection_close(conn);
 		dbus_connection_unref(conn);
 		conn = NULL;
 	}
 
 	obc_driver_unregister(&pbap);
 }
+
+int pbap_init(void)
+{
+	return logind_register(pbap_init_cb, pbap_exit_cb);
+}
+void pbap_exit(void)
+{
+	return logind_unregister(pbap_init_cb, pbap_exit_cb);
+}
obexd/plugins/bluetooth.c +12 -2
index 8cf718922..7ff27a8a8 100644
@@ -35,6 +35,7 @@
 #include "obexd/src/transport.h"
 #include "obexd/src/service.h"
 #include "obexd/src/log.h"
+#include "obexd/src/logind.h"
 
 #define BT_RX_MTU 32767
 #define BT_TX_MTU 32767
@@ -426,7 +427,7 @@ static const struct obex_transport_driver driver = {
 
 static unsigned int listener_id = 0;
 
-static int bluetooth_init(void)
+static int bluetooth_init_cb(void)
 {
 	connection = g_dbus_setup_private(DBUS_BUS_SYSTEM, NULL, NULL);
 	if (connection == NULL)
@@ -438,7 +439,7 @@ static int bluetooth_init(void)
 	return obex_transport_driver_register(&driver);
 }
 
-static void bluetooth_exit(void)
+static void bluetooth_exit_cb(void)
 {
 	GSList *l;
 
@@ -462,4 +463,13 @@ static void bluetooth_exit(void)
 	obex_transport_driver_unregister(&driver);
 }
 
+static int bluetooth_init(void)
+{
+	return logind_register(bluetooth_init_cb, bluetooth_exit_cb);
+}
+static void bluetooth_exit(void)
+{
+	return logind_unregister(bluetooth_init_cb, bluetooth_exit_cb);
+}
+
 OBEX_PLUGIN_DEFINE(bluetooth, bluetooth_init, bluetooth_exit)
obexd/src/logind.c new file +239 -0

239 lines not on this page · raw message

obexd/src/logind.h new file +37 -0
new file mode 100644
index 000000000..eb3ff8d7b
@@ -0,0 +1,37 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ *
+ *  Enable functionality only when the user is active
+ *
+ *  Copyright (C) 2007-2010  Marcel Holtmann <[email protected]>
+ *
+ *
+ */
+
+typedef int (*logind_init_cb)(void);
+typedef void (*logind_exit_cb)(void);
+
+#ifdef SYSTEMD
+
+int logind_register(logind_init_cb init_cb, logind_exit_cb exit_cb);
+void logind_unregister(logind_init_cb init_cb, logind_exit_cb exit_cb);
+int logind_set(gboolean enabled);
+
+#else
+
+static inline int logind_register(logind_init_cb init_cb,
+					logind_exit_cb exit_cb)
+{
+	return init_cb();
+}
+static inline void logind_unregister(logind_init_cb init_cb,
+					logind_exit_cb exit_cb)
+{
+	return exit_cb();
+}
+static inline int logind_set(gboolean enabled)
+{
+	return 0;
+}
+
+#endif
obexd/src/main.c +4 -0
index ca95a70de..df150973e 100644
@@ -35,6 +35,7 @@
 #include "../client/manager.h"
 
 #include "log.h"
+#include "logind.h"
 #include "obexd.h"
 #include "server.h"
 
@@ -283,6 +284,9 @@ int main(int argc, char *argv[])
 
 	__obex_log_init(option_debug, option_detach);
 
+	if (option_system_bus)
+		logind_set(FALSE);
+
 	DBG("Entering main loop");
 
 	main_loop = g_main_loop_new(NULL, FALSE);
Signature · 1 line
2.49.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=958531

---Test result---

Test Summary:

CheckPatch                    PENDING   0.33 seconds
GitLint                       PENDING   0.22 seconds
BuildEll                      PASS      21.34 seconds
BluezMake                     PASS      3010.56 seconds
MakeCheck                     PASS      20.59 seconds
MakeDistcheck                 PASS      207.85 seconds
CheckValgrind                 PASS      286.19 seconds
CheckSmatch                   PASS      317.67 seconds
bluezmakeextell               PASS      136.41 seconds
IncrementalBuild              PENDING   0.25 seconds
ScanBuild                     PASS      968.58 seconds

Details
##############################
Test: CheckPatch - PENDING
Desc: Run checkpatch.pl script
Output:

##############################
Test: GitLint - PENDING
Desc: Run gitlint
Output:

##############################
Test: IncrementalBuild - PENDING
Desc: Incremental build with the patches in the series Output:

---
Regards,
Linux Bluetooth

#3 lore raw

Re: [PATCH BlueZ v6 1/3] obexd: Unregister profiles when the user is inactive

In reply to Andrew Sayers

Hi Andrew,

Quoted 29 lines · Andrew Sayers

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Quoted 6 lines

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

I have a problem to connect PBAP profile using obexctl, using upstream bluetoothd and obexd built on Ubuntu 24.04, the org.bluez.obex.PhonebookAccess1 interface doesn't appear.

After bisecting I found that this commit cause this issue.

Signature · 6 lines
Frédéric Danis
Senior Software Engineer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom
Registered in England & Wales, no. 5513718
#4 lore raw

Re: [PATCH BlueZ v6 1/3] obexd: Unregister profiles when the user is inactive

Quoted 37 lines · Frédéric Danis

Hi Andrew,

Quoted 33 lines · Andrew Sayers

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

Quoted 8 lines
Quoted 2 lines

I have a problem to connect PBAP profile using obexctl, using upstream bluetoothd and obexd built on Ubuntu 24.04, the org.bluez.obex.PhonebookAccess1 interface doesn't appear.

After bisecting I found that this commit cause this issue.

Hmm, my guess is that seat detection isn't working properly. Could you try this gist? You should get "Active: 1" when it starts, "Active: 0" when you switch away from your current screen, and "Active: 1" when you switch back.

https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

Quoted 10 lines
Signature · 7 lines
-- 
Frédéric Danis
Senior Software Engineer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom
Registered in England & Wales, no. 5513718
#5 lore raw

Re: [PATCH BlueZ v6 1/3] obexd: Unregister profiles when the user is inactive

Hi Andrew,

Quoted 44 lines · Andrew Sayers
Quoted 43 lines · Frédéric Danis

Hi Andrew,

Quoted 29 lines · Andrew Sayers

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Quoted 6 lines

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

I have a problem to connect PBAP profile using obexctl, using upstream bluetoothd and obexd built on Ubuntu 24.04, the org.bluez.obex.PhonebookAccess1 interface doesn't appear.

Quoted 8 lines
Quoted 1 line

After bisecting I found that this commit cause this issue.

Hmm, my guess is that seat detection isn't working properly. Could you try this gist? You should get "Active: 1" when it starts, "Active: 0" when you switch away from your current screen, and "Active: 1" when you switch back.

https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

It seems to work:

$ ./user-on-seat
Active: 1
Active: 0
Active: 1

But the problem occurred while I didn't try any "seat" change.
I just started bluetoothd, obexd and obexctl, then tried to connect to my phone and got:

    connect 24:24:B7:11:82:6C pbap
    Attempting to connect to 24:24:B7:11:82:6C
    [NEW] Session /org/bluez/obex/client/session0 [default]
    [24:24:B7:11:82:6C]# Connection successful

While I'm expecting:

    connect 24:24:B7:11:82:6C pbap
    Attempting to connect to 24:24:B7:11:82:6C
    [NEW] Session /org/bluez/obex/client/session0 [default]
    [24:24:B7:11:82:6C]# [NEW] PhonebookAccess /org/bluez/obex/client/session0
    [24:24:B7:11:82:6C]# Connection successful
Signature · 6 lines
Frédéric Danis
Senior Software Engineer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom
Registered in England & Wales, no. 5513718
#6 lore raw

Re: [PATCH BlueZ v6 1/3] obexd: Unregister profiles when the user is inactive

Quoted 56 lines · Frédéric Danis

Hi Andrew,

Quoted 44 lines · Andrew Sayers
Quoted 43 lines · Frédéric Danis

Hi Andrew,

Quoted 29 lines · Andrew Sayers

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Quoted 6 lines

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

I have a problem to connect PBAP profile using obexctl, using upstream bluetoothd and obexd built on Ubuntu 24.04, the org.bluez.obex.PhonebookAccess1 interface doesn't appear.

Quoted 8 lines
Quoted 1 line

After bisecting I found that this commit cause this issue.

Hmm, my guess is that seat detection isn't working properly. Could you try this gist? You should get "Active: 1" when it starts, "Active: 0" when you switch away from your current screen, and "Active: 1" when you switch back.

https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

Quoted 7 lines

It seems to work:

$ ./user-on-seat
Active: 1
Active: 0
Active: 1

But the problem occurred while I didn't try any "seat" change.

Yeah, I was hoping you'd get e.g. 0 / 0 / 1 as output, suggesting it was inactive at first but changing seats worked around the problem. Sorry, but this means it'll be harder to debug. Can you...

1. Check whether PBAP is registered with bluetoothctl:

       bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should see one line, like:

       UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)

2. Tail the bluetooth and obex logs in another terminal:

sudo journalctl -f SYSLOG_IDENTIFIER=bluetoothd SYSLOG_IDENTIFIER=obexd

Steps below will ask you to "make a note" in the log (e.g. append "about to do XYZ" to the file) - this will tell me which messages were triggered by which events.

3. Make a note in the log, then stop the obex service:

       systemctl --user stop obex.service

4. Check PBAP again:

       bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should not see any output.

5. Make another note in the log, then start the obex service:

       systemctl --user start obex.service

6. Check PBAP again:

       bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should see the same as in step 1.

8. Make a final note in the log, then try ctrl+alt+F{7,8}.

9. Send me the full log with notes, or at least anything that jumps out at you

Note: the instructions above only print things that are likely to show problems, and unlikely to contain anything that can't go in an e-mail. You're welcome to do sudo journalctl -f and or bluetoothctl show in another terminal and go looking for trouble :)

Quoted 24 lines

I just started bluetoothd, obexd and obexctl, then tried to connect to my phone and got:

    connect 24:24:B7:11:82:6C pbap
    Attempting to connect to 24:24:B7:11:82:6C
    [NEW] Session /org/bluez/obex/client/session0 [default]
    [24:24:B7:11:82:6C]# Connection successful

While I'm expecting:

    connect 24:24:B7:11:82:6C pbap
    Attempting to connect to 24:24:B7:11:82:6C
    [NEW] Session /org/bluez/obex/client/session0 [default]
    [24:24:B7:11:82:6C]# [NEW] PhonebookAccess /org/bluez/obex/client/session0
    [24:24:B7:11:82:6C]# Connection successful
Signature · 6 lines
Frédéric Danis
Senior Software Engineer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom
Registered in England & Wales, no. 5513718
#7 lore raw

Re: [PATCH BlueZ v6 1/3] obexd: Unregister profiles when the user is inactive

Hi Andrew,

Quoted 63 lines · Andrew Sayers
Quoted 62 lines · Frédéric Danis

Hi Andrew,

Quoted 44 lines · Andrew Sayers
Quoted 43 lines · Frédéric Danis

Hi Andrew,

Quoted 29 lines · Andrew Sayers

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Quoted 6 lines

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

I have a problem to connect PBAP profile using obexctl, using upstream bluetoothd and obexd built on Ubuntu 24.04, the org.bluez.obex.PhonebookAccess1 interface doesn't appear.

Quoted 8 lines
Quoted 1 line

After bisecting I found that this commit cause this issue.

Hmm, my guess is that seat detection isn't working properly. Could you try this gist? You should get "Active: 1" when it starts, "Active: 0" when you switch away from your current screen, and "Active: 1" when you switch back.

https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

It seems to work:

$ ./user-on-seat
Active: 1
Active: 0
Active: 1
Quoted 4 lines
Quoted 1 line

But the problem occurred while I didn't try any "seat" change.

Yeah, I was hoping you'd get e.g. 0 / 0 / 1 as output, suggesting it was inactive at first but changing seats worked around the problem. Sorry, but this means it'll be harder to debug. Can you...

I manually start bluetoothd and obexd, i.e. I stopped them from systemctl (disable and stop) and start them using cli:

$ src/bluetoothd -nd -E
$ obexd/src/obexd -nd
Quoted 7 lines

1. Check whether PBAP is registered with bluetoothctl:

        bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

    You should see one line, like:

        UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)

got:

$ client/bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb
	UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)
Quoted 10 lines

2. Tail the bluetooth and obex logs in another terminal:

sudo journalctl -f SYSLOG_IDENTIFIER=bluetoothd SYSLOG_IDENTIFIER=obexd

Steps below will ask you to "make a note" in the log (e.g. append "about to do XYZ" to the file) - this will tell me which messages were triggered by which events.

3. Make a note in the log, then stop the obex service:

Quoted 7 lines
        systemctl --user stop obex.service

4. Check PBAP again:

bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should not see any output.

Yes, no output

Quoted 9 lines

5. Make another note in the log, then start the obex service:

        systemctl --user start obex.service

6. Check PBAP again:

bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should see the same as in step 1.

Yes:

$ client/bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb
	UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)
Quoted 8 lines

8. Make a final note in the log, then try ctrl+alt+F{7,8}.

9. Send me the full log with notes, or at least anything that jumps out at you

Note: the instructions above only print things that are likely to show problems, and unlikely to contain anything that can't go in an e-mail. You're welcome to do sudo journalctl -f and or bluetoothctl show in another terminal and go looking for trouble :)

I added the logs for pbap connection.

You can find the log at https://gist.github.com/fdanis-oss/eb6fba440c6b07e9ed5a266672bdbcf3

Signature · 6 lines
Frédéric Danis
Senior Software Engineer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom
Registered in England & Wales, no. 5513718
#8 lore raw

Re: [PATCH BlueZ v6 1/3] obexd: Unregister profiles when the user is inactive

Quoted 126 lines · Frédéric Danis

Hi Andrew,

Quoted 63 lines · Andrew Sayers
Quoted 62 lines · Frédéric Danis

Hi Andrew,

Quoted 44 lines · Andrew Sayers
Quoted 43 lines · Frédéric Danis

Hi Andrew,

Quoted 29 lines · Andrew Sayers

Obexd is usually run as a user service, and can exhibit surprising behaviour if two users are logged in at the same time.

Unregister profiles when the user is detected to be off-seat.

It may be impossible to detect whether a user is on-seat in some cases. For example, a version of obexd compiled with systemd support might be run outside of a systemd environment. Warn and leave services registered if that happens.

Obexd can be run as a system service, in which case this check makes no sense. Disable this check when called with --system-bus.

Obexd can also be run by a user that does not have an active session. For example, someone could use ssh to access the system. There might be a use case where someone needs Bluetooth access but can't log in with a keyboard, or there might be a security issue with doing so. This isn't handled explicitly by this patch, but a future patch could add support by calling logind_set(FALSE) in the same way as is currently done with --system-bus.

Unregister profiles by closing private connections instead of sending UnregisterProfile on the shared connection. Pipewire has apparently found the latter to cause long shutdown delays, because bluetoothd may be shutting down and unable to handle this message.

Based in large part on the wireplumber code mentioned by Pauli Virtanen:
https://gitlab.freedesktop.org/pipewire/wireplumber/-/blob/master/modules/module-logind.c#L52

Quoted 6 lines

Other services are likely to need similar functionality, so I have created a gist to demonstrate the basic technique:
https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

I have a problem to connect PBAP profile using obexctl, using upstream bluetoothd and obexd built on Ubuntu 24.04, the org.bluez.obex.PhonebookAccess1 interface doesn't appear.

Quoted 8 lines
Quoted 1 line

After bisecting I found that this commit cause this issue.

Hmm, my guess is that seat detection isn't working properly. Could you try this gist? You should get "Active: 1" when it starts, "Active: 0" when you switch away from your current screen, and "Active: 1" when you switch back.

https://gist.github.com/andrew-sayers/1c4a24f86a9a4c1b1e38d109f1bd1d1e

It seems to work:

$ ./user-on-seat
Active: 1
Active: 0
Active: 1
Quoted 4 lines
Quoted 1 line

But the problem occurred while I didn't try any "seat" change.

Yeah, I was hoping you'd get e.g. 0 / 0 / 1 as output, suggesting it was inactive at first but changing seats worked around the problem. Sorry, but this means it'll be harder to debug. Can you...

I manually start bluetoothd and obexd, i.e. I stopped them from systemctl (disable and stop) and start them using cli:

$ src/bluetoothd -nd -E
$ obexd/src/obexd -nd
Quoted 7 lines

1. Check whether PBAP is registered with bluetoothctl:

        bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

    You should see one line, like:

        UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)

got:

$ client/bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb
	UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)
Quoted 10 lines

2. Tail the bluetooth and obex logs in another terminal:

sudo journalctl -f SYSLOG_IDENTIFIER=bluetoothd SYSLOG_IDENTIFIER=obexd

Steps below will ask you to "make a note" in the log (e.g. append "about to do XYZ" to the file) - this will tell me which messages were triggered by which events.

3. Make a note in the log, then stop the obex service:

Quoted 7 lines
        systemctl --user stop obex.service

4. Check PBAP again:

bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should not see any output.

Yes, no output

Quoted 9 lines

5. Make another note in the log, then start the obex service:

        systemctl --user start obex.service

6. Check PBAP again:

bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb

You should see the same as in step 1.

Yes:

$ client/bluetoothctl show | grep 0000112e-0000-1000-8000-00805f9b34fb
	UUID: Phonebook Access Client   (0000112e-0000-1000-8000-00805f9b34fb)
Quoted 4 lines

8. Make a final note in the log, then try ctrl+alt+F{7,8}.

9. Send me the full log with notes, or at least anything that jumps out at you

Quoted 8 lines
Quoted 4 lines

Note: the instructions above only print things that are likely to show problems, and unlikely to contain anything that can't go in an e-mail. You're welcome to do sudo journalctl -f and or bluetoothctl show in another terminal and go looking for trouble :)

I added the logs for pbap connection.

You can find the log at https://gist.github.com/fdanis-oss/eb6fba440c6b07e9ed5a266672bdbcf3

Quick update:

The above was enough to replicate the bug - thanks!
I'm working on a fix, but am unlikely to have a patch before next week.

Long update:

Changing g_dbus_setup_private() to g_dbus_setup() in pbap.c seems to fix this problem (but introduces other problems). I think obexctl is sending a request that gets received by the normal DBus connection, which no longer routes the message through to PBAP. But I'm not yet confident enough with the codebase to be sure that's what's happening.

If I'm right, the solution is probably to either revisit the idea of (un)registering things in the normal connection, or to listen for messages on PBAP's private connection. But I'm not yet confident enough with DBus to properly assess either solution.

Neither of the above should take a *huge* amount of time, but will need a larger block of focussed attention than I'm likely to have this week. I might get lucky and have a patch by mid-week, but the safer assumption is that it'll be top of next week's todo list.

Quoted 10 lines
Signature · 7 lines
-- 
Frédéric Danis
Senior Software Engineer

Collabora Ltd.
Platinum Building, St John's Innovation Park, Cambridge CB4 0DS, United Kingdom
Registered in England & Wales, no. 5513718
#9 lore raw

PATCH v6 2/3 obexd: Support sd_login_monitor_get_timeout()

In reply to Andrew Sayers

The documentation for sd_login_monitor_get_timeout() implies the API may need to be checked after some time, even if no events have been received via the fd.

In practice, the implementation has always returned a dummy value, and changing it now would cause enough breakage in other projects to make it unlikely to ever happen.

Add a handler for that case, even though it can't currently happen in the real world.

The API assumes we call poll() directly, so in theory it could change the timeout based on some event that doesn't trigger a callback (e.g. sending a signal to the service). It's hard to see how we'd handle that without running a poll() in a separate thread, which would be a lot of complexity for an untestable edge case. Don't try to handle that problem.

---
 obexd/src/logind.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
obexd/src/logind.c +43 -0
index ff2bf3219..a0eb62b1e 100644
@@ -30,6 +30,7 @@ static int uid;
 static gboolean active = FALSE;
 static gboolean monitoring_enabled = TRUE;
 static guint event_source;
+static guint timeout_source;
 
 struct callback_pair {
 	logind_init_cb init_cb;
@@ -82,8 +83,11 @@ static int update(void)
 	return res;
 }
 
+static gboolean timeout_handler(gpointer user_data);
+
 static int check_event(void)
 {
+	uint64_t timeout_usec;
 	int res;
 
 	res = sd_login_monitor_flush(monitor);
@@ -95,6 +99,25 @@ static int check_event(void)
 	if (res < 0)
 		return res;
 
+	res = sd_login_monitor_get_timeout(monitor, &timeout_usec);
+	if (res < 0)
+		return res;
+
+	if (timeout_usec != (uint64_t)-1) {
+		uint64_t time_usec;
+		struct timespec ts;
+		guint interval;
+
+		res = clock_gettime(CLOCK_MONOTONIC, &ts);
+		if (res < 0)
+			return -errno;
+		time_usec = (uint64_t) ts.tv_sec * 1000000 + ts.tv_nsec / 1000;
+		if (time_usec > timeout_usec)
+			return check_event();
+		interval = (timeout_usec - time_usec + 999) / 1000;
+		timeout_source = g_timeout_add(interval, timeout_handler, NULL);
+	}
+
 	return 0;
 }
 
@@ -104,6 +127,11 @@ static gboolean event_handler(GIOChannel *source, GIOCondition condition,
 {
 	int res;
 
+	if (timeout_source) {
+		g_source_remove(timeout_source);
+		timeout_source = 0;
+	}
+
 	res = check_event();
 	if (res) {
 		error("%s: %s", __func__, strerror(-res));
@@ -113,6 +141,17 @@ static gboolean event_handler(GIOChannel *source, GIOCondition condition,
 	return TRUE;
 }
 
+static gboolean timeout_handler(gpointer user_data)
+{
+	int res;
+
+	res = check_event();
+	if (res)
+		error("%s: %s", __func__, strerror(-res));
+
+	return FALSE;
+}
+
 static int logind_init(void)
 {
 	GIOChannel *channel;
@@ -173,6 +212,10 @@ static void logind_exit(void)
 		g_source_remove(event_source);
 		event_source = 0;
 	}
+	if (timeout_source) {
+		g_source_remove(timeout_source);
+		timeout_source = 0;
+	}
 	sd_login_monitor_unref(monitor);
 }
 
Signature · 1 line
2.49.0
#10 lore raw

PATCH v6 3/3 Revert "obexd: only run one instance at once"

In reply to Andrew Sayers

No longer needed now services can share resources.

This reverts commit 8d472b8758dcdd89bf13cf2fb06a8846e1f483a0.

---
 obexd/src/obex.service.in | 9 ---------
 1 file changed, 9 deletions(-)
obexd/src/obex.service.in +0 -9
index f269bc513..cf4d8c985 100644
@@ -1,19 +1,10 @@
 [Unit]
 Description=Bluetooth OBEX service
-# This is a user-specific service, but bluetooth is a device-specific protocol.
-# Only run one instance at a time, even if multiple users log in at once:
-ConditionPathExists=!/run/lock/bluez/obexd.lock
-ConditionUser=!@system
 
 [Service]
 Type=dbus
 BusName=org.bluez.obex
 ExecStart=@PKGLIBEXECDIR@/obexd
 
-# If the service fails on the following line, please ensure
-# the bluez tmpfile has been installed in /usr/lib/tmpfiles.d/
-ExecStartPre=touch /run/lock/bluez/obexd.lock
-ExecStopPost=rm -f /run/lock/bluez/obexd.lock
-
 [Install]
 Alias=dbus-org.bluez.obex.service
Signature · 1 line
2.49.0
#11 lore raw
In reply to Andrew Sayers
This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Hello:

This series was applied to bluetooth/bluez.git (master) by Luiz Augusto von Dentz <[email protected]>:

Quoted 6 lines · On Wed, 30 Apr 2025 14:13:59 +0100 you wrote:

This is a follow-up to "obexd: only run one instance at once". Instead of refusing to run parallel services, it unregisters profiles when the user is inactive. This avoids the need for tmpfiles, and avoids issues where the user with the obex service logs out, leaving obex disabled altogether.

Quoted 6 lines

Luiz previously suggested moving this to systemd, but I haven't had much luck getting the systemd devs to accept changes, and Pauli's mention of elogind (i.e. logind without systemd) suggests it's probably better to avoid the dependency anyway.

[...]

Here is the summary with links:
- [BlueZ,v6,1/3] obexd: Unregister profiles when the user is inactive

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=765356e80262
  - [BlueZ,v6,2/3] obexd: Support sd_login_monitor_get_timeout()
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=8b29b5a46cdc
  - [BlueZ,v6,3/3] Revert "obexd: only run one instance at once"
    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=f8d0dc114036

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html