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 5f48205beScasper * Common Development and Distribution License (the "License"). 6f48205beScasper * 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*7166d658SMenno Lageman * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*7166d658SMenno Lageman * 25*7166d658SMenno Lageman * Portions Copyright 2009 Chad Mynhier 267c478bd9Sstevel@tonic-gate */ 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 51f48205beScasper 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*7166d658SMenno Lageman pwd_getname(uid_t uid, char *name, int length, int noresolve) 637c478bd9Sstevel@tonic-gate { 647c478bd9Sstevel@tonic-gate struct passwd *pwd; 657c478bd9Sstevel@tonic-gate 66*7166d658SMenno Lageman if (noresolve || (pwd = getpwuid(uid)) == NULL) { 67f48205beScasper (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 74*7166d658SMenno Lageman add_uid(uidtbl_t *tbl, char *name) 757c478bd9Sstevel@tonic-gate { 76*7166d658SMenno Lageman uid_t *uid; 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 */ 81*7166d658SMenno Lageman tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t)); 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate 84*7166d658SMenno Lageman uid = &tbl->n_list[tbl->n_nent++]; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (isdigit(name[0])) { 87*7166d658SMenno Lageman *uid = Atoi(name); 887c478bd9Sstevel@tonic-gate } else { 89*7166d658SMenno Lageman *uid = pwd_getid(name); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate } 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate int 94*7166d658SMenno Lageman has_uid(uidtbl_t *tbl, uid_t uid) 957c478bd9Sstevel@tonic-gate { 967c478bd9Sstevel@tonic-gate size_t i; 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate if (tbl->n_nent) { /* do linear search if table is not empty */ 997c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->n_nent; i++) 100*7166d658SMenno Lageman if (tbl->n_list[i] == uid) 1017c478bd9Sstevel@tonic-gate return (1); 1027c478bd9Sstevel@tonic-gate } else { 1037c478bd9Sstevel@tonic-gate return (1); /* if table is empty return true */ 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate return (0); /* nothing has been found */ 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate void 1107c478bd9Sstevel@tonic-gate add_element(table_t *table, long element) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate if (table->t_size == table->t_nent) { 1137c478bd9Sstevel@tonic-gate if ((table->t_size *= 2) == 0) 1147c478bd9Sstevel@tonic-gate table->t_size = 4; 1157c478bd9Sstevel@tonic-gate table->t_list = Realloc(table->t_list, 1167c478bd9Sstevel@tonic-gate table->t_size * sizeof (long)); 1177c478bd9Sstevel@tonic-gate } 1187c478bd9Sstevel@tonic-gate table->t_list[table->t_nent++] = element; 1197c478bd9Sstevel@tonic-gate } 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate int 1227c478bd9Sstevel@tonic-gate has_element(table_t *table, long element) 1237c478bd9Sstevel@tonic-gate { 1247c478bd9Sstevel@tonic-gate size_t i; 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate if (table->t_nent) { /* do linear search if table is not empty */ 1277c478bd9Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1287c478bd9Sstevel@tonic-gate if (table->t_list[i] == element) 1297c478bd9Sstevel@tonic-gate return (1); 1307c478bd9Sstevel@tonic-gate } else { /* if table is empty then */ 1317c478bd9Sstevel@tonic-gate return (1); /* pretend that element was found */ 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate return (0); /* element was not found */ 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate int 1387c478bd9Sstevel@tonic-gate foreach_element(table_t *table, void *buf, void (*walker)(long, void *)) 1397c478bd9Sstevel@tonic-gate { 1407c478bd9Sstevel@tonic-gate size_t i; 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate if (table->t_nent) { 1437c478bd9Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1447c478bd9Sstevel@tonic-gate walker(table->t_list[i], buf); 1457c478bd9Sstevel@tonic-gate } else { 1467c478bd9Sstevel@tonic-gate return (0); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate return (1); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate void 1527c478bd9Sstevel@tonic-gate add_zone(zonetbl_t *tbl, char *str) 1537c478bd9Sstevel@tonic-gate { 1547c478bd9Sstevel@tonic-gate zonename_t *entp; 1557c478bd9Sstevel@tonic-gate zoneid_t id; 1567c478bd9Sstevel@tonic-gate char *cp; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* 1597c478bd9Sstevel@tonic-gate * str should be either the name of a configured zone, or the 1607c478bd9Sstevel@tonic-gate * id of a running zone. If str is a zone name, store the name 1617c478bd9Sstevel@tonic-gate * in the table; otherwise, just store the id. 1627c478bd9Sstevel@tonic-gate */ 1637c478bd9Sstevel@tonic-gate if (zone_get_id(str, &id) != 0) { 1647c478bd9Sstevel@tonic-gate Die(gettext("unknown zone -- %s\n"), str); 1657c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* was zone specified by name or id? */ 1697c478bd9Sstevel@tonic-gate errno = 0; 1707c478bd9Sstevel@tonic-gate if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str && 1717c478bd9Sstevel@tonic-gate *cp == '\0') { 1727c478bd9Sstevel@tonic-gate /* found it by id, don't store the name */ 1737c478bd9Sstevel@tonic-gate str = NULL; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate 1767c478bd9Sstevel@tonic-gate if (tbl->z_size == tbl->z_nent) { /* reallocation */ 1777c478bd9Sstevel@tonic-gate if ((tbl->z_size *= 2) == 0) 1787c478bd9Sstevel@tonic-gate tbl->z_size = 4; /* first time */ 1797c478bd9Sstevel@tonic-gate tbl->z_list = 1807c478bd9Sstevel@tonic-gate Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t)); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate entp = &tbl->z_list[tbl->z_nent++]; 1847c478bd9Sstevel@tonic-gate if (str) 1857c478bd9Sstevel@tonic-gate (void) strlcpy(entp->z_name, str, ZONENAME_MAX); 1867c478bd9Sstevel@tonic-gate else 1877c478bd9Sstevel@tonic-gate entp->z_name[0] = '\0'; 1887c478bd9Sstevel@tonic-gate entp->z_id = id; 1897c478bd9Sstevel@tonic-gate } 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate int 1927c478bd9Sstevel@tonic-gate has_zone(zonetbl_t *tbl, zoneid_t id) 1937c478bd9Sstevel@tonic-gate { 1947c478bd9Sstevel@tonic-gate long i; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate if (tbl->z_nent) { /* do linear search if table is not empty */ 1977c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) 1987c478bd9Sstevel@tonic-gate if (tbl->z_list[i].z_id == id) 1997c478bd9Sstevel@tonic-gate return (1); 2007c478bd9Sstevel@tonic-gate return (0); /* nothing has been found */ 2017c478bd9Sstevel@tonic-gate } 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate return (1); /* if table is empty return true */ 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* 2077c478bd9Sstevel@tonic-gate * Lookup ids for each zone name; this is done once each time /proc 2087c478bd9Sstevel@tonic-gate * is scanned to avoid calling getzoneidbyname for each process. 2097c478bd9Sstevel@tonic-gate */ 2107c478bd9Sstevel@tonic-gate void 2117c478bd9Sstevel@tonic-gate convert_zone(zonetbl_t *tbl) 2127c478bd9Sstevel@tonic-gate { 2137c478bd9Sstevel@tonic-gate long i; 2147c478bd9Sstevel@tonic-gate zoneid_t id; 2157c478bd9Sstevel@tonic-gate char *name; 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) { 2187c478bd9Sstevel@tonic-gate name = tbl->z_list[i].z_name; 2197c478bd9Sstevel@tonic-gate if (name != NULL) { 2207c478bd9Sstevel@tonic-gate if ((id = getzoneidbyname(name)) != -1) 2217c478bd9Sstevel@tonic-gate tbl->z_list[i].z_id = id; 2227c478bd9Sstevel@tonic-gate } 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate } 2257c478bd9Sstevel@tonic-gate 2267c478bd9Sstevel@tonic-gate void 2277c478bd9Sstevel@tonic-gate lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid) 2287c478bd9Sstevel@tonic-gate { 2297c478bd9Sstevel@tonic-gate plwp_t *elm = Zalloc(sizeof (plwp_t)); 2307c478bd9Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate elm->l_pid = pid; 2337c478bd9Sstevel@tonic-gate elm->l_lwpid = lwpid; 2347c478bd9Sstevel@tonic-gate elm->l_lwp = lwp; 2357c478bd9Sstevel@tonic-gate elm->l_next = plwp_tbl[hash]; /* add in front of chain */ 2367c478bd9Sstevel@tonic-gate plwp_tbl[hash] = elm; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate void 2407c478bd9Sstevel@tonic-gate lwpid_del(pid_t pid, id_t lwpid) 2417c478bd9Sstevel@tonic-gate { 2427c478bd9Sstevel@tonic-gate plwp_t *elm, *elm_prev; 2437c478bd9Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2447c478bd9Sstevel@tonic-gate 2457c478bd9Sstevel@tonic-gate elm = plwp_tbl[hash]; 2467c478bd9Sstevel@tonic-gate elm_prev = NULL; 2477c478bd9Sstevel@tonic-gate 2487c478bd9Sstevel@tonic-gate while (elm) { 2497c478bd9Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) { 2507c478bd9Sstevel@tonic-gate if (!elm_prev) /* first chain element */ 2517c478bd9Sstevel@tonic-gate plwp_tbl[hash] = elm->l_next; 2527c478bd9Sstevel@tonic-gate else 2537c478bd9Sstevel@tonic-gate elm_prev->l_next = elm->l_next; 2547c478bd9Sstevel@tonic-gate free(elm); 2557c478bd9Sstevel@tonic-gate break; 2567c478bd9Sstevel@tonic-gate } else { 2577c478bd9Sstevel@tonic-gate elm_prev = elm; 2587c478bd9Sstevel@tonic-gate elm = elm->l_next; 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate } 2617c478bd9Sstevel@tonic-gate } 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static plwp_t * 2647c478bd9Sstevel@tonic-gate lwpid_getptr(pid_t pid, id_t lwpid) 2657c478bd9Sstevel@tonic-gate { 2667c478bd9Sstevel@tonic-gate plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2677c478bd9Sstevel@tonic-gate while (elm) { 2687c478bd9Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) 2697c478bd9Sstevel@tonic-gate return (elm); 2707c478bd9Sstevel@tonic-gate else 2717c478bd9Sstevel@tonic-gate elm = elm->l_next; 2727c478bd9Sstevel@tonic-gate } 2737c478bd9Sstevel@tonic-gate return (NULL); 2747c478bd9Sstevel@tonic-gate } 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate lwp_info_t * 2777c478bd9Sstevel@tonic-gate lwpid_get(pid_t pid, id_t lwpid) 2787c478bd9Sstevel@tonic-gate { 2797c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 2807c478bd9Sstevel@tonic-gate if (elm) 2817c478bd9Sstevel@tonic-gate return (elm->l_lwp); 2827c478bd9Sstevel@tonic-gate else 2837c478bd9Sstevel@tonic-gate return (NULL); 2847c478bd9Sstevel@tonic-gate } 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate int 2877c478bd9Sstevel@tonic-gate lwpid_pidcheck(pid_t pid) 2887c478bd9Sstevel@tonic-gate { 2897c478bd9Sstevel@tonic-gate plwp_t *elm; 2907c478bd9Sstevel@tonic-gate elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2917c478bd9Sstevel@tonic-gate while (elm) { 2927c478bd9Sstevel@tonic-gate if (elm->l_pid == pid) 2937c478bd9Sstevel@tonic-gate return (1); 2947c478bd9Sstevel@tonic-gate else 2957c478bd9Sstevel@tonic-gate elm = elm->l_next; 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate return (0); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate int 3017c478bd9Sstevel@tonic-gate lwpid_is_active(pid_t pid, id_t lwpid) 3027c478bd9Sstevel@tonic-gate { 3037c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3047c478bd9Sstevel@tonic-gate if (elm) 3057c478bd9Sstevel@tonic-gate return (elm->l_active); 3067c478bd9Sstevel@tonic-gate else 3077c478bd9Sstevel@tonic-gate return (0); 3087c478bd9Sstevel@tonic-gate } 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate void 3117c478bd9Sstevel@tonic-gate lwpid_set_active(pid_t pid, id_t lwpid) 3127c478bd9Sstevel@tonic-gate { 3137c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3147c478bd9Sstevel@tonic-gate if (elm) 3157c478bd9Sstevel@tonic-gate elm->l_active = LWP_ACTIVE; 3167c478bd9Sstevel@tonic-gate } 317