There are several methods that can be used for text message exchanging between different non-privileged users. Usually, when the sender of such a message is a service and the recipient, who is supposed to see the message, is a human, that message is called a “notification“. The method used by many services that run within the scope of a desktop session in order to send notifications to the desktop session owners involves the use of DBus and libnotify. But, would it be possible to use libnotify to send notification messages from a system service to one or more desktop users?
Despite my last post’s overwhelming optimism, it is sometimes impossible to find a solution for a problem, because one does not exist. It turns out that the above task cannot be accomplished, at least at the moment of writing, as it is impossible for one user (system account or other non-privileged user) to send a notification through libnotify to another user that runs a desktop session. The easiest way to test this is with a cron job in the system’s crontab:
*/1 * * * * root /usr/bin/notify-send "lala" "test message"
No notification appears on the desktop, no matter how long I wait. After doing some research it turns out that notify-send cannot automatically send notifications to all users that run a desktop session. It can only send notifications only to one user at a time provided that the user’s D-Bus session address is available to
So, I decided to set the cron job to send a notification to my own user account by providing notify-send with my DBUS_SESSION_BUS_ADDRESS.
The following is a method a system user (or any user except me) would use to retrieve my DBUS_SESSION_BUS_ADDRESS address. These steps are actually taken from this GNOME-Hack, which is supposed to send notifications from a cronjob.
In order to retrieve this address, my gnome-session’s PID number is needed.
$ ps axu | grep gnome-session | grep -v grep gnot 2602 0.0 1.3 33432 7064 ? Ssl 16:56 0:00 /usr/bin/gnome-session
The needed PID number is 2602. The D-BUS session’s address exists as an environment variable (DBUS_SESSION_BUS_ADDRESS) of that process (2602). This can be retrieved with the following:
$ grep -z DBUS_SESSION_BUS_ADDRESS /proc/2602/environ DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-vKzOHyaDdi,guid=f45436454463d8df2f8d265447405220
Notice that the PID number has been used in the
Adding this information to the initial cronjob one would expect that the notification would appear on the desktop.
*/1 * * * * root DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-vKzOHyaDdi,guid=f45436454463d8df2f8d265447405220 /usr/bin/notify-send "lala" "test message"
Unfortunately, it didn’t. I also retrieved my desktop session’s display number and added it to the cron job.
*/1 * * * * root DISPLAY=:0.0 DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-vKzOHyaDdi,guid=f45436454463d8df2f8d265447405220 /usr/bin/notify-send "lala" "test message"
Unfortunately, nothing happened.
It seems that not only
notify-send can send a notification to only one desktop session owner at a time, but also it is that desktop session owner who must issue the
notify-send command. So, as long as most system services drop privileges and run as processes of non-privileged system users, they cannot send notifications to desktop users.
This is probably because of one or more of the following reasons:
- either D-Bus is incomplete when it comes to message exchanging between non-privileged users
- or there is some kind of security mechanism that prevents the above from happening. Unfortunately, I haven’t found any information about such a mechanism.
- or I miss something way too important.
- or I make a mistake in one of the steps.
If I miss anything, please let me know.
Problems using libnotify for User to User Notifications by George Notaras, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2007 - Some Rights Reserved