You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Daniel Risacher <ma...@alum.mit.edu> on 2007/01/17 23:14:05 UTC

file descriptor for client socket?

Is it possible to get the file descriptor for the client socket from the RequestRec?

I.e. something like $r->FILENO (which doesn't seem to work) or perhaps $r->connection->client_socket->os_sock

(os_sock exists in the APR structure in C, but there doesn't seem to be a perl accessor method.)

Reason I want to do this... I'm trying write a perl module to pass the connection to another process using File::FDpasser.





Re: file descriptor for client socket?

Posted by Daniel Risacher <ma...@alum.mit.edu>.
I determined empirically that the file descriptor of the client socket is always 0 in my environment.  I assume this is only true for the Prefork MPM.

For the curious, the code I developed is for a Comet/pushlet type server.  Looks like this:

--- Handoff.pm --- 
package Handoff;

use strict;
use warnings;

use Apache::RequestRec ();
use Apache::RequestIO ();
use Apache::Const -compile => qw(:common :conn_keepalive :http);

use File::FDpasser;
use POSIX;

# 1. subserver creates a named socket (/tmp/handoff) and listens on it

# 2. browser connects to apache (thus creating client-socket) makes a request

# 3. Apache looks at the request, and decides that it should go to
# mod_handoff

# 4. mod_handoff connects to the subserver using the /tmp/handoff and
# creates a pipe-type socket (ipc-pipe).  It sends client-socket to
# the subserver over ipc-pipe, closes client-socket, sends the request
# over ipc-pipe, and then logs the result.  Note that client-socket
# only is closed from apache...  it's still open in the subserver.
#
# 5. The subserver reads the request on ipc-pipe, and responds over
# client-socket.
#
# 6. If mod_handoff cannot open the fs-based socket, then it returns an
# error code of some kind to the client.
sub handler {
    my ($r) = @_;

    my $fh = endp_connect("/tmp/handoff");
    if (not $fh) {
        $r->warn("could not connect to handoff endpoint");
        return &Apache2::Const::HTTP_SERVICE_UNAVAILABLE;
    }

    # This only works with the preform MPM, I think.
    my $client = 0;
    my $rc = send_file($fh,$client);

    # To prevent Apache from writing to the socket (which  we have handed
    # off to another process), replace the client socket with /dev/null
    POSIX::dup2(POSIX::open("/dev/null", &POSIX::O_RDWR), $client);

    # send the request to the subserver
    print $fh $r->the_request."\r\n";

    # send the headers too
    $r->headers_in->do(sub { my ($k, $v)=@_; print $fh "$k: $v\r\n"; } );
    close $fh;

    # do whatever we can to prevent apache from using the client socket again
    $r->assbackwards(1);
    $r->connection->keepalive(&Apache2::Const::CONN_CLOSE);

    return &Apache2::Const::DONE;
}

1;

------


On Fri, 19 Jan 2007 10:34:25 +0100, Torsten Foertsch <to...@gmx.net> wrote:
> On Friday 19 January 2007 08:01, Daniel Risacher wrote:
>> From within a mod_perl request handler, fileno(STDOUT) returns -1, which
> is
>> the same thing I got from $r->FILENO.
>>
>> Any other ideas?
> 
> $r->connection->client_socket is an APR::Socket. It corresponds to an 
> apr_socket_t in C. This structure contains on UNIX an element called 
> socketdes. That is probably what you want. AFAIK there is currently no way
> to 
> access that bit from Perl level. But it's not very complicated to add such
> an 
> interface.
> 
> Torsten


Re: file descriptor for client socket?

Posted by Torsten Foertsch <to...@gmx.net>.
On Friday 19 January 2007 08:01, Daniel Risacher wrote:
> From within a mod_perl request handler, fileno(STDOUT) returns -1, which is
> the same thing I got from $r->FILENO.
>
> Any other ideas?

$r->connection->client_socket is an APR::Socket. It corresponds to an 
apr_socket_t in C. This structure contains on UNIX an element called 
socketdes. That is probably what you want. AFAIK there is currently no way to 
access that bit from Perl level. But it's not very complicated to add such an 
interface.

Torsten

Re: file descriptor for client socket?

Posted by Daniel Risacher <ma...@alum.mit.edu>.
>From within a mod_perl request handler, fileno(STDOUT) returns -1, which is the same thing I got from $r->FILENO.

Any other ideas?


On Wed, 17 Jan 2007 17:26:56 -0500, Robert Landrum <rl...@aol.net> wrote:
> Daniel Risacher wrote:
>> Is it possible to get the file descriptor for the client socket from the
> RequestRec?
>>
>> I.e. something like $r->FILENO (which doesn't seem to work) or perhaps
> $r->connection->client_socket->os_sock
>>
>> (os_sock exists in the APR structure in C, but there doesn't seem to be
> a perl accessor method.)
>>
>> Reason I want to do this... I'm trying write a perl module to pass the
> connection to another process using File::FDpasser.
>>
> 
> $fd = fileno(STDOUT); won't work?
> 
> Is this really what you want to do?  I think apache will still terminate
> the connection (I'm not sure there's away to avoid that).
> 
> Rob


Re: file descriptor for client socket?

Posted by Robert Landrum <rl...@aol.net>.
Daniel Risacher wrote:
> Is it possible to get the file descriptor for the client socket from the RequestRec?
> 
> I.e. something like $r->FILENO (which doesn't seem to work) or perhaps $r->connection->client_socket->os_sock
> 
> (os_sock exists in the APR structure in C, but there doesn't seem to be a perl accessor method.)
> 
> Reason I want to do this... I'm trying write a perl module to pass the connection to another process using File::FDpasser.
> 

$fd = fileno(STDOUT); won't work?

Is this really what you want to do?  I think apache will still terminate 
the connection (I'm not sure there's away to avoid that).

Rob