summaryrefslogtreecommitdiff
path: root/usr/src/cmd/prstat/prtable.c
blob: e144ef7ff8cd78511a44a2791fe31a085374f272 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2013 Gary Mills
 *
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Portions Copyright 2009 Chad Mynhier
 */

#include <procfs.h>
#include <unistd.h>
#include <stdlib.h>
#include <pwd.h>
#include <ctype.h>
#include <string.h>
#include <libintl.h>
#include <errno.h>
#include <zone.h>
#include <libzonecfg.h>
#include <wchar.h>

#include "prstat.h"
#include "prutil.h"
#include "prtable.h"

static plwp_t	*plwp_tbl[PLWP_TBL_SZ];

void
lwpid_init()
{
	(void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ);
}

static uid_t
pwd_getid(char *name)
{
	struct passwd *pwd;

	if ((pwd = getpwnam(name)) == NULL)
		Die(gettext("invalid user name: %s\n"), name);
	return (pwd->pw_uid);
}

void
pwd_getname(uid_t uid, char *name, size_t length, int noresolve,
    int trunc, size_t width)
{
	struct passwd *pwd;
	size_t n;

	if (noresolve || (pwd = getpwuid(uid)) == NULL) {
		n = snprintf(NULL, 0, "%u", uid);
		if (trunc && n > width)
			(void) snprintf(name, length, "%.*u%c",
			    width - 1, uid, '*');
		else
			(void) snprintf(name, length, "%u", uid);
	} else {
		n = mbstowcs(NULL, pwd->pw_name, 0);
		if (n == (size_t)-1)
			(void) snprintf(name, length, "%s", "ERROR");
		else if (trunc && n > width)
			(void) snprintf(name, length, "%.*s%c",
			    width - 1, pwd->pw_name, '*');
		else
			(void) snprintf(name, length, "%s", pwd->pw_name);
	}
}

void
add_uid(uidtbl_t *tbl, char *name)
{
	uid_t *uid;

	if (tbl->n_size == tbl->n_nent) {	/* reallocation */
		if ((tbl->n_size *= 2) == 0)
			tbl->n_size = 4;	/* first time */
		tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t));
	}

	uid = &tbl->n_list[tbl->n_nent++];

	if (isdigit(name[0])) {
		*uid = Atoi(name);
	} else {
		*uid = pwd_getid(name);
	}
}

int
has_uid(uidtbl_t *tbl, uid_t uid)
{
	size_t i;

	if (tbl->n_nent) {	/* do linear search if table is not empty */
		for (i = 0; i < tbl->n_nent; i++)
			if (tbl->n_list[i] == uid)
				return (1);
	} else {
		return (1);	/* if table is empty return true */
	}

	return (0);		/* nothing has been found */
}

void
add_element(table_t *table, long element)
{
	if (table->t_size == table->t_nent) {
		if ((table->t_size *= 2) == 0)
			table->t_size = 4;
		table->t_list = Realloc(table->t_list,
		    table->t_size * sizeof (long));
	}
	table->t_list[table->t_nent++] = element;
}

int
has_element(table_t *table, long element)
{
	size_t i;

	if (table->t_nent) {	/* do linear search if table is not empty */
		for (i = 0; i < table->t_nent; i++)
			if (table->t_list[i] == element)
				return (1);
	} else {		/* if table is empty then */
		return (1);	/* pretend that element was found */
	}

	return (0);	/* element was not found */
}

int
foreach_element(table_t *table, void *buf, void (*walker)(long, void *))
{
	size_t i;

	if (table->t_nent) {
		for (i = 0; i < table->t_nent; i++)
			walker(table->t_list[i], buf);
	} else {
		return (0);
	}
	return (1);
}

