xref: /titanic_53/usr/src/cmd/prstat/prtable.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <procfs.h>
30*7c478bd9Sstevel@tonic-gate #include <unistd.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <pwd.h>
33*7c478bd9Sstevel@tonic-gate #include <ctype.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <libintl.h>
36*7c478bd9Sstevel@tonic-gate #include <errno.h>
37*7c478bd9Sstevel@tonic-gate #include <zone.h>
38*7c478bd9Sstevel@tonic-gate #include <libzonecfg.h>
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate #include "prstat.h"
41*7c478bd9Sstevel@tonic-gate #include "prutil.h"
42*7c478bd9Sstevel@tonic-gate #include "prtable.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate static plwp_t	*plwp_tbl[PLWP_TBL_SZ];
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate void
47*7c478bd9Sstevel@tonic-gate lwpid_init()
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	(void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ);
50*7c478bd9Sstevel@tonic-gate }
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate static int
53*7c478bd9Sstevel@tonic-gate pwd_getid(char *name)
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate 	if ((pwd = getpwnam(name)) == NULL)
58*7c478bd9Sstevel@tonic-gate 		Die(gettext("invalid user name: %s\n"), name);
59*7c478bd9Sstevel@tonic-gate 	return (pwd->pw_uid);
60*7c478bd9Sstevel@tonic-gate }
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate void
63*7c478bd9Sstevel@tonic-gate pwd_getname(int uid, char *name, int length)
64*7c478bd9Sstevel@tonic-gate {
65*7c478bd9Sstevel@tonic-gate 	struct passwd *pwd;
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	if ((pwd = getpwuid(uid)) == NULL) {
68*7c478bd9Sstevel@tonic-gate 		(void) snprintf(name, length, "%d", uid);
69*7c478bd9Sstevel@tonic-gate 	} else {
70*7c478bd9Sstevel@tonic-gate 		(void) snprintf(name, length, "%s", pwd->pw_name);
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate void
75*7c478bd9Sstevel@tonic-gate add_uid(nametbl_t *tbl, char *name)
76*7c478bd9Sstevel@tonic-gate {
77*7c478bd9Sstevel@tonic-gate 	name_t *entp;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (tbl->n_size == tbl->n_nent) {	/* reallocation */
80*7c478bd9Sstevel@tonic-gate 		if ((tbl->n_size *= 2) == 0)
81*7c478bd9Sstevel@tonic-gate 			tbl->n_size = 4;	/* first time */
82*7c478bd9Sstevel@tonic-gate 		tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (name_t));
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	entp = &tbl->n_list[tbl->n_nent++];
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate 	if (isdigit(name[0])) {
88*7c478bd9Sstevel@tonic-gate 		entp->u_id = Atoi(name);
89*7c478bd9Sstevel@tonic-gate 		pwd_getname(entp->u_id, entp->u_name, LOGNAME_MAX);
90*7c478bd9Sstevel@tonic-gate 	} else {
91*7c478bd9Sstevel@tonic-gate 		entp->u_id = pwd_getid(name);
92*7c478bd9Sstevel@tonic-gate 		(void) snprintf(entp->u_name, LOGNAME_MAX, "%s", name);
93*7c478bd9Sstevel@tonic-gate 	}
94*7c478bd9Sstevel@tonic-gate }
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate int
97*7c478bd9Sstevel@tonic-gate has_uid(nametbl_t *tbl, uid_t uid)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	size_t i;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (tbl->n_nent) {	/* do linear search if table is not empty */
102*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < tbl->n_nent; i++)
103*7c478bd9Sstevel@tonic-gate 			if (tbl->n_list[i].u_id == uid)
104*7c478bd9Sstevel@tonic-gate 				return (1);
105*7c478bd9Sstevel@tonic-gate 	} else {
106*7c478bd9Sstevel@tonic-gate 		return (1);	/* if table is empty return true */
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	return (0);		/* nothing has been found */
110*7c478bd9Sstevel@tonic-gate }
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate void
113*7c478bd9Sstevel@tonic-gate add_element(table_t *table, long element)
114*7c478bd9Sstevel@tonic-gate {
115*7c478bd9Sstevel@tonic-gate 	if (table->t_size == table->t_nent) {
116*7c478bd9Sstevel@tonic-gate 		if ((table->t_size *= 2) == 0)
117*7c478bd9Sstevel@tonic-gate 			table->t_size = 4;
118*7c478bd9Sstevel@tonic-gate 		table->t_list = Realloc(table->t_list,
119*7c478bd9Sstevel@tonic-gate 		    table->t_size * sizeof (long));
120*7c478bd9Sstevel@tonic-gate 	}
121*7c478bd9Sstevel@tonic-gate 	table->t_list[table->t_nent++] = element;
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate int
125*7c478bd9Sstevel@tonic-gate has_element(table_t *table, long element)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	size_t i;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if (table->t_nent) {	/* do linear search if table is not empty */
130*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < table->t_nent; i++)
131*7c478bd9Sstevel@tonic-gate 			if (table->t_list[i] == element)
132*7c478bd9Sstevel@tonic-gate 				return (1);
133*7c478bd9Sstevel@tonic-gate 	} else {		/* if table is empty then */
134*7c478bd9Sstevel@tonic-gate 		return (1);	/* pretend that element was found */
135*7c478bd9Sstevel@tonic-gate 	}
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	return (0);	/* element was not found */
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate int
141*7c478bd9Sstevel@tonic-gate foreach_element(table_t *table, void *buf, void (*walker)(long, void *))
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	size_t i;
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 	if (table->t_nent) {
146*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < table->t_nent; i++)
147*7c478bd9Sstevel@tonic-gate 			walker(table->t_list[i], buf);
148*7c478bd9Sstevel@tonic-gate 	} else {
149*7c478bd9Sstevel@tonic-gate 		return (0);
150*7c478bd9Sstevel@tonic-gate 	}
151*7c478bd9Sstevel@tonic-gate 	return (1);
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate void
155*7c478bd9Sstevel@tonic-gate add_zone(zonetbl_t *tbl, char *str)
156*7c478bd9Sstevel@tonic-gate {
157*7c478bd9Sstevel@tonic-gate 	zonename_t *entp;
158*7c478bd9Sstevel@tonic-gate 	zoneid_t id;
159*7c478bd9Sstevel@tonic-gate 	char *cp;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	/*
162*7c478bd9Sstevel@tonic-gate 	 * str should be either the name of a configured zone, or the
163*7c478bd9Sstevel@tonic-gate 	 * id of a running zone.  If str is a zone name, store the name
164*7c478bd9Sstevel@tonic-gate 	 * in the table; otherwise, just store the id.
165*7c478bd9Sstevel@tonic-gate 	 */
166*7c478bd9Sstevel@tonic-gate 	if (zone_get_id(str, &id) != 0) {
167*7c478bd9Sstevel@tonic-gate 		Die(gettext("unknown zone -- %s\n"), str);
168*7c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
169*7c478bd9Sstevel@tonic-gate 	}
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	/* was zone specified by name or id? */
172*7c478bd9Sstevel@tonic-gate 	errno = 0;
173*7c478bd9Sstevel@tonic-gate 	if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str &&
174*7c478bd9Sstevel@tonic-gate 	    *cp == '\0') {
175*7c478bd9Sstevel@tonic-gate 		/* found it by id, don't store the name */
176*7c478bd9Sstevel@tonic-gate 		str = NULL;
177*7c478bd9Sstevel@tonic-gate 	}
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	if (tbl->z_size == tbl->z_nent) {	/* reallocation */
180*7c478bd9Sstevel@tonic-gate 		if ((tbl->z_size *= 2) == 0)
181*7c478bd9Sstevel@tonic-gate 			tbl->z_size = 4;	/* first time */
182*7c478bd9Sstevel@tonic-gate 		tbl->z_list =
183*7c478bd9Sstevel@tonic-gate 		    Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t));
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	entp = &tbl->z_list[tbl->z_nent++];
187*7c478bd9Sstevel@tonic-gate 	if (str)
188*7c478bd9Sstevel@tonic-gate 		(void) strlcpy(entp->z_name, str, ZONENAME_MAX);
189*7c478bd9Sstevel@tonic-gate 	else
190*7c478bd9Sstevel@tonic-gate 		entp->z_name[0] = '\0';
191*7c478bd9Sstevel@tonic-gate 	entp->z_id = id;
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate int
195*7c478bd9Sstevel@tonic-gate has_zone(zonetbl_t *tbl, zoneid_t id)
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	long i;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	if (tbl->z_nent) {	/* do linear search if table is not empty */
200*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < tbl->z_nent; i++)
201*7c478bd9Sstevel@tonic-gate 			if (tbl->z_list[i].z_id == id)
202*7c478bd9Sstevel@tonic-gate 				return (1);
203*7c478bd9Sstevel@tonic-gate 		return (0);	/* nothing has been found */
204*7c478bd9Sstevel@tonic-gate 	}
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	return (1);	/* if table is empty return true */
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate /*
210*7c478bd9Sstevel@tonic-gate  * Lookup ids for each zone name; this is done once each time /proc
211*7c478bd9Sstevel@tonic-gate  * is scanned to avoid calling getzoneidbyname for each process.
212*7c478bd9Sstevel@tonic-gate  */
213*7c478bd9Sstevel@tonic-gate void
214*7c478bd9Sstevel@tonic-gate convert_zone(zonetbl_t *tbl)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	long i;
217*7c478bd9Sstevel@tonic-gate 	zoneid_t id;
218*7c478bd9Sstevel@tonic-gate 	char *name;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < tbl->z_nent; i++) {
221*7c478bd9Sstevel@tonic-gate 		name = tbl->z_list[i].z_name;
222*7c478bd9Sstevel@tonic-gate 		if (name != NULL) {
223*7c478bd9Sstevel@tonic-gate 			if ((id = getzoneidbyname(name)) != -1)
224*7c478bd9Sstevel@tonic-gate 				tbl->z_list[i].z_id = id;
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate }
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate void
230*7c478bd9Sstevel@tonic-gate lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid)
231*7c478bd9Sstevel@tonic-gate {
232*7c478bd9Sstevel@tonic-gate 	plwp_t *elm = Zalloc(sizeof (plwp_t));
233*7c478bd9Sstevel@tonic-gate 	int hash = pid % PLWP_TBL_SZ;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 	elm->l_pid = pid;
236*7c478bd9Sstevel@tonic-gate 	elm->l_lwpid = lwpid;
237*7c478bd9Sstevel@tonic-gate 	elm->l_lwp = lwp;
238*7c478bd9Sstevel@tonic-gate 	elm->l_next = plwp_tbl[hash]; /* add in front of chain */
239*7c478bd9Sstevel@tonic-gate 	plwp_tbl[hash] = elm;
240*7c478bd9Sstevel@tonic-gate }
241*7c478bd9Sstevel@tonic-gate 
242*7c478bd9Sstevel@tonic-gate void
243*7c478bd9Sstevel@tonic-gate lwpid_del(pid_t pid, id_t lwpid)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	plwp_t *elm, *elm_prev;
246*7c478bd9Sstevel@tonic-gate 	int hash = pid % PLWP_TBL_SZ;
247*7c478bd9Sstevel@tonic-gate 
248*7c478bd9Sstevel@tonic-gate 	elm = plwp_tbl[hash];
249*7c478bd9Sstevel@tonic-gate 	elm_prev = NULL;
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 	while (elm) {
252*7c478bd9Sstevel@tonic-gate 		if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) {
253*7c478bd9Sstevel@tonic-gate 			if (!elm_prev)	/* first chain element */
254*7c478bd9Sstevel@tonic-gate 				plwp_tbl[hash] = elm->l_next;
255*7c478bd9Sstevel@tonic-gate 			else
256*7c478bd9Sstevel@tonic-gate 				elm_prev->l_next = elm->l_next;
257*7c478bd9Sstevel@tonic-gate 			free(elm);
258*7c478bd9Sstevel@tonic-gate 			break;
259*7c478bd9Sstevel@tonic-gate 		} else {
260*7c478bd9Sstevel@tonic-gate 			elm_prev = elm;
261*7c478bd9Sstevel@tonic-gate 			elm = elm->l_next;
262*7c478bd9Sstevel@tonic-gate 		}
263*7c478bd9Sstevel@tonic-gate 	}
264*7c478bd9Sstevel@tonic-gate }
265*7c478bd9Sstevel@tonic-gate 
266*7c478bd9Sstevel@tonic-gate static plwp_t *
267*7c478bd9Sstevel@tonic-gate lwpid_getptr(pid_t pid, id_t lwpid)
268*7c478bd9Sstevel@tonic-gate {
269*7c478bd9Sstevel@tonic-gate 	plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ];
270*7c478bd9Sstevel@tonic-gate 	while (elm) {
271*7c478bd9Sstevel@tonic-gate 		if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid))
272*7c478bd9Sstevel@tonic-gate 			return (elm);
273*7c478bd9Sstevel@tonic-gate 		else
274*7c478bd9Sstevel@tonic-gate 			elm = elm->l_next;
275*7c478bd9Sstevel@tonic-gate 	}
276*7c478bd9Sstevel@tonic-gate 	return (NULL);
277*7c478bd9Sstevel@tonic-gate }
278*7c478bd9Sstevel@tonic-gate 
279*7c478bd9Sstevel@tonic-gate lwp_info_t *
280*7c478bd9Sstevel@tonic-gate lwpid_get(pid_t pid, id_t lwpid)
281*7c478bd9Sstevel@tonic-gate {
282*7c478bd9Sstevel@tonic-gate 	plwp_t *elm = lwpid_getptr(pid, lwpid);
283*7c478bd9Sstevel@tonic-gate 	if (elm)
284*7c478bd9Sstevel@tonic-gate 		return (elm->l_lwp);
285*7c478bd9Sstevel@tonic-gate 	else
286*7c478bd9Sstevel@tonic-gate 		return (NULL);
287*7c478bd9Sstevel@tonic-gate }
288*7c478bd9Sstevel@tonic-gate 
289*7c478bd9Sstevel@tonic-gate int
290*7c478bd9Sstevel@tonic-gate lwpid_pidcheck(pid_t pid)
291*7c478bd9Sstevel@tonic-gate {
292*7c478bd9Sstevel@tonic-gate 	plwp_t *elm;
293*7c478bd9Sstevel@tonic-gate 	elm = plwp_tbl[pid % PLWP_TBL_SZ];
294*7c478bd9Sstevel@tonic-gate 	while (elm) {
295*7c478bd9Sstevel@tonic-gate 		if (elm->l_pid == pid)
296*7c478bd9Sstevel@tonic-gate 			return (1);
297*7c478bd9Sstevel@tonic-gate 		else
298*7c478bd9Sstevel@tonic-gate 			elm = elm->l_next;
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate 	return (0);
301*7c478bd9Sstevel@tonic-gate }
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate int
304*7c478bd9Sstevel@tonic-gate lwpid_is_active(pid_t pid, id_t lwpid)
305*7c478bd9Sstevel@tonic-gate {
306*7c478bd9Sstevel@tonic-gate 	plwp_t *elm = lwpid_getptr(pid, lwpid);
307*7c478bd9Sstevel@tonic-gate 	if (elm)
308*7c478bd9Sstevel@tonic-gate 		return (elm->l_active);
309*7c478bd9Sstevel@tonic-gate 	else
310*7c478bd9Sstevel@tonic-gate 		return (0);
311*7c478bd9Sstevel@tonic-gate }
312*7c478bd9Sstevel@tonic-gate 
313*7c478bd9Sstevel@tonic-gate void
314*7c478bd9Sstevel@tonic-gate lwpid_set_active(pid_t pid, id_t lwpid)
315*7c478bd9Sstevel@tonic-gate {
316*7c478bd9Sstevel@tonic-gate 	plwp_t *elm = lwpid_getptr(pid, lwpid);
317*7c478bd9Sstevel@tonic-gate 	if (elm)
318*7c478bd9Sstevel@tonic-gate 		elm->l_active = LWP_ACTIVE;
319*7c478bd9Sstevel@tonic-gate }
320