Ask a Question related to UNIX Programming, Design and Development.
-
Peteris Krumins #1
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
-
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... -
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... -
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 -
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... -
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" -
Barry Margolin #2
Re: Saving file descriptors
In article <Xns93BC42D72A49whitesuneapollolv@130.133.1.4>,
Peteris Krumins <pkruminsREMOVETHIS@inbox.lv> wrote:dup() them before closing them. Then dup2() them back to restore.> 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?
--
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
-
Peteris Krumins #3
Re: Saving file descriptors
Barry Margolin <barry.margolin@level3.com> wrote in
news:HIERa.256$0z4.155@news.level3.com:
Thanks!> 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.
>
P.Krumins
Peteris Krumins Guest
-
Eric Sosman #4
Re: Saving file descriptors
Barry Margolin wrote:
If there's any chance that output was generated with>
> 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.
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
-
Geoff Clare #5
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.Oops, yes I forgot about the offset. In some circumstances> 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.
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



Reply With Quote

