lestrrat has asked for the wisdom of the Perl Monks concerning the following question:

So I've started fiddling with Net::Server, and so far so good. I no longer need to write all of this accept/fork/wait for child type routines. I also gave the author some comments, and I think a couple of them actually went into the newer version.

Now I have a problem , though. The way I'd like to use this is so that my server which extends Net::Server receives requests, and then forks off a child that performs certain tasks.

One of the underlying packages ( an in-house thing ) is a sort of a rsh-type thing which works _very_ well. you can run arbitrary commands anywhere, and you get to control logs, input, output. Think of it as a Telnet.pm on steroids. ( the only drawback being that it was written for perl4 / early perl5 )

So when I fork from the Net::Server subclass, I want to use this package to talk to other machines. However, it seems to be confused about where to read from or write to, presumably because Net::Server does some funky things with these handles... As far as I could tell, it switches STDOUT and STDERR with the equivalent of the incoming sockets, and STDERR points to STDOUT. I thought that dup'ing these handles before hand and restoring them after forking would fix the problem, but apparently not.

The underlying package seems to be forking as well, and creates a pipe to the ( possibly ) remote process' std(out|err|in).. it's somehow getting very confused there, I think

At this point I don't even know if I'm going about the correct way... Can anybody tell me how to set all of these std(out|err|in) related settings to the 'default' state, as in when the script was invoked without Net::Server?

  • Comment on Restoring std(outerrin) with Net::Server

Replies are listed 'Best First'.
Re: Restoring std(outerrin) with Net::Server
by stefp (Vicar) on Oct 02, 2001 at 23:57 UTC
    By default, , on a UNIX box, when forking, the open handles are inherited from the father so this is not surprising that STDIN and STDERR are sockets if the father has opened sockets. IIRC a process can "restore" STDIN, STDOUT and STDERR if not detached of the current tty. For a start, try to adpat the following code that reopens the STD* handle:
    close STDIN; close STDOUT; close STDERR; # not really necessary open STDIN, "</dev/tty"; open STDOUT, ">/dev/tty"; open STDERR, ">/dev/tty"; # testing print "STDOUT\n"; print "STDERR\n"; $_ = <STDIN>; print $_;
    Really, these are not Perl issues. They are thouroughly covered in Advanced Programming in the UNIX Environment by Richard Stevens. I really need to reread it from cover to cover.

    I would be happy to learn that there is a more portable way to reopen this file handlles.

    -- stefp

Re: Restoring std(outerrin) with Net::Server
by lestrrat (Deacon) on Oct 03, 2001 at 01:59 UTC

    UPDATE : I think my problem is not that I need to restore these std* handles, but that somehow the piping that is done in the underlying package is not being perfomed correctly....

    Hmm, any thoughts on where to start looking??