Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions bin/omarchy-bluetooth-agent-supervisor
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/bin/bash

# omarchy:summary=Keep the Bluetooth pairing agent registered with BlueZ
# omarchy:hidden=true

set -uo pipefail

poll_interval=${OMARCHY_BLUETOOTH_AGENT_POLL_INTERVAL:-2}

bluez_pid() {
busctl --system list --no-pager --no-legend 2>/dev/null |
awk '$1 == "org.bluez" { print $2; exit }'
}

# The user manager cannot order a user unit against bluetooth.service in the
# system manager. Wait here so a session that wins that startup race still
# registers its pairing agent as soon as BlueZ appears.
while :; do
initial_bluez_pid=$(bluez_pid)
if [[ $initial_bluez_pid =~ ^[0-9]+$ ]]; then
break
fi
sleep "$poll_interval"
done

bt-agent -c NoInputNoOutput &
agent_pid=$!

stop_agent() {
kill -KILL "$agent_pid" 2>/dev/null || true
wait "$agent_pid" 2>/dev/null || true
}

trap 'stop_agent; exit 0' TERM INT HUP

# bt-agent remains alive when bluetoothd disappears, but AgentManager
# registrations belong to that daemon instance. Exit on an owner change so
# systemd replaces the stale agent with one registered to the live daemon.
while kill -0 "$agent_pid" 2>/dev/null; do
sleep "$poll_interval"
current_bluez_pid=$(bluez_pid)
if [[ $current_bluez_pid != "$initial_bluez_pid" ]]; then
stop_agent
exit 1
fi
done

wait "$agent_pid"
16 changes: 9 additions & 7 deletions default/systemd/user/bt-agent.service
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,24 @@
Description=Bluetooth pairing agent (auto-accept)
Documentation=man:bt-agent(1)
ConditionPathIsDirectory=/sys/class/bluetooth
# bluez must be reachable on the system bus before we can register.
After=dbus.socket
Requires=dbus.socket

[Service]
Type=simple
# If bluetoothd is unavailable (for example in VMs or machines without a
# usable adapter), skip cleanly instead of entering a restart loop.
ExecCondition=/usr/bin/systemctl is-active --quiet bluetooth.service
# NoInputNoOutput auto-accepts pair requests. Safe because the adapter
# is only `pairable: true` when the user explicitly opens the omarchy
# bluetoothPanel and starts scanning; outside that window bluez refuses
# inbound pair attempts at a lower layer.
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
Restart=on-failure
RestartSec=2
ExecStart=/usr/bin/omarchy-bluetooth-agent-supervisor
# The supervisor exits whenever the org.bluez owner changes, including a
# clean daemon replacement, so the agent always registers with the live one.
Restart=always
RestartSec=1
# bluez-tools' bt-agent ignores SIGTERM. Bound service stops and let the
# supervisor reap it instead of leaving updates waiting for the default 90s.
KillMode=mixed
TimeoutStopSec=3

[Install]
WantedBy=graphical-session.target
110 changes: 110 additions & 0 deletions test/shell.d/bluetooth-agent-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

test_tmp=$(mktemp -d)
supervisor_pid=

cleanup() {
if [[ $supervisor_pid =~ ^[0-9]+$ ]]; then
kill "$supervisor_pid" 2>/dev/null || true
wait "$supervisor_pid" 2>/dev/null || true
fi
rm -rf "$test_tmp"
}

trap cleanup EXIT

mock_bin="$test_tmp/bin"
mkdir -p "$mock_bin"

cat >"$mock_bin/busctl" <<'SH'
#!/bin/bash

count=0
[[ ! -f $BUSCTL_COUNT_FILE ]] || read -r count <"$BUSCTL_COUNT_FILE"
count=$((count + 1))
printf '%s\n' "$count" >"$BUSCTL_COUNT_FILE"

case $BUSCTL_SEQUENCE in
replaced)
if (( count <= 2 )); then
printf 'org.bluez 101 bluetoothd root system.slice 1 bluetooth.service - -\n'
else
printf 'org.bluez 202 bluetoothd root system.slice 1 bluetooth.service - -\n'
fi
;;
absent-then-stable)
if (( count >= 3 )); then
printf 'org.bluez 303 bluetoothd root system.slice 1 bluetooth.service - -\n'
fi
;;
esac
SH

cat >"$mock_bin/bt-agent" <<'SH'
#!/bin/bash

printf '%s\n' "$*" >>"$AGENT_LOG"
printf '%s\n' "$$" >"$AGENT_PID_FILE"
while :; do sleep 1; done
SH

