summaryrefslogtreecommitdiff
path: root/debian/patches/introspection0002-libintrospection-a-library-for-Hurd-server-introspec.patch
blob: a4f6168029fc24e6058be9caf415d965080f2f25 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
From 52b7d557401f9e78cc4a7796cefe1c14bd2a4381 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Fri, 23 May 2014 17:01:48 +0200
Subject: [PATCH hurd 02/12] libintrospection: a library for Hurd server
 introspection

* Makefile (lib-subdirs): Add libintrospection.
* libintrospection/Makefile: New file.
* libintrospection/introspection.c: Likewise.
* libintrospection/introspection.h: Likewise.
* libintrospection/trace.c: Likewise.
---
 Makefile                         |   2 +
 libintrospection/Makefile        |  27 ++++++
 libintrospection/introspection.c |  86 +++++++++++++++++++
 libintrospection/introspection.h |  50 +++++++++++
 libintrospection/trace.c         | 175 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 340 insertions(+)
 create mode 100644 libintrospection/Makefile
 create mode 100644 libintrospection/introspection.c
 create mode 100644 libintrospection/introspection.h
 create mode 100644 libintrospection/trace.c

diff --git a/Makefile b/Makefile
index 3178740..76eec21 100644
--- a/Makefile
+++ b/Makefile
@@ -50,6 +50,8 @@ endif
 # Other directories
 other-subdirs = hurd doc config release include
 
+lib-subdirs += libintrospection
+
 # All the subdirectories together
 subdirs = $(lib-subdirs) $(prog-subdirs) $(other-subdirs)
 
