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 * 3. All advertising materials mentioning features or use of this software 144b88c807SRodney W. Grimes * must display the following acknowledgement: 154b88c807SRodney W. Grimes * This product includes software developed by the University of 164b88c807SRodney W. Grimes * California, Berkeley and its contributors. 174b88c807SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 184b88c807SRodney W. Grimes * may be used to endorse or promote products derived from this software 194b88c807SRodney W. Grimes * without specific prior written permission. 204b88c807SRodney W. Grimes * 214b88c807SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 224b88c807SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 234b88c807SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 244b88c807SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 254b88c807SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 264b88c807SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 274b88c807SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 284b88c807SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 294b88c807SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 304b88c807SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 314b88c807SRodney W. Grimes * SUCH DAMAGE. 324b88c807SRodney W. Grimes */ 334b88c807SRodney W. Grimes 344b88c807SRodney W. Grimes #ifndef lint 3578b09ffeSSteve Price static char const copyright[] = 364b88c807SRodney W. Grimes "@(#) Copyright (c) 1990, 1993, 1994\n\ 374b88c807SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 384b88c807SRodney W. Grimes #endif /* not lint */ 394b88c807SRodney W. Grimes 404b88c807SRodney W. Grimes #ifndef lint 41c9a8d1f4SPhilippe Charnier #if 0 42c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 43c9a8d1f4SPhilippe Charnier #endif 44c9a8d1f4SPhilippe Charnier static const char rcsid[] = 452a456239SPeter Wemm "$FreeBSD$"; 464b88c807SRodney W. Grimes #endif /* not lint */ 474b88c807SRodney W. Grimes 484b88c807SRodney W. Grimes #include <sys/param.h> 498b6f5f5fSDavid Greenman #include <sys/user.h> 504b88c807SRodney W. Grimes #include <sys/time.h> 514b88c807SRodney W. Grimes #include <sys/resource.h> 524b88c807SRodney W. Grimes #include <sys/stat.h> 534b88c807SRodney W. Grimes #include <sys/ioctl.h> 544b88c807SRodney W. Grimes #include <sys/sysctl.h> 554b88c807SRodney W. Grimes 564b88c807SRodney W. Grimes #include <ctype.h> 574b88c807SRodney W. Grimes #include <err.h> 584b88c807SRodney W. Grimes #include <errno.h> 594b88c807SRodney W. Grimes #include <fcntl.h> 604b88c807SRodney W. Grimes #include <kvm.h> 61b2496d93SMike Pritchard #include <limits.h> 6208017519SAndrey A. Chernov #include <locale.h> 634b88c807SRodney W. Grimes #include <nlist.h> 644b88c807SRodney W. Grimes #include <paths.h> 654b88c807SRodney W. Grimes #include <stdio.h> 664b88c807SRodney W. Grimes #include <stdlib.h> 674b88c807SRodney W. Grimes #include <string.h> 684b88c807SRodney W. Grimes #include <unistd.h> 6973eb8310SPeter Wemm #include <pwd.h> 70cf22dcfcSBrian Somers #include <utmp.h> 714b88c807SRodney W. Grimes 72f1ff35aeSMaxim Sobolev #include "lomac.h" 734b88c807SRodney W. Grimes #include "ps.h" 744b88c807SRodney W. Grimes 75cf22dcfcSBrian Somers #define SEP ", \t" /* username separators */ 76cf22dcfcSBrian Somers 774b88c807SRodney W. Grimes KINFO *kinfo; 784b88c807SRodney W. Grimes struct varent *vhead, *vtail; 794b88c807SRodney W. Grimes 804b88c807SRodney W. Grimes int eval; /* exit value */ 81db91faacSPeter Wemm int cflag; /* -c */ 824b88c807SRodney W. Grimes int rawcpu; /* -C */ 834b88c807SRodney W. Grimes int sumrusage; /* -S */ 844b88c807SRodney W. Grimes int termwidth; /* width of screen (0 == infinity) */ 854b88c807SRodney W. Grimes int totwidth; /* calculated width of requested variables */ 864b88c807SRodney W. Grimes 874b88c807SRodney W. Grimes static int needuser, needcomm, needenv; 883ac5e955SJohn Dyson #if defined(LAZY_PS) 893ac5e955SJohn Dyson static int forceuread=0; 903ac5e955SJohn Dyson #else 913ac5e955SJohn Dyson static int forceuread=1; 923ac5e955SJohn Dyson #endif 934b88c807SRodney W. Grimes 944b88c807SRodney W. Grimes enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 954b88c807SRodney W. Grimes 9646251ddeSWarner Losh static char *fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 9746251ddeSWarner Losh KINFO *, char *, int); 9846251ddeSWarner Losh static char *kludge_oldps_options(char *); 9946251ddeSWarner Losh static int pscomp(const void *, const void *); 10046251ddeSWarner Losh static void saveuser(KINFO *); 10146251ddeSWarner Losh static void scanvars(void); 10246251ddeSWarner Losh static void dynsizevars(KINFO *); 10346251ddeSWarner Losh static void sizevars(void); 10446251ddeSWarner Losh static void usage(void); 105cf22dcfcSBrian Somers static uid_t *getuids(const char *, int *); 1064b88c807SRodney W. Grimes 1074b88c807SRodney W. Grimes char dfmt[] = "pid tt state time command"; 10852f8369eSBrian Somers char jfmt[] = "user pid ppid pgid jobc state tt time command"; 1094b88c807SRodney W. Grimes char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command"; 1104b88c807SRodney W. Grimes char o1[] = "pid"; 1114b88c807SRodney W. Grimes char o2[] = "tt state time command"; 1124b88c807SRodney W. Grimes char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command"; 1134b88c807SRodney W. Grimes char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command"; 1147304f61fSBrian Feldman char Zfmt[] = "lvl"; 1154b88c807SRodney W. Grimes 1164b88c807SRodney W. Grimes kvm_t *kd; 1174b88c807SRodney W. Grimes 1184b88c807SRodney W. Grimes int 11946251ddeSWarner Losh main(int argc, char *argv[]) 1204b88c807SRodney W. Grimes { 1214b88c807SRodney W. Grimes struct kinfo_proc *kp; 1224b88c807SRodney W. Grimes struct varent *vent; 1234b88c807SRodney W. Grimes struct winsize ws; 1244b88c807SRodney W. Grimes dev_t ttydev; 1254b88c807SRodney W. Grimes pid_t pid; 126cf22dcfcSBrian Somers uid_t *uids; 12785082fc3SPoul-Henning Kamp int all, ch, flag, i, fmt, lineno, nentries, dropgid; 128cf22dcfcSBrian Somers int prtheader, wflag, what, xflg, uid, nuids; 129831c910aSRuslan Ermilov char *nlistf, *memf, errbuf[_POSIX2_LINE_MAX]; 1304b88c807SRodney W. Grimes 1312bf4b9cfSAndrey A. Chernov (void) setlocale(LC_ALL, ""); 1322bf4b9cfSAndrey A. Chernov 1334b88c807SRodney W. Grimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1344b88c807SRodney W. Grimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1354b88c807SRodney W. Grimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1364b88c807SRodney W. Grimes ws.ws_col == 0) 1374b88c807SRodney W. Grimes termwidth = 79; 1384b88c807SRodney W. Grimes else 1394b88c807SRodney W. Grimes termwidth = ws.ws_col - 1; 1404b88c807SRodney W. Grimes 1414b88c807SRodney W. Grimes if (argc > 1) 1424b88c807SRodney W. Grimes argv[1] = kludge_oldps_options(argv[1]); 1434b88c807SRodney W. Grimes 1444b88c807SRodney W. Grimes all = fmt = prtheader = wflag = xflg = 0; 1454b88c807SRodney W. Grimes pid = -1; 146cf22dcfcSBrian Somers nuids = 0; 147cf22dcfcSBrian Somers uids = NULL; 1484b88c807SRodney W. Grimes ttydev = NODEV; 14985082fc3SPoul-Henning Kamp dropgid = 0; 150831c910aSRuslan Ermilov memf = nlistf = _PATH_DEVNULL; 1514b88c807SRodney W. Grimes while ((ch = getopt(argc, argv, 1523ac5e955SJohn Dyson #if defined(LAZY_PS) 1539e1c120cSRuslan Ermilov "aCcefghjLlM:mN:O:o:p:rSTt:U:uvwxZ")) != -1) 1543ac5e955SJohn Dyson #else 1559e1c120cSRuslan Ermilov "aCceghjLlM:mN:O:o:p:rSTt:U:uvwxZ")) != -1) 1563ac5e955SJohn Dyson #endif 1574b88c807SRodney W. Grimes switch((char)ch) { 1584b88c807SRodney W. Grimes case 'a': 1594b88c807SRodney W. Grimes all = 1; 1604b88c807SRodney W. Grimes break; 1614b88c807SRodney W. Grimes case 'C': 1624b88c807SRodney W. Grimes rawcpu = 1; 1634b88c807SRodney W. Grimes break; 164db91faacSPeter Wemm case 'c': 165db91faacSPeter Wemm cflag = 1; 166db91faacSPeter Wemm break; 167db91faacSPeter Wemm case 'e': /* XXX set ufmt */ 168db91faacSPeter Wemm needenv = 1; 169db91faacSPeter Wemm break; 1704b88c807SRodney W. Grimes case 'g': 1714b88c807SRodney W. Grimes break; /* no-op */ 1724b88c807SRodney W. Grimes case 'h': 1734b88c807SRodney W. Grimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 1744b88c807SRodney W. Grimes break; 1754b88c807SRodney W. Grimes case 'j': 1764b88c807SRodney W. Grimes parsefmt(jfmt); 1774b88c807SRodney W. Grimes fmt = 1; 1784b88c807SRodney W. Grimes jfmt[0] = '\0'; 1794b88c807SRodney W. Grimes break; 1804b88c807SRodney W. Grimes case 'L': 1814b88c807SRodney W. Grimes showkey(); 1824b88c807SRodney W. Grimes exit(0); 1834b88c807SRodney W. Grimes case 'l': 1844b88c807SRodney W. Grimes parsefmt(lfmt); 1854b88c807SRodney W. Grimes fmt = 1; 1864b88c807SRodney W. Grimes lfmt[0] = '\0'; 1874b88c807SRodney W. Grimes break; 1884b88c807SRodney W. Grimes case 'M': 1894b88c807SRodney W. Grimes memf = optarg; 19085082fc3SPoul-Henning Kamp dropgid = 1; 1914b88c807SRodney W. Grimes break; 1924b88c807SRodney W. Grimes case 'm': 1934b88c807SRodney W. Grimes sortby = SORTMEM; 1944b88c807SRodney W. Grimes break; 1954b88c807SRodney W. Grimes case 'N': 1964b88c807SRodney W. Grimes nlistf = optarg; 19785082fc3SPoul-Henning Kamp dropgid = 1; 1984b88c807SRodney W. Grimes break; 1994b88c807SRodney W. Grimes case 'O': 2004b88c807SRodney W. Grimes parsefmt(o1); 2014b88c807SRodney W. Grimes parsefmt(optarg); 2024b88c807SRodney W. Grimes parsefmt(o2); 2034b88c807SRodney W. Grimes o1[0] = o2[0] = '\0'; 2044b88c807SRodney W. Grimes fmt = 1; 2054b88c807SRodney W. Grimes break; 2064b88c807SRodney W. Grimes case 'o': 2074b88c807SRodney W. Grimes parsefmt(optarg); 2084b88c807SRodney W. Grimes fmt = 1; 2094b88c807SRodney W. Grimes break; 2103ac5e955SJohn Dyson #if defined(LAZY_PS) 2113ac5e955SJohn Dyson case 'f': 2123ac5e955SJohn Dyson if (getuid() == 0 || getgid() == 0) 2133ac5e955SJohn Dyson forceuread = 1; 2143ac5e955SJohn Dyson break; 2153ac5e955SJohn Dyson #endif 2164b88c807SRodney W. Grimes case 'p': 2174b88c807SRodney W. Grimes pid = atol(optarg); 2184b88c807SRodney W. Grimes xflg = 1; 2194b88c807SRodney W. Grimes break; 2204b88c807SRodney W. Grimes case 'r': 2214b88c807SRodney W. Grimes sortby = SORTCPU; 2224b88c807SRodney W. Grimes break; 2234b88c807SRodney W. Grimes case 'S': 2244b88c807SRodney W. Grimes sumrusage = 1; 2254b88c807SRodney W. Grimes break; 2264b88c807SRodney W. Grimes case 'T': 2274b88c807SRodney W. Grimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 2284b88c807SRodney W. Grimes errx(1, "stdin: not a terminal"); 2294b88c807SRodney W. Grimes /* FALLTHROUGH */ 2304b88c807SRodney W. Grimes case 't': { 2314b88c807SRodney W. Grimes struct stat sb; 2325782e272SWarner Losh char *ttypath, pathbuf[PATH_MAX]; 2334b88c807SRodney W. Grimes 2344b88c807SRodney W. Grimes if (strcmp(optarg, "co") == 0) 2354b88c807SRodney W. Grimes ttypath = _PATH_CONSOLE; 2364b88c807SRodney W. Grimes else if (*optarg != '/') 2374b88c807SRodney W. Grimes (void)snprintf(ttypath = pathbuf, 2384b88c807SRodney W. Grimes sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); 2394b88c807SRodney W. Grimes else 2404b88c807SRodney W. Grimes ttypath = optarg; 2414b88c807SRodney W. Grimes if (stat(ttypath, &sb) == -1) 2424b88c807SRodney W. Grimes err(1, "%s", ttypath); 2434b88c807SRodney W. Grimes if (!S_ISCHR(sb.st_mode)) 2444b88c807SRodney W. Grimes errx(1, "%s: not a terminal", ttypath); 2454b88c807SRodney W. Grimes ttydev = sb.st_rdev; 2464b88c807SRodney W. Grimes break; 2474b88c807SRodney W. Grimes } 24873eb8310SPeter Wemm case 'U': 249cf22dcfcSBrian Somers uids = getuids(optarg, &nuids); 25073eb8310SPeter Wemm xflg++; /* XXX: intuitive? */ 25173eb8310SPeter Wemm break; 2524b88c807SRodney W. Grimes case 'u': 2534b88c807SRodney W. Grimes parsefmt(ufmt); 2544b88c807SRodney W. Grimes sortby = SORTCPU; 2554b88c807SRodney W. Grimes fmt = 1; 2564b88c807SRodney W. Grimes ufmt[0] = '\0'; 2574b88c807SRodney W. Grimes break; 2584b88c807SRodney W. Grimes case 'v': 2594b88c807SRodney W. Grimes parsefmt(vfmt); 2604b88c807SRodney W. Grimes sortby = SORTMEM; 2614b88c807SRodney W. Grimes fmt = 1; 2624b88c807SRodney W. Grimes vfmt[0] = '\0'; 2634b88c807SRodney W. Grimes break; 2644b88c807SRodney W. Grimes case 'w': 2654b88c807SRodney W. Grimes if (wflag) 2664b88c807SRodney W. Grimes termwidth = UNLIMITED; 2674b88c807SRodney W. Grimes else if (termwidth < 131) 2684b88c807SRodney W. Grimes termwidth = 131; 2694b88c807SRodney W. Grimes wflag++; 2704b88c807SRodney W. Grimes break; 2714b88c807SRodney W. Grimes case 'x': 2724b88c807SRodney W. Grimes xflg = 1; 2734b88c807SRodney W. Grimes break; 2747304f61fSBrian Feldman case 'Z': 2757304f61fSBrian Feldman parsefmt(Zfmt); 2767304f61fSBrian Feldman Zfmt[0] = '\0'; 2777304f61fSBrian Feldman break; 2784b88c807SRodney W. Grimes case '?': 2794b88c807SRodney W. Grimes default: 2804b88c807SRodney W. Grimes usage(); 2814b88c807SRodney W. Grimes } 2824b88c807SRodney W. Grimes argc -= optind; 2834b88c807SRodney W. Grimes argv += optind; 2844b88c807SRodney W. Grimes 2854b88c807SRodney W. Grimes #define BACKWARD_COMPATIBILITY 2864b88c807SRodney W. Grimes #ifdef BACKWARD_COMPATIBILITY 2874b88c807SRodney W. Grimes if (*argv) { 2884b88c807SRodney W. Grimes nlistf = *argv; 2894b88c807SRodney W. Grimes if (*++argv) { 2904b88c807SRodney W. Grimes memf = *argv; 2914b88c807SRodney W. Grimes } 2924b88c807SRodney W. Grimes } 2934b88c807SRodney W. Grimes #endif 2944b88c807SRodney W. Grimes /* 2954b88c807SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 2964b88c807SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 2974b88c807SRodney W. Grimes */ 29885082fc3SPoul-Henning Kamp if (dropgid) { 2994b88c807SRodney W. Grimes setgid(getgid()); 30085082fc3SPoul-Henning Kamp setuid(getuid()); 30185082fc3SPoul-Henning Kamp } 3024b88c807SRodney W. Grimes 303831c910aSRuslan Ermilov kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 3044b88c807SRodney W. Grimes if (kd == 0) 3054b88c807SRodney W. Grimes errx(1, "%s", errbuf); 3064b88c807SRodney W. Grimes 3074b88c807SRodney W. Grimes if (!fmt) 3084b88c807SRodney W. Grimes parsefmt(dfmt); 3094b88c807SRodney W. Grimes 31073eb8310SPeter Wemm /* XXX - should be cleaner */ 311cf22dcfcSBrian Somers if (!all && ttydev == NODEV && pid == -1 && !nuids) { 312cf22dcfcSBrian Somers if ((uids = malloc(sizeof (*uids))) == NULL) 313cf22dcfcSBrian Somers errx(1, "malloc: %s", strerror(errno)); 314cf22dcfcSBrian Somers nuids = 1; 315cf22dcfcSBrian Somers *uids = getuid(); 316cf22dcfcSBrian Somers } 3174b88c807SRodney W. Grimes 3184b88c807SRodney W. Grimes /* 3194b88c807SRodney W. Grimes * scan requested variables, noting what structures are needed, 320bdfebd84SKris Kennaway * and adjusting header widths as appropriate. 3214b88c807SRodney W. Grimes */ 3224b88c807SRodney W. Grimes scanvars(); 3234b88c807SRodney W. Grimes /* 3244b88c807SRodney W. Grimes * get proc list 3254b88c807SRodney W. Grimes */ 326cf22dcfcSBrian Somers if (nuids == 1) { 3274b88c807SRodney W. Grimes what = KERN_PROC_UID; 328cf22dcfcSBrian Somers flag = *uids; 3294b88c807SRodney W. Grimes } else if (ttydev != NODEV) { 3304b88c807SRodney W. Grimes what = KERN_PROC_TTY; 3314b88c807SRodney W. Grimes flag = ttydev; 3324b88c807SRodney W. Grimes } else if (pid != -1) { 3334b88c807SRodney W. Grimes what = KERN_PROC_PID; 3344b88c807SRodney W. Grimes flag = pid; 3354b88c807SRodney W. Grimes } else { 3364b88c807SRodney W. Grimes what = KERN_PROC_ALL; 3374b88c807SRodney W. Grimes flag = 0; 3384b88c807SRodney W. Grimes } 3394b88c807SRodney W. Grimes /* 3404b88c807SRodney W. Grimes * select procs 3414b88c807SRodney W. Grimes */ 342fbb75ab8SPeter Wemm if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0 || nentries < 0) 3434b88c807SRodney W. Grimes errx(1, "%s", kvm_geterr(kd)); 3444b88c807SRodney W. Grimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 3454b88c807SRodney W. Grimes err(1, NULL); 3464b88c807SRodney W. Grimes for (i = nentries; --i >= 0; ++kp) { 3474b88c807SRodney W. Grimes kinfo[i].ki_p = kp; 3484b88c807SRodney W. Grimes if (needuser) 3494b88c807SRodney W. Grimes saveuser(&kinfo[i]); 3506a2d726bSJordan K. Hubbard dynsizevars(&kinfo[i]); 3514b88c807SRodney W. Grimes } 3526a2d726bSJordan K. Hubbard 3536a2d726bSJordan K. Hubbard sizevars(); 3546a2d726bSJordan K. Hubbard 3554b88c807SRodney W. Grimes /* 3564b88c807SRodney W. Grimes * print header 3574b88c807SRodney W. Grimes */ 3584b88c807SRodney W. Grimes printheader(); 3594b88c807SRodney W. Grimes if (nentries == 0) 360f8c9c11cSWill Andrews exit(1); 3614b88c807SRodney W. Grimes /* 3624b88c807SRodney W. Grimes * sort proc list 3634b88c807SRodney W. Grimes */ 3644b88c807SRodney W. Grimes qsort(kinfo, nentries, sizeof(KINFO), pscomp); 3654b88c807SRodney W. Grimes /* 3664b88c807SRodney W. Grimes * for each proc, call each variable output function. 3674b88c807SRodney W. Grimes */ 3684b88c807SRodney W. Grimes for (i = lineno = 0; i < nentries; i++) { 3691f7d2501SKirk McKusick if (xflg == 0 && ((&kinfo[i])->ki_p->ki_tdev == NODEV || 3701f7d2501SKirk McKusick ((&kinfo[i])->ki_p->ki_flag & P_CONTROLT ) == 0)) 3714b88c807SRodney W. Grimes continue; 372cf22dcfcSBrian Somers if (nuids > 1) { 373cf22dcfcSBrian Somers for (uid = 0; uid < nuids; uid++) 3741f7d2501SKirk McKusick if ((&kinfo[i])->ki_p->ki_uid == uids[uid]) 375cf22dcfcSBrian Somers break; 376cf22dcfcSBrian Somers if (uid == nuids) 377cf22dcfcSBrian Somers continue; 378cf22dcfcSBrian Somers } 3794b88c807SRodney W. Grimes for (vent = vhead; vent; vent = vent->next) { 3804b88c807SRodney W. Grimes (vent->var->oproc)(&kinfo[i], vent); 3814b88c807SRodney W. Grimes if (vent->next != NULL) 3824b88c807SRodney W. Grimes (void)putchar(' '); 3834b88c807SRodney W. Grimes } 3844b88c807SRodney W. Grimes (void)putchar('\n'); 3854b88c807SRodney W. Grimes if (prtheader && lineno++ == prtheader - 4) { 3864b88c807SRodney W. Grimes (void)putchar('\n'); 3874b88c807SRodney W. Grimes printheader(); 3884b88c807SRodney W. Grimes lineno = 0; 3894b88c807SRodney W. Grimes } 3904b88c807SRodney W. Grimes } 391cf22dcfcSBrian Somers free(uids); 3927304f61fSBrian Feldman lomac_stop(); 393cf22dcfcSBrian Somers 3944b88c807SRodney W. Grimes exit(eval); 3954b88c807SRodney W. Grimes } 3964b88c807SRodney W. Grimes 397cf22dcfcSBrian Somers uid_t * 398cf22dcfcSBrian Somers getuids(const char *arg, int *nuids) 399cf22dcfcSBrian Somers { 400cf22dcfcSBrian Somers char name[UT_NAMESIZE + 1]; 401cf22dcfcSBrian Somers struct passwd *pwd; 402cf22dcfcSBrian Somers uid_t *uids, *moreuids; 403cf22dcfcSBrian Somers int l, alloc; 404cf22dcfcSBrian Somers 405cf22dcfcSBrian Somers 406cf22dcfcSBrian Somers alloc = 0; 407cf22dcfcSBrian Somers *nuids = 0; 408cf22dcfcSBrian Somers uids = NULL; 409cf22dcfcSBrian Somers for (; (l = strcspn(arg, SEP)) > 0; arg += l + strspn(arg + l, SEP)) { 410cf22dcfcSBrian Somers if (l >= sizeof name) { 411cf22dcfcSBrian Somers warnx("%.*s: name too long", l, arg); 412cf22dcfcSBrian Somers continue; 413cf22dcfcSBrian Somers } 414cf22dcfcSBrian Somers strncpy(name, arg, l); 415cf22dcfcSBrian Somers name[l] = '\0'; 416cf22dcfcSBrian Somers if ((pwd = getpwnam(name)) == NULL) { 417cf22dcfcSBrian Somers warnx("%s: no such user", name); 418cf22dcfcSBrian Somers continue; 419cf22dcfcSBrian Somers } 420cf22dcfcSBrian Somers if (*nuids >= alloc) { 421cf22dcfcSBrian Somers alloc = (alloc + 1) << 1; 422cf22dcfcSBrian Somers moreuids = realloc(uids, alloc * sizeof (*uids)); 423cf22dcfcSBrian Somers if (moreuids == NULL) { 424cf22dcfcSBrian Somers free(uids); 425cf22dcfcSBrian Somers errx(1, "realloc: %s", strerror(errno)); 426cf22dcfcSBrian Somers } 427cf22dcfcSBrian Somers uids = moreuids; 428cf22dcfcSBrian Somers } 429cf22dcfcSBrian Somers uids[(*nuids)++] = pwd->pw_uid; 430cf22dcfcSBrian Somers } 431cf22dcfcSBrian Somers endpwent(); 432cf22dcfcSBrian Somers 433cf22dcfcSBrian Somers if (!*nuids) 434cf22dcfcSBrian Somers errx(1, "No users specified"); 435cf22dcfcSBrian Somers 436cf22dcfcSBrian Somers return uids; 437cf22dcfcSBrian Somers } 438cf22dcfcSBrian Somers 4394b88c807SRodney W. Grimes static void 44046251ddeSWarner Losh scanvars(void) 4414b88c807SRodney W. Grimes { 4424b88c807SRodney W. Grimes struct varent *vent; 4434b88c807SRodney W. Grimes VAR *v; 4446a2d726bSJordan K. Hubbard 4456a2d726bSJordan K. Hubbard for (vent = vhead; vent; vent = vent->next) { 4466a2d726bSJordan K. Hubbard v = vent->var; 4476a2d726bSJordan K. Hubbard if (v->flag & DSIZ) { 4486a2d726bSJordan K. Hubbard v->dwidth = v->width; 4496a2d726bSJordan K. Hubbard v->width = 0; 4506a2d726bSJordan K. Hubbard } 4516a2d726bSJordan K. Hubbard if (v->flag & USER) 4526a2d726bSJordan K. Hubbard needuser = 1; 4536a2d726bSJordan K. Hubbard if (v->flag & COMM) 4546a2d726bSJordan K. Hubbard needcomm = 1; 4556a2d726bSJordan K. Hubbard } 4566a2d726bSJordan K. Hubbard } 4576a2d726bSJordan K. Hubbard 4586a2d726bSJordan K. Hubbard static void 45946251ddeSWarner Losh dynsizevars(KINFO *ki) 4606a2d726bSJordan K. Hubbard { 4616a2d726bSJordan K. Hubbard struct varent *vent; 4626a2d726bSJordan K. Hubbard VAR *v; 4636a2d726bSJordan K. Hubbard int i; 4646a2d726bSJordan K. Hubbard 4656a2d726bSJordan K. Hubbard for (vent = vhead; vent; vent = vent->next) { 4666a2d726bSJordan K. Hubbard v = vent->var; 4676a2d726bSJordan K. Hubbard if (!(v->flag & DSIZ)) 4686a2d726bSJordan K. Hubbard continue; 4696a2d726bSJordan K. Hubbard i = (v->sproc)( ki); 4706a2d726bSJordan K. Hubbard if (v->width < i) 4716a2d726bSJordan K. Hubbard v->width = i; 4726a2d726bSJordan K. Hubbard if (v->width > v->dwidth) 4736a2d726bSJordan K. Hubbard v->width = v->dwidth; 4746a2d726bSJordan K. Hubbard } 4756a2d726bSJordan K. Hubbard } 4766a2d726bSJordan K. Hubbard 4776a2d726bSJordan K. Hubbard static void 47846251ddeSWarner Losh sizevars(void) 4796a2d726bSJordan K. Hubbard { 4806a2d726bSJordan K. Hubbard struct varent *vent; 4816a2d726bSJordan K. Hubbard VAR *v; 4824b88c807SRodney W. Grimes int i; 4834b88c807SRodney W. Grimes 4844b88c807SRodney W. Grimes for (vent = vhead; vent; vent = vent->next) { 4854b88c807SRodney W. Grimes v = vent->var; 4864b88c807SRodney W. Grimes i = strlen(v->header); 4874b88c807SRodney W. Grimes if (v->width < i) 4884b88c807SRodney W. Grimes v->width = i; 4894b88c807SRodney W. Grimes totwidth += v->width + 1; /* +1 for space */ 4904b88c807SRodney W. Grimes } 4914b88c807SRodney W. Grimes totwidth--; 4924b88c807SRodney W. Grimes } 4934b88c807SRodney W. Grimes 4944b88c807SRodney W. Grimes static char * 49546251ddeSWarner Losh fmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 49646251ddeSWarner Losh char *comm, int maxlen) 4974b88c807SRodney W. Grimes { 4984b88c807SRodney W. Grimes char *s; 4994b88c807SRodney W. Grimes 5004b88c807SRodney W. Grimes if ((s = 5014b88c807SRodney W. Grimes fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL) 5024b88c807SRodney W. Grimes err(1, NULL); 5034b88c807SRodney W. Grimes return (s); 5044b88c807SRodney W. Grimes } 5054b88c807SRodney W. Grimes 506e0aa5ab7SJohn Baldwin #define UREADOK(ki) (forceuread || (ki->ki_p->ki_sflag & PS_INMEM)) 5073ac5e955SJohn Dyson 5084b88c807SRodney W. Grimes static void 50946251ddeSWarner Losh saveuser(KINFO *ki) 5104b88c807SRodney W. Grimes { 5114b88c807SRodney W. Grimes 512e0aa5ab7SJohn Baldwin if (ki->ki_p->ki_sflag & PS_INMEM) { 5134b88c807SRodney W. Grimes /* 5144b88c807SRodney W. Grimes * The u-area might be swapped out, and we can't get 5154b88c807SRodney W. Grimes * at it because we have a crashdump and no swap. 5164b88c807SRodney W. Grimes * If it's here fill in these fields, otherwise, just 5174b88c807SRodney W. Grimes * leave them 0. 5184b88c807SRodney W. Grimes */ 5191f7d2501SKirk McKusick ki->ki_valid = 1; 5204b88c807SRodney W. Grimes } else 5211f7d2501SKirk McKusick ki->ki_valid = 0; 5224b88c807SRodney W. Grimes /* 5234b88c807SRodney W. Grimes * save arguments if needed 5244b88c807SRodney W. Grimes */ 5251f7d2501SKirk McKusick if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) { 5261f7d2501SKirk McKusick ki->ki_args = fmt(kvm_getargv, ki, ki->ki_p->ki_comm, 5274b88c807SRodney W. Grimes MAXCOMLEN); 5283ac5e955SJohn Dyson } else if (needcomm) { 5291f7d2501SKirk McKusick ki->ki_args = malloc(strlen(ki->ki_p->ki_comm) + 3); 5301f7d2501SKirk McKusick sprintf(ki->ki_args, "(%s)", ki->ki_p->ki_comm); 5313ac5e955SJohn Dyson } else { 5324b88c807SRodney W. Grimes ki->ki_args = NULL; 5333ac5e955SJohn Dyson } 5343ac5e955SJohn Dyson if (needenv && UREADOK(ki)) { 5354b88c807SRodney W. Grimes ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0); 5363ac5e955SJohn Dyson } else if (needenv) { 5373ac5e955SJohn Dyson ki->ki_env = malloc(3); 5383ac5e955SJohn Dyson strcpy(ki->ki_env, "()"); 5393ac5e955SJohn Dyson } else { 5404b88c807SRodney W. Grimes ki->ki_env = NULL; 5414b88c807SRodney W. Grimes } 5423ac5e955SJohn Dyson } 5434b88c807SRodney W. Grimes 5444b88c807SRodney W. Grimes static int 54546251ddeSWarner Losh pscomp(const void *a, const void *b) 5464b88c807SRodney W. Grimes { 5474b88c807SRodney W. Grimes int i; 5481f7d2501SKirk McKusick #define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \ 5491f7d2501SKirk McKusick (k)->ki_p->ki_tsize) 5504b88c807SRodney W. Grimes 5514b88c807SRodney W. Grimes if (sortby == SORTCPU) 5524b88c807SRodney W. Grimes return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a)); 5534b88c807SRodney W. Grimes if (sortby == SORTMEM) 5544b88c807SRodney W. Grimes return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a)); 5551f7d2501SKirk McKusick i = ((KINFO *)a)->ki_p->ki_tdev - ((KINFO *)b)->ki_p->ki_tdev; 5564b88c807SRodney W. Grimes if (i == 0) 5571f7d2501SKirk McKusick i = ((KINFO *)a)->ki_p->ki_pid - ((KINFO *)b)->ki_p->ki_pid; 5584b88c807SRodney W. Grimes return (i); 5594b88c807SRodney W. Grimes } 5604b88c807SRodney W. Grimes 5614b88c807SRodney W. Grimes /* 5624b88c807SRodney W. Grimes * ICK (all for getopt), would rather hide the ugliness 5634b88c807SRodney W. Grimes * here than taint the main code. 5644b88c807SRodney W. Grimes * 5654b88c807SRodney W. Grimes * ps foo -> ps -foo 5664b88c807SRodney W. Grimes * ps 34 -> ps -p34 5674b88c807SRodney W. Grimes * 5684b88c807SRodney W. Grimes * The old convention that 't' with no trailing tty arg means the users 5694b88c807SRodney W. Grimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 5704b88c807SRodney W. Grimes * feature is available with the option 'T', which takes no argument. 5714b88c807SRodney W. Grimes */ 5724b88c807SRodney W. Grimes static char * 57346251ddeSWarner Losh kludge_oldps_options(char *s) 5744b88c807SRodney W. Grimes { 5754b88c807SRodney W. Grimes size_t len; 5764b88c807SRodney W. Grimes char *newopts, *ns, *cp; 5774b88c807SRodney W. Grimes 5784b88c807SRodney W. Grimes len = strlen(s); 5794b88c807SRodney W. Grimes if ((newopts = ns = malloc(len + 2)) == NULL) 5804b88c807SRodney W. Grimes err(1, NULL); 5814b88c807SRodney W. Grimes /* 5824b88c807SRodney W. Grimes * options begin with '-' 5834b88c807SRodney W. Grimes */ 5844b88c807SRodney W. Grimes if (*s != '-') 5854b88c807SRodney W. Grimes *ns++ = '-'; /* add option flag */ 5864b88c807SRodney W. Grimes /* 5874b88c807SRodney W. Grimes * gaze to end of argv[1] 5884b88c807SRodney W. Grimes */ 5894b88c807SRodney W. Grimes cp = s + len - 1; 5904b88c807SRodney W. Grimes /* 5914b88c807SRodney W. Grimes * if last letter is a 't' flag with no argument (in the context 5924b88c807SRodney W. Grimes * of the oldps options -- option string NOT starting with a '-' -- 5934b88c807SRodney W. Grimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 594380434d4SBrian Somers * 595380434d4SBrian Somers * However, if a flag accepting a string argument is found in the 596380434d4SBrian Somers * option string, the remainder of the string is the argument to 597380434d4SBrian Somers * that flag; do not modify that argument. 5984b88c807SRodney W. Grimes */ 599831c910aSRuslan Ermilov if (strcspn(s, "MNOoU") == len && *cp == 't' && *s != '-') 6004b88c807SRodney W. Grimes *cp = 'T'; 6014b88c807SRodney W. Grimes else { 6024b88c807SRodney W. Grimes /* 6034b88c807SRodney W. Grimes * otherwise check for trailing number, which *may* be a 6044b88c807SRodney W. Grimes * pid. 6054b88c807SRodney W. Grimes */ 6064b88c807SRodney W. Grimes while (cp >= s && isdigit(*cp)) 6074b88c807SRodney W. Grimes --cp; 6084b88c807SRodney W. Grimes } 6094b88c807SRodney W. Grimes cp++; 6104b88c807SRodney W. Grimes memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 6114b88c807SRodney W. Grimes ns += cp - s; 6124b88c807SRodney W. Grimes /* 6134b88c807SRodney W. Grimes * if there's a trailing number, and not a preceding 'p' (pid) or 6144b88c807SRodney W. Grimes * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 6154b88c807SRodney W. Grimes */ 6160fd510b7SJoerg Wunsch if (isdigit(*cp) && 6170fd510b7SJoerg Wunsch (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) && 6180fd510b7SJoerg Wunsch (cp - 1 == s || cp[-2] != 't')) 6194b88c807SRodney W. Grimes *ns++ = 'p'; 6204b88c807SRodney W. Grimes (void)strcpy(ns, cp); /* and append the number */ 6214b88c807SRodney W. Grimes 6224b88c807SRodney W. Grimes return (newopts); 6234b88c807SRodney W. Grimes } 6244b88c807SRodney W. Grimes 6254b88c807SRodney W. Grimes static void 62646251ddeSWarner Losh usage(void) 6274b88c807SRodney W. Grimes { 6284b88c807SRodney W. Grimes 629749d4bb6SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n%s\n", 630749d4bb6SPhilippe Charnier "usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty] [-U user]", 631831c910aSRuslan Ermilov " [-M core] [-N system]", 6324b88c807SRodney W. Grimes " ps [-L]"); 6334b88c807SRodney W. Grimes exit(1); 6344b88c807SRodney W. Grimes } 635