2019-01-19 20:02:54 +01:00
|
|
|
/* fhandler_pipe.cc: pipes for Cygwin.
|
2000-02-17 19:38:33 +00:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
2000-08-02 16:28:18 +00:00
|
|
|
#include "winsup.h"
|
2004-09-03 01:32:02 +00:00
|
|
|
#include <stdlib.h>
|
2002-11-09 03:17:40 +00:00
|
|
|
#include <sys/socket.h>
|
2000-08-22 03:58:47 +00:00
|
|
|
#include "cygerrno.h"
|
2001-07-26 19:22:24 +00:00
|
|
|
#include "security.h"
|
2001-10-01 04:10:07 +00:00
|
|
|
#include "path.h"
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 00:37:18 +00:00
|
|
|
#include "fhandler.h"
|
2024-08-18 22:07:16 +02:00
|
|
|
#include "select.h"
|
2000-08-22 05:10:20 +00:00
|
|
|
#include "dtable.h"
|
2001-04-18 21:10:15 +00:00
|
|
|
#include "cygheap.h"
|
2001-09-24 21:50:44 +00:00
|
|
|
#include "pinfo.h"
|
2009-10-31 13:24:06 +00:00
|
|
|
#include "shared_info.h"
|
2021-09-21 08:02:43 +09:00
|
|
|
#include "tls_pbuf.h"
|
2024-03-11 22:08:00 +09:00
|
|
|
#include "sigproc.h"
|
2021-12-26 16:42:26 -05:00
|
|
|
#include <assert.h>
|
2001-09-24 21:50:44 +00:00
|
|
|
|
2019-05-25 13:03:08 -04:00
|
|
|
/* This is only to be used for writing. When reading,
|
|
|
|
STATUS_PIPE_EMPTY simply means there's no data to be read. */
|
|
|
|
#define STATUS_PIPE_IS_CLOSED(status) \
|
|
|
|
({ NTSTATUS _s = (status); \
|
|
|
|
_s == STATUS_PIPE_CLOSING \
|
|
|
|
|| _s == STATUS_PIPE_BROKEN \
|
|
|
|
|| _s == STATUS_PIPE_EMPTY; })
|
|
|
|
|
2021-09-03 19:41:52 -04:00
|
|
|
fhandler_pipe_fifo::fhandler_pipe_fifo ()
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
: fhandler_base (), pipe_buf_size (DEFAULT_PIPEBUFSIZE), pipe_mtx (NULL)
|
2021-09-03 19:41:52 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
* devices.cc: New file.
* devices.gperf: New file.
* devices.shilka: New file.
* cygwin-gperf: New file.
* cygwin-shilka: New file.
* fhandler_fifo.cc: New file.
* fhandler_nodevice.cc : New file. Reorganize headers so that path.h precedes
fhandler.h throughout. Remove device argument and unit arguments from fhandler
constructors throughout. Remove pc arguments to fhandler functions and use
internal pc element instead, throughout. Use dev element in pc throughout.
Use major/minor elements rather than units and device numbers previously in
fhandler class. Use correct methods for fhandler file names rather than
directly accessing file name variables, throughout.
* Makefile.in (DLL_OFILES): Add devices.o, fhandler_fifo.o
* dcrt0.cc (dll_crt0_1): Call device::init.
* devices.h: Renumber devices based on more Linux-like major/minor numbers.
Add more devices. Declare standard device storage.
(device): Declare struct.
* dir.cc (opendir): Use new 'build_fh_name' to construct a fhandler_* type.
* dtable.cc (dtable::get_debugger_info): Ditto.
(cygwin_attach_handle_to_fd): Ditto.
(dtable::release): Remove special FH_SOCKET case in favor of generic
"need_fixup_before" test.
(dtable::init_std_file_from_handle): Use either build_fh_dev or build_fh_name
to build standard fhandler.
(dtable::build_fh_name): Renamed from dtable::build_fhandler_from_name. Move
out of dtable class. Don't accept a path_conv argument. Just build it here
and pass it to:
(build_fh_pc): Renamed from dtable::build_fhandler. Move out of dtable class.
Use intrinsic device type in path_conv to create new fhandler.
(build_fh_dev): Renamed from dtable::build_fhandler. Move out of dtable class.
Simplify arguments to just take new 'device' type and a name. Just return
pointer to fhandler rather than trying to insert into dtable.
(dtable::dup_worker): Accommodate above build_fh name changes.
(dtable::find_fifo): New (currently broken) function.
(handle_to_fn): Use strechr for efficiency.
* dtable.h: Reflect above build_fh name changes and argument differences.
(fhandler_base *&operator []): Return self rather than copy of self.
* fhandler.cc (fhandler_base::operator =): Use pc element to set normalized
path.
(fhandler_base::set_name): Ditto.
(fhandler_base::raw_read): Use method to access name.
(fhandler_base::write): Correctly use get_output_handle rather than get_handle.
(handler_base::device_access_denied): New function.
(fhandler_base::open): Eliminate pc argument and use pc element of
fhandler_base throughout.
(fhandler_base::fstat): Detect if device is based in filesystem and use
fstat_fs to calculate stat, if so.
(fhandler_base::fhandler_base): Eliminate handling of file names and, instead,
just free appropriate component from pc.
(fhandler_base::opendir): Remove path_conv parameter.
* fhandler.h: Remove all device flags.
(fhandler_base::pc): New element.
(fhandler_base::set_name): Change argument to path_conv.
(fhandler_base::error): New function.
(fhandler_base::exists): New function.
(fhandler_base::pc_binmode): New function.
(fhandler_base::dev): New function.
(fhandler_base::open_fs): New function.
(fhandler_base::fstat_fs): New function.
(fhandler_base::fstat_by_name): New function.
(fhandler_base::fstat_by_handle): New function.
(fhandler_base::isfifo): New function.
(fhandler_base::is_slow): New function.
(fhandler_base::is_auto_device): New function.
(fhandler_base::is_fs_special): New function.
(fhandler_base::device_access_denied): New function.
(fhandler_base::operator DWORD&): New operator.
(fhandler_base::get_name): Return normalized path from pc.
(fhandler_base::get_win32_name): Return windows path from pc.
(fhandler_base::isdevice): Renamed from is_device.
(fhandler_base::get_native_name): Return device format.
(fhandler_fifo): New class.
(fhandler_nodevice): New class.
(select_stuff::device_specific): Remove array.
(select_stuff::device_specific_pipe): New class element.
(select_stuff::device_specific_socket): New class element.
(select_stuff::device_specific_serial): New class element.
(select_stuff::select_stuff): Initialize new elements.
* fhandler_disk_file.cc (fhandler_base::fstat_by_handle): Move to base class
from fhandler_disk_file.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_base::fstat_by_name): Ditto.
(fhandler_disk_file::open): Move most functionality into
fhandler_base::open_fs.
(fhandler_base::open_fs): New function.
(fhandler_disk_file::close): Move most functionality into
fhandler_base::close_fs.
(fhandler_base::close_fs): New function.
* fhandler_mem.cc (fhandler_dev_mem::open): Use device name in debugging
output.
* fhandler_socket.cc (fhandler_socket::set_connect_secret): Copy standard
urandom device into appropriate place.
(fhandler_socket::accept): Reflect change in fdsock return value.
* fhandler_tty.cc: See "throughouts" above.
* net.cc: Accommodate fdsock change throughout.
(fdsock): Return success or failure, accept fd argument and device argument.
* path.cc (symlink_info::major): New element.
(symlink_info::minor): New element.
(symlink_info::parse_device): Declare new function.
(fs_info::update): Accommodate changes in path_conv class.
(path_conv::fillin): Ditto.
(path_conv::return_and_clear_normalized_path): Eliminate.
(path_conv::set_normalized_path): New function.
(path_conv::path_conv): Set info in dev element. Use path_conv methods Check
for FH_FS rather than FH_BAD to indicate when to fill in filesystem stuff.
where appropriate rather than direct access. Use set_normalized_path to set
normalized path.
(windows_device_names): Eliminate.
(get_dev): Ditto.
(get_raw_device_number): Ditto.
(get_device_number): Ditto.
(win32_device_name): Call new device name parser to do most of the heavy
lifting.
(mount_info::conv_to_win32_path): Fill in dev field as appropriate.
(symlink_worker): Handle new device files.
(symlink_info::check): Ditto.
(symlink_info::parse_device): Define new function.
* path.h (executable_states): Move here from fhandler.h.
(fs_info): Rename variables to *_storage and create methods for accessing same.
(path_conv): Add dev element, remove devn and unit and adjust inline methods to
accommodate.
(set_normalized_path): Declare new function.
* pinfo.cc (_pinfo::commune_recv): Add broken support for handling fifos.
(_pinfo::commune_send): Ditto.
* pipe.cc (fhandler_pipe::close): check for existence of handle before closing
it.
(handler_pipe::create): Rename from make_pipe. Change arguments to accept
fhandler_pipe array. Accommodate fifos.
(pipe): Rework to deal with fhandler_pipe::create changes.
(_pipe): Ditto.
* select.cc: Use individual device_specific types throughout rather than
indexing with obsolete device number.
(set_bits): Use is_socket call rather than checking device number.
* shared_info.h (CURR_MOUNT_MAGIC): Update.
(conv_to_win32_path): Reflect addition of device argument.
* syscalls.cc (mknod_worker): New function.
(open): Use build_fh_name to build fhandler.
(chown_worker): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(chmod_device): New function.
(chmod): Detect if this is an 'auto' device rather than an on-filesystem device
and handle appropriately. Use chmod_device to set mode of in-filesystem
devices.
(stat_worker): Eliminate path_conv argument. Call build_fh_name to construct
fhandler. Use fh->error() rather than pc->error to detect errors in fhandler
construction.
(access_worker): New function pulled from access. Accommodate in-filesystem
devices.
(access): Use access_worker.
(fpathconf): Detect if this is an 'auto' device rather than an on-filesystem
device and handle appropriately.
(mknod_worker): New function.
(mknod32): New function.
(chroot): Free normalized path -- assuming it was actually cmalloced.
* tty.cc (create_tty_master): Tweak for new device class.
(tty::common_init): Ditto.
* winsup.h (stat_worker): Remove.
(symlink_worker): Declare.
* exceptions.cc (set_process_mask): Just call sig_dispatch_pending and don't
worry about pending_signals since sig_dispatch_pending should always do the
right thing now.
(sig_handle): Reorganize SIGCONT handling to more closely conform to SUSv3.
* pinfo.h: Move __SIG enum to sigproc.h.
(PICOM_FIFO): New enum element.
(_pinfo): Remove 'thread2signal' stuff throughout class.
(_pinfo::commune_send): Make varargs.
(_pinfo::sigtodo): Eliminate.
(_pinfo::thread2signal): Ditto.
* signal.cc (kill_worker): Eliminate call to setthread2signal.
* sigproc.cc (local_sigtodo): Eliminate.
(getlocal_sigtodo): Ditto.
(sigelem): New class.
(pending_signals): New class.
(sigqueue): New variable, start of sigqueue linked list.
(sigcatch_nonmain): Eliminate.
(sigcatch_main): Eliminate.
(sigcatch_nosync): Eliminate.
(sigcomplete_nonmain): Eliminate.
(pending_signals): Eliminate.
(sig_clear): Call signal thread to clear pending signals, unless already in
signal thread.
(sigpending): Call signal thread to get pending signals.
(sig_dispatch_pending): Eliminate use of pending_signals and just check
sigqueue.
(sigproc_terminate): Eliminate all of the obsolete semaphore stuff. Close
signal pipe handle.
(sig_send): Eliminate all of the obsolete semaphore stuff and use pipe to send
signals.
(getevent): Eliminate.
(pending_signals::add): New function.
(pending_signals::del): New function.
(pending_signals::next): New function.
(wait_sig): Eliminate all of the obsolete semaphore stuff. Use pipe to
communicate and maintain a linked list of signals.
* sigproc.h: Move __SIG defines here. Add __SIGPENDING.
(sig_dispatch_pending): Remove "C" specifier.
(sig_handle): Accept a mask argument.
* thread.cc: Remove signal handling considerations throughout.
2003-09-25 00:37:18 +00:00
|
|
|
fhandler_pipe::fhandler_pipe ()
|
2021-09-03 19:41:52 -04:00
|
|
|
: fhandler_pipe_fifo (), popen_pid (0)
|
2001-09-22 16:55:02 +00:00
|
|
|
{
|
2006-05-21 05:25:49 +00:00
|
|
|
need_fork_fixup (true);
|
2001-09-22 16:55:02 +00:00
|
|
|
}
|
|
|
|
|
2019-05-25 10:27:38 -04:00
|
|
|
/* The following function is intended for fhandler_pipe objects
|
|
|
|
created by the second version of fhandler_pipe::create below. See
|
|
|
|
the comment preceding the latter.
|
|
|
|
|
|
|
|
In addition to setting the blocking mode of the pipe handle, it
|
|
|
|
also sets the pipe's read mode to byte_stream unconditionally. */
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
NTSTATUS
|
2019-05-25 13:05:49 -04:00
|
|
|
fhandler_pipe::set_pipe_non_blocking (bool nonblocking)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
FILE_PIPE_INFORMATION fpi;
|
|
|
|
|
|
|
|
fpi.ReadMode = FILE_PIPE_BYTE_STREAM_MODE;
|
|
|
|
fpi.CompletionMode = nonblocking ? FILE_PIPE_COMPLETE_OPERATION
|
|
|
|
: FILE_PIPE_QUEUE_OPERATION;
|
|
|
|
status = NtSetInformationFile (get_handle (), &io, &fpi, sizeof fpi,
|
|
|
|
FilePipeInformation);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
debug_printf ("NtSetInformationFile(FilePipeInformation): %y", status);
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
return status;
|
2019-05-25 13:05:49 -04:00
|
|
|
}
|
|
|
|
|
2009-07-03 18:05:51 +00:00
|
|
|
int
|
2016-01-11 12:35:41 +01:00
|
|
|
fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id)
|
2007-12-16 21:21:23 +00:00
|
|
|
{
|
2009-08-04 04:20:36 +00:00
|
|
|
/* FIXME: Have to clean this up someday
|
|
|
|
FIXME: Do we have to check for both !get_win32_name() and
|
|
|
|
!*get_win32_name()? */
|
|
|
|
if ((!get_win32_name () || !*get_win32_name ()) && get_name ())
|
2007-12-16 21:21:23 +00:00
|
|
|
{
|
2009-08-04 04:20:36 +00:00
|
|
|
char *d;
|
|
|
|
const char *s;
|
2007-12-16 21:21:23 +00:00
|
|
|
char *hold_normalized_name = (char *) alloca (strlen (get_name ()) + 1);
|
2009-08-04 04:20:36 +00:00
|
|
|
for (s = get_name (), d = hold_normalized_name; *s; s++, d++)
|
2007-12-16 21:21:23 +00:00
|
|
|
if (*s == '/')
|
|
|
|
*d = '\\';
|
|
|
|
else
|
|
|
|
*d = *s;
|
|
|
|
*d = '\0';
|
|
|
|
set_name (hold_normalized_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool opened_properly = a & FILE_CREATE_PIPE_INSTANCE;
|
|
|
|
a &= ~FILE_CREATE_PIPE_INSTANCE;
|
2008-01-01 18:51:23 +00:00
|
|
|
fhandler_base::init (f, a, mode);
|
2010-01-14 18:46:02 +00:00
|
|
|
close_on_exec (mode & O_CLOEXEC);
|
2016-01-11 12:35:41 +01:00
|
|
|
set_ino (uniq_id);
|
|
|
|
set_unique_id (uniq_id | !!(mode & GENERIC_WRITE));
|
2010-04-02 18:55:02 +00:00
|
|
|
if (opened_properly)
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
/* Set pipe always blocking and simulate non-blocking mode in
|
|
|
|
raw_read()/raw_write() to allow signal handling even with
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT. */
|
|
|
|
set_pipe_non_blocking (false);
|
2024-03-05 23:34:21 +09:00
|
|
|
|
2009-07-03 18:05:51 +00:00
|
|
|
return 1;
|
2007-12-16 21:21:23 +00:00
|
|
|
}
|
|
|
|
|
2005-02-06 11:15:29 +00:00
|
|
|
extern "C" int sscanf (const char *, const char *, ...);
|
|
|
|
|
2005-02-01 15:11:47 +00:00
|
|
|
int
|
|
|
|
fhandler_pipe::open (int flags, mode_t mode)
|
|
|
|
{
|
2016-01-11 12:35:41 +01:00
|
|
|
HANDLE proc, nio_hdl = NULL;
|
|
|
|
int64_t uniq_id;
|
|
|
|
fhandler_pipe *fh = NULL, *fhr = NULL, *fhw = NULL;
|
2005-02-06 11:15:29 +00:00
|
|
|
size_t size;
|
|
|
|
int pid, rwflags = (flags & O_ACCMODE);
|
2006-07-17 19:30:30 +00:00
|
|
|
bool inh;
|
2016-01-11 12:35:41 +01:00
|
|
|
bool got_one = false;
|
2005-02-06 11:15:29 +00:00
|
|
|
|
2019-02-05 15:32:08 +01:00
|
|
|
if (sscanf (get_name (), "/proc/%d/fd/pipe:[%llu]",
|
|
|
|
&pid, (long long *) &uniq_id) < 2)
|
2019-01-05 21:42:33 +01:00
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-06 11:15:29 +00:00
|
|
|
if (pid == myself->pid)
|
|
|
|
{
|
2005-10-24 15:09:07 +00:00
|
|
|
cygheap_fdenum cfd (true);
|
2005-02-06 11:15:29 +00:00
|
|
|
while (cfd.next () >= 0)
|
|
|
|
{
|
2016-01-11 12:35:41 +01:00
|
|
|
/* Windows doesn't allow to copy a pipe HANDLE with another access
|
|
|
|
mode. So we check for read and write side of pipe and try to
|
|
|
|
find the one matching the requested access mode. */
|
|
|
|
if (cfd->get_unique_id () == uniq_id)
|
|
|
|
got_one = true;
|
|
|
|
else if (cfd->get_unique_id () == uniq_id + 1)
|
|
|
|
got_one = true;
|
|
|
|
else
|
2005-02-06 11:15:29 +00:00
|
|
|
continue;
|
|
|
|
if ((rwflags == O_RDONLY && !(cfd->get_access () & GENERIC_READ))
|
|
|
|
|| (rwflags == O_WRONLY && !(cfd->get_access () & GENERIC_WRITE)))
|
2016-01-11 12:35:41 +01:00
|
|
|
continue;
|
2021-02-10 10:42:05 +01:00
|
|
|
copy_from (cfd);
|
2019-03-30 16:12:02 +09:00
|
|
|
set_handle (NULL);
|
2021-02-10 10:37:13 +01:00
|
|
|
pc.close_conv_handle ();
|
2011-10-15 22:37:30 +00:00
|
|
|
if (!cfd->dup (this, flags))
|
2005-02-06 11:15:29 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
2005-02-01 16:43:29 +00:00
|
|
|
}
|
2016-01-11 12:35:41 +01:00
|
|
|
/* Found the pipe but access mode didn't match? EACCES.
|
|
|
|
Otherwise ENOENT */
|
|
|
|
set_errno (got_one ? EACCES : ENOENT);
|
2005-02-06 11:15:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
pinfo p (pid);
|
|
|
|
if (!p)
|
|
|
|
{
|
|
|
|
set_errno (ESRCH);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!(proc = OpenProcess (PROCESS_DUP_HANDLE, false, p->dwProcessId)))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-11 12:35:41 +01:00
|
|
|
fhr = p->pipe_fhandler (uniq_id, size);
|
|
|
|
if (fhr && rwflags == O_RDONLY)
|
|
|
|
fh = fhr;
|
|
|
|
else
|
2005-02-06 11:15:29 +00:00
|
|
|
{
|
2016-01-11 12:35:41 +01:00
|
|
|
fhw = p->pipe_fhandler (uniq_id + 1, size);
|
|
|
|
if (fhw && rwflags == O_WRONLY)
|
|
|
|
fh = fhw;
|
2005-02-06 11:15:29 +00:00
|
|
|
}
|
2016-01-11 12:35:41 +01:00
|
|
|
if (!fh)
|
2005-02-06 11:15:29 +00:00
|
|
|
{
|
2016-01-11 12:35:41 +01:00
|
|
|
/* Too bad, but Windows only allows the same access mode when dup'ing
|
|
|
|
the pipe. */
|
|
|
|
set_errno (fhr || fhw ? EACCES : ENOENT);
|
2005-02-06 11:15:29 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2010-01-14 18:46:02 +00:00
|
|
|
inh = !(flags & O_CLOEXEC);
|
2016-01-11 12:35:41 +01:00
|
|
|
if (!DuplicateHandle (proc, fh->get_handle (), GetCurrentProcess (),
|
|
|
|
&nio_hdl, 0, inh, DUPLICATE_SAME_ACCESS))
|
2005-02-06 11:15:29 +00:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
goto out;
|
|
|
|
}
|
2016-01-11 19:10:45 +01:00
|
|
|
init (nio_hdl, fh->get_access (), mode & O_TEXT ?: O_BINARY,
|
|
|
|
fh->get_plain_ino ());
|
2005-09-17 20:36:34 +00:00
|
|
|
cfree (fh);
|
2005-02-06 11:15:29 +00:00
|
|
|
CloseHandle (proc);
|
|
|
|
return 1;
|
2005-02-01 16:43:29 +00:00
|
|
|
out:
|
2005-02-06 11:15:29 +00:00
|
|
|
if (nio_hdl)
|
|
|
|
CloseHandle (nio_hdl);
|
|
|
|
if (fh)
|
|
|
|
free (fh);
|
|
|
|
if (proc)
|
|
|
|
CloseHandle (proc);
|
2005-02-01 15:11:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
extern "C" int swscanf (const wchar_t *, const wchar_t *, ...);
|
|
|
|
|
|
|
|
static char *
|
|
|
|
get_mtx_name (HANDLE h, const char *io, char *name)
|
|
|
|
{
|
|
|
|
ULONG len;
|
|
|
|
NTSTATUS status;
|
|
|
|
tmp_pathbuf tp;
|
|
|
|
OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION *) tp.w_get ();
|
|
|
|
DWORD pid;
|
|
|
|
LONG uniq_id;
|
|
|
|
|
|
|
|
status = NtQueryObject (h, ObjectNameInformation, ntfn, 65536, &len);
|
|
|
|
if (!NT_SUCCESS (status) || !ntfn->Name.Buffer)
|
|
|
|
return NULL;
|
|
|
|
ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = L'\0';
|
|
|
|
WCHAR *p = wcschr (ntfn->Name.Buffer, L'-');
|
|
|
|
if (p == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (2 != swscanf (p, L"-%u-pipe-nt-0x%x", &pid, &uniq_id))
|
|
|
|
return NULL;
|
|
|
|
__small_sprintf (name, "cygpipe.%s.mutex.%u-%p",
|
|
|
|
io, pid, (intptr_t) uniq_id);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2021-09-18 08:13:55 -04:00
|
|
|
bool
|
2021-09-12 15:06:05 +09:00
|
|
|
fhandler_pipe::open_setup (int flags)
|
|
|
|
{
|
2021-09-18 08:13:55 -04:00
|
|
|
if (!fhandler_base::open_setup (flags))
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
return false;
|
|
|
|
if (!pipe_mtx)
|
2021-09-16 23:21:57 +09:00
|
|
|
{
|
|
|
|
SECURITY_ATTRIBUTES *sa = sec_none_cloexec (flags);
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
char name[MAX_PATH];
|
|
|
|
const char *io = get_device () == FH_PIPER ? "input" : "output";
|
|
|
|
char *mtx_name = get_mtx_name (get_handle (), io, name);
|
|
|
|
pipe_mtx = CreateMutex (sa, FALSE, mtx_name);
|
|
|
|
if (!pipe_mtx)
|
2021-09-18 08:34:04 -04:00
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
debug_printf ("CreateMutex pipe_mtx failed: %E");
|
|
|
|
return false;
|
2021-09-18 08:34:04 -04:00
|
|
|
}
|
2021-09-12 15:06:05 +09:00
|
|
|
}
|
2021-09-18 08:13:55 -04:00
|
|
|
return true;
|
2021-09-12 15:06:05 +09:00
|
|
|
}
|
|
|
|
|
2013-04-23 09:44:36 +00:00
|
|
|
off_t
|
|
|
|
fhandler_pipe::lseek (off_t offset, int whence)
|
2001-09-22 16:55:02 +00:00
|
|
|
{
|
2013-04-23 09:44:36 +00:00
|
|
|
debug_printf ("(%D, %d)", offset, whence);
|
2001-09-22 16:55:02 +00:00
|
|
|
set_errno (ESPIPE);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-08-07 19:29:14 +00:00
|
|
|
int
|
2013-04-23 09:44:36 +00:00
|
|
|
fhandler_pipe::fadvise (off_t offset, off_t length, int advice)
|
2006-08-07 19:29:14 +00:00
|
|
|
{
|
2017-11-02 16:45:34 +01:00
|
|
|
return ESPIPE;
|
2006-08-07 19:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2023-11-26 12:50:38 +01:00
|
|
|
fhandler_pipe::fallocate (int mode, off_t offset, off_t length)
|
2006-08-07 19:29:14 +00:00
|
|
|
{
|
2023-11-26 12:50:38 +01:00
|
|
|
return (mode & __FALLOC_FL_TRUNCATE) ? EINVAL : ESPIPE;
|
2006-08-07 19:29:14 +00:00
|
|
|
}
|
|
|
|
|
2006-05-21 05:25:49 +00:00
|
|
|
char *
|
|
|
|
fhandler_pipe::get_proc_fd_name (char *buf)
|
2005-01-31 10:28:55 +00:00
|
|
|
{
|
2019-01-13 22:30:33 +01:00
|
|
|
__small_sprintf (buf, "pipe:[%U]", get_plain_ino ());
|
2005-01-31 10:28:55 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2021-09-14 18:50:49 +09:00
|
|
|
void
|
|
|
|
fhandler_pipe::release_select_sem (const char *from)
|
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
LONG n_release = get_obj_handle_count (select_sem)
|
2021-11-10 21:47:09 +09:00
|
|
|
- get_obj_handle_count (get_handle ());
|
2021-09-14 18:50:49 +09:00
|
|
|
debug_printf("%s(%s) release %d", from,
|
|
|
|
get_dev () == FH_PIPER ? "PIPER" : "PIPEW", n_release);
|
|
|
|
if (n_release)
|
|
|
|
ReleaseSemaphore (select_sem, n_release, NULL);
|
|
|
|
}
|
|
|
|
|
2022-05-23 15:52:52 -04:00
|
|
|
void
|
2019-05-25 13:03:08 -04:00
|
|
|
fhandler_pipe::raw_read (void *ptr, size_t& len)
|
|
|
|
{
|
2021-09-06 20:12:16 +09:00
|
|
|
size_t nbytes = 0;
|
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
2019-05-25 13:03:08 -04:00
|
|
|
IO_STATUS_BLOCK io;
|
2022-12-08 22:02:43 +09:00
|
|
|
ULONGLONG t0 = GetTickCount64 (); /* Init timer */
|
|
|
|
const ULONGLONG t0_threshold = 20;
|
2019-05-25 13:03:08 -04:00
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return;
|
|
|
|
|
2021-09-06 20:12:16 +09:00
|
|
|
DWORD timeout = is_nonblocking () ? 0 : INFINITE;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
DWORD waitret = cygwait (pipe_mtx, timeout);
|
2021-09-06 20:12:16 +09:00
|
|
|
switch (waitret)
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
len = (size_t) -1;
|
|
|
|
return;
|
2021-11-10 12:24:53 +09:00
|
|
|
case WAIT_SIGNALED:
|
2021-09-06 20:12:16 +09:00
|
|
|
set_errno (EINTR);
|
|
|
|
len = (size_t) -1;
|
|
|
|
return;
|
2021-11-10 12:24:53 +09:00
|
|
|
case WAIT_CANCELED:
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/* NOTREACHED */
|
|
|
|
default:
|
|
|
|
/* Should not reach here. */
|
|
|
|
__seterrno ();
|
|
|
|
len = (size_t) -1;
|
|
|
|
return;
|
2021-09-06 20:12:16 +09:00
|
|
|
}
|
|
|
|
while (nbytes < len)
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
2021-09-06 20:12:16 +09:00
|
|
|
ULONG_PTR nbytes_now = 0;
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
ULONG len1 = (ULONG) (len - nbytes);
|
2022-12-08 22:02:43 +09:00
|
|
|
DWORD select_sem_timeout = 0;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
bool real_non_blocking_mode = false;
|
2021-09-06 20:12:16 +09:00
|
|
|
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
FILE_PIPE_LOCAL_INFORMATION fpli;
|
|
|
|
status = NtQueryInformationFile (get_handle (), &io,
|
|
|
|
&fpli, sizeof (fpli),
|
|
|
|
FilePipeLocalInformation);
|
|
|
|
if (NT_SUCCESS (status))
|
2021-09-03 10:48:40 +02:00
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (fpli.ReadDataAvailable == 0)
|
|
|
|
{
|
|
|
|
if (fpli.NamedPipeState == FILE_PIPE_CLOSING_STATE)
|
|
|
|
/* EOF */
|
|
|
|
break;
|
|
|
|
if (nbytes != 0)
|
|
|
|
break;
|
|
|
|
if (is_nonblocking ())
|
|
|
|
{
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
nbytes = (size_t) -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* If the pipe is a non-cygwin pipe, select_sem trick
|
|
|
|
does not work. As a result, the following cygwait()
|
|
|
|
will return only after timeout occurs. This causes
|
|
|
|
performance degradation. However, setting timeout
|
|
|
|
to zero causes high CPU load. So, set timeout to
|
|
|
|
non-zero only when select_sem is valid or pipe is
|
|
|
|
not ready to read for more than t0_threshold.
|
|
|
|
This prevents both the performance degradation and
|
|
|
|
the high CPU load. */
|
|
|
|
if (select_sem || GetTickCount64 () - t0 > t0_threshold)
|
|
|
|
select_sem_timeout = 1;
|
|
|
|
waitret = cygwait (select_sem, select_sem_timeout);
|
|
|
|
if (waitret == WAIT_CANCELED)
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
else if (waitret == WAIT_SIGNALED)
|
|
|
|
{
|
|
|
|
set_errno (EINTR);
|
|
|
|
nbytes = (size_t) -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2021-09-03 10:48:40 +02:00
|
|
|
}
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
else if (nbytes != 0)
|
|
|
|
break;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
else if (status == STATUS_END_OF_FILE || status == STATUS_PIPE_BROKEN)
|
|
|
|
/* EOF */
|
|
|
|
break;
|
|
|
|
else if (!NT_SUCCESS (status) && is_nonblocking ())
|
|
|
|
{
|
|
|
|
status = set_pipe_non_blocking (true);
|
|
|
|
if (status == STATUS_END_OF_FILE || status == STATUS_PIPE_BROKEN)
|
|
|
|
/* EOF */
|
|
|
|
break;
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
/* Cannot continue. What should we do? */
|
|
|
|
set_errno (EIO);
|
|
|
|
nbytes = (size_t) -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
real_non_blocking_mode = true;
|
|
|
|
}
|
2021-11-10 21:47:09 +09:00
|
|
|
status = NtReadFile (get_handle (), NULL, NULL, NULL, &io, ptr,
|
2021-09-06 20:12:16 +09:00
|
|
|
len1, NULL, NULL);
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (real_non_blocking_mode)
|
|
|
|
set_pipe_non_blocking (false);
|
2021-09-07 11:14:09 +02:00
|
|
|
if (isclosed ()) /* A signal handler might have closed the fd. */
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
2021-11-10 21:47:09 +09:00
|
|
|
set_errno (EBADF);
|
2021-09-06 20:12:16 +09:00
|
|
|
nbytes = (size_t) -1;
|
2019-05-25 13:03:08 -04:00
|
|
|
}
|
2021-11-11 10:22:41 -05:00
|
|
|
else if (NT_SUCCESS (status) || status == STATUS_BUFFER_OVERFLOW)
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
2021-09-06 20:12:16 +09:00
|
|
|
nbytes_now = io.Information;
|
|
|
|
ptr = ((char *) ptr) + nbytes_now;
|
|
|
|
nbytes += nbytes_now;
|
2021-09-09 11:35:54 +02:00
|
|
|
if (select_sem && nbytes_now > 0)
|
2021-09-14 18:50:49 +09:00
|
|
|
release_select_sem ("raw_read");
|
2019-05-25 13:03:08 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Some errors are not really errors. Detect such cases here. */
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_END_OF_FILE:
|
|
|
|
case STATUS_PIPE_BROKEN:
|
|
|
|
/* This is really EOF. */
|
|
|
|
break;
|
|
|
|
case STATUS_PIPE_LISTENING:
|
|
|
|
case STATUS_PIPE_EMPTY:
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
/* Only for real_non_blocking_mode */
|
2021-09-06 20:12:16 +09:00
|
|
|
if (nbytes != 0)
|
|
|
|
break;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
set_errno (EAGAIN);
|
|
|
|
nbytes = (size_t) -1;
|
|
|
|
break;
|
2019-05-25 13:03:08 -04:00
|
|
|
default:
|
|
|
|
__seterrno_from_nt_status (status);
|
2021-09-06 20:12:16 +09:00
|
|
|
nbytes = (size_t) -1;
|
2019-05-25 13:03:08 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 20:12:16 +09:00
|
|
|
|
2021-11-07 12:47:18 +09:00
|
|
|
if ((nbytes_now == 0 && !NT_SUCCESS (status))
|
|
|
|
|| status == STATUS_BUFFER_OVERFLOW)
|
2021-09-06 20:12:16 +09:00
|
|
|
break;
|
|
|
|
}
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
ReleaseMutex (pipe_mtx);
|
2021-09-06 20:12:16 +09:00
|
|
|
len = nbytes;
|
2019-05-25 13:03:08 -04:00
|
|
|
}
|
|
|
|
|
2022-05-23 15:52:52 -04:00
|
|
|
ssize_t
|
2021-09-03 19:41:52 -04:00
|
|
|
fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
|
|
|
size_t nbytes = 0;
|
|
|
|
ULONG chunk;
|
|
|
|
NTSTATUS status = STATUS_SUCCESS;
|
|
|
|
IO_STATUS_BLOCK io;
|
2021-11-17 08:13:17 +09:00
|
|
|
HANDLE evt;
|
2024-08-18 22:07:16 +02:00
|
|
|
bool short_write_once = false;
|
2019-05-25 13:03:08 -04:00
|
|
|
|
|
|
|
if (!len)
|
|
|
|
return 0;
|
|
|
|
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
ssize_t avail = pipe_buf_size;
|
|
|
|
bool real_non_blocking_mode = false;
|
|
|
|
|
|
|
|
if (pipe_mtx) /* pipe_mtx is NULL in the fifo case */
|
|
|
|
{
|
|
|
|
DWORD timeout = is_nonblocking () ? 0 : INFINITE;
|
|
|
|
DWORD waitret = cygwait (pipe_mtx, timeout);
|
|
|
|
switch (waitret)
|
|
|
|
{
|
|
|
|
case WAIT_OBJECT_0:
|
|
|
|
break;
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
return -1;
|
|
|
|
case WAIT_SIGNALED:
|
|
|
|
set_errno (EINTR);
|
|
|
|
return -1;
|
|
|
|
case WAIT_CANCELED:
|
|
|
|
pthread::static_cancel_self ();
|
|
|
|
/* NOTREACHED */
|
|
|
|
default:
|
|
|
|
/* Should not reach here. */
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (get_device () == FH_PIPEW && is_nonblocking ())
|
2021-09-14 20:59:08 +09:00
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
fhandler_pipe *fh = (fhandler_pipe *) this;
|
|
|
|
FILE_PIPE_LOCAL_INFORMATION fpli;
|
|
|
|
status = NtQueryInformationFile (get_handle (), &io, &fpli, sizeof fpli,
|
|
|
|
FilePipeLocalInformation);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
if (fpli.WriteQuotaAvailable != 0)
|
|
|
|
avail = fpli.WriteQuotaAvailable;
|
|
|
|
else /* WriteQuotaAvailable == 0 */
|
|
|
|
{ /* Refer to the comment in select.cc: pipe_data_available(). */
|
|
|
|
/* NtSetInformationFile() in set_pipe_non_blocking(true) seems
|
|
|
|
to fail with STATUS_PIPE_BUSY if the pipe is not empty.
|
|
|
|
In this case, the pipe is really full if WriteQuotaAvailable
|
|
|
|
is zero. Otherwise, the pipe is empty. */
|
|
|
|
status = fh->set_pipe_non_blocking (true);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
/* Pipe should be empty because reader is waiting for data. */
|
|
|
|
/* Restore the pipe mode to blocking. */
|
|
|
|
fh->set_pipe_non_blocking (false);
|
|
|
|
else if (status == STATUS_PIPE_BUSY)
|
|
|
|
{
|
|
|
|
/* Full */
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* The pipe space is unknown. */
|
|
|
|
status = fh->set_pipe_non_blocking (true);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
real_non_blocking_mode = true;
|
|
|
|
else if (status == STATUS_PIPE_BUSY)
|
|
|
|
{
|
|
|
|
/* The pipe is not empty and may be full.
|
|
|
|
It is not safe to write now. */
|
|
|
|
set_errno (EAGAIN);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (STATUS_PIPE_IS_CLOSED (status))
|
|
|
|
{
|
|
|
|
set_errno (EPIPE);
|
|
|
|
raise (SIGPIPE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
else if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
/* Cannot continue. What should we do? */
|
|
|
|
set_errno (EIO);
|
|
|
|
goto err;
|
|
|
|
}
|
2021-09-14 20:59:08 +09:00
|
|
|
}
|
|
|
|
|
2024-11-06 14:06:58 +09:00
|
|
|
if (len <= (size_t) avail)
|
2019-05-25 13:03:08 -04:00
|
|
|
chunk = len;
|
|
|
|
else
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
chunk = avail;
|
2019-05-25 13:03:08 -04:00
|
|
|
|
2021-11-17 08:13:17 +09:00
|
|
|
if (!(evt = CreateEvent (NULL, false, false, NULL)))
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
|
|
|
__seterrno ();
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
goto err;
|
2019-05-25 13:03:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write in chunks, accumulating a total. If there's an error, just
|
|
|
|
return the accumulated total unless the first write fails, in
|
|
|
|
which case return -1. */
|
|
|
|
while (nbytes < len)
|
|
|
|
{
|
|
|
|
ULONG_PTR nbytes_now = 0;
|
|
|
|
size_t left = len - nbytes;
|
|
|
|
ULONG len1;
|
|
|
|
DWORD waitret = WAIT_OBJECT_0;
|
|
|
|
|
2024-11-06 14:06:58 +09:00
|
|
|
if (left > chunk && !is_nonblocking ())
|
2019-05-25 13:03:08 -04:00
|
|
|
len1 = chunk;
|
|
|
|
else
|
|
|
|
len1 = (ULONG) left;
|
2024-08-18 22:07:16 +02:00
|
|
|
|
2021-09-03 10:45:49 +02:00
|
|
|
/* NtWriteFile returns success with # of bytes written == 0 if writing
|
|
|
|
on a non-blocking pipe fails because the pipe buffer doesn't have
|
|
|
|
sufficient space.
|
|
|
|
|
|
|
|
POSIX requires
|
|
|
|
- A write request for {PIPE_BUF} or fewer bytes shall have the
|
|
|
|
following effect: if there is sufficient space available in the
|
|
|
|
pipe, write() shall transfer all the data and return the number
|
|
|
|
of bytes requested. Otherwise, write() shall transfer no data and
|
|
|
|
return -1 with errno set to [EAGAIN].
|
|
|
|
|
|
|
|
- A write request for more than {PIPE_BUF} bytes shall cause one
|
|
|
|
of the following:
|
|
|
|
|
|
|
|
- When at least one byte can be written, transfer what it can and
|
|
|
|
return the number of bytes written. When all data previously
|
|
|
|
written to the pipe is read, it shall transfer at least {PIPE_BUF}
|
|
|
|
bytes.
|
|
|
|
|
|
|
|
- When no data can be written, transfer no data, and return -1 with
|
|
|
|
errno set to [EAGAIN]. */
|
|
|
|
while (len1 > 0)
|
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (is_nonblocking() && !real_non_blocking_mode && len1 > avail)
|
|
|
|
/* Avoid being blocked in NtWriteFile() */
|
|
|
|
io.Information = 0;
|
|
|
|
else
|
|
|
|
status = NtWriteFile (get_handle (), evt, NULL, NULL, &io,
|
|
|
|
(PVOID) ptr, len1, NULL, NULL);
|
2021-11-17 08:13:17 +09:00
|
|
|
if (status == STATUS_PENDING)
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
{
|
2024-08-18 21:40:51 +02:00
|
|
|
do
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
waitret = cygwait (evt, (DWORD) 0);
|
2024-08-18 21:40:51 +02:00
|
|
|
/* Break out if no SA_RESTART. */
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (waitret == WAIT_SIGNALED)
|
2024-08-18 21:40:51 +02:00
|
|
|
break;
|
2024-09-01 04:31:03 +09:00
|
|
|
/* Break out on completion */
|
|
|
|
if (waitret == WAIT_OBJECT_0)
|
|
|
|
break;
|
2024-08-18 22:07:16 +02:00
|
|
|
/* If we got a timeout in the blocking case, and we already
|
|
|
|
did a short write, we got a signal in the previous loop. */
|
|
|
|
if (waitret == WAIT_TIMEOUT && short_write_once)
|
|
|
|
{
|
|
|
|
waitret = WAIT_SIGNALED;
|
|
|
|
break;
|
|
|
|
}
|
2024-09-01 04:31:03 +09:00
|
|
|
cygwait (select_sem, 10, cw_cancel);
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
}
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
while (waitret == WAIT_TIMEOUT);
|
2021-11-17 08:13:17 +09:00
|
|
|
/* If io.Status is STATUS_CANCELLED after CancelIo, IO has
|
|
|
|
actually been cancelled and io.Information contains the
|
|
|
|
number of bytes processed so far.
|
|
|
|
Otherwise IO has been finished regulary and io.Status
|
|
|
|
contains valid success or error information. */
|
|
|
|
CancelIo (get_handle ());
|
|
|
|
if (waitret == WAIT_SIGNALED && io.Status != STATUS_CANCELLED)
|
|
|
|
waitret = WAIT_OBJECT_0;
|
|
|
|
|
|
|
|
if (waitret == WAIT_CANCELED)
|
|
|
|
status = STATUS_THREAD_CANCELED;
|
|
|
|
else if (waitret == WAIT_SIGNALED)
|
|
|
|
status = STATUS_THREAD_SIGNALED;
|
2024-08-18 22:07:16 +02:00
|
|
|
else if (waitret == WAIT_TIMEOUT && io.Status == STATUS_CANCELLED)
|
|
|
|
status = STATUS_SUCCESS;
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
else
|
2021-11-17 08:13:17 +09:00
|
|
|
status = io.Status;
|
Cygwin: pipe: Use read pipe handle for select() on write pipe.
- Usually WriteQuotaAvailable retrieved by NtQueryInformationFile()
on the write side reflects the space available in the inbound buffer
on the read side. However, if a pipe read is currently pending,
WriteQuotaAvailable on the write side is decremented by the number
of bytes the read side is requesting. So it's possible (even likely)
that WriteQuotaAvailable is 0, even if the inbound buffer on the
read side is not full. This can lead to a deadlock situation:
The reader is waiting for data, but select on the writer side
assumes that no space is available in the read side inbound buffer.
Currently, to avoid this stuation, read() does not request larger
block than pipe size - 1. However, this mechanism does not take
effect if the reader side is non-cygwin app.
The only reliable information is available on the read side, so
fetch info from the read side via the pipe-specific query handle
(query_hdl) introduced.
If the query_hdl (read handle) is kept in write side, writer can
not detect closure of read pipe. Therefore, raw_write() counts
write handle and query_hdl. If they are equal, only the pairs of
write handle and query_hdl are alive. In this case, raw_write()
returns EPIPE and raises SIGPIPE.
- Nonblocking pipes (PIPE_NOWAIT) are not well handled by non-Cygwin
tools, so convert pipe handles to PIPE_WAIT handles when spawning
a non-Cygwin process.
2021-09-14 19:10:48 +09:00
|
|
|
}
|
2024-08-18 22:07:16 +02:00
|
|
|
if (status != STATUS_THREAD_SIGNALED && !NT_SUCCESS (status))
|
2021-11-17 08:13:17 +09:00
|
|
|
break;
|
2024-08-18 22:07:16 +02:00
|
|
|
if (io.Information > 0 || len <= PIPE_BUF || short_write_once)
|
|
|
|
break;
|
|
|
|
/* Independent of being blocking or non-blocking, if we're here,
|
|
|
|
the pipe has less space than requested. If the pipe is a
|
|
|
|
non-Cygwin pipe, just try the old strategy of trying a half
|
|
|
|
write. If the pipe has at
|
|
|
|
least PIPE_BUF bytes available, try to write all matching
|
|
|
|
PIPE_BUF sized blocks. If it's less than PIPE_BUF, try
|
|
|
|
the next less power of 2 bytes. This is not really the Linux
|
|
|
|
strategy because Linux is filling the pages of a pipe buffer
|
|
|
|
in a very implementation-defined way we can't emulate, but it
|
|
|
|
resembles it closely enough to get useful results. */
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
avail = pipe_data_available (-1, this, get_handle (), PDA_WRITE);
|
2024-08-18 22:07:16 +02:00
|
|
|
if (avail < 1) /* error or pipe closed */
|
|
|
|
break;
|
2024-08-19 11:44:42 +02:00
|
|
|
if (avail > len1) /* somebody read from the pipe */
|
2024-08-18 22:07:16 +02:00
|
|
|
avail = len1;
|
|
|
|
if (avail == 1) /* 1 byte left or non-Cygwin pipe */
|
|
|
|
len1 >>= 1;
|
|
|
|
else if (avail >= PIPE_BUF)
|
|
|
|
len1 = avail & ~(PIPE_BUF - 1);
|
|
|
|
else
|
2024-10-31 11:52:26 +01:00
|
|
|
len1 = 1 << (63 - __builtin_clzl (avail));
|
2024-08-19 11:51:14 +02:00
|
|
|
short_write_once = true;
|
2021-09-07 10:50:47 +02:00
|
|
|
}
|
2021-09-07 11:14:09 +02:00
|
|
|
if (isclosed ()) /* A signal handler might have closed the fd. */
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
|
|
|
if (waitret == WAIT_OBJECT_0)
|
|
|
|
set_errno (EBADF);
|
|
|
|
else
|
|
|
|
__seterrno ();
|
|
|
|
}
|
2021-09-09 11:35:54 +02:00
|
|
|
else if (NT_SUCCESS (status)
|
|
|
|
|| status == STATUS_THREAD_CANCELED
|
|
|
|
|| status == STATUS_THREAD_SIGNALED)
|
2019-05-25 13:03:08 -04:00
|
|
|
{
|
|
|
|
nbytes_now = io.Information;
|
2021-09-03 10:24:15 +02:00
|
|
|
ptr = ((char *) ptr) + nbytes_now;
|
2019-05-25 13:03:08 -04:00
|
|
|
nbytes += nbytes_now;
|
2021-09-09 11:35:54 +02:00
|
|
|
if (select_sem && nbytes_now > 0)
|
2021-09-14 18:50:49 +09:00
|
|
|
release_select_sem ("raw_write");
|
2021-09-03 10:45:49 +02:00
|
|
|
/* 0 bytes returned? EAGAIN. See above. */
|
2021-09-09 11:35:54 +02:00
|
|
|
if (NT_SUCCESS (status) && nbytes == 0)
|
2021-09-03 10:45:49 +02:00
|
|
|
set_errno (EAGAIN);
|
2019-05-25 13:03:08 -04:00
|
|
|
}
|
|
|
|
else if (STATUS_PIPE_IS_CLOSED (status))
|
|
|
|
{
|
|
|
|
set_errno (EPIPE);
|
|
|
|
raise (SIGPIPE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
|
2024-08-18 22:07:16 +02:00
|
|
|
if (nbytes_now == 0 || short_write_once)
|
2021-09-03 10:45:49 +02:00
|
|
|
break;
|
2019-05-25 13:03:08 -04:00
|
|
|
}
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
|
|
|
|
if (real_non_blocking_mode)
|
|
|
|
((fhandler_pipe *) this)->set_pipe_non_blocking (false);
|
|
|
|
|
2021-11-17 08:13:17 +09:00
|
|
|
CloseHandle (evt);
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (pipe_mtx) /* pipe_mtx is NULL in the fifo case */
|
|
|
|
ReleaseMutex (pipe_mtx);
|
2021-09-03 10:45:49 +02:00
|
|
|
if (status == STATUS_THREAD_SIGNALED && nbytes == 0)
|
2019-05-25 13:03:08 -04:00
|
|
|
set_errno (EINTR);
|
|
|
|
else if (status == STATUS_THREAD_CANCELED)
|
2021-09-07 10:50:47 +02:00
|
|
|
pthread::static_cancel_self ();
|
2021-09-03 10:45:49 +02:00
|
|
|
return nbytes ?: -1;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
|
|
|
|
err:
|
|
|
|
if (pipe_mtx)
|
|
|
|
ReleaseMutex (pipe_mtx);
|
|
|
|
return -1;
|
2021-09-03 10:45:49 +02:00
|
|
|
}
|
2021-09-03 10:48:40 +02:00
|
|
|
|
2021-09-14 12:49:35 +09:00
|
|
|
void
|
|
|
|
fhandler_pipe::set_close_on_exec (bool val)
|
|
|
|
{
|
|
|
|
fhandler_base::set_close_on_exec (val);
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (pipe_mtx)
|
|
|
|
set_no_inheritance (pipe_mtx, val);
|
2021-09-14 12:49:35 +09:00
|
|
|
if (select_sem)
|
|
|
|
set_no_inheritance (select_sem, val);
|
|
|
|
}
|
|
|
|
|
2021-09-03 10:48:40 +02:00
|
|
|
void
|
|
|
|
fhandler_pipe::fixup_after_fork (HANDLE parent)
|
|
|
|
{
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (pipe_mtx)
|
|
|
|
fork_fixup (parent, pipe_mtx, "pipe_mtx");
|
2021-09-08 17:18:35 +09:00
|
|
|
if (select_sem)
|
|
|
|
fork_fixup (parent, select_sem, "select_sem");
|
2021-09-03 10:48:40 +02:00
|
|
|
fhandler_base::fixup_after_fork (parent);
|
2024-03-11 22:08:00 +09:00
|
|
|
}
|
|
|
|
|
2001-09-22 16:55:02 +00:00
|
|
|
int
|
2011-10-15 22:37:30 +00:00
|
|
|
fhandler_pipe::dup (fhandler_base *child, int flags)
|
2001-09-22 16:55:02 +00:00
|
|
|
{
|
2004-01-23 23:05:33 +00:00
|
|
|
fhandler_pipe *ftp = (fhandler_pipe *) child;
|
2006-12-11 18:55:29 +00:00
|
|
|
ftp->set_popen_pid (0);
|
2004-01-23 23:05:33 +00:00
|
|
|
|
2021-09-03 10:48:40 +02:00
|
|
|
int res = 0;
|
|
|
|
if (fhandler_base::dup (child, flags))
|
2007-07-07 17:00:33 +00:00
|
|
|
res = -1;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
else if (pipe_mtx &&
|
|
|
|
!DuplicateHandle (GetCurrentProcess (), pipe_mtx,
|
|
|
|
GetCurrentProcess (), &ftp->pipe_mtx,
|
2021-09-03 10:48:40 +02:00
|
|
|
0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
ftp->close ();
|
|
|
|
res = -1;
|
|
|
|
}
|
2021-09-08 17:18:35 +09:00
|
|
|
else if (select_sem &&
|
|
|
|
!DuplicateHandle (GetCurrentProcess (), select_sem,
|
2021-09-16 23:21:57 +09:00
|
|
|
GetCurrentProcess (), &ftp->select_sem,
|
|
|
|
0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
|
2021-09-08 17:18:35 +09:00
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
ftp->close ();
|
|
|
|
res = -1;
|
|
|
|
}
|
2001-09-24 21:50:44 +00:00
|
|
|
|
2004-01-23 23:05:33 +00:00
|
|
|
debug_printf ("res %d", res);
|
|
|
|
return res;
|
2001-09-22 16:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-03 10:48:40 +02:00
|
|
|
int
|
|
|
|
fhandler_pipe::close ()
|
|
|
|
{
|
2021-09-08 17:18:35 +09:00
|
|
|
if (select_sem)
|
|
|
|
{
|
2021-09-14 18:50:49 +09:00
|
|
|
release_select_sem ("close");
|
2021-09-08 17:18:35 +09:00
|
|
|
CloseHandle (select_sem);
|
|
|
|
}
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
if (pipe_mtx)
|
|
|
|
CloseHandle (pipe_mtx);
|
2021-09-16 23:21:57 +09:00
|
|
|
int ret = fhandler_base::close ();
|
|
|
|
return ret;
|
2021-09-03 10:48:40 +02:00
|
|
|
}
|
|
|
|
|
2008-03-22 21:04:16 +00:00
|
|
|
#define PIPE_INTRO "\\\\.\\pipe\\cygwin-"
|
|
|
|
|
2004-09-03 01:32:02 +00:00
|
|
|
/* Create a pipe, and return handles to the read and write ends,
|
|
|
|
just like CreatePipe, but ensure that the write end permits
|
|
|
|
FILE_READ_ATTRIBUTES access, on later versions of win32 where
|
|
|
|
this is supported. This access is needed by NtQueryInformationFile,
|
|
|
|
which is used to implement select and nonblocking writes.
|
2005-04-22 13:58:09 +00:00
|
|
|
Note that the return value is either 0 or GetLastError,
|
2004-09-03 01:32:02 +00:00
|
|
|
unlike CreatePipe, which returns a bool for success or failure. */
|
2011-11-23 18:56:57 +00:00
|
|
|
DWORD
|
|
|
|
fhandler_pipe::create (LPSECURITY_ATTRIBUTES sa_ptr, PHANDLE r, PHANDLE w,
|
2016-01-11 12:35:41 +01:00
|
|
|
DWORD psize, const char *name, DWORD open_mode,
|
|
|
|
int64_t *unique_id)
|
2004-09-03 01:32:02 +00:00
|
|
|
{
|
|
|
|
/* Default to error. */
|
2011-10-30 04:50:36 +00:00
|
|
|
if (r)
|
|
|
|
*r = NULL;
|
|
|
|
if (w)
|
|
|
|
*w = NULL;
|
2004-09-03 01:32:02 +00:00
|
|
|
|
|
|
|
/* Ensure that there is enough pipe buffer space for atomic writes. */
|
2011-11-23 18:56:57 +00:00
|
|
|
if (!psize)
|
2011-03-09 16:47:44 +00:00
|
|
|
psize = DEFAULT_PIPEBUFSIZE;
|
2004-09-03 01:32:02 +00:00
|
|
|
|
2009-10-31 13:24:06 +00:00
|
|
|
char pipename[MAX_PATH];
|
2012-04-30 15:38:45 +00:00
|
|
|
size_t len = __small_sprintf (pipename, PIPE_INTRO "%S-",
|
|
|
|
&cygheap->installation_key);
|
2015-12-15 16:02:35 +01:00
|
|
|
DWORD pipe_mode = PIPE_READMODE_BYTE | PIPE_REJECT_REMOTE_CLIENTS;
|
2012-04-28 19:49:58 +00:00
|
|
|
if (!name)
|
|
|
|
pipe_mode |= pipe_byte ? PIPE_TYPE_BYTE : PIPE_TYPE_MESSAGE;
|
|
|
|
else
|
2012-05-12 19:17:17 +00:00
|
|
|
pipe_mode |= PIPE_TYPE_MESSAGE;
|
2009-10-31 13:24:06 +00:00
|
|
|
|
2012-05-12 19:17:17 +00:00
|
|
|
if (!name || (open_mode & PIPE_ADD_PID))
|
2012-04-30 15:38:45 +00:00
|
|
|
{
|
|
|
|
len += __small_sprintf (pipename + len, "%u-", GetCurrentProcessId ());
|
|
|
|
open_mode &= ~PIPE_ADD_PID;
|
|
|
|
}
|
|
|
|
|
2012-05-12 19:17:17 +00:00
|
|
|
if (name)
|
|
|
|
len += __small_sprintf (pipename + len, "%s", name);
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
open_mode |= PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE;
|
2004-09-03 01:32:02 +00:00
|
|
|
|
|
|
|
/* Retry CreateNamedPipe as long as the pipe name is in use.
|
|
|
|
Retrying will probably never be necessary, but we want
|
|
|
|
to be as robust as possible. */
|
2011-10-30 04:50:36 +00:00
|
|
|
DWORD err = 0;
|
2021-08-30 09:53:06 -04:00
|
|
|
while (r && !*r)
|
2004-09-03 01:32:02 +00:00
|
|
|
{
|
2005-04-22 13:58:09 +00:00
|
|
|
static volatile ULONG pipe_unique_id;
|
2008-03-22 21:04:16 +00:00
|
|
|
if (!name)
|
2016-01-11 12:35:41 +01:00
|
|
|
{
|
|
|
|
LONG id = InterlockedIncrement ((LONG *) &pipe_unique_id);
|
|
|
|
__small_sprintf (pipename + len, "pipe-%p", id);
|
|
|
|
if (unique_id)
|
|
|
|
*unique_id = ((int64_t) id << 32 | GetCurrentProcessId ());
|
|
|
|
}
|
2004-09-03 01:32:02 +00:00
|
|
|
|
2013-04-23 09:44:36 +00:00
|
|
|
debug_printf ("name %s, size %u, mode %s", pipename, psize,
|
2012-04-28 19:49:58 +00:00
|
|
|
(pipe_mode & PIPE_TYPE_MESSAGE)
|
|
|
|
? "PIPE_TYPE_MESSAGE" : "PIPE_TYPE_BYTE");
|
2004-09-03 01:32:02 +00:00
|
|
|
|
|
|
|
/* Use CreateNamedPipe instead of CreatePipe, because the latter
|
2004-09-12 03:47:57 +00:00
|
|
|
returns a write handle that does not permit FILE_READ_ATTRIBUTES
|
|
|
|
access, on versions of win32 earlier than WinXP SP2.
|
|
|
|
CreatePipe also stupidly creates a full duplex pipe, which is
|
|
|
|
a waste, since only a single direction is actually used.
|
|
|
|
It's important to only allow a single instance, to ensure that
|
|
|
|
the pipe was not created earlier by some other process, even if
|
2013-04-23 09:44:36 +00:00
|
|
|
the pid has been reused.
|
2011-10-23 19:01:47 +00:00
|
|
|
|
|
|
|
Note that the write side of the pipe is opened as PIPE_TYPE_MESSAGE.
|
|
|
|
This *seems* to more closely mimic Linux pipe behavior and is
|
|
|
|
definitely required for pty handling since fhandler_pty_master
|
|
|
|
writes to the pipe in chunks, terminated by newline when CANON mode
|
|
|
|
is specified. */
|
2021-08-30 09:53:06 -04:00
|
|
|
*r = CreateNamedPipe (pipename, open_mode, pipe_mode, 1, psize,
|
2005-04-22 13:58:09 +00:00
|
|
|
psize, NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
|
2004-09-03 01:32:02 +00:00
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
if (*r != INVALID_HANDLE_VALUE)
|
2004-09-12 03:47:57 +00:00
|
|
|
{
|
2021-08-30 09:53:06 -04:00
|
|
|
debug_printf ("pipe read handle %p", *r);
|
2011-10-30 04:50:36 +00:00
|
|
|
err = 0;
|
2004-09-12 03:47:57 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-09-03 01:32:02 +00:00
|
|
|
|
2008-03-22 21:04:16 +00:00
|
|
|
err = GetLastError ();
|
2004-09-03 01:32:02 +00:00
|
|
|
switch (err)
|
2004-09-12 03:47:57 +00:00
|
|
|
{
|
|
|
|
case ERROR_PIPE_BUSY:
|
|
|
|
/* The pipe is already open with compatible parameters.
|
|
|
|
Pick a new name and retry. */
|
2011-10-30 04:50:36 +00:00
|
|
|
debug_printf ("pipe busy", !name ? ", retrying" : "");
|
2011-11-23 21:58:43 +00:00
|
|
|
if (!name)
|
2021-08-30 09:53:06 -04:00
|
|
|
*r = NULL;
|
2005-04-22 13:58:09 +00:00
|
|
|
break;
|
2004-09-12 03:47:57 +00:00
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
/* The pipe is already open with incompatible parameters.
|
|
|
|
Pick a new name and retry. */
|
2011-10-30 04:50:36 +00:00
|
|
|
debug_printf ("pipe access denied%s", !name ? ", retrying" : "");
|
2011-11-23 21:58:43 +00:00
|
|
|
if (!name)
|
2021-08-30 09:53:06 -04:00
|
|
|
*r = NULL;
|
2005-04-22 13:58:09 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-07-07 17:00:33 +00:00
|
|
|
{
|
|
|
|
err = GetLastError ();
|
2011-10-30 04:50:36 +00:00
|
|
|
debug_printf ("failed, %E");
|
2007-07-07 17:00:33 +00:00
|
|
|
}
|
2004-09-12 03:47:57 +00:00
|
|
|
}
|
2004-09-03 01:32:02 +00:00
|
|
|
}
|
2008-03-22 21:04:16 +00:00
|
|
|
|
|
|
|
if (err)
|
2004-09-03 01:32:02 +00:00
|
|
|
{
|
2021-08-30 09:53:06 -04:00
|
|
|
*r = NULL;
|
2004-09-03 01:32:02 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
if (!w)
|
|
|
|
debug_printf ("pipe write handle NULL");
|
2011-10-30 04:50:36 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
debug_printf ("CreateFile: name %s", pipename);
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
/* Open the named pipe for writing.
|
|
|
|
Be sure to permit FILE_READ_ATTRIBUTES access. */
|
|
|
|
DWORD access = GENERIC_WRITE | FILE_READ_ATTRIBUTES;
|
2011-10-30 04:50:36 +00:00
|
|
|
if ((open_mode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
|
2021-08-30 09:53:06 -04:00
|
|
|
access |= GENERIC_READ | FILE_WRITE_ATTRIBUTES;
|
|
|
|
*w = CreateFile (pipename, access, 0, sa_ptr, OPEN_EXISTING,
|
2011-10-30 04:50:36 +00:00
|
|
|
open_mode & FILE_FLAG_OVERLAPPED, 0);
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
if (!*w || *w == INVALID_HANDLE_VALUE)
|
2011-10-30 04:50:36 +00:00
|
|
|
{
|
|
|
|
/* Failure. */
|
|
|
|
DWORD err = GetLastError ();
|
2012-01-22 21:43:25 +00:00
|
|
|
debug_printf ("CreateFile failed, r %p, %E", r);
|
2021-08-30 09:53:06 -04:00
|
|
|
if (r)
|
|
|
|
CloseHandle (*r);
|
|
|
|
*w = NULL;
|
2011-10-30 04:50:36 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
debug_printf ("pipe write handle %p", *w);
|
2011-10-30 04:50:36 +00:00
|
|
|
}
|
2004-09-03 01:32:02 +00:00
|
|
|
|
|
|
|
/* Success. */
|
2005-04-22 13:58:09 +00:00
|
|
|
return 0;
|
2004-09-03 01:32:02 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 08:02:43 +09:00
|
|
|
inline static bool
|
|
|
|
is_running_as_service (void)
|
|
|
|
{
|
|
|
|
return check_token_membership (well_known_service_sid)
|
|
|
|
|| cygheap->user.saved_sid () == well_known_system_sid;
|
|
|
|
}
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
/* The next version of fhandler_pipe::create used to call the previous
|
|
|
|
version. But the read handle created by the latter doesn't have
|
|
|
|
FILE_WRITE_ATTRIBUTES access unless the pipe is created with
|
|
|
|
PIPE_ACCESS_DUPLEX, and it doesn't seem possible to add that access
|
|
|
|
right. This causes set_pipe_non_blocking to fail.
|
|
|
|
|
|
|
|
To fix this we will define a helper function 'nt_create' that is
|
|
|
|
similar to the above fhandler_pipe::create but uses
|
|
|
|
NtCreateNamedPipeFile instead of CreateNamedPipe; this gives more
|
|
|
|
flexibility in setting the access rights. We will use this helper
|
|
|
|
function in the version of fhandler_pipe::create below, which
|
|
|
|
suffices for all of our uses of set_pipe_non_blocking. For
|
|
|
|
simplicity, nt_create will omit the 'open_mode' and 'name'
|
|
|
|
parameters, which aren't needed for our purposes. */
|
|
|
|
|
2021-09-03 10:57:21 +02:00
|
|
|
static int nt_create (LPSECURITY_ATTRIBUTES, HANDLE &, HANDLE &, DWORD,
|
2021-08-30 09:53:06 -04:00
|
|
|
int64_t *);
|
|
|
|
|
2001-09-24 21:50:44 +00:00
|
|
|
int
|
2007-07-10 01:21:03 +00:00
|
|
|
fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode)
|
2000-02-17 19:38:33 +00:00
|
|
|
{
|
|
|
|
HANDLE r, w;
|
2010-01-14 18:46:02 +00:00
|
|
|
SECURITY_ATTRIBUTES *sa = sec_none_cloexec (mode);
|
2011-06-30 09:37:36 +00:00
|
|
|
int res = -1;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
int64_t unique_id = 0; /* Compiler complains if not initialized... */
|
2000-02-17 19:38:33 +00:00
|
|
|
|
2021-09-03 10:57:21 +02:00
|
|
|
int ret = nt_create (sa, r, w, psize, &unique_id);
|
2005-04-22 13:58:09 +00:00
|
|
|
if (ret)
|
2008-01-01 18:51:23 +00:00
|
|
|
{
|
2021-09-16 23:15:17 +09:00
|
|
|
__seterrno_from_win_error (ret);
|
|
|
|
goto out;
|
2000-02-17 19:38:33 +00:00
|
|
|
}
|
2021-09-16 23:15:17 +09:00
|
|
|
if ((fhs[0] = (fhandler_pipe *) build_fh_dev (*piper_dev)) == NULL)
|
|
|
|
goto err_close_rw_handle;
|
|
|
|
if ((fhs[1] = (fhandler_pipe *) build_fh_dev (*pipew_dev)) == NULL)
|
|
|
|
goto err_delete_fhs0;
|
|
|
|
mode |= mode & O_TEXT ?: O_BINARY;
|
|
|
|
fhs[0]->init (r, FILE_CREATE_PIPE_INSTANCE | GENERIC_READ, mode, unique_id);
|
|
|
|
fhs[1]->init (w, FILE_CREATE_PIPE_INSTANCE | GENERIC_WRITE, mode, unique_id);
|
|
|
|
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
char name[MAX_PATH], *mtx_name;
|
|
|
|
mtx_name = get_mtx_name (fhs[0]->get_handle (), "input", name);
|
|
|
|
fhs[0]->pipe_mtx = CreateMutex (sa, FALSE, mtx_name);
|
|
|
|
if (!fhs[0]->pipe_mtx)
|
2021-09-16 23:15:17 +09:00
|
|
|
goto err_delete_fhs1;
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
mtx_name = get_mtx_name (fhs[1]->get_handle (), "output", name);
|
|
|
|
fhs[1]->pipe_mtx = CreateMutex (sa, FALSE, mtx_name);
|
|
|
|
if (!fhs[1]->pipe_mtx)
|
|
|
|
goto err_close_pipe_mtx0;
|
2021-09-16 23:15:17 +09:00
|
|
|
|
|
|
|
fhs[0]->select_sem = CreateSemaphore (sa, 0, INT32_MAX, NULL);
|
|
|
|
if (!fhs[0]->select_sem)
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
goto err_close_pipe_mtx1;
|
2021-09-16 23:15:17 +09:00
|
|
|
if (!DuplicateHandle (GetCurrentProcess (), fhs[0]->select_sem,
|
|
|
|
GetCurrentProcess (), &fhs[1]->select_sem,
|
|
|
|
0, sa->bInheritHandle, DUPLICATE_SAME_ACCESS))
|
|
|
|
goto err_close_select_sem0;
|
|
|
|
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
err_close_select_sem0:
|
|
|
|
CloseHandle (fhs[0]->select_sem);
|
Cygwin: pipe: Switch pipe mode to blocking mode by default
Previously, cygwin read pipe used non-blocking mode although non-
cygwin app uses blocking-mode by default. Despite this requirement,
if a cygwin app is executed from a non-cygwin app and the cygwin
app exits, read pipe remains on non-blocking mode because of the
commit fc691d0246b9. Due to this behaviour, the non-cygwin app
cannot read the pipe correctly after that. Similarly, if a non-
cygwin app is executed from a cygwin app and the non-cygwin app
exits, the read pipe mode remains on blocking mode although cygwin
read pipe should be non-blocking mode.
These bugs were provoked by pipe mode toggling between cygwin and
non-cygwin apps. To make management of pipe mode simpler, this
patch has re-designed the pipe implementation. In this new
implementation, both read and write pipe basically use only blocking
mode and the behaviour corresponding to the pipe mode is simulated
in raw_read() and raw_write(). Only when NtQueryInformationFile
(FilePipeLocalInformation) fails for some reasons, the raw_read()/
raw_write() cannot simulate non-blocking access. Therefore, the pipe
mode is temporarily changed to non-blocking mode.
Moreover, because the fact that NtSetInformationFile() in
set_pipe_non_blocking(true) fails with STATUS_PIPE_BUSY if the pipe
is not empty has been found, query handle is not necessary anymore.
This allows the implementation much simpler than before.
Addresses: https://github.com/git-for-windows/git/issues/5115
Fixes: fc691d0246b9 ("Cygwin: pipe: Make sure to set read pipe non-blocking for cygwin apps.");
Reported-by: isaacag, Johannes Schindelin <Johannes.Schindelin@gmx.de>
Reviewed-by: Corinna Vinschen <corinna@vinschen.de>, Ken Brown <kbrown@cornell.edu>
Signed-off-by: Takashi Yano <takashi.yano@nifty.ne.jp>
2024-09-05 11:36:27 +09:00
|
|
|
err_close_pipe_mtx1:
|
|
|
|
CloseHandle (fhs[1]->pipe_mtx);
|
|
|
|
err_close_pipe_mtx0:
|
|
|
|
CloseHandle (fhs[0]->pipe_mtx);
|
2021-09-16 23:15:17 +09:00
|
|
|
err_delete_fhs1:
|
|
|
|
delete fhs[1];
|
|
|
|
err_delete_fhs0:
|
|
|
|
delete fhs[0];
|
|
|
|
err_close_rw_handle:
|
|
|
|
NtClose (r);
|
|
|
|
NtClose (w);
|
|
|
|
out:
|
|
|
|
debug_printf ("%R = pipe([%p, %p], %d, %y)",
|
|
|
|
res, fhs[0], fhs[1], psize, mode);
|
2000-10-07 18:12:11 +00:00
|
|
|
return res;
|
2000-02-17 19:38:33 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 09:53:06 -04:00
|
|
|
static int
|
2021-09-03 10:57:21 +02:00
|
|
|
nt_create (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE &r, HANDLE &w,
|
2021-08-30 09:53:06 -04:00
|
|
|
DWORD psize, int64_t *unique_id)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
HANDLE npfsh;
|
|
|
|
ACCESS_MASK access;
|
|
|
|
OBJECT_ATTRIBUTES attr;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
LARGE_INTEGER timeout;
|
|
|
|
|
|
|
|
/* Default to error. */
|
2021-09-03 10:57:21 +02:00
|
|
|
r = NULL;
|
|
|
|
w = NULL;
|
2021-08-30 09:53:06 -04:00
|
|
|
|
|
|
|
status = fhandler_base::npfs_handle (npfsh);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
return GetLastError ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that there is enough pipe buffer space for atomic writes. */
|
|
|
|
if (!psize)
|
|
|
|
psize = DEFAULT_PIPEBUFSIZE;
|
|
|
|
|
|
|
|
UNICODE_STRING pipename;
|
|
|
|
WCHAR pipename_buf[MAX_PATH];
|
|
|
|
size_t len = __small_swprintf (pipename_buf, L"%S-%u-",
|
|
|
|
&cygheap->installation_key,
|
|
|
|
GetCurrentProcessId ());
|
|
|
|
|
2021-09-03 10:32:30 +02:00
|
|
|
access = GENERIC_READ | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE;
|
2021-08-30 09:53:06 -04:00
|
|
|
|
|
|
|
ULONG pipe_type = pipe_byte ? FILE_PIPE_BYTE_STREAM_TYPE
|
|
|
|
: FILE_PIPE_MESSAGE_TYPE;
|
|
|
|
|
|
|
|
/* Retry NtCreateNamedPipeFile as long as the pipe name is in use.
|
|
|
|
Retrying will probably never be necessary, but we want
|
|
|
|
to be as robust as possible. */
|
|
|
|
DWORD err = 0;
|
2021-09-03 10:57:21 +02:00
|
|
|
while (!r)
|
2021-08-30 09:53:06 -04:00
|
|
|
{
|
|
|
|
static volatile ULONG pipe_unique_id;
|
|
|
|
LONG id = InterlockedIncrement ((LONG *) &pipe_unique_id);
|
|
|
|
__small_swprintf (pipename_buf + len, L"pipe-nt-%p", id);
|
|
|
|
if (unique_id)
|
|
|
|
*unique_id = ((int64_t) id << 32 | GetCurrentProcessId ());
|
|
|
|
|
|
|
|
debug_printf ("name %W, size %u, mode %s", pipename_buf, psize,
|
|
|
|
(pipe_type & FILE_PIPE_MESSAGE_TYPE)
|
|
|
|
? "PIPE_TYPE_MESSAGE" : "PIPE_TYPE_BYTE");
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&pipename, pipename_buf);
|
|
|
|
|
|
|
|
InitializeObjectAttributes (&attr, &pipename,
|
|
|
|
sa_ptr->bInheritHandle ? OBJ_INHERIT : 0,
|
|
|
|
npfsh, sa_ptr->lpSecurityDescriptor);
|
|
|
|
|
|
|
|
timeout.QuadPart = -500000;
|
2021-11-10 21:47:09 +09:00
|
|
|
/* Set FILE_SYNCHRONOUS_IO_NONALERT flag so that native
|
|
|
|
C# programs work with cygwin pipe. */
|
2021-09-03 10:57:21 +02:00
|
|
|
status = NtCreateNamedPipeFile (&r, access, &attr, &io,
|
2021-08-31 11:39:49 +02:00
|
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
2021-11-10 21:47:09 +09:00
|
|
|
FILE_CREATE,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT, pipe_type,
|
2021-08-31 11:39:52 +02:00
|
|
|
FILE_PIPE_BYTE_STREAM_MODE,
|
2021-08-30 09:53:06 -04:00
|
|
|
0, 1, psize, psize, &timeout);
|
|
|
|
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
{
|
2021-09-03 10:57:21 +02:00
|
|
|
debug_printf ("pipe read handle %p", r);
|
2021-08-30 09:53:06 -04:00
|
|
|
err = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case STATUS_PIPE_BUSY:
|
|
|
|
case STATUS_INSTANCE_NOT_AVAILABLE:
|
|
|
|
case STATUS_PIPE_NOT_AVAILABLE:
|
|
|
|
/* The pipe is already open with compatible parameters.
|
|
|
|
Pick a new name and retry. */
|
|
|
|
debug_printf ("pipe busy, retrying");
|
2021-09-03 10:57:21 +02:00
|
|
|
r = NULL;
|
2021-08-30 09:53:06 -04:00
|
|
|
break;
|
|
|
|
case STATUS_ACCESS_DENIED:
|
|
|
|
/* The pipe is already open with incompatible parameters.
|
|
|
|
Pick a new name and retry. */
|
|
|
|
debug_printf ("pipe access denied, retrying");
|
2021-09-03 10:57:21 +02:00
|
|
|
r = NULL;
|
2021-08-30 09:53:06 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
__seterrno_from_nt_status (status);
|
|
|
|
err = GetLastError ();
|
|
|
|
debug_printf ("failed, %E");
|
2021-09-03 10:59:02 +02:00
|
|
|
r = NULL;
|
2021-08-30 09:53:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
{
|
2021-09-03 10:57:21 +02:00
|
|
|
r = NULL;
|
2021-08-30 09:53:06 -04:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2021-09-03 10:57:21 +02:00
|
|
|
debug_printf ("NtOpenFile: name %S", &pipename);
|
2021-08-30 09:53:06 -04:00
|
|
|
|
2021-09-03 10:57:21 +02:00
|
|
|
access = GENERIC_WRITE | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
|
|
|
|
status = NtOpenFile (&w, access, &attr, &io, 0, 0);
|
|
|
|
if (!NT_SUCCESS (status))
|
|
|
|
{
|
|
|
|
DWORD err = GetLastError ();
|
|
|
|
debug_printf ("NtOpenFile failed, r %p, %E", r);
|
|
|
|
if (r)
|
|
|
|
NtClose (r);
|
|
|
|
w = NULL;
|
|
|
|
return err;
|
2021-08-30 09:53:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Success. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-07 17:40:21 -04:00
|
|
|
/* Called by dtable::init_std_file_from_handle for stdio handles
|
|
|
|
inherited from non-Cygwin processes. */
|
|
|
|
void
|
|
|
|
fhandler_pipe::set_pipe_buf_size ()
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
IO_STATUS_BLOCK io;
|
|
|
|
FILE_PIPE_LOCAL_INFORMATION fpli;
|
|
|
|
|
|
|
|
status = NtQueryInformationFile (get_handle (), &io, &fpli, sizeof fpli,
|
|
|
|
FilePipeLocalInformation);
|
|
|
|
if (NT_SUCCESS (status))
|
|
|
|
pipe_buf_size = fpli.InboundQuota;
|
|
|
|
}
|
|
|
|
|
2002-11-09 03:17:40 +00:00
|
|
|
int
|
|
|
|
fhandler_pipe::ioctl (unsigned int cmd, void *p)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
switch (cmd)
|
|
|
|
{
|
|
|
|
case FIONREAD:
|
|
|
|
if (get_device () == FH_PIPEW)
|
|
|
|
{
|
|
|
|
set_errno (EINVAL);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (!PeekNamedPipe (get_handle (), NULL, 0, NULL, (DWORD *) &n, NULL))
|
|
|
|
{
|
|
|
|
__seterrno ();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return fhandler_base::ioctl (cmd, p);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*(int *) p = n;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-05-23 15:52:52 -04:00
|
|
|
int
|
2016-01-11 12:35:41 +01:00
|
|
|
fhandler_pipe::fstat (struct stat *buf)
|
|
|
|
{
|
|
|
|
int ret = fhandler_base::fstat (buf);
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
buf->st_dev = FH_PIPE;
|
2016-01-11 19:10:45 +01:00
|
|
|
if (!(buf->st_ino = get_plain_ino ()))
|
2019-01-13 22:30:33 +01:00
|
|
|
sscanf (get_name (), "/proc/%*d/fd/pipe:[%llu]",
|
2016-01-11 12:35:41 +01:00
|
|
|
(long long *) &buf->st_ino);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-05-23 15:52:52 -04:00
|
|
|
int
|
2007-02-27 12:58:56 +00:00
|
|
|
fhandler_pipe::fstatvfs (struct statvfs *sfs)
|
|
|
|
{
|
|
|
|
set_errno (EBADF);
|
|
|
|
return -1;
|
|
|
|
}
|