0

From anther tty I can run

DISPLAY=:0 notify-send hullo

and see the message in my toothrot session. But if I log in as root, this doesn't work. Nor does

USER=toothrot DISPLAY=:0 notify-send hullo

How do I make it work from root?

I need this because I want to create a udev rule that affects my x session. Using Arch.

Toothrot
  • 3,435
  • Usually an authorization issue, see man xauth. But I promise you a world of pain if you try to write a udev rule that affects your X session. Don't do that, try to find a proper solution, if necessary in layers, using a dbus interface, or whatever. (What should happen if there's no X session? What if there are two or more X sessions?). – dirkt Mar 01 '17 at 17:29
  • @dirkt, I don't know why that wouldn't be 'proper'? All I want to do is update my keyboard layout when my external keyboard is attached to and removed from my laptop. – Toothrot Mar 01 '17 at 17:39

3 Answers3

0

X can have two ways of authorizing clients. One way is using cookies; then you have to set DISPLAY=:0 and XAUTHORITY=/home/yourusername/.Xauthority.

In your case I assume it is done with xhost, otherwise setting DISPLAY would not be enough. Type xhost to see who is authorized.

Maybe you have an ~/.Xauthority cookie along with xhost +SI:localuser:toothrot authorization.

You can allow root to access display :0 with DISPLAY=:0 xhost +SI:localuser:root, but you have to do it as normal user. Using the cookie is the better way:

DISPLAY=:0 XAUTHORITY=/home/toothrot/.Xauthority notify-send hullo

To execute notify-send as user:

su toothrot -c 'DISPLAY=:0 XAUTHORITY=/home/toothrot/.Xauthority notify-send hullo'
mviereck
  • 2,467
0

Presumably you solved your issue long ago, but for the sake of completion a general solution is (adapted from here):

$ my_user="toothrot"
$ my_user_id=$(more /etc/passwd | awk -F: '/^'${my_user}':/ {print $3}')
$ sudo -u $my_user DISPLAY=':0' DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/${my_user_id}/bus /usr/bin/notify-send "hullo"

If you have several displays of screen sessions, your mileage may vary as far as DISPLAY=":0" is concerned in the above. That'd be for a different post though.

Cbhihe
  • 2,701
0

The above answers are kind of crufty, and don't work cleanly for multiple users or multiple displays. There could be a privilege escalation attack if a user symlinks ~/.Xauthority to a protected file. An alternative might be to forget udev entirely and instead use uudev, which runs as non-root in your own X session. You just set up a ~/.config/uudev.conf file that contains:

* ACTION="bind", DRIVER="whatever", ...
notify-send hullo

If you run systemctl --user restart uudev from within your ~/.xsession or ~/.xinitrc file, then notify-send will just get run with the appropriate DISPLAY and XAUTHORITY variables already set.

user3188445
  • 5,257