On 07/06/17 20:01, Niklas Keller wrote:
>>
>> On Thu, Jul 6, 2017 at 10:53 AM, Kalle Sommer Nielsen <ka...@php.net>
>> wrote:
>>> 2017-07-06 11:12 GMT+02:00 Martijn van Duren <p...@list.imperialat.at>:
>>>> I have an (exotic) case where I need to be able to get the
>>>> filedescriptor from a previously opened stream (via stream_socket_pair)
>>>> to hand over to the child process, which needs to reexecute.
>>>>
>>>
>> "hand over to a child process"
>> Child as in pcntl_fork()? Or a subprocess via exec() et. al.?

The latter.
>>
>> For the former, the variable should (I think) carry over.
>> For the latter, the stream number is going to be a meaningless value
>> to the subprocess. ((e.g. stdin (fd=0) for the parent is a different
>> source than stdin for the child, despite being the same number)).

Not entirely, when I use stream_socket_pair I get 2 filedescriptors
and two resources with possible 2 different IDs.
E.g. fd 5 and 6 while the resources have id 8 and 9.

When I do the fork I close one end in the parent and one end in the
child. If I exec into another program I want to be able to say:
we can communicate over this fd, so that the child program can hook
into that specific file descriptor.
>>
>> I'm not sure of your exact use case, but you might be able to use
>> proc_open to spawn the child and supply the socket in the descriptor
>> spec which will (I think, iirc) bind the internal socket to a new pipe
>> between the parent and child.  Though it's possible you'll need to
>> maintain a proxy loop in the parent.

My use case is to set up a cluster of processes each with their own task
and permission sets. These processes need to be able to communicate with
each other over socket pairs, not just between the parent and the child,
but also between children.

Although the proc_open does solve some problems, it doesn't solve them
all. E.g. I can't create socket pairs between children via this options,
and socketpairs opened to the proc_open would be left dangling.

Hence I want to set up all my socket pairs, pcntl_fork the child
process, close the socket pair ends that are not owned by the child
and execute the new process and specify the fds via commandline
arguments.

For processes that execute a new PHP process I would also prefer to wrap
an existing fd in a stream (similar to fdopen in C), but I can live with
the dup-ing of the file-descriptor in fopen(php://fd/N, X).

So the end result would (somewhat) look like:
           +------+
           |master|
           +------+
           3      7
          /        \
         /          \
        4            8
+------+             +------+
|child1|5-----------6|child2|
+------+             +------+

instead of:
             4568
           +------+
           |master|
           +------+
           3      7
          /        \
         /          \
        4            8
+------+             +------+
|child1|5-----------6|child2|
+------+             +------+
  3678                 3457
>>
>> 40% of the above is guesswork and conjecture, but maybe it'll spawn
>> the right question.
> 
> 
> There's a (I think undocumented) feature that allow transferring sockets to
> other processes with the socket extension.
> 
> https://github.com/amphp/aerys/commit/40fae01e4f5f82570a0bc3cb8ebbf1fee36dbb0b

That's a big help on another project I have, so thank you for that hint.
I've actually been looking for that option for quite some time.
> 
> Bob implemented that, so maybe he can help you.
> 
> But what's your exact use case?
> 
> Regards, Niklas
> 

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to