diff mbox series

[hurd,commited] hurd: Implement _S_msg_get_dtable

Message ID 20220102015448.2379845-1-samuel.thibault@ens-lyon.org
State New
Headers show
Series [hurd,commited] hurd: Implement _S_msg_get_dtable | expand

Commit Message

Samuel Thibault Jan. 2, 2022, 1:54 a.m. UTC
This will be needed for implementing lsof.
---
 hurd/hurdmsg.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 47 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/hurd/hurdmsg.c b/hurd/hurdmsg.c
index 2b9b214102..4e6ddf2836 100644
--- a/hurd/hurdmsg.c
+++ b/hurd/hurdmsg.c
@@ -385,15 +385,58 @@  _S_msg_set_environment (mach_port_t msgport, mach_port_t auth,
 }
 
 
-/* XXX */
-
 kern_return_t
 _S_msg_get_dtable (mach_port_t process,
-		   mach_port_t refport,
+		   mach_port_t auth,
 		   portarray_t *dtable,
 		   mach_msg_type_name_t *dtablePoly,
 		   mach_msg_type_number_t *dtableCnt)
-{ return EOPNOTSUPP; }
+{
+  mach_port_t *ports;
+  mach_msg_type_number_t i;
+  error_t err;
+
+  AUTHCHECK;
+
+  HURD_CRITICAL_BEGIN;
+  __mutex_lock (&_hurd_dtable_lock);
+
+  if (err = __vm_allocate (__mach_task_self (), (vm_address_t *) &ports,
+			   _hurd_dtablesize * sizeof(mach_port_t), 1))
+    goto out;
+
+  for (i = 0; i < _hurd_dtablesize; i++)
+    {
+      struct hurd_fd *cell = _hurd_dtable[i];
+      if (cell == NULL)
+	ports[i] = MACH_PORT_NULL;
+      else
+	{
+	  __spin_lock (&cell->port.lock);
+	  if (cell->port.port == MACH_PORT_NULL)
+	    ports[i] = MACH_PORT_NULL;
+	  else
+	    {
+	      ports[i] = cell->port.port;
+	      /* We will move this send right.  */
+	      __mach_port_mod_refs (__mach_task_self (), ports[i],
+				    MACH_PORT_RIGHT_SEND, +1);
+	    }
+	  __spin_unlock (&cell->port.lock);
+	}
+    }
+
+  *dtable = ports;
+  *dtablePoly = MACH_MSG_TYPE_MOVE_SEND;
+  *dtableCnt = _hurd_dtablesize;
+
+out:
+  __mutex_unlock (&_hurd_dtable_lock);
+  HURD_CRITICAL_END;
+  return err;
+}
+
+/* XXX */
 
 kern_return_t
 _S_msg_set_dtable (mach_port_t process,