Commit Graph

675 Commits

Author SHA1 Message Date
Ken Brown f35dfff3de Cygwin: FIFO: find a new owner when closing
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.
2020-05-08 06:45:24 -04:00
Ken Brown 39a9cd9465 Cygwin: FIFO: take ownership on exec
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.
2020-05-08 06:45:20 -04:00
Ken Brown d9918451e2 Cygwin: FIFO: add a shared fifo_client_handler list
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.
2020-05-08 06:36:31 -04:00
Ken Brown c76ded2ca0 Cygwin: FIFO: allow fc_handler list to grow dynamically
Make fc_handler a pointer to malloc'd memory instead of a fixed-size
array.  The size is now a new data member 'shandlers'.  Call realloc
in add_client_handler if we need to grow the array.

free fc_handler in close.  As long as we're touching that code, also
remove an unneeded lock.
2020-05-08 06:32:00 -04:00
Ken Brown 606baf5566 Cygwin: FIFO: designate one reader as owner
Among all the open readers of a FIFO, one is declared to be the owner.
This is the only reader that listens for client connections, and it is
the only one that has an accurate fc_handler list.

Add shared data and methods for getting and setting the owner, as well
as a lock to prevent more than one reader from accessing these data
simultaneously.

Modify the fifo_reader_thread so that it checks the owner at the
beginning of its loop.  If there is no owner, it takes ownership.  If
there is an owner but it is a different reader, the thread just waits
to be canceled.  Otherwise, it listens for client connections as
before.

Remove the 'first' argument from create_pipe_instance.  It is not
needed, and it may be confusing in the future since only the owner
knows whether a pipe instance is the first.

When opening a reader, don't return until the fifo_reader_thread has
time to set an owner.

If the owner closes, indicate that there is no longer an owner.

Clear the child's fc_handler list in dup, and don't bother duplicating
the handles.  The child never starts out as owner, so it can't use
those handles.

Do the same thing in fixup_after_fork in the close-on-exec case.  In
the non-close-on-exec case, the child inherits an fc_handler list that
it can't use, but we can just leave it alone; the handles will be
closed when the child is closed.
2020-05-08 06:32:00 -04:00
Ken Brown 16e7c10578 Cygwin: FIFO: introduce a new type, fifo_reader_id_t
This uniquely identifies an fhandler_fifo open for reading in any
process.

Add a new data member 'me' of this type, which is set in open, dup,
fork, and exec.
2020-05-08 06:32:00 -04:00
Ken Brown 365818a4a5 Cygwin: FIFO: keep track of the number of readers
Add data and methods to the shared memory that keep track of the
number of open readers.

Increment this number in open, dup, fork, and exec.  Decrement it in
close.  Reset read_ready if there are no readers left.
2020-05-08 06:32:00 -04:00
Ken Brown 878eb22462 Cygwin: FIFO: add shared memory
Even though we currently allow a FIFO to be opened for reading only
once, we can still have more than one reader open because of dup and
fork.  Add a named shared memory section accessible to all readers of
a given FIFO.  In future commits we will add information needed by all
readers to this section

Add a class fifo_shmem_t that lets us access this information.

Add a method create_shmem that is called when a reader opens, and add
a method reopen_shmem that is called by dup, fork, and exec.  (Each
new reader needs its own view of the shared memory.)
2020-05-08 06:32:00 -04:00
Ken Brown 71726ba70b Cygwin: FIFO: use a cygthread instead of a homemade thread
This will simplify future work.

Rename the thread from "listen_client_thread" to "fifo_reader_thread"
because it will be used for more than just listening.

Remove the fixup_before stuff, which won't be needed after future
changes to fixup_after_fork and fixup_after_exec.
2020-05-08 06:32:00 -04:00
Ken Brown 9ee8fdf2b3 Cygwin: FIFO: make opening a writer more robust
- Make read_ready a manual-reset event.

- Signal read_ready in open instead of in the listen_client_thread.

- Don't reset read_ready when the listen_client thread terminates;
  instead do it in close().

- Rearrange open and change its error handling.

- Add a wait_open_pipe method that waits for a pipe instance to be
  available and then calls open_pipe.  Use it when opening a writer if
  we can't connect immediately.  This can happen if the system is
  heavily loaded and/or if many writers are trying to open
  simultaneously.
2020-05-08 06:32:00 -04:00
Ken Brown 301454f132 Cygwin: FIFO: fix hit_eof
According to Posix, a FIFO open for reading is at EOF if it is empty
and there are no writers open.

The only way to test this is to poll the fifo_client_handlers as in
raw_read and select.cc:peek_fifo.  The current hit_eof instead relies
on the value of nconnected, which can be out of date.  On the one
hand, it doesn't take into account writers that were connected but
have since closed.  On the other hand, it doesn't take into account
writers that are in the process of opening but haven't yet connected.

Fix this by introducing a maybe_eof method that tentatively assumes
EOF if there are no connected writers after polling.  Then check for
writers currently opening (via a new 'writer_opening' event), and wait
for the fifo_reader_thread to record any new connection that was made
while we were polling.

To handle the needs of peek_fifo, replace the get_fc_handle method
by a get_fc_handler method, and add a fifo_client_handler::get_state
method.

Remove the is_connected method, which was used only in peek_fifo and
is no longer needed.

Remove the nconnected data member, which was used only for the flawed
hit_eof.

Add some comments about events to fhandler.h.
2020-05-08 06:32:00 -04:00
Ken Brown 25e8727368 Cygwin: FIFO: remove the arm method
There's no reason to check for errors when we set read_ready or
write_ready.  We don't do that for other events.
2020-05-08 06:32:00 -04:00
Ken Brown 9b2afd78ce Cygwin: FIFO: simplify the listen_client_thread code
Always return 0; no one is doing anything with the return value
anyway.

Remove the return value from stop_listen_client.

Make the connection event auto-reset, so that we don't have to reset
it later.

Simplify the process of connecting a bogus client when thread
termination is signaled.

Make some failures fatal.

Remove the unnecessary extra check for thread termination near the end
of listen_client_thread.
2020-05-08 06:32:00 -04:00
Ken Brown 32dbc3d215 Cygwin: FIFO: change the fifo_client_connect_state enum
Make the values correspond to the possible return values of
fifo_client_handler::pipe_state().

When cleaning up the fc_handler list in listen_client_thread(), don't
delete handlers in the fc_closing state.  I think the pipe might still
have input to be read in that case.

Set the state to fc_closing later in the same function if a connection
is made and the status returned by NtFsControlFile is
STATUS_PIPE_CLOSING.

In raw_read, don't error out if NtReadFile returns an unexpected
status; just set the state of that handler to fc_error.  One writer in
a bad state doesn't justify giving up on reading.
2020-05-08 06:32:00 -04:00
Ken Brown ce23e97640 Cygwin: FIFO: simplify the fifo_client_handler structure
Replace the 'fhandler_base *' member by a HANDLE to the server side of
the Windows named pipe instance.  Make the corresponding
simplifications throughout.
2020-05-08 06:32:00 -04:00
Corinna Vinschen 2a4b1de773 Cygwin: serial: use per call OVERLAPPED structs
Sharing the OVERLAPPED struct and event object in there between
read and select calls in the fhandler might have been a nice
optimization way back when, but it is a dangerous, not thread-safe
approach.  Fix this by creating per-fhandler, per-call OVERLAPPED
structs and event objects on demand.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-03-23 21:06:03 +01:00
Åke Rehnman e4f9fc92ac Cygwin: serial: select: fix previous revamp patch
- We need a verify function.
- The event object referenced in WaitCommEvent must not be a local var,
  move it into fhandler_serial.
2020-03-22 15:15:19 +01:00
Corinna Vinschen 2929599590 Cygwin: serial: revamp overlapped IO in read and select
Get rid of WaitCommEvent and using overlapped_armed to share the
same overlapped operation between read and select.  Rather, make
sure to cancel the overlapped IO before leaving any of these functions.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-03-22 15:15:19 +01:00
Corinna Vinschen 93b491c4f2 Cygwin: serial: read: revamp raw_read, change vmin_ and vtime_ to cc_t
- Datatypes were incorrect, especially vmin_ and vtime_.
  Change them to cc_t, as in user space.

- Error checking had a gap or two.  Debug output used the
  wrong formatting.

- Don't use ev member for ClearCommError and WaitCommEvent.
  Both returned values are different (error value vs. event
  code).  The values are not used elsewhere so it doesn't make
  sense to store them in the object.  Therefore, drop ev member.

- Some variable names were not very helpful.  Especially using
  n as lpNumberOfBytesTransferred from GetOverlappedResult and
  then actually printing it as if it makes sense was quite
  puzzeling.

- Rework the loop and the definition of minchars so that it
  still makes sense when looping.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-03-22 15:15:19 +01:00
Corinna Vinschen b74bc88385 Cygwin: fix formatting: drop trailing whitespace
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-03-11 13:45:58 +01:00
Corinna Vinschen d2ef2331f9 Cygwin: fix formatting: drop spaces leading tabs
Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-03-11 13:45:58 +01:00
Takashi Yano b4bc238311 Cygwin: console: Add a workaround for "ESC 7" and "ESC 8".
- In xterm compatible mode, "ESC 7" and "ESC 8" do not work properly
  in the senario:
   1) Execute /bin/ls /bin to fill screen.
   2) Sned CSI?1049h to alternate screen.
   3) Reduce window size.
   4) Send CSI?1049l to resume screen.
   5) Send "ESC 7" and "ESC 8".
  After sending "ESC 8", the cursor goes to incorrect position. This
  patch adds a workaround for this issue.
2020-03-02 19:46:40 +01:00
Takashi Yano 002206dc7c Cygwin: console: Adjust the detailed behaviour of ESC sequences.
- This patch makes some detailed behaviour of ESC sequences such as
  "CSI Ps L" (IL), "CSI Ps M" (DL) and "ESC M" (RI) in xterm mode
  match with real xterm.
2020-02-28 15:32:51 +01:00
Corinna Vinschen 729cb70bcf Cygwin: AF_UNIX: rework fixup_after_exec
fhandler_socket_unix::fixup_after_exec incorrectly calls
fhandler_socket_unix::fixup_after_fork with a NULL parent process
handle.  Not only that calling DuplicateHandle with a NULL parent
handle fails, but it's utterly wrong trying to duplicate the handles
at all here.

Rather just set some important values to NULL and reopen the shared
memory region.  Create a fixup_helper method to call common code from
fixup_after_fork and fixup_after_exec.

Add comments to other invocations of fixup_after_fork with NULL
handle to mark them as correct this way.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-02-28 15:27:16 +01:00
Takashi Yano 81b3440998 Cygwin: console: Add guard for set/unset xterm compatible mode.
- Setting / unsetting xterm compatible mode may cause race issue
  between multiple processes. This patch adds guard for that.
