Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Open a file on a specific file descriptor?

by polettix (Vicar)
on Jan 02, 2008 at 01:32 UTC ( [id://659898]=note: print w/replies, xml ) Need Help??


in reply to Open a file on a specific file descriptor?

I don't understand if you're trying to use those programs or to recreate similar ones.

In the former case, your Perl program already has the child, so file descriptors 6 and 7 will be open when your program is started, and the two opens in your example should more or less work. In this case, you can also take a look at IO::Handle to get handles for file descriptors you already have.

OTOH, if you want to recreate the same "effect", you should probably first ask yourself if it really makes sense (e.g. maybe redirecting child's STDIN and STDOUT just before calling exec can be acceptable), then use dave_the_m's suggestion.

Update: added stuff about IO::Handle

Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
perl -ple'$_=reverse' <<<ti.xittelop@oivalf

Io ho capito... ma tu che hai detto?

Replies are listed 'Best First'.
Re: Open a file on a specific file descriptor?
by benizi (Hermit) on Jan 02, 2008 at 07:46 UTC
    I don't understand if you're trying to use those programs or to recreate similar ones.

    The attempt was a little of both, but I should have specified that I was trying to provide an sshclient-like interface for a qmail filtering program, since (as you mentioned) running a Perl program as sshclient's child is pretty easy.

    I'd used sshclient in the past, and even though I often mix up which fd is read and which is write does what (different djb/djb-like util's use different fd's for different things), I like the nice, generic, cross-language interface. It fits nicely with both "Do one thing, and do it well" and "Everything's a pipe". I wondered how easy it'd be to provide that interface in Perl.

    The idea was that I would provide the filtering program (child) fd's 0,1,4,5,6,7 for message-in, message-out, header-in, header-out, body-in, and body-out. If any of the '-out's was the same as its '-in' or was empty, the original would be used. And, either message-out or (header-out + body-out) would be used, depending on what the filter program changed.

    Since this isn't easy to do, I'm probably going to use an option flag to specify which one of 'message', 'headers', or 'body' the filter wants to have and just use STDIN + STDOUT.

    Thanks

    Update: clarified my confusion re: djb{|-esque} tools' use of fd's

      Well, opening the fd you like isn't difficult at all, as dave_the_m suggested - even if I have to admit that things might be less portable than I'm impling here. Pragmatically, what I see in my Linux system is that the lowest available file descriptor is always going to be used.

      The first thing you have to pay attention to is the starting file descriptor. For a brand new process, you should get 0, 1 and 2 assigned to standard input, standard output and standard error resp. (I know you *already* know this, but bear with me). So, if you really want to start from 4 instead of 3 (I wonder why, but I'm sure you have a good reason), you first have to do a fake open to get the spare file descriptor:

      open my $fh, '<', '/dev/zero' or die "$!";
      Then, pipe calls come in pairs, so you can get the couples you want:
      pipe my ($header_in, $header_out); pipe my ($body_in, $body_out);
      To take into account your need for handles 0 and 1 as well:
      use Fatal qw( close ); close STDIN; close STDOUT; pipe my ($message_in, $message_out);

      The best thing to do is probably write a subroutine for this:

      #!/usr/bin/perl use strict; use warnings; use Fatal qw( close ); # monitored (auto-)close close STDIN; close STDOUT; my %handle_for; @handle_for{qw( message_in message_out )} = pipe_from(0); @handle_for{qw( header_in header_out )} = pipe_from(4); @handle_for{qw( body_in body_out )} = pipe_from(6); print {*STDERR} "$_ has descriptor ", fileno($handle_for{$_}), "\n" for sort { fileno($handle_for{$a}) <=> fileno($handle_for{$b}) } keys %handle_for; sub pipe_from { # Assuming consecutive numbers are ok my ($n) = @_; my @fakes; while (1) { open my $fh, '<', '/dev/zero' or die "open(): $!"; push @fakes, $fh; last if fileno($fh) == $n; die "file descriptor $n not available" if fileno($fh) > $n; } ## end while (1) close pop @fakes; # Free last used descriptor, that's equal to $n pipe my ($r, $w); die "pipe(): $!" unless defined $r; close $_ for @fakes; return ($r, $w); } ## end sub pipe_from __END__ poletti@PolettiX:~/sviluppo/perl$ perl pipes.pl message_in has descriptor 0 message_out has descriptor 1 header_in has descriptor 4 header_out has descriptor 5 body_in has descriptor 6 body_out has descriptor 7
      I know, using this for each pair means that you will re-open fake handles for lower descriptors at each call. Feel free to optimise, if you think this is really needed :)

      Update: if you're wondering why I'm explicitly close-ing the handles instead of letting scoping do its work, I have to admit that I've been biten by this. In particular, I noted that the file descriptor 0 wasn't released when getting out of the while. If anyone can shed a light on it I'd be grateful! This was the original loop:

      while (1) { open my $fh, '<', '/dev/zero' or die "open(): $!"; last if fileno($fh) == $n; die "file descriptor $n not available" if fileno($fh) > $n; push @fakes, $fh; } ## end while (1)
      and of course I had no close pop @fakes; line after it. I thought that it could be that any time you open file descriptor 0, you can access it via STDIN (that is *STDIN{IO}), so when $fh goes out of scope the handle's reference count does not drop to zero, and auto-closing is not triggered.

      Hey! Up to Dec 16, 2007 I was named frodo72, take note of the change! Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Io ho capito... ma tu che hai detto?
        For a brand new process, you should get 0, 1 and 2 assigned to standard input, standard output and standard error resp. (I know you *already* know this, but bear with me).

        It's the shell that's responsible setting stdin, -out, and -err up. I know you can have more open when perl starts up if you explicitly request it (e.g. $ perl prog 3> extra-output). I assume there aren't any at-all-POSIX-y shells that open more fd's by default. Anyone know if that's true?

        So, if you really want to start from 4 instead of 3 (I wonder why, but I'm sure you have a good reason)

        I wanted to leave STDERR as fd 2, but I figured it'd be easier to remember that even-numbered fd's were input and odd-numbered fd's were output.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://659898]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-03-29 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found