Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99808 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 86938 invoked from network); 7 Jul 2017 14:04:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 7 Jul 2017 14:04:16 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@list.imperialat.at; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=php@list.imperialat.at; sender-id=pass Received-SPF: pass (pb1.pair.com: domain list.imperialat.at designates 80.101.55.235 as permitted sender) X-PHP-List-Original-Sender: php@list.imperialat.at X-Host-Fingerprint: 80.101.55.235 mail.imperialat.at Received: from [80.101.55.235] ([80.101.55.235:50939] helo=mail.imperialat.at) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 80/B1-47109-ED49F595 for ; Fri, 07 Jul 2017 10:04:15 -0400 Received: from mail.imperialat.at (localhost [127.0.0.1]) by mail.imperialat.at (OpenSMTPD) with ESMTP id 1b3a6da9; Fri, 7 Jul 2017 16:04:11 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d= list.imperialat.at; h=subject:to:cc:references:from:message-id :date:mime-version:in-reply-to:content-type :content-transfer-encoding; s=deathstar; bh=Pqv46h8aA346FN3sRol6 MZ2ErI0ZrovZVfsIXf0HyuM=; b=ZCOVuJophB+Z/0GpoSgD5jIbUXTu5tnxTdXm 8zn2AE91e/rbwZxtITWp8t4wpBy6bfVEYpvtq48voK6E2TkwpIKdBaztMmFPupyF r06OSI44+VVrBM47CjYC2s9NEatgXlBo6lraWkN7WfNJluT0Pcc1XS7rWvqLbqkN cx63/zZ/fRRcOHLyZPU+biqLXpkxVPLiVI05As97/CXoX7VwLdsAr0VYW9jjVLsl Ly/wiiAq684/P2oAv2anp0XEc0p3iIM6MexKk21H/neqhnW8BY7+zjofaka4c0qh sYSf/DJZ/B8bUH4vwla7xj00ltnVk8F+UKZHZLP0ZMugHeQJPA== Received: from martijn.office.rootnet.nl (095-097-046-226.static.chello.nl [95.97.46.226]) by mail.imperialat.at (OpenSMTPD) with ESMTPSA id 66f63d94 (TLSv1.2:ECDHE-RSA-CHACHA20-POLY1305:256:NO); Fri, 7 Jul 2017 16:04:11 +0200 (CEST) To: Niklas Keller , Sara Golemon , Bob Weinand Cc: Kalle Sommer Nielsen , Internals References: Message-ID: <105535b4-a629-b2e4-c5e9-a75ff565b054@list.imperialat.at> Date: Fri, 7 Jul 2017 16:04:10 +0200 User-Agent: Mozilla/5.0 (X11; OpenBSD amd64; rv:52.0) Gecko/20100101 Thunderbird/52.1.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Getting fd from stream From: php@list.imperialat.at (Martijn van Duren) On 07/06/17 20:01, Niklas Keller wrote: >> >> On Thu, Jul 6, 2017 at 10:53 AM, Kalle Sommer Nielsen >> wrote: >>> 2017-07-06 11:12 GMT+02:00 Martijn van Duren : >>>> 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 >