summaryrefslogtreecommitdiffstats
path: root/winsup/cygwin/fhandler.h
Commit message (Collapse)AuthorAgeFilesLines
* Replace bogus resize-window-to-clear-screen logic.Kaz Kylheku2021-07-261-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | This removes (ab)uses of SetConsoleScreenBufferSize which sometimes cause cmd.exe to badly misbehave. It probably doesn't like the closely spaced timing of shrinking the window down to one line followed by a restore of the size. Instead we just output newlines to clear the window. * winsup/cygwin/fhandler.h (dev_console::scroll_window): Member function declaration removed. (dev_console::clear_should_scroll): New member function declared. * winsup/cygwin/fhandler_console.cc (dev_console::scroll_window): Member function removed. (dev_console::clear_should_scroll): New member function. Performs only the test that was performed by scroll_window, not the actual scrolling. The scrolling is now done in the caller in the fhandler_console class. (fhandler_console::clear_screen): Call con.clear_should_scroll instead of con.scroll_window. If this returns false, act as before. Otherwise, clear the screen by scrolling the window. This is done not by making SetConsoleScreenBufferSize calls to shrink and restore the window, but by earnestly emitting a number of carriage returns equal to the vertical screen size and then restoring the cursor position.
* Console: provide VT100-like end-of-line print behavior.Kaz Kylheku2021-07-261-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | On the majority of VT100-like terminals, when you print a character in the last column, the cursor sits in a kind of "limbo", as if the cursor position were one column past the edge of the screen. Thus when a line feed is output next, the cursor is then at the start of the next line. The Win32 console write doesn't do this; the cursor position wraps to the start of the next line. If a newline is put out, it translates to an extra newline going to the next-next line. This spoils the behavior of programs which depend on the VT100 behavior. In this patch, the VT100 behavior is simulated as follows. A new flag in the dev_console structure is set when a character is output to the last column. This flag is then observed in order to discard a newline (or rather any character which is mapped mapped to the DWN action). * winsup/cygwin/fhandler.h (class dev_console): New boolean data member, eat_newline. This indicates that if a character is output which moves down to the next line, it should be discarded rather than sent to the console. * winsup/cygwin/fhandler_console.cc (fhandler_console::write_normal): Set the eat_newline flag if the text output ends up at column zero. Honor the eat_newline flag when processing a DWN character, and clear it when processing certain other control characters.
* Cygwin: pty: Discard input already accepted on interrupt.Takashi Yano via Cygwin-patches2021-03-081-0/+2
| | | | | - Currently, input already accepted is not discarded on interrupt by VINTR, VQUIT and VSUSP keys. This patch fixes the issue.
* Cygwin: console: Fix restoring console mode failure.Takashi Yano via Cygwin-patches2021-03-051-0/+1
| | | | | | | - Restoring console mode fails in the following scenario. 1) Start cygwin shell in command prompt. 2) Run 'exec chcp.com'. This patch fixes the issue.
* Cygwin: AF_UNIX: allow opening with the O_PATH flagKen Brown2021-02-241-0/+1
| | | | | | This was done for the fhandler_socket_local class in commits 3a2191653a, 141437d374, and 477121317d, but the fhandler_socket_unix class was overlooked.
* Cygwin: define fhandler_fifo::fstatKen Brown2021-02-191-0/+1
| | | | | | | | | | | | | | | | | | Previously fstat on a FIFO would call fhandler_base::fstat. The latter is not appropriate if fhandler_fifo::open has already been called (and O_PATH is not set), for the following reason. If a FIFO has been opened as a writer or duplexer, then it has an io_handle that is a pipe handle rather than a file handle. fhandler_base::fstat will use this handle and potentially return incorrect results. If the FIFO has been opened as a reader, then it has no io_handle, and a call to fhandler_base::fstat will lead to a call to fhandler_base::open. Opening the fhandler a second time can change it in undesired ways; for example, it can modify the flags and status_flags. The new fhandler_fifo::fstat avoids these problems by creating an fhandler_disk_file and calling its fstat method in case fhandler_fifo::open has already been called and O_PATH is not set.
* Cygwin: default to O_BINARY in fhandler_base::reset_to_open_binmode()Corinna Vinschen2021-02-191-1/+2
| | | | | | | | | | This only affects the very seldom bordercase of apps calling setmode(fd, 0) on fhandlers not calling fhandler_base::set_open_status(). All fhandlers not calling set_open_status() are binary mode only, but the way reset_to_open_binmode worked, calling setmode(fd, 0) would have "reset" their open flags to O_TEXT accidentally. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: pty: Make FLUSHO and Ctrl-O work.Takashi Yano via Cygwin-patches2021-02-191-0/+1
| | | | | - Previously, FLUSHO feature was implemented incompletely. With this patch, FLUSHO and Ctrl-O (VDISCARD) get working.
* Cygwin: console: Fix handling of Ctrl-S in Win7.Takashi Yano via Cygwin-patches2021-02-191-5/+4
| | | | | | | - If ENABLE_LINE_INPUT is set, Ctrl-S is handled by Windows if the OS is Windows 7. This conflicts with Ctrl-S handling in cygwin console code. This patch unsets ENABLE_LINE_INPUT flag in cygwin and set it when native app is executed.
* Cygwin: console: Introduce new thread which handles input signal.Takashi Yano via Cygwin-patches2021-02-171-1/+4
| | | | | | | | | | | | | | - Currently, Ctrl-Z, Ctrl-\ and SIGWINCH does not work in console if the process does not call read() or select(). This is because these are processed in process_input_message() which is called from read() or select(). This is a long standing issue of console. Addresses: https://cygwin.com/pipermail/cygwin/2020-May/244898.html https://cygwin.com/pipermail/cygwin/2021-February/247779.html With this patch, new thread which handles only input signals is introduced so that Crtl-Z, etc. work without calling read() or select(). Ctrl-S and Ctrl-Q are also handled in this thread.
* Cygwin: pty: Reduce unecessary input transfer.Takashi Yano via Cygwin-patches2021-02-121-11/+4
| | | | | | | - Currently, input transfer is performed every time one line is read(), if the non-cygwin app is running in the background. With this patch, transfer is triggered by setpgid() rather than read() so that the unnecessary input transfer can be reduced much in that situation.
* Cygwin: fhandler: clean up 'copyto' logicCorinna Vinschen2021-02-101-183/+194
| | | | | | | | | | | | | | | | | Analyzing the fhandler::copyto logic shows that the fhandler_base::reset method was only called from copyto anyway. Trying to convert reset to a protected method uncovered that the copyto method is actually thought upside down from an object oriented POV. Rather than calling copyto, manipulating the object given as parameter, rename the method to copy_from, which manipulates the calling object itself with data from the object given as parameter. Eventually make reset a protected method and rename it to _copy_from_reset_helper to clarify it's only called from copy_from. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: console: Make read() thread-safe.Takashi Yano via Cygwin-patches2021-02-011-0/+10
| | | | | - Currently read() is somehow not thread-safe. This patch fixes the issue.
* Cygwin: pty: Allow multiple apps to enable pseudo console simultaneously.Takashi Yano via Cygwin-patches2021-01-281-2/+5
| | | | | | | - After commit bb428520, there has been the disadvantage: 7) Pseudo console cannot be activated if it is already activated for another process on same pty. This patch clears this disadvantage.
* Cygwin: pty: Make apps using console APIs be able to debug with gdb.Takashi Yano via Cygwin-patches2021-01-281-0/+3
| | | | | | | | | | - After commit bb428520, there has been the disadvantage: 2) The apps which use console API cannot be debugged with gdb. This is because pseudo console is not activated since gdb uses CreateProcess() rather than exec(). Even with this limitation, attaching gdb to native app, in which pseudo console is already activated, works. This patch clears this disadvantage.
* Cygwin: pty: Inherit typeahead data between two input pipes.Takashi Yano via Cygwin-patches2021-01-281-1/+13
| | | | | | | | | | | | | | | - PTY has a problem that the key input, which is typed during windows native app is running, disappears when it returns to shell. This is beacuse pty has two input pipes, one is for cygwin apps and the other one is for native windows apps. The key input during windows native program is running is sent to the second input pipe while cygwin shell reads input from the first input pipe. This issue had been fixed once by commit 29431fcb, however, the new implementation of pseudo console support by commit bb428520 could not inherit this feature. This patch realize transfering input data between these two pipes bidirectionally by utilizing cygwin-console-helper process. The helper process is launched prior to starting the non-cygwin app, however, exits immediately unlike previous implementation.
* Cygwin: rmdir: handle /dev in fhandler_dev::rmdirCorinna Vinschen2021-01-191-0/+1
| | | | | | | The isdev_dev check in rmdir is unclean. Create a virtual method fhandler_dev::rmdir to handle this transparently. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: pty: Make master thread functions be static.Takashi Yano via Cygwin-patches2021-01-181-4/+26
| | | | | | | | | | | | | - The functions pty_master_thread() and pty_master_fwd_thread() should be static (i.e. should not access class member) because the instance is deleted if the master is dup()'ed and the first master is closed. In this case, because the dup()'ed instance still exists, these master threads are also still alive even though the instance has been deleted. As a result, accesing class members in these functions causes accessi violation. Addresses: https://cygwin.com/pipermail/cygwin-developers/2021-January/012030.html
* Cygwin: pty: Prevent pty from changing code page of parent console.Takashi Yano via Cygwin-patches2021-01-181-2/+3
| | | | | | | | | - After commit 232fde0e, pty changes console code page when the first non-cygwin app is executed. If pty is started in real console device, pty changes the code page of root console. This causes very annoying result because changing code page changes the font of command prompt if console is in legacy mode. This patch avoids this by creating a new invisible console for the first pty started in console device.
* Cygwin: pty: Make close_pseudoconsole() be a static member function.Takashi Yano via Cygwin-patches2021-01-181-1/+2
| | | | | | | - The function close_pseudoconsole() should be static so that it can be safely called in spawn.cc even after the fhandler_pty_slave instance has been deleted. That is, there is a problem with the current code. This patch fixes the issue.
* Cygwin: console: Revise the code to switch xterm mode.Takashi Yano via Cygwin-patches2021-01-181-4/+14
| | | | | | | | | - If application changes the console mode, mode management introduced by commit 10d8c278 will be corrupted. For example, stdout of jansi v2.0.1 or later is piped to less, jansi resets the xterm mode flag ENABLE_VIRTUAL_TERMINA_PROCESSING when jansi is terminated. This causes garbled output in less because less needs this flag enabled. This patch fixes the issue.
* Cygwin: fhandler_fifo: reduce sizeKen Brown2020-11-091-5/+7
| | | | | | | | | | Replace the 'WCHAR pipe_name_buf[48]' class member by 'PWCHAR pipe_name_buf', and allocate space for the latter as needed. Change the default constructor to accommodate this change, and add a destructor that frees the allocated space. Also change get_pipe_name and clone to accommodate this change.
* Cygwin: drop internal O_NOSYMLINK and O_DIROPEN flagsCorinna Vinschen2020-09-071-4/+1
| | | | | | | | | | | | | | | | | | | | Both flags are outdated and collide with official flags in sys/_default_fcntl.h, which may result in weird misbehaviour of file functions. O_NOSYMLINK is not used anyway. O_DIROPEN is used in fhandler_virtual and derived classes. The collision with O_NOFOLLOW results in spurious EISDIR errors when, e. g., reading files in the registry. fhandler_base::open_fs uses O_DIROPEN in the call to fhandler_base::open, but it's not used in this context further down the road. Drop both flags and create an alternative "diropen" bool flag in fhandler_virtual. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: pty: Disable pseudo console if TERM does not have CSI6n.Takashi Yano via Cygwin-patches2020-08-311-0/+1
| | | | | | | | | | | | | | | | | | | - Pseudo console internally sends escape sequence CSI6n (query cursor position) on startup of non-cygwin apps. If the terminal does not support CSI6n, CreateProcess() hangs waiting for response. To prevent hang, this patch disables pseudo console if the terminal does not have CSI6n. This is checked on the first execution of non-cygwin app using the following steps. 1) Check if the terminal support ANSI escape sequences by looking into terminfo database. If terminfo has cursor_home (ESC [H), the terminal is supposed to support ANSI escape sequences. 2) If the terminal supports ANSI escape sequneces, send CSI6n for a test and wait for a responce for 40ms. 3) If there is a responce within 40ms, CSI6n is supposed to be supported. Also set-title capability is checked, and removes escape sequence for setting window title if the terminal does not have the set- title capability.
* Cygwin: pty: Implement new pseudo console support.Takashi Yano2020-08-221-44/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - In this implementation, pseudo console is created for each native console app. Advantages and disadvantages of this implementation over the previous implementation are as follows. Advantages: 1) No performance degradation in pty output for cygwin process. https://cygwin.com/pipermail/cygwin/2020-February/243858.html 2) Free from the problem caused by difference of behaviour of control sequences between real terminal and pseudo console. https://cygwin.com/pipermail/cygwin/2019-December/243281.html https://cygwin.com/pipermail/cygwin/2020-February/243855.html 3) Free from the problem in cgdb and emacs gud. https://cygwin.com/pipermail/cygwin/2020-January/243601.html https://cygwin.com/pipermail/cygwin/2020-March/244146.html 4) Redrawing screen on executing native console apps is not necessary. 5) cygwin-console-helper is not necessary for the pseudo console support. 6) The codes for pseudo console support are much simpler than that of the previous one. Disadvantages: 1) The cygwin program which calls console API directly does not work. 2) The apps which use console API cannot be debugged with gdb. This is because pseudo console is not activated since gdb uses CreateProcess() rather than exec(). Even with this limitation, attaching gdb to native apps, in which pseudo console is already activated, works. 3) Typeahead key inputs are discarded while native console app is executed. Simirally, typeahead key inputs while cygwin app is executed are not inherited to native console app. 4) Code page cannot be changed by chcp.com. Acctually, chcp works itself and changes code page of its own pseudo console. However, since pseudo console is recreated for another process, it cannot inherit the code page. 5) system_printf() does not work after stderr is closed. (Same with cygwin 3.0.7) 6) Startup time of native console apps is about 3 times slower than previous implemenation. 7) Pseudo console cannot be activated if it is already activated for another process on same pty.
* Cygwin: FIFO: synchronize the fifo_reader and fifosel threadsKen Brown2020-08-041-2/+2
| | | | | | | | | | | | | | | | | | | | | | The fifo_reader thread function and the function select.cc:peek_fifo() can both change the state of a fifo_client_handler. These changes are made under fifo_client_lock, so there is no race, but the changes can still be incompatible. Add code to make sure that only one of these functions can change the state from its initial fc_listening state. Whichever function does this calls the fhandler_fifo::record_connection method, which is now public so that peek_fifo can call it. Slightly modify that method to make it suitable for being called by peek_fifo. Make a few other small changes to the fifo_reader thread function to change how it deals with the STATUS_PIPE_CLOSING value that can (rarely) be returned by NtFsControlFile. Add commentary to fhandler_fifo.cc to explain fifo_client connect states and where they can be changed.
* Cygwin: FIFO: don't read from pipes that are closingKen Brown2020-08-041-8/+1
| | | | | | | | | Don't try to read from fifo_client_handlers that are in the fc_closing state. Experiments have shown that this always yields STATUS_PIPE_BROKEN, so it just wastes a Windows system call. Re-order the values in enum fifo_client_connect_state to reflect the new status of fc_closing.
* Cygwin: FIFO: reorganize some fifo_client_handler methodsKen Brown2020-08-041-3/+6
| | | | | | | Rename the existing set_state() to query_and_set_state() to reflect what it really does. (It queries the O/S for the pipe state.) Add a new set_state() method, which is a standard setter, and a corresponding getter get_state().
* Cygwin: FIFO: add a timeout to take_ownershipKen Brown2020-08-041-1/+1
| | | | | | | | | | | | | | fhandler_fifo::take_ownership() is called from select.cc::peek_fifo and fhandler_fifo::raw_read and could potentially block indefinitely if something goes wrong. This is always undesirable in peek_fifo, and it is undesirable in a nonblocking read. Fix this by adding a timeout parameter to take_ownership. Arbitrarily use a 1 ms timeout in peek_fifo and a 10 ms timeout in raw_read. These numbers may have to be tweaked based on experience. Replace the call to cygwait in take_ownership by a call to WFSO. There's no need to allow interruption now that we have a timeout.
* Cygwin: FIFO: clean upKen Brown2020-07-161-6/+4
| | | | | | Remove the fhandler_fifo::get_me method, which is no longer used. Make the methods get_owner, set_owner, owner_lock, and owner_unlock private.
* Cygwin: FIFO: allow take_ownership to be interruptedKen Brown2020-07-161-1/+1
| | | | | | | | Use cygwait in take_ownership to allow interruption while waiting to become owner. Return the cygwait return value or a suitable value to indicate an error. raw_read now checks the return value and acts accordingly.
* Cygwin: FIFO: reduce I/O interleavingKen Brown2020-07-161-1/+2
| | | | | | | | | | | | | | | | Add a bool member 'last_read' to the fifo_client_handler structure, which is set to true on a successful read. This is used by raw_read as follows. When raw_read is called, it first locates the writer (if any) for which last_read is true. raw_read tries to read from that writer and returns if there is input available. Otherwise, it proceeds to poll all the writers, as before. The effect of this is that if a writer writes some data that is only partially read, the next attempt to read will continue to read from the same writer. This should reduce the interleaving of output from different writers.
* Cygwin: fhandler_fifo::hit_eof: improve reliabilityKen Brown2020-07-161-4/+3
| | | | | Use the writer count introduced in the previous commit to help detect EOF. Drop the maybe_eof method, which is no longer needed.
* Cygwin: FIFO: keep a writer count in shared memoryKen Brown2020-07-161-11/+16
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | When a reader opens, it needs to block if there are no writers open (unless is is opened with O_NONBLOCK). This is easy for the first reader to test, since it can just wait for a writer to signal that it is open (via the write_ready event). But when a second reader wants to open, all writers might have closed. To check this, use a new '_nwriters' member of struct fifo_shmem_t, which keeps track of the number of open writers. This should be more reliable than the previous method. Add nwriters_lock to control access to shmem->_nwriters, and remove reader_opening_lock, which is no longer needed. Previously only readers had access to the shared memory, but now writers access it too so that they can increment _nwriters during open/dup/fork/exec and decrement it during close. Add an optional 'only_open' argument to create_shmem for use by writers, which only open the shared memory rather than first trying to create it. Since writers don't need to access the shared memory until they have successfully connected to a pipe instance, they can safely assume that a reader has already created the shared memory. For debugging purposes, change create_shmem to return 1 instead of 0 when a reader successfully opens the shared memory after finding that it had already been created. Remove check_write_ready_evt, write_ready_ok_evt, and check_write_ready(), which are no longer needed. When opening a writer and looping to try to get a connection, recheck read_ready at the top of the loop since the number of readers might have changed. To slightly speed up the process of opening the first reader, take ownership immediately rather than waiting for the fifo_reader_thread to handle it.
* Cygwin: FIFO: fix problems finding new ownerKen Brown2020-07-161-1/+7
| | | | | | | | | | | | | | | | When the owning reader closes and there are still readers open, the owner needs to wait for a new owner to be found before closing its fifo_client handlers. This involves a loop in which dec_nreaders is called at the beginning and inc_nreaders is called at the end. Any other reader that tries to access shmem->_nreaders during this loop will therefore get an inaccurate answer. Fix this by adding an nreaders method and using it instead of dec_nreaders and inc_nreaders. Also add nreaders_lock to control access to the shmem->_nreaders. Make various other changes to improve the reliability of finding a new owner.
* Cygwin: tcp: Support TCP_QUICKACKCorinna Vinschen2020-07-011-0/+1
| | | | Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: tcp: Support TCP_KEEPIDLE, TCP_KEEPCNT, TCP_KEEPINTVLCorinna Vinschen2020-07-011-0/+5
| | | | | | | | | | Use WSAIoctl(SIO_KEEPALIVE_VALS) on older systems. Make sure that keep-alive timeout is equivalent to TCP_KEEPIDLE + TCP_KEEPCNT * TCP_KEEPINTVL on older systems, even with TCP_KEEPCNT being a fixed value on those systems. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: tcp: Support TCP_FASTOPENCorinna Vinschen2020-07-011-1/+2
| | | | | | TCP_FASTOPEN is supported since W10 1607. Fake otherwise. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: pty: Fix screen distortion after less for native apps again.Takashi Yano via Cygwin-patches2020-06-051-1/+0
| | | | | | | | | | | | | - Commit c4b060e3fe3bed05b3a69ccbcc20993ad85e163d seems to be not enough. Moreover, it does not work as expected at all in Win10 1809. This patch essentially reverts that commit and add another fix. After all, the cause of the problem was a race issue in switch_to_pcon_out flag. That is, this flag is set when native app starts, however, it is delayed by wait_pcon_fwd(). Since the flag is not set yet when less starts, the data which should go into the output_handle accidentally goes into output_handle_cyg. This patch fixes the problem more essentially for the cause of the problem than previous one.
* Cygwin: pty: Prevent garbage remained in read ahead buffer.Takashi Yano via Cygwin-patches2020-05-311-1/+2
| | | | | | | | | | | | | | | | - After commit 29431fcb5b14d4c5ac3b3161a076eb1a208349d9, the issue reported in https://cygwin.com/pipermail/cygwin/2020-May/245057.html occurs. This is caused by the following mechanism. Cygwin less called from non-cygwin git is executed under /dev/cons* rather than /dev/pty* because parent git process only inherits pseudo console handle. Therefore, less sets ICANON flag for /dev/cons* rather than original /dev/pty*. When pty is switched to non-cygwin git process, line_edit() is used in fhandler_pty_master::write() only to set input_available_event and read ahead buffer is supposed to be flushed in accept_input(). However, ICANON flag is not set for /dev/pty*, so accept_input() is not called unless newline is entered. As a result, the input data remains in the read ahead buffer. This patch fixes the issue.
* Cygwin: console: Make cursor keys work in vim under ConEmu.Takashi Yano via Cygwin-patches2020-05-301-0/+1
| | | | | | - After commit 774b8996d1f3e535e8267be4eb8e751d756c2cec, cursor keys do not work in vim under ConEmu without cygwin-connector. This patch fixes the issue.
* Cygwin: FIFO: Revert "take ownership on exec"Ken Brown2020-05-221-1/+1
| | | | | | | | | | | | This reverts commit 39a9cd94651d306117c47ea1ac3eab45f6098d0e. There is no need to explicitly take ownership in fixup_after_exec; if ownership transfer is needed, it will be taken care of by fhandler_fifo::close when the parent closes. Moreover, closing the parent's fifo_reader_thread can cause problems, such as the one reported here: https://cygwin.com/pipermail/cygwin-patches/2020q2/010235.html
* Cygwin: pty: Call FreeConsole() only if attached to current pty.Takashi Yano via Cygwin-patches2020-05-191-0/+1
| | | | | | | | | - After commit 071b8e0cbd4be33449c12bb0d58f514ed8ef893c, the problem reported in https://cygwin.com/pipermail/cygwin/2020-May/244873.html occurs. This is due to freeing console device accidentally rather than pseudo console. This patch makes sure to call FreeConsole() only if the process is attached to the pseudo console of the current pty.
* Cygwin: FIFO: code simplificationKen Brown2020-05-111-13/+10
| | | | | | | | | | | | | | | | | | | | | | | | | There are currently three functions that call NtQueryInformationFile to determine the state of a pipe instance. Do this only once, in a new fifo_client_handler::set_state () function, and call that when state information is needed. Remove the fifo_client_handler methods pipe_state and get_state, which are no longer needed. Make fhandler_fifo::get_fc_handler return a reference, for use in select.cc:peek_fifo. Make other small changes to ensure that this commit doesn't change any decisions based on the state of a fifo_client_handler. The tricky part is interpreting FILE_PIPE_CLOSING_STATE, which we translate to fc_closing. Our current interpretation, which is not changing as a result of this commit, is that the writer at the other end of the pipe instance is viewed as still connected from the point of view of raw_read and determining EOF. But it is not viewed as still connected if we are deciding whether to unblock a new reader that is trying to open.
* Cygwin: fifo: fix type of fifo_reader_id_t operatorsCorinna Vinschen2020-05-081-2/+2
| | | | | | | | fifo_reader_id_t::operator == and != have been defined without type accidentally. For some weird reason, only x86 gcc complains about this problem, not x86_64 gcc. Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
* Cygwin: FIFO: support opening multiple readersKen Brown2020-05-081-1/+8
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | Although we can have multiple readers open because of dup/fork/exec, the current code does not support multiple readers opening a FIFO by explicitly calling 'open'. The main complication in supporting this is that when a blocking reader tries to open and there's already one open, it has to check whether there any writers open. It can't rely on the write_ready event, whose state hasn't changed since the first writer opened. To fix this, add two new named events, check_write_ready_evt and write_ready_ok_evt, and a new method, check_write_ready(). The first event signals the owner's reader thread to call check_write_ready(), which polls the fc_handler list to check for connected writers. If it finds none, it checks to see if there's a writer in the process and then sets/resets write_ready appropriately. When check_write_ready() finishes it sets write_ready_ok_evt to signal the reader that write_ready has been updated. The polling is done via fifo_client_handler::pipe_state(). As long as it's calling that function anyway, check_write_ready() updates the state of each handler. Also add a new lock to prevent a race if two readers are trying to open simultaneously.
* Cygwin: FIFO: allow any reader to take ownershipKen Brown2020-05-081-3/+25
| | | | | | | | | | | | | | | | | | | | | Add a take_ownership method, used by raw_read and select.cc:peek_fifo. It wakes up all fifo_reader_threads and allows the caller to become owner. The work is done by the fifo_reader_threads. For synchronization we introduce several new fhandler_fifo data members and methods: - update_needed_evt signals the current owner to stop listening for writer connections and update its fc_handler list. - shared_fc_handler() gets and sets the status of the fc_handler update process. - get_pending_owner() and set_pending_owner() get and set the reader that is requesting ownership. Finally, a new 'reading_lock' prevents two readers from trying to take ownership simultaneously.
* Cygwin: FIFO: find a new owner when closingKen Brown2020-05-081-0/+14
| | | | | | | | | | | | | | | | If the owning reader is closing, wait for another reader (if there is one) to take ownership before closing the owner's pipe handles. To synchronize the ownership transfer, add events owner_needed_evt and owner_found_evt, and add methods owner_needed and owner_found to set/reset them. Modify the fifo_reader_thread function to wake up all non-owners when a new owner is needed. Make a cosmetic change in close so that fhandler_base::close is called only if we have a write handle. This prevents strace output from being littered with statements that the null handle is being closed.
* Cygwin: FIFO: take ownership on execKen Brown2020-05-081-1/+1
| | | | | | | | | | | | | | | If fixup_after_exec is called on a non-close-on-exec reader whose parent is the owner, transfer ownership to the child. Otherwise the parent's pipe handles will be closed before any other reader can duplicate them. To help with this, make the cancel_evt and thr_sync_evt handles inheritable, so that the child can terminate the parent's fifo_reader_thread (and the parent will update the shared fc_handler list). Add an optional argument 'from_exec' to update_my_handlers to simplify its use in this case; no handle duplication is required.
* Cygwin: FIFO: add a shared fifo_client_handler listKen Brown2020-05-081-2/+41
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This is in a new shared memory section. We will use it for temporary storage of the owner's fc_handler list when we need to change owner. The new owner can then duplicate the pipe handles from that list before taking ownership. Add several shared data members and methods that are needed for the duplication process Add methods update_my_handlers and update_shared_handlers that carry out the duplication. Allow the shared list to grow dynamically, up to a point. Do this by initially reserving a block of memory (currently 100 pages) and only committing pages as needed. Add methods create_shared_fc_handler, reopen_shared_fc_handler, and remap_shared_fc_handler to create the new shared memory section, reopen it, and commit new pages. The first is called in open, the second is called in dup/fork/exec, and the third is called in update_shared_handlers if more shared memory is needed. Modify the fifo_reader_thread function to call update_my_handlers when it finds that there is no owner. Also make it call update_shared_handlers when the owner's thread terminates, so that the new owner will have an accurate shared fc_handler list from which to duplicate. For convenience, add new methods cleanup_handlers and close_all_handlers. And add an optional arg to add_client_handler that allows it to create a new fifo_client_handler without creating a new pipe instance.