This is the mail archive of the
mailing list for the Cygwin project.
non-blocking reads of stdin in native child of cygwin process
- From: David Byron <dbyron at dbyron dot com>
- To: cygwin at cygwin dot com
- Date: Mon, 14 May 2012 15:51:59 -0700
- Subject: non-blocking reads of stdin in native child of cygwin process
I can do a non-blocking read from standard input in a native C program
run in a cmd window. It involves WaitForSingleObject on the return
value from GetStdHandle(STD_INPUT_HANDLE). I could use some sample code
for doing the equivalent thing when run from mintty/bash or
cygwin.bat/bash. The story of what I've tried so far follows.
When running a native C program from mintty/bash or cygwin.bat/bash
GetStdHandle returns a pipe, not a console handle so
WaitForMultipleObjects doesn't work...or at least not the same way. The
docs don't say it works on named pipes and for me it returns
WAIT_OBJECT_0 whether there's something to read or not.
I've got my hands on the name of the pipe via GetNamedPipeInfo and
GetFileInformationByHandleEx -- something like
\\pipe\.\cygwin-dcb128a9c32912c6-pty1-from-master. This matches what I
see in usr/src/cygwin-1.7.15/winsup/cygwin/tty.cc (tty::not_allocated)
and pipe.cc (fhandler_pipe::create).
I'm running with an elevated token but I still get access denied trying
to use that handle in CreateFileW. I'm not sure this is the right call
to make but it works for me in other scenarios for non-blocking IO on
named pipes (when followed by ReadFile w/ an OVERLAPPED structure,
ERROR_IO_PENDING, WaitForMultipleObjects, GetOverlappedResult, etc.).
In case it's relevant, GetNamedPipe info reports 0x05 (PIPE_SERVER_END |
PIPE_TYPE_MESSAGE) for the return value of
GetStdHandle(STD_INPUT_HANDLE) whether the CYGWIN environment variable
contains pipe_byte or not.
As well, GetNamedPipeHandleState reports a state of 0x00 (also
independent of CYGWIN=pipe_byte as far as I can tell) which I interpret
as a blocking, byte-mode pipe. This matches my reading of the
CreateNamedPipe call in pipe.cc. So even if I wasn't getting access
denied from CreateFileW I still do think I could use my standard input
handle in a call to ReadFile that specifies an OVERLAPPED structure,
etc. SetNamedPipeHandleState is tempting, but the docs there
for PIPE_NOWAIT say:
"Note that nonblocking mode is supported for compatibility with
Microsoft LAN Manager version 2.0 and should not be used to achieve
asynchronous input and output (I/O) with named pipes."
I see that fhandler_pty_master::setup in fhandler_tty.cc does this so
maybe the docs are warning against something that actually works.
Any chance someone out there has come up with a way to do this? I
imagine this isn't (very high) on the priority list for cygwin but with
any luck I haven't crossed the line by trying to get help here.
Thanks much for your time.
Description: Text document
Problem reports: http://cygwin.com/problems.html
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple