You are not logged in.

#1 2020-09-25 23:01:49

kevinen98
Member
Registered: 2020-04-19
Posts: 10

[SOLVED] udev VGA trigger doesn't execute

Hello, so after fixing screen position on xorg after i got help on this post i created. But when external monitor is disconnected the position of the laptop screen is fucked up. So i tried using udev triggers to fixed it but udev doesn't seem to execute my script. It says something along the lines "took longer then expected to execute" and then "failed". When i did

sudo journalctl --since="5 minutes ago" | grep udev

at first this message showed up but not any more. What may be the cause of this problem?

My udev trigger file

/etc/udev/rules.d/95-monitor-hotplug.rules

looks like this

KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/kevin/.Xauthority", RUN+="/usr/local/bin/hotplug_monitor.sh"

hotplug_monitor.sh looks like this

#! /usr/bin/bash

xhost + 
export DISPLAY=:0.0
export XAUTHORITY=/home/dan/.Xauthority

function connect(){
    xrandr --output eDP1 --pos 1920x312
}
  
function disconnect(){
      xrandr --output eDP1 --pos 0x0
}
   
xrandr | grep "VGA1 connected" &> /dev/null && connect || disconnect

This script works fine when executing it manually but nothing happens with udev trigger.

Last edited by kevinen98 (2020-09-27 16:54:04)

Offline

#2 2020-09-25 23:24:54

solskog
Member
Registered: 2020-09-05
Posts: 412

Re: [SOLVED] udev VGA trigger doesn't execute

I tested your setup during system startup I got this:

systemd-udevd[226]: card0: Spawned process '/usr/local/bin/monitor.sh' [352] timed out after 2min 59s,
systemd-udevd[211]: card0: Worker [226] processing SEQNUM=2872 killed
systemd-udevd[211]: Worker [226] terminated by signal 9 (KILL)
card0: Worker [226] failed

This happened IF the script in udev.rules takes too long to wait for X to start. systemd-udevd kills the process.
But after X started, tested plug/unplug monitor in a openbox-session, your script automatically triggered by systemd-udevd as expected, and it works.

If you wants to dealing with startup issue, you can let the udev.rule hand over this script to another systemd service at startup.

$ cat /etc/udev/rules.d/95-monitor-hotplug.rules
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/kevin/.Xauthority", ENV{SYSTEMD_WANTS}+="hotplug_monitor.service"

$ cat hotplug_monitor.service
[Unit]
Description=Hotplug Monitor Daemon
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/bin/hotplug_monitor.sh
[Install]
WantedBy=multi-user.target

You also need a delay inside script until X starts. Otherwise it will be triggered too early.

$ cat hotplug_monitor.sh
while [[ -z $(/bin/ps --no-header -C Xorg -o pid=) ]];do
    /bin/sleep 1
done

Another notice is xhost have some security concerns, If you can figure out without xhost, that would be the best.

Last edited by solskog (2020-09-26 03:48:38)

Offline

#3 2020-09-26 13:22:11

kevinen98
Member
Registered: 2020-04-19
Posts: 10

Re: [SOLVED] udev VGA trigger doesn't execute

Sorry for taking so long to respond and thank you for responding!
So this fixed the issue on start up which is probably enough but nothing happens when plugging in or out vga cable when laptop is already started.

If you wants to dealing with startup issue, you can let the udev.rule hand over this script to another systemd service at startup.

$ cat /etc/udev/rules.d/95-monitor-hotplug.rules
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/kevin/.Xauthority", ENV{SYSTEMD_WANTS}+="hotplug_monitor.service"

$ cat hotplug_monitor.service
[Unit]
Description=Hotplug Monitor Daemon
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/bin/hotplug_monitor.sh
[Install]
WantedBy=multi-user.target

You also need a delay inside script until X starts. Otherwise it will be triggered too early.

$ cat hotplug_monitor.sh
while [[ -z $(/bin/ps --no-header -C Xorg -o pid=) ]];do
    /bin/sleep 1
