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