summaryrefslogtreecommitdiff
path: root/libshouldbeinlibc/portinfo.c
blob: 42b220818d89185e6575ac5ba7aa85ec8ca858e1 (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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* Print information about a task's ports

   Copyright (C) 1996,98,99,2002 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.org>

   This program 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.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include <sys/types.h>
#include <sys/mman.h>
#include <arpa/inet.h>
#include <sys/un.h>

#include <mach/default_pager.h>
#include <hurd/fsys.h>
#include <hurd/msg.h>
#include <hurd/pci.h>
#include <hurd/process.h>
#include <hurd/socket.h>
#include <hurd/term.h>
#include <hurd/tioctl.h>
#include <hurd.h>

#include "portinfo.h"

/* Prints info about NAME in TASK to STREAM, in a way described by the flags
   in SHOW.  If TYPE is non-zero, it should be what mach_port_type returns
   for NAME.  */
error_t
print_port_info (mach_port_t name, mach_port_type_t type, task_t task,
		 unsigned show, FILE *stream)
{
  int hex_names = (show & PORTINFO_HEX_NAMES);
  int first = 1, subfirst = 1;
  void comma ()
    {
      if (first)
	first = 0;
      else
	fprintf (stream, ", ");
    }
  void subcomma ()
    {
      if (subfirst)
	subfirst = 0;
      else
	fprintf (stream, ",");
    }
  void prefs (mach_port_right_t right)
    {
      mach_port_urefs_t refs;
      error_t err = mach_port_get_refs (task, name, right, &refs);
      if (! err)
	fprintf (stream, " (refs: %zu)", refs);
    }

  if (type == 0)
    {
      error_t err = mach_port_type (task, name, &type);
      if (err)
	return err;
    }

  fprintf (stream, hex_names ? "%#6x: " : "%6u: ", name);

  if (type & MACH_PORT_TYPE_RECEIVE)
    {
      comma ();
      fprintf (stream, "receive");
    }
  if (type & MACH_PORT_TYPE_SEND)
    {
      comma ();
      fprintf (stream, "send");
    }
  if (type & MACH_PORT_TYPE_SEND_ONCE)
    {
      comma ();
      fprintf (stream, "send-once");
    }
  if (type & MACH_PORT_TYPE_DEAD_NAME)
    {
      comma ();
      fprintf (stream, "dead-name");
    }
  if (type & MACH_PORT_TYPE_PORT_SET)
    {
      comma ();
      fprintf (stream, "port-set");
    }

  if (show & PORTINFO_DETAILS)
    {
      error_t err;
      mach_port_t port;
      mach_msg_type_name_t acquired_type;

      /* Get the port itself.  */
      err = mach_port_extract_right (task, name,
				     (type & MACH_PORT_TYPE_RECEIVE
				      ? MACH_MSG_TYPE_MAKE_SEND
				      : MACH_MSG_TYPE_COPY_SEND),
				     &port, &acquired_type);

      if (!err)
	{
	  process_t proc = getproc ();
	  mach_port_t msgport;
	  pid_t pid;

	  if (port == MACH_PORT_DEAD)
	    err = EIEIO;
	  else
	    err = proc_task2pid (proc, task, &pid);
	  if (!err)
	    err = proc_getmsgport (proc, pid, &msgport);
	  if (!err)
	    {
	      /* Check if it is installed as an FD.  */
	      mach_port_t *dtable = NULL;
	      mach_msg_type_number_t ndtable = 0;
	      unsigned i;

	      msg_get_dtable (msgport, task, &dtable, &ndtable);

	      for (i = 0; i < ndtable; i++)
		{
		  if (dtable[i] == port)
		    fprintf (stream, " fd(%d)", i);
		  mach_port_deallocate (mach_task_self (), dtable[i]);
		}
	      vm_deallocate (mach_task_self (), (vm_address_t) dtable, ndtable * sizeof (*dtable));

	      /* First check for init ports.  */
	      void check_init_port (int num, const char *name)
		{
		  mach_port_t init_port;

		  err = msg_get_init_port (msgport, task, num, &init_port);
		  if (!err)
		    {
		      if (port == init_port)
			fprintf (stream, " %s", name);
		      mach_port_deallocate (mach_task_self (), init_port);
		    }
		}

	      check_init_port (INIT_PORT_CWDIR, "CWDIR");
	      check_init_port (INIT_PORT_CRDIR, "CRDIR");
	      check_init_port (INIT_PORT_AUTH, "AUTH");
	      check_init_port (INIT_PORT_PROC, "PROC");
	      check_init_port (INIT_PORT_CTTYID, "CTTYID");
	      check_init_port (INIT_PORT_BOOTSTRAP, "BOOTSTRAP");

	      mach_port_deallocate (mach_task_self (), msgport);
	    }

	  /* Then try to use the ports to see what they are.  */
	  if (type & MACH_PORT_TYPE_RECEIVE)
	    {
	      if (port == msgport)
		fprintf (stream, " msg");
	    }
	  else if (type & MACH_PORT_TYPE_SEND)
	    {
	      /* auth_t */
	      {
		uid_t *euids = 0, *auids = 0, *egids = 0, *agids = 0;
		mach_msg_type_number_t neuids = 0, nauids = 0, negids = 0, nagids = 0, i;

		err = auth_getids (port,
				   &euids, &neuids,
				   &auids, &nauids,
				   &egids, &negids,
				   &agids, &nagids);

		if (!err)
		  {
		    fprintf (stream, " auth([");
		    subfirst = 1;
		    for (i = 0; i < neuids; i++)
		      {
			subcomma ();
			fprintf (stream, "%d", euids[i]);
		      }
		    fprintf (stream, "],[");
		    subfirst = 1;
		    for (i = 0; i < nauids; i++)
		      {
			subcomma ();
			fprintf (stream, "%d", auids[i]);
		      }
		    fprintf (stream, "],[");
		    subfirst = 1;
		    for (i = 0; i < negids; i++)
		      {
			subcomma ();
			fprintf (stream, "%d", egids[i]);
		      }
		    fprintf (stream, "],[");
		    subfirst = 1;
		    for (i = 0; i < nagids; i++)
		      {
			subcomma ();
			fprintf (stream, "%d", agids[i]);
		      }
		    fprintf (stream, "])");
		    munmap (euids, neuids * sizeof (*euids));
		    munmap (auids, nauids * sizeof (*auids));
		    munmap (egids, negids * sizeof (*egids));
		    munmap (agids, nagids * sizeof (*agids));
		  }
	      }

	      /* file_t */
	      {
		int allowed;

		err = file_check_access (port, &allowed);

		if (!err)
		  {
		    int printbar = 0;
		    void bar (void)
		      {
			if (printbar)
			  fprintf (stream, "|");
			else
			  printbar = 1;
		      }

		    fprintf (stream, " file(");
		    if (allowed & O_READ)
		      {
			bar ();
			fprintf (stream, "READ");
		      }
		    if (allowed & O_WRITE)
		      {
			bar ();
			fprintf (stream, "WRITE");
		      }
		    if (allowed & O_EXEC)
		      {
			bar ();
			fprintf (stream, "EXEC");
		      }
		    fprintf (stream, ")");
		  }
	      }

	      /* fsys_t */
	      {
		string_t source;

		err = fsys_get_source (port, source);

		if (!err)
		  fprintf (stream, " fsys(%s)", source);
	      }

	      /* io_t */
	      {
		struct stat st;

		err = io_stat (port, &st);

		if (!err)
		  fprintf (stream, " io(%llu,%llu)",
			   (unsigned long long) st.st_ino,
			   (unsigned long long) st.st_dev);
	      }

	      /* msg */
	      {
		mach_port_t proc;

		err = msg_get_init_port (port, task, INIT_PORT_PROC, &proc);
		if (!err)
		  {
		    pid_t pid, ppid;
		    int orphaned;

		    fprintf (stream, " msg");
		    err = proc_getpids (port, &pid, &ppid, &orphaned);
		    if (!err)
		      fprintf (stream, "(%d)", pid);

		    mach_port_deallocate (mach_task_self (), proc);
		  }
	      }

	      /* pci_t */
	      {
		vm_size_t ndevs;

		err = pci_get_ndevs (port, &ndevs);

		if (!err)
		  fprintf (stream, " pci(%d)", ndevs);
	      }

	      /* process_t */
	      {
		pid_t pid, ppid;
		int orphaned;

		err = proc_getpids (port, &pid, &ppid, &orphaned);

		if (!err)
		  fprintf (stream, " proc(%d)", pid);
	      }

	      /* socket_t */
	      {
		mach_port_t addr = MACH_PORT_NULL;
		int type;
		struct sockaddr_storage sa;
		char *psa = (char*) &sa;
		mach_msg_type_number_t salen = sizeof (sa);

		err = socket_name (port, &addr);

		if (!err)
		  fprintf (stream, " socket");
		else
		  addr = port;

		err = socket_whatis_address (addr, &type, &psa, &salen);
		if (!err)
		  {
		    if (addr == port)
		      fprintf (stream, " addr_port");

		    if (type == AF_UNIX)
		      {
			struct sockaddr_un *sa_un = (struct sockaddr_un *) psa;

			fprintf (stream, "(UNIX:%s)", sa_un->sun_path);
		      }
		    else if (type == AF_INET)
		      {
			struct sockaddr_in *sa_in = (struct sockaddr_in *) psa;

			fprintf (stream, "(INET:%s:%d)",
				 inet_ntoa (sa_in->sin_addr),
				 ntohs (sa_in->sin_port));
		      }
		    else if (type == AF_INET6)
		      {
			struct sockaddr_in6 *sa_in6 = (struct sockaddr_in6 *) psa;
			char buf[INET6_ADDRSTRLEN];
			const char *s;

			s = inet_ntop (type, &sa_in6->sin6_addr, buf, sizeof (buf));
			fprintf (stream, "(INET6:%s:%d)", s,
				 ntohs (sa_in6->sin6_port));
		      }
		    else
		      fprintf (stream, "(%d)", type);
		  }
		if (psa != (char*) &sa)
		  vm_deallocate (mach_task_self (), (vm_address_t) psa, salen);

		if (addr != port)
		  mach_port_deallocate (mach_task_self (), addr);
	      }

	      /* terminal */
	      {
		string_t name;

		err = term_get_nodename (port, name);

		if (!err)
		  fprintf (stream, " term(%s)", name);
	      }

	      /* default pager */
	      {
		struct default_pager_info def_pager_info;

		err = default_pager_info (port, &def_pager_info);

		if (!err)
		  fprintf (stream, " default_pager(%llu)",
		      (unsigned long long) def_pager_info.dpi_total_space);
	      }

	      /* host_t */
	      {
		struct host_basic_info info;
		mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;

		err = host_info (port, HOST_BASIC_INFO,
		    (host_info_t) &info, &count);

		if (!err)
		  fprintf (stream, " host");
		if (port == mach_host_self ())
		  fprintf (stream, "(self)");
	      }

	      /* memory_object */
	      {
		boolean_t ready, may_cache;
		memory_object_copy_strategy_t strategy;

		err = memory_object_get_attributes (port, &ready, &may_cache, &strategy);

		if (!err)
		  fprintf (stream, " memory_object");
	      }

	      /* processor_t */
	      {
		struct processor_basic_info info;
		host_t host;
		mach_msg_type_number_t count = PROCESSOR_BASIC_INFO_COUNT;

		err = processor_info (port, PROCESSOR_BASIC_INFO,
		    &host, (processor_info_t) &info, &count);

		if (!err)
		  {
		    fprintf (stream, " processor(%d)", info.slot_num);
		    mach_port_deallocate (mach_task_self (), host);
		  }
	      }

	      /* task_t */
	      {
		pid_t pid;

		err = proc_task2pid (proc, port, &pid);
		if (!err)
		  fprintf (stream, " task(%d)", pid);
		if (port == task)
		  {
		    if (err)
		      fprintf (stream, " task");
		    fprintf (stream, "(self)");
		  }
	      }

	      /* thread_t */
	      {
		struct thread_basic_info info;
		mach_msg_type_number_t count = THREAD_BASIC_INFO_COUNT;

		err = thread_info (port, THREAD_BASIC_INFO,
				   (thread_info_t) &info, &count);

		if (!err)
		  fprintf (stream, " thread");
	      }
	    }

	  mach_port_deallocate (mach_task_self (), proc);
	  mach_port_deallocate (mach_task_self (), port);
	}

      if (type & MACH_PORT_TYPE_RECEIVE)
	{
	  struct mach_port_status status;
	  error_t err = mach_port_get_receive_status (task, name, &status);
	  if (! err)
	    {
	      fprintf (stream, " (");
	      if (status.mps_pset != MACH_PORT_NULL)
		fprintf (stream,
			 hex_names ? "port-set: %#x, " : "port-set: %u, ",
			 status.mps_pset);
	      fprintf (stream, "seqno: %zu", status.mps_seqno);
	      if (status.mps_mscount)
		fprintf (stream, ", ms-count: %zu", status.mps_mscount);
	      if (status.mps_qlimit != MACH_PORT_QLIMIT_DEFAULT)
		fprintf (stream, ", qlimit: %zu", status.mps_qlimit);
	      if (status.mps_msgcount)
		fprintf (stream, ", msgs: %zu", status.mps_msgcount);
	      fprintf (stream, "%s%s%s)",
		       status.mps_srights ? ", send-rights" : "",
		       status.mps_pdrequest ? ", pd-req" : "",
		       status.mps_nsrequest ? ", ns-req" : "");
	    }
	}

      if (type & MACH_PORT_TYPE_SEND)
	prefs (MACH_PORT_RIGHT_SEND);

      if (type & MACH_PORT_TYPE_DEAD_NAME)
	prefs (MACH_PORT_RIGHT_DEAD_NAME);

      if (type & MACH_PORT_TYPE_PORT_SET)
	{
	  mach_port_t *members = 0;
	  mach_msg_type_number_t members_len = 0, i;
	  error_t err =
	    mach_port_get_set_status (task, name, &members, &members_len);
	  if (! err)
	    {
	      if (members_len == 0)
		fprintf (stream, " (empty)");
	      else
		{
		  fprintf (stream, hex_names ? " (%#x" : " (%u", members[0]);
		  for (i = 1; i < members_len; i++)
		    fprintf (stream, hex_names ? ", %#x" : ", %u",
			     members[i]);
		  fprintf (stream, ")");
		  munmap ((caddr_t) members, members_len * sizeof *members);
		}
	    }
	}
    }

  putc ('\n', stream);

  return 0;
}

/* Prints info about every port in TASK that has a type in ONLY to STREAM. */
error_t
print_task_ports_info (task_t task, mach_port_type_t only,
		       unsigned show, FILE *stream)
{
  mach_port_t *names = 0;
  mach_port_type_t *types = 0;
  mach_msg_type_number_t names_len = 0, types_len = 0, i;
  error_t err = mach_port_names (task, &names, &names_len, &types, &types_len);

  if (err)
    return err;

  for (i = 0; i < names_len; i++)
    if (types[i] & only)
      print_port_info (names[i], types[i], task, show, stream);

  munmap ((caddr_t) names, names_len * sizeof *names);
  munmap ((caddr_t) types, types_len * sizeof *types);

  return 0;
}