core: when setting up PAM, try to get tty of STDIN_FILENO if not set explicitly

When stdin/stdout/stderr is initialized from an fd, let's read the tty
name of it if we can, and pass that to PAM.

This makes sure that "machinectl shell" sessions have proper TTY fields
initialized that "loginctl" then shows.
This commit is contained in:
Lennart Poettering 2018-08-03 21:30:16 +02:00 committed by Zbigniew Jędrzejewski-Szmek
parent 37ec0fdd34
commit 3cd24c1aa9
1 changed files with 10 additions and 0 deletions

View File

@ -1158,6 +1158,16 @@ static int setup_pam(
goto fail;
}
if (!tty) {
_cleanup_free_ char *q = NULL;
/* Hmm, so no TTY was explicitly passed, but an fd passed to us directly might be a TTY. Let's figure
* out if that's the case, and read the TTY off it. */
if (getttyname_malloc(STDIN_FILENO, &q) >= 0)
tty = strjoina("/dev/", q);
}
if (tty) {
pam_code = pam_set_item(handle, PAM_TTY, tty);
if (pam_code != PAM_SUCCESS)