Professional Web Applications Themes

Saving file descriptors - UNIX Programming

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...

  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. #2

    Default Re: Saving file descriptors

    In article <Xns93BC42D72A49whitesuneapollolv130.133.1.4>,
    Peteris Krumins <pkruminsREMOVETHISinbox.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.margolinlevel3.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

  3. #3

    Default Re: Saving file descriptors

    Barry Margolin <barry.margolinlevel3.com> wrote in
    news:HIERa.256$0z4.155news.level3.com:
    > In article <Xns93BC42D72A49whitesuneapollolv130.133.1.4>,
    > Peteris Krumins <pkruminsREMOVETHISinbox.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

  4. #4

    Default Re: Saving file descriptors

    Barry Margolin wrote:
    >
    > In article <Xns93BC42D72A49whitesuneapollolv130.133.1.4>,
    > Peteris Krumins <pkruminsREMOVETHISinbox.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.Sosmansun.com[/email]
    Eric Sosman Guest

  5. #5

    Default Re: Saving file descriptors

    Eric Sosman <Eric.Sosmansun.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 cirstances
    you also need to do an fseek() after fiddling with the
    file descriptors. The cirstances 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 <nospamgclare.org.uk>
    Geoff Clare Guest

Similar Threads

  1. Type Descriptors, Converters and designer serialization
    By Chris B via .NET 247 in forum ASP.NET Building Controls
    Replies: 1
    Last Post: April 4th, 09:26 PM
  2. File Descriptors
    By Jim Bo in forum Linux / Unix Administration
    Replies: 1
    Last Post: August 18th, 04:16 PM
  3. Displaying Open File Descriptors for a Process
    By Peter von Zirpolo in forum Linux / Unix Administration
    Replies: 5
    Last Post: January 25th, 09:54 PM
  4. select() max number of file descriptors
    By Charles Wilkins in forum UNIX Programming
    Replies: 5
    Last Post: August 8th, 06:39 PM
  5. increasing file descriptors (Solaris 8) on the fly
    By Oscar del Rio in forum Sun Solaris
    Replies: 0
    Last Post: June 24th, 04:44 PM

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