summaryrefslogtreecommitdiff
path: root/sysdeps/aix/utils.c
blob: 71e482cc145320fce6cbc3158c12ebe3e0b02afb (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
/* $Id$ */

/* Copyright (C) 1998-99 Martin Baulig
   This file is part of LibGTop 1.0.

   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.

   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., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>
#include <nlist.h>

#include <procinfo.h>
#include <sys/proc.h>
#include <sys/vminfo.h>

#include <glibtop.h>
#include <glibtop/error.h>
#include "glibtop_suid.h"
#include "utils.h"

#ifndef HAVE_VMGETINFO
#include <dlfcn.h>
typedef int (*vmgetinfo_proto)(void *out, int command, int arg);
#endif

off_t
_glibtop_get_kmem_offset(glibtop* server, char* kname)
{
	int result;
	struct nlist kernelnames[] =
		{ {NULL, 0, 0, 0, 0, 0},
		  {NULL, 0, 0, 0, 0, 0},
		};

	kernelnames[0]._n._n_name = kname;

	glibtop_suid_enter(server);

	result = knlist(kernelnames, 1, sizeof(struct nlist));

	glibtop_suid_leave(server);

	if (result == -1)
	{
		return -1;
	}

	return kernelnames[0].n_value;
}

int
_glibtop_get_kmem_info(glibtop* server, off_t offset, void* buf, size_t len)
{
	int result;

	glibtop_suid_enter(server);

	lseek(server->machine.kmem_fd, offset, SEEK_SET);

	result = read(server->machine.kmem_fd, buf, len);

        glibtop_suid_leave(server);

	return result;
}

struct procsinfo*
_glibtop_get_procinfo (glibtop *server, pid_t pid)
{
	int result;
	pid_t current;
	static int first_time = 1;

	/* test if procsinfo already found */

	if ((server->machine.last_pinfo.pi_pid == pid) && (!first_time))
	{
		return &server->machine.last_pinfo;
	}

	/* seek procsinfo if given pid */

	first_time = 0;
	current = 0;
	while ((result =  getprocs( &server->machine.last_pinfo
				  , sizeof(struct procsinfo)
				  , NULL, 0, &current, 1)) == 1)
	{

		if (pid == server->machine.last_pinfo.pi_pid)
		{
			return &server->machine.last_pinfo;
		}
	}
	return NULL;
}

#ifndef HAVE_VMGETINFO
int
_glibtop_vmgetinfo (void *out, int command, int arg)
{
	void* handle;

	static vmgetinfo_proto kern_vmgetinfo = NULL;

	if (kern_vmgetinfo == NULL)
	{
		handle = dlopen("/unix", RTLD_NOW | RTLD_GLOBAL);
		if (handle == NULL)
		{
			return -1;
		}

		kern_vmgetinfo = dlsym( handle, "vmgetinfo");

		dlclose(handle);

		if (kern_vmgetinfo == NULL)
		{
			return -1;
		}
	}

	return kern_vmgetinfo(out, command, arg);
}
#endif