diff --git a/libintrospection/Makefile b/libintrospection/Makefile
new file mode 100644
index 0000000..75adef2
--- /dev/null
+++ b/libintrospection/Makefile
@@ -0,0 +1,27 @@
+#   Copyright (C) 2014 Free Software Foundation, Inc.
+#
+#   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/>.
+
+dir := libintrospection
+makemode := library
+
+libname := libintrospection
+SRCS = introspection.c trace.c hurd_portUser.c
+installhdrs = introspection.h
+
+OBJS = $(SRCS:.c=.o)
+
+include ../Makeconf
diff --git a/libintrospection/introspection.c b/libintrospection/introspection.c
new file mode 100644
index 0000000..65c0727
--- /dev/null
+++ b/libintrospection/introspection.c
@@ -0,0 +1,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;
+}
diff --git a/libintrospection/introspection.h b/libintrospection/introspection.h
new file mode 100644
index 0000000..ae9fcfa
--- /dev/null
+++ b/libintrospection/introspection.h
@@ -0,0 +1,50 @@
+/* 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/>.  */
+
+#ifndef _HURD_INTROSPECTION_H_
+#define _HURD_INTROSPECTION_H_
+
+#include <errno.h>
+#include <mach.h>
+
+error_t
+introspection_set_port (mach_port_t task,
+			mach_port_t introspection_port);
+
+error_t
+introspection_get_port (mach_port_t task,
+			mach_port_t *introspection_port);
+
+error_t
+introspection_trace_message (mach_port_t trace_port,
+			     const mach_msg_header_t *msgp,
+			     mach_port_t id);
+
+error_t
+introspection_trace_request (mach_port_t trace_port,
+			     const mach_msg_header_t *msgp,
+			     mach_port_t *id);
+
+error_t
+introspection_extract_message (mach_msg_header_t *msgp,
+			       mach_port_t *id);
+
+#endif /* _HURD_INTROSPECTION_H_ */
diff --git a/libintrospection/trace.c b/libintrospection/trace.c
new file mode 100644
index 0000000..ac2fdb0
--- /dev/null
+++ b/libintrospection/trace.c
@@ -0,0 +1,175 @@
+/* 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 <error.h>
+#include <hurd/ports.h>
+#include <mach/mig_errors.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "introspection.h"
+
+static const mach_msg_type_t ports_type = {
+  /* msgt_name = */               MACH_MSG_TYPE_PORT_NAME,
+  /* msgt_size = */               sizeof (mach_port_t) << 3,
+  /* msgt_number = */             4,
+  /* msgt_inline = */             TRUE,
+  /* msgt_longform = */           FALSE,
+  /* msgt_deallocate = */         FALSE,
+  /* msgt_unused = */             0
+};
+
+struct trace_footer
+{
+  mach_msg_type_t ports_type;
+  mach_port_t id;
+  mach_port_t bits;
+  mach_port_t remote_port;
+  mach_port_t local_port;
+};
+
+error_t
+introspection_trace_message (mach_port_t trace_port,
+			     const mach_msg_header_t *msgp,
+			     mach_port_t id)
+{
+  error_t err;
+  mach_msg_header_t *copyp;
+  void *msg_buf_ptr;
+  struct trace_footer *footer;
+  size_t size = msgp->msgh_size + sizeof *footer;
+
+  copyp = malloc (size);
+  if (copyp == NULL)
+    return ENOMEM;
+
+  memcpy (copyp, msgp, msgp->msgh_size);
+  footer = (void *) copyp + msgp->msgh_size;
+
+  /* Process the message data, clear msgt_deallocate and turn rights
+     into mere port names.  */
+  msg_buf_ptr = (void *) copyp + sizeof *copyp;
+  while (msg_buf_ptr < (void *) copyp + msgp->msgh_size)
+    {
+      mach_msg_type_long_t *type_long = msg_buf_ptr;
+      mach_msg_type_t *type = &type_long->msgtl_header;
+      mach_msg_type_number_t nelt; /* Number of data items.  */
+      mach_msg_type_size_t eltsize; /* Bytes per item.	*/
+
+      type->msgt_deallocate = 0;
+
+      if (! type->msgt_longform)
+	{
+	  nelt = type->msgt_number;
+	  eltsize = type->msgt_size / 8;
+	  if (MACH_MSG_TYPE_PORT_ANY (type->msgt_name))
+	    type->msgt_name = MACH_MSG_TYPE_PORT_NAME;
+	  msg_buf_ptr += sizeof *type;
+	}
+      else
+	{
+	  nelt = type_long->msgtl_number;
+	  eltsize = type_long->msgtl_size / 8;
+	  if (MACH_MSG_TYPE_PORT_ANY (type_long->msgtl_name))
+	    type_long->msgtl_name = MACH_MSG_TYPE_PORT_NAME;
+	  msg_buf_ptr += sizeof *type_long;
+	}
+
+      if (! type->msgt_inline)
+	/* This datum is out-of-line, meaning the message actually
+	   contains a pointer to a vm_allocate'd region of data.  */
+	msg_buf_ptr += sizeof (void *);
+      else
+	msg_buf_ptr += ((nelt * eltsize + sizeof(natural_t) - 1)
+			& ~(sizeof(natural_t) - 1));
+    }
+
+  copyp->msgh_bits =
+    MACH_MSGH_BITS (MACH_MSG_TYPE_COPY_SEND, 0)
+    | (msgp->msgh_bits & MACH_MSGH_BITS_COMPLEX);
+  copyp->msgh_remote_port = trace_port;
+  copyp->msgh_local_port = MACH_PORT_NULL;
+
+  *footer = (struct trace_footer)
+    {
+      ports_type,
+      id,
+      (mach_port_t) msgp->msgh_bits,
+      msgp->msgh_remote_port,
+      MACH_MSGH_BITS_LOCAL (msgp->msgh_bits)
+	== MACH_MSG_TYPE_PROTECTED_PAYLOAD
+      ? ports_payload_get_name (msgp->msgh_protected_payload)
+      : msgp->msgh_local_port,
+    };
+
+  err = mach_msg (copyp, MACH_SEND_MSG|MACH_MSG_OPTION_NONE, size,
+		  0, MACH_PORT_NULL, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
+
+  free (copyp);
+  return err;
+}
+
+static mach_port_t
+make_id (void)
+{
+  static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
+  static mach_port_t id;
+  mach_port_t result;
+
+  pthread_mutex_lock (&lock);
+
+  do
+    id += 1;
+  while (! MACH_PORT_VALID (id));
+
+  result = id;
+  pthread_mutex_unlock (&lock);
+
+  return result;
+}
+
+error_t
+introspection_trace_request (mach_port_t trace_port,
+			     const mach_msg_header_t *msgp,
+			     mach_port_t *id)
+{
+  *id = make_id ();
+  return introspection_trace_message (trace_port, msgp, *id);
+}
+
+error_t
+introspection_extract_message (mach_msg_header_t *msgp,
+			       mach_port_t *id)
+{
+  size_t size = msgp->msgh_size - sizeof (struct trace_footer);
+  struct trace_footer *footer = (void *) msgp + size;
+
+  if (memcmp (&footer->ports_type, &ports_type, sizeof ports_type) != 0)
+    return MIG_BAD_ARGUMENTS;
+
+  msgp->msgh_bits = (mach_msg_bits_t) footer->bits;
+  msgp->msgh_remote_port = footer->remote_port;
+  msgp->msgh_local_port = footer->local_port;
+  msgp->msgh_size = size;
+  *id = footer->id;
+  return 0;
+}
-- 
2.1.4