You are not logged in.

#1 2018-04-20 03:41:40

rogerthat
Member
Registered: 2016-11-15
Posts: 56

Screen lock (sflock) fails... how to autorun on suspend?

I created a system-sleep script to lock the screen upon sleep using sflock based on the wiki. (https://wiki.archlinux.org/index.php/Power_management)

The parts:

1) The package sflock is installed:

$ ls -l /usr/bin/sflock
-rwsr-xr-x 1 root root 9872 Apr 10  2017 /usr/bin/sflock

2) A custom wrapper script I made for it. (NOTE: This script works perfectly correctly when run by a non-root user by typing "lock".)

$ cat /usr/local/bin/lock
#!/bin/sh
echo "lock script running"
/usr/bin/sflock -f '-*-fixed-*-*-*--13-*-*-*-*-*-*-*'

$ ls -l /usr/local/bin/lock
-rwxr-xr-x 1 root root 91 Mar  5 15:52 /usr/local/bin/lock

3) The system-sleep script I made:
(NOTE: I added the 'DISPLAY' and '/usr/bin/sleep' lines while troubleshooting, but it still didn't work.)

$ cat /usr/lib/systemd/system-sleep/sleep-lock.sh
#!/bin/sh
case $1 in
  pre)
    echo "sleep-lock.sh script running: $1/$2"
#    DISPLAY=:0
    /usr/local/bin/lock
#    /usr/bin/sleep 1
    echo "sleep-lock.sh script done: $1/$2"
    ;;
esac

$ ls -l /usr/lib/systemd/system-sleep/sleep-lock.sh
-rwxr-xr-x 1 root root 191 Mar  5 16:33 /usr/lib/systemd/system-sleep/sleep-lock.sh

And here are the results after suspending and resuming using the power button:
(NOTE: the screen never locks hmm )

$ journalctl -b -u systemd-suspend
systemd[1]: Starting Suspend...
systemd-sleep[1229]: sleep-lock.sh script running: pre/suspend
systemd-sleep[1229]: lock script running
systemd-sleep[1229]: sleep-lock.sh script done: pre/suspend
systemd-sleep[1229]: Suspending system...
systemd-sleep[1229]: Suspending resumed.
systemd[1]: Started Suspend.

So it should have run. But there is a core dump for sflock in the journal:

$ journalctl -p 3 -xb
systemd-coredump[423]: Process 420 (sflock) of user 0 dumped core.
                                           
                                           Stack trace of thread 420:
                                           #0  0x00000000b7363d59 _nss_files_getspnam_r (libnss_files.so.2)
                                           #1  0x00000000b7642b0e getspnam_r@@GLIBC_2.1.2 (libc.so.6)
                                           #2  0x00000000b7641eba getspnam (libc.so.6)
                                           #3  0x00000000080491ea n/a (sflock)
                                           #4  0x00000000b755e6c3 __libc_start_main (libc.so.6)
                                           #5  0x0000000008049948 n/a (sflock)
-- Subject: Process 420 (sflock) dumped core
-- Defined-By: systemd
-- Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
-- Documentation: man:core(5)
-- 
-- Process 420 (sflock) crashed and dumped core.
-- 
-- This usually indicates a programming error in the crashing program and
-- should be reported to its vendor as a bug.

How do I troubleshoot this?!?

Last edited by rogerthat (2018-04-23 15:05:11)

Offline

#2 2018-04-20 06:02:02

seth
Member
Registered: 2012-09-03
Posts: 50,012

Re: Screen lock (sflock) fails... how to autorun on suspend?

You need slock to run as user w/ the X11 authority and of course you need to *export* DISPLAY.
Look around https://gist.github.com/AladW/de1c5676d93d05a5a0e1 as inspiration.

Offline

#3 2018-04-20 06:21:59

x33a
Forum Fellow
Registered: 2009-08-15
Posts: 4,587

Re: Screen lock (sflock) fails... how to autorun on suspend?

@rogerthat, I have removed your post from https://bbs.archlinux.org/viewtopic.php?id=235827, on account of cross-posting.

https://wiki.archlinux.org/index.php/Co … ss-posting

Offline

#4 2018-04-22 00:47:48

rogerthat
Member
Registered: 2016-11-15
Posts: 56

Re: Screen lock (sflock) fails... how to autorun on suspend?

I defer to your expert judgment, x33a. And also for the public record, I would note that posting in two threads that have similar topics is not inherently cross-posting as I understand it. On the other thread, I was answering another user's question with a suggestion. And here I am asking for help troubleshooting a problem. Both threads had to do with sflock, but my two posts were not the same.

Cheers.

Offline

#5 2018-04-22 00:48:30

rogerthat
Member
Registered: 2016-11-15
Posts: 56

Re: Screen lock (sflock) fails... how to autorun on suspend?

Muchas gracias, seth. I will read more into that and post an update.