done

In

udevadm monitor

i can see something are actually happening

KERNEL[460.749217] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [460.756659] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
KERNEL[465.049984] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)
UDEV  [465.057607] change   /devices/pci0000:00/0000:00:02.0/drm/card0 (drm)

The problem i got when laptop is started without monitor connected absolutely nothing happens. Monitor don't get a signal. if xrandr output might help here it is

Screen 0: minimum 8 x 8, current 1366 x 768, maximum 32767 x 32767
eDP1 connected primary 1366x768+0+0 (normal left inverted right x axis y axis) 310mm x 170mm
   1366x768      60.00*+
   1280x720      59.86    60.00    59.74  
   1024x768      60.00  
   1024x576      60.00    59.90    59.82  
   960x540       60.00    59.63    59.82  
   800x600       60.32    56.25  
   864x486       60.00    59.92    59.57  
   640x480       59.94  
   720x405       59.51    60.00    58.99  
   680x384       60.00  
   640x360       59.84    59.32    60.00  
DP1 disconnected (normal left inverted right x axis y axis)
DP2 disconnected (normal left inverted right x axis y axis)
HDMI1 disconnected (normal left inverted right x axis y axis)
HDMI2 disconnected (normal left inverted right x axis y axis)
VGA1 connected (normal left inverted right x axis y axis)
   1920x1080R    59.93 +
   1024x768      60.00  
   800x600       60.32    56.25  
   848x480       60.00  
   640x480       59.94  
VIRTUAL1 disconnected (normal left inverted right x axis y axis)

But if monitor is connected from startup it is like a virtual monitor. I can move my mouse over and windows stays there.

Offline

#4 2020-09-27 02:18:56

solskog
Member
Registered: 2020-09-05
Posts: 412

Re: [SOLVED] udev VGA trigger doesn't execute

You are right, the previous rule doesn't work well. And now I have tested this new rule and it works. Added two more environments, HOTPLUG and SEQNUM which came from $(udevadm monitor --environment --udev) while plug/unplug monitor. It seems a new instance of systemd unit file for each and every hotplug is needed. fortunately it's easy accomplished by a systemd unit template file.

$ cat /etc/udev/rules/99-monitor-hotplug.rules
KERNEL=="card0", SUBSYSTEM=="drm", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/kevin/.Xauthority", ENV{HOTPLUG}="1", ENV{SYSTEMD_WANTS}+="monitor@$env{SEQNUM}.service", TAG+="systemd"

And the systemd unit template file:

$ cat /lib/systemd/system/monitor@.service
[Unit]
Description=Monitor Hotplug Daemon
[Service]
Type=oneshot
TimeoutStartSec=300
ExecStart=/usr/local/bin/monitor.sh
[Install]
WantedBy=multi-user.target

To trigger the rule without physically plug/unplug monitor for testing the rule.

# udevadm trigger -s drm

To monitor it in realtime:

# /usr/sbin/journalctl -f

Last edited by solskog (2020-09-27 02:34:36)

Offline

#5 2020-09-27 16:49:08

kevinen98
Member
Registered: 2020-04-19
Posts: 10

Re: [SOLVED] udev VGA trigger doesn't execute

Okey, it seems to work now so thank you so much! But i want to lean what we did here so i just have a one question if you feel like answering it.

What does this do? It seems to me you set a variable to 1 but how is this used?

ENV{HOTPLUG}="1"

I have learned a lite bit of udev rule files and systemd which i now want to learn more about so again, thank you!

Offline

#6 2020-09-28 00:19:21

solskog
Member
Registered: 2020-09-05
Posts: 412

Re: [SOLVED] udev VGA trigger doesn't execute

The ENV{HOTPLUG}=1 property value shows up when I plug/unplug monitor physically. It doesn't show up when you manually call $(udevadm trigger -s drm).
I thought it should be included, but when I tested again without ENV{HOTPLUG}, it just worked as well.

Offline

Board footer

Powered by FluxBB