chmod +x "$mock_bin/busctl" "$mock_bin/bt-agent"

supervisor="$ROOT/bin/omarchy-bluetooth-agent-supervisor"

replacement_dir="$test_tmp/replacement"
mkdir -p "$replacement_dir"

set +e
PATH="$mock_bin:$PATH" \
BUSCTL_SEQUENCE=replaced \
BUSCTL_COUNT_FILE="$replacement_dir/busctl-count" \
AGENT_LOG="$replacement_dir/agent-log" \
AGENT_PID_FILE="$replacement_dir/agent-pid" \
OMARCHY_BLUETOOTH_AGENT_POLL_INTERVAL=0.02 \
timeout 5 "$supervisor"
replacement_status=$?
set -e

(( replacement_status == 1 )) ||
fail "Bluetooth agent supervisor did not exit for replacement after BlueZ changed owner"
grep -Fx -- '-c NoInputNoOutput' "$replacement_dir/agent-log" >/dev/null
replacement_agent_pid=$(<"$replacement_dir/agent-pid")
kill -0 "$replacement_agent_pid" 2>/dev/null &&
fail "Bluetooth agent supervisor left the stale agent running after BlueZ changed owner"
pass "Bluetooth agent supervisor replaces an agent registered to an old BlueZ owner"

startup_dir="$test_tmp/startup"
mkdir -p "$startup_dir"

PATH="$mock_bin:$PATH" \
BUSCTL_SEQUENCE=absent-then-stable \
BUSCTL_COUNT_FILE="$startup_dir/busctl-count" \
AGENT_LOG="$startup_dir/agent-log" \
AGENT_PID_FILE="$startup_dir/agent-pid" \
OMARCHY_BLUETOOTH_AGENT_POLL_INTERVAL=0.02 \
"$supervisor" &
supervisor_pid=$!

for _ in {1..100}; do
[[ -s $startup_dir/agent-pid ]] && break
sleep 0.02
done

[[ -s $startup_dir/agent-pid ]] ||
fail "Bluetooth agent supervisor did not start the agent after BlueZ appeared"
startup_checks=$(<"$startup_dir/busctl-count")
(( startup_checks >= 3 )) ||
fail "Bluetooth agent supervisor started the agent before BlueZ appeared"

startup_agent_pid=$(<"$startup_dir/agent-pid")
kill "$supervisor_pid"
wait "$supervisor_pid"
supervisor_pid=
kill -0 "$startup_agent_pid" 2>/dev/null &&
fail "Bluetooth agent supervisor left the agent running when its service stopped"
pass "Bluetooth agent supervisor waits for BlueZ and cleans up with its service"
21 changes: 15 additions & 6 deletions test/shell.d/systemd-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,21 @@ set -euo pipefail
source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

service="$ROOT/default/systemd/user/bt-agent.service"

grep -Fx 'ExecCondition=/usr/bin/systemctl is-active --quiet bluetooth.service' "$service" >/dev/null
pass "bt-agent skips when bluetooth.service is inactive"

grep -Fx 'Restart=on-failure' "$service" >/dev/null
pass "bt-agent still restarts after runtime failures"
agent_supervisor="$ROOT/bin/omarchy-bluetooth-agent-supervisor"

grep -Fx 'ExecStart=/usr/bin/omarchy-bluetooth-agent-supervisor' "$service" >/dev/null
grep -Fx 'Restart=always' "$service" >/dev/null
grep -Fx 'KillMode=mixed' "$service" >/dev/null
grep -Fx 'TimeoutStopSec=3' "$service" >/dev/null
grep -F 'current_bluez_pid != "$initial_bluez_pid"' "$agent_supervisor" >/dev/null
pass "bt-agent follows BlueZ daemon replacements"

grep -F 'while :' "$agent_supervisor" >/dev/null
grep -F 'initial_bluez_pid=$(bluez_pid)' "$agent_supervisor" >/dev/null
grep -F 'sleep "$poll_interval"' "$agent_supervisor" >/dev/null
grep -F 'ExecCondition=' "$service" >/dev/null &&
fail "bt-agent still permanently skips when the user manager wins the BlueZ startup race"
pass "bt-agent waits for BlueZ instead of permanently skipping"

sleep_service="$ROOT/default/systemd/user/omarchy-sleep-lock.service"
grep -Fx 'ExecStart=/usr/bin/omarchy-system-sleep-monitor' "$sleep_service" >/dev/null
Expand Down