Skip to content

Conversation

@Karlson2k
Copy link
Contributor

@Karlson2k Karlson2k commented Aug 10, 2025

This is a correction for PR #1292 (and is an alternative version of PR #1326; this PR includes a full revert as the first commit.)

After additional code and documentation analysis I found out that copy of the pointer is not enough, a deep copy must be used instead.
I also checked glibc sources to be absolutely sure.

As allocation is needed for every successful match (either full or partial), I restructured the code to avoid code duplication.

I think this kind of code could be improved with additional comments, as currently it is not fully clear that match by PID has priority over match by line (tty).

Last two commits could be squashed into one commit.

This reverts commit 8417765.

The pointer returned by getutxent() function may always point to
the same shared and reused buffer.

Instead of copying the utmp entry pointer value the content of utmp
entry must be copied otherwise the next call of getutxent() will
overwrite previously found entry.

Signed-off-by: Evgeny Grin (Karlson2k) <[email protected]>
Updated utmp entry search algorithm to follow GNU/Linux description:
https://man7.org/linux/man-pages/man5/utmp.5.html#DESCRIPTION

An entry is found by looking for matching PID.  If several such entries
found (for example, due to cleanup failure of old entries) then first
entry with both matching PID and matching 'ut_line' (current terminal)
is used.  If not entry has matching 'ut_line' then first entry with
matching PID is used (if getty/init process does not set 'ut_line').

When no single entry is matched by PID, then but at least one entry is
matched current terminal the the first such entry is selected (if getty
does not set correct PID).

This commit uses non-portable Elvis operator is it is already used
everywhere in the code.

Signed-off-by: Evgeny Grin (Karlson2k) <[email protected]>
Instead of using up to three allocation every time when
get_current_utmp() function is called, the maximum a single allocation
is used.

A minor code optimisation is applied: the match by "line" is not checked
if entry matched by PID has been already found.

Fixes: 8417765 (14-07-2025; "lib/utmp.c: Fix umtp entry search")
Signed-off-by: Evgeny Grin (Karlson2k) <[email protected]>
@Karlson2k Karlson2k changed the title Utmp entry correction alt1 lib/utmp.c: Fix use of last utmp entry instead of patrial-match entry Aug 10, 2025
@alejandro-colomar
Copy link
Collaborator

We merged the alternative implementation without reverting.

@Karlson2k Karlson2k deleted the utmp_entry_correction-alt1 branch August 10, 2025 14:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants