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/prutil.c
+++ new/usr/src/cmd/prstat/prutil.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 <sys/types.h>
29 31 #include <sys/param.h>
30 32 #include <sys/resource.h>
31 33 #include <sys/priocntl.h>
32 34 #include <sys/rtpriocntl.h>
33 35 #include <sys/tspriocntl.h>
34 36 #include <zone.h>
35 37
36 38 #include <libintl.h>
37 39 #include <limits.h>
38 40 #include <wchar.h>
39 41 #include <unistd.h>
40 42 #include <string.h>
41 43 #include <stdlib.h>
42 44 #include <stdarg.h>
43 45 #include <stdio.h>
44 46 #include <stdio_ext.h>
45 47 #include <errno.h>
46 48 #include <ctype.h>
47 49 #include <poll.h>
48 50 #include <project.h>
49 51
50 52 #include "prfile.h"
51 53 #include "prstat.h"
52 54 #include "prutil.h"
53 55
54 56 static char PRG_FMT[] = "%s: ";
55 57 static char ERR_FMT[] = ": %s\n";
56 58 static char *progname;
57 59 static char projbuf[PROJECT_BUFSZ];
58 60
59 61 #define RLIMIT_NOFILE_MAX 32767
60 62
61 63 /*PRINTFLIKE1*/
62 64 void
63 65 Warn(char *format, ...)
64 66 {
65 67 int err = errno;
66 68 va_list alist;
67 69
68 70 if (progname != NULL)
69 71 (void) fprintf(stderr, PRG_FMT, progname);
70 72 va_start(alist, format);
71 73 (void) vfprintf(stderr, format, alist);
72 74 va_end(alist);
73 75 if (strchr(format, '\n') == NULL)
74 76 (void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
75 77 }
76 78
77 79 /*PRINTFLIKE1*/
78 80 void
79 81 Die(char *format, ...)
80 82 {
81 83 int err = errno;
82 84 va_list alist;
83 85
84 86 if (progname != NULL)
85 87 (void) fprintf(stderr, PRG_FMT, progname);
86 88 va_start(alist, format);
87 89 (void) vfprintf(stderr, format, alist);
88 90 va_end(alist);
89 91 if (strchr(format, '\n') == NULL)
90 92 (void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
91 93 exit(1);
92 94 }
93 95
94 96 void
95 97 Progname(char *arg0)
96 98 {
97 99 char *p = strrchr(arg0, '/');
98 100 if (p == NULL)
↓ open down ↓ |
67 lines elided |
↑ open up ↑ |
99 101 p = arg0;
100 102 else
101 103 p++;
102 104 progname = p;
103 105 }
104 106
105 107 void
106 108 Usage()
107 109 {
108 110 (void) fprintf(stderr, gettext(
109 - "Usage:\tprstat [-acHJLmrRtTvZ] [-u euidlist] [-U uidlist]\n"
111 + "Usage:\tprstat [-acHJLmrRtTvWZ] [-u euidlist] [-U uidlist]\n"
110 112 "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
111 113 "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
112 114 "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
113 115 "\t[interval [counter]]\n"));
114 116 exit(1);
115 117 }
116 118
117 119 int
118 120 Atoi(char *p)
119 121 {
120 122 int i;
121 123 char *q;
122 124 errno = 0;
123 125 i = (int)strtol(p, &q, 10);
124 126 if (errno != 0 || q == p || i < 0 || *q != '\0')
125 127 Die(gettext("illegal argument -- %s\n"), p);
126 128 /*NOTREACHED*/
127 129 else
128 130 return (i);
129 131 return (0); /* keep gcc happy */
130 132 }
131 133
132 134 void
133 135 Format_size(char *str, size_t size, int length)
134 136 {
135 137 char tag = 'K';
136 138 if (size >= 10000) {
137 139 size = (size + 512) / 1024;
138 140 tag = 'M';
139 141 if (size >= 10000) {
140 142 size = (size + 512) / 1024;
141 143 tag = 'G';
142 144 }
143 145 }
144 146 (void) snprintf(str, length, "%4d%c", (int)size, tag);
145 147 }
146 148
147 149 void
148 150 Format_time(char *str, ulong_t time, int length)
149 151 {
150 152 (void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
151 153 (int)time/3600, (int)(time % 3600)/60, (int)time % 60);
152 154 }
153 155
154 156 void
155 157 Format_pct(char *str, float val, int length)
156 158 {
157 159 if (val > (float)100)
158 160 val = 100;
159 161 if (val < 0)
160 162 val = 0;
161 163
162 164 if (val < (float)9.95)
163 165 (void) snprintf(str, length, "%1.1f", val);
164 166 else
165 167 (void) snprintf(str, length, "%.0f", val);
166 168 }
167 169
168 170 void
169 171 Format_num(char *str, int num, int length)
170 172 {
171 173 if (num >= 100000) {
172 174 (void) snprintf(str, length, ".%1dM", num/100000);
173 175 } else {
174 176 if (num >= 1000)
175 177 (void) snprintf(str, length, "%2dK", num/1000);
176 178 else
177 179 (void) snprintf(str, length, "%3d", num);
178 180 }
179 181 }
180 182
181 183 void
182 184 Format_state(char *str, char state, processorid_t pr_id, int length)
183 185 {
184 186 switch (state) {
185 187 case 'S':
186 188 (void) strncpy(str, "sleep", length);
187 189 break;
188 190 case 'R':
189 191 (void) strncpy(str, "run", length);
190 192 break;
191 193 case 'Z':
192 194 (void) strncpy(str, "zombie", length);
193 195 break;
194 196 case 'T':
195 197 (void) strncpy(str, "stop", length);
196 198 break;
197 199 case 'I':
198 200 (void) strncpy(str, "idle", length);
199 201 break;
200 202 case 'W':
201 203 (void) strncpy(str, "wait", length);
202 204 break;
203 205 case 'O':
204 206 (void) snprintf(str, length, "cpu%-3d", (int)pr_id);
205 207 break;
206 208 default:
207 209 (void) strncpy(str, "?", length);
208 210 break;
209 211 }
210 212 }
211 213
212 214 void *
213 215 Realloc(void *ptr, size_t size)
214 216 {
215 217 int cnt = 0;
216 218 void *sav = ptr;
217 219
218 220 eagain: if ((ptr = realloc(ptr, size)))
219 221 return (ptr);
220 222
221 223 if ((++cnt <= 3) && (errno == EAGAIN)) {
222 224 Warn(gettext("realloc() failed, attempt %d"), cnt);
223 225 (void) poll(NULL, 0, 5000); /* wait for 5 seconds */
224 226 ptr = sav;
225 227 goto eagain;
226 228 }
227 229 ptr = sav;
228 230 Die(gettext("not enough memory"));
229 231 /*NOTREACHED*/
230 232 return (NULL); /* keep gcc happy */
231 233 }
232 234
233 235 void *
234 236 Malloc(size_t size)
235 237 {
236 238 return (Realloc(NULL, size));
237 239 }
238 240
239 241 void *
240 242 Zalloc(size_t size)
241 243 {
242 244 return (memset(Realloc(NULL, size), 0, size));
243 245 }
244 246
245 247 int
246 248 Setrlimit()
247 249 {
248 250 struct rlimit rlim;
249 251 int fd_limit;
250 252 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
251 253 Die(gettext("getrlimit failed"));
252 254 fd_limit = rlim.rlim_cur;
253 255 rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX);
254 256 rlim.rlim_cur = rlim.rlim_max;
255 257 (void) enable_extended_FILE_stdio(-1, -1);
256 258 if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
257 259 return (fd_limit);
258 260 else
259 261 return (rlim.rlim_cur);
260 262 }
261 263
262 264 void
263 265 Priocntl(char *class)
264 266 {
265 267 pcinfo_t pcinfo;
266 268 pcparms_t pcparms;
267 269 (void) strcpy(pcinfo.pc_clname, class);
268 270 if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) {
269 271 Warn(gettext("cannot get real time class parameters"));
270 272 return;
↓ open down ↓ |
151 lines elided |
↑ open up ↑ |
271 273 }
272 274 pcparms.pc_cid = pcinfo.pc_cid;
273 275 ((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0;
274 276 ((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0;
275 277 ((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
276 278 if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1)
277 279 Warn(gettext("cannot enter the real time class"));
278 280 }
279 281
280 282 void
281 -getprojname(projid_t projid, char *str, int len, int noresolve)
283 +getprojname(projid_t projid, char *str, size_t len, int noresolve,
284 + int trunc, size_t width)
282 285 {
283 286 struct project proj;
287 + size_t n;
284 288
285 289 if (noresolve || getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) ==
286 - NULL)
290 + NULL) {
287 291 (void) snprintf(str, len, "%-6d", (int)projid);
288 - else
289 - (void) snprintf(str, len, "%-28s", proj.pj_name);
292 + } else {
293 + n = mbstowcs(NULL, proj.pj_name, 0);
294 + if (n == (size_t)-1)
295 + (void) snprintf(str, len, "%-28s", "ERROR");
296 + else if (trunc && n > width)
297 + (void) snprintf(str, len, "%.*s%c", width - 1,
298 + proj.pj_name, '*');
299 + else
300 + (void) snprintf(str, len, "%-28s", proj.pj_name);
301 + }
290 302 }
291 303
292 304 void
293 -getzonename(zoneid_t zoneid, char *str, int len)
305 +getzonename(zoneid_t zoneid, char *str, size_t len, int trunc, size_t width)
294 306 {
295 307 char zone_name[ZONENAME_MAX];
308 + size_t n;
296 309
297 - if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0)
310 + if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0) {
298 311 (void) snprintf(str, len, "%-6d", (int)zoneid);
299 - else
300 - (void) snprintf(str, len, "%-28s", zone_name);
312 + } else {
313 + n = mbstowcs(NULL, zone_name, 0);
314 + if (n == (size_t)-1)
315 + (void) snprintf(str, len, "%-28s", "ERROR");
316 + else if (trunc && n > width)
317 + (void) snprintf(str, len, "%.*s%c", width - 1,
318 + zone_name, '*');
319 + else
320 + (void) snprintf(str, len, "%-28s", zone_name);
321 + }
301 322 }
302 323
303 324 /*
304 325 * Remove all unprintable characters from process name
305 326 */
306 327 void
307 328 stripfname(char *buf)
308 329 {
309 330 int bytesleft = PRFNSZ;
310 331 wchar_t wchar;
311 332 int length;
312 333 char *cp;
313 334
314 335 buf[bytesleft - 1] = '\0';
315 336
316 337 for (cp = buf; *cp != '\0'; cp += length) {
317 338 length = mbtowc(&wchar, cp, MB_LEN_MAX);
318 339 if (length <= 0) {
319 340 *cp = '\0';
320 341 break;
321 342 }
322 343 if (!iswprint(wchar)) {
323 344 if (bytesleft <= length) {
324 345 *cp = '\0';
325 346 break;
326 347 }
327 348 (void) memmove(cp, cp + length, bytesleft - length);
328 349 length = 0;
329 350 }
330 351 bytesleft -= length;
331 352 }
332 353 }
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX