From 3319f7f6a238cf88b9f46849e7be84d3c96376d6 Mon Sep 17 00:00:00 2001
From: Justus Winter <justus@gnupg.org>
Date: Sun, 9 Oct 2016 17:00:31 +0200
Subject: fshelp: Fix fetching the control port.

* libfshelp/fetch-control.c (fshelp_fetch_control): Check if the
control port is still alive, or deallocate the dead name, record the
fact in the transbox, and return MACH_PORT_NULL.
---
 libfshelp/fetch-control.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

(limited to 'libfshelp/fetch-control.c')

diff --git a/libfshelp/fetch-control.c b/libfshelp/fetch-control.c
index 26c12d88..91c65bb0 100644
--- a/libfshelp/fetch-control.c
+++ b/libfshelp/fetch-control.c
@@ -19,13 +19,24 @@
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
 
 #include "fshelp.h"
+#include <assert.h>
 
 error_t
 fshelp_fetch_control (struct transbox *box,
 		      mach_port_t *control)
 {
+  error_t err = 0;
   *control = box->active;
   if (*control != MACH_PORT_NULL)
-    mach_port_mod_refs (mach_task_self (), *control, MACH_PORT_RIGHT_SEND, 1);
-  return 0;
+    err = mach_port_mod_refs (mach_task_self (), *control,
+                              MACH_PORT_RIGHT_SEND, 1);
+
+  if (err == KERN_INVALID_RIGHT)
+    {
+      err = mach_port_deallocate (mach_task_self (), *control);
+      assert_perror (err);
+      *control = box->active = MACH_PORT_NULL;
+    }
+
+  return err;
 }
-- 
cgit v1.2.3