What user woudl be running the system-sleep scripts? I would have thought root, or perhaps the current user. I would assume that root has all needed permissions. And the current user can run the script, and it works. So I'm inferring that it's not a permission problem.

UPDATE: Looked at the link, but I'm not getting any insight. How does one export DISPLAY?

Last edited by rogerthat (2018-04-22 20:06:39)

Offline

#6 2018-04-22 05:52:30

seth
Member
Registered: 2012-09-03
Posts: 50,012

Re: Screen lock (sflock) fails... how to autorun on suspend?

systemd runs as root, but that's not relevant. the x11 authority is with your regular user, other users (incl) root cannot talk to the display server.
DISPLAY is just an environment variable, and actually the linked script more or less just exports some relevant environment variables, albeit inferring them from the foreground console that runs xinit (what's not necessarily the relevant process on your site) so you have to adapt that aspect of the script.

ftr, I reported the other post because it frankly looked more like presenting the disfunctional hook as "solution" to promote your own (unrelated, he's got trouble on resuming the system from S3, not with running the locker) problem. Sorry if that was a misinterpretation

Offline

#7 2018-04-24 05:07:29

rogerthat
Member
Registered: 2016-11-15
Posts: 56

Re: Screen lock (sflock) fails... how to autorun on suspend?

Got an update with new efforts and new obstacles...

Using the suggestion, I changed the systemd script to run as the user using the terminal (openbox), and I made sure to load the relevant environment variables from the same terminal process user in the lock wrapper script called by systemd...

#!/bin/bash
case $1 in
  pre)
    echo "sleep-lock.sh script running: $1/$2"
    pid=$(pgrep openbox)
    IFS='=' read key val < <(grep -z USER /proc/$pid/environ)
    su -c /usr/local/bin/lock - $val
    echo "sleep-lock.sh script done: $1/$2"
    ;;
esac
#!/bin/bash
echo "lock script running"
pid=$(pgrep openbox)
IFS='=' read key val < <(grep -z DISPLAY /proc/$pid/environ)
DISPLAY=$val
IFS='=' read key val < <(grep -z XAUTHORITY /proc/$pid/environ)
XAUTHORITY=$val
echo "USER=$USER"
echo "DISPLAY=$DISPLAY"
echo "XAUTHORITY=$XAUTHORITY"
export DISPLAY
export XAUTHORITY
/usr/bin/sflock -f '-*-fixed-*-*-*--13-*-*-*-*-*-*-*'

(I had an error that I posted about on another thread and solved... "sflock: cannot open dpy". Caused by missing export's.)

So no more errors, but the lock didn't happen. Journal log shows:

Apr 24 00:07:29 msi systemd[1]: Starting Suspend...
Apr 24 00:07:29 msi systemd-sleep[22010]: sleep-lock.sh script running: pre/suspend
Apr 24 00:07:29 msi su[22016]: (to roger) root on none
Apr 24 00:07:29 msi su[22016]: pam_unix(su-l:session): session opened for user roger by (uid=0)
Apr 24 00:07:30 msi systemd-sleep[22010]: lock script running
Apr 24 00:07:30 msi systemd-sleep[22010]: USER=roger
Apr 24 00:07:30 msi systemd-sleep[22010]: DISPLAY=:0
Apr 24 00:07:30 msi systemd-sleep[22010]: XAUTHORITY=/home/roger/.Xauthority
Apr 24 00:07:30 msi su[22016]: pam_unix(su-l:session): session closed for user roger
Apr 24 00:07:30 msi systemd-sleep[22010]: sleep-lock.sh script done: pre/suspend
Apr 24 00:07:30 msi systemd-sleep[22010]: Suspending system...

Researching about xwindows in general, I came across a suggestion to use xhost to allow any local user access to the display. So I tried adding that to the lock script as:

....
xhost +local:
/usr/bin/sflock -f '-*-fixed-*-*-*--13-*-*-*-*-*-*-*'

I had an error (xhost: unable to open display "") until I fixed the same error from missing export's.

But still the lock doesn't work.

Looking at the source code, I can see that sflock forks the process, so I guessed maybe it was losing access to the needed environment for the display or something like that. But I think with the above, that should all be solved. Still, the lock script works when run manually by the terminal user.

https://github.com/benruijl/sflock/blob/master/sflock.c  ::

143             /* deamonize */
144             pid = fork();
145             if (pid < 0) 
146                 die("Could not fork sflock.");
147             if (pid > 0) 
148                 exit(0); // exit parent 
149         
150         #ifndef HAVE_BSD_AUTH
151             pws = get_password();
152             username = getpwuid(geteuid())->pw_name;
153         #else
154             username = getlogin();
155         #endif
156         
157             if(!(dpy = XOpenDisplay(0)))
158         die("sflock: cannot open dpy\n");

So, I'm flummoxed...

seth wrote:

systemd runs as root, but that's not relevant. the x11 authority is with your regular user, other users (incl) root cannot talk to the display server.
DISPLAY is just an environment variable, and actually the linked script more or less just exports some relevant environment variables, albeit inferring them from the foreground console that runs xinit (what's not necessarily the relevant process on your site) so you have to adapt that aspect of the script.

So, if I've got my script running as the terminal user (not root), and have DISPLAY and XAUTHORITY in the environment, is there something required that I'm missing here?

Last edited by rogerthat (2018-04-24 05:27:20)

Offline

#8 2018-04-24 05:17:13

seth
Member
Registered: 2012-09-03
Posts: 50,012

Re: Screen lock (sflock) fails... how to autorun on suspend?

"man export", [edit: or "man env"] -  I take that you actually have no idea what you're doing and try to google together a script?

Last edited by seth (2018-04-24 05:18:15)

Offline

#9 2018-04-24 05:35:39

rogerthat
Member
Registered: 2016-11-15
Posts: 56

Re: Screen lock (sflock) fails... how to autorun on suspend?

Oh, we crossed edits. If you recheck my post, I think it comes after what you suggested already, if you were referring to the missing export's.
Thanks for your assistance. Yes, I'm learning a lot as I go. I'm not a linux administration expert. But that mistake was just due to lack of sleep. LOL

Last edited by rogerthat (2018-04-24 05:38:13)

Offline

#10 2018-04-24 12:15:18

seth
Member
Registered: 2012-09-03
Posts: 50,012

Re: Screen lock (sflock) fails... how to autorun on suspend?

This works "fine" for me (but sflock seems to be a relilable trigger to nuke the nvidia framebuffer handling, ie. my screen won't come up and changing the VT fails, but the 10 second delay offered enough time for POC)

/usr/lib/systemd/system-sleep/lock.sh

#!/bin/bash
case $1 in
	pre)
		PID=`pidof openbox`
		IFS='=' read key USER < <(grep -z USER /proc/$PID/environ)
		IFS='=' read key DISPLAY < <(grep -z DISPLAY /proc/$PID/environ)
		su -c "env DISPLAY=$DISPLAY /usr/bin/sflock" - $USER
		sleep 10
	;;
esac

Offline

#11 2018-04-24 21:49:19

rogerthat
Member
Registered: 2016-11-15
Posts: 56

Re: Screen lock (sflock) fails... how to autorun on suspend?

Thank you, Seth. I guess this sflock is not the best or something. Using your "POC" it still doesn't work.

First, I guess "env" is working better than "export" did, because the screen goes black. But once the machine is resumed, the lock flashes briefly and then disappears.

I tried adding it to the "post" case also. As long as the sleep command is running, the sceen shows a lock. But once the sleep expires, the screen unlocks again. I guess the process running the lock dies when systemd suspend finishes? I also tried forking with "&".

/usr/lib/systemd/system-sleep/sleep-lock.sh ::

#!/bin/bash
case $1 in
  pre|post)
    echo "sleep-lock.sh script running: $1/$2"
    pid=$(pgrep openbox)
    IFS='=' read key user < <(grep -z USER /proc/$pid/environ)
    su -c /usr/local/bin/lock - $user
    #su -c /usr/local/bin/lock - $user &
    /usr/bin/sleep 5
    echo "sleep-lock.sh script done: $1/$2"
    ;;
esac

/usr/local/bin/lock ::

#!/bin/bash
echo "lock script running"
pid=$(pgrep openbox)
IFS='=' read key display < <(grep -z DISPLAY /proc/$pid/environ)
env DISPLAY=$display /usr/bin/sflock -f '-*-fixed-*-*-*--13-*-*-*-*-*-*-*'
echo "lock script finished"

Logs are all good though:

Apr 24 12:01:30 msi systemd[1]: Starting Suspend...
Apr 24 12:01:30 msi systemd-sleep[1340]: Suspending system...
Apr 24 12:01:44 msi systemd-sleep[1340]: System resumed.
Apr 24 12:01:44 msi systemd-sleep[1340]: sleep-lock.sh script running: post/suspend
Apr 24 12:01:44 msi su[1350]: (to roger) root on none
Apr 24 12:01:44 msi su[1350]: pam_unix(su-l:session): session opened for user roger by (uid=0)
Apr 24 12:01:44 msi systemd-sleep[1340]: lock script running
Apr 24 12:01:44 msi systemd-sleep[1340]: lock script finished
Apr 24 12:01:44 msi su[1350]: pam_unix(su-l:session): session closed for user roger
Apr 24 12:01:49 msi systemd-sleep[1340]: sleep-lock.sh script done: post/suspend                       <-- SCREEN UNLOCKS ITSELF HERE
Apr 24 12:01:49 msi systemd[1]: Started Suspend.

Last edited by rogerthat (2018-04-24 21:58:22)

Offline

Board footer

Powered by FluxBB