14b88c807SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 44b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 54b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 64b88c807SRodney W. Grimes * 74b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 84b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 94b88c807SRodney W. Grimes * are met: 104b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 124b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 134b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 144b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 164b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 174b88c807SRodney W. Grimes * without specific prior written permission. 184b88c807SRodney W. Grimes * 194b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 204b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 214b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 224b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 234b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 244b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 254b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 264b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 274b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 284b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 294b88c807SRodney W. Grimes * SUCH DAMAGE. 30a4c8a745SGarance A Drosehn * ------+---------+---------+-------- + --------+---------+---------+---------* 31a4c8a745SGarance A Drosehn * Copyright (c) 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 32a4c8a745SGarance A Drosehn * All rights reserved. 33a4c8a745SGarance A Drosehn * 34a4c8a745SGarance A Drosehn * Significant modifications made to bring `ps' options somewhat closer 35a4c8a745SGarance A Drosehn * to the standard for `ps' as described in SingleUnixSpec-v3. 36a4c8a745SGarance A Drosehn * ------+---------+---------+-------- + --------+---------+---------+---------* 374b88c807SRodney W. Grimes */ 384b88c807SRodney W. Grimes 394b88c807SRodney W. Grimes #ifndef lint 40871e8d8cSMark Murray static const char copyright[] = 414b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 424b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 434b88c807SRodney W. Grimes #endif /* not lint */ 444b88c807SRodney W. Grimes 45c9a8d1f4SPhilippe Charnier #if 0 46871e8d8cSMark Murray #ifndef lint 47c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 484b88c807SRodney W. Grimes #endif /* not lint */ 49871e8d8cSMark Murray #endif 50eaed5652SPhilippe Charnier 512749b141SDavid E. O'Brien #include <sys/cdefs.h> 524b88c807SRodney W. Grimes #include <sys/param.h> 5313767130SBryan Drewery #include <sys/jail.h> 54c7910c3aSGarance A Drosehn #include <sys/proc.h> 558b6f5f5fSDavid Greenman #include <sys/user.h> 564b88c807SRodney W. Grimes #include <sys/stat.h> 574b88c807SRodney W. Grimes #include <sys/ioctl.h> 584b88c807SRodney W. Grimes #include <sys/sysctl.h> 59a951d1f8SChristian S.J. Peron #include <sys/mount.h> 604b88c807SRodney W. Grimes 614b88c807SRodney W. Grimes #include <ctype.h> 624b88c807SRodney W. Grimes #include <err.h> 634e8b6a6fSGarance A Drosehn #include <errno.h> 644b88c807SRodney W. Grimes #include <fcntl.h> 65a4c8a745SGarance A Drosehn #include <grp.h> 6613767130SBryan Drewery #include <jail.h> 674b88c807SRodney W. Grimes #include <kvm.h> 68b2496d93SMike Pritchard #include <limits.h> 6908017519SAndrey A. Chernov #include <locale.h> 704b88c807SRodney W. Grimes #include <paths.h> 71871e8d8cSMark Murray #include <pwd.h> 724b88c807SRodney W. Grimes #include <stdio.h> 734b88c807SRodney W. Grimes #include <stdlib.h> 744b88c807SRodney W. Grimes #include <string.h> 754b88c807SRodney W. Grimes #include <unistd.h> 768beb1a2fSMarcel Moolenaar #include <libxo/xo.h> 774b88c807SRodney W. Grimes 784b88c807SRodney W. Grimes #include "ps.h" 794b88c807SRodney W. Grimes 808a529f59SJohn Baldwin #define _PATH_PTS "/dev/pts/" 818a529f59SJohn Baldwin 82a4c8a745SGarance A Drosehn #define W_SEP " \t" /* "Whitespace" list separators */ 83a4c8a745SGarance A Drosehn #define T_SEP "," /* "Terminate-element" list separators */ 84cf22dcfcSBrian Somers 85de800cd4SGarance A Drosehn #ifdef LAZY_PS 86c46bb4b3SGarance A Drosehn #define DEF_UREAD 0 87de800cd4SGarance A Drosehn #define OPT_LAZY_f "f" 88de800cd4SGarance A Drosehn #else 89c46bb4b3SGarance A Drosehn #define DEF_UREAD 1 /* Always do the more-expensive read. */ 90de800cd4SGarance A Drosehn #define OPT_LAZY_f /* I.e., the `-f' option is not added. */ 91de800cd4SGarance A Drosehn #endif 924b88c807SRodney W. Grimes 93c675340aSGarance A Drosehn /* 94ecbb06f8SGarance A Drosehn * isdigit takes an `int', but expects values in the range of unsigned char. 95ecbb06f8SGarance A Drosehn * This wrapper ensures that values from a 'char' end up in the correct range. 96c675340aSGarance A Drosehn */ 97ecbb06f8SGarance A Drosehn #define isdigitch(Anychar) isdigit((u_char)(Anychar)) 98c675340aSGarance A Drosehn 99db91faacSPeter Wemm int cflag; /* -c */ 100de800cd4SGarance A Drosehn int eval; /* Exit value */ 101de800cd4SGarance A Drosehn time_t now; /* Current time(3) value */ 1024b88c807SRodney W. Grimes int rawcpu; /* -C */ 1034b88c807SRodney W. Grimes int sumrusage; /* -S */ 104de800cd4SGarance A Drosehn int termwidth; /* Width of the screen (0 == infinity). */ 1057ab24ea3SJulian Elischer int showthreads; /* will threads be shown? */ 1064b88c807SRodney W. Grimes 107bdf8ab46SGarance A Drosehn struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); 108de800cd4SGarance A Drosehn 109de800cd4SGarance A Drosehn static int forceuread = DEF_UREAD; /* Do extra work to get u-area. */ 110de800cd4SGarance A Drosehn static kvm_t *kd; 111de800cd4SGarance A Drosehn static int needcomm; /* -o "command" */ 112de800cd4SGarance A Drosehn static int needenv; /* -e */ 113de800cd4SGarance A Drosehn static int needuser; /* -o "user" */ 114de800cd4SGarance A Drosehn static int optfatal; /* Fatal error parsing some list-option. */ 11564b0bf0bSPawel Jakub Dawidek static int pid_max; /* kern.max_pid */ 116de800cd4SGarance A Drosehn 117de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 118ba2cd770SJuli Mallett 119a4c8a745SGarance A Drosehn struct listinfo; 120de800cd4SGarance A Drosehn typedef int addelem_rtn(struct listinfo *_inf, const char *_elem); 121a4c8a745SGarance A Drosehn 122a4c8a745SGarance A Drosehn struct listinfo { 123a4c8a745SGarance A Drosehn int count; 124a4c8a745SGarance A Drosehn int maxcount; 125a4c8a745SGarance A Drosehn int elemsize; 126a4c8a745SGarance A Drosehn addelem_rtn *addelem; 127a4c8a745SGarance A Drosehn const char *lname; 128a4c8a745SGarance A Drosehn union { 129a4c8a745SGarance A Drosehn gid_t *gids; 13013767130SBryan Drewery int *jids; 131a4c8a745SGarance A Drosehn pid_t *pids; 132a4c8a745SGarance A Drosehn dev_t *ttys; 133a4c8a745SGarance A Drosehn uid_t *uids; 134a4c8a745SGarance A Drosehn void *ptr; 135d822163fSGarance A Drosehn } l; 136a4c8a745SGarance A Drosehn }; 137a4c8a745SGarance A Drosehn 138a4c8a745SGarance A Drosehn static int addelem_gid(struct listinfo *, const char *); 13913767130SBryan Drewery static int addelem_jid(struct listinfo *, const char *); 140a4c8a745SGarance A Drosehn static int addelem_pid(struct listinfo *, const char *); 141a4c8a745SGarance A Drosehn static int addelem_tty(struct listinfo *, const char *); 142a4c8a745SGarance A Drosehn static int addelem_uid(struct listinfo *, const char *); 143a4c8a745SGarance A Drosehn static void add_list(struct listinfo *, const char *); 144044fce53SBrian Somers static void descendant_sort(KINFO *, int); 1451d1143ecSEdward Tomasz Napierala static void format_output(KINFO *); 146a4c8a745SGarance A Drosehn static void *expand_list(struct listinfo *); 147f35e0715SGarance A Drosehn static const char * 148f35e0715SGarance A Drosehn fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 1491c67ef12SJohn Baldwin KINFO *, char *, char *, int); 150a4c8a745SGarance A Drosehn static void free_list(struct listinfo *); 151a4c8a745SGarance A Drosehn static void init_list(struct listinfo *, addelem_rtn, int, const char *); 15225113083SGarance A Drosehn static char *kludge_oldps_options(const char *, char *, const char *); 1534857f240SGarance A Drosehn static int pscomp(const void *, const void *); 1544857f240SGarance A Drosehn static void saveuser(KINFO *); 1554857f240SGarance A Drosehn static void scanvars(void); 1564857f240SGarance A Drosehn static void sizevars(void); 15764b0bf0bSPawel Jakub Dawidek static void pidmax_init(void); 1584857f240SGarance A Drosehn static void usage(void); 1594b88c807SRodney W. Grimes 160c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command"; 161259fcfacSGarance A Drosehn static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command"; 1621d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state," 1631d2324f4SGarance A Drosehn "tt,time,command"; 164871e8d8cSMark Murray static char o1[] = "pid"; 165c0716492SJuli Mallett static char o2[] = "tt,state,time,command"; 166c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command"; 1671d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz," 1681d2324f4SGarance A Drosehn "%cpu,%mem,command"; 1692af538ebSRobert Watson static char Zfmt[] = "label"; 1704b88c807SRodney W. Grimes 171*5c0a1c15SPiotr Pawel Stefaniak #define PS_ARGS "AaCcD:de" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ" 17241623b2dSMaxim Sobolev 1734b88c807SRodney W. Grimes int 17446251ddeSWarner Losh main(int argc, char *argv[]) 1754b88c807SRodney W. Grimes { 17613767130SBryan Drewery struct listinfo gidlist, jidlist, pgrplist, pidlist; 177a4c8a745SGarance A Drosehn struct listinfo ruidlist, sesslist, ttylist, uidlist; 1784b88c807SRodney W. Grimes struct kinfo_proc *kp; 1791d1143ecSEdward Tomasz Napierala KINFO *kinfo = NULL, *next_KINFO; 1801d1143ecSEdward Tomasz Napierala KINFO_STR *ks; 1814b88c807SRodney W. Grimes struct varent *vent; 182484f5781SPedro F. Giffuni struct winsize ws = { .ws_row = 0 }; 18316eb3576SMarcelo Araujo const char *nlistf, *memf, *str; 184ca62e195SGarance A Drosehn char *cols; 1851d1143ecSEdward Tomasz Napierala int all, ch, elem, flag, _fmt, i, lineno, linelen, left; 186044fce53SBrian Somers int descendancy, nentries, nkept, nselectors; 1877ab24ea3SJulian Elischer int prtheader, wflag, what, xkeep, xkeep_implied; 1888beb1a2fSMarcel Moolenaar int fwidthmin, fwidthmax; 189871e8d8cSMark Murray char errbuf[_POSIX2_LINE_MAX]; 1908beb1a2fSMarcel Moolenaar char fmtbuf[_POSIX2_LINE_MAX]; 191*5c0a1c15SPiotr Pawel Stefaniak enum { NONE = 0, UP = 1, DOWN = 2, BOTH = 1 | 2 } directions = NONE; 192*5c0a1c15SPiotr Pawel Stefaniak struct { int traversed; int initial; } pid_count; 1934b88c807SRodney W. Grimes 1942bf4b9cfSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 195352b6523SGarance A Drosehn time(&now); /* Used by routines in print.c. */ 1962bf4b9cfSAndrey A. Chernov 197b93bb974SMike Karels /* 198b93bb974SMike Karels * Compute default output line length before processing options. 199b93bb974SMike Karels * If COLUMNS is set, use it. Otherwise, if this is part of an 200b93bb974SMike Karels * interactive job (i.e. one associated with a terminal), use 201b93bb974SMike Karels * the terminal width. "Interactive" is determined by whether 202b93bb974SMike Karels * any of stdout, stderr, or stdin is a terminal. The intent 203b93bb974SMike Karels * is that "ps", "ps | more", and "ps | grep" all use the same 204b93bb974SMike Karels * default line length unless -w is specified. 20530417f2cSMike Karels * 20630417f2cSMike Karels * If not interactive, the default length was traditionally 79. 20730417f2cSMike Karels * It has been changed to unlimited. This is mostly for the 20830417f2cSMike Karels * benefit of non-interactive scripts, which arguably should 20930417f2cSMike Karels * use -ww, but is compatible with Linux. 210b93bb974SMike Karels */ 2114f18100dSTim J. Robbins if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') 2124f18100dSTim J. Robbins termwidth = atoi(cols); 2134f18100dSTim J. Robbins else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 2144b88c807SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 2154b88c807SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 2164b88c807SRodney W. Grimes ws.ws_col == 0) 21730417f2cSMike Karels termwidth = UNLIMITED; 2184b88c807SRodney W. Grimes else 2194b88c807SRodney W. Grimes termwidth = ws.ws_col - 1; 2204b88c807SRodney W. Grimes 22141623b2dSMaxim Sobolev /* 222c675340aSGarance A Drosehn * Hide a number of option-processing kludges in a separate routine, 223c675340aSGarance A Drosehn * to support some historical BSD behaviors, such as `ps axu'. 22441623b2dSMaxim Sobolev */ 225c675340aSGarance A Drosehn if (argc > 1) 226bf46a3bfSGarance A Drosehn argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]); 2274b88c807SRodney W. Grimes 22864b0bf0bSPawel Jakub Dawidek pidmax_init(); 22964b0bf0bSPawel Jakub Dawidek 230044fce53SBrian Somers all = descendancy = _fmt = nselectors = optfatal = 0; 231352b6523SGarance A Drosehn prtheader = showthreads = wflag = xkeep_implied = 0; 232352b6523SGarance A Drosehn xkeep = -1; /* Neither -x nor -X. */ 233a4c8a745SGarance A Drosehn init_list(&gidlist, addelem_gid, sizeof(gid_t), "group"); 23413767130SBryan Drewery init_list(&jidlist, addelem_jid, sizeof(int), "jail id"); 235a4c8a745SGarance A Drosehn init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group"); 236a4c8a745SGarance A Drosehn init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id"); 237a4c8a745SGarance A Drosehn init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser"); 238a4c8a745SGarance A Drosehn init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id"); 239a4c8a745SGarance A Drosehn init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty"); 240a4c8a745SGarance A Drosehn init_list(&uidlist, addelem_uid, sizeof(uid_t), "user"); 241c08dcaf1SRebecca Cran memf = _PATH_DEVNULL; 242c08dcaf1SRebecca Cran nlistf = NULL; 2438beb1a2fSMarcel Moolenaar 2448beb1a2fSMarcel Moolenaar argc = xo_parse_args(argc, argv); 2458beb1a2fSMarcel Moolenaar if (argc < 0) 2468beb1a2fSMarcel Moolenaar exit(1); 2478beb1a2fSMarcel Moolenaar 24841623b2dSMaxim Sobolev while ((ch = getopt(argc, argv, PS_ARGS)) != -1) 2490c025af9SKevin Lo switch (ch) { 250a4c8a745SGarance A Drosehn case 'A': 251a4c8a745SGarance A Drosehn /* 252a4c8a745SGarance A Drosehn * Exactly the same as `-ax'. This has been 253bf2fe08eSUlrich Spörlein * added for compatibility with SUSv3, but for 254a4c8a745SGarance A Drosehn * now it will not be described in the man page. 255a4c8a745SGarance A Drosehn */ 256a4c8a745SGarance A Drosehn all = xkeep = 1; 257a4c8a745SGarance A Drosehn break; 2584b88c807SRodney W. Grimes case 'a': 2594b88c807SRodney W. Grimes all = 1; 2604b88c807SRodney W. Grimes break; 2614b88c807SRodney W. Grimes case 'C': 2624b88c807SRodney W. Grimes rawcpu = 1; 2634b88c807SRodney W. Grimes break; 264db91faacSPeter Wemm case 'c': 265db91faacSPeter Wemm cflag = 1; 266db91faacSPeter Wemm break; 267*5c0a1c15SPiotr Pawel Stefaniak case 'D': { 268*5c0a1c15SPiotr Pawel Stefaniak size_t len = strlen(optarg); 269*5c0a1c15SPiotr Pawel Stefaniak 270*5c0a1c15SPiotr Pawel Stefaniak if (len <= 2 && 271*5c0a1c15SPiotr Pawel Stefaniak strncasecmp(optarg, "up", len) == 0) 272*5c0a1c15SPiotr Pawel Stefaniak directions |= UP; 273*5c0a1c15SPiotr Pawel Stefaniak else if (len <= 4 && 274*5c0a1c15SPiotr Pawel Stefaniak strncasecmp(optarg, "down", len) == 0) 275*5c0a1c15SPiotr Pawel Stefaniak directions |= DOWN; 276*5c0a1c15SPiotr Pawel Stefaniak else if (len <= 4 && 277*5c0a1c15SPiotr Pawel Stefaniak strncasecmp(optarg, "both", len) == 0) 278*5c0a1c15SPiotr Pawel Stefaniak directions |= BOTH; 279*5c0a1c15SPiotr Pawel Stefaniak break; 280*5c0a1c15SPiotr Pawel Stefaniak } 281044fce53SBrian Somers case 'd': 282044fce53SBrian Somers descendancy = 1; 283044fce53SBrian Somers break; 284db91faacSPeter Wemm case 'e': /* XXX set ufmt */ 285db91faacSPeter Wemm needenv = 1; 286db91faacSPeter Wemm break; 2874a355d17SGarance A Drosehn #ifdef LAZY_PS 2884a355d17SGarance A Drosehn case 'f': 2894a355d17SGarance A Drosehn if (getuid() == 0 || getgid() == 0) 2904a355d17SGarance A Drosehn forceuread = 1; 2914a355d17SGarance A Drosehn break; 2924a355d17SGarance A Drosehn #endif 293a4c8a745SGarance A Drosehn case 'G': 294a4c8a745SGarance A Drosehn add_list(&gidlist, optarg); 295a4c8a745SGarance A Drosehn xkeep_implied = 1; 296a4c8a745SGarance A Drosehn nselectors++; 297a4c8a745SGarance A Drosehn break; 2984b88c807SRodney W. Grimes case 'g': 299352b6523SGarance A Drosehn #if 0 300ba50b0e0SGarance A Drosehn /*- 301352b6523SGarance A Drosehn * XXX - This SUSv3 behavior is still under debate 302352b6523SGarance A Drosehn * since it conflicts with the (undocumented) 303352b6523SGarance A Drosehn * `-g' option. So we skip it for now. 304352b6523SGarance A Drosehn */ 305a4c8a745SGarance A Drosehn add_list(&pgrplist, optarg); 306a4c8a745SGarance A Drosehn xkeep_implied = 1; 307a4c8a745SGarance A Drosehn nselectors++; 308a4c8a745SGarance A Drosehn break; 309a4c8a745SGarance A Drosehn #else 310352b6523SGarance A Drosehn /* The historical BSD-ish (from SunOS) behavior. */ 3114b88c807SRodney W. Grimes break; /* no-op */ 312a4c8a745SGarance A Drosehn #endif 31348b8c0deSScott Long case 'H': 314d75c1d83SDaniel Eischen showthreads = KERN_PROC_INC_THREAD; 31548b8c0deSScott Long break; 3164b88c807SRodney W. Grimes case 'h': 3174b88c807SRodney W. Grimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 3184b88c807SRodney W. Grimes break; 31913767130SBryan Drewery case 'J': 32013767130SBryan Drewery add_list(&jidlist, optarg); 32113767130SBryan Drewery xkeep_implied = 1; 32213767130SBryan Drewery nselectors++; 32313767130SBryan Drewery break; 3244b88c807SRodney W. Grimes case 'j': 325fde411d5SJuli Mallett parsefmt(jfmt, 0); 326871e8d8cSMark Murray _fmt = 1; 3274b88c807SRodney W. Grimes jfmt[0] = '\0'; 3284b88c807SRodney W. Grimes break; 3294b88c807SRodney W. Grimes case 'L': 3304b88c807SRodney W. Grimes showkey(); 3314b88c807SRodney W. Grimes exit(0); 3324b88c807SRodney W. Grimes case 'l': 333fde411d5SJuli Mallett parsefmt(lfmt, 0); 334871e8d8cSMark Murray _fmt = 1; 3354b88c807SRodney W. Grimes lfmt[0] = '\0'; 3364b88c807SRodney W. Grimes break; 3374b88c807SRodney W. Grimes case 'M': 3384b88c807SRodney W. Grimes memf = optarg; 3394b88c807SRodney W. Grimes break; 3404b88c807SRodney W. Grimes case 'm': 3414b88c807SRodney W. Grimes sortby = SORTMEM; 3424b88c807SRodney W. Grimes break; 3434b88c807SRodney W. Grimes case 'N': 3444b88c807SRodney W. Grimes nlistf = optarg; 3454b88c807SRodney W. Grimes break; 3464b88c807SRodney W. Grimes case 'O': 347fde411d5SJuli Mallett parsefmt(o1, 1); 348fde411d5SJuli Mallett parsefmt(optarg, 1); 349fde411d5SJuli Mallett parsefmt(o2, 1); 3504b88c807SRodney W. Grimes o1[0] = o2[0] = '\0'; 351871e8d8cSMark Murray _fmt = 1; 3524b88c807SRodney W. Grimes break; 3534b88c807SRodney W. Grimes case 'o': 354fde411d5SJuli Mallett parsefmt(optarg, 1); 355871e8d8cSMark Murray _fmt = 1; 3564b88c807SRodney W. Grimes break; 3574b88c807SRodney W. Grimes case 'p': 358a4c8a745SGarance A Drosehn add_list(&pidlist, optarg); 359a4c8a745SGarance A Drosehn /* 360a4c8a745SGarance A Drosehn * Note: `-p' does not *set* xkeep, but any values 361a4c8a745SGarance A Drosehn * from pidlist are checked before xkeep is. That 362a4c8a745SGarance A Drosehn * way they are always matched, even if the user 363a4c8a745SGarance A Drosehn * specifies `-X'. 364a4c8a745SGarance A Drosehn */ 365a4c8a745SGarance A Drosehn nselectors++; 3664b88c807SRodney W. Grimes break; 367a4c8a745SGarance A Drosehn #if 0 368a4c8a745SGarance A Drosehn case 'R': 369ba50b0e0SGarance A Drosehn /*- 370352b6523SGarance A Drosehn * XXX - This un-standard option is still under 371352b6523SGarance A Drosehn * debate. This is what SUSv3 defines as 372352b6523SGarance A Drosehn * the `-U' option, and while it would be 373352b6523SGarance A Drosehn * nice to have, it could cause even more 374352b6523SGarance A Drosehn * confusion to implement it as `-R'. 375352b6523SGarance A Drosehn */ 376a4c8a745SGarance A Drosehn add_list(&ruidlist, optarg); 377a4c8a745SGarance A Drosehn xkeep_implied = 1; 378a4c8a745SGarance A Drosehn nselectors++; 379a4c8a745SGarance A Drosehn break; 380a4c8a745SGarance A Drosehn #endif 3814b88c807SRodney W. Grimes case 'r': 3824b88c807SRodney W. Grimes sortby = SORTCPU; 3834b88c807SRodney W. Grimes break; 3844b88c807SRodney W. Grimes case 'S': 3854b88c807SRodney W. Grimes sumrusage = 1; 3864b88c807SRodney W. Grimes break; 387a4c8a745SGarance A Drosehn #if 0 388a4c8a745SGarance A Drosehn case 's': 389ba50b0e0SGarance A Drosehn /*- 390352b6523SGarance A Drosehn * XXX - This non-standard option is still under 391352b6523SGarance A Drosehn * debate. This *is* supported on Solaris, 392352b6523SGarance A Drosehn * Linux, and IRIX, but conflicts with `-s' 393352b6523SGarance A Drosehn * on NetBSD and maybe some older BSD's. 394352b6523SGarance A Drosehn */ 395a4c8a745SGarance A Drosehn add_list(&sesslist, optarg); 396a4c8a745SGarance A Drosehn xkeep_implied = 1; 397a4c8a745SGarance A Drosehn nselectors++; 398a4c8a745SGarance A Drosehn break; 399a4c8a745SGarance A Drosehn #endif 4004b88c807SRodney W. Grimes case 'T': 4014b88c807SRodney W. Grimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 4028beb1a2fSMarcel Moolenaar xo_errx(1, "stdin: not a terminal"); 4034b88c807SRodney W. Grimes /* FALLTHROUGH */ 404a4c8a745SGarance A Drosehn case 't': 405a4c8a745SGarance A Drosehn add_list(&ttylist, optarg); 406a4c8a745SGarance A Drosehn xkeep_implied = 1; 407a4c8a745SGarance A Drosehn nselectors++; 4084b88c807SRodney W. Grimes break; 40973eb8310SPeter Wemm case 'U': 410a4c8a745SGarance A Drosehn /* This is what SUSv3 defines as the `-u' option. */ 411a4c8a745SGarance A Drosehn add_list(&uidlist, optarg); 412a4c8a745SGarance A Drosehn xkeep_implied = 1; 413a4c8a745SGarance A Drosehn nselectors++; 41473eb8310SPeter Wemm break; 4154b88c807SRodney W. Grimes case 'u': 416fde411d5SJuli Mallett parsefmt(ufmt, 0); 4174b88c807SRodney W. Grimes sortby = SORTCPU; 418871e8d8cSMark Murray _fmt = 1; 4194b88c807SRodney W. Grimes ufmt[0] = '\0'; 4204b88c807SRodney W. Grimes break; 4214b88c807SRodney W. Grimes case 'v': 422fde411d5SJuli Mallett parsefmt(vfmt, 0); 4234b88c807SRodney W. Grimes sortby = SORTMEM; 424871e8d8cSMark Murray _fmt = 1; 4254b88c807SRodney W. Grimes vfmt[0] = '\0'; 4264b88c807SRodney W. Grimes break; 4274b88c807SRodney W. Grimes case 'w': 4284b88c807SRodney W. Grimes if (wflag) 4294b88c807SRodney W. Grimes termwidth = UNLIMITED; 430ef1d40daSConrad Meyer else if (termwidth < 131 && termwidth != UNLIMITED) 4314b88c807SRodney W. Grimes termwidth = 131; 4324b88c807SRodney W. Grimes wflag++; 4334b88c807SRodney W. Grimes break; 434a4c8a745SGarance A Drosehn case 'X': 435a4c8a745SGarance A Drosehn /* 436a4c8a745SGarance A Drosehn * Note that `-X' and `-x' are not standard "selector" 437a4c8a745SGarance A Drosehn * options. For most selector-options, we check *all* 438a4c8a745SGarance A Drosehn * processes to see if any are matched by the given 439a4c8a745SGarance A Drosehn * value(s). After we have a set of all the matched 440a4c8a745SGarance A Drosehn * processes, then `-X' and `-x' govern whether we 441a4c8a745SGarance A Drosehn * modify that *matched* set for processes which do 442a4c8a745SGarance A Drosehn * not have a controlling terminal. `-X' causes 443a4c8a745SGarance A Drosehn * those processes to be deleted from the matched 444a4c8a745SGarance A Drosehn * set, while `-x' causes them to be kept. 445a4c8a745SGarance A Drosehn */ 446a4c8a745SGarance A Drosehn xkeep = 0; 447a4c8a745SGarance A Drosehn break; 4484b88c807SRodney W. Grimes case 'x': 449a4c8a745SGarance A Drosehn xkeep = 1; 4504b88c807SRodney W. Grimes break; 4517304f61fSBrian Feldman case 'Z': 452fde411d5SJuli Mallett parsefmt(Zfmt, 0); 4537304f61fSBrian Feldman Zfmt[0] = '\0'; 4547304f61fSBrian Feldman break; 4554b88c807SRodney W. Grimes case '?': 4564b88c807SRodney W. Grimes default: 4574b88c807SRodney W. Grimes usage(); 4584b88c807SRodney W. Grimes } 4594b88c807SRodney W. Grimes argc -= optind; 4604b88c807SRodney W. Grimes argv += optind; 461c675340aSGarance A Drosehn 462c675340aSGarance A Drosehn /* 463c675340aSGarance A Drosehn * If there arguments after processing all the options, attempt 464c675340aSGarance A Drosehn * to treat them as a list of process ids. 465c675340aSGarance A Drosehn */ 466c675340aSGarance A Drosehn while (*argv) { 467c675340aSGarance A Drosehn if (!isdigitch(**argv)) 468c675340aSGarance A Drosehn break; 469c675340aSGarance A Drosehn add_list(&pidlist, *argv); 470c675340aSGarance A Drosehn argv++; 471c675340aSGarance A Drosehn } 472c675340aSGarance A Drosehn if (*argv) { 4738beb1a2fSMarcel Moolenaar xo_warnx("illegal argument: %s\n", *argv); 474c675340aSGarance A Drosehn usage(); 475c675340aSGarance A Drosehn } 476a4c8a745SGarance A Drosehn if (optfatal) 477352b6523SGarance A Drosehn exit(1); /* Error messages already printed. */ 478352b6523SGarance A Drosehn if (xkeep < 0) /* Neither -X nor -x was specified. */ 479a4c8a745SGarance A Drosehn xkeep = xkeep_implied; 480a4c8a745SGarance A Drosehn 481831c910aSRuslan Ermilov kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 482ec23a763SMarcelo Araujo if (kd == NULL) 4838beb1a2fSMarcel Moolenaar xo_errx(1, "%s", errbuf); 4844b88c807SRodney W. Grimes 485871e8d8cSMark Murray if (!_fmt) 486fde411d5SJuli Mallett parsefmt(dfmt, 0); 4874b88c807SRodney W. Grimes 488bf27a225SMath Ieu if (!all && nselectors == 0) { 489d822163fSGarance A Drosehn uidlist.l.ptr = malloc(sizeof(uid_t)); 490d822163fSGarance A Drosehn if (uidlist.l.ptr == NULL) 4918beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 492a4c8a745SGarance A Drosehn nselectors = 1; 493a4c8a745SGarance A Drosehn uidlist.count = uidlist.maxcount = 1; 494d822163fSGarance A Drosehn *uidlist.l.uids = getuid(); 495cf22dcfcSBrian Somers } 4964b88c807SRodney W. Grimes 4974b88c807SRodney W. Grimes /* 4984b88c807SRodney W. Grimes * scan requested variables, noting what structures are needed, 499bdfebd84SKris Kennaway * and adjusting header widths as appropriate. 5004b88c807SRodney W. Grimes */ 5014b88c807SRodney W. Grimes scanvars(); 502a4c8a745SGarance A Drosehn 5034b88c807SRodney W. Grimes /* 504a4c8a745SGarance A Drosehn * Get process list. If the user requested just one selector- 505a4c8a745SGarance A Drosehn * option, then kvm_getprocs can be asked to return just those 506a4c8a745SGarance A Drosehn * processes. Otherwise, have it return all processes, and 507a4c8a745SGarance A Drosehn * then this routine will search that full list and select the 508a4c8a745SGarance A Drosehn * processes which match any of the user's selector-options. 5094b88c807SRodney W. Grimes */ 510d75c1d83SDaniel Eischen what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC; 51148b8c0deSScott Long flag = 0; 512a4c8a745SGarance A Drosehn if (nselectors == 1) { 5137bd42165SGarance A Drosehn if (gidlist.count == 1) { 5147bd42165SGarance A Drosehn what = KERN_PROC_RGID | showthreads; 5157bd42165SGarance A Drosehn flag = *gidlist.l.gids; 5167bd42165SGarance A Drosehn nselectors = 0; 5177bd42165SGarance A Drosehn } else if (pgrplist.count == 1) { 518a4c8a745SGarance A Drosehn what = KERN_PROC_PGRP | showthreads; 519d822163fSGarance A Drosehn flag = *pgrplist.l.pids; 520a4c8a745SGarance A Drosehn nselectors = 0; 521*5c0a1c15SPiotr Pawel Stefaniak } else if (pidlist.count == 1 && directions == NONE) { 522a4c8a745SGarance A Drosehn what = KERN_PROC_PID | showthreads; 523d822163fSGarance A Drosehn flag = *pidlist.l.pids; 524a4c8a745SGarance A Drosehn nselectors = 0; 525a4c8a745SGarance A Drosehn } else if (ruidlist.count == 1) { 526a4c8a745SGarance A Drosehn what = KERN_PROC_RUID | showthreads; 527d822163fSGarance A Drosehn flag = *ruidlist.l.uids; 528a4c8a745SGarance A Drosehn nselectors = 0; 529a4c8a745SGarance A Drosehn } else if (sesslist.count == 1) { 530a4c8a745SGarance A Drosehn what = KERN_PROC_SESSION | showthreads; 531d822163fSGarance A Drosehn flag = *sesslist.l.pids; 532a4c8a745SGarance A Drosehn nselectors = 0; 533a4c8a745SGarance A Drosehn } else if (ttylist.count == 1) { 534a4c8a745SGarance A Drosehn what = KERN_PROC_TTY | showthreads; 535d822163fSGarance A Drosehn flag = *ttylist.l.ttys; 536a4c8a745SGarance A Drosehn nselectors = 0; 537a4c8a745SGarance A Drosehn } else if (uidlist.count == 1) { 538a4c8a745SGarance A Drosehn what = KERN_PROC_UID | showthreads; 539d822163fSGarance A Drosehn flag = *uidlist.l.uids; 540a4c8a745SGarance A Drosehn nselectors = 0; 541a4c8a745SGarance A Drosehn } 5424b88c807SRodney W. Grimes } 543d75c1d83SDaniel Eischen 5444b88c807SRodney W. Grimes /* 5454b88c807SRodney W. Grimes * select procs 5464b88c807SRodney W. Grimes */ 547a4c8a745SGarance A Drosehn nentries = -1; 5484e8b6a6fSGarance A Drosehn kp = kvm_getprocs(kd, what, flag, &nentries); 549d7026b96SEdward Tomasz Napierala /* 550d7026b96SEdward Tomasz Napierala * Ignore ESRCH to preserve behaviour of "ps -p nonexistent-pid" 551d7026b96SEdward Tomasz Napierala * not reporting an error. 552d7026b96SEdward Tomasz Napierala */ 553d7026b96SEdward Tomasz Napierala if ((kp == NULL && errno != ESRCH) || (kp != NULL && nentries < 0)) 5548beb1a2fSMarcel Moolenaar xo_errx(1, "%s", kvm_geterr(kd)); 555a4c8a745SGarance A Drosehn nkept = 0; 556*5c0a1c15SPiotr Pawel Stefaniak pid_count.initial = pidlist.count; 557*5c0a1c15SPiotr Pawel Stefaniak if (directions & DOWN) 558*5c0a1c15SPiotr Pawel Stefaniak for (elem = 0; elem < pidlist.count; elem++) 559*5c0a1c15SPiotr Pawel Stefaniak for (i = 0; i < nentries; i++) { 560*5c0a1c15SPiotr Pawel Stefaniak if (kp[i].ki_ppid == kp[i].ki_pid) 561*5c0a1c15SPiotr Pawel Stefaniak continue; 562*5c0a1c15SPiotr Pawel Stefaniak if (kp[i].ki_ppid == pidlist.l.pids[elem]) { 563*5c0a1c15SPiotr Pawel Stefaniak if (pidlist.count >= pidlist.maxcount) 564*5c0a1c15SPiotr Pawel Stefaniak expand_list(&pidlist); 565*5c0a1c15SPiotr Pawel Stefaniak pidlist.l.pids[pidlist.count++] = kp[i].ki_pid; 566*5c0a1c15SPiotr Pawel Stefaniak } 567*5c0a1c15SPiotr Pawel Stefaniak } 568*5c0a1c15SPiotr Pawel Stefaniak pid_count.traversed = pidlist.count; 569*5c0a1c15SPiotr Pawel Stefaniak if (directions & UP) 570*5c0a1c15SPiotr Pawel Stefaniak for (elem = 0; elem < pidlist.count; elem++) { 571*5c0a1c15SPiotr Pawel Stefaniak if (elem >= pid_count.initial && elem < pid_count.traversed) 572*5c0a1c15SPiotr Pawel Stefaniak continue; 573*5c0a1c15SPiotr Pawel Stefaniak for (i = 0; i < nentries; i++) { 574*5c0a1c15SPiotr Pawel Stefaniak if (kp[i].ki_ppid == kp[i].ki_pid) 575*5c0a1c15SPiotr Pawel Stefaniak continue; 576*5c0a1c15SPiotr Pawel Stefaniak if (kp[i].ki_pid == pidlist.l.pids[elem]) { 577*5c0a1c15SPiotr Pawel Stefaniak if (pidlist.count >= pidlist.maxcount) 578*5c0a1c15SPiotr Pawel Stefaniak expand_list(&pidlist); 579*5c0a1c15SPiotr Pawel Stefaniak pidlist.l.pids[pidlist.count++] = kp[i].ki_ppid; 580*5c0a1c15SPiotr Pawel Stefaniak } 581*5c0a1c15SPiotr Pawel Stefaniak } 582*5c0a1c15SPiotr Pawel Stefaniak } 5834e8b6a6fSGarance A Drosehn if (nentries > 0) { 5844b88c807SRodney W. Grimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 5858beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 5864b88c807SRodney W. Grimes for (i = nentries; --i >= 0; ++kp) { 587a4c8a745SGarance A Drosehn /* 588a4c8a745SGarance A Drosehn * If the user specified multiple selection-criteria, 589a4c8a745SGarance A Drosehn * then keep any process matched by the inclusive OR 590a4c8a745SGarance A Drosehn * of all the selection-criteria given. 591a4c8a745SGarance A Drosehn */ 592a4c8a745SGarance A Drosehn if (pidlist.count > 0) { 593a4c8a745SGarance A Drosehn for (elem = 0; elem < pidlist.count; elem++) 594d822163fSGarance A Drosehn if (kp->ki_pid == pidlist.l.pids[elem]) 595a4c8a745SGarance A Drosehn goto keepit; 596a4c8a745SGarance A Drosehn } 597a4c8a745SGarance A Drosehn /* 598a4c8a745SGarance A Drosehn * Note that we had to process pidlist before 599a4c8a745SGarance A Drosehn * filtering out processes which do not have 600a4c8a745SGarance A Drosehn * a controlling terminal. 601a4c8a745SGarance A Drosehn */ 602a4c8a745SGarance A Drosehn if (xkeep == 0) { 603a4c8a745SGarance A Drosehn if ((kp->ki_tdev == NODEV || 604a4c8a745SGarance A Drosehn (kp->ki_flag & P_CONTROLT) == 0)) 605a4c8a745SGarance A Drosehn continue; 606a4c8a745SGarance A Drosehn } 607a4c8a745SGarance A Drosehn if (nselectors == 0) 608a4c8a745SGarance A Drosehn goto keepit; 609a4c8a745SGarance A Drosehn if (gidlist.count > 0) { 610a4c8a745SGarance A Drosehn for (elem = 0; elem < gidlist.count; elem++) 611d822163fSGarance A Drosehn if (kp->ki_rgid == gidlist.l.gids[elem]) 612a4c8a745SGarance A Drosehn goto keepit; 613a4c8a745SGarance A Drosehn } 61413767130SBryan Drewery if (jidlist.count > 0) { 61513767130SBryan Drewery for (elem = 0; elem < jidlist.count; elem++) 61613767130SBryan Drewery if (kp->ki_jid == jidlist.l.jids[elem]) 61713767130SBryan Drewery goto keepit; 61813767130SBryan Drewery } 619a4c8a745SGarance A Drosehn if (pgrplist.count > 0) { 620a4c8a745SGarance A Drosehn for (elem = 0; elem < pgrplist.count; elem++) 621d822163fSGarance A Drosehn if (kp->ki_pgid == 622d822163fSGarance A Drosehn pgrplist.l.pids[elem]) 623a4c8a745SGarance A Drosehn goto keepit; 624a4c8a745SGarance A Drosehn } 625a4c8a745SGarance A Drosehn if (ruidlist.count > 0) { 626a4c8a745SGarance A Drosehn for (elem = 0; elem < ruidlist.count; elem++) 627d822163fSGarance A Drosehn if (kp->ki_ruid == 628d822163fSGarance A Drosehn ruidlist.l.uids[elem]) 629a4c8a745SGarance A Drosehn goto keepit; 630a4c8a745SGarance A Drosehn } 631a4c8a745SGarance A Drosehn if (sesslist.count > 0) { 632a4c8a745SGarance A Drosehn for (elem = 0; elem < sesslist.count; elem++) 633d822163fSGarance A Drosehn if (kp->ki_sid == sesslist.l.pids[elem]) 634a4c8a745SGarance A Drosehn goto keepit; 635a4c8a745SGarance A Drosehn } 636a4c8a745SGarance A Drosehn if (ttylist.count > 0) { 637a4c8a745SGarance A Drosehn for (elem = 0; elem < ttylist.count; elem++) 638d822163fSGarance A Drosehn if (kp->ki_tdev == ttylist.l.ttys[elem]) 639a4c8a745SGarance A Drosehn goto keepit; 640a4c8a745SGarance A Drosehn } 641a4c8a745SGarance A Drosehn if (uidlist.count > 0) { 642a4c8a745SGarance A Drosehn for (elem = 0; elem < uidlist.count; elem++) 643d822163fSGarance A Drosehn if (kp->ki_uid == uidlist.l.uids[elem]) 644a4c8a745SGarance A Drosehn goto keepit; 645a4c8a745SGarance A Drosehn } 646a4c8a745SGarance A Drosehn /* 647a4c8a745SGarance A Drosehn * This process did not match any of the user's 648a4c8a745SGarance A Drosehn * selector-options, so skip the process. 649a4c8a745SGarance A Drosehn */ 650a4c8a745SGarance A Drosehn continue; 651a4c8a745SGarance A Drosehn 652a4c8a745SGarance A Drosehn keepit: 6534bac4483SGarance A Drosehn next_KINFO = &kinfo[nkept]; 6544bac4483SGarance A Drosehn next_KINFO->ki_p = kp; 655044fce53SBrian Somers next_KINFO->ki_d.level = 0; 656044fce53SBrian Somers next_KINFO->ki_d.prefix = NULL; 6574bac4483SGarance A Drosehn next_KINFO->ki_pcpu = getpcpu(next_KINFO); 6584bac4483SGarance A Drosehn if (sortby == SORTMEM) 6594bac4483SGarance A Drosehn next_KINFO->ki_memsize = kp->ki_tsize + 6604bac4483SGarance A Drosehn kp->ki_dsize + kp->ki_ssize; 6614b88c807SRodney W. Grimes if (needuser) 6624bac4483SGarance A Drosehn saveuser(next_KINFO); 663a4c8a745SGarance A Drosehn nkept++; 6644b88c807SRodney W. Grimes } 6654e8b6a6fSGarance A Drosehn } 6666a2d726bSJordan K. Hubbard 6676a2d726bSJordan K. Hubbard sizevars(); 6686a2d726bSJordan K. Hubbard 6691d1143ecSEdward Tomasz Napierala if (nkept == 0) { 6704b88c807SRodney W. Grimes printheader(); 67181fc45fcSKonstantin Belousov xo_finish(); 672f8c9c11cSWill Andrews exit(1); 6731d1143ecSEdward Tomasz Napierala } 674a4c8a745SGarance A Drosehn 6754b88c807SRodney W. Grimes /* 6764b88c807SRodney W. Grimes * sort proc list 6774b88c807SRodney W. Grimes */ 678a4c8a745SGarance A Drosehn qsort(kinfo, nkept, sizeof(KINFO), pscomp); 679044fce53SBrian Somers 680044fce53SBrian Somers /* 681044fce53SBrian Somers * We want things in descendant order 682044fce53SBrian Somers */ 683044fce53SBrian Somers if (descendancy) 684044fce53SBrian Somers descendant_sort(kinfo, nkept); 685044fce53SBrian Somers 6861d1143ecSEdward Tomasz Napierala 6874b88c807SRodney W. Grimes /* 6881d1143ecSEdward Tomasz Napierala * Prepare formatted output. 6891d1143ecSEdward Tomasz Napierala */ 6901d1143ecSEdward Tomasz Napierala for (i = 0; i < nkept; i++) 6911d1143ecSEdward Tomasz Napierala format_output(&kinfo[i]); 6921d1143ecSEdward Tomasz Napierala 6931d1143ecSEdward Tomasz Napierala /* 6941d1143ecSEdward Tomasz Napierala * Print header. 6951d1143ecSEdward Tomasz Napierala */ 6968beb1a2fSMarcel Moolenaar xo_open_container("process-information"); 6971d1143ecSEdward Tomasz Napierala printheader(); 6988beb1a2fSMarcel Moolenaar if (xo_get_style(NULL) != XO_STYLE_TEXT) 6998beb1a2fSMarcel Moolenaar termwidth = UNLIMITED; 7001d1143ecSEdward Tomasz Napierala 7011d1143ecSEdward Tomasz Napierala /* 7021d1143ecSEdward Tomasz Napierala * Output formatted lines. 7034b88c807SRodney W. Grimes */ 7048beb1a2fSMarcel Moolenaar xo_open_list("process"); 705a4c8a745SGarance A Drosehn for (i = lineno = 0; i < nkept; i++) { 7061d1143ecSEdward Tomasz Napierala linelen = 0; 7078beb1a2fSMarcel Moolenaar xo_open_instance("process"); 708bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 7091d1143ecSEdward Tomasz Napierala ks = STAILQ_FIRST(&kinfo[i].ki_ks); 7101d1143ecSEdward Tomasz Napierala STAILQ_REMOVE_HEAD(&kinfo[i].ki_ks, ks_next); 71138494effSUlrich Spörlein /* Truncate rightmost column if necessary. */ 7128beb1a2fSMarcel Moolenaar fwidthmax = _POSIX2_LINE_MAX; 7131d1143ecSEdward Tomasz Napierala if (STAILQ_NEXT(vent, next_ve) == NULL && 7141d1143ecSEdward Tomasz Napierala termwidth != UNLIMITED && ks->ks_str != NULL) { 7151d1143ecSEdward Tomasz Napierala left = termwidth - linelen; 7161d1143ecSEdward Tomasz Napierala if (left > 0 && left < (int)strlen(ks->ks_str)) 7178beb1a2fSMarcel Moolenaar fwidthmax = left; 7181d1143ecSEdward Tomasz Napierala } 7198beb1a2fSMarcel Moolenaar 7201d1143ecSEdward Tomasz Napierala str = ks->ks_str; 7211d1143ecSEdward Tomasz Napierala if (str == NULL) 7221d1143ecSEdward Tomasz Napierala str = "-"; 7231d1143ecSEdward Tomasz Napierala /* No padding for the last column, if it's LJUST. */ 7248beb1a2fSMarcel Moolenaar fwidthmin = (xo_get_style(NULL) != XO_STYLE_TEXT || 7258beb1a2fSMarcel Moolenaar (STAILQ_NEXT(vent, next_ve) == NULL && 7268beb1a2fSMarcel Moolenaar (vent->var->flag & LJUST))) ? 0 : vent->var->width; 727aa8ab146SYuri Pankov snprintf(fmtbuf, sizeof(fmtbuf), "{:%s/%%%s%d..%dhs}", 728c6a5ff71SEitan Adler vent->var->field ? vent->var->field : vent->var->name, 7298beb1a2fSMarcel Moolenaar (vent->var->flag & LJUST) ? "-" : "", 7308beb1a2fSMarcel Moolenaar fwidthmin, fwidthmax); 7318beb1a2fSMarcel Moolenaar xo_emit(fmtbuf, str); 7328beb1a2fSMarcel Moolenaar linelen += fwidthmin; 7331d1143ecSEdward Tomasz Napierala 7341d1143ecSEdward Tomasz Napierala if (ks->ks_str != NULL) { 7351d1143ecSEdward Tomasz Napierala free(ks->ks_str); 7361d1143ecSEdward Tomasz Napierala ks->ks_str = NULL; 7371d1143ecSEdward Tomasz Napierala } 7381d1143ecSEdward Tomasz Napierala free(ks); 7391d1143ecSEdward Tomasz Napierala ks = NULL; 7401d1143ecSEdward Tomasz Napierala 7411d1143ecSEdward Tomasz Napierala if (STAILQ_NEXT(vent, next_ve) != NULL) { 7428beb1a2fSMarcel Moolenaar xo_emit("{P: }"); 7431d1143ecSEdward Tomasz Napierala linelen++; 7441d1143ecSEdward Tomasz Napierala } 7454b88c807SRodney W. Grimes } 7468beb1a2fSMarcel Moolenaar xo_emit("\n"); 7478beb1a2fSMarcel Moolenaar xo_close_instance("process"); 7484b88c807SRodney W. Grimes if (prtheader && lineno++ == prtheader - 4) { 7498beb1a2fSMarcel Moolenaar xo_emit("\n"); 7504b88c807SRodney W. Grimes printheader(); 7514b88c807SRodney W. Grimes lineno = 0; 7524b88c807SRodney W. Grimes } 7534b88c807SRodney W. Grimes } 7548beb1a2fSMarcel Moolenaar xo_close_list("process"); 7558beb1a2fSMarcel Moolenaar xo_close_container("process-information"); 7568beb1a2fSMarcel Moolenaar xo_finish(); 7578beb1a2fSMarcel Moolenaar 758a4c8a745SGarance A Drosehn free_list(&gidlist); 75913767130SBryan Drewery free_list(&jidlist); 760a4c8a745SGarance A Drosehn free_list(&pidlist); 761a4c8a745SGarance A Drosehn free_list(&pgrplist); 762a4c8a745SGarance A Drosehn free_list(&ruidlist); 763a4c8a745SGarance A Drosehn free_list(&sesslist); 764a4c8a745SGarance A Drosehn free_list(&ttylist); 765a4c8a745SGarance A Drosehn free_list(&uidlist); 766044fce53SBrian Somers for (i = 0; i < nkept; i++) 767044fce53SBrian Somers free(kinfo[i].ki_d.prefix); 768044fce53SBrian Somers free(kinfo); 7692e6c6ac4SGarance A Drosehn 7704b88c807SRodney W. Grimes exit(eval); 7714b88c807SRodney W. Grimes } 7724b88c807SRodney W. Grimes 773a4c8a745SGarance A Drosehn static int 774a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem) 7754e8b6a6fSGarance A Drosehn { 776a4c8a745SGarance A Drosehn struct group *grp; 777a4c8a745SGarance A Drosehn const char *nameorID; 778a4c8a745SGarance A Drosehn char *endp; 7790b42be7cSGarance A Drosehn u_long bigtemp; 7804e8b6a6fSGarance A Drosehn 781a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 782a4c8a745SGarance A Drosehn if (*elem == '\0') 7838beb1a2fSMarcel Moolenaar xo_warnx("Invalid (zero-length) %s name", inf->lname); 784a4c8a745SGarance A Drosehn else 7858beb1a2fSMarcel Moolenaar xo_warnx("%s name too long: %s", inf->lname, elem); 786a4c8a745SGarance A Drosehn optfatal = 1; 787352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 7884e8b6a6fSGarance A Drosehn } 789a4c8a745SGarance A Drosehn 7904e8b6a6fSGarance A Drosehn /* 791a4c8a745SGarance A Drosehn * SUSv3 states that `ps -G grouplist' should match "real-group 792a4c8a745SGarance A Drosehn * ID numbers", and does not mention group-names. I do want to 793a4c8a745SGarance A Drosehn * also support group-names, so this tries for a group-id first, 794a4c8a745SGarance A Drosehn * and only tries for a name if that doesn't work. This is the 795a4c8a745SGarance A Drosehn * opposite order of what is done in addelem_uid(), but in 796a4c8a745SGarance A Drosehn * practice the order would only matter for group-names which 797a4c8a745SGarance A Drosehn * are all-numeric. 7984e8b6a6fSGarance A Drosehn */ 799a4c8a745SGarance A Drosehn grp = NULL; 800a4c8a745SGarance A Drosehn nameorID = "named"; 801a4c8a745SGarance A Drosehn errno = 0; 8020b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 8030b42be7cSGarance A Drosehn if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) { 804a4c8a745SGarance A Drosehn nameorID = "name or ID matches"; 8050b42be7cSGarance A Drosehn grp = getgrgid((gid_t)bigtemp); 806a4c8a745SGarance A Drosehn } 807a4c8a745SGarance A Drosehn if (grp == NULL) 808a4c8a745SGarance A Drosehn grp = getgrnam(elem); 809a4c8a745SGarance A Drosehn if (grp == NULL) { 8108beb1a2fSMarcel Moolenaar xo_warnx("No %s %s '%s'", inf->lname, nameorID, elem); 811a4c8a745SGarance A Drosehn optfatal = 1; 812c23b00b7SGarance A Drosehn return (0); 813a4c8a745SGarance A Drosehn } 814a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 815a4c8a745SGarance A Drosehn expand_list(inf); 816d822163fSGarance A Drosehn inf->l.gids[(inf->count)++] = grp->gr_gid; 817a4c8a745SGarance A Drosehn return (1); 818a4c8a745SGarance A Drosehn } 819a4c8a745SGarance A Drosehn 820a4c8a745SGarance A Drosehn static int 82113767130SBryan Drewery addelem_jid(struct listinfo *inf, const char *elem) 82213767130SBryan Drewery { 82313767130SBryan Drewery int tempid; 82413767130SBryan Drewery 82513767130SBryan Drewery if (*elem == '\0') { 82613767130SBryan Drewery warnx("Invalid (zero-length) jail id"); 82713767130SBryan Drewery optfatal = 1; 82813767130SBryan Drewery return (0); /* Do not add this value. */ 82913767130SBryan Drewery } 83013767130SBryan Drewery 83113767130SBryan Drewery tempid = jail_getid(elem); 83213767130SBryan Drewery if (tempid < 0) { 83313767130SBryan Drewery warnx("Invalid %s: %s", inf->lname, elem); 83413767130SBryan Drewery optfatal = 1; 83513767130SBryan Drewery return (0); 83613767130SBryan Drewery } 83713767130SBryan Drewery 83813767130SBryan Drewery if (inf->count >= inf->maxcount) 83913767130SBryan Drewery expand_list(inf); 84013767130SBryan Drewery inf->l.jids[(inf->count)++] = tempid; 84113767130SBryan Drewery return (1); 84213767130SBryan Drewery } 84313767130SBryan Drewery 84413767130SBryan Drewery static int 845a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem) 846a4c8a745SGarance A Drosehn { 847a4c8a745SGarance A Drosehn char *endp; 848ca62e195SGarance A Drosehn long tempid; 849a4c8a745SGarance A Drosehn 850de1702f4SGarance A Drosehn if (*elem == '\0') { 8518beb1a2fSMarcel Moolenaar xo_warnx("Invalid (zero-length) process id"); 852de1702f4SGarance A Drosehn optfatal = 1; 853de1702f4SGarance A Drosehn return (0); /* Do not add this value. */ 85425113083SGarance A Drosehn } 85525113083SGarance A Drosehn 856a4c8a745SGarance A Drosehn errno = 0; 857a4c8a745SGarance A Drosehn tempid = strtol(elem, &endp, 10); 858a4c8a745SGarance A Drosehn if (*endp != '\0' || tempid < 0 || elem == endp) { 8598beb1a2fSMarcel Moolenaar xo_warnx("Invalid %s: %s", inf->lname, elem); 8604e8b6a6fSGarance A Drosehn errno = ERANGE; 86164b0bf0bSPawel Jakub Dawidek } else if (errno != 0 || tempid > pid_max) { 8628beb1a2fSMarcel Moolenaar xo_warnx("%s too large: %s", inf->lname, elem); 8634e8b6a6fSGarance A Drosehn errno = ERANGE; 8644e8b6a6fSGarance A Drosehn } 8654e8b6a6fSGarance A Drosehn if (errno == ERANGE) { 866a4c8a745SGarance A Drosehn optfatal = 1; 867c23b00b7SGarance A Drosehn return (0); 8684e8b6a6fSGarance A Drosehn } 869a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 870a4c8a745SGarance A Drosehn expand_list(inf); 871d822163fSGarance A Drosehn inf->l.pids[(inf->count)++] = tempid; 872a4c8a745SGarance A Drosehn return (1); 8734e8b6a6fSGarance A Drosehn } 8744e8b6a6fSGarance A Drosehn 87523b8efadSGarance A Drosehn /*- 87623b8efadSGarance A Drosehn * The user can specify a device via one of three formats: 8778a529f59SJohn Baldwin * 1) fully qualified, e.g.: /dev/ttyp0 /dev/console /dev/pts/0 8788a529f59SJohn Baldwin * 2) missing "/dev", e.g.: ttyp0 console pts/0 8798a529f59SJohn Baldwin * 3) two-letters, e.g.: p0 co 0 88023b8efadSGarance A Drosehn * (matching letters that would be seen in the "TT" column) 88123b8efadSGarance A Drosehn */ 882a4c8a745SGarance A Drosehn static int 883a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem) 884cf22dcfcSBrian Somers { 885a4c8a745SGarance A Drosehn const char *ttypath; 886ca62e195SGarance A Drosehn struct stat sb; 8878a529f59SJohn Baldwin char pathbuf[PATH_MAX], pathbuf2[PATH_MAX], pathbuf3[PATH_MAX]; 888a4c8a745SGarance A Drosehn 88923b8efadSGarance A Drosehn ttypath = NULL; 890f2fbfae6SGarance A Drosehn pathbuf2[0] = '\0'; 8918a529f59SJohn Baldwin pathbuf3[0] = '\0'; 89223b8efadSGarance A Drosehn switch (*elem) { 89323b8efadSGarance A Drosehn case '/': 894a4c8a745SGarance A Drosehn ttypath = elem; 89523b8efadSGarance A Drosehn break; 89623b8efadSGarance A Drosehn case 'c': 89723b8efadSGarance A Drosehn if (strcmp(elem, "co") == 0) { 89823b8efadSGarance A Drosehn ttypath = _PATH_CONSOLE; 89923b8efadSGarance A Drosehn break; 90023b8efadSGarance A Drosehn } 90123b8efadSGarance A Drosehn /* FALLTHROUGH */ 90223b8efadSGarance A Drosehn default: 90323b8efadSGarance A Drosehn strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf)); 904a4c8a745SGarance A Drosehn strlcat(pathbuf, elem, sizeof(pathbuf)); 905a4c8a745SGarance A Drosehn ttypath = pathbuf; 906f2fbfae6SGarance A Drosehn if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0) 90723b8efadSGarance A Drosehn break; 9088a529f59SJohn Baldwin if (strncmp(pathbuf, _PATH_PTS, strlen(_PATH_PTS)) == 0) 9098a529f59SJohn Baldwin break; 91023b8efadSGarance A Drosehn if (strcmp(pathbuf, _PATH_CONSOLE) == 0) 91123b8efadSGarance A Drosehn break; 912f2fbfae6SGarance A Drosehn /* Check to see if /dev/tty${elem} exists */ 913f2fbfae6SGarance A Drosehn strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2)); 914f2fbfae6SGarance A Drosehn strlcat(pathbuf2, elem, sizeof(pathbuf2)); 915f2fbfae6SGarance A Drosehn if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) { 91623b8efadSGarance A Drosehn /* No need to repeat stat() && S_ISCHR() checks */ 91723b8efadSGarance A Drosehn ttypath = NULL; 91823b8efadSGarance A Drosehn break; 919a4c8a745SGarance A Drosehn } 9208a529f59SJohn Baldwin /* Check to see if /dev/pts/${elem} exists */ 9218a529f59SJohn Baldwin strlcpy(pathbuf3, _PATH_PTS, sizeof(pathbuf3)); 9228a529f59SJohn Baldwin strlcat(pathbuf3, elem, sizeof(pathbuf3)); 9238a529f59SJohn Baldwin if (stat(pathbuf3, &sb) == 0 && S_ISCHR(sb.st_mode)) { 9248a529f59SJohn Baldwin /* No need to repeat stat() && S_ISCHR() checks */ 9258a529f59SJohn Baldwin ttypath = NULL; 9268a529f59SJohn Baldwin break; 9278a529f59SJohn Baldwin } 92823b8efadSGarance A Drosehn break; 92923b8efadSGarance A Drosehn } 93023b8efadSGarance A Drosehn if (ttypath) { 931a4c8a745SGarance A Drosehn if (stat(ttypath, &sb) == -1) { 9328a529f59SJohn Baldwin if (pathbuf3[0] != '\0') 9338beb1a2fSMarcel Moolenaar xo_warn("%s, %s, and %s", pathbuf3, pathbuf2, 9348a529f59SJohn Baldwin ttypath); 935f2fbfae6SGarance A Drosehn else 9368beb1a2fSMarcel Moolenaar xo_warn("%s", ttypath); 937a4c8a745SGarance A Drosehn optfatal = 1; 938c23b00b7SGarance A Drosehn return (0); 939a4c8a745SGarance A Drosehn } 940a4c8a745SGarance A Drosehn if (!S_ISCHR(sb.st_mode)) { 9418a529f59SJohn Baldwin if (pathbuf3[0] != '\0') 9428beb1a2fSMarcel Moolenaar xo_warnx("%s, %s, and %s: Not a terminal", 9438a529f59SJohn Baldwin pathbuf3, pathbuf2, ttypath); 944f2fbfae6SGarance A Drosehn else 9458beb1a2fSMarcel Moolenaar xo_warnx("%s: Not a terminal", ttypath); 946a4c8a745SGarance A Drosehn optfatal = 1; 947c23b00b7SGarance A Drosehn return (0); 948a4c8a745SGarance A Drosehn } 94923b8efadSGarance A Drosehn } 950a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 951a4c8a745SGarance A Drosehn expand_list(inf); 952d822163fSGarance A Drosehn inf->l.ttys[(inf->count)++] = sb.st_rdev; 953a4c8a745SGarance A Drosehn return (1); 954a4c8a745SGarance A Drosehn } 955a4c8a745SGarance A Drosehn 956a4c8a745SGarance A Drosehn static int 957a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem) 958a4c8a745SGarance A Drosehn { 959cf22dcfcSBrian Somers struct passwd *pwd; 960a4c8a745SGarance A Drosehn char *endp; 9610b42be7cSGarance A Drosehn u_long bigtemp; 962cf22dcfcSBrian Somers 963a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 964a4c8a745SGarance A Drosehn if (*elem == '\0') 9658beb1a2fSMarcel Moolenaar xo_warnx("Invalid (zero-length) %s name", inf->lname); 966a4c8a745SGarance A Drosehn else 9678beb1a2fSMarcel Moolenaar xo_warnx("%s name too long: %s", inf->lname, elem); 968a4c8a745SGarance A Drosehn optfatal = 1; 969352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 970a4c8a745SGarance A Drosehn } 971cf22dcfcSBrian Somers 972a4c8a745SGarance A Drosehn pwd = getpwnam(elem); 973a4c8a745SGarance A Drosehn if (pwd == NULL) { 974a4c8a745SGarance A Drosehn errno = 0; 9750b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 9760b42be7cSGarance A Drosehn if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX) 9778beb1a2fSMarcel Moolenaar xo_warnx("No %s named '%s'", inf->lname, elem); 978a4c8a745SGarance A Drosehn else { 979a4c8a745SGarance A Drosehn /* The string is all digits, so it might be a userID. */ 9800b42be7cSGarance A Drosehn pwd = getpwuid((uid_t)bigtemp); 981a4c8a745SGarance A Drosehn if (pwd == NULL) 9828beb1a2fSMarcel Moolenaar xo_warnx("No %s name or ID matches '%s'", 983a4c8a745SGarance A Drosehn inf->lname, elem); 984cf22dcfcSBrian Somers } 985cf22dcfcSBrian Somers } 986a4c8a745SGarance A Drosehn if (pwd == NULL) { 987e3c4e1ddSGarance A Drosehn /* 988e3c4e1ddSGarance A Drosehn * These used to be treated as minor warnings (and the 989e3c4e1ddSGarance A Drosehn * option was simply ignored), but now they are fatal 990e3c4e1ddSGarance A Drosehn * errors (and the command will be aborted). 991e3c4e1ddSGarance A Drosehn */ 992e3c4e1ddSGarance A Drosehn optfatal = 1; 993c23b00b7SGarance A Drosehn return (0); 994cf22dcfcSBrian Somers } 995a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 996a4c8a745SGarance A Drosehn expand_list(inf); 997d822163fSGarance A Drosehn inf->l.uids[(inf->count)++] = pwd->pw_uid; 998a4c8a745SGarance A Drosehn return (1); 999a4c8a745SGarance A Drosehn } 1000cf22dcfcSBrian Somers 1001a4c8a745SGarance A Drosehn static void 1002a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp) 1003a4c8a745SGarance A Drosehn { 1004a4c8a745SGarance A Drosehn const char *savep; 1005a4c8a745SGarance A Drosehn char *cp, *endp; 1006a4c8a745SGarance A Drosehn int toolong; 1007ca62e195SGarance A Drosehn char elemcopy[PATH_MAX]; 1008a4c8a745SGarance A Drosehn 100942856668SEd Maste if (*argp == '\0') 101042856668SEd Maste inf->addelem(inf, argp); 1011a4c8a745SGarance A Drosehn while (*argp != '\0') { 1012a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 1013a4c8a745SGarance A Drosehn argp++; 1014a4c8a745SGarance A Drosehn savep = argp; 1015a4c8a745SGarance A Drosehn toolong = 0; 1016a4c8a745SGarance A Drosehn cp = elemcopy; 1017a4c8a745SGarance A Drosehn if (strchr(T_SEP, *argp) == NULL) { 1018a4c8a745SGarance A Drosehn endp = elemcopy + sizeof(elemcopy) - 1; 1019a4c8a745SGarance A Drosehn while (*argp != '\0' && cp <= endp && 1020a4c8a745SGarance A Drosehn strchr(W_SEP T_SEP, *argp) == NULL) 1021a4c8a745SGarance A Drosehn *cp++ = *argp++; 1022a4c8a745SGarance A Drosehn if (cp > endp) 1023a4c8a745SGarance A Drosehn toolong = 1; 1024a4c8a745SGarance A Drosehn } 1025a4c8a745SGarance A Drosehn if (!toolong) { 1026a4c8a745SGarance A Drosehn *cp = '\0'; 1027352b6523SGarance A Drosehn /* 1028a9626fb3SGarance A Drosehn * Add this single element to the given list. 1029352b6523SGarance A Drosehn */ 1030a4c8a745SGarance A Drosehn inf->addelem(inf, elemcopy); 1031a4c8a745SGarance A Drosehn } else { 1032a4c8a745SGarance A Drosehn /* 1033a4c8a745SGarance A Drosehn * The string is too long to copy. Find the end 1034a4c8a745SGarance A Drosehn * of the string to print out the warning message. 1035a4c8a745SGarance A Drosehn */ 1036a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP T_SEP, 1037a4c8a745SGarance A Drosehn *argp) == NULL) 1038a4c8a745SGarance A Drosehn argp++; 10398beb1a2fSMarcel Moolenaar xo_warnx("Value too long: %.*s", (int)(argp - savep), 1040a4c8a745SGarance A Drosehn savep); 1041a4c8a745SGarance A Drosehn optfatal = 1; 1042a4c8a745SGarance A Drosehn } 1043a4c8a745SGarance A Drosehn /* 1044a4c8a745SGarance A Drosehn * Skip over any number of trailing whitespace characters, 1045a4c8a745SGarance A Drosehn * but only one (at most) trailing element-terminating 1046a4c8a745SGarance A Drosehn * character. 1047a4c8a745SGarance A Drosehn */ 1048a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 1049a4c8a745SGarance A Drosehn argp++; 1050a4c8a745SGarance A Drosehn if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) { 1051a4c8a745SGarance A Drosehn argp++; 1052a4c8a745SGarance A Drosehn /* Catch case where string ended with a comma. */ 1053a4c8a745SGarance A Drosehn if (*argp == '\0') 1054a4c8a745SGarance A Drosehn inf->addelem(inf, argp); 1055a4c8a745SGarance A Drosehn } 1056a4c8a745SGarance A Drosehn } 1057a4c8a745SGarance A Drosehn } 1058a4c8a745SGarance A Drosehn 1059044fce53SBrian Somers static void 1060044fce53SBrian Somers descendant_sort(KINFO *ki, int items) 1061044fce53SBrian Somers { 1062044fce53SBrian Somers int dst, lvl, maxlvl, n, ndst, nsrc, siblings, src; 1063044fce53SBrian Somers unsigned char *path; 1064044fce53SBrian Somers KINFO kn; 1065044fce53SBrian Somers 1066044fce53SBrian Somers /* 1067044fce53SBrian Somers * First, sort the entries by descendancy, tracking the descendancy 1068044fce53SBrian Somers * depth in the ki_d.level field. 1069044fce53SBrian Somers */ 1070044fce53SBrian Somers src = 0; 1071044fce53SBrian Somers maxlvl = 0; 1072044fce53SBrian Somers while (src < items) { 1073044fce53SBrian Somers if (ki[src].ki_d.level) { 1074044fce53SBrian Somers src++; 1075044fce53SBrian Somers continue; 1076044fce53SBrian Somers } 1077044fce53SBrian Somers for (nsrc = 1; src + nsrc < items; nsrc++) 1078044fce53SBrian Somers if (!ki[src + nsrc].ki_d.level) 1079044fce53SBrian Somers break; 1080044fce53SBrian Somers 1081044fce53SBrian Somers for (dst = 0; dst < items; dst++) { 1082044fce53SBrian Somers if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_pid) 1083044fce53SBrian Somers continue; 1084044fce53SBrian Somers if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_ppid) 1085044fce53SBrian Somers break; 1086044fce53SBrian Somers } 1087044fce53SBrian Somers 1088044fce53SBrian Somers if (dst == items) { 1089044fce53SBrian Somers src += nsrc; 1090044fce53SBrian Somers continue; 1091044fce53SBrian Somers } 1092044fce53SBrian Somers 1093044fce53SBrian Somers for (ndst = 1; dst + ndst < items; ndst++) 1094044fce53SBrian Somers if (ki[dst + ndst].ki_d.level <= ki[dst].ki_d.level) 1095044fce53SBrian Somers break; 1096044fce53SBrian Somers 1097044fce53SBrian Somers for (n = src; n < src + nsrc; n++) { 1098044fce53SBrian Somers ki[n].ki_d.level += ki[dst].ki_d.level + 1; 1099044fce53SBrian Somers if (maxlvl < ki[n].ki_d.level) 1100044fce53SBrian Somers maxlvl = ki[n].ki_d.level; 1101044fce53SBrian Somers } 1102044fce53SBrian Somers 1103044fce53SBrian Somers while (nsrc) { 1104044fce53SBrian Somers if (src < dst) { 1105044fce53SBrian Somers kn = ki[src]; 1106044fce53SBrian Somers memmove(ki + src, ki + src + 1, 1107044fce53SBrian Somers (dst - src + ndst - 1) * sizeof *ki); 1108044fce53SBrian Somers ki[dst + ndst - 1] = kn; 1109044fce53SBrian Somers nsrc--; 1110044fce53SBrian Somers dst--; 1111044fce53SBrian Somers ndst++; 1112044fce53SBrian Somers } else if (src != dst + ndst) { 1113044fce53SBrian Somers kn = ki[src]; 1114044fce53SBrian Somers memmove(ki + dst + ndst + 1, ki + dst + ndst, 1115044fce53SBrian Somers (src - dst - ndst) * sizeof *ki); 1116044fce53SBrian Somers ki[dst + ndst] = kn; 1117044fce53SBrian Somers ndst++; 1118044fce53SBrian Somers nsrc--; 1119044fce53SBrian Somers src++; 1120044fce53SBrian Somers } else { 1121044fce53SBrian Somers ndst += nsrc; 1122044fce53SBrian Somers src += nsrc; 1123044fce53SBrian Somers nsrc = 0; 1124044fce53SBrian Somers } 1125044fce53SBrian Somers } 1126044fce53SBrian Somers } 1127044fce53SBrian Somers 1128044fce53SBrian Somers /* 1129044fce53SBrian Somers * Now populate ki_d.prefix (instead of ki_d.level) with the command 1130044fce53SBrian Somers * prefix used to show descendancies. 1131044fce53SBrian Somers */ 11323322d1c0SAlfonso path = calloc((maxlvl + 7) / 8, sizeof(unsigned char)); 1133044fce53SBrian Somers for (src = 0; src < items; src++) { 1134044fce53SBrian Somers if ((lvl = ki[src].ki_d.level) == 0) { 1135044fce53SBrian Somers ki[src].ki_d.prefix = NULL; 1136044fce53SBrian Somers continue; 1137044fce53SBrian Somers } 1138044fce53SBrian Somers if ((ki[src].ki_d.prefix = malloc(lvl * 2 + 1)) == NULL) 11398beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 1140044fce53SBrian Somers for (n = 0; n < lvl - 2; n++) { 1141044fce53SBrian Somers ki[src].ki_d.prefix[n * 2] = 1142044fce53SBrian Somers path[n / 8] & 1 << (n % 8) ? '|' : ' '; 1143044fce53SBrian Somers ki[src].ki_d.prefix[n * 2 + 1] = ' '; 1144044fce53SBrian Somers } 1145044fce53SBrian Somers if (n == lvl - 2) { 1146044fce53SBrian Somers /* Have I any more siblings? */ 1147044fce53SBrian Somers for (siblings = 0, dst = src + 1; dst < items; dst++) { 1148044fce53SBrian Somers if (ki[dst].ki_d.level > lvl) 1149044fce53SBrian Somers continue; 1150044fce53SBrian Somers if (ki[dst].ki_d.level == lvl) 1151044fce53SBrian Somers siblings = 1; 1152044fce53SBrian Somers break; 1153044fce53SBrian Somers } 1154044fce53SBrian Somers if (siblings) 1155044fce53SBrian Somers path[n / 8] |= 1 << (n % 8); 1156044fce53SBrian Somers else 1157044fce53SBrian Somers path[n / 8] &= ~(1 << (n % 8)); 1158044fce53SBrian Somers ki[src].ki_d.prefix[n * 2] = siblings ? '|' : '`'; 1159044fce53SBrian Somers ki[src].ki_d.prefix[n * 2 + 1] = '-'; 1160044fce53SBrian Somers n++; 1161044fce53SBrian Somers } 1162044fce53SBrian Somers strcpy(ki[src].ki_d.prefix + n * 2, "- "); 1163044fce53SBrian Somers } 1164044fce53SBrian Somers free(path); 1165044fce53SBrian Somers } 1166044fce53SBrian Somers 1167a4c8a745SGarance A Drosehn static void * 1168a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf) 1169a4c8a745SGarance A Drosehn { 1170a4c8a745SGarance A Drosehn void *newlist; 1171ca62e195SGarance A Drosehn int newmax; 1172a4c8a745SGarance A Drosehn 1173a4c8a745SGarance A Drosehn newmax = (inf->maxcount + 1) << 1; 1174d822163fSGarance A Drosehn newlist = realloc(inf->l.ptr, newmax * inf->elemsize); 1175a4c8a745SGarance A Drosehn if (newlist == NULL) { 1176d822163fSGarance A Drosehn free(inf->l.ptr); 11778beb1a2fSMarcel Moolenaar xo_errx(1, "realloc to %d %ss failed", newmax, inf->lname); 1178a4c8a745SGarance A Drosehn } 1179a4c8a745SGarance A Drosehn inf->maxcount = newmax; 1180d822163fSGarance A Drosehn inf->l.ptr = newlist; 1181a4c8a745SGarance A Drosehn 1182a4c8a745SGarance A Drosehn return (newlist); 1183a4c8a745SGarance A Drosehn } 1184a4c8a745SGarance A Drosehn 1185a4c8a745SGarance A Drosehn static void 1186a4c8a745SGarance A Drosehn free_list(struct listinfo *inf) 1187a4c8a745SGarance A Drosehn { 1188a4c8a745SGarance A Drosehn 1189a4c8a745SGarance A Drosehn inf->count = inf->elemsize = inf->maxcount = 0; 1190d822163fSGarance A Drosehn if (inf->l.ptr != NULL) 1191d822163fSGarance A Drosehn free(inf->l.ptr); 1192a4c8a745SGarance A Drosehn inf->addelem = NULL; 1193a4c8a745SGarance A Drosehn inf->lname = NULL; 1194d822163fSGarance A Drosehn inf->l.ptr = NULL; 1195a4c8a745SGarance A Drosehn } 1196a4c8a745SGarance A Drosehn 1197a4c8a745SGarance A Drosehn static void 1198a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize, 1199a4c8a745SGarance A Drosehn const char *lname) 1200a4c8a745SGarance A Drosehn { 1201a4c8a745SGarance A Drosehn 1202a4c8a745SGarance A Drosehn inf->count = inf->maxcount = 0; 1203a4c8a745SGarance A Drosehn inf->elemsize = elemsize; 1204a4c8a745SGarance A Drosehn inf->addelem = artn; 1205a4c8a745SGarance A Drosehn inf->lname = lname; 1206d822163fSGarance A Drosehn inf->l.ptr = NULL; 1207cf22dcfcSBrian Somers } 1208cf22dcfcSBrian Somers 1209fde411d5SJuli Mallett VARENT * 1210fde411d5SJuli Mallett find_varentry(VAR *v) 1211fde411d5SJuli Mallett { 1212fde411d5SJuli Mallett struct varent *vent; 1213fde411d5SJuli Mallett 1214bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 1215fde411d5SJuli Mallett if (strcmp(vent->var->name, v->name) == 0) 1216fde411d5SJuli Mallett return vent; 1217fde411d5SJuli Mallett } 1218fde411d5SJuli Mallett return NULL; 1219fde411d5SJuli Mallett } 1220fde411d5SJuli Mallett 12214b88c807SRodney W. Grimes static void 122246251ddeSWarner Losh scanvars(void) 12234b88c807SRodney W. Grimes { 12244b88c807SRodney W. Grimes struct varent *vent; 12254b88c807SRodney W. Grimes VAR *v; 12266a2d726bSJordan K. Hubbard 1227bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 12286a2d726bSJordan K. Hubbard v = vent->var; 12296a2d726bSJordan K. Hubbard if (v->flag & USER) 12306a2d726bSJordan K. Hubbard needuser = 1; 12316a2d726bSJordan K. Hubbard if (v->flag & COMM) 12326a2d726bSJordan K. Hubbard needcomm = 1; 12336a2d726bSJordan K. Hubbard } 12346a2d726bSJordan K. Hubbard } 12356a2d726bSJordan K. Hubbard 12366a2d726bSJordan K. Hubbard static void 12371d1143ecSEdward Tomasz Napierala format_output(KINFO *ki) 12386a2d726bSJordan K. Hubbard { 12396a2d726bSJordan K. Hubbard struct varent *vent; 12406a2d726bSJordan K. Hubbard VAR *v; 12411d1143ecSEdward Tomasz Napierala KINFO_STR *ks; 12421d1143ecSEdward Tomasz Napierala char *str; 12431d1143ecSEdward Tomasz Napierala int len; 12446a2d726bSJordan K. Hubbard 12451d1143ecSEdward Tomasz Napierala STAILQ_INIT(&ki->ki_ks); 1246bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 12476a2d726bSJordan K. Hubbard v = vent->var; 12481d1143ecSEdward Tomasz Napierala str = (v->oproc)(ki, vent); 12491d1143ecSEdward Tomasz Napierala ks = malloc(sizeof(*ks)); 12501d1143ecSEdward Tomasz Napierala if (ks == NULL) 12518beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 12521d1143ecSEdward Tomasz Napierala ks->ks_str = str; 12531d1143ecSEdward Tomasz Napierala STAILQ_INSERT_TAIL(&ki->ki_ks, ks, ks_next); 12541d1143ecSEdward Tomasz Napierala if (str != NULL) { 12551d1143ecSEdward Tomasz Napierala len = strlen(str); 12561d1143ecSEdward Tomasz Napierala } else 12571d1143ecSEdward Tomasz Napierala len = 1; /* "-" */ 12581d1143ecSEdward Tomasz Napierala if (v->width < len) 12591d1143ecSEdward Tomasz Napierala v->width = len; 12606a2d726bSJordan K. Hubbard } 12616a2d726bSJordan K. Hubbard } 12626a2d726bSJordan K. Hubbard 12636a2d726bSJordan K. Hubbard static void 126446251ddeSWarner Losh sizevars(void) 12656a2d726bSJordan K. Hubbard { 12666a2d726bSJordan K. Hubbard struct varent *vent; 12676a2d726bSJordan K. Hubbard VAR *v; 12684b88c807SRodney W. Grimes int i; 12694b88c807SRodney W. Grimes 1270bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 12714b88c807SRodney W. Grimes v = vent->var; 127278b1878aSJuli Mallett i = strlen(vent->header); 12734b88c807SRodney W. Grimes if (v->width < i) 12744b88c807SRodney W. Grimes v->width = i; 12754b88c807SRodney W. Grimes } 12764b88c807SRodney W. Grimes } 12774b88c807SRodney W. Grimes 1278871e8d8cSMark Murray static const char * 127946251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 12801c67ef12SJohn Baldwin char *comm, char *thread, int maxlen) 12814b88c807SRodney W. Grimes { 1282871e8d8cSMark Murray const char *s; 12834b88c807SRodney W. Grimes 12841c67ef12SJohn Baldwin s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, 12859f0b6e5eSJohn Baldwin showthreads && ki->ki_p->ki_numthreads > 1 ? thread : NULL, maxlen); 12864b88c807SRodney W. Grimes return (s); 12874b88c807SRodney W. Grimes } 12884b88c807SRodney W. Grimes 1289b61ce5b0SJeff Roberson #define UREADOK(ki) (forceuread || (ki->ki_p->ki_flag & P_INMEM)) 12903ac5e955SJohn Dyson 12914b88c807SRodney W. Grimes static void 129246251ddeSWarner Losh saveuser(KINFO *ki) 12934b88c807SRodney W. Grimes { 1294c7f893a4SMark Johnston char tdname[COMMLEN + 1]; 12955912ca59SDon Lewis char *argsp; 12964b88c807SRodney W. Grimes 1297b61ce5b0SJeff Roberson if (ki->ki_p->ki_flag & P_INMEM) { 12984b88c807SRodney W. Grimes /* 12994b88c807SRodney W. Grimes * The u-area might be swapped out, and we can't get 13004b88c807SRodney W. Grimes * at it because we have a crashdump and no swap. 13014b88c807SRodney W. Grimes * If it's here fill in these fields, otherwise, just 13024b88c807SRodney W. Grimes * leave them 0. 13034b88c807SRodney W. Grimes */ 13041f7d2501SKirk McKusick ki->ki_valid = 1; 13054b88c807SRodney W. Grimes } else 13061f7d2501SKirk McKusick ki->ki_valid = 0; 13074b88c807SRodney W. Grimes /* 13084b88c807SRodney W. Grimes * save arguments if needed 13094b88c807SRodney W. Grimes */ 1310dd693acfSGarance A Drosehn if (needcomm) { 1311c7f893a4SMark Johnston if (ki->ki_p->ki_stat == SZOMB) { 1312dd693acfSGarance A Drosehn ki->ki_args = strdup("<defunct>"); 1313c7f893a4SMark Johnston } else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) { 1314c7f893a4SMark Johnston (void)snprintf(tdname, sizeof(tdname), "%s%s", 1315c7f893a4SMark Johnston ki->ki_p->ki_tdname, ki->ki_p->ki_moretdname); 13165912ca59SDon Lewis ki->ki_args = fmt(kvm_getargv, ki, 1317c7f893a4SMark Johnston ki->ki_p->ki_comm, tdname, COMMLEN * 2 + 1); 1318c7f893a4SMark Johnston } else { 13195912ca59SDon Lewis asprintf(&argsp, "(%s)", ki->ki_p->ki_comm); 13205912ca59SDon Lewis ki->ki_args = argsp; 13215912ca59SDon Lewis } 1322bd6233fdSGarance A Drosehn if (ki->ki_args == NULL) 13238beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 13243ac5e955SJohn Dyson } else { 13254b88c807SRodney W. Grimes ki->ki_args = NULL; 13263ac5e955SJohn Dyson } 1327dd693acfSGarance A Drosehn if (needenv) { 1328dd693acfSGarance A Drosehn if (UREADOK(ki)) 13295912ca59SDon Lewis ki->ki_env = fmt(kvm_getenvv, ki, 13305912ca59SDon Lewis (char *)NULL, (char *)NULL, 0); 1331dd693acfSGarance A Drosehn else 1332dd693acfSGarance A Drosehn ki->ki_env = strdup("()"); 1333dd693acfSGarance A Drosehn if (ki->ki_env == NULL) 13348beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 13353ac5e955SJohn Dyson } else { 13364b88c807SRodney W. Grimes ki->ki_env = NULL; 13374b88c807SRodney W. Grimes } 13383ac5e955SJohn Dyson } 13394b88c807SRodney W. Grimes 13404bac4483SGarance A Drosehn /* A macro used to improve the readability of pscomp(). */ 13414bac4483SGarance A Drosehn #define DIFF_RETURN(a, b, field) do { \ 13424bac4483SGarance A Drosehn if ((a)->field != (b)->field) \ 13434bac4483SGarance A Drosehn return (((a)->field < (b)->field) ? -1 : 1); \ 13444bac4483SGarance A Drosehn } while (0) 13454bac4483SGarance A Drosehn 13464b88c807SRodney W. Grimes static int 134746251ddeSWarner Losh pscomp(const void *a, const void *b) 13484b88c807SRodney W. Grimes { 13495bd7b1f3SGarance A Drosehn const KINFO *ka, *kb; 13504b88c807SRodney W. Grimes 13515bd7b1f3SGarance A Drosehn ka = a; 13525bd7b1f3SGarance A Drosehn kb = b; 13535bd7b1f3SGarance A Drosehn /* SORTCPU and SORTMEM are sorted in descending order. */ 13544bac4483SGarance A Drosehn if (sortby == SORTCPU) 13554bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_pcpu); 13564bac4483SGarance A Drosehn if (sortby == SORTMEM) 13574bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_memsize); 13585bd7b1f3SGarance A Drosehn /* 13595bd7b1f3SGarance A Drosehn * TTY's are sorted in ascending order, except that all NODEV 13605bd7b1f3SGarance A Drosehn * processes come before all processes with a device. 13615bd7b1f3SGarance A Drosehn */ 13624bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) { 13634bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev == NODEV) 13645bd7b1f3SGarance A Drosehn return (-1); 13654bac4483SGarance A Drosehn if (kb->ki_p->ki_tdev == NODEV) 13665bd7b1f3SGarance A Drosehn return (1); 13674bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tdev); 13684bac4483SGarance A Drosehn } 13694bac4483SGarance A Drosehn 1370b4b24324SGarance A Drosehn /* PID's and TID's (threads) are sorted in ascending order. */ 13714bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_pid); 1372b4b24324SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tid); 13735bd7b1f3SGarance A Drosehn return (0); 13744b88c807SRodney W. Grimes } 13754bac4483SGarance A Drosehn #undef DIFF_RETURN 13764b88c807SRodney W. Grimes 13774b88c807SRodney W. Grimes /* 13784b88c807SRodney W. Grimes * ICK (all for getopt), would rather hide the ugliness 13794b88c807SRodney W. Grimes * here than taint the main code. 13804b88c807SRodney W. Grimes * 13814b88c807SRodney W. Grimes * ps foo -> ps -foo 13824b88c807SRodney W. Grimes * ps 34 -> ps -p34 13834b88c807SRodney W. Grimes * 13844b88c807SRodney W. Grimes * The old convention that 't' with no trailing tty arg means the users 13854b88c807SRodney W. Grimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 13864b88c807SRodney W. Grimes * feature is available with the option 'T', which takes no argument. 13874b88c807SRodney W. Grimes */ 13884b88c807SRodney W. Grimes static char * 1389bf46a3bfSGarance A Drosehn kludge_oldps_options(const char *optlist, char *origval, const char *nextarg) 13904b88c807SRodney W. Grimes { 13914b88c807SRodney W. Grimes size_t len; 1392c675340aSGarance A Drosehn char *argp, *cp, *newopts, *ns, *optp, *pidp; 13934b88c807SRodney W. Grimes 1394daed3ad6SJuli Mallett /* 1395c675340aSGarance A Drosehn * See if the original value includes any option which takes an 1396c675340aSGarance A Drosehn * argument (and will thus use up the remainder of the string). 1397daed3ad6SJuli Mallett */ 1398c675340aSGarance A Drosehn argp = NULL; 1399c675340aSGarance A Drosehn if (optlist != NULL) { 1400c675340aSGarance A Drosehn for (cp = origval; *cp != '\0'; cp++) { 1401c675340aSGarance A Drosehn optp = strchr(optlist, *cp); 1402c675340aSGarance A Drosehn if ((optp != NULL) && *(optp + 1) == ':') { 1403c675340aSGarance A Drosehn argp = cp; 1404c675340aSGarance A Drosehn break; 1405c675340aSGarance A Drosehn } 1406c675340aSGarance A Drosehn } 1407c675340aSGarance A Drosehn } 1408c675340aSGarance A Drosehn if (argp != NULL && *origval == '-') 1409c675340aSGarance A Drosehn return (origval); 1410daed3ad6SJuli Mallett 14114b88c807SRodney W. Grimes /* 14124b88c807SRodney W. Grimes * if last letter is a 't' flag with no argument (in the context 14134b88c807SRodney W. Grimes * of the oldps options -- option string NOT starting with a '-' -- 14144b88c807SRodney W. Grimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 1415380434d4SBrian Somers * 14162631c777SGarance A Drosehn * However, if a flag accepting a string argument is found earlier 14172631c777SGarance A Drosehn * in the option string (including a possible `t' flag), then the 14182631c777SGarance A Drosehn * remainder of the string must be the argument to that flag; so 1419c675340aSGarance A Drosehn * do not modify that argument. Note that a trailing `t' would 1420c675340aSGarance A Drosehn * cause argp to be set, if argp was not already set by some 1421c675340aSGarance A Drosehn * earlier option. 14224b88c807SRodney W. Grimes */ 1423c675340aSGarance A Drosehn len = strlen(origval); 1424c675340aSGarance A Drosehn cp = origval + len - 1; 1425c675340aSGarance A Drosehn pidp = NULL; 1426bf46a3bfSGarance A Drosehn if (*cp == 't' && *origval != '-' && cp == argp) { 1427bf46a3bfSGarance A Drosehn if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg)) 14284b88c807SRodney W. Grimes *cp = 'T'; 1429bf46a3bfSGarance A Drosehn } else if (argp == NULL) { 1430c675340aSGarance A Drosehn /* 1431c675340aSGarance A Drosehn * The original value did not include any option which takes 1432c675340aSGarance A Drosehn * an argument (and that would include `p' and `t'), so check 1433c675340aSGarance A Drosehn * the value for trailing number, or comma-separated list of 1434c675340aSGarance A Drosehn * numbers, which we will treat as a pid request. 1435c675340aSGarance A Drosehn */ 1436c675340aSGarance A Drosehn if (isdigitch(*cp)) { 1437c675340aSGarance A Drosehn while (cp >= origval && (*cp == ',' || isdigitch(*cp))) 1438c675340aSGarance A Drosehn --cp; 1439c675340aSGarance A Drosehn pidp = cp + 1; 1440c675340aSGarance A Drosehn } 1441c675340aSGarance A Drosehn } 1442c675340aSGarance A Drosehn 1443c675340aSGarance A Drosehn /* 1444c675340aSGarance A Drosehn * If nothing needs to be added to the string, then return 1445c675340aSGarance A Drosehn * the "original" (although possibly modified) value. 1446c675340aSGarance A Drosehn */ 1447c675340aSGarance A Drosehn if (*origval == '-' && pidp == NULL) 1448c675340aSGarance A Drosehn return (origval); 1449c675340aSGarance A Drosehn 1450c675340aSGarance A Drosehn /* 1451c675340aSGarance A Drosehn * Create a copy of the string to add '-' and/or 'p' to the 1452c675340aSGarance A Drosehn * original value. 1453c675340aSGarance A Drosehn */ 1454c675340aSGarance A Drosehn if ((newopts = ns = malloc(len + 3)) == NULL) 14558beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 1456c675340aSGarance A Drosehn 1457c675340aSGarance A Drosehn if (*origval != '-') 1458c675340aSGarance A Drosehn *ns++ = '-'; /* add option flag */ 1459c675340aSGarance A Drosehn 1460c675340aSGarance A Drosehn if (pidp == NULL) 1461c675340aSGarance A Drosehn strcpy(ns, origval); 14624b88c807SRodney W. Grimes else { 14634b88c807SRodney W. Grimes /* 1464c675340aSGarance A Drosehn * Copy everything before the pid string, add the `p', 1465c675340aSGarance A Drosehn * and then copy the pid string. 14664b88c807SRodney W. Grimes */ 1467c675340aSGarance A Drosehn len = pidp - origval; 1468c675340aSGarance A Drosehn memcpy(ns, origval, len); 1469c675340aSGarance A Drosehn ns += len; 14704b88c807SRodney W. Grimes *ns++ = 'p'; 1471c675340aSGarance A Drosehn strcpy(ns, pidp); 1472c675340aSGarance A Drosehn } 14734b88c807SRodney W. Grimes 14744b88c807SRodney W. Grimes return (newopts); 14754b88c807SRodney W. Grimes } 14764b88c807SRodney W. Grimes 14774b88c807SRodney W. Grimes static void 147864b0bf0bSPawel Jakub Dawidek pidmax_init(void) 147964b0bf0bSPawel Jakub Dawidek { 148064b0bf0bSPawel Jakub Dawidek size_t intsize; 148164b0bf0bSPawel Jakub Dawidek 148264b0bf0bSPawel Jakub Dawidek intsize = sizeof(pid_max); 148364b0bf0bSPawel Jakub Dawidek if (sysctlbyname("kern.pid_max", &pid_max, &intsize, NULL, 0) < 0) { 14848beb1a2fSMarcel Moolenaar xo_warn("unable to read kern.pid_max"); 148564b0bf0bSPawel Jakub Dawidek pid_max = 99999; 148664b0bf0bSPawel Jakub Dawidek } 148764b0bf0bSPawel Jakub Dawidek } 148864b0bf0bSPawel Jakub Dawidek 1489c6a5ff71SEitan Adler static void __dead2 149046251ddeSWarner Losh usage(void) 14914b88c807SRodney W. Grimes { 1492aa14b5abSBrian Somers #define SINGLE_OPTS "[-aCcde" OPT_LAZY_f "HhjlmrSTuvwXxZ]" 14934b88c807SRodney W. Grimes 1494*5c0a1c15SPiotr Pawel Stefaniak (void)xo_error("%s\n%s\n%s\n%s\n%s\n", 1495820ac126SMateusz Piotrowski "usage: ps [--libxo] " SINGLE_OPTS " [-O fmt | -o fmt]", 1496820ac126SMateusz Piotrowski " [-G gid[,gid...]] [-J jid[,jid...]] [-M core] [-N system]", 1497a89237aeSRuslan Ermilov " [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]", 1498*5c0a1c15SPiotr Pawel Stefaniak " [-D up | down | both]", 1499820ac126SMateusz Piotrowski " ps [--libxo] -L"); 15004b88c807SRodney W. Grimes exit(1); 15014b88c807SRodney W. Grimes } 1502