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/ps/ps.c
+++ new/usr/src/cmd/ps/ps.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.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
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 22 /*
23 + * Copyright (c) 2013 Gary Mills
24 + *
23 25 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 26 * Use is subject to license terms.
25 27 */
26 28
27 29 /*
28 30 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
29 31 */
30 32
31 33 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
32 34 /* All Rights Reserved */
33 35
34 36 /*
35 37 * ps -- print things about processes.
36 38 */
37 39 #include <stdio.h>
38 40 #include <ctype.h>
39 41 #include <string.h>
40 42 #include <errno.h>
41 43 #include <fcntl.h>
42 44 #include <pwd.h>
43 45 #include <grp.h>
44 46 #include <sys/types.h>
45 47 #include <sys/stat.h>
46 48 #include <sys/mkdev.h>
47 49 #include <unistd.h>
48 50 #include <stdlib.h>
49 51 #include <limits.h>
50 52 #include <dirent.h>
51 53 #include <sys/signal.h>
52 54 #include <sys/fault.h>
53 55 #include <sys/syscall.h>
54 56 #include <sys/time.h>
55 57 #include <procfs.h>
56 58 #include <locale.h>
57 59 #include <wctype.h>
58 60 #include <wchar.h>
59 61 #include <libw.h>
60 62 #include <stdarg.h>
61 63 #include <sys/proc.h>
62 64 #include <sys/pset.h>
63 65 #include <project.h>
64 66 #include <zone.h>
65 67
66 68 #define min(a, b) ((a) > (b) ? (b) : (a))
67 69 #define max(a, b) ((a) < (b) ? (b) : (a))
68 70
↓ open down ↓ |
36 lines elided |
↑ open up ↑ |
69 71 #define NTTYS 20 /* initial size of table for -t option */
70 72 #define SIZ 30 /* initial size of tables for -p, -s, -g, -h and -z */
71 73
72 74 /*
73 75 * Size of buffer holding args for t, p, s, g, u, U, G, z options.
74 76 * Set to ZONENAME_MAX, the minimum value needed to allow any
75 77 * zone to be specified.
76 78 */
77 79 #define ARGSIZ ZONENAME_MAX
78 80
79 -#define MAXUGNAME 10 /* max chars in a user/group name or printed u/g id */
81 +#define MAXUGNAME (LOGNAME_MAX_ILLUMOS+2) /* max chars in a user/group */
82 + /* name or printed u/g id */
80 83
81 84 /* Structure for storing user or group info */
82 85 struct ugdata {
83 86 id_t id; /* numeric user-id or group-id */
84 87 char name[MAXUGNAME+1]; /* user/group name, null terminated */
85 88 };
86 89
87 90 struct ughead {
88 91 size_t size; /* number of ugdata structs allocated */
89 92 size_t nent; /* number of active entries */
90 93 struct ugdata *ent; /* pointer to array of actual entries */
91 94 };
92 95
93 96 enum fname { /* enumeration of field names */
94 97 F_USER, /* effective user of the process */
95 98 F_RUSER, /* real user of the process */
96 99 F_GROUP, /* effective group of the process */
97 100 F_RGROUP, /* real group of the process */
98 101 F_UID, /* numeric effective uid of the process */
99 102 F_RUID, /* numeric real uid of the process */
100 103 F_GID, /* numeric effective gid of the process */
101 104 F_RGID, /* numeric real gid of the process */
102 105 F_PID, /* process id */
103 106 F_PPID, /* parent process id */
104 107 F_PGID, /* process group id */
105 108 F_SID, /* session id */
106 109 F_PSR, /* bound processor */
107 110 F_LWP, /* lwp-id */
108 111 F_NLWP, /* number of lwps */
109 112 F_OPRI, /* old priority (obsolete) */
110 113 F_PRI, /* new priority */
111 114 F_F, /* process flags */
112 115 F_S, /* letter indicating the state */
113 116 F_C, /* processor utilization (obsolete) */
114 117 F_PCPU, /* percent of recently used cpu time */
115 118 F_PMEM, /* percent of physical memory used (rss) */
116 119 F_OSZ, /* virtual size of the process in pages */
117 120 F_VSZ, /* virtual size of the process in kilobytes */
118 121 F_RSS, /* resident set size of the process in kilobytes */
119 122 F_NICE, /* "nice" value of the process */
120 123 F_CLASS, /* scheduler class */
121 124 F_STIME, /* start time of the process, hh:mm:ss or Month Day */
122 125 F_ETIME, /* elapsed time of the process, [[dd-]hh:]mm:ss */
123 126 F_TIME, /* cpu time of the process, [[dd-]hh:]mm:ss */
124 127 F_TTY, /* name of the controlling terminal */
125 128 F_ADDR, /* address of the process (obsolete) */
126 129 F_WCHAN, /* wait channel (sleep condition variable) */
127 130 F_FNAME, /* file name of command */
128 131 F_COMM, /* name of command (argv[0] value) */
129 132 F_ARGS, /* name of command plus all its arguments */
130 133 F_TASKID, /* task id */
131 134 F_PROJID, /* project id */
132 135 F_PROJECT, /* project name of the process */
133 136 F_PSET, /* bound processor set */
134 137 F_ZONE, /* zone name */
135 138 F_ZONEID, /* zone id */
136 139 F_CTID, /* process contract id */
137 140 F_LGRP, /* process home lgroup */
138 141 F_DMODEL /* process data model */
139 142 };
140 143
141 144 struct field {
142 145 struct field *next; /* linked list */
143 146 int fname; /* field index */
144 147 const char *header; /* header to use */
145 148 int width; /* width of field */
146 149 };
147 150
148 151 static struct field *fields = NULL; /* fields selected via -o */
149 152 static struct field *last_field = NULL;
150 153 static int do_header = 0;
151 154 static struct timeval now;
152 155
153 156 /* array of defined fields, in fname order */
154 157 struct def_field {
155 158 const char *fname;
156 159 const char *header;
157 160 int width;
158 161 int minwidth;
159 162 };
160 163
161 164 static struct def_field fname[] = {
162 165 /* fname header width minwidth */
163 166 { "user", "USER", 8, 8 },
164 167 { "ruser", "RUSER", 8, 8 },
165 168 { "group", "GROUP", 8, 8 },
166 169 { "rgroup", "RGROUP", 8, 8 },
167 170 { "uid", "UID", 5, 5 },
168 171 { "ruid", "RUID", 5, 5 },
169 172 { "gid", "GID", 5, 5 },
170 173 { "rgid", "RGID", 5, 5 },
171 174 { "pid", "PID", 5, 5 },
172 175 { "ppid", "PPID", 5, 5 },
173 176 { "pgid", "PGID", 5, 5 },
174 177 { "sid", "SID", 5, 5 },
175 178 { "psr", "PSR", 3, 2 },
176 179 { "lwp", "LWP", 6, 2 },
177 180 { "nlwp", "NLWP", 4, 2 },
178 181 { "opri", "PRI", 3, 2 },
179 182 { "pri", "PRI", 3, 2 },
180 183 { "f", "F", 2, 2 },
181 184 { "s", "S", 1, 1 },
182 185 { "c", "C", 2, 2 },
183 186 { "pcpu", "%CPU", 4, 4 },
184 187 { "pmem", "%MEM", 4, 4 },
185 188 { "osz", "SZ", 4, 4 },
186 189 { "vsz", "VSZ", 4, 4 },
187 190 { "rss", "RSS", 4, 4 },
188 191 { "nice", "NI", 2, 2 },
189 192 { "class", "CLS", 4, 2 },
190 193 { "stime", "STIME", 8, 8 },
191 194 { "etime", "ELAPSED", 11, 7 },
192 195 { "time", "TIME", 11, 5 },
193 196 { "tty", "TT", 7, 7 },
194 197 #ifdef _LP64
195 198 { "addr", "ADDR", 16, 8 },
196 199 { "wchan", "WCHAN", 16, 8 },
197 200 #else
198 201 { "addr", "ADDR", 8, 8 },
199 202 { "wchan", "WCHAN", 8, 8 },
200 203 #endif
201 204 { "fname", "COMMAND", 8, 8 },
202 205 { "comm", "COMMAND", 80, 8 },
203 206 { "args", "COMMAND", 80, 80 },
204 207 { "taskid", "TASKID", 5, 5 },
205 208 { "projid", "PROJID", 5, 5 },
206 209 { "project", "PROJECT", 8, 8 },
207 210 { "pset", "PSET", 3, 3 },
208 211 { "zone", "ZONE", 8, 8 },
209 212 { "zoneid", "ZONEID", 5, 5 },
210 213 { "ctid", "CTID", 5, 5 },
211 214 { "lgrp", "LGRP", 4, 2 },
212 215 { "dmodel", "DMODEL", 6, 6 },
213 216 };
214 217
215 218 #define NFIELDS (sizeof (fname) / sizeof (fname[0]))
216 219
217 220 static int retcode = 1;
218 221 static int lflg;
219 222 static int Aflg;
220 223 static int uflg;
221 224 static int Uflg;
222 225 static int Gflg;
223 226 static int aflg;
224 227 static int dflg;
225 228 static int Lflg;
226 229 static int Pflg;
227 230 static int yflg;
228 231 static int pflg;
229 232 static int fflg;
230 233 static int cflg;
231 234 static int jflg;
232 235 static int gflg;
233 236 static int sflg;
234 237 static int tflg;
235 238 static int zflg;
236 239 static int Zflg;
237 240 static int hflg;
238 241 static int Hflg;
239 242 static uid_t tuid = (uid_t)-1;
240 243 static int errflg;
241 244
242 245 static int ndev; /* number of devices */
243 246 static int maxdev; /* number of devl structures allocated */
244 247
245 248 #define DNINCR 100
246 249 #define DNSIZE 14
247 250 static struct devl { /* device list */
248 251 char dname[DNSIZE]; /* device name */
249 252 dev_t ddev; /* device number */
250 253 } *devl;
251 254
252 255 static struct tty {
253 256 char *tname;
254 257 dev_t tdev;
255 258 } *tty = NULL; /* for t option */
256 259 static size_t ttysz = 0;
257 260 static int ntty = 0;
258 261
259 262 static pid_t *pid = NULL; /* for p option */
260 263 static size_t pidsz = 0;
261 264 static size_t npid = 0;
262 265
263 266 static int *lgrps = NULL; /* list of lgroup IDs for for h option */
264 267 static size_t lgrps_size = 0; /* size of the lgrps list */
265 268 static size_t nlgrps = 0; /* number elements in the list */
266 269
267 270 /* Maximum possible lgroup ID value */
268 271 #define MAX_LGRP_ID 256
269 272
270 273 static pid_t *grpid = NULL; /* for g option */
271 274 static size_t grpidsz = 0;
272 275 static int ngrpid = 0;
273 276
274 277 static pid_t *sessid = NULL; /* for s option */
275 278 static size_t sessidsz = 0;
276 279 static int nsessid = 0;
277 280
278 281 static zoneid_t *zoneid = NULL; /* for z option */
279 282 static size_t zoneidsz = 0;
280 283 static int nzoneid = 0;
281 284
282 285 static int kbytes_per_page;
283 286 static int pidwidth;
284 287
285 288 static char *procdir = "/proc"; /* standard /proc directory */
286 289
287 290 static struct ughead euid_tbl; /* table to store selected euid's */
288 291 static struct ughead ruid_tbl; /* table to store selected real uid's */
289 292 static struct ughead egid_tbl; /* table to store selected egid's */
290 293 static struct ughead rgid_tbl; /* table to store selected real gid's */
291 294 static prheader_t *lpsinfobuf; /* buffer to contain lpsinfo */
292 295 static size_t lpbufsize;
293 296
294 297 /*
295 298 * This constant defines the sentinal number of process IDs below which we
296 299 * only examine individual entries in /proc rather than scanning through
297 300 * /proc. This optimization is a huge win in the common case.
298 301 */
299 302 #define PTHRESHOLD 40
300 303
301 304 #define UCB_OPTS "-aceglnrtuvwxSU"
302 305
303 306 static void usage(void);
304 307 static char *getarg(char **);
305 308 static char *parse_format(char *);
306 309 static char *gettty(psinfo_t *);
307 310 static int prfind(int, psinfo_t *, char **);
308 311 static void prcom(psinfo_t *, char *);
309 312 static void prtpct(ushort_t, int);
310 313 static void print_time(time_t, int);
311 314 static void print_field(psinfo_t *, struct field *, const char *);
312 315 static void print_zombie_field(psinfo_t *, struct field *, const char *);
313 316 static void pr_fields(psinfo_t *, const char *,
314 317 void (*print_fld)(psinfo_t *, struct field *, const char *));
315 318 static int search(pid_t *, int, pid_t);
316 319 static void add_ugentry(struct ughead *, char *);
317 320 static int uconv(struct ughead *);
318 321 static int gconv(struct ughead *);
319 322 static int ugfind(id_t, struct ughead *);
320 323 static void prtime(timestruc_t, int, int);
321 324 static void przom(psinfo_t *);
322 325 static int namencnt(char *, int, int);
323 326 static char *err_string(int);
324 327 static int print_proc(char *pname);
325 328 static time_t delta_secs(const timestruc_t *);
326 329 static int str2id(const char *, pid_t *, long, long);
327 330 static int str2uid(const char *, uid_t *, unsigned long, unsigned long);
328 331 static void *Realloc(void *, size_t);
329 332 static int pidcmp(const void *p1, const void *p2);
330 333
331 334 extern int ucbmain(int, char **);
332 335 static int stdmain(int, char **);
333 336
334 337 int
335 338 main(int argc, char **argv)
336 339 {
337 340 const char *me;
338 341
339 342 /*
340 343 * The original two ps'es are linked in a single binary;
341 344 * their main()s are renamed to stdmain for /usr/bin/ps and
342 345 * ucbmain for /usr/ucb/ps.
343 346 * We try to figure out which instance of ps the user wants to run.
344 347 * Traditionally, the UCB variant doesn't require the flag argument
345 348 * start with a "-". If the first argument doesn't start with a
346 349 * "-", we call "ucbmain".
347 350 * If there's a first argument and it starts with a "-", we check
348 351 * whether any of the options isn't acceptable to "ucbmain"; in that
349 352 * case we run "stdmain".
350 353 * If we can't tell from the options which main to call, we check
351 354 * the binary we are running. We default to "stdmain" but
352 355 * any mention in the executable name of "ucb" causes us to call
353 356 * ucbmain.
354 357 */
355 358 if (argv[1] != NULL) {
356 359 if (argv[1][0] != '-')
357 360 return (ucbmain(argc, argv));
358 361 else if (argv[1][strspn(argv[1], UCB_OPTS)] != '\0')
359 362 return (stdmain(argc, argv));
360 363 }
361 364
362 365 me = getexecname();
363 366
364 367 if (me != NULL && strstr(me, "ucb") != NULL)
365 368 return (ucbmain(argc, argv));
366 369 else
367 370 return (stdmain(argc, argv));
368 371 }
369 372
370 373 static int
371 374 stdmain(int argc, char **argv)
372 375 {
373 376 char *p;
374 377 char *p1;
375 378 char *parg;
376 379 int c;
377 380 int i;
378 381 int pgerrflg = 0; /* err flg: non-numeric arg w/p & g options */
379 382 size_t size, len;
380 383 DIR *dirp;
381 384 struct dirent *dentp;
382 385 pid_t maxpid;
383 386 pid_t id;
384 387 int ret;
385 388 char loc_stime_str[32];
386 389
387 390 (void) setlocale(LC_ALL, "");
388 391 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
389 392 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
390 393 #endif
391 394 (void) textdomain(TEXT_DOMAIN);
392 395
393 396 (void) memset(&euid_tbl, 0, sizeof (euid_tbl));
394 397 (void) memset(&ruid_tbl, 0, sizeof (ruid_tbl));
395 398 (void) memset(&egid_tbl, 0, sizeof (egid_tbl));
396 399 (void) memset(&rgid_tbl, 0, sizeof (rgid_tbl));
397 400
398 401 kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024;
399 402
400 403 (void) gettimeofday(&now, NULL);
401 404
402 405 /*
403 406 * calculate width of pid fields based on configured MAXPID
404 407 * (must be at least 5 to retain output format compatibility)
405 408 */
406 409 id = maxpid = (pid_t)sysconf(_SC_MAXPID);
407 410 pidwidth = 1;
408 411 while ((id /= 10) > 0)
409 412 ++pidwidth;
410 413 pidwidth = pidwidth < 5 ? 5 : pidwidth;
411 414
412 415 fname[F_PID].width = fname[F_PPID].width = pidwidth;
413 416 fname[F_PGID].width = fname[F_SID].width = pidwidth;
414 417
415 418 /*
416 419 * TRANSLATION_NOTE
417 420 * Specify the printf format with width and precision for
418 421 * the STIME field.
419 422 */
420 423 len = snprintf(loc_stime_str, sizeof (loc_stime_str),
421 424 dcgettext(NULL, "%8.8s", LC_TIME), "STIME");
422 425 if (len >= sizeof (loc_stime_str))
423 426 len = sizeof (loc_stime_str) - 1;
424 427
425 428 fname[F_STIME].width = fname[F_STIME].minwidth = len;
426 429
427 430 while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:"))
428 431 != EOF)
429 432 switch (c) {
430 433 case 'H': /* Show home lgroups */
431 434 Hflg++;
432 435 break;
433 436 case 'h':
434 437 /*
435 438 * Show processes/threads with given home lgroups
436 439 */
437 440 hflg++;
438 441 p1 = optarg;
439 442 do {
440 443 int id;
441 444
442 445 /*
443 446 * Get all IDs in the list, verify for
444 447 * correctness and place in lgrps array.
445 448 */
446 449 parg = getarg(&p1);
447 450 /* Convert string to integer */
448 451 ret = str2id(parg, (pid_t *)&id, 0,
449 452 MAX_LGRP_ID);
450 453 /* Complain if ID didn't parse correctly */
451 454 if (ret != 0) {
452 455 pgerrflg++;
453 456 (void) fprintf(stderr,
454 457 gettext("ps: %s "), parg);
455 458 if (ret == EINVAL)
456 459 (void) fprintf(stderr,
457 460 gettext("is an invalid "
458 461 "non-numeric argument"));
459 462 else
460 463 (void) fprintf(stderr,
461 464 gettext("exceeds valid "
462 465 "range"));
463 466 (void) fprintf(stderr,
464 467 gettext(" for -h option\n"));
465 468 continue;
466 469 }
467 470
468 471 /* Extend lgrps array if needed */
469 472 if (nlgrps == lgrps_size) {
470 473 /* Double the size of the lgrps array */
471 474 if (lgrps_size == 0)
472 475 lgrps_size = SIZ;
473 476 lgrps_size *= 2;
474 477 lgrps = Realloc(lgrps,
475 478 lgrps_size * sizeof (int));
476 479 }
477 480 /* place the id in the lgrps table */
478 481 lgrps[nlgrps++] = id;
479 482 } while (*p1);
480 483 break;
481 484 case 'l': /* long listing */
482 485 lflg++;
483 486 break;
484 487 case 'f': /* full listing */
485 488 fflg++;
486 489 break;
487 490 case 'j':
488 491 jflg++;
489 492 break;
490 493 case 'c':
491 494 /*
492 495 * Format output to reflect scheduler changes:
493 496 * high numbers for high priorities and don't
494 497 * print nice or p_cpu values. 'c' option only
495 498 * effective when used with 'l' or 'f' options.
496 499 */
497 500 cflg++;
498 501 break;
499 502 case 'A': /* list every process */
500 503 case 'e': /* (obsolete) list every process */
501 504 Aflg++;
502 505 tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0;
503 506 zflg = hflg = 0;
504 507 break;
505 508 case 'a':
506 509 /*
507 510 * Same as 'e' except no session group leaders
508 511 * and no non-terminal processes.
509 512 */
510 513 aflg++;
511 514 break;
512 515 case 'd': /* same as e except no session leaders */
513 516 dflg++;
514 517 break;
515 518 case 'L': /* show lwps */
516 519 Lflg++;
517 520 break;
518 521 case 'P': /* show bound processor */
519 522 Pflg++;
520 523 break;
521 524 case 'y': /* omit F & ADDR, report RSS & SZ in Kby */
522 525 yflg++;
523 526 break;
524 527 case 'n': /* no longer needed; retain as no-op */
525 528 (void) fprintf(stderr,
526 529 gettext("ps: warning: -n option ignored\n"));
527 530 break;
528 531 case 't': /* terminals */
529 532 #define TSZ 30
530 533 tflg++;
531 534 p1 = optarg;
532 535 do {
533 536 char nambuf[TSZ+6]; /* for "/dev/" + '\0' */
534 537 struct stat64 s;
535 538 parg = getarg(&p1);
536 539 p = Realloc(NULL, TSZ+1); /* for '\0' */
537 540 /* zero the buffer before using it */
538 541 p[0] = '\0';
539 542 size = TSZ;
540 543 if (isdigit(*parg)) {
541 544 (void) strcpy(p, "tty");
542 545 size -= 3;
543 546 }
544 547 (void) strncat(p, parg, size);
545 548 if (ntty == ttysz) {
546 549 if ((ttysz *= 2) == 0)
547 550 ttysz = NTTYS;
548 551 tty = Realloc(tty,
549 552 (ttysz + 1) * sizeof (struct tty));
550 553 }
551 554 tty[ntty].tdev = PRNODEV;
552 555 (void) strcpy(nambuf, "/dev/");
553 556 (void) strcat(nambuf, p);
554 557 if (stat64(nambuf, &s) == 0)
555 558 tty[ntty].tdev = s.st_rdev;
556 559 tty[ntty++].tname = p;
557 560 } while (*p1);
558 561 break;
559 562 case 'p': /* proc ids */
560 563 pflg++;
561 564 p1 = optarg;
562 565 do {
563 566 pid_t id;
564 567
565 568 parg = getarg(&p1);
566 569 if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
567 570 pgerrflg++;
568 571 (void) fprintf(stderr,
569 572 gettext("ps: %s "), parg);
570 573 if (ret == EINVAL)
571 574 (void) fprintf(stderr,
572 575 gettext("is an invalid "
573 576 "non-numeric argument"));
574 577 else
575 578 (void) fprintf(stderr,
576 579 gettext("exceeds valid "
577 580 "range"));
578 581 (void) fprintf(stderr,
579 582 gettext(" for -p option\n"));
580 583 continue;
581 584 }
582 585
583 586 if (npid == pidsz) {
584 587 if ((pidsz *= 2) == 0)
585 588 pidsz = SIZ;
586 589 pid = Realloc(pid,
587 590 pidsz * sizeof (pid_t));
588 591 }
589 592 pid[npid++] = id;
590 593 } while (*p1);
591 594 break;
592 595 case 's': /* session */
593 596 sflg++;
594 597 p1 = optarg;
595 598 do {
596 599 pid_t id;
597 600
598 601 parg = getarg(&p1);
599 602 if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
600 603 pgerrflg++;
601 604 (void) fprintf(stderr,
602 605 gettext("ps: %s "), parg);
603 606 if (ret == EINVAL)
604 607 (void) fprintf(stderr,
605 608 gettext("is an invalid "
606 609 "non-numeric argument"));
607 610 else
608 611 (void) fprintf(stderr,
609 612 gettext("exceeds valid "
610 613 "range"));
611 614 (void) fprintf(stderr,
612 615 gettext(" for -s option\n"));
613 616 continue;
614 617 }
615 618
616 619 if (nsessid == sessidsz) {
617 620 if ((sessidsz *= 2) == 0)
618 621 sessidsz = SIZ;
619 622 sessid = Realloc(sessid,
620 623 sessidsz * sizeof (pid_t));
621 624 }
622 625 sessid[nsessid++] = id;
623 626 } while (*p1);
624 627 break;
625 628 case 'g': /* proc group */
626 629 gflg++;
627 630 p1 = optarg;
628 631 do {
629 632 pid_t id;
630 633
631 634 parg = getarg(&p1);
632 635 if ((ret = str2id(parg, &id, 0, maxpid)) != 0) {
633 636 pgerrflg++;
634 637 (void) fprintf(stderr,
635 638 gettext("ps: %s "), parg);
636 639 if (ret == EINVAL)
637 640 (void) fprintf(stderr,
638 641 gettext("is an invalid "
639 642 "non-numeric argument"));
640 643 else
641 644 (void) fprintf(stderr,
642 645 gettext("exceeds valid "
643 646 "range"));
644 647 (void) fprintf(stderr,
645 648 gettext(" for -g option\n"));
646 649 continue;
647 650 }
648 651
649 652 if (ngrpid == grpidsz) {
650 653 if ((grpidsz *= 2) == 0)
651 654 grpidsz = SIZ;
652 655 grpid = Realloc(grpid,
653 656 grpidsz * sizeof (pid_t));
654 657 }
655 658 grpid[ngrpid++] = id;
656 659 } while (*p1);
657 660 break;
658 661 case 'u': /* effective user name or number */
659 662 uflg++;
660 663 p1 = optarg;
661 664 do {
662 665 parg = getarg(&p1);
663 666 add_ugentry(&euid_tbl, parg);
664 667 } while (*p1);
665 668 break;
666 669 case 'U': /* real user name or number */
667 670 Uflg++;
668 671 p1 = optarg;
669 672 do {
670 673 parg = getarg(&p1);
671 674 add_ugentry(&ruid_tbl, parg);
672 675 } while (*p1);
673 676 break;
674 677 case 'G': /* real group name or number */
675 678 Gflg++;
676 679 p1 = optarg;
677 680 do {
678 681 parg = getarg(&p1);
679 682 add_ugentry(&rgid_tbl, parg);
680 683 } while (*p1);
681 684 break;
682 685 case 'o': /* output format */
683 686 p = optarg;
684 687 while ((p = parse_format(p)) != NULL)
685 688 ;
686 689 break;
687 690 case 'z': /* zone name or number */
688 691 zflg++;
689 692 p1 = optarg;
690 693 do {
691 694 zoneid_t id;
692 695
693 696 parg = getarg(&p1);
694 697 if (zone_get_id(parg, &id) != 0) {
695 698 pgerrflg++;
696 699 (void) fprintf(stderr,
697 700 gettext("ps: unknown zone %s\n"),
698 701 parg);
699 702 continue;
700 703 }
701 704
702 705 if (nzoneid == zoneidsz) {
703 706 if ((zoneidsz *= 2) == 0)
704 707 zoneidsz = SIZ;
705 708 zoneid = Realloc(zoneid,
706 709 zoneidsz * sizeof (zoneid_t));
707 710 }
708 711 zoneid[nzoneid++] = id;
709 712 } while (*p1);
710 713 break;
711 714 case 'Z': /* show zone name */
712 715 Zflg++;
713 716 break;
714 717 default: /* error on ? */
715 718 errflg++;
716 719 break;
717 720 }
718 721
719 722 if (errflg || optind < argc || pgerrflg)
720 723 usage();
721 724
722 725 if (tflg)
723 726 tty[ntty].tname = NULL;
724 727 /*
725 728 * If an appropriate option has not been specified, use the
726 729 * current terminal and effective uid as the default.
727 730 */
728 731 if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) {
729 732 psinfo_t info;
730 733 int procfd;
731 734 char *name;
732 735 char pname[100];
733 736
734 737 /* get our own controlling tty name using /proc */
735 738 (void) snprintf(pname, sizeof (pname),
736 739 "%s/self/psinfo", procdir);
737 740 if ((procfd = open(pname, O_RDONLY)) < 0 ||
738 741 read(procfd, (char *)&info, sizeof (info)) < 0 ||
739 742 info.pr_ttydev == PRNODEV) {
740 743 (void) fprintf(stderr,
741 744 gettext("ps: no controlling terminal\n"));
742 745 exit(1);
743 746 }
744 747 (void) close(procfd);
745 748
746 749 i = 0;
747 750 name = gettty(&info);
748 751 if (*name == '?') {
749 752 (void) fprintf(stderr,
750 753 gettext("ps: can't find controlling terminal\n"));
751 754 exit(1);
752 755 }
753 756 if (ntty == ttysz) {
754 757 if ((ttysz *= 2) == 0)
755 758 ttysz = NTTYS;
756 759 tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty));
757 760 }
758 761 tty[ntty].tdev = info.pr_ttydev;
759 762 tty[ntty++].tname = name;
760 763 tty[ntty].tname = NULL;
761 764 tflg++;
762 765 tuid = getuid();
763 766 }
764 767 if (Aflg) {
765 768 Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0;
766 769 zflg = hflg = 0;
767 770 }
768 771 if (Aflg | aflg | dflg)
769 772 tflg = 0;
770 773
771 774 i = 0; /* prepare to exit on name lookup errors */
772 775 i += uconv(&euid_tbl);
773 776 i += uconv(&ruid_tbl);
774 777 i += gconv(&egid_tbl);
775 778 i += gconv(&rgid_tbl);
776 779 if (i)
777 780 exit(1);
778 781
779 782 /* allocate a buffer for lwpsinfo structures */
780 783 lpbufsize = 4096;
781 784 if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) {
782 785 (void) fprintf(stderr,
783 786 gettext("ps: no memory\n"));
784 787 exit(1);
785 788 }
786 789
787 790 if (fields) { /* print user-specified header */
788 791 if (do_header) {
789 792 struct field *f;
790 793
791 794 for (f = fields; f != NULL; f = f->next) {
792 795 if (f != fields)
793 796 (void) printf(" ");
794 797 switch (f->fname) {
795 798 case F_TTY:
796 799 (void) printf("%-*s",
797 800 f->width, f->header);
798 801 break;
799 802 case F_FNAME:
800 803 case F_COMM:
801 804 case F_ARGS:
802 805 /*
803 806 * Print these headers full width
804 807 * unless they appear at the end.
805 808 */
806 809 if (f->next != NULL) {
807 810 (void) printf("%-*s",
808 811 f->width, f->header);
809 812 } else {
810 813 (void) printf("%s",
811 814 f->header);
812 815 }
813 816 break;
814 817 default:
815 818 (void) printf("%*s",
816 819 f->width, f->header);
817 820 break;
818 821 }
819 822 }
820 823 (void) printf("\n");
821 824 }
822 825 } else { /* print standard header */
823 826 /*
824 827 * All fields before 'PID' are printed with a trailing space
825 828 * as a separator and that is how we print the headers too.
826 829 */
827 830 if (lflg) {
828 831 if (yflg)
829 832 (void) printf("S ");
830 833 else
831 834 (void) printf(" F S ");
832 835 }
833 836 if (Zflg)
834 837 (void) printf(" ZONE ");
835 838 if (fflg) {
836 839 (void) printf(" UID ");
837 840 } else if (lflg)
838 841 (void) printf(" UID ");
839 842
840 843 (void) printf("%*s", pidwidth, "PID");
841 844 if (lflg || fflg)
842 845 (void) printf(" %*s", pidwidth, "PPID");
843 846 if (jflg)
844 847 (void) printf(" %*s %*s", pidwidth, "PGID",
845 848 pidwidth, "SID");
846 849 if (Lflg)
847 850 (void) printf(" LWP");
848 851 if (Pflg)
849 852 (void) printf(" PSR");
850 853 if (Lflg && fflg)
851 854 (void) printf(" NLWP");
852 855 if (cflg)
853 856 (void) printf(" CLS PRI");
854 857 else if (lflg || fflg) {
855 858 (void) printf(" C");
856 859 if (lflg)
857 860 (void) printf(" PRI NI");
858 861 }
859 862 if (lflg) {
860 863 if (yflg)
861 864 (void) printf(" RSS SZ WCHAN");
862 865 else
863 866 (void) printf(" ADDR SZ WCHAN");
864 867 }
865 868 if (fflg)
866 869 (void) printf(" %s", loc_stime_str);
867 870 if (Hflg)
868 871 (void) printf(" LGRP");
869 872 if (Lflg)
870 873 (void) printf(" TTY LTIME CMD\n");
871 874 else
872 875 (void) printf(" TTY TIME CMD\n");
873 876 }
874 877
875 878
876 879 if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) &&
877 880 npid <= PTHRESHOLD) {
878 881 /*
879 882 * If we are looking at specific processes go straight
880 883 * to their /proc entries and don't scan /proc.
881 884 */
882 885 int i;
883 886
884 887 (void) qsort(pid, npid, sizeof (pid_t), pidcmp);
885 888 for (i = 0; i < npid; i++) {
886 889 char pname[12];
887 890
888 891 if (i >= 1 && pid[i] == pid[i - 1])
889 892 continue;
890 893 (void) sprintf(pname, "%d", (int)pid[i]);
891 894 if (print_proc(pname) == 0)
892 895 retcode = 0;
893 896 }
894 897 } else {
895 898 /*
896 899 * Determine which processes to print info about by searching
897 900 * the /proc directory and looking at each process.
898 901 */
899 902 if ((dirp = opendir(procdir)) == NULL) {
900 903 (void) fprintf(stderr,
901 904 gettext("ps: cannot open PROC directory %s\n"),
902 905 procdir);
903 906 exit(1);
904 907 }
905 908
906 909 /* for each active process --- */
907 910 while (dentp = readdir(dirp)) {
908 911 if (dentp->d_name[0] == '.') /* skip . and .. */
909 912 continue;
910 913 if (print_proc(dentp->d_name) == 0)
911 914 retcode = 0;
912 915 }
913 916
914 917 (void) closedir(dirp);
915 918 }
916 919 return (retcode);
917 920 }
918 921
919 922
920 923 int
921 924 print_proc(char *pid_name)
922 925 {
923 926 char pname[PATH_MAX];
924 927 int pdlen;
925 928 int found;
926 929 int procfd; /* filedescriptor for /proc/nnnnn/psinfo */
927 930 char *tp; /* ptr to ttyname, if any */
928 931 psinfo_t info; /* process information from /proc */
929 932 lwpsinfo_t *lwpsinfo; /* array of lwpsinfo structs */
930 933
931 934 pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name);
932 935 if (pdlen >= sizeof (pname) - 10)
933 936 return (1);
934 937 retry:
935 938 (void) strcpy(&pname[pdlen], "psinfo");
936 939 if ((procfd = open(pname, O_RDONLY)) == -1) {
937 940 /* Process may have exited meanwhile. */
938 941 return (1);
939 942 }
940 943 /*
941 944 * Get the info structure for the process and close quickly.
942 945 */
943 946 if (read(procfd, (char *)&info, sizeof (info)) < 0) {
944 947 int saverr = errno;
945 948
946 949 (void) close(procfd);
947 950 if (saverr == EAGAIN)
948 951 goto retry;
949 952 if (saverr != ENOENT)
950 953 (void) fprintf(stderr,
951 954 gettext("ps: read() on %s: %s\n"),
952 955 pname, err_string(saverr));
953 956 return (1);
954 957 }
955 958 (void) close(procfd);
956 959
957 960 found = 0;
958 961 if (info.pr_lwp.pr_state == 0) /* can't happen? */
959 962 return (1);
960 963
961 964 /*
962 965 * Omit session group leaders for 'a' and 'd' options.
963 966 */
964 967 if ((info.pr_pid == info.pr_sid) && (dflg || aflg))
965 968 return (1);
966 969 if (Aflg || dflg)
967 970 found++;
968 971 else if (pflg && search(pid, npid, info.pr_pid))
969 972 found++; /* ppid in p option arg list */
970 973 else if (uflg && ugfind((id_t)info.pr_euid, &euid_tbl))
971 974 found++; /* puid in u option arg list */
972 975 else if (Uflg && ugfind((id_t)info.pr_uid, &ruid_tbl))
973 976 found++; /* puid in U option arg list */
974 977 #ifdef NOT_YET
975 978 else if (gflg && ugfind((id_t)info.pr_egid, &egid_tbl))
976 979 found++; /* pgid in g option arg list */
977 980 #endif /* NOT_YET */
978 981 else if (Gflg && ugfind((id_t)info.pr_gid, &rgid_tbl))
979 982 found++; /* pgid in G option arg list */
980 983 else if (gflg && search(grpid, ngrpid, info.pr_pgid))
981 984 found++; /* grpid in g option arg list */
982 985 else if (sflg && search(sessid, nsessid, info.pr_sid))
983 986 found++; /* sessid in s option arg list */
984 987 else if (zflg && search(zoneid, nzoneid, info.pr_zoneid))
985 988 found++; /* zoneid in z option arg list */
986 989 else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp))
987 990 found++; /* home lgroup in h option arg list */
988 991 if (!found && !tflg && !aflg)
989 992 return (1);
990 993 if (!prfind(found, &info, &tp))
991 994 return (1);
992 995 if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) {
993 996 ssize_t prsz;
994 997
995 998 (void) strcpy(&pname[pdlen], "lpsinfo");
996 999 if ((procfd = open(pname, O_RDONLY)) == -1)
997 1000 return (1);
998 1001 /*
999 1002 * Get the info structures for the lwps.
1000 1003 */
1001 1004 prsz = read(procfd, lpsinfobuf, lpbufsize);
1002 1005 if (prsz == -1) {
1003 1006 int saverr = errno;
1004 1007
1005 1008 (void) close(procfd);
1006 1009 if (saverr == EAGAIN)
1007 1010 goto retry;
1008 1011 if (saverr != ENOENT)
1009 1012 (void) fprintf(stderr,
1010 1013 gettext("ps: read() on %s: %s\n"),
1011 1014 pname, err_string(saverr));
1012 1015 return (1);
1013 1016 }
1014 1017 (void) close(procfd);
1015 1018 if (prsz == lpbufsize) {
1016 1019 /*
1017 1020 * buffer overflow. Realloc new buffer.
1018 1021 * Error handling is done in Realloc().
1019 1022 */
1020 1023 lpbufsize *= 2;
1021 1024 lpsinfobuf = Realloc(lpsinfobuf, lpbufsize);
1022 1025 goto retry;
1023 1026 }
1024 1027 if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb))
1025 1028 goto retry;
1026 1029 lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1);
1027 1030 }
1028 1031 if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) {
1029 1032 prcom(&info, tp);
1030 1033 } else {
1031 1034 int nlwp = 0;
1032 1035
1033 1036 do {
1034 1037 info.pr_lwp = *lwpsinfo;
1035 1038 prcom(&info, tp);
1036 1039 /* LINTED improper alignment */
1037 1040 lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo +
1038 1041 lpsinfobuf->pr_entsize);
1039 1042 } while (++nlwp < lpsinfobuf->pr_nent);
1040 1043 }
1041 1044 return (0);
1042 1045 }
1043 1046
1044 1047 static int
1045 1048 field_cmp(const void *l, const void *r)
1046 1049 {
1047 1050 struct def_field *lhs = *((struct def_field **)l);
1048 1051 struct def_field *rhs = *((struct def_field **)r);
1049 1052
1050 1053 return (strcmp(lhs->fname, rhs->fname));
1051 1054 }
1052 1055
1053 1056 static void
1054 1057 usage(void) /* print usage message and quit */
1055 1058 {
1056 1059 struct def_field *df, *sorted[NFIELDS];
1057 1060 int pos = 80, i = 0;
1058 1061
1059 1062 static char usage1[] =
1060 1063 "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]";
1061 1064 static char usage2[] =
1062 1065 "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]";
1063 1066 static char usage3[] =
1064 1067 "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ]";
1065 1068 static char usage4[] =
1066 1069 "\t[ -z zonelist ] [-h lgrplist]";
1067 1070 static char usage5[] =
1068 1071 " 'format' is one or more of:";
1069 1072
1070 1073 (void) fprintf(stderr,
1071 1074 gettext("usage: %s\n%s\n%s\n%s\n%s"),
1072 1075 gettext(usage1), gettext(usage2), gettext(usage3),
1073 1076 gettext(usage4), gettext(usage5));
1074 1077
1075 1078 /*
1076 1079 * Now print out the possible output formats such that they neatly fit
1077 1080 * into eighty columns. Note that the fact that we are determining
1078 1081 * this output programmatically means that a gettext() is impossible --
1079 1082 * but it would be a mistake to localize the output formats anyway as
1080 1083 * they are tokens for input, not output themselves.
1081 1084 */
1082 1085 for (df = &fname[0]; df < &fname[NFIELDS]; df++)
1083 1086 sorted[i++] = df;
1084 1087
1085 1088 (void) qsort(sorted, NFIELDS, sizeof (void *), field_cmp);
1086 1089
1087 1090 for (i = 0; i < NFIELDS; i++) {
1088 1091 if (pos + strlen((df = sorted[i])->fname) + 1 >= 80) {
1089 1092 (void) fprintf(stderr, "\n\t");
1090 1093 pos = 8;
1091 1094 }
1092 1095
1093 1096 (void) fprintf(stderr, "%s%s", pos > 8 ? " " : "", df->fname);
1094 1097 pos += strlen(df->fname) + 1;
1095 1098 }
1096 1099
1097 1100 (void) fprintf(stderr, "\n");
1098 1101
1099 1102 exit(1);
1100 1103 }
1101 1104
1102 1105 /*
1103 1106 * getarg() finds the next argument in list and copies arg into argbuf.
1104 1107 * p1 first pts to arg passed back from getopt routine. p1 is then
1105 1108 * bumped to next character that is not a comma or blank -- p1 NULL
1106 1109 * indicates end of list.
1107 1110 */
1108 1111 static char *
1109 1112 getarg(char **pp1)
1110 1113 {
1111 1114 static char argbuf[ARGSIZ];
1112 1115 char *p1 = *pp1;
1113 1116 char *parga = argbuf;
1114 1117 int c;
1115 1118
1116 1119 while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
1117 1120 p1++;
1118 1121
1119 1122 while ((c = *p1) != '\0' && c != ',' && !isspace(c)) {
1120 1123 if (parga < argbuf + ARGSIZ - 1)
1121 1124 *parga++ = c;
1122 1125 p1++;
1123 1126 }
1124 1127 *parga = '\0';
1125 1128
1126 1129 while ((c = *p1) != '\0' && (c == ',' || isspace(c)))
1127 1130 p1++;
1128 1131
1129 1132 *pp1 = p1;
1130 1133
1131 1134 return (argbuf);
1132 1135 }
1133 1136
1134 1137 /*
1135 1138 * parse_format() takes the argument to the -o option,
1136 1139 * sets up the next output field structure, and returns
1137 1140 * a pointer to any further output field specifier(s).
1138 1141 * As a side-effect, it increments errflg if encounters a format error.
1139 1142 */
1140 1143 static char *
1141 1144 parse_format(char *arg)
1142 1145 {
1143 1146 int c;
1144 1147 char *name;
1145 1148 char *header = NULL;
1146 1149 int width = 0;
1147 1150 struct def_field *df;
1148 1151 struct field *f;
1149 1152
1150 1153 while ((c = *arg) != '\0' && (c == ',' || isspace(c)))
1151 1154 arg++;
1152 1155 if (c == '\0')
1153 1156 return (NULL);
1154 1157 name = arg;
1155 1158 arg = strpbrk(arg, " \t\r\v\f\n,=");
1156 1159 if (arg != NULL) {
1157 1160 c = *arg;
1158 1161 *arg++ = '\0';
1159 1162 if (c == '=') {
1160 1163 char *s;
1161 1164
1162 1165 header = arg;
1163 1166 arg = NULL;
1164 1167 width = strlen(header);
1165 1168 s = header + width;
1166 1169 while (s > header && isspace(*--s))
1167 1170 *s = '\0';
1168 1171 while (isspace(*header))
1169 1172 header++;
1170 1173 }
1171 1174 }
1172 1175 for (df = &fname[0]; df < &fname[NFIELDS]; df++)
1173 1176 if (strcmp(name, df->fname) == 0) {
1174 1177 if (strcmp(name, "lwp") == 0)
1175 1178 Lflg++;
1176 1179 break;
1177 1180 }
1178 1181 if (df >= &fname[NFIELDS]) {
1179 1182 (void) fprintf(stderr,
1180 1183 gettext("ps: unknown output format: -o %s\n"),
1181 1184 name);
1182 1185 errflg++;
1183 1186 return (arg);
1184 1187 }
1185 1188 if ((f = malloc(sizeof (*f))) == NULL) {
1186 1189 (void) fprintf(stderr,
1187 1190 gettext("ps: malloc() for output format failed, %s\n"),
1188 1191 err_string(errno));
1189 1192 exit(1);
1190 1193 }
1191 1194 f->next = NULL;
1192 1195 f->fname = df - &fname[0];
1193 1196 f->header = header? header : df->header;
1194 1197 if (width == 0)
1195 1198 width = df->width;
1196 1199 if (*f->header != '\0')
1197 1200 do_header = 1;
1198 1201 f->width = max(width, df->minwidth);
1199 1202
1200 1203 if (fields == NULL)
1201 1204 fields = last_field = f;
1202 1205 else {
1203 1206 last_field->next = f;
1204 1207 last_field = f;
1205 1208 }
1206 1209
1207 1210 return (arg);
1208 1211 }
1209 1212
1210 1213 static char *
1211 1214 devlookup(dev_t ddev)
1212 1215 {
1213 1216 struct devl *dp;
1214 1217 int i;
1215 1218
1216 1219 for (dp = devl, i = 0; i < ndev; dp++, i++) {
1217 1220 if (dp->ddev == ddev)
1218 1221 return (dp->dname);
1219 1222 }
1220 1223 return (NULL);
1221 1224 }
1222 1225
1223 1226 static char *
1224 1227 devadd(char *name, dev_t ddev)
1225 1228 {
1226 1229 struct devl *dp;
1227 1230 int leng, start, i;
1228 1231
1229 1232 if (ndev == maxdev) {
1230 1233 maxdev += DNINCR;
1231 1234 devl = Realloc(devl, maxdev * sizeof (struct devl));
1232 1235 }
1233 1236 dp = &devl[ndev++];
1234 1237
1235 1238 dp->ddev = ddev;
1236 1239 if (name == NULL) {
1237 1240 (void) strcpy(dp->dname, "??");
1238 1241 return (dp->dname);
1239 1242 }
1240 1243
1241 1244 leng = strlen(name);
1242 1245 /* Strip off /dev/ */
1243 1246 if (leng < DNSIZE + 4)
1244 1247 (void) strcpy(dp->dname, &name[5]);
1245 1248 else {
1246 1249 start = leng - DNSIZE - 1;
1247 1250
1248 1251 for (i = start; i < leng && name[i] != '/'; i++)
1249 1252 ;
1250 1253 if (i == leng)
1251 1254 (void) strncpy(dp->dname, &name[start], DNSIZE);
1252 1255 else
1253 1256 (void) strncpy(dp->dname, &name[i+1], DNSIZE);
1254 1257 }
1255 1258 return (dp->dname);
1256 1259 }
1257 1260
1258 1261 /*
1259 1262 * gettty returns the user's tty number or ? if none.
1260 1263 */
1261 1264 static char *
1262 1265 gettty(psinfo_t *psinfo)
1263 1266 {
1264 1267 extern char *_ttyname_dev(dev_t, char *, size_t);
1265 1268 static zoneid_t zid = -1;
1266 1269 char devname[TTYNAME_MAX];
1267 1270 char *retval;
1268 1271
1269 1272 if (zid == -1)
1270 1273 zid = getzoneid();
1271 1274
1272 1275 if (psinfo->pr_ttydev == PRNODEV || psinfo->pr_zoneid != zid)
1273 1276 return ("?");
1274 1277
1275 1278 if ((retval = devlookup(psinfo->pr_ttydev)) != NULL)
1276 1279 return (retval);
1277 1280
1278 1281 retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname));
1279 1282
1280 1283 return (devadd(retval, psinfo->pr_ttydev));
1281 1284 }
1282 1285
1283 1286 /*
1284 1287 * Find the process's tty and return 1 if process is to be printed.
1285 1288 */
1286 1289 static int
1287 1290 prfind(int found, psinfo_t *psinfo, char **tpp)
1288 1291 {
1289 1292 char *tp;
1290 1293 struct tty *ttyp;
1291 1294
1292 1295 if (psinfo->pr_nlwp == 0) {
1293 1296 /* process is a zombie */
1294 1297 *tpp = "?";
1295 1298 if (tflg && !found)
1296 1299 return (0);
1297 1300 return (1);
1298 1301 }
1299 1302
1300 1303 /*
1301 1304 * Get current terminal. If none ("?") and 'a' is set, don't print
1302 1305 * info. If 't' is set, check if term is in list of desired terminals
1303 1306 * and print it if it is.
1304 1307 */
1305 1308 tp = gettty(psinfo);
1306 1309 if (aflg && *tp == '?') {
1307 1310 *tpp = tp;
1308 1311 return (0);
1309 1312 }
1310 1313 if (tflg && !found) {
1311 1314 int match = 0;
1312 1315 char *other = NULL;
1313 1316 for (ttyp = tty; ttyp->tname != NULL; ttyp++) {
1314 1317 /*
1315 1318 * Look for a name match
1316 1319 */
1317 1320 if (strcmp(tp, ttyp->tname) == 0) {
1318 1321 match = 1;
1319 1322 break;
1320 1323 }
1321 1324 /*
1322 1325 * Look for same device under different names.
1323 1326 */
1324 1327 if ((other == NULL) &&
1325 1328 (ttyp->tdev != PRNODEV) &&
1326 1329 (psinfo->pr_ttydev == ttyp->tdev))
1327 1330 other = ttyp->tname;
1328 1331 }
1329 1332 if (!match && (other != NULL)) {
1330 1333 /*
1331 1334 * found under a different name
1332 1335 */
1333 1336 match = 1;
1334 1337 tp = other;
1335 1338 }
1336 1339 if (!match || (tuid != (uid_t)-1 && tuid != psinfo->pr_euid)) {
1337 1340 /*
1338 1341 * not found OR not matching euid
1339 1342 */
1340 1343 *tpp = tp;
1341 1344 return (0);
1342 1345 }
1343 1346 }
1344 1347 *tpp = tp;
1345 1348 return (1);
1346 1349 }
1347 1350
1348 1351 /*
1349 1352 * Print info about the process.
1350 1353 */
1351 1354 static void
1352 1355 prcom(psinfo_t *psinfo, char *ttyp)
1353 1356 {
1354 1357 char *cp;
1355 1358 long tm;
1356 1359 int bytesleft;
1357 1360 int wcnt, length;
1358 1361 wchar_t wchar;
1359 1362 struct passwd *pwd;
1360 1363 int zombie_lwp;
1361 1364 char zonename[ZONENAME_MAX];
1362 1365
1363 1366 /*
1364 1367 * If process is zombie, call zombie print routine and return.
1365 1368 */
1366 1369 if (psinfo->pr_nlwp == 0) {
1367 1370 if (fields != NULL)
1368 1371 pr_fields(psinfo, ttyp, print_zombie_field);
1369 1372 else
1370 1373 przom(psinfo);
1371 1374 return;
1372 1375 }
1373 1376
1374 1377 zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
1375 1378
1376 1379 /*
1377 1380 * If user specified '-o format', print requested fields and return.
1378 1381 */
1379 1382 if (fields != NULL) {
1380 1383 pr_fields(psinfo, ttyp, print_field);
1381 1384 return;
1382 1385 }
1383 1386
1384 1387 /*
1385 1388 * All fields before 'PID' are printed with a trailing space as a
1386 1389 * separator, rather than keeping track of which column is first. All
1387 1390 * other fields are printed with a leading space.
1388 1391 */
1389 1392 if (lflg) {
1390 1393 if (!yflg)
1391 1394 (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
1392 1395 (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */
1393 1396 }
1394 1397
1395 1398 if (Zflg) { /* ZONE */
1396 1399 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1397 1400 sizeof (zonename)) < 0) {
1398 1401 (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
1399 1402 } else {
1400 1403 (void) printf("%8.8s ", zonename);
1401 1404 }
1402 1405 }
1403 1406
1404 1407 if (fflg) { /* UID */
1405 1408 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
1406 1409 (void) printf("%8.8s ", pwd->pw_name);
1407 1410 else
1408 1411 (void) printf(" %7.7u ", psinfo->pr_euid);
1409 1412 } else if (lflg) {
1410 1413 (void) printf("%6u ", psinfo->pr_euid);
1411 1414 }
1412 1415 (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
1413 1416 if (lflg || fflg)
1414 1417 (void) printf(" %*d", pidwidth,
1415 1418 (int)psinfo->pr_ppid); /* PPID */
1416 1419 if (jflg) {
1417 1420 (void) printf(" %*d", pidwidth,
1418 1421 (int)psinfo->pr_pgid); /* PGID */
1419 1422 (void) printf(" %*d", pidwidth,
1420 1423 (int)psinfo->pr_sid); /* SID */
1421 1424 }
1422 1425 if (Lflg)
1423 1426 (void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */
1424 1427 if (Pflg) {
1425 1428 if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */
1426 1429 (void) printf(" -");
1427 1430 else
1428 1431 (void) printf(" %3d", psinfo->pr_lwp.pr_bindpro);
1429 1432 }
1430 1433 if (Lflg && fflg) /* NLWP */
1431 1434 (void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb);
1432 1435 if (cflg) {
1433 1436 if (zombie_lwp) /* CLS */
1434 1437 (void) printf(" ");
1435 1438 else
1436 1439 (void) printf(" %4s", psinfo->pr_lwp.pr_clname);
1437 1440 (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */
1438 1441 } else if (lflg || fflg) {
1439 1442 (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */
1440 1443 if (lflg) { /* PRI NI */
1441 1444 /*
1442 1445 * Print priorities the old way (lower numbers
1443 1446 * mean higher priority) and print nice value
1444 1447 * for time sharing procs.
1445 1448 */
1446 1449 (void) printf(" %3d", psinfo->pr_lwp.pr_oldpri);
1447 1450 if (psinfo->pr_lwp.pr_oldpri != 0)
1448 1451 (void) printf(" %2d", psinfo->pr_lwp.pr_nice);
1449 1452 else
1450 1453 (void) printf(" %2.2s",
1451 1454 psinfo->pr_lwp.pr_clname);
1452 1455 }
1453 1456 }
1454 1457 if (lflg) {
1455 1458 if (yflg) {
1456 1459 if (psinfo->pr_flag & SSYS) /* RSS */
1457 1460 (void) printf(" 0");
1458 1461 else if (psinfo->pr_rssize)
1459 1462 (void) printf(" %5lu",
1460 1463 (ulong_t)psinfo->pr_rssize);
1461 1464 else
1462 1465 (void) printf(" ?");
1463 1466 if (psinfo->pr_flag & SSYS) /* SZ */
1464 1467 (void) printf(" 0");
1465 1468 else if (psinfo->pr_size)
1466 1469 (void) printf(" %6lu",
1467 1470 (ulong_t)psinfo->pr_size);
1468 1471 else
1469 1472 (void) printf(" ?");
1470 1473 } else {
1471 1474 #ifndef _LP64
1472 1475 if (psinfo->pr_addr) /* ADDR */
1473 1476 (void) printf(" %8lx",
1474 1477 (ulong_t)psinfo->pr_addr);
1475 1478 else
1476 1479 #endif
1477 1480 (void) printf(" ?");
1478 1481 if (psinfo->pr_flag & SSYS) /* SZ */
1479 1482 (void) printf(" 0");
1480 1483 else if (psinfo->pr_size)
1481 1484 (void) printf(" %6lu",
1482 1485 (ulong_t)psinfo->pr_size / kbytes_per_page);
1483 1486 else
1484 1487 (void) printf(" ?");
1485 1488 }
1486 1489 if (psinfo->pr_lwp.pr_sname != 'S') /* WCHAN */
1487 1490 (void) printf(" ");
1488 1491 #ifndef _LP64
1489 1492 else if (psinfo->pr_lwp.pr_wchan)
1490 1493 (void) printf(" %8lx",
1491 1494 (ulong_t)psinfo->pr_lwp.pr_wchan);
1492 1495 #endif
1493 1496 else
1494 1497 (void) printf(" ?");
1495 1498 }
1496 1499 if (fflg) { /* STIME */
1497 1500 int width = fname[F_STIME].width;
1498 1501 if (Lflg)
1499 1502 prtime(psinfo->pr_lwp.pr_start, width + 1, 1);
1500 1503 else
1501 1504 prtime(psinfo->pr_start, width + 1, 1);
1502 1505 }
1503 1506
1504 1507 if (Hflg) {
1505 1508 /* Display home lgroup */
1506 1509 (void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp);
1507 1510 }
1508 1511
1509 1512 (void) printf(" %-8.14s", ttyp); /* TTY */
1510 1513 if (Lflg) {
1511 1514 tm = psinfo->pr_lwp.pr_time.tv_sec;
1512 1515 if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
1513 1516 tm++;
1514 1517 } else {
1515 1518 tm = psinfo->pr_time.tv_sec;
1516 1519 if (psinfo->pr_time.tv_nsec > 500000000)
1517 1520 tm++;
1518 1521 }
1519 1522 (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* [L]TIME */
1520 1523
1521 1524 if (zombie_lwp) {
1522 1525 (void) printf(" <defunct>\n");
1523 1526 return;
1524 1527 }
1525 1528
1526 1529 if (!fflg) { /* CMD */
1527 1530 wcnt = namencnt(psinfo->pr_fname, 16, 8);
1528 1531 (void) printf(" %.*s\n", wcnt, psinfo->pr_fname);
1529 1532 return;
1530 1533 }
1531 1534
1532 1535
1533 1536 /*
1534 1537 * PRARGSZ == length of cmd arg string.
1535 1538 */
1536 1539 psinfo->pr_psargs[PRARGSZ-1] = '\0';
1537 1540 bytesleft = PRARGSZ;
1538 1541 for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
1539 1542 length = mbtowc(&wchar, cp, MB_LEN_MAX);
1540 1543 if (length == 0)
1541 1544 break;
1542 1545 if (length < 0 || !iswprint(wchar)) {
1543 1546 if (length < 0)
1544 1547 length = 1;
1545 1548 if (bytesleft <= length) {
1546 1549 *cp = '\0';
1547 1550 break;
1548 1551 }
1549 1552 /* omit the unprintable character */
1550 1553 (void) memmove(cp, cp+length, bytesleft-length);
1551 1554 length = 0;
1552 1555 }
1553 1556 bytesleft -= length;
1554 1557 }
1555 1558 wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ);
1556 1559 (void) printf(" %.*s\n", wcnt, psinfo->pr_psargs);
1557 1560 }
1558 1561
1559 1562 /*
1560 1563 * Print percent from 16-bit binary fraction [0 .. 1]
1561 1564 * Round up .01 to .1 to indicate some small percentage (the 0x7000 below).
1562 1565 */
1563 1566 static void
1564 1567 prtpct(ushort_t pct, int width)
1565 1568 {
1566 1569 uint_t value = pct; /* need 32 bits to compute with */
1567 1570
1568 1571 value = ((value * 1000) + 0x7000) >> 15; /* [0 .. 1000] */
1569 1572 if (value >= 1000)
1570 1573 value = 999;
1571 1574 if ((width -= 2) < 2)
1572 1575 width = 2;
1573 1576 (void) printf("%*u.%u", width, value / 10, value % 10);
1574 1577 }
1575 1578
1576 1579 static void
1577 1580 print_time(time_t tim, int width)
1578 1581 {
1579 1582 char buf[30];
1580 1583 time_t seconds;
1581 1584 time_t minutes;
1582 1585 time_t hours;
1583 1586 time_t days;
1584 1587
1585 1588 if (tim < 0) {
1586 1589 (void) printf("%*s", width, "-");
1587 1590 return;
1588 1591 }
1589 1592
1590 1593 seconds = tim % 60;
1591 1594 tim /= 60;
1592 1595 minutes = tim % 60;
1593 1596 tim /= 60;
1594 1597 hours = tim % 24;
1595 1598 days = tim / 24;
1596 1599
1597 1600 if (days > 0) {
1598 1601 (void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld",
1599 1602 days, hours, minutes, seconds);
1600 1603 } else if (hours > 0) {
1601 1604 (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld",
1602 1605 hours, minutes, seconds);
1603 1606 } else {
1604 1607 (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld",
1605 1608 minutes, seconds);
1606 1609 }
1607 1610
1608 1611 (void) printf("%*s", width, buf);
1609 1612 }
1610 1613
1611 1614 static void
1612 1615 print_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
1613 1616 {
1614 1617 int width = f->width;
1615 1618 struct passwd *pwd;
1616 1619 struct group *grp;
1617 1620 time_t cputime;
1618 1621 int bytesleft;
1619 1622 int wcnt;
1620 1623 wchar_t wchar;
1621 1624 char *cp;
1622 1625 int length;
1623 1626 ulong_t mask;
1624 1627 char c, *csave;
1625 1628 int zombie_lwp;
1626 1629
1627 1630 zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z');
1628 1631
1629 1632 switch (f->fname) {
1630 1633 case F_RUSER:
1631 1634 if ((pwd = getpwuid(psinfo->pr_uid)) != NULL)
1632 1635 (void) printf("%*s", width, pwd->pw_name);
1633 1636 else
1634 1637 (void) printf("%*u", width, psinfo->pr_uid);
1635 1638 break;
1636 1639 case F_USER:
1637 1640 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
1638 1641 (void) printf("%*s", width, pwd->pw_name);
1639 1642 else
1640 1643 (void) printf("%*u", width, psinfo->pr_euid);
1641 1644 break;
1642 1645 case F_RGROUP:
1643 1646 if ((grp = getgrgid(psinfo->pr_gid)) != NULL)
1644 1647 (void) printf("%*s", width, grp->gr_name);
1645 1648 else
1646 1649 (void) printf("%*u", width, psinfo->pr_gid);
1647 1650 break;
1648 1651 case F_GROUP:
1649 1652 if ((grp = getgrgid(psinfo->pr_egid)) != NULL)
1650 1653 (void) printf("%*s", width, grp->gr_name);
1651 1654 else
1652 1655 (void) printf("%*u", width, psinfo->pr_egid);
1653 1656 break;
1654 1657 case F_RUID:
1655 1658 (void) printf("%*u", width, psinfo->pr_uid);
1656 1659 break;
1657 1660 case F_UID:
1658 1661 (void) printf("%*u", width, psinfo->pr_euid);
1659 1662 break;
1660 1663 case F_RGID:
1661 1664 (void) printf("%*u", width, psinfo->pr_gid);
1662 1665 break;
1663 1666 case F_GID:
1664 1667 (void) printf("%*u", width, psinfo->pr_egid);
1665 1668 break;
1666 1669 case F_PID:
1667 1670 (void) printf("%*d", width, (int)psinfo->pr_pid);
1668 1671 break;
1669 1672 case F_PPID:
1670 1673 (void) printf("%*d", width, (int)psinfo->pr_ppid);
1671 1674 break;
1672 1675 case F_PGID:
1673 1676 (void) printf("%*d", width, (int)psinfo->pr_pgid);
1674 1677 break;
1675 1678 case F_SID:
1676 1679 (void) printf("%*d", width, (int)psinfo->pr_sid);
1677 1680 break;
1678 1681 case F_PSR:
1679 1682 if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE)
1680 1683 (void) printf("%*s", width, "-");
1681 1684 else
1682 1685 (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro);
1683 1686 break;
1684 1687 case F_LWP:
1685 1688 (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid);
1686 1689 break;
1687 1690 case F_NLWP:
1688 1691 (void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb);
1689 1692 break;
1690 1693 case F_OPRI:
1691 1694 if (zombie_lwp)
1692 1695 (void) printf("%*s", width, "-");
1693 1696 else
1694 1697 (void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri);
1695 1698 break;
1696 1699 case F_PRI:
1697 1700 if (zombie_lwp)
1698 1701 (void) printf("%*s", width, "-");
1699 1702 else
1700 1703 (void) printf("%*d", width, psinfo->pr_lwp.pr_pri);
1701 1704 break;
1702 1705 case F_F:
1703 1706 mask = 0xffffffffUL;
1704 1707 if (width < 8)
1705 1708 mask >>= (8 - width) * 4;
1706 1709 (void) printf("%*lx", width, psinfo->pr_flag & mask);
1707 1710 break;
1708 1711 case F_S:
1709 1712 (void) printf("%*c", width, psinfo->pr_lwp.pr_sname);
1710 1713 break;
1711 1714 case F_C:
1712 1715 if (zombie_lwp)
1713 1716 (void) printf("%*s", width, "-");
1714 1717 else
1715 1718 (void) printf("%*d", width, psinfo->pr_lwp.pr_cpu);
1716 1719 break;
1717 1720 case F_PCPU:
1718 1721 if (zombie_lwp)
1719 1722 (void) printf("%*s", width, "-");
1720 1723 else if (Lflg)
1721 1724 prtpct(psinfo->pr_lwp.pr_pctcpu, width);
1722 1725 else
1723 1726 prtpct(psinfo->pr_pctcpu, width);
1724 1727 break;
1725 1728 case F_PMEM:
1726 1729 prtpct(psinfo->pr_pctmem, width);
1727 1730 break;
1728 1731 case F_OSZ:
1729 1732 (void) printf("%*lu", width,
1730 1733 (ulong_t)psinfo->pr_size / kbytes_per_page);
1731 1734 break;
1732 1735 case F_VSZ:
1733 1736 (void) printf("%*lu", width, (ulong_t)psinfo->pr_size);
1734 1737 break;
1735 1738 case F_RSS:
1736 1739 (void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize);
1737 1740 break;
1738 1741 case F_NICE:
1739 1742 /* if pr_oldpri is zero, then this class has no nice */
1740 1743 if (zombie_lwp)
1741 1744 (void) printf("%*s", width, "-");
1742 1745 else if (psinfo->pr_lwp.pr_oldpri != 0)
1743 1746 (void) printf("%*d", width, psinfo->pr_lwp.pr_nice);
1744 1747 else
1745 1748 (void) printf("%*.*s", width, width,
1746 1749 psinfo->pr_lwp.pr_clname);
1747 1750 break;
1748 1751 case F_CLASS:
1749 1752 if (zombie_lwp)
1750 1753 (void) printf("%*s", width, "-");
1751 1754 else
1752 1755 (void) printf("%*.*s", width, width,
1753 1756 psinfo->pr_lwp.pr_clname);
1754 1757 break;
1755 1758 case F_STIME:
1756 1759 if (Lflg)
1757 1760 prtime(psinfo->pr_lwp.pr_start, width, 0);
1758 1761 else
1759 1762 prtime(psinfo->pr_start, width, 0);
1760 1763 break;
1761 1764 case F_ETIME:
1762 1765 if (Lflg)
1763 1766 print_time(delta_secs(&psinfo->pr_lwp.pr_start),
1764 1767 width);
1765 1768 else
1766 1769 print_time(delta_secs(&psinfo->pr_start), width);
1767 1770 break;
1768 1771 case F_TIME:
1769 1772 if (Lflg) {
1770 1773 cputime = psinfo->pr_lwp.pr_time.tv_sec;
1771 1774 if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000)
1772 1775 cputime++;
1773 1776 } else {
1774 1777 cputime = psinfo->pr_time.tv_sec;
1775 1778 if (psinfo->pr_time.tv_nsec > 500000000)
1776 1779 cputime++;
1777 1780 }
1778 1781 print_time(cputime, width);
1779 1782 break;
1780 1783 case F_TTY:
1781 1784 (void) printf("%-*s", width, ttyp);
1782 1785 break;
1783 1786 case F_ADDR:
1784 1787 if (zombie_lwp)
1785 1788 (void) printf("%*s", width, "-");
1786 1789 else if (Lflg)
1787 1790 (void) printf("%*lx", width,
1788 1791 (long)psinfo->pr_lwp.pr_addr);
1789 1792 else
1790 1793 (void) printf("%*lx", width, (long)psinfo->pr_addr);
1791 1794 break;
1792 1795 case F_WCHAN:
1793 1796 if (!zombie_lwp && psinfo->pr_lwp.pr_wchan)
1794 1797 (void) printf("%*lx", width,
1795 1798 (long)psinfo->pr_lwp.pr_wchan);
1796 1799 else
1797 1800 (void) printf("%*.*s", width, width, "-");
1798 1801 break;
1799 1802 case F_FNAME:
1800 1803 /*
1801 1804 * Print full width unless this is the last output format.
1802 1805 */
1803 1806 if (zombie_lwp) {
1804 1807 if (f->next != NULL)
1805 1808 (void) printf("%-*s", width, "<defunct>");
1806 1809 else
1807 1810 (void) printf("%s", "<defunct>");
1808 1811 break;
1809 1812 }
1810 1813 wcnt = namencnt(psinfo->pr_fname, 16, width);
1811 1814 if (f->next != NULL)
1812 1815 (void) printf("%-*.*s", width, wcnt, psinfo->pr_fname);
1813 1816 else
1814 1817 (void) printf("%-.*s", wcnt, psinfo->pr_fname);
1815 1818 break;
1816 1819 case F_COMM:
1817 1820 if (zombie_lwp) {
1818 1821 if (f->next != NULL)
1819 1822 (void) printf("%-*s", width, "<defunct>");
1820 1823 else
1821 1824 (void) printf("%s", "<defunct>");
1822 1825 break;
1823 1826 }
1824 1827 csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n");
1825 1828 if (csave) {
1826 1829 c = *csave;
1827 1830 *csave = '\0';
1828 1831 }
1829 1832 /* FALLTHROUGH */
1830 1833 case F_ARGS:
1831 1834 /*
1832 1835 * PRARGSZ == length of cmd arg string.
1833 1836 */
1834 1837 if (zombie_lwp) {
1835 1838 (void) printf("%-*s", width, "<defunct>");
1836 1839 break;
1837 1840 }
1838 1841 psinfo->pr_psargs[PRARGSZ-1] = '\0';
1839 1842 bytesleft = PRARGSZ;
1840 1843 for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) {
1841 1844 length = mbtowc(&wchar, cp, MB_LEN_MAX);
1842 1845 if (length == 0)
1843 1846 break;
1844 1847 if (length < 0 || !iswprint(wchar)) {
1845 1848 if (length < 0)
1846 1849 length = 1;
1847 1850 if (bytesleft <= length) {
1848 1851 *cp = '\0';
1849 1852 break;
1850 1853 }
1851 1854 /* omit the unprintable character */
1852 1855 (void) memmove(cp, cp+length, bytesleft-length);
1853 1856 length = 0;
1854 1857 }
1855 1858 bytesleft -= length;
1856 1859 }
1857 1860 wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width);
1858 1861 /*
1859 1862 * Print full width unless this is the last format.
1860 1863 */
1861 1864 if (f->next != NULL)
1862 1865 (void) printf("%-*.*s", width, wcnt,
1863 1866 psinfo->pr_psargs);
1864 1867 else
1865 1868 (void) printf("%-.*s", wcnt,
1866 1869 psinfo->pr_psargs);
1867 1870 if (f->fname == F_COMM && csave)
1868 1871 *csave = c;
1869 1872 break;
1870 1873 case F_TASKID:
1871 1874 (void) printf("%*d", width, (int)psinfo->pr_taskid);
1872 1875 break;
1873 1876 case F_PROJID:
1874 1877 (void) printf("%*d", width, (int)psinfo->pr_projid);
1875 1878 break;
1876 1879 case F_PROJECT:
1877 1880 {
1878 1881 struct project cproj;
1879 1882 char proj_buf[PROJECT_BUFSZ];
1880 1883
1881 1884 if ((getprojbyid(psinfo->pr_projid, &cproj,
1882 1885 (void *)&proj_buf, PROJECT_BUFSZ)) == NULL)
1883 1886 (void) printf("%*d", width,
1884 1887 (int)psinfo->pr_projid);
1885 1888 else
1886 1889 (void) printf("%*s", width,
1887 1890 (cproj.pj_name != NULL) ?
1888 1891 cproj.pj_name : "---");
1889 1892 }
1890 1893 break;
1891 1894 case F_PSET:
1892 1895 if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE)
1893 1896 (void) printf("%*s", width, "-");
1894 1897 else
1895 1898 (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset);
1896 1899 break;
1897 1900 case F_ZONEID:
1898 1901 (void) printf("%*d", width, (int)psinfo->pr_zoneid);
1899 1902 break;
1900 1903 case F_ZONE:
1901 1904 {
1902 1905 char zonename[ZONENAME_MAX];
1903 1906
1904 1907 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
1905 1908 sizeof (zonename)) < 0) {
1906 1909 (void) printf("%*d", width,
1907 1910 ((int)psinfo->pr_zoneid));
1908 1911 } else {
1909 1912 (void) printf("%*s", width, zonename);
1910 1913 }
1911 1914 }
1912 1915 break;
1913 1916 case F_CTID:
1914 1917 if (psinfo->pr_contract == -1)
1915 1918 (void) printf("%*s", width, "-");
1916 1919 else
1917 1920 (void) printf("%*ld", width, (long)psinfo->pr_contract);
1918 1921 break;
1919 1922 case F_LGRP:
1920 1923 /* Display home lgroup */
1921 1924 (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp);
1922 1925 break;
1923 1926
1924 1927 case F_DMODEL:
1925 1928 (void) printf("%*s", width,
1926 1929 psinfo->pr_dmodel == PR_MODEL_LP64 ? "_LP64" : "_ILP32");
1927 1930 break;
1928 1931 }
1929 1932 }
1930 1933
1931 1934 static void
1932 1935 print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp)
1933 1936 {
1934 1937 int wcnt;
1935 1938 int width = f->width;
1936 1939
1937 1940 switch (f->fname) {
1938 1941 case F_FNAME:
1939 1942 case F_COMM:
1940 1943 case F_ARGS:
1941 1944 /*
1942 1945 * Print full width unless this is the last output format.
1943 1946 */
1944 1947 wcnt = min(width, sizeof ("<defunct>"));
1945 1948 if (f->next != NULL)
1946 1949 (void) printf("%-*.*s", width, wcnt, "<defunct>");
1947 1950 else
1948 1951 (void) printf("%-.*s", wcnt, "<defunct>");
1949 1952 break;
1950 1953
1951 1954 case F_PSR:
1952 1955 case F_PCPU:
1953 1956 case F_PMEM:
1954 1957 case F_NICE:
1955 1958 case F_CLASS:
1956 1959 case F_STIME:
1957 1960 case F_ETIME:
1958 1961 case F_WCHAN:
1959 1962 case F_PSET:
1960 1963 (void) printf("%*s", width, "-");
1961 1964 break;
1962 1965
1963 1966 case F_OPRI:
1964 1967 case F_PRI:
1965 1968 case F_OSZ:
1966 1969 case F_VSZ:
1967 1970 case F_RSS:
1968 1971 (void) printf("%*d", width, 0);
1969 1972 break;
1970 1973
1971 1974 default:
1972 1975 print_field(psinfo, f, ttyp);
1973 1976 break;
1974 1977 }
1975 1978 }
1976 1979
1977 1980 static void
1978 1981 pr_fields(psinfo_t *psinfo, const char *ttyp,
1979 1982 void (*print_fld)(psinfo_t *, struct field *, const char *))
1980 1983 {
1981 1984 struct field *f;
1982 1985
1983 1986 for (f = fields; f != NULL; f = f->next) {
1984 1987 print_fld(psinfo, f, ttyp);
1985 1988 if (f->next != NULL)
1986 1989 (void) printf(" ");
1987 1990 }
1988 1991 (void) printf("\n");
1989 1992 }
1990 1993
1991 1994 /*
1992 1995 * Returns 1 if arg is found in array arr, of length num; 0 otherwise.
1993 1996 */
1994 1997 static int
1995 1998 search(pid_t *arr, int number, pid_t arg)
1996 1999 {
1997 2000 int i;
1998 2001
1999 2002 for (i = 0; i < number; i++)
2000 2003 if (arg == arr[i])
2001 2004 return (1);
2002 2005 return (0);
2003 2006 }
2004 2007
2005 2008 /*
2006 2009 * Add an entry (user, group) to the specified table.
2007 2010 */
2008 2011 static void
2009 2012 add_ugentry(struct ughead *tbl, char *name)
2010 2013 {
2011 2014 struct ugdata *entp;
2012 2015
2013 2016 if (tbl->size == tbl->nent) { /* reallocate the table entries */
2014 2017 if ((tbl->size *= 2) == 0)
2015 2018 tbl->size = 32; /* first time */
2016 2019 tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata));
2017 2020 }
2018 2021 entp = &tbl->ent[tbl->nent++];
2019 2022 entp->id = 0;
2020 2023 (void) strncpy(entp->name, name, MAXUGNAME);
2021 2024 entp->name[MAXUGNAME] = '\0';
2022 2025 }
2023 2026
2024 2027 static int
2025 2028 uconv(struct ughead *uhead)
2026 2029 {
2027 2030 struct ugdata *utbl = uhead->ent;
2028 2031 int n = uhead->nent;
2029 2032 struct passwd *pwd;
2030 2033 int i;
2031 2034 int fnd = 0;
2032 2035 uid_t uid;
2033 2036
2034 2037 /*
2035 2038 * Ask the name service for names.
2036 2039 */
2037 2040 for (i = 0; i < n; i++) {
2038 2041 /*
2039 2042 * If name is numeric, ask for numeric id
2040 2043 */
2041 2044 if (str2uid(utbl[i].name, &uid, 0, MAXEPHUID) == 0)
2042 2045 pwd = getpwuid(uid);
2043 2046 else
2044 2047 pwd = getpwnam(utbl[i].name);
2045 2048
2046 2049 /*
2047 2050 * If found, enter found index into tbl array.
2048 2051 */
2049 2052 if (pwd == NULL) {
2050 2053 (void) fprintf(stderr,
2051 2054 gettext("ps: unknown user %s\n"), utbl[i].name);
2052 2055 continue;
2053 2056 }
2054 2057
2055 2058 utbl[fnd].id = pwd->pw_uid;
2056 2059 (void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME);
2057 2060 fnd++;
2058 2061 }
2059 2062
2060 2063 uhead->nent = fnd; /* in case it changed */
2061 2064 return (n - fnd);
2062 2065 }
2063 2066
2064 2067 static int
2065 2068 gconv(struct ughead *ghead)
2066 2069 {
2067 2070 struct ugdata *gtbl = ghead->ent;
2068 2071 int n = ghead->nent;
2069 2072 struct group *grp;
2070 2073 gid_t gid;
2071 2074 int i;
2072 2075 int fnd = 0;
2073 2076
2074 2077 /*
2075 2078 * Ask the name service for names.
2076 2079 */
2077 2080 for (i = 0; i < n; i++) {
2078 2081 /*
2079 2082 * If name is numeric, ask for numeric id
2080 2083 */
2081 2084 if (str2uid(gtbl[i].name, (uid_t *)&gid, 0, MAXEPHUID) == 0)
2082 2085 grp = getgrgid(gid);
2083 2086 else
2084 2087 grp = getgrnam(gtbl[i].name);
2085 2088 /*
2086 2089 * If found, enter found index into tbl array.
2087 2090 */
2088 2091 if (grp == NULL) {
2089 2092 (void) fprintf(stderr,
2090 2093 gettext("ps: unknown group %s\n"), gtbl[i].name);
2091 2094 continue;
2092 2095 }
2093 2096
2094 2097 gtbl[fnd].id = grp->gr_gid;
2095 2098 (void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME);
2096 2099 fnd++;
2097 2100 }
2098 2101
2099 2102 ghead->nent = fnd; /* in case it changed */
2100 2103 return (n - fnd);
2101 2104 }
2102 2105
2103 2106 /*
2104 2107 * Return 1 if puid is in table, otherwise 0.
2105 2108 */
2106 2109 static int
2107 2110 ugfind(id_t id, struct ughead *ughead)
2108 2111 {
2109 2112 struct ugdata *utbl = ughead->ent;
2110 2113 int n = ughead->nent;
2111 2114 int i;
2112 2115
2113 2116 for (i = 0; i < n; i++)
2114 2117 if (utbl[i].id == id)
2115 2118 return (1);
2116 2119 return (0);
2117 2120 }
2118 2121
2119 2122 /*
2120 2123 * Print starting time of process unless process started more than 24 hours
2121 2124 * ago, in which case the date is printed. The date is printed in the form
2122 2125 * "MMM dd" if old format, else the blank is replaced with an '_' so
2123 2126 * it appears as a single word (for parseability).
2124 2127 */
2125 2128 static void
2126 2129 prtime(timestruc_t st, int width, int old)
2127 2130 {
2128 2131 char sttim[26];
2129 2132 time_t starttime;
2130 2133
2131 2134 starttime = st.tv_sec;
2132 2135 if (st.tv_nsec > 500000000)
2133 2136 starttime++;
2134 2137 if ((now.tv_sec - starttime) >= 24*60*60) {
2135 2138 (void) strftime(sttim, sizeof (sttim), old?
2136 2139 /*
2137 2140 * TRANSLATION_NOTE
2138 2141 * This time format is used by STIME field when -f option
2139 2142 * is specified. Used for processes that begun more than
2140 2143 * 24 hours.
2141 2144 */
2142 2145 dcgettext(NULL, "%b %d", LC_TIME) :
2143 2146 /*
2144 2147 * TRANSLATION_NOTE
2145 2148 * This time format is used by STIME field when -o option
2146 2149 * is specified. Used for processes that begun more than
2147 2150 * 24 hours.
2148 2151 */
2149 2152 dcgettext(NULL, "%b_%d", LC_TIME), localtime(&starttime));
2150 2153 } else {
2151 2154 /*
2152 2155 * TRANSLATION_NOTE
2153 2156 * This time format is used by STIME field when -f or -o option
2154 2157 * is specified. Used for processes that begun less than
2155 2158 * 24 hours.
2156 2159 */
2157 2160 (void) strftime(sttim, sizeof (sttim),
2158 2161 dcgettext(NULL, "%H:%M:%S", LC_TIME),
2159 2162 localtime(&starttime));
2160 2163 }
2161 2164 (void) printf("%*.*s", width, width, sttim);
2162 2165 }
2163 2166
2164 2167 static void
2165 2168 przom(psinfo_t *psinfo)
2166 2169 {
2167 2170 long tm;
2168 2171 struct passwd *pwd;
2169 2172 char zonename[ZONENAME_MAX];
2170 2173
2171 2174 /*
2172 2175 * All fields before 'PID' are printed with a trailing space as a
2173 2176 * spearator, rather than keeping track of which column is first. All
2174 2177 * other fields are printed with a leading space.
2175 2178 */
2176 2179 if (lflg) { /* F S */
2177 2180 if (!yflg)
2178 2181 (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */
2179 2182 (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */
2180 2183 }
2181 2184 if (Zflg) {
2182 2185 if (getzonenamebyid(psinfo->pr_zoneid, zonename,
2183 2186 sizeof (zonename)) < 0) {
2184 2187 (void) printf(" %7.7d ", ((int)psinfo->pr_zoneid));
2185 2188 } else {
2186 2189 (void) printf("%8.8s ", zonename);
2187 2190 }
2188 2191 }
2189 2192 if (Hflg) {
2190 2193 /* Display home lgroup */
2191 2194 (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */
2192 2195 }
2193 2196 if (fflg) {
2194 2197 if ((pwd = getpwuid(psinfo->pr_euid)) != NULL)
2195 2198 (void) printf("%8.8s ", pwd->pw_name);
2196 2199 else
2197 2200 (void) printf(" %7.7u ", psinfo->pr_euid);
2198 2201 } else if (lflg)
2199 2202 (void) printf("%6u ", psinfo->pr_euid);
2200 2203
2201 2204 (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */
2202 2205 if (lflg || fflg)
2203 2206 (void) printf(" %*d", pidwidth,
2204 2207 (int)psinfo->pr_ppid); /* PPID */
2205 2208
2206 2209 if (jflg) {
2207 2210 (void) printf(" %*d", pidwidth,
2208 2211 (int)psinfo->pr_pgid); /* PGID */
2209 2212 (void) printf(" %*d", pidwidth,
2210 2213 (int)psinfo->pr_sid); /* SID */
2211 2214 }
2212 2215
2213 2216 if (Lflg)
2214 2217 (void) printf(" %5d", 0); /* LWP */
2215 2218 if (Pflg)
2216 2219 (void) printf(" -"); /* PSR */
2217 2220 if (Lflg && fflg)
2218 2221 (void) printf(" %5d", 0); /* NLWP */
2219 2222
2220 2223 if (cflg) {
2221 2224 (void) printf(" %4s", "-"); /* zombies have no class */
2222 2225 (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */
2223 2226 } else if (lflg || fflg) {
2224 2227 (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */
2225 2228 if (lflg)
2226 2229 (void) printf(" %3d %2s",
2227 2230 psinfo->pr_lwp.pr_oldpri, "-"); /* PRI NI */
2228 2231 }
2229 2232 if (lflg) {
2230 2233 if (yflg) /* RSS SZ WCHAN */
2231 2234 (void) printf(" %5d %6d %8s", 0, 0, "-");
2232 2235 else /* ADDR SZ WCHAN */
2233 2236 (void) printf(" %8s %6d %8s", "-", 0, "-");
2234 2237 }
2235 2238 if (fflg) {
2236 2239 int width = fname[F_STIME].width;
2237 2240 (void) printf(" %*.*s", width, width, "-"); /* STIME */
2238 2241 }
2239 2242 (void) printf(" %-8.14s", "?"); /* TTY */
2240 2243
2241 2244 tm = psinfo->pr_time.tv_sec;
2242 2245 if (psinfo->pr_time.tv_nsec > 500000000)
2243 2246 tm++;
2244 2247 (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* TIME */
2245 2248 (void) printf(" <defunct>\n");
2246 2249 }
2247 2250
2248 2251 /*
2249 2252 * Function to compute the number of printable bytes in a multibyte
2250 2253 * command string ("internationalization").
2251 2254 */
2252 2255 static int
2253 2256 namencnt(char *cmd, int csisize, int scrsize)
2254 2257 {
2255 2258 int csiwcnt = 0, scrwcnt = 0;
2256 2259 int ncsisz, nscrsz;
2257 2260 wchar_t wchar;
2258 2261 int len;
2259 2262
2260 2263 while (*cmd != '\0') {
2261 2264 if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX)
2262 2265 len = MB_CUR_MAX;
2263 2266 if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0)
2264 2267 return (8); /* default to use for illegal chars */
2265 2268 if ((nscrsz = wcwidth(wchar)) <= 0)
2266 2269 return (8);
2267 2270 if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize)
2268 2271 break;
2269 2272 csiwcnt += ncsisz;
2270 2273 scrwcnt += nscrsz;
2271 2274 cmd += ncsisz;
2272 2275 }
2273 2276 return (csiwcnt);
2274 2277 }
2275 2278
2276 2279 static char *
2277 2280 err_string(int err)
2278 2281 {
2279 2282 static char buf[32];
2280 2283 char *str = strerror(err);
2281 2284
2282 2285 if (str == NULL)
2283 2286 (void) snprintf(str = buf, sizeof (buf), "Errno #%d", err);
2284 2287
2285 2288 return (str);
2286 2289 }
2287 2290
2288 2291 /* If allocation fails, die */
2289 2292 static void *
2290 2293 Realloc(void *ptr, size_t size)
2291 2294 {
2292 2295 ptr = realloc(ptr, size);
2293 2296 if (ptr == NULL) {
2294 2297 (void) fprintf(stderr, gettext("ps: no memory\n"));
2295 2298 exit(1);
2296 2299 }
2297 2300 return (ptr);
2298 2301 }
2299 2302
2300 2303 static time_t
2301 2304 delta_secs(const timestruc_t *start)
2302 2305 {
2303 2306 time_t seconds = now.tv_sec - start->tv_sec;
2304 2307 long nanosecs = now.tv_usec * 1000 - start->tv_nsec;
2305 2308
2306 2309 if (nanosecs >= (NANOSEC / 2))
2307 2310 seconds++;
2308 2311 else if (nanosecs < -(NANOSEC / 2))
2309 2312 seconds--;
2310 2313
2311 2314 return (seconds);
2312 2315 }
2313 2316
2314 2317 /*
2315 2318 * Returns the following:
2316 2319 *
2317 2320 * 0 No error
2318 2321 * EINVAL Invalid number
2319 2322 * ERANGE Value exceeds (min, max) range
2320 2323 */
2321 2324 static int
2322 2325 str2id(const char *p, pid_t *val, long min, long max)
2323 2326 {
2324 2327 char *q;
2325 2328 long number;
2326 2329 int error;
2327 2330
2328 2331 errno = 0;
2329 2332 number = strtol(p, &q, 10);
2330 2333
2331 2334 if (errno != 0 || q == p || *q != '\0') {
2332 2335 if ((error = errno) == 0) {
2333 2336 /*
2334 2337 * strtol() can fail without setting errno, or it can
2335 2338 * set it to EINVAL or ERANGE. In the case errno is
2336 2339 * still zero, return EINVAL.
2337 2340 */
2338 2341 error = EINVAL;
2339 2342 }
2340 2343 } else if (number < min || number > max) {
2341 2344 error = ERANGE;
2342 2345 } else {
2343 2346 error = 0;
2344 2347 }
2345 2348
2346 2349 *val = number;
2347 2350
2348 2351 return (error);
2349 2352 }
2350 2353
2351 2354 /*
2352 2355 * Returns the following:
2353 2356 *
2354 2357 * 0 No error
2355 2358 * EINVAL Invalid number
2356 2359 * ERANGE Value exceeds (min, max) range
2357 2360 */
2358 2361 static int
2359 2362 str2uid(const char *p, uid_t *val, unsigned long min, unsigned long max)
2360 2363 {
2361 2364 char *q;
2362 2365 unsigned long number;
2363 2366 int error;
2364 2367
2365 2368 errno = 0;
2366 2369 number = strtoul(p, &q, 10);
2367 2370
2368 2371 if (errno != 0 || q == p || *q != '\0') {
2369 2372 if ((error = errno) == 0) {
2370 2373 /*
2371 2374 * strtoul() can fail without setting errno, or it can
2372 2375 * set it to EINVAL or ERANGE. In the case errno is
2373 2376 * still zero, return EINVAL.
2374 2377 */
2375 2378 error = EINVAL;
2376 2379 }
2377 2380 } else if (number < min || number > max) {
2378 2381 error = ERANGE;
2379 2382 } else {
2380 2383 error = 0;
2381 2384 }
2382 2385
2383 2386 *val = number;
2384 2387
2385 2388 return (error);
2386 2389 }
2387 2390
2388 2391 static int
2389 2392 pidcmp(const void *p1, const void *p2)
2390 2393 {
2391 2394 pid_t i = *((pid_t *)p1);
2392 2395 pid_t j = *((pid_t *)p2);
2393 2396
2394 2397 return (i - j);
2395 2398 }
↓ open down ↓ |
2306 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX