Professional Web Applications Themes

/dev/tty vs. /dev/stdout - UNIX Programming

I need to find out whether /dev/tty is talking to the same file/device as stdout or stderr. Assuming they're both character devices, I thought I could just fstat() them and compare the st_rdev field for equality. Unfortunately, on my Linux machine, /dev/tty is 5:0 and /dev/stdout is 136:16 (/dev/pts/16). So much for st_rdev. Is there any reasonably portable way to find out whether two file descriptors point to the same TTY ? -- André Majorel <URL:http://www.teaser.fr/~amajorel/> Agree with them now, it will save so much time....

  1. #1

    Default /dev/tty vs. /dev/stdout

    I need to find out whether /dev/tty is talking to the same
    file/device as stdout or stderr. Assuming they're both character
    devices, I thought I could just fstat() them and compare the
    st_rdev field for equality. Unfortunately, on my Linux machine,
    /dev/tty is 5:0 and /dev/stdout is 136:16 (/dev/pts/16). So much
    for st_rdev.

    Is there any reasonably portable way to find out whether two
    file descriptors point to the same TTY ?

    --
    André Majorel <URL:http://www.teaser.fr/~amajorel/>
    Agree with them now, it will save so much time.
    Andre Guest

  2. #2

    Default Re: /dev/tty vs. /dev/stdout

    > I need to find out whether /dev/tty is talking to the same 

    Don't know if this helps any, but on my system:
    /dev/stdout -> fd/1

    (File descriptor 1)

    --
    Jem Berkes
    http://www.sysdesign.ca/
    Jem Guest

  3. #3

    Default Re: /dev/tty vs. /dev/stdout

    Jem Berkes <pc9.org> writes:
     
    >
    > Don't know if this helps any, but on my system:
    > /dev/stdout -> fd/1[/ref]

    On my Linux system /dev/fd/1 is a link to /proc/self/fd/1, which is a
    link to /dev/pts/0.

    --
    Måns Rullgård
    se
    Måns Guest

  4. #4

    Default Re: /dev/tty vs. /dev/stdout

    Andre Majorel <fr> wrote: 

    Why? More to the point: why is it important that it's precisely
    /dev/tty, in particular --- wouldn't it suffice to know that it's
    _some_ tty? That you could check by the isatty() function.

    --
    Hans-Bernhard Broeker (rwth-aachen.de)
    Even if all the snow were burnt, ashes would remain.
    Hans-Bernhard Guest

  5. #5

    Default Re: /dev/tty vs. /dev/stdout

    2004-02-8, 18:35(+00), Andre Majorel: 

    On a Linux system you can read /proc/self/stat
    Seventh field is
    ((major & 0xff) << 8) | (minor & 0xff) | ((minor & ~0xff) << 12)

    15094 (vim) R 128 128 128 34820 128 0 349 0 647 0 213 33 0 0 10 0 0 0 5107993 6848512 681 4294967295 134512640 135291951 3221219712 3221217824 1075036328 0 0 2109440 1870679807 0 0 0 17 0

    34820 == 136 << 8 + 4
    (/dev/pts/4).

    --
    Stéphane ["Stephane.Chazelas" at "free.fr"]
    Stephane Guest

  6. #6

    Default Re: /dev/tty vs. /dev/stdout

    2004-02-9, 00:44(+01), Stephane CHAZELAS:
    [...] 

    ((major & 0xfff) << 8) | (minor & 0xff) | ((minor & ~0xff) << 12)

    Actually:

    mmmMMMmm
    0xffffffff

    in 2.6.

    in 2.4, it was

    MMmm
    0xffff

    ((major & 0xff) << 8) | (minor & 0xff)

    See /usr/include/linux/kdev_t.h

    --
    Stéphane ["Stephane.Chazelas" at "free.fr"]
    Stephane Guest

  7. #7

    Default Re: /dev/tty vs. /dev/stdout

    Thanks for your replies. I guess I should clarify.

    I have a program that writes to stdout and stderr and, since it
    can run for a long time without printing any output, also writes
    a periodic progress report to the terminal (/dev/tty).

    When stdout and/or stderr are not redirected, they also talk to
    the terminal. The program needs to detect this condition. The
    problem is how.

    In a perfect world, we could fstat() the file descriptors and
    compare st_rdev. Unfortunately, on Linux, the same tty device
    has different device numbers depending on whether you see it
    from /dev/tty or from /dev/stdout. There doesn't seem to be a
    way to bridge the gap between the /dev/tty and /dev/pts/##
    views. Stéphane has provided a solution but it involves #ifdefs.

    This is a not a solve-my-problem question. I'm not looking for a
    workaround, or a platform-specific hack. I'm looking for
    education. If anybody knows whether there's a portable way to
    find out whether two arbitrary FDs point to the same terminal,
    please enlighten me because I'm stumped.

    --
    André Majorel <URL:http://www.teaser.fr/~amajorel/>
    Agree with them now, it will save so much time.
    Andre Guest

  8. #8

    Default Re: /dev/tty vs. /dev/stdout

    Andre Majorel wrote: 

    Are you sure your program always will be associated with a tty?

    You could redirect stdout to /dev/tty programatically if isatty(1).
    This would allow redirection to files, but guarantee the answer to your
    question. Of course, delicate doentation would be in order.

    Jeff Guest

  9. #9

    Default Re: /dev/tty vs. /dev/stdout



    Jeff Schwab wrote:
     
    >
    >
    > Are you sure your program always will be associated with a tty?
    >
    > You could redirect stdout to /dev/tty programatically if isatty(1). This
    > would allow redirection to files, but guarantee the answer to your
    > question. Of course, delicate doentation would be in order.
    > [/ref]

    Good point Jeff!

    I note from the man pages that there is also a ttyname(2) call
    which returns the character string with the ttyname for a file
    descriptor or NULL if it's not a tty. The combination of isatty(2)
    or ttyname(2) may do the trick.


    --
    Ñ
    "It is impossible to make anything foolproof because fools are so
    ingenious" - A. Bloch

    Nick Guest

  10. #10

    Default Re: /dev/tty vs. /dev/stdout

    2004-02-9, 02:02(+00), Andre Majorel:
    [...] 
    [...]

    You could use tcgetpgrp. I don't think a process group can be a
    foreground group of several ttys. At least, it is very unlikely.

    To my mind, checking wether stdout isatty should be enough for
    your purpose.

    --
    Stéphane ["Stephane.Chazelas" at "free.fr"]
    Stephane Guest

  11. #11

    Default Re: /dev/tty vs. /dev/stdout


    Stephane> [...]
    Stephane> See /usr/include/linux/kdev_t.h

    Still not perfectly portable, but better than Stephane's solution:
    fork off a copy of ps(1) and p the output.

    --
    Nothing can be explained to a stone.
    Or to a stoned person, either.
    Ian Guest

Similar Threads

  1. nohup stdout
    By sukhpreet in forum Linux / Unix Administration
    Replies: 1
    Last Post: August 4th, 03:17 PM
  2. tie STDOUT to TextUndo
    By lobach@email.chop.edu in forum PERL Beginners
    Replies: 0
    Last Post: January 21st, 04:57 PM
  3. Redirect stdout, stderr to file and stdout
    By Larry Guest in forum PERL Beginners
    Replies: 8
    Last Post: January 20th, 10:42 AM
  4. redirect stdout
    By T. Onoma in forum Ruby
    Replies: 10
    Last Post: November 28th, 07:31 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139