From 5383c11bd5a6da3edffebfcb7c33e94b54390afa Mon Sep 17 00:00:00 2001 From: bynect <68197565+bynect@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:11:52 +0100 Subject: [PATCH 1/3] Check DBUS_SESSION_BUS_ADDRESS and more meaningful messages --- dunstctl | 29 +++++++++++++++++++---------- src/dbus.c | 5 +++++ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/dunstctl b/dunstctl index d7e74e06d..e1fa82ae6 100755 --- a/dunstctl +++ b/dunstctl @@ -8,7 +8,12 @@ DBUS_IFAC_DUNST="org.dunstproject.cmd0" DBUS_IFAC_PROP="org.freedesktop.DBus.Properties" DBUS_IFAC_FDN="org.freedesktop.Notifications" -die(){ printf "%s\n" "${1}" >&2; exit 1; } +die() { + printf "%s\n" "${1}" >&2 + [ -z "$DBUS_SESSION_BUS_ADDRESS" ] \ + && echo "DBUS_SESSION_BUS_ADDRESS is blank. Is the dbus session configured correctly?" + exit 1 +} show_help() { # Below, each line starts with a tab character @@ -44,6 +49,12 @@ show_help() { help Show help EOH } + +busctl_checked() { + command -v busctl >/dev/null 2>/dev/null || die "Command busctl not found" + busctl --user --json=pretty --no-pager call "${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_DUNST}" "$@" +} + dbus_send_checked() { dbus-send "$@" \ || die "Failed to communicate with dunst, is it running? Or maybe the version is outdated. You can try 'dunstctl debug' as a next debugging step." @@ -61,9 +72,7 @@ property_set() { dbus_send_checked --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_PROP}.Set" "string:${DBUS_IFAC_DUNST}" "string:${1}" "${2}" } -command -v dbus-send >/dev/null 2>/dev/null || \ - die "Command dbus-send not found" - +command -v dbus-send >/dev/null 2>/dev/null || die "Command dbus-send not found" case "${1:-}" in "action") @@ -151,16 +160,17 @@ case "${1:-}" in "rules") case "${2:-}" in "" | --json) - busctl --user --json=pretty --no-pager call "${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_DUNST}" RuleList \ + busctl_checked RuleList \ | { if [ "${2:-}" = '--json' ] then cat else + command -v jq >/dev/null 2>/dev/null || die "Command jq not found" jq --raw-output '.data[][] | ["[\(.name.data)]"], [to_entries[] | select(.key != "name") | " \(.key) = \(.value.data)"] | join("\n")' fi } \ - || die "Dunst is not running." + || die "Dunst is not running or unreachable." ;; *) die "Unknown format \"${2}\". Please use either \"--json\" or no option at all." @@ -205,7 +215,7 @@ case "${1:-}" in ;; "debug") dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_FDN}.GetServerInformation" >/dev/null 2>/dev/null \ - || die "Dunst is not running." + || die "No notification manager is running." dbus-send --print-reply=literal --dest="${DBUS_NAME}" "${DBUS_PATH}" "${DBUS_IFAC_FDN}.GetServerInformation" \ | ( @@ -219,8 +229,8 @@ case "${1:-}" in || die "Dunst controlling interface not available. Is the version too old?" ;; "history") - busctl --user --json=pretty --no-pager call org.freedesktop.Notifications /org/freedesktop/Notifications org.dunstproject.cmd0 NotificationListHistory 2>/dev/null \ - || die "Dunst is not running." + busctl_checked NotificationListHistory \ + || die "Dunst is not running or unreachable." ;; "reload") shift @@ -234,4 +244,3 @@ case "${1:-}" in ;; esac # vim: noexpandtab - diff --git a/src/dbus.c b/src/dbus.c index 4b2f53a87..8d8ec23c0 100644 --- a/src/dbus.c +++ b/src/dbus.c @@ -1379,6 +1379,11 @@ static void dbus_cb_name_lost(GDBusConnection *connection, DIE("Cannot acquire '"FDN_NAME"'."); } } else { + const char *env = getenv("DBUS_SESSION_BUS_ADDRESS"); + if (STR_EMPTY(env)) { + LOG_W("DBUS_SESSION_BUS_ADDRESS is blank. Is the dbus session configured correctly?"); + } + DIE("Cannot connect to DBus."); } exit(1); From bbd22ef0d1ccd7b00eb2e01e9fda797b763d1c85 Mon Sep 17 00:00:00 2001 From: bynect <68197565+bynect@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:44:06 +0100 Subject: [PATCH 2/3] Add busctl to deps --- README.md | 1 + dunstctl | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 445659e71..699ec6e36 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ distro's repositories, don't worry, it's not hard to build it yourself. - wayland-protocols (optional, for recompiling protocols) - xdg-utils (optional, xdg-open is the default 'browser' for opening URLs) - jq (optional, for installed completions and tools in contrib) +- busctl (optional, for dunstctl rules and dunstctl history) The names will be different depending on your [distribution](https://github.com/dunst-project/dunst/wiki/Dependencies). diff --git a/dunstctl b/dunstctl index e1fa82ae6..3ad793a18 100755 --- a/dunstctl +++ b/dunstctl @@ -170,7 +170,7 @@ case "${1:-}" in jq --raw-output '.data[][] | ["[\(.name.data)]"], [to_entries[] | select(.key != "name") | " \(.key) = \(.value.data)"] | join("\n")' fi } \ - || die "Dunst is not running or unreachable." + || die "Dunst is unreachable or the version is too old." ;; *) die "Unknown format \"${2}\". Please use either \"--json\" or no option at all." From 3fb71a63cd90070c180b6c0817ed43daea8972cd Mon Sep 17 00:00:00 2001 From: nect <68197565+bynect@users.noreply.github.com> Date: Sun, 10 Nov 2024 16:22:54 +0100 Subject: [PATCH 3/3] Update dunstctl behehei fix Co-authored-by: Benedikt Heine --- dunstctl | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dunstctl b/dunstctl index 3ad793a18..d5e76de13 100755 --- a/dunstctl +++ b/dunstctl @@ -10,8 +10,9 @@ DBUS_IFAC_FDN="org.freedesktop.Notifications" die() { printf "%s\n" "${1}" >&2 - [ -z "$DBUS_SESSION_BUS_ADDRESS" ] \ - && echo "DBUS_SESSION_BUS_ADDRESS is blank. Is the dbus session configured correctly?" + if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then + echo "DBUS_SESSION_BUS_ADDRESS is blank. Is the D-Bus session configured correctly?" >&2 + fi exit 1 }