Saving file descriptors

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Saving file descriptors

    Hello,

    I have the following problem,

    I close fileno(stdout) and fileno(stderr), then dup() the closed
    descriptors to the descriptor pointing "/dev/null", so there was no output
    from the program.

    But now, at certain situation i want to restore stdout and stderr, so i
    could output something to stderr or stdout.

    How to do it?

    I tried saving stdout and stderr, both as file pointers or just
    descriptor numbers and later restoring them, but it didnt work.


    P.Krumins
    Peteris Krumins Guest

  2. Similar Questions and Discussions

    1. Type Descriptors, Converters and designer serialization
      (Type your message here) Hi ive built a custom menubar control that uses a strongly typedHashtable to store the colours needed to paint the...
    2. File Descriptors
      Greetings, all. Can anyone tell me if there are any risks associated with increasing the number of file descriptors for an application/user...
    3. Displaying Open File Descriptors for a Process
      Is there a system command a user can execute to display the current number of file descriptors a process is using? Thanks, Peter
    4. select() max number of file descriptors
      It seems that my system's select call has a limitation of 1024 open file descriptors. I need to learn about this restriction as I am programming...
    5. increasing file descriptors (Solaris 8) on the fly
      /bin/sh: % ulimit -a nofiles(descriptors) 256 % ulimit -n 1024 % ulimit -a nofiles(descriptors) 1024 See "man ulimit"
  3. #2

    Default Re: Saving file descriptors

    In article <Xns93BC42D72A49whitesuneapollolv@130.133.1.4>,
    Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:
    > But now, at certain situation i want to restore stdout and stderr, so i
    >could output something to stderr or stdout.
    >
    > How to do it?
    dup() them before closing them. Then dup2() them back to restore.

    --
    Barry Margolin, [email]barry.margolin@level3.com[/email]
    Level(3), Woburn, MA
    *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
    Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
    Barry Margolin Guest

  4. #3

    Default Re: Saving file descriptors

    Barry Margolin <barry.margolin@level3.com> wrote in
    news:HIERa.256$0z4.155@news.level3.com:
    > In article <Xns93BC42D72A49whitesuneapollolv@130.133.1.4>,
    > Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:
    >> But now, at certain situation i want to restore stdout and stderr,
    >> so i
    >>could output something to stderr or stdout.
    >>
    >> How to do it?
    >
    > dup() them before closing them. Then dup2() them back to restore.
    >
    Thanks!


    P.Krumins
    Peteris Krumins Guest

  5. #4

    Default Re: Saving file descriptors

    Barry Margolin wrote:
    >
    > In article <Xns93BC42D72A49whitesuneapollolv@130.133.1.4>,
    > Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:
    > > But now, at certain situation i want to restore stdout and stderr, so i
    > >could output something to stderr or stdout.
    > >
    > > How to do it?
    >
    > dup() them before closing them. Then dup2() them back to restore.
    If there's any chance that output was generated with
    the <stdio.h> functions (printf(), putc(), perror(), ...),
    you'll also need to "synchronize" the stdio buffers with
    the file descriptors. The best way to do this is probably

    dup() the original descriptors
    fclose() both streams
    fdopen() both streams to the "/dev/null" descriptor
    ...
    fclose() both streams
    dup2() to restore the descriptors
    fdopen() both streams to the original descriptors,
    possibly using "a" instead of "w".

    Another thing: if you ever seek employment with a sane
    software company, don't admit that you implemented this awful
    mess. The interviewer will admire your ingenuity, but will
    make the sign of the Cross as you depart and will offer the
    job to somebody else. (1/2 ;-)

    --
    [email]Eric.Sosman@sun.com[/email]
    Eric Sosman Guest

  6. #5

    Default Re: Saving file descriptors

    Eric Sosman <Eric.Sosman@sun.com> writes:
    >> The best way is just to use fflush() before messing with the
    >> underlying file descriptors.
    > If I understand you correctly, your suggestion amounts to
    >using fflush() to drain buffers, then fiddling with the file
    >descriptors "behind the back" of the stdio implementation
    >and proceeding as if nothing untoward had happened.
    > This will most likely work on many implementations, but it
    >also embodies some guesses about how the implementation works,
    >guesses that (AFAIK) aren't promises by any of the relevant
    >standards. For example, a FILE* must mediate between the
    >different notions of "current position" that ftell() and
    >lseek() return.
    Oops, yes I forgot about the offset. In some circumstances
    you also need to do an fseek() after fiddling with the
    file descriptors. The circumstances are defined by POSIX in
    the (lengthy) description of how to coordinate the use of
    multiple "handles" (file descriptors and stdio streams) that
    refer to the same open file description:

    "If any previous active handle has been used by a function that
    explicitly changed the file offset, except as required above for
    the first handle, the application shall perform an lseek() or
    fseek() (as appropriate to the type of handle) to an appropriate
    location."

    --
    Geoff Clare <nospam@gclare.org.uk>
    Geoff Clare Guest

Posting Permissions

  • You may not post new threads
  • You may 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