From fbcd61063b10ab053c4067095383b535c5d3c125 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Tue, 25 May 2021 16:50:16 +0200 Subject: [PATCH] Cygwin: POSIX msg queues: Implement dup Create a private method fhandler_mqueue::_dup and call it from dup and fixup_after_fork methods. Signed-off-by: Corinna Vinschen --- winsup/cygwin/fhandler.h | 2 ++ winsup/cygwin/fhandler_mqueue.cc | 44 +++++++++++++++++++------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index 4c8fe0f1f..ff51d29a5 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -3113,6 +3113,8 @@ class fhandler_mqueue: public fhandler_disk_file struct mq_info *mqinfo_open (int); void mq_open_finish (bool success, bool created); + int _dup (HANDLE parent, fhandler_mqueue *child); + public: fhandler_mqueue (); fhandler_mqueue (void *) {} diff --git a/winsup/cygwin/fhandler_mqueue.cc b/winsup/cygwin/fhandler_mqueue.cc index 6b0d98d75..9114d8f32 100644 --- a/winsup/cygwin/fhandler_mqueue.cc +++ b/winsup/cygwin/fhandler_mqueue.cc @@ -353,15 +353,7 @@ fhandler_mqueue::fstat (struct stat *buf) } int -fhandler_mqueue::dup (fhandler_base *child, int flags) -{ - /* FIXME */ - set_errno (EBADF); - return -1; -} - -void -fhandler_mqueue::fixup_after_fork (HANDLE parent) +fhandler_mqueue::_dup (HANDLE parent, fhandler_mqueue *fhc) { __try { @@ -370,7 +362,7 @@ fhandler_mqueue::fixup_after_fork (HANDLE parent) NTSTATUS status; if (!DuplicateHandle (parent, mqinfo ()->mqi_sect, - GetCurrentProcess (), &mqinfo ()->mqi_sect, + GetCurrentProcess (), &fhc->mqinfo ()->mqi_sect, 0, FALSE, DUPLICATE_SAME_ACCESS)) __leave; status = NtMapViewOfSection (mqinfo ()->mqi_sect, NtCurrentProcess (), @@ -380,31 +372,47 @@ fhandler_mqueue::fixup_after_fork (HANDLE parent) api_fatal ("Mapping message queue failed in fork, status 0x%x\n", status); - mqinfo ()->mqi_hdr = (struct mq_hdr *) mptr; + fhc->mqinfo ()->mqi_hdr = (struct mq_hdr *) mptr; if (!DuplicateHandle (parent, mqinfo ()->mqi_waitsend, - GetCurrentProcess (), &mqinfo ()->mqi_waitsend, + GetCurrentProcess (), &fhc->mqinfo ()->mqi_waitsend, 0, FALSE, DUPLICATE_SAME_ACCESS)) __leave; if (!DuplicateHandle (parent, mqinfo ()->mqi_waitrecv, - GetCurrentProcess (), &mqinfo ()->mqi_waitrecv, + GetCurrentProcess (), &fhc->mqinfo ()->mqi_waitrecv, 0, FALSE, DUPLICATE_SAME_ACCESS)) __leave; if (!DuplicateHandle (parent, mqinfo ()->mqi_lock, - GetCurrentProcess (), &mqinfo ()->mqi_lock, + GetCurrentProcess (), &fhc->mqinfo ()->mqi_lock, 0, FALSE, DUPLICATE_SAME_ACCESS)) __leave; - return; + return 0; } __except (EFAULT) {} __endtry - api_fatal ("Creating IPC object failed in fork, %E"); + return -1; +} + +int +fhandler_mqueue::dup (fhandler_base *child, int flags) +{ + fhandler_mqueue *fhc = (fhandler_mqueue *) child; + + int ret = fhandler_disk_file::dup (child, flags); + if (!ret) + ret = _dup (GetCurrentProcess (), fhc); + return ret; +} + +void +fhandler_mqueue::fixup_after_fork (HANDLE parent) +{ + if (_dup (parent, this)) + api_fatal ("Creating IPC object failed in fork, %E"); } int fhandler_mqueue::close () { - int ret = -1; - __try { mqinfo ()->mqi_magic = 0; /* just in case */