From c7eb1a1f5225bb8e1a992519c28b72d74a0b59fd Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Tue, 28 Jan 2025 16:07:22 +0100 Subject: [PATCH] Cygwin: fhandler: move "isclosed" status flag into fhandler_pipe_fifo The isclosed flag is only used in pipe and FIFO code, so move the flag down into the fhandler_pipe_fifo class. Note that such a flag is not sufficient to avoid evil, like closing an already closing fhandler from another thread. If we ever need this, it has to be implemented threadsafe. Signed-off-by: Corinna Vinschen --- winsup/cygwin/fhandler/base.cc | 1 - winsup/cygwin/fhandler/fifo.cc | 1 + winsup/cygwin/fhandler/pipe.cc | 6 +++++- winsup/cygwin/local_includes/fhandler.h | 13 ++++++++++--- winsup/cygwin/posix_ipc.cc | 1 - winsup/cygwin/syscalls.cc | 1 - 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/winsup/cygwin/fhandler/base.cc b/winsup/cygwin/fhandler/base.cc index 0902bf0c2..b889157d6 100644 --- a/winsup/cygwin/fhandler/base.cc +++ b/winsup/cygwin/fhandler/base.cc @@ -1579,7 +1579,6 @@ fhandler_base::fhandler_base () : ra.raixget = 0; ra.raixput = 0; ra.rabuflen = 0; - isclosed (false); } /* Normal I/O destructor */ diff --git a/winsup/cygwin/fhandler/fifo.cc b/winsup/cygwin/fhandler/fifo.cc index ee49ce695..7a03894fa 100644 --- a/winsup/cygwin/fhandler/fifo.cc +++ b/winsup/cygwin/fhandler/fifo.cc @@ -1524,6 +1524,7 @@ fhandler_fifo::cancel_reader_thread () int fhandler_fifo::close () { + isclosed (true); if (select_sem) { release_select_sem ("close"); diff --git a/winsup/cygwin/fhandler/pipe.cc b/winsup/cygwin/fhandler/pipe.cc index 76af6bc05..d76a4e74e 100644 --- a/winsup/cygwin/fhandler/pipe.cc +++ b/winsup/cygwin/fhandler/pipe.cc @@ -31,7 +31,10 @@ STATUS_PIPE_EMPTY simply means there's no data to be read. */ || _s == STATUS_PIPE_EMPTY; }) fhandler_pipe_fifo::fhandler_pipe_fifo () - : fhandler_base (), pipe_buf_size (DEFAULT_PIPEBUFSIZE), pipe_mtx (NULL) + : fhandler_base (), + status (), + pipe_buf_size (DEFAULT_PIPEBUFSIZE), + pipe_mtx (NULL) { } @@ -758,6 +761,7 @@ fhandler_pipe::dup (fhandler_base *child, int flags) int fhandler_pipe::close () { + isclosed (true); if (select_sem) { release_select_sem ("close"); diff --git a/winsup/cygwin/local_includes/fhandler.h b/winsup/cygwin/local_includes/fhandler.h index 327b636a7..3550481b5 100644 --- a/winsup/cygwin/local_includes/fhandler.h +++ b/winsup/cygwin/local_includes/fhandler.h @@ -179,14 +179,13 @@ class fhandler_base read or write access */ unsigned close_on_exec : 1; /* close-on-exec */ unsigned need_fork_fixup : 1; /* Set if need to fixup after fork. */ - unsigned isclosed : 1; /* Set when fhandler is closed. */ unsigned mandatory_locking : 1; /* Windows mandatory locking */ public: status_flags () : rbinary (0), rbinset (0), wbinary (0), wbinset (0), nohandle (0), did_lseek (0), query_open (no_query), close_on_exec (0), - need_fork_fixup (0), isclosed (0), mandatory_locking (0) + need_fork_fixup (0), mandatory_locking (0) {} } status, open_status; @@ -290,7 +289,6 @@ class fhandler_base IMPLEMENT_STATUS_FLAG (query_state, query_open) IMPLEMENT_STATUS_FLAG (bool, close_on_exec) IMPLEMENT_STATUS_FLAG (bool, need_fork_fixup) - IMPLEMENT_STATUS_FLAG (bool, isclosed) IMPLEMENT_STATUS_FLAG (bool, mandatory_locking) int get_default_fmode (int flags); @@ -1195,11 +1193,20 @@ class fhandler_socket_unix : public fhandler_socket /* A parent of fhandler_pipe and fhandler_fifo. */ class fhandler_pipe_fifo: public fhandler_base { + struct status_flags + { + unsigned isclosed : 1; /* Set when pipe/FIFO fhandler is closed. */ + public: + status_flags () : isclosed (0) {} + } status; + protected: size_t pipe_buf_size; HANDLE pipe_mtx; /* Used only in the pipe case */ virtual void release_select_sem (const char *) {}; + IMPLEMENT_STATUS_FLAG (bool, isclosed) + public: fhandler_pipe_fifo (); diff --git a/winsup/cygwin/posix_ipc.cc b/winsup/cygwin/posix_ipc.cc index c188b0ce2..001b4a70c 100644 --- a/winsup/cygwin/posix_ipc.cc +++ b/winsup/cygwin/posix_ipc.cc @@ -333,7 +333,6 @@ mq_close (mqd_t mqd) if (mq_notify (mqd, NULL)) /* unregister calling process */ __leave; - fd->isclosed (true); fd->close (); fd.release (); return 0; diff --git a/winsup/cygwin/syscalls.cc b/winsup/cygwin/syscalls.cc index fa26592bc..4890131b7 100644 --- a/winsup/cygwin/syscalls.cc +++ b/winsup/cygwin/syscalls.cc @@ -1692,7 +1692,6 @@ close (int fd) res = -1; else { - cfd->isclosed (true); res = cfd->close_with_arch (); cfd.release (); }