Discussion:
devd lid close event
(too old to reply)
Dr. Amr Osman
2023-12-04 18:24:30 UTC
Permalink
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?

Thank You
Dr. Amr Osman


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Steffen Nurpmeso
2023-12-04 17:46:24 UTC
Permalink
Dr. Amr Osman wrote in
<***@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.


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Steffen Nurpmeso
2023-12-04 20:15:11 UTC
Permalink
--- Forwarded from Steffen Nurpmeso <***@sdaoden.eu> ---
Date: Mon, 04 Dec 2023 21:04:14 +0100
Author: Steffen Nurpmeso <***@sdaoden.eu>
From: Steffen Nurpmeso <***@sdaoden.eu>
To: "Dr. Amr Osman" <***@gmail.com>
Subject: Re: devd lid close event
Message-ID: <***@steffen%sdaoden.eu>

Dr. Amr Osman wrote in
<CAEYFr1C-f+KX99wJ21qKy8pA2D4btuJNUaX-***@mail.gmail.com>:
|The issue now is that is activates twice, as the devd notify happens twice
|Tried pgrep the slock but it always launches slock twice

Well for one (i have no freebsd around to truly test this) it
might be necessary to background the thing in order not to block.
(This is what i need to do on Linux, for example i now do

volume() {
(
cd /
/root/bin/volume.sh "$1"
) </dev/null >/dev/null 2>&1 &
}

so that i neither have a CWD in a path of a mounted volume that
may go away upon LID close etc. Also, due to a bug, i got piles
of dozens / hundreds of jobs in the queue, which can only be
avoided by moving the ACPI job to the background, aka by
daemonizing it. (That it then is reparented to init/process 1 is
something i still have to think about.))

And second, if all else fails, you may need to enwrap it in
a shell script, maybe even with file locking. (flock(1).)
Ie, my zzz.sh does in the main path

# Neutralize $PWD to avoid umount(8) etc problems
cd /

if [ -f /run/.zzz.run ]; then
logger -s -t /root/bin/zzz.sh "recursively invoked"
exit 0
fi
: > /run/.zzz.run
trap 'rm -f /run/.zzz.*' EXIT
trap 'trap "" HUP INT QUIT TERM; exit 1' HUP INT QUIT TERM

down_stuff
down_check
down_main

[ $# -eq 0 ] && once= || once=y

while :; do
[ -n "$once" ] && lid_is_open && break
once=y
down_off
lid_exists || break
sleep $SLEEP &
wait $!
done

# Sleep a bit to let it settle
( sleep .5 ) >/dev/null 2>&1
up_main
sleep 1
up_stuff

Ciao,

--steffen
-- End forward <***@steffen%sdaoden.eu>

--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.


--
Posted automagically by a mail2news gateway at muc.de e.V.
Please direct questions, flames, donations, etc. to news-***@muc.de
Loading...