jonathanstowe / raku-fastcgi-nativecall

An implementation of FastCGI using NativeCall
Other
5 stars 3 forks source link

sock file editing the rights to default after new initialization #2

Open Disinterpreter opened 5 years ago

Disinterpreter commented 5 years ago

I've got a problem with working this module.

When I restart the script I need to change rights each times (over chmod).

Do you have any ideas?

jonathanstowe commented 5 years ago

Hi, The socket is opened with permissions that are modified by the current umask so for example:

jonathan@coriolanus p6-fcgi]$ umask 000
[jonathan@coriolanus p6-fcgi]$ perl6 -I. examples/synopsis 
^C
[jonathan@coriolanus p6-fcgi]$ umask 077
[jonathan@coriolanus p6-fcgi]$ perl6 -I. examples/synopsis 
^C
[jonathan@coriolanus p6-fcgi]$ ls -l /tmp/fastcgi.sock 
srwx------. 1 jonathan jonathan 0 Nov  7 21:14 /tmp/fastcgi.sock
[jonathan@coriolanus p6-fcgi]$ umask 007
[jonathan@coriolanus p6-fcgi]$ perl6 -I. examples/synopsis 
^C
[jonathan@coriolanus p6-fcgi]$ ls -l /tmp/fastcgi.sock 
srwxrwx---. 1 jonathan jonathan 0 Nov  7 21:15 /tmp/fastcgi.sock
[jonathan@coriolanus p6-fcgi]$ umask 000
[jonathan@coriolanus p6-fcgi]$ perl6 -I. examples/synopsis 
^C
[jonathan@coriolanus p6-fcgi]$ ls -l /tmp/fastcgi.sock 
srwxrwxrwx. 1 jonathan jonathan 0 Nov  7 21:16 /tmp/fastcgi.sock

There is no umask built-in but I guess one could make a NativeCall binding ....

Disinterpreter commented 4 years ago

I guess one could make a NativeCall binding ....

It will be a great idea. Because the uWSGI works fine in same situation.

jonathanstowe commented 4 years ago

Hi, At the moment I'm not sure whether to add it to the module or make a separate (very tiny) module that does:


use NativeCall;

sub umask(int32 $cmask --> int32) is native(Str, Version) is export { * }

umask(0o000);

(obviously adjusting the value supplied to umask as appropriate..) But you could put the above in your own code so that umask is called before the FastCGI::NativeCall object is created you will be able to control the mode of the created socket.

I would recommend against creating the socket as world-readable/witeable (i.e. with a mask of 0o000,) and rather have your FastCGI run as the same user or group as the HTTP server and set the mask to 0o007 for instance (so the mode of the socket is srwxrwx---. )

Disinterpreter commented 4 years ago

Got it. By the way, could you make an IP connection? (besides the unix sock)

jonathanstowe commented 4 years ago

Sure, but because it uses a low level native you need to pass the socket file descriptor so you'd do something like (untested ):

my $listen = IO::Socket::INET.new( :listen, localhost => 'localhost', localport => 3333 );
my $fcgi = FastCGI::NativeCall.new(socket => $listen.native-descriptor );

And the rest of the code would be the same.

Disinterpreter commented 4 years ago

Something went wrong? 🤔

#!/usr/bin/perl6
use v6.d;

use FastCGI::NativeCall::Async;

my $listen = IO::Socket::INET.new( :listen, localhost => 'localhost', localport => 3333 );
my $fna = FastCGI::NativeCall.new(socket => $listen.native-descriptor );

my $count = 0;

react {
    whenever $fna -> $fcgi {
        say $fcgi.env;
        $fcgi.Print("Content-Type: text/html\r\n\r\n{++$count}");
    }
}

Output:

root@cirno:/opt/ko-chan# ./main.raku
{}
root@cirno:/opt/ko-chan#

P.S I can to create a new issue about the IP socket.

jonathanstowe commented 4 years ago

Hi, the reason that isn't working is that you are mixing up the behaviour of FastCGI::NativeCall and FastCGI::NativeCall::Async the latter doesn't currently take a socket as an argument.

Though I'm getting different behaviour altogether, I'll try to take a better look later.

jonathanstowe commented 4 years ago

So I've just tested a modified version of the synopsis code, like:

#!/usr/bin/env perl6

use FastCGI::NativeCall;

my $socket = IO::Socket::INET.new(:listen, localport => 9928);

my $fcgi = FastCGI::NativeCall.new(socket => $socket.native-descriptor );

my $count = 0;

while $fcgi.accept() {
    say $fcgi.env;
    $fcgi.header(Content-Type => "text/html");
    $fcgi.Print("{ ++$count }");
}

With an nginx configuration like:


error_log stderr;
pid /tmp/nginx.pid;

events {
}

http {
    access_log off;
    error_log /tmp/nginx_error.log;
    root /tmp/nginx;
    client_body_temp_path /tmp/nginx/client_body;
    proxy_temp_path /tmp/nginx/proxy;
    fastcgi_temp_path /tmp/nginx/fastcgi;
    uwsgi_temp_path /tmp/nginx/uwsgi;
    scgi_temp_path /tmp/nginx/scgi;
    server {
        listen 8189;

        location / {
            fastcgi_pass  127.0.0.1:9928;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param QUERY_STRING    $query_string;
            fastcgi_param REQUEST_METHOD  $request_method;
            fastcgi_param CONTENT_TYPE    $content_type;
            fastcgi_param CONTENT_LENGTH  $content_length;
        }
    }
}

And it works fine. The FastCGI::NativeCall::Async will need some adjustment as it can't instantiate a socket like this.