# Implementing fdopen in PHP

- Thread: https://externals.io/message/47250
- Original archive: https://news-web.php.net/php.internals/47250
- Started by Dennis Hotson on 2010-03-14T06:38:59+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #47250 — Dennis Hotson — 2010-03-14T06:38:59+00:00

URL: https://externals.io/message/47250#47250

Hi all,

It's my first post, so go easy on me. :-)

I'm trying to implement PHP support for github's erlang RPC server called
ernie.
So basically I've been porting the following ruby code to PHP:
http://github.com/mojombo/ernie/blob/master/lib/ernie.rb

The problem I'm having is on line 127-128:
  input = IO.new(3)

I believe this is equivalent to fdopen() in C, but there doesn't appear to
be any way to do this in PHP.

So basically, I'm a bit stuck and looking for advice on how to proceed.
Should I implement this in core PHP or as an extension? What's the best way
to get a function like this into PHP?

Regards,
Dennis Hotson

---

## #47266 — Keisial — 2010-03-14T14:30:15+00:00

URL: https://externals.io/message/47250#47266 · In reply to: #47250

Dennis Hotson wrote:
> Hi all,
>
> It's my first post, so go easy on me. :-)
>
> I'm trying to implement PHP support for github's erlang RPC server called
> ernie.
> So basically I've been porting the following ruby code to PHP:
> http://github.com/mojombo/ernie/blob/master/lib/ernie.rb
>
> The problem I'm having is on line 127-128:
>    input = IO.new(3)
>
> I believe this is equivalent to fdopen() in C, but there doesn't appear to
> be any way to do this in PHP.
>
> So basically, I'm a bit stuck and looking for advice on how to proceed.
> Should I implement this in core PHP or as an extension? What's the best way
> to get a function like this into PHP?
>
> Regards,
> Dennis Hotson
>    
You perform a fdopen() in C where you have a low level file descriptor and
make it a stdio on. In php there's only one kind of file descriptor, and 
all file
functions work with them.

Creating a fdopen call is useless, since it needs a previous descriptor to
work from, and all descriptors are equal. Where's that file descriptor 
coming
from?
Since it's an RPC server I suspect you will create it somehow with 
fsockopen.

---

## #47269 — karpeles@ookoo.org — 2010-03-14T14:39:05+00:00

URL: https://externals.io/message/47250#47269 · In reply to: #47266

Hi,

Le dimanche 14 mars 2010 à 15:30 +0100, Keisial a écrit :
> Creating a fdopen call is useless, since it needs a previous
> descriptor to
> work from, and all descriptors are equal. Where's that file
> descriptor 
> coming
> from? 

When you spawn a new program, you can pass it additionnal descriptors on
top of stdin/stdout/stderr.

For example look at `proc_open()`. You can pass more than 3 descriptors,
and communicate if the program is able to use them.

An alternative on linux is to $fp = fopen("/proc/self/fd/3","r+")


Mark

---

## #47270 — Pierre Joye — 2010-03-14T14:41:27+00:00

URL: https://externals.io/message/47250#47270 · In reply to: #47269

> Hi,
>
> Le dimanche 14 mars 2010 à 15:30 +0100, Keisial a écrit :
> > […]
>
> When you spawn a new program, you can pass it additionnal descriptors on
> top of stdin/stdout/stderr.
>
> For example look at `proc_open()`. You can pass more than 3 descriptors,
> and communicate if the program is able to use them.
>
> An alternative on linux is to $fp = fopen("/proc/self/fd/3","r+")

That's what proc_open does and allows.

Cheers,
-- 
Pierre

@pierrejoye | http://blog.thepimp.net | http://www.libgd.org

---

## #47283 — Dennis Hotson — 2010-03-14T23:36:36+00:00

URL: https://externals.io/message/47250#47283 · In reply to: #47270

On Mon, Mar 15, 2010 at 1:41 AM, Pierre Joye <pierre.php@gmail.com> wrote:>

> > […]
>
> That's what proc_open does and allows.
>

So in my case, the erlang server is doing the equivalent of `proc_open()` to
call my PHP program. My PHP program needs
a way to access these 'extra' file descriptors.

I'm fine with using fopen("/proc/self/fd/3","r") for now, but something like
fdopen() would be more appropriate IMHO so that
it will work on platforms other than linux.


Slightly off topic, but another useful application of fdopen() is for hot
swapping a running program.
http://nathanwiegand.com/wp/2010/02/hot-swapping-binaries/
I realise this is probably not a priority for PHP, but I actually enjoy
writing network servers in PHP and this would be a pretty awesome feature.

Regards,
Dennis Hotson


(PS. Sorry for the dupe Pierre, I forgot to reply to the list.)

---

## #47292 — Arnaud Le Blanc — 2010-03-15T18:16:27+00:00

URL: https://externals.io/message/47250#47292 · In reply to: #47250

Le dimanche 14 mars 2010 à 17:38 +1100, Dennis Hotson a écrit :
> Hi all,
> 
> It's my first post, so go easy on me. :-)
> 
> I'm trying to implement PHP support for github's erlang RPC server called
> ernie.
> So basically I've been porting the following ruby code to PHP:
> http://github.com/mojombo/ernie/blob/master/lib/ernie.rb
> 
> The problem I'm having is on line 127-128:
>   input = IO.new(3)
> 
> I believe this is equivalent to fdopen() in C, but there doesn't appear to
> be any way to do this in PHP.
> 
> So basically, I'm a bit stuck and looking for advice on how to proceed.
> Should I implement this in core PHP or as an extension? What's the best way
> to get a function like this into PHP?

Hi,

I guess that this can go to some future PHP release, and in a small
extension if you want to have it in current / older versions too. You
can implement it with php_stream_fopen_from_fd(). 

Regards,