void
add_zone(zonetbl_t *tbl, char *str)
{
	zonename_t *entp;
	zoneid_t id;
	char *cp;

	/*
	 * str should be either the name of a configured zone, or the
	 * id of a running zone.  If str is a zone name, store the name
	 * in the table; otherwise, just store the id.
	 */
	if (zone_get_id(str, &id) != 0) {
		Die(gettext("unknown zone -- %s\n"), str);
		/*NOTREACHED*/
	}

	/* was zone specified by name or id? */
	errno = 0;
	if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str &&
	    *cp == '\0') {
		/* found it by id, don't store the name */
		str = NULL;
	}

	if (tbl->z_size == tbl->z_nent) {	/* reallocation */
		if ((tbl->z_size *= 2) == 0)
			tbl->z_size = 4;	/* first time */
		tbl->z_list =
		    Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t));
	}

	entp = &tbl->z_list[tbl->z_nent++];
	if (str)
		(void) strlcpy(entp->z_name, str, ZONENAME_MAX);
	else
		entp->z_name[0] = '\0';
	entp->z_id = id;
}

int
has_zone(zonetbl_t *tbl, zoneid_t id)
{
	long i;

	if (tbl->z_nent) {	/* do linear search if table is not empty */
		for (i = 0; i < tbl->z_nent; i++)
			if (tbl->z_list[i].z_id == id)
				return (1);
		return (0);	/* nothing has been found */
	}

	return (1);	/* if table is empty return true */
}

/*
 * Lookup ids for each zone name; this is done once each time /proc
 * is scanned to avoid calling getzoneidbyname for each process.
 */
void
convert_zone(zonetbl_t *tbl)
{
	long i;
	zoneid_t id;
	char *name;

	for (i = 0; i < tbl->z_nent; i++) {
		name = tbl->z_list[i].z_name;
		if (name != NULL) {
			if ((id = getzoneidbyname(name)) != -1)
				tbl->z_list[i].z_id = id;
		}
	}
}

void
lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid)
{
	plwp_t *elm = Zalloc(sizeof (plwp_t));
	int hash = pid % PLWP_TBL_SZ;

	elm->l_pid = pid;
	elm->l_lwpid = lwpid;
	elm->l_lwp = lwp;
	elm->l_next = plwp_tbl[hash]; /* add in front of chain */
	plwp_tbl[hash] = elm;
}

void
lwpid_del(pid_t pid, id_t lwpid)
{
	plwp_t *elm, *elm_prev;
	int hash = pid % PLWP_TBL_SZ;

	elm = plwp_tbl[hash];
	elm_prev = NULL;

	while (elm) {
		if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) {
			if (!elm_prev)	/* first chain element */
				plwp_tbl[hash] = elm->l_next;
			else
				elm_prev->l_next = elm->l_next;
			free(elm);
			break;
		} else {
			elm_prev = elm;
			elm = elm->l_next;
		}
	}
}

static plwp_t *
lwpid_getptr(pid_t pid, id_t lwpid)
{
	plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ];
	while (elm) {
		if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid))
			return (elm);
		else
			elm = elm->l_next;
	}
	return (NULL);
}

lwp_info_t *
lwpid_get(pid_t pid, id_t lwpid)
{
	plwp_t *elm = lwpid_getptr(pid, lwpid);
	if (elm)
		return (elm->l_lwp);
	else
		return (NULL);
}

int
lwpid_pidcheck(pid_t pid)
{
	plwp_t *elm;
	elm = plwp_tbl[pid % PLWP_TBL_SZ];
	while (elm) {
		if (elm->l_pid == pid)
			return (1);
		else
			elm = elm->l_next;
	}
	return (0);
}

int
lwpid_is_active(pid_t pid, id_t lwpid)
{
	plwp_t *elm = lwpid_getptr(pid, lwpid);
	if (elm)
		return (elm->l_active);
	else
		return (0);
}

void
lwpid_set_active(pid_t pid, id_t lwpid)
{
	plwp_t *elm = lwpid_getptr(pid, lwpid);
	if (elm)
		elm->l_active = LWP_ACTIVE;
}