Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check DBUS_SESSION_BUS_ADDRESS and give more meaningful errors #1389

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
29 changes: 19 additions & 10 deletions dunstctl
Original file line number Diff line number Diff line change
Expand Up @@ -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?"
bynect marked this conversation as resolved.
Show resolved Hide resolved
exit 1
}

show_help() {
# Below, each line starts with a tab character
Expand Down Expand Up @@ -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."
Expand All @@ -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")
Expand Down Expand Up @@ -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 unreachable or the version is too old."
;;
*)
die "Unknown format \"${2}\". Please use either \"--json\" or no option at all."
Expand Down Expand Up @@ -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" \
| (
Expand All @@ -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
Expand All @@ -234,4 +244,3 @@ case "${1:-}" in
;;
esac
# vim: noexpandtab

5 changes: 5 additions & 0 deletions src/dbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down