ps.c revision 141578
11556Srgrimes/*- 21556Srgrimes * Copyright (c) 1990, 1993, 1994 31556Srgrimes * The Regents of the University of California. All rights reserved. 41556Srgrimes * 51556Srgrimes * Redistribution and use in source and binary forms, with or without 61556Srgrimes * modification, are permitted provided that the following conditions 71556Srgrimes * are met: 81556Srgrimes * 1. Redistributions of source code must retain the above copyright 91556Srgrimes * notice, this list of conditions and the following disclaimer. 101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111556Srgrimes * notice, this list of conditions and the following disclaimer in the 121556Srgrimes * documentation and/or other materials provided with the distribution. 131556Srgrimes * 4. Neither the name of the University nor the names of its contributors 141556Srgrimes * may be used to endorse or promote products derived from this software 151556Srgrimes * without specific prior written permission. 161556Srgrimes * 171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271556Srgrimes * SUCH DAMAGE. 28127499Sgad * ------+---------+---------+-------- + --------+---------+---------+---------* 29127499Sgad * Copyright (c) 2004 - Garance Alistair Drosehn <gad@FreeBSD.org>. 30127499Sgad * All rights reserved. 31127499Sgad * 32127499Sgad * Significant modifications made to bring `ps' options somewhat closer 33127499Sgad * to the standard for `ps' as described in SingleUnixSpec-v3. 34127499Sgad * ------+---------+---------+-------- + --------+---------+---------+---------* 351556Srgrimes */ 361556Srgrimes 371556Srgrimes#ifndef lint 3890143Smarkmstatic const char copyright[] = 391556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\ 401556Srgrimes The Regents of the University of California. All rights reserved.\n"; 411556Srgrimes#endif /* not lint */ 421556Srgrimes 4390143Smarkm#if 0 441556Srgrimes#ifndef lint 4536049Scharnierstatic char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 4690143Smarkm#endif /* not lint */ 4736049Scharnier#endif 48110391Scharnier 4999110Sobrien#include <sys/cdefs.h> 5099110Sobrien__FBSDID("$FreeBSD: head/bin/ps/ps.c 141578 2005-02-09 17:37:39Z ru $"); 511556Srgrimes 521556Srgrimes#include <sys/param.h> 53127546Sgad#include <sys/proc.h> 543296Sdg#include <sys/user.h> 551556Srgrimes#include <sys/stat.h> 561556Srgrimes#include <sys/ioctl.h> 571556Srgrimes#include <sys/sysctl.h> 58137696Scsjp#include <sys/mount.h> 591556Srgrimes 601556Srgrimes#include <ctype.h> 611556Srgrimes#include <err.h> 62127149Sgad#include <errno.h> 631556Srgrimes#include <fcntl.h> 64127499Sgad#include <grp.h> 651556Srgrimes#include <kvm.h> 6613514Smpp#include <limits.h> 6773367Sache#include <locale.h> 681556Srgrimes#include <paths.h> 6990143Smarkm#include <pwd.h> 701556Srgrimes#include <stdio.h> 711556Srgrimes#include <stdlib.h> 721556Srgrimes#include <string.h> 731556Srgrimes#include <unistd.h> 741556Srgrimes 751556Srgrimes#include "ps.h" 761556Srgrimes 77127499Sgad#define W_SEP " \t" /* "Whitespace" list separators */ 78127499Sgad#define T_SEP "," /* "Terminate-element" list separators */ 7966377Sbrian 80127537Sgad#ifdef LAZY_PS 81127555Sgad#define DEF_UREAD 0 82127537Sgad#define OPT_LAZY_f "f" 83127537Sgad#else 84127555Sgad#define DEF_UREAD 1 /* Always do the more-expensive read. */ 85127537Sgad#define OPT_LAZY_f /* I.e., the `-f' option is not added. */ 86127537Sgad#endif 87127537Sgad 88129914Sgad/* 89129971Sgad * isdigit takes an `int', but expects values in the range of unsigned char. 90129971Sgad * This wrapper ensures that values from a 'char' end up in the correct range. 91129914Sgad */ 92129971Sgad#define isdigitch(Anychar) isdigit((u_char)(Anychar)) 93129914Sgad 94127537Sgadint cflag; /* -c */ 95127537Sgadint eval; /* Exit value */ 96127537Sgadtime_t now; /* Current time(3) value */ 97127537Sgadint rawcpu; /* -C */ 98127537Sgadint sumrusage; /* -S */ 99127537Sgadint termwidth; /* Width of the screen (0 == infinity). */ 100127537Sgadint totwidth; /* Calculated-width of requested variables. */ 101127537Sgad 102130999Sgadstruct velisthead varlist = STAILQ_HEAD_INITIALIZER(varlist); 1031556Srgrimes 104127537Sgadstatic int forceuread = DEF_UREAD; /* Do extra work to get u-area. */ 105127537Sgadstatic kvm_t *kd; 106127537Sgadstatic KINFO *kinfo; 107127537Sgadstatic int needcomm; /* -o "command" */ 108127537Sgadstatic int needenv; /* -e */ 109127537Sgadstatic int needuser; /* -o "user" */ 110127537Sgadstatic int optfatal; /* Fatal error parsing some list-option. */ 1111556Srgrimes 112127537Sgadstatic enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 11397966Sjmallett 114127499Sgadstruct listinfo; 115127537Sgadtypedef int addelem_rtn(struct listinfo *_inf, const char *_elem); 116127499Sgad 117127499Sgadstruct listinfo { 118127499Sgad int count; 119127499Sgad int maxcount; 120127499Sgad int elemsize; 121127499Sgad addelem_rtn *addelem; 122127499Sgad const char *lname; 123127499Sgad union { 124127499Sgad gid_t *gids; 125127499Sgad pid_t *pids; 126127499Sgad dev_t *ttys; 127127499Sgad uid_t *uids; 128127499Sgad void *ptr; 129127823Sgad } l; 130127499Sgad}; 131127499Sgad 132137696Scsjpstatic int check_procfs(void); 133127499Sgadstatic int addelem_gid(struct listinfo *, const char *); 134127499Sgadstatic int addelem_pid(struct listinfo *, const char *); 135127499Sgadstatic int addelem_tty(struct listinfo *, const char *); 136127499Sgadstatic int addelem_uid(struct listinfo *, const char *); 137127499Sgadstatic void add_list(struct listinfo *, const char *); 138127536Sgadstatic void dynsizevars(KINFO *); 139127499Sgadstatic void *expand_list(struct listinfo *); 140127598Sgadstatic const char * 141127598Sgad fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 142127536Sgad KINFO *, char *, int); 143127499Sgadstatic void free_list(struct listinfo *); 144127499Sgadstatic void init_list(struct listinfo *, addelem_rtn, int, const char *); 145129952Sgadstatic char *kludge_oldps_options(const char *, char *, const char *); 146127536Sgadstatic int pscomp(const void *, const void *); 147127536Sgadstatic void saveuser(KINFO *); 148127536Sgadstatic void scanvars(void); 149127536Sgadstatic void sizevars(void); 150127536Sgadstatic void usage(void); 151127499Sgad 15297875Sjmallettstatic char dfmt[] = "pid,tt,state,time,command"; 153129635Sgadstatic char jfmt[] = "user,pid,ppid,pgid,sid,jobc,state,tt,time,command"; 154127538Sgadstatic char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state," 155127538Sgad "tt,time,command"; 15690143Smarkmstatic char o1[] = "pid"; 15797875Sjmallettstatic char o2[] = "tt,state,time,command"; 15897875Sjmallettstatic char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command"; 159127538Sgadstatic char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz," 160127538Sgad "%cpu,%mem,command"; 161105831Srwatsonstatic char Zfmt[] = "label"; 1621556Srgrimes 163127843Sgad#define PS_ARGS "AaCce" OPT_LAZY_f "G:gHhjLlM:mN:O:o:p:rSTt:U:uvwXxZ" 16498494Ssobomax 1651556Srgrimesint 16690110Simpmain(int argc, char *argv[]) 1671556Srgrimes{ 168127499Sgad struct listinfo gidlist, pgrplist, pidlist; 169127499Sgad struct listinfo ruidlist, sesslist, ttylist, uidlist; 1701556Srgrimes struct kinfo_proc *kp; 171130816Sgad KINFO *next_KINFO; 1721556Srgrimes struct varent *vent; 1731556Srgrimes struct winsize ws; 174129914Sgad const char *nlistf, *memf; 175127539Sgad char *cols; 176137670Sru int all, ch, elem, flag, _fmt, i, lineno; 177129914Sgad int nentries, nkept, nselectors; 178127499Sgad int prtheader, showthreads, wflag, what, xkeep, xkeep_implied; 17990143Smarkm char errbuf[_POSIX2_LINE_MAX]; 1801556Srgrimes 18111809Sache (void) setlocale(LC_ALL, ""); 182127542Sgad time(&now); /* Used by routines in print.c. */ 18311809Sache 18497804Stjr if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') 18597804Stjr termwidth = atoi(cols); 18697804Stjr else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1871556Srgrimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1881556Srgrimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1891556Srgrimes ws.ws_col == 0) 1901556Srgrimes termwidth = 79; 1911556Srgrimes else 1921556Srgrimes termwidth = ws.ws_col - 1; 1931556Srgrimes 19498494Ssobomax /* 195129914Sgad * Hide a number of option-processing kludges in a separate routine, 196129914Sgad * to support some historical BSD behaviors, such as `ps axu'. 19798494Ssobomax */ 198129914Sgad if (argc > 1) 199129915Sgad argv[1] = kludge_oldps_options(PS_ARGS, argv[1], argv[2]); 2001556Srgrimes 201137670Sru all = _fmt = nselectors = optfatal = 0; 202127542Sgad prtheader = showthreads = wflag = xkeep_implied = 0; 203127542Sgad xkeep = -1; /* Neither -x nor -X. */ 204127499Sgad init_list(&gidlist, addelem_gid, sizeof(gid_t), "group"); 205127499Sgad init_list(&pgrplist, addelem_pid, sizeof(pid_t), "process group"); 206127499Sgad init_list(&pidlist, addelem_pid, sizeof(pid_t), "process id"); 207127499Sgad init_list(&ruidlist, addelem_uid, sizeof(uid_t), "ruser"); 208127499Sgad init_list(&sesslist, addelem_pid, sizeof(pid_t), "session id"); 209127499Sgad init_list(&ttylist, addelem_tty, sizeof(dev_t), "tty"); 210127499Sgad init_list(&uidlist, addelem_uid, sizeof(uid_t), "user"); 21189909Sru memf = nlistf = _PATH_DEVNULL; 21298494Ssobomax while ((ch = getopt(argc, argv, PS_ARGS)) != -1) 213129967Sgad switch ((char)ch) { 214127499Sgad case 'A': 215127499Sgad /* 216127499Sgad * Exactly the same as `-ax'. This has been 217127499Sgad * added for compatability with SUSv3, but for 218127499Sgad * now it will not be described in the man page. 219127499Sgad */ 220127499Sgad nselectors++; 221127499Sgad all = xkeep = 1; 222127499Sgad break; 2231556Srgrimes case 'a': 224127499Sgad nselectors++; 2251556Srgrimes all = 1; 2261556Srgrimes break; 22719068Speter case 'C': 22819068Speter rawcpu = 1; 22919068Speter break; 23019068Speter case 'c': 23119068Speter cflag = 1; 23219068Speter break; 2331556Srgrimes case 'e': /* XXX set ufmt */ 2341556Srgrimes needenv = 1; 2351556Srgrimes break; 236127506Sgad#ifdef LAZY_PS 237127506Sgad case 'f': 238127506Sgad if (getuid() == 0 || getgid() == 0) 239127542Sgad forceuread = 1; 240127506Sgad break; 241127506Sgad#endif 242127499Sgad case 'G': 243127499Sgad add_list(&gidlist, optarg); 244127499Sgad xkeep_implied = 1; 245127499Sgad nselectors++; 246127499Sgad break; 247127542Sgad case 'g': 248127499Sgad#if 0 249127597Sgad /*- 250127542Sgad * XXX - This SUSv3 behavior is still under debate 251127542Sgad * since it conflicts with the (undocumented) 252127542Sgad * `-g' option. So we skip it for now. 253127542Sgad */ 254127499Sgad add_list(&pgrplist, optarg); 255127499Sgad xkeep_implied = 1; 256127499Sgad nselectors++; 257127499Sgad break; 258127499Sgad#else 259127542Sgad /* The historical BSD-ish (from SunOS) behavior. */ 2601556Srgrimes break; /* no-op */ 261127499Sgad#endif 262116265Sscottl case 'H': 263126127Sdeischen showthreads = KERN_PROC_INC_THREAD; 264116265Sscottl break; 2651556Srgrimes case 'h': 2661556Srgrimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 2671556Srgrimes break; 2681556Srgrimes case 'j': 269109502Sjmallett parsefmt(jfmt, 0); 27090143Smarkm _fmt = 1; 2711556Srgrimes jfmt[0] = '\0'; 2721556Srgrimes break; 2731556Srgrimes case 'L': 2741556Srgrimes showkey(); 2751556Srgrimes exit(0); 2761556Srgrimes case 'l': 277109502Sjmallett parsefmt(lfmt, 0); 27890143Smarkm _fmt = 1; 2791556Srgrimes lfmt[0] = '\0'; 2801556Srgrimes break; 2811556Srgrimes case 'M': 2821556Srgrimes memf = optarg; 2831556Srgrimes break; 2841556Srgrimes case 'm': 2851556Srgrimes sortby = SORTMEM; 2861556Srgrimes break; 2871556Srgrimes case 'N': 2881556Srgrimes nlistf = optarg; 2891556Srgrimes break; 2901556Srgrimes case 'O': 291109502Sjmallett parsefmt(o1, 1); 292109502Sjmallett parsefmt(optarg, 1); 293109502Sjmallett parsefmt(o2, 1); 2941556Srgrimes o1[0] = o2[0] = '\0'; 29590143Smarkm _fmt = 1; 2961556Srgrimes break; 2971556Srgrimes case 'o': 298109502Sjmallett parsefmt(optarg, 1); 29990143Smarkm _fmt = 1; 3001556Srgrimes break; 3011556Srgrimes case 'p': 302127499Sgad add_list(&pidlist, optarg); 303127499Sgad /* 304127499Sgad * Note: `-p' does not *set* xkeep, but any values 305127499Sgad * from pidlist are checked before xkeep is. That 306127499Sgad * way they are always matched, even if the user 307127499Sgad * specifies `-X'. 308127499Sgad */ 309127499Sgad nselectors++; 3101556Srgrimes break; 311127499Sgad#if 0 312127499Sgad case 'R': 313127597Sgad /*- 314127542Sgad * XXX - This un-standard option is still under 315127542Sgad * debate. This is what SUSv3 defines as 316127542Sgad * the `-U' option, and while it would be 317127542Sgad * nice to have, it could cause even more 318127542Sgad * confusion to implement it as `-R'. 319127542Sgad */ 320127499Sgad add_list(&ruidlist, optarg); 321127499Sgad xkeep_implied = 1; 322127499Sgad nselectors++; 323127499Sgad break; 324127499Sgad#endif 3251556Srgrimes case 'r': 3261556Srgrimes sortby = SORTCPU; 3271556Srgrimes break; 3281556Srgrimes case 'S': 3291556Srgrimes sumrusage = 1; 3301556Srgrimes break; 331127499Sgad#if 0 332127499Sgad case 's': 333127597Sgad /*- 334127542Sgad * XXX - This non-standard option is still under 335127542Sgad * debate. This *is* supported on Solaris, 336127542Sgad * Linux, and IRIX, but conflicts with `-s' 337127542Sgad * on NetBSD and maybe some older BSD's. 338127542Sgad */ 339127499Sgad add_list(&sesslist, optarg); 340127499Sgad xkeep_implied = 1; 341127499Sgad nselectors++; 342127499Sgad break; 343127499Sgad#endif 3441556Srgrimes case 'T': 3451556Srgrimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 3461556Srgrimes errx(1, "stdin: not a terminal"); 3471556Srgrimes /* FALLTHROUGH */ 348127499Sgad case 't': 349127499Sgad add_list(&ttylist, optarg); 350127499Sgad xkeep_implied = 1; 351127499Sgad nselectors++; 3521556Srgrimes break; 35313020Speter case 'U': 354127499Sgad /* This is what SUSv3 defines as the `-u' option. */ 355127499Sgad add_list(&uidlist, optarg); 356127499Sgad xkeep_implied = 1; 357127499Sgad nselectors++; 35813020Speter break; 3591556Srgrimes case 'u': 360109502Sjmallett parsefmt(ufmt, 0); 3611556Srgrimes sortby = SORTCPU; 36290143Smarkm _fmt = 1; 3631556Srgrimes ufmt[0] = '\0'; 3641556Srgrimes break; 3651556Srgrimes case 'v': 366109502Sjmallett parsefmt(vfmt, 0); 3671556Srgrimes sortby = SORTMEM; 36890143Smarkm _fmt = 1; 3691556Srgrimes vfmt[0] = '\0'; 3701556Srgrimes break; 3711556Srgrimes case 'w': 3721556Srgrimes if (wflag) 3731556Srgrimes termwidth = UNLIMITED; 3741556Srgrimes else if (termwidth < 131) 3751556Srgrimes termwidth = 131; 3761556Srgrimes wflag++; 3771556Srgrimes break; 378127499Sgad case 'X': 379127499Sgad /* 380127499Sgad * Note that `-X' and `-x' are not standard "selector" 381127499Sgad * options. For most selector-options, we check *all* 382127499Sgad * processes to see if any are matched by the given 383127499Sgad * value(s). After we have a set of all the matched 384127499Sgad * processes, then `-X' and `-x' govern whether we 385127499Sgad * modify that *matched* set for processes which do 386127499Sgad * not have a controlling terminal. `-X' causes 387127499Sgad * those processes to be deleted from the matched 388127499Sgad * set, while `-x' causes them to be kept. 389127499Sgad */ 390127499Sgad xkeep = 0; 391127499Sgad break; 3921556Srgrimes case 'x': 393127499Sgad xkeep = 1; 3941556Srgrimes break; 39586922Sgreen case 'Z': 396109502Sjmallett parsefmt(Zfmt, 0); 39786922Sgreen Zfmt[0] = '\0'; 39886922Sgreen break; 3991556Srgrimes case '?': 4001556Srgrimes default: 4011556Srgrimes usage(); 4021556Srgrimes } 4031556Srgrimes argc -= optind; 4041556Srgrimes argv += optind; 405129914Sgad 406129914Sgad /* 407137696Scsjp * If the user specified ps -e then they want a copy of the process 408137696Scsjp * environment kvm_getenvv(3) attempts to open /proc/<pid>/mem. 409137696Scsjp * Check to make sure that procfs is mounted on /proc, otherwise 410137696Scsjp * print a warning informing the user that output will be incomplete. 411137696Scsjp */ 412137696Scsjp if (needenv == 1 && check_procfs() == 0) 413137696Scsjp warnx("Process environment requires procfs(5)"); 414137696Scsjp /* 415129914Sgad * If there arguments after processing all the options, attempt 416129914Sgad * to treat them as a list of process ids. 417129914Sgad */ 418129914Sgad while (*argv) { 419129914Sgad if (!isdigitch(**argv)) 420129914Sgad break; 421129914Sgad add_list(&pidlist, *argv); 422129914Sgad argv++; 423129914Sgad } 424129914Sgad if (*argv) { 425129914Sgad fprintf(stderr, "%s: illegal argument: %s\n", 426129914Sgad getprogname(), *argv); 427129914Sgad usage(); 428129914Sgad } 429127499Sgad if (optfatal) 430127542Sgad exit(1); /* Error messages already printed. */ 431127542Sgad if (xkeep < 0) /* Neither -X nor -x was specified. */ 432127499Sgad xkeep = xkeep_implied; 433127499Sgad 43489909Sru kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 4351556Srgrimes if (kd == 0) 4361556Srgrimes errx(1, "%s", errbuf); 4371556Srgrimes 43890143Smarkm if (!_fmt) 439109502Sjmallett parsefmt(dfmt, 0); 4401556Srgrimes 441127499Sgad if (nselectors == 0) { 442127823Sgad uidlist.l.ptr = malloc(sizeof(uid_t)); 443127823Sgad if (uidlist.l.ptr == NULL) 44497877Sjmallett errx(1, "malloc failed"); 445127499Sgad nselectors = 1; 446127499Sgad uidlist.count = uidlist.maxcount = 1; 447127823Sgad *uidlist.l.uids = getuid(); 44866377Sbrian } 4491556Srgrimes 4501556Srgrimes /* 4511556Srgrimes * scan requested variables, noting what structures are needed, 45253170Skris * and adjusting header widths as appropriate. 4531556Srgrimes */ 4541556Srgrimes scanvars(); 455127499Sgad 4561556Srgrimes /* 457127499Sgad * Get process list. If the user requested just one selector- 458127499Sgad * option, then kvm_getprocs can be asked to return just those 459127499Sgad * processes. Otherwise, have it return all processes, and 460127499Sgad * then this routine will search that full list and select the 461127499Sgad * processes which match any of the user's selector-options. 4621556Srgrimes */ 463127499Sgad what = showthreads != 0 ? KERN_PROC_ALL : KERN_PROC_PROC; 464127499Sgad flag = 0; 465127499Sgad if (nselectors == 1) { 466129600Sgad if (gidlist.count == 1) { 467129600Sgad what = KERN_PROC_RGID | showthreads; 468129600Sgad flag = *gidlist.l.gids; 469129600Sgad nselectors = 0; 470129600Sgad } else if (pgrplist.count == 1) { 471127499Sgad what = KERN_PROC_PGRP | showthreads; 472127823Sgad flag = *pgrplist.l.pids; 473127499Sgad nselectors = 0; 474127499Sgad } else if (pidlist.count == 1) { 475127499Sgad what = KERN_PROC_PID | showthreads; 476127823Sgad flag = *pidlist.l.pids; 477127499Sgad nselectors = 0; 478127499Sgad } else if (ruidlist.count == 1) { 479127499Sgad what = KERN_PROC_RUID | showthreads; 480127823Sgad flag = *ruidlist.l.uids; 481127499Sgad nselectors = 0; 482127499Sgad } else if (sesslist.count == 1) { 483127499Sgad what = KERN_PROC_SESSION | showthreads; 484127823Sgad flag = *sesslist.l.pids; 485127499Sgad nselectors = 0; 486127499Sgad } else if (ttylist.count == 1) { 487127499Sgad what = KERN_PROC_TTY | showthreads; 488127823Sgad flag = *ttylist.l.ttys; 489127499Sgad nselectors = 0; 490127499Sgad } else if (uidlist.count == 1) { 491127499Sgad what = KERN_PROC_UID | showthreads; 492127823Sgad flag = *uidlist.l.uids; 493127499Sgad nselectors = 0; 494127499Sgad } else if (all) { 495127499Sgad /* No need for this routine to select processes. */ 496127499Sgad nselectors = 0; 497127499Sgad } 4981556Srgrimes } 499126127Sdeischen 5001556Srgrimes /* 5011556Srgrimes * select procs 5021556Srgrimes */ 503127499Sgad nentries = -1; 504127149Sgad kp = kvm_getprocs(kd, what, flag, &nentries); 505127544Sgad if ((kp == NULL && nentries > 0) || (kp != NULL && nentries < 0)) 5061556Srgrimes errx(1, "%s", kvm_geterr(kd)); 507127499Sgad nkept = 0; 508127149Sgad if (nentries > 0) { 509127149Sgad if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 510127149Sgad errx(1, "malloc failed"); 511127149Sgad for (i = nentries; --i >= 0; ++kp) { 512127499Sgad /* 513127499Sgad * If the user specified multiple selection-criteria, 514127499Sgad * then keep any process matched by the inclusive OR 515127499Sgad * of all the selection-criteria given. 516127499Sgad */ 517127499Sgad if (pidlist.count > 0) { 518127499Sgad for (elem = 0; elem < pidlist.count; elem++) 519127823Sgad if (kp->ki_pid == pidlist.l.pids[elem]) 520127499Sgad goto keepit; 521127499Sgad } 522127499Sgad /* 523127499Sgad * Note that we had to process pidlist before 524127499Sgad * filtering out processes which do not have 525127499Sgad * a controlling terminal. 526127499Sgad */ 527127499Sgad if (xkeep == 0) { 528127499Sgad if ((kp->ki_tdev == NODEV || 529127499Sgad (kp->ki_flag & P_CONTROLT) == 0)) 530127499Sgad continue; 531127499Sgad } 532127499Sgad if (nselectors == 0) 533127499Sgad goto keepit; 534127499Sgad if (gidlist.count > 0) { 535127499Sgad for (elem = 0; elem < gidlist.count; elem++) 536127823Sgad if (kp->ki_rgid == gidlist.l.gids[elem]) 537127499Sgad goto keepit; 538127499Sgad } 539127499Sgad if (pgrplist.count > 0) { 540127499Sgad for (elem = 0; elem < pgrplist.count; elem++) 541127823Sgad if (kp->ki_pgid == 542127823Sgad pgrplist.l.pids[elem]) 543127499Sgad goto keepit; 544127499Sgad } 545127499Sgad if (ruidlist.count > 0) { 546127499Sgad for (elem = 0; elem < ruidlist.count; elem++) 547127823Sgad if (kp->ki_ruid == 548127823Sgad ruidlist.l.uids[elem]) 549127499Sgad goto keepit; 550127499Sgad } 551127499Sgad if (sesslist.count > 0) { 552127499Sgad for (elem = 0; elem < sesslist.count; elem++) 553127823Sgad if (kp->ki_sid == sesslist.l.pids[elem]) 554127499Sgad goto keepit; 555127499Sgad } 556127499Sgad if (ttylist.count > 0) { 557127499Sgad for (elem = 0; elem < ttylist.count; elem++) 558127823Sgad if (kp->ki_tdev == ttylist.l.ttys[elem]) 559127499Sgad goto keepit; 560127499Sgad } 561127499Sgad if (uidlist.count > 0) { 562127499Sgad for (elem = 0; elem < uidlist.count; elem++) 563127823Sgad if (kp->ki_uid == uidlist.l.uids[elem]) 564127499Sgad goto keepit; 565127499Sgad } 566127499Sgad /* 567127499Sgad * This process did not match any of the user's 568127499Sgad * selector-options, so skip the process. 569127499Sgad */ 570127499Sgad continue; 571127499Sgad 572127499Sgad keepit: 573130816Sgad next_KINFO = &kinfo[nkept]; 574130816Sgad next_KINFO->ki_p = kp; 575130816Sgad next_KINFO->ki_pcpu = getpcpu(next_KINFO); 576130816Sgad if (sortby == SORTMEM) 577130816Sgad next_KINFO->ki_memsize = kp->ki_tsize + 578130816Sgad kp->ki_dsize + kp->ki_ssize; 579127149Sgad if (needuser) 580130816Sgad saveuser(next_KINFO); 581130816Sgad dynsizevars(next_KINFO); 582127499Sgad nkept++; 583127149Sgad } 5841556Srgrimes } 58525271Sjkh 58625271Sjkh sizevars(); 58725271Sjkh 5881556Srgrimes /* 5891556Srgrimes * print header 5901556Srgrimes */ 5911556Srgrimes printheader(); 592127499Sgad if (nkept == 0) 59362803Swill exit(1); 594127499Sgad 5951556Srgrimes /* 5961556Srgrimes * sort proc list 5971556Srgrimes */ 598127499Sgad qsort(kinfo, nkept, sizeof(KINFO), pscomp); 5991556Srgrimes /* 600127499Sgad * For each process, call each variable output function. 6011556Srgrimes */ 602127499Sgad for (i = lineno = 0; i < nkept; i++) { 603130999Sgad STAILQ_FOREACH(vent, &varlist, next_ve) { 6041556Srgrimes (vent->var->oproc)(&kinfo[i], vent); 605130999Sgad if (STAILQ_NEXT(vent, next_ve) != NULL) 6061556Srgrimes (void)putchar(' '); 6071556Srgrimes } 6081556Srgrimes (void)putchar('\n'); 6091556Srgrimes if (prtheader && lineno++ == prtheader - 4) { 6101556Srgrimes (void)putchar('\n'); 6111556Srgrimes printheader(); 6121556Srgrimes lineno = 0; 6131556Srgrimes } 6141556Srgrimes } 615127499Sgad free_list(&gidlist); 616127499Sgad free_list(&pidlist); 617127499Sgad free_list(&pgrplist); 618127499Sgad free_list(&ruidlist); 619127499Sgad free_list(&sesslist); 620127499Sgad free_list(&ttylist); 621127499Sgad free_list(&uidlist); 62266377Sbrian 6231556Srgrimes exit(eval); 6241556Srgrimes} 6251556Srgrimes 626127499Sgadstatic int 627127499Sgadaddelem_gid(struct listinfo *inf, const char *elem) 628127499Sgad{ 629127499Sgad struct group *grp; 630127499Sgad const char *nameorID; 631127499Sgad char *endp; 632127602Sgad u_long bigtemp; 633127499Sgad 634127499Sgad if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 635127499Sgad if (*elem == '\0') 636127499Sgad warnx("Invalid (zero-length) %s name", inf->lname); 637127499Sgad else 638127499Sgad warnx("%s name too long: %s", inf->lname, elem); 639127499Sgad optfatal = 1; 640127542Sgad return (0); /* Do not add this value. */ 641127499Sgad } 642127499Sgad 643127499Sgad /* 644127499Sgad * SUSv3 states that `ps -G grouplist' should match "real-group 645127499Sgad * ID numbers", and does not mention group-names. I do want to 646127499Sgad * also support group-names, so this tries for a group-id first, 647127499Sgad * and only tries for a name if that doesn't work. This is the 648127499Sgad * opposite order of what is done in addelem_uid(), but in 649127499Sgad * practice the order would only matter for group-names which 650127499Sgad * are all-numeric. 651127499Sgad */ 652127499Sgad grp = NULL; 653127499Sgad nameorID = "named"; 654127499Sgad errno = 0; 655127602Sgad bigtemp = strtoul(elem, &endp, 10); 656127602Sgad if (errno == 0 && *endp == '\0' && bigtemp <= GID_MAX) { 657127499Sgad nameorID = "name or ID matches"; 658127602Sgad grp = getgrgid((gid_t)bigtemp); 659127499Sgad } 660127499Sgad if (grp == NULL) 661127499Sgad grp = getgrnam(elem); 662127499Sgad if (grp == NULL) { 663127499Sgad warnx("No %s %s '%s'", inf->lname, nameorID, elem); 664127499Sgad optfatal = 1; 665129967Sgad return (0); 666127499Sgad } 667127499Sgad if (inf->count >= inf->maxcount) 668127499Sgad expand_list(inf); 669127823Sgad inf->l.gids[(inf->count)++] = grp->gr_gid; 670127499Sgad return (1); 671127499Sgad} 672127499Sgad 673127597Sgad#define BSD_PID_MAX 99999 /* Copy of PID_MAX from sys/proc.h. */ 674127499Sgadstatic int 675127499Sgadaddelem_pid(struct listinfo *inf, const char *elem) 676127149Sgad{ 677127539Sgad char *endp; 678127149Sgad long tempid; 679127149Sgad 680129917Sgad if (*elem == '\0') { 681129917Sgad warnx("Invalid (zero-length) process id"); 682129917Sgad optfatal = 1; 683129917Sgad return (0); /* Do not add this value. */ 684127149Sgad } 685127149Sgad 686129952Sgad errno = 0; 687129952Sgad tempid = strtol(elem, &endp, 10); 688129952Sgad if (*endp != '\0' || tempid < 0 || elem == endp) { 689129952Sgad warnx("Invalid %s: %s", inf->lname, elem); 690129952Sgad errno = ERANGE; 691129952Sgad } else if (errno != 0 || tempid > BSD_PID_MAX) { 692129952Sgad warnx("%s too large: %s", inf->lname, elem); 693129952Sgad errno = ERANGE; 694129952Sgad } 695129952Sgad if (errno == ERANGE) { 696129952Sgad optfatal = 1; 697129967Sgad return (0); 698129952Sgad } 699127499Sgad if (inf->count >= inf->maxcount) 700127499Sgad expand_list(inf); 701127823Sgad inf->l.pids[(inf->count)++] = tempid; 702127499Sgad return (1); 703127499Sgad} 704127499Sgad#undef BSD_PID_MAX 705127149Sgad 706131010Sgad/*- 707131010Sgad * The user can specify a device via one of three formats: 708131010Sgad * 1) fully qualified, e.g.: /dev/ttyp0 /dev/console 709131010Sgad * 2) missing "/dev", e.g.: ttyp0 console 710131010Sgad * 3) two-letters, e.g.: p0 co 711131010Sgad * (matching letters that would be seen in the "TT" column) 712131010Sgad */ 713127499Sgadstatic int 714127499Sgadaddelem_tty(struct listinfo *inf, const char *elem) 715127499Sgad{ 716127539Sgad const char *ttypath; 717127539Sgad struct stat sb; 718131209Sgad char pathbuf[PATH_MAX], pathbuf2[PATH_MAX]; 719127499Sgad 720131010Sgad ttypath = NULL; 721131209Sgad pathbuf2[0] = '\0'; 722131010Sgad switch (*elem) { 723131010Sgad case '/': 724127499Sgad ttypath = elem; 725131010Sgad break; 726131010Sgad case 'c': 727131010Sgad if (strcmp(elem, "co") == 0) { 728131010Sgad ttypath = _PATH_CONSOLE; 729131010Sgad break; 730131010Sgad } 731131010Sgad /* FALLTHROUGH */ 732131010Sgad default: 733131010Sgad strlcpy(pathbuf, _PATH_DEV, sizeof(pathbuf)); 734131010Sgad strlcat(pathbuf, elem, sizeof(pathbuf)); 735131010Sgad ttypath = pathbuf; 736131209Sgad if (strncmp(pathbuf, _PATH_TTY, strlen(_PATH_TTY)) == 0) 737131010Sgad break; 738131010Sgad if (strcmp(pathbuf, _PATH_CONSOLE) == 0) 739131010Sgad break; 740131209Sgad /* Check to see if /dev/tty${elem} exists */ 741131209Sgad strlcpy(pathbuf2, _PATH_TTY, sizeof(pathbuf2)); 742131209Sgad strlcat(pathbuf2, elem, sizeof(pathbuf2)); 743131209Sgad if (stat(pathbuf2, &sb) == 0 && S_ISCHR(sb.st_mode)) { 744131010Sgad /* No need to repeat stat() && S_ISCHR() checks */ 745131010Sgad ttypath = NULL; 746131010Sgad break; 747131010Sgad } 748131010Sgad break; 749127499Sgad } 750131010Sgad if (ttypath) { 751131010Sgad if (stat(ttypath, &sb) == -1) { 752131209Sgad if (pathbuf2[0] != '\0') 753131209Sgad warn("%s and %s", pathbuf2, ttypath); 754131209Sgad else 755131209Sgad warn("%s", ttypath); 756131010Sgad optfatal = 1; 757131010Sgad return (0); 758131010Sgad } 759131010Sgad if (!S_ISCHR(sb.st_mode)) { 760131209Sgad if (pathbuf2[0] != '\0') 761131209Sgad warnx("%s and %s: Not a terminal", pathbuf2, 762131209Sgad ttypath); 763131209Sgad else 764131209Sgad warnx("%s: Not a terminal", ttypath); 765131010Sgad optfatal = 1; 766131010Sgad return (0); 767131010Sgad } 768127499Sgad } 769127499Sgad if (inf->count >= inf->maxcount) 770127499Sgad expand_list(inf); 771127823Sgad inf->l.ttys[(inf->count)++] = sb.st_rdev; 772127499Sgad return (1); 773127149Sgad} 774127149Sgad 775127499Sgadstatic int 776127499Sgadaddelem_uid(struct listinfo *inf, const char *elem) 77766377Sbrian{ 77866377Sbrian struct passwd *pwd; 779127539Sgad char *endp; 780127602Sgad u_long bigtemp; 78166377Sbrian 782127499Sgad if (*elem == '\0' || strlen(elem) >= MAXLOGNAME) { 783127499Sgad if (*elem == '\0') 784127499Sgad warnx("Invalid (zero-length) %s name", inf->lname); 785127499Sgad else 786127499Sgad warnx("%s name too long: %s", inf->lname, elem); 787127499Sgad optfatal = 1; 788127542Sgad return (0); /* Do not add this value. */ 789127499Sgad } 79066377Sbrian 791127499Sgad pwd = getpwnam(elem); 792127499Sgad if (pwd == NULL) { 793127499Sgad errno = 0; 794127602Sgad bigtemp = strtoul(elem, &endp, 10); 795127602Sgad if (errno != 0 || *endp != '\0' || bigtemp > UID_MAX) 796127499Sgad warnx("No %s named '%s'", inf->lname, elem); 797127499Sgad else { 798127499Sgad /* The string is all digits, so it might be a userID. */ 799127602Sgad pwd = getpwuid((uid_t)bigtemp); 800127499Sgad if (pwd == NULL) 801127499Sgad warnx("No %s name or ID matches '%s'", 802127499Sgad inf->lname, elem); 80366377Sbrian } 804127499Sgad } 805127499Sgad if (pwd == NULL) { 806127509Sgad /* 807127509Sgad * These used to be treated as minor warnings (and the 808127509Sgad * option was simply ignored), but now they are fatal 809127509Sgad * errors (and the command will be aborted). 810127509Sgad */ 811127509Sgad optfatal = 1; 812129967Sgad return (0); 813127499Sgad } 814127499Sgad if (inf->count >= inf->maxcount) 815127499Sgad expand_list(inf); 816127823Sgad inf->l.uids[(inf->count)++] = pwd->pw_uid; 817127499Sgad return (1); 818127499Sgad} 819127499Sgad 820127499Sgadstatic void 821127499Sgadadd_list(struct listinfo *inf, const char *argp) 822127499Sgad{ 823127499Sgad const char *savep; 824127499Sgad char *cp, *endp; 825127499Sgad int toolong; 826127539Sgad char elemcopy[PATH_MAX]; 827127499Sgad 828129917Sgad if (*argp == 0) 829129967Sgad inf->addelem(inf, elemcopy); 830127499Sgad while (*argp != '\0') { 831127499Sgad while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 832127499Sgad argp++; 833127499Sgad savep = argp; 834127499Sgad toolong = 0; 835127499Sgad cp = elemcopy; 836127499Sgad if (strchr(T_SEP, *argp) == NULL) { 837127499Sgad endp = elemcopy + sizeof(elemcopy) - 1; 838127499Sgad while (*argp != '\0' && cp <= endp && 839127499Sgad strchr(W_SEP T_SEP, *argp) == NULL) 840127499Sgad *cp++ = *argp++; 841127499Sgad if (cp > endp) 842127499Sgad toolong = 1; 84366377Sbrian } 844127499Sgad if (!toolong) { 845127499Sgad *cp = '\0'; 846127542Sgad /* 847129953Sgad * Add this single element to the given list. 848127542Sgad */ 849127499Sgad inf->addelem(inf, elemcopy); 850127499Sgad } else { 851127499Sgad /* 852127499Sgad * The string is too long to copy. Find the end 853127499Sgad * of the string to print out the warning message. 854127499Sgad */ 855127499Sgad while (*argp != '\0' && strchr(W_SEP T_SEP, 856127499Sgad *argp) == NULL) 857127499Sgad argp++; 858127499Sgad warnx("Value too long: %.*s", (int)(argp - savep), 859127499Sgad savep); 860127499Sgad optfatal = 1; 86166377Sbrian } 862127499Sgad /* 863127499Sgad * Skip over any number of trailing whitespace characters, 864127499Sgad * but only one (at most) trailing element-terminating 865127499Sgad * character. 866127499Sgad */ 867127499Sgad while (*argp != '\0' && strchr(W_SEP, *argp) != NULL) 868127499Sgad argp++; 869127499Sgad if (*argp != '\0' && strchr(T_SEP, *argp) != NULL) { 870127499Sgad argp++; 871127499Sgad /* Catch case where string ended with a comma. */ 872127499Sgad if (*argp == '\0') 873127499Sgad inf->addelem(inf, argp); 874127499Sgad } 87566377Sbrian } 876127499Sgad} 87766377Sbrian 878127499Sgadstatic void * 879127499Sgadexpand_list(struct listinfo *inf) 880127499Sgad{ 881127539Sgad void *newlist; 882127499Sgad int newmax; 88366377Sbrian 884127499Sgad newmax = (inf->maxcount + 1) << 1; 885127823Sgad newlist = realloc(inf->l.ptr, newmax * inf->elemsize); 886127499Sgad if (newlist == NULL) { 887127823Sgad free(inf->l.ptr); 888129967Sgad errx(1, "realloc to %d %ss failed", newmax, inf->lname); 889127499Sgad } 890127499Sgad inf->maxcount = newmax; 891127823Sgad inf->l.ptr = newlist; 892127499Sgad 893127499Sgad return (newlist); 89466377Sbrian} 89566377Sbrian 896127499Sgadstatic void 897127499Sgadfree_list(struct listinfo *inf) 898127499Sgad{ 899127499Sgad 900127499Sgad inf->count = inf->elemsize = inf->maxcount = 0; 901127823Sgad if (inf->l.ptr != NULL) 902127823Sgad free(inf->l.ptr); 903127499Sgad inf->addelem = NULL; 904127499Sgad inf->lname = NULL; 905127823Sgad inf->l.ptr = NULL; 906127499Sgad} 907127499Sgad 908127499Sgadstatic void 909127499Sgadinit_list(struct listinfo *inf, addelem_rtn artn, int elemsize, 910127499Sgad const char *lname) 911127499Sgad{ 912127499Sgad 913127499Sgad inf->count = inf->maxcount = 0; 914127499Sgad inf->elemsize = elemsize; 915127499Sgad inf->addelem = artn; 916127499Sgad inf->lname = lname; 917127823Sgad inf->l.ptr = NULL; 918127499Sgad} 919127499Sgad 920109502SjmallettVARENT * 921109502Sjmallettfind_varentry(VAR *v) 922109502Sjmallett{ 923109502Sjmallett struct varent *vent; 924109502Sjmallett 925130999Sgad STAILQ_FOREACH(vent, &varlist, next_ve) { 926109502Sjmallett if (strcmp(vent->var->name, v->name) == 0) 927109502Sjmallett return vent; 928109502Sjmallett } 929109502Sjmallett return NULL; 930109502Sjmallett} 931109502Sjmallett 9321556Srgrimesstatic void 93390110Simpscanvars(void) 9341556Srgrimes{ 9351556Srgrimes struct varent *vent; 9361556Srgrimes VAR *v; 93725271Sjkh 938130999Sgad STAILQ_FOREACH(vent, &varlist, next_ve) { 93925271Sjkh v = vent->var; 94025271Sjkh if (v->flag & DSIZ) { 94125271Sjkh v->dwidth = v->width; 94225271Sjkh v->width = 0; 94325271Sjkh } 94425271Sjkh if (v->flag & USER) 94525271Sjkh needuser = 1; 94625271Sjkh if (v->flag & COMM) 94725271Sjkh needcomm = 1; 94825271Sjkh } 94925271Sjkh} 95025271Sjkh 95125271Sjkhstatic void 95290110Simpdynsizevars(KINFO *ki) 95325271Sjkh{ 95425271Sjkh struct varent *vent; 95525271Sjkh VAR *v; 9561556Srgrimes int i; 9571556Srgrimes 958130999Sgad STAILQ_FOREACH(vent, &varlist, next_ve) { 9591556Srgrimes v = vent->var; 96025271Sjkh if (!(v->flag & DSIZ)) 96125271Sjkh continue; 96225271Sjkh i = (v->sproc)( ki); 96325271Sjkh if (v->width < i) 96425271Sjkh v->width = i; 96525271Sjkh if (v->width > v->dwidth) 96625271Sjkh v->width = v->dwidth; 96725271Sjkh } 96825271Sjkh} 96925271Sjkh 97025271Sjkhstatic void 97190110Simpsizevars(void) 97225271Sjkh{ 97325271Sjkh struct varent *vent; 97425271Sjkh VAR *v; 97525271Sjkh int i; 97625271Sjkh 977130999Sgad STAILQ_FOREACH(vent, &varlist, next_ve) { 97825271Sjkh v = vent->var; 979109504Sjmallett i = strlen(vent->header); 9801556Srgrimes if (v->width < i) 9811556Srgrimes v->width = i; 9821556Srgrimes totwidth += v->width + 1; /* +1 for space */ 9831556Srgrimes } 9841556Srgrimes totwidth--; 9851556Srgrimes} 9861556Srgrimes 98790143Smarkmstatic const char * 98890110Simpfmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 989127542Sgad char *comm, int maxlen) 9901556Srgrimes{ 99190143Smarkm const char *s; 9921556Srgrimes 99390143Smarkm s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen); 9941556Srgrimes return (s); 9951556Srgrimes} 9961556Srgrimes 99771578Sjhb#define UREADOK(ki) (forceuread || (ki->ki_p->ki_sflag & PS_INMEM)) 99831552Sdyson 9991556Srgrimesstatic void 100090110Simpsaveuser(KINFO *ki) 10011556Srgrimes{ 10021556Srgrimes 100371578Sjhb if (ki->ki_p->ki_sflag & PS_INMEM) { 10041556Srgrimes /* 10051556Srgrimes * The u-area might be swapped out, and we can't get 10061556Srgrimes * at it because we have a crashdump and no swap. 10071556Srgrimes * If it's here fill in these fields, otherwise, just 10081556Srgrimes * leave them 0. 10091556Srgrimes */ 101069896Smckusick ki->ki_valid = 1; 10111556Srgrimes } else 101269896Smckusick ki->ki_valid = 0; 10131556Srgrimes /* 10141556Srgrimes * save arguments if needed 10151556Srgrimes */ 1016130991Sgad if (needcomm) { 1017130991Sgad if (ki->ki_p->ki_stat == SZOMB) 1018130991Sgad ki->ki_args = strdup("<defunct>"); 1019130991Sgad else if (UREADOK(ki) || (ki->ki_p->ki_args != NULL)) 1020130991Sgad ki->ki_args = strdup(fmt(kvm_getargv, ki, 1021130991Sgad ki->ki_p->ki_comm, MAXCOMLEN)); 1022130991Sgad else 1023130991Sgad asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm); 1024131024Sgad if (ki->ki_args == NULL) 1025130991Sgad errx(1, "malloc failed"); 102653276Speter } else { 102753276Speter ki->ki_args = NULL; 102853276Speter } 1029130991Sgad if (needenv) { 1030130991Sgad if (UREADOK(ki)) 1031130991Sgad ki->ki_env = strdup(fmt(kvm_getenvv, ki, 1032130991Sgad (char *)NULL, 0)); 1033130991Sgad else 1034130991Sgad ki->ki_env = strdup("()"); 1035130991Sgad if (ki->ki_env == NULL) 1036130991Sgad errx(1, "malloc failed"); 103753276Speter } else { 103853276Speter ki->ki_env = NULL; 103953276Speter } 10401556Srgrimes} 10411556Srgrimes 1042130816Sgad/* A macro used to improve the readability of pscomp(). */ 1043130816Sgad#define DIFF_RETURN(a, b, field) do { \ 1044130816Sgad if ((a)->field != (b)->field) \ 1045130816Sgad return (((a)->field < (b)->field) ? -1 : 1); \ 1046130816Sgad} while (0) 1047130816Sgad 10481556Srgrimesstatic int 104990110Simppscomp(const void *a, const void *b) 10501556Srgrimes{ 1051127596Sgad const KINFO *ka, *kb; 10521556Srgrimes 1053127596Sgad ka = a; 1054127596Sgad kb = b; 1055127596Sgad /* SORTCPU and SORTMEM are sorted in descending order. */ 1056130816Sgad if (sortby == SORTCPU) 1057130816Sgad DIFF_RETURN(kb, ka, ki_pcpu); 1058130816Sgad if (sortby == SORTMEM) 1059130816Sgad DIFF_RETURN(kb, ka, ki_memsize); 1060127596Sgad /* 1061127596Sgad * TTY's are sorted in ascending order, except that all NODEV 1062127596Sgad * processes come before all processes with a device. 1063127596Sgad */ 1064130816Sgad if (ka->ki_p->ki_tdev != kb->ki_p->ki_tdev) { 1065130816Sgad if (ka->ki_p->ki_tdev == NODEV) 1066130816Sgad return (-1); 1067130816Sgad if (kb->ki_p->ki_tdev == NODEV) 1068130816Sgad return (1); 1069130816Sgad DIFF_RETURN(ka, kb, ki_p->ki_tdev); 1070130816Sgad } 1071130816Sgad 1072130972Sgad /* PID's and TID's (threads) are sorted in ascending order. */ 1073130816Sgad DIFF_RETURN(ka, kb, ki_p->ki_pid); 1074130972Sgad DIFF_RETURN(ka, kb, ki_p->ki_tid); 1075127596Sgad return (0); 10761556Srgrimes} 1077130816Sgad#undef DIFF_RETURN 10781556Srgrimes 10791556Srgrimes/* 10801556Srgrimes * ICK (all for getopt), would rather hide the ugliness 10811556Srgrimes * here than taint the main code. 10821556Srgrimes * 10831556Srgrimes * ps foo -> ps -foo 10841556Srgrimes * ps 34 -> ps -p34 10851556Srgrimes * 10861556Srgrimes * The old convention that 't' with no trailing tty arg means the users 10871556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 10881556Srgrimes * feature is available with the option 'T', which takes no argument. 10891556Srgrimes */ 10901556Srgrimesstatic char * 1091129915Sgadkludge_oldps_options(const char *optlist, char *origval, const char *nextarg) 10921556Srgrimes{ 10931556Srgrimes size_t len; 1094129914Sgad char *argp, *cp, *newopts, *ns, *optp, *pidp; 10951556Srgrimes 1096102886Sjmallett /* 1097129914Sgad * See if the original value includes any option which takes an 1098129914Sgad * argument (and will thus use up the remainder of the string). 1099102886Sjmallett */ 1100129914Sgad argp = NULL; 1101129914Sgad if (optlist != NULL) { 1102129914Sgad for (cp = origval; *cp != '\0'; cp++) { 1103129914Sgad optp = strchr(optlist, *cp); 1104129914Sgad if ((optp != NULL) && *(optp + 1) == ':') { 1105129914Sgad argp = cp; 1106129914Sgad break; 1107129914Sgad } 1108129914Sgad } 1109129914Sgad } 1110129914Sgad if (argp != NULL && *origval == '-') 1111129914Sgad return (origval); 1112102886Sjmallett 11131556Srgrimes /* 11141556Srgrimes * if last letter is a 't' flag with no argument (in the context 11151556Srgrimes * of the oldps options -- option string NOT starting with a '-' -- 11161556Srgrimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 111781743Sbrian * 1118129634Sgad * However, if a flag accepting a string argument is found earlier 1119129634Sgad * in the option string (including a possible `t' flag), then the 1120129634Sgad * remainder of the string must be the argument to that flag; so 1121129914Sgad * do not modify that argument. Note that a trailing `t' would 1122129914Sgad * cause argp to be set, if argp was not already set by some 1123129914Sgad * earlier option. 11241556Srgrimes */ 1125129914Sgad len = strlen(origval); 1126129914Sgad cp = origval + len - 1; 1127129914Sgad pidp = NULL; 1128129915Sgad if (*cp == 't' && *origval != '-' && cp == argp) { 1129129915Sgad if (nextarg == NULL || *nextarg == '-' || isdigitch(*nextarg)) 1130129915Sgad *cp = 'T'; 1131129915Sgad } else if (argp == NULL) { 11321556Srgrimes /* 1133129914Sgad * The original value did not include any option which takes 1134129914Sgad * an argument (and that would include `p' and `t'), so check 1135129914Sgad * the value for trailing number, or comma-separated list of 1136129914Sgad * numbers, which we will treat as a pid request. 11371556Srgrimes */ 1138129914Sgad if (isdigitch(*cp)) { 1139129914Sgad while (cp >= origval && (*cp == ',' || isdigitch(*cp))) 1140129914Sgad --cp; 1141129914Sgad pidp = cp + 1; 1142129914Sgad } 11431556Srgrimes } 1144129914Sgad 11451556Srgrimes /* 1146129914Sgad * If nothing needs to be added to the string, then return 1147129914Sgad * the "original" (although possibly modified) value. 11481556Srgrimes */ 1149129914Sgad if (*origval == '-' && pidp == NULL) 1150129914Sgad return (origval); 1151129914Sgad 1152129914Sgad /* 1153129914Sgad * Create a copy of the string to add '-' and/or 'p' to the 1154129914Sgad * original value. 1155129914Sgad */ 1156129914Sgad if ((newopts = ns = malloc(len + 3)) == NULL) 1157129914Sgad errx(1, "malloc failed"); 1158129914Sgad 1159129914Sgad if (*origval != '-') 1160129914Sgad *ns++ = '-'; /* add option flag */ 1161129914Sgad 1162129914Sgad if (pidp == NULL) 1163129914Sgad strcpy(ns, origval); 1164129914Sgad else { 1165129914Sgad /* 1166129914Sgad * Copy everything before the pid string, add the `p', 1167129914Sgad * and then copy the pid string. 1168129914Sgad */ 1169129914Sgad len = pidp - origval; 1170129914Sgad memcpy(ns, origval, len); 1171129914Sgad ns += len; 11721556Srgrimes *ns++ = 'p'; 1173129914Sgad strcpy(ns, pidp); 1174129914Sgad } 11751556Srgrimes 11761556Srgrimes return (newopts); 11771556Srgrimes} 11781556Srgrimes 1179137696Scsjpstatic int 1180137696Scsjpcheck_procfs(void) 1181137696Scsjp{ 1182137890Scsjp struct statfs mnt; 1183137696Scsjp 1184137890Scsjp if (statfs("/proc", &mnt) < 0) 1185137890Scsjp return (0); 1186137890Scsjp if (strcmp(mnt.f_fstypename, "procfs") != 0) 1187137890Scsjp return (0); 1188137890Scsjp return (1); 1189137696Scsjp} 1190137696Scsjp 11911556Srgrimesstatic void 119290110Simpusage(void) 11931556Srgrimes{ 1194141578Sru#define SINGLE_OPTS "[-aCce" OPT_LAZY_f "HhjlmrSTuvwXxZ]" 11951556Srgrimes 1196127499Sgad (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", 1197141578Sru "usage: ps " SINGLE_OPTS " [-O fmt | -o fmt] [-G gid[,gid...]]", 1198127499Sgad " [-M core] [-N system]", 1199141578Sru " [-p pid[,pid...]] [-t tty[,tty...]] [-U user[,user...]]", 120026465Scharnier " ps [-L]"); 12011556Srgrimes exit(1); 12021556Srgrimes} 1203