17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*f48205beScasper * Common Development and Distribution License (the "License"). 6*f48205beScasper * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*f48205beScasper * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <procfs.h> 297c478bd9Sstevel@tonic-gate #include <unistd.h> 307c478bd9Sstevel@tonic-gate #include <stdlib.h> 317c478bd9Sstevel@tonic-gate #include <pwd.h> 327c478bd9Sstevel@tonic-gate #include <ctype.h> 337c478bd9Sstevel@tonic-gate #include <string.h> 347c478bd9Sstevel@tonic-gate #include <libintl.h> 357c478bd9Sstevel@tonic-gate #include <errno.h> 367c478bd9Sstevel@tonic-gate #include <zone.h> 377c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #include "prstat.h" 407c478bd9Sstevel@tonic-gate #include "prutil.h" 417c478bd9Sstevel@tonic-gate #include "prtable.h" 427c478bd9Sstevel@tonic-gate 437c478bd9Sstevel@tonic-gate static plwp_t *plwp_tbl[PLWP_TBL_SZ]; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate void 467c478bd9Sstevel@tonic-gate lwpid_init() 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate (void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ); 497c478bd9Sstevel@tonic-gate } 507c478bd9Sstevel@tonic-gate 51*f48205beScasper static uid_t 527c478bd9Sstevel@tonic-gate pwd_getid(char *name) 537c478bd9Sstevel@tonic-gate { 547c478bd9Sstevel@tonic-gate struct passwd *pwd; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate if ((pwd = getpwnam(name)) == NULL) 577c478bd9Sstevel@tonic-gate Die(gettext("invalid user name: %s\n"), name); 587c478bd9Sstevel@tonic-gate return (pwd->pw_uid); 597c478bd9Sstevel@tonic-gate } 607c478bd9Sstevel@tonic-gate 617c478bd9Sstevel@tonic-gate void 62*f48205beScasper pwd_getname(uid_t uid, char *name, int length) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate struct passwd *pwd; 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate if ((pwd = getpwuid(uid)) == NULL) { 67*f48205beScasper (void) snprintf(name, length, "%u", uid); 687c478bd9Sstevel@tonic-gate } else { 697c478bd9Sstevel@tonic-gate (void) snprintf(name, length, "%s", pwd->pw_name); 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate } 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate void 747c478bd9Sstevel@tonic-gate add_uid(nametbl_t *tbl, char *name) 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate name_t *entp; 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate if (tbl->n_size == tbl->n_nent) { /* reallocation */ 797c478bd9Sstevel@tonic-gate if ((tbl->n_size *= 2) == 0) 807c478bd9Sstevel@tonic-gate tbl->n_size = 4; /* first time */ 817c478bd9Sstevel@tonic-gate tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (name_t)); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate entp = &tbl->n_list[tbl->n_nent++]; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (isdigit(name[0])) { 877c478bd9Sstevel@tonic-gate entp->u_id = Atoi(name); 887c478bd9Sstevel@tonic-gate pwd_getname(entp->u_id, entp->u_name, LOGNAME_MAX); 897c478bd9Sstevel@tonic-gate } else { 907c478bd9Sstevel@tonic-gate entp->u_id = pwd_getid(name); 917c478bd9Sstevel@tonic-gate (void) snprintf(entp->u_name, LOGNAME_MAX, "%s", name); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate } 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate int 967c478bd9Sstevel@tonic-gate has_uid(nametbl_t *tbl, uid_t uid) 977c478bd9Sstevel@tonic-gate { 987c478bd9Sstevel@tonic-gate size_t i; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (tbl->n_nent) { /* do linear search if table is not empty */ 1017c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->n_nent; i++) 1027c478bd9Sstevel@tonic-gate if (tbl->n_list[i].u_id == uid) 1037c478bd9Sstevel@tonic-gate return (1); 1047c478bd9Sstevel@tonic-gate } else { 1057c478bd9Sstevel@tonic-gate return (1); /* if table is empty return true */ 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate return (0); /* nothing has been found */ 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate void 1127c478bd9Sstevel@tonic-gate add_element(table_t *table, long element) 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate if (table->t_size == table->t_nent) { 1157c478bd9Sstevel@tonic-gate if ((table->t_size *= 2) == 0) 1167c478bd9Sstevel@tonic-gate table->t_size = 4; 1177c478bd9Sstevel@tonic-gate table->t_list = Realloc(table->t_list, 1187c478bd9Sstevel@tonic-gate table->t_size * sizeof (long)); 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate table->t_list[table->t_nent++] = element; 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate int 1247c478bd9Sstevel@tonic-gate has_element(table_t *table, long element) 1257c478bd9Sstevel@tonic-gate { 1267c478bd9Sstevel@tonic-gate size_t i; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if (table->t_nent) { /* do linear search if table is not empty */ 1297c478bd9Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1307c478bd9Sstevel@tonic-gate if (table->t_list[i] == element) 1317c478bd9Sstevel@tonic-gate return (1); 1327c478bd9Sstevel@tonic-gate } else { /* if table is empty then */ 1337c478bd9Sstevel@tonic-gate return (1); /* pretend that element was found */ 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate return (0); /* element was not found */ 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate int 1407c478bd9Sstevel@tonic-gate foreach_element(table_t *table, void *buf, void (*walker)(long, void *)) 1417c478bd9Sstevel@tonic-gate { 1427c478bd9Sstevel@tonic-gate size_t i; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate if (table->t_nent) { 1457c478bd9Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1467c478bd9Sstevel@tonic-gate walker(table->t_list[i], buf); 1477c478bd9Sstevel@tonic-gate } else { 1487c478bd9Sstevel@tonic-gate return (0); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate return (1); 1517c478bd9Sstevel@tonic-gate } 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate void 1547c478bd9Sstevel@tonic-gate add_zone(zonetbl_t *tbl, char *str) 1557c478bd9Sstevel@tonic-gate { 1567c478bd9Sstevel@tonic-gate zonename_t *entp; 1577c478bd9Sstevel@tonic-gate zoneid_t id; 1587c478bd9Sstevel@tonic-gate char *cp; 1597c478bd9Sstevel@tonic-gate 1607c478bd9Sstevel@tonic-gate /* 1617c478bd9Sstevel@tonic-gate * str should be either the name of a configured zone, or the 1627c478bd9Sstevel@tonic-gate * id of a running zone. If str is a zone name, store the name 1637c478bd9Sstevel@tonic-gate * in the table; otherwise, just store the id. 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate if (zone_get_id(str, &id) != 0) { 1667c478bd9Sstevel@tonic-gate Die(gettext("unknown zone -- %s\n"), str); 1677c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate /* was zone specified by name or id? */ 1717c478bd9Sstevel@tonic-gate errno = 0; 1727c478bd9Sstevel@tonic-gate if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str && 1737c478bd9Sstevel@tonic-gate *cp == '\0') { 1747c478bd9Sstevel@tonic-gate /* found it by id, don't store the name */ 1757c478bd9Sstevel@tonic-gate str = NULL; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate if (tbl->z_size == tbl->z_nent) { /* reallocation */ 1797c478bd9Sstevel@tonic-gate if ((tbl->z_size *= 2) == 0) 1807c478bd9Sstevel@tonic-gate tbl->z_size = 4; /* first time */ 1817c478bd9Sstevel@tonic-gate tbl->z_list = 1827c478bd9Sstevel@tonic-gate Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t)); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate entp = &tbl->z_list[tbl->z_nent++]; 1867c478bd9Sstevel@tonic-gate if (str) 1877c478bd9Sstevel@tonic-gate (void) strlcpy(entp->z_name, str, ZONENAME_MAX); 1887c478bd9Sstevel@tonic-gate else 1897c478bd9Sstevel@tonic-gate entp->z_name[0] = '\0'; 1907c478bd9Sstevel@tonic-gate entp->z_id = id; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate int 1947c478bd9Sstevel@tonic-gate has_zone(zonetbl_t *tbl, zoneid_t id) 1957c478bd9Sstevel@tonic-gate { 1967c478bd9Sstevel@tonic-gate long i; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate if (tbl->z_nent) { /* do linear search if table is not empty */ 1997c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) 2007c478bd9Sstevel@tonic-gate if (tbl->z_list[i].z_id == id) 2017c478bd9Sstevel@tonic-gate return (1); 2027c478bd9Sstevel@tonic-gate return (0); /* nothing has been found */ 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate return (1); /* if table is empty return true */ 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate /* 2097c478bd9Sstevel@tonic-gate * Lookup ids for each zone name; this is done once each time /proc 2107c478bd9Sstevel@tonic-gate * is scanned to avoid calling getzoneidbyname for each process. 2117c478bd9Sstevel@tonic-gate */ 2127c478bd9Sstevel@tonic-gate void 2137c478bd9Sstevel@tonic-gate convert_zone(zonetbl_t *tbl) 2147c478bd9Sstevel@tonic-gate { 2157c478bd9Sstevel@tonic-gate long i; 2167c478bd9Sstevel@tonic-gate zoneid_t id; 2177c478bd9Sstevel@tonic-gate char *name; 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) { 2207c478bd9Sstevel@tonic-gate name = tbl->z_list[i].z_name; 2217c478bd9Sstevel@tonic-gate if (name != NULL) { 2227c478bd9Sstevel@tonic-gate if ((id = getzoneidbyname(name)) != -1) 2237c478bd9Sstevel@tonic-gate tbl->z_list[i].z_id = id; 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate } 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate void 2297c478bd9Sstevel@tonic-gate lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate plwp_t *elm = Zalloc(sizeof (plwp_t)); 2327c478bd9Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate elm->l_pid = pid; 2357c478bd9Sstevel@tonic-gate elm->l_lwpid = lwpid; 2367c478bd9Sstevel@tonic-gate elm->l_lwp = lwp; 2377c478bd9Sstevel@tonic-gate elm->l_next = plwp_tbl[hash]; /* add in front of chain */ 2387c478bd9Sstevel@tonic-gate plwp_tbl[hash] = elm; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate 2417c478bd9Sstevel@tonic-gate void 2427c478bd9Sstevel@tonic-gate lwpid_del(pid_t pid, id_t lwpid) 2437c478bd9Sstevel@tonic-gate { 2447c478bd9Sstevel@tonic-gate plwp_t *elm, *elm_prev; 2457c478bd9Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate elm = plwp_tbl[hash]; 2487c478bd9Sstevel@tonic-gate elm_prev = NULL; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate while (elm) { 2517c478bd9Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) { 2527c478bd9Sstevel@tonic-gate if (!elm_prev) /* first chain element */ 2537c478bd9Sstevel@tonic-gate plwp_tbl[hash] = elm->l_next; 2547c478bd9Sstevel@tonic-gate else 2557c478bd9Sstevel@tonic-gate elm_prev->l_next = elm->l_next; 2567c478bd9Sstevel@tonic-gate free(elm); 2577c478bd9Sstevel@tonic-gate break; 2587c478bd9Sstevel@tonic-gate } else { 2597c478bd9Sstevel@tonic-gate elm_prev = elm; 2607c478bd9Sstevel@tonic-gate elm = elm->l_next; 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate static plwp_t * 2667c478bd9Sstevel@tonic-gate lwpid_getptr(pid_t pid, id_t lwpid) 2677c478bd9Sstevel@tonic-gate { 2687c478bd9Sstevel@tonic-gate plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2697c478bd9Sstevel@tonic-gate while (elm) { 2707c478bd9Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) 2717c478bd9Sstevel@tonic-gate return (elm); 2727c478bd9Sstevel@tonic-gate else 2737c478bd9Sstevel@tonic-gate elm = elm->l_next; 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate return (NULL); 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate lwp_info_t * 2797c478bd9Sstevel@tonic-gate lwpid_get(pid_t pid, id_t lwpid) 2807c478bd9Sstevel@tonic-gate { 2817c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 2827c478bd9Sstevel@tonic-gate if (elm) 2837c478bd9Sstevel@tonic-gate return (elm->l_lwp); 2847c478bd9Sstevel@tonic-gate else 2857c478bd9Sstevel@tonic-gate return (NULL); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate int 2897c478bd9Sstevel@tonic-gate lwpid_pidcheck(pid_t pid) 2907c478bd9Sstevel@tonic-gate { 2917c478bd9Sstevel@tonic-gate plwp_t *elm; 2927c478bd9Sstevel@tonic-gate elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2937c478bd9Sstevel@tonic-gate while (elm) { 2947c478bd9Sstevel@tonic-gate if (elm->l_pid == pid) 2957c478bd9Sstevel@tonic-gate return (1); 2967c478bd9Sstevel@tonic-gate else 2977c478bd9Sstevel@tonic-gate elm = elm->l_next; 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate return (0); 3007c478bd9Sstevel@tonic-gate } 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate int 3037c478bd9Sstevel@tonic-gate lwpid_is_active(pid_t pid, id_t lwpid) 3047c478bd9Sstevel@tonic-gate { 3057c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3067c478bd9Sstevel@tonic-gate if (elm) 3077c478bd9Sstevel@tonic-gate return (elm->l_active); 3087c478bd9Sstevel@tonic-gate else 3097c478bd9Sstevel@tonic-gate return (0); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate void 3137c478bd9Sstevel@tonic-gate lwpid_set_active(pid_t pid, id_t lwpid) 3147c478bd9Sstevel@tonic-gate { 3157c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3167c478bd9Sstevel@tonic-gate if (elm) 3177c478bd9Sstevel@tonic-gate elm->l_active = LWP_ACTIVE; 3187c478bd9Sstevel@tonic-gate } 319