summaryrefslogtreecommitdiff
path: root/sysdeps/solaris/procdata.c
blob: a4a2bb87de0bb9f63c1df07021ff2f70353f1fd3 (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
/* 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 <glibtop.h>
#include <glibtop_private.h>
#include <glibtop/procuid.h>
#include <glibtop/error.h>

#include <errno.h>

#include "safeio.h"

/*
 * The differences between old and new procfs API are:
 * - old has /proc/<pid> file and ioctl() is used to obtain the data.
 * - new has /proc/<pid>/* files and read() & friends are used to
 *   obtain the data.
 * - names of structures and members are different. It's mostly the
 *   prefix. Old uses `pr' and new uses `ps'.
 *
 * Since almost every line would be in #ifdef, I think it would
 * be more readable to just copy everything twice. This is not a big
 * file, after all.
 */

#ifdef HAVE_PROCFS_H

/* Read /proc/<pid>/psinfo. */

int
glibtop_get_proc_data_psinfo_s (glibtop *server, struct psinfo *psinfo, pid_t pid)
{
	int fd;
	char buffer [BUFSIZ];

	sprintf (buffer, "/proc/%d/psinfo", (int) pid);

	fd = s_open (buffer, O_RDONLY);

	if (fd < 0) {
		glibtop_warn_io_r (server, "open (%s)", buffer);
		return -1;
	}

	if (s_pread (fd, psinfo, sizeof (struct psinfo), 0)
	    != sizeof (struct psinfo))
	{
		s_close (fd);
		glibtop_warn_io_r (server, "pread (%s)", buffer);
		return -1;
	}

	s_close (fd);
	return 0;
}

int
glibtop_get_proc_data_usage_s (glibtop *server, struct prusage *prusage, pid_t pid)
{
	int fd;
	char buffer [BUFSIZ];

	sprintf (buffer, "/proc/%d/usage", (int) pid);

	fd = s_open (buffer, O_RDONLY);

	if (fd < 0) {
		glibtop_warn_io_r (server, "open (%s)", buffer);
		return -1;
	}

	if (s_pread (fd, prusage, sizeof (struct prusage), 0)
	    != sizeof (struct prusage))
	{
		s_close (fd);
		glibtop_warn_io_r (server, "pread (%s)", buffer);
		return -1;
	}

	s_close (fd);
	return 0;
}

#if LIBGTOP_VERSION_CODE >= 1001002
int
glibtop_get_proc_credentials_s(glibtop *server,
			       struct prcred *prcred,
			       gid_t *groups,
			       pid_t pid)
{
	int fd;
	size_t toread;
	char buffer[BUFSIZ];

	sprintf(buffer, "/proc/%d/cred", (int)pid);

	if((fd = s_open(buffer, O_RDONLY)) < 0)
	{
		if(errno != EPERM && errno != EACCES)
			glibtop_warn_io_r(server, "open (%s)", buffer);
		return -1;
	}

	if(s_pread(fd, prcred, sizeof(struct prcred), 0)
	   != sizeof(struct prcred))
	{
		s_close(fd);
		glibtop_warn_io_r(server, "pread (%s)", buffer);
		return -1;
	}

	if(prcred->pr_ngroups >= 0)
	{
		toread = MIN(prcred->pr_ngroups, GLIBTOP_MAX_GROUPS) * sizeof(gid_t);

		if((size_t) s_pread(fd,groups, toread,
				    G_STRUCT_OFFSET(struct prcred, pr_groups))
		   != toread)
			prcred->pr_ngroups = 0;
	}
	s_close(fd);
	return 0;
}
#endif /* LIBGTOP_VERSION_CODE >= 1001002 */

int
glibtop_get_proc_status_s(glibtop *server, struct pstatus *pstatus, pid_t pid)
{
	int fd;
	char buffer[BUFSIZ];

	sprintf(buffer, "/proc/%d/status", (int)pid);
	if((fd = s_open(buffer, O_RDONLY)) < 0)
	{
		if(errno != EPERM && errno != EACCES)
			glibtop_warn_io_r(server, "open (%s)", buffer);
		return -1;
	}
	if(s_pread(fd, pstatus, sizeof(struct pstatus), 0)
	   != sizeof(struct pstatus))
	{
		s_close(fd);
		glibtop_warn_io_r(server, "pread (%s)", buffer);
		return -1;
	}
	s_close(fd);
	return 0;
}

#else /* old API */

int
glibtop_get_proc_data_psinfo_s (glibtop *server,
				struct prpsinfo *psinfo,
				pid_t pid)
{
	int fd;
	char buffer [BUFSIZ];

	sprintf (buffer, "/proc/%d", (int) pid);
	fd = s_open (buffer, O_RDONLY);
	if (fd < 0) {
		if(errno != EPERM && errno != EACCES)
			glibtop_warn_io_r (server, "open (%s)", buffer);
		return -1;
	}

	if(ioctl(fd, PIOCPSINFO, psinfo) < 0)
	{
		s_close (fd);
		glibtop_warn_io_r (server, "ioctl(%s, PIOCPSINFO)", buffer);
		return -1;
	}

	s_close (fd);
	return 0;
}

int
glibtop_get_proc_data_usage_s (glibtop *server,
			       struct prusage *prusage,
			       pid_t pid)
{
	int fd;
	char buffer [BUFSIZ];

	sprintf (buffer, "/proc/%d", (int) pid);
	fd = s_open (buffer, O_RDONLY);
	if (fd < 0) {
		if(errno != EPERM && errno != EACCES)
			glibtop_warn_io_r (server, "open (%s)", buffer);
		return -1;
	}

	if(ioctl(fd, PIOCUSAGE, prusage) < 0)
	{
		s_close (fd);
		glibtop_warn_io_r (server, "ioctl(%s, PIOCUSAGE)", buffer);
		return -1;
	}

	s_close (fd);
	return 0;
}

int
glibtop_get_proc_credentials_s(glibtop *server,
			       struct prcred *prcred,
			       gid_t *groups,
			       pid_t pid)
{
	int fd;
	size_t toread;
	char buffer[BUFSIZ];

	sprintf(buffer, "/proc/%d", (int)pid);
	if((fd = s_open(buffer, O_RDONLY)) < 0)
	{
		if(errno != EPERM && errno != EACCES)
			glibtop_warn_io_r(server, "open (%s)", buffer);
		return -1;
	}
	if(ioctl(fd, PIOCCRED, prcred) < 0)
	{
		s_close(fd);
		glibtop_warn_io_r(server, "ioctl(%s, PIOCCRED)", buffer);
		return -1;
	}
	s_close(fd);
	return 0;
}

int
glibtop_get_proc_status_s(glibtop *server, struct prstatus *pstatus, pid_t pid)
{
	int fd;
	char buffer[BUFSIZ];

	sprintf(buffer, "/proc/%d", (int)pid);
	if((fd = s_open(buffer, O_RDONLY)) < 0)
	{
		if(errno != EPERM && errno != EACCES)
			glibtop_warn_io_r(server, "open (%s)", buffer);
		return -1;
	}
	if(ioctl(fd, PIOCSTATUS, pstatus) < 0)
	{
		s_close(fd);
		glibtop_warn_io_r(server, "ioctl(%s, PIOCSTATUS)", buffer);
		return -1;
	}
	s_close(fd);
	return 0;
}
#endif