Discussion:
improve cygwin_attach_handle_to_fd()
Basin Ilya
2015-10-01 09:52:57 UTC
Permalink
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.

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.
Corinna Vinschen
2015-10-21 08:48:17 UTC
Permalink
Post by Basin Ilya
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
called.
Post by Basin Ilya
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
--
Corinna Vinschen Please, send mails regarding Cygwin to
Cygwin Maintainer cygwin AT cygwin DOT com
Red Hat
Loading...