17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*51223f18Sjonb * Common Development and Distribution License (the "License"). 6*51223f18Sjonb * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*51223f18Sjonb 227c478bd9Sstevel@tonic-gate /* 23*51223f18Sjonb * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate /* 337c478bd9Sstevel@tonic-gate * ps -- print things about processes. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate #include <stdio.h> 367c478bd9Sstevel@tonic-gate #include <ctype.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <fcntl.h> 407c478bd9Sstevel@tonic-gate #include <pwd.h> 417c478bd9Sstevel@tonic-gate #include <grp.h> 427c478bd9Sstevel@tonic-gate #include <sys/types.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 457c478bd9Sstevel@tonic-gate #include <unistd.h> 467c478bd9Sstevel@tonic-gate #include <stdlib.h> 477c478bd9Sstevel@tonic-gate #include <limits.h> 487c478bd9Sstevel@tonic-gate #include <dirent.h> 497c478bd9Sstevel@tonic-gate #include <sys/signal.h> 507c478bd9Sstevel@tonic-gate #include <sys/fault.h> 517c478bd9Sstevel@tonic-gate #include <sys/syscall.h> 527c478bd9Sstevel@tonic-gate #include <sys/time.h> 537c478bd9Sstevel@tonic-gate #include <procfs.h> 547c478bd9Sstevel@tonic-gate #include <locale.h> 557c478bd9Sstevel@tonic-gate #include <wctype.h> 567c478bd9Sstevel@tonic-gate #include <wchar.h> 577c478bd9Sstevel@tonic-gate #include <libw.h> 587c478bd9Sstevel@tonic-gate #include <stdarg.h> 597c478bd9Sstevel@tonic-gate #include <sys/proc.h> 607c478bd9Sstevel@tonic-gate #include <sys/pset.h> 617c478bd9Sstevel@tonic-gate #include <project.h> 627c478bd9Sstevel@tonic-gate #include <zone.h> 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate #define min(a, b) ((a) > (b) ? (b) : (a)) 657c478bd9Sstevel@tonic-gate #define max(a, b) ((a) < (b) ? (b) : (a)) 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #define NTTYS 20 /* initial size of table for -t option */ 687c478bd9Sstevel@tonic-gate #define SIZ 30 /* initial size of tables for -p, -s, -g, and -z */ 69*51223f18Sjonb 70*51223f18Sjonb /* 71*51223f18Sjonb * Size of buffer holding args for t, p, s, g, u, U, G, z options. 72*51223f18Sjonb * Set to ZONENAME_MAX, the minimum value needed to allow any 73*51223f18Sjonb * zone to be specified. 74*51223f18Sjonb */ 75*51223f18Sjonb #define ARGSIZ ZONENAME_MAX 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate #define MAXUGNAME 10 /* max chars in a user/group name or printed u/g id */ 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* Structure for storing user or group info */ 807c478bd9Sstevel@tonic-gate struct ugdata { 817c478bd9Sstevel@tonic-gate id_t id; /* numeric user-id or group-id */ 827c478bd9Sstevel@tonic-gate char name[MAXUGNAME+1]; /* user/group name, null terminated */ 837c478bd9Sstevel@tonic-gate }; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate struct ughead { 867c478bd9Sstevel@tonic-gate size_t size; /* number of ugdata structs allocated */ 877c478bd9Sstevel@tonic-gate size_t nent; /* number of active entries */ 887c478bd9Sstevel@tonic-gate struct ugdata *ent; /* pointer to array of actual entries */ 897c478bd9Sstevel@tonic-gate }; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate enum fname { /* enumeration of field names */ 927c478bd9Sstevel@tonic-gate F_USER, /* effective user of the process */ 937c478bd9Sstevel@tonic-gate F_RUSER, /* real user of the process */ 947c478bd9Sstevel@tonic-gate F_GROUP, /* effective group of the process */ 957c478bd9Sstevel@tonic-gate F_RGROUP, /* real group of the process */ 967c478bd9Sstevel@tonic-gate F_UID, /* numeric effective uid of the process */ 977c478bd9Sstevel@tonic-gate F_RUID, /* numeric real uid of the process */ 987c478bd9Sstevel@tonic-gate F_GID, /* numeric effective gid of the process */ 997c478bd9Sstevel@tonic-gate F_RGID, /* numeric real gid of the process */ 1007c478bd9Sstevel@tonic-gate F_PID, /* process id */ 1017c478bd9Sstevel@tonic-gate F_PPID, /* parent process id */ 1027c478bd9Sstevel@tonic-gate F_PGID, /* process group id */ 1037c478bd9Sstevel@tonic-gate F_SID, /* session id */ 1047c478bd9Sstevel@tonic-gate F_PSR, /* bound processor */ 1057c478bd9Sstevel@tonic-gate F_LWP, /* lwp-id */ 1067c478bd9Sstevel@tonic-gate F_NLWP, /* number of lwps */ 1077c478bd9Sstevel@tonic-gate F_OPRI, /* old priority (obsolete) */ 1087c478bd9Sstevel@tonic-gate F_PRI, /* new priority */ 1097c478bd9Sstevel@tonic-gate F_F, /* process flags */ 1107c478bd9Sstevel@tonic-gate F_S, /* letter indicating the state */ 1117c478bd9Sstevel@tonic-gate F_C, /* processor utilization (obsolete) */ 1127c478bd9Sstevel@tonic-gate F_PCPU, /* percent of recently used cpu time */ 1137c478bd9Sstevel@tonic-gate F_PMEM, /* percent of physical memory used (rss) */ 1147c478bd9Sstevel@tonic-gate F_OSZ, /* virtual size of the process in pages */ 1157c478bd9Sstevel@tonic-gate F_VSZ, /* virtual size of the process in kilobytes */ 1167c478bd9Sstevel@tonic-gate F_RSS, /* resident set size of the process in kilobytes */ 1177c478bd9Sstevel@tonic-gate F_NICE, /* "nice" value of the process */ 1187c478bd9Sstevel@tonic-gate F_CLASS, /* scheduler class */ 1197c478bd9Sstevel@tonic-gate F_STIME, /* start time of the process, hh:mm:ss or Month Day */ 1207c478bd9Sstevel@tonic-gate F_ETIME, /* elapsed time of the process, [[dd-]hh:]mm:ss */ 1217c478bd9Sstevel@tonic-gate F_TIME, /* cpu time of the process, [[dd-]hh:]mm:ss */ 1227c478bd9Sstevel@tonic-gate F_TTY, /* name of the controlling terminal */ 1237c478bd9Sstevel@tonic-gate F_ADDR, /* address of the process (obsolete) */ 1247c478bd9Sstevel@tonic-gate F_WCHAN, /* wait channel (sleep condition variable) */ 1257c478bd9Sstevel@tonic-gate F_FNAME, /* file name of command */ 1267c478bd9Sstevel@tonic-gate F_COMM, /* name of command (argv[0] value) */ 1277c478bd9Sstevel@tonic-gate F_ARGS, /* name of command plus all its arguments */ 1287c478bd9Sstevel@tonic-gate F_TASKID, /* task id */ 1297c478bd9Sstevel@tonic-gate F_PROJID, /* project id */ 1307c478bd9Sstevel@tonic-gate F_PROJECT, /* project name of the process */ 1317c478bd9Sstevel@tonic-gate F_PSET, /* bound processor set */ 1327c478bd9Sstevel@tonic-gate F_ZONE, /* zone name */ 1337c478bd9Sstevel@tonic-gate F_ZONEID, /* zone id */ 1347c478bd9Sstevel@tonic-gate F_CTID /* process contract id */ 1357c478bd9Sstevel@tonic-gate }; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate struct field { 1387c478bd9Sstevel@tonic-gate struct field *next; /* linked list */ 1397c478bd9Sstevel@tonic-gate int fname; /* field index */ 1407c478bd9Sstevel@tonic-gate const char *header; /* header to use */ 1417c478bd9Sstevel@tonic-gate int width; /* width of field */ 1427c478bd9Sstevel@tonic-gate }; 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate static struct field *fields = NULL; /* fields selected via -o */ 1457c478bd9Sstevel@tonic-gate static struct field *last_field = NULL; 1467c478bd9Sstevel@tonic-gate static int do_header = 0; 1477c478bd9Sstevel@tonic-gate static struct timeval now; 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate /* array of defined fields, in fname order */ 1507c478bd9Sstevel@tonic-gate struct def_field { 1517c478bd9Sstevel@tonic-gate const char *fname; 1527c478bd9Sstevel@tonic-gate const char *header; 1537c478bd9Sstevel@tonic-gate int width; 1547c478bd9Sstevel@tonic-gate int minwidth; 1557c478bd9Sstevel@tonic-gate }; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate static struct def_field fname[] = { 1587c478bd9Sstevel@tonic-gate /* fname header width minwidth */ 1597c478bd9Sstevel@tonic-gate { "user", "USER", 8, 8 }, 1607c478bd9Sstevel@tonic-gate { "ruser", "RUSER", 8, 8 }, 1617c478bd9Sstevel@tonic-gate { "group", "GROUP", 8, 8 }, 1627c478bd9Sstevel@tonic-gate { "rgroup", "RGROUP", 8, 8 }, 1637c478bd9Sstevel@tonic-gate { "uid", "UID", 5, 5 }, 1647c478bd9Sstevel@tonic-gate { "ruid", "RUID", 5, 5 }, 1657c478bd9Sstevel@tonic-gate { "gid", "GID", 5, 5 }, 1667c478bd9Sstevel@tonic-gate { "rgid", "RGID", 5, 5 }, 1677c478bd9Sstevel@tonic-gate { "pid", "PID", 5, 5 }, 1687c478bd9Sstevel@tonic-gate { "ppid", "PPID", 5, 5 }, 1697c478bd9Sstevel@tonic-gate { "pgid", "PGID", 5, 5 }, 1707c478bd9Sstevel@tonic-gate { "sid", "SID", 5, 5 }, 1717c478bd9Sstevel@tonic-gate { "psr", "PSR", 3, 2 }, 1727c478bd9Sstevel@tonic-gate { "lwp", "LWP", 6, 2 }, 1737c478bd9Sstevel@tonic-gate { "nlwp", "NLWP", 4, 2 }, 1747c478bd9Sstevel@tonic-gate { "opri", "PRI", 3, 2 }, 1757c478bd9Sstevel@tonic-gate { "pri", "PRI", 3, 2 }, 1767c478bd9Sstevel@tonic-gate { "f", "F", 2, 2 }, 1777c478bd9Sstevel@tonic-gate { "s", "S", 1, 1 }, 1787c478bd9Sstevel@tonic-gate { "c", "C", 2, 2 }, 1797c478bd9Sstevel@tonic-gate { "pcpu", "%CPU", 4, 4 }, 1807c478bd9Sstevel@tonic-gate { "pmem", "%MEM", 4, 4 }, 1817c478bd9Sstevel@tonic-gate { "osz", "SZ", 4, 4 }, 1827c478bd9Sstevel@tonic-gate { "vsz", "VSZ", 4, 4 }, 1837c478bd9Sstevel@tonic-gate { "rss", "RSS", 4, 4 }, 1847c478bd9Sstevel@tonic-gate { "nice", "NI", 2, 2 }, 1857c478bd9Sstevel@tonic-gate { "class", "CLS", 4, 2 }, 1867c478bd9Sstevel@tonic-gate { "stime", "STIME", 8, 8 }, 1877c478bd9Sstevel@tonic-gate { "etime", "ELAPSED", 11, 7 }, 1887c478bd9Sstevel@tonic-gate { "time", "TIME", 11, 5 }, 1897c478bd9Sstevel@tonic-gate { "tty", "TT", 7, 7 }, 1907c478bd9Sstevel@tonic-gate #ifdef _LP64 1917c478bd9Sstevel@tonic-gate { "addr", "ADDR", 16, 8 }, 1927c478bd9Sstevel@tonic-gate { "wchan", "WCHAN", 16, 8 }, 1937c478bd9Sstevel@tonic-gate #else 1947c478bd9Sstevel@tonic-gate { "addr", "ADDR", 8, 8 }, 1957c478bd9Sstevel@tonic-gate { "wchan", "WCHAN", 8, 8 }, 1967c478bd9Sstevel@tonic-gate #endif 1977c478bd9Sstevel@tonic-gate { "fname", "COMMAND", 8, 8 }, 1987c478bd9Sstevel@tonic-gate { "comm", "COMMAND", 80, 8 }, 1997c478bd9Sstevel@tonic-gate { "args", "COMMAND", 80, 80 }, 2007c478bd9Sstevel@tonic-gate { "taskid", "TASKID", 5, 5 }, 2017c478bd9Sstevel@tonic-gate { "projid", "PROJID", 5, 5 }, 2027c478bd9Sstevel@tonic-gate { "project", "PROJECT", 8, 8 }, 2037c478bd9Sstevel@tonic-gate { "pset", "PSET", 3, 3 }, 2047c478bd9Sstevel@tonic-gate { "zone", "ZONE", 8, 8 }, 2057c478bd9Sstevel@tonic-gate { "zoneid", "ZONEID", 5, 5 }, 2067c478bd9Sstevel@tonic-gate { "ctid", "CTID", 5, 5 }, 2077c478bd9Sstevel@tonic-gate }; 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate #define NFIELDS (sizeof (fname) / sizeof (fname[0])) 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate static int retcode = 1; 2127c478bd9Sstevel@tonic-gate static int lflg; 2137c478bd9Sstevel@tonic-gate static int Aflg; 2147c478bd9Sstevel@tonic-gate static int uflg; 2157c478bd9Sstevel@tonic-gate static int Uflg; 2167c478bd9Sstevel@tonic-gate static int Gflg; 2177c478bd9Sstevel@tonic-gate static int aflg; 2187c478bd9Sstevel@tonic-gate static int dflg; 2197c478bd9Sstevel@tonic-gate static int Lflg; 2207c478bd9Sstevel@tonic-gate static int Pflg; 2217c478bd9Sstevel@tonic-gate static int yflg; 2227c478bd9Sstevel@tonic-gate static int pflg; 2237c478bd9Sstevel@tonic-gate static int fflg; 2247c478bd9Sstevel@tonic-gate static int cflg; 2257c478bd9Sstevel@tonic-gate static int jflg; 2267c478bd9Sstevel@tonic-gate static int gflg; 2277c478bd9Sstevel@tonic-gate static int sflg; 2287c478bd9Sstevel@tonic-gate static int tflg; 2297c478bd9Sstevel@tonic-gate static int zflg; 2307c478bd9Sstevel@tonic-gate static int Zflg; 2317c478bd9Sstevel@tonic-gate static uid_t tuid = -1; 2327c478bd9Sstevel@tonic-gate static int errflg; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static int ndev; /* number of devices */ 2357c478bd9Sstevel@tonic-gate static int maxdev; /* number of devl structures allocated */ 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate #define DNINCR 100 2387c478bd9Sstevel@tonic-gate #define DNSIZE 14 2397c478bd9Sstevel@tonic-gate static struct devl { /* device list */ 2407c478bd9Sstevel@tonic-gate char dname[DNSIZE]; /* device name */ 2417c478bd9Sstevel@tonic-gate dev_t ddev; /* device number */ 2427c478bd9Sstevel@tonic-gate } *devl; 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate static struct tty { 2457c478bd9Sstevel@tonic-gate char *tname; 2467c478bd9Sstevel@tonic-gate dev_t tdev; 2477c478bd9Sstevel@tonic-gate } *tty = NULL; /* for t option */ 2487c478bd9Sstevel@tonic-gate static size_t ttysz = 0; 2497c478bd9Sstevel@tonic-gate static int ntty = 0; 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate static pid_t *pid = NULL; /* for p option */ 2527c478bd9Sstevel@tonic-gate static size_t pidsz = 0; 2537c478bd9Sstevel@tonic-gate static size_t npid = 0; 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate static pid_t *grpid = NULL; /* for g option */ 2567c478bd9Sstevel@tonic-gate static size_t grpidsz = 0; 2577c478bd9Sstevel@tonic-gate static int ngrpid = 0; 2587c478bd9Sstevel@tonic-gate 2597c478bd9Sstevel@tonic-gate static pid_t *sessid = NULL; /* for s option */ 2607c478bd9Sstevel@tonic-gate static size_t sessidsz = 0; 2617c478bd9Sstevel@tonic-gate static int nsessid = 0; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate static zoneid_t *zoneid = NULL; /* for z option */ 2647c478bd9Sstevel@tonic-gate static size_t zoneidsz = 0; 2657c478bd9Sstevel@tonic-gate static int nzoneid = 0; 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate static int kbytes_per_page; 2687c478bd9Sstevel@tonic-gate static int pidwidth; 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate static char *procdir = "/proc"; /* standard /proc directory */ 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate static struct ughead euid_tbl; /* table to store selected euid's */ 2737c478bd9Sstevel@tonic-gate static struct ughead ruid_tbl; /* table to store selected real uid's */ 2747c478bd9Sstevel@tonic-gate static struct ughead egid_tbl; /* table to store selected egid's */ 2757c478bd9Sstevel@tonic-gate static struct ughead rgid_tbl; /* table to store selected real gid's */ 2767c478bd9Sstevel@tonic-gate static prheader_t *lpsinfobuf; /* buffer to contain lpsinfo */ 2777c478bd9Sstevel@tonic-gate static size_t lpbufsize; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* 2807c478bd9Sstevel@tonic-gate * This constant defines the sentinal number of process IDs below which we 2817c478bd9Sstevel@tonic-gate * only examine individual entries in /proc rather than scanning through 2827c478bd9Sstevel@tonic-gate * /proc. This optimization is a huge win in the common case. 2837c478bd9Sstevel@tonic-gate */ 2847c478bd9Sstevel@tonic-gate #define PTHRESHOLD 40 2857c478bd9Sstevel@tonic-gate 2867c478bd9Sstevel@tonic-gate static void usage(void); 2877c478bd9Sstevel@tonic-gate static char *getarg(char **); 2887c478bd9Sstevel@tonic-gate static char *parse_format(char *); 2897c478bd9Sstevel@tonic-gate static char *gettty(psinfo_t *); 2907c478bd9Sstevel@tonic-gate static int prfind(int, psinfo_t *, char **); 2917c478bd9Sstevel@tonic-gate static void prcom(psinfo_t *, char *); 2927c478bd9Sstevel@tonic-gate static void prtpct(ushort_t, int); 2937c478bd9Sstevel@tonic-gate static void print_time(time_t, int); 2947c478bd9Sstevel@tonic-gate static void print_field(psinfo_t *, struct field *, const char *); 2957c478bd9Sstevel@tonic-gate static void print_zombie_field(psinfo_t *, struct field *, const char *); 2967c478bd9Sstevel@tonic-gate static void pr_fields(psinfo_t *, const char *, 2977c478bd9Sstevel@tonic-gate void (*print_fld)(psinfo_t *, struct field *, const char *)); 2987c478bd9Sstevel@tonic-gate static int search(pid_t *, int, pid_t); 2997c478bd9Sstevel@tonic-gate static void add_ugentry(struct ughead *, char *); 3007c478bd9Sstevel@tonic-gate static int uconv(struct ughead *); 3017c478bd9Sstevel@tonic-gate static int gconv(struct ughead *); 3027c478bd9Sstevel@tonic-gate static int ugfind(uid_t, struct ughead *); 3037c478bd9Sstevel@tonic-gate static void prtime(timestruc_t, int, int); 3047c478bd9Sstevel@tonic-gate static void przom(psinfo_t *); 3057c478bd9Sstevel@tonic-gate static int namencnt(char *, int, int); 3067c478bd9Sstevel@tonic-gate static char *err_string(int); 3077c478bd9Sstevel@tonic-gate static int print_proc(char *pname); 3087c478bd9Sstevel@tonic-gate static time_t delta_secs(const timestruc_t *); 3097c478bd9Sstevel@tonic-gate static int str2id(const char *, pid_t *, long, long); 3107c478bd9Sstevel@tonic-gate static void *Realloc(void *, size_t); 3117c478bd9Sstevel@tonic-gate static int pidcmp(const void *p1, const void *p2); 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate int 3147c478bd9Sstevel@tonic-gate main(int argc, char **argv) 3157c478bd9Sstevel@tonic-gate { 3167c478bd9Sstevel@tonic-gate char *p; 3177c478bd9Sstevel@tonic-gate char *p1; 3187c478bd9Sstevel@tonic-gate char *parg; 3197c478bd9Sstevel@tonic-gate int c; 3207c478bd9Sstevel@tonic-gate int i; 3217c478bd9Sstevel@tonic-gate int pgerrflg = 0; /* err flg: non-numeric arg w/p & g options */ 3227c478bd9Sstevel@tonic-gate size_t size; 3237c478bd9Sstevel@tonic-gate DIR *dirp; 3247c478bd9Sstevel@tonic-gate struct dirent *dentp; 3257c478bd9Sstevel@tonic-gate pid_t maxpid; 3267c478bd9Sstevel@tonic-gate pid_t id; 3277c478bd9Sstevel@tonic-gate int ret; 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3307c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 3317c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 3327c478bd9Sstevel@tonic-gate #endif 3337c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate (void) memset(&euid_tbl, 0, sizeof (euid_tbl)); 3367c478bd9Sstevel@tonic-gate (void) memset(&ruid_tbl, 0, sizeof (ruid_tbl)); 3377c478bd9Sstevel@tonic-gate (void) memset(&egid_tbl, 0, sizeof (egid_tbl)); 3387c478bd9Sstevel@tonic-gate (void) memset(&rgid_tbl, 0, sizeof (rgid_tbl)); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024; 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * calculate width of pid fields based on configured MAXPID 3467c478bd9Sstevel@tonic-gate * (must be at least 5 to retain output format compatibility) 3477c478bd9Sstevel@tonic-gate */ 3487c478bd9Sstevel@tonic-gate id = maxpid = (pid_t)sysconf(_SC_MAXPID); 3497c478bd9Sstevel@tonic-gate pidwidth = 1; 3507c478bd9Sstevel@tonic-gate while ((id /= 10) > 0) 3517c478bd9Sstevel@tonic-gate ++pidwidth; 3527c478bd9Sstevel@tonic-gate pidwidth = pidwidth < 5 ? 5 : pidwidth; 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate fname[F_PID].width = fname[F_PPID].width = pidwidth; 3557c478bd9Sstevel@tonic-gate fname[F_PGID].width = fname[F_SID].width = pidwidth; 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "jlfceAadLPyZt:p:g:u:U:G:n:s:o:z:")) != 3587c478bd9Sstevel@tonic-gate EOF) 3597c478bd9Sstevel@tonic-gate switch (c) { 3607c478bd9Sstevel@tonic-gate case 'l': /* long listing */ 3617c478bd9Sstevel@tonic-gate lflg++; 3627c478bd9Sstevel@tonic-gate break; 3637c478bd9Sstevel@tonic-gate case 'f': /* full listing */ 3647c478bd9Sstevel@tonic-gate fflg++; 3657c478bd9Sstevel@tonic-gate break; 3667c478bd9Sstevel@tonic-gate case 'j': 3677c478bd9Sstevel@tonic-gate jflg++; 3687c478bd9Sstevel@tonic-gate break; 3697c478bd9Sstevel@tonic-gate case 'c': 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * Format output to reflect scheduler changes: 3727c478bd9Sstevel@tonic-gate * high numbers for high priorities and don't 3737c478bd9Sstevel@tonic-gate * print nice or p_cpu values. 'c' option only 3747c478bd9Sstevel@tonic-gate * effective when used with 'l' or 'f' options. 3757c478bd9Sstevel@tonic-gate */ 3767c478bd9Sstevel@tonic-gate cflg++; 3777c478bd9Sstevel@tonic-gate break; 3787c478bd9Sstevel@tonic-gate case 'A': /* list every process */ 3797c478bd9Sstevel@tonic-gate case 'e': /* (obsolete) list every process */ 3807c478bd9Sstevel@tonic-gate Aflg++; 3817c478bd9Sstevel@tonic-gate tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0; 3827c478bd9Sstevel@tonic-gate zflg = 0; 3837c478bd9Sstevel@tonic-gate break; 3847c478bd9Sstevel@tonic-gate case 'a': 3857c478bd9Sstevel@tonic-gate /* 3867c478bd9Sstevel@tonic-gate * Same as 'e' except no session group leaders 3877c478bd9Sstevel@tonic-gate * and no non-terminal processes. 3887c478bd9Sstevel@tonic-gate */ 3897c478bd9Sstevel@tonic-gate aflg++; 3907c478bd9Sstevel@tonic-gate break; 3917c478bd9Sstevel@tonic-gate case 'd': /* same as e except no session leaders */ 3927c478bd9Sstevel@tonic-gate dflg++; 3937c478bd9Sstevel@tonic-gate break; 3947c478bd9Sstevel@tonic-gate case 'L': /* show lwps */ 3957c478bd9Sstevel@tonic-gate Lflg++; 3967c478bd9Sstevel@tonic-gate break; 3977c478bd9Sstevel@tonic-gate case 'P': /* show bound processor */ 3987c478bd9Sstevel@tonic-gate Pflg++; 3997c478bd9Sstevel@tonic-gate break; 4007c478bd9Sstevel@tonic-gate case 'y': /* omit F & ADDR, report RSS & SZ in Kby */ 4017c478bd9Sstevel@tonic-gate yflg++; 4027c478bd9Sstevel@tonic-gate break; 4037c478bd9Sstevel@tonic-gate case 'n': /* no longer needed; retain as no-op */ 4047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4057c478bd9Sstevel@tonic-gate gettext("ps: warning: -n option ignored\n")); 4067c478bd9Sstevel@tonic-gate break; 4077c478bd9Sstevel@tonic-gate case 't': /* terminals */ 4087c478bd9Sstevel@tonic-gate #define TSZ 30 4097c478bd9Sstevel@tonic-gate tflg++; 4107c478bd9Sstevel@tonic-gate p1 = optarg; 4117c478bd9Sstevel@tonic-gate do { 4127c478bd9Sstevel@tonic-gate char nambuf[TSZ+6]; /* for "/dev/" + '\0' */ 4137c478bd9Sstevel@tonic-gate struct stat64 s; 4147c478bd9Sstevel@tonic-gate parg = getarg(&p1); 4157c478bd9Sstevel@tonic-gate p = Realloc(NULL, TSZ+1); /* for '\0' */ 4167c478bd9Sstevel@tonic-gate /* zero the buffer before using it */ 4177c478bd9Sstevel@tonic-gate p[0] = '\0'; 4187c478bd9Sstevel@tonic-gate size = TSZ; 4197c478bd9Sstevel@tonic-gate if (isdigit(*parg)) { 4207c478bd9Sstevel@tonic-gate (void) strcpy(p, "tty"); 4217c478bd9Sstevel@tonic-gate size -= 3; 4227c478bd9Sstevel@tonic-gate } 4237c478bd9Sstevel@tonic-gate (void) strncat(p, parg, size); 4247c478bd9Sstevel@tonic-gate if (ntty == ttysz) { 4257c478bd9Sstevel@tonic-gate if ((ttysz *= 2) == 0) 4267c478bd9Sstevel@tonic-gate ttysz = NTTYS; 4277c478bd9Sstevel@tonic-gate tty = Realloc(tty, 4287c478bd9Sstevel@tonic-gate (ttysz + 1) * sizeof (struct tty)); 4297c478bd9Sstevel@tonic-gate } 4307c478bd9Sstevel@tonic-gate tty[ntty].tdev = PRNODEV; 4317c478bd9Sstevel@tonic-gate (void) strcpy(nambuf, "/dev/"); 4327c478bd9Sstevel@tonic-gate (void) strcat(nambuf, p); 4337c478bd9Sstevel@tonic-gate if (stat64(nambuf, &s) == 0) 4347c478bd9Sstevel@tonic-gate tty[ntty].tdev = s.st_rdev; 4357c478bd9Sstevel@tonic-gate tty[ntty++].tname = p; 4367c478bd9Sstevel@tonic-gate } while (*p1); 4377c478bd9Sstevel@tonic-gate break; 4387c478bd9Sstevel@tonic-gate case 'p': /* proc ids */ 4397c478bd9Sstevel@tonic-gate pflg++; 4407c478bd9Sstevel@tonic-gate p1 = optarg; 4417c478bd9Sstevel@tonic-gate do { 4427c478bd9Sstevel@tonic-gate pid_t id; 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate parg = getarg(&p1); 4457c478bd9Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) { 4467c478bd9Sstevel@tonic-gate pgerrflg++; 4477c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4487c478bd9Sstevel@tonic-gate gettext("ps: %s "), parg); 4497c478bd9Sstevel@tonic-gate if (ret == EINVAL) 4507c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4517c478bd9Sstevel@tonic-gate gettext("is an invalid " 4527c478bd9Sstevel@tonic-gate "non-numeric argument")); 4537c478bd9Sstevel@tonic-gate else 4547c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4557c478bd9Sstevel@tonic-gate gettext("exceeds valid " 4567c478bd9Sstevel@tonic-gate "range")); 4577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4587c478bd9Sstevel@tonic-gate gettext(" for -p option\n")); 4597c478bd9Sstevel@tonic-gate continue; 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate 4627c478bd9Sstevel@tonic-gate if (npid == pidsz) { 4637c478bd9Sstevel@tonic-gate if ((pidsz *= 2) == 0) 4647c478bd9Sstevel@tonic-gate pidsz = SIZ; 4657c478bd9Sstevel@tonic-gate pid = Realloc(pid, 4667c478bd9Sstevel@tonic-gate pidsz * sizeof (pid_t)); 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate pid[npid++] = id; 4697c478bd9Sstevel@tonic-gate } while (*p1); 4707c478bd9Sstevel@tonic-gate break; 4717c478bd9Sstevel@tonic-gate case 's': /* session */ 4727c478bd9Sstevel@tonic-gate sflg++; 4737c478bd9Sstevel@tonic-gate p1 = optarg; 4747c478bd9Sstevel@tonic-gate do { 4757c478bd9Sstevel@tonic-gate pid_t id; 4767c478bd9Sstevel@tonic-gate 4777c478bd9Sstevel@tonic-gate parg = getarg(&p1); 4787c478bd9Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) { 4797c478bd9Sstevel@tonic-gate pgerrflg++; 4807c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4817c478bd9Sstevel@tonic-gate gettext("ps: %s "), parg); 4827c478bd9Sstevel@tonic-gate if (ret == EINVAL) 4837c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4847c478bd9Sstevel@tonic-gate gettext("is an invalid " 4857c478bd9Sstevel@tonic-gate "non-numeric argument")); 4867c478bd9Sstevel@tonic-gate else 4877c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4887c478bd9Sstevel@tonic-gate gettext("exceeds valid " 4897c478bd9Sstevel@tonic-gate "range")); 4907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 4917c478bd9Sstevel@tonic-gate gettext(" for -s option\n")); 4927c478bd9Sstevel@tonic-gate continue; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate if (nsessid == sessidsz) { 4967c478bd9Sstevel@tonic-gate if ((sessidsz *= 2) == 0) 4977c478bd9Sstevel@tonic-gate sessidsz = SIZ; 4987c478bd9Sstevel@tonic-gate sessid = Realloc(sessid, 4997c478bd9Sstevel@tonic-gate sessidsz * sizeof (pid_t)); 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate sessid[nsessid++] = id; 5027c478bd9Sstevel@tonic-gate } while (*p1); 5037c478bd9Sstevel@tonic-gate break; 5047c478bd9Sstevel@tonic-gate case 'g': /* proc group */ 5057c478bd9Sstevel@tonic-gate gflg++; 5067c478bd9Sstevel@tonic-gate p1 = optarg; 5077c478bd9Sstevel@tonic-gate do { 5087c478bd9Sstevel@tonic-gate pid_t id; 5097c478bd9Sstevel@tonic-gate 5107c478bd9Sstevel@tonic-gate parg = getarg(&p1); 5117c478bd9Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) { 5127c478bd9Sstevel@tonic-gate pgerrflg++; 5137c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5147c478bd9Sstevel@tonic-gate gettext("ps: %s "), parg); 5157c478bd9Sstevel@tonic-gate if (ret == EINVAL) 5167c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5177c478bd9Sstevel@tonic-gate gettext("is an invalid " 5187c478bd9Sstevel@tonic-gate "non-numeric argument")); 5197c478bd9Sstevel@tonic-gate else 5207c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5217c478bd9Sstevel@tonic-gate gettext("exceeds valid " 5227c478bd9Sstevel@tonic-gate "range")); 5237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5247c478bd9Sstevel@tonic-gate gettext(" for -g option\n")); 5257c478bd9Sstevel@tonic-gate continue; 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate 5287c478bd9Sstevel@tonic-gate if (ngrpid == grpidsz) { 5297c478bd9Sstevel@tonic-gate if ((grpidsz *= 2) == 0) 5307c478bd9Sstevel@tonic-gate grpidsz = SIZ; 5317c478bd9Sstevel@tonic-gate grpid = Realloc(grpid, 5327c478bd9Sstevel@tonic-gate grpidsz * sizeof (pid_t)); 5337c478bd9Sstevel@tonic-gate } 5347c478bd9Sstevel@tonic-gate grpid[ngrpid++] = id; 5357c478bd9Sstevel@tonic-gate } while (*p1); 5367c478bd9Sstevel@tonic-gate break; 5377c478bd9Sstevel@tonic-gate case 'u': /* effective user name or number */ 5387c478bd9Sstevel@tonic-gate uflg++; 5397c478bd9Sstevel@tonic-gate p1 = optarg; 5407c478bd9Sstevel@tonic-gate do { 5417c478bd9Sstevel@tonic-gate parg = getarg(&p1); 5427c478bd9Sstevel@tonic-gate add_ugentry(&euid_tbl, parg); 5437c478bd9Sstevel@tonic-gate } while (*p1); 5447c478bd9Sstevel@tonic-gate break; 5457c478bd9Sstevel@tonic-gate case 'U': /* real user name or number */ 5467c478bd9Sstevel@tonic-gate Uflg++; 5477c478bd9Sstevel@tonic-gate p1 = optarg; 5487c478bd9Sstevel@tonic-gate do { 5497c478bd9Sstevel@tonic-gate parg = getarg(&p1); 5507c478bd9Sstevel@tonic-gate add_ugentry(&ruid_tbl, parg); 5517c478bd9Sstevel@tonic-gate } while (*p1); 5527c478bd9Sstevel@tonic-gate break; 5537c478bd9Sstevel@tonic-gate case 'G': /* real group name or number */ 5547c478bd9Sstevel@tonic-gate Gflg++; 5557c478bd9Sstevel@tonic-gate p1 = optarg; 5567c478bd9Sstevel@tonic-gate do { 5577c478bd9Sstevel@tonic-gate parg = getarg(&p1); 5587c478bd9Sstevel@tonic-gate add_ugentry(&rgid_tbl, parg); 5597c478bd9Sstevel@tonic-gate } while (*p1); 5607c478bd9Sstevel@tonic-gate break; 5617c478bd9Sstevel@tonic-gate case 'o': /* output format */ 5627c478bd9Sstevel@tonic-gate p = optarg; 5637c478bd9Sstevel@tonic-gate while ((p = parse_format(p)) != NULL) 5647c478bd9Sstevel@tonic-gate ; 5657c478bd9Sstevel@tonic-gate break; 5667c478bd9Sstevel@tonic-gate case 'z': /* zone name or number */ 5677c478bd9Sstevel@tonic-gate zflg++; 5687c478bd9Sstevel@tonic-gate p1 = optarg; 5697c478bd9Sstevel@tonic-gate do { 5707c478bd9Sstevel@tonic-gate zoneid_t id; 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate parg = getarg(&p1); 5737c478bd9Sstevel@tonic-gate if (zone_get_id(parg, &id) != 0) { 5747c478bd9Sstevel@tonic-gate pgerrflg++; 5757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 5767c478bd9Sstevel@tonic-gate gettext("ps: unknown zone %s\n"), 5777c478bd9Sstevel@tonic-gate parg); 5787c478bd9Sstevel@tonic-gate continue; 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate if (nzoneid == zoneidsz) { 5827c478bd9Sstevel@tonic-gate if ((zoneidsz *= 2) == 0) 5837c478bd9Sstevel@tonic-gate zoneidsz = SIZ; 5847c478bd9Sstevel@tonic-gate zoneid = Realloc(zoneid, 5857c478bd9Sstevel@tonic-gate zoneidsz * sizeof (zoneid_t)); 5867c478bd9Sstevel@tonic-gate } 5877c478bd9Sstevel@tonic-gate zoneid[nzoneid++] = id; 5887c478bd9Sstevel@tonic-gate } while (*p1); 5897c478bd9Sstevel@tonic-gate break; 5907c478bd9Sstevel@tonic-gate case 'Z': /* show zone name */ 5917c478bd9Sstevel@tonic-gate Zflg++; 5927c478bd9Sstevel@tonic-gate break; 5937c478bd9Sstevel@tonic-gate default: /* error on ? */ 5947c478bd9Sstevel@tonic-gate errflg++; 5957c478bd9Sstevel@tonic-gate break; 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate 5987c478bd9Sstevel@tonic-gate if (errflg || optind < argc || pgerrflg) 5997c478bd9Sstevel@tonic-gate usage(); 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate if (tflg) 6027c478bd9Sstevel@tonic-gate tty[ntty].tname = NULL; 6037c478bd9Sstevel@tonic-gate /* 6047c478bd9Sstevel@tonic-gate * If an appropriate option has not been specified, use the 6057c478bd9Sstevel@tonic-gate * current terminal and effective uid as the default. 6067c478bd9Sstevel@tonic-gate */ 6077c478bd9Sstevel@tonic-gate if (!(aflg|Aflg|dflg|Gflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) { 6087c478bd9Sstevel@tonic-gate psinfo_t info; 6097c478bd9Sstevel@tonic-gate int procfd; 6107c478bd9Sstevel@tonic-gate char *name; 6117c478bd9Sstevel@tonic-gate char pname[100]; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate /* get our own controlling tty name using /proc */ 6147c478bd9Sstevel@tonic-gate (void) snprintf(pname, sizeof (pname), 6157c478bd9Sstevel@tonic-gate "%s/self/psinfo", procdir); 6167c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0 || 6177c478bd9Sstevel@tonic-gate read(procfd, (char *)&info, sizeof (info)) < 0 || 6187c478bd9Sstevel@tonic-gate info.pr_ttydev == PRNODEV) { 6197c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6207c478bd9Sstevel@tonic-gate gettext("ps: no controlling terminal\n")); 6217c478bd9Sstevel@tonic-gate exit(1); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate (void) close(procfd); 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate i = 0; 6267c478bd9Sstevel@tonic-gate name = gettty(&info); 6277c478bd9Sstevel@tonic-gate if (*name == '?') { 6287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6297c478bd9Sstevel@tonic-gate gettext("ps: can't find controlling terminal\n")); 6307c478bd9Sstevel@tonic-gate exit(1); 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate if (ntty == ttysz) { 6337c478bd9Sstevel@tonic-gate if ((ttysz *= 2) == 0) 6347c478bd9Sstevel@tonic-gate ttysz = NTTYS; 6357c478bd9Sstevel@tonic-gate tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty)); 6367c478bd9Sstevel@tonic-gate } 6377c478bd9Sstevel@tonic-gate tty[ntty].tdev = info.pr_ttydev; 6387c478bd9Sstevel@tonic-gate tty[ntty++].tname = name; 6397c478bd9Sstevel@tonic-gate tty[ntty].tname = NULL; 6407c478bd9Sstevel@tonic-gate tflg++; 6417c478bd9Sstevel@tonic-gate tuid = getuid(); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate if (Aflg) { 6447c478bd9Sstevel@tonic-gate Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0; 6457c478bd9Sstevel@tonic-gate zflg = 0; 6467c478bd9Sstevel@tonic-gate } 6477c478bd9Sstevel@tonic-gate if (Aflg | aflg | dflg) 6487c478bd9Sstevel@tonic-gate tflg = 0; 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate i = 0; /* prepare to exit on name lookup errors */ 6517c478bd9Sstevel@tonic-gate i += uconv(&euid_tbl); 6527c478bd9Sstevel@tonic-gate i += uconv(&ruid_tbl); 6537c478bd9Sstevel@tonic-gate i += gconv(&egid_tbl); 6547c478bd9Sstevel@tonic-gate i += gconv(&rgid_tbl); 6557c478bd9Sstevel@tonic-gate if (i) 6567c478bd9Sstevel@tonic-gate exit(1); 6577c478bd9Sstevel@tonic-gate 6587c478bd9Sstevel@tonic-gate /* allocate a buffer for lwpsinfo structures */ 6597c478bd9Sstevel@tonic-gate lpbufsize = 4096; 6607c478bd9Sstevel@tonic-gate if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) { 6617c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 6627c478bd9Sstevel@tonic-gate gettext("ps: no memory\n")); 6637c478bd9Sstevel@tonic-gate exit(1); 6647c478bd9Sstevel@tonic-gate } 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate if (fields) { /* print user-specified header */ 6677c478bd9Sstevel@tonic-gate if (do_header) { 6687c478bd9Sstevel@tonic-gate struct field *f; 6697c478bd9Sstevel@tonic-gate 6707c478bd9Sstevel@tonic-gate for (f = fields; f != NULL; f = f->next) { 6717c478bd9Sstevel@tonic-gate if (f != fields) 6727c478bd9Sstevel@tonic-gate (void) printf(" "); 6737c478bd9Sstevel@tonic-gate switch (f->fname) { 6747c478bd9Sstevel@tonic-gate case F_TTY: 6757c478bd9Sstevel@tonic-gate (void) printf("%-*s", 6767c478bd9Sstevel@tonic-gate f->width, f->header); 6777c478bd9Sstevel@tonic-gate break; 6787c478bd9Sstevel@tonic-gate case F_FNAME: 6797c478bd9Sstevel@tonic-gate case F_COMM: 6807c478bd9Sstevel@tonic-gate case F_ARGS: 6817c478bd9Sstevel@tonic-gate /* 6827c478bd9Sstevel@tonic-gate * Print these headers full width 6837c478bd9Sstevel@tonic-gate * unless they appear at the end. 6847c478bd9Sstevel@tonic-gate */ 6857c478bd9Sstevel@tonic-gate if (f->next != NULL) { 6867c478bd9Sstevel@tonic-gate (void) printf("%-*s", 6877c478bd9Sstevel@tonic-gate f->width, f->header); 6887c478bd9Sstevel@tonic-gate } else { 6897c478bd9Sstevel@tonic-gate (void) printf("%s", 6907c478bd9Sstevel@tonic-gate f->header); 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate break; 6937c478bd9Sstevel@tonic-gate default: 6947c478bd9Sstevel@tonic-gate (void) printf("%*s", 6957c478bd9Sstevel@tonic-gate f->width, f->header); 6967c478bd9Sstevel@tonic-gate break; 6977c478bd9Sstevel@tonic-gate } 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate (void) printf("\n"); 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate } else { /* print standard header */ 7027c478bd9Sstevel@tonic-gate if (lflg) { 7037c478bd9Sstevel@tonic-gate if (yflg) 7047c478bd9Sstevel@tonic-gate (void) printf(" S"); 7057c478bd9Sstevel@tonic-gate else 7067c478bd9Sstevel@tonic-gate (void) printf(" F S"); 7077c478bd9Sstevel@tonic-gate } 7087c478bd9Sstevel@tonic-gate if (Zflg) 7097c478bd9Sstevel@tonic-gate (void) printf(" ZONE"); 7107c478bd9Sstevel@tonic-gate if (fflg) { 7117c478bd9Sstevel@tonic-gate if (lflg) 7127c478bd9Sstevel@tonic-gate (void) printf(" "); 7137c478bd9Sstevel@tonic-gate (void) printf(" UID"); 7147c478bd9Sstevel@tonic-gate } else if (lflg) 7157c478bd9Sstevel@tonic-gate (void) printf(" UID"); 7167c478bd9Sstevel@tonic-gate 7177c478bd9Sstevel@tonic-gate (void) printf(" %*s", pidwidth, "PID"); 7187c478bd9Sstevel@tonic-gate if (lflg || fflg) 7197c478bd9Sstevel@tonic-gate (void) printf(" %*s", pidwidth, "PPID"); 7207c478bd9Sstevel@tonic-gate if (jflg) 7217c478bd9Sstevel@tonic-gate (void) printf(" %*s %*s", pidwidth, "PGID", 7227c478bd9Sstevel@tonic-gate pidwidth, "SID"); 7237c478bd9Sstevel@tonic-gate if (Lflg) 7247c478bd9Sstevel@tonic-gate (void) printf(" LWP"); 7257c478bd9Sstevel@tonic-gate if (Pflg) 7267c478bd9Sstevel@tonic-gate (void) printf(" PSR"); 7277c478bd9Sstevel@tonic-gate if (Lflg && fflg) 7287c478bd9Sstevel@tonic-gate (void) printf(" NLWP"); 7297c478bd9Sstevel@tonic-gate if (cflg) 7307c478bd9Sstevel@tonic-gate (void) printf(" CLS PRI"); 7317c478bd9Sstevel@tonic-gate else if (lflg || fflg) { 7327c478bd9Sstevel@tonic-gate (void) printf(" C"); 7337c478bd9Sstevel@tonic-gate if (lflg) 7347c478bd9Sstevel@tonic-gate (void) printf(" PRI NI"); 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate if (lflg) { 7377c478bd9Sstevel@tonic-gate if (yflg) 7387c478bd9Sstevel@tonic-gate (void) printf(" RSS SZ WCHAN"); 7397c478bd9Sstevel@tonic-gate else 7407c478bd9Sstevel@tonic-gate (void) printf(" ADDR SZ WCHAN"); 7417c478bd9Sstevel@tonic-gate } 7427c478bd9Sstevel@tonic-gate if (fflg) 7437c478bd9Sstevel@tonic-gate (void) printf(" STIME"); 7447c478bd9Sstevel@tonic-gate if (Lflg) 7457c478bd9Sstevel@tonic-gate (void) printf(" TTY LTIME CMD\n"); 7467c478bd9Sstevel@tonic-gate else 7477c478bd9Sstevel@tonic-gate (void) printf(" TTY TIME CMD\n"); 7487c478bd9Sstevel@tonic-gate } 7497c478bd9Sstevel@tonic-gate 7507c478bd9Sstevel@tonic-gate 7517c478bd9Sstevel@tonic-gate if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|tflg|gflg|sflg|zflg) && 7527c478bd9Sstevel@tonic-gate npid <= PTHRESHOLD) { 7537c478bd9Sstevel@tonic-gate /* 7547c478bd9Sstevel@tonic-gate * If we are looking at specific processes go straight 7557c478bd9Sstevel@tonic-gate * to their /proc entries and don't scan /proc. 7567c478bd9Sstevel@tonic-gate */ 7577c478bd9Sstevel@tonic-gate int i; 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate (void) qsort(pid, npid, sizeof (pid_t), pidcmp); 7607c478bd9Sstevel@tonic-gate for (i = 0; i < npid; i++) { 7617c478bd9Sstevel@tonic-gate char pname[12]; 7627c478bd9Sstevel@tonic-gate 7637c478bd9Sstevel@tonic-gate if (i >= 1 && pid[i] == pid[i - 1]) 7647c478bd9Sstevel@tonic-gate continue; 7657c478bd9Sstevel@tonic-gate (void) sprintf(pname, "%d", (int)pid[i]); 7667c478bd9Sstevel@tonic-gate if (print_proc(pname) == 0) 7677c478bd9Sstevel@tonic-gate retcode = 0; 7687c478bd9Sstevel@tonic-gate } 7697c478bd9Sstevel@tonic-gate } else { 7707c478bd9Sstevel@tonic-gate /* 7717c478bd9Sstevel@tonic-gate * Determine which processes to print info about by searching 7727c478bd9Sstevel@tonic-gate * the /proc directory and looking at each process. 7737c478bd9Sstevel@tonic-gate */ 7747c478bd9Sstevel@tonic-gate if ((dirp = opendir(procdir)) == NULL) { 7757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 7767c478bd9Sstevel@tonic-gate gettext("ps: cannot open PROC directory %s\n"), 7777c478bd9Sstevel@tonic-gate procdir); 7787c478bd9Sstevel@tonic-gate exit(1); 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate /* for each active process --- */ 7827c478bd9Sstevel@tonic-gate while (dentp = readdir(dirp)) { 7837c478bd9Sstevel@tonic-gate if (dentp->d_name[0] == '.') /* skip . and .. */ 7847c478bd9Sstevel@tonic-gate continue; 7857c478bd9Sstevel@tonic-gate if (print_proc(dentp->d_name) == 0) 7867c478bd9Sstevel@tonic-gate retcode = 0; 7877c478bd9Sstevel@tonic-gate } 7887c478bd9Sstevel@tonic-gate 7897c478bd9Sstevel@tonic-gate (void) closedir(dirp); 7907c478bd9Sstevel@tonic-gate } 7917c478bd9Sstevel@tonic-gate return (retcode); 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate 7957c478bd9Sstevel@tonic-gate int 7967c478bd9Sstevel@tonic-gate print_proc(char *pid_name) 7977c478bd9Sstevel@tonic-gate { 7987c478bd9Sstevel@tonic-gate char pname[PATH_MAX]; 7997c478bd9Sstevel@tonic-gate int pdlen; 8007c478bd9Sstevel@tonic-gate int found; 8017c478bd9Sstevel@tonic-gate int procfd; /* filedescriptor for /proc/nnnnn/psinfo */ 8027c478bd9Sstevel@tonic-gate char *tp; /* ptr to ttyname, if any */ 8037c478bd9Sstevel@tonic-gate psinfo_t info; /* process information from /proc */ 8047c478bd9Sstevel@tonic-gate lwpsinfo_t *lwpsinfo; /* array of lwpsinfo structs */ 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name); 8077c478bd9Sstevel@tonic-gate if (pdlen >= sizeof (pname) - 10) 8087c478bd9Sstevel@tonic-gate return (1); 8097c478bd9Sstevel@tonic-gate retry: 8107c478bd9Sstevel@tonic-gate (void) strcpy(&pname[pdlen], "psinfo"); 8117c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) == -1) { 8127c478bd9Sstevel@tonic-gate /* Process may have exited meanwhile. */ 8137c478bd9Sstevel@tonic-gate return (1); 8147c478bd9Sstevel@tonic-gate } 8157c478bd9Sstevel@tonic-gate /* 8167c478bd9Sstevel@tonic-gate * Get the info structure for the process and close quickly. 8177c478bd9Sstevel@tonic-gate */ 8187c478bd9Sstevel@tonic-gate if (read(procfd, (char *)&info, sizeof (info)) < 0) { 8197c478bd9Sstevel@tonic-gate int saverr = errno; 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate (void) close(procfd); 8227c478bd9Sstevel@tonic-gate if (saverr == EAGAIN) 8237c478bd9Sstevel@tonic-gate goto retry; 8247c478bd9Sstevel@tonic-gate if (saverr != ENOENT) 8257c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8267c478bd9Sstevel@tonic-gate gettext("ps: read() on %s: %s\n"), 8277c478bd9Sstevel@tonic-gate pname, err_string(saverr)); 8287c478bd9Sstevel@tonic-gate return (1); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate (void) close(procfd); 8317c478bd9Sstevel@tonic-gate 8327c478bd9Sstevel@tonic-gate found = 0; 8337c478bd9Sstevel@tonic-gate if (info.pr_lwp.pr_state == 0) /* can't happen? */ 8347c478bd9Sstevel@tonic-gate return (1); 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate /* 8377c478bd9Sstevel@tonic-gate * Omit session group leaders for 'a' and 'd' options. 8387c478bd9Sstevel@tonic-gate */ 8397c478bd9Sstevel@tonic-gate if ((info.pr_pid == info.pr_sid) && (dflg || aflg)) 8407c478bd9Sstevel@tonic-gate return (1); 8417c478bd9Sstevel@tonic-gate if (Aflg || dflg) 8427c478bd9Sstevel@tonic-gate found++; 8437c478bd9Sstevel@tonic-gate else if (pflg && search(pid, npid, info.pr_pid)) 8447c478bd9Sstevel@tonic-gate found++; /* ppid in p option arg list */ 8457c478bd9Sstevel@tonic-gate else if (uflg && ugfind(info.pr_euid, &euid_tbl)) 8467c478bd9Sstevel@tonic-gate found++; /* puid in u option arg list */ 8477c478bd9Sstevel@tonic-gate else if (Uflg && ugfind(info.pr_uid, &ruid_tbl)) 8487c478bd9Sstevel@tonic-gate found++; /* puid in U option arg list */ 8497c478bd9Sstevel@tonic-gate #ifdef NOT_YET 8507c478bd9Sstevel@tonic-gate else if (gflg && ugfind(info.pr_egid, &egid_tbl)) 8517c478bd9Sstevel@tonic-gate found++; /* pgid in g option arg list */ 8527c478bd9Sstevel@tonic-gate #endif /* NOT_YET */ 8537c478bd9Sstevel@tonic-gate else if (Gflg && ugfind(info.pr_gid, &rgid_tbl)) 8547c478bd9Sstevel@tonic-gate found++; /* pgid in G option arg list */ 8557c478bd9Sstevel@tonic-gate else if (gflg && search(grpid, ngrpid, info.pr_pgid)) 8567c478bd9Sstevel@tonic-gate found++; /* grpid in g option arg list */ 8577c478bd9Sstevel@tonic-gate else if (sflg && search(sessid, nsessid, info.pr_sid)) 8587c478bd9Sstevel@tonic-gate found++; /* sessid in s option arg list */ 8597c478bd9Sstevel@tonic-gate else if (zflg && search(zoneid, nzoneid, info.pr_zoneid)) 8607c478bd9Sstevel@tonic-gate found++; /* zoneid in z option arg list */ 8617c478bd9Sstevel@tonic-gate if (!found && !tflg && !aflg) 8627c478bd9Sstevel@tonic-gate return (1); 8637c478bd9Sstevel@tonic-gate if (!prfind(found, &info, &tp)) 8647c478bd9Sstevel@tonic-gate return (1); 8657c478bd9Sstevel@tonic-gate if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) { 8667c478bd9Sstevel@tonic-gate ssize_t prsz; 8677c478bd9Sstevel@tonic-gate 8687c478bd9Sstevel@tonic-gate (void) strcpy(&pname[pdlen], "lpsinfo"); 8697c478bd9Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) == -1) 8707c478bd9Sstevel@tonic-gate return (1); 8717c478bd9Sstevel@tonic-gate /* 8727c478bd9Sstevel@tonic-gate * Get the info structures for the lwps. 8737c478bd9Sstevel@tonic-gate */ 8747c478bd9Sstevel@tonic-gate prsz = read(procfd, lpsinfobuf, lpbufsize); 8757c478bd9Sstevel@tonic-gate if (prsz == -1) { 8767c478bd9Sstevel@tonic-gate int saverr = errno; 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate (void) close(procfd); 8797c478bd9Sstevel@tonic-gate if (saverr == EAGAIN) 8807c478bd9Sstevel@tonic-gate goto retry; 8817c478bd9Sstevel@tonic-gate if (saverr != ENOENT) 8827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 8837c478bd9Sstevel@tonic-gate gettext("ps: read() on %s: %s\n"), 8847c478bd9Sstevel@tonic-gate pname, err_string(saverr)); 8857c478bd9Sstevel@tonic-gate return (1); 8867c478bd9Sstevel@tonic-gate } 8877c478bd9Sstevel@tonic-gate (void) close(procfd); 8887c478bd9Sstevel@tonic-gate if (prsz == lpbufsize) { 8897c478bd9Sstevel@tonic-gate /* 8907c478bd9Sstevel@tonic-gate * buffer overflow. Realloc new buffer. 8917c478bd9Sstevel@tonic-gate * Error handling is done in Realloc(). 8927c478bd9Sstevel@tonic-gate */ 8937c478bd9Sstevel@tonic-gate lpbufsize *= 2; 8947c478bd9Sstevel@tonic-gate lpsinfobuf = Realloc(lpsinfobuf, lpbufsize); 8957c478bd9Sstevel@tonic-gate goto retry; 8967c478bd9Sstevel@tonic-gate } 8977c478bd9Sstevel@tonic-gate if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb)) 8987c478bd9Sstevel@tonic-gate goto retry; 8997c478bd9Sstevel@tonic-gate lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1); 9007c478bd9Sstevel@tonic-gate } 9017c478bd9Sstevel@tonic-gate if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) { 9027c478bd9Sstevel@tonic-gate prcom(&info, tp); 9037c478bd9Sstevel@tonic-gate } else { 9047c478bd9Sstevel@tonic-gate int nlwp = 0; 9057c478bd9Sstevel@tonic-gate 9067c478bd9Sstevel@tonic-gate do { 9077c478bd9Sstevel@tonic-gate info.pr_lwp = *lwpsinfo; 9087c478bd9Sstevel@tonic-gate prcom(&info, tp); 9097c478bd9Sstevel@tonic-gate /* LINTED improper alignment */ 9107c478bd9Sstevel@tonic-gate lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo + 9117c478bd9Sstevel@tonic-gate lpsinfobuf->pr_entsize); 9127c478bd9Sstevel@tonic-gate } while (++nlwp < lpsinfobuf->pr_nent); 9137c478bd9Sstevel@tonic-gate } 9147c478bd9Sstevel@tonic-gate return (0); 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate static void 9197c478bd9Sstevel@tonic-gate usage(void) /* print usage message and quit */ 9207c478bd9Sstevel@tonic-gate { 9217c478bd9Sstevel@tonic-gate static char usage1[] = 9227c478bd9Sstevel@tonic-gate "ps [ -aAdeflcjLPyZ ] [ -o format ] [ -t termlist ]"; 9237c478bd9Sstevel@tonic-gate static char usage2[] = 9247c478bd9Sstevel@tonic-gate "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]"; 9257c478bd9Sstevel@tonic-gate static char usage3[] = 9267c478bd9Sstevel@tonic-gate "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ]"; 9277c478bd9Sstevel@tonic-gate static char usage4[] = 9287c478bd9Sstevel@tonic-gate " 'format' is one or more of:"; 9297c478bd9Sstevel@tonic-gate static char usage5[] = 9307c478bd9Sstevel@tonic-gate "\tuser ruser group rgroup uid ruid gid rgid pid ppid pgid " 9317c478bd9Sstevel@tonic-gate "sid taskid ctid"; 9327c478bd9Sstevel@tonic-gate static char usage6[] = 9337c478bd9Sstevel@tonic-gate "\tpri opri pcpu pmem vsz rss osz nice class time etime stime zone " 9347c478bd9Sstevel@tonic-gate "zoneid"; 9357c478bd9Sstevel@tonic-gate static char usage7[] = 9367c478bd9Sstevel@tonic-gate "\tf s c lwp nlwp psr tty addr wchan fname comm args " 9377c478bd9Sstevel@tonic-gate "projid project pset"; 9387c478bd9Sstevel@tonic-gate 9397c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 9407c478bd9Sstevel@tonic-gate gettext("usage: %s\n%s\n%s\n%s\n%s\n%s\n%s\n"), 9417c478bd9Sstevel@tonic-gate gettext(usage1), gettext(usage2), gettext(usage3), 9427c478bd9Sstevel@tonic-gate gettext(usage4), gettext(usage5), gettext(usage6), gettext(usage7)); 9437c478bd9Sstevel@tonic-gate exit(1); 9447c478bd9Sstevel@tonic-gate } 9457c478bd9Sstevel@tonic-gate 9467c478bd9Sstevel@tonic-gate /* 9477c478bd9Sstevel@tonic-gate * getarg() finds the next argument in list and copies arg into argbuf. 9487c478bd9Sstevel@tonic-gate * p1 first pts to arg passed back from getopt routine. p1 is then 9497c478bd9Sstevel@tonic-gate * bumped to next character that is not a comma or blank -- p1 NULL 9507c478bd9Sstevel@tonic-gate * indicates end of list. 9517c478bd9Sstevel@tonic-gate */ 9527c478bd9Sstevel@tonic-gate static char * 9537c478bd9Sstevel@tonic-gate getarg(char **pp1) 9547c478bd9Sstevel@tonic-gate { 9557c478bd9Sstevel@tonic-gate static char argbuf[ARGSIZ]; 9567c478bd9Sstevel@tonic-gate char *p1 = *pp1; 9577c478bd9Sstevel@tonic-gate char *parga = argbuf; 9587c478bd9Sstevel@tonic-gate int c; 9597c478bd9Sstevel@tonic-gate 9607c478bd9Sstevel@tonic-gate while ((c = *p1) != '\0' && (c == ',' || isspace(c))) 9617c478bd9Sstevel@tonic-gate p1++; 9627c478bd9Sstevel@tonic-gate 9637c478bd9Sstevel@tonic-gate while ((c = *p1) != '\0' && c != ',' && !isspace(c)) { 9647c478bd9Sstevel@tonic-gate if (parga < argbuf + ARGSIZ - 1) 9657c478bd9Sstevel@tonic-gate *parga++ = c; 9667c478bd9Sstevel@tonic-gate p1++; 9677c478bd9Sstevel@tonic-gate } 9687c478bd9Sstevel@tonic-gate *parga = '\0'; 9697c478bd9Sstevel@tonic-gate 9707c478bd9Sstevel@tonic-gate while ((c = *p1) != '\0' && (c == ',' || isspace(c))) 9717c478bd9Sstevel@tonic-gate p1++; 9727c478bd9Sstevel@tonic-gate 9737c478bd9Sstevel@tonic-gate *pp1 = p1; 9747c478bd9Sstevel@tonic-gate 9757c478bd9Sstevel@tonic-gate return (argbuf); 9767c478bd9Sstevel@tonic-gate } 9777c478bd9Sstevel@tonic-gate 9787c478bd9Sstevel@tonic-gate /* 9797c478bd9Sstevel@tonic-gate * parse_format() takes the argument to the -o option, 9807c478bd9Sstevel@tonic-gate * sets up the next output field structure, and returns 9817c478bd9Sstevel@tonic-gate * a pointer to any further output field specifier(s). 9827c478bd9Sstevel@tonic-gate * As a side-effect, it increments errflg if encounters a format error. 9837c478bd9Sstevel@tonic-gate */ 9847c478bd9Sstevel@tonic-gate static char * 9857c478bd9Sstevel@tonic-gate parse_format(char *arg) 9867c478bd9Sstevel@tonic-gate { 9877c478bd9Sstevel@tonic-gate int c; 9887c478bd9Sstevel@tonic-gate char *name; 9897c478bd9Sstevel@tonic-gate char *header = NULL; 9907c478bd9Sstevel@tonic-gate int width = 0; 9917c478bd9Sstevel@tonic-gate struct def_field *df; 9927c478bd9Sstevel@tonic-gate struct field *f; 9937c478bd9Sstevel@tonic-gate 9947c478bd9Sstevel@tonic-gate while ((c = *arg) != '\0' && (c == ',' || isspace(c))) 9957c478bd9Sstevel@tonic-gate arg++; 9967c478bd9Sstevel@tonic-gate if (c == '\0') 9977c478bd9Sstevel@tonic-gate return (NULL); 9987c478bd9Sstevel@tonic-gate name = arg; 9997c478bd9Sstevel@tonic-gate arg = strpbrk(arg, " \t\r\v\f\n,="); 10007c478bd9Sstevel@tonic-gate if (arg != NULL) { 10017c478bd9Sstevel@tonic-gate c = *arg; 10027c478bd9Sstevel@tonic-gate *arg++ = '\0'; 10037c478bd9Sstevel@tonic-gate if (c == '=') { 10047c478bd9Sstevel@tonic-gate char *s; 10057c478bd9Sstevel@tonic-gate 10067c478bd9Sstevel@tonic-gate header = arg; 10077c478bd9Sstevel@tonic-gate arg = NULL; 10087c478bd9Sstevel@tonic-gate width = strlen(header); 10097c478bd9Sstevel@tonic-gate s = header + width; 10107c478bd9Sstevel@tonic-gate while (s > header && isspace(*--s)) 10117c478bd9Sstevel@tonic-gate *s = '\0'; 10127c478bd9Sstevel@tonic-gate while (isspace(*header)) 10137c478bd9Sstevel@tonic-gate header++; 10147c478bd9Sstevel@tonic-gate } 10157c478bd9Sstevel@tonic-gate } 10167c478bd9Sstevel@tonic-gate for (df = &fname[0]; df < &fname[NFIELDS]; df++) 10177c478bd9Sstevel@tonic-gate if (strcmp(name, df->fname) == 0) { 10187c478bd9Sstevel@tonic-gate if (strcmp(name, "lwp") == 0) 10197c478bd9Sstevel@tonic-gate Lflg++; 10207c478bd9Sstevel@tonic-gate break; 10217c478bd9Sstevel@tonic-gate } 10227c478bd9Sstevel@tonic-gate if (df >= &fname[NFIELDS]) { 10237c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10247c478bd9Sstevel@tonic-gate gettext("ps: unknown output format: -o %s\n"), 10257c478bd9Sstevel@tonic-gate name); 10267c478bd9Sstevel@tonic-gate errflg++; 10277c478bd9Sstevel@tonic-gate return (arg); 10287c478bd9Sstevel@tonic-gate } 10297c478bd9Sstevel@tonic-gate if ((f = malloc(sizeof (*f))) == NULL) { 10307c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 10317c478bd9Sstevel@tonic-gate gettext("ps: malloc() for output format failed, %s\n"), 10327c478bd9Sstevel@tonic-gate err_string(errno)); 10337c478bd9Sstevel@tonic-gate exit(1); 10347c478bd9Sstevel@tonic-gate } 10357c478bd9Sstevel@tonic-gate f->next = NULL; 10367c478bd9Sstevel@tonic-gate f->fname = df - &fname[0]; 10377c478bd9Sstevel@tonic-gate f->header = header? header : df->header; 10387c478bd9Sstevel@tonic-gate if (width == 0) 10397c478bd9Sstevel@tonic-gate width = df->width; 10407c478bd9Sstevel@tonic-gate if (*f->header != '\0') 10417c478bd9Sstevel@tonic-gate do_header = 1; 10427c478bd9Sstevel@tonic-gate f->width = max(width, df->minwidth); 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate if (fields == NULL) 10457c478bd9Sstevel@tonic-gate fields = last_field = f; 10467c478bd9Sstevel@tonic-gate else { 10477c478bd9Sstevel@tonic-gate last_field->next = f; 10487c478bd9Sstevel@tonic-gate last_field = f; 10497c478bd9Sstevel@tonic-gate } 10507c478bd9Sstevel@tonic-gate 10517c478bd9Sstevel@tonic-gate return (arg); 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate 10547c478bd9Sstevel@tonic-gate static char * 10557c478bd9Sstevel@tonic-gate devlookup(dev_t ddev) 10567c478bd9Sstevel@tonic-gate { 10577c478bd9Sstevel@tonic-gate struct devl *dp; 10587c478bd9Sstevel@tonic-gate int i; 10597c478bd9Sstevel@tonic-gate 10607c478bd9Sstevel@tonic-gate for (dp = devl, i = 0; i < ndev; dp++, i++) { 10617c478bd9Sstevel@tonic-gate if (dp->ddev == ddev) 10627c478bd9Sstevel@tonic-gate return (dp->dname); 10637c478bd9Sstevel@tonic-gate } 10647c478bd9Sstevel@tonic-gate return (NULL); 10657c478bd9Sstevel@tonic-gate } 10667c478bd9Sstevel@tonic-gate 10677c478bd9Sstevel@tonic-gate static char * 10687c478bd9Sstevel@tonic-gate devadd(char *name, dev_t ddev) 10697c478bd9Sstevel@tonic-gate { 10707c478bd9Sstevel@tonic-gate struct devl *dp; 10717c478bd9Sstevel@tonic-gate int leng, start, i; 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate if (ndev == maxdev) { 10747c478bd9Sstevel@tonic-gate maxdev += DNINCR; 10757c478bd9Sstevel@tonic-gate devl = Realloc(devl, maxdev * sizeof (struct devl)); 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate dp = &devl[ndev++]; 10787c478bd9Sstevel@tonic-gate 10797c478bd9Sstevel@tonic-gate dp->ddev = ddev; 10807c478bd9Sstevel@tonic-gate if (name == NULL) { 10817c478bd9Sstevel@tonic-gate (void) strcpy(dp->dname, "??"); 10827c478bd9Sstevel@tonic-gate return (dp->dname); 10837c478bd9Sstevel@tonic-gate } 10847c478bd9Sstevel@tonic-gate 10857c478bd9Sstevel@tonic-gate leng = strlen(name); 10867c478bd9Sstevel@tonic-gate /* Strip off /dev/ */ 10877c478bd9Sstevel@tonic-gate if (leng < DNSIZE + 4) 10887c478bd9Sstevel@tonic-gate (void) strcpy(dp->dname, &name[5]); 10897c478bd9Sstevel@tonic-gate else { 10907c478bd9Sstevel@tonic-gate start = leng - DNSIZE - 1; 10917c478bd9Sstevel@tonic-gate 10927c478bd9Sstevel@tonic-gate for (i = start; i < leng && name[i] != '/'; i++) 10937c478bd9Sstevel@tonic-gate ; 10947c478bd9Sstevel@tonic-gate if (i == leng) 10957c478bd9Sstevel@tonic-gate (void) strncpy(dp->dname, &name[start], DNSIZE); 10967c478bd9Sstevel@tonic-gate else 10977c478bd9Sstevel@tonic-gate (void) strncpy(dp->dname, &name[i+1], DNSIZE); 10987c478bd9Sstevel@tonic-gate } 10997c478bd9Sstevel@tonic-gate return (dp->dname); 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate 11027c478bd9Sstevel@tonic-gate /* 11037c478bd9Sstevel@tonic-gate * gettty returns the user's tty number or ? if none. 11047c478bd9Sstevel@tonic-gate */ 11057c478bd9Sstevel@tonic-gate static char * 11067c478bd9Sstevel@tonic-gate gettty(psinfo_t *psinfo) 11077c478bd9Sstevel@tonic-gate { 11087c478bd9Sstevel@tonic-gate extern char *_ttyname_dev(dev_t, char *, size_t); 11097c478bd9Sstevel@tonic-gate char devname[TTYNAME_MAX]; 11107c478bd9Sstevel@tonic-gate char *retval; 11117c478bd9Sstevel@tonic-gate 11127c478bd9Sstevel@tonic-gate if (psinfo->pr_ttydev == PRNODEV) 11137c478bd9Sstevel@tonic-gate return ("?"); 11147c478bd9Sstevel@tonic-gate 11157c478bd9Sstevel@tonic-gate if ((retval = devlookup(psinfo->pr_ttydev)) != NULL) 11167c478bd9Sstevel@tonic-gate return (retval); 11177c478bd9Sstevel@tonic-gate 11187c478bd9Sstevel@tonic-gate retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname)); 11197c478bd9Sstevel@tonic-gate 11207c478bd9Sstevel@tonic-gate return (devadd(retval, psinfo->pr_ttydev)); 11217c478bd9Sstevel@tonic-gate } 11227c478bd9Sstevel@tonic-gate 11237c478bd9Sstevel@tonic-gate /* 11247c478bd9Sstevel@tonic-gate * Find the process's tty and return 1 if process is to be printed. 11257c478bd9Sstevel@tonic-gate */ 11267c478bd9Sstevel@tonic-gate static int 11277c478bd9Sstevel@tonic-gate prfind(int found, psinfo_t *psinfo, char **tpp) 11287c478bd9Sstevel@tonic-gate { 11297c478bd9Sstevel@tonic-gate char *tp; 11307c478bd9Sstevel@tonic-gate struct tty *ttyp; 11317c478bd9Sstevel@tonic-gate 11327c478bd9Sstevel@tonic-gate if (psinfo->pr_nlwp == 0) { 11337c478bd9Sstevel@tonic-gate /* process is a zombie */ 11347c478bd9Sstevel@tonic-gate *tpp = "?"; 11357c478bd9Sstevel@tonic-gate if (tflg && !found) 11367c478bd9Sstevel@tonic-gate return (0); 11377c478bd9Sstevel@tonic-gate return (1); 11387c478bd9Sstevel@tonic-gate } 11397c478bd9Sstevel@tonic-gate 11407c478bd9Sstevel@tonic-gate /* 11417c478bd9Sstevel@tonic-gate * Get current terminal. If none ("?") and 'a' is set, don't print 11427c478bd9Sstevel@tonic-gate * info. If 't' is set, check if term is in list of desired terminals 11437c478bd9Sstevel@tonic-gate * and print it if it is. 11447c478bd9Sstevel@tonic-gate */ 11457c478bd9Sstevel@tonic-gate tp = gettty(psinfo); 11467c478bd9Sstevel@tonic-gate if (aflg && *tp == '?') { 11477c478bd9Sstevel@tonic-gate *tpp = tp; 11487c478bd9Sstevel@tonic-gate return (0); 11497c478bd9Sstevel@tonic-gate } 11507c478bd9Sstevel@tonic-gate if (tflg && !found) { 11517c478bd9Sstevel@tonic-gate int match = 0; 11527c478bd9Sstevel@tonic-gate char *other = NULL; 11537c478bd9Sstevel@tonic-gate for (ttyp = tty; ttyp->tname != NULL; ttyp++) { 11547c478bd9Sstevel@tonic-gate /* 11557c478bd9Sstevel@tonic-gate * Look for a name match 11567c478bd9Sstevel@tonic-gate */ 11577c478bd9Sstevel@tonic-gate if (strcmp(tp, ttyp->tname) == 0) { 11587c478bd9Sstevel@tonic-gate match = 1; 11597c478bd9Sstevel@tonic-gate break; 11607c478bd9Sstevel@tonic-gate } 11617c478bd9Sstevel@tonic-gate /* 11627c478bd9Sstevel@tonic-gate * Look for same device under different names. 11637c478bd9Sstevel@tonic-gate */ 11647c478bd9Sstevel@tonic-gate if ((other == NULL) && 11657c478bd9Sstevel@tonic-gate (ttyp->tdev != PRNODEV) && 11667c478bd9Sstevel@tonic-gate (psinfo->pr_ttydev == ttyp->tdev)) 11677c478bd9Sstevel@tonic-gate other = ttyp->tname; 11687c478bd9Sstevel@tonic-gate } 11697c478bd9Sstevel@tonic-gate if (!match && (other != NULL)) { 11707c478bd9Sstevel@tonic-gate /* 11717c478bd9Sstevel@tonic-gate * found under a different name 11727c478bd9Sstevel@tonic-gate */ 11737c478bd9Sstevel@tonic-gate match = 1; 11747c478bd9Sstevel@tonic-gate tp = other; 11757c478bd9Sstevel@tonic-gate } 11767c478bd9Sstevel@tonic-gate if (!match || (tuid != -1 && tuid != psinfo->pr_euid)) { 11777c478bd9Sstevel@tonic-gate /* 11787c478bd9Sstevel@tonic-gate * not found OR not matching euid 11797c478bd9Sstevel@tonic-gate */ 11807c478bd9Sstevel@tonic-gate *tpp = tp; 11817c478bd9Sstevel@tonic-gate return (0); 11827c478bd9Sstevel@tonic-gate } 11837c478bd9Sstevel@tonic-gate } 11847c478bd9Sstevel@tonic-gate *tpp = tp; 11857c478bd9Sstevel@tonic-gate return (1); 11867c478bd9Sstevel@tonic-gate } 11877c478bd9Sstevel@tonic-gate 11887c478bd9Sstevel@tonic-gate /* 11897c478bd9Sstevel@tonic-gate * Print info about the process. 11907c478bd9Sstevel@tonic-gate */ 11917c478bd9Sstevel@tonic-gate static void 11927c478bd9Sstevel@tonic-gate prcom(psinfo_t *psinfo, char *ttyp) 11937c478bd9Sstevel@tonic-gate { 11947c478bd9Sstevel@tonic-gate char *cp; 11957c478bd9Sstevel@tonic-gate long tm; 11967c478bd9Sstevel@tonic-gate int bytesleft; 11977c478bd9Sstevel@tonic-gate int wcnt, length; 11987c478bd9Sstevel@tonic-gate wchar_t wchar; 11997c478bd9Sstevel@tonic-gate struct passwd *pwd; 12007c478bd9Sstevel@tonic-gate int zombie_lwp; 12017c478bd9Sstevel@tonic-gate char zonename[ZONENAME_MAX]; 12027c478bd9Sstevel@tonic-gate 12037c478bd9Sstevel@tonic-gate /* 12047c478bd9Sstevel@tonic-gate * If process is zombie, call zombie print routine and return. 12057c478bd9Sstevel@tonic-gate */ 12067c478bd9Sstevel@tonic-gate if (psinfo->pr_nlwp == 0) { 12077c478bd9Sstevel@tonic-gate if (fields != NULL) 12087c478bd9Sstevel@tonic-gate pr_fields(psinfo, ttyp, print_zombie_field); 12097c478bd9Sstevel@tonic-gate else 12107c478bd9Sstevel@tonic-gate przom(psinfo); 12117c478bd9Sstevel@tonic-gate return; 12127c478bd9Sstevel@tonic-gate } 12137c478bd9Sstevel@tonic-gate 12147c478bd9Sstevel@tonic-gate zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z'); 12157c478bd9Sstevel@tonic-gate 12167c478bd9Sstevel@tonic-gate /* 12177c478bd9Sstevel@tonic-gate * If user specified '-o format', print requested fields and return. 12187c478bd9Sstevel@tonic-gate */ 12197c478bd9Sstevel@tonic-gate if (fields != NULL) { 12207c478bd9Sstevel@tonic-gate pr_fields(psinfo, ttyp, print_field); 12217c478bd9Sstevel@tonic-gate return; 12227c478bd9Sstevel@tonic-gate } 12237c478bd9Sstevel@tonic-gate 12247c478bd9Sstevel@tonic-gate /* 12257c478bd9Sstevel@tonic-gate * All fields before 'PID' are printed with a trailing space as a 12267c478bd9Sstevel@tonic-gate * spearator, rather than keeping track of which column is first. All 12277c478bd9Sstevel@tonic-gate * other fields are printed with a leading space. 12287c478bd9Sstevel@tonic-gate */ 12297c478bd9Sstevel@tonic-gate if (lflg) { 12307c478bd9Sstevel@tonic-gate if (!yflg) 12317c478bd9Sstevel@tonic-gate (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */ 12327c478bd9Sstevel@tonic-gate (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */ 12337c478bd9Sstevel@tonic-gate } 12347c478bd9Sstevel@tonic-gate 12357c478bd9Sstevel@tonic-gate if (Zflg) { /* ZONE */ 12367c478bd9Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename, 12377c478bd9Sstevel@tonic-gate sizeof (zonename)) < 0) { 12387c478bd9Sstevel@tonic-gate (void) printf("%7.7d ", ((int)psinfo->pr_zoneid)); 12397c478bd9Sstevel@tonic-gate } else { 12407c478bd9Sstevel@tonic-gate (void) printf("%8.8s ", zonename); 12417c478bd9Sstevel@tonic-gate } 12427c478bd9Sstevel@tonic-gate } 12437c478bd9Sstevel@tonic-gate 12447c478bd9Sstevel@tonic-gate if (fflg) { /* UID */ 12457c478bd9Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) 12467c478bd9Sstevel@tonic-gate (void) printf("%8.8s ", pwd->pw_name); 12477c478bd9Sstevel@tonic-gate else 12487c478bd9Sstevel@tonic-gate (void) printf("%7.7d ", (int)psinfo->pr_euid); 12497c478bd9Sstevel@tonic-gate } else if (lflg) { 12507c478bd9Sstevel@tonic-gate (void) printf("%6d ", (int)psinfo->pr_euid); 12517c478bd9Sstevel@tonic-gate } 12527c478bd9Sstevel@tonic-gate (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */ 12537c478bd9Sstevel@tonic-gate if (lflg || fflg) 12547c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 12557c478bd9Sstevel@tonic-gate (int)psinfo->pr_ppid); /* PPID */ 12567c478bd9Sstevel@tonic-gate if (jflg) { 12577c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 12587c478bd9Sstevel@tonic-gate (int)psinfo->pr_pgid); /* PGID */ 12597c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 12607c478bd9Sstevel@tonic-gate (int)psinfo->pr_sid); /* SID */ 12617c478bd9Sstevel@tonic-gate } 12627c478bd9Sstevel@tonic-gate if (Lflg) 12637c478bd9Sstevel@tonic-gate (void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */ 12647c478bd9Sstevel@tonic-gate if (Pflg) { 12657c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */ 12667c478bd9Sstevel@tonic-gate (void) printf(" -"); 12677c478bd9Sstevel@tonic-gate else 12687c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_bindpro); 12697c478bd9Sstevel@tonic-gate } 12707c478bd9Sstevel@tonic-gate if (Lflg && fflg) /* NLWP */ 12717c478bd9Sstevel@tonic-gate (void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb); 12727c478bd9Sstevel@tonic-gate if (cflg) { 12737c478bd9Sstevel@tonic-gate if (zombie_lwp) /* CLS */ 12747c478bd9Sstevel@tonic-gate (void) printf(" "); 12757c478bd9Sstevel@tonic-gate else 12767c478bd9Sstevel@tonic-gate (void) printf(" %4s", psinfo->pr_lwp.pr_clname); 12777c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */ 12787c478bd9Sstevel@tonic-gate } else if (lflg || fflg) { 12797c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */ 12807c478bd9Sstevel@tonic-gate if (lflg) { /* PRI NI */ 12817c478bd9Sstevel@tonic-gate /* 12827c478bd9Sstevel@tonic-gate * Print priorities the old way (lower numbers 12837c478bd9Sstevel@tonic-gate * mean higher priority) and print nice value 12847c478bd9Sstevel@tonic-gate * for time sharing procs. 12857c478bd9Sstevel@tonic-gate */ 12867c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_oldpri); 12877c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_oldpri != 0) 12887c478bd9Sstevel@tonic-gate (void) printf(" %2d", psinfo->pr_lwp.pr_nice); 12897c478bd9Sstevel@tonic-gate else 12907c478bd9Sstevel@tonic-gate (void) printf(" %2.2s", 12917c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_clname); 12927c478bd9Sstevel@tonic-gate } 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate if (lflg) { 12957c478bd9Sstevel@tonic-gate if (yflg) { 12967c478bd9Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* RSS */ 12977c478bd9Sstevel@tonic-gate (void) printf(" 0"); 12987c478bd9Sstevel@tonic-gate else if (psinfo->pr_rssize) 12997c478bd9Sstevel@tonic-gate (void) printf(" %5lu", 13007c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_rssize); 13017c478bd9Sstevel@tonic-gate else 13027c478bd9Sstevel@tonic-gate (void) printf(" ?"); 13037c478bd9Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* SZ */ 13047c478bd9Sstevel@tonic-gate (void) printf(" 0"); 13057c478bd9Sstevel@tonic-gate else if (psinfo->pr_size) 13067c478bd9Sstevel@tonic-gate (void) printf(" %6lu", 13077c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_size); 13087c478bd9Sstevel@tonic-gate else 13097c478bd9Sstevel@tonic-gate (void) printf(" ?"); 13107c478bd9Sstevel@tonic-gate } else { 13117c478bd9Sstevel@tonic-gate #ifndef _LP64 13127c478bd9Sstevel@tonic-gate if (psinfo->pr_addr) /* ADDR */ 13137c478bd9Sstevel@tonic-gate (void) printf(" %8lx", 13147c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_addr); 13157c478bd9Sstevel@tonic-gate else 13167c478bd9Sstevel@tonic-gate #endif 13177c478bd9Sstevel@tonic-gate (void) printf(" ?"); 13187c478bd9Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* SZ */ 13197c478bd9Sstevel@tonic-gate (void) printf(" 0"); 13207c478bd9Sstevel@tonic-gate else if (psinfo->pr_size) 13217c478bd9Sstevel@tonic-gate (void) printf(" %6lu", 13227c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_size / kbytes_per_page); 13237c478bd9Sstevel@tonic-gate else 13247c478bd9Sstevel@tonic-gate (void) printf(" ?"); 13257c478bd9Sstevel@tonic-gate } 13267c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_sname != 'S') /* WCHAN */ 13277c478bd9Sstevel@tonic-gate (void) printf(" "); 13287c478bd9Sstevel@tonic-gate #ifndef _LP64 13297c478bd9Sstevel@tonic-gate else if (psinfo->pr_lwp.pr_wchan) 13307c478bd9Sstevel@tonic-gate (void) printf(" %8lx", 13317c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_lwp.pr_wchan); 13327c478bd9Sstevel@tonic-gate #endif 13337c478bd9Sstevel@tonic-gate else 13347c478bd9Sstevel@tonic-gate (void) printf(" ?"); 13357c478bd9Sstevel@tonic-gate } 13367c478bd9Sstevel@tonic-gate if (fflg) { /* STIME */ 13377c478bd9Sstevel@tonic-gate if (Lflg) 13387c478bd9Sstevel@tonic-gate prtime(psinfo->pr_lwp.pr_start, 9, 1); 13397c478bd9Sstevel@tonic-gate else 13407c478bd9Sstevel@tonic-gate prtime(psinfo->pr_start, 9, 1); 13417c478bd9Sstevel@tonic-gate } 13427c478bd9Sstevel@tonic-gate (void) printf(" %-8.14s", ttyp); /* TTY */ 13437c478bd9Sstevel@tonic-gate if (Lflg) { 13447c478bd9Sstevel@tonic-gate tm = psinfo->pr_lwp.pr_time.tv_sec; 13457c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000) 13467c478bd9Sstevel@tonic-gate tm++; 13477c478bd9Sstevel@tonic-gate } else { 13487c478bd9Sstevel@tonic-gate tm = psinfo->pr_time.tv_sec; 13497c478bd9Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000) 13507c478bd9Sstevel@tonic-gate tm++; 13517c478bd9Sstevel@tonic-gate } 13527c478bd9Sstevel@tonic-gate (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* [L]TIME */ 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate if (zombie_lwp) { 13557c478bd9Sstevel@tonic-gate (void) printf(" <defunct>\n"); 13567c478bd9Sstevel@tonic-gate return; 13577c478bd9Sstevel@tonic-gate } 13587c478bd9Sstevel@tonic-gate 13597c478bd9Sstevel@tonic-gate if (!fflg) { /* CMD */ 13607c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_fname, 16, 8); 13617c478bd9Sstevel@tonic-gate (void) printf(" %.*s\n", wcnt, psinfo->pr_fname); 13627c478bd9Sstevel@tonic-gate return; 13637c478bd9Sstevel@tonic-gate } 13647c478bd9Sstevel@tonic-gate 13657c478bd9Sstevel@tonic-gate /* 13667c478bd9Sstevel@tonic-gate * PRARGSZ == length of cmd arg string. 13677c478bd9Sstevel@tonic-gate */ 13687c478bd9Sstevel@tonic-gate psinfo->pr_psargs[PRARGSZ-1] = '\0'; 13697c478bd9Sstevel@tonic-gate bytesleft = PRARGSZ; 13707c478bd9Sstevel@tonic-gate for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) { 13717c478bd9Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX); 13727c478bd9Sstevel@tonic-gate if (length == 0) 13737c478bd9Sstevel@tonic-gate break; 13747c478bd9Sstevel@tonic-gate if (length < 0 || !iswprint(wchar)) { 13757c478bd9Sstevel@tonic-gate if (length < 0) 13767c478bd9Sstevel@tonic-gate length = 1; 13777c478bd9Sstevel@tonic-gate if (bytesleft <= length) { 13787c478bd9Sstevel@tonic-gate *cp = '\0'; 13797c478bd9Sstevel@tonic-gate break; 13807c478bd9Sstevel@tonic-gate } 13817c478bd9Sstevel@tonic-gate /* omit the unprintable character */ 13827c478bd9Sstevel@tonic-gate (void) memmove(cp, cp+length, bytesleft-length); 13837c478bd9Sstevel@tonic-gate length = 0; 13847c478bd9Sstevel@tonic-gate } 13857c478bd9Sstevel@tonic-gate bytesleft -= length; 13867c478bd9Sstevel@tonic-gate } 13877c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ); 13887c478bd9Sstevel@tonic-gate (void) printf(" %.*s\n", wcnt, psinfo->pr_psargs); 13897c478bd9Sstevel@tonic-gate } 13907c478bd9Sstevel@tonic-gate 13917c478bd9Sstevel@tonic-gate /* 13927c478bd9Sstevel@tonic-gate * Print percent from 16-bit binary fraction [0 .. 1] 13937c478bd9Sstevel@tonic-gate * Round up .01 to .1 to indicate some small percentage (the 0x7000 below). 13947c478bd9Sstevel@tonic-gate */ 13957c478bd9Sstevel@tonic-gate static void 13967c478bd9Sstevel@tonic-gate prtpct(ushort_t pct, int width) 13977c478bd9Sstevel@tonic-gate { 13987c478bd9Sstevel@tonic-gate uint_t value = pct; /* need 32 bits to compute with */ 13997c478bd9Sstevel@tonic-gate 14007c478bd9Sstevel@tonic-gate value = ((value * 1000) + 0x7000) >> 15; /* [0 .. 1000] */ 14017c478bd9Sstevel@tonic-gate if (value >= 1000) 14027c478bd9Sstevel@tonic-gate value = 999; 14037c478bd9Sstevel@tonic-gate if ((width -= 2) < 2) 14047c478bd9Sstevel@tonic-gate width = 2; 14057c478bd9Sstevel@tonic-gate (void) printf("%*u.%u", width, value / 10, value % 10); 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate 14087c478bd9Sstevel@tonic-gate static void 14097c478bd9Sstevel@tonic-gate print_time(time_t tim, int width) 14107c478bd9Sstevel@tonic-gate { 14117c478bd9Sstevel@tonic-gate char buf[30]; 14127c478bd9Sstevel@tonic-gate time_t seconds; 14137c478bd9Sstevel@tonic-gate time_t minutes; 14147c478bd9Sstevel@tonic-gate time_t hours; 14157c478bd9Sstevel@tonic-gate time_t days; 14167c478bd9Sstevel@tonic-gate 14177c478bd9Sstevel@tonic-gate if (tim < 0) { 14187c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 14197c478bd9Sstevel@tonic-gate return; 14207c478bd9Sstevel@tonic-gate } 14217c478bd9Sstevel@tonic-gate 14227c478bd9Sstevel@tonic-gate seconds = tim % 60; 14237c478bd9Sstevel@tonic-gate tim /= 60; 14247c478bd9Sstevel@tonic-gate minutes = tim % 60; 14257c478bd9Sstevel@tonic-gate tim /= 60; 14267c478bd9Sstevel@tonic-gate hours = tim % 24; 14277c478bd9Sstevel@tonic-gate days = tim / 24; 14287c478bd9Sstevel@tonic-gate 14297c478bd9Sstevel@tonic-gate if (days > 0) { 14307c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld", 14317c478bd9Sstevel@tonic-gate days, hours, minutes, seconds); 14327c478bd9Sstevel@tonic-gate } else if (hours > 0) { 14337c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld", 14347c478bd9Sstevel@tonic-gate hours, minutes, seconds); 14357c478bd9Sstevel@tonic-gate } else { 14367c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld", 14377c478bd9Sstevel@tonic-gate minutes, seconds); 14387c478bd9Sstevel@tonic-gate } 14397c478bd9Sstevel@tonic-gate 14407c478bd9Sstevel@tonic-gate (void) printf("%*s", width, buf); 14417c478bd9Sstevel@tonic-gate } 14427c478bd9Sstevel@tonic-gate 14437c478bd9Sstevel@tonic-gate static void 14447c478bd9Sstevel@tonic-gate print_field(psinfo_t *psinfo, struct field *f, const char *ttyp) 14457c478bd9Sstevel@tonic-gate { 14467c478bd9Sstevel@tonic-gate int width = f->width; 14477c478bd9Sstevel@tonic-gate struct passwd *pwd; 14487c478bd9Sstevel@tonic-gate struct group *grp; 14497c478bd9Sstevel@tonic-gate time_t cputime; 14507c478bd9Sstevel@tonic-gate int bytesleft; 14517c478bd9Sstevel@tonic-gate int wcnt; 14527c478bd9Sstevel@tonic-gate wchar_t wchar; 14537c478bd9Sstevel@tonic-gate char *cp; 14547c478bd9Sstevel@tonic-gate int length; 14557c478bd9Sstevel@tonic-gate ulong_t mask; 14567c478bd9Sstevel@tonic-gate char c, *csave; 14577c478bd9Sstevel@tonic-gate int zombie_lwp; 14587c478bd9Sstevel@tonic-gate 14597c478bd9Sstevel@tonic-gate zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z'); 14607c478bd9Sstevel@tonic-gate 14617c478bd9Sstevel@tonic-gate switch (f->fname) { 14627c478bd9Sstevel@tonic-gate case F_RUSER: 14637c478bd9Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) 14647c478bd9Sstevel@tonic-gate (void) printf("%*s", width, pwd->pw_name); 14657c478bd9Sstevel@tonic-gate else 14667c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_uid); 14677c478bd9Sstevel@tonic-gate break; 14687c478bd9Sstevel@tonic-gate case F_USER: 14697c478bd9Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) 14707c478bd9Sstevel@tonic-gate (void) printf("%*s", width, pwd->pw_name); 14717c478bd9Sstevel@tonic-gate else 14727c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_euid); 14737c478bd9Sstevel@tonic-gate break; 14747c478bd9Sstevel@tonic-gate case F_RGROUP: 14757c478bd9Sstevel@tonic-gate if ((grp = getgrgid(psinfo->pr_gid)) != NULL) 14767c478bd9Sstevel@tonic-gate (void) printf("%*s", width, grp->gr_name); 14777c478bd9Sstevel@tonic-gate else 14787c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_gid); 14797c478bd9Sstevel@tonic-gate break; 14807c478bd9Sstevel@tonic-gate case F_GROUP: 14817c478bd9Sstevel@tonic-gate if ((grp = getgrgid(psinfo->pr_egid)) != NULL) 14827c478bd9Sstevel@tonic-gate (void) printf("%*s", width, grp->gr_name); 14837c478bd9Sstevel@tonic-gate else 14847c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_egid); 14857c478bd9Sstevel@tonic-gate break; 14867c478bd9Sstevel@tonic-gate case F_RUID: 14877c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_uid); 14887c478bd9Sstevel@tonic-gate break; 14897c478bd9Sstevel@tonic-gate case F_UID: 14907c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_euid); 14917c478bd9Sstevel@tonic-gate break; 14927c478bd9Sstevel@tonic-gate case F_RGID: 14937c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_gid); 14947c478bd9Sstevel@tonic-gate break; 14957c478bd9Sstevel@tonic-gate case F_GID: 14967c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_egid); 14977c478bd9Sstevel@tonic-gate break; 14987c478bd9Sstevel@tonic-gate case F_PID: 14997c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_pid); 15007c478bd9Sstevel@tonic-gate break; 15017c478bd9Sstevel@tonic-gate case F_PPID: 15027c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_ppid); 15037c478bd9Sstevel@tonic-gate break; 15047c478bd9Sstevel@tonic-gate case F_PGID: 15057c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_pgid); 15067c478bd9Sstevel@tonic-gate break; 15077c478bd9Sstevel@tonic-gate case F_SID: 15087c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_sid); 15097c478bd9Sstevel@tonic-gate break; 15107c478bd9Sstevel@tonic-gate case F_PSR: 15117c478bd9Sstevel@tonic-gate if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE) 15127c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15137c478bd9Sstevel@tonic-gate else 15147c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro); 15157c478bd9Sstevel@tonic-gate break; 15167c478bd9Sstevel@tonic-gate case F_LWP: 15177c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid); 15187c478bd9Sstevel@tonic-gate break; 15197c478bd9Sstevel@tonic-gate case F_NLWP: 15207c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb); 15217c478bd9Sstevel@tonic-gate break; 15227c478bd9Sstevel@tonic-gate case F_OPRI: 15237c478bd9Sstevel@tonic-gate if (zombie_lwp) 15247c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15257c478bd9Sstevel@tonic-gate else 15267c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri); 15277c478bd9Sstevel@tonic-gate break; 15287c478bd9Sstevel@tonic-gate case F_PRI: 15297c478bd9Sstevel@tonic-gate if (zombie_lwp) 15307c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15317c478bd9Sstevel@tonic-gate else 15327c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_pri); 15337c478bd9Sstevel@tonic-gate break; 15347c478bd9Sstevel@tonic-gate case F_F: 15357c478bd9Sstevel@tonic-gate mask = 0xffffffffUL; 15367c478bd9Sstevel@tonic-gate if (width < 8) 15377c478bd9Sstevel@tonic-gate mask >>= (8 - width) * 4; 15387c478bd9Sstevel@tonic-gate (void) printf("%*lx", width, psinfo->pr_flag & mask); 15397c478bd9Sstevel@tonic-gate break; 15407c478bd9Sstevel@tonic-gate case F_S: 15417c478bd9Sstevel@tonic-gate (void) printf("%*c", width, psinfo->pr_lwp.pr_sname); 15427c478bd9Sstevel@tonic-gate break; 15437c478bd9Sstevel@tonic-gate case F_C: 15447c478bd9Sstevel@tonic-gate if (zombie_lwp) 15457c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15467c478bd9Sstevel@tonic-gate else 15477c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_cpu); 15487c478bd9Sstevel@tonic-gate break; 15497c478bd9Sstevel@tonic-gate case F_PCPU: 15507c478bd9Sstevel@tonic-gate if (zombie_lwp) 15517c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15527c478bd9Sstevel@tonic-gate else if (Lflg) 15537c478bd9Sstevel@tonic-gate prtpct(psinfo->pr_lwp.pr_pctcpu, width); 15547c478bd9Sstevel@tonic-gate else 15557c478bd9Sstevel@tonic-gate prtpct(psinfo->pr_pctcpu, width); 15567c478bd9Sstevel@tonic-gate break; 15577c478bd9Sstevel@tonic-gate case F_PMEM: 15587c478bd9Sstevel@tonic-gate prtpct(psinfo->pr_pctmem, width); 15597c478bd9Sstevel@tonic-gate break; 15607c478bd9Sstevel@tonic-gate case F_OSZ: 15617c478bd9Sstevel@tonic-gate (void) printf("%*lu", width, 15627c478bd9Sstevel@tonic-gate (ulong_t)psinfo->pr_size / kbytes_per_page); 15637c478bd9Sstevel@tonic-gate break; 15647c478bd9Sstevel@tonic-gate case F_VSZ: 15657c478bd9Sstevel@tonic-gate (void) printf("%*lu", width, (ulong_t)psinfo->pr_size); 15667c478bd9Sstevel@tonic-gate break; 15677c478bd9Sstevel@tonic-gate case F_RSS: 15687c478bd9Sstevel@tonic-gate (void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize); 15697c478bd9Sstevel@tonic-gate break; 15707c478bd9Sstevel@tonic-gate case F_NICE: 15717c478bd9Sstevel@tonic-gate /* if pr_oldpri is zero, then this class has no nice */ 15727c478bd9Sstevel@tonic-gate if (zombie_lwp) 15737c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15747c478bd9Sstevel@tonic-gate else if (psinfo->pr_lwp.pr_oldpri != 0) 15757c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_nice); 15767c478bd9Sstevel@tonic-gate else 15777c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width, 15787c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_clname); 15797c478bd9Sstevel@tonic-gate break; 15807c478bd9Sstevel@tonic-gate case F_CLASS: 15817c478bd9Sstevel@tonic-gate if (zombie_lwp) 15827c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15837c478bd9Sstevel@tonic-gate else 15847c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width, 15857c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_clname); 15867c478bd9Sstevel@tonic-gate break; 15877c478bd9Sstevel@tonic-gate case F_STIME: 15887c478bd9Sstevel@tonic-gate if (Lflg) 15897c478bd9Sstevel@tonic-gate prtime(psinfo->pr_lwp.pr_start, width, 0); 15907c478bd9Sstevel@tonic-gate else 15917c478bd9Sstevel@tonic-gate prtime(psinfo->pr_start, width, 0); 15927c478bd9Sstevel@tonic-gate break; 15937c478bd9Sstevel@tonic-gate case F_ETIME: 15947c478bd9Sstevel@tonic-gate if (Lflg) 15957c478bd9Sstevel@tonic-gate print_time(delta_secs(&psinfo->pr_lwp.pr_start), 15967c478bd9Sstevel@tonic-gate width); 15977c478bd9Sstevel@tonic-gate else 15987c478bd9Sstevel@tonic-gate print_time(delta_secs(&psinfo->pr_start), width); 15997c478bd9Sstevel@tonic-gate break; 16007c478bd9Sstevel@tonic-gate case F_TIME: 16017c478bd9Sstevel@tonic-gate if (Lflg) { 16027c478bd9Sstevel@tonic-gate cputime = psinfo->pr_lwp.pr_time.tv_sec; 16037c478bd9Sstevel@tonic-gate if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000) 16047c478bd9Sstevel@tonic-gate cputime++; 16057c478bd9Sstevel@tonic-gate } else { 16067c478bd9Sstevel@tonic-gate cputime = psinfo->pr_time.tv_sec; 16077c478bd9Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000) 16087c478bd9Sstevel@tonic-gate cputime++; 16097c478bd9Sstevel@tonic-gate } 16107c478bd9Sstevel@tonic-gate print_time(cputime, width); 16117c478bd9Sstevel@tonic-gate break; 16127c478bd9Sstevel@tonic-gate case F_TTY: 16137c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, ttyp); 16147c478bd9Sstevel@tonic-gate break; 16157c478bd9Sstevel@tonic-gate case F_ADDR: 16167c478bd9Sstevel@tonic-gate if (zombie_lwp) 16177c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16187c478bd9Sstevel@tonic-gate else if (Lflg) 16197c478bd9Sstevel@tonic-gate (void) printf("%*lx", width, 16207c478bd9Sstevel@tonic-gate (long)psinfo->pr_lwp.pr_addr); 16217c478bd9Sstevel@tonic-gate else 16227c478bd9Sstevel@tonic-gate (void) printf("%*lx", width, (long)psinfo->pr_addr); 16237c478bd9Sstevel@tonic-gate break; 16247c478bd9Sstevel@tonic-gate case F_WCHAN: 16257c478bd9Sstevel@tonic-gate if (!zombie_lwp && psinfo->pr_lwp.pr_wchan) 16267c478bd9Sstevel@tonic-gate (void) printf("%*lx", width, 16277c478bd9Sstevel@tonic-gate (long)psinfo->pr_lwp.pr_wchan); 16287c478bd9Sstevel@tonic-gate else 16297c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width, "-"); 16307c478bd9Sstevel@tonic-gate break; 16317c478bd9Sstevel@tonic-gate case F_FNAME: 16327c478bd9Sstevel@tonic-gate /* 16337c478bd9Sstevel@tonic-gate * Print full width unless this is the last output format. 16347c478bd9Sstevel@tonic-gate */ 16357c478bd9Sstevel@tonic-gate if (zombie_lwp) { 16367c478bd9Sstevel@tonic-gate if (f->next != NULL) 16377c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>"); 16387c478bd9Sstevel@tonic-gate else 16397c478bd9Sstevel@tonic-gate (void) printf("%s", "<defunct>"); 16407c478bd9Sstevel@tonic-gate break; 16417c478bd9Sstevel@tonic-gate } 16427c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_fname, 16, width); 16437c478bd9Sstevel@tonic-gate if (f->next != NULL) 16447c478bd9Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, psinfo->pr_fname); 16457c478bd9Sstevel@tonic-gate else 16467c478bd9Sstevel@tonic-gate (void) printf("%-.*s", wcnt, psinfo->pr_fname); 16477c478bd9Sstevel@tonic-gate break; 16487c478bd9Sstevel@tonic-gate case F_COMM: 16497c478bd9Sstevel@tonic-gate if (zombie_lwp) { 16507c478bd9Sstevel@tonic-gate if (f->next != NULL) 16517c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>"); 16527c478bd9Sstevel@tonic-gate else 16537c478bd9Sstevel@tonic-gate (void) printf("%s", "<defunct>"); 16547c478bd9Sstevel@tonic-gate break; 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n"); 16577c478bd9Sstevel@tonic-gate if (csave) { 16587c478bd9Sstevel@tonic-gate c = *csave; 16597c478bd9Sstevel@tonic-gate *csave = '\0'; 16607c478bd9Sstevel@tonic-gate } 16617c478bd9Sstevel@tonic-gate /* FALLTHROUGH */ 16627c478bd9Sstevel@tonic-gate case F_ARGS: 16637c478bd9Sstevel@tonic-gate /* 16647c478bd9Sstevel@tonic-gate * PRARGSZ == length of cmd arg string. 16657c478bd9Sstevel@tonic-gate */ 16667c478bd9Sstevel@tonic-gate if (zombie_lwp) { 16677c478bd9Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>"); 16687c478bd9Sstevel@tonic-gate break; 16697c478bd9Sstevel@tonic-gate } 16707c478bd9Sstevel@tonic-gate psinfo->pr_psargs[PRARGSZ-1] = '\0'; 16717c478bd9Sstevel@tonic-gate bytesleft = PRARGSZ; 16727c478bd9Sstevel@tonic-gate for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) { 16737c478bd9Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX); 16747c478bd9Sstevel@tonic-gate if (length == 0) 16757c478bd9Sstevel@tonic-gate break; 16767c478bd9Sstevel@tonic-gate if (length < 0 || !iswprint(wchar)) { 16777c478bd9Sstevel@tonic-gate if (length < 0) 16787c478bd9Sstevel@tonic-gate length = 1; 16797c478bd9Sstevel@tonic-gate if (bytesleft <= length) { 16807c478bd9Sstevel@tonic-gate *cp = '\0'; 16817c478bd9Sstevel@tonic-gate break; 16827c478bd9Sstevel@tonic-gate } 16837c478bd9Sstevel@tonic-gate /* omit the unprintable character */ 16847c478bd9Sstevel@tonic-gate (void) memmove(cp, cp+length, bytesleft-length); 16857c478bd9Sstevel@tonic-gate length = 0; 16867c478bd9Sstevel@tonic-gate } 16877c478bd9Sstevel@tonic-gate bytesleft -= length; 16887c478bd9Sstevel@tonic-gate } 16897c478bd9Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width); 16907c478bd9Sstevel@tonic-gate /* 16917c478bd9Sstevel@tonic-gate * Print full width unless this is the last format. 16927c478bd9Sstevel@tonic-gate */ 16937c478bd9Sstevel@tonic-gate if (f->next != NULL) 16947c478bd9Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, 16957c478bd9Sstevel@tonic-gate psinfo->pr_psargs); 16967c478bd9Sstevel@tonic-gate else 16977c478bd9Sstevel@tonic-gate (void) printf("%-.*s", wcnt, 16987c478bd9Sstevel@tonic-gate psinfo->pr_psargs); 16997c478bd9Sstevel@tonic-gate if (f->fname == F_COMM && csave) 17007c478bd9Sstevel@tonic-gate *csave = c; 17017c478bd9Sstevel@tonic-gate break; 17027c478bd9Sstevel@tonic-gate case F_TASKID: 17037c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_taskid); 17047c478bd9Sstevel@tonic-gate break; 17057c478bd9Sstevel@tonic-gate case F_PROJID: 17067c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_projid); 17077c478bd9Sstevel@tonic-gate break; 17087c478bd9Sstevel@tonic-gate case F_PROJECT: 17097c478bd9Sstevel@tonic-gate { 17107c478bd9Sstevel@tonic-gate struct project cproj; 17117c478bd9Sstevel@tonic-gate char proj_buf[PROJECT_BUFSZ]; 17127c478bd9Sstevel@tonic-gate 17137c478bd9Sstevel@tonic-gate if ((getprojbyid(psinfo->pr_projid, &cproj, 17147c478bd9Sstevel@tonic-gate (void *)&proj_buf, PROJECT_BUFSZ)) == NULL) 17157c478bd9Sstevel@tonic-gate (void) printf("%*d", width, 17167c478bd9Sstevel@tonic-gate (int)psinfo->pr_projid); 17177c478bd9Sstevel@tonic-gate else 17187c478bd9Sstevel@tonic-gate (void) printf("%*s", width, 17197c478bd9Sstevel@tonic-gate (cproj.pj_name != NULL) ? 17207c478bd9Sstevel@tonic-gate cproj.pj_name : "---"); 17217c478bd9Sstevel@tonic-gate } 17227c478bd9Sstevel@tonic-gate break; 17237c478bd9Sstevel@tonic-gate case F_PSET: 17247c478bd9Sstevel@tonic-gate if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE) 17257c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 17267c478bd9Sstevel@tonic-gate else 17277c478bd9Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset); 17287c478bd9Sstevel@tonic-gate break; 17297c478bd9Sstevel@tonic-gate case F_ZONEID: 17307c478bd9Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_zoneid); 17317c478bd9Sstevel@tonic-gate break; 17327c478bd9Sstevel@tonic-gate case F_ZONE: 17337c478bd9Sstevel@tonic-gate { 17347c478bd9Sstevel@tonic-gate char zonename[ZONENAME_MAX]; 17357c478bd9Sstevel@tonic-gate 17367c478bd9Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename, 17377c478bd9Sstevel@tonic-gate sizeof (zonename)) < 0) { 17387c478bd9Sstevel@tonic-gate (void) printf("%*d", width, 17397c478bd9Sstevel@tonic-gate ((int)psinfo->pr_zoneid)); 17407c478bd9Sstevel@tonic-gate } else { 17417c478bd9Sstevel@tonic-gate (void) printf("%*s", width, zonename); 17427c478bd9Sstevel@tonic-gate } 17437c478bd9Sstevel@tonic-gate } 17447c478bd9Sstevel@tonic-gate break; 17457c478bd9Sstevel@tonic-gate case F_CTID: 17467c478bd9Sstevel@tonic-gate if (psinfo->pr_contract == -1) 17477c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 17487c478bd9Sstevel@tonic-gate else 17497c478bd9Sstevel@tonic-gate (void) printf("%*ld", width, (long)psinfo->pr_contract); 17507c478bd9Sstevel@tonic-gate break; 17517c478bd9Sstevel@tonic-gate } 17527c478bd9Sstevel@tonic-gate } 17537c478bd9Sstevel@tonic-gate 17547c478bd9Sstevel@tonic-gate static void 17557c478bd9Sstevel@tonic-gate print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp) 17567c478bd9Sstevel@tonic-gate { 17577c478bd9Sstevel@tonic-gate int wcnt; 17587c478bd9Sstevel@tonic-gate int width = f->width; 17597c478bd9Sstevel@tonic-gate 17607c478bd9Sstevel@tonic-gate switch (f->fname) { 17617c478bd9Sstevel@tonic-gate case F_FNAME: 17627c478bd9Sstevel@tonic-gate case F_COMM: 17637c478bd9Sstevel@tonic-gate case F_ARGS: 17647c478bd9Sstevel@tonic-gate /* 17657c478bd9Sstevel@tonic-gate * Print full width unless this is the last output format. 17667c478bd9Sstevel@tonic-gate */ 17677c478bd9Sstevel@tonic-gate wcnt = min(width, sizeof ("<defunct>")); 17687c478bd9Sstevel@tonic-gate if (f->next != NULL) 17697c478bd9Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, "<defunct>"); 17707c478bd9Sstevel@tonic-gate else 17717c478bd9Sstevel@tonic-gate (void) printf("%-.*s", wcnt, "<defunct>"); 17727c478bd9Sstevel@tonic-gate break; 17737c478bd9Sstevel@tonic-gate 17747c478bd9Sstevel@tonic-gate case F_PSR: 17757c478bd9Sstevel@tonic-gate case F_PCPU: 17767c478bd9Sstevel@tonic-gate case F_PMEM: 17777c478bd9Sstevel@tonic-gate case F_NICE: 17787c478bd9Sstevel@tonic-gate case F_CLASS: 17797c478bd9Sstevel@tonic-gate case F_STIME: 17807c478bd9Sstevel@tonic-gate case F_ETIME: 17817c478bd9Sstevel@tonic-gate case F_WCHAN: 17827c478bd9Sstevel@tonic-gate case F_PSET: 17837c478bd9Sstevel@tonic-gate (void) printf("%*s", width, "-"); 17847c478bd9Sstevel@tonic-gate break; 17857c478bd9Sstevel@tonic-gate 17867c478bd9Sstevel@tonic-gate case F_OPRI: 17877c478bd9Sstevel@tonic-gate case F_PRI: 17887c478bd9Sstevel@tonic-gate case F_OSZ: 17897c478bd9Sstevel@tonic-gate case F_VSZ: 17907c478bd9Sstevel@tonic-gate case F_RSS: 17917c478bd9Sstevel@tonic-gate (void) printf("%*d", width, 0); 17927c478bd9Sstevel@tonic-gate break; 17937c478bd9Sstevel@tonic-gate 17947c478bd9Sstevel@tonic-gate default: 17957c478bd9Sstevel@tonic-gate print_field(psinfo, f, ttyp); 17967c478bd9Sstevel@tonic-gate break; 17977c478bd9Sstevel@tonic-gate } 17987c478bd9Sstevel@tonic-gate } 17997c478bd9Sstevel@tonic-gate 18007c478bd9Sstevel@tonic-gate static void 18017c478bd9Sstevel@tonic-gate pr_fields(psinfo_t *psinfo, const char *ttyp, 18027c478bd9Sstevel@tonic-gate void (*print_fld)(psinfo_t *, struct field *, const char *)) 18037c478bd9Sstevel@tonic-gate { 18047c478bd9Sstevel@tonic-gate struct field *f; 18057c478bd9Sstevel@tonic-gate 18067c478bd9Sstevel@tonic-gate for (f = fields; f != NULL; f = f->next) { 18077c478bd9Sstevel@tonic-gate print_fld(psinfo, f, ttyp); 18087c478bd9Sstevel@tonic-gate if (f->next != NULL) 18097c478bd9Sstevel@tonic-gate (void) printf(" "); 18107c478bd9Sstevel@tonic-gate } 18117c478bd9Sstevel@tonic-gate (void) printf("\n"); 18127c478bd9Sstevel@tonic-gate } 18137c478bd9Sstevel@tonic-gate 18147c478bd9Sstevel@tonic-gate /* 18157c478bd9Sstevel@tonic-gate * Returns 1 if arg is found in array arr, of length num; 0 otherwise. 18167c478bd9Sstevel@tonic-gate */ 18177c478bd9Sstevel@tonic-gate static int 18187c478bd9Sstevel@tonic-gate search(pid_t *arr, int number, pid_t arg) 18197c478bd9Sstevel@tonic-gate { 18207c478bd9Sstevel@tonic-gate int i; 18217c478bd9Sstevel@tonic-gate 18227c478bd9Sstevel@tonic-gate for (i = 0; i < number; i++) 18237c478bd9Sstevel@tonic-gate if (arg == arr[i]) 18247c478bd9Sstevel@tonic-gate return (1); 18257c478bd9Sstevel@tonic-gate return (0); 18267c478bd9Sstevel@tonic-gate } 18277c478bd9Sstevel@tonic-gate 18287c478bd9Sstevel@tonic-gate /* 18297c478bd9Sstevel@tonic-gate * Add an entry (user, group) to the specified table. 18307c478bd9Sstevel@tonic-gate */ 18317c478bd9Sstevel@tonic-gate static void 18327c478bd9Sstevel@tonic-gate add_ugentry(struct ughead *tbl, char *name) 18337c478bd9Sstevel@tonic-gate { 18347c478bd9Sstevel@tonic-gate struct ugdata *entp; 18357c478bd9Sstevel@tonic-gate 18367c478bd9Sstevel@tonic-gate if (tbl->size == tbl->nent) { /* reallocate the table entries */ 18377c478bd9Sstevel@tonic-gate if ((tbl->size *= 2) == 0) 18387c478bd9Sstevel@tonic-gate tbl->size = 32; /* first time */ 18397c478bd9Sstevel@tonic-gate tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata)); 18407c478bd9Sstevel@tonic-gate } 18417c478bd9Sstevel@tonic-gate entp = &tbl->ent[tbl->nent++]; 18427c478bd9Sstevel@tonic-gate entp->id = 0; 18437c478bd9Sstevel@tonic-gate (void) strncpy(entp->name, name, MAXUGNAME); 18447c478bd9Sstevel@tonic-gate entp->name[MAXUGNAME] = '\0'; 18457c478bd9Sstevel@tonic-gate } 18467c478bd9Sstevel@tonic-gate 18477c478bd9Sstevel@tonic-gate static int 18487c478bd9Sstevel@tonic-gate uconv(struct ughead *uhead) 18497c478bd9Sstevel@tonic-gate { 18507c478bd9Sstevel@tonic-gate struct ugdata *utbl = uhead->ent; 18517c478bd9Sstevel@tonic-gate int n = uhead->nent; 18527c478bd9Sstevel@tonic-gate struct passwd *pwd; 18537c478bd9Sstevel@tonic-gate int i; 18547c478bd9Sstevel@tonic-gate int fnd = 0; 18557c478bd9Sstevel@tonic-gate uid_t uid; 18567c478bd9Sstevel@tonic-gate 18577c478bd9Sstevel@tonic-gate /* 18587c478bd9Sstevel@tonic-gate * Ask the name service for names. 18597c478bd9Sstevel@tonic-gate */ 18607c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) { 18617c478bd9Sstevel@tonic-gate /* 18627c478bd9Sstevel@tonic-gate * If name is numeric, ask for numeric id 18637c478bd9Sstevel@tonic-gate */ 18647c478bd9Sstevel@tonic-gate if (str2id(utbl[i].name, &uid, 0, UID_MAX) == 0) 18657c478bd9Sstevel@tonic-gate pwd = getpwuid(uid); 18667c478bd9Sstevel@tonic-gate else 18677c478bd9Sstevel@tonic-gate pwd = getpwnam(utbl[i].name); 18687c478bd9Sstevel@tonic-gate 18697c478bd9Sstevel@tonic-gate /* 18707c478bd9Sstevel@tonic-gate * If found, enter found index into tbl array. 18717c478bd9Sstevel@tonic-gate */ 18727c478bd9Sstevel@tonic-gate if (pwd == NULL) { 18737c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 18747c478bd9Sstevel@tonic-gate gettext("ps: unknown user %s\n"), utbl[i].name); 18757c478bd9Sstevel@tonic-gate continue; 18767c478bd9Sstevel@tonic-gate } 18777c478bd9Sstevel@tonic-gate 18787c478bd9Sstevel@tonic-gate utbl[fnd].id = pwd->pw_uid; 18797c478bd9Sstevel@tonic-gate (void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME); 18807c478bd9Sstevel@tonic-gate fnd++; 18817c478bd9Sstevel@tonic-gate } 18827c478bd9Sstevel@tonic-gate 18837c478bd9Sstevel@tonic-gate uhead->nent = fnd; /* in case it changed */ 18847c478bd9Sstevel@tonic-gate return (n - fnd); 18857c478bd9Sstevel@tonic-gate } 18867c478bd9Sstevel@tonic-gate 18877c478bd9Sstevel@tonic-gate static int 18887c478bd9Sstevel@tonic-gate gconv(struct ughead *ghead) 18897c478bd9Sstevel@tonic-gate { 18907c478bd9Sstevel@tonic-gate struct ugdata *gtbl = ghead->ent; 18917c478bd9Sstevel@tonic-gate int n = ghead->nent; 18927c478bd9Sstevel@tonic-gate struct group *grp; 18937c478bd9Sstevel@tonic-gate gid_t gid; 18947c478bd9Sstevel@tonic-gate int i; 18957c478bd9Sstevel@tonic-gate int fnd = 0; 18967c478bd9Sstevel@tonic-gate 18977c478bd9Sstevel@tonic-gate /* 18987c478bd9Sstevel@tonic-gate * Ask the name service for names. 18997c478bd9Sstevel@tonic-gate */ 19007c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) { 19017c478bd9Sstevel@tonic-gate /* 19027c478bd9Sstevel@tonic-gate * If name is numeric, ask for numeric id 19037c478bd9Sstevel@tonic-gate */ 19047c478bd9Sstevel@tonic-gate if (str2id(gtbl[i].name, &gid, 0, UID_MAX) == 0) 19057c478bd9Sstevel@tonic-gate grp = getgrgid(gid); 19067c478bd9Sstevel@tonic-gate else 19077c478bd9Sstevel@tonic-gate grp = getgrnam(gtbl[i].name); 19087c478bd9Sstevel@tonic-gate /* 19097c478bd9Sstevel@tonic-gate * If found, enter found index into tbl array. 19107c478bd9Sstevel@tonic-gate */ 19117c478bd9Sstevel@tonic-gate if (grp == NULL) { 19127c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 19137c478bd9Sstevel@tonic-gate gettext("ps: unknown group %s\n"), gtbl[i].name); 19147c478bd9Sstevel@tonic-gate continue; 19157c478bd9Sstevel@tonic-gate } 19167c478bd9Sstevel@tonic-gate 19177c478bd9Sstevel@tonic-gate gtbl[fnd].id = grp->gr_gid; 19187c478bd9Sstevel@tonic-gate (void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME); 19197c478bd9Sstevel@tonic-gate fnd++; 19207c478bd9Sstevel@tonic-gate } 19217c478bd9Sstevel@tonic-gate 19227c478bd9Sstevel@tonic-gate ghead->nent = fnd; /* in case it changed */ 19237c478bd9Sstevel@tonic-gate return (n - fnd); 19247c478bd9Sstevel@tonic-gate } 19257c478bd9Sstevel@tonic-gate 19267c478bd9Sstevel@tonic-gate /* 19277c478bd9Sstevel@tonic-gate * Return 1 if puid is in table, otherwise 0. 19287c478bd9Sstevel@tonic-gate */ 19297c478bd9Sstevel@tonic-gate static int 19307c478bd9Sstevel@tonic-gate ugfind(id_t id, struct ughead *ughead) 19317c478bd9Sstevel@tonic-gate { 19327c478bd9Sstevel@tonic-gate struct ugdata *utbl = ughead->ent; 19337c478bd9Sstevel@tonic-gate int n = ughead->nent; 19347c478bd9Sstevel@tonic-gate int i; 19357c478bd9Sstevel@tonic-gate 19367c478bd9Sstevel@tonic-gate for (i = 0; i < n; i++) 19377c478bd9Sstevel@tonic-gate if (utbl[i].id == id) 19387c478bd9Sstevel@tonic-gate return (1); 19397c478bd9Sstevel@tonic-gate return (0); 19407c478bd9Sstevel@tonic-gate } 19417c478bd9Sstevel@tonic-gate 19427c478bd9Sstevel@tonic-gate /* 19437c478bd9Sstevel@tonic-gate * Print starting time of process unless process started more than 24 hours 19447c478bd9Sstevel@tonic-gate * ago, in which case the date is printed. The date is printed in the form 19457c478bd9Sstevel@tonic-gate * "MMM dd" if old format, else the blank is replaced with an '_' so 19467c478bd9Sstevel@tonic-gate * it appears as a single word (for parseability). 19477c478bd9Sstevel@tonic-gate */ 19487c478bd9Sstevel@tonic-gate static void 19497c478bd9Sstevel@tonic-gate prtime(timestruc_t st, int width, int old) 19507c478bd9Sstevel@tonic-gate { 19517c478bd9Sstevel@tonic-gate char sttim[26]; 19527c478bd9Sstevel@tonic-gate time_t starttime; 19537c478bd9Sstevel@tonic-gate 19547c478bd9Sstevel@tonic-gate starttime = st.tv_sec; 19557c478bd9Sstevel@tonic-gate if (st.tv_nsec > 500000000) 19567c478bd9Sstevel@tonic-gate starttime++; 19577c478bd9Sstevel@tonic-gate if ((now.tv_sec - starttime) >= 24*60*60) { 19587c478bd9Sstevel@tonic-gate (void) strftime(sttim, sizeof (sttim), old? \ 19597c478bd9Sstevel@tonic-gate "%b %d" : "%b_%d", localtime(&starttime)); 19607c478bd9Sstevel@tonic-gate sttim[7] = '\0'; 19617c478bd9Sstevel@tonic-gate } else { 19627c478bd9Sstevel@tonic-gate (void) strftime(sttim, sizeof (sttim), \ 19637c478bd9Sstevel@tonic-gate "%H:%M:%S", localtime(&starttime)); 19647c478bd9Sstevel@tonic-gate sttim[8] = '\0'; 19657c478bd9Sstevel@tonic-gate } 19667c478bd9Sstevel@tonic-gate (void) printf("%*.*s", width, width, sttim); 19677c478bd9Sstevel@tonic-gate } 19687c478bd9Sstevel@tonic-gate 19697c478bd9Sstevel@tonic-gate static void 19707c478bd9Sstevel@tonic-gate przom(psinfo_t *psinfo) 19717c478bd9Sstevel@tonic-gate { 19727c478bd9Sstevel@tonic-gate long tm; 19737c478bd9Sstevel@tonic-gate struct passwd *pwd; 19747c478bd9Sstevel@tonic-gate char zonename[ZONENAME_MAX]; 19757c478bd9Sstevel@tonic-gate 19767c478bd9Sstevel@tonic-gate /* 19777c478bd9Sstevel@tonic-gate * All fields before 'PID' are printed with a trailing space as a 19787c478bd9Sstevel@tonic-gate * spearator, rather than keeping track of which column is first. All 19797c478bd9Sstevel@tonic-gate * other fields are printed with a leading space. 19807c478bd9Sstevel@tonic-gate */ 19817c478bd9Sstevel@tonic-gate if (lflg) { /* F S */ 19827c478bd9Sstevel@tonic-gate if (!yflg) 19837c478bd9Sstevel@tonic-gate (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */ 19847c478bd9Sstevel@tonic-gate (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */ 19857c478bd9Sstevel@tonic-gate } 19867c478bd9Sstevel@tonic-gate if (Zflg) { 19877c478bd9Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename, 19887c478bd9Sstevel@tonic-gate sizeof (zonename)) < 0) { 19897c478bd9Sstevel@tonic-gate (void) printf("%7.7d ", ((int)psinfo->pr_zoneid)); 19907c478bd9Sstevel@tonic-gate } else { 19917c478bd9Sstevel@tonic-gate (void) printf("%8.8s ", zonename); 19927c478bd9Sstevel@tonic-gate } 19937c478bd9Sstevel@tonic-gate } 19947c478bd9Sstevel@tonic-gate if (fflg) { 19957c478bd9Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) 19967c478bd9Sstevel@tonic-gate (void) printf("%8.8s ", pwd->pw_name); 19977c478bd9Sstevel@tonic-gate else 19987c478bd9Sstevel@tonic-gate (void) printf("%7.7d ", (int)psinfo->pr_euid); 19997c478bd9Sstevel@tonic-gate } else if (lflg) 20007c478bd9Sstevel@tonic-gate (void) printf("%6d ", (int)psinfo->pr_euid); 20017c478bd9Sstevel@tonic-gate 20027c478bd9Sstevel@tonic-gate (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */ 20037c478bd9Sstevel@tonic-gate if (lflg || fflg) 20047c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 20057c478bd9Sstevel@tonic-gate (int)psinfo->pr_ppid); /* PPID */ 20067c478bd9Sstevel@tonic-gate 20077c478bd9Sstevel@tonic-gate if (jflg) { 20087c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 20097c478bd9Sstevel@tonic-gate (int)psinfo->pr_pgid); /* PGID */ 20107c478bd9Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 20117c478bd9Sstevel@tonic-gate (int)psinfo->pr_sid); /* SID */ 20127c478bd9Sstevel@tonic-gate } 20137c478bd9Sstevel@tonic-gate 20147c478bd9Sstevel@tonic-gate if (Lflg) 20157c478bd9Sstevel@tonic-gate (void) printf(" %5d", 0); /* LWP */ 20167c478bd9Sstevel@tonic-gate if (Pflg) 20177c478bd9Sstevel@tonic-gate (void) printf(" -"); /* PSR */ 20187c478bd9Sstevel@tonic-gate if (Lflg && fflg) 20197c478bd9Sstevel@tonic-gate (void) printf(" %5d", 0); /* NLWP */ 20207c478bd9Sstevel@tonic-gate 20217c478bd9Sstevel@tonic-gate if (cflg) { 20227c478bd9Sstevel@tonic-gate (void) printf(" %4s", "-"); /* zombies have no class */ 20237c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */ 20247c478bd9Sstevel@tonic-gate } else if (lflg || fflg) { 20257c478bd9Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */ 20267c478bd9Sstevel@tonic-gate if (lflg) 20277c478bd9Sstevel@tonic-gate (void) printf(" %3d %2s", 20287c478bd9Sstevel@tonic-gate psinfo->pr_lwp.pr_oldpri, "-"); /* PRI NI */ 20297c478bd9Sstevel@tonic-gate } 20307c478bd9Sstevel@tonic-gate if (lflg) { 20317c478bd9Sstevel@tonic-gate if (yflg) /* RSS SZ WCHAN */ 20327c478bd9Sstevel@tonic-gate (void) printf(" %5d %6d %8s", 0, 0, "-"); 20337c478bd9Sstevel@tonic-gate else /* ADDR SZ WCHAN */ 20347c478bd9Sstevel@tonic-gate (void) printf(" %8s %6d %8s", "-", 0, "-"); 20357c478bd9Sstevel@tonic-gate } 20367c478bd9Sstevel@tonic-gate if (fflg) 20377c478bd9Sstevel@tonic-gate (void) printf(" %8.8s", "-"); /* STIME */ 20387c478bd9Sstevel@tonic-gate (void) printf(" %-8.14s", "?"); /* TTY */ 20397c478bd9Sstevel@tonic-gate 20407c478bd9Sstevel@tonic-gate tm = psinfo->pr_time.tv_sec; 20417c478bd9Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000) 20427c478bd9Sstevel@tonic-gate tm++; 20437c478bd9Sstevel@tonic-gate (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* TIME */ 20447c478bd9Sstevel@tonic-gate (void) printf(" <defunct>\n"); 20457c478bd9Sstevel@tonic-gate } 20467c478bd9Sstevel@tonic-gate 20477c478bd9Sstevel@tonic-gate /* 20487c478bd9Sstevel@tonic-gate * Function to compute the number of printable bytes in a multibyte 20497c478bd9Sstevel@tonic-gate * command string ("internationalization"). 20507c478bd9Sstevel@tonic-gate */ 20517c478bd9Sstevel@tonic-gate static int 20527c478bd9Sstevel@tonic-gate namencnt(char *cmd, int csisize, int scrsize) 20537c478bd9Sstevel@tonic-gate { 20547c478bd9Sstevel@tonic-gate int csiwcnt = 0, scrwcnt = 0; 20557c478bd9Sstevel@tonic-gate int ncsisz, nscrsz; 20567c478bd9Sstevel@tonic-gate wchar_t wchar; 20577c478bd9Sstevel@tonic-gate int len; 20587c478bd9Sstevel@tonic-gate 20597c478bd9Sstevel@tonic-gate while (*cmd != '\0') { 20607c478bd9Sstevel@tonic-gate if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX) 20617c478bd9Sstevel@tonic-gate len = MB_CUR_MAX; 20627c478bd9Sstevel@tonic-gate if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0) 20637c478bd9Sstevel@tonic-gate return (8); /* default to use for illegal chars */ 20647c478bd9Sstevel@tonic-gate if ((nscrsz = wcwidth(wchar)) <= 0) 20657c478bd9Sstevel@tonic-gate return (8); 20667c478bd9Sstevel@tonic-gate if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize) 20677c478bd9Sstevel@tonic-gate break; 20687c478bd9Sstevel@tonic-gate csiwcnt += ncsisz; 20697c478bd9Sstevel@tonic-gate scrwcnt += nscrsz; 20707c478bd9Sstevel@tonic-gate cmd += ncsisz; 20717c478bd9Sstevel@tonic-gate } 20727c478bd9Sstevel@tonic-gate return (csiwcnt); 20737c478bd9Sstevel@tonic-gate } 20747c478bd9Sstevel@tonic-gate 20757c478bd9Sstevel@tonic-gate static char * 20767c478bd9Sstevel@tonic-gate err_string(int err) 20777c478bd9Sstevel@tonic-gate { 20787c478bd9Sstevel@tonic-gate static char buf[32]; 20797c478bd9Sstevel@tonic-gate char *str = strerror(err); 20807c478bd9Sstevel@tonic-gate 20817c478bd9Sstevel@tonic-gate if (str == NULL) 20827c478bd9Sstevel@tonic-gate (void) snprintf(str = buf, sizeof (buf), "Errno #%d", err); 20837c478bd9Sstevel@tonic-gate 20847c478bd9Sstevel@tonic-gate return (str); 20857c478bd9Sstevel@tonic-gate } 20867c478bd9Sstevel@tonic-gate 20877c478bd9Sstevel@tonic-gate /* If allocation fails, die */ 20887c478bd9Sstevel@tonic-gate static void * 20897c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size) 20907c478bd9Sstevel@tonic-gate { 20917c478bd9Sstevel@tonic-gate ptr = realloc(ptr, size); 20927c478bd9Sstevel@tonic-gate if (ptr == NULL) { 20937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext("ps: no memory\n")); 20947c478bd9Sstevel@tonic-gate exit(1); 20957c478bd9Sstevel@tonic-gate } 20967c478bd9Sstevel@tonic-gate return (ptr); 20977c478bd9Sstevel@tonic-gate } 20987c478bd9Sstevel@tonic-gate 20997c478bd9Sstevel@tonic-gate static time_t 21007c478bd9Sstevel@tonic-gate delta_secs(const timestruc_t *start) 21017c478bd9Sstevel@tonic-gate { 21027c478bd9Sstevel@tonic-gate time_t seconds = now.tv_sec - start->tv_sec; 21037c478bd9Sstevel@tonic-gate long nanosecs = now.tv_usec * 1000 - start->tv_nsec; 21047c478bd9Sstevel@tonic-gate 21057c478bd9Sstevel@tonic-gate if (nanosecs >= (NANOSEC / 2)) 21067c478bd9Sstevel@tonic-gate seconds++; 21077c478bd9Sstevel@tonic-gate else if (nanosecs < -(NANOSEC / 2)) 21087c478bd9Sstevel@tonic-gate seconds--; 21097c478bd9Sstevel@tonic-gate 21107c478bd9Sstevel@tonic-gate return (seconds); 21117c478bd9Sstevel@tonic-gate } 21127c478bd9Sstevel@tonic-gate 21137c478bd9Sstevel@tonic-gate /* 21147c478bd9Sstevel@tonic-gate * Returns the following: 21157c478bd9Sstevel@tonic-gate * 21167c478bd9Sstevel@tonic-gate * 0 No error 21177c478bd9Sstevel@tonic-gate * EINVAL Invalid number 21187c478bd9Sstevel@tonic-gate * ERANGE Value exceeds (min, max) range 21197c478bd9Sstevel@tonic-gate */ 21207c478bd9Sstevel@tonic-gate static int 21217c478bd9Sstevel@tonic-gate str2id(const char *p, pid_t *val, long min, long max) 21227c478bd9Sstevel@tonic-gate { 21237c478bd9Sstevel@tonic-gate char *q; 21247c478bd9Sstevel@tonic-gate long number; 21257c478bd9Sstevel@tonic-gate int error; 21267c478bd9Sstevel@tonic-gate 21277c478bd9Sstevel@tonic-gate errno = 0; 21287c478bd9Sstevel@tonic-gate number = strtol(p, &q, 10); 21297c478bd9Sstevel@tonic-gate 21307c478bd9Sstevel@tonic-gate if (errno != 0 || q == p || *q != '\0') { 21317c478bd9Sstevel@tonic-gate if ((error = errno) == 0) { 21327c478bd9Sstevel@tonic-gate /* 21337c478bd9Sstevel@tonic-gate * strtol() can fail without setting errno, or it can 21347c478bd9Sstevel@tonic-gate * set it to EINVAL or ERANGE. In the case errno is 21357c478bd9Sstevel@tonic-gate * still zero, return EINVAL. 21367c478bd9Sstevel@tonic-gate */ 21377c478bd9Sstevel@tonic-gate error = EINVAL; 21387c478bd9Sstevel@tonic-gate } 21397c478bd9Sstevel@tonic-gate } else if (number < min || number > max) { 21407c478bd9Sstevel@tonic-gate error = ERANGE; 21417c478bd9Sstevel@tonic-gate } else { 21427c478bd9Sstevel@tonic-gate error = 0; 21437c478bd9Sstevel@tonic-gate } 21447c478bd9Sstevel@tonic-gate 21457c478bd9Sstevel@tonic-gate *val = number; 21467c478bd9Sstevel@tonic-gate 21477c478bd9Sstevel@tonic-gate return (error); 21487c478bd9Sstevel@tonic-gate } 21497c478bd9Sstevel@tonic-gate 21507c478bd9Sstevel@tonic-gate static int 21517c478bd9Sstevel@tonic-gate pidcmp(const void *p1, const void *p2) 21527c478bd9Sstevel@tonic-gate { 21537c478bd9Sstevel@tonic-gate pid_t i = *((pid_t *)p1); 21547c478bd9Sstevel@tonic-gate pid_t j = *((pid_t *)p2); 21557c478bd9Sstevel@tonic-gate 21567c478bd9Sstevel@tonic-gate return (i - j); 21577c478bd9Sstevel@tonic-gate } 2158