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 - (void) snprintf(name, length, "%u", uid);
71 + n = snprintf(NULL, 0, "%u", uid);
72 + if (trunc && n > width)
73 + (void) snprintf(name, length, "%.*u%c",
74 + width - 1, uid, '*');
75 + else
76 + (void) snprintf(name, length, "%u", uid);
68 77 } else {
69 - (void) snprintf(name, length, "%s", pwd->pw_name);
78 + n = strlen(pwd->pw_name);
79 + if (trunc && n > width)
80 + (void) snprintf(name, length, "%.*s%c",
81 + width - 1, pwd->pw_name, '*');
82 + else
83 + (void) snprintf(name, length, "%s", pwd->pw_name);
70 84 }
71 85 }
72 86
73 87 void
74 88 add_uid(uidtbl_t *tbl, char *name)
75 89 {
76 90 uid_t *uid;
77 91
78 92 if (tbl->n_size == tbl->n_nent) { /* reallocation */
79 93 if ((tbl->n_size *= 2) == 0)
80 94 tbl->n_size = 4; /* first time */
81 95 tbl->n_list = Realloc(tbl->n_list, tbl->n_size*sizeof (uid_t));
82 96 }
83 97
84 98 uid = &tbl->n_list[tbl->n_nent++];
85 99
86 100 if (isdigit(name[0])) {
87 101 *uid = Atoi(name);
88 102 } else {
89 103 *uid = pwd_getid(name);
90 104 }
91 105 }
92 106
93 107 int
94 108 has_uid(uidtbl_t *tbl, uid_t uid)
95 109 {
96 110 size_t i;
97 111
98 112 if (tbl->n_nent) { /* do linear search if table is not empty */
99 113 for (i = 0; i < tbl->n_nent; i++)
100 114 if (tbl->n_list[i] == uid)
101 115 return (1);
102 116 } else {
103 117 return (1); /* if table is empty return true */
104 118 }
105 119
106 120 return (0); /* nothing has been found */
107 121 }
108 122
109 123 void
110 124 add_element(table_t *table, long element)
111 125 {
112 126 if (table->t_size == table->t_nent) {
113 127 if ((table->t_size *= 2) == 0)
114 128 table->t_size = 4;
115 129 table->t_list = Realloc(table->t_list,
116 130 table->t_size * sizeof (long));
117 131 }
118 132 table->t_list[table->t_nent++] = element;
119 133 }
120 134
121 135 int
122 136 has_element(table_t *table, long element)
123 137 {
124 138 size_t i;
125 139
126 140 if (table->t_nent) { /* do linear search if table is not empty */
127 141 for (i = 0; i < table->t_nent; i++)
128 142 if (table->t_list[i] == element)
129 143 return (1);
130 144 } else { /* if table is empty then */
131 145 return (1); /* pretend that element was found */
132 146 }
133 147
134 148 return (0); /* element was not found */
135 149 }
136 150
137 151 int
138 152 foreach_element(table_t *table, void *buf, void (*walker)(long, void *))
139 153 {
140 154 size_t i;
141 155
142 156 if (table->t_nent) {
143 157 for (i = 0; i < table->t_nent; i++)
144 158 walker(table->t_list[i], buf);
145 159 } else {
146 160 return (0);
147 161 }
148 162 return (1);
149 163 }
150 164
151 165 void
152 166 add_zone(zonetbl_t *tbl, char *str)
153 167 {
154 168 zonename_t *entp;
155 169 zoneid_t id;
156 170 char *cp;
157 171
158 172 /*
159 173 * str should be either the name of a configured zone, or the
160 174 * id of a running zone. If str is a zone name, store the name
161 175 * in the table; otherwise, just store the id.
162 176 */
163 177 if (zone_get_id(str, &id) != 0) {
164 178 Die(gettext("unknown zone -- %s\n"), str);
165 179 /*NOTREACHED*/
166 180 }
167 181
168 182 /* was zone specified by name or id? */
169 183 errno = 0;
170 184 if (id == (zoneid_t)strtol(str, &cp, 0) && errno == 0 && cp != str &&
171 185 *cp == '\0') {
172 186 /* found it by id, don't store the name */
173 187 str = NULL;
174 188 }
175 189
176 190 if (tbl->z_size == tbl->z_nent) { /* reallocation */
177 191 if ((tbl->z_size *= 2) == 0)
178 192 tbl->z_size = 4; /* first time */
179 193 tbl->z_list =
180 194 Realloc(tbl->z_list, tbl->z_size * sizeof (zonename_t));
181 195 }
182 196
183 197 entp = &tbl->z_list[tbl->z_nent++];
184 198 if (str)
185 199 (void) strlcpy(entp->z_name, str, ZONENAME_MAX);
186 200 else
187 201 entp->z_name[0] = '\0';
188 202 entp->z_id = id;
189 203 }
190 204
191 205 int
192 206 has_zone(zonetbl_t *tbl, zoneid_t id)
193 207 {
194 208 long i;
195 209
196 210 if (tbl->z_nent) { /* do linear search if table is not empty */
197 211 for (i = 0; i < tbl->z_nent; i++)
198 212 if (tbl->z_list[i].z_id == id)
199 213 return (1);
200 214 return (0); /* nothing has been found */
201 215 }
202 216
203 217 return (1); /* if table is empty return true */
204 218 }
205 219
206 220 /*
207 221 * Lookup ids for each zone name; this is done once each time /proc
208 222 * is scanned to avoid calling getzoneidbyname for each process.
209 223 */
210 224 void
211 225 convert_zone(zonetbl_t *tbl)
212 226 {
213 227 long i;
214 228 zoneid_t id;
215 229 char *name;
216 230
217 231 for (i = 0; i < tbl->z_nent; i++) {
218 232 name = tbl->z_list[i].z_name;
219 233 if (name != NULL) {
220 234 if ((id = getzoneidbyname(name)) != -1)
221 235 tbl->z_list[i].z_id = id;
222 236 }
223 237 }
224 238 }
225 239
226 240 void
227 241 lwpid_add(lwp_info_t *lwp, pid_t pid, id_t lwpid)
228 242 {
229 243 plwp_t *elm = Zalloc(sizeof (plwp_t));
230 244 int hash = pid % PLWP_TBL_SZ;
231 245
232 246 elm->l_pid = pid;
233 247 elm->l_lwpid = lwpid;
234 248 elm->l_lwp = lwp;
235 249 elm->l_next = plwp_tbl[hash]; /* add in front of chain */
236 250 plwp_tbl[hash] = elm;
237 251 }
238 252
239 253 void
240 254 lwpid_del(pid_t pid, id_t lwpid)
241 255 {
242 256 plwp_t *elm, *elm_prev;
243 257 int hash = pid % PLWP_TBL_SZ;
244 258
245 259 elm = plwp_tbl[hash];
246 260 elm_prev = NULL;
247 261
248 262 while (elm) {
249 263 if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid)) {
250 264 if (!elm_prev) /* first chain element */
251 265 plwp_tbl[hash] = elm->l_next;
252 266 else
253 267 elm_prev->l_next = elm->l_next;
254 268 free(elm);
255 269 break;
256 270 } else {
257 271 elm_prev = elm;
258 272 elm = elm->l_next;
259 273 }
260 274 }
261 275 }
262 276
263 277 static plwp_t *
264 278 lwpid_getptr(pid_t pid, id_t lwpid)
265 279 {
266 280 plwp_t *elm = plwp_tbl[pid % PLWP_TBL_SZ];
267 281 while (elm) {
268 282 if ((elm->l_pid == pid) && (elm->l_lwpid == lwpid))
269 283 return (elm);
270 284 else
271 285 elm = elm->l_next;
272 286 }
273 287 return (NULL);
274 288 }
275 289
276 290 lwp_info_t *
277 291 lwpid_get(pid_t pid, id_t lwpid)
278 292 {
279 293 plwp_t *elm = lwpid_getptr(pid, lwpid);
280 294 if (elm)
281 295 return (elm->l_lwp);
282 296 else
283 297 return (NULL);
284 298 }
285 299
286 300 int
287 301 lwpid_pidcheck(pid_t pid)
288 302 {
289 303 plwp_t *elm;
290 304 elm = plwp_tbl[pid % PLWP_TBL_SZ];
291 305 while (elm) {
292 306 if (elm->l_pid == pid)
293 307 return (1);
294 308 else
295 309 elm = elm->l_next;
296 310 }
297 311 return (0);
298 312 }
299 313
300 314 int
301 315 lwpid_is_active(pid_t pid, id_t lwpid)
302 316 {
303 317 plwp_t *elm = lwpid_getptr(pid, lwpid);
304 318 if (elm)
305 319 return (elm->l_active);
306 320 else
307 321 return (0);
308 322 }
309 323
310 324 void
311 325 lwpid_set_active(pid_t pid, id_t lwpid)
312 326 {
313 327 plwp_t *elm = lwpid_getptr(pid, lwpid);
314 328 if (elm)
315 329 elm->l_active = LWP_ACTIVE;
316 330 }
↓ open down ↓ |
237 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX