Solved devd run X11 command as user

I am trying to run some X11 commands (xmodmap, xrandr) from devd as ordinary user but it doesn't work.
Running same command as root works.

Code:
notify 101 {
        match "system"          "USB";
        match "subsystem"       "!usbus[0-9]+";
        match "vendor"          "0x0416";
        match "product"         "0xa0f8";
        match "type"            "attach";
        action "su -l user -c 'DISPLAY=unix:0.0 xmodmap ~/.xmodmap'";
        action "logger USB keyboard attached, running xmodmap as user";
};

logger action works but not xmodmap:
Code:
grep xmodmap /var/log/messages
Jul 14 09:12:17 hostname user[22373]: USB keyboard attached, running xmodmap as user
Jul 14 09:12:17 hostname user[22376]: USB keyboard attached, running xmodmap as user
Jul 14 09:12:17 hostname user[22379]: USB keyboard attached, running xmodmap as user

Running su -l user -c 'DISPLAY=unix:0.0 xmodmap ~/.xmodmap' as root works as intended.
devd is run as root:
Code:
ps aux | grep devd
root       90594   0.0  0.0    10572   1444  -  Ss   18:39       2:27.65 /sbin/devd

Same results when trying to run command as root but from /bin/sh or changing '~' to '$HOME' or using absolute path.
 
Note that devd(8) runs without a controlling TTY, and the standard I/O streams are not connected to a terminal. That can make a difference when running commands.

Try without the -l option, so it doesn't run your profile scripts, and use absolute paths, i.e.:

action "su user -c 'DISPLAY=unix:0.0 /usr/local/bin/xmodmap /home/user/.xmodmap'";

To debug that kind of problems, it's helpful to put the actual command inside a script,redirect standard output and error to a file, and then execute the script from devd:

action "/home/user/bin/myscript";

... and myscript would look like this (make sure to chmod +x it):
Code:
#!/bin/sh
export DISPLAY=unix:0.0
/usr/local/bin/xmodmap /home/user/.xmodmap >/tmp/myscript.debug 2>&1
Also watch /var/log/devd.log for any error messages. I think it's not enabled by default, so enable it in /etc/syslog.conf and restart syslogd(8).
 
That works, that way I can run X11 program from devd fine, but it does not when keyboard is attached. It works when other (random) USB device is triggering devd event :rolleyes:
VID & PID are correct, script will be run, xmodmap (or xsetkbmap) also, but won't have effect. Logs from script redirections are normal - nothing unusual. Running same script as root from terminal results with expected behavior.

Code:
notify 100 {
        match "system"          "USB";
        match "subsystem"       "!usbus[0-9]+";
        match "vendor"          "0x0416";
        match "product"         "0xa0f8";
        match "type"            "attach";
        action "/tmp/script.sh";
        action "logger USB keyboard attached, running xmodmap as user";
};

# test 200717
notify 100 {
        match "system"          "USB";
        match "subsystem"       "!usbus[0-9]+";
        match "vendor"          "0x0403";
        match "product"         "0x6010";
        match "type"            "attach";
        action "/tmp/script.sh";
        action "logger USB devd X11 test";
};

/tmp/script.sh:
Code:
#!/bin/sh
export DISPLAY=unix:0.0

FILE=/tmp/devd-test.log

echo "START ----------------------------------------------------------" >> $FILE
echo $(/bin/date +%y%m%d) $(/bin/date +%H:%M) >> $FILE

export DISPLAY=unix:0.0
/usr/local/bin/xmodmap /home/johnny/.xmodmap >> $FILE 2>&1

echo "END ------------------------------------------------------------" >> $FILE

/var/log/messages after attaching keyboard:
Code:
Jul 17 15:28:51 innovator user[42726]: USB keyboard attached, running xmodmap as user
Jul 17 15:28:52 innovator user[42731]: USB keyboard attached, running xmodmap as user
Jul 17 15:28:52 innovator user[42736]: USB keyboard attached, running xmodmap as user
But no effect on the system - xmodmap is not applied, still have default us keyboard layout

/var/log/messages after after connecting (random) USB dongle
Code:
Jul 17 15:29:25 innovator user[43007]: USB devd X11 test
Jul 17 15:29:26 innovator user[43015]: USB devd X11 test
Jul 17 15:29:26 innovator user[43022]: USB devd X11 test

/tmp/devd-test.log shows that everything was without error:
Code:
START ----------------------------------------------------------
200717 15:28
END ------------------------------------------------------------
START ----------------------------------------------------------
200717 15:28
END ------------------------------------------------------------
START ----------------------------------------------------------
200717 15:28
END ------------------------------------------------------------
START ----------------------------------------------------------
200717 15:29
END ------------------------------------------------------------
START ----------------------------------------------------------
200717 15:29
END ------------------------------------------------------------
START ----------------------------------------------------------
200717 15:29
END ------------------------------------------------------------
 
That works, that way I can run X11 program from devd fine, but it does not when keyboard is attached. It works when other (random) USB device is triggering devd event :rolleyes:
In that case I guess that the action is executed too early, so the xmodmap command runs before the attachment of the keyboard is communicated to the X11 server, so the effect of the xmodmap command is reset again immediately.

Maybe try to insert sleep 3 or similar in the script before executing xmodmap. Since devd(8) waits for the command to finish before further processing, it might be necessary to do that in the background, i.e.:
Code:
sh -c "sleep 3; /usr/local/bin/xmodmap /home/johnny/.xmodmap" &
 
Back
Top