10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
54321Scasper * Common Development and Distribution License (the "License").
64321Scasper * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*9966SMenno.Lageman@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
24*9966SMenno.Lageman@Sun.COM *
25*9966SMenno.Lageman@Sun.COM * Portions Copyright 2009 Chad Mynhier
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <procfs.h>
290Sstevel@tonic-gate #include <unistd.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <pwd.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <libintl.h>
350Sstevel@tonic-gate #include <errno.h>
360Sstevel@tonic-gate #include <zone.h>
370Sstevel@tonic-gate #include <libzonecfg.h>
380Sstevel@tonic-gate
390Sstevel@tonic-gate #include "prstat.h"
400Sstevel@tonic-gate #include "prutil.h"
410Sstevel@tonic-gate #include "prtable.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate static plwp_t *plwp_tbl[PLWP_TBL_SZ];
440Sstevel@tonic-gate
450Sstevel@tonic-gate void
lwpid_init()460Sstevel@tonic-gate lwpid_init()
470Sstevel@tonic-gate {
480Sstevel@tonic-gate (void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ);
490Sstevel@tonic-gate }
500Sstevel@tonic-gate
514321Scasper static uid_t
pwd_getid(char * name)520Sstevel@tonic-gate pwd_getid(char *name)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate struct passwd *pwd;
550Sstevel@tonic-gate
560Sstevel@tonic-gate if ((pwd = getpwnam(name)) == NULL)
570Sstevel@tonic-gate Die(gettext("invalid user name: %s\n"), name);
580Sstevel@tonic-gate return (pwd->pw_uid);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate
610Sstevel@tonic-gate void
pwd_getname(uid_t uid,char * name,int length,int noresolve)62*9966SMenno.Lageman@Sun.COM pwd_getname(uid_t uid, char *name, int length, int noresolve)
630Sstevel@tonic-gate {
640Sstevel@tonic-gate struct passwd *pwd;
650Sstevel@tonic-gate
66*9966SMenno.Lageman@Sun.COM if (noresolve || (pwd = getpwuid(uid)) == NULL) {
674321Scasper (void) snprintf(name, length, "%u", uid);
680Sstevel@tonic-gate } else {
690Sstevel@tonic-gate (void) snprintf(name, length, "%s", pwd->pw_name);
700Sstevel@tonic-gate }
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate void
add_uid(uidtbl_t * tbl,char * name)74*9966SMenno.Lageman@Sun.COM add_uid(uidtbl_t *tbl, char *name)
750Sstevel@tonic-gate {
76*9966SMenno.Lageman@Sun.COM uid_t *uid;
770Sstevel@tonic-gate
780Sstevel@tonic-gate if (tbl->n_size == tbl->n_nent) { /* reallocation */
790Sstevel@tonic-gate if ((tbl->n_size *= 2) == 0)
800Sstevel@tonic-gate tbl->n_size = 4; /* first time */
81*9966SMenno.Lageman@Sun.COM tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t));
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
84*9966SMenno.Lageman@Sun.COM uid = &tbl->n_list[tbl->n_nent++];
850Sstevel@tonic-gate
860Sstevel@tonic-gate if (isdigit(name[0])) {
87*9966SMenno.Lageman@Sun.COM *uid = Atoi(name);
880Sstevel@tonic-gate } else {
89*9966SMenno.Lageman@Sun.COM *uid = pwd_getid(name);
900Sstevel@tonic-gate }
910Sstevel@tonic-gate }
920Sstevel@tonic-gate
930Sstevel@tonic-gate int
has_uid(uidtbl_t * tbl,uid_t uid)94*9966SMenno.Lageman@Sun.COM has_uid(uidtbl_t *tbl, uid_t uid)
950Sstevel@tonic-gate {
960Sstevel@tonic-gate size_t i;
970Sstevel@tonic-gate
980Sstevel@tonic-gate if (tbl->n_nent) { /* do linear search if table is not empty */
990Sstevel@tonic-gate for (i = 0; i < tbl->n_nent; i++)
100*9966SMenno.Lageman@Sun.COM if (tbl->n_list[i] == uid)
1010Sstevel@tonic-gate return (1);
1020Sstevel@tonic-gate } else {
1030Sstevel@tonic-gate return (1); /* if table is empty return true */
1040Sstevel@tonic-gate }
1050Sstevel@tonic-gate
1060Sstevel@tonic-gate return (0); /* nothing has been found */
1070Sstevel@tonic-gate }
1080Sstevel@tonic-gate
1090Sstevel@tonic-gate void
add_element(table_t * table,long element)1100Sstevel@tonic-gate add_element(table_t *table, long element)
1110Sstevel@tonic-gate {
1120Sstevel@tonic-gate if (table->t_size == table->t_nent) {
1130Sstevel@tonic-gate if ((table->t_size *= 2) == 0)
1140Sstevel@tonic-gate table->t_size = 4;
1150Sstevel@tonic-gate table->t_list = Realloc(table->t_list,
1160Sstevel@tonic-gate table->t_size * sizeof (long));
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate table->t_list[table->t_nent++] = element;
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate int
has_element(table_t * table,long element)1220Sstevel@tonic-gate has_element(table_t *table, long element)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate size_t i;
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate if (table->t_nent) { /* do linear search if table is not empty */
1270Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++)
1280Sstevel@tonic-gate if (table->t_list[i] == element)
1290Sstevel@tonic-gate return (1);
1300Sstevel@tonic-gate } else { /* if table is empty then */
1310Sstevel@tonic-gate return (1); /* pretend that element was found */
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate return (0); /* element was not found */
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
1370Sstevel@tonic-gate int
foreach_element(table_t * table,void * buf,void (* walker)(long,void *))1380Sstevel@tonic-gate foreach_element(table_t *table, void *buf, void (*walker)(long, void *))
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate size_t i;
1410Sstevel@tonic-gate
1420Sstevel@tonic-gate if (table->t_nent) {
1430Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++)
1440Sstevel@tonic-gate walker(table->t_list[i], buf);
1450Sstevel@tonic-gate } else {
1460Sstevel@tonic-gate return (0);
1470Sstevel@tonic-gate }
1480Sstevel@tonic-gate return (1);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate
1510Sstevel@tonic-gate void
add_zone(zonetbl_t * tbl,char * str)1520Sstevel@tonic-gate add_zone(zonetbl_t *tbl, char *str)
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate zonename_t *entp;
1550Sstevel@tonic-gate zoneid_t id;
1560Sstevel@tonic-gate char *cp;
1570Sstevel@tonic-gate
1580Sstevel@tonic-gate /*
1590Sstevel@tonic-gate * str should be either the name of a configured zone, or the
1600Sstevel@tonic-gate * id of a running zone. If str is a zone name, store the name
1610Sstevel@tonic-gate * in the table; otherwise, just store the id.
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate if (zone_get_id(str, &id) != 0) {
1640Sstevel@tonic-gate Die(gettext("unknown zone -- %s\n"), str);
1650Sstevel@tonic-gate /*NOTREACHED*/
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /* was zone specified by name or id? */
1690Sstevel@tonic-gate errno = 0;
1700Sstevel@tonic-gate if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str &&
1710Sstevel@tonic-gate *cp == '\0') {
1720Sstevel@tonic-gate /* found it by id, don't store the name */
1730Sstevel@tonic-gate str = NULL;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate if (tbl->z_size == tbl->z_nent) { /* reallocation */
1770Sstevel@tonic-gate if ((tbl->z_size *= 2) == 0)
1780Sstevel@tonic-gate tbl->z_size = 4; /* first time */
1790Sstevel@tonic-gate tbl->z_list =
1800Sstevel@tonic-gate Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t));
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate
1830Sstevel@tonic-gate entp = &tbl->z_list[tbl->z_nent++];
1840Sstevel@tonic-gate if (str)
1850Sstevel@tonic-gate (void) strlcpy(entp->z_name, str, ZONENAME_MAX);
1860Sstevel@tonic-gate else
1870Sstevel@tonic-gate entp->z_name[0] = '\0';
1880Sstevel@tonic-gate entp->z_id = id;
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate
1910Sstevel@tonic-gate int
has_zone(zonetbl_t * tbl,zoneid_t id)1920Sstevel@tonic-gate has_zone(zonetbl_t *tbl, zoneid_t id)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate long i;
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate if (tbl->z_nent) { /* do linear search if table is not empty */
1970Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++)
1980Sstevel@tonic-gate if (tbl->z_list[i].z_id == id)
1990Sstevel@tonic-gate return (1);
2000Sstevel@tonic-gate return (0); /* nothing has been found */
2010Sstevel@tonic-gate }
2020Sstevel@tonic-gate
2030Sstevel@tonic-gate return (1); /* if table is empty return true */
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate * Lookup ids for each zone name; this is done once each time /proc
2080Sstevel@tonic-gate * is scanned to avoid calling getzoneidbyname for each process.
2090Sstevel@tonic-gate */
2100Sstevel@tonic-gate void
convert_zone(zonetbl_t * tbl)2110Sstevel@tonic-gate convert_zone(zonetbl_t *tbl)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate long i;
2140Sstevel@tonic-gate zoneid_t id;
2150Sstevel@tonic-gate char *name;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) {
2180Sstevel@tonic-gate name = tbl->z_list[i].z_name;
2190Sstevel@tonic-gate if (name != NULL) {
2200Sstevel@tonic-gate if ((id = getzoneidbyname(name)) != -1)
2210Sstevel@tonic-gate tbl->z_list[i].z_id = id;
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate
2260Sstevel@tonic-gate void
lwpid_add(lwp_info_t * lwp,pid_t pid,id_t lwpid)2270Sstevel@tonic-gate lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate plwp_t *elm = Zalloc(sizeof (plwp_t));
2300Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ;
2310Sstevel@tonic-gate
2320Sstevel@tonic-gate elm->l_pid = pid;
2330Sstevel@tonic-gate elm->l_lwpid = lwpid;
2340Sstevel@tonic-gate elm->l_lwp = lwp;
2350Sstevel@tonic-gate elm->l_next = plwp_tbl[hash]; /* add in front of chain */
2360Sstevel@tonic-gate plwp_tbl[hash] = elm;
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate void
lwpid_del(pid_t pid,id_t lwpid)2400Sstevel@tonic-gate lwpid_del(pid_t pid, id_t lwpid)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate plwp_t *elm, *elm_prev;
2430Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ;
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate elm = plwp_tbl[hash];
2460Sstevel@tonic-gate elm_prev = NULL;
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate while (elm) {
2490Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) {
2500Sstevel@tonic-gate if (!elm_prev) /* first chain element */
2510Sstevel@tonic-gate plwp_tbl[hash] = elm->l_next;
2520Sstevel@tonic-gate else
2530Sstevel@tonic-gate elm_prev->l_next = elm->l_next;
2540Sstevel@tonic-gate free(elm);
2550Sstevel@tonic-gate break;
2560Sstevel@tonic-gate } else {
2570Sstevel@tonic-gate elm_prev = elm;
2580Sstevel@tonic-gate elm = elm->l_next;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate }
2620Sstevel@tonic-gate
2630Sstevel@tonic-gate static plwp_t *
lwpid_getptr(pid_t pid,id_t lwpid)2640Sstevel@tonic-gate lwpid_getptr(pid_t pid, id_t lwpid)
2650Sstevel@tonic-gate {
2660Sstevel@tonic-gate plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ];
2670Sstevel@tonic-gate while (elm) {
2680Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid))
2690Sstevel@tonic-gate return (elm);
2700Sstevel@tonic-gate else
2710Sstevel@tonic-gate elm = elm->l_next;
2720Sstevel@tonic-gate }
2730Sstevel@tonic-gate return (NULL);
2740Sstevel@tonic-gate }
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate lwp_info_t *
lwpid_get(pid_t pid,id_t lwpid)2770Sstevel@tonic-gate lwpid_get(pid_t pid, id_t lwpid)
2780Sstevel@tonic-gate {
2790Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid);
2800Sstevel@tonic-gate if (elm)
2810Sstevel@tonic-gate return (elm->l_lwp);
2820Sstevel@tonic-gate else
2830Sstevel@tonic-gate return (NULL);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate int
lwpid_pidcheck(pid_t pid)2870Sstevel@tonic-gate lwpid_pidcheck(pid_t pid)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate plwp_t *elm;
2900Sstevel@tonic-gate elm = plwp_tbl[pid % PLWP_TBL_SZ];
2910Sstevel@tonic-gate while (elm) {
2920Sstevel@tonic-gate if (elm->l_pid == pid)
2930Sstevel@tonic-gate return (1);
2940Sstevel@tonic-gate else
2950Sstevel@tonic-gate elm = elm->l_next;
2960Sstevel@tonic-gate }
2970Sstevel@tonic-gate return (0);
2980Sstevel@tonic-gate }
2990Sstevel@tonic-gate
3000Sstevel@tonic-gate int
lwpid_is_active(pid_t pid,id_t lwpid)3010Sstevel@tonic-gate lwpid_is_active(pid_t pid, id_t lwpid)
3020Sstevel@tonic-gate {
3030Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid);
3040Sstevel@tonic-gate if (elm)
3050Sstevel@tonic-gate return (elm->l_active);
3060Sstevel@tonic-gate else
3070Sstevel@tonic-gate return (0);
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate void
lwpid_set_active(pid_t pid,id_t lwpid)3110Sstevel@tonic-gate lwpid_set_active(pid_t pid, id_t lwpid)
3120Sstevel@tonic-gate {
3130Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid);
3140Sstevel@tonic-gate if (elm)
3150Sstevel@tonic-gate elm->l_active = LWP_ACTIVE;
3160Sstevel@tonic-gate }
317