10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 51454Sjonb * Common Development and Distribution License (the "License"). 61454Sjonb * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 211454Sjonb 220Sstevel@tonic-gate /* 231454Sjonb * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 280Sstevel@tonic-gate /* All Rights Reserved */ 290Sstevel@tonic-gate 300Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 310Sstevel@tonic-gate 320Sstevel@tonic-gate /* 330Sstevel@tonic-gate * ps -- print things about processes. 340Sstevel@tonic-gate */ 350Sstevel@tonic-gate #include <stdio.h> 360Sstevel@tonic-gate #include <ctype.h> 370Sstevel@tonic-gate #include <string.h> 380Sstevel@tonic-gate #include <errno.h> 390Sstevel@tonic-gate #include <fcntl.h> 400Sstevel@tonic-gate #include <pwd.h> 410Sstevel@tonic-gate #include <grp.h> 420Sstevel@tonic-gate #include <sys/types.h> 430Sstevel@tonic-gate #include <sys/stat.h> 440Sstevel@tonic-gate #include <sys/mkdev.h> 450Sstevel@tonic-gate #include <unistd.h> 460Sstevel@tonic-gate #include <stdlib.h> 470Sstevel@tonic-gate #include <limits.h> 480Sstevel@tonic-gate #include <dirent.h> 490Sstevel@tonic-gate #include <sys/signal.h> 500Sstevel@tonic-gate #include <sys/fault.h> 510Sstevel@tonic-gate #include <sys/syscall.h> 520Sstevel@tonic-gate #include <sys/time.h> 530Sstevel@tonic-gate #include <procfs.h> 540Sstevel@tonic-gate #include <locale.h> 550Sstevel@tonic-gate #include <wctype.h> 560Sstevel@tonic-gate #include <wchar.h> 570Sstevel@tonic-gate #include <libw.h> 580Sstevel@tonic-gate #include <stdarg.h> 590Sstevel@tonic-gate #include <sys/proc.h> 600Sstevel@tonic-gate #include <sys/pset.h> 610Sstevel@tonic-gate #include <project.h> 620Sstevel@tonic-gate #include <zone.h> 630Sstevel@tonic-gate 640Sstevel@tonic-gate #define min(a, b) ((a) > (b) ? (b) : (a)) 650Sstevel@tonic-gate #define max(a, b) ((a) < (b) ? (b) : (a)) 660Sstevel@tonic-gate 670Sstevel@tonic-gate #define NTTYS 20 /* initial size of table for -t option */ 68*2685Sakolb #define SIZ 30 /* initial size of tables for -p, -s, -g, -h and -z */ 691454Sjonb 701454Sjonb /* 711454Sjonb * Size of buffer holding args for t, p, s, g, u, U, G, z options. 721454Sjonb * Set to ZONENAME_MAX, the minimum value needed to allow any 731454Sjonb * zone to be specified. 741454Sjonb */ 751454Sjonb #define ARGSIZ ZONENAME_MAX 760Sstevel@tonic-gate 770Sstevel@tonic-gate #define MAXUGNAME 10 /* max chars in a user/group name or printed u/g id */ 780Sstevel@tonic-gate 790Sstevel@tonic-gate /* Structure for storing user or group info */ 800Sstevel@tonic-gate struct ugdata { 810Sstevel@tonic-gate id_t id; /* numeric user-id or group-id */ 820Sstevel@tonic-gate char name[MAXUGNAME+1]; /* user/group name, null terminated */ 830Sstevel@tonic-gate }; 840Sstevel@tonic-gate 850Sstevel@tonic-gate struct ughead { 860Sstevel@tonic-gate size_t size; /* number of ugdata structs allocated */ 870Sstevel@tonic-gate size_t nent; /* number of active entries */ 880Sstevel@tonic-gate struct ugdata *ent; /* pointer to array of actual entries */ 890Sstevel@tonic-gate }; 900Sstevel@tonic-gate 910Sstevel@tonic-gate enum fname { /* enumeration of field names */ 920Sstevel@tonic-gate F_USER, /* effective user of the process */ 930Sstevel@tonic-gate F_RUSER, /* real user of the process */ 940Sstevel@tonic-gate F_GROUP, /* effective group of the process */ 950Sstevel@tonic-gate F_RGROUP, /* real group of the process */ 960Sstevel@tonic-gate F_UID, /* numeric effective uid of the process */ 970Sstevel@tonic-gate F_RUID, /* numeric real uid of the process */ 980Sstevel@tonic-gate F_GID, /* numeric effective gid of the process */ 990Sstevel@tonic-gate F_RGID, /* numeric real gid of the process */ 1000Sstevel@tonic-gate F_PID, /* process id */ 1010Sstevel@tonic-gate F_PPID, /* parent process id */ 1020Sstevel@tonic-gate F_PGID, /* process group id */ 1030Sstevel@tonic-gate F_SID, /* session id */ 1040Sstevel@tonic-gate F_PSR, /* bound processor */ 1050Sstevel@tonic-gate F_LWP, /* lwp-id */ 1060Sstevel@tonic-gate F_NLWP, /* number of lwps */ 1070Sstevel@tonic-gate F_OPRI, /* old priority (obsolete) */ 1080Sstevel@tonic-gate F_PRI, /* new priority */ 1090Sstevel@tonic-gate F_F, /* process flags */ 1100Sstevel@tonic-gate F_S, /* letter indicating the state */ 1110Sstevel@tonic-gate F_C, /* processor utilization (obsolete) */ 1120Sstevel@tonic-gate F_PCPU, /* percent of recently used cpu time */ 1130Sstevel@tonic-gate F_PMEM, /* percent of physical memory used (rss) */ 1140Sstevel@tonic-gate F_OSZ, /* virtual size of the process in pages */ 1150Sstevel@tonic-gate F_VSZ, /* virtual size of the process in kilobytes */ 1160Sstevel@tonic-gate F_RSS, /* resident set size of the process in kilobytes */ 1170Sstevel@tonic-gate F_NICE, /* "nice" value of the process */ 1180Sstevel@tonic-gate F_CLASS, /* scheduler class */ 1190Sstevel@tonic-gate F_STIME, /* start time of the process, hh:mm:ss or Month Day */ 1200Sstevel@tonic-gate F_ETIME, /* elapsed time of the process, [[dd-]hh:]mm:ss */ 1210Sstevel@tonic-gate F_TIME, /* cpu time of the process, [[dd-]hh:]mm:ss */ 1220Sstevel@tonic-gate F_TTY, /* name of the controlling terminal */ 1230Sstevel@tonic-gate F_ADDR, /* address of the process (obsolete) */ 1240Sstevel@tonic-gate F_WCHAN, /* wait channel (sleep condition variable) */ 1250Sstevel@tonic-gate F_FNAME, /* file name of command */ 1260Sstevel@tonic-gate F_COMM, /* name of command (argv[0] value) */ 1270Sstevel@tonic-gate F_ARGS, /* name of command plus all its arguments */ 1280Sstevel@tonic-gate F_TASKID, /* task id */ 1290Sstevel@tonic-gate F_PROJID, /* project id */ 1300Sstevel@tonic-gate F_PROJECT, /* project name of the process */ 1310Sstevel@tonic-gate F_PSET, /* bound processor set */ 1320Sstevel@tonic-gate F_ZONE, /* zone name */ 1330Sstevel@tonic-gate F_ZONEID, /* zone id */ 134*2685Sakolb F_CTID, /* process contract id */ 135*2685Sakolb F_LGRP /* process home lgroup */ 1360Sstevel@tonic-gate }; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate struct field { 1390Sstevel@tonic-gate struct field *next; /* linked list */ 1400Sstevel@tonic-gate int fname; /* field index */ 1410Sstevel@tonic-gate const char *header; /* header to use */ 1420Sstevel@tonic-gate int width; /* width of field */ 1430Sstevel@tonic-gate }; 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate static struct field *fields = NULL; /* fields selected via -o */ 1460Sstevel@tonic-gate static struct field *last_field = NULL; 1470Sstevel@tonic-gate static int do_header = 0; 1480Sstevel@tonic-gate static struct timeval now; 1490Sstevel@tonic-gate 1500Sstevel@tonic-gate /* array of defined fields, in fname order */ 1510Sstevel@tonic-gate struct def_field { 1520Sstevel@tonic-gate const char *fname; 1530Sstevel@tonic-gate const char *header; 1540Sstevel@tonic-gate int width; 1550Sstevel@tonic-gate int minwidth; 1560Sstevel@tonic-gate }; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate static struct def_field fname[] = { 1590Sstevel@tonic-gate /* fname header width minwidth */ 1600Sstevel@tonic-gate { "user", "USER", 8, 8 }, 1610Sstevel@tonic-gate { "ruser", "RUSER", 8, 8 }, 1620Sstevel@tonic-gate { "group", "GROUP", 8, 8 }, 1630Sstevel@tonic-gate { "rgroup", "RGROUP", 8, 8 }, 1640Sstevel@tonic-gate { "uid", "UID", 5, 5 }, 1650Sstevel@tonic-gate { "ruid", "RUID", 5, 5 }, 1660Sstevel@tonic-gate { "gid", "GID", 5, 5 }, 1670Sstevel@tonic-gate { "rgid", "RGID", 5, 5 }, 1680Sstevel@tonic-gate { "pid", "PID", 5, 5 }, 1690Sstevel@tonic-gate { "ppid", "PPID", 5, 5 }, 1700Sstevel@tonic-gate { "pgid", "PGID", 5, 5 }, 1710Sstevel@tonic-gate { "sid", "SID", 5, 5 }, 1720Sstevel@tonic-gate { "psr", "PSR", 3, 2 }, 1730Sstevel@tonic-gate { "lwp", "LWP", 6, 2 }, 1740Sstevel@tonic-gate { "nlwp", "NLWP", 4, 2 }, 1750Sstevel@tonic-gate { "opri", "PRI", 3, 2 }, 1760Sstevel@tonic-gate { "pri", "PRI", 3, 2 }, 1770Sstevel@tonic-gate { "f", "F", 2, 2 }, 1780Sstevel@tonic-gate { "s", "S", 1, 1 }, 1790Sstevel@tonic-gate { "c", "C", 2, 2 }, 1800Sstevel@tonic-gate { "pcpu", "%CPU", 4, 4 }, 1810Sstevel@tonic-gate { "pmem", "%MEM", 4, 4 }, 1820Sstevel@tonic-gate { "osz", "SZ", 4, 4 }, 1830Sstevel@tonic-gate { "vsz", "VSZ", 4, 4 }, 1840Sstevel@tonic-gate { "rss", "RSS", 4, 4 }, 1850Sstevel@tonic-gate { "nice", "NI", 2, 2 }, 1860Sstevel@tonic-gate { "class", "CLS", 4, 2 }, 1870Sstevel@tonic-gate { "stime", "STIME", 8, 8 }, 1880Sstevel@tonic-gate { "etime", "ELAPSED", 11, 7 }, 1890Sstevel@tonic-gate { "time", "TIME", 11, 5 }, 1900Sstevel@tonic-gate { "tty", "TT", 7, 7 }, 1910Sstevel@tonic-gate #ifdef _LP64 1920Sstevel@tonic-gate { "addr", "ADDR", 16, 8 }, 1930Sstevel@tonic-gate { "wchan", "WCHAN", 16, 8 }, 1940Sstevel@tonic-gate #else 1950Sstevel@tonic-gate { "addr", "ADDR", 8, 8 }, 1960Sstevel@tonic-gate { "wchan", "WCHAN", 8, 8 }, 1970Sstevel@tonic-gate #endif 1980Sstevel@tonic-gate { "fname", "COMMAND", 8, 8 }, 1990Sstevel@tonic-gate { "comm", "COMMAND", 80, 8 }, 2000Sstevel@tonic-gate { "args", "COMMAND", 80, 80 }, 2010Sstevel@tonic-gate { "taskid", "TASKID", 5, 5 }, 2020Sstevel@tonic-gate { "projid", "PROJID", 5, 5 }, 2030Sstevel@tonic-gate { "project", "PROJECT", 8, 8 }, 2040Sstevel@tonic-gate { "pset", "PSET", 3, 3 }, 2050Sstevel@tonic-gate { "zone", "ZONE", 8, 8 }, 2060Sstevel@tonic-gate { "zoneid", "ZONEID", 5, 5 }, 2070Sstevel@tonic-gate { "ctid", "CTID", 5, 5 }, 208*2685Sakolb { "lgrp", "LGRP", 4, 2 }, 2090Sstevel@tonic-gate }; 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate #define NFIELDS (sizeof (fname) / sizeof (fname[0])) 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate static int retcode = 1; 2140Sstevel@tonic-gate static int lflg; 2150Sstevel@tonic-gate static int Aflg; 2160Sstevel@tonic-gate static int uflg; 2170Sstevel@tonic-gate static int Uflg; 2180Sstevel@tonic-gate static int Gflg; 2190Sstevel@tonic-gate static int aflg; 2200Sstevel@tonic-gate static int dflg; 2210Sstevel@tonic-gate static int Lflg; 2220Sstevel@tonic-gate static int Pflg; 2230Sstevel@tonic-gate static int yflg; 2240Sstevel@tonic-gate static int pflg; 2250Sstevel@tonic-gate static int fflg; 2260Sstevel@tonic-gate static int cflg; 2270Sstevel@tonic-gate static int jflg; 2280Sstevel@tonic-gate static int gflg; 2290Sstevel@tonic-gate static int sflg; 2300Sstevel@tonic-gate static int tflg; 2310Sstevel@tonic-gate static int zflg; 2320Sstevel@tonic-gate static int Zflg; 233*2685Sakolb static int hflg; 234*2685Sakolb static int Hflg; 2350Sstevel@tonic-gate static uid_t tuid = -1; 2360Sstevel@tonic-gate static int errflg; 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate static int ndev; /* number of devices */ 2390Sstevel@tonic-gate static int maxdev; /* number of devl structures allocated */ 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate #define DNINCR 100 2420Sstevel@tonic-gate #define DNSIZE 14 2430Sstevel@tonic-gate static struct devl { /* device list */ 2440Sstevel@tonic-gate char dname[DNSIZE]; /* device name */ 2450Sstevel@tonic-gate dev_t ddev; /* device number */ 2460Sstevel@tonic-gate } *devl; 2470Sstevel@tonic-gate 2480Sstevel@tonic-gate static struct tty { 2490Sstevel@tonic-gate char *tname; 2500Sstevel@tonic-gate dev_t tdev; 2510Sstevel@tonic-gate } *tty = NULL; /* for t option */ 2520Sstevel@tonic-gate static size_t ttysz = 0; 2530Sstevel@tonic-gate static int ntty = 0; 2540Sstevel@tonic-gate 2550Sstevel@tonic-gate static pid_t *pid = NULL; /* for p option */ 2560Sstevel@tonic-gate static size_t pidsz = 0; 2570Sstevel@tonic-gate static size_t npid = 0; 2580Sstevel@tonic-gate 259*2685Sakolb static int *lgrps = NULL; /* list of lgroup IDs for for h option */ 260*2685Sakolb static size_t lgrps_size = 0; /* size of the lgrps list */ 261*2685Sakolb static size_t nlgrps = 0; /* number elements in the list */ 262*2685Sakolb 263*2685Sakolb /* Maximum possible lgroup ID value */ 264*2685Sakolb #define MAX_LGRP_ID 256 265*2685Sakolb 2660Sstevel@tonic-gate static pid_t *grpid = NULL; /* for g option */ 2670Sstevel@tonic-gate static size_t grpidsz = 0; 2680Sstevel@tonic-gate static int ngrpid = 0; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate static pid_t *sessid = NULL; /* for s option */ 2710Sstevel@tonic-gate static size_t sessidsz = 0; 2720Sstevel@tonic-gate static int nsessid = 0; 2730Sstevel@tonic-gate 2740Sstevel@tonic-gate static zoneid_t *zoneid = NULL; /* for z option */ 2750Sstevel@tonic-gate static size_t zoneidsz = 0; 2760Sstevel@tonic-gate static int nzoneid = 0; 2770Sstevel@tonic-gate 2780Sstevel@tonic-gate static int kbytes_per_page; 2790Sstevel@tonic-gate static int pidwidth; 2800Sstevel@tonic-gate 2810Sstevel@tonic-gate static char *procdir = "/proc"; /* standard /proc directory */ 2820Sstevel@tonic-gate 2830Sstevel@tonic-gate static struct ughead euid_tbl; /* table to store selected euid's */ 2840Sstevel@tonic-gate static struct ughead ruid_tbl; /* table to store selected real uid's */ 2850Sstevel@tonic-gate static struct ughead egid_tbl; /* table to store selected egid's */ 2860Sstevel@tonic-gate static struct ughead rgid_tbl; /* table to store selected real gid's */ 2870Sstevel@tonic-gate static prheader_t *lpsinfobuf; /* buffer to contain lpsinfo */ 2880Sstevel@tonic-gate static size_t lpbufsize; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate /* 2910Sstevel@tonic-gate * This constant defines the sentinal number of process IDs below which we 2920Sstevel@tonic-gate * only examine individual entries in /proc rather than scanning through 2930Sstevel@tonic-gate * /proc. This optimization is a huge win in the common case. 2940Sstevel@tonic-gate */ 2950Sstevel@tonic-gate #define PTHRESHOLD 40 2960Sstevel@tonic-gate 2970Sstevel@tonic-gate static void usage(void); 2980Sstevel@tonic-gate static char *getarg(char **); 2990Sstevel@tonic-gate static char *parse_format(char *); 3000Sstevel@tonic-gate static char *gettty(psinfo_t *); 3010Sstevel@tonic-gate static int prfind(int, psinfo_t *, char **); 3020Sstevel@tonic-gate static void prcom(psinfo_t *, char *); 3030Sstevel@tonic-gate static void prtpct(ushort_t, int); 3040Sstevel@tonic-gate static void print_time(time_t, int); 3050Sstevel@tonic-gate static void print_field(psinfo_t *, struct field *, const char *); 3060Sstevel@tonic-gate static void print_zombie_field(psinfo_t *, struct field *, const char *); 3070Sstevel@tonic-gate static void pr_fields(psinfo_t *, const char *, 3080Sstevel@tonic-gate void (*print_fld)(psinfo_t *, struct field *, const char *)); 3090Sstevel@tonic-gate static int search(pid_t *, int, pid_t); 3100Sstevel@tonic-gate static void add_ugentry(struct ughead *, char *); 3110Sstevel@tonic-gate static int uconv(struct ughead *); 3120Sstevel@tonic-gate static int gconv(struct ughead *); 3130Sstevel@tonic-gate static int ugfind(uid_t, struct ughead *); 3140Sstevel@tonic-gate static void prtime(timestruc_t, int, int); 3150Sstevel@tonic-gate static void przom(psinfo_t *); 3160Sstevel@tonic-gate static int namencnt(char *, int, int); 3170Sstevel@tonic-gate static char *err_string(int); 3180Sstevel@tonic-gate static int print_proc(char *pname); 3190Sstevel@tonic-gate static time_t delta_secs(const timestruc_t *); 3200Sstevel@tonic-gate static int str2id(const char *, pid_t *, long, long); 3210Sstevel@tonic-gate static void *Realloc(void *, size_t); 3220Sstevel@tonic-gate static int pidcmp(const void *p1, const void *p2); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate int 3250Sstevel@tonic-gate main(int argc, char **argv) 3260Sstevel@tonic-gate { 3270Sstevel@tonic-gate char *p; 3280Sstevel@tonic-gate char *p1; 3290Sstevel@tonic-gate char *parg; 3300Sstevel@tonic-gate int c; 3310Sstevel@tonic-gate int i; 3320Sstevel@tonic-gate int pgerrflg = 0; /* err flg: non-numeric arg w/p & g options */ 3330Sstevel@tonic-gate size_t size; 3340Sstevel@tonic-gate DIR *dirp; 3350Sstevel@tonic-gate struct dirent *dentp; 3360Sstevel@tonic-gate pid_t maxpid; 3370Sstevel@tonic-gate pid_t id; 3380Sstevel@tonic-gate int ret; 3390Sstevel@tonic-gate 3400Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 3410Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 3420Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 3430Sstevel@tonic-gate #endif 3440Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 3450Sstevel@tonic-gate 3460Sstevel@tonic-gate (void) memset(&euid_tbl, 0, sizeof (euid_tbl)); 3470Sstevel@tonic-gate (void) memset(&ruid_tbl, 0, sizeof (ruid_tbl)); 3480Sstevel@tonic-gate (void) memset(&egid_tbl, 0, sizeof (egid_tbl)); 3490Sstevel@tonic-gate (void) memset(&rgid_tbl, 0, sizeof (rgid_tbl)); 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate kbytes_per_page = sysconf(_SC_PAGESIZE) / 1024; 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate (void) gettimeofday(&now, NULL); 3540Sstevel@tonic-gate 3550Sstevel@tonic-gate /* 3560Sstevel@tonic-gate * calculate width of pid fields based on configured MAXPID 3570Sstevel@tonic-gate * (must be at least 5 to retain output format compatibility) 3580Sstevel@tonic-gate */ 3590Sstevel@tonic-gate id = maxpid = (pid_t)sysconf(_SC_MAXPID); 3600Sstevel@tonic-gate pidwidth = 1; 3610Sstevel@tonic-gate while ((id /= 10) > 0) 3620Sstevel@tonic-gate ++pidwidth; 3630Sstevel@tonic-gate pidwidth = pidwidth < 5 ? 5 : pidwidth; 3640Sstevel@tonic-gate 3650Sstevel@tonic-gate fname[F_PID].width = fname[F_PPID].width = pidwidth; 3660Sstevel@tonic-gate fname[F_PGID].width = fname[F_SID].width = pidwidth; 3670Sstevel@tonic-gate 368*2685Sakolb while ((c = getopt(argc, argv, "jlfceAadLPyZHh:t:p:g:u:U:G:n:s:o:z:")) 369*2685Sakolb != EOF) 3700Sstevel@tonic-gate switch (c) { 371*2685Sakolb case 'H': /* Show home lgroups */ 372*2685Sakolb Hflg++; 373*2685Sakolb break; 374*2685Sakolb case 'h': 375*2685Sakolb /* 376*2685Sakolb * Show processes/threads with given home lgroups 377*2685Sakolb */ 378*2685Sakolb hflg++; 379*2685Sakolb p1 = optarg; 380*2685Sakolb do { 381*2685Sakolb int id; 382*2685Sakolb 383*2685Sakolb /* 384*2685Sakolb * Get all IDs in the list, verify for 385*2685Sakolb * correctness and place in lgrps array. 386*2685Sakolb */ 387*2685Sakolb parg = getarg(&p1); 388*2685Sakolb /* Convert string to integer */ 389*2685Sakolb ret = str2id(parg, (pid_t *)&id, 0, 390*2685Sakolb MAX_LGRP_ID); 391*2685Sakolb /* Complain if ID didn't parse correctly */ 392*2685Sakolb if (ret != 0) { 393*2685Sakolb pgerrflg++; 394*2685Sakolb (void) fprintf(stderr, 395*2685Sakolb gettext("ps: %s "), parg); 396*2685Sakolb if (ret == EINVAL) 397*2685Sakolb (void) fprintf(stderr, 398*2685Sakolb gettext("is an invalid " 399*2685Sakolb "non-numeric argument")); 400*2685Sakolb else 401*2685Sakolb (void) fprintf(stderr, 402*2685Sakolb gettext("exceeds valid " 403*2685Sakolb "range")); 404*2685Sakolb (void) fprintf(stderr, 405*2685Sakolb gettext(" for -h option\n")); 406*2685Sakolb continue; 407*2685Sakolb } 408*2685Sakolb 409*2685Sakolb /* Extend lgrps array if needed */ 410*2685Sakolb if (nlgrps == lgrps_size) { 411*2685Sakolb /* Double the size of the lgrps array */ 412*2685Sakolb if (lgrps_size == 0) 413*2685Sakolb lgrps_size = SIZ; 414*2685Sakolb lgrps_size *= 2; 415*2685Sakolb lgrps = Realloc(lgrps, 416*2685Sakolb lgrps_size * sizeof (int)); 417*2685Sakolb } 418*2685Sakolb /* place the id in the lgrps table */ 419*2685Sakolb lgrps[nlgrps++] = id; 420*2685Sakolb } while (*p1); 421*2685Sakolb break; 4220Sstevel@tonic-gate case 'l': /* long listing */ 4230Sstevel@tonic-gate lflg++; 4240Sstevel@tonic-gate break; 4250Sstevel@tonic-gate case 'f': /* full listing */ 4260Sstevel@tonic-gate fflg++; 4270Sstevel@tonic-gate break; 4280Sstevel@tonic-gate case 'j': 4290Sstevel@tonic-gate jflg++; 4300Sstevel@tonic-gate break; 4310Sstevel@tonic-gate case 'c': 4320Sstevel@tonic-gate /* 4330Sstevel@tonic-gate * Format output to reflect scheduler changes: 4340Sstevel@tonic-gate * high numbers for high priorities and don't 4350Sstevel@tonic-gate * print nice or p_cpu values. 'c' option only 4360Sstevel@tonic-gate * effective when used with 'l' or 'f' options. 4370Sstevel@tonic-gate */ 4380Sstevel@tonic-gate cflg++; 4390Sstevel@tonic-gate break; 4400Sstevel@tonic-gate case 'A': /* list every process */ 4410Sstevel@tonic-gate case 'e': /* (obsolete) list every process */ 4420Sstevel@tonic-gate Aflg++; 4430Sstevel@tonic-gate tflg = Gflg = Uflg = uflg = pflg = gflg = sflg = 0; 444*2685Sakolb zflg = hflg = 0; 4450Sstevel@tonic-gate break; 4460Sstevel@tonic-gate case 'a': 4470Sstevel@tonic-gate /* 4480Sstevel@tonic-gate * Same as 'e' except no session group leaders 4490Sstevel@tonic-gate * and no non-terminal processes. 4500Sstevel@tonic-gate */ 4510Sstevel@tonic-gate aflg++; 4520Sstevel@tonic-gate break; 4530Sstevel@tonic-gate case 'd': /* same as e except no session leaders */ 4540Sstevel@tonic-gate dflg++; 4550Sstevel@tonic-gate break; 4560Sstevel@tonic-gate case 'L': /* show lwps */ 4570Sstevel@tonic-gate Lflg++; 4580Sstevel@tonic-gate break; 4590Sstevel@tonic-gate case 'P': /* show bound processor */ 4600Sstevel@tonic-gate Pflg++; 4610Sstevel@tonic-gate break; 4620Sstevel@tonic-gate case 'y': /* omit F & ADDR, report RSS & SZ in Kby */ 4630Sstevel@tonic-gate yflg++; 4640Sstevel@tonic-gate break; 4650Sstevel@tonic-gate case 'n': /* no longer needed; retain as no-op */ 4660Sstevel@tonic-gate (void) fprintf(stderr, 4670Sstevel@tonic-gate gettext("ps: warning: -n option ignored\n")); 4680Sstevel@tonic-gate break; 4690Sstevel@tonic-gate case 't': /* terminals */ 4700Sstevel@tonic-gate #define TSZ 30 4710Sstevel@tonic-gate tflg++; 4720Sstevel@tonic-gate p1 = optarg; 4730Sstevel@tonic-gate do { 4740Sstevel@tonic-gate char nambuf[TSZ+6]; /* for "/dev/" + '\0' */ 4750Sstevel@tonic-gate struct stat64 s; 4760Sstevel@tonic-gate parg = getarg(&p1); 4770Sstevel@tonic-gate p = Realloc(NULL, TSZ+1); /* for '\0' */ 4780Sstevel@tonic-gate /* zero the buffer before using it */ 4790Sstevel@tonic-gate p[0] = '\0'; 4800Sstevel@tonic-gate size = TSZ; 4810Sstevel@tonic-gate if (isdigit(*parg)) { 4820Sstevel@tonic-gate (void) strcpy(p, "tty"); 4830Sstevel@tonic-gate size -= 3; 4840Sstevel@tonic-gate } 4850Sstevel@tonic-gate (void) strncat(p, parg, size); 4860Sstevel@tonic-gate if (ntty == ttysz) { 4870Sstevel@tonic-gate if ((ttysz *= 2) == 0) 4880Sstevel@tonic-gate ttysz = NTTYS; 4890Sstevel@tonic-gate tty = Realloc(tty, 4900Sstevel@tonic-gate (ttysz + 1) * sizeof (struct tty)); 4910Sstevel@tonic-gate } 4920Sstevel@tonic-gate tty[ntty].tdev = PRNODEV; 4930Sstevel@tonic-gate (void) strcpy(nambuf, "/dev/"); 4940Sstevel@tonic-gate (void) strcat(nambuf, p); 4950Sstevel@tonic-gate if (stat64(nambuf, &s) == 0) 4960Sstevel@tonic-gate tty[ntty].tdev = s.st_rdev; 4970Sstevel@tonic-gate tty[ntty++].tname = p; 4980Sstevel@tonic-gate } while (*p1); 4990Sstevel@tonic-gate break; 5000Sstevel@tonic-gate case 'p': /* proc ids */ 5010Sstevel@tonic-gate pflg++; 5020Sstevel@tonic-gate p1 = optarg; 5030Sstevel@tonic-gate do { 5040Sstevel@tonic-gate pid_t id; 5050Sstevel@tonic-gate 5060Sstevel@tonic-gate parg = getarg(&p1); 5070Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) { 5080Sstevel@tonic-gate pgerrflg++; 5090Sstevel@tonic-gate (void) fprintf(stderr, 5100Sstevel@tonic-gate gettext("ps: %s "), parg); 5110Sstevel@tonic-gate if (ret == EINVAL) 5120Sstevel@tonic-gate (void) fprintf(stderr, 5130Sstevel@tonic-gate gettext("is an invalid " 5140Sstevel@tonic-gate "non-numeric argument")); 5150Sstevel@tonic-gate else 5160Sstevel@tonic-gate (void) fprintf(stderr, 5170Sstevel@tonic-gate gettext("exceeds valid " 5180Sstevel@tonic-gate "range")); 5190Sstevel@tonic-gate (void) fprintf(stderr, 5200Sstevel@tonic-gate gettext(" for -p option\n")); 5210Sstevel@tonic-gate continue; 5220Sstevel@tonic-gate } 5230Sstevel@tonic-gate 5240Sstevel@tonic-gate if (npid == pidsz) { 5250Sstevel@tonic-gate if ((pidsz *= 2) == 0) 5260Sstevel@tonic-gate pidsz = SIZ; 5270Sstevel@tonic-gate pid = Realloc(pid, 5280Sstevel@tonic-gate pidsz * sizeof (pid_t)); 5290Sstevel@tonic-gate } 5300Sstevel@tonic-gate pid[npid++] = id; 5310Sstevel@tonic-gate } while (*p1); 5320Sstevel@tonic-gate break; 5330Sstevel@tonic-gate case 's': /* session */ 5340Sstevel@tonic-gate sflg++; 5350Sstevel@tonic-gate p1 = optarg; 5360Sstevel@tonic-gate do { 5370Sstevel@tonic-gate pid_t id; 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate parg = getarg(&p1); 5400Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) { 5410Sstevel@tonic-gate pgerrflg++; 5420Sstevel@tonic-gate (void) fprintf(stderr, 5430Sstevel@tonic-gate gettext("ps: %s "), parg); 5440Sstevel@tonic-gate if (ret == EINVAL) 5450Sstevel@tonic-gate (void) fprintf(stderr, 5460Sstevel@tonic-gate gettext("is an invalid " 5470Sstevel@tonic-gate "non-numeric argument")); 5480Sstevel@tonic-gate else 5490Sstevel@tonic-gate (void) fprintf(stderr, 5500Sstevel@tonic-gate gettext("exceeds valid " 5510Sstevel@tonic-gate "range")); 5520Sstevel@tonic-gate (void) fprintf(stderr, 5530Sstevel@tonic-gate gettext(" for -s option\n")); 5540Sstevel@tonic-gate continue; 5550Sstevel@tonic-gate } 5560Sstevel@tonic-gate 5570Sstevel@tonic-gate if (nsessid == sessidsz) { 5580Sstevel@tonic-gate if ((sessidsz *= 2) == 0) 5590Sstevel@tonic-gate sessidsz = SIZ; 5600Sstevel@tonic-gate sessid = Realloc(sessid, 5610Sstevel@tonic-gate sessidsz * sizeof (pid_t)); 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate sessid[nsessid++] = id; 5640Sstevel@tonic-gate } while (*p1); 5650Sstevel@tonic-gate break; 5660Sstevel@tonic-gate case 'g': /* proc group */ 5670Sstevel@tonic-gate gflg++; 5680Sstevel@tonic-gate p1 = optarg; 5690Sstevel@tonic-gate do { 5700Sstevel@tonic-gate pid_t id; 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate parg = getarg(&p1); 5730Sstevel@tonic-gate if ((ret = str2id(parg, &id, 0, maxpid)) != 0) { 5740Sstevel@tonic-gate pgerrflg++; 5750Sstevel@tonic-gate (void) fprintf(stderr, 5760Sstevel@tonic-gate gettext("ps: %s "), parg); 5770Sstevel@tonic-gate if (ret == EINVAL) 5780Sstevel@tonic-gate (void) fprintf(stderr, 5790Sstevel@tonic-gate gettext("is an invalid " 5800Sstevel@tonic-gate "non-numeric argument")); 5810Sstevel@tonic-gate else 5820Sstevel@tonic-gate (void) fprintf(stderr, 5830Sstevel@tonic-gate gettext("exceeds valid " 5840Sstevel@tonic-gate "range")); 5850Sstevel@tonic-gate (void) fprintf(stderr, 5860Sstevel@tonic-gate gettext(" for -g option\n")); 5870Sstevel@tonic-gate continue; 5880Sstevel@tonic-gate } 5890Sstevel@tonic-gate 5900Sstevel@tonic-gate if (ngrpid == grpidsz) { 5910Sstevel@tonic-gate if ((grpidsz *= 2) == 0) 5920Sstevel@tonic-gate grpidsz = SIZ; 5930Sstevel@tonic-gate grpid = Realloc(grpid, 5940Sstevel@tonic-gate grpidsz * sizeof (pid_t)); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate grpid[ngrpid++] = id; 5970Sstevel@tonic-gate } while (*p1); 5980Sstevel@tonic-gate break; 5990Sstevel@tonic-gate case 'u': /* effective user name or number */ 6000Sstevel@tonic-gate uflg++; 6010Sstevel@tonic-gate p1 = optarg; 6020Sstevel@tonic-gate do { 6030Sstevel@tonic-gate parg = getarg(&p1); 6040Sstevel@tonic-gate add_ugentry(&euid_tbl, parg); 6050Sstevel@tonic-gate } while (*p1); 6060Sstevel@tonic-gate break; 6070Sstevel@tonic-gate case 'U': /* real user name or number */ 6080Sstevel@tonic-gate Uflg++; 6090Sstevel@tonic-gate p1 = optarg; 6100Sstevel@tonic-gate do { 6110Sstevel@tonic-gate parg = getarg(&p1); 6120Sstevel@tonic-gate add_ugentry(&ruid_tbl, parg); 6130Sstevel@tonic-gate } while (*p1); 6140Sstevel@tonic-gate break; 6150Sstevel@tonic-gate case 'G': /* real group name or number */ 6160Sstevel@tonic-gate Gflg++; 6170Sstevel@tonic-gate p1 = optarg; 6180Sstevel@tonic-gate do { 6190Sstevel@tonic-gate parg = getarg(&p1); 6200Sstevel@tonic-gate add_ugentry(&rgid_tbl, parg); 6210Sstevel@tonic-gate } while (*p1); 6220Sstevel@tonic-gate break; 6230Sstevel@tonic-gate case 'o': /* output format */ 6240Sstevel@tonic-gate p = optarg; 6250Sstevel@tonic-gate while ((p = parse_format(p)) != NULL) 6260Sstevel@tonic-gate ; 6270Sstevel@tonic-gate break; 6280Sstevel@tonic-gate case 'z': /* zone name or number */ 6290Sstevel@tonic-gate zflg++; 6300Sstevel@tonic-gate p1 = optarg; 6310Sstevel@tonic-gate do { 6320Sstevel@tonic-gate zoneid_t id; 6330Sstevel@tonic-gate 6340Sstevel@tonic-gate parg = getarg(&p1); 6350Sstevel@tonic-gate if (zone_get_id(parg, &id) != 0) { 6360Sstevel@tonic-gate pgerrflg++; 6370Sstevel@tonic-gate (void) fprintf(stderr, 6380Sstevel@tonic-gate gettext("ps: unknown zone %s\n"), 6390Sstevel@tonic-gate parg); 6400Sstevel@tonic-gate continue; 6410Sstevel@tonic-gate } 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate if (nzoneid == zoneidsz) { 6440Sstevel@tonic-gate if ((zoneidsz *= 2) == 0) 6450Sstevel@tonic-gate zoneidsz = SIZ; 6460Sstevel@tonic-gate zoneid = Realloc(zoneid, 6470Sstevel@tonic-gate zoneidsz * sizeof (zoneid_t)); 6480Sstevel@tonic-gate } 6490Sstevel@tonic-gate zoneid[nzoneid++] = id; 6500Sstevel@tonic-gate } while (*p1); 6510Sstevel@tonic-gate break; 6520Sstevel@tonic-gate case 'Z': /* show zone name */ 6530Sstevel@tonic-gate Zflg++; 6540Sstevel@tonic-gate break; 6550Sstevel@tonic-gate default: /* error on ? */ 6560Sstevel@tonic-gate errflg++; 6570Sstevel@tonic-gate break; 6580Sstevel@tonic-gate } 6590Sstevel@tonic-gate 6600Sstevel@tonic-gate if (errflg || optind < argc || pgerrflg) 6610Sstevel@tonic-gate usage(); 6620Sstevel@tonic-gate 6630Sstevel@tonic-gate if (tflg) 6640Sstevel@tonic-gate tty[ntty].tname = NULL; 6650Sstevel@tonic-gate /* 6660Sstevel@tonic-gate * If an appropriate option has not been specified, use the 6670Sstevel@tonic-gate * current terminal and effective uid as the default. 6680Sstevel@tonic-gate */ 669*2685Sakolb if (!(aflg|Aflg|dflg|Gflg|hflg|Uflg|uflg|tflg|pflg|gflg|sflg|zflg)) { 6700Sstevel@tonic-gate psinfo_t info; 6710Sstevel@tonic-gate int procfd; 6720Sstevel@tonic-gate char *name; 6730Sstevel@tonic-gate char pname[100]; 6740Sstevel@tonic-gate 6750Sstevel@tonic-gate /* get our own controlling tty name using /proc */ 6760Sstevel@tonic-gate (void) snprintf(pname, sizeof (pname), 6770Sstevel@tonic-gate "%s/self/psinfo", procdir); 6780Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) < 0 || 6790Sstevel@tonic-gate read(procfd, (char *)&info, sizeof (info)) < 0 || 6800Sstevel@tonic-gate info.pr_ttydev == PRNODEV) { 6810Sstevel@tonic-gate (void) fprintf(stderr, 6820Sstevel@tonic-gate gettext("ps: no controlling terminal\n")); 6830Sstevel@tonic-gate exit(1); 6840Sstevel@tonic-gate } 6850Sstevel@tonic-gate (void) close(procfd); 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate i = 0; 6880Sstevel@tonic-gate name = gettty(&info); 6890Sstevel@tonic-gate if (*name == '?') { 6900Sstevel@tonic-gate (void) fprintf(stderr, 6910Sstevel@tonic-gate gettext("ps: can't find controlling terminal\n")); 6920Sstevel@tonic-gate exit(1); 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate if (ntty == ttysz) { 6950Sstevel@tonic-gate if ((ttysz *= 2) == 0) 6960Sstevel@tonic-gate ttysz = NTTYS; 6970Sstevel@tonic-gate tty = Realloc(tty, (ttysz + 1) * sizeof (struct tty)); 6980Sstevel@tonic-gate } 6990Sstevel@tonic-gate tty[ntty].tdev = info.pr_ttydev; 7000Sstevel@tonic-gate tty[ntty++].tname = name; 7010Sstevel@tonic-gate tty[ntty].tname = NULL; 7020Sstevel@tonic-gate tflg++; 7030Sstevel@tonic-gate tuid = getuid(); 7040Sstevel@tonic-gate } 7050Sstevel@tonic-gate if (Aflg) { 7060Sstevel@tonic-gate Gflg = Uflg = uflg = pflg = sflg = gflg = aflg = dflg = 0; 707*2685Sakolb zflg = hflg = 0; 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate if (Aflg | aflg | dflg) 7100Sstevel@tonic-gate tflg = 0; 7110Sstevel@tonic-gate 7120Sstevel@tonic-gate i = 0; /* prepare to exit on name lookup errors */ 7130Sstevel@tonic-gate i += uconv(&euid_tbl); 7140Sstevel@tonic-gate i += uconv(&ruid_tbl); 7150Sstevel@tonic-gate i += gconv(&egid_tbl); 7160Sstevel@tonic-gate i += gconv(&rgid_tbl); 7170Sstevel@tonic-gate if (i) 7180Sstevel@tonic-gate exit(1); 7190Sstevel@tonic-gate 7200Sstevel@tonic-gate /* allocate a buffer for lwpsinfo structures */ 7210Sstevel@tonic-gate lpbufsize = 4096; 7220Sstevel@tonic-gate if (Lflg && (lpsinfobuf = malloc(lpbufsize)) == NULL) { 7230Sstevel@tonic-gate (void) fprintf(stderr, 7240Sstevel@tonic-gate gettext("ps: no memory\n")); 7250Sstevel@tonic-gate exit(1); 7260Sstevel@tonic-gate } 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate if (fields) { /* print user-specified header */ 7290Sstevel@tonic-gate if (do_header) { 7300Sstevel@tonic-gate struct field *f; 7310Sstevel@tonic-gate 7320Sstevel@tonic-gate for (f = fields; f != NULL; f = f->next) { 7330Sstevel@tonic-gate if (f != fields) 7340Sstevel@tonic-gate (void) printf(" "); 7350Sstevel@tonic-gate switch (f->fname) { 7360Sstevel@tonic-gate case F_TTY: 7370Sstevel@tonic-gate (void) printf("%-*s", 7380Sstevel@tonic-gate f->width, f->header); 7390Sstevel@tonic-gate break; 7400Sstevel@tonic-gate case F_FNAME: 7410Sstevel@tonic-gate case F_COMM: 7420Sstevel@tonic-gate case F_ARGS: 7430Sstevel@tonic-gate /* 7440Sstevel@tonic-gate * Print these headers full width 7450Sstevel@tonic-gate * unless they appear at the end. 7460Sstevel@tonic-gate */ 7470Sstevel@tonic-gate if (f->next != NULL) { 7480Sstevel@tonic-gate (void) printf("%-*s", 7490Sstevel@tonic-gate f->width, f->header); 7500Sstevel@tonic-gate } else { 7510Sstevel@tonic-gate (void) printf("%s", 7520Sstevel@tonic-gate f->header); 7530Sstevel@tonic-gate } 7540Sstevel@tonic-gate break; 7550Sstevel@tonic-gate default: 7560Sstevel@tonic-gate (void) printf("%*s", 7570Sstevel@tonic-gate f->width, f->header); 7580Sstevel@tonic-gate break; 7590Sstevel@tonic-gate } 7600Sstevel@tonic-gate } 7610Sstevel@tonic-gate (void) printf("\n"); 7620Sstevel@tonic-gate } 7630Sstevel@tonic-gate } else { /* print standard header */ 7640Sstevel@tonic-gate if (lflg) { 7650Sstevel@tonic-gate if (yflg) 7660Sstevel@tonic-gate (void) printf(" S"); 7670Sstevel@tonic-gate else 7680Sstevel@tonic-gate (void) printf(" F S"); 7690Sstevel@tonic-gate } 7700Sstevel@tonic-gate if (Zflg) 7710Sstevel@tonic-gate (void) printf(" ZONE"); 7720Sstevel@tonic-gate if (fflg) { 7730Sstevel@tonic-gate if (lflg) 7740Sstevel@tonic-gate (void) printf(" "); 7750Sstevel@tonic-gate (void) printf(" UID"); 7760Sstevel@tonic-gate } else if (lflg) 7770Sstevel@tonic-gate (void) printf(" UID"); 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate (void) printf(" %*s", pidwidth, "PID"); 7800Sstevel@tonic-gate if (lflg || fflg) 7810Sstevel@tonic-gate (void) printf(" %*s", pidwidth, "PPID"); 7820Sstevel@tonic-gate if (jflg) 7830Sstevel@tonic-gate (void) printf(" %*s %*s", pidwidth, "PGID", 7840Sstevel@tonic-gate pidwidth, "SID"); 7850Sstevel@tonic-gate if (Lflg) 7860Sstevel@tonic-gate (void) printf(" LWP"); 7870Sstevel@tonic-gate if (Pflg) 7880Sstevel@tonic-gate (void) printf(" PSR"); 7890Sstevel@tonic-gate if (Lflg && fflg) 7900Sstevel@tonic-gate (void) printf(" NLWP"); 7910Sstevel@tonic-gate if (cflg) 7920Sstevel@tonic-gate (void) printf(" CLS PRI"); 7930Sstevel@tonic-gate else if (lflg || fflg) { 7940Sstevel@tonic-gate (void) printf(" C"); 7950Sstevel@tonic-gate if (lflg) 7960Sstevel@tonic-gate (void) printf(" PRI NI"); 7970Sstevel@tonic-gate } 7980Sstevel@tonic-gate if (lflg) { 7990Sstevel@tonic-gate if (yflg) 8000Sstevel@tonic-gate (void) printf(" RSS SZ WCHAN"); 8010Sstevel@tonic-gate else 8020Sstevel@tonic-gate (void) printf(" ADDR SZ WCHAN"); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate if (fflg) 8050Sstevel@tonic-gate (void) printf(" STIME"); 806*2685Sakolb if (Hflg) 807*2685Sakolb (void) printf(" LGRP"); 8080Sstevel@tonic-gate if (Lflg) 8090Sstevel@tonic-gate (void) printf(" TTY LTIME CMD\n"); 8100Sstevel@tonic-gate else 8110Sstevel@tonic-gate (void) printf(" TTY TIME CMD\n"); 8120Sstevel@tonic-gate } 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate 815*2685Sakolb if (pflg && !(aflg|Aflg|dflg|Gflg|Uflg|uflg|hflg|tflg|gflg|sflg|zflg) && 8160Sstevel@tonic-gate npid <= PTHRESHOLD) { 8170Sstevel@tonic-gate /* 8180Sstevel@tonic-gate * If we are looking at specific processes go straight 8190Sstevel@tonic-gate * to their /proc entries and don't scan /proc. 8200Sstevel@tonic-gate */ 8210Sstevel@tonic-gate int i; 8220Sstevel@tonic-gate 8230Sstevel@tonic-gate (void) qsort(pid, npid, sizeof (pid_t), pidcmp); 8240Sstevel@tonic-gate for (i = 0; i < npid; i++) { 8250Sstevel@tonic-gate char pname[12]; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate if (i >= 1 && pid[i] == pid[i - 1]) 8280Sstevel@tonic-gate continue; 8290Sstevel@tonic-gate (void) sprintf(pname, "%d", (int)pid[i]); 8300Sstevel@tonic-gate if (print_proc(pname) == 0) 8310Sstevel@tonic-gate retcode = 0; 8320Sstevel@tonic-gate } 8330Sstevel@tonic-gate } else { 8340Sstevel@tonic-gate /* 8350Sstevel@tonic-gate * Determine which processes to print info about by searching 8360Sstevel@tonic-gate * the /proc directory and looking at each process. 8370Sstevel@tonic-gate */ 8380Sstevel@tonic-gate if ((dirp = opendir(procdir)) == NULL) { 8390Sstevel@tonic-gate (void) fprintf(stderr, 8400Sstevel@tonic-gate gettext("ps: cannot open PROC directory %s\n"), 8410Sstevel@tonic-gate procdir); 8420Sstevel@tonic-gate exit(1); 8430Sstevel@tonic-gate } 8440Sstevel@tonic-gate 8450Sstevel@tonic-gate /* for each active process --- */ 8460Sstevel@tonic-gate while (dentp = readdir(dirp)) { 8470Sstevel@tonic-gate if (dentp->d_name[0] == '.') /* skip . and .. */ 8480Sstevel@tonic-gate continue; 8490Sstevel@tonic-gate if (print_proc(dentp->d_name) == 0) 8500Sstevel@tonic-gate retcode = 0; 8510Sstevel@tonic-gate } 8520Sstevel@tonic-gate 8530Sstevel@tonic-gate (void) closedir(dirp); 8540Sstevel@tonic-gate } 8550Sstevel@tonic-gate return (retcode); 8560Sstevel@tonic-gate } 8570Sstevel@tonic-gate 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate int 8600Sstevel@tonic-gate print_proc(char *pid_name) 8610Sstevel@tonic-gate { 8620Sstevel@tonic-gate char pname[PATH_MAX]; 8630Sstevel@tonic-gate int pdlen; 8640Sstevel@tonic-gate int found; 8650Sstevel@tonic-gate int procfd; /* filedescriptor for /proc/nnnnn/psinfo */ 8660Sstevel@tonic-gate char *tp; /* ptr to ttyname, if any */ 8670Sstevel@tonic-gate psinfo_t info; /* process information from /proc */ 8680Sstevel@tonic-gate lwpsinfo_t *lwpsinfo; /* array of lwpsinfo structs */ 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate pdlen = snprintf(pname, sizeof (pname), "%s/%s/", procdir, pid_name); 8710Sstevel@tonic-gate if (pdlen >= sizeof (pname) - 10) 8720Sstevel@tonic-gate return (1); 8730Sstevel@tonic-gate retry: 8740Sstevel@tonic-gate (void) strcpy(&pname[pdlen], "psinfo"); 8750Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) == -1) { 8760Sstevel@tonic-gate /* Process may have exited meanwhile. */ 8770Sstevel@tonic-gate return (1); 8780Sstevel@tonic-gate } 8790Sstevel@tonic-gate /* 8800Sstevel@tonic-gate * Get the info structure for the process and close quickly. 8810Sstevel@tonic-gate */ 8820Sstevel@tonic-gate if (read(procfd, (char *)&info, sizeof (info)) < 0) { 8830Sstevel@tonic-gate int saverr = errno; 8840Sstevel@tonic-gate 8850Sstevel@tonic-gate (void) close(procfd); 8860Sstevel@tonic-gate if (saverr == EAGAIN) 8870Sstevel@tonic-gate goto retry; 8880Sstevel@tonic-gate if (saverr != ENOENT) 8890Sstevel@tonic-gate (void) fprintf(stderr, 8900Sstevel@tonic-gate gettext("ps: read() on %s: %s\n"), 8910Sstevel@tonic-gate pname, err_string(saverr)); 8920Sstevel@tonic-gate return (1); 8930Sstevel@tonic-gate } 8940Sstevel@tonic-gate (void) close(procfd); 8950Sstevel@tonic-gate 8960Sstevel@tonic-gate found = 0; 8970Sstevel@tonic-gate if (info.pr_lwp.pr_state == 0) /* can't happen? */ 8980Sstevel@tonic-gate return (1); 8990Sstevel@tonic-gate 9000Sstevel@tonic-gate /* 9010Sstevel@tonic-gate * Omit session group leaders for 'a' and 'd' options. 9020Sstevel@tonic-gate */ 9030Sstevel@tonic-gate if ((info.pr_pid == info.pr_sid) && (dflg || aflg)) 9040Sstevel@tonic-gate return (1); 9050Sstevel@tonic-gate if (Aflg || dflg) 9060Sstevel@tonic-gate found++; 9070Sstevel@tonic-gate else if (pflg && search(pid, npid, info.pr_pid)) 9080Sstevel@tonic-gate found++; /* ppid in p option arg list */ 9090Sstevel@tonic-gate else if (uflg && ugfind(info.pr_euid, &euid_tbl)) 9100Sstevel@tonic-gate found++; /* puid in u option arg list */ 9110Sstevel@tonic-gate else if (Uflg && ugfind(info.pr_uid, &ruid_tbl)) 9120Sstevel@tonic-gate found++; /* puid in U option arg list */ 9130Sstevel@tonic-gate #ifdef NOT_YET 9140Sstevel@tonic-gate else if (gflg && ugfind(info.pr_egid, &egid_tbl)) 9150Sstevel@tonic-gate found++; /* pgid in g option arg list */ 9160Sstevel@tonic-gate #endif /* NOT_YET */ 9170Sstevel@tonic-gate else if (Gflg && ugfind(info.pr_gid, &rgid_tbl)) 9180Sstevel@tonic-gate found++; /* pgid in G option arg list */ 9190Sstevel@tonic-gate else if (gflg && search(grpid, ngrpid, info.pr_pgid)) 9200Sstevel@tonic-gate found++; /* grpid in g option arg list */ 9210Sstevel@tonic-gate else if (sflg && search(sessid, nsessid, info.pr_sid)) 9220Sstevel@tonic-gate found++; /* sessid in s option arg list */ 9230Sstevel@tonic-gate else if (zflg && search(zoneid, nzoneid, info.pr_zoneid)) 9240Sstevel@tonic-gate found++; /* zoneid in z option arg list */ 925*2685Sakolb else if (hflg && search((pid_t *)lgrps, nlgrps, info.pr_lwp.pr_lgrp)) 926*2685Sakolb found++; /* home lgroup in h option arg list */ 9270Sstevel@tonic-gate if (!found && !tflg && !aflg) 9280Sstevel@tonic-gate return (1); 9290Sstevel@tonic-gate if (!prfind(found, &info, &tp)) 9300Sstevel@tonic-gate return (1); 9310Sstevel@tonic-gate if (Lflg && (info.pr_nlwp + info.pr_nzomb) > 1) { 9320Sstevel@tonic-gate ssize_t prsz; 9330Sstevel@tonic-gate 9340Sstevel@tonic-gate (void) strcpy(&pname[pdlen], "lpsinfo"); 9350Sstevel@tonic-gate if ((procfd = open(pname, O_RDONLY)) == -1) 9360Sstevel@tonic-gate return (1); 9370Sstevel@tonic-gate /* 9380Sstevel@tonic-gate * Get the info structures for the lwps. 9390Sstevel@tonic-gate */ 9400Sstevel@tonic-gate prsz = read(procfd, lpsinfobuf, lpbufsize); 9410Sstevel@tonic-gate if (prsz == -1) { 9420Sstevel@tonic-gate int saverr = errno; 9430Sstevel@tonic-gate 9440Sstevel@tonic-gate (void) close(procfd); 9450Sstevel@tonic-gate if (saverr == EAGAIN) 9460Sstevel@tonic-gate goto retry; 9470Sstevel@tonic-gate if (saverr != ENOENT) 9480Sstevel@tonic-gate (void) fprintf(stderr, 9490Sstevel@tonic-gate gettext("ps: read() on %s: %s\n"), 9500Sstevel@tonic-gate pname, err_string(saverr)); 9510Sstevel@tonic-gate return (1); 9520Sstevel@tonic-gate } 9530Sstevel@tonic-gate (void) close(procfd); 9540Sstevel@tonic-gate if (prsz == lpbufsize) { 9550Sstevel@tonic-gate /* 9560Sstevel@tonic-gate * buffer overflow. Realloc new buffer. 9570Sstevel@tonic-gate * Error handling is done in Realloc(). 9580Sstevel@tonic-gate */ 9590Sstevel@tonic-gate lpbufsize *= 2; 9600Sstevel@tonic-gate lpsinfobuf = Realloc(lpsinfobuf, lpbufsize); 9610Sstevel@tonic-gate goto retry; 9620Sstevel@tonic-gate } 9630Sstevel@tonic-gate if (lpsinfobuf->pr_nent != (info.pr_nlwp + info.pr_nzomb)) 9640Sstevel@tonic-gate goto retry; 9650Sstevel@tonic-gate lwpsinfo = (lwpsinfo_t *)(lpsinfobuf + 1); 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate if (!Lflg || (info.pr_nlwp + info.pr_nzomb) <= 1) { 9680Sstevel@tonic-gate prcom(&info, tp); 9690Sstevel@tonic-gate } else { 9700Sstevel@tonic-gate int nlwp = 0; 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate do { 9730Sstevel@tonic-gate info.pr_lwp = *lwpsinfo; 9740Sstevel@tonic-gate prcom(&info, tp); 9750Sstevel@tonic-gate /* LINTED improper alignment */ 9760Sstevel@tonic-gate lwpsinfo = (lwpsinfo_t *)((char *)lwpsinfo + 9770Sstevel@tonic-gate lpsinfobuf->pr_entsize); 9780Sstevel@tonic-gate } while (++nlwp < lpsinfobuf->pr_nent); 9790Sstevel@tonic-gate } 9800Sstevel@tonic-gate return (0); 9810Sstevel@tonic-gate } 9820Sstevel@tonic-gate 9830Sstevel@tonic-gate 9840Sstevel@tonic-gate static void 9850Sstevel@tonic-gate usage(void) /* print usage message and quit */ 9860Sstevel@tonic-gate { 9870Sstevel@tonic-gate static char usage1[] = 988*2685Sakolb "ps [ -aAdefHlcjLPyZ ] [ -o format ] [ -t termlist ]"; 9890Sstevel@tonic-gate static char usage2[] = 9900Sstevel@tonic-gate "\t[ -u userlist ] [ -U userlist ] [ -G grouplist ]"; 9910Sstevel@tonic-gate static char usage3[] = 992*2685Sakolb "\t[ -p proclist ] [ -g pgrplist ] [ -s sidlist ] [ -z zonelist ] " 993*2685Sakolb "[-h lgrplist]"; 9940Sstevel@tonic-gate static char usage4[] = 9950Sstevel@tonic-gate " 'format' is one or more of:"; 9960Sstevel@tonic-gate static char usage5[] = 9970Sstevel@tonic-gate "\tuser ruser group rgroup uid ruid gid rgid pid ppid pgid " 9980Sstevel@tonic-gate "sid taskid ctid"; 9990Sstevel@tonic-gate static char usage6[] = 10000Sstevel@tonic-gate "\tpri opri pcpu pmem vsz rss osz nice class time etime stime zone " 10010Sstevel@tonic-gate "zoneid"; 10020Sstevel@tonic-gate static char usage7[] = 10030Sstevel@tonic-gate "\tf s c lwp nlwp psr tty addr wchan fname comm args " 1004*2685Sakolb "projid project pset lgrp"; 10050Sstevel@tonic-gate 10060Sstevel@tonic-gate (void) fprintf(stderr, 10070Sstevel@tonic-gate gettext("usage: %s\n%s\n%s\n%s\n%s\n%s\n%s\n"), 10080Sstevel@tonic-gate gettext(usage1), gettext(usage2), gettext(usage3), 10090Sstevel@tonic-gate gettext(usage4), gettext(usage5), gettext(usage6), gettext(usage7)); 10100Sstevel@tonic-gate exit(1); 10110Sstevel@tonic-gate } 10120Sstevel@tonic-gate 10130Sstevel@tonic-gate /* 10140Sstevel@tonic-gate * getarg() finds the next argument in list and copies arg into argbuf. 10150Sstevel@tonic-gate * p1 first pts to arg passed back from getopt routine. p1 is then 10160Sstevel@tonic-gate * bumped to next character that is not a comma or blank -- p1 NULL 10170Sstevel@tonic-gate * indicates end of list. 10180Sstevel@tonic-gate */ 10190Sstevel@tonic-gate static char * 10200Sstevel@tonic-gate getarg(char **pp1) 10210Sstevel@tonic-gate { 10220Sstevel@tonic-gate static char argbuf[ARGSIZ]; 10230Sstevel@tonic-gate char *p1 = *pp1; 10240Sstevel@tonic-gate char *parga = argbuf; 10250Sstevel@tonic-gate int c; 10260Sstevel@tonic-gate 10270Sstevel@tonic-gate while ((c = *p1) != '\0' && (c == ',' || isspace(c))) 10280Sstevel@tonic-gate p1++; 10290Sstevel@tonic-gate 10300Sstevel@tonic-gate while ((c = *p1) != '\0' && c != ',' && !isspace(c)) { 10310Sstevel@tonic-gate if (parga < argbuf + ARGSIZ - 1) 10320Sstevel@tonic-gate *parga++ = c; 10330Sstevel@tonic-gate p1++; 10340Sstevel@tonic-gate } 10350Sstevel@tonic-gate *parga = '\0'; 10360Sstevel@tonic-gate 10370Sstevel@tonic-gate while ((c = *p1) != '\0' && (c == ',' || isspace(c))) 10380Sstevel@tonic-gate p1++; 10390Sstevel@tonic-gate 10400Sstevel@tonic-gate *pp1 = p1; 10410Sstevel@tonic-gate 10420Sstevel@tonic-gate return (argbuf); 10430Sstevel@tonic-gate } 10440Sstevel@tonic-gate 10450Sstevel@tonic-gate /* 10460Sstevel@tonic-gate * parse_format() takes the argument to the -o option, 10470Sstevel@tonic-gate * sets up the next output field structure, and returns 10480Sstevel@tonic-gate * a pointer to any further output field specifier(s). 10490Sstevel@tonic-gate * As a side-effect, it increments errflg if encounters a format error. 10500Sstevel@tonic-gate */ 10510Sstevel@tonic-gate static char * 10520Sstevel@tonic-gate parse_format(char *arg) 10530Sstevel@tonic-gate { 10540Sstevel@tonic-gate int c; 10550Sstevel@tonic-gate char *name; 10560Sstevel@tonic-gate char *header = NULL; 10570Sstevel@tonic-gate int width = 0; 10580Sstevel@tonic-gate struct def_field *df; 10590Sstevel@tonic-gate struct field *f; 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate while ((c = *arg) != '\0' && (c == ',' || isspace(c))) 10620Sstevel@tonic-gate arg++; 10630Sstevel@tonic-gate if (c == '\0') 10640Sstevel@tonic-gate return (NULL); 10650Sstevel@tonic-gate name = arg; 10660Sstevel@tonic-gate arg = strpbrk(arg, " \t\r\v\f\n,="); 10670Sstevel@tonic-gate if (arg != NULL) { 10680Sstevel@tonic-gate c = *arg; 10690Sstevel@tonic-gate *arg++ = '\0'; 10700Sstevel@tonic-gate if (c == '=') { 10710Sstevel@tonic-gate char *s; 10720Sstevel@tonic-gate 10730Sstevel@tonic-gate header = arg; 10740Sstevel@tonic-gate arg = NULL; 10750Sstevel@tonic-gate width = strlen(header); 10760Sstevel@tonic-gate s = header + width; 10770Sstevel@tonic-gate while (s > header && isspace(*--s)) 10780Sstevel@tonic-gate *s = '\0'; 10790Sstevel@tonic-gate while (isspace(*header)) 10800Sstevel@tonic-gate header++; 10810Sstevel@tonic-gate } 10820Sstevel@tonic-gate } 10830Sstevel@tonic-gate for (df = &fname[0]; df < &fname[NFIELDS]; df++) 10840Sstevel@tonic-gate if (strcmp(name, df->fname) == 0) { 10850Sstevel@tonic-gate if (strcmp(name, "lwp") == 0) 10860Sstevel@tonic-gate Lflg++; 10870Sstevel@tonic-gate break; 10880Sstevel@tonic-gate } 10890Sstevel@tonic-gate if (df >= &fname[NFIELDS]) { 10900Sstevel@tonic-gate (void) fprintf(stderr, 10910Sstevel@tonic-gate gettext("ps: unknown output format: -o %s\n"), 10920Sstevel@tonic-gate name); 10930Sstevel@tonic-gate errflg++; 10940Sstevel@tonic-gate return (arg); 10950Sstevel@tonic-gate } 10960Sstevel@tonic-gate if ((f = malloc(sizeof (*f))) == NULL) { 10970Sstevel@tonic-gate (void) fprintf(stderr, 10980Sstevel@tonic-gate gettext("ps: malloc() for output format failed, %s\n"), 10990Sstevel@tonic-gate err_string(errno)); 11000Sstevel@tonic-gate exit(1); 11010Sstevel@tonic-gate } 11020Sstevel@tonic-gate f->next = NULL; 11030Sstevel@tonic-gate f->fname = df - &fname[0]; 11040Sstevel@tonic-gate f->header = header? header : df->header; 11050Sstevel@tonic-gate if (width == 0) 11060Sstevel@tonic-gate width = df->width; 11070Sstevel@tonic-gate if (*f->header != '\0') 11080Sstevel@tonic-gate do_header = 1; 11090Sstevel@tonic-gate f->width = max(width, df->minwidth); 11100Sstevel@tonic-gate 11110Sstevel@tonic-gate if (fields == NULL) 11120Sstevel@tonic-gate fields = last_field = f; 11130Sstevel@tonic-gate else { 11140Sstevel@tonic-gate last_field->next = f; 11150Sstevel@tonic-gate last_field = f; 11160Sstevel@tonic-gate } 11170Sstevel@tonic-gate 11180Sstevel@tonic-gate return (arg); 11190Sstevel@tonic-gate } 11200Sstevel@tonic-gate 11210Sstevel@tonic-gate static char * 11220Sstevel@tonic-gate devlookup(dev_t ddev) 11230Sstevel@tonic-gate { 11240Sstevel@tonic-gate struct devl *dp; 11250Sstevel@tonic-gate int i; 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate for (dp = devl, i = 0; i < ndev; dp++, i++) { 11280Sstevel@tonic-gate if (dp->ddev == ddev) 11290Sstevel@tonic-gate return (dp->dname); 11300Sstevel@tonic-gate } 11310Sstevel@tonic-gate return (NULL); 11320Sstevel@tonic-gate } 11330Sstevel@tonic-gate 11340Sstevel@tonic-gate static char * 11350Sstevel@tonic-gate devadd(char *name, dev_t ddev) 11360Sstevel@tonic-gate { 11370Sstevel@tonic-gate struct devl *dp; 11380Sstevel@tonic-gate int leng, start, i; 11390Sstevel@tonic-gate 11400Sstevel@tonic-gate if (ndev == maxdev) { 11410Sstevel@tonic-gate maxdev += DNINCR; 11420Sstevel@tonic-gate devl = Realloc(devl, maxdev * sizeof (struct devl)); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate dp = &devl[ndev++]; 11450Sstevel@tonic-gate 11460Sstevel@tonic-gate dp->ddev = ddev; 11470Sstevel@tonic-gate if (name == NULL) { 11480Sstevel@tonic-gate (void) strcpy(dp->dname, "??"); 11490Sstevel@tonic-gate return (dp->dname); 11500Sstevel@tonic-gate } 11510Sstevel@tonic-gate 11520Sstevel@tonic-gate leng = strlen(name); 11530Sstevel@tonic-gate /* Strip off /dev/ */ 11540Sstevel@tonic-gate if (leng < DNSIZE + 4) 11550Sstevel@tonic-gate (void) strcpy(dp->dname, &name[5]); 11560Sstevel@tonic-gate else { 11570Sstevel@tonic-gate start = leng - DNSIZE - 1; 11580Sstevel@tonic-gate 11590Sstevel@tonic-gate for (i = start; i < leng && name[i] != '/'; i++) 11600Sstevel@tonic-gate ; 11610Sstevel@tonic-gate if (i == leng) 11620Sstevel@tonic-gate (void) strncpy(dp->dname, &name[start], DNSIZE); 11630Sstevel@tonic-gate else 11640Sstevel@tonic-gate (void) strncpy(dp->dname, &name[i+1], DNSIZE); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate return (dp->dname); 11670Sstevel@tonic-gate } 11680Sstevel@tonic-gate 11690Sstevel@tonic-gate /* 11700Sstevel@tonic-gate * gettty returns the user's tty number or ? if none. 11710Sstevel@tonic-gate */ 11720Sstevel@tonic-gate static char * 11730Sstevel@tonic-gate gettty(psinfo_t *psinfo) 11740Sstevel@tonic-gate { 11750Sstevel@tonic-gate extern char *_ttyname_dev(dev_t, char *, size_t); 11760Sstevel@tonic-gate char devname[TTYNAME_MAX]; 11770Sstevel@tonic-gate char *retval; 11780Sstevel@tonic-gate 11790Sstevel@tonic-gate if (psinfo->pr_ttydev == PRNODEV) 11800Sstevel@tonic-gate return ("?"); 11810Sstevel@tonic-gate 11820Sstevel@tonic-gate if ((retval = devlookup(psinfo->pr_ttydev)) != NULL) 11830Sstevel@tonic-gate return (retval); 11840Sstevel@tonic-gate 11850Sstevel@tonic-gate retval = _ttyname_dev(psinfo->pr_ttydev, devname, sizeof (devname)); 11860Sstevel@tonic-gate 11870Sstevel@tonic-gate return (devadd(retval, psinfo->pr_ttydev)); 11880Sstevel@tonic-gate } 11890Sstevel@tonic-gate 11900Sstevel@tonic-gate /* 11910Sstevel@tonic-gate * Find the process's tty and return 1 if process is to be printed. 11920Sstevel@tonic-gate */ 11930Sstevel@tonic-gate static int 11940Sstevel@tonic-gate prfind(int found, psinfo_t *psinfo, char **tpp) 11950Sstevel@tonic-gate { 11960Sstevel@tonic-gate char *tp; 11970Sstevel@tonic-gate struct tty *ttyp; 11980Sstevel@tonic-gate 11990Sstevel@tonic-gate if (psinfo->pr_nlwp == 0) { 12000Sstevel@tonic-gate /* process is a zombie */ 12010Sstevel@tonic-gate *tpp = "?"; 12020Sstevel@tonic-gate if (tflg && !found) 12030Sstevel@tonic-gate return (0); 12040Sstevel@tonic-gate return (1); 12050Sstevel@tonic-gate } 12060Sstevel@tonic-gate 12070Sstevel@tonic-gate /* 12080Sstevel@tonic-gate * Get current terminal. If none ("?") and 'a' is set, don't print 12090Sstevel@tonic-gate * info. If 't' is set, check if term is in list of desired terminals 12100Sstevel@tonic-gate * and print it if it is. 12110Sstevel@tonic-gate */ 12120Sstevel@tonic-gate tp = gettty(psinfo); 12130Sstevel@tonic-gate if (aflg && *tp == '?') { 12140Sstevel@tonic-gate *tpp = tp; 12150Sstevel@tonic-gate return (0); 12160Sstevel@tonic-gate } 12170Sstevel@tonic-gate if (tflg && !found) { 12180Sstevel@tonic-gate int match = 0; 12190Sstevel@tonic-gate char *other = NULL; 12200Sstevel@tonic-gate for (ttyp = tty; ttyp->tname != NULL; ttyp++) { 12210Sstevel@tonic-gate /* 12220Sstevel@tonic-gate * Look for a name match 12230Sstevel@tonic-gate */ 12240Sstevel@tonic-gate if (strcmp(tp, ttyp->tname) == 0) { 12250Sstevel@tonic-gate match = 1; 12260Sstevel@tonic-gate break; 12270Sstevel@tonic-gate } 12280Sstevel@tonic-gate /* 12290Sstevel@tonic-gate * Look for same device under different names. 12300Sstevel@tonic-gate */ 12310Sstevel@tonic-gate if ((other == NULL) && 12320Sstevel@tonic-gate (ttyp->tdev != PRNODEV) && 12330Sstevel@tonic-gate (psinfo->pr_ttydev == ttyp->tdev)) 12340Sstevel@tonic-gate other = ttyp->tname; 12350Sstevel@tonic-gate } 12360Sstevel@tonic-gate if (!match && (other != NULL)) { 12370Sstevel@tonic-gate /* 12380Sstevel@tonic-gate * found under a different name 12390Sstevel@tonic-gate */ 12400Sstevel@tonic-gate match = 1; 12410Sstevel@tonic-gate tp = other; 12420Sstevel@tonic-gate } 12430Sstevel@tonic-gate if (!match || (tuid != -1 && tuid != psinfo->pr_euid)) { 12440Sstevel@tonic-gate /* 12450Sstevel@tonic-gate * not found OR not matching euid 12460Sstevel@tonic-gate */ 12470Sstevel@tonic-gate *tpp = tp; 12480Sstevel@tonic-gate return (0); 12490Sstevel@tonic-gate } 12500Sstevel@tonic-gate } 12510Sstevel@tonic-gate *tpp = tp; 12520Sstevel@tonic-gate return (1); 12530Sstevel@tonic-gate } 12540Sstevel@tonic-gate 12550Sstevel@tonic-gate /* 12560Sstevel@tonic-gate * Print info about the process. 12570Sstevel@tonic-gate */ 12580Sstevel@tonic-gate static void 12590Sstevel@tonic-gate prcom(psinfo_t *psinfo, char *ttyp) 12600Sstevel@tonic-gate { 12610Sstevel@tonic-gate char *cp; 12620Sstevel@tonic-gate long tm; 12630Sstevel@tonic-gate int bytesleft; 12640Sstevel@tonic-gate int wcnt, length; 12650Sstevel@tonic-gate wchar_t wchar; 12660Sstevel@tonic-gate struct passwd *pwd; 12670Sstevel@tonic-gate int zombie_lwp; 12680Sstevel@tonic-gate char zonename[ZONENAME_MAX]; 12690Sstevel@tonic-gate 12700Sstevel@tonic-gate /* 12710Sstevel@tonic-gate * If process is zombie, call zombie print routine and return. 12720Sstevel@tonic-gate */ 12730Sstevel@tonic-gate if (psinfo->pr_nlwp == 0) { 12740Sstevel@tonic-gate if (fields != NULL) 12750Sstevel@tonic-gate pr_fields(psinfo, ttyp, print_zombie_field); 12760Sstevel@tonic-gate else 12770Sstevel@tonic-gate przom(psinfo); 12780Sstevel@tonic-gate return; 12790Sstevel@tonic-gate } 12800Sstevel@tonic-gate 12810Sstevel@tonic-gate zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z'); 12820Sstevel@tonic-gate 12830Sstevel@tonic-gate /* 12840Sstevel@tonic-gate * If user specified '-o format', print requested fields and return. 12850Sstevel@tonic-gate */ 12860Sstevel@tonic-gate if (fields != NULL) { 12870Sstevel@tonic-gate pr_fields(psinfo, ttyp, print_field); 12880Sstevel@tonic-gate return; 12890Sstevel@tonic-gate } 12900Sstevel@tonic-gate 12910Sstevel@tonic-gate /* 12920Sstevel@tonic-gate * All fields before 'PID' are printed with a trailing space as a 12930Sstevel@tonic-gate * spearator, rather than keeping track of which column is first. All 12940Sstevel@tonic-gate * other fields are printed with a leading space. 12950Sstevel@tonic-gate */ 12960Sstevel@tonic-gate if (lflg) { 12970Sstevel@tonic-gate if (!yflg) 12980Sstevel@tonic-gate (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */ 12990Sstevel@tonic-gate (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */ 13000Sstevel@tonic-gate } 13010Sstevel@tonic-gate 13020Sstevel@tonic-gate if (Zflg) { /* ZONE */ 13030Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename, 13040Sstevel@tonic-gate sizeof (zonename)) < 0) { 13050Sstevel@tonic-gate (void) printf("%7.7d ", ((int)psinfo->pr_zoneid)); 13060Sstevel@tonic-gate } else { 13070Sstevel@tonic-gate (void) printf("%8.8s ", zonename); 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate } 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate if (fflg) { /* UID */ 13120Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) 13130Sstevel@tonic-gate (void) printf("%8.8s ", pwd->pw_name); 13140Sstevel@tonic-gate else 13150Sstevel@tonic-gate (void) printf("%7.7d ", (int)psinfo->pr_euid); 13160Sstevel@tonic-gate } else if (lflg) { 13170Sstevel@tonic-gate (void) printf("%6d ", (int)psinfo->pr_euid); 13180Sstevel@tonic-gate } 13190Sstevel@tonic-gate (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */ 13200Sstevel@tonic-gate if (lflg || fflg) 13210Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 13220Sstevel@tonic-gate (int)psinfo->pr_ppid); /* PPID */ 13230Sstevel@tonic-gate if (jflg) { 13240Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 13250Sstevel@tonic-gate (int)psinfo->pr_pgid); /* PGID */ 13260Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 13270Sstevel@tonic-gate (int)psinfo->pr_sid); /* SID */ 13280Sstevel@tonic-gate } 13290Sstevel@tonic-gate if (Lflg) 13300Sstevel@tonic-gate (void) printf(" %5d", (int)psinfo->pr_lwp.pr_lwpid); /* LWP */ 13310Sstevel@tonic-gate if (Pflg) { 13320Sstevel@tonic-gate if (psinfo->pr_lwp.pr_bindpro == PBIND_NONE) /* PSR */ 13330Sstevel@tonic-gate (void) printf(" -"); 13340Sstevel@tonic-gate else 13350Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_bindpro); 13360Sstevel@tonic-gate } 13370Sstevel@tonic-gate if (Lflg && fflg) /* NLWP */ 13380Sstevel@tonic-gate (void) printf(" %5d", psinfo->pr_nlwp + psinfo->pr_nzomb); 13390Sstevel@tonic-gate if (cflg) { 13400Sstevel@tonic-gate if (zombie_lwp) /* CLS */ 13410Sstevel@tonic-gate (void) printf(" "); 13420Sstevel@tonic-gate else 13430Sstevel@tonic-gate (void) printf(" %4s", psinfo->pr_lwp.pr_clname); 13440Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */ 13450Sstevel@tonic-gate } else if (lflg || fflg) { 13460Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */ 13470Sstevel@tonic-gate if (lflg) { /* PRI NI */ 13480Sstevel@tonic-gate /* 13490Sstevel@tonic-gate * Print priorities the old way (lower numbers 13500Sstevel@tonic-gate * mean higher priority) and print nice value 13510Sstevel@tonic-gate * for time sharing procs. 13520Sstevel@tonic-gate */ 13530Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_oldpri); 13540Sstevel@tonic-gate if (psinfo->pr_lwp.pr_oldpri != 0) 13550Sstevel@tonic-gate (void) printf(" %2d", psinfo->pr_lwp.pr_nice); 13560Sstevel@tonic-gate else 13570Sstevel@tonic-gate (void) printf(" %2.2s", 13580Sstevel@tonic-gate psinfo->pr_lwp.pr_clname); 13590Sstevel@tonic-gate } 13600Sstevel@tonic-gate } 13610Sstevel@tonic-gate if (lflg) { 13620Sstevel@tonic-gate if (yflg) { 13630Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* RSS */ 13640Sstevel@tonic-gate (void) printf(" 0"); 13650Sstevel@tonic-gate else if (psinfo->pr_rssize) 13660Sstevel@tonic-gate (void) printf(" %5lu", 13670Sstevel@tonic-gate (ulong_t)psinfo->pr_rssize); 13680Sstevel@tonic-gate else 13690Sstevel@tonic-gate (void) printf(" ?"); 13700Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* SZ */ 13710Sstevel@tonic-gate (void) printf(" 0"); 13720Sstevel@tonic-gate else if (psinfo->pr_size) 13730Sstevel@tonic-gate (void) printf(" %6lu", 13740Sstevel@tonic-gate (ulong_t)psinfo->pr_size); 13750Sstevel@tonic-gate else 13760Sstevel@tonic-gate (void) printf(" ?"); 13770Sstevel@tonic-gate } else { 13780Sstevel@tonic-gate #ifndef _LP64 13790Sstevel@tonic-gate if (psinfo->pr_addr) /* ADDR */ 13800Sstevel@tonic-gate (void) printf(" %8lx", 13810Sstevel@tonic-gate (ulong_t)psinfo->pr_addr); 13820Sstevel@tonic-gate else 13830Sstevel@tonic-gate #endif 13840Sstevel@tonic-gate (void) printf(" ?"); 13850Sstevel@tonic-gate if (psinfo->pr_flag & SSYS) /* SZ */ 13860Sstevel@tonic-gate (void) printf(" 0"); 13870Sstevel@tonic-gate else if (psinfo->pr_size) 13880Sstevel@tonic-gate (void) printf(" %6lu", 13890Sstevel@tonic-gate (ulong_t)psinfo->pr_size / kbytes_per_page); 13900Sstevel@tonic-gate else 13910Sstevel@tonic-gate (void) printf(" ?"); 13920Sstevel@tonic-gate } 13930Sstevel@tonic-gate if (psinfo->pr_lwp.pr_sname != 'S') /* WCHAN */ 13940Sstevel@tonic-gate (void) printf(" "); 13950Sstevel@tonic-gate #ifndef _LP64 13960Sstevel@tonic-gate else if (psinfo->pr_lwp.pr_wchan) 13970Sstevel@tonic-gate (void) printf(" %8lx", 13980Sstevel@tonic-gate (ulong_t)psinfo->pr_lwp.pr_wchan); 13990Sstevel@tonic-gate #endif 14000Sstevel@tonic-gate else 14010Sstevel@tonic-gate (void) printf(" ?"); 14020Sstevel@tonic-gate } 14030Sstevel@tonic-gate if (fflg) { /* STIME */ 14040Sstevel@tonic-gate if (Lflg) 14050Sstevel@tonic-gate prtime(psinfo->pr_lwp.pr_start, 9, 1); 14060Sstevel@tonic-gate else 14070Sstevel@tonic-gate prtime(psinfo->pr_start, 9, 1); 14080Sstevel@tonic-gate } 1409*2685Sakolb 1410*2685Sakolb if (Hflg) { 1411*2685Sakolb /* Display home lgroup */ 1412*2685Sakolb (void) printf(" %4d", (int)psinfo->pr_lwp.pr_lgrp); 1413*2685Sakolb } 1414*2685Sakolb 14150Sstevel@tonic-gate (void) printf(" %-8.14s", ttyp); /* TTY */ 14160Sstevel@tonic-gate if (Lflg) { 14170Sstevel@tonic-gate tm = psinfo->pr_lwp.pr_time.tv_sec; 14180Sstevel@tonic-gate if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000) 14190Sstevel@tonic-gate tm++; 14200Sstevel@tonic-gate } else { 14210Sstevel@tonic-gate tm = psinfo->pr_time.tv_sec; 14220Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000) 14230Sstevel@tonic-gate tm++; 14240Sstevel@tonic-gate } 14250Sstevel@tonic-gate (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* [L]TIME */ 14260Sstevel@tonic-gate 14270Sstevel@tonic-gate if (zombie_lwp) { 14280Sstevel@tonic-gate (void) printf(" <defunct>\n"); 14290Sstevel@tonic-gate return; 14300Sstevel@tonic-gate } 14310Sstevel@tonic-gate 14320Sstevel@tonic-gate if (!fflg) { /* CMD */ 14330Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_fname, 16, 8); 14340Sstevel@tonic-gate (void) printf(" %.*s\n", wcnt, psinfo->pr_fname); 14350Sstevel@tonic-gate return; 14360Sstevel@tonic-gate } 14370Sstevel@tonic-gate 1438*2685Sakolb 14390Sstevel@tonic-gate /* 14400Sstevel@tonic-gate * PRARGSZ == length of cmd arg string. 14410Sstevel@tonic-gate */ 14420Sstevel@tonic-gate psinfo->pr_psargs[PRARGSZ-1] = '\0'; 14430Sstevel@tonic-gate bytesleft = PRARGSZ; 14440Sstevel@tonic-gate for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) { 14450Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX); 14460Sstevel@tonic-gate if (length == 0) 14470Sstevel@tonic-gate break; 14480Sstevel@tonic-gate if (length < 0 || !iswprint(wchar)) { 14490Sstevel@tonic-gate if (length < 0) 14500Sstevel@tonic-gate length = 1; 14510Sstevel@tonic-gate if (bytesleft <= length) { 14520Sstevel@tonic-gate *cp = '\0'; 14530Sstevel@tonic-gate break; 14540Sstevel@tonic-gate } 14550Sstevel@tonic-gate /* omit the unprintable character */ 14560Sstevel@tonic-gate (void) memmove(cp, cp+length, bytesleft-length); 14570Sstevel@tonic-gate length = 0; 14580Sstevel@tonic-gate } 14590Sstevel@tonic-gate bytesleft -= length; 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, lflg ? 35 : PRARGSZ); 14620Sstevel@tonic-gate (void) printf(" %.*s\n", wcnt, psinfo->pr_psargs); 14630Sstevel@tonic-gate } 14640Sstevel@tonic-gate 14650Sstevel@tonic-gate /* 14660Sstevel@tonic-gate * Print percent from 16-bit binary fraction [0 .. 1] 14670Sstevel@tonic-gate * Round up .01 to .1 to indicate some small percentage (the 0x7000 below). 14680Sstevel@tonic-gate */ 14690Sstevel@tonic-gate static void 14700Sstevel@tonic-gate prtpct(ushort_t pct, int width) 14710Sstevel@tonic-gate { 14720Sstevel@tonic-gate uint_t value = pct; /* need 32 bits to compute with */ 14730Sstevel@tonic-gate 14740Sstevel@tonic-gate value = ((value * 1000) + 0x7000) >> 15; /* [0 .. 1000] */ 14750Sstevel@tonic-gate if (value >= 1000) 14760Sstevel@tonic-gate value = 999; 14770Sstevel@tonic-gate if ((width -= 2) < 2) 14780Sstevel@tonic-gate width = 2; 14790Sstevel@tonic-gate (void) printf("%*u.%u", width, value / 10, value % 10); 14800Sstevel@tonic-gate } 14810Sstevel@tonic-gate 14820Sstevel@tonic-gate static void 14830Sstevel@tonic-gate print_time(time_t tim, int width) 14840Sstevel@tonic-gate { 14850Sstevel@tonic-gate char buf[30]; 14860Sstevel@tonic-gate time_t seconds; 14870Sstevel@tonic-gate time_t minutes; 14880Sstevel@tonic-gate time_t hours; 14890Sstevel@tonic-gate time_t days; 14900Sstevel@tonic-gate 14910Sstevel@tonic-gate if (tim < 0) { 14920Sstevel@tonic-gate (void) printf("%*s", width, "-"); 14930Sstevel@tonic-gate return; 14940Sstevel@tonic-gate } 14950Sstevel@tonic-gate 14960Sstevel@tonic-gate seconds = tim % 60; 14970Sstevel@tonic-gate tim /= 60; 14980Sstevel@tonic-gate minutes = tim % 60; 14990Sstevel@tonic-gate tim /= 60; 15000Sstevel@tonic-gate hours = tim % 24; 15010Sstevel@tonic-gate days = tim / 24; 15020Sstevel@tonic-gate 15030Sstevel@tonic-gate if (days > 0) { 15040Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%ld-%2.2ld:%2.2ld:%2.2ld", 15050Sstevel@tonic-gate days, hours, minutes, seconds); 15060Sstevel@tonic-gate } else if (hours > 0) { 15070Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld:%2.2ld", 15080Sstevel@tonic-gate hours, minutes, seconds); 15090Sstevel@tonic-gate } else { 15100Sstevel@tonic-gate (void) snprintf(buf, sizeof (buf), "%2.2ld:%2.2ld", 15110Sstevel@tonic-gate minutes, seconds); 15120Sstevel@tonic-gate } 15130Sstevel@tonic-gate 15140Sstevel@tonic-gate (void) printf("%*s", width, buf); 15150Sstevel@tonic-gate } 15160Sstevel@tonic-gate 15170Sstevel@tonic-gate static void 15180Sstevel@tonic-gate print_field(psinfo_t *psinfo, struct field *f, const char *ttyp) 15190Sstevel@tonic-gate { 15200Sstevel@tonic-gate int width = f->width; 15210Sstevel@tonic-gate struct passwd *pwd; 15220Sstevel@tonic-gate struct group *grp; 15230Sstevel@tonic-gate time_t cputime; 15240Sstevel@tonic-gate int bytesleft; 15250Sstevel@tonic-gate int wcnt; 15260Sstevel@tonic-gate wchar_t wchar; 15270Sstevel@tonic-gate char *cp; 15280Sstevel@tonic-gate int length; 15290Sstevel@tonic-gate ulong_t mask; 15300Sstevel@tonic-gate char c, *csave; 15310Sstevel@tonic-gate int zombie_lwp; 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate zombie_lwp = (Lflg && psinfo->pr_lwp.pr_sname == 'Z'); 15340Sstevel@tonic-gate 15350Sstevel@tonic-gate switch (f->fname) { 15360Sstevel@tonic-gate case F_RUSER: 15370Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_uid)) != NULL) 15380Sstevel@tonic-gate (void) printf("%*s", width, pwd->pw_name); 15390Sstevel@tonic-gate else 15400Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_uid); 15410Sstevel@tonic-gate break; 15420Sstevel@tonic-gate case F_USER: 15430Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) 15440Sstevel@tonic-gate (void) printf("%*s", width, pwd->pw_name); 15450Sstevel@tonic-gate else 15460Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_euid); 15470Sstevel@tonic-gate break; 15480Sstevel@tonic-gate case F_RGROUP: 15490Sstevel@tonic-gate if ((grp = getgrgid(psinfo->pr_gid)) != NULL) 15500Sstevel@tonic-gate (void) printf("%*s", width, grp->gr_name); 15510Sstevel@tonic-gate else 15520Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_gid); 15530Sstevel@tonic-gate break; 15540Sstevel@tonic-gate case F_GROUP: 15550Sstevel@tonic-gate if ((grp = getgrgid(psinfo->pr_egid)) != NULL) 15560Sstevel@tonic-gate (void) printf("%*s", width, grp->gr_name); 15570Sstevel@tonic-gate else 15580Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_egid); 15590Sstevel@tonic-gate break; 15600Sstevel@tonic-gate case F_RUID: 15610Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_uid); 15620Sstevel@tonic-gate break; 15630Sstevel@tonic-gate case F_UID: 15640Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_euid); 15650Sstevel@tonic-gate break; 15660Sstevel@tonic-gate case F_RGID: 15670Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_gid); 15680Sstevel@tonic-gate break; 15690Sstevel@tonic-gate case F_GID: 15700Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_egid); 15710Sstevel@tonic-gate break; 15720Sstevel@tonic-gate case F_PID: 15730Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_pid); 15740Sstevel@tonic-gate break; 15750Sstevel@tonic-gate case F_PPID: 15760Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_ppid); 15770Sstevel@tonic-gate break; 15780Sstevel@tonic-gate case F_PGID: 15790Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_pgid); 15800Sstevel@tonic-gate break; 15810Sstevel@tonic-gate case F_SID: 15820Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_sid); 15830Sstevel@tonic-gate break; 15840Sstevel@tonic-gate case F_PSR: 15850Sstevel@tonic-gate if (zombie_lwp || psinfo->pr_lwp.pr_bindpro == PBIND_NONE) 15860Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15870Sstevel@tonic-gate else 15880Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpro); 15890Sstevel@tonic-gate break; 15900Sstevel@tonic-gate case F_LWP: 15910Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lwpid); 15920Sstevel@tonic-gate break; 15930Sstevel@tonic-gate case F_NLWP: 15940Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_nlwp + psinfo->pr_nzomb); 15950Sstevel@tonic-gate break; 15960Sstevel@tonic-gate case F_OPRI: 15970Sstevel@tonic-gate if (zombie_lwp) 15980Sstevel@tonic-gate (void) printf("%*s", width, "-"); 15990Sstevel@tonic-gate else 16000Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_oldpri); 16010Sstevel@tonic-gate break; 16020Sstevel@tonic-gate case F_PRI: 16030Sstevel@tonic-gate if (zombie_lwp) 16040Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16050Sstevel@tonic-gate else 16060Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_pri); 16070Sstevel@tonic-gate break; 16080Sstevel@tonic-gate case F_F: 16090Sstevel@tonic-gate mask = 0xffffffffUL; 16100Sstevel@tonic-gate if (width < 8) 16110Sstevel@tonic-gate mask >>= (8 - width) * 4; 16120Sstevel@tonic-gate (void) printf("%*lx", width, psinfo->pr_flag & mask); 16130Sstevel@tonic-gate break; 16140Sstevel@tonic-gate case F_S: 16150Sstevel@tonic-gate (void) printf("%*c", width, psinfo->pr_lwp.pr_sname); 16160Sstevel@tonic-gate break; 16170Sstevel@tonic-gate case F_C: 16180Sstevel@tonic-gate if (zombie_lwp) 16190Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16200Sstevel@tonic-gate else 16210Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_cpu); 16220Sstevel@tonic-gate break; 16230Sstevel@tonic-gate case F_PCPU: 16240Sstevel@tonic-gate if (zombie_lwp) 16250Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16260Sstevel@tonic-gate else if (Lflg) 16270Sstevel@tonic-gate prtpct(psinfo->pr_lwp.pr_pctcpu, width); 16280Sstevel@tonic-gate else 16290Sstevel@tonic-gate prtpct(psinfo->pr_pctcpu, width); 16300Sstevel@tonic-gate break; 16310Sstevel@tonic-gate case F_PMEM: 16320Sstevel@tonic-gate prtpct(psinfo->pr_pctmem, width); 16330Sstevel@tonic-gate break; 16340Sstevel@tonic-gate case F_OSZ: 16350Sstevel@tonic-gate (void) printf("%*lu", width, 16360Sstevel@tonic-gate (ulong_t)psinfo->pr_size / kbytes_per_page); 16370Sstevel@tonic-gate break; 16380Sstevel@tonic-gate case F_VSZ: 16390Sstevel@tonic-gate (void) printf("%*lu", width, (ulong_t)psinfo->pr_size); 16400Sstevel@tonic-gate break; 16410Sstevel@tonic-gate case F_RSS: 16420Sstevel@tonic-gate (void) printf("%*lu", width, (ulong_t)psinfo->pr_rssize); 16430Sstevel@tonic-gate break; 16440Sstevel@tonic-gate case F_NICE: 16450Sstevel@tonic-gate /* if pr_oldpri is zero, then this class has no nice */ 16460Sstevel@tonic-gate if (zombie_lwp) 16470Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16480Sstevel@tonic-gate else if (psinfo->pr_lwp.pr_oldpri != 0) 16490Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_nice); 16500Sstevel@tonic-gate else 16510Sstevel@tonic-gate (void) printf("%*.*s", width, width, 16520Sstevel@tonic-gate psinfo->pr_lwp.pr_clname); 16530Sstevel@tonic-gate break; 16540Sstevel@tonic-gate case F_CLASS: 16550Sstevel@tonic-gate if (zombie_lwp) 16560Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16570Sstevel@tonic-gate else 16580Sstevel@tonic-gate (void) printf("%*.*s", width, width, 16590Sstevel@tonic-gate psinfo->pr_lwp.pr_clname); 16600Sstevel@tonic-gate break; 16610Sstevel@tonic-gate case F_STIME: 16620Sstevel@tonic-gate if (Lflg) 16630Sstevel@tonic-gate prtime(psinfo->pr_lwp.pr_start, width, 0); 16640Sstevel@tonic-gate else 16650Sstevel@tonic-gate prtime(psinfo->pr_start, width, 0); 16660Sstevel@tonic-gate break; 16670Sstevel@tonic-gate case F_ETIME: 16680Sstevel@tonic-gate if (Lflg) 16690Sstevel@tonic-gate print_time(delta_secs(&psinfo->pr_lwp.pr_start), 16700Sstevel@tonic-gate width); 16710Sstevel@tonic-gate else 16720Sstevel@tonic-gate print_time(delta_secs(&psinfo->pr_start), width); 16730Sstevel@tonic-gate break; 16740Sstevel@tonic-gate case F_TIME: 16750Sstevel@tonic-gate if (Lflg) { 16760Sstevel@tonic-gate cputime = psinfo->pr_lwp.pr_time.tv_sec; 16770Sstevel@tonic-gate if (psinfo->pr_lwp.pr_time.tv_nsec > 500000000) 16780Sstevel@tonic-gate cputime++; 16790Sstevel@tonic-gate } else { 16800Sstevel@tonic-gate cputime = psinfo->pr_time.tv_sec; 16810Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000) 16820Sstevel@tonic-gate cputime++; 16830Sstevel@tonic-gate } 16840Sstevel@tonic-gate print_time(cputime, width); 16850Sstevel@tonic-gate break; 16860Sstevel@tonic-gate case F_TTY: 16870Sstevel@tonic-gate (void) printf("%-*s", width, ttyp); 16880Sstevel@tonic-gate break; 16890Sstevel@tonic-gate case F_ADDR: 16900Sstevel@tonic-gate if (zombie_lwp) 16910Sstevel@tonic-gate (void) printf("%*s", width, "-"); 16920Sstevel@tonic-gate else if (Lflg) 16930Sstevel@tonic-gate (void) printf("%*lx", width, 16940Sstevel@tonic-gate (long)psinfo->pr_lwp.pr_addr); 16950Sstevel@tonic-gate else 16960Sstevel@tonic-gate (void) printf("%*lx", width, (long)psinfo->pr_addr); 16970Sstevel@tonic-gate break; 16980Sstevel@tonic-gate case F_WCHAN: 16990Sstevel@tonic-gate if (!zombie_lwp && psinfo->pr_lwp.pr_wchan) 17000Sstevel@tonic-gate (void) printf("%*lx", width, 17010Sstevel@tonic-gate (long)psinfo->pr_lwp.pr_wchan); 17020Sstevel@tonic-gate else 17030Sstevel@tonic-gate (void) printf("%*.*s", width, width, "-"); 17040Sstevel@tonic-gate break; 17050Sstevel@tonic-gate case F_FNAME: 17060Sstevel@tonic-gate /* 17070Sstevel@tonic-gate * Print full width unless this is the last output format. 17080Sstevel@tonic-gate */ 17090Sstevel@tonic-gate if (zombie_lwp) { 17100Sstevel@tonic-gate if (f->next != NULL) 17110Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>"); 17120Sstevel@tonic-gate else 17130Sstevel@tonic-gate (void) printf("%s", "<defunct>"); 17140Sstevel@tonic-gate break; 17150Sstevel@tonic-gate } 17160Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_fname, 16, width); 17170Sstevel@tonic-gate if (f->next != NULL) 17180Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, psinfo->pr_fname); 17190Sstevel@tonic-gate else 17200Sstevel@tonic-gate (void) printf("%-.*s", wcnt, psinfo->pr_fname); 17210Sstevel@tonic-gate break; 17220Sstevel@tonic-gate case F_COMM: 17230Sstevel@tonic-gate if (zombie_lwp) { 17240Sstevel@tonic-gate if (f->next != NULL) 17250Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>"); 17260Sstevel@tonic-gate else 17270Sstevel@tonic-gate (void) printf("%s", "<defunct>"); 17280Sstevel@tonic-gate break; 17290Sstevel@tonic-gate } 17300Sstevel@tonic-gate csave = strpbrk(psinfo->pr_psargs, " \t\r\v\f\n"); 17310Sstevel@tonic-gate if (csave) { 17320Sstevel@tonic-gate c = *csave; 17330Sstevel@tonic-gate *csave = '\0'; 17340Sstevel@tonic-gate } 17350Sstevel@tonic-gate /* FALLTHROUGH */ 17360Sstevel@tonic-gate case F_ARGS: 17370Sstevel@tonic-gate /* 17380Sstevel@tonic-gate * PRARGSZ == length of cmd arg string. 17390Sstevel@tonic-gate */ 17400Sstevel@tonic-gate if (zombie_lwp) { 17410Sstevel@tonic-gate (void) printf("%-*s", width, "<defunct>"); 17420Sstevel@tonic-gate break; 17430Sstevel@tonic-gate } 17440Sstevel@tonic-gate psinfo->pr_psargs[PRARGSZ-1] = '\0'; 17450Sstevel@tonic-gate bytesleft = PRARGSZ; 17460Sstevel@tonic-gate for (cp = psinfo->pr_psargs; *cp != '\0'; cp += length) { 17470Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX); 17480Sstevel@tonic-gate if (length == 0) 17490Sstevel@tonic-gate break; 17500Sstevel@tonic-gate if (length < 0 || !iswprint(wchar)) { 17510Sstevel@tonic-gate if (length < 0) 17520Sstevel@tonic-gate length = 1; 17530Sstevel@tonic-gate if (bytesleft <= length) { 17540Sstevel@tonic-gate *cp = '\0'; 17550Sstevel@tonic-gate break; 17560Sstevel@tonic-gate } 17570Sstevel@tonic-gate /* omit the unprintable character */ 17580Sstevel@tonic-gate (void) memmove(cp, cp+length, bytesleft-length); 17590Sstevel@tonic-gate length = 0; 17600Sstevel@tonic-gate } 17610Sstevel@tonic-gate bytesleft -= length; 17620Sstevel@tonic-gate } 17630Sstevel@tonic-gate wcnt = namencnt(psinfo->pr_psargs, PRARGSZ, width); 17640Sstevel@tonic-gate /* 17650Sstevel@tonic-gate * Print full width unless this is the last format. 17660Sstevel@tonic-gate */ 17670Sstevel@tonic-gate if (f->next != NULL) 17680Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, 17690Sstevel@tonic-gate psinfo->pr_psargs); 17700Sstevel@tonic-gate else 17710Sstevel@tonic-gate (void) printf("%-.*s", wcnt, 17720Sstevel@tonic-gate psinfo->pr_psargs); 17730Sstevel@tonic-gate if (f->fname == F_COMM && csave) 17740Sstevel@tonic-gate *csave = c; 17750Sstevel@tonic-gate break; 17760Sstevel@tonic-gate case F_TASKID: 17770Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_taskid); 17780Sstevel@tonic-gate break; 17790Sstevel@tonic-gate case F_PROJID: 17800Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_projid); 17810Sstevel@tonic-gate break; 17820Sstevel@tonic-gate case F_PROJECT: 17830Sstevel@tonic-gate { 17840Sstevel@tonic-gate struct project cproj; 17850Sstevel@tonic-gate char proj_buf[PROJECT_BUFSZ]; 17860Sstevel@tonic-gate 17870Sstevel@tonic-gate if ((getprojbyid(psinfo->pr_projid, &cproj, 17880Sstevel@tonic-gate (void *)&proj_buf, PROJECT_BUFSZ)) == NULL) 17890Sstevel@tonic-gate (void) printf("%*d", width, 17900Sstevel@tonic-gate (int)psinfo->pr_projid); 17910Sstevel@tonic-gate else 17920Sstevel@tonic-gate (void) printf("%*s", width, 17930Sstevel@tonic-gate (cproj.pj_name != NULL) ? 17940Sstevel@tonic-gate cproj.pj_name : "---"); 17950Sstevel@tonic-gate } 17960Sstevel@tonic-gate break; 17970Sstevel@tonic-gate case F_PSET: 17980Sstevel@tonic-gate if (zombie_lwp || psinfo->pr_lwp.pr_bindpset == PS_NONE) 17990Sstevel@tonic-gate (void) printf("%*s", width, "-"); 18000Sstevel@tonic-gate else 18010Sstevel@tonic-gate (void) printf("%*d", width, psinfo->pr_lwp.pr_bindpset); 18020Sstevel@tonic-gate break; 18030Sstevel@tonic-gate case F_ZONEID: 18040Sstevel@tonic-gate (void) printf("%*d", width, (int)psinfo->pr_zoneid); 18050Sstevel@tonic-gate break; 18060Sstevel@tonic-gate case F_ZONE: 18070Sstevel@tonic-gate { 18080Sstevel@tonic-gate char zonename[ZONENAME_MAX]; 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename, 18110Sstevel@tonic-gate sizeof (zonename)) < 0) { 18120Sstevel@tonic-gate (void) printf("%*d", width, 18130Sstevel@tonic-gate ((int)psinfo->pr_zoneid)); 18140Sstevel@tonic-gate } else { 18150Sstevel@tonic-gate (void) printf("%*s", width, zonename); 18160Sstevel@tonic-gate } 18170Sstevel@tonic-gate } 18180Sstevel@tonic-gate break; 18190Sstevel@tonic-gate case F_CTID: 18200Sstevel@tonic-gate if (psinfo->pr_contract == -1) 18210Sstevel@tonic-gate (void) printf("%*s", width, "-"); 18220Sstevel@tonic-gate else 18230Sstevel@tonic-gate (void) printf("%*ld", width, (long)psinfo->pr_contract); 18240Sstevel@tonic-gate break; 1825*2685Sakolb case F_LGRP: 1826*2685Sakolb /* Display home lgroup */ 1827*2685Sakolb (void) printf("%*d", width, (int)psinfo->pr_lwp.pr_lgrp); 1828*2685Sakolb break; 18290Sstevel@tonic-gate } 18300Sstevel@tonic-gate } 18310Sstevel@tonic-gate 18320Sstevel@tonic-gate static void 18330Sstevel@tonic-gate print_zombie_field(psinfo_t *psinfo, struct field *f, const char *ttyp) 18340Sstevel@tonic-gate { 18350Sstevel@tonic-gate int wcnt; 18360Sstevel@tonic-gate int width = f->width; 18370Sstevel@tonic-gate 18380Sstevel@tonic-gate switch (f->fname) { 18390Sstevel@tonic-gate case F_FNAME: 18400Sstevel@tonic-gate case F_COMM: 18410Sstevel@tonic-gate case F_ARGS: 18420Sstevel@tonic-gate /* 18430Sstevel@tonic-gate * Print full width unless this is the last output format. 18440Sstevel@tonic-gate */ 18450Sstevel@tonic-gate wcnt = min(width, sizeof ("<defunct>")); 18460Sstevel@tonic-gate if (f->next != NULL) 18470Sstevel@tonic-gate (void) printf("%-*.*s", width, wcnt, "<defunct>"); 18480Sstevel@tonic-gate else 18490Sstevel@tonic-gate (void) printf("%-.*s", wcnt, "<defunct>"); 18500Sstevel@tonic-gate break; 18510Sstevel@tonic-gate 18520Sstevel@tonic-gate case F_PSR: 18530Sstevel@tonic-gate case F_PCPU: 18540Sstevel@tonic-gate case F_PMEM: 18550Sstevel@tonic-gate case F_NICE: 18560Sstevel@tonic-gate case F_CLASS: 18570Sstevel@tonic-gate case F_STIME: 18580Sstevel@tonic-gate case F_ETIME: 18590Sstevel@tonic-gate case F_WCHAN: 18600Sstevel@tonic-gate case F_PSET: 18610Sstevel@tonic-gate (void) printf("%*s", width, "-"); 18620Sstevel@tonic-gate break; 18630Sstevel@tonic-gate 18640Sstevel@tonic-gate case F_OPRI: 18650Sstevel@tonic-gate case F_PRI: 18660Sstevel@tonic-gate case F_OSZ: 18670Sstevel@tonic-gate case F_VSZ: 18680Sstevel@tonic-gate case F_RSS: 18690Sstevel@tonic-gate (void) printf("%*d", width, 0); 18700Sstevel@tonic-gate break; 18710Sstevel@tonic-gate 18720Sstevel@tonic-gate default: 18730Sstevel@tonic-gate print_field(psinfo, f, ttyp); 18740Sstevel@tonic-gate break; 18750Sstevel@tonic-gate } 18760Sstevel@tonic-gate } 18770Sstevel@tonic-gate 18780Sstevel@tonic-gate static void 18790Sstevel@tonic-gate pr_fields(psinfo_t *psinfo, const char *ttyp, 18800Sstevel@tonic-gate void (*print_fld)(psinfo_t *, struct field *, const char *)) 18810Sstevel@tonic-gate { 18820Sstevel@tonic-gate struct field *f; 18830Sstevel@tonic-gate 18840Sstevel@tonic-gate for (f = fields; f != NULL; f = f->next) { 18850Sstevel@tonic-gate print_fld(psinfo, f, ttyp); 18860Sstevel@tonic-gate if (f->next != NULL) 18870Sstevel@tonic-gate (void) printf(" "); 18880Sstevel@tonic-gate } 18890Sstevel@tonic-gate (void) printf("\n"); 18900Sstevel@tonic-gate } 18910Sstevel@tonic-gate 18920Sstevel@tonic-gate /* 18930Sstevel@tonic-gate * Returns 1 if arg is found in array arr, of length num; 0 otherwise. 18940Sstevel@tonic-gate */ 18950Sstevel@tonic-gate static int 18960Sstevel@tonic-gate search(pid_t *arr, int number, pid_t arg) 18970Sstevel@tonic-gate { 18980Sstevel@tonic-gate int i; 18990Sstevel@tonic-gate 19000Sstevel@tonic-gate for (i = 0; i < number; i++) 19010Sstevel@tonic-gate if (arg == arr[i]) 19020Sstevel@tonic-gate return (1); 19030Sstevel@tonic-gate return (0); 19040Sstevel@tonic-gate } 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate /* 19070Sstevel@tonic-gate * Add an entry (user, group) to the specified table. 19080Sstevel@tonic-gate */ 19090Sstevel@tonic-gate static void 19100Sstevel@tonic-gate add_ugentry(struct ughead *tbl, char *name) 19110Sstevel@tonic-gate { 19120Sstevel@tonic-gate struct ugdata *entp; 19130Sstevel@tonic-gate 19140Sstevel@tonic-gate if (tbl->size == tbl->nent) { /* reallocate the table entries */ 19150Sstevel@tonic-gate if ((tbl->size *= 2) == 0) 19160Sstevel@tonic-gate tbl->size = 32; /* first time */ 19170Sstevel@tonic-gate tbl->ent = Realloc(tbl->ent, tbl->size*sizeof (struct ugdata)); 19180Sstevel@tonic-gate } 19190Sstevel@tonic-gate entp = &tbl->ent[tbl->nent++]; 19200Sstevel@tonic-gate entp->id = 0; 19210Sstevel@tonic-gate (void) strncpy(entp->name, name, MAXUGNAME); 19220Sstevel@tonic-gate entp->name[MAXUGNAME] = '\0'; 19230Sstevel@tonic-gate } 19240Sstevel@tonic-gate 19250Sstevel@tonic-gate static int 19260Sstevel@tonic-gate uconv(struct ughead *uhead) 19270Sstevel@tonic-gate { 19280Sstevel@tonic-gate struct ugdata *utbl = uhead->ent; 19290Sstevel@tonic-gate int n = uhead->nent; 19300Sstevel@tonic-gate struct passwd *pwd; 19310Sstevel@tonic-gate int i; 19320Sstevel@tonic-gate int fnd = 0; 19330Sstevel@tonic-gate uid_t uid; 19340Sstevel@tonic-gate 19350Sstevel@tonic-gate /* 19360Sstevel@tonic-gate * Ask the name service for names. 19370Sstevel@tonic-gate */ 19380Sstevel@tonic-gate for (i = 0; i < n; i++) { 19390Sstevel@tonic-gate /* 19400Sstevel@tonic-gate * If name is numeric, ask for numeric id 19410Sstevel@tonic-gate */ 19420Sstevel@tonic-gate if (str2id(utbl[i].name, &uid, 0, UID_MAX) == 0) 19430Sstevel@tonic-gate pwd = getpwuid(uid); 19440Sstevel@tonic-gate else 19450Sstevel@tonic-gate pwd = getpwnam(utbl[i].name); 19460Sstevel@tonic-gate 19470Sstevel@tonic-gate /* 19480Sstevel@tonic-gate * If found, enter found index into tbl array. 19490Sstevel@tonic-gate */ 19500Sstevel@tonic-gate if (pwd == NULL) { 19510Sstevel@tonic-gate (void) fprintf(stderr, 19520Sstevel@tonic-gate gettext("ps: unknown user %s\n"), utbl[i].name); 19530Sstevel@tonic-gate continue; 19540Sstevel@tonic-gate } 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate utbl[fnd].id = pwd->pw_uid; 19570Sstevel@tonic-gate (void) strncpy(utbl[fnd].name, pwd->pw_name, MAXUGNAME); 19580Sstevel@tonic-gate fnd++; 19590Sstevel@tonic-gate } 19600Sstevel@tonic-gate 19610Sstevel@tonic-gate uhead->nent = fnd; /* in case it changed */ 19620Sstevel@tonic-gate return (n - fnd); 19630Sstevel@tonic-gate } 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate static int 19660Sstevel@tonic-gate gconv(struct ughead *ghead) 19670Sstevel@tonic-gate { 19680Sstevel@tonic-gate struct ugdata *gtbl = ghead->ent; 19690Sstevel@tonic-gate int n = ghead->nent; 19700Sstevel@tonic-gate struct group *grp; 19710Sstevel@tonic-gate gid_t gid; 19720Sstevel@tonic-gate int i; 19730Sstevel@tonic-gate int fnd = 0; 19740Sstevel@tonic-gate 19750Sstevel@tonic-gate /* 19760Sstevel@tonic-gate * Ask the name service for names. 19770Sstevel@tonic-gate */ 19780Sstevel@tonic-gate for (i = 0; i < n; i++) { 19790Sstevel@tonic-gate /* 19800Sstevel@tonic-gate * If name is numeric, ask for numeric id 19810Sstevel@tonic-gate */ 19820Sstevel@tonic-gate if (str2id(gtbl[i].name, &gid, 0, UID_MAX) == 0) 19830Sstevel@tonic-gate grp = getgrgid(gid); 19840Sstevel@tonic-gate else 19850Sstevel@tonic-gate grp = getgrnam(gtbl[i].name); 19860Sstevel@tonic-gate /* 19870Sstevel@tonic-gate * If found, enter found index into tbl array. 19880Sstevel@tonic-gate */ 19890Sstevel@tonic-gate if (grp == NULL) { 19900Sstevel@tonic-gate (void) fprintf(stderr, 19910Sstevel@tonic-gate gettext("ps: unknown group %s\n"), gtbl[i].name); 19920Sstevel@tonic-gate continue; 19930Sstevel@tonic-gate } 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate gtbl[fnd].id = grp->gr_gid; 19960Sstevel@tonic-gate (void) strncpy(gtbl[fnd].name, grp->gr_name, MAXUGNAME); 19970Sstevel@tonic-gate fnd++; 19980Sstevel@tonic-gate } 19990Sstevel@tonic-gate 20000Sstevel@tonic-gate ghead->nent = fnd; /* in case it changed */ 20010Sstevel@tonic-gate return (n - fnd); 20020Sstevel@tonic-gate } 20030Sstevel@tonic-gate 20040Sstevel@tonic-gate /* 20050Sstevel@tonic-gate * Return 1 if puid is in table, otherwise 0. 20060Sstevel@tonic-gate */ 20070Sstevel@tonic-gate static int 20080Sstevel@tonic-gate ugfind(id_t id, struct ughead *ughead) 20090Sstevel@tonic-gate { 20100Sstevel@tonic-gate struct ugdata *utbl = ughead->ent; 20110Sstevel@tonic-gate int n = ughead->nent; 20120Sstevel@tonic-gate int i; 20130Sstevel@tonic-gate 20140Sstevel@tonic-gate for (i = 0; i < n; i++) 20150Sstevel@tonic-gate if (utbl[i].id == id) 20160Sstevel@tonic-gate return (1); 20170Sstevel@tonic-gate return (0); 20180Sstevel@tonic-gate } 20190Sstevel@tonic-gate 20200Sstevel@tonic-gate /* 20210Sstevel@tonic-gate * Print starting time of process unless process started more than 24 hours 20220Sstevel@tonic-gate * ago, in which case the date is printed. The date is printed in the form 20230Sstevel@tonic-gate * "MMM dd" if old format, else the blank is replaced with an '_' so 20240Sstevel@tonic-gate * it appears as a single word (for parseability). 20250Sstevel@tonic-gate */ 20260Sstevel@tonic-gate static void 20270Sstevel@tonic-gate prtime(timestruc_t st, int width, int old) 20280Sstevel@tonic-gate { 20290Sstevel@tonic-gate char sttim[26]; 20300Sstevel@tonic-gate time_t starttime; 20310Sstevel@tonic-gate 20320Sstevel@tonic-gate starttime = st.tv_sec; 20330Sstevel@tonic-gate if (st.tv_nsec > 500000000) 20340Sstevel@tonic-gate starttime++; 20350Sstevel@tonic-gate if ((now.tv_sec - starttime) >= 24*60*60) { 20360Sstevel@tonic-gate (void) strftime(sttim, sizeof (sttim), old? \ 20370Sstevel@tonic-gate "%b %d" : "%b_%d", localtime(&starttime)); 20380Sstevel@tonic-gate sttim[7] = '\0'; 20390Sstevel@tonic-gate } else { 20400Sstevel@tonic-gate (void) strftime(sttim, sizeof (sttim), \ 20410Sstevel@tonic-gate "%H:%M:%S", localtime(&starttime)); 20420Sstevel@tonic-gate sttim[8] = '\0'; 20430Sstevel@tonic-gate } 20440Sstevel@tonic-gate (void) printf("%*.*s", width, width, sttim); 20450Sstevel@tonic-gate } 20460Sstevel@tonic-gate 20470Sstevel@tonic-gate static void 20480Sstevel@tonic-gate przom(psinfo_t *psinfo) 20490Sstevel@tonic-gate { 20500Sstevel@tonic-gate long tm; 20510Sstevel@tonic-gate struct passwd *pwd; 20520Sstevel@tonic-gate char zonename[ZONENAME_MAX]; 20530Sstevel@tonic-gate 20540Sstevel@tonic-gate /* 20550Sstevel@tonic-gate * All fields before 'PID' are printed with a trailing space as a 20560Sstevel@tonic-gate * spearator, rather than keeping track of which column is first. All 20570Sstevel@tonic-gate * other fields are printed with a leading space. 20580Sstevel@tonic-gate */ 20590Sstevel@tonic-gate if (lflg) { /* F S */ 20600Sstevel@tonic-gate if (!yflg) 20610Sstevel@tonic-gate (void) printf("%2x ", psinfo->pr_flag & 0377); /* F */ 20620Sstevel@tonic-gate (void) printf("%c ", psinfo->pr_lwp.pr_sname); /* S */ 20630Sstevel@tonic-gate } 20640Sstevel@tonic-gate if (Zflg) { 20650Sstevel@tonic-gate if (getzonenamebyid(psinfo->pr_zoneid, zonename, 20660Sstevel@tonic-gate sizeof (zonename)) < 0) { 20670Sstevel@tonic-gate (void) printf("%7.7d ", ((int)psinfo->pr_zoneid)); 20680Sstevel@tonic-gate } else { 20690Sstevel@tonic-gate (void) printf("%8.8s ", zonename); 20700Sstevel@tonic-gate } 20710Sstevel@tonic-gate } 2072*2685Sakolb if (Hflg) { 2073*2685Sakolb /* Display home lgroup */ 2074*2685Sakolb (void) printf(" %6d", (int)psinfo->pr_lwp.pr_lgrp); /* LGRP */ 2075*2685Sakolb } 20760Sstevel@tonic-gate if (fflg) { 20770Sstevel@tonic-gate if ((pwd = getpwuid(psinfo->pr_euid)) != NULL) 20780Sstevel@tonic-gate (void) printf("%8.8s ", pwd->pw_name); 20790Sstevel@tonic-gate else 20800Sstevel@tonic-gate (void) printf("%7.7d ", (int)psinfo->pr_euid); 20810Sstevel@tonic-gate } else if (lflg) 20820Sstevel@tonic-gate (void) printf("%6d ", (int)psinfo->pr_euid); 20830Sstevel@tonic-gate 20840Sstevel@tonic-gate (void) printf("%*d", pidwidth, (int)psinfo->pr_pid); /* PID */ 20850Sstevel@tonic-gate if (lflg || fflg) 20860Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 20870Sstevel@tonic-gate (int)psinfo->pr_ppid); /* PPID */ 20880Sstevel@tonic-gate 20890Sstevel@tonic-gate if (jflg) { 20900Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 20910Sstevel@tonic-gate (int)psinfo->pr_pgid); /* PGID */ 20920Sstevel@tonic-gate (void) printf(" %*d", pidwidth, 20930Sstevel@tonic-gate (int)psinfo->pr_sid); /* SID */ 20940Sstevel@tonic-gate } 20950Sstevel@tonic-gate 20960Sstevel@tonic-gate if (Lflg) 20970Sstevel@tonic-gate (void) printf(" %5d", 0); /* LWP */ 20980Sstevel@tonic-gate if (Pflg) 20990Sstevel@tonic-gate (void) printf(" -"); /* PSR */ 21000Sstevel@tonic-gate if (Lflg && fflg) 21010Sstevel@tonic-gate (void) printf(" %5d", 0); /* NLWP */ 21020Sstevel@tonic-gate 21030Sstevel@tonic-gate if (cflg) { 21040Sstevel@tonic-gate (void) printf(" %4s", "-"); /* zombies have no class */ 21050Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_pri); /* PRI */ 21060Sstevel@tonic-gate } else if (lflg || fflg) { 21070Sstevel@tonic-gate (void) printf(" %3d", psinfo->pr_lwp.pr_cpu & 0377); /* C */ 21080Sstevel@tonic-gate if (lflg) 21090Sstevel@tonic-gate (void) printf(" %3d %2s", 21100Sstevel@tonic-gate psinfo->pr_lwp.pr_oldpri, "-"); /* PRI NI */ 21110Sstevel@tonic-gate } 21120Sstevel@tonic-gate if (lflg) { 21130Sstevel@tonic-gate if (yflg) /* RSS SZ WCHAN */ 21140Sstevel@tonic-gate (void) printf(" %5d %6d %8s", 0, 0, "-"); 21150Sstevel@tonic-gate else /* ADDR SZ WCHAN */ 21160Sstevel@tonic-gate (void) printf(" %8s %6d %8s", "-", 0, "-"); 21170Sstevel@tonic-gate } 21180Sstevel@tonic-gate if (fflg) 21190Sstevel@tonic-gate (void) printf(" %8.8s", "-"); /* STIME */ 21200Sstevel@tonic-gate (void) printf(" %-8.14s", "?"); /* TTY */ 21210Sstevel@tonic-gate 21220Sstevel@tonic-gate tm = psinfo->pr_time.tv_sec; 21230Sstevel@tonic-gate if (psinfo->pr_time.tv_nsec > 500000000) 21240Sstevel@tonic-gate tm++; 21250Sstevel@tonic-gate (void) printf(" %4ld:%.2ld", tm / 60, tm % 60); /* TIME */ 21260Sstevel@tonic-gate (void) printf(" <defunct>\n"); 21270Sstevel@tonic-gate } 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate /* 21300Sstevel@tonic-gate * Function to compute the number of printable bytes in a multibyte 21310Sstevel@tonic-gate * command string ("internationalization"). 21320Sstevel@tonic-gate */ 21330Sstevel@tonic-gate static int 21340Sstevel@tonic-gate namencnt(char *cmd, int csisize, int scrsize) 21350Sstevel@tonic-gate { 21360Sstevel@tonic-gate int csiwcnt = 0, scrwcnt = 0; 21370Sstevel@tonic-gate int ncsisz, nscrsz; 21380Sstevel@tonic-gate wchar_t wchar; 21390Sstevel@tonic-gate int len; 21400Sstevel@tonic-gate 21410Sstevel@tonic-gate while (*cmd != '\0') { 21420Sstevel@tonic-gate if ((len = csisize - csiwcnt) > (int)MB_CUR_MAX) 21430Sstevel@tonic-gate len = MB_CUR_MAX; 21440Sstevel@tonic-gate if ((ncsisz = mbtowc(&wchar, cmd, len)) < 0) 21450Sstevel@tonic-gate return (8); /* default to use for illegal chars */ 21460Sstevel@tonic-gate if ((nscrsz = wcwidth(wchar)) <= 0) 21470Sstevel@tonic-gate return (8); 21480Sstevel@tonic-gate if (csiwcnt + ncsisz > csisize || scrwcnt + nscrsz > scrsize) 21490Sstevel@tonic-gate break; 21500Sstevel@tonic-gate csiwcnt += ncsisz; 21510Sstevel@tonic-gate scrwcnt += nscrsz; 21520Sstevel@tonic-gate cmd += ncsisz; 21530Sstevel@tonic-gate } 21540Sstevel@tonic-gate return (csiwcnt); 21550Sstevel@tonic-gate } 21560Sstevel@tonic-gate 21570Sstevel@tonic-gate static char * 21580Sstevel@tonic-gate err_string(int err) 21590Sstevel@tonic-gate { 21600Sstevel@tonic-gate static char buf[32]; 21610Sstevel@tonic-gate char *str = strerror(err); 21620Sstevel@tonic-gate 21630Sstevel@tonic-gate if (str == NULL) 21640Sstevel@tonic-gate (void) snprintf(str = buf, sizeof (buf), "Errno #%d", err); 21650Sstevel@tonic-gate 21660Sstevel@tonic-gate return (str); 21670Sstevel@tonic-gate } 21680Sstevel@tonic-gate 21690Sstevel@tonic-gate /* If allocation fails, die */ 21700Sstevel@tonic-gate static void * 21710Sstevel@tonic-gate Realloc(void *ptr, size_t size) 21720Sstevel@tonic-gate { 21730Sstevel@tonic-gate ptr = realloc(ptr, size); 21740Sstevel@tonic-gate if (ptr == NULL) { 21750Sstevel@tonic-gate (void) fprintf(stderr, gettext("ps: no memory\n")); 21760Sstevel@tonic-gate exit(1); 21770Sstevel@tonic-gate } 21780Sstevel@tonic-gate return (ptr); 21790Sstevel@tonic-gate } 21800Sstevel@tonic-gate 21810Sstevel@tonic-gate static time_t 21820Sstevel@tonic-gate delta_secs(const timestruc_t *start) 21830Sstevel@tonic-gate { 21840Sstevel@tonic-gate time_t seconds = now.tv_sec - start->tv_sec; 21850Sstevel@tonic-gate long nanosecs = now.tv_usec * 1000 - start->tv_nsec; 21860Sstevel@tonic-gate 21870Sstevel@tonic-gate if (nanosecs >= (NANOSEC / 2)) 21880Sstevel@tonic-gate seconds++; 21890Sstevel@tonic-gate else if (nanosecs < -(NANOSEC / 2)) 21900Sstevel@tonic-gate seconds--; 21910Sstevel@tonic-gate 21920Sstevel@tonic-gate return (seconds); 21930Sstevel@tonic-gate } 21940Sstevel@tonic-gate 21950Sstevel@tonic-gate /* 21960Sstevel@tonic-gate * Returns the following: 21970Sstevel@tonic-gate * 21980Sstevel@tonic-gate * 0 No error 21990Sstevel@tonic-gate * EINVAL Invalid number 22000Sstevel@tonic-gate * ERANGE Value exceeds (min, max) range 22010Sstevel@tonic-gate */ 22020Sstevel@tonic-gate static int 22030Sstevel@tonic-gate str2id(const char *p, pid_t *val, long min, long max) 22040Sstevel@tonic-gate { 22050Sstevel@tonic-gate char *q; 22060Sstevel@tonic-gate long number; 22070Sstevel@tonic-gate int error; 22080Sstevel@tonic-gate 22090Sstevel@tonic-gate errno = 0; 22100Sstevel@tonic-gate number = strtol(p, &q, 10); 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate if (errno != 0 || q == p || *q != '\0') { 22130Sstevel@tonic-gate if ((error = errno) == 0) { 22140Sstevel@tonic-gate /* 22150Sstevel@tonic-gate * strtol() can fail without setting errno, or it can 22160Sstevel@tonic-gate * set it to EINVAL or ERANGE. In the case errno is 22170Sstevel@tonic-gate * still zero, return EINVAL. 22180Sstevel@tonic-gate */ 22190Sstevel@tonic-gate error = EINVAL; 22200Sstevel@tonic-gate } 22210Sstevel@tonic-gate } else if (number < min || number > max) { 22220Sstevel@tonic-gate error = ERANGE; 22230Sstevel@tonic-gate } else { 22240Sstevel@tonic-gate error = 0; 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate *val = number; 22280Sstevel@tonic-gate 22290Sstevel@tonic-gate return (error); 22300Sstevel@tonic-gate } 22310Sstevel@tonic-gate 22320Sstevel@tonic-gate static int 22330Sstevel@tonic-gate pidcmp(const void *p1, const void *p2) 22340Sstevel@tonic-gate { 22350Sstevel@tonic-gate pid_t i = *((pid_t *)p1); 22360Sstevel@tonic-gate pid_t j = *((pid_t *)p2); 22370Sstevel@tonic-gate 22380Sstevel@tonic-gate return (i - j); 22390Sstevel@tonic-gate } 2240