Skip to content

Conversation

@alejandro-colomar
Copy link
Collaborator

@alejandro-colomar alejandro-colomar commented Oct 24, 2025

Let's keep it simple.


Revisions:

v2
  • Fix intermediate commit. [@hallyn ]
$ git rd 
1:  0d6e5f800 = 1:  0d6e5f800 lib/utmp.c: is_my_tty(): Don't cache ttyname(3).
2:  fa3a25919 ! 2:  120ad06c1 lib/utmp.c: is_my_tty(): Rename local variable
    @@ lib/utmp.c: static bool
        strncat(full_tty, tty, UTX_LINESIZE);
      
     -  tmptty = ttyname(STDIN_FILENO);
    +-  if (NULL != tmptty)
     +  my_tty = ttyname(STDIN_FILENO);
    -   if (NULL != tmptty)
    ++  if (NULL != my_tty)
                (void) puts (_("Unable to determine your tty name."));
                exit (EXIT_FAILURE);
        }
3:  b7b40d6a5 ! 3:  379d10c2d lib/utmp.c: is_my_tty(): Use ttyname_r(3) to make it re-entrant
    @@ lib/utmp.c
        strncat(full_tty, tty, UTX_LINESIZE);
      
     -  my_tty = ttyname(STDIN_FILENO);
    --  if (NULL != tmptty)
    +-  if (NULL != my_tty)
     +  if (ttyname_r(STDIN_FILENO, my_tty, countof(my_tty)) != 0) {
                (void) puts (_("Unable to determine your tty name."));
                exit (EXIT_FAILURE);
4:  993fc4e7f = 4:  2fc993adb lib/utmp.c: ttyname_ra(): Add macro
v2b
$ git rd 
1:  0d6e5f800 ! 1:  a1cb667e7 lib/utmp.c: is_my_tty(): Don't cache ttyname(3).
    @@ Commit message
         ttyname(3), we use it directly, without needing a temporary copy, which
         removes opportunities for bugs.
     
    +    Reviewed-by: Serge Hallyn <serge@hallyn.com>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## lib/utmp.c ##
2:  120ad06c1 ! 2:  c4ca166ba lib/utmp.c: is_my_tty(): Rename local variable
    @@ Commit message
     
         This name makes the function definition more readable.
     
    +    Reviewed-by: Serge Hallyn <serge@hallyn.com>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## lib/utmp.c ##
3:  379d10c2d ! 3:  6ac300300 lib/utmp.c: is_my_tty(): Use ttyname_r(3) to make it re-entrant
    @@ Metadata
      ## Commit message ##
         lib/utmp.c: is_my_tty(): Use ttyname_r(3) to make it re-entrant
     
    +    Reviewed-by: Serge Hallyn <serge@hallyn.com>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## lib/utmp.c ##
4:  2fc993adb ! 4:  7d02a222d lib/utmp.c: ttyname_ra(): Add macro
    @@ Commit message
     
         This macro enforces correct use of ttyname_r(3) with arrays.
     
    +    Reviewed-by: Serge Hallyn <serge@hallyn.com>
         Signed-off-by: Alejandro Colomar <alx@kernel.org>
     
      ## lib/utmp.c ##

@alejandro-colomar alejandro-colomar changed the title is_my_tty(): Fix and simplify lib/utmp.c: is_my_tty(): Fix and simplify Oct 24, 2025
@alejandro-colomar alejandro-colomar force-pushed the mytty branch 2 times, most recently from b4a9595 to be26914 Compare October 24, 2025 10:26
@alejandro-colomar alejandro-colomar changed the title lib/utmp.c: is_my_tty(): Fix and simplify lib/utmp.c: is_my_tty(): Simplify Oct 24, 2025
@alejandro-colomar alejandro-colomar force-pushed the mytty branch 6 times, most recently from 3c46edb to 993fc4e Compare October 27, 2025 13:53
@alejandro-colomar alejandro-colomar marked this pull request as ready for review October 27, 2025 13:54
@hallyn
Copy link
Member

hallyn commented Nov 4, 2025

I know it doesn't affect the end result, but patch two can't compile because you didn't change all instances of the ttyname variable.

@alejandro-colomar
Copy link
Collaborator Author

I know it doesn't affect the end result, but patch two can't compile because you didn't change all instances of the ttyname variable.

Oops, my bad. Thanks! Will fix.

@alejandro-colomar
Copy link
Collaborator Author

I know it doesn't affect the end result, but patch two can't compile because you didn't change all instances of the ttyname variable.

Done. Thanks!

(I suspect it was the result of reordering commits.)

The method for checking for truncation was quite weird.  By not caching
ttyname(3), we use it directly, without needing a temporary copy, which
removes opportunities for bugs.

Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
This name makes the function definition more readable.

Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
And use it instead of its pattern.

This macro enforces correct use of ttyname_r(3) with arrays.

Reviewed-by: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@alejandro-colomar alejandro-colomar merged commit 9bf3ddc into shadow-maint:master Nov 4, 2025
10 checks passed
@alejandro-colomar alejandro-colomar deleted the mytty branch November 4, 2025 02:18
@alejandro-colomar alejandro-colomar self-assigned this Dec 27, 2025
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