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 5*4321Scasper * Common Development and Distribution License (the "License"). 6*4321Scasper * 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*4321Scasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 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 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 51*4321Scasper static uid_t 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 62*4321Scasper pwd_getname(uid_t uid, char *name, int length) 630Sstevel@tonic-gate { 640Sstevel@tonic-gate struct passwd *pwd; 650Sstevel@tonic-gate 660Sstevel@tonic-gate if ((pwd = getpwuid(uid)) == NULL) { 67*4321Scasper (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 740Sstevel@tonic-gate add_uid(nametbl_t *tbl, char *name) 750Sstevel@tonic-gate { 760Sstevel@tonic-gate name_t *entp; 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 */ 810Sstevel@tonic-gate tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (name_t)); 820Sstevel@tonic-gate } 830Sstevel@tonic-gate 840Sstevel@tonic-gate entp = &tbl->n_list[tbl->n_nent++]; 850Sstevel@tonic-gate 860Sstevel@tonic-gate if (isdigit(name[0])) { 870Sstevel@tonic-gate entp->u_id = Atoi(name); 880Sstevel@tonic-gate pwd_getname(entp->u_id, entp->u_name, LOGNAME_MAX); 890Sstevel@tonic-gate } else { 900Sstevel@tonic-gate entp->u_id = pwd_getid(name); 910Sstevel@tonic-gate (void) snprintf(entp->u_name, LOGNAME_MAX, "%s", name); 920Sstevel@tonic-gate } 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate int 960Sstevel@tonic-gate has_uid(nametbl_t *tbl, uid_t uid) 970Sstevel@tonic-gate { 980Sstevel@tonic-gate size_t i; 990Sstevel@tonic-gate 1000Sstevel@tonic-gate if (tbl->n_nent) { /* do linear search if table is not empty */ 1010Sstevel@tonic-gate for (i = 0; i < tbl->n_nent; i++) 1020Sstevel@tonic-gate if (tbl->n_list[i].u_id == uid) 1030Sstevel@tonic-gate return (1); 1040Sstevel@tonic-gate } else { 1050Sstevel@tonic-gate return (1); /* if table is empty return true */ 1060Sstevel@tonic-gate } 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate return (0); /* nothing has been found */ 1090Sstevel@tonic-gate } 1100Sstevel@tonic-gate 1110Sstevel@tonic-gate void 1120Sstevel@tonic-gate add_element(table_t *table, long element) 1130Sstevel@tonic-gate { 1140Sstevel@tonic-gate if (table->t_size == table->t_nent) { 1150Sstevel@tonic-gate if ((table->t_size *= 2) == 0) 1160Sstevel@tonic-gate table->t_size = 4; 1170Sstevel@tonic-gate table->t_list = Realloc(table->t_list, 1180Sstevel@tonic-gate table->t_size * sizeof (long)); 1190Sstevel@tonic-gate } 1200Sstevel@tonic-gate table->t_list[table->t_nent++] = element; 1210Sstevel@tonic-gate } 1220Sstevel@tonic-gate 1230Sstevel@tonic-gate int 1240Sstevel@tonic-gate has_element(table_t *table, long element) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate size_t i; 1270Sstevel@tonic-gate 1280Sstevel@tonic-gate if (table->t_nent) { /* do linear search if table is not empty */ 1290Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1300Sstevel@tonic-gate if (table->t_list[i] == element) 1310Sstevel@tonic-gate return (1); 1320Sstevel@tonic-gate } else { /* if table is empty then */ 1330Sstevel@tonic-gate return (1); /* pretend that element was found */ 1340Sstevel@tonic-gate } 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate return (0); /* element was not found */ 1370Sstevel@tonic-gate } 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate int 1400Sstevel@tonic-gate foreach_element(table_t *table, void *buf, void (*walker)(long, void *)) 1410Sstevel@tonic-gate { 1420Sstevel@tonic-gate size_t i; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate if (table->t_nent) { 1450Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1460Sstevel@tonic-gate walker(table->t_list[i], buf); 1470Sstevel@tonic-gate } else { 1480Sstevel@tonic-gate return (0); 1490Sstevel@tonic-gate } 1500Sstevel@tonic-gate return (1); 1510Sstevel@tonic-gate } 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate void 1540Sstevel@tonic-gate add_zone(zonetbl_t *tbl, char *str) 1550Sstevel@tonic-gate { 1560Sstevel@tonic-gate zonename_t *entp; 1570Sstevel@tonic-gate zoneid_t id; 1580Sstevel@tonic-gate char *cp; 1590Sstevel@tonic-gate 1600Sstevel@tonic-gate /* 1610Sstevel@tonic-gate * str should be either the name of a configured zone, or the 1620Sstevel@tonic-gate * id of a running zone. If str is a zone name, store the name 1630Sstevel@tonic-gate * in the table; otherwise, just store the id. 1640Sstevel@tonic-gate */ 1650Sstevel@tonic-gate if (zone_get_id(str, &id) != 0) { 1660Sstevel@tonic-gate Die(gettext("unknown zone -- %s\n"), str); 1670Sstevel@tonic-gate /*NOTREACHED*/ 1680Sstevel@tonic-gate } 1690Sstevel@tonic-gate 1700Sstevel@tonic-gate /* was zone specified by name or id? */ 1710Sstevel@tonic-gate errno = 0; 1720Sstevel@tonic-gate if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str && 1730Sstevel@tonic-gate *cp == '\0') { 1740Sstevel@tonic-gate /* found it by id, don't store the name */ 1750Sstevel@tonic-gate str = NULL; 1760Sstevel@tonic-gate } 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate if (tbl->z_size == tbl->z_nent) { /* reallocation */ 1790Sstevel@tonic-gate if ((tbl->z_size *= 2) == 0) 1800Sstevel@tonic-gate tbl->z_size = 4; /* first time */ 1810Sstevel@tonic-gate tbl->z_list = 1820Sstevel@tonic-gate Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t)); 1830Sstevel@tonic-gate } 1840Sstevel@tonic-gate 1850Sstevel@tonic-gate entp = &tbl->z_list[tbl->z_nent++]; 1860Sstevel@tonic-gate if (str) 1870Sstevel@tonic-gate (void) strlcpy(entp->z_name, str, ZONENAME_MAX); 1880Sstevel@tonic-gate else 1890Sstevel@tonic-gate entp->z_name[0] = '\0'; 1900Sstevel@tonic-gate entp->z_id = id; 1910Sstevel@tonic-gate } 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate int 1940Sstevel@tonic-gate has_zone(zonetbl_t *tbl, zoneid_t id) 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate long i; 1970Sstevel@tonic-gate 1980Sstevel@tonic-gate if (tbl->z_nent) { /* do linear search if table is not empty */ 1990Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) 2000Sstevel@tonic-gate if (tbl->z_list[i].z_id == id) 2010Sstevel@tonic-gate return (1); 2020Sstevel@tonic-gate return (0); /* nothing has been found */ 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate return (1); /* if table is empty return true */ 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate 2080Sstevel@tonic-gate /* 2090Sstevel@tonic-gate * Lookup ids for each zone name; this is done once each time /proc 2100Sstevel@tonic-gate * is scanned to avoid calling getzoneidbyname for each process. 2110Sstevel@tonic-gate */ 2120Sstevel@tonic-gate void 2130Sstevel@tonic-gate convert_zone(zonetbl_t *tbl) 2140Sstevel@tonic-gate { 2150Sstevel@tonic-gate long i; 2160Sstevel@tonic-gate zoneid_t id; 2170Sstevel@tonic-gate char *name; 2180Sstevel@tonic-gate 2190Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) { 2200Sstevel@tonic-gate name = tbl->z_list[i].z_name; 2210Sstevel@tonic-gate if (name != NULL) { 2220Sstevel@tonic-gate if ((id = getzoneidbyname(name)) != -1) 2230Sstevel@tonic-gate tbl->z_list[i].z_id = id; 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate } 2260Sstevel@tonic-gate } 2270Sstevel@tonic-gate 2280Sstevel@tonic-gate void 2290Sstevel@tonic-gate lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid) 2300Sstevel@tonic-gate { 2310Sstevel@tonic-gate plwp_t *elm = Zalloc(sizeof (plwp_t)); 2320Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2330Sstevel@tonic-gate 2340Sstevel@tonic-gate elm->l_pid = pid; 2350Sstevel@tonic-gate elm->l_lwpid = lwpid; 2360Sstevel@tonic-gate elm->l_lwp = lwp; 2370Sstevel@tonic-gate elm->l_next = plwp_tbl[hash]; /* add in front of chain */ 2380Sstevel@tonic-gate plwp_tbl[hash] = elm; 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate void 2420Sstevel@tonic-gate lwpid_del(pid_t pid, id_t lwpid) 2430Sstevel@tonic-gate { 2440Sstevel@tonic-gate plwp_t *elm, *elm_prev; 2450Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2460Sstevel@tonic-gate 2470Sstevel@tonic-gate elm = plwp_tbl[hash]; 2480Sstevel@tonic-gate elm_prev = NULL; 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate while (elm) { 2510Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) { 2520Sstevel@tonic-gate if (!elm_prev) /* first chain element */ 2530Sstevel@tonic-gate plwp_tbl[hash] = elm->l_next; 2540Sstevel@tonic-gate else 2550Sstevel@tonic-gate elm_prev->l_next = elm->l_next; 2560Sstevel@tonic-gate free(elm); 2570Sstevel@tonic-gate break; 2580Sstevel@tonic-gate } else { 2590Sstevel@tonic-gate elm_prev = elm; 2600Sstevel@tonic-gate elm = elm->l_next; 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate } 2630Sstevel@tonic-gate } 2640Sstevel@tonic-gate 2650Sstevel@tonic-gate static plwp_t * 2660Sstevel@tonic-gate lwpid_getptr(pid_t pid, id_t lwpid) 2670Sstevel@tonic-gate { 2680Sstevel@tonic-gate plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2690Sstevel@tonic-gate while (elm) { 2700Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) 2710Sstevel@tonic-gate return (elm); 2720Sstevel@tonic-gate else 2730Sstevel@tonic-gate elm = elm->l_next; 2740Sstevel@tonic-gate } 2750Sstevel@tonic-gate return (NULL); 2760Sstevel@tonic-gate } 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate lwp_info_t * 2790Sstevel@tonic-gate lwpid_get(pid_t pid, id_t lwpid) 2800Sstevel@tonic-gate { 2810Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 2820Sstevel@tonic-gate if (elm) 2830Sstevel@tonic-gate return (elm->l_lwp); 2840Sstevel@tonic-gate else 2850Sstevel@tonic-gate return (NULL); 2860Sstevel@tonic-gate } 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate int 2890Sstevel@tonic-gate lwpid_pidcheck(pid_t pid) 2900Sstevel@tonic-gate { 2910Sstevel@tonic-gate plwp_t *elm; 2920Sstevel@tonic-gate elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2930Sstevel@tonic-gate while (elm) { 2940Sstevel@tonic-gate if (elm->l_pid == pid) 2950Sstevel@tonic-gate return (1); 2960Sstevel@tonic-gate else 2970Sstevel@tonic-gate elm = elm->l_next; 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate return (0); 3000Sstevel@tonic-gate } 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate int 3030Sstevel@tonic-gate lwpid_is_active(pid_t pid, id_t lwpid) 3040Sstevel@tonic-gate { 3050Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3060Sstevel@tonic-gate if (elm) 3070Sstevel@tonic-gate return (elm->l_active); 3080Sstevel@tonic-gate else 3090Sstevel@tonic-gate return (0); 3100Sstevel@tonic-gate } 3110Sstevel@tonic-gate 3120Sstevel@tonic-gate void 3130Sstevel@tonic-gate lwpid_set_active(pid_t pid, id_t lwpid) 3140Sstevel@tonic-gate { 3150Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3160Sstevel@tonic-gate if (elm) 3170Sstevel@tonic-gate elm->l_active = LWP_ACTIVE; 3180Sstevel@tonic-gate } 319