2020-02-18 11:35:42 +01:00
Takashi Yano 29431fcb5b Cygwin: pty: Inherit typeahead data between two input pipes.
- PTY has a problem that the key input, which is typed during
  windows native app is running, disappear when it returns to shell.
  (Problem 3 in https://cygwin.com/ml/cygwin/2020-02/msg00007.html)
  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 patch realize transfering input data between these two pipes.
2020-02-10 10:19:38 +01:00
Takashi Yano b0474b704a Cygwin: pty: Avoid screen distortion on slave read.
- Echo back print is distorted when the cygwin program which calls
  Win32 console API directly calls slave read(). This patch fixes
  the issue.
2020-02-10 10:19:38 +01:00
Takashi Yano 2bae159171 Cygwin: pty: Define mask_switch_to_pcon_in() in fhandler_tty.cc.
- This patch moves the definition of mask_switch_to_pcon() from
  fhandler.h to fhandler_tty.cc.
2020-02-10 10:19:38 +01:00
Takashi Yano 3a71c46380 Cygwin: pty: Use pinfo() rather than kill() with signal 0.
- PTY code has a problem that tcsh is terminated if the following
  command is executed.
    true; chcp &
  This seems to be caused by invalid pointer access which occurs
  when the process exits during the kill() code is execuetd. This
  patch avoids the issue by not using kill().
2020-02-06 19:58:31 +01:00
Takashi Yano ac1ccc29e1 Cygwin: console: Revise color setting codes in legacy console mode.
- With this patch, foreground color and background color are allowed
  to be set simultaneously by 24 bit color escape sequence such as
  ESC[38;2;0;0;255;48;2;128;128;0m in legacy console mode.
2020-02-04 11:49:20 +01:00
Ken Brown 477121317d Cygwin: AF_LOCAL: fix fcntl and dup if O_PATH is set
Make fhandler_socket_local::dup and fhandler_socket_local::fcntl (a
new method) call fhandler_base::dup and fhandler_base::fcntl if O_PATH
is set.

We're viewing the socket as a disk file here, but there's no need to
implement the actions of fhandler_disk_file::dup and
fhandler_disk_file::fcntl, which do nothing useful in this case beyond
what the fhandler_base methods do.  (The extra actions are only useful
when I/O is going to be done on the file.)
2020-01-30 09:43:19 -05:00
Ken Brown 3a2191653a Cygwin: AF_LOCAL: allow opening with the O_PATH flag
If that flag is not set, or if an attempt is made to open a different
type of socket, the errno is now EOPNOTSUPP instead of ENXIO.  This is
consistent with POSIX, starting with the 2016 edition.  Earlier
editions were silent on this issue.

Opening is done in a (new) fhandler_socket_local::open method by
calling fhandler_base::open_fs.

Also add a corresponding fhandler_socket_local::close method.
2020-01-30 09:43:19 -05:00
Ken Brown 68b7a457f7 Cygwin: fhandler_disk_file::fstatvfs: refactor
Define a new method fhandler_base::fstatvfs_by_handle, extracted from
fhandler_disk_file::fstatvfs, which gets the statvfs information when
a handle is available.

This will be used in future commits for special files that have been
opened with O_PATH.
2020-01-28 14:05:13 -05:00
Takashi Yano e38f2dc9b9 Cygwin: pty: Revise code waiting for forwarding again.
- After commit 6cc299f0e2, outputs of
  cygwin programs which call both printf() and WriteConsole() are
  frequently distorted. This patch fixes the issue.
2020-01-28 09:33:28 +01:00
Takashi Yano 5ba41ad6e9 Cygwin: console: Share readahead buffer within the same process.
- The cause of the problem reported in
  https://www.cygwin.com/ml/cygwin/2020-01/msg00220.html is that the
  chars input before dup() cannot be read from the new file descriptor.
  This is because the readahead buffer (rabuf) in the console is newly
  created by dup(), and does not inherit from the parent. This patch
  fixes the issue.
2020-01-28 09:30:27 +01:00
Takashi Yano 2f415d5efa Cygwin: pty: Disable FreeConsole() on close for non cygwin process.
- After commit e1a0775dc0, the problem
  reported in https://www.cygwin.com/ml/cygwin/2020-01/msg00093.html
  occurs. For Gnu scren and tmux, calling FreeConsole() on pty close
  is necessary. However, if FreeConsole() is called, cygwin setup
  with '-h' option does not work. Therefore, the commit
  e1a0775dc0 delayed closing pty.
  This is the cause of the problem above. Now, instead of delaying
  pty close, FreeConsole() is not called if the process is non cygwin
  processes such as cygwin setup.
2020-01-14 17:19:19 +01:00
Takashi Yano fd03749b9c Cygwin: pty: Remove destructor for fhandler_pty_master class.
- The destructor for fhandler_pty_master class does not seem to be
  necessary anymore. Therefore, it has been removed.
2020-01-13 16:51:17 +01:00
Takashi Yano edb1be4cce Cygwin: pty: Convert CamelCase names to snake_case names. 2019-11-18 11:12:08 +01:00
Takashi Yano 3880efb283 Cygwin: console, pty: Prevent error in legacy console mode. 2019-11-08 10:08:45 +01:00
Takashi Yano 1a44ad03e0 Cygwin: console: Revive Win7 compatibility.
- The commit fca4cda7a4 broke Win7
  compatibility. This patch fixes the issue.
2019-09-19 22:17:54 -04:00
Takashi Yano d83c45b46c Cygwin: pty: Use system NLS function instead of PTY's own one.
- Since calling system __loadlocale() caused execution error,
  PTY used its own NLS function. The cause of the error has been
  found, the corresponding code has been rewritten using system
  function.
2019-09-15 11:09:06 -04:00
Takashi Yano 3355a6d4b9 Cygwin: pty: Switch input and output pipes individually.
- Previously, input and output pipes were switched together between
  the traditional pty and the pseudo console. However, for example,
  if stdin is redirected to another device, it is better to leave
  input pipe traditional pty side even for non-cygwin program. This
  patch realizes such behaviour.
2019-09-14 09:19:04 -04:00
Takashi Yano b088f50426 Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.
- When the I/O pipe is switched to the pseudo console side, the
  behaviour of Ctrl-C was unstable. This rarely happens, however,
  for example, shell sometimes crashes by Ctrl-C in that situation.
  Furthermore, Ctrl-C was ignored if output of non-cygwin program
  is redirected to pipe. This patch fixes these issues.
2019-09-14 08:58:35 -04:00
Takashi Yano 915fcd0ae8 Cygwin: pty: Fix select() with pseudo console support.
- select() did not work correctly when both read and except are
  polled simultaneously for the same fd and the r/w pipe is switched
  to pseudo console side. This patch fixes this isseu.
2019-09-05 13:59:37 +02:00
Takashi Yano 583102e7c9 Cygwin: pty: Fix state management for pseudo console support.
- Pseudo console support introduced by commit
  169d65a577 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.
2019-09-04 12:05:15 +02:00
Takashi Yano 169d65a577 Cygwin: pty: add pseudo console support.
- Support pseudo console in PTY. Pseudo console is a new feature
  in Windows 10 1809, which provides console APIs on virtual
  terminal. With this patch, native console applications can work
  in PTYs such as mintty, ssh, gnu screen or tmux.
2019-08-29 13:47:40 +02:00
Corinna Vinschen dec444bee3 Cygwin: change virtual_ftype_t to not rely on negative values
So far negative values were denoting files, positive values
denoting directories.  We should prefer a less error prone
method.  Redefine virtual_ftype_t to contain only positive
values and replace checks for negativ or positive values with
inline functions virt_ftype_isfile() and virt_ftype_isdir().

Drop outdcated comments referring to numerical virtual_ftype_t
values.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2019-07-22 18:42:39 +02:00
Ken Brown 4beb9da118 Cygwin: fhandler_*: remove isdevice() and is_auto_device()
isdevice() is used only in the definition of is_auto_device().  And
the latter is used only once, in a context where isdevice() always
returns true.
2019-07-22 08:15:16 -04:00
Ken Brown d54edfdf81 Cygwin: FIFO: minor cleanup
Don't use a label with the same name as a variable.

Also fix indentation in fhandler.h.
2019-06-23 10:16:34 -04:00
Ken Brown 6e7e82fee7 Cygwin: FIFO: remove fifo_client_handler::connect_evt
It's not needed.  Instead just create and use an event in
fhandler_fifo::listen_client_thread.
2019-06-23 10:16:34 -04:00