File: procmap.c

package info (click to toggle)
libgtop2 2.41.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,176 kB
  • sloc: ansic: 25,849; sh: 7,327; makefile: 408; perl: 268; xml: 179; sed: 16
file content (134 lines) | stat: -rw-r--r-- 3,670 bytes parent folder | download | duplicates (5)
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
/*
   This file is part of LibGTop 2.0.

   LibGTop 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 of the License,
   or (at your option) any later version.

   LibGTop 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 LibGTop; see the file COPYING. If not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <config.h>
#include <unistd.h>

#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procmap.h>

#include <glibtop_suid.h>

#include <mach/mach_init.h>
#include <mach/mach_host.h>
#include <mach/vm_map.h>

static const unsigned long _glibtop_sysdeps_proc_map =
(1L << GLIBTOP_PROC_MAP_TOTAL) + (1L << GLIBTOP_PROC_MAP_NUMBER) +
(1L << GLIBTOP_PROC_MAP_SIZE);

static const unsigned long _glibtop_sysdeps_proc_map_entry =
(1L << GLIBTOP_MAP_ENTRY_START) + (1L << GLIBTOP_MAP_ENTRY_END) +
(1L << GLIBTOP_MAP_ENTRY_OFFSET) + (1L << GLIBTOP_MAP_ENTRY_PERM);

/* Init function. */

void
_glibtop_init_proc_map_p (glibtop *server)
{
	server->sysdeps.proc_map = _glibtop_sysdeps_proc_map;
}

/* Provides detailed information about a process. */

glibtop_map_entry *
glibtop_get_proc_map_p (glibtop *server, glibtop_proc_map *buf,	pid_t pid)
{
	glibtop_map_entry *maps;
	mach_port_t task;
	vm_address_t address;
	natural_t nesting_depth;
	int n;

	glibtop_init_p (server, (1 << GLIBTOP_SYSDEPS_PROC_MAP), 0);

	memset (buf, 0, sizeof (glibtop_proc_map));

	/* !!! THE FOLLOWING CODE RUNS SUID ROOT - CHANGE WITH CAUTION !!! */

	glibtop_suid_enter (server);

	if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS) {
		glibtop_suid_leave (server);
		glibtop_warn_io_r (server, "task_for_pid (procmap)");
		return NULL;
	}

	maps = NULL;
	n = 0;
	address = 0;
	nesting_depth = 0;
	while (1) {
		vm_region_submap_info_data_64_t info;
		mach_msg_type_number_t info_count;
		vm_size_t size;

		info_count = VM_REGION_SUBMAP_INFO_COUNT_64;
		if (vm_region_recurse_64 (task, &address, &size, &nesting_depth,
			(vm_region_recurse_info_64_t)&info, &info_count))
			break;

		if (info.is_submap) {
			nesting_depth++;
			continue;
		}

		maps = g_realloc (maps, (n + 1) * sizeof (glibtop_map_entry));
		if (!maps) {
			glibtop_suid_leave (server);
			return NULL;
		}

		memset (maps + n, 0, sizeof (glibtop_map_entry));

		maps[n].start	= address;
		maps[n].end	= address + size - 1;
		maps[n].offset	= info.offset;
		if (info.protection & VM_PROT_READ)
			maps[n].perm |= GLIBTOP_MAP_PERM_READ;
		if (info.protection & VM_PROT_WRITE) 
			maps[n].perm |= GLIBTOP_MAP_PERM_WRITE;
		if (info.protection & VM_PROT_EXECUTE)      
			maps[n].perm |= GLIBTOP_MAP_PERM_EXECUTE;
		if (info.share_mode == SM_PRIVATE ||
		    info.share_mode == SM_PRIVATE_ALIASED)
			maps[n].perm |= GLIBTOP_MAP_PERM_PRIVATE;
		if (info.share_mode == SM_SHARED ||
		    info.share_mode == SM_TRUESHARED ||
		    info.share_mode == SM_SHARED_ALIASED)
			maps[n].perm |= GLIBTOP_MAP_PERM_SHARED;

		maps[n].flags	= _glibtop_sysdeps_proc_map_entry;

		address += size;
		n++;
	}

	glibtop_suid_leave (server);

	/* !!! END OF SUID ROOT PART !!! */

	buf->number	= n;
	buf->size	= sizeof (glibtop_map_entry);
	buf->total	= n * sizeof (glibtop_map_entry);
	buf->flags	= _glibtop_sysdeps_proc_map;

	return maps;
}