Re: devd lid close event

From: Steffen Nurpmeso <steffen_at_sdaoden.eu>
Date: Mon, 04 Dec 2023 17:46:24 UTC
Dr. Amr Osman wrote in
 <CAEYFr1A0_qe5BJR_FVnkEEL1bdYZHbSeidqy6L4mZbVtzROKuw@mail.gmail.com>:
 |Hello,
 |I have freeBSD 14 on Thinkpad x270,
 |I just did do a fresh install and installed dwm and slock
 |the issue is I want to make laptop lock screen whenever I close the lid
 |
 |here is my /usr/local/etc/devd/lid.conf
 |```
 |notify 10 {
 |    match "system"        "ACPI";
 |    match "subsystem"    "Lid";
 |    match "notify"        "0x00";
 |    action "/usr/local/bin/slock";
 |};
 |
 |```
 |here is my /var/run/devd.pipe when I close the lid
 |
 |```
 |!system=ACPI subsystem=Lid type=\_SB_.LID_ notify=0x00 !system=ACPI
 |subsystem=Lid type=\_SB_.LID_ notify=0x00 !system=ACPI
 |subsystem=Suspend type=\ notify=0x03 !system=IFNET subsystem=wlan0
 |type=LINK_DOWN
 |```
 |
 |what am I doing wrong?

At minimum slock needs the $DISPLAY to connect to, eg :0.
devd does not run within the X session, so no chance.
Also the above runs slock as root, not the user who actually owns
the X display.
For the very same use case, on linux, i use

  if command -v X >/dev/null 2>&1 && command -v slock >/dev/null 2>&1; then
          had_z=
          for p in $(pgrep X); do

^ use pgrep to find all X instances

                  uid=$(awk '/^Uid:/{print $2}' < /proc/$p/status)
                  disp=$(sed -Ee 's/^.*DISPLAY=:([[:digit:]]+).*$/\1/' < /proc/$p/environ)
                  [ -z "$disp" ] && disp=$(xargs -0 printf '%s\n' < /proc/$p/cmdline |
                                  awk '/^:[[:digit:]]+/{sub(":", ""); print}')

^ unfortunately non-portable approach to find out user id and
actual display of _this_ X instance

                  if [ -z "$disp" ]; then
                          [ -n "$had_z" ] && continue
                          had_z=y
                          disp=0
                  elif [ $disp = 0 ]; then
                          [ -n "$had_z" ] && continue
                          had_z=y
                  fi
                  act "DISPLAY=:$disp $SUPER -u $uid slock </dev/null >/dev/null 2>&1 &"

^ and here we lock that display for the given user.
$SUPER is doas(1) in this case, but was sudo(1) in the past (and
could be super(1) like ~20 years ago if that could be find
anywhere on the internet).

          done
  fi

 |Thank You
 |Dr. Amr Osman

Ciao.

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)
|
| Only in December: lightful Dubai COP28 Narendra Modi quote:
|  A small part of humanity has ruthlessly exploited nature.
|  But the entire humanity is bearing the cost of it,
|  especially the inhabitants of the Global South.
|  The selfishness of a few will lead the world into darkness,
|  not just for themselves but for the entire world.