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> 522749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 534b88c807SRodney W. Grimes 544b88c807SRodney W. Grimes #include <sys/param.h> 5513767130SBryan Drewery #include <sys/jail.h> 56c7910c3aSGarance A Drosehn #include <sys/proc.h> 578b6f5f5fSDavid Greenman #include <sys/user.h> 584b88c807SRodney W. Grimes #include <sys/stat.h> 594b88c807SRodney W. Grimes #include <sys/ioctl.h> 604b88c807SRodney W. Grimes #include <sys/sysctl.h> 61a951d1f8SChristian S.J. Peron #include <sys/mount.h> 624b88c807SRodney W. Grimes 634b88c807SRodney W. Grimes #include <ctype.h> 644b88c807SRodney W. Grimes #include <err.h> 654e8b6a6fSGarance A Drosehn #include <errno.h> 664b88c807SRodney W. Grimes #include <fcntl.h> 67a4c8a745SGarance A Drosehn #include <grp.h> 6813767130SBryan Drewery #include <jail.h> 694b88c807SRodney W. Grimes #include <kvm.h> 70b2496d93SMike Pritchard #include <limits.h> 7108017519SAndrey A. Chernov #include <locale.h> 724b88c807SRodney W. Grimes #include <paths.h> 73871e8d8cSMark Murray #include <pwd.h> 744b88c807SRodney W. Grimes #include <stdio.h> 754b88c807SRodney W. Grimes #include <stdlib.h> 764b88c807SRodney W. Grimes #include <string.h> 774b88c807SRodney W. Grimes #include <unistd.h> 788beb1a2fSMarcel Moolenaar #include <libxo/xo.h> 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes #include "ps.h" 814b88c807SRodney W. Grimes 828a529f59SJohn Baldwin #define _PATH_PTS "/dev/pts/" 838a529f59SJohn Baldwin 84a4c8a745SGarance A Drosehn #define W_SEP " \t" /* "Whitespace" list separators */ 85a4c8a745SGarance A Drosehn #define T_SEP "," /* "Terminate-element" list separators */ 86cf22dcfcSBrian Somers 87de800cd4SGarance A Drosehn #ifdef LAZY_PS 88c46bb4b3SGarance A Drosehn #define DEF_UREAD 0 89de800cd4SGarance A Drosehn #define OPT_LAZY_f "f" 90de800cd4SGarance A Drosehn #else 91c46bb4b3SGarance A Drosehn #define DEF_UREAD 1 /* Always do the more-expensive read. */ 92de800cd4SGarance A Drosehn #define OPT_LAZY_f /* I.e., the `-f' option is not added. */ 93de800cd4SGarance A Drosehn #endif 944b88c807SRodney W. Grimes 95c675340aSGarance A Drosehn /* 96ecbb06f8SGarance A Drosehn * isdigit takes an `int', but expects values in the range of unsigned char. 97ecbb06f8SGarance A Drosehn * This wrapper ensures that values from a 'char' end up in the correct range. 98c675340aSGarance A Drosehn */ 99ecbb06f8SGarance A Drosehn #define isdigitch(Anychar) isdigit((u_char)(Anychar)) 100c675340aSGarance A Drosehn 101db91faacSPeter Wemm int cflag; /* -c */ 102de800cd4SGarance A Drosehn int eval; /* Exit value */ 103de800cd4SGarance A Drosehn time_t now; /* Current time(3) value */ 1044b88c807SRodney W. Grimes int rawcpu; /* -C */ 1054b88c807SRodney W. Grimes int sumrusage; /* -S */ 106de800cd4SGarance A Drosehn int termwidth; /* Width of the screen (0 == infinity). */ 1077ab24ea3SJulian Elischer int showthreads; /* will threads be shown? */ 1084b88c807SRodney W. Grimes 109bdf8ab46SGarance A Drosehn struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); 110de800cd4SGarance A Drosehn 111de800cd4SGarance A Drosehn static int forceuread = DEF_UREAD; /* Do extra work to get u-area. */ 112de800cd4SGarance A Drosehn static kvm_t *kd; 113de800cd4SGarance A Drosehn static int needcomm; /* -o "command" */ 114de800cd4SGarance A Drosehn static int needenv; /* -e */ 115de800cd4SGarance A Drosehn static int needuser; /* -o "user" */ 116de800cd4SGarance A Drosehn static int optfatal; /* Fatal error parsing some list-option. */ 11764b0bf0bSPawel Jakub Dawidek static int pid_max; /* kern.max_pid */ 118de800cd4SGarance A Drosehn 119de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 120ba2cd770SJuli Mallett 121a4c8a745SGarance A Drosehn struct listinfo; 122de800cd4SGarance A Drosehn typedef int addelem_rtn(struct listinfo *_inf, const char *_elem); 123a4c8a745SGarance A Drosehn 124a4c8a745SGarance A Drosehn struct listinfo { 125a4c8a745SGarance A Drosehn int count; 126a4c8a745SGarance A Drosehn int maxcount; 127a4c8a745SGarance A Drosehn int elemsize; 128a4c8a745SGarance A Drosehn addelem_rtn *addelem; 129a4c8a745SGarance A Drosehn const char *lname; 130a4c8a745SGarance A Drosehn union { 131a4c8a745SGarance A Drosehn gid_t *gids; 13213767130SBryan Drewery int *jids; 133a4c8a745SGarance A Drosehn pid_t *pids; 134a4c8a745SGarance A Drosehn dev_t *ttys; 135a4c8a745SGarance A Drosehn uid_t *uids; 136a4c8a745SGarance A Drosehn void *ptr; 137d822163fSGarance A Drosehn } l; 138a4c8a745SGarance A Drosehn }; 139a4c8a745SGarance A Drosehn 140a4c8a745SGarance A Drosehn static int addelem_gid(struct listinfo *, const char *); 14113767130SBryan Drewery static int addelem_jid(struct listinfo *, const char *); 142a4c8a745SGarance A Drosehn static int addelem_pid(struct listinfo *, const char *); 143a4c8a745SGarance A Drosehn static int addelem_tty(struct listinfo *, const char *); 144a4c8a745SGarance A Drosehn static int addelem_uid(struct listinfo *, const char *); 145a4c8a745SGarance A Drosehn static void add_list(struct listinfo *, const char *); 146044fce53SBrian Somers static void descendant_sort(KINFO *, int); 1471d1143ecSEdward Tomasz Napierala static void format_output(KINFO *); 148a4c8a745SGarance A Drosehn static void *expand_list(struct listinfo *); 149f35e0715SGarance A Drosehn static const char * 150f35e0715SGarance A Drosehn fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 1511c67ef12SJohn Baldwin KINFO *, char *, char *, int); 152a4c8a745SGarance A Drosehn static void free_list(struct listinfo *); 153a4c8a745SGarance A Drosehn static void init_list(struct listinfo *, addelem_rtn, int, const char *); 15425113083SGarance A Drosehn static char *kludge_oldps_options(const char *, char *, const char *); 1554857f240SGarance A Drosehn static int pscomp(const void *, const void *); 1564857f240SGarance A Drosehn static void saveuser(KINFO *); 1574857f240SGarance A Drosehn static void scanvars(void); 1584857f240SGarance A Drosehn static void sizevars(void); 15964b0bf0bSPawel Jakub Dawidek static void pidmax_init(void); 1604857f240SGarance A Drosehn static void usage(void); 1614b88c807SRodney W. Grimes 162c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command"; 163259fcfacSGarance A Drosehn static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command"; 1641d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state," 1651d2324f4SGarance A Drosehn "tt,time,command"; 166871e8d8cSMark Murray static char o1[] = "pid"; 167c0716492SJuli Mallett static char o2[] = "tt,state,time,command"; 168c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command"; 1691d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz," 1701d2324f4SGarance A Drosehn "%cpu,%mem,command"; 1712af538ebSRobert Watson static char Zfmt[] = "label"; 1724b88c807SRodney W. Grimes 17313767130SBryan Drewery #define PS_ARGS "AaCcde" OPT_LAZY_f "G:gHhjJ:LlM:mN:O:o:p:rSTt:U:uvwXxZ" 17441623b2dSMaxim Sobolev 1754b88c807SRodney W. Grimes int 17646251ddeSWarner Losh main(int argc, char *argv[]) 1774b88c807SRodney W. Grimes { 17813767130SBryan Drewery struct listinfo gidlist, jidlist, pgrplist, pidlist; 179a4c8a745SGarance A Drosehn struct listinfo ruidlist, sesslist, ttylist, uidlist; 1804b88c807SRodney W. Grimes struct kinfo_proc *kp; 1811d1143ecSEdward Tomasz Napierala KINFO *kinfo = NULL, *next_KINFO; 1821d1143ecSEdward Tomasz Napierala KINFO_STR *ks; 1834b88c807SRodney W. Grimes struct varent *vent; 184484f5781SPedro F. Giffuni struct winsize ws = { .ws_row = 0 }; 18516eb3576SMarcelo Araujo const char *nlistf, *memf, *str; 186ca62e195SGarance A Drosehn char *cols; 1871d1143ecSEdward Tomasz Napierala int all, ch, elem, flag, _fmt, i, lineno, linelen, left; 188044fce53SBrian Somers int descendancy, nentries, nkept, nselectors; 1897ab24ea3SJulian Elischer int prtheader, wflag, what, xkeep, xkeep_implied; 1908beb1a2fSMarcel Moolenaar int fwidthmin, fwidthmax; 191871e8d8cSMark Murray char errbuf[_POSIX2_LINE_MAX]; 1928beb1a2fSMarcel Moolenaar char fmtbuf[_POSIX2_LINE_MAX]; 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 nselectors++; 257a4c8a745SGarance A Drosehn all = xkeep = 1; 258a4c8a745SGarance A Drosehn break; 2594b88c807SRodney W. Grimes case 'a': 260a4c8a745SGarance A Drosehn nselectors++; 2614b88c807SRodney W. Grimes all = 1; 2624b88c807SRodney W. Grimes break; 2634b88c807SRodney W. Grimes case 'C': 2644b88c807SRodney W. Grimes rawcpu = 1; 2654b88c807SRodney W. Grimes break; 266db91faacSPeter Wemm case 'c': 267db91faacSPeter Wemm cflag = 1; 268db91faacSPeter Wemm break; 269044fce53SBrian Somers case 'd': 270044fce53SBrian Somers descendancy = 1; 271044fce53SBrian Somers break; 272db91faacSPeter Wemm case 'e': /* XXX set ufmt */ 273db91faacSPeter Wemm needenv = 1; 274db91faacSPeter Wemm break; 2754a355d17SGarance A Drosehn #ifdef LAZY_PS 2764a355d17SGarance A Drosehn case 'f': 2774a355d17SGarance A Drosehn if (getuid() == 0 || getgid() == 0) 2784a355d17SGarance A Drosehn forceuread = 1; 2794a355d17SGarance A Drosehn break; 2804a355d17SGarance A Drosehn #endif 281a4c8a745SGarance A Drosehn case 'G': 282a4c8a745SGarance A Drosehn add_list(&gidlist, optarg); 283a4c8a745SGarance A Drosehn xkeep_implied = 1; 284a4c8a745SGarance A Drosehn nselectors++; 285a4c8a745SGarance A Drosehn break; 2864b88c807SRodney W. Grimes case 'g': 287352b6523SGarance A Drosehn #if 0 288ba50b0e0SGarance A Drosehn /*- 289352b6523SGarance A Drosehn * XXX - This SUSv3 behavior is still under debate 290352b6523SGarance A Drosehn * since it conflicts with the (undocumented) 291352b6523SGarance A Drosehn * `-g' option. So we skip it for now. 292352b6523SGarance A Drosehn */ 293a4c8a745SGarance A Drosehn add_list(&pgrplist, optarg); 294a4c8a745SGarance A Drosehn xkeep_implied = 1; 295a4c8a745SGarance A Drosehn nselectors++; 296a4c8a745SGarance A Drosehn break; 297a4c8a745SGarance A Drosehn #else 298352b6523SGarance A Drosehn /* The historical BSD-ish (from SunOS) behavior. */ 2994b88c807SRodney W. Grimes break; /* no-op */ 300a4c8a745SGarance A Drosehn #endif 30148b8c0deSScott Long case 'H': 302d75c1d83SDaniel Eischen showthreads = KERN_PROC_INC_THREAD; 30348b8c0deSScott Long break; 3044b88c807SRodney W. Grimes case 'h': 3054b88c807SRodney W. Grimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 3064b88c807SRodney W. Grimes break; 30713767130SBryan Drewery case 'J': 30813767130SBryan Drewery add_list(&jidlist, optarg); 30913767130SBryan Drewery xkeep_implied = 1; 31013767130SBryan Drewery nselectors++; 31113767130SBryan Drewery break; 3124b88c807SRodney W. Grimes case 'j': 313fde411d5SJuli Mallett parsefmt(jfmt, 0); 314871e8d8cSMark Murray _fmt = 1; 3154b88c807SRodney W. Grimes jfmt[0] = '\0'; 3164b88c807SRodney W. Grimes break; 3174b88c807SRodney W. Grimes case 'L': 3184b88c807SRodney W. Grimes showkey(); 3194b88c807SRodney W. Grimes exit(0); 3204b88c807SRodney W. Grimes case 'l': 321fde411d5SJuli Mallett parsefmt(lfmt, 0); 322871e8d8cSMark Murray _fmt = 1; 3234b88c807SRodney W. Grimes lfmt[0] = '\0'; 3244b88c807SRodney W. Grimes break; 3254b88c807SRodney W. Grimes case 'M': 3264b88c807SRodney W. Grimes memf = optarg; 3274b88c807SRodney W. Grimes break; 3284b88c807SRodney W. Grimes case 'm': 3294b88c807SRodney W. Grimes sortby = SORTMEM; 3304b88c807SRodney W. Grimes break; 3314b88c807SRodney W. Grimes case 'N': 3324b88c807SRodney W. Grimes nlistf = optarg; 3334b88c807SRodney W. Grimes break; 3344b88c807SRodney W. Grimes case 'O': 335fde411d5SJuli Mallett parsefmt(o1, 1); 336fde411d5SJuli Mallett parsefmt(optarg, 1); 337fde411d5SJuli Mallett parsefmt(o2, 1); 3384b88c807SRodney W. Grimes o1[0] = o2[0] = '\0'; 339871e8d8cSMark Murray _fmt = 1; 3404b88c807SRodney W. Grimes break; 3414b88c807SRodney W. Grimes case 'o': 342fde411d5SJuli Mallett parsefmt(optarg, 1); 343871e8d8cSMark Murray _fmt = 1; 3444b88c807SRodney W. Grimes break; 3454b88c807SRodney W. Grimes case 'p': 346a4c8a745SGarance A Drosehn add_list(&pidlist, optarg); 347a4c8a745SGarance A Drosehn /* 348a4c8a745SGarance A Drosehn * Note: `-p' does not *set* xkeep, but any values 349a4c8a745SGarance A Drosehn * from pidlist are checked before xkeep is. That 350a4c8a745SGarance A Drosehn * way they are always matched, even if the user 351a4c8a745SGarance A Drosehn * specifies `-X'. 352a4c8a745SGarance A Drosehn */ 353a4c8a745SGarance A Drosehn nselectors++; 3544b88c807SRodney W. Grimes break; 355a4c8a745SGarance A Drosehn #if 0 356a4c8a745SGarance A Drosehn case 'R': 357ba50b0e0SGarance A Drosehn /*- 358352b6523SGarance A Drosehn * XXX - This un-standard option is still under 359352b6523SGarance A Drosehn * debate. This is what SUSv3 defines as 360352b6523SGarance A Drosehn * the `-U' option, and while it would be 361352b6523SGarance A Drosehn * nice to have, it could cause even more 362352b6523SGarance A Drosehn * confusion to implement it as `-R'. 363352b6523SGarance A Drosehn */ 364a4c8a745SGarance A Drosehn add_list(&ruidlist, optarg); 365a4c8a745SGarance A Drosehn xkeep_implied = 1; 366a4c8a745SGarance A Drosehn nselectors++; 367a4c8a745SGarance A Drosehn break; 368a4c8a745SGarance A Drosehn #endif 3694b88c807SRodney W. Grimes case 'r': 3704b88c807SRodney W. Grimes sortby = SORTCPU; 3714b88c807SRodney W. Grimes break; 3724b88c807SRodney W. Grimes case 'S': 3734b88c807SRodney W. Grimes sumrusage = 1; 3744b88c807SRodney W. Grimes break; 375a4c8a745SGarance A Drosehn #if 0 376a4c8a745SGarance A Drosehn case 's': 377ba50b0e0SGarance A Drosehn /*- 378352b6523SGarance A Drosehn * XXX - This non-standard option is still under 379352b6523SGarance A Drosehn * debate. This *is* supported on Solaris, 380352b6523SGarance A Drosehn * Linux, and IRIX, but conflicts with `-s' 381352b6523SGarance A Drosehn * on NetBSD and maybe some older BSD's. 382352b6523SGarance A Drosehn */ 383a4c8a745SGarance A Drosehn add_list(&sesslist, optarg); 384a4c8a745SGarance A Drosehn xkeep_implied = 1; 385a4c8a745SGarance A Drosehn nselectors++; 386a4c8a745SGarance A Drosehn break; 387a4c8a745SGarance A Drosehn #endif 3884b88c807SRodney W. Grimes case 'T': 3894b88c807SRodney W. Grimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 3908beb1a2fSMarcel Moolenaar xo_errx(1, "stdin: not a terminal"); 3914b88c807SRodney W. Grimes /* FALLTHROUGH */ 392a4c8a745SGarance A Drosehn case 't': 393a4c8a745SGarance A Drosehn add_list(&ttylist, optarg); 394a4c8a745SGarance A Drosehn xkeep_implied = 1; 395a4c8a745SGarance A Drosehn nselectors++; 3964b88c807SRodney W. Grimes break; 39773eb8310SPeter Wemm case 'U': 398a4c8a745SGarance A Drosehn /* This is what SUSv3 defines as the `-u' option. */ 399a4c8a745SGarance A Drosehn add_list(&uidlist, optarg); 400a4c8a745SGarance A Drosehn xkeep_implied = 1; 401a4c8a745SGarance A Drosehn nselectors++; 40273eb8310SPeter Wemm break; 4034b88c807SRodney W. Grimes case 'u': 404fde411d5SJuli Mallett parsefmt(ufmt, 0); 4054b88c807SRodney W. Grimes sortby = SORTCPU; 406871e8d8cSMark Murray _fmt = 1; 4074b88c807SRodney W. Grimes ufmt[0] = '\0'; 4084b88c807SRodney W. Grimes break; 4094b88c807SRodney W. Grimes case 'v': 410fde411d5SJuli Mallett parsefmt(vfmt, 0); 4114b88c807SRodney W. Grimes sortby = SORTMEM; 412871e8d8cSMark Murray _fmt = 1; 4134b88c807SRodney W. Grimes vfmt[0] = '\0'; 4144b88c807SRodney W. Grimes break; 4154b88c807SRodney W. Grimes case 'w': 4164b88c807SRodney W. Grimes if (wflag) 4174b88c807SRodney W. Grimes termwidth = UNLIMITED; 418ef1d40daSConrad Meyer else if (termwidth < 131 && termwidth != UNLIMITED) 4194b88c807SRodney W. Grimes termwidth = 131; 4204b88c807SRodney W. Grimes wflag++; 4214b88c807SRodney W. Grimes break; 422a4c8a745SGarance A Drosehn case 'X': 423a4c8a745SGarance A Drosehn /* 424a4c8a745SGarance A Drosehn * Note that `-X' and `-x' are not standard "selector" 425a4c8a745SGarance A Drosehn * options. For most selector-options, we check *all* 426a4c8a745SGarance A Drosehn * processes to see if any are matched by the given 427a4c8a745SGarance A Drosehn * value(s). After we have a set of all the matched 428a4c8a745SGarance A Drosehn * processes, then `-X' and `-x' govern whether we 429a4c8a745SGarance A Drosehn * modify that *matched* set for processes which do 430a4c8a745SGarance A Drosehn * not have a controlling terminal. `-X' causes 431a4c8a745SGarance A Drosehn * those processes to be deleted from the matched 432a4c8a745SGarance A Drosehn * set, while `-x' causes them to be kept. 433a4c8a745SGarance A Drosehn */ 434a4c8a745SGarance A Drosehn xkeep = 0; 435a4c8a745SGarance A Drosehn break; 4364b88c807SRodney W. Grimes case 'x': 437a4c8a745SGarance A Drosehn xkeep = 1; 4384b88c807SRodney W. Grimes break; 4397304f61fSBrian Feldman case 'Z': 440fde411d5SJuli Mallett parsefmt(Zfmt, 0); 4417304f61fSBrian Feldman Zfmt[0] = '\0'; 4427304f61fSBrian Feldman break; 4434b88c807SRodney W. Grimes case '?': 4444b88c807SRodney W. Grimes default: 4454b88c807SRodney W. Grimes usage(); 4464b88c807SRodney W. Grimes } 4474b88c807SRodney W. Grimes argc -= optind; 4484b88c807SRodney W. Grimes argv += optind; 449c675340aSGarance A Drosehn 450c675340aSGarance A Drosehn /* 451c675340aSGarance A Drosehn * If there arguments after processing all the options, attempt 452c675340aSGarance A Drosehn * to treat them as a list of process ids. 453c675340aSGarance A Drosehn */ 454c675340aSGarance A Drosehn while (*argv) { 455c675340aSGarance A Drosehn if (!isdigitch(**argv)) 456c675340aSGarance A Drosehn break; 457c675340aSGarance A Drosehn add_list(&pidlist, *argv); 458c675340aSGarance A Drosehn argv++; 459c675340aSGarance A Drosehn } 460c675340aSGarance A Drosehn if (*argv) { 4618beb1a2fSMarcel Moolenaar xo_warnx("illegal argument: %s\n", *argv); 462c675340aSGarance A Drosehn usage(); 463c675340aSGarance A Drosehn } 464a4c8a745SGarance A Drosehn if (optfatal) 465352b6523SGarance A Drosehn exit(1); /* Error messages already printed. */ 466352b6523SGarance A Drosehn if (xkeep < 0) /* Neither -X nor -x was specified. */ 467a4c8a745SGarance A Drosehn xkeep = xkeep_implied; 468a4c8a745SGarance A Drosehn 469831c910aSRuslan Ermilov kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 470ec23a763SMarcelo Araujo if (kd == NULL) 4718beb1a2fSMarcel Moolenaar xo_errx(1, "%s", errbuf); 4724b88c807SRodney W. Grimes 473871e8d8cSMark Murray if (!_fmt) 474fde411d5SJuli Mallett parsefmt(dfmt, 0); 4754b88c807SRodney W. Grimes 476a4c8a745SGarance A Drosehn if (nselectors == 0) { 477d822163fSGarance A Drosehn uidlist.l.ptr = malloc(sizeof(uid_t)); 478d822163fSGarance A Drosehn if (uidlist.l.ptr == NULL) 4798beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 480a4c8a745SGarance A Drosehn nselectors = 1; 481a4c8a745SGarance A Drosehn uidlist.count = uidlist.maxcount = 1; 482d822163fSGarance A Drosehn *uidlist.l.uids = getuid(); 483cf22dcfcSBrian Somers } 4844b88c807SRodney W. Grimes 4854b88c807SRodney W. Grimes /* 4864b88c807SRodney W. Grimes * scan requested variables, noting what structures are needed, 487bdfebd84SKris Kennaway * and adjusting header widths as appropriate. 4884b88c807SRodney W. Grimes */ 4894b88c807SRodney W. Grimes scanvars(); 490a4c8a745SGarance A Drosehn 4914b88c807SRodney W. Grimes /* 492a4c8a745SGarance A Drosehn * Get process list. If the user requested just one selector- 493a4c8a745SGarance A Drosehn * option, then kvm_getprocs can be asked to return just those 494a4c8a745SGarance A Drosehn * processes. Otherwise, have it return all processes, and 495a4c8a745SGarance A Drosehn * then this routine will search that full list and select the 496a4c8a745SGarance A Drosehn * processes which match any of the user's selector-options. 4974b88c807SRodney W. Grimes */ 498d75c1d83SDaniel Eischen what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC; 49948b8c0deSScott Long flag = 0; 500a4c8a745SGarance A Drosehn if (nselectors == 1) { 5017bd42165SGarance A Drosehn if (gidlist.count == 1) { 5027bd42165SGarance A Drosehn what = KERN_PROC_RGID | showthreads; 5037bd42165SGarance A Drosehn flag = *gidlist.l.gids; 5047bd42165SGarance A Drosehn nselectors = 0; 5057bd42165SGarance A Drosehn } else if (pgrplist.count == 1) { 506a4c8a745SGarance A Drosehn what = KERN_PROC_PGRP | showthreads; 507d822163fSGarance A Drosehn flag = *pgrplist.l.pids; 508a4c8a745SGarance A Drosehn nselectors = 0; 509a4c8a745SGarance A Drosehn } else if (pidlist.count == 1) { 510a4c8a745SGarance A Drosehn what = KERN_PROC_PID | showthreads; 511d822163fSGarance A Drosehn flag = *pidlist.l.pids; 512a4c8a745SGarance A Drosehn nselectors = 0; 513a4c8a745SGarance A Drosehn } else if (ruidlist.count == 1) { 514a4c8a745SGarance A Drosehn what = KERN_PROC_RUID | showthreads; 515d822163fSGarance A Drosehn flag = *ruidlist.l.uids; 516a4c8a745SGarance A Drosehn nselectors = 0; 517a4c8a745SGarance A Drosehn } else if (sesslist.count == 1) { 518a4c8a745SGarance A Drosehn what = KERN_PROC_SESSION | showthreads; 519d822163fSGarance A Drosehn flag = *sesslist.l.pids; 520a4c8a745SGarance A Drosehn nselectors = 0; 521a4c8a745SGarance A Drosehn } else if (ttylist.count == 1) { 522a4c8a745SGarance A Drosehn what = KERN_PROC_TTY | showthreads; 523d822163fSGarance A Drosehn flag = *ttylist.l.ttys; 524a4c8a745SGarance A Drosehn nselectors = 0; 525a4c8a745SGarance A Drosehn } else if (uidlist.count == 1) { 526a4c8a745SGarance A Drosehn what = KERN_PROC_UID | showthreads; 527d822163fSGarance A Drosehn flag = *uidlist.l.uids; 528a4c8a745SGarance A Drosehn nselectors = 0; 529a4c8a745SGarance A Drosehn } else if (all) { 530a4c8a745SGarance A Drosehn /* No need for this routine to select processes. */ 531a4c8a745SGarance A Drosehn nselectors = 0; 532a4c8a745SGarance A Drosehn } 5334b88c807SRodney W. Grimes } 534d75c1d83SDaniel Eischen 5354b88c807SRodney W. Grimes /* 5364b88c807SRodney W. Grimes * select procs 5374b88c807SRodney W. Grimes */ 538a4c8a745SGarance A Drosehn nentries = -1; 5394e8b6a6fSGarance A Drosehn kp = kvm_getprocs(kd, what, flag, &nentries); 540d7026b96SEdward Tomasz Napierala /* 541d7026b96SEdward Tomasz Napierala * Ignore ESRCH to preserve behaviour of "ps -p nonexistent-pid" 542d7026b96SEdward Tomasz Napierala * not reporting an error. 543d7026b96SEdward Tomasz Napierala */ 544d7026b96SEdward Tomasz Napierala if ((kp == NULL && errno != ESRCH) || (kp != NULL && nentries < 0)) 5458beb1a2fSMarcel Moolenaar xo_errx(1, "%s", kvm_geterr(kd)); 546a4c8a745SGarance A Drosehn nkept = 0; 5474e8b6a6fSGarance A Drosehn if (nentries > 0) { 5484b88c807SRodney W. Grimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 5498beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 5504b88c807SRodney W. Grimes for (i = nentries; --i >= 0; ++kp) { 551a4c8a745SGarance A Drosehn /* 552a4c8a745SGarance A Drosehn * If the user specified multiple selection-criteria, 553a4c8a745SGarance A Drosehn * then keep any process matched by the inclusive OR 554a4c8a745SGarance A Drosehn * of all the selection-criteria given. 555a4c8a745SGarance A Drosehn */ 556a4c8a745SGarance A Drosehn if (pidlist.count > 0) { 557a4c8a745SGarance A Drosehn for (elem = 0; elem < pidlist.count; elem++) 558d822163fSGarance A Drosehn if (kp->ki_pid == pidlist.l.pids[elem]) 559a4c8a745SGarance A Drosehn goto keepit; 560a4c8a745SGarance A Drosehn } 561a4c8a745SGarance A Drosehn /* 562a4c8a745SGarance A Drosehn * Note that we had to process pidlist before 563a4c8a745SGarance A Drosehn * filtering out processes which do not have 564a4c8a745SGarance A Drosehn * a controlling terminal. 565a4c8a745SGarance A Drosehn */ 566a4c8a745SGarance A Drosehn if (xkeep == 0) { 567a4c8a745SGarance A Drosehn if ((kp->ki_tdev == NODEV || 568a4c8a745SGarance A Drosehn (kp->ki_flag & P_CONTROLT) == 0)) 569a4c8a745SGarance A Drosehn continue; 570a4c8a745SGarance A Drosehn } 571a4c8a745SGarance A Drosehn if (nselectors == 0) 572a4c8a745SGarance A Drosehn goto keepit; 573a4c8a745SGarance A Drosehn if (gidlist.count > 0) { 574a4c8a745SGarance A Drosehn for (elem = 0; elem < gidlist.count; elem++) 575d822163fSGarance A Drosehn if (kp->ki_rgid == gidlist.l.gids[elem]) 576a4c8a745SGarance A Drosehn goto keepit; 577a4c8a745SGarance A Drosehn } 57813767130SBryan Drewery if (jidlist.count > 0) { 57913767130SBryan Drewery for (elem = 0; elem < jidlist.count; elem++) 58013767130SBryan Drewery if (kp->ki_jid == jidlist.l.jids[elem]) 58113767130SBryan Drewery goto keepit; 58213767130SBryan Drewery } 583a4c8a745SGarance A Drosehn if (pgrplist.count > 0) { 584a4c8a745SGarance A Drosehn for (elem = 0; elem < pgrplist.count; elem++) 585d822163fSGarance A Drosehn if (kp->ki_pgid == 586d822163fSGarance A Drosehn pgrplist.l.pids[elem]) 587a4c8a745SGarance A Drosehn goto keepit; 588a4c8a745SGarance A Drosehn } 589a4c8a745SGarance A Drosehn if (ruidlist.count > 0) { 590a4c8a745SGarance A Drosehn for (elem = 0; elem < ruidlist.count; elem++) 591d822163fSGarance A Drosehn if (kp->ki_ruid == 592d822163fSGarance A Drosehn ruidlist.l.uids[elem]) 593a4c8a745SGarance A Drosehn goto keepit; 594a4c8a745SGarance A Drosehn } 595a4c8a745SGarance A Drosehn if (sesslist.count > 0) { 596a4c8a745SGarance A Drosehn for (elem = 0; elem < sesslist.count; elem++) 597d822163fSGarance A Drosehn if (kp->ki_sid == sesslist.l.pids[elem]) 598a4c8a745SGarance A Drosehn goto keepit; 599a4c8a745SGarance A Drosehn } 600a4c8a745SGarance A Drosehn if (ttylist.count > 0) { 601a4c8a745SGarance A Drosehn for (elem = 0; elem < ttylist.count; elem++) 602d822163fSGarance A Drosehn if (kp->ki_tdev == ttylist.l.ttys[elem]) 603a4c8a745SGarance A Drosehn goto keepit; 604a4c8a745SGarance A Drosehn } 605a4c8a745SGarance A Drosehn if (uidlist.count > 0) { 606a4c8a745SGarance A Drosehn for (elem = 0; elem < uidlist.count; elem++) 607d822163fSGarance A Drosehn if (kp->ki_uid == uidlist.l.uids[elem]) 608a4c8a745SGarance A Drosehn goto keepit; 609a4c8a745SGarance A Drosehn } 610a4c8a745SGarance A Drosehn /* 611a4c8a745SGarance A Drosehn * This process did not match any of the user's 612a4c8a745SGarance A Drosehn * selector-options, so skip the process. 613a4c8a745SGarance A Drosehn */ 614a4c8a745SGarance A Drosehn continue; 615a4c8a745SGarance A Drosehn 616a4c8a745SGarance A Drosehn keepit: 6174bac4483SGarance A Drosehn next_KINFO = &kinfo[nkept]; 6184bac4483SGarance A Drosehn next_KINFO->ki_p = kp; 619044fce53SBrian Somers next_KINFO->ki_d.level = 0; 620044fce53SBrian Somers next_KINFO->ki_d.prefix = NULL; 6214bac4483SGarance A Drosehn next_KINFO->ki_pcpu = getpcpu(next_KINFO); 6224bac4483SGarance A Drosehn if (sortby == SORTMEM) 6234bac4483SGarance A Drosehn next_KINFO->ki_memsize = kp->ki_tsize + 6244bac4483SGarance A Drosehn kp->ki_dsize + kp->ki_ssize; 6254b88c807SRodney W. Grimes if (needuser) 6264bac4483SGarance A Drosehn saveuser(next_KINFO); 627a4c8a745SGarance A Drosehn nkept++; 6284b88c807SRodney W. Grimes } 6294e8b6a6fSGarance A Drosehn } 6306a2d726bSJordan K. Hubbard 6316a2d726bSJordan K. Hubbard sizevars(); 6326a2d726bSJordan K. Hubbard 6331d1143ecSEdward Tomasz Napierala if (nkept == 0) { 6344b88c807SRodney W. Grimes printheader(); 63581fc45fcSKonstantin Belousov xo_finish(); 636f8c9c11cSWill Andrews exit(1); 6371d1143ecSEdward Tomasz Napierala } 638a4c8a745SGarance A Drosehn 6394b88c807SRodney W. Grimes /* 6404b88c807SRodney W. Grimes * sort proc list 6414b88c807SRodney W. Grimes */ 642a4c8a745SGarance A Drosehn qsort(kinfo, nkept, sizeof(KINFO), pscomp); 643044fce53SBrian Somers 644044fce53SBrian Somers /* 645044fce53SBrian Somers * We want things in descendant order 646044fce53SBrian Somers */ 647044fce53SBrian Somers if (descendancy) 648044fce53SBrian Somers descendant_sort(kinfo, nkept); 649044fce53SBrian Somers 6501d1143ecSEdward Tomasz Napierala 6514b88c807SRodney W. Grimes /* 6521d1143ecSEdward Tomasz Napierala * Prepare formatted output. 6531d1143ecSEdward Tomasz Napierala */ 6541d1143ecSEdward Tomasz Napierala for (i = 0; i < nkept; i++) 6551d1143ecSEdward Tomasz Napierala format_output(&kinfo[i]); 6561d1143ecSEdward Tomasz Napierala 6571d1143ecSEdward Tomasz Napierala /* 6581d1143ecSEdward Tomasz Napierala * Print header. 6591d1143ecSEdward Tomasz Napierala */ 6608beb1a2fSMarcel Moolenaar xo_open_container("process-information"); 6611d1143ecSEdward Tomasz Napierala printheader(); 6628beb1a2fSMarcel Moolenaar if (xo_get_style(NULL) != XO_STYLE_TEXT) 6638beb1a2fSMarcel Moolenaar termwidth = UNLIMITED; 6641d1143ecSEdward Tomasz Napierala 6651d1143ecSEdward Tomasz Napierala /* 6661d1143ecSEdward Tomasz Napierala * Output formatted lines. 6674b88c807SRodney W. Grimes */ 6688beb1a2fSMarcel Moolenaar xo_open_list("process"); 669a4c8a745SGarance A Drosehn for (i = lineno = 0; i < nkept; i++) { 6701d1143ecSEdward Tomasz Napierala linelen = 0; 6718beb1a2fSMarcel Moolenaar xo_open_instance("process"); 672bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 6731d1143ecSEdward Tomasz Napierala ks = STAILQ_FIRST(&kinfo[i].ki_ks); 6741d1143ecSEdward Tomasz Napierala STAILQ_REMOVE_HEAD(&kinfo[i].ki_ks, ks_next); 67538494effSUlrich Spörlein /* Truncate rightmost column if necessary. */ 6768beb1a2fSMarcel Moolenaar fwidthmax = _POSIX2_LINE_MAX; 6771d1143ecSEdward Tomasz Napierala if (STAILQ_NEXT(vent, next_ve) == NULL && 6781d1143ecSEdward Tomasz Napierala termwidth != UNLIMITED && ks->ks_str != NULL) { 6791d1143ecSEdward Tomasz Napierala left = termwidth - linelen; 6801d1143ecSEdward Tomasz Napierala if (left > 0 && left < (int)strlen(ks->ks_str)) 6818beb1a2fSMarcel Moolenaar fwidthmax = left; 6821d1143ecSEdward Tomasz Napierala } 6838beb1a2fSMarcel Moolenaar 6841d1143ecSEdward Tomasz Napierala str = ks->ks_str; 6851d1143ecSEdward Tomasz Napierala if (str == NULL) 6861d1143ecSEdward Tomasz Napierala str = "-"; 6871d1143ecSEdward Tomasz Napierala /* No padding for the last column, if it's LJUST. */ 6888beb1a2fSMarcel Moolenaar fwidthmin = (xo_get_style(NULL) != XO_STYLE_TEXT || 6898beb1a2fSMarcel Moolenaar (STAILQ_NEXT(vent, next_ve) == NULL && 6908beb1a2fSMarcel Moolenaar (vent->var->flag & LJUST))) ? 0 : vent->var->width; 6918beb1a2fSMarcel Moolenaar snprintf(fmtbuf, sizeof(fmtbuf), "{:%s/%%%s%d..%ds}", 692*c6a5ff71SEitan Adler vent->var->field ? vent->var->field : vent->var->name, 6938beb1a2fSMarcel Moolenaar (vent->var->flag & LJUST) ? "-" : "", 6948beb1a2fSMarcel Moolenaar fwidthmin, fwidthmax); 6958beb1a2fSMarcel Moolenaar xo_emit(fmtbuf, str); 6968beb1a2fSMarcel Moolenaar linelen += fwidthmin; 6971d1143ecSEdward Tomasz Napierala 6981d1143ecSEdward Tomasz Napierala if (ks->ks_str != NULL) { 6991d1143ecSEdward Tomasz Napierala free(ks->ks_str); 7001d1143ecSEdward Tomasz Napierala ks->ks_str = NULL; 7011d1143ecSEdward Tomasz Napierala } 7021d1143ecSEdward Tomasz Napierala free(ks); 7031d1143ecSEdward Tomasz Napierala ks = NULL; 7041d1143ecSEdward Tomasz Napierala 7051d1143ecSEdward Tomasz Napierala if (STAILQ_NEXT(vent, next_ve) != NULL) { 7068beb1a2fSMarcel Moolenaar xo_emit("{P: }"); 7071d1143ecSEdward Tomasz Napierala linelen++; 7081d1143ecSEdward Tomasz Napierala } 7094b88c807SRodney W. Grimes } 7108beb1a2fSMarcel Moolenaar xo_emit("\n"); 7118beb1a2fSMarcel Moolenaar xo_close_instance("process"); 7124b88c807SRodney W. Grimes if (prtheader && lineno++ == prtheader - 4) { 7138beb1a2fSMarcel Moolenaar xo_emit("\n"); 7144b88c807SRodney W. Grimes printheader(); 7154b88c807SRodney W. Grimes lineno = 0; 7164b88c807SRodney W. Grimes } 7174b88c807SRodney W. Grimes } 7188beb1a2fSMarcel Moolenaar xo_close_list("process"); 7198beb1a2fSMarcel Moolenaar xo_close_container("process-information"); 7208beb1a2fSMarcel Moolenaar xo_finish(); 7218beb1a2fSMarcel Moolenaar 722a4c8a745SGarance A Drosehn free_list(&gidlist); 72313767130SBryan Drewery free_list(&jidlist); 724a4c8a745SGarance A Drosehn free_list(&pidlist); 725a4c8a745SGarance A Drosehn free_list(&pgrplist); 726a4c8a745SGarance A Drosehn free_list(&ruidlist); 727a4c8a745SGarance A Drosehn free_list(&sesslist); 728a4c8a745SGarance A Drosehn free_list(&ttylist); 729a4c8a745SGarance A Drosehn free_list(&uidlist); 730044fce53SBrian Somers for (i = 0; i < nkept; i++) 731044fce53SBrian Somers free(kinfo[i].ki_d.prefix); 732044fce53SBrian Somers free(kinfo); 7332e6c6ac4SGarance A Drosehn 7344b88c807SRodney W. Grimes exit(eval); 7354b88c807SRodney W. Grimes } 7364b88c807SRodney W. Grimes 737a4c8a745SGarance A Drosehn static int 738a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem) 7394e8b6a6fSGarance A Drosehn { 740a4c8a745SGarance A Drosehn struct group *grp; 741a4c8a745SGarance A Drosehn const char *nameorID; 742a4c8a745SGarance A Drosehn char *endp; 7430b42be7cSGarance A Drosehn u_long bigtemp; 7444e8b6a6fSGarance A Drosehn 745a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 746a4c8a745SGarance A Drosehn if (*elem == '\0') 7478beb1a2fSMarcel Moolenaar xo_warnx("Invalid (zero-length) %s name", inf->lname); 748a4c8a745SGarance A Drosehn else 7498beb1a2fSMarcel Moolenaar xo_warnx("%s name too long: %s", inf->lname, elem); 750a4c8a745SGarance A Drosehn optfatal = 1; 751352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 7524e8b6a6fSGarance A Drosehn } 753a4c8a745SGarance A Drosehn 7544e8b6a6fSGarance A Drosehn /* 755a4c8a745SGarance A Drosehn * SUSv3 states that `ps -G grouplist' should match "real-group 756a4c8a745SGarance A Drosehn * ID numbers", and does not mention group-names. I do want to 757a4c8a745SGarance A Drosehn * also support group-names, so this tries for a group-id first, 758a4c8a745SGarance A Drosehn * and only tries for a name if that doesn't work. This is the 759a4c8a745SGarance A Drosehn * opposite order of what is done in addelem_uid(), but in 760a4c8a745SGarance A Drosehn * practice the order would only matter for group-names which 761a4c8a745SGarance A Drosehn * are all-numeric. 7624e8b6a6fSGarance A Drosehn */ 763a4c8a745SGarance A Drosehn grp = NULL; 764a4c8a745SGarance A Drosehn nameorID = "named"; 765a4c8a745SGarance A Drosehn errno = 0; 7660b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 7670b42be7cSGarance A Drosehn if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) { 768a4c8a745SGarance A Drosehn nameorID = "name or ID matches"; 7690b42be7cSGarance A Drosehn grp = getgrgid((gid_t)bigtemp); 770a4c8a745SGarance A Drosehn } 771a4c8a745SGarance A Drosehn if (grp == NULL) 772a4c8a745SGarance A Drosehn grp = getgrnam(elem); 773a4c8a745SGarance A Drosehn if (grp == NULL) { 7748beb1a2fSMarcel Moolenaar xo_warnx("No %s %s '%s'", inf->lname, nameorID, elem); 775a4c8a745SGarance A Drosehn optfatal = 1; 776c23b00b7SGarance A Drosehn return (0); 777a4c8a745SGarance A Drosehn } 778a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 779a4c8a745SGarance A Drosehn expand_list(inf); 780d822163fSGarance A Drosehn inf->l.gids[(inf->count)++] = grp->gr_gid; 781a4c8a745SGarance A Drosehn return (1); 782a4c8a745SGarance A Drosehn } 783a4c8a745SGarance A Drosehn 784a4c8a745SGarance A Drosehn static int 78513767130SBryan Drewery addelem_jid(struct listinfo *inf, const char *elem) 78613767130SBryan Drewery { 78713767130SBryan Drewery int tempid; 78813767130SBryan Drewery 78913767130SBryan Drewery if (*elem == '\0') { 79013767130SBryan Drewery warnx("Invalid (zero-length) jail id"); 79113767130SBryan Drewery optfatal = 1; 79213767130SBryan Drewery return (0); /* Do not add this value. */ 79313767130SBryan Drewery } 79413767130SBryan Drewery 79513767130SBryan Drewery tempid = jail_getid(elem); 79613767130SBryan Drewery if (tempid < 0) { 79713767130SBryan Drewery warnx("Invalid %s: %s", inf->lname, elem); 79813767130SBryan Drewery optfatal = 1; 79913767130SBryan Drewery return (0); 80013767130SBryan Drewery } 80113767130SBryan Drewery 80213767130SBryan Drewery if (inf->count >= inf->maxcount) 80313767130SBryan Drewery expand_list(inf); 80413767130SBryan Drewery inf->l.jids[(inf->count)++] = tempid; 80513767130SBryan Drewery return (1); 80613767130SBryan Drewery } 80713767130SBryan Drewery 80813767130SBryan Drewery static int 809a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem) 810a4c8a745SGarance A Drosehn { 811a4c8a745SGarance A Drosehn char *endp; 812ca62e195SGarance A Drosehn long tempid; 813a4c8a745SGarance A Drosehn 814de1702f4SGarance A Drosehn if (*elem == '\0') { 8158beb1a2fSMarcel Moolenaar xo_warnx("Invalid (zero-length) process id"); 816de1702f4SGarance A Drosehn optfatal = 1; 817de1702f4SGarance A Drosehn return (0); /* Do not add this value. */ 81825113083SGarance A Drosehn } 81925113083SGarance A Drosehn 820a4c8a745SGarance A Drosehn errno = 0; 821a4c8a745SGarance A Drosehn tempid = strtol(elem, &endp, 10); 822a4c8a745SGarance A Drosehn if (*endp != '\0' || tempid < 0 || elem == endp) { 8238beb1a2fSMarcel Moolenaar xo_warnx("Invalid %s: %s", inf->lname, elem); 8244e8b6a6fSGarance A Drosehn errno = ERANGE; 82564b0bf0bSPawel Jakub Dawidek } else if (errno != 0 || tempid > pid_max) { 8268beb1a2fSMarcel Moolenaar xo_warnx("%s too large: %s", inf->lname, elem); 8274e8b6a6fSGarance A Drosehn errno = ERANGE; 8284e8b6a6fSGarance A Drosehn } 8294e8b6a6fSGarance A Drosehn if (errno == ERANGE) { 830a4c8a745SGarance A Drosehn optfatal = 1; 831c23b00b7SGarance A Drosehn return (0); 8324e8b6a6fSGarance A Drosehn } 833a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 834a4c8a745SGarance A Drosehn expand_list(inf); 835d822163fSGarance A Drosehn inf->l.pids[(inf->count)++] = tempid; 836a4c8a745SGarance A Drosehn return (1); 8374e8b6a6fSGarance A Drosehn } 8384e8b6a6fSGarance A Drosehn 83923b8efadSGarance A Drosehn /*- 84023b8efadSGarance A Drosehn * The user can specify a device via one of three formats: 8418a529f59SJohn Baldwin * 1) fully qualified, e.g.: /dev/ttyp0 /dev/console /dev/pts/0 8428a529f59SJohn Baldwin * 2) missing "/dev", e.g.: ttyp0 console pts/0 8438a529f59SJohn Baldwin * 3) two-letters, e.g.: p0 co 0 84423b8efadSGarance A Drosehn * (matching letters that would be seen in the "TT" column) 84523b8efadSGarance A Drosehn */ 846a4c8a745SGarance A Drosehn static int 847a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem) 848cf22dcfcSBrian Somers { 849a4c8a745SGarance A Drosehn const char *ttypath; 850ca62e195SGarance A Drosehn struct stat sb; 8518a529f59SJohn Baldwin char pathbuf[PATH_MAX], pathbuf2[PATH_MAX], pathbuf3[PATH_MAX]; 852a4c8a745SGarance A Drosehn 85323b8efadSGarance A Drosehn ttypath = NULL; 854f2fbfae6SGarance A Drosehn pathbuf2[0] = '\0'; 8558a529f59SJohn Baldwin pathbuf3[0] = '\0'; 85623b8efadSGarance A Drosehn switch (*elem) { 85723b8efadSGarance A Drosehn case '/': 858a4c8a745SGarance A Drosehn ttypath = elem; 85923b8efadSGarance A Drosehn break; 86023b8efadSGarance A Drosehn case 'c': 86123b8efadSGarance A Drosehn if (strcmp(elem, "co") == 0) { 86223b8efadSGarance A Drosehn ttypath = _PATH_CONSOLE; 86323b8efadSGarance A Drosehn break; 86423b8efadSGarance A Drosehn } 86523b8efadSGarance A Drosehn /* FALLTHROUGH */ 86623b8efadSGarance A Drosehn default: 86723b8efadSGarance A Drosehn strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf)); 868a4c8a745SGarance A Drosehn strlcat(pathbuf, elem, sizeof(pathbuf)); 869a4c8a745SGarance A Drosehn ttypath = pathbuf; 870f2fbfae6SGarance A Drosehn if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0) 87123b8efadSGarance A Drosehn break; 8728a529f59SJohn Baldwin if (strncmp(pathbuf, _PATH_PTS, strlen(_PATH_PTS)) == 0) 8738a529f59SJohn Baldwin break; 87423b8efadSGarance A Drosehn if (strcmp(pathbuf, _PATH_CONSOLE) == 0) 87523b8efadSGarance A Drosehn break; 876f2fbfae6SGarance A Drosehn /* Check to see if /dev/tty${elem} exists */ 877f2fbfae6SGarance A Drosehn strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2)); 878f2fbfae6SGarance A Drosehn strlcat(pathbuf2, elem, sizeof(pathbuf2)); 879f2fbfae6SGarance A Drosehn if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) { 88023b8efadSGarance A Drosehn /* No need to repeat stat() && S_ISCHR() checks */ 88123b8efadSGarance A Drosehn ttypath = NULL; 88223b8efadSGarance A Drosehn break; 883a4c8a745SGarance A Drosehn } 8848a529f59SJohn Baldwin /* Check to see if /dev/pts/${elem} exists */ 8858a529f59SJohn Baldwin strlcpy(pathbuf3, _PATH_PTS, sizeof(pathbuf3)); 8868a529f59SJohn Baldwin strlcat(pathbuf3, elem, sizeof(pathbuf3)); 8878a529f59SJohn Baldwin if (stat(pathbuf3, &sb) == 0 && S_ISCHR(sb.st_mode)) { 8888a529f59SJohn Baldwin /* No need to repeat stat() && S_ISCHR() checks */ 8898a529f59SJohn Baldwin ttypath = NULL; 8908a529f59SJohn Baldwin break; 8918a529f59SJohn Baldwin } 89223b8efadSGarance A Drosehn break; 89323b8efadSGarance A Drosehn } 89423b8efadSGarance A Drosehn if (ttypath) { 895a4c8a745SGarance A Drosehn if (stat(ttypath, &sb) == -1) { 8968a529f59SJohn Baldwin if (pathbuf3[0] != '\0') 8978beb1a2fSMarcel Moolenaar xo_warn("%s, %s, and %s", pathbuf3, pathbuf2, 8988a529f59SJohn Baldwin ttypath); 899f2fbfae6SGarance A Drosehn else 9008beb1a2fSMarcel Moolenaar xo_warn("%s", ttypath); 901a4c8a745SGarance A Drosehn optfatal = 1; 902c23b00b7SGarance A Drosehn return (0); 903a4c8a745SGarance A Drosehn } 904a4c8a745SGarance A Drosehn if (!S_ISCHR(sb.st_mode)) { 9058a529f59SJohn Baldwin if (pathbuf3[0] != '\0') 9068beb1a2fSMarcel Moolenaar xo_warnx("%s, %s, and %s: Not a terminal", 9078a529f59SJohn Baldwin pathbuf3, pathbuf2, ttypath); 908f2fbfae6SGarance A Drosehn else 9098beb1a2fSMarcel Moolenaar xo_warnx("%s: Not a terminal", ttypath); 910a4c8a745SGarance A Drosehn optfatal = 1; 911c23b00b7SGarance A Drosehn return (0); 912a4c8a745SGarance A Drosehn } 91323b8efadSGarance A Drosehn } 914a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 915a4c8a745SGarance A Drosehn expand_list(inf); 916d822163fSGarance A Drosehn inf->l.ttys[(inf->count)++] = sb.st_rdev; 917a4c8a745SGarance A Drosehn return (1); 918a4c8a745SGarance A Drosehn } 919a4c8a745SGarance A Drosehn 920a4c8a745SGarance A Drosehn static int 921a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem) 922a4c8a745SGarance A Drosehn { 923cf22dcfcSBrian Somers struct passwd *pwd; 924a4c8a745SGarance A Drosehn char *endp; 9250b42be7cSGarance A Drosehn u_long bigtemp; 926cf22dcfcSBrian Somers 927a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 928a4c8a745SGarance A Drosehn if (*elem == '\0') 9298beb1a2fSMarcel Moolenaar xo_warnx("Invalid (zero-length) %s name", inf->lname); 930a4c8a745SGarance A Drosehn else 9318beb1a2fSMarcel Moolenaar xo_warnx("%s name too long: %s", inf->lname, elem); 932a4c8a745SGarance A Drosehn optfatal = 1; 933352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 934a4c8a745SGarance A Drosehn } 935cf22dcfcSBrian Somers 936a4c8a745SGarance A Drosehn pwd = getpwnam(elem); 937a4c8a745SGarance A Drosehn if (pwd == NULL) { 938a4c8a745SGarance A Drosehn errno = 0; 9390b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 9400b42be7cSGarance A Drosehn if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX) 9418beb1a2fSMarcel Moolenaar xo_warnx("No %s named '%s'", inf->lname, elem); 942a4c8a745SGarance A Drosehn else { 943a4c8a745SGarance A Drosehn /* The string is all digits, so it might be a userID. */ 9440b42be7cSGarance A Drosehn pwd = getpwuid((uid_t)bigtemp); 945a4c8a745SGarance A Drosehn if (pwd == NULL) 9468beb1a2fSMarcel Moolenaar xo_warnx("No %s name or ID matches '%s'", 947a4c8a745SGarance A Drosehn inf->lname, elem); 948cf22dcfcSBrian Somers } 949cf22dcfcSBrian Somers } 950a4c8a745SGarance A Drosehn if (pwd == NULL) { 951e3c4e1ddSGarance A Drosehn /* 952e3c4e1ddSGarance A Drosehn * These used to be treated as minor warnings (and the 953e3c4e1ddSGarance A Drosehn * option was simply ignored), but now they are fatal 954e3c4e1ddSGarance A Drosehn * errors (and the command will be aborted). 955e3c4e1ddSGarance A Drosehn */ 956e3c4e1ddSGarance A Drosehn optfatal = 1; 957c23b00b7SGarance A Drosehn return (0); 958cf22dcfcSBrian Somers } 959a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 960a4c8a745SGarance A Drosehn expand_list(inf); 961d822163fSGarance A Drosehn inf->l.uids[(inf->count)++] = pwd->pw_uid; 962a4c8a745SGarance A Drosehn return (1); 963a4c8a745SGarance A Drosehn } 964cf22dcfcSBrian Somers 965a4c8a745SGarance A Drosehn static void 966a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp) 967a4c8a745SGarance A Drosehn { 968a4c8a745SGarance A Drosehn const char *savep; 969a4c8a745SGarance A Drosehn char *cp, *endp; 970a4c8a745SGarance A Drosehn int toolong; 971ca62e195SGarance A Drosehn char elemcopy[PATH_MAX]; 972a4c8a745SGarance A Drosehn 97342856668SEd Maste if (*argp == '\0') 97442856668SEd Maste inf->addelem(inf, argp); 975a4c8a745SGarance A Drosehn while (*argp != '\0') { 976a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 977a4c8a745SGarance A Drosehn argp++; 978a4c8a745SGarance A Drosehn savep = argp; 979a4c8a745SGarance A Drosehn toolong = 0; 980a4c8a745SGarance A Drosehn cp = elemcopy; 981a4c8a745SGarance A Drosehn if (strchr(T_SEP, *argp) == NULL) { 982a4c8a745SGarance A Drosehn endp = elemcopy + sizeof(elemcopy) - 1; 983a4c8a745SGarance A Drosehn while (*argp != '\0' && cp <= endp && 984a4c8a745SGarance A Drosehn strchr(W_SEP T_SEP, *argp) == NULL) 985a4c8a745SGarance A Drosehn *cp++ = *argp++; 986a4c8a745SGarance A Drosehn if (cp > endp) 987a4c8a745SGarance A Drosehn toolong = 1; 988a4c8a745SGarance A Drosehn } 989a4c8a745SGarance A Drosehn if (!toolong) { 990a4c8a745SGarance A Drosehn *cp = '\0'; 991352b6523SGarance A Drosehn /* 992a9626fb3SGarance A Drosehn * Add this single element to the given list. 993352b6523SGarance A Drosehn */ 994a4c8a745SGarance A Drosehn inf->addelem(inf, elemcopy); 995a4c8a745SGarance A Drosehn } else { 996a4c8a745SGarance A Drosehn /* 997a4c8a745SGarance A Drosehn * The string is too long to copy. Find the end 998a4c8a745SGarance A Drosehn * of the string to print out the warning message. 999a4c8a745SGarance A Drosehn */ 1000a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP T_SEP, 1001a4c8a745SGarance A Drosehn *argp) == NULL) 1002a4c8a745SGarance A Drosehn argp++; 10038beb1a2fSMarcel Moolenaar xo_warnx("Value too long: %.*s", (int)(argp - savep), 1004a4c8a745SGarance A Drosehn savep); 1005a4c8a745SGarance A Drosehn optfatal = 1; 1006a4c8a745SGarance A Drosehn } 1007a4c8a745SGarance A Drosehn /* 1008a4c8a745SGarance A Drosehn * Skip over any number of trailing whitespace characters, 1009a4c8a745SGarance A Drosehn * but only one (at most) trailing element-terminating 1010a4c8a745SGarance A Drosehn * character. 1011a4c8a745SGarance A Drosehn */ 1012a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 1013a4c8a745SGarance A Drosehn argp++; 1014a4c8a745SGarance A Drosehn if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) { 1015a4c8a745SGarance A Drosehn argp++; 1016a4c8a745SGarance A Drosehn /* Catch case where string ended with a comma. */ 1017a4c8a745SGarance A Drosehn if (*argp == '\0') 1018a4c8a745SGarance A Drosehn inf->addelem(inf, argp); 1019a4c8a745SGarance A Drosehn } 1020a4c8a745SGarance A Drosehn } 1021a4c8a745SGarance A Drosehn } 1022a4c8a745SGarance A Drosehn 1023044fce53SBrian Somers static void 1024044fce53SBrian Somers descendant_sort(KINFO *ki, int items) 1025044fce53SBrian Somers { 1026044fce53SBrian Somers int dst, lvl, maxlvl, n, ndst, nsrc, siblings, src; 1027044fce53SBrian Somers unsigned char *path; 1028044fce53SBrian Somers KINFO kn; 1029044fce53SBrian Somers 1030044fce53SBrian Somers /* 1031044fce53SBrian Somers * First, sort the entries by descendancy, tracking the descendancy 1032044fce53SBrian Somers * depth in the ki_d.level field. 1033044fce53SBrian Somers */ 1034044fce53SBrian Somers src = 0; 1035044fce53SBrian Somers maxlvl = 0; 1036044fce53SBrian Somers while (src < items) { 1037044fce53SBrian Somers if (ki[src].ki_d.level) { 1038044fce53SBrian Somers src++; 1039044fce53SBrian Somers continue; 1040044fce53SBrian Somers } 1041044fce53SBrian Somers for (nsrc = 1; src + nsrc < items; nsrc++) 1042044fce53SBrian Somers if (!ki[src + nsrc].ki_d.level) 1043044fce53SBrian Somers break; 1044044fce53SBrian Somers 1045044fce53SBrian Somers for (dst = 0; dst < items; dst++) { 1046044fce53SBrian Somers if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_pid) 1047044fce53SBrian Somers continue; 1048044fce53SBrian Somers if (ki[dst].ki_p->ki_pid == ki[src].ki_p->ki_ppid) 1049044fce53SBrian Somers break; 1050044fce53SBrian Somers } 1051044fce53SBrian Somers 1052044fce53SBrian Somers if (dst == items) { 1053044fce53SBrian Somers src += nsrc; 1054044fce53SBrian Somers continue; 1055044fce53SBrian Somers } 1056044fce53SBrian Somers 1057044fce53SBrian Somers for (ndst = 1; dst + ndst < items; ndst++) 1058044fce53SBrian Somers if (ki[dst + ndst].ki_d.level <= ki[dst].ki_d.level) 1059044fce53SBrian Somers break; 1060044fce53SBrian Somers 1061044fce53SBrian Somers for (n = src; n < src + nsrc; n++) { 1062044fce53SBrian Somers ki[n].ki_d.level += ki[dst].ki_d.level + 1; 1063044fce53SBrian Somers if (maxlvl < ki[n].ki_d.level) 1064044fce53SBrian Somers maxlvl = ki[n].ki_d.level; 1065044fce53SBrian Somers } 1066044fce53SBrian Somers 1067044fce53SBrian Somers while (nsrc) { 1068044fce53SBrian Somers if (src < dst) { 1069044fce53SBrian Somers kn = ki[src]; 1070044fce53SBrian Somers memmove(ki + src, ki + src + 1, 1071044fce53SBrian Somers (dst - src + ndst - 1) * sizeof *ki); 1072044fce53SBrian Somers ki[dst + ndst - 1] = kn; 1073044fce53SBrian Somers nsrc--; 1074044fce53SBrian Somers dst--; 1075044fce53SBrian Somers ndst++; 1076044fce53SBrian Somers } else if (src != dst + ndst) { 1077044fce53SBrian Somers kn = ki[src]; 1078044fce53SBrian Somers memmove(ki + dst + ndst + 1, ki + dst + ndst, 1079044fce53SBrian Somers (src - dst - ndst) * sizeof *ki); 1080044fce53SBrian Somers ki[dst + ndst] = kn; 1081044fce53SBrian Somers ndst++; 1082044fce53SBrian Somers nsrc--; 1083044fce53SBrian Somers src++; 1084044fce53SBrian Somers } else { 1085044fce53SBrian Somers ndst += nsrc; 1086044fce53SBrian Somers src += nsrc; 1087044fce53SBrian Somers nsrc = 0; 1088044fce53SBrian Somers } 1089044fce53SBrian Somers } 1090044fce53SBrian Somers } 1091044fce53SBrian Somers 1092044fce53SBrian Somers /* 1093044fce53SBrian Somers * Now populate ki_d.prefix (instead of ki_d.level) with the command 1094044fce53SBrian Somers * prefix used to show descendancies. 1095044fce53SBrian Somers */ 1096044fce53SBrian Somers path = malloc((maxlvl + 7) / 8); 1097044fce53SBrian Somers memset(path, '\0', (maxlvl + 7) / 8); 1098044fce53SBrian Somers for (src = 0; src < items; src++) { 1099044fce53SBrian Somers if ((lvl = ki[src].ki_d.level) == 0) { 1100044fce53SBrian Somers ki[src].ki_d.prefix = NULL; 1101044fce53SBrian Somers continue; 1102044fce53SBrian Somers } 1103044fce53SBrian Somers if ((ki[src].ki_d.prefix = malloc(lvl * 2 + 1)) == NULL) 11048beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 1105044fce53SBrian Somers for (n = 0; n < lvl - 2; n++) { 1106044fce53SBrian Somers ki[src].ki_d.prefix[n * 2] = 1107044fce53SBrian Somers path[n / 8] & 1 << (n % 8) ? '|' : ' '; 1108044fce53SBrian Somers ki[src].ki_d.prefix[n * 2 + 1] = ' '; 1109044fce53SBrian Somers } 1110044fce53SBrian Somers if (n == lvl - 2) { 1111044fce53SBrian Somers /* Have I any more siblings? */ 1112044fce53SBrian Somers for (siblings = 0, dst = src + 1; dst < items; dst++) { 1113044fce53SBrian Somers if (ki[dst].ki_d.level > lvl) 1114044fce53SBrian Somers continue; 1115044fce53SBrian Somers if (ki[dst].ki_d.level == lvl) 1116044fce53SBrian Somers siblings = 1; 1117044fce53SBrian Somers break; 1118044fce53SBrian Somers } 1119044fce53SBrian Somers if (siblings) 1120044fce53SBrian Somers path[n / 8] |= 1 << (n % 8); 1121044fce53SBrian Somers else 1122044fce53SBrian Somers path[n / 8] &= ~(1 << (n % 8)); 1123044fce53SBrian Somers ki[src].ki_d.prefix[n * 2] = siblings ? '|' : '`'; 1124044fce53SBrian Somers ki[src].ki_d.prefix[n * 2 + 1] = '-'; 1125044fce53SBrian Somers n++; 1126044fce53SBrian Somers } 1127044fce53SBrian Somers strcpy(ki[src].ki_d.prefix + n * 2, "- "); 1128044fce53SBrian Somers } 1129044fce53SBrian Somers free(path); 1130044fce53SBrian Somers } 1131044fce53SBrian Somers 1132a4c8a745SGarance A Drosehn static void * 1133a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf) 1134a4c8a745SGarance A Drosehn { 1135a4c8a745SGarance A Drosehn void *newlist; 1136ca62e195SGarance A Drosehn int newmax; 1137a4c8a745SGarance A Drosehn 1138a4c8a745SGarance A Drosehn newmax = (inf->maxcount + 1) << 1; 1139d822163fSGarance A Drosehn newlist = realloc(inf->l.ptr, newmax * inf->elemsize); 1140a4c8a745SGarance A Drosehn if (newlist == NULL) { 1141d822163fSGarance A Drosehn free(inf->l.ptr); 11428beb1a2fSMarcel Moolenaar xo_errx(1, "realloc to %d %ss failed", newmax, inf->lname); 1143a4c8a745SGarance A Drosehn } 1144a4c8a745SGarance A Drosehn inf->maxcount = newmax; 1145d822163fSGarance A Drosehn inf->l.ptr = newlist; 1146a4c8a745SGarance A Drosehn 1147a4c8a745SGarance A Drosehn return (newlist); 1148a4c8a745SGarance A Drosehn } 1149a4c8a745SGarance A Drosehn 1150a4c8a745SGarance A Drosehn static void 1151a4c8a745SGarance A Drosehn free_list(struct listinfo *inf) 1152a4c8a745SGarance A Drosehn { 1153a4c8a745SGarance A Drosehn 1154a4c8a745SGarance A Drosehn inf->count = inf->elemsize = inf->maxcount = 0; 1155d822163fSGarance A Drosehn if (inf->l.ptr != NULL) 1156d822163fSGarance A Drosehn free(inf->l.ptr); 1157a4c8a745SGarance A Drosehn inf->addelem = NULL; 1158a4c8a745SGarance A Drosehn inf->lname = NULL; 1159d822163fSGarance A Drosehn inf->l.ptr = NULL; 1160a4c8a745SGarance A Drosehn } 1161a4c8a745SGarance A Drosehn 1162a4c8a745SGarance A Drosehn static void 1163a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize, 1164a4c8a745SGarance A Drosehn const char *lname) 1165a4c8a745SGarance A Drosehn { 1166a4c8a745SGarance A Drosehn 1167a4c8a745SGarance A Drosehn inf->count = inf->maxcount = 0; 1168a4c8a745SGarance A Drosehn inf->elemsize = elemsize; 1169a4c8a745SGarance A Drosehn inf->addelem = artn; 1170a4c8a745SGarance A Drosehn inf->lname = lname; 1171d822163fSGarance A Drosehn inf->l.ptr = NULL; 1172cf22dcfcSBrian Somers } 1173cf22dcfcSBrian Somers 1174fde411d5SJuli Mallett VARENT * 1175fde411d5SJuli Mallett find_varentry(VAR *v) 1176fde411d5SJuli Mallett { 1177fde411d5SJuli Mallett struct varent *vent; 1178fde411d5SJuli Mallett 1179bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 1180fde411d5SJuli Mallett if (strcmp(vent->var->name, v->name) == 0) 1181fde411d5SJuli Mallett return vent; 1182fde411d5SJuli Mallett } 1183fde411d5SJuli Mallett return NULL; 1184fde411d5SJuli Mallett } 1185fde411d5SJuli Mallett 11864b88c807SRodney W. Grimes static void 118746251ddeSWarner Losh scanvars(void) 11884b88c807SRodney W. Grimes { 11894b88c807SRodney W. Grimes struct varent *vent; 11904b88c807SRodney W. Grimes VAR *v; 11916a2d726bSJordan K. Hubbard 1192bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 11936a2d726bSJordan K. Hubbard v = vent->var; 11946a2d726bSJordan K. Hubbard if (v->flag & USER) 11956a2d726bSJordan K. Hubbard needuser = 1; 11966a2d726bSJordan K. Hubbard if (v->flag & COMM) 11976a2d726bSJordan K. Hubbard needcomm = 1; 11986a2d726bSJordan K. Hubbard } 11996a2d726bSJordan K. Hubbard } 12006a2d726bSJordan K. Hubbard 12016a2d726bSJordan K. Hubbard static void 12021d1143ecSEdward Tomasz Napierala format_output(KINFO *ki) 12036a2d726bSJordan K. Hubbard { 12046a2d726bSJordan K. Hubbard struct varent *vent; 12056a2d726bSJordan K. Hubbard VAR *v; 12061d1143ecSEdward Tomasz Napierala KINFO_STR *ks; 12071d1143ecSEdward Tomasz Napierala char *str; 12081d1143ecSEdward Tomasz Napierala int len; 12096a2d726bSJordan K. Hubbard 12101d1143ecSEdward Tomasz Napierala STAILQ_INIT(&ki->ki_ks); 1211bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 12126a2d726bSJordan K. Hubbard v = vent->var; 12131d1143ecSEdward Tomasz Napierala str = (v->oproc)(ki, vent); 12141d1143ecSEdward Tomasz Napierala ks = malloc(sizeof(*ks)); 12151d1143ecSEdward Tomasz Napierala if (ks == NULL) 12168beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 12171d1143ecSEdward Tomasz Napierala ks->ks_str = str; 12181d1143ecSEdward Tomasz Napierala STAILQ_INSERT_TAIL(&ki->ki_ks, ks, ks_next); 12191d1143ecSEdward Tomasz Napierala if (str != NULL) { 12201d1143ecSEdward Tomasz Napierala len = strlen(str); 12211d1143ecSEdward Tomasz Napierala } else 12221d1143ecSEdward Tomasz Napierala len = 1; /* "-" */ 12231d1143ecSEdward Tomasz Napierala if (v->width < len) 12241d1143ecSEdward Tomasz Napierala v->width = len; 12256a2d726bSJordan K. Hubbard } 12266a2d726bSJordan K. Hubbard } 12276a2d726bSJordan K. Hubbard 12286a2d726bSJordan K. Hubbard static void 122946251ddeSWarner Losh sizevars(void) 12306a2d726bSJordan K. Hubbard { 12316a2d726bSJordan K. Hubbard struct varent *vent; 12326a2d726bSJordan K. Hubbard VAR *v; 12334b88c807SRodney W. Grimes int i; 12344b88c807SRodney W. Grimes 1235bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 12364b88c807SRodney W. Grimes v = vent->var; 123778b1878aSJuli Mallett i = strlen(vent->header); 12384b88c807SRodney W. Grimes if (v->width < i) 12394b88c807SRodney W. Grimes v->width = i; 12404b88c807SRodney W. Grimes } 12414b88c807SRodney W. Grimes } 12424b88c807SRodney W. Grimes 1243871e8d8cSMark Murray static const char * 124446251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 12451c67ef12SJohn Baldwin char *comm, char *thread, int maxlen) 12464b88c807SRodney W. Grimes { 1247871e8d8cSMark Murray const char *s; 12484b88c807SRodney W. Grimes 12491c67ef12SJohn Baldwin s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, 12509f0b6e5eSJohn Baldwin showthreads && ki->ki_p->ki_numthreads > 1 ? thread : NULL, maxlen); 12514b88c807SRodney W. Grimes return (s); 12524b88c807SRodney W. Grimes } 12534b88c807SRodney W. Grimes 1254b61ce5b0SJeff Roberson #define UREADOK(ki) (forceuread || (ki->ki_p->ki_flag & P_INMEM)) 12553ac5e955SJohn Dyson 12564b88c807SRodney W. Grimes static void 125746251ddeSWarner Losh saveuser(KINFO *ki) 12584b88c807SRodney W. Grimes { 12595912ca59SDon Lewis char *argsp; 12604b88c807SRodney W. Grimes 1261b61ce5b0SJeff Roberson if (ki->ki_p->ki_flag & P_INMEM) { 12624b88c807SRodney W. Grimes /* 12634b88c807SRodney W. Grimes * The u-area might be swapped out, and we can't get 12644b88c807SRodney W. Grimes * at it because we have a crashdump and no swap. 12654b88c807SRodney W. Grimes * If it's here fill in these fields, otherwise, just 12664b88c807SRodney W. Grimes * leave them 0. 12674b88c807SRodney W. Grimes */ 12681f7d2501SKirk McKusick ki->ki_valid = 1; 12694b88c807SRodney W. Grimes } else 12701f7d2501SKirk McKusick ki->ki_valid = 0; 12714b88c807SRodney W. Grimes /* 12724b88c807SRodney W. Grimes * save arguments if needed 12734b88c807SRodney W. Grimes */ 1274dd693acfSGarance A Drosehn if (needcomm) { 1275dd693acfSGarance A Drosehn if (ki->ki_p->ki_stat == SZOMB) 1276dd693acfSGarance A Drosehn ki->ki_args = strdup("<defunct>"); 1277dd693acfSGarance A Drosehn else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) 12785912ca59SDon Lewis ki->ki_args = fmt(kvm_getargv, ki, 12795912ca59SDon Lewis ki->ki_p->ki_comm, ki->ki_p->ki_tdname, MAXCOMLEN); 12805912ca59SDon Lewis else { 12815912ca59SDon Lewis asprintf(&argsp, "(%s)", ki->ki_p->ki_comm); 12825912ca59SDon Lewis ki->ki_args = argsp; 12835912ca59SDon Lewis } 1284bd6233fdSGarance A Drosehn if (ki->ki_args == NULL) 12858beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 12863ac5e955SJohn Dyson } else { 12874b88c807SRodney W. Grimes ki->ki_args = NULL; 12883ac5e955SJohn Dyson } 1289dd693acfSGarance A Drosehn if (needenv) { 1290dd693acfSGarance A Drosehn if (UREADOK(ki)) 12915912ca59SDon Lewis ki->ki_env = fmt(kvm_getenvv, ki, 12925912ca59SDon Lewis (char *)NULL, (char *)NULL, 0); 1293dd693acfSGarance A Drosehn else 1294dd693acfSGarance A Drosehn ki->ki_env = strdup("()"); 1295dd693acfSGarance A Drosehn if (ki->ki_env == NULL) 12968beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 12973ac5e955SJohn Dyson } else { 12984b88c807SRodney W. Grimes ki->ki_env = NULL; 12994b88c807SRodney W. Grimes } 13003ac5e955SJohn Dyson } 13014b88c807SRodney W. Grimes 13024bac4483SGarance A Drosehn /* A macro used to improve the readability of pscomp(). */ 13034bac4483SGarance A Drosehn #define DIFF_RETURN(a, b, field) do { \ 13044bac4483SGarance A Drosehn if ((a)->field != (b)->field) \ 13054bac4483SGarance A Drosehn return (((a)->field < (b)->field) ? -1 : 1); \ 13064bac4483SGarance A Drosehn } while (0) 13074bac4483SGarance A Drosehn 13084b88c807SRodney W. Grimes static int 130946251ddeSWarner Losh pscomp(const void *a, const void *b) 13104b88c807SRodney W. Grimes { 13115bd7b1f3SGarance A Drosehn const KINFO *ka, *kb; 13124b88c807SRodney W. Grimes 13135bd7b1f3SGarance A Drosehn ka = a; 13145bd7b1f3SGarance A Drosehn kb = b; 13155bd7b1f3SGarance A Drosehn /* SORTCPU and SORTMEM are sorted in descending order. */ 13164bac4483SGarance A Drosehn if (sortby == SORTCPU) 13174bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_pcpu); 13184bac4483SGarance A Drosehn if (sortby == SORTMEM) 13194bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_memsize); 13205bd7b1f3SGarance A Drosehn /* 13215bd7b1f3SGarance A Drosehn * TTY's are sorted in ascending order, except that all NODEV 13225bd7b1f3SGarance A Drosehn * processes come before all processes with a device. 13235bd7b1f3SGarance A Drosehn */ 13244bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) { 13254bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev == NODEV) 13265bd7b1f3SGarance A Drosehn return (-1); 13274bac4483SGarance A Drosehn if (kb->ki_p->ki_tdev == NODEV) 13285bd7b1f3SGarance A Drosehn return (1); 13294bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tdev); 13304bac4483SGarance A Drosehn } 13314bac4483SGarance A Drosehn 1332b4b24324SGarance A Drosehn /* PID's and TID's (threads) are sorted in ascending order. */ 13334bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_pid); 1334b4b24324SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tid); 13355bd7b1f3SGarance A Drosehn return (0); 13364b88c807SRodney W. Grimes } 13374bac4483SGarance A Drosehn #undef DIFF_RETURN 13384b88c807SRodney W. Grimes 13394b88c807SRodney W. Grimes /* 13404b88c807SRodney W. Grimes * ICK (all for getopt), would rather hide the ugliness 13414b88c807SRodney W. Grimes * here than taint the main code. 13424b88c807SRodney W. Grimes * 13434b88c807SRodney W. Grimes * ps foo -> ps -foo 13444b88c807SRodney W. Grimes * ps 34 -> ps -p34 13454b88c807SRodney W. Grimes * 13464b88c807SRodney W. Grimes * The old convention that 't' with no trailing tty arg means the users 13474b88c807SRodney W. Grimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 13484b88c807SRodney W. Grimes * feature is available with the option 'T', which takes no argument. 13494b88c807SRodney W. Grimes */ 13504b88c807SRodney W. Grimes static char * 1351bf46a3bfSGarance A Drosehn kludge_oldps_options(const char *optlist, char *origval, const char *nextarg) 13524b88c807SRodney W. Grimes { 13534b88c807SRodney W. Grimes size_t len; 1354c675340aSGarance A Drosehn char *argp, *cp, *newopts, *ns, *optp, *pidp; 13554b88c807SRodney W. Grimes 1356daed3ad6SJuli Mallett /* 1357c675340aSGarance A Drosehn * See if the original value includes any option which takes an 1358c675340aSGarance A Drosehn * argument (and will thus use up the remainder of the string). 1359daed3ad6SJuli Mallett */ 1360c675340aSGarance A Drosehn argp = NULL; 1361c675340aSGarance A Drosehn if (optlist != NULL) { 1362c675340aSGarance A Drosehn for (cp = origval; *cp != '\0'; cp++) { 1363c675340aSGarance A Drosehn optp = strchr(optlist, *cp); 1364c675340aSGarance A Drosehn if ((optp != NULL) && *(optp + 1) == ':') { 1365c675340aSGarance A Drosehn argp = cp; 1366c675340aSGarance A Drosehn break; 1367c675340aSGarance A Drosehn } 1368c675340aSGarance A Drosehn } 1369c675340aSGarance A Drosehn } 1370c675340aSGarance A Drosehn if (argp != NULL && *origval == '-') 1371c675340aSGarance A Drosehn return (origval); 1372daed3ad6SJuli Mallett 13734b88c807SRodney W. Grimes /* 13744b88c807SRodney W. Grimes * if last letter is a 't' flag with no argument (in the context 13754b88c807SRodney W. Grimes * of the oldps options -- option string NOT starting with a '-' -- 13764b88c807SRodney W. Grimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 1377380434d4SBrian Somers * 13782631c777SGarance A Drosehn * However, if a flag accepting a string argument is found earlier 13792631c777SGarance A Drosehn * in the option string (including a possible `t' flag), then the 13802631c777SGarance A Drosehn * remainder of the string must be the argument to that flag; so 1381c675340aSGarance A Drosehn * do not modify that argument. Note that a trailing `t' would 1382c675340aSGarance A Drosehn * cause argp to be set, if argp was not already set by some 1383c675340aSGarance A Drosehn * earlier option. 13844b88c807SRodney W. Grimes */ 1385c675340aSGarance A Drosehn len = strlen(origval); 1386c675340aSGarance A Drosehn cp = origval + len - 1; 1387c675340aSGarance A Drosehn pidp = NULL; 1388bf46a3bfSGarance A Drosehn if (*cp == 't' && *origval != '-' && cp == argp) { 1389bf46a3bfSGarance A Drosehn if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg)) 13904b88c807SRodney W. Grimes *cp = 'T'; 1391bf46a3bfSGarance A Drosehn } else if (argp == NULL) { 1392c675340aSGarance A Drosehn /* 1393c675340aSGarance A Drosehn * The original value did not include any option which takes 1394c675340aSGarance A Drosehn * an argument (and that would include `p' and `t'), so check 1395c675340aSGarance A Drosehn * the value for trailing number, or comma-separated list of 1396c675340aSGarance A Drosehn * numbers, which we will treat as a pid request. 1397c675340aSGarance A Drosehn */ 1398c675340aSGarance A Drosehn if (isdigitch(*cp)) { 1399c675340aSGarance A Drosehn while (cp >= origval && (*cp == ',' || isdigitch(*cp))) 1400c675340aSGarance A Drosehn --cp; 1401c675340aSGarance A Drosehn pidp = cp + 1; 1402c675340aSGarance A Drosehn } 1403c675340aSGarance A Drosehn } 1404c675340aSGarance A Drosehn 1405c675340aSGarance A Drosehn /* 1406c675340aSGarance A Drosehn * If nothing needs to be added to the string, then return 1407c675340aSGarance A Drosehn * the "original" (although possibly modified) value. 1408c675340aSGarance A Drosehn */ 1409c675340aSGarance A Drosehn if (*origval == '-' && pidp == NULL) 1410c675340aSGarance A Drosehn return (origval); 1411c675340aSGarance A Drosehn 1412c675340aSGarance A Drosehn /* 1413c675340aSGarance A Drosehn * Create a copy of the string to add '-' and/or 'p' to the 1414c675340aSGarance A Drosehn * original value. 1415c675340aSGarance A Drosehn */ 1416c675340aSGarance A Drosehn if ((newopts = ns = malloc(len + 3)) == NULL) 14178beb1a2fSMarcel Moolenaar xo_errx(1, "malloc failed"); 1418c675340aSGarance A Drosehn 1419c675340aSGarance A Drosehn if (*origval != '-') 1420c675340aSGarance A Drosehn *ns++ = '-'; /* add option flag */ 1421c675340aSGarance A Drosehn 1422c675340aSGarance A Drosehn if (pidp == NULL) 1423c675340aSGarance A Drosehn strcpy(ns, origval); 14244b88c807SRodney W. Grimes else { 14254b88c807SRodney W. Grimes /* 1426c675340aSGarance A Drosehn * Copy everything before the pid string, add the `p', 1427c675340aSGarance A Drosehn * and then copy the pid string. 14284b88c807SRodney W. Grimes */ 1429c675340aSGarance A Drosehn len = pidp - origval; 1430c675340aSGarance A Drosehn memcpy(ns, origval, len); 1431c675340aSGarance A Drosehn ns += len; 14324b88c807SRodney W. Grimes *ns++ = 'p'; 1433c675340aSGarance A Drosehn strcpy(ns, pidp); 1434c675340aSGarance A Drosehn } 14354b88c807SRodney W. Grimes 14364b88c807SRodney W. Grimes return (newopts); 14374b88c807SRodney W. Grimes } 14384b88c807SRodney W. Grimes 14394b88c807SRodney W. Grimes static void 144064b0bf0bSPawel Jakub Dawidek pidmax_init(void) 144164b0bf0bSPawel Jakub Dawidek { 144264b0bf0bSPawel Jakub Dawidek size_t intsize; 144364b0bf0bSPawel Jakub Dawidek 144464b0bf0bSPawel Jakub Dawidek intsize = sizeof(pid_max); 144564b0bf0bSPawel Jakub Dawidek if (sysctlbyname("kern.pid_max", &pid_max, &intsize, NULL, 0) < 0) { 14468beb1a2fSMarcel Moolenaar xo_warn("unable to read kern.pid_max"); 144764b0bf0bSPawel Jakub Dawidek pid_max = 99999; 144864b0bf0bSPawel Jakub Dawidek } 144964b0bf0bSPawel Jakub Dawidek } 145064b0bf0bSPawel Jakub Dawidek 1451*c6a5ff71SEitan Adler static void __dead2 145246251ddeSWarner Losh usage(void) 14534b88c807SRodney W. Grimes { 1454aa14b5abSBrian Somers #define SINGLE_OPTS "[-aCcde" OPT_LAZY_f "HhjlmrSTuvwXxZ]" 14554b88c807SRodney W. Grimes 14568beb1a2fSMarcel Moolenaar (void)xo_error("%s\n%s\n%s\n%s\n", 1457a89237aeSRuslan Ermilov "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]", 145813767130SBryan Drewery " [-J jid[,jid...]] [-M core] [-N system]", 1459a89237aeSRuslan Ermilov " [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]", 14604b88c807SRodney W. Grimes " ps [-L]"); 14614b88c807SRodney W. Grimes exit(1); 14624b88c807SRodney W. Grimes } 1463