UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2006-05-26 07:01 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: redirect socket descriptor to stderr?

what i need to know:
how can i redirect a socket to stderr during runtime?

example:
in the command line i would do something like this:
./myprog 2>&1
this redirects stderr to stdout

i need the same during runtime:

int main(int argc,char **argv){
   int server = createsomesocketserver();
   int client = acceptclient(server);
   redirect(stderr,client);  // redirect stderr to the client
   fprintf(stderr,"some error occured");
}

why do i need this:
i finally need it in a php prog
since i'm sure that php isn't able to this as a "standard" funcionality, i will do my own extension
but to make my extension, i would need to know how this works in C

i know of freopen(), to redirect e.g. stdout to a log file
but i need this for descriptors

Q1: is there a C function which can handle this? or do i have to write my own?
Q2: if i need to write my own, how should it work?
Q3: just cause of interest: how does the bash handle descriptor redirects like 2>&1?

[email protected]

Offline

#2 2006-05-26 01:20 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: redirect socket descriptor to stderr?

Well, the function you're looking for is dup2()...

But, if you have any need for I/O in the other direction as well, you might also need
to dup it to stdin along with stdout/stderr...

Also, I'm not sure on the details of what app is ultimately going to be using this
redirected socket as its stdin/out/err, but generally mixing stdio with sockets is a
bad idea, because you add an extra layer of buffering on top of the socket buffering,
which can cause some problems...  If you disable stdio buffering (at least for output),
it should probably be ok, though...  (If you're just using stderr, it should already be
unbuffered anyway, so no problem there...)  Or, if you're not actually going to be
using stdio functions at all, but just using the stdin/out/err file descriptors for FD-level
I/O, that's fine too...

However, most apps that assume they'll be doing I/O with a TTY probably aren't
going to be prepared for the subtle differences of socket I/O instead...  Eg: short
reads/writes are going to be possible...  So, things may not work as expected...
But, then again, they just might, too... *shrug*

Offline

#3 2006-05-28 04:03 PM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: redirect socket descriptor to stderr?

And if you really need a terminal on stderr but have it connected to a socket it is possible (though alot more work and less portable) to allocate a pseudo terminal and have that redirected to a socket (though I have no clue how to do that in php).

Offline

#4 2006-05-30 10:53 AM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: redirect socket descriptor to stderr?

Offline

#5 2006-05-30 12:07 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: redirect socket descriptor to stderr?

Libc does buffering for stdio functions in userspace too by default, so both libc and the kernel do buffering, which can have unexpected results when using sockets. This is only with stdio functions, not systemcalls, so write() will be fine (it is used on sockets all the time), but fprintf is a sdio function. All functions taking an fd don't do extra buffering, but all stdio functions using FILE* instead of a fd do in general, if you don't disable it. Though stderr is already unbuffered, so it will probably work fine when just dupping the socket to stderr, as the FILE* object doesn't know about the change...

Offline

#6 2006-05-30 12:56 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: redirect socket descriptor to stderr?

Yeah, what i3839 said... ;-)

If you're JUST going to be using stderr, then it should be ok...  But, if you want to
use stdout, you'll run into the double-buffering issue, and will probably want to
disable stdio buffering on stdout (via setbuf() or setvbuf() or similar)...

Offline

#7 2006-05-30 04:06 PM

thinking
Member
Registered: 2005-09-15
Posts: 103

Re: redirect socket descriptor to stderr?

got it
and i think i have a problem with php too, cause php does a buffering too
but i should get around with it

btw: just of interest
why does libc buffer?
what i'm asking myself is: why should i (if i were a libc hacker) be interested in buffering the data?
why should it matter if the data is buffered, or just written through write

for me, stdio buffer will just make it slower, not?
i know if i do network programming that sending "bigger" chunks will be faster than sending just 1 byte
but why should it be used in stdio?

Offline

#8 2006-05-30 08:29 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: redirect socket descriptor to stderr?

Well, for one thing, stdio was never intended for use with network I/O, but mainly
with files and TTYs...  There, buffering of some sort makes sense, to optimize
disk I/O, or I/O to old slow dumb terminals (which almost no one actually uses
anymore, so it's a bit of an ancient relic, there)...  But, I think it must be defined
by Posix or someone like that, that stdio is supposed to buffer most stuff by default...
Typically, stderr is never buffered, so writes go out immediately to screen (or
wherever you've redirected it to), while stdout is generally line-buffered, so nothing
is output until a newline is encountered (or you manually fflush())...  And, files
opened via fopen() are generally block-buffered, so after some amount that it
thinks makes for an optimum write block is buffered, it'll then get sent to disk...

But, sockets were always intended to be used via low-level read()/write() (or
recv()/send()), at an FD-level, since the kernel already does appropriate buffering
there, so adding a user-space buffer on top of that can cause issues...

Offline

#9 2006-05-31 07:07 AM

Uzume
Administrator
Registered: 2002-08-30
Posts: 186

Re: redirect socket descriptor to stderr?

BTW, the Bourne/POSIX/bash shell syntax: 2>&1 is basically syntax for a dup2() call as you can put any descriptor numbers in place of such. Interestingly csh derivatives cannot do such (without significant syntax changes)--one of the many reasons csh is nasty.

Offline

Board footer

Powered by FluxBB