Blame hurd/report-wait.c

Packit 6c4009
/* Report on what a thread in our task is waiting for.
Packit 6c4009
   Copyright (C) 1996-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <hurd.h>
Packit 6c4009
#include <hurd/signal.h>
Packit 6c4009
#include <hurd/fd.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <hurd/msg_server.h>
Packit 6c4009
#include <thread_state.h>
Packit 6c4009
#include <intr-msg.h>
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
describe_number (string_t description, const char *flavor, long int i)
Packit 6c4009
{
Packit 6c4009
  unsigned long int j;
Packit 6c4009
  char *p = flavor == NULL ? description : __stpcpy (description, flavor);
Packit 6c4009
  char *end;
Packit 6c4009
Packit 6c4009
  /* Handle sign.  */
Packit 6c4009
  if (i < 0)
Packit 6c4009
    {
Packit 6c4009
      i = -i;
Packit 6c4009
      *p++ = '-';
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Allocate space for the number at the end of DESCRIPTION.  */
Packit 6c4009
  for (j = i; j >= 10; j /= 10)
Packit 6c4009
    p++;
Packit 6c4009
  end = p + 1;
Packit 6c4009
  *end = '\0';
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      *p-- = '0' + i % 10;
Packit 6c4009
      i /= 10;
Packit 6c4009
    } while (i != 0);
Packit 6c4009
Packit 6c4009
  return end;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static char *
Packit 6c4009
describe_port (string_t description, mach_port_t port)
Packit 6c4009
{
Packit 6c4009
  int i;
Packit 6c4009
Packit 6c4009
  if (port == MACH_PORT_NULL)
Packit 6c4009
    return __stpcpy (description, "(null)");
Packit 6c4009
  if (port == MACH_PORT_DEAD)
Packit 6c4009
    return __stpcpy (description, "(dead)");
Packit 6c4009
Packit 6c4009
  if (port == __mach_task_self ())
Packit 6c4009
    return __stpcpy (description, "task-self");
Packit 6c4009
Packit 6c4009
  for (i = 0; i < _hurd_nports; ++i)
Packit 6c4009
    if (port == _hurd_ports[i].port)
Packit 6c4009
      return describe_number (description, "init#", i);
Packit 6c4009
Packit 6c4009
  if (_hurd_init_dtable)
Packit 6c4009
    {
Packit 6c4009
      for (i = 0; i < _hurd_init_dtablesize; ++i)
Packit 6c4009
	if (port == _hurd_init_dtable[i])
Packit 6c4009
	  return describe_number (description, "fd#", i);
Packit 6c4009
    }
Packit 6c4009
  else if (_hurd_dtable)
Packit 6c4009
    {
Packit 6c4009
      for (i = 0; i < _hurd_dtablesize; ++i)
Packit 6c4009
	if (_hurd_dtable[i] == NULL)
Packit 6c4009
	  continue;
Packit 6c4009
	else if (port == _hurd_dtable[i]->port.port)
Packit 6c4009
	  return describe_number (description, "fd#", i);
Packit 6c4009
	else if (port == _hurd_dtable[i]->ctty.port)
Packit 6c4009
	  return describe_number (description, "bgfd#", i);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return describe_number (description, "port#", port);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* We want _HURD_ITIMER_THREAD, but don't want to link in the itimer code
Packit 6c4009
   unnecessarily.  */
Packit 6c4009
#if 0 /* libc.so.0.0 needs this defined, so make it a weak alias for now.  */
Packit 6c4009
extern thread_t _hurd_itimer_thread; /* XXX */
Packit 6c4009
weak_extern (_hurd_itimer_thread)
Packit 6c4009
#else
Packit 6c4009
static thread_t default_hurd_itimer_thread;
Packit 6c4009
weak_alias (default_hurd_itimer_thread, _hurd_itimer_thread)
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
kern_return_t
Packit 6c4009
_S_msg_report_wait (mach_port_t msgport, thread_t thread,
Packit 6c4009
		    string_t description, mach_msg_id_t *msgid)
Packit 6c4009
{
Packit 6c4009
  *msgid = 0;
Packit 6c4009
Packit 6c4009
  if (thread == _hurd_msgport_thread)
Packit 6c4009
    /* Cute.  */
Packit 6c4009
    strcpy (description, "msgport");
Packit 6c4009
  else if (&_hurd_itimer_thread && thread == _hurd_itimer_thread)
Packit 6c4009
    strcpy (description, "itimer");
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      /* Make sure this is really one of our threads.  */
Packit 6c4009
Packit 6c4009
      struct hurd_sigstate *ss;
Packit 6c4009
Packit 6c4009
      __mutex_lock (&_hurd_siglock);
Packit 6c4009
      for (ss = _hurd_sigstates; ss != NULL; ss = ss->next)
Packit 6c4009
	if (ss->thread == thread)
Packit 6c4009
	  break;
Packit 6c4009
      __mutex_unlock (&_hurd_siglock);
Packit 6c4009
      if (ss == NULL)
Packit 6c4009
	/* To hell with you.  */
Packit 6c4009
	return EINVAL;
Packit 6c4009
Packit 6c4009
      if (ss->suspended != MACH_PORT_NULL)
Packit 6c4009
	strcpy (description, "sigsuspend");
Packit 6c4009
      else
Packit 6c4009
	{
Packit 6c4009
	  /* Examine the thread's state to see if it is blocked in an RPC.  */
Packit 6c4009
Packit 6c4009
	  struct machine_thread_state state;
Packit 6c4009
	  mach_msg_type_number_t count = MACHINE_THREAD_STATE_COUNT;
Packit 6c4009
	  error_t err;
Packit 6c4009
Packit 6c4009
	  err = __thread_get_state (thread, MACHINE_THREAD_STATE_FLAVOR,
Packit 6c4009
				    (natural_t *) &state, &count);
Packit 6c4009
	  if (err)
Packit 6c4009
	    return err;
Packit 6c4009
	  assert (count == MACHINE_THREAD_STATE_COUNT);
Packit 6c4009
	  if (SYSCALL_EXAMINE (&state, msgid))
Packit 6c4009
	    {
Packit 6c4009
	      mach_port_t send_port, rcv_port;
Packit 6c4009
	      mach_msg_option_t option;
Packit 6c4009
	      mach_msg_timeout_t timeout;
Packit 6c4009
Packit 6c4009
	      /* Blocked in a system call.  */
Packit 6c4009
	      if (*msgid == -25
Packit 6c4009
		  /* mach_msg system call.  Examine its parameters.  */
Packit 6c4009
		  && MSG_EXAMINE (&state, msgid, &send_port, &rcv_port,
Packit 6c4009
				  &option, &timeout) == 0)
Packit 6c4009
		{
Packit 6c4009
		  char *p;
Packit 6c4009
		  if (send_port != MACH_PORT_NULL && *msgid != 0)
Packit 6c4009
		    {
Packit 6c4009
		      /* For the normal case of RPCs, we consider the
Packit 6c4009
			 destination port to be the interesting thing
Packit 6c4009
			 whether we are in fact sending or receiving at the
Packit 6c4009
			 moment.  That tells us who we are waiting for the
Packit 6c4009
			 reply from.  */
Packit 6c4009
		      if (send_port == ss->intr_port)
Packit 6c4009
			{
Packit 6c4009
			  /* This is a Hurd interruptible RPC.
Packit 6c4009
			     Mark it by surrounding the port description
Packit 6c4009
			     string with [...] brackets.  */
Packit 6c4009
			  description[0] = '[';
Packit 6c4009
			  p = describe_port (description + 1, send_port);
Packit 6c4009
			  *p++ = ']';
Packit 6c4009
			  *p = '\0';
Packit 6c4009
			}
Packit 6c4009
		      else
Packit 6c4009
			(void) describe_port (description, send_port);
Packit 6c4009
		    }
Packit 6c4009
		  else if (rcv_port != MACH_PORT_NULL)
Packit 6c4009
		    {
Packit 6c4009
		      /* This system call had no send port, but had a
Packit 6c4009
			 receive port.  The msgid we extracted is then just
Packit 6c4009
			 some garbage or perhaps the msgid of the last
Packit 6c4009
			 message this thread received, but it's not a
Packit 6c4009
			 helpful thing to return.  */
Packit 6c4009
		      strcpy (describe_port (description, rcv_port), ":rcv");
Packit 6c4009
		      *msgid = 0;
Packit 6c4009
		    }
Packit 6c4009
		  else if ((option & (MACH_RCV_MSG|MACH_RCV_TIMEOUT))
Packit 6c4009
			   == (MACH_RCV_MSG|MACH_RCV_TIMEOUT))
Packit 6c4009
		    {
Packit 6c4009
		      /* A receive with no valid port can be used for a
Packit 6c4009
			 pure timeout.  Report the timeout value (counted
Packit 6c4009
			 in milliseconds); note this is the original total
Packit 6c4009
			 time, not the time remaining.  */
Packit 6c4009
		      strcpy (describe_number (description, 0, timeout), "ms");
Packit 6c4009
		      *msgid = 0;
Packit 6c4009
		    }
Packit 6c4009
		  else
Packit 6c4009
		    {
Packit 6c4009
		      strcpy (description, "mach_msg");
Packit 6c4009
		      *msgid = 0;
Packit 6c4009
		    }
Packit 6c4009
		}
Packit 6c4009
	      else		/* Some other system call.  */
Packit 6c4009
		{
Packit 6c4009
		  (void) describe_number (description, "syscall#", *msgid);
Packit 6c4009
		  *msgid = 0;
Packit 6c4009
		}
Packit 6c4009
	    }
Packit 6c4009
	  else
Packit 6c4009
	    description[0] = '\0';
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  __mach_port_deallocate (__mach_task_self (), thread);
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
kern_return_t
Packit 6c4009
_S_msg_describe_ports (mach_port_t msgport, mach_port_t refport,
Packit 6c4009
		       mach_port_t *ports, mach_msg_type_number_t nports,
Packit 6c4009
		       char **desc, mach_msg_type_number_t *desclen)
Packit 6c4009
{
Packit 6c4009
  char *p, *end;
Packit 6c4009
Packit 6c4009
  if (__USEPORT (AUTH, msgport != port))
Packit 6c4009
    return EPERM;
Packit 6c4009
Packit 6c4009
  end = *desc + *desclen;
Packit 6c4009
  p = *desc;
Packit 6c4009
  while (nports-- > 0)
Packit 6c4009
    {
Packit 6c4009
      char this[200];
Packit 6c4009
      describe_port (this, *ports++);
Packit 6c4009
      p = __stpncpy (p, this, end - p);
Packit 6c4009
      if (p == end && p[-1] != '\0')
Packit 6c4009
	return ENOMEM;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *desclen = p - *desc;
Packit 6c4009
  return 0;
Packit 6c4009
}