Print this page
2989 Eliminate use of LOGNAME_MAX in ON
1166 useradd have warning with name more 8 chars
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/prstat/prtable.c
+++ new/usr/src/cmd/prstat/prtable.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 + * Copyright (c) 2013 Gary Mills
23 + *
22 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23 25 * Use is subject to license terms.
24 26 *
25 27 * Portions Copyright 2009 Chad Mynhier
26 28 */
27 29
28 30 #include <procfs.h>
29 31 #include <unistd.h>
30 32 #include <stdlib.h>
31 33 #include <pwd.h>
32 34 #include <ctype.h>
33 35 #include <string.h>
34 36 #include <libintl.h>
35 37 #include <errno.h>
36 38 #include <zone.h>
37 39 #include <libzonecfg.h>
38 40
39 41 #include "prstat.h"
40 42 #include "prutil.h"
41 43 #include "prtable.h"
42 44
43 45 static plwp_t *plwp_tbl[PLWP_TBL_SZ];
44 46
45 47 void
46 48 lwpid_init()
47 49 {
48 50 (void) memset(&plwp_tbl, 0, sizeof (plwp_t *) * PLWP_TBL_SZ);
49 51 }
50 52
51 53 static uid_t
↓ open down ↓ |
20 lines elided |
↑ open up ↑ |
52 54 pwd_getid(char *name)
53 55 {
54 56 struct passwd *pwd;
55 57
56 58 if ((pwd = getpwnam(name)) == NULL)
57 59 Die(gettext("invalid user name: %s\n"), name);
58 60 return (pwd->pw_uid);
59 61 }
60 62
61 63 void
62 -pwd_getname(uid_t uid, char *name, int length, int noresolve)
64 +pwd_getname(uid_t uid, char *name, size_t length, int noresolve,
65 + int trunc, size_t width)
63 66 {
64 67 struct passwd *pwd;
68 + size_t n;
65 69
66 70 if (noresolve || (pwd = getpwuid(uid)) == NULL) {
67 71 (void) snprintf(name, length, "%u", uid);
68 72 } else {
69 - (void) snprintf(name, length, "%s", pwd->pw_name);
73 + n = strlen(pwd->pw_name);
74 + if (trunc && n > width)
75 + (void) snprintf(name, length, "%.*s%c",
76 + width - 1, pwd->pw_name, '*');
77 + else
78 + (void) snprintf(name, length, "%s", pwd->pw_name);
70 79 }
71 80 }
72 81
73 82 void
74 83 add_uid(uidtbl_t *tbl, char *name)
75 84 {
76 85 uid_t *uid;
77 86
78 87 if (tbl->n_size == tbl->n_nent) { /* reallocation */
79 88 if ((tbl->n_size *= 2) == 0)
80 89 tbl->n_size = 4; /* first time */
81 90 tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t));
82 91 }
83 92
84 93 uid = &tbl->n_list[tbl->n_nent++];
85 94
86 95 if (isdigit(name[0])) {
87 96 *uid = Atoi(name);
88 97 } else {
89 98 *uid = pwd_getid(name);
90 99 }
91 100 }
92 101
93 102 int
94 103 has_uid(uidtbl_t *tbl, uid_t uid)
95 104 {
96 105 size_t i;
97 106
98 107 if (tbl->n_nent) { /* do linear search if table is not empty */
99 108 for (i = 0; i < tbl->n_nent; i++)
100 109 if (tbl->n_list[i] == uid)
101 110 return (1);
102 111 } else {
103 112 return (1); /* if table is empty return true */
104 113 }
105 114
106 115 return (0); /* nothing has been found */
107 116 }
108 117
109 118 void
110 119 add_element(table_t *table, long element)
111 120 {
112 121 if (table->t_size == table->t_nent) {
113 122 if ((table->t_size *= 2) == 0)
114 123 table->t_size = 4;
115 124 table->t_list = Realloc(table->t_list,
116 125 table->t_size * sizeof (long));
117 126 }
118 127 table->t_list[table->t_nent++] = element;
119 128 }
120 129
121 130 int
122 131 has_element(table_t *table, long element)
123 132 {
124 133 size_t i;
125 134
126 135 if (table->t_nent) { /* do linear search if table is not empty */
127 136 for (i = 0; i < table->t_nent; i++)
128 137 if (table->t_list[i] == element)
129 138 return (1);
130 139 } else { /* if table is empty then */
131 140 return (1); /* pretend that element was found */
132 141 }
133 142
134 143 return (0); /* element was not found */
135 144 }
136 145
137 146 int
138 147 foreach_element(table_t *table, void *buf, void (*walker)(long, void *))
139 148 {
140 149 size_t i;
141 150
142 151 if (table->t_nent) {
143 152 for (i = 0; i < table->t_nent; i++)
144 153 walker(table->t_list[i], buf);
145 154 } else {
146 155 return (0);
147 156 }
148 157 return (1);
149 158 }
150 159
151 160 void
152 161 add_zone(zonetbl_t *tbl, char *str)
153 162 {
154 163 zonename_t *entp;
155 164 zoneid_t id;
156 165 char *cp;
157 166
158 167 /*
159 168 * str should be either the name of a configured zone, or the
160 169 * id of a running zone. If str is a zone name, store the name
161 170 * in the table; otherwise, just store the id.
162 171 */
163 172 if (zone_get_id(str, &id) != 0) {
164 173 Die(gettext("unknown zone -- %s\n"), str);
165 174 /*NOTREACHED*/
166 175 }
167 176
168 177 /* was zone specified by name or id? */
169 178 errno = 0;
170 179 if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str &&
171 180 *cp == '\0') {
172 181 /* found it by id, don't store the name */
173 182 str = NULL;
174 183 }
175 184
176 185 if (tbl->z_size == tbl->z_nent) { /* reallocation */
177 186 if ((tbl->z_size *= 2) == 0)
178 187 tbl->z_size = 4; /* first time */
179 188 tbl->z_list =
180 189 Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t));
181 190 }
182 191
183 192 entp = &tbl->z_list[tbl->z_nent++];
184 193 if (str)
185 194 (void) strlcpy(entp->z_name, str, ZONENAME_MAX);
186 195 else
187 196 entp->z_name[0] = '\0';
188 197 entp->z_id = id;
189 198 }
190 199
191 200 int
192 201 has_zone(zonetbl_t *tbl, zoneid_t id)
193 202 {
194 203 long i;
195 204
196 205 if (tbl->z_nent) { /* do linear search if table is not empty */
197 206 for (i = 0; i < tbl->z_nent; i++)
198 207 if (tbl->z_list[i].z_id == id)
199 208 return (1);
200 209 return (0); /* nothing has been found */
201 210 }
202 211
203 212 return (1); /* if table is empty return true */
204 213 }
205 214
206 215 /*
207 216 * Lookup ids for each zone name; this is done once each time /proc
208 217 * is scanned to avoid calling getzoneidbyname for each process.
209 218 */
210 219 void
211 220 convert_zone(zonetbl_t *tbl)
212 221 {
213 222 long i;
214 223 zoneid_t id;
215 224 char *name;
216 225
217 226 for (i = 0; i < tbl->z_nent; i++) {
218 227 name = tbl->z_list[i].z_name;
219 228 if (name != NULL) {
220 229 if ((id = getzoneidbyname(name)) != -1)
221 230 tbl->z_list[i].z_id = id;
222 231 }
223 232 }
224 233 }
225 234
226 235 void
227 236 lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid)
228 237 {
229 238 plwp_t *elm = Zalloc(sizeof (plwp_t));
230 239 int hash = pid % PLWP_TBL_SZ;
231 240
232 241 elm->l_pid = pid;
233 242 elm->l_lwpid = lwpid;
234 243 elm->l_lwp = lwp;
235 244 elm->l_next = plwp_tbl[hash]; /* add in front of chain */
236 245 plwp_tbl[hash] = elm;
237 246 }
238 247
239 248 void
240 249 lwpid_del(pid_t pid, id_t lwpid)
241 250 {
242 251 plwp_t *elm, *elm_prev;
243 252 int hash = pid % PLWP_TBL_SZ;
244 253
245 254 elm = plwp_tbl[hash];
246 255 elm_prev = NULL;
247 256
248 257 while (elm) {
249 258 if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) {
250 259 if (!elm_prev) /* first chain element */
251 260 plwp_tbl[hash] = elm->l_next;
252 261 else
253 262 elm_prev->l_next = elm->l_next;
254 263 free(elm);
255 264 break;
256 265 } else {
257 266 elm_prev = elm;
258 267 elm = elm->l_next;
259 268 }
260 269 }
261 270 }
262 271
263 272 static plwp_t *
264 273 lwpid_getptr(pid_t pid, id_t lwpid)
265 274 {
266 275 plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ];
267 276 while (elm) {
268 277 if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid))
269 278 return (elm);
270 279 else
271 280 elm = elm->l_next;
272 281 }
273 282 return (NULL);
274 283 }
275 284
276 285 lwp_info_t *
277 286 lwpid_get(pid_t pid, id_t lwpid)
278 287 {
279 288 plwp_t *elm = lwpid_getptr(pid, lwpid);
280 289 if (elm)
281 290 return (elm->l_lwp);
282 291 else
283 292 return (NULL);
284 293 }
285 294
286 295 int
287 296 lwpid_pidcheck(pid_t pid)
288 297 {
289 298 plwp_t *elm;
290 299 elm = plwp_tbl[pid % PLWP_TBL_SZ];
291 300 while (elm) {
292 301 if (elm->l_pid == pid)
293 302 return (1);
294 303 else
295 304 elm = elm->l_next;
296 305 }
297 306 return (0);
298 307 }
299 308
300 309 int
301 310 lwpid_is_active(pid_t pid, id_t lwpid)
302 311 {
303 312 plwp_t *elm = lwpid_getptr(pid, lwpid);
304 313 if (elm)
305 314 return (elm->l_active);
306 315 else
307 316 return (0);
308 317 }
309 318
310 319 void
311 320 lwpid_set_active(pid_t pid, id_t lwpid)
312 321 {
313 322 plwp_t *elm = lwpid_getptr(pid, lwpid);
314 323 if (elm)
315 324 elm->l_active = LWP_ACTIVE;
316 325 }
↓ open down ↓ |
237 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX