bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#64937: "who" reports funny dates


From: Bruno Haible
Subject: bug#64937: "who" reports funny dates
Date: Wed, 02 Aug 2023 01:16:44 +0200

Thorsten Kukuk wrote:
> If you haven't seen yet, I made some time ago a mapping
> between utmp struct entries and libsystemd functions: 
> https://github.com/thkukuk/utmpx/blob/main/utmp-to-logind.md

Thanks. This is helpful.

The only problem with this mapping is for the ut_line row, which
used to contain, for inbound ssh, the pty device name. It is not easy
to find the pty name in this situation; at least, none of the systemd
APIs and /run/systemd/** files that I looked at helped.

> But be aware:
> 1. you need to extend ut_tv from 32bit time_t to 64bit time_t, or your
> patch will not survive 2038.

This is already done by the 'year2038' module of Gnulib, which
coreutils and many other packages use.

> 2. the string entries are size limited in struct utmp, but are not with
> libsystemd. So could be that you need to truncate device names, user
> names and host names.

Thanks for the hint. Truncating would mean to introduce arbitrary limits,
which is against the GNU Coding Standards
<https://www.gnu.org/prep/standards/html_node/Semantics.html>. Hence
we'll happily use arbitrary-length strings in our code.

Paul Eggert wrote:
> This suggests that Gnulib needs to redo the readutmp API so that it's 
> not tied to struct utmpx's fixed string lengths. That's a bit of a 
> hassle but should be doable (change char array to char *). An advantage 
> is that Gnulib is a source-code library and so we can make binary- (and 
> even source-) incompatible changes to the API without too much trouble.

For the moment, I've extended the 'readutmp' module to use arbitrary-
length strings only when compiling with libsystemd.

Here are the changes I committed in gnulib.


2023-08-01  Bruno Haible  <bruno@clisp.org>

        readutmp: Small changes to reduce the code size on the coreutils side.
        * m4/readutmp.m4 (gl_READUTMP): Test also for the ut_host field in
        'struct utmpx' and 'struct utmp'.
        * lib/readutmp.h (HAVE_STRUCT_XTMP_UT_HOST): New macro.
        (UT_USER_SIZE): Define also as a macro. Set to -1 if
        READUTMP_USE_SYSTEMD.
        (UT_LINE_SIZE, UT_HOST_SIZE): New constants and macros.

2023-08-01  Bruno Haible  <bruno@clisp.org>

        readutmp: For year-2038 safety on Linux/{x86,arm}, use systemd APIs.
        Suggested by Thorsten Kukuk <kukuk@suse.com> in
        <https://www.thkukuk.de/blog/Y2038_glibc_utmp_64bit/> and
        <https://github.com/thkukuk/utmpx/blob/main/utmp-to-logind.md>.
        * m4/systemd.m4: New file.
        * m4/readutmp.m4 (gl_READUTMP): Require gl_SYSTEMD_CHOICE. Set
        READUTMP_LIB. Conditionally define READUTMP_USE_SYSTEMD.
        * lib/readutmp.h: For READUTMP_USE_SYSTEMD, include <sys/time.h> and
        <utmpx.h>.
        (struct gl_utmp): New type.
        (UTMP_STRUCT_NAME, UT_TIME_MEMBER, UT_EXIT_E_TERMINATION,
        UT_EXIT_E_EXIT, UT_USER, HAVE_STRUCT_XTMP_UT_EXIT,
        HAVE_STRUCT_XTMP_UT_ID, HAVE_STRUCT_XTMP_UT_PID): Define differently for
        READUTMP_USE_SYSTEMD.
        (UT_USER_SIZE): Don't define for READUTMP_USE_SYSTEMD.
        (UT_TYPE_EQ, UT_TYPE_NOT_DEFINED, READ_UTMP_SUPPORTED): Define also for
        READUTMP_USE_SYSTEMD.
        (free_utmp): New declaration.
        * lib/readutmp.c: Add new includes for READUTMP_USE_SYSTEMD.
        (extract_trimmed_name): Adapt to READUTMP_USE_SYSTEMD.
        (get_boot_time_uncached, get_boot_time, guess_pty_name): New functions.
        (read_utmp): New implementation for READUTMP_USE_SYSTEMD.
        (free_utmp): New function.
        * tests/test-readutmp.c (main): At the end, invoke free_utmp.
        * modules/readutmp (Files): Add m4/systemd.m4.
        (Link): New section.
        * modules/readutmp-tests (Makefile.am): Link test-readutmp with
        READUTMP_LIB.
        * NEWS: Mention the free_utmp function and the READUTMP_LIB link
        requirement.


When the output of the 'test-readutmp' program without libsystemd is
 ==============================================================================
Here are the read_utmp results.
Flags: B = Boot, U = User Process

                                                              Termi‐      Flags
    Time (GMT)             User          Device        PID    nation Exit  B U
------------------- ------------------ ----------- ---------- ------ ----  - -
2023-07-30 09:25:15 reboot             ~                    0    0     0   X  
2023-07-30 09:25:31 runlevel           ~                   53    0     0      
2023-07-30 09:25:48 bruno              seat0             1663    0     0     X
2023-07-30 09:25:48 bruno              tty2              1663    0     0     X
2023-07-30 09:31:17                    pts/1             3210    0     0      
2023-07-31 23:36:52 bruno              pts/3            30619    0     0     X
2023-08-01 15:19:26 other              pts/4            40513    0     0     X
2023-08-01 19:02:20                    pts/5            43376    0     1      
 ==============================================================================

with libsystemd it is:

 ==============================================================================
Here are the read_utmp results.
Flags: B = Boot, U = User Process

                                                              Termi‐      Flags
    Time (GMT)             User          Device        PID    nation Exit  B U
------------------- ------------------ ----------- ---------- ------ ----  - -
2023-07-30 09:25:14 reboot             ~                    0    0     0   X  
2023-07-30 09:25:47 bruno              seat0             1593    0     0     X
2023-07-30 09:25:47 bruno              tty2              1593    0     0     X
2023-08-01 15:19:25 other              sshd pts/4       40513    0     0     X
 ==============================================================================

The major changes are:
* Entries without BOOT_TIME or USER_PROCESS flag are gone. This includes
  - runlevel and possibly "old time" / "new time" entries, which I don't know
    how to get from systemd,
  - entries for pseudo-terminals (xterm etc.) that are not login shells;
    I don't know how to get them from systemd either, and (IMO) they were
    uninteresting anyway,
  - login shells that have exited.
* The reboot time is a little more precise, since the systemd code uses
  the /proc/uptime virtual file.
* The pids now refer to the session leader, which is a parent (or ancestor)
  of the process which has allocated the pty.
* In the ut_line field ("Device" column) I added extra info about the
  service name, in this case "sshd". I imagine that is at least as useful as
  knowing the pty name ("pts/4").

Attachment: 0001-readutmp-For-year-2038-safety-on-Linux-x86-arm-use-s.patch
Description: Text Data

Attachment: 0002-readutmp-Small-changes-to-reduce-the-code-size-on-th.patch
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]