improve cygwin_attach_handle_to_fd()

Corinna Vinschen
Wed Oct 21 08:48:00 GMT 2015

On Oct  1 12:52, Basin Ilya wrote:
> Hi All.
> There's undocumented function cygwin_attach_handle_to_fd() , which is
> similar to Visual C _open_osfhandle() for wrapping Windows file handles.
> I wanted to wrap a handle returned by CreateNamedPipe() using a custom
> tool running in background and then dup the fd to parent shell using
> exec 4</proc/$!/fd/4
> I knew Cygwin internally used CreateNamedPipe() instead of CreatePipe()
> and hoped the idea would work. Unfortunately it didn't, mainly because
> cygwin_attach_handle_to_fd() calls some other windows functions on the
> handle, which is fine for normal files, but makes a connected pipe unusable.

I don't understand your scenario.  If /proc/$!/fd/4 exists, then the
custom tool is a Cygwin tool and cygwin_attach_handle_to_fd is never

> Also for stealing a pipe handle via procfs, the fd should have a special
> name like "pipe:[652]". You cannot freely choose the name with
> cygwin_attach_handle_to_fd()
> Do you think it is possible to add another function for wrapping native
> pipes and sockets? It could be less destructive for a hanlde, if it took
> all the needed info from its parameters instead of querying the hanlde.

I'm not positive on that.  From the Cygwin side the pipes and sockets
need specific setting, e.g., the pipe must be overlapped, the socket
nonblocking.  Cygwin_attach_handle_to_fd is only a band-aid.  It's
tricky to make sure the handle behaves correctly for the way they are
used inside Cygwin.


Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Maintainer                 cygwin AT cygwin DOT com
Red Hat
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 819 bytes
Desc: not available
URL: <>

More information about the Cygwin-developers mailing list