14b88c807SRodney W. Grimes /*- 24b88c807SRodney W. Grimes * Copyright (c) 1990, 1993, 1994 34b88c807SRodney W. Grimes * The Regents of the University of California. All rights reserved. 44b88c807SRodney W. Grimes * 54b88c807SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 64b88c807SRodney W. Grimes * modification, are permitted provided that the following conditions 74b88c807SRodney W. Grimes * are met: 84b88c807SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 94b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 104b88c807SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 114b88c807SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 124b88c807SRodney W. Grimes * documentation and/or other materials provided with the distribution. 134b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 144b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 154b88c807SRodney W. Grimes * without specific prior written permission. 164b88c807SRodney W. Grimes * 174b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 184b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 194b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 204b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 214b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 224b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 234b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 244b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 254b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 264b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 274b88c807SRodney W. Grimes * SUCH DAMAGE. 28a4c8a745SGarance A Drosehn * ------+---------+---------+-------- + --------+---------+---------+---------* 29a4c8a745SGarance A Drosehn * Copyright (c) 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 30a4c8a745SGarance A Drosehn * All rights reserved. 31a4c8a745SGarance A Drosehn * 32a4c8a745SGarance A Drosehn * Significant modifications made to bring `ps' options somewhat closer 33a4c8a745SGarance A Drosehn * to the standard for `ps' as described in SingleUnixSpec-v3. 34a4c8a745SGarance A Drosehn * ------+---------+---------+-------- + --------+---------+---------+---------* 354b88c807SRodney W. Grimes */ 364b88c807SRodney W. Grimes 374b88c807SRodney W. Grimes #ifndef lint 38871e8d8cSMark Murray static const char copyright[] = 394b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 404b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 414b88c807SRodney W. Grimes #endif /* not lint */ 424b88c807SRodney W. Grimes 43c9a8d1f4SPhilippe Charnier #if 0 44871e8d8cSMark Murray #ifndef lint 45c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 464b88c807SRodney W. Grimes #endif /* not lint */ 47871e8d8cSMark Murray #endif 48eaed5652SPhilippe Charnier 492749b141SDavid E. O'Brien #include <sys/cdefs.h> 502749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 514b88c807SRodney W. Grimes 524b88c807SRodney W. Grimes #include <sys/param.h> 53c7910c3aSGarance A Drosehn #include <sys/proc.h> 548b6f5f5fSDavid Greenman #include <sys/user.h> 554b88c807SRodney W. Grimes #include <sys/stat.h> 564b88c807SRodney W. Grimes #include <sys/ioctl.h> 574b88c807SRodney W. Grimes #include <sys/sysctl.h> 58a951d1f8SChristian S.J. Peron #include <sys/mount.h> 594b88c807SRodney W. Grimes 604b88c807SRodney W. Grimes #include <ctype.h> 614b88c807SRodney W. Grimes #include <err.h> 624e8b6a6fSGarance A Drosehn #include <errno.h> 634b88c807SRodney W. Grimes #include <fcntl.h> 64a4c8a745SGarance A Drosehn #include <grp.h> 654b88c807SRodney W. Grimes #include <kvm.h> 66b2496d93SMike Pritchard #include <limits.h> 6708017519SAndrey A. Chernov #include <locale.h> 684b88c807SRodney W. Grimes #include <paths.h> 69871e8d8cSMark Murray #include <pwd.h> 704b88c807SRodney W. Grimes #include <stdio.h> 714b88c807SRodney W. Grimes #include <stdlib.h> 724b88c807SRodney W. Grimes #include <string.h> 734b88c807SRodney W. Grimes #include <unistd.h> 744b88c807SRodney W. Grimes 754b88c807SRodney W. Grimes #include "ps.h" 764b88c807SRodney W. Grimes 77a4c8a745SGarance A Drosehn #define W_SEP " \t" /* "Whitespace" list separators */ 78a4c8a745SGarance A Drosehn #define T_SEP "," /* "Terminate-element" list separators */ 79cf22dcfcSBrian Somers 80de800cd4SGarance A Drosehn #ifdef LAZY_PS 81c46bb4b3SGarance A Drosehn #define DEF_UREAD 0 82de800cd4SGarance A Drosehn #define OPT_LAZY_f "f" 83de800cd4SGarance A Drosehn #else 84c46bb4b3SGarance A Drosehn #define DEF_UREAD 1 /* Always do the more-expensive read. */ 85de800cd4SGarance A Drosehn #define OPT_LAZY_f /* I.e., the `-f' option is not added. */ 86de800cd4SGarance A Drosehn #endif 874b88c807SRodney W. Grimes 88c675340aSGarance A Drosehn /* 89ecbb06f8SGarance A Drosehn * isdigit takes an `int', but expects values in the range of unsigned char. 90ecbb06f8SGarance A Drosehn * This wrapper ensures that values from a 'char' end up in the correct range. 91c675340aSGarance A Drosehn */ 92ecbb06f8SGarance A Drosehn #define isdigitch(Anychar) isdigit((u_char)(Anychar)) 93c675340aSGarance A Drosehn 94db91faacSPeter Wemm int cflag; /* -c */ 95de800cd4SGarance A Drosehn int eval; /* Exit value */ 96de800cd4SGarance A Drosehn time_t now; /* Current time(3) value */ 974b88c807SRodney W. Grimes int rawcpu; /* -C */ 984b88c807SRodney W. Grimes int sumrusage; /* -S */ 99de800cd4SGarance A Drosehn int termwidth; /* Width of the screen (0 == infinity). */ 100de800cd4SGarance A Drosehn int totwidth; /* Calculated-width of requested variables. */ 1017ab24ea3SJulian Elischer int showthreads; /* will threads be shown? */ 1024b88c807SRodney W. Grimes 103bdf8ab46SGarance A Drosehn struct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); 104de800cd4SGarance A Drosehn 105de800cd4SGarance A Drosehn static int forceuread = DEF_UREAD; /* Do extra work to get u-area. */ 106de800cd4SGarance A Drosehn static kvm_t *kd; 107de800cd4SGarance A Drosehn static KINFO *kinfo; 108de800cd4SGarance A Drosehn static int needcomm; /* -o "command" */ 109de800cd4SGarance A Drosehn static int needenv; /* -e */ 110de800cd4SGarance A Drosehn static int needuser; /* -o "user" */ 111de800cd4SGarance A Drosehn static int optfatal; /* Fatal error parsing some list-option. */ 112de800cd4SGarance A Drosehn 113de800cd4SGarance A Drosehn static enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 114ba2cd770SJuli Mallett 115a4c8a745SGarance A Drosehn struct listinfo; 116de800cd4SGarance A Drosehn typedef int addelem_rtn(struct listinfo *_inf, const char *_elem); 117a4c8a745SGarance A Drosehn 118a4c8a745SGarance A Drosehn struct listinfo { 119a4c8a745SGarance A Drosehn int count; 120a4c8a745SGarance A Drosehn int maxcount; 121a4c8a745SGarance A Drosehn int elemsize; 122a4c8a745SGarance A Drosehn addelem_rtn *addelem; 123a4c8a745SGarance A Drosehn const char *lname; 124a4c8a745SGarance A Drosehn union { 125a4c8a745SGarance A Drosehn gid_t *gids; 126a4c8a745SGarance A Drosehn pid_t *pids; 127a4c8a745SGarance A Drosehn dev_t *ttys; 128a4c8a745SGarance A Drosehn uid_t *uids; 129a4c8a745SGarance A Drosehn void *ptr; 130d822163fSGarance A Drosehn } l; 131a4c8a745SGarance A Drosehn }; 132a4c8a745SGarance A Drosehn 133a951d1f8SChristian S.J. Peron static int check_procfs(void); 134a4c8a745SGarance A Drosehn static int addelem_gid(struct listinfo *, const char *); 135a4c8a745SGarance A Drosehn static int addelem_pid(struct listinfo *, const char *); 136a4c8a745SGarance A Drosehn static int addelem_tty(struct listinfo *, const char *); 137a4c8a745SGarance A Drosehn static int addelem_uid(struct listinfo *, const char *); 138a4c8a745SGarance A Drosehn static void add_list(struct listinfo *, const char *); 1394857f240SGarance A Drosehn static void dynsizevars(KINFO *); 140a4c8a745SGarance A Drosehn static void *expand_list(struct listinfo *); 141f35e0715SGarance A Drosehn static const char * 142f35e0715SGarance A Drosehn fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 1434857f240SGarance A Drosehn KINFO *, char *, int); 144a4c8a745SGarance A Drosehn static void free_list(struct listinfo *); 145a4c8a745SGarance A Drosehn static void init_list(struct listinfo *, addelem_rtn, int, const char *); 14625113083SGarance A Drosehn static char *kludge_oldps_options(const char *, char *, const char *); 1474857f240SGarance A Drosehn static int pscomp(const void *, const void *); 1484857f240SGarance A Drosehn static void saveuser(KINFO *); 1494857f240SGarance A Drosehn static void scanvars(void); 1504857f240SGarance A Drosehn static void sizevars(void); 1514857f240SGarance A Drosehn static void usage(void); 1524b88c807SRodney W. Grimes 153c0716492SJuli Mallett static char dfmt[] = "pid,tt,state,time,command"; 154259fcfacSGarance A Drosehn static char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command"; 1551d2324f4SGarance A Drosehn static char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state," 1561d2324f4SGarance A Drosehn "tt,time,command"; 157871e8d8cSMark Murray static char o1[] = "pid"; 158c0716492SJuli Mallett static char o2[] = "tt,state,time,command"; 159c0716492SJuli Mallett static char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command"; 1601d2324f4SGarance A Drosehn static char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz," 1611d2324f4SGarance A Drosehn "%cpu,%mem,command"; 1622af538ebSRobert Watson static char Zfmt[] = "label"; 1634b88c807SRodney W. Grimes 1649bf69950SGarance A Drosehn #define PS_ARGS "AaCce" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ" 16541623b2dSMaxim Sobolev 1664b88c807SRodney W. Grimes int 16746251ddeSWarner Losh main(int argc, char *argv[]) 1684b88c807SRodney W. Grimes { 169a4c8a745SGarance A Drosehn struct listinfo gidlist, pgrplist, pidlist; 170a4c8a745SGarance A Drosehn struct listinfo ruidlist, sesslist, ttylist, uidlist; 1714b88c807SRodney W. Grimes struct kinfo_proc *kp; 1724bac4483SGarance A Drosehn KINFO *next_KINFO; 1734b88c807SRodney W. Grimes struct varent *vent; 1744b88c807SRodney W. Grimes struct winsize ws; 175c675340aSGarance A Drosehn const char *nlistf, *memf; 176ca62e195SGarance A Drosehn char *cols; 177832b0b55SRuslan Ermilov int all, ch, elem, flag, _fmt, i, lineno; 178c675340aSGarance A Drosehn int nentries, nkept, nselectors; 1797ab24ea3SJulian Elischer int prtheader, wflag, what, xkeep, xkeep_implied; 180871e8d8cSMark Murray char errbuf[_POSIX2_LINE_MAX]; 1814b88c807SRodney W. Grimes 1822bf4b9cfSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 183352b6523SGarance A Drosehn time(&now); /* Used by routines in print.c. */ 1842bf4b9cfSAndrey A. Chernov 1854f18100dSTim J. Robbins if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') 1864f18100dSTim J. Robbins termwidth = atoi(cols); 1874f18100dSTim J. Robbins else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1884b88c807SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1894b88c807SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1904b88c807SRodney W. Grimes ws.ws_col == 0) 1914b88c807SRodney W. Grimes termwidth = 79; 1924b88c807SRodney W. Grimes else 1934b88c807SRodney W. Grimes termwidth = ws.ws_col - 1; 1944b88c807SRodney W. Grimes 19541623b2dSMaxim Sobolev /* 196c675340aSGarance A Drosehn * Hide a number of option-processing kludges in a separate routine, 197c675340aSGarance A Drosehn * to support some historical BSD behaviors, such as `ps axu'. 19841623b2dSMaxim Sobolev */ 199c675340aSGarance A Drosehn if (argc > 1) 200bf46a3bfSGarance A Drosehn argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]); 2014b88c807SRodney W. Grimes 202832b0b55SRuslan Ermilov all = _fmt = nselectors = optfatal = 0; 203352b6523SGarance A Drosehn prtheader = showthreads = wflag = xkeep_implied = 0; 204352b6523SGarance A Drosehn xkeep = -1; /* Neither -x nor -X. */ 205a4c8a745SGarance A Drosehn init_list(&gidlist, addelem_gid, sizeof(gid_t), "group"); 206a4c8a745SGarance A Drosehn init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group"); 207a4c8a745SGarance A Drosehn init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id"); 208a4c8a745SGarance A Drosehn init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser"); 209a4c8a745SGarance A Drosehn init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id"); 210a4c8a745SGarance A Drosehn init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty"); 211a4c8a745SGarance A Drosehn init_list(&uidlist, addelem_uid, sizeof(uid_t), "user"); 212831c910aSRuslan Ermilov memf = nlistf = _PATH_DEVNULL; 21341623b2dSMaxim Sobolev while ((ch = getopt(argc, argv, PS_ARGS)) != -1) 2144b88c807SRodney W. Grimes switch ((char)ch) { 215a4c8a745SGarance A Drosehn case 'A': 216a4c8a745SGarance A Drosehn /* 217a4c8a745SGarance A Drosehn * Exactly the same as `-ax'. This has been 218a4c8a745SGarance A Drosehn * added for compatability with SUSv3, but for 219a4c8a745SGarance A Drosehn * now it will not be described in the man page. 220a4c8a745SGarance A Drosehn */ 221a4c8a745SGarance A Drosehn nselectors++; 222a4c8a745SGarance A Drosehn all = xkeep = 1; 223a4c8a745SGarance A Drosehn break; 2244b88c807SRodney W. Grimes case 'a': 225a4c8a745SGarance A Drosehn nselectors++; 2264b88c807SRodney W. Grimes all = 1; 2274b88c807SRodney W. Grimes break; 2284b88c807SRodney W. Grimes case 'C': 2294b88c807SRodney W. Grimes rawcpu = 1; 2304b88c807SRodney W. Grimes break; 231db91faacSPeter Wemm case 'c': 232db91faacSPeter Wemm cflag = 1; 233db91faacSPeter Wemm break; 234db91faacSPeter Wemm case 'e': /* XXX set ufmt */ 235db91faacSPeter Wemm needenv = 1; 236db91faacSPeter Wemm break; 2374a355d17SGarance A Drosehn #ifdef LAZY_PS 2384a355d17SGarance A Drosehn case 'f': 2394a355d17SGarance A Drosehn if (getuid() == 0 || getgid() == 0) 2404a355d17SGarance A Drosehn forceuread = 1; 2414a355d17SGarance A Drosehn break; 2424a355d17SGarance A Drosehn #endif 243a4c8a745SGarance A Drosehn case 'G': 244a4c8a745SGarance A Drosehn add_list(&gidlist, optarg); 245a4c8a745SGarance A Drosehn xkeep_implied = 1; 246a4c8a745SGarance A Drosehn nselectors++; 247a4c8a745SGarance A Drosehn break; 2484b88c807SRodney W. Grimes case 'g': 249352b6523SGarance A Drosehn #if 0 250ba50b0e0SGarance A Drosehn /*- 251352b6523SGarance A Drosehn * XXX - This SUSv3 behavior is still under debate 252352b6523SGarance A Drosehn * since it conflicts with the (undocumented) 253352b6523SGarance A Drosehn * `-g' option. So we skip it for now. 254352b6523SGarance A Drosehn */ 255a4c8a745SGarance A Drosehn add_list(&pgrplist, optarg); 256a4c8a745SGarance A Drosehn xkeep_implied = 1; 257a4c8a745SGarance A Drosehn nselectors++; 258a4c8a745SGarance A Drosehn break; 259a4c8a745SGarance A Drosehn #else 260352b6523SGarance A Drosehn /* The historical BSD-ish (from SunOS) behavior. */ 2614b88c807SRodney W. Grimes break; /* no-op */ 262a4c8a745SGarance A Drosehn #endif 26348b8c0deSScott Long case 'H': 264d75c1d83SDaniel Eischen showthreads = KERN_PROC_INC_THREAD; 26548b8c0deSScott Long break; 2664b88c807SRodney W. Grimes case 'h': 2674b88c807SRodney W. Grimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 2684b88c807SRodney W. Grimes break; 2694b88c807SRodney W. Grimes case 'j': 270fde411d5SJuli Mallett parsefmt(jfmt, 0); 271871e8d8cSMark Murray _fmt = 1; 2724b88c807SRodney W. Grimes jfmt[0] = '\0'; 2734b88c807SRodney W. Grimes break; 2744b88c807SRodney W. Grimes case 'L': 2754b88c807SRodney W. Grimes showkey(); 2764b88c807SRodney W. Grimes exit(0); 2774b88c807SRodney W. Grimes case 'l': 278fde411d5SJuli Mallett parsefmt(lfmt, 0); 279871e8d8cSMark Murray _fmt = 1; 2804b88c807SRodney W. Grimes lfmt[0] = '\0'; 2814b88c807SRodney W. Grimes break; 2824b88c807SRodney W. Grimes case 'M': 2834b88c807SRodney W. Grimes memf = optarg; 2844b88c807SRodney W. Grimes break; 2854b88c807SRodney W. Grimes case 'm': 2864b88c807SRodney W. Grimes sortby = SORTMEM; 2874b88c807SRodney W. Grimes break; 2884b88c807SRodney W. Grimes case 'N': 2894b88c807SRodney W. Grimes nlistf = optarg; 2904b88c807SRodney W. Grimes break; 2914b88c807SRodney W. Grimes case 'O': 292fde411d5SJuli Mallett parsefmt(o1, 1); 293fde411d5SJuli Mallett parsefmt(optarg, 1); 294fde411d5SJuli Mallett parsefmt(o2, 1); 2954b88c807SRodney W. Grimes o1[0] = o2[0] = '\0'; 296871e8d8cSMark Murray _fmt = 1; 2974b88c807SRodney W. Grimes break; 2984b88c807SRodney W. Grimes case 'o': 299fde411d5SJuli Mallett parsefmt(optarg, 1); 300871e8d8cSMark Murray _fmt = 1; 3014b88c807SRodney W. Grimes break; 3024b88c807SRodney W. Grimes case 'p': 303a4c8a745SGarance A Drosehn add_list(&pidlist, optarg); 304a4c8a745SGarance A Drosehn /* 305a4c8a745SGarance A Drosehn * Note: `-p' does not *set* xkeep, but any values 306a4c8a745SGarance A Drosehn * from pidlist are checked before xkeep is. That 307a4c8a745SGarance A Drosehn * way they are always matched, even if the user 308a4c8a745SGarance A Drosehn * specifies `-X'. 309a4c8a745SGarance A Drosehn */ 310a4c8a745SGarance A Drosehn nselectors++; 3114b88c807SRodney W. Grimes break; 312a4c8a745SGarance A Drosehn #if 0 313a4c8a745SGarance A Drosehn case 'R': 314ba50b0e0SGarance A Drosehn /*- 315352b6523SGarance A Drosehn * XXX - This un-standard option is still under 316352b6523SGarance A Drosehn * debate. This is what SUSv3 defines as 317352b6523SGarance A Drosehn * the `-U' option, and while it would be 318352b6523SGarance A Drosehn * nice to have, it could cause even more 319352b6523SGarance A Drosehn * confusion to implement it as `-R'. 320352b6523SGarance A Drosehn */ 321a4c8a745SGarance A Drosehn add_list(&ruidlist, optarg); 322a4c8a745SGarance A Drosehn xkeep_implied = 1; 323a4c8a745SGarance A Drosehn nselectors++; 324a4c8a745SGarance A Drosehn break; 325a4c8a745SGarance A Drosehn #endif 3264b88c807SRodney W. Grimes case 'r': 3274b88c807SRodney W. Grimes sortby = SORTCPU; 3284b88c807SRodney W. Grimes break; 3294b88c807SRodney W. Grimes case 'S': 3304b88c807SRodney W. Grimes sumrusage = 1; 3314b88c807SRodney W. Grimes break; 332a4c8a745SGarance A Drosehn #if 0 333a4c8a745SGarance A Drosehn case 's': 334ba50b0e0SGarance A Drosehn /*- 335352b6523SGarance A Drosehn * XXX - This non-standard option is still under 336352b6523SGarance A Drosehn * debate. This *is* supported on Solaris, 337352b6523SGarance A Drosehn * Linux, and IRIX, but conflicts with `-s' 338352b6523SGarance A Drosehn * on NetBSD and maybe some older BSD's. 339352b6523SGarance A Drosehn */ 340a4c8a745SGarance A Drosehn add_list(&sesslist, optarg); 341a4c8a745SGarance A Drosehn xkeep_implied = 1; 342a4c8a745SGarance A Drosehn nselectors++; 343a4c8a745SGarance A Drosehn break; 344a4c8a745SGarance A Drosehn #endif 3454b88c807SRodney W. Grimes case 'T': 3464b88c807SRodney W. Grimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 3474b88c807SRodney W. Grimes errx(1, "stdin: not a terminal"); 3484b88c807SRodney W. Grimes /* FALLTHROUGH */ 349a4c8a745SGarance A Drosehn case 't': 350a4c8a745SGarance A Drosehn add_list(&ttylist, optarg); 351a4c8a745SGarance A Drosehn xkeep_implied = 1; 352a4c8a745SGarance A Drosehn nselectors++; 3534b88c807SRodney W. Grimes break; 35473eb8310SPeter Wemm case 'U': 355a4c8a745SGarance A Drosehn /* This is what SUSv3 defines as the `-u' option. */ 356a4c8a745SGarance A Drosehn add_list(&uidlist, optarg); 357a4c8a745SGarance A Drosehn xkeep_implied = 1; 358a4c8a745SGarance A Drosehn nselectors++; 35973eb8310SPeter Wemm break; 3604b88c807SRodney W. Grimes case 'u': 361fde411d5SJuli Mallett parsefmt(ufmt, 0); 3624b88c807SRodney W. Grimes sortby = SORTCPU; 363871e8d8cSMark Murray _fmt = 1; 3644b88c807SRodney W. Grimes ufmt[0] = '\0'; 3654b88c807SRodney W. Grimes break; 3664b88c807SRodney W. Grimes case 'v': 367fde411d5SJuli Mallett parsefmt(vfmt, 0); 3684b88c807SRodney W. Grimes sortby = SORTMEM; 369871e8d8cSMark Murray _fmt = 1; 3704b88c807SRodney W. Grimes vfmt[0] = '\0'; 3714b88c807SRodney W. Grimes break; 3724b88c807SRodney W. Grimes case 'w': 3734b88c807SRodney W. Grimes if (wflag) 3744b88c807SRodney W. Grimes termwidth = UNLIMITED; 3754b88c807SRodney W. Grimes else if (termwidth < 131) 3764b88c807SRodney W. Grimes termwidth = 131; 3774b88c807SRodney W. Grimes wflag++; 3784b88c807SRodney W. Grimes break; 379a4c8a745SGarance A Drosehn case 'X': 380a4c8a745SGarance A Drosehn /* 381a4c8a745SGarance A Drosehn * Note that `-X' and `-x' are not standard "selector" 382a4c8a745SGarance A Drosehn * options. For most selector-options, we check *all* 383a4c8a745SGarance A Drosehn * processes to see if any are matched by the given 384a4c8a745SGarance A Drosehn * value(s). After we have a set of all the matched 385a4c8a745SGarance A Drosehn * processes, then `-X' and `-x' govern whether we 386a4c8a745SGarance A Drosehn * modify that *matched* set for processes which do 387a4c8a745SGarance A Drosehn * not have a controlling terminal. `-X' causes 388a4c8a745SGarance A Drosehn * those processes to be deleted from the matched 389a4c8a745SGarance A Drosehn * set, while `-x' causes them to be kept. 390a4c8a745SGarance A Drosehn */ 391a4c8a745SGarance A Drosehn xkeep = 0; 392a4c8a745SGarance A Drosehn break; 3934b88c807SRodney W. Grimes case 'x': 394a4c8a745SGarance A Drosehn xkeep = 1; 3954b88c807SRodney W. Grimes break; 3967304f61fSBrian Feldman case 'Z': 397fde411d5SJuli Mallett parsefmt(Zfmt, 0); 3987304f61fSBrian Feldman Zfmt[0] = '\0'; 3997304f61fSBrian Feldman break; 4004b88c807SRodney W. Grimes case '?': 4014b88c807SRodney W. Grimes default: 4024b88c807SRodney W. Grimes usage(); 4034b88c807SRodney W. Grimes } 4044b88c807SRodney W. Grimes argc -= optind; 4054b88c807SRodney W. Grimes argv += optind; 406c675340aSGarance A Drosehn 407c675340aSGarance A Drosehn /* 408a951d1f8SChristian S.J. Peron * If the user specified ps -e then they want a copy of the process 409a951d1f8SChristian S.J. Peron * environment kvm_getenvv(3) attempts to open /proc/<pid>/mem. 410a951d1f8SChristian S.J. Peron * Check to make sure that procfs is mounted on /proc, otherwise 411a951d1f8SChristian S.J. Peron * print a warning informing the user that output will be incomplete. 412a951d1f8SChristian S.J. Peron */ 413a951d1f8SChristian S.J. Peron if (needenv == 1 && check_procfs() == 0) 414a951d1f8SChristian S.J. Peron warnx("Process environment requires procfs(5)"); 415a951d1f8SChristian S.J. Peron /* 416c675340aSGarance A Drosehn * If there arguments after processing all the options, attempt 417c675340aSGarance A Drosehn * to treat them as a list of process ids. 418c675340aSGarance A Drosehn */ 419c675340aSGarance A Drosehn while (*argv) { 420c675340aSGarance A Drosehn if (!isdigitch(**argv)) 421c675340aSGarance A Drosehn break; 422c675340aSGarance A Drosehn add_list(&pidlist, *argv); 423c675340aSGarance A Drosehn argv++; 424c675340aSGarance A Drosehn } 425c675340aSGarance A Drosehn if (*argv) { 426c675340aSGarance A Drosehn fprintf(stderr, "%s: illegal argument: %s\n", 427c675340aSGarance A Drosehn getprogname(), *argv); 428c675340aSGarance A Drosehn usage(); 429c675340aSGarance A Drosehn } 430a4c8a745SGarance A Drosehn if (optfatal) 431352b6523SGarance A Drosehn exit(1); /* Error messages already printed. */ 432352b6523SGarance A Drosehn if (xkeep < 0) /* Neither -X nor -x was specified. */ 433a4c8a745SGarance A Drosehn xkeep = xkeep_implied; 434a4c8a745SGarance A Drosehn 435831c910aSRuslan Ermilov kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 4364b88c807SRodney W. Grimes if (kd == 0) 4374b88c807SRodney W. Grimes errx(1, "%s", errbuf); 4384b88c807SRodney W. Grimes 439871e8d8cSMark Murray if (!_fmt) 440fde411d5SJuli Mallett parsefmt(dfmt, 0); 4414b88c807SRodney W. Grimes 442a4c8a745SGarance A Drosehn if (nselectors == 0) { 443d822163fSGarance A Drosehn uidlist.l.ptr = malloc(sizeof(uid_t)); 444d822163fSGarance A Drosehn if (uidlist.l.ptr == NULL) 4454fa7d788SJuli Mallett errx(1, "malloc failed"); 446a4c8a745SGarance A Drosehn nselectors = 1; 447a4c8a745SGarance A Drosehn uidlist.count = uidlist.maxcount = 1; 448d822163fSGarance A Drosehn *uidlist.l.uids = getuid(); 449cf22dcfcSBrian Somers } 4504b88c807SRodney W. Grimes 4514b88c807SRodney W. Grimes /* 4524b88c807SRodney W. Grimes * scan requested variables, noting what structures are needed, 453bdfebd84SKris Kennaway * and adjusting header widths as appropriate. 4544b88c807SRodney W. Grimes */ 4554b88c807SRodney W. Grimes scanvars(); 456a4c8a745SGarance A Drosehn 4574b88c807SRodney W. Grimes /* 458a4c8a745SGarance A Drosehn * Get process list. If the user requested just one selector- 459a4c8a745SGarance A Drosehn * option, then kvm_getprocs can be asked to return just those 460a4c8a745SGarance A Drosehn * processes. Otherwise, have it return all processes, and 461a4c8a745SGarance A Drosehn * then this routine will search that full list and select the 462a4c8a745SGarance A Drosehn * processes which match any of the user's selector-options. 4634b88c807SRodney W. Grimes */ 464d75c1d83SDaniel Eischen what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC; 46548b8c0deSScott Long flag = 0; 466a4c8a745SGarance A Drosehn if (nselectors == 1) { 4677bd42165SGarance A Drosehn if (gidlist.count == 1) { 4687bd42165SGarance A Drosehn what = KERN_PROC_RGID | showthreads; 4697bd42165SGarance A Drosehn flag = *gidlist.l.gids; 4707bd42165SGarance A Drosehn nselectors = 0; 4717bd42165SGarance A Drosehn } else if (pgrplist.count == 1) { 472a4c8a745SGarance A Drosehn what = KERN_PROC_PGRP | showthreads; 473d822163fSGarance A Drosehn flag = *pgrplist.l.pids; 474a4c8a745SGarance A Drosehn nselectors = 0; 475a4c8a745SGarance A Drosehn } else if (pidlist.count == 1) { 476a4c8a745SGarance A Drosehn what = KERN_PROC_PID | showthreads; 477d822163fSGarance A Drosehn flag = *pidlist.l.pids; 478a4c8a745SGarance A Drosehn nselectors = 0; 479a4c8a745SGarance A Drosehn } else if (ruidlist.count == 1) { 480a4c8a745SGarance A Drosehn what = KERN_PROC_RUID | showthreads; 481d822163fSGarance A Drosehn flag = *ruidlist.l.uids; 482a4c8a745SGarance A Drosehn nselectors = 0; 483a4c8a745SGarance A Drosehn } else if (sesslist.count == 1) { 484a4c8a745SGarance A Drosehn what = KERN_PROC_SESSION | showthreads; 485d822163fSGarance A Drosehn flag = *sesslist.l.pids; 486a4c8a745SGarance A Drosehn nselectors = 0; 487a4c8a745SGarance A Drosehn } else if (ttylist.count == 1) { 488a4c8a745SGarance A Drosehn what = KERN_PROC_TTY | showthreads; 489d822163fSGarance A Drosehn flag = *ttylist.l.ttys; 490a4c8a745SGarance A Drosehn nselectors = 0; 491a4c8a745SGarance A Drosehn } else if (uidlist.count == 1) { 492a4c8a745SGarance A Drosehn what = KERN_PROC_UID | showthreads; 493d822163fSGarance A Drosehn flag = *uidlist.l.uids; 494a4c8a745SGarance A Drosehn nselectors = 0; 495a4c8a745SGarance A Drosehn } else if (all) { 496a4c8a745SGarance A Drosehn /* No need for this routine to select processes. */ 497a4c8a745SGarance A Drosehn nselectors = 0; 498a4c8a745SGarance A Drosehn } 4994b88c807SRodney W. Grimes } 500d75c1d83SDaniel Eischen 5014b88c807SRodney W. Grimes /* 5024b88c807SRodney W. Grimes * select procs 5034b88c807SRodney W. Grimes */ 504a4c8a745SGarance A Drosehn nentries = -1; 5054e8b6a6fSGarance A Drosehn kp = kvm_getprocs(kd, what, flag, &nentries); 50687e47750SGarance A Drosehn if ((kp == NULL && nentries > 0) || (kp != NULL && nentries < 0)) 5074b88c807SRodney W. Grimes errx(1, "%s", kvm_geterr(kd)); 508a4c8a745SGarance A Drosehn nkept = 0; 5094e8b6a6fSGarance A Drosehn if (nentries > 0) { 5104b88c807SRodney W. Grimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 5114fa7d788SJuli Mallett errx(1, "malloc failed"); 5124b88c807SRodney W. Grimes for (i = nentries; --i >= 0; ++kp) { 513a4c8a745SGarance A Drosehn /* 514a4c8a745SGarance A Drosehn * If the user specified multiple selection-criteria, 515a4c8a745SGarance A Drosehn * then keep any process matched by the inclusive OR 516a4c8a745SGarance A Drosehn * of all the selection-criteria given. 517a4c8a745SGarance A Drosehn */ 518a4c8a745SGarance A Drosehn if (pidlist.count > 0) { 519a4c8a745SGarance A Drosehn for (elem = 0; elem < pidlist.count; elem++) 520d822163fSGarance A Drosehn if (kp->ki_pid == pidlist.l.pids[elem]) 521a4c8a745SGarance A Drosehn goto keepit; 522a4c8a745SGarance A Drosehn } 523a4c8a745SGarance A Drosehn /* 524a4c8a745SGarance A Drosehn * Note that we had to process pidlist before 525a4c8a745SGarance A Drosehn * filtering out processes which do not have 526a4c8a745SGarance A Drosehn * a controlling terminal. 527a4c8a745SGarance A Drosehn */ 528a4c8a745SGarance A Drosehn if (xkeep == 0) { 529a4c8a745SGarance A Drosehn if ((kp->ki_tdev == NODEV || 530a4c8a745SGarance A Drosehn (kp->ki_flag & P_CONTROLT) == 0)) 531a4c8a745SGarance A Drosehn continue; 532a4c8a745SGarance A Drosehn } 533a4c8a745SGarance A Drosehn if (nselectors == 0) 534a4c8a745SGarance A Drosehn goto keepit; 535a4c8a745SGarance A Drosehn if (gidlist.count > 0) { 536a4c8a745SGarance A Drosehn for (elem = 0; elem < gidlist.count; elem++) 537d822163fSGarance A Drosehn if (kp->ki_rgid == gidlist.l.gids[elem]) 538a4c8a745SGarance A Drosehn goto keepit; 539a4c8a745SGarance A Drosehn } 540a4c8a745SGarance A Drosehn if (pgrplist.count > 0) { 541a4c8a745SGarance A Drosehn for (elem = 0; elem < pgrplist.count; elem++) 542d822163fSGarance A Drosehn if (kp->ki_pgid == 543d822163fSGarance A Drosehn pgrplist.l.pids[elem]) 544a4c8a745SGarance A Drosehn goto keepit; 545a4c8a745SGarance A Drosehn } 546a4c8a745SGarance A Drosehn if (ruidlist.count > 0) { 547a4c8a745SGarance A Drosehn for (elem = 0; elem < ruidlist.count; elem++) 548d822163fSGarance A Drosehn if (kp->ki_ruid == 549d822163fSGarance A Drosehn ruidlist.l.uids[elem]) 550a4c8a745SGarance A Drosehn goto keepit; 551a4c8a745SGarance A Drosehn } 552a4c8a745SGarance A Drosehn if (sesslist.count > 0) { 553a4c8a745SGarance A Drosehn for (elem = 0; elem < sesslist.count; elem++) 554d822163fSGarance A Drosehn if (kp->ki_sid == sesslist.l.pids[elem]) 555a4c8a745SGarance A Drosehn goto keepit; 556a4c8a745SGarance A Drosehn } 557a4c8a745SGarance A Drosehn if (ttylist.count > 0) { 558a4c8a745SGarance A Drosehn for (elem = 0; elem < ttylist.count; elem++) 559d822163fSGarance A Drosehn if (kp->ki_tdev == ttylist.l.ttys[elem]) 560a4c8a745SGarance A Drosehn goto keepit; 561a4c8a745SGarance A Drosehn } 562a4c8a745SGarance A Drosehn if (uidlist.count > 0) { 563a4c8a745SGarance A Drosehn for (elem = 0; elem < uidlist.count; elem++) 564d822163fSGarance A Drosehn if (kp->ki_uid == uidlist.l.uids[elem]) 565a4c8a745SGarance A Drosehn goto keepit; 566a4c8a745SGarance A Drosehn } 567a4c8a745SGarance A Drosehn /* 568a4c8a745SGarance A Drosehn * This process did not match any of the user's 569a4c8a745SGarance A Drosehn * selector-options, so skip the process. 570a4c8a745SGarance A Drosehn */ 571a4c8a745SGarance A Drosehn continue; 572a4c8a745SGarance A Drosehn 573a4c8a745SGarance A Drosehn keepit: 5744bac4483SGarance A Drosehn next_KINFO = &kinfo[nkept]; 5754bac4483SGarance A Drosehn next_KINFO->ki_p = kp; 5764bac4483SGarance A Drosehn next_KINFO->ki_pcpu = getpcpu(next_KINFO); 5774bac4483SGarance A Drosehn if (sortby == SORTMEM) 5784bac4483SGarance A Drosehn next_KINFO->ki_memsize = kp->ki_tsize + 5794bac4483SGarance A Drosehn kp->ki_dsize + kp->ki_ssize; 5804b88c807SRodney W. Grimes if (needuser) 5814bac4483SGarance A Drosehn saveuser(next_KINFO); 5824bac4483SGarance A Drosehn dynsizevars(next_KINFO); 583a4c8a745SGarance A Drosehn nkept++; 5844b88c807SRodney W. Grimes } 5854e8b6a6fSGarance A Drosehn } 5866a2d726bSJordan K. Hubbard 5876a2d726bSJordan K. Hubbard sizevars(); 5886a2d726bSJordan K. Hubbard 5894b88c807SRodney W. Grimes /* 5904b88c807SRodney W. Grimes * print header 5914b88c807SRodney W. Grimes */ 5924b88c807SRodney W. Grimes printheader(); 593a4c8a745SGarance A Drosehn if (nkept == 0) 594f8c9c11cSWill Andrews exit(1); 595a4c8a745SGarance A Drosehn 5964b88c807SRodney W. Grimes /* 5974b88c807SRodney W. Grimes * sort proc list 5984b88c807SRodney W. Grimes */ 599a4c8a745SGarance A Drosehn qsort(kinfo, nkept, sizeof(KINFO), pscomp); 6004b88c807SRodney W. Grimes /* 601a4c8a745SGarance A Drosehn * For each process, call each variable output function. 6024b88c807SRodney W. Grimes */ 603a4c8a745SGarance A Drosehn for (i = lineno = 0; i < nkept; i++) { 604bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 6054b88c807SRodney W. Grimes (vent->var->oproc)(&kinfo[i], vent); 606bdf8ab46SGarance A Drosehn if (STAILQ_NEXT(vent, next_ve) != NULL) 6074b88c807SRodney W. Grimes (void)putchar(' '); 6084b88c807SRodney W. Grimes } 6094b88c807SRodney W. Grimes (void)putchar('\n'); 6104b88c807SRodney W. Grimes if (prtheader && lineno++ == prtheader - 4) { 6114b88c807SRodney W. Grimes (void)putchar('\n'); 6124b88c807SRodney W. Grimes printheader(); 6134b88c807SRodney W. Grimes lineno = 0; 6144b88c807SRodney W. Grimes } 6154b88c807SRodney W. Grimes } 616a4c8a745SGarance A Drosehn free_list(&gidlist); 617a4c8a745SGarance A Drosehn free_list(&pidlist); 618a4c8a745SGarance A Drosehn free_list(&pgrplist); 619a4c8a745SGarance A Drosehn free_list(&ruidlist); 620a4c8a745SGarance A Drosehn free_list(&sesslist); 621a4c8a745SGarance A Drosehn free_list(&ttylist); 622a4c8a745SGarance A Drosehn free_list(&uidlist); 6232e6c6ac4SGarance A Drosehn 6244b88c807SRodney W. Grimes exit(eval); 6254b88c807SRodney W. Grimes } 6264b88c807SRodney W. Grimes 627a4c8a745SGarance A Drosehn static int 628a4c8a745SGarance A Drosehn addelem_gid(struct listinfo *inf, const char *elem) 6294e8b6a6fSGarance A Drosehn { 630a4c8a745SGarance A Drosehn struct group *grp; 631a4c8a745SGarance A Drosehn const char *nameorID; 632a4c8a745SGarance A Drosehn char *endp; 6330b42be7cSGarance A Drosehn u_long bigtemp; 6344e8b6a6fSGarance A Drosehn 635a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 636a4c8a745SGarance A Drosehn if (*elem == '\0') 637a4c8a745SGarance A Drosehn warnx("Invalid (zero-length) %s name", inf->lname); 638a4c8a745SGarance A Drosehn else 639a4c8a745SGarance A Drosehn warnx("%s name too long: %s", inf->lname, elem); 640a4c8a745SGarance A Drosehn optfatal = 1; 641352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 6424e8b6a6fSGarance A Drosehn } 643a4c8a745SGarance A Drosehn 6444e8b6a6fSGarance A Drosehn /* 645a4c8a745SGarance A Drosehn * SUSv3 states that `ps -G grouplist' should match "real-group 646a4c8a745SGarance A Drosehn * ID numbers", and does not mention group-names. I do want to 647a4c8a745SGarance A Drosehn * also support group-names, so this tries for a group-id first, 648a4c8a745SGarance A Drosehn * and only tries for a name if that doesn't work. This is the 649a4c8a745SGarance A Drosehn * opposite order of what is done in addelem_uid(), but in 650a4c8a745SGarance A Drosehn * practice the order would only matter for group-names which 651a4c8a745SGarance A Drosehn * are all-numeric. 6524e8b6a6fSGarance A Drosehn */ 653a4c8a745SGarance A Drosehn grp = NULL; 654a4c8a745SGarance A Drosehn nameorID = "named"; 655a4c8a745SGarance A Drosehn errno = 0; 6560b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 6570b42be7cSGarance A Drosehn if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) { 658a4c8a745SGarance A Drosehn nameorID = "name or ID matches"; 6590b42be7cSGarance A Drosehn grp = getgrgid((gid_t)bigtemp); 660a4c8a745SGarance A Drosehn } 661a4c8a745SGarance A Drosehn if (grp == NULL) 662a4c8a745SGarance A Drosehn grp = getgrnam(elem); 663a4c8a745SGarance A Drosehn if (grp == NULL) { 664a4c8a745SGarance A Drosehn warnx("No %s %s '%s'", inf->lname, nameorID, elem); 665a4c8a745SGarance A Drosehn optfatal = 1; 666c23b00b7SGarance A Drosehn return (0); 667a4c8a745SGarance A Drosehn } 668a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 669a4c8a745SGarance A Drosehn expand_list(inf); 670d822163fSGarance A Drosehn inf->l.gids[(inf->count)++] = grp->gr_gid; 671a4c8a745SGarance A Drosehn return (1); 672a4c8a745SGarance A Drosehn } 673a4c8a745SGarance A Drosehn 674352b6523SGarance A Drosehn #define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc.h. */ 675a4c8a745SGarance A Drosehn static int 676a4c8a745SGarance A Drosehn addelem_pid(struct listinfo *inf, const char *elem) 677a4c8a745SGarance A Drosehn { 678a4c8a745SGarance A Drosehn char *endp; 679ca62e195SGarance A Drosehn long tempid; 680a4c8a745SGarance A Drosehn 681de1702f4SGarance A Drosehn if (*elem == '\0') { 682de1702f4SGarance A Drosehn warnx("Invalid (zero-length) process id"); 683de1702f4SGarance A Drosehn optfatal = 1; 684de1702f4SGarance A Drosehn return (0); /* Do not add this value. */ 68525113083SGarance A Drosehn } 68625113083SGarance A Drosehn 687a4c8a745SGarance A Drosehn errno = 0; 688a4c8a745SGarance A Drosehn tempid = strtol(elem, &endp, 10); 689a4c8a745SGarance A Drosehn if (*endp != '\0' || tempid < 0 || elem == endp) { 690a4c8a745SGarance A Drosehn warnx("Invalid %s: %s", inf->lname, elem); 6914e8b6a6fSGarance A Drosehn errno = ERANGE; 6924e8b6a6fSGarance A Drosehn } else if (errno != 0 || tempid > BSD_PID_MAX) { 693a4c8a745SGarance A Drosehn warnx("%s too large: %s", inf->lname, elem); 6944e8b6a6fSGarance A Drosehn errno = ERANGE; 6954e8b6a6fSGarance A Drosehn } 6964e8b6a6fSGarance A Drosehn if (errno == ERANGE) { 697a4c8a745SGarance A Drosehn optfatal = 1; 698c23b00b7SGarance A Drosehn return (0); 6994e8b6a6fSGarance A Drosehn } 700a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 701a4c8a745SGarance A Drosehn expand_list(inf); 702d822163fSGarance A Drosehn inf->l.pids[(inf->count)++] = tempid; 703a4c8a745SGarance A Drosehn return (1); 7044e8b6a6fSGarance A Drosehn } 705a4c8a745SGarance A Drosehn #undef BSD_PID_MAX 7064e8b6a6fSGarance A Drosehn 70723b8efadSGarance A Drosehn /*- 70823b8efadSGarance A Drosehn * The user can specify a device via one of three formats: 70923b8efadSGarance A Drosehn * 1) fully qualified, e.g.: /dev/ttyp0 /dev/console 71023b8efadSGarance A Drosehn * 2) missing "/dev", e.g.: ttyp0 console 71123b8efadSGarance A Drosehn * 3) two-letters, e.g.: p0 co 71223b8efadSGarance A Drosehn * (matching letters that would be seen in the "TT" column) 71323b8efadSGarance A Drosehn */ 714a4c8a745SGarance A Drosehn static int 715a4c8a745SGarance A Drosehn addelem_tty(struct listinfo *inf, const char *elem) 716cf22dcfcSBrian Somers { 717a4c8a745SGarance A Drosehn const char *ttypath; 718ca62e195SGarance A Drosehn struct stat sb; 719f2fbfae6SGarance A Drosehn char pathbuf[PATH_MAX], pathbuf2[PATH_MAX]; 720a4c8a745SGarance A Drosehn 72123b8efadSGarance A Drosehn ttypath = NULL; 722f2fbfae6SGarance A Drosehn pathbuf2[0] = '\0'; 72323b8efadSGarance A Drosehn switch (*elem) { 72423b8efadSGarance A Drosehn case '/': 725a4c8a745SGarance A Drosehn ttypath = elem; 72623b8efadSGarance A Drosehn break; 72723b8efadSGarance A Drosehn case 'c': 72823b8efadSGarance A Drosehn if (strcmp(elem, "co") == 0) { 72923b8efadSGarance A Drosehn ttypath = _PATH_CONSOLE; 73023b8efadSGarance A Drosehn break; 73123b8efadSGarance A Drosehn } 73223b8efadSGarance A Drosehn /* FALLTHROUGH */ 73323b8efadSGarance A Drosehn default: 73423b8efadSGarance A Drosehn strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf)); 735a4c8a745SGarance A Drosehn strlcat(pathbuf, elem, sizeof(pathbuf)); 736a4c8a745SGarance A Drosehn ttypath = pathbuf; 737f2fbfae6SGarance A Drosehn if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0) 73823b8efadSGarance A Drosehn break; 73923b8efadSGarance A Drosehn if (strcmp(pathbuf, _PATH_CONSOLE) == 0) 74023b8efadSGarance A Drosehn break; 741f2fbfae6SGarance A Drosehn /* Check to see if /dev/tty${elem} exists */ 742f2fbfae6SGarance A Drosehn strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2)); 743f2fbfae6SGarance A Drosehn strlcat(pathbuf2, elem, sizeof(pathbuf2)); 744f2fbfae6SGarance A Drosehn if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) { 74523b8efadSGarance A Drosehn /* No need to repeat stat() && S_ISCHR() checks */ 74623b8efadSGarance A Drosehn ttypath = NULL; 74723b8efadSGarance A Drosehn break; 748a4c8a745SGarance A Drosehn } 74923b8efadSGarance A Drosehn break; 75023b8efadSGarance A Drosehn } 75123b8efadSGarance A Drosehn if (ttypath) { 752a4c8a745SGarance A Drosehn if (stat(ttypath, &sb) == -1) { 753f2fbfae6SGarance A Drosehn if (pathbuf2[0] != '\0') 754f2fbfae6SGarance A Drosehn warn("%s and %s", pathbuf2, ttypath); 755f2fbfae6SGarance A Drosehn else 756a4c8a745SGarance A Drosehn warn("%s", ttypath); 757a4c8a745SGarance A Drosehn optfatal = 1; 758c23b00b7SGarance A Drosehn return (0); 759a4c8a745SGarance A Drosehn } 760a4c8a745SGarance A Drosehn if (!S_ISCHR(sb.st_mode)) { 761f2fbfae6SGarance A Drosehn if (pathbuf2[0] != '\0') 762f2fbfae6SGarance A Drosehn warnx("%s and %s: Not a terminal", pathbuf2, 763f2fbfae6SGarance A Drosehn ttypath); 764f2fbfae6SGarance A Drosehn else 765f2fbfae6SGarance A Drosehn warnx("%s: Not a terminal", ttypath); 766a4c8a745SGarance A Drosehn optfatal = 1; 767c23b00b7SGarance A Drosehn return (0); 768a4c8a745SGarance A Drosehn } 76923b8efadSGarance A Drosehn } 770a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 771a4c8a745SGarance A Drosehn expand_list(inf); 772d822163fSGarance A Drosehn inf->l.ttys[(inf->count)++] = sb.st_rdev; 773a4c8a745SGarance A Drosehn return (1); 774a4c8a745SGarance A Drosehn } 775a4c8a745SGarance A Drosehn 776a4c8a745SGarance A Drosehn static int 777a4c8a745SGarance A Drosehn addelem_uid(struct listinfo *inf, const char *elem) 778a4c8a745SGarance A Drosehn { 779cf22dcfcSBrian Somers struct passwd *pwd; 780a4c8a745SGarance A Drosehn char *endp; 7810b42be7cSGarance A Drosehn u_long bigtemp; 782cf22dcfcSBrian Somers 783a4c8a745SGarance A Drosehn if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 784a4c8a745SGarance A Drosehn if (*elem == '\0') 785a4c8a745SGarance A Drosehn warnx("Invalid (zero-length) %s name", inf->lname); 786a4c8a745SGarance A Drosehn else 787a4c8a745SGarance A Drosehn warnx("%s name too long: %s", inf->lname, elem); 788a4c8a745SGarance A Drosehn optfatal = 1; 789352b6523SGarance A Drosehn return (0); /* Do not add this value. */ 790a4c8a745SGarance A Drosehn } 791cf22dcfcSBrian Somers 792a4c8a745SGarance A Drosehn pwd = getpwnam(elem); 793a4c8a745SGarance A Drosehn if (pwd == NULL) { 794a4c8a745SGarance A Drosehn errno = 0; 7950b42be7cSGarance A Drosehn bigtemp = strtoul(elem, &endp, 10); 7960b42be7cSGarance A Drosehn if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX) 797a4c8a745SGarance A Drosehn warnx("No %s named '%s'", inf->lname, elem); 798a4c8a745SGarance A Drosehn else { 799a4c8a745SGarance A Drosehn /* The string is all digits, so it might be a userID. */ 8000b42be7cSGarance A Drosehn pwd = getpwuid((uid_t)bigtemp); 801a4c8a745SGarance A Drosehn if (pwd == NULL) 802a4c8a745SGarance A Drosehn warnx("No %s name or ID matches '%s'", 803a4c8a745SGarance A Drosehn inf->lname, elem); 804cf22dcfcSBrian Somers } 805cf22dcfcSBrian Somers } 806a4c8a745SGarance A Drosehn if (pwd == NULL) { 807e3c4e1ddSGarance A Drosehn /* 808e3c4e1ddSGarance A Drosehn * These used to be treated as minor warnings (and the 809e3c4e1ddSGarance A Drosehn * option was simply ignored), but now they are fatal 810e3c4e1ddSGarance A Drosehn * errors (and the command will be aborted). 811e3c4e1ddSGarance A Drosehn */ 812e3c4e1ddSGarance A Drosehn optfatal = 1; 813c23b00b7SGarance A Drosehn return (0); 814cf22dcfcSBrian Somers } 815a4c8a745SGarance A Drosehn if (inf->count >= inf->maxcount) 816a4c8a745SGarance A Drosehn expand_list(inf); 817d822163fSGarance A Drosehn inf->l.uids[(inf->count)++] = pwd->pw_uid; 818a4c8a745SGarance A Drosehn return (1); 819a4c8a745SGarance A Drosehn } 820cf22dcfcSBrian Somers 821a4c8a745SGarance A Drosehn static void 822a4c8a745SGarance A Drosehn add_list(struct listinfo *inf, const char *argp) 823a4c8a745SGarance A Drosehn { 824a4c8a745SGarance A Drosehn const char *savep; 825a4c8a745SGarance A Drosehn char *cp, *endp; 826a4c8a745SGarance A Drosehn int toolong; 827ca62e195SGarance A Drosehn char elemcopy[PATH_MAX]; 828a4c8a745SGarance A Drosehn 829de1702f4SGarance A Drosehn if (*argp == 0) 830de1702f4SGarance A Drosehn inf->addelem(inf, elemcopy); 831a4c8a745SGarance A Drosehn while (*argp != '\0') { 832a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 833a4c8a745SGarance A Drosehn argp++; 834a4c8a745SGarance A Drosehn savep = argp; 835a4c8a745SGarance A Drosehn toolong = 0; 836a4c8a745SGarance A Drosehn cp = elemcopy; 837a4c8a745SGarance A Drosehn if (strchr(T_SEP, *argp) == NULL) { 838a4c8a745SGarance A Drosehn endp = elemcopy + sizeof(elemcopy) - 1; 839a4c8a745SGarance A Drosehn while (*argp != '\0' && cp <= endp && 840a4c8a745SGarance A Drosehn strchr(W_SEP T_SEP, *argp) == NULL) 841a4c8a745SGarance A Drosehn *cp++ = *argp++; 842a4c8a745SGarance A Drosehn if (cp > endp) 843a4c8a745SGarance A Drosehn toolong = 1; 844a4c8a745SGarance A Drosehn } 845a4c8a745SGarance A Drosehn if (!toolong) { 846a4c8a745SGarance A Drosehn *cp = '\0'; 847352b6523SGarance A Drosehn /* 848a9626fb3SGarance A Drosehn * Add this single element to the given list. 849352b6523SGarance A Drosehn */ 850a4c8a745SGarance A Drosehn inf->addelem(inf, elemcopy); 851a4c8a745SGarance A Drosehn } else { 852a4c8a745SGarance A Drosehn /* 853a4c8a745SGarance A Drosehn * The string is too long to copy. Find the end 854a4c8a745SGarance A Drosehn * of the string to print out the warning message. 855a4c8a745SGarance A Drosehn */ 856a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP T_SEP, 857a4c8a745SGarance A Drosehn *argp) == NULL) 858a4c8a745SGarance A Drosehn argp++; 859a4c8a745SGarance A Drosehn warnx("Value too long: %.*s", (int)(argp - savep), 860a4c8a745SGarance A Drosehn savep); 861a4c8a745SGarance A Drosehn optfatal = 1; 862a4c8a745SGarance A Drosehn } 863a4c8a745SGarance A Drosehn /* 864a4c8a745SGarance A Drosehn * Skip over any number of trailing whitespace characters, 865a4c8a745SGarance A Drosehn * but only one (at most) trailing element-terminating 866a4c8a745SGarance A Drosehn * character. 867a4c8a745SGarance A Drosehn */ 868a4c8a745SGarance A Drosehn while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 869a4c8a745SGarance A Drosehn argp++; 870a4c8a745SGarance A Drosehn if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) { 871a4c8a745SGarance A Drosehn argp++; 872a4c8a745SGarance A Drosehn /* Catch case where string ended with a comma. */ 873a4c8a745SGarance A Drosehn if (*argp == '\0') 874a4c8a745SGarance A Drosehn inf->addelem(inf, argp); 875a4c8a745SGarance A Drosehn } 876a4c8a745SGarance A Drosehn } 877a4c8a745SGarance A Drosehn } 878a4c8a745SGarance A Drosehn 879a4c8a745SGarance A Drosehn static void * 880a4c8a745SGarance A Drosehn expand_list(struct listinfo *inf) 881a4c8a745SGarance A Drosehn { 882a4c8a745SGarance A Drosehn void *newlist; 883ca62e195SGarance A Drosehn int newmax; 884a4c8a745SGarance A Drosehn 885a4c8a745SGarance A Drosehn newmax = (inf->maxcount + 1) << 1; 886d822163fSGarance A Drosehn newlist = realloc(inf->l.ptr, newmax * inf->elemsize); 887a4c8a745SGarance A Drosehn if (newlist == NULL) { 888d822163fSGarance A Drosehn free(inf->l.ptr); 889c23b00b7SGarance A Drosehn errx(1, "realloc to %d %ss failed", newmax, inf->lname); 890a4c8a745SGarance A Drosehn } 891a4c8a745SGarance A Drosehn inf->maxcount = newmax; 892d822163fSGarance A Drosehn inf->l.ptr = newlist; 893a4c8a745SGarance A Drosehn 894a4c8a745SGarance A Drosehn return (newlist); 895a4c8a745SGarance A Drosehn } 896a4c8a745SGarance A Drosehn 897a4c8a745SGarance A Drosehn static void 898a4c8a745SGarance A Drosehn free_list(struct listinfo *inf) 899a4c8a745SGarance A Drosehn { 900a4c8a745SGarance A Drosehn 901a4c8a745SGarance A Drosehn inf->count = inf->elemsize = inf->maxcount = 0; 902d822163fSGarance A Drosehn if (inf->l.ptr != NULL) 903d822163fSGarance A Drosehn free(inf->l.ptr); 904a4c8a745SGarance A Drosehn inf->addelem = NULL; 905a4c8a745SGarance A Drosehn inf->lname = NULL; 906d822163fSGarance A Drosehn inf->l.ptr = NULL; 907a4c8a745SGarance A Drosehn } 908a4c8a745SGarance A Drosehn 909a4c8a745SGarance A Drosehn static void 910a4c8a745SGarance A Drosehn init_list(struct listinfo *inf, addelem_rtn artn, int elemsize, 911a4c8a745SGarance A Drosehn const char *lname) 912a4c8a745SGarance A Drosehn { 913a4c8a745SGarance A Drosehn 914a4c8a745SGarance A Drosehn inf->count = inf->maxcount = 0; 915a4c8a745SGarance A Drosehn inf->elemsize = elemsize; 916a4c8a745SGarance A Drosehn inf->addelem = artn; 917a4c8a745SGarance A Drosehn inf->lname = lname; 918d822163fSGarance A Drosehn inf->l.ptr = NULL; 919cf22dcfcSBrian Somers } 920cf22dcfcSBrian Somers 921fde411d5SJuli Mallett VARENT * 922fde411d5SJuli Mallett find_varentry(VAR *v) 923fde411d5SJuli Mallett { 924fde411d5SJuli Mallett struct varent *vent; 925fde411d5SJuli Mallett 926bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 927fde411d5SJuli Mallett if (strcmp(vent->var->name, v->name) == 0) 928fde411d5SJuli Mallett return vent; 929fde411d5SJuli Mallett } 930fde411d5SJuli Mallett return NULL; 931fde411d5SJuli Mallett } 932fde411d5SJuli Mallett 9334b88c807SRodney W. Grimes static void 93446251ddeSWarner Losh scanvars(void) 9354b88c807SRodney W. Grimes { 9364b88c807SRodney W. Grimes struct varent *vent; 9374b88c807SRodney W. Grimes VAR *v; 9386a2d726bSJordan K. Hubbard 939bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 9406a2d726bSJordan K. Hubbard v = vent->var; 9416a2d726bSJordan K. Hubbard if (v->flag & DSIZ) { 9426a2d726bSJordan K. Hubbard v->dwidth = v->width; 9436a2d726bSJordan K. Hubbard v->width = 0; 9446a2d726bSJordan K. Hubbard } 9456a2d726bSJordan K. Hubbard if (v->flag & USER) 9466a2d726bSJordan K. Hubbard needuser = 1; 9476a2d726bSJordan K. Hubbard if (v->flag & COMM) 9486a2d726bSJordan K. Hubbard needcomm = 1; 9496a2d726bSJordan K. Hubbard } 9506a2d726bSJordan K. Hubbard } 9516a2d726bSJordan K. Hubbard 9526a2d726bSJordan K. Hubbard static void 95346251ddeSWarner Losh dynsizevars(KINFO *ki) 9546a2d726bSJordan K. Hubbard { 9556a2d726bSJordan K. Hubbard struct varent *vent; 9566a2d726bSJordan K. Hubbard VAR *v; 9576a2d726bSJordan K. Hubbard int i; 9586a2d726bSJordan K. Hubbard 959bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 9606a2d726bSJordan K. Hubbard v = vent->var; 9616a2d726bSJordan K. Hubbard if (!(v->flag & DSIZ)) 9626a2d726bSJordan K. Hubbard continue; 9636a2d726bSJordan K. Hubbard i = (v->sproc)( ki); 9646a2d726bSJordan K. Hubbard if (v->width < i) 9656a2d726bSJordan K. Hubbard v->width = i; 9666a2d726bSJordan K. Hubbard if (v->width > v->dwidth) 9676a2d726bSJordan K. Hubbard v->width = v->dwidth; 9686a2d726bSJordan K. Hubbard } 9696a2d726bSJordan K. Hubbard } 9706a2d726bSJordan K. Hubbard 9716a2d726bSJordan K. Hubbard static void 97246251ddeSWarner Losh sizevars(void) 9736a2d726bSJordan K. Hubbard { 9746a2d726bSJordan K. Hubbard struct varent *vent; 9756a2d726bSJordan K. Hubbard VAR *v; 9764b88c807SRodney W. Grimes int i; 9774b88c807SRodney W. Grimes 978bdf8ab46SGarance A Drosehn STAILQ_FOREACH(vent, &varlist, next_ve) { 9794b88c807SRodney W. Grimes v = vent->var; 98078b1878aSJuli Mallett i = strlen(vent->header); 9814b88c807SRodney W. Grimes if (v->width < i) 9824b88c807SRodney W. Grimes v->width = i; 9834b88c807SRodney W. Grimes totwidth += v->width + 1; /* +1 for space */ 9844b88c807SRodney W. Grimes } 9854b88c807SRodney W. Grimes totwidth--; 9864b88c807SRodney W. Grimes } 9874b88c807SRodney W. Grimes 988871e8d8cSMark Murray static const char * 98946251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 99046251ddeSWarner Losh char *comm, int maxlen) 9914b88c807SRodney W. Grimes { 992871e8d8cSMark Murray const char *s; 9934b88c807SRodney W. Grimes 994871e8d8cSMark Murray s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen); 9954b88c807SRodney W. Grimes return (s); 9964b88c807SRodney W. Grimes } 9974b88c807SRodney W. Grimes 998b61ce5b0SJeff Roberson #define UREADOK(ki) (forceuread || (ki->ki_p->ki_flag & P_INMEM)) 9993ac5e955SJohn Dyson 10004b88c807SRodney W. Grimes static void 100146251ddeSWarner Losh saveuser(KINFO *ki) 10024b88c807SRodney W. Grimes { 10034b88c807SRodney W. Grimes 1004b61ce5b0SJeff Roberson if (ki->ki_p->ki_flag & P_INMEM) { 10054b88c807SRodney W. Grimes /* 10064b88c807SRodney W. Grimes * The u-area might be swapped out, and we can't get 10074b88c807SRodney W. Grimes * at it because we have a crashdump and no swap. 10084b88c807SRodney W. Grimes * If it's here fill in these fields, otherwise, just 10094b88c807SRodney W. Grimes * leave them 0. 10104b88c807SRodney W. Grimes */ 10111f7d2501SKirk McKusick ki->ki_valid = 1; 10124b88c807SRodney W. Grimes } else 10131f7d2501SKirk McKusick ki->ki_valid = 0; 10144b88c807SRodney W. Grimes /* 10154b88c807SRodney W. Grimes * save arguments if needed 10164b88c807SRodney W. Grimes */ 1017dd693acfSGarance A Drosehn if (needcomm) { 1018dd693acfSGarance A Drosehn if (ki->ki_p->ki_stat == SZOMB) 1019dd693acfSGarance A Drosehn ki->ki_args = strdup("<defunct>"); 1020dd693acfSGarance A Drosehn else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) 1021dd693acfSGarance A Drosehn ki->ki_args = strdup(fmt(kvm_getargv, ki, 1022dd693acfSGarance A Drosehn ki->ki_p->ki_comm, MAXCOMLEN)); 1023dd693acfSGarance A Drosehn else 1024871e8d8cSMark Murray asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm); 1025bd6233fdSGarance A Drosehn if (ki->ki_args == NULL) 1026dd693acfSGarance A Drosehn errx(1, "malloc failed"); 10273ac5e955SJohn Dyson } else { 10284b88c807SRodney W. Grimes ki->ki_args = NULL; 10293ac5e955SJohn Dyson } 1030dd693acfSGarance A Drosehn if (needenv) { 1031dd693acfSGarance A Drosehn if (UREADOK(ki)) 1032dd693acfSGarance A Drosehn ki->ki_env = strdup(fmt(kvm_getenvv, ki, 1033dd693acfSGarance A Drosehn (char *)NULL, 0)); 1034dd693acfSGarance A Drosehn else 1035dd693acfSGarance A Drosehn ki->ki_env = strdup("()"); 1036dd693acfSGarance A Drosehn if (ki->ki_env == NULL) 1037dd693acfSGarance A Drosehn errx(1, "malloc failed"); 10383ac5e955SJohn Dyson } else { 10394b88c807SRodney W. Grimes ki->ki_env = NULL; 10404b88c807SRodney W. Grimes } 10413ac5e955SJohn Dyson } 10424b88c807SRodney W. Grimes 10434bac4483SGarance A Drosehn /* A macro used to improve the readability of pscomp(). */ 10444bac4483SGarance A Drosehn #define DIFF_RETURN(a, b, field) do { \ 10454bac4483SGarance A Drosehn if ((a)->field != (b)->field) \ 10464bac4483SGarance A Drosehn return (((a)->field < (b)->field) ? -1 : 1); \ 10474bac4483SGarance A Drosehn } while (0) 10484bac4483SGarance A Drosehn 10494b88c807SRodney W. Grimes static int 105046251ddeSWarner Losh pscomp(const void *a, const void *b) 10514b88c807SRodney W. Grimes { 10525bd7b1f3SGarance A Drosehn const KINFO *ka, *kb; 10534b88c807SRodney W. Grimes 10545bd7b1f3SGarance A Drosehn ka = a; 10555bd7b1f3SGarance A Drosehn kb = b; 10565bd7b1f3SGarance A Drosehn /* SORTCPU and SORTMEM are sorted in descending order. */ 10574bac4483SGarance A Drosehn if (sortby == SORTCPU) 10584bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_pcpu); 10594bac4483SGarance A Drosehn if (sortby == SORTMEM) 10604bac4483SGarance A Drosehn DIFF_RETURN(kb, ka, ki_memsize); 10615bd7b1f3SGarance A Drosehn /* 10625bd7b1f3SGarance A Drosehn * TTY's are sorted in ascending order, except that all NODEV 10635bd7b1f3SGarance A Drosehn * processes come before all processes with a device. 10645bd7b1f3SGarance A Drosehn */ 10654bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) { 10664bac4483SGarance A Drosehn if (ka->ki_p->ki_tdev == NODEV) 10675bd7b1f3SGarance A Drosehn return (-1); 10684bac4483SGarance A Drosehn if (kb->ki_p->ki_tdev == NODEV) 10695bd7b1f3SGarance A Drosehn return (1); 10704bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tdev); 10714bac4483SGarance A Drosehn } 10724bac4483SGarance A Drosehn 1073b4b24324SGarance A Drosehn /* PID's and TID's (threads) are sorted in ascending order. */ 10744bac4483SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_pid); 1075b4b24324SGarance A Drosehn DIFF_RETURN(ka, kb, ki_p->ki_tid); 10765bd7b1f3SGarance A Drosehn return (0); 10774b88c807SRodney W. Grimes } 10784bac4483SGarance A Drosehn #undef DIFF_RETURN 10794b88c807SRodney W. Grimes 10804b88c807SRodney W. Grimes /* 10814b88c807SRodney W. Grimes * ICK (all for getopt), would rather hide the ugliness 10824b88c807SRodney W. Grimes * here than taint the main code. 10834b88c807SRodney W. Grimes * 10844b88c807SRodney W. Grimes * ps foo -> ps -foo 10854b88c807SRodney W. Grimes * ps 34 -> ps -p34 10864b88c807SRodney W. Grimes * 10874b88c807SRodney W. Grimes * The old convention that 't' with no trailing tty arg means the users 10884b88c807SRodney W. Grimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 10894b88c807SRodney W. Grimes * feature is available with the option 'T', which takes no argument. 10904b88c807SRodney W. Grimes */ 10914b88c807SRodney W. Grimes static char * 1092bf46a3bfSGarance A Drosehn kludge_oldps_options(const char *optlist, char *origval, const char *nextarg) 10934b88c807SRodney W. Grimes { 10944b88c807SRodney W. Grimes size_t len; 1095c675340aSGarance A Drosehn char *argp, *cp, *newopts, *ns, *optp, *pidp; 10964b88c807SRodney W. Grimes 1097daed3ad6SJuli Mallett /* 1098c675340aSGarance A Drosehn * See if the original value includes any option which takes an 1099c675340aSGarance A Drosehn * argument (and will thus use up the remainder of the string). 1100daed3ad6SJuli Mallett */ 1101c675340aSGarance A Drosehn argp = NULL; 1102c675340aSGarance A Drosehn if (optlist != NULL) { 1103c675340aSGarance A Drosehn for (cp = origval; *cp != '\0'; cp++) { 1104c675340aSGarance A Drosehn optp = strchr(optlist, *cp); 1105c675340aSGarance A Drosehn if ((optp != NULL) && *(optp + 1) == ':') { 1106c675340aSGarance A Drosehn argp = cp; 1107c675340aSGarance A Drosehn break; 1108c675340aSGarance A Drosehn } 1109c675340aSGarance A Drosehn } 1110c675340aSGarance A Drosehn } 1111c675340aSGarance A Drosehn if (argp != NULL && *origval == '-') 1112c675340aSGarance A Drosehn return (origval); 1113daed3ad6SJuli Mallett 11144b88c807SRodney W. Grimes /* 11154b88c807SRodney W. Grimes * if last letter is a 't' flag with no argument (in the context 11164b88c807SRodney W. Grimes * of the oldps options -- option string NOT starting with a '-' -- 11174b88c807SRodney W. Grimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 1118380434d4SBrian Somers * 11192631c777SGarance A Drosehn * However, if a flag accepting a string argument is found earlier 11202631c777SGarance A Drosehn * in the option string (including a possible `t' flag), then the 11212631c777SGarance A Drosehn * remainder of the string must be the argument to that flag; so 1122c675340aSGarance A Drosehn * do not modify that argument. Note that a trailing `t' would 1123c675340aSGarance A Drosehn * cause argp to be set, if argp was not already set by some 1124c675340aSGarance A Drosehn * earlier option. 11254b88c807SRodney W. Grimes */ 1126c675340aSGarance A Drosehn len = strlen(origval); 1127c675340aSGarance A Drosehn cp = origval + len - 1; 1128c675340aSGarance A Drosehn pidp = NULL; 1129bf46a3bfSGarance A Drosehn if (*cp == 't' && *origval != '-' && cp == argp) { 1130bf46a3bfSGarance A Drosehn if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg)) 11314b88c807SRodney W. Grimes *cp = 'T'; 1132bf46a3bfSGarance A Drosehn } else if (argp == NULL) { 1133c675340aSGarance A Drosehn /* 1134c675340aSGarance A Drosehn * The original value did not include any option which takes 1135c675340aSGarance A Drosehn * an argument (and that would include `p' and `t'), so check 1136c675340aSGarance A Drosehn * the value for trailing number, or comma-separated list of 1137c675340aSGarance A Drosehn * numbers, which we will treat as a pid request. 1138c675340aSGarance A Drosehn */ 1139c675340aSGarance A Drosehn if (isdigitch(*cp)) { 1140c675340aSGarance A Drosehn while (cp >= origval && (*cp == ',' || isdigitch(*cp))) 1141c675340aSGarance A Drosehn --cp; 1142c675340aSGarance A Drosehn pidp = cp + 1; 1143c675340aSGarance A Drosehn } 1144c675340aSGarance A Drosehn } 1145c675340aSGarance A Drosehn 1146c675340aSGarance A Drosehn /* 1147c675340aSGarance A Drosehn * If nothing needs to be added to the string, then return 1148c675340aSGarance A Drosehn * the "original" (although possibly modified) value. 1149c675340aSGarance A Drosehn */ 1150c675340aSGarance A Drosehn if (*origval == '-' && pidp == NULL) 1151c675340aSGarance A Drosehn return (origval); 1152c675340aSGarance A Drosehn 1153c675340aSGarance A Drosehn /* 1154c675340aSGarance A Drosehn * Create a copy of the string to add '-' and/or 'p' to the 1155c675340aSGarance A Drosehn * original value. 1156c675340aSGarance A Drosehn */ 1157c675340aSGarance A Drosehn if ((newopts = ns = malloc(len + 3)) == NULL) 1158c675340aSGarance A Drosehn errx(1, "malloc failed"); 1159c675340aSGarance A Drosehn 1160c675340aSGarance A Drosehn if (*origval != '-') 1161c675340aSGarance A Drosehn *ns++ = '-'; /* add option flag */ 1162c675340aSGarance A Drosehn 1163c675340aSGarance A Drosehn if (pidp == NULL) 1164c675340aSGarance A Drosehn strcpy(ns, origval); 11654b88c807SRodney W. Grimes else { 11664b88c807SRodney W. Grimes /* 1167c675340aSGarance A Drosehn * Copy everything before the pid string, add the `p', 1168c675340aSGarance A Drosehn * and then copy the pid string. 11694b88c807SRodney W. Grimes */ 1170c675340aSGarance A Drosehn len = pidp - origval; 1171c675340aSGarance A Drosehn memcpy(ns, origval, len); 1172c675340aSGarance A Drosehn ns += len; 11734b88c807SRodney W. Grimes *ns++ = 'p'; 1174c675340aSGarance A Drosehn strcpy(ns, pidp); 1175c675340aSGarance A Drosehn } 11764b88c807SRodney W. Grimes 11774b88c807SRodney W. Grimes return (newopts); 11784b88c807SRodney W. Grimes } 11794b88c807SRodney W. Grimes 1180a951d1f8SChristian S.J. Peron static int 1181a951d1f8SChristian S.J. Peron check_procfs(void) 1182a951d1f8SChristian S.J. Peron { 1183d8654c64SChristian S.J. Peron struct statfs mnt; 1184a951d1f8SChristian S.J. Peron 1185d8654c64SChristian S.J. Peron if (statfs("/proc", &mnt) < 0) 1186a951d1f8SChristian S.J. Peron return (0); 1187d8654c64SChristian S.J. Peron if (strcmp(mnt.f_fstypename, "procfs") != 0) 1188d8654c64SChristian S.J. Peron return (0); 1189d8654c64SChristian S.J. Peron return (1); 1190a951d1f8SChristian S.J. Peron } 1191a951d1f8SChristian S.J. Peron 11924b88c807SRodney W. Grimes static void 119346251ddeSWarner Losh usage(void) 11944b88c807SRodney W. Grimes { 1195a89237aeSRuslan Ermilov #define SINGLE_OPTS "[-aCce" OPT_LAZY_f "HhjlmrSTuvwXxZ]" 11964b88c807SRodney W. Grimes 1197a4c8a745SGarance A Drosehn (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 1198a89237aeSRuslan Ermilov "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]", 1199a4c8a745SGarance A Drosehn " [-M core] [-N system]", 1200a89237aeSRuslan Ermilov " [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]", 12014b88c807SRodney W. Grimes " ps [-L]"); 12024b88c807SRodney W. Grimes exit(1); 12034b88c807SRodney W. Grimes } 1204