14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 64b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 74b88c807SRodney W. Grimes * are met: 84b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 94b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 104b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 124b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 134b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 144b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 154b88c807SRodney W. Grimes * without specific prior written permission. 164b88c807SRodney W. Grimes * 174b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 184b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 194b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 204b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 214b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 224b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 234b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 244b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 254b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 264b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274b88c807SRodney W. Grimes * SUCH DAMAGE. 28a4c8a745SGarance A Drosehn * ------+---------+---------+-------- + --------+---------+---------+---------* 29a4c8a745SGarance A Drosehn * Copyright (c) 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 30a4c8a745SGarance A Drosehn * All rights reserved. 31a4c8a745SGarance A Drosehn * 32a4c8a745SGarance A Drosehn * Significant modifications made to bring `ps' options somewhat closer 33a4c8a745SGarance A Drosehn * to the standard for `ps' as described in SingleUnixSpec-v3. 34a4c8a745SGarance A Drosehn * ------+---------+---------+-------- + --------+---------+---------+---------* 354b88c807SRodney W. Grimes */ 364b88c807SRodney W. Grimes 374b88c807SRodney W. Grimes #ifndef lint 38871e8d8cSMark Murray static const char copyright[] = 394b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 404b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 43c9a8d1f4SPhilippe Charnier #if 0 44871e8d8cSMark Murray #ifndef lint 45c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 464b88c807SRodney W. Grimes #endif /* not lint */ 47871e8d8cSMark Murray #endif 48eaed5652SPhilippe Charnier 492749b141SDavid E. O'Brien #include <sys/cdefs.h> 502749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes #include <sys/param.h> 53c7910c3aSGarance A Drosehn #include <sys/proc.h> 548b6f5f5fSDavid Greenman #include <sys/user.h> 554b88c807SRodney W. Grimes #include <sys/stat.h> 564b88c807SRodney W. Grimes #include <sys/ioctl.h> 574b88c807SRodney W. Grimes #include <sys/sysctl.h> 584b88c807SRodney W. Grimes 594b88c807SRodney W. Grimes #include <ctype.h> 604b88c807SRodney W. Grimes #include <err.h> 614e8b6a6fSGarance A Drosehn #include <errno.h> 624b88c807SRodney W. Grimes #include <fcntl.h> 63a4c8a745SGarance A Drosehn #include <grp.h> 644b88c807SRodney W. Grimes #include <kvm.h> 65b2496d93SMike Pritchard #include <limits.h> 6608017519SAndrey A. Chernov #include <locale.h> 674b88c807SRodney W. Grimes #include <paths.h> 68871e8d8cSMark Murray #include <pwd.h> 694b88c807SRodney W. Grimes #include <stdio.h> 704b88c807SRodney W. Grimes #include <stdlib.h> 714b88c807SRodney W. Grimes #include <string.h> 724b88c807SRodney W. Grimes #include <unistd.h> 734b88c807SRodney W. Grimes 744b88c807SRodney W. Grimes #include "ps.h" 754b88c807SRodney W. Grimes 76a4c8a745SGarance A Drosehn #define W_SEP " \t" /* "Whitespace" list separators */ 77a4c8a745SGarance A Drosehn #define T_SEP "," /* "Terminate-element" list separators */ 78cf22dcfcSBrian Somers 79de800cd4SGarance A Drosehn #ifdef LAZY_PS 80c46bb4b3SGarance A Drosehn #define DEF_UREAD 0 81de800cd4SGarance A Drosehn #define OPT_LAZY_f "f" 82de800cd4SGarance A Drosehn #else 83c46bb4b3SGarance A Drosehn #define DEF_UREAD 1 /* Always do the more-expensive read. */ 84de800cd4SGarance A Drosehn #define OPT_LAZY_f /* I.e., the `-f' option is not added. */ 85de800cd4SGarance A Drosehn #endif 864b88c807SRodney W. Grimes 87c675340aSGarance A Drosehn /* 88ecbb06f8SGarance A Drosehn * isdigit takes an `int', but expects values in the range of unsigned char. 89ecbb06f8SGarance A Drosehn * This wrapper ensures that values from a 'char' end up in the correct range. 90c675340aSGarance A Drosehn */ 91ecbb06f8SGarance A Drosehn #define isdigitch(Anychar) isdigit((u_char)(Anychar)) 92c675340aSGarance A Drosehn 93db91faacSPeter Wemm int cflag; /* -c */ 94de800cd4SGarance A Drosehn int eval; /* Exit value */ 95de800cd4SGarance A Drosehn time_t now; /* Current time(3) value */ 964b88c807SRodney W. Grimes int rawcpu; /* -C */ 974b88c807SRodney W. Grimes int sumrusage; /* -S */ 98de800cd4SGarance A Drosehn int termwidth; /* Width of the screen (0 == infinity). */ 99de800cd4SGarance A Drosehn int totwidth; /* Calculated-width of requested variables. */ 1004b88c807SRodney W. Grimes 101bdf8ab46SGarance A Drosehn struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); 102de800cd4SGarance A Drosehn 103de800cd4SGarance A Drosehn static int forceuread = DEF_UREAD; /* Do extra work to get u-area. */ 104de800cd4SGarance A Drosehn static kvm_t *kd; 105de800cd4SGarance A Drosehn static KINFO *kinfo; 106de800cd4SGarance A Drosehn static int needcomm; /* -o "command" */ 107de800cd4SGarance A Drosehn static int needenv; /* -e */ 108de800cd4SGarance A Drosehn static int needuser; /* -o "user" */ 109de800cd4SGarance A Drosehn static int optfatal; /* Fatal error parsing some list-option. */ 110de800cd4SGarance A Drosehn 111de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 112ba2cd770SJuli Mallett 113a4c8a745SGarance A Drosehn struct listinfo; 114de800cd4SGarance A Drosehn typedef int addelem_rtn(struct listinfo *_inf, const char *_elem); 115a4c8a745SGarance A Drosehn 116a4c8a745SGarance A Drosehn struct listinfo { 117a4c8a745SGarance A Drosehn int count; 118a4c8a745SGarance A Drosehn int maxcount; 119a4c8a745SGarance A Drosehn int elemsize; 120a4c8a745SGarance A Drosehn addelem_rtn *addelem; 121a4c8a745SGarance A Drosehn const char *lname; 122a4c8a745SGarance A Drosehn union { 123a4c8a745SGarance A Drosehn gid_t *gids; 124a4c8a745SGarance A Drosehn pid_t *pids; 125a4c8a745SGarance A Drosehn dev_t *ttys; 126a4c8a745SGarance A Drosehn uid_t *uids; 127a4c8a745SGarance A Drosehn void *ptr; 128d822163fSGarance A Drosehn } l; 129a4c8a745SGarance A Drosehn }; 130a4c8a745SGarance A Drosehn 131a4c8a745SGarance A Drosehn static int addelem_gid(struct listinfo *, const char *); 132a4c8a745SGarance A Drosehn static int addelem_pid(struct listinfo *, const char *); 133a4c8a745SGarance A Drosehn static int addelem_tty(struct listinfo *, const char *); 134a4c8a745SGarance A Drosehn static int addelem_uid(struct listinfo *, const char *); 135a4c8a745SGarance A Drosehn static void add_list(struct listinfo *, const char *); 1364857f240SGarance A Drosehn static void dynsizevars(KINFO *); 137a4c8a745SGarance A Drosehn static void *expand_list(struct listinfo *); 138f35e0715SGarance A Drosehn static const char * 139f35e0715SGarance A Drosehn fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 1404857f240SGarance A Drosehn KINFO *, char *, int); 141a4c8a745SGarance A Drosehn static void free_list(struct listinfo *); 142a4c8a745SGarance A Drosehn static void init_list(struct listinfo *, addelem_rtn, int, const char *); 14325113083SGarance A Drosehn static char *kludge_oldps_options(const char *, char *, const char *); 1444857f240SGarance A Drosehn static int pscomp(const void *, const void *); 1454857f240SGarance A Drosehn static void saveuser(KINFO *); 1464857f240SGarance A Drosehn static void scanvars(void); 1474857f240SGarance A Drosehn static void sizevars(void); 1484857f240SGarance A Drosehn static void usage(void); 1494b88c807SRodney W. Grimes 150c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command"; 151259fcfacSGarance A Drosehn static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command"; 1521d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state," 1531d2324f4SGarance A Drosehn "tt,time,command"; 154871e8d8cSMark Murray static char o1[] = "pid"; 155c0716492SJuli Mallett static char o2[] = "tt,state,time,command"; 156c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command"; 1571d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz," 1581d2324f4SGarance A Drosehn "%cpu,%mem,command"; 1592af538ebSRobert Watson static char Zfmt[] = "label"; 1604b88c807SRodney W. Grimes 1619bf69950SGarance A Drosehn #define PS_ARGS "AaCce" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ" 16241623b2dSMaxim Sobolev 1634b88c807SRodney W. Grimes int 16446251ddeSWarner Losh main(int argc, char *argv[]) 1654b88c807SRodney W. Grimes { 166a4c8a745SGarance A Drosehn struct listinfo gidlist, pgrplist, pidlist; 167a4c8a745SGarance A Drosehn struct listinfo ruidlist, sesslist, ttylist, uidlist; 1684b88c807SRodney W. Grimes struct kinfo_proc *kp; 1694bac4483SGarance A Drosehn KINFO *next_KINFO; 1704b88c807SRodney W. Grimes struct varent *vent; 1714b88c807SRodney W. Grimes struct winsize ws; 172c675340aSGarance A Drosehn const char *nlistf, *memf; 173ca62e195SGarance A Drosehn char *cols; 174a4c8a745SGarance A Drosehn int all, ch, dropgid, elem, flag, _fmt, i, lineno; 175c675340aSGarance A Drosehn int nentries, nkept, nselectors; 176a4c8a745SGarance A Drosehn int prtheader, showthreads, wflag, what, xkeep, xkeep_implied; 177871e8d8cSMark Murray char errbuf[_POSIX2_LINE_MAX]; 1784b88c807SRodney W. Grimes 1792bf4b9cfSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 180352b6523SGarance A Drosehn time(&now); /* Used by routines in print.c. */ 1812bf4b9cfSAndrey A. Chernov 1824f18100dSTim J. Robbins if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') 1834f18100dSTim J. Robbins termwidth = atoi(cols); 1844f18100dSTim J. Robbins else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1854b88c807SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1864b88c807SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1874b88c807SRodney W. Grimes ws.ws_col == 0) 1884b88c807SRodney W. Grimes termwidth = 79; 1894b88c807SRodney W. Grimes else 1904b88c807SRodney W. Grimes termwidth = ws.ws_col - 1; 1914b88c807SRodney W. Grimes 19241623b2dSMaxim Sobolev /* 193c675340aSGarance A Drosehn * Hide a number of option-processing kludges in a separate routine, 194c675340aSGarance A Drosehn * to support some historical BSD behaviors, such as `ps axu'. 19541623b2dSMaxim Sobolev */ 196c675340aSGarance A Drosehn if (argc > 1) 197bf46a3bfSGarance A Drosehn argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]); 1984b88c807SRodney W. Grimes 199352b6523SGarance A Drosehn all = dropgid = _fmt = nselectors = optfatal = 0; 200352b6523SGarance A Drosehn prtheader = showthreads = wflag = xkeep_implied = 0; 201352b6523SGarance A Drosehn xkeep = -1; /* Neither -x nor -X. */ 202a4c8a745SGarance A Drosehn init_list(&gidlist, addelem_gid, sizeof(gid_t), "group"); 203a4c8a745SGarance A Drosehn init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group"); 204a4c8a745SGarance A Drosehn init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id"); 205a4c8a745SGarance A Drosehn init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser"); 206a4c8a745SGarance A Drosehn init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id"); 207a4c8a745SGarance A Drosehn init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty"); 208a4c8a745SGarance A Drosehn init_list(&uidlist, addelem_uid, sizeof(uid_t), "user"); 209831c910aSRuslan Ermilov memf = nlistf = _PATH_DEVNULL; 21041623b2dSMaxim Sobolev while ((ch = getopt(argc, argv, PS_ARGS)) != -1) 2114b88c807SRodney W. Grimes switch ((char)ch) { 212a4c8a745SGarance A Drosehn case 'A': 213a4c8a745SGarance A Drosehn /* 214a4c8a745SGarance A Drosehn * Exactly the same as `-ax'. This has been 215a4c8a745SGarance A Drosehn * added for compatability with SUSv3, but for 216a4c8a745SGarance A Drosehn * now it will not be described in the man page. 217a4c8a745SGarance A Drosehn */ 218a4c8a745SGarance A Drosehn nselectors++; 219a4c8a745SGarance A Drosehn all = xkeep = 1; 220a4c8a745SGarance A Drosehn break; 2214b88c807SRodney W. Grimes case 'a': 222a4c8a745SGarance A Drosehn nselectors++; 2234b88c807SRodney W. Grimes all = 1; 2244b88c807SRodney W. Grimes break; 2254b88c807SRodney W. Grimes case 'C': 2264b88c807SRodney W. Grimes rawcpu = 1; 2274b88c807SRodney W. Grimes break; 228db91faacSPeter Wemm case 'c': 229db91faacSPeter Wemm cflag = 1; 230db91faacSPeter Wemm break; 231db91faacSPeter Wemm case 'e': /* XXX set ufmt */ 232db91faacSPeter Wemm needenv = 1; 233db91faacSPeter Wemm break; 2344a355d17SGarance A Drosehn #ifdef LAZY_PS 2354a355d17SGarance A Drosehn case 'f': 2364a355d17SGarance A Drosehn if (getuid() == 0 || getgid() == 0) 2374a355d17SGarance A Drosehn forceuread = 1; 2384a355d17SGarance A Drosehn break; 2394a355d17SGarance A Drosehn #endif 240a4c8a745SGarance A Drosehn case 'G': 241a4c8a745SGarance A Drosehn add_list(&gidlist, optarg); 242a4c8a745SGarance A Drosehn xkeep_implied = 1; 243a4c8a745SGarance A Drosehn nselectors++; 244a4c8a745SGarance A Drosehn break; 2454b88c807SRodney W. Grimes case 'g': 246352b6523SGarance A Drosehn #if 0 247ba50b0e0SGarance A Drosehn /*- 248352b6523SGarance A Drosehn * XXX - This SUSv3 behavior is still under debate 249352b6523SGarance A Drosehn * since it conflicts with the (undocumented) 250352b6523SGarance A Drosehn * `-g' option. So we skip it for now. 251352b6523SGarance A Drosehn */ 252a4c8a745SGarance A Drosehn add_list(&pgrplist, optarg); 253a4c8a745SGarance A Drosehn xkeep_implied = 1; 254a4c8a745SGarance A Drosehn nselectors++; 255a4c8a745SGarance A Drosehn break; 256a4c8a745SGarance A Drosehn #else 257352b6523SGarance A Drosehn /* The historical BSD-ish (from SunOS) behavior. */ 2584b88c807SRodney W. Grimes break; /* no-op */ 259a4c8a745SGarance A Drosehn #endif 26048b8c0deSScott Long case 'H': 261d75c1d83SDaniel Eischen showthreads = KERN_PROC_INC_THREAD; 26248b8c0deSScott Long break; 2634b88c807SRodney W. Grimes case 'h': 2644b88c807SRodney W. Grimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 2654b88c807SRodney W. Grimes break; 2664b88c807SRodney W. Grimes case 'j': 267fde411d5SJuli Mallett parsefmt(jfmt, 0); 268871e8d8cSMark Murray _fmt = 1; 2694b88c807SRodney W. Grimes jfmt[0] = '\0'; 2704b88c807SRodney W. Grimes break; 2714b88c807SRodney W. Grimes case 'L': 2724b88c807SRodney W. Grimes showkey(); 2734b88c807SRodney W. Grimes exit(0); 2744b88c807SRodney W. Grimes case 'l': 275fde411d5SJuli Mallett parsefmt(lfmt, 0); 276871e8d8cSMark Murray _fmt = 1; 2774b88c807SRodney W. Grimes lfmt[0] = '\0'; 2784b88c807SRodney W. Grimes break; 2794b88c807SRodney W. Grimes case 'M': 2804b88c807SRodney W. Grimes memf = optarg; 28185082fc3SPoul-Henning Kamp dropgid = 1; 2824b88c807SRodney W. Grimes break; 2834b88c807SRodney W. Grimes case 'm': 2844b88c807SRodney W. Grimes sortby = SORTMEM; 2854b88c807SRodney W. Grimes break; 2864b88c807SRodney W. Grimes case 'N': 2874b88c807SRodney W. Grimes nlistf = optarg; 28885082fc3SPoul-Henning Kamp dropgid = 1; 2894b88c807SRodney W. Grimes break; 2904b88c807SRodney W. Grimes case 'O': 291fde411d5SJuli Mallett parsefmt(o1, 1); 292fde411d5SJuli Mallett parsefmt(optarg, 1); 293fde411d5SJuli Mallett parsefmt(o2, 1); 2944b88c807SRodney W. Grimes o1[0] = o2[0] = '\0'; 295871e8d8cSMark Murray _fmt = 1; 2964b88c807SRodney W. Grimes break; 2974b88c807SRodney W. Grimes case 'o': 298fde411d5SJuli Mallett parsefmt(optarg, 1); 299871e8d8cSMark Murray _fmt = 1; 3004b88c807SRodney W. Grimes break; 3014b88c807SRodney W. Grimes case 'p': 302a4c8a745SGarance A Drosehn add_list(&pidlist, optarg); 303a4c8a745SGarance A Drosehn /* 304a4c8a745SGarance A Drosehn * Note: `-p' does not *set* xkeep, but any values 305a4c8a745SGarance A Drosehn * from pidlist are checked before xkeep is. That 306a4c8a745SGarance A Drosehn * way they are always matched, even if the user 307a4c8a745SGarance A Drosehn * specifies `-X'. 308a4c8a745SGarance A Drosehn */ 309a4c8a745SGarance A Drosehn nselectors++; 3104b88c807SRodney W. Grimes break; 311a4c8a745SGarance A Drosehn #if 0 312a4c8a745SGarance A Drosehn case 'R': 313ba50b0e0SGarance A Drosehn /*- 314352b6523SGarance A Drosehn * XXX - This un-standard option is still under 315352b6523SGarance A Drosehn * debate. This is what SUSv3 defines as 316352b6523SGarance A Drosehn * the `-U' option, and while it would be 317352b6523SGarance A Drosehn * nice to have, it could cause even more 318352b6523SGarance A Drosehn * confusion to implement it as `-R'. 319352b6523SGarance A Drosehn */ 320a4c8a745SGarance A Drosehn add_list(&ruidlist, optarg); 321a4c8a745SGarance A Drosehn xkeep_implied = 1; 322a4c8a745SGarance A Drosehn nselectors++; 323a4c8a745SGarance A Drosehn break; 324a4c8a745SGarance A Drosehn #endif 3254b88c807SRodney W. Grimes case 'r': 3264b88c807SRodney W. Grimes sortby = SORTCPU; 3274b88c807SRodney W. Grimes break; 3284b88c807SRodney W. Grimes case 'S': 3294b88c807SRodney W. Grimes sumrusage = 1; 3304b88c807SRodney W. Grimes break; 331a4c8a745SGarance A Drosehn #if 0 332a4c8a745SGarance A Drosehn case 's': 333ba50b0e0SGarance A Drosehn /*- 334352b6523SGarance A Drosehn * XXX - This non-standard option is still under 335352b6523SGarance A Drosehn * debate. This *is* supported on Solaris, 336352b6523SGarance A Drosehn * Linux, and IRIX, but conflicts with `-s' 337352b6523SGarance A Drosehn * on NetBSD and maybe some older BSD's. 338352b6523SGarance A Drosehn */ 339a4c8a745SGarance A Drosehn add_list(&sesslist, optarg); 340a4c8a745SGarance A Drosehn xkeep_implied = 1; 341a4c8a745SGarance A Drosehn nselectors++; 342a4c8a745SGarance A Drosehn break; 343a4c8a745SGarance A Drosehn #endif 3444b88c807SRodney W. Grimes case 'T': 3454b88c807SRodney W. Grimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 3464b88c807SRodney W. Grimes errx(1, "stdin: not a terminal"); 3474b88c807SRodney W. Grimes /* FALLTHROUGH */ 348a4c8a745SGarance A Drosehn case 't': 349a4c8a745SGarance A Drosehn add_list(&ttylist, optarg); 350a4c8a745SGarance A Drosehn xkeep_implied = 1; 351a4c8a745SGarance A Drosehn nselectors++; 3524b88c807SRodney W. Grimes break; 35373eb8310SPeter Wemm case 'U': 354a4c8a745SGarance A Drosehn /* This is what SUSv3 defines as the `-u' option. */ 355a4c8a745SGarance A Drosehn add_list(&uidlist, optarg); 356a4c8a745SGarance A Drosehn xkeep_implied = 1; 357a4c8a745SGarance A Drosehn nselectors++; 35873eb8310SPeter Wemm break; 3594b88c807SRodney W. Grimes case 'u': 360fde411d5SJuli Mallett parsefmt(ufmt, 0); 3614b88c807SRodney W. Grimes sortby = SORTCPU; 362871e8d8cSMark Murray _fmt = 1; 3634b88c807SRodney W. Grimes ufmt[0] = '\0'; 3644b88c807SRodney W. Grimes break; 3654b88c807SRodney W. Grimes case 'v': 366fde411d5SJuli Mallett parsefmt(vfmt, 0); 3674b88c807SRodney W. Grimes sortby = SORTMEM; 368871e8d8cSMark Murray _fmt = 1; 3694b88c807SRodney W. Grimes vfmt[0] = '\0'; 3704b88c807SRodney W. Grimes break; 3714b88c807SRodney W. Grimes case 'w': 3724b88c807SRodney W. Grimes if (wflag) 3734b88c807SRodney W. Grimes termwidth = UNLIMITED; 3744b88c807SRodney W. Grimes else if (termwidth < 131) 3754b88c807SRodney W. Grimes termwidth = 131; 3764b88c807SRodney W. Grimes wflag++; 3774b88c807SRodney W. Grimes break; 378a4c8a745SGarance A Drosehn case 'X': 379a4c8a745SGarance A Drosehn /* 380a4c8a745SGarance A Drosehn * Note that `-X' and `-x' are not standard "selector" 381a4c8a745SGarance A Drosehn * options. For most selector-options, we check *all* 382a4c8a745SGarance A Drosehn * processes to see if any are matched by the given 383a4c8a745SGarance A Drosehn * value(s). After we have a set of all the matched 384a4c8a745SGarance A Drosehn * processes, then `-X' and `-x' govern whether we 385a4c8a745SGarance A Drosehn * modify that *matched* set for processes which do 386a4c8a745SGarance A Drosehn * not have a controlling terminal. `-X' causes 387a4c8a745SGarance A Drosehn * those processes to be deleted from the matched 388a4c8a745SGarance A Drosehn * set, while `-x' causes them to be kept. 389a4c8a745SGarance A Drosehn */ 390a4c8a745SGarance A Drosehn xkeep = 0; 391a4c8a745SGarance A Drosehn break; 3924b88c807SRodney W. Grimes case 'x': 393a4c8a745SGarance A Drosehn xkeep = 1; 3944b88c807SRodney W. Grimes break; 3957304f61fSBrian Feldman case 'Z': 396fde411d5SJuli Mallett parsefmt(Zfmt, 0); 3977304f61fSBrian Feldman Zfmt[0] = '\0'; 3987304f61fSBrian Feldman break; 3994b88c807SRodney W. Grimes case '?': 4004b88c807SRodney W. Grimes default: 4014b88c807SRodney W. Grimes usage(); 4024b88c807SRodney W. Grimes } 4034b88c807SRodney W. Grimes argc -= optind; 4044b88c807SRodney W. Grimes argv += optind; 405c675340aSGarance A Drosehn 406c675340aSGarance A Drosehn /* 407c675340aSGarance A Drosehn * If there arguments after processing all the options, attempt 408c675340aSGarance A Drosehn * to treat them as a list of process ids. 409c675340aSGarance A Drosehn */ 410c675340aSGarance A Drosehn while (*argv) { 411c675340aSGarance A Drosehn if (!isdigitch(**argv)) 412c675340aSGarance A Drosehn break; 413c675340aSGarance A Drosehn add_list(&pidlist, *argv); 414c675340aSGarance A Drosehn argv++; 415c675340aSGarance A Drosehn } 416c675340aSGarance A Drosehn if (*argv) { 417c675340aSGarance A Drosehn fprintf(stderr, "%s: illegal argument: %s\n", 418c675340aSGarance A Drosehn getprogname(), *argv); 419c675340aSGarance A Drosehn usage(); 420c675340aSGarance A Drosehn } 421a4c8a745SGarance A Drosehn if (optfatal) 422352b6523SGarance A Drosehn exit(1); /* Error messages already printed. */ 423352b6523SGarance A Drosehn if (xkeep < 0) /* Neither -X nor -x was specified. */ 424a4c8a745SGarance A Drosehn xkeep = xkeep_implied; 425a4c8a745SGarance A Drosehn 426c675340aSGarance A Drosehn 4274b88c807SRodney W. Grimes /* 4284b88c807SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 4294b88c807SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 4304b88c807SRodney W. Grimes */ 431c23b00b7SGarance A Drosehn if (dropgid) 4324b88c807SRodney W. Grimes setgid(getgid()); 4334b88c807SRodney W. Grimes 434831c910aSRuslan Ermilov kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 4354b88c807SRodney W. Grimes if (kd == 0) 4364b88c807SRodney W. Grimes errx(1, "%s", errbuf); 4374b88c807SRodney W. Grimes 438871e8d8cSMark Murray if (!_fmt) 439fde411d5SJuli Mallett parsefmt(dfmt, 0); 4404b88c807SRodney W. Grimes 441a4c8a745SGarance A Drosehn if (nselectors == 0) { 442d822163fSGarance A Drosehn uidlist.l.ptr = malloc(sizeof(uid_t)); 443d822163fSGarance A Drosehn if (uidlist.l.ptr == NULL) 4444fa7d788SJuli Mallett errx(1, "malloc failed"); 445a4c8a745SGarance A Drosehn nselectors = 1; 446a4c8a745SGarance A Drosehn uidlist.count = uidlist.maxcount = 1; 447d822163fSGarance A Drosehn *uidlist.l.uids = getuid(); 448cf22dcfcSBrian Somers } 4494b88c807SRodney W. Grimes 4504b88c807SRodney W. Grimes /* 4514b88c807SRodney W. Grimes * scan requested variables, noting what structures are needed, 452bdfebd84SKris Kennaway * and adjusting header widths as appropriate. 4534b88c807SRodney W. Grimes */ 4544b88c807SRodney W. Grimes scanvars(); 455a4c8a745SGarance A Drosehn 4564b88c807SRodney W. Grimes /* 457a4c8a745SGarance A Drosehn * Get process list. If the user requested just one selector- 458a4c8a745SGarance A Drosehn * option, then kvm_getprocs can be asked to return just those 459a4c8a745SGarance A Drosehn * processes. Otherwise, have it return all processes, and 460a4c8a745SGarance A Drosehn * then this routine will search that full list and select the 461a4c8a745SGarance A Drosehn * processes which match any of the user's selector-options. 4624b88c807SRodney W. Grimes */ 463d75c1d83SDaniel Eischen what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC; 46448b8c0deSScott Long flag = 0; 465a4c8a745SGarance A Drosehn if (nselectors == 1) { 4667bd42165SGarance A Drosehn if (gidlist.count == 1) { 4677bd42165SGarance A Drosehn what = KERN_PROC_RGID | showthreads; 4687bd42165SGarance A Drosehn flag = *gidlist.l.gids; 4697bd42165SGarance A Drosehn nselectors = 0; 4707bd42165SGarance A Drosehn } else if (pgrplist.count == 1) { 471a4c8a745SGarance A Drosehn what = KERN_PROC_PGRP | showthreads; 472d822163fSGarance A Drosehn flag = *pgrplist.l.pids; 473a4c8a745SGarance A Drosehn nselectors = 0; 474a4c8a745SGarance A Drosehn } else if (pidlist.count == 1) { 475a4c8a745SGarance A Drosehn what = KERN_PROC_PID | showthreads; 476d822163fSGarance A Drosehn flag = *pidlist.l.pids; 477a4c8a745SGarance A Drosehn nselectors = 0; 478a4c8a745SGarance A Drosehn } else if (ruidlist.count == 1) { 479a4c8a745SGarance A Drosehn what = KERN_PROC_RUID | showthreads; 480d822163fSGarance A Drosehn flag = *ruidlist.l.uids; 481a4c8a745SGarance A Drosehn nselectors = 0; 482a4c8a745SGarance A Drosehn } else if (sesslist.count == 1) { 483a4c8a745SGarance A Drosehn what = KERN_PROC_SESSION | showthreads; 484d822163fSGarance A Drosehn flag = *sesslist.l.pids; 485a4c8a745SGarance A Drosehn nselectors = 0; 486a4c8a745SGarance A Drosehn } else if (ttylist.count == 1) { 487a4c8a745SGarance A Drosehn what = KERN_PROC_TTY | showthreads; 488d822163fSGarance A Drosehn flag = *ttylist.l.ttys; 489a4c8a745SGarance A Drosehn nselectors = 0; 490a4c8a745SGarance A Drosehn } else if (uidlist.count == 1) { 491a4c8a745SGarance A Drosehn what = KERN_PROC_UID | showthreads; 492d822163fSGarance A Drosehn flag = *uidlist.l.uids; 493a4c8a745SGarance A Drosehn nselectors = 0; 494a4c8a745SGarance A Drosehn } else if (all) { 495a4c8a745SGarance A Drosehn /* No need for this routine to select processes. */ 496a4c8a745SGarance A Drosehn nselectors = 0; 497a4c8a745SGarance A Drosehn } 4984b88c807SRodney W. Grimes } 499d75c1d83SDaniel Eischen 5004b88c807SRodney W. Grimes /* 5014b88c807SRodney W. Grimes * select procs 5024b88c807SRodney W. Grimes */ 503a4c8a745SGarance A Drosehn nentries = -1; 5044e8b6a6fSGarance A Drosehn kp = kvm_getprocs(kd, what, flag, &nentries); 50587e47750SGarance A Drosehn if ((kp == NULL && nentries > 0) || (kp != NULL && nentries < 0)) 5064b88c807SRodney W. Grimes errx(1, "%s", kvm_geterr(kd)); 507a4c8a745SGarance A Drosehn nkept = 0; 5084e8b6a6fSGarance A Drosehn if (nentries > 0) { 5094b88c807SRodney W. Grimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 5104fa7d788SJuli Mallett errx(1, "malloc failed"); 5114b88c807SRodney W. Grimes for (i = nentries; --i >= 0; ++kp) { 512a4c8a745SGarance A Drosehn /* 513a4c8a745SGarance A Drosehn * If the user specified multiple selection-criteria, 514a4c8a745SGarance A Drosehn * then keep any process matched by the inclusive OR 515a4c8a745SGarance A Drosehn * of all the selection-criteria given. 516a4c8a745SGarance A Drosehn */ 517a4c8a745SGarance A Drosehn if (pidlist.count > 0) { 518a4c8a745SGarance A Drosehn for (elem = 0; elem < pidlist.count; elem++) 519d822163fSGarance A Drosehn if (kp->ki_pid == pidlist.l.pids[elem]) 520a4c8a745SGarance A Drosehn goto keepit; 521a4c8a745SGarance A Drosehn } 522a4c8a745SGarance A Drosehn /* 523a4c8a745SGarance A Drosehn * Note that we had to process pidlist before 524a4c8a745SGarance A Drosehn * filtering out processes which do not have 525a4c8a745SGarance A Drosehn * a controlling terminal. 526a4c8a745SGarance A Drosehn */ 527a4c8a745SGarance A Drosehn if (xkeep == 0) { 528a4c8a745SGarance A Drosehn if ((kp->ki_tdev == NODEV || 529a4c8a745SGarance A Drosehn (kp->ki_flag & P_CONTROLT) == 0)) 530a4c8a745SGarance A Drosehn continue; 531a4c8a745SGarance A Drosehn } 532a4c8a745SGarance A Drosehn if (nselectors == 0) 533a4c8a745SGarance A Drosehn goto keepit; 534a4c8a745SGarance A Drosehn if (gidlist.count > 0) { 535a4c8a745SGarance A Drosehn for (elem = 0; elem < gidlist.count; elem++) 536d822163fSGarance A Drosehn if (kp->ki_rgid == gidlist.l.gids[elem]) 537a4c8a745SGarance A Drosehn goto keepit; 538a4c8a745SGarance A Drosehn } 539a4c8a745SGarance A Drosehn if (pgrplist.count > 0) { 540a4c8a745SGarance A Drosehn for (elem = 0; elem < pgrplist.count; elem++) 541d822163fSGarance A Drosehn if (kp->ki_pgid == 542d822163fSGarance A Drosehn pgrplist.l.pids[elem]) 543a4c8a745SGarance A Drosehn goto keepit; 544a4c8a745SGarance A Drosehn } 545a4c8a745SGarance A Drosehn if (ruidlist.count > 0) { 546a4c8a745SGarance A Drosehn for (elem = 0; elem < ruidlist.count; elem++) 547d822163fSGarance A Drosehn if (kp->ki_ruid == 548d822163fSGarance A Drosehn ruidlist.l.uids[elem]) 549a4c8a745SGarance A Drosehn goto keepit; 550a4c8a745SGarance A Drosehn } 551a4c8a745SGarance A Drosehn if (sesslist.count > 0) { 552a4c8a745SGarance A Drosehn for (elem = 0; elem < sesslist.count; elem++) 553d822163fSGarance A Drosehn if (kp->ki_sid == sesslist.l.pids[elem]) 554a4c8a745SGarance A Drosehn goto keepit; 555a4c8a745SGarance A Drosehn } 556a4c8a745SGarance A Drosehn if (ttylist.count > 0) { 557a4c8a745SGarance A Drosehn for (elem = 0; elem < ttylist.count; elem++) 558d822163fSGarance A Drosehn if (kp->ki_tdev == ttylist.l.ttys[elem]) 559a4c8a745SGarance A Drosehn goto keepit; 560a4c8a745SGarance A Drosehn } 561a4c8a745SGarance A Drosehn if (uidlist.count > 0) { 562a4c8a745SGarance A Drosehn for (elem = 0; elem < uidlist.count; elem++) 563d822163fSGarance A Drosehn if (kp->ki_uid == uidlist.l.uids[elem]) 564a4c8a745SGarance A Drosehn goto keepit; 565a4c8a745SGarance A Drosehn } 566a4c8a745SGarance A Drosehn /* 567a4c8a745SGarance A Drosehn * This process did not match any of the user's 568a4c8a745SGarance A Drosehn * selector-options, so skip the process. 569a4c8a745SGarance A Drosehn */ 570a4c8a745SGarance A Drosehn continue; 571a4c8a745SGarance A Drosehn 572a4c8a745SGarance A Drosehn keepit: 5734bac4483SGarance A Drosehn next_KINFO = &kinfo[nkept]; 5744bac4483SGarance A Drosehn next_KINFO->ki_p = kp; 5754bac4483SGarance A Drosehn next_KINFO->ki_pcpu = getpcpu(next_KINFO); 5764bac4483SGarance A Drosehn if (sortby == SORTMEM) 5774bac4483SGarance A Drosehn next_KINFO->ki_memsize = kp->ki_tsize + 5784bac4483SGarance A Drosehn kp->ki_dsize + kp->ki_ssize; 5794b88c807SRodney W. Grimes if (needuser) 5804bac4483SGarance A Drosehn saveuser(next_KINFO); 5814bac4483SGarance A Drosehn dynsizevars(next_KINFO); 582a4c8a745SGarance A Drosehn nkept++; 5834b88c807SRodney W. Grimes } 5844e8b6a6fSGarance A Drosehn } 5856a2d726bSJordan K. Hubbard 5866a2d726bSJordan K. Hubbard sizevars(); 5876a2d726bSJordan K. Hubbard 5884b88c807SRodney W. Grimes /* 5894b88c807SRodney W. Grimes * print header 5904b88c807SRodney W. Grimes */ 5914b88c807SRodney W. Grimes printheader(); 592a4c8a745SGarance A Drosehn if (nkept == 0) 593f8c9c11cSWill Andrews exit(1); 594a4c8a745SGarance A Drosehn 5954b88c807SRodney W. Grimes /* 5964b88c807SRodney W. Grimes * sort proc list 5974b88c807SRodney W. Grimes */ 598a4c8a745SGarance A Drosehn qsort(kinfo, nkept, sizeof(KINFO), pscomp); 5994b88c807SRodney W. Grimes /* 600a4c8a745SGarance A Drosehn * For each process, call each variable output function. 6014b88c807SRodney W. Grimes */ 602a4c8a745SGarance A Drosehn for (i = lineno = 0; i < nkept; i++) { 603bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 6044b88c807SRodney W. Grimes (vent->var->oproc)(&kinfo[i], vent); 605bdf8ab46SGarance A Drosehn if (STAILQ_NEXT(vent, next_ve) != NULL) 6064b88c807SRodney W. Grimes (void)putchar(' '); 6074b88c807SRodney W. Grimes } 6084b88c807SRodney W. Grimes (void)putchar('\n'); 6094b88c807SRodney W. Grimes if (prtheader && lineno++ == prtheader - 4) { 6104b88c807SRodney W. Grimes (void)putchar('\n'); 6114b88c807SRodney W. Grimes printheader(); 6124b88c807SRodney W. Grimes lineno = 0; 6134b88c807SRodney W. Grimes } 6144b88c807SRodney W. Grimes } 615a4c8a745SGarance A Drosehn free_list(&gidlist); 616a4c8a745SGarance A Drosehn free_list(&pidlist); 617a4c8a745SGarance A Drosehn free_list(&pgrplist); 618a4c8a745SGarance A Drosehn free_list(&ruidlist); 619a4c8a745SGarance A Drosehn free_list(&sesslist); 620a4c8a745SGarance A Drosehn free_list(&ttylist); 621a4c8a745SGarance A Drosehn free_list(&uidlist); 6222e6c6ac4SGarance A Drosehn 6234b88c807SRodney W. Grimes exit(eval); 6244b88c807SRodney W. Grimes } 6254b88c807SRodney W. Grimes 626a4c8a745SGarance A Drosehn static int 627a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem) 6284e8b6a6fSGarance A Drosehn { 629a4c8a745SGarance A Drosehn struct group *grp; 630a4c8a745SGarance A Drosehn const char *nameorID; 631a4c8a745SGarance A Drosehn char *endp; 6320b42be7cSGarance A Drosehn u_long bigtemp; 6334e8b6a6fSGarance A Drosehn 634a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 635a4c8a745SGarance A Drosehn if (*elem == '\0') 636a4c8a745SGarance A Drosehn warnx("Invalid (zero-length) %s name", inf->lname); 637a4c8a745SGarance A Drosehn else 638a4c8a745SGarance A Drosehn warnx("%s name too long: %s", inf->lname, elem); 639a4c8a745SGarance A Drosehn optfatal = 1; 640352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 6414e8b6a6fSGarance A Drosehn } 642a4c8a745SGarance A Drosehn 6434e8b6a6fSGarance A Drosehn /* 644a4c8a745SGarance A Drosehn * SUSv3 states that `ps -G grouplist' should match "real-group 645a4c8a745SGarance A Drosehn * ID numbers", and does not mention group-names. I do want to 646a4c8a745SGarance A Drosehn * also support group-names, so this tries for a group-id first, 647a4c8a745SGarance A Drosehn * and only tries for a name if that doesn't work. This is the 648a4c8a745SGarance A Drosehn * opposite order of what is done in addelem_uid(), but in 649a4c8a745SGarance A Drosehn * practice the order would only matter for group-names which 650a4c8a745SGarance A Drosehn * are all-numeric. 6514e8b6a6fSGarance A Drosehn */ 652a4c8a745SGarance A Drosehn grp = NULL; 653a4c8a745SGarance A Drosehn nameorID = "named"; 654a4c8a745SGarance A Drosehn errno = 0; 6550b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 6560b42be7cSGarance A Drosehn if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) { 657a4c8a745SGarance A Drosehn nameorID = "name or ID matches"; 6580b42be7cSGarance A Drosehn grp = getgrgid((gid_t)bigtemp); 659a4c8a745SGarance A Drosehn } 660a4c8a745SGarance A Drosehn if (grp == NULL) 661a4c8a745SGarance A Drosehn grp = getgrnam(elem); 662a4c8a745SGarance A Drosehn if (grp == NULL) { 663a4c8a745SGarance A Drosehn warnx("No %s %s '%s'", inf->lname, nameorID, elem); 664a4c8a745SGarance A Drosehn optfatal = 1; 665c23b00b7SGarance A Drosehn return (0); 666a4c8a745SGarance A Drosehn } 667a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 668a4c8a745SGarance A Drosehn expand_list(inf); 669d822163fSGarance A Drosehn inf->l.gids[(inf->count)++] = grp->gr_gid; 670a4c8a745SGarance A Drosehn return (1); 671a4c8a745SGarance A Drosehn } 672a4c8a745SGarance A Drosehn 673352b6523SGarance A Drosehn #define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc.h. */ 674a4c8a745SGarance A Drosehn static int 675a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem) 676a4c8a745SGarance A Drosehn { 677a4c8a745SGarance A Drosehn char *endp; 678ca62e195SGarance A Drosehn long tempid; 679a4c8a745SGarance A Drosehn 680de1702f4SGarance A Drosehn if (*elem == '\0') { 681de1702f4SGarance A Drosehn warnx("Invalid (zero-length) process id"); 682de1702f4SGarance A Drosehn optfatal = 1; 683de1702f4SGarance A Drosehn return (0); /* Do not add this value. */ 68425113083SGarance A Drosehn } 68525113083SGarance A Drosehn 686a4c8a745SGarance A Drosehn errno = 0; 687a4c8a745SGarance A Drosehn tempid = strtol(elem, &endp, 10); 688a4c8a745SGarance A Drosehn if (*endp != '\0' || tempid < 0 || elem == endp) { 689a4c8a745SGarance A Drosehn warnx("Invalid %s: %s", inf->lname, elem); 6904e8b6a6fSGarance A Drosehn errno = ERANGE; 6914e8b6a6fSGarance A Drosehn } else if (errno != 0 || tempid > BSD_PID_MAX) { 692a4c8a745SGarance A Drosehn warnx("%s too large: %s", inf->lname, elem); 6934e8b6a6fSGarance A Drosehn errno = ERANGE; 6944e8b6a6fSGarance A Drosehn } 6954e8b6a6fSGarance A Drosehn if (errno == ERANGE) { 696a4c8a745SGarance A Drosehn optfatal = 1; 697c23b00b7SGarance A Drosehn return (0); 6984e8b6a6fSGarance A Drosehn } 699a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 700a4c8a745SGarance A Drosehn expand_list(inf); 701d822163fSGarance A Drosehn inf->l.pids[(inf->count)++] = tempid; 702a4c8a745SGarance A Drosehn return (1); 7034e8b6a6fSGarance A Drosehn } 704a4c8a745SGarance A Drosehn #undef BSD_PID_MAX 7054e8b6a6fSGarance A Drosehn 70623b8efadSGarance A Drosehn /*- 70723b8efadSGarance A Drosehn * The user can specify a device via one of three formats: 70823b8efadSGarance A Drosehn * 1) fully qualified, e.g.: /dev/ttyp0 /dev/console 70923b8efadSGarance A Drosehn * 2) missing "/dev", e.g.: ttyp0 console 71023b8efadSGarance A Drosehn * 3) two-letters, e.g.: p0 co 71123b8efadSGarance A Drosehn * (matching letters that would be seen in the "TT" column) 71223b8efadSGarance A Drosehn */ 713a4c8a745SGarance A Drosehn static int 714a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem) 715cf22dcfcSBrian Somers { 716a4c8a745SGarance A Drosehn const char *ttypath; 717ca62e195SGarance A Drosehn struct stat sb; 718ca62e195SGarance A Drosehn char pathbuf[PATH_MAX]; 719a4c8a745SGarance A Drosehn 72023b8efadSGarance A Drosehn ttypath = NULL; 72123b8efadSGarance A Drosehn switch (*elem) { 72223b8efadSGarance A Drosehn case '/': 723a4c8a745SGarance A Drosehn ttypath = elem; 72423b8efadSGarance A Drosehn break; 72523b8efadSGarance A Drosehn case 'c': 72623b8efadSGarance A Drosehn if (strcmp(elem, "co") == 0) { 72723b8efadSGarance A Drosehn ttypath = _PATH_CONSOLE; 72823b8efadSGarance A Drosehn break; 72923b8efadSGarance A Drosehn } 73023b8efadSGarance A Drosehn /* FALLTHROUGH */ 73123b8efadSGarance A Drosehn default: 73223b8efadSGarance A Drosehn strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf)); 733a4c8a745SGarance A Drosehn strlcat(pathbuf, elem, sizeof(pathbuf)); 734a4c8a745SGarance A Drosehn ttypath = pathbuf; 73523b8efadSGarance A Drosehn if (strncmp(pathbuf, _PATH_TTY, sizeof(_PATH_TTY)) == 0) 73623b8efadSGarance A Drosehn break; 73723b8efadSGarance A Drosehn if (strcmp(pathbuf, _PATH_CONSOLE) == 0) 73823b8efadSGarance A Drosehn break; 73923b8efadSGarance A Drosehn if (stat(pathbuf, &sb) == 0 && S_ISCHR(sb.st_mode)) { 74023b8efadSGarance A Drosehn /* No need to repeat stat() && S_ISCHR() checks */ 74123b8efadSGarance A Drosehn ttypath = NULL; 74223b8efadSGarance A Drosehn break; 743a4c8a745SGarance A Drosehn } 74423b8efadSGarance A Drosehn /* /dev/${elem} does not exist, so try /dev/tty${elem} */ 74523b8efadSGarance A Drosehn strlcpy(pathbuf, _PATH_TTY, sizeof(pathbuf)); 74623b8efadSGarance A Drosehn strlcat(pathbuf, elem, sizeof(pathbuf)); 74723b8efadSGarance A Drosehn break; 74823b8efadSGarance A Drosehn } 74923b8efadSGarance A Drosehn if (ttypath) { 750a4c8a745SGarance A Drosehn if (stat(ttypath, &sb) == -1) { 751a4c8a745SGarance A Drosehn warn("%s", ttypath); 752a4c8a745SGarance A Drosehn optfatal = 1; 753c23b00b7SGarance A Drosehn return (0); 754a4c8a745SGarance A Drosehn } 755a4c8a745SGarance A Drosehn if (!S_ISCHR(sb.st_mode)) { 756a4c8a745SGarance A Drosehn warn("%s: Not a terminal", ttypath); 757a4c8a745SGarance A Drosehn optfatal = 1; 758c23b00b7SGarance A Drosehn return (0); 759a4c8a745SGarance A Drosehn } 76023b8efadSGarance A Drosehn } 761a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 762a4c8a745SGarance A Drosehn expand_list(inf); 763d822163fSGarance A Drosehn inf->l.ttys[(inf->count)++] = sb.st_rdev; 764a4c8a745SGarance A Drosehn return (1); 765a4c8a745SGarance A Drosehn } 766a4c8a745SGarance A Drosehn 767a4c8a745SGarance A Drosehn static int 768a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem) 769a4c8a745SGarance A Drosehn { 770cf22dcfcSBrian Somers struct passwd *pwd; 771a4c8a745SGarance A Drosehn char *endp; 7720b42be7cSGarance A Drosehn u_long bigtemp; 773cf22dcfcSBrian Somers 774a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 775a4c8a745SGarance A Drosehn if (*elem == '\0') 776a4c8a745SGarance A Drosehn warnx("Invalid (zero-length) %s name", inf->lname); 777a4c8a745SGarance A Drosehn else 778a4c8a745SGarance A Drosehn warnx("%s name too long: %s", inf->lname, elem); 779a4c8a745SGarance A Drosehn optfatal = 1; 780352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 781a4c8a745SGarance A Drosehn } 782cf22dcfcSBrian Somers 783a4c8a745SGarance A Drosehn pwd = getpwnam(elem); 784a4c8a745SGarance A Drosehn if (pwd == NULL) { 785a4c8a745SGarance A Drosehn errno = 0; 7860b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 7870b42be7cSGarance A Drosehn if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX) 788a4c8a745SGarance A Drosehn warnx("No %s named '%s'", inf->lname, elem); 789a4c8a745SGarance A Drosehn else { 790a4c8a745SGarance A Drosehn /* The string is all digits, so it might be a userID. */ 7910b42be7cSGarance A Drosehn pwd = getpwuid((uid_t)bigtemp); 792a4c8a745SGarance A Drosehn if (pwd == NULL) 793a4c8a745SGarance A Drosehn warnx("No %s name or ID matches '%s'", 794a4c8a745SGarance A Drosehn inf->lname, elem); 795cf22dcfcSBrian Somers } 796cf22dcfcSBrian Somers } 797a4c8a745SGarance A Drosehn if (pwd == NULL) { 798e3c4e1ddSGarance A Drosehn /* 799e3c4e1ddSGarance A Drosehn * These used to be treated as minor warnings (and the 800e3c4e1ddSGarance A Drosehn * option was simply ignored), but now they are fatal 801e3c4e1ddSGarance A Drosehn * errors (and the command will be aborted). 802e3c4e1ddSGarance A Drosehn */ 803e3c4e1ddSGarance A Drosehn optfatal = 1; 804c23b00b7SGarance A Drosehn return (0); 805cf22dcfcSBrian Somers } 806a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 807a4c8a745SGarance A Drosehn expand_list(inf); 808d822163fSGarance A Drosehn inf->l.uids[(inf->count)++] = pwd->pw_uid; 809a4c8a745SGarance A Drosehn return (1); 810a4c8a745SGarance A Drosehn } 811cf22dcfcSBrian Somers 812a4c8a745SGarance A Drosehn static void 813a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp) 814a4c8a745SGarance A Drosehn { 815a4c8a745SGarance A Drosehn const char *savep; 816a4c8a745SGarance A Drosehn char *cp, *endp; 817a4c8a745SGarance A Drosehn int toolong; 818ca62e195SGarance A Drosehn char elemcopy[PATH_MAX]; 819a4c8a745SGarance A Drosehn 820de1702f4SGarance A Drosehn if (*argp == 0) 821de1702f4SGarance A Drosehn inf->addelem(inf, elemcopy); 822a4c8a745SGarance A Drosehn while (*argp != '\0') { 823a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 824a4c8a745SGarance A Drosehn argp++; 825a4c8a745SGarance A Drosehn savep = argp; 826a4c8a745SGarance A Drosehn toolong = 0; 827a4c8a745SGarance A Drosehn cp = elemcopy; 828a4c8a745SGarance A Drosehn if (strchr(T_SEP, *argp) == NULL) { 829a4c8a745SGarance A Drosehn endp = elemcopy + sizeof(elemcopy) - 1; 830a4c8a745SGarance A Drosehn while (*argp != '\0' && cp <= endp && 831a4c8a745SGarance A Drosehn strchr(W_SEP T_SEP, *argp) == NULL) 832a4c8a745SGarance A Drosehn *cp++ = *argp++; 833a4c8a745SGarance A Drosehn if (cp > endp) 834a4c8a745SGarance A Drosehn toolong = 1; 835a4c8a745SGarance A Drosehn } 836a4c8a745SGarance A Drosehn if (!toolong) { 837a4c8a745SGarance A Drosehn *cp = '\0'; 838352b6523SGarance A Drosehn /* 839a9626fb3SGarance A Drosehn * Add this single element to the given list. 840352b6523SGarance A Drosehn */ 841a4c8a745SGarance A Drosehn inf->addelem(inf, elemcopy); 842a4c8a745SGarance A Drosehn } else { 843a4c8a745SGarance A Drosehn /* 844a4c8a745SGarance A Drosehn * The string is too long to copy. Find the end 845a4c8a745SGarance A Drosehn * of the string to print out the warning message. 846a4c8a745SGarance A Drosehn */ 847a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP T_SEP, 848a4c8a745SGarance A Drosehn *argp) == NULL) 849a4c8a745SGarance A Drosehn argp++; 850a4c8a745SGarance A Drosehn warnx("Value too long: %.*s", (int)(argp - savep), 851a4c8a745SGarance A Drosehn savep); 852a4c8a745SGarance A Drosehn optfatal = 1; 853a4c8a745SGarance A Drosehn } 854a4c8a745SGarance A Drosehn /* 855a4c8a745SGarance A Drosehn * Skip over any number of trailing whitespace characters, 856a4c8a745SGarance A Drosehn * but only one (at most) trailing element-terminating 857a4c8a745SGarance A Drosehn * character. 858a4c8a745SGarance A Drosehn */ 859a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 860a4c8a745SGarance A Drosehn argp++; 861a4c8a745SGarance A Drosehn if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) { 862a4c8a745SGarance A Drosehn argp++; 863a4c8a745SGarance A Drosehn /* Catch case where string ended with a comma. */ 864a4c8a745SGarance A Drosehn if (*argp == '\0') 865a4c8a745SGarance A Drosehn inf->addelem(inf, argp); 866a4c8a745SGarance A Drosehn } 867a4c8a745SGarance A Drosehn } 868a4c8a745SGarance A Drosehn } 869a4c8a745SGarance A Drosehn 870a4c8a745SGarance A Drosehn static void * 871a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf) 872a4c8a745SGarance A Drosehn { 873a4c8a745SGarance A Drosehn void *newlist; 874ca62e195SGarance A Drosehn int newmax; 875a4c8a745SGarance A Drosehn 876a4c8a745SGarance A Drosehn newmax = (inf->maxcount + 1) << 1; 877d822163fSGarance A Drosehn newlist = realloc(inf->l.ptr, newmax * inf->elemsize); 878a4c8a745SGarance A Drosehn if (newlist == NULL) { 879d822163fSGarance A Drosehn free(inf->l.ptr); 880c23b00b7SGarance A Drosehn errx(1, "realloc to %d %ss failed", newmax, inf->lname); 881a4c8a745SGarance A Drosehn } 882a4c8a745SGarance A Drosehn inf->maxcount = newmax; 883d822163fSGarance A Drosehn inf->l.ptr = newlist; 884a4c8a745SGarance A Drosehn 885a4c8a745SGarance A Drosehn return (newlist); 886a4c8a745SGarance A Drosehn } 887a4c8a745SGarance A Drosehn 888a4c8a745SGarance A Drosehn static void 889a4c8a745SGarance A Drosehn free_list(struct listinfo *inf) 890a4c8a745SGarance A Drosehn { 891a4c8a745SGarance A Drosehn 892a4c8a745SGarance A Drosehn inf->count = inf->elemsize = inf->maxcount = 0; 893d822163fSGarance A Drosehn if (inf->l.ptr != NULL) 894d822163fSGarance A Drosehn free(inf->l.ptr); 895a4c8a745SGarance A Drosehn inf->addelem = NULL; 896a4c8a745SGarance A Drosehn inf->lname = NULL; 897d822163fSGarance A Drosehn inf->l.ptr = NULL; 898a4c8a745SGarance A Drosehn } 899a4c8a745SGarance A Drosehn 900a4c8a745SGarance A Drosehn static void 901a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize, 902a4c8a745SGarance A Drosehn const char *lname) 903a4c8a745SGarance A Drosehn { 904a4c8a745SGarance A Drosehn 905a4c8a745SGarance A Drosehn inf->count = inf->maxcount = 0; 906a4c8a745SGarance A Drosehn inf->elemsize = elemsize; 907a4c8a745SGarance A Drosehn inf->addelem = artn; 908a4c8a745SGarance A Drosehn inf->lname = lname; 909d822163fSGarance A Drosehn inf->l.ptr = NULL; 910cf22dcfcSBrian Somers } 911cf22dcfcSBrian Somers 912fde411d5SJuli Mallett VARENT * 913fde411d5SJuli Mallett find_varentry(VAR *v) 914fde411d5SJuli Mallett { 915fde411d5SJuli Mallett struct varent *vent; 916fde411d5SJuli Mallett 917bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 918fde411d5SJuli Mallett if (strcmp(vent->var->name, v->name) == 0) 919fde411d5SJuli Mallett return vent; 920fde411d5SJuli Mallett } 921fde411d5SJuli Mallett return NULL; 922fde411d5SJuli Mallett } 923fde411d5SJuli Mallett 9244b88c807SRodney W. Grimes static void 92546251ddeSWarner Losh scanvars(void) 9264b88c807SRodney W. Grimes { 9274b88c807SRodney W. Grimes struct varent *vent; 9284b88c807SRodney W. Grimes VAR *v; 9296a2d726bSJordan K. Hubbard 930bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 9316a2d726bSJordan K. Hubbard v = vent->var; 9326a2d726bSJordan K. Hubbard if (v->flag & DSIZ) { 9336a2d726bSJordan K. Hubbard v->dwidth = v->width; 9346a2d726bSJordan K. Hubbard v->width = 0; 9356a2d726bSJordan K. Hubbard } 9366a2d726bSJordan K. Hubbard if (v->flag & USER) 9376a2d726bSJordan K. Hubbard needuser = 1; 9386a2d726bSJordan K. Hubbard if (v->flag & COMM) 9396a2d726bSJordan K. Hubbard needcomm = 1; 9406a2d726bSJordan K. Hubbard } 9416a2d726bSJordan K. Hubbard } 9426a2d726bSJordan K. Hubbard 9436a2d726bSJordan K. Hubbard static void 94446251ddeSWarner Losh dynsizevars(KINFO *ki) 9456a2d726bSJordan K. Hubbard { 9466a2d726bSJordan K. Hubbard struct varent *vent; 9476a2d726bSJordan K. Hubbard VAR *v; 9486a2d726bSJordan K. Hubbard int i; 9496a2d726bSJordan K. Hubbard 950bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 9516a2d726bSJordan K. Hubbard v = vent->var; 9526a2d726bSJordan K. Hubbard if (!(v->flag & DSIZ)) 9536a2d726bSJordan K. Hubbard continue; 9546a2d726bSJordan K. Hubbard i = (v->sproc)( ki); 9556a2d726bSJordan K. Hubbard if (v->width < i) 9566a2d726bSJordan K. Hubbard v->width = i; 9576a2d726bSJordan K. Hubbard if (v->width > v->dwidth) 9586a2d726bSJordan K. Hubbard v->width = v->dwidth; 9596a2d726bSJordan K. Hubbard } 9606a2d726bSJordan K. Hubbard } 9616a2d726bSJordan K. Hubbard 9626a2d726bSJordan K. Hubbard static void 96346251ddeSWarner Losh sizevars(void) 9646a2d726bSJordan K. Hubbard { 9656a2d726bSJordan K. Hubbard struct varent *vent; 9666a2d726bSJordan K. Hubbard VAR *v; 9674b88c807SRodney W. Grimes int i; 9684b88c807SRodney W. Grimes 969bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 9704b88c807SRodney W. Grimes v = vent->var; 97178b1878aSJuli Mallett i = strlen(vent->header); 9724b88c807SRodney W. Grimes if (v->width < i) 9734b88c807SRodney W. Grimes v->width = i; 9744b88c807SRodney W. Grimes totwidth += v->width + 1; /* +1 for space */ 9754b88c807SRodney W. Grimes } 9764b88c807SRodney W. Grimes totwidth--; 9774b88c807SRodney W. Grimes } 9784b88c807SRodney W. Grimes 979871e8d8cSMark Murray static const char * 98046251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 98146251ddeSWarner Losh char *comm, int maxlen) 9824b88c807SRodney W. Grimes { 983871e8d8cSMark Murray const char *s; 9844b88c807SRodney W. Grimes 985871e8d8cSMark Murray s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen); 9864b88c807SRodney W. Grimes return (s); 9874b88c807SRodney W. Grimes } 9884b88c807SRodney W. Grimes 989e0aa5ab7SJohn Baldwin #define UREADOK(ki) (forceuread || (ki->ki_p->ki_sflag & PS_INMEM)) 9903ac5e955SJohn Dyson 9914b88c807SRodney W. Grimes static void 99246251ddeSWarner Losh saveuser(KINFO *ki) 9934b88c807SRodney W. Grimes { 9944b88c807SRodney W. Grimes 995e0aa5ab7SJohn Baldwin if (ki->ki_p->ki_sflag & PS_INMEM) { 9964b88c807SRodney W. Grimes /* 9974b88c807SRodney W. Grimes * The u-area might be swapped out, and we can't get 9984b88c807SRodney W. Grimes * at it because we have a crashdump and no swap. 9994b88c807SRodney W. Grimes * If it's here fill in these fields, otherwise, just 10004b88c807SRodney W. Grimes * leave them 0. 10014b88c807SRodney W. Grimes */ 10021f7d2501SKirk McKusick ki->ki_valid = 1; 10034b88c807SRodney W. Grimes } else 10041f7d2501SKirk McKusick ki->ki_valid = 0; 10054b88c807SRodney W. Grimes /* 10064b88c807SRodney W. Grimes * save arguments if needed 10074b88c807SRodney W. Grimes */ 1008dd693acfSGarance A Drosehn if (needcomm) { 1009dd693acfSGarance A Drosehn if (ki->ki_p->ki_stat == SZOMB) 1010dd693acfSGarance A Drosehn ki->ki_args = strdup("<defunct>"); 1011dd693acfSGarance A Drosehn else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) 1012dd693acfSGarance A Drosehn ki->ki_args = strdup(fmt(kvm_getargv, ki, 1013dd693acfSGarance A Drosehn ki->ki_p->ki_comm, MAXCOMLEN)); 1014dd693acfSGarance A Drosehn else 1015871e8d8cSMark Murray asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm); 1016bd6233fdSGarance A Drosehn if (ki->ki_args == NULL) 1017dd693acfSGarance A Drosehn errx(1, "malloc failed"); 10183ac5e955SJohn Dyson } else { 10194b88c807SRodney W. Grimes ki->ki_args = NULL; 10203ac5e955SJohn Dyson } 1021dd693acfSGarance A Drosehn if (needenv) { 1022dd693acfSGarance A Drosehn if (UREADOK(ki)) 1023dd693acfSGarance A Drosehn ki->ki_env = strdup(fmt(kvm_getenvv, ki, 1024dd693acfSGarance A Drosehn (char *)NULL, 0)); 1025dd693acfSGarance A Drosehn else 1026dd693acfSGarance A Drosehn ki->ki_env = strdup("()"); 1027dd693acfSGarance A Drosehn if (ki->ki_env == NULL) 1028dd693acfSGarance A Drosehn errx(1, "malloc failed"); 10293ac5e955SJohn Dyson } else { 10304b88c807SRodney W. Grimes ki->ki_env = NULL; 10314b88c807SRodney W. Grimes } 10323ac5e955SJohn Dyson } 10334b88c807SRodney W. Grimes 10344bac4483SGarance A Drosehn /* A macro used to improve the readability of pscomp(). */ 10354bac4483SGarance A Drosehn #define DIFF_RETURN(a, b, field) do { \ 10364bac4483SGarance A Drosehn if ((a)->field != (b)->field) \ 10374bac4483SGarance A Drosehn return (((a)->field < (b)->field) ? -1 : 1); \ 10384bac4483SGarance A Drosehn } while (0) 10394bac4483SGarance A Drosehn 10404b88c807SRodney W. Grimes static int 104146251ddeSWarner Losh pscomp(const void *a, const void *b) 10424b88c807SRodney W. Grimes { 10435bd7b1f3SGarance A Drosehn const KINFO *ka, *kb; 10444b88c807SRodney W. Grimes 10455bd7b1f3SGarance A Drosehn ka = a; 10465bd7b1f3SGarance A Drosehn kb = b; 10475bd7b1f3SGarance A Drosehn /* SORTCPU and SORTMEM are sorted in descending order. */ 10484bac4483SGarance A Drosehn if (sortby == SORTCPU) 10494bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_pcpu); 10504bac4483SGarance A Drosehn if (sortby == SORTMEM) 10514bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_memsize); 10525bd7b1f3SGarance A Drosehn /* 10535bd7b1f3SGarance A Drosehn * TTY's are sorted in ascending order, except that all NODEV 10545bd7b1f3SGarance A Drosehn * processes come before all processes with a device. 10555bd7b1f3SGarance A Drosehn */ 10564bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) { 10574bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev == NODEV) 10585bd7b1f3SGarance A Drosehn return (-1); 10594bac4483SGarance A Drosehn if (kb->ki_p->ki_tdev == NODEV) 10605bd7b1f3SGarance A Drosehn return (1); 10614bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tdev); 10624bac4483SGarance A Drosehn } 10634bac4483SGarance A Drosehn 1064b4b24324SGarance A Drosehn /* PID's and TID's (threads) are sorted in ascending order. */ 10654bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_pid); 1066b4b24324SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tid); 10675bd7b1f3SGarance A Drosehn return (0); 10684b88c807SRodney W. Grimes } 10694bac4483SGarance A Drosehn #undef DIFF_RETURN 10704b88c807SRodney W. Grimes 10714b88c807SRodney W. Grimes /* 10724b88c807SRodney W. Grimes * ICK (all for getopt), would rather hide the ugliness 10734b88c807SRodney W. Grimes * here than taint the main code. 10744b88c807SRodney W. Grimes * 10754b88c807SRodney W. Grimes * ps foo -> ps -foo 10764b88c807SRodney W. Grimes * ps 34 -> ps -p34 10774b88c807SRodney W. Grimes * 10784b88c807SRodney W. Grimes * The old convention that 't' with no trailing tty arg means the users 10794b88c807SRodney W. Grimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 10804b88c807SRodney W. Grimes * feature is available with the option 'T', which takes no argument. 10814b88c807SRodney W. Grimes */ 10824b88c807SRodney W. Grimes static char * 1083bf46a3bfSGarance A Drosehn kludge_oldps_options(const char *optlist, char *origval, const char *nextarg) 10844b88c807SRodney W. Grimes { 10854b88c807SRodney W. Grimes size_t len; 1086c675340aSGarance A Drosehn char *argp, *cp, *newopts, *ns, *optp, *pidp; 10874b88c807SRodney W. Grimes 1088daed3ad6SJuli Mallett /* 1089c675340aSGarance A Drosehn * See if the original value includes any option which takes an 1090c675340aSGarance A Drosehn * argument (and will thus use up the remainder of the string). 1091daed3ad6SJuli Mallett */ 1092c675340aSGarance A Drosehn argp = NULL; 1093c675340aSGarance A Drosehn if (optlist != NULL) { 1094c675340aSGarance A Drosehn for (cp = origval; *cp != '\0'; cp++) { 1095c675340aSGarance A Drosehn optp = strchr(optlist, *cp); 1096c675340aSGarance A Drosehn if ((optp != NULL) && *(optp + 1) == ':') { 1097c675340aSGarance A Drosehn argp = cp; 1098c675340aSGarance A Drosehn break; 1099c675340aSGarance A Drosehn } 1100c675340aSGarance A Drosehn } 1101c675340aSGarance A Drosehn } 1102c675340aSGarance A Drosehn if (argp != NULL && *origval == '-') 1103c675340aSGarance A Drosehn return (origval); 1104daed3ad6SJuli Mallett 11054b88c807SRodney W. Grimes /* 11064b88c807SRodney W. Grimes * if last letter is a 't' flag with no argument (in the context 11074b88c807SRodney W. Grimes * of the oldps options -- option string NOT starting with a '-' -- 11084b88c807SRodney W. Grimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 1109380434d4SBrian Somers * 11102631c777SGarance A Drosehn * However, if a flag accepting a string argument is found earlier 11112631c777SGarance A Drosehn * in the option string (including a possible `t' flag), then the 11122631c777SGarance A Drosehn * remainder of the string must be the argument to that flag; so 1113c675340aSGarance A Drosehn * do not modify that argument. Note that a trailing `t' would 1114c675340aSGarance A Drosehn * cause argp to be set, if argp was not already set by some 1115c675340aSGarance A Drosehn * earlier option. 11164b88c807SRodney W. Grimes */ 1117c675340aSGarance A Drosehn len = strlen(origval); 1118c675340aSGarance A Drosehn cp = origval + len - 1; 1119c675340aSGarance A Drosehn pidp = NULL; 1120bf46a3bfSGarance A Drosehn if (*cp == 't' && *origval != '-' && cp == argp) { 1121bf46a3bfSGarance A Drosehn if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg)) 11224b88c807SRodney W. Grimes *cp = 'T'; 1123bf46a3bfSGarance A Drosehn } else if (argp == NULL) { 1124c675340aSGarance A Drosehn /* 1125c675340aSGarance A Drosehn * The original value did not include any option which takes 1126c675340aSGarance A Drosehn * an argument (and that would include `p' and `t'), so check 1127c675340aSGarance A Drosehn * the value for trailing number, or comma-separated list of 1128c675340aSGarance A Drosehn * numbers, which we will treat as a pid request. 1129c675340aSGarance A Drosehn */ 1130c675340aSGarance A Drosehn if (isdigitch(*cp)) { 1131c675340aSGarance A Drosehn while (cp >= origval && (*cp == ',' || isdigitch(*cp))) 1132c675340aSGarance A Drosehn --cp; 1133c675340aSGarance A Drosehn pidp = cp + 1; 1134c675340aSGarance A Drosehn } 1135c675340aSGarance A Drosehn } 1136c675340aSGarance A Drosehn 1137c675340aSGarance A Drosehn /* 1138c675340aSGarance A Drosehn * If nothing needs to be added to the string, then return 1139c675340aSGarance A Drosehn * the "original" (although possibly modified) value. 1140c675340aSGarance A Drosehn */ 1141c675340aSGarance A Drosehn if (*origval == '-' && pidp == NULL) 1142c675340aSGarance A Drosehn return (origval); 1143c675340aSGarance A Drosehn 1144c675340aSGarance A Drosehn /* 1145c675340aSGarance A Drosehn * Create a copy of the string to add '-' and/or 'p' to the 1146c675340aSGarance A Drosehn * original value. 1147c675340aSGarance A Drosehn */ 1148c675340aSGarance A Drosehn if ((newopts = ns = malloc(len + 3)) == NULL) 1149c675340aSGarance A Drosehn errx(1, "malloc failed"); 1150c675340aSGarance A Drosehn 1151c675340aSGarance A Drosehn if (*origval != '-') 1152c675340aSGarance A Drosehn *ns++ = '-'; /* add option flag */ 1153c675340aSGarance A Drosehn 1154c675340aSGarance A Drosehn if (pidp == NULL) 1155c675340aSGarance A Drosehn strcpy(ns, origval); 11564b88c807SRodney W. Grimes else { 11574b88c807SRodney W. Grimes /* 1158c675340aSGarance A Drosehn * Copy everything before the pid string, add the `p', 1159c675340aSGarance A Drosehn * and then copy the pid string. 11604b88c807SRodney W. Grimes */ 1161c675340aSGarance A Drosehn len = pidp - origval; 1162c675340aSGarance A Drosehn memcpy(ns, origval, len); 1163c675340aSGarance A Drosehn ns += len; 11644b88c807SRodney W. Grimes *ns++ = 'p'; 1165c675340aSGarance A Drosehn strcpy(ns, pidp); 1166c675340aSGarance A Drosehn } 11674b88c807SRodney W. Grimes 11684b88c807SRodney W. Grimes return (newopts); 11694b88c807SRodney W. Grimes } 11704b88c807SRodney W. Grimes 11714b88c807SRodney W. Grimes static void 117246251ddeSWarner Losh usage(void) 11734b88c807SRodney W. Grimes { 117431172f6eSGarance A Drosehn #define SINGLE_OPTS "[-aCc" OPT_LAZY_f "HhjlmrSTuvwXxZ]" 11754b88c807SRodney W. Grimes 1176a4c8a745SGarance A Drosehn (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 1177b34f38aeSGarance A Drosehn "usage: ps " SINGLE_OPTS " [-G gid[,gid]] [-O|o fmt]", 1178a4c8a745SGarance A Drosehn " [-p pid[,pid]] [-t tty[,tty]] [-U user[,user]]", 1179a4c8a745SGarance A Drosehn " [-M core] [-N system]", 11804b88c807SRodney W. Grimes " ps [-L]"); 11814b88c807SRodney W. Grimes exit(1); 11824b88c807SRodney W. Grimes } 1183