You are not logged in.

#1 2020-09-20 08:36:20

justasug
Member
Registered: 2014-08-03
Posts: 174

[SOLVED] Automatic login causing logout problems

I wanted my computer to automatically log-in and go straight to an X session after turning it on. I have accomplished that using automatic login into a virtual console (with systemd and agetty) and automatic starting of X (with .bash_profile running startx).

The problem is that when I log out (exit) the X session, it restarts instantly because bash_profile runs startx again. I solved that by creating a file before starting X and then preventing X from starting if that file exists.

My xinitrc:

#!/bin/sh
if [ -d /etc/X11/xinit/xinitrc.d ] ; then
 for f in /etc/X11/xinit/xinitrc.d/?*.sh ; do
  [ -x "$f" ] && . "$f"
 done
 unset f
fi

touch "/tmp/logged-in.lock"
exec openbox-session

My bash_profile:

#
# ~/.bash_profile
#
[[ -f ~/.bashrc ]] && . ~/.bashrc

if systemctl -q is-active graphical.target && \
  [[ ! $DISPLAY && $XDG_VTNR -eq 1 ]] && \
  [[ ! -e /tmp/logged-in.lock ]];  then
  [[ $(fgconsole 2>/dev/null) == 1 ]] && exec startx &> /dev/null
fi

I then applied the same "lock-file" principle to the automatic login into the virtual console. The problem is that when I log out, there's just an unresponsive cursor on a black screen. There is no login prompt. I assume that means that the getty service isn't running?

Modified getty.service file utilizing ConditionPathExists that doesn't work:

[Unit]
ConditionPathExists=!/tmp/logged-in.lock

[Service]
ExecStart=
ExecStart=-/usr/bin/agetty --skip-login --autologin dino --nonewline --noissue --noclear %I $TERM
Type=simple

Does anyone know how to solve this? I want it to log out back to a login prompt. I have found this thread and that person called a shell script in agetty's service file. I was wondering if there is a "cleaner" way to accomplish this, without abstracting it to another shell script.

Last edited by justasug (2020-09-23 08:55:53)

Offline

#2 2020-09-20 11:04:50

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

Re: [SOLVED] Automatic login causing logout problems

The problem is that when I log out (exit) the X session, it restarts instantly because bash_profile runs startx again.

This is because exec startx replaced current shell with starx. You want to keep it:

[[ ! $DISPLAY && $XDG_VTNR -eq 1 ]] && startx 

Offline

#3 2020-09-20 17:59:08

justasug
Member
Registered: 2014-08-03
Posts: 174

Re: [SOLVED] Automatic login causing logout problems

I have tried to remove the "exec" before startx in my bash_profile as you mentioned, but I'm still getting the same behaviour. Upon exiting X, there's just a cursor (underscore) on a black screen. Am I missing something else or did I do it wrongly?

Offline

#4 2020-09-20 21:25:50

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,724

Re: [SOLVED] Automatic login causing logout problems

What removes /tmp/logged-in.lock ?
If the answer is (likely) "nothing", it should not be an agetty condition.

Afaiu you want to autologin and autostart  X11 but then be able to log out w/o re-starting X11 (but remain in the console)?
In that case you want to create the lock w/ the first login, not remove it, but also not use it in the agetty condition.
Also you can exec startx - when exiting X11 you'll exit the login, re-login automatically (because of the altered agetty) but not startx (because of the logged-in.lock condition in bashrc)
If you do not want to auto-login again at all (but face a console login) you'll have to condition the agetty "--skip-login --autologin dino" switches w/ the logged-in.lock

Offline

#5 2020-09-20 22:00:28

justasug
Member
Registered: 2014-08-03
Posts: 174

Re: [SOLVED] Automatic login causing logout problems

seth wrote:

What removes /tmp/logged-in.lock ?
If the answer is (likely) "nothing", it should not be an agetty condition.

I thought that it's okay to clear it on every reboot.

seth wrote:

Afaiu you want to autologin and autostart  X11 but then be able to log out w/o re-starting X11 (but remain in the console)?

Yes, I want it to go back to the console, with the login prompt.

seth wrote:

If you do not want to auto-login again at all (but face a console login) you'll have to condition the agetty "--skip-login --autologin dino" switches w/ the logged-in.lock

Can you elaborate on the "condition the agetty"? I looked through the options for agetty, but couldn't find anything which would allow me to incorporate the lock-file. Do you mean as in the thread mentioned above, with a shell script which would run agetty with different options depending on the existence of the lock-file?

Offline

#6 2020-09-20 22:19:42

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,724

Re: [SOLVED] Automatic login causing logout problems

Do you mean as in the thread mentioned above

Yes. Whether that's the only/cleanest way depends on the exact behavior you want to create.

Offline

#7 2020-09-21 02:29:45

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

Re: [SOLVED] Automatic login causing logout problems

I think OP wants This:
1, Automatically log-in and go straight to an X session after turning it on (cold start/restart).
2, log out back to a login prompt (able to login as another user).
@justasug If this is what you want, the following methods have been tested working:

$ cat /usr/local/bin/conditionlogin.sh
#!/usr/sbin/env /usr/sbin/bash
tty.conditionlogin ()
{
    local lock=/run/lock/login.lock;
    local tty=${1:-tty1};
    local term=${2:-$TERM};
    [[ -e $lock ]] && {
        /usr/sbin/agetty --noclear $tty $term;
        return
    };
    /usr/sbin/touch $lock;
    /usr/sbin/agetty --autologin dino --noclear $tty $term
}
tty.conditionlogin "$@"

$ cat /etc/systemd//system/getty\@tty1.service.d/override.conf
[Service]
ExecStart=
ExecStart=/usr/local/bin/conditionlogin.sh %I $TERM

$ cat .bash_profile
[[ -f ~/.bashrc ]] && . ~/.bashrc
[[ -z $DISPLAY && $XDG_VTNR -eq 1 ]] && exec startx

Last edited by solskog (2020-09-21 02:33:13)

Offline

#8 2020-09-22 21:40:32

justasug
Member
Registered: 2014-08-03
Posts: 174

Re: [SOLVED] Automatic login causing logout problems

Thank you solskog, it's working reasonably so far.

I have run into an issue though. I wanted to stick to the default agetty options that are used in the service file that comes with the package. It has

[Service]
# the VT is cleared by TTYVTDisallocate
# The '-o' option value tells agetty to replace 'login' arguments with an
# option to preserve environment (-p), followed by '--' for safety, and then
# the entered username.
ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM

When I add the -o '-p --\\u' part, it's not logging in automatically; it gives me a prompt for a password. I tried to look into what that means (together with the explanation in the comments above the line. I am worried that not preserving the environment will cause some issues down the line since I'm straying from the default options that come with the package.
I am not understanding the consequences and which environment is meant by that option. Does anyone know what it means?

seth wrote:

What removes /tmp/logged-in.lock ?
If the answer is (likely) "nothing", it should not be an agetty condition.

What's the preferred location for user lock files? I can't create the lock-file upon starting X, because my user has no write permissions for /run/lock/. Is /run/user/my_id/ a better location? That gets cleared on every reboot, but doesn't /tmp too?

Last edited by justasug (2020-09-22 21:45:36)

Offline

#9 2020-09-22 22:03:44

seth
Member
From: Won't reply 2 private help req
Registered: 2012-09-03
Posts: 77,724

Re: [SOLVED] Automatic login causing logout problems

The point is not the location, but that the files existence will block the agetty call, so it will not be re-executed after the session ends but leaves the lockfile behind.

The default getty doesn't provide an autologin, thus the override.

Offline

#10 2020-09-22 23:27:53

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

Re: [SOLVED] Automatic login causing logout problems

You can still keep the default getty options inside the script, You can even squeeze the content of this script inside getty\@tty1.service file and get rid of the script. how about this?

$ cat /etc/systemd//system/getty\@tty1.service.d/override.conf

[Service]
ExecStart=
ExecStart=-/bin/bash -c "test -e /run/lock/login.lock && /sbin/agetty -o '-p -- \u' --noclear %I $TERM || { /sbin/touch /run/lock/login.lock; /sbin/agetty --autologin dino --noclear %I $TERM; }"
justasug wrote:

I am not understanding the consequences and which environment is meant by that option. Does anyone know what it means?

The -o/--login-options are passed to program 'login'

$ man login
The environment variable $TERM will be preserved, if it exists (other  environment  variables  are preserved if the -p option is given), else it will be initialized to the terminal type on your tty.

Therefor you only need to preserve these environment when you log out back to a login prompt thus.

 
test -e /run/lock/login.lock && /sbin/agetty -o '-p -- \u' --noclear %I $TERM

And you shouldn't do that on autologin thus.

 
/sbin/agetty --autologin dino --noclear %I $TERM 

The '--' means the end of the command arguments, and is there to prevent argument injection.

justasug wrote:

What's the preferred location for user lock files?

Since the getty@tty1.servce is controlled by root, the best location is /run/lock/. /tmp isn't always a tmpfs/RAM type. It could be a normal disk partition that never been cleared up. /run/user/UID is created after user login, which means you have to rely on bash_profile. Another location could be /dev/shm, which always use RAM.

Furthermore, instead using override.conf, I replaced 'ExecStart=' line from the orignal /lib/systemd/system/getty\@.service with my suggested 'ExecStart=' line and kept everyting else intact. And it just worked!

It worked with a warning though:

agetty[358]: /dev/tty1: cannot get controlling tty: Operation not permitted
agetty[358]: /dev/tty1: cannot set process group: Inappropriate ioctl for device

Last edited by solskog (2020-09-23 05:54:49)

Offline

#11 2020-09-23 08:55:37

justasug
Member
Registered: 2014-08-03
Posts: 174

Re: [SOLVED] Automatic login causing logout problems

Thanks for the help everyone, it's working now and seems like an acceptable solution.

Offline

Board footer

Powered by FluxBB