On Thu, Apr 04, 2002 at 06:17:16PM +0300, Nadav Har'El wrote:
> On Thu, Apr 04, 2002, mulix wrote about "Re: c question":
> > On Thu, Apr 04, 2002 at 06:00:36PM +0300, Shlomi Fish wrote:
> > 
> > >     write(1, "World!\n", 7);
> > 
> > please use fileno(stdout). daemon writers everywhere will thank you. 
> 
> You're right about the existance of the fileno() function, but I'm not sure
> I understand your advice. The only reason fileno(stdout) is *ever* going to
> be different than 1, is if freopen() is used. freopen() is an even more
> obscure function than the fdopen() function I mentioned in a previous
> post, and I don't think I ever saw it actually used. Why is it used by
> "daemon writers everywhere"?
> I guess I'm missing some important point.

consider this snippet, in a library function:

int do_something()
{
        char msg[] = "an incredibly important event occured"; 
        write(fileno(stdin), msg, sizeof(msg)); 
        return 0; 
}

and now consider this typical daemon code:

int daemonize()
{
        ...
        /* close all open file descriptors */
        ...
        /* open something application specific, which happens to get the fd 1 */
        ...
        /* inditectly direct library output to our logging files */
        stdout = fopen("/my/logging/file"); 
        stderr = fopen("/my/other/logging/file");
}

now, wherever the library writer used stdout/stderr by name, whether
using the glibc IO facilities (printf(), fprintf() or by system calls,
everything will just work - unless the writer made the undue
assumption that stdout == 1. if the value 1 (or 2) is hard coded, much
fun will ensue. 

as for the fact that libraries shouldn't write to stdout and stderr,
that's a completely different discussion. 
-- 
The ill-formed Orange
Fails to satisfy the eye:       http://vipe.technion.ac.il/~mulix/
Segmentation fault.             http://syscalltrack.sf.net/

=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]

Reply via email to