Index | Thread | Search

From:
Rafael Sadowski <rafael@sizeofvoid.org>
Subject:
ck-session-notify: send notifications to all active ConsoleKit sessions
To:
ports@openbsd.org
Cc:
Jonathan Matthew <jmatthew@openbsd.org>
Date:
Sat, 24 May 2025 09:50:41 +0200

Download raw body.

Thread
  • Rafael Sadowski:

    ck-session-notify: send notifications to all active ConsoleKit sessions

Please find attached ck-session-notify. A simple script to send
notifications to all active ConsoleKit sessions. For almost all desktop
environments under OpenBSD it is recommended to start them with
${LOCALBASE}/bin/ck-launch-session (default on GNOME). This allows us to
get session information.

The script goes through all active sessions and sends a message to the
session dbus. For this you have to allow "root" access to all session buses.
(Keep in mind that you have to restart you running dbus sessions)

/etc/dbus-1/session.conf:

 <busconfig>
   <policy context="mandatory">
     <allow user="root"/>
   </policy>
 </busconfig>


At the end you will find a simple example of how I send hotplugd events
as notification to my KDE Plasma desktop. This should also work on Xfce,
Mate GNOME you name it.

robert@ points to sysutils/toad. Which also does hotplugd operations but
for my case I only need a simple "from root to user notification
method".

#!/bin/sh

DEVCLASS=$1
DEVNAME=$2

case $DEVCLASS in
    0) DEVCLASS_NAME="generic" ;;
    1) DEVCLASS_NAME="CPU" ;;
    2) DEVCLASS_NAME="disk drive" ;;
    3) DEVCLASS_NAME="network interface" ;;
    4) DEVCLASS_NAME="tape device" ;;
    5) DEVCLASS_NAME="serial interface" ;;
esac

case $DEVNAME in
    uaudio*)
        pkill -HUP sndiod
    ;;
    fido*)
        /etc/hotplug/ck-session-notify "hotplugd" "" \
            "Attachment" "$DEVCLASS_NAME: Yubico YubiKey OTP+FIDO+CCID" 5000
        chown rsadowski /dev/ugen*.*
        chown rsadowski /dev/usb2
        chown rsadowski /dev/usb1
        chown rsadowski /dev/usb0
    ;;
    *)
        /etc/hotplug/ck-session-notify "hotplugd" "" \
            "Attachment" "$DEVCLASS_NAME: $DEVNAME" 5000
    ;;
esac
#!/bin/sh

# Send notifications to all active ConsoleKit sessions

# XXX
# You have to allow root access to all session buses. /etc/dbus-1/session.conf:
# <busconfig>
#   <policy context="mandatory">
#     <allow user="root"/>
#   </policy>
# </busconfig>

# Usage: ck-session-notify [app_name] [icon] [title] [message] [timeout_ms]

# Examples:
# ck-session-notify "hotplugd" "emblem-important" "Attached" "New fido dev" 8000

PATH="/usr/bin:/bin:/usr/local/bin"

if [ "$(id -u)" -ne 0 ]; then
	exit 1
fi

if ! command -v ck-list-sessions >/dev/null 2>&1; then
	echo "Error: ck-list-sessions not found" >&2
	exit 1
fi

if ! command -v gdbus >/dev/null 2>&1; then
	echo "Error: gdbus not found" >&2
	exit 1
fi

ck-list-sessions | grep -G7 -A9 "active = TRUE" | while read -r line; do
	case "$line" in
	*"unix-user = "*)
		user_id=$(echo "$line" | cut -d"'" -f2)
		;;
	*"x11-display = "*)
		display=$(echo "$line" | cut -d"'" -f2)
		username=$(id -nu "$user_id")

		dbus_socket=$(find /tmp -name "dbus-*" -user "$username" \
			-type s 2>/dev/null | head -1)

		if test -n "$username" && test -S "$dbus_socket"; then
			USER=$username \
				DISPLAY="${display:-:0}" \
				DBUS_SESSION_BUS_ADDRESS="unix:path=$dbus_socket" \
				gdbus call --session \
				--dest org.freedesktop.Notifications \
				--object-path /org/freedesktop/Notifications \
				--method org.freedesktop.Notifications.Notify \
				"${1:-System}" 0 "${2:-dialog-information}" \
				"${3:-Message}" "${4:-Broadcast}" "[]" "{}" "${5:-3000}"
		fi
		;;
	esac
done