summaryrefslogtreecommitdiff
path: root/libintrospection/introspection.c
blob: 65c0727280dd34cf0b9f04e1a85fb8cd625c632c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* Hurd server introspection.

   Copyright (C) 2014 Free Software Foundation, Inc.

   Written by Justus Winter <4winter@informatik.uni-hamburg.de>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#include <mach.h>

#include "introspection.h"
#include "hurd_port_U.h"

error_t
introspection_set_port (mach_port_t task,
			mach_port_t introspection_port)
{
  error_t err;
  mach_port_t *ports;
  size_t ports_len;

  err = mach_ports_lookup (task, &ports, &ports_len);
  if (err)
    return err;

  if (MACH_PORT_VALID (ports[HURD_PORT_REGISTER_INTROSPECTION]))
    mach_port_deallocate (mach_task_self (),
			  ports[HURD_PORT_REGISTER_INTROSPECTION]);

  ports[HURD_PORT_REGISTER_INTROSPECTION] = introspection_port;

  err = mach_ports_register (task, ports, ports_len);
  if (err)
    {
      size_t i;
      for (i = 0; i < ports_len; i++)
	if (MACH_PORT_VALID (ports[i]))
	  mach_port_deallocate (mach_task_self (), ports[i]);

      return err;
    }

  return 0;
}

error_t
introspection_get_port (mach_port_t task, mach_port_t *introspection_port)
{
  error_t err;
  mach_port_t *ports;
  size_t ports_len;

  err = mach_ports_lookup (task, &ports, &ports_len);
  if (! err)
    {
      size_t i;
      if (MACH_PORT_VALID (*introspection_port))
	mach_port_deallocate (mach_task_self (), *introspection_port);

      for (i = 0; i < ports_len; i++)
	if (i == HURD_PORT_REGISTER_INTROSPECTION)
	  *introspection_port = ports[i];
	else
	  {
	    if (MACH_PORT_VALID (ports[i]))
	      mach_port_deallocate (mach_task_self (), ports[i]);
	  }
    }
  else
    *introspection_port = MACH_PORT_DEAD;

  return err;
}