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*0a1278f2SGary Mills * Copyright (c) 2013 Gary Mills 23*0a1278f2SGary Mills * 247166d658SMenno Lageman * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 257c478bd9Sstevel@tonic-gate * Use is subject to license terms. 267166d658SMenno Lageman * 277166d658SMenno Lageman * Portions Copyright 2009 Chad Mynhier 287c478bd9Sstevel@tonic-gate */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #include <procfs.h> 317c478bd9Sstevel@tonic-gate #include <unistd.h> 327c478bd9Sstevel@tonic-gate #include <stdlib.h> 337c478bd9Sstevel@tonic-gate #include <pwd.h> 347c478bd9Sstevel@tonic-gate #include <ctype.h> 357c478bd9Sstevel@tonic-gate #include <string.h> 367c478bd9Sstevel@tonic-gate #include <libintl.h> 377c478bd9Sstevel@tonic-gate #include <errno.h> 387c478bd9Sstevel@tonic-gate #include <zone.h> 397c478bd9Sstevel@tonic-gate #include <libzonecfg.h> 40*0a1278f2SGary Mills #include <wchar.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include "prstat.h" 437c478bd9Sstevel@tonic-gate #include "prutil.h" 447c478bd9Sstevel@tonic-gate #include "prtable.h" 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate static plwp_t *plwp_tbl[PLWP_TBL_SZ]; 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate void 497c478bd9Sstevel@tonic-gate lwpid_init() 507c478bd9Sstevel@tonic-gate { 517c478bd9Sstevel@tonic-gate (void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ); 527c478bd9Sstevel@tonic-gate } 537c478bd9Sstevel@tonic-gate 54f48205beScasper static uid_t 557c478bd9Sstevel@tonic-gate pwd_getid(char *name) 567c478bd9Sstevel@tonic-gate { 577c478bd9Sstevel@tonic-gate struct passwd *pwd; 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate if ((pwd = getpwnam(name)) == NULL) 607c478bd9Sstevel@tonic-gate Die(gettext("invalid user name: %s\n"), name); 617c478bd9Sstevel@tonic-gate return (pwd->pw_uid); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate void 65*0a1278f2SGary Mills pwd_getname(uid_t uid, char *name, size_t length, int noresolve, 66*0a1278f2SGary Mills int trunc, size_t width) 677c478bd9Sstevel@tonic-gate { 687c478bd9Sstevel@tonic-gate struct passwd *pwd; 69*0a1278f2SGary Mills size_t n; 707c478bd9Sstevel@tonic-gate 717166d658SMenno Lageman if (noresolve || (pwd = getpwuid(uid)) == NULL) { 72*0a1278f2SGary Mills n = snprintf(NULL, 0, "%u", uid); 73*0a1278f2SGary Mills if (trunc && n > width) 74*0a1278f2SGary Mills (void) snprintf(name, length, "%.*u%c", 75*0a1278f2SGary Mills width - 1, uid, '*'); 76*0a1278f2SGary Mills else 77f48205beScasper (void) snprintf(name, length, "%u", uid); 787c478bd9Sstevel@tonic-gate } else { 79*0a1278f2SGary Mills n = mbstowcs(NULL, pwd->pw_name, 0); 80*0a1278f2SGary Mills if (n == (size_t)-1) 81*0a1278f2SGary Mills (void) snprintf(name, length, "%s", "ERROR"); 82*0a1278f2SGary Mills else if (trunc && n > width) 83*0a1278f2SGary Mills (void) snprintf(name, length, "%.*s%c", 84*0a1278f2SGary Mills width - 1, pwd->pw_name, '*'); 85*0a1278f2SGary Mills else 867c478bd9Sstevel@tonic-gate (void) snprintf(name, length, "%s", pwd->pw_name); 877c478bd9Sstevel@tonic-gate } 887c478bd9Sstevel@tonic-gate } 897c478bd9Sstevel@tonic-gate 907c478bd9Sstevel@tonic-gate void 917166d658SMenno Lageman add_uid(uidtbl_t *tbl, char *name) 927c478bd9Sstevel@tonic-gate { 937166d658SMenno Lageman uid_t *uid; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (tbl->n_size == tbl->n_nent) { /* reallocation */ 967c478bd9Sstevel@tonic-gate if ((tbl->n_size *= 2) == 0) 977c478bd9Sstevel@tonic-gate tbl->n_size = 4; /* first time */ 987166d658SMenno Lageman tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t)); 997c478bd9Sstevel@tonic-gate } 1007c478bd9Sstevel@tonic-gate 1017166d658SMenno Lageman uid = &tbl->n_list[tbl->n_nent++]; 1027c478bd9Sstevel@tonic-gate 1037c478bd9Sstevel@tonic-gate if (isdigit(name[0])) { 1047166d658SMenno Lageman *uid = Atoi(name); 1057c478bd9Sstevel@tonic-gate } else { 1067166d658SMenno Lageman *uid = pwd_getid(name); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate int 1117166d658SMenno Lageman has_uid(uidtbl_t *tbl, uid_t uid) 1127c478bd9Sstevel@tonic-gate { 1137c478bd9Sstevel@tonic-gate size_t i; 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate if (tbl->n_nent) { /* do linear search if table is not empty */ 1167c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->n_nent; i++) 1177166d658SMenno Lageman if (tbl->n_list[i] == uid) 1187c478bd9Sstevel@tonic-gate return (1); 1197c478bd9Sstevel@tonic-gate } else { 1207c478bd9Sstevel@tonic-gate return (1); /* if table is empty return true */ 1217c478bd9Sstevel@tonic-gate } 1227c478bd9Sstevel@tonic-gate 1237c478bd9Sstevel@tonic-gate return (0); /* nothing has been found */ 1247c478bd9Sstevel@tonic-gate } 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate void 1277c478bd9Sstevel@tonic-gate add_element(table_t *table, long element) 1287c478bd9Sstevel@tonic-gate { 1297c478bd9Sstevel@tonic-gate if (table->t_size == table->t_nent) { 1307c478bd9Sstevel@tonic-gate if ((table->t_size *= 2) == 0) 1317c478bd9Sstevel@tonic-gate table->t_size = 4; 1327c478bd9Sstevel@tonic-gate table->t_list = Realloc(table->t_list, 1337c478bd9Sstevel@tonic-gate table->t_size * sizeof (long)); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate table->t_list[table->t_nent++] = element; 1367c478bd9Sstevel@tonic-gate } 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate int 1397c478bd9Sstevel@tonic-gate has_element(table_t *table, long element) 1407c478bd9Sstevel@tonic-gate { 1417c478bd9Sstevel@tonic-gate size_t i; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate if (table->t_nent) { /* do linear search if table is not empty */ 1447c478bd9Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1457c478bd9Sstevel@tonic-gate if (table->t_list[i] == element) 1467c478bd9Sstevel@tonic-gate return (1); 1477c478bd9Sstevel@tonic-gate } else { /* if table is empty then */ 1487c478bd9Sstevel@tonic-gate return (1); /* pretend that element was found */ 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate return (0); /* element was not found */ 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate int 1557c478bd9Sstevel@tonic-gate foreach_element(table_t *table, void *buf, void (*walker)(long, void *)) 1567c478bd9Sstevel@tonic-gate { 1577c478bd9Sstevel@tonic-gate size_t i; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (table->t_nent) { 1607c478bd9Sstevel@tonic-gate for (i = 0; i < table->t_nent; i++) 1617c478bd9Sstevel@tonic-gate walker(table->t_list[i], buf); 1627c478bd9Sstevel@tonic-gate } else { 1637c478bd9Sstevel@tonic-gate return (0); 1647c478bd9Sstevel@tonic-gate } 1657c478bd9Sstevel@tonic-gate return (1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate void 1697c478bd9Sstevel@tonic-gate add_zone(zonetbl_t *tbl, char *str) 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate zonename_t *entp; 1727c478bd9Sstevel@tonic-gate zoneid_t id; 1737c478bd9Sstevel@tonic-gate char *cp; 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* 1767c478bd9Sstevel@tonic-gate * str should be either the name of a configured zone, or the 1777c478bd9Sstevel@tonic-gate * id of a running zone. If str is a zone name, store the name 1787c478bd9Sstevel@tonic-gate * in the table; otherwise, just store the id. 1797c478bd9Sstevel@tonic-gate */ 1807c478bd9Sstevel@tonic-gate if (zone_get_id(str, &id) != 0) { 1817c478bd9Sstevel@tonic-gate Die(gettext("unknown zone -- %s\n"), str); 1827c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* was zone specified by name or id? */ 1867c478bd9Sstevel@tonic-gate errno = 0; 1877c478bd9Sstevel@tonic-gate if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str && 1887c478bd9Sstevel@tonic-gate *cp == '\0') { 1897c478bd9Sstevel@tonic-gate /* found it by id, don't store the name */ 1907c478bd9Sstevel@tonic-gate str = NULL; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate if (tbl->z_size == tbl->z_nent) { /* reallocation */ 1947c478bd9Sstevel@tonic-gate if ((tbl->z_size *= 2) == 0) 1957c478bd9Sstevel@tonic-gate tbl->z_size = 4; /* first time */ 1967c478bd9Sstevel@tonic-gate tbl->z_list = 1977c478bd9Sstevel@tonic-gate Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t)); 1987c478bd9Sstevel@tonic-gate } 1997c478bd9Sstevel@tonic-gate 2007c478bd9Sstevel@tonic-gate entp = &tbl->z_list[tbl->z_nent++]; 2017c478bd9Sstevel@tonic-gate if (str) 2027c478bd9Sstevel@tonic-gate (void) strlcpy(entp->z_name, str, ZONENAME_MAX); 2037c478bd9Sstevel@tonic-gate else 2047c478bd9Sstevel@tonic-gate entp->z_name[0] = '\0'; 2057c478bd9Sstevel@tonic-gate entp->z_id = id; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate int 2097c478bd9Sstevel@tonic-gate has_zone(zonetbl_t *tbl, zoneid_t id) 2107c478bd9Sstevel@tonic-gate { 2117c478bd9Sstevel@tonic-gate long i; 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate if (tbl->z_nent) { /* do linear search if table is not empty */ 2147c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) 2157c478bd9Sstevel@tonic-gate if (tbl->z_list[i].z_id == id) 2167c478bd9Sstevel@tonic-gate return (1); 2177c478bd9Sstevel@tonic-gate return (0); /* nothing has been found */ 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate return (1); /* if table is empty return true */ 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * Lookup ids for each zone name; this is done once each time /proc 2257c478bd9Sstevel@tonic-gate * is scanned to avoid calling getzoneidbyname for each process. 2267c478bd9Sstevel@tonic-gate */ 2277c478bd9Sstevel@tonic-gate void 2287c478bd9Sstevel@tonic-gate convert_zone(zonetbl_t *tbl) 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate long i; 2317c478bd9Sstevel@tonic-gate zoneid_t id; 2327c478bd9Sstevel@tonic-gate char *name; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate for (i = 0; i < tbl->z_nent; i++) { 2357c478bd9Sstevel@tonic-gate name = tbl->z_list[i].z_name; 2367c478bd9Sstevel@tonic-gate if (name != NULL) { 2377c478bd9Sstevel@tonic-gate if ((id = getzoneidbyname(name)) != -1) 2387c478bd9Sstevel@tonic-gate tbl->z_list[i].z_id = id; 2397c478bd9Sstevel@tonic-gate } 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate } 2427c478bd9Sstevel@tonic-gate 2437c478bd9Sstevel@tonic-gate void 2447c478bd9Sstevel@tonic-gate lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid) 2457c478bd9Sstevel@tonic-gate { 2467c478bd9Sstevel@tonic-gate plwp_t *elm = Zalloc(sizeof (plwp_t)); 2477c478bd9Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate elm->l_pid = pid; 2507c478bd9Sstevel@tonic-gate elm->l_lwpid = lwpid; 2517c478bd9Sstevel@tonic-gate elm->l_lwp = lwp; 2527c478bd9Sstevel@tonic-gate elm->l_next = plwp_tbl[hash]; /* add in front of chain */ 2537c478bd9Sstevel@tonic-gate plwp_tbl[hash] = elm; 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate void 2577c478bd9Sstevel@tonic-gate lwpid_del(pid_t pid, id_t lwpid) 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate plwp_t *elm, *elm_prev; 2607c478bd9Sstevel@tonic-gate int hash = pid % PLWP_TBL_SZ; 2617c478bd9Sstevel@tonic-gate 2627c478bd9Sstevel@tonic-gate elm = plwp_tbl[hash]; 2637c478bd9Sstevel@tonic-gate elm_prev = NULL; 2647c478bd9Sstevel@tonic-gate 2657c478bd9Sstevel@tonic-gate while (elm) { 2667c478bd9Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) { 2677c478bd9Sstevel@tonic-gate if (!elm_prev) /* first chain element */ 2687c478bd9Sstevel@tonic-gate plwp_tbl[hash] = elm->l_next; 2697c478bd9Sstevel@tonic-gate else 2707c478bd9Sstevel@tonic-gate elm_prev->l_next = elm->l_next; 2717c478bd9Sstevel@tonic-gate free(elm); 2727c478bd9Sstevel@tonic-gate break; 2737c478bd9Sstevel@tonic-gate } else { 2747c478bd9Sstevel@tonic-gate elm_prev = elm; 2757c478bd9Sstevel@tonic-gate elm = elm->l_next; 2767c478bd9Sstevel@tonic-gate } 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate static plwp_t * 2817c478bd9Sstevel@tonic-gate lwpid_getptr(pid_t pid, id_t lwpid) 2827c478bd9Sstevel@tonic-gate { 2837c478bd9Sstevel@tonic-gate plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ]; 2847c478bd9Sstevel@tonic-gate while (elm) { 2857c478bd9Sstevel@tonic-gate if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) 2867c478bd9Sstevel@tonic-gate return (elm); 2877c478bd9Sstevel@tonic-gate else 2887c478bd9Sstevel@tonic-gate elm = elm->l_next; 2897c478bd9Sstevel@tonic-gate } 2907c478bd9Sstevel@tonic-gate return (NULL); 2917c478bd9Sstevel@tonic-gate } 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate lwp_info_t * 2947c478bd9Sstevel@tonic-gate lwpid_get(pid_t pid, id_t lwpid) 2957c478bd9Sstevel@tonic-gate { 2967c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 2977c478bd9Sstevel@tonic-gate if (elm) 2987c478bd9Sstevel@tonic-gate return (elm->l_lwp); 2997c478bd9Sstevel@tonic-gate else 3007c478bd9Sstevel@tonic-gate return (NULL); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate int 3047c478bd9Sstevel@tonic-gate lwpid_pidcheck(pid_t pid) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate plwp_t *elm; 3077c478bd9Sstevel@tonic-gate elm = plwp_tbl[pid % PLWP_TBL_SZ]; 3087c478bd9Sstevel@tonic-gate while (elm) { 3097c478bd9Sstevel@tonic-gate if (elm->l_pid == pid) 3107c478bd9Sstevel@tonic-gate return (1); 3117c478bd9Sstevel@tonic-gate else 3127c478bd9Sstevel@tonic-gate elm = elm->l_next; 3137c478bd9Sstevel@tonic-gate } 3147c478bd9Sstevel@tonic-gate return (0); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate int 3187c478bd9Sstevel@tonic-gate lwpid_is_active(pid_t pid, id_t lwpid) 3197c478bd9Sstevel@tonic-gate { 3207c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3217c478bd9Sstevel@tonic-gate if (elm) 3227c478bd9Sstevel@tonic-gate return (elm->l_active); 3237c478bd9Sstevel@tonic-gate else 3247c478bd9Sstevel@tonic-gate return (0); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate void 3287c478bd9Sstevel@tonic-gate lwpid_set_active(pid_t pid, id_t lwpid) 3297c478bd9Sstevel@tonic-gate { 3307c478bd9Sstevel@tonic-gate plwp_t *elm = lwpid_getptr(pid, lwpid); 3317c478bd9Sstevel@tonic-gate if (elm) 3327c478bd9Sstevel@tonic-gate elm->l_active = LWP_ACTIVE; 3337c478bd9Sstevel@tonic-gate } 334