ps.c revision 31552
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 * 3. All advertising materials mentioning features or use of this software 141556Srgrimes * must display the following acknowledgement: 151556Srgrimes * This product includes software developed by the University of 161556Srgrimes * California, Berkeley and its contributors. 171556Srgrimes * 4. Neither the name of the University nor the names of its contributors 181556Srgrimes * may be used to endorse or promote products derived from this software 191556Srgrimes * without specific prior written permission. 201556Srgrimes * 211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241556Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311556Srgrimes * SUCH DAMAGE. 323044Sdg * 3331552Sdyson * $Id: ps.c,v 1.21 1997/08/03 08:25:01 peter Exp $ 341556Srgrimes */ 351556Srgrimes 361556Srgrimes#ifndef lint 3720420Sstevestatic char const copyright[] = 381556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\ 391556Srgrimes The Regents of the University of California. All rights reserved.\n"; 401556Srgrimes#endif /* not lint */ 411556Srgrimes 421556Srgrimes#ifndef lint 4320420Sstevestatic char const sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 441556Srgrimes#endif /* not lint */ 451556Srgrimes 461556Srgrimes#include <sys/param.h> 473296Sdg#include <sys/user.h> 481556Srgrimes#include <sys/time.h> 491556Srgrimes#include <sys/resource.h> 501556Srgrimes#include <sys/stat.h> 511556Srgrimes#include <sys/ioctl.h> 521556Srgrimes#include <sys/sysctl.h> 531556Srgrimes 541556Srgrimes#include <ctype.h> 551556Srgrimes#include <err.h> 561556Srgrimes#include <errno.h> 571556Srgrimes#include <fcntl.h> 581556Srgrimes#include <kvm.h> 5913514Smpp#include <limits.h> 601556Srgrimes#include <nlist.h> 611556Srgrimes#include <paths.h> 621556Srgrimes#include <stdio.h> 631556Srgrimes#include <stdlib.h> 641556Srgrimes#include <string.h> 651556Srgrimes#include <unistd.h> 6611744Sache#include <locale.h> 6713020Speter#include <pwd.h> 681556Srgrimes 691556Srgrimes#include "ps.h" 701556Srgrimes 711556SrgrimesKINFO *kinfo; 721556Srgrimesstruct varent *vhead, *vtail; 731556Srgrimes 741556Srgrimesint eval; /* exit value */ 7519068Speterint cflag; /* -c */ 761556Srgrimesint rawcpu; /* -C */ 771556Srgrimesint sumrusage; /* -S */ 781556Srgrimesint termwidth; /* width of screen (0 == infinity) */ 791556Srgrimesint totwidth; /* calculated width of requested variables */ 801556Srgrimes 811556Srgrimesstatic int needuser, needcomm, needenv; 8231552Sdyson#if defined(LAZY_PS) 8331552Sdysonstatic int forceuread=0; 8431552Sdyson#else 8531552Sdysonstatic int forceuread=1; 8631552Sdyson#endif 871556Srgrimes 881556Srgrimesenum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 891556Srgrimes 901556Srgrimesstatic char *fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int), 911556Srgrimes KINFO *, char *, int)); 921556Srgrimesstatic char *kludge_oldps_options __P((char *)); 931556Srgrimesstatic int pscomp __P((const void *, const void *)); 941556Srgrimesstatic void saveuser __P((KINFO *)); 951556Srgrimesstatic void scanvars __P((void)); 9625271Sjkhstatic void dynsizevars __P((KINFO *)); 9725271Sjkhstatic void sizevars __P((void)); 981556Srgrimesstatic void usage __P((void)); 991556Srgrimes 1001556Srgrimeschar dfmt[] = "pid tt state time command"; 1011556Srgrimeschar jfmt[] = "user pid ppid pgid sess jobc state tt time command"; 1021556Srgrimeschar lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command"; 1031556Srgrimeschar o1[] = "pid"; 1041556Srgrimeschar o2[] = "tt state time command"; 1051556Srgrimeschar ufmt[] = "user pid %cpu %mem vsz rss tt state start time command"; 1061556Srgrimeschar vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command"; 1071556Srgrimes 1081556Srgrimeskvm_t *kd; 1091556Srgrimes 1101556Srgrimesint 1111556Srgrimesmain(argc, argv) 1121556Srgrimes int argc; 1131556Srgrimes char *argv[]; 1141556Srgrimes{ 1151556Srgrimes struct kinfo_proc *kp; 1161556Srgrimes struct varent *vent; 1171556Srgrimes struct winsize ws; 11813020Speter struct passwd *pwd; 1191556Srgrimes dev_t ttydev; 1201556Srgrimes pid_t pid; 1211556Srgrimes uid_t uid; 1221556Srgrimes int all, ch, flag, i, fmt, lineno, nentries; 1231556Srgrimes int prtheader, wflag, what, xflg; 12413514Smpp char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX]; 1251556Srgrimes 12611809Sache (void) setlocale(LC_ALL, ""); 12711809Sache 1281556Srgrimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1291556Srgrimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1301556Srgrimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1311556Srgrimes ws.ws_col == 0) 1321556Srgrimes termwidth = 79; 1331556Srgrimes else 1341556Srgrimes termwidth = ws.ws_col - 1; 1351556Srgrimes 1361556Srgrimes if (argc > 1) 1371556Srgrimes argv[1] = kludge_oldps_options(argv[1]); 1381556Srgrimes 1391556Srgrimes all = fmt = prtheader = wflag = xflg = 0; 1401556Srgrimes pid = -1; 1411556Srgrimes uid = (uid_t) -1; 1421556Srgrimes ttydev = NODEV; 1431556Srgrimes memf = nlistf = swapf = NULL; 1441556Srgrimes while ((ch = getopt(argc, argv, 14531552Sdyson#if defined(LAZY_PS) 14631552Sdyson "aCcfeghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) 14731552Sdyson#else 14824348Simp "aCceghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) 14931552Sdyson#endif 1501556Srgrimes switch((char)ch) { 1511556Srgrimes case 'a': 1521556Srgrimes all = 1; 1531556Srgrimes break; 15419068Speter case 'C': 15519068Speter rawcpu = 1; 15619068Speter break; 15719068Speter case 'c': 15819068Speter cflag = 1; 15919068Speter break; 1601556Srgrimes case 'e': /* XXX set ufmt */ 1611556Srgrimes needenv = 1; 1621556Srgrimes break; 1631556Srgrimes case 'g': 1641556Srgrimes break; /* no-op */ 1651556Srgrimes case 'h': 1661556Srgrimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 1671556Srgrimes break; 1681556Srgrimes case 'j': 1691556Srgrimes parsefmt(jfmt); 1701556Srgrimes fmt = 1; 1711556Srgrimes jfmt[0] = '\0'; 1721556Srgrimes break; 1731556Srgrimes case 'L': 1741556Srgrimes showkey(); 1751556Srgrimes exit(0); 1761556Srgrimes case 'l': 1771556Srgrimes parsefmt(lfmt); 1781556Srgrimes fmt = 1; 1791556Srgrimes lfmt[0] = '\0'; 1801556Srgrimes break; 1811556Srgrimes case 'M': 1821556Srgrimes memf = optarg; 1831556Srgrimes break; 1841556Srgrimes case 'm': 1851556Srgrimes sortby = SORTMEM; 1861556Srgrimes break; 1871556Srgrimes case 'N': 1881556Srgrimes nlistf = optarg; 1891556Srgrimes break; 1901556Srgrimes case 'O': 1911556Srgrimes parsefmt(o1); 1921556Srgrimes parsefmt(optarg); 1931556Srgrimes parsefmt(o2); 1941556Srgrimes o1[0] = o2[0] = '\0'; 1951556Srgrimes fmt = 1; 1961556Srgrimes break; 1971556Srgrimes case 'o': 1981556Srgrimes parsefmt(optarg); 1991556Srgrimes fmt = 1; 2001556Srgrimes break; 20131552Sdyson#if defined(LAZY_PS) 20231552Sdyson case 'f': 20331552Sdyson if (getuid() == 0 || getgid() == 0) 20431552Sdyson forceuread = 1; 20531552Sdyson break; 20631552Sdyson#endif 2071556Srgrimes case 'p': 2081556Srgrimes pid = atol(optarg); 2091556Srgrimes xflg = 1; 2101556Srgrimes break; 2111556Srgrimes case 'r': 2121556Srgrimes sortby = SORTCPU; 2131556Srgrimes break; 2141556Srgrimes case 'S': 2151556Srgrimes sumrusage = 1; 2161556Srgrimes break; 2171556Srgrimes case 'T': 2181556Srgrimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 2191556Srgrimes errx(1, "stdin: not a terminal"); 2201556Srgrimes /* FALLTHROUGH */ 2211556Srgrimes case 't': { 2221556Srgrimes struct stat sb; 2231556Srgrimes char *ttypath, pathbuf[MAXPATHLEN]; 2241556Srgrimes 2251556Srgrimes if (strcmp(optarg, "co") == 0) 2261556Srgrimes ttypath = _PATH_CONSOLE; 2271556Srgrimes else if (*optarg != '/') 2281556Srgrimes (void)snprintf(ttypath = pathbuf, 2291556Srgrimes sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); 2301556Srgrimes else 2311556Srgrimes ttypath = optarg; 2321556Srgrimes if (stat(ttypath, &sb) == -1) 2331556Srgrimes err(1, "%s", ttypath); 2341556Srgrimes if (!S_ISCHR(sb.st_mode)) 2351556Srgrimes errx(1, "%s: not a terminal", ttypath); 2361556Srgrimes ttydev = sb.st_rdev; 2371556Srgrimes break; 2381556Srgrimes } 23913020Speter case 'U': 24013020Speter pwd = getpwnam(optarg); 24113020Speter if (pwd == NULL) 24213020Speter errx(1, "%s: no such user", optarg); 24313020Speter uid = pwd->pw_uid; 24413020Speter endpwent(); 24513020Speter xflg++; /* XXX: intuitive? */ 24613020Speter break; 2471556Srgrimes case 'u': 2481556Srgrimes parsefmt(ufmt); 2491556Srgrimes sortby = SORTCPU; 2501556Srgrimes fmt = 1; 2511556Srgrimes ufmt[0] = '\0'; 2521556Srgrimes break; 2531556Srgrimes case 'v': 2541556Srgrimes parsefmt(vfmt); 2551556Srgrimes sortby = SORTMEM; 2561556Srgrimes fmt = 1; 2571556Srgrimes vfmt[0] = '\0'; 2581556Srgrimes break; 2591556Srgrimes case 'W': 2601556Srgrimes swapf = optarg; 2611556Srgrimes break; 2621556Srgrimes case 'w': 2631556Srgrimes if (wflag) 2641556Srgrimes termwidth = UNLIMITED; 2651556Srgrimes else if (termwidth < 131) 2661556Srgrimes termwidth = 131; 2671556Srgrimes wflag++; 2681556Srgrimes break; 2691556Srgrimes case 'x': 2701556Srgrimes xflg = 1; 2711556Srgrimes break; 2721556Srgrimes case '?': 2731556Srgrimes default: 2741556Srgrimes usage(); 2751556Srgrimes } 2761556Srgrimes argc -= optind; 2771556Srgrimes argv += optind; 2781556Srgrimes 2791556Srgrimes#define BACKWARD_COMPATIBILITY 2801556Srgrimes#ifdef BACKWARD_COMPATIBILITY 2811556Srgrimes if (*argv) { 2821556Srgrimes nlistf = *argv; 2831556Srgrimes if (*++argv) { 2841556Srgrimes memf = *argv; 2851556Srgrimes if (*++argv) 2861556Srgrimes swapf = *argv; 2871556Srgrimes } 2881556Srgrimes } 2891556Srgrimes#endif 2901556Srgrimes /* 2911556Srgrimes * Discard setgid privileges if not the running kernel so that bad 2921556Srgrimes * guys can't print interesting stuff from kernel memory. 2931556Srgrimes */ 2941556Srgrimes if (nlistf != NULL || memf != NULL || swapf != NULL) 2951556Srgrimes setgid(getgid()); 2961556Srgrimes 2971556Srgrimes kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf); 2981556Srgrimes if (kd == 0) 2991556Srgrimes errx(1, "%s", errbuf); 3001556Srgrimes 3011556Srgrimes if (!fmt) 3021556Srgrimes parsefmt(dfmt); 3031556Srgrimes 30413020Speter /* XXX - should be cleaner */ 30513020Speter if (!all && ttydev == NODEV && pid == -1 && uid == (uid_t)-1) 3061556Srgrimes uid = getuid(); 3071556Srgrimes 3081556Srgrimes /* 3091556Srgrimes * scan requested variables, noting what structures are needed, 3101556Srgrimes * and adjusting header widths as appropiate. 3111556Srgrimes */ 3121556Srgrimes scanvars(); 3131556Srgrimes /* 3141556Srgrimes * get proc list 3151556Srgrimes */ 3161556Srgrimes if (uid != (uid_t) -1) { 3171556Srgrimes what = KERN_PROC_UID; 3181556Srgrimes flag = uid; 3191556Srgrimes } else if (ttydev != NODEV) { 3201556Srgrimes what = KERN_PROC_TTY; 3211556Srgrimes flag = ttydev; 3221556Srgrimes } else if (pid != -1) { 3231556Srgrimes what = KERN_PROC_PID; 3241556Srgrimes flag = pid; 3251556Srgrimes } else { 3261556Srgrimes what = KERN_PROC_ALL; 3271556Srgrimes flag = 0; 3281556Srgrimes } 3291556Srgrimes /* 3301556Srgrimes * select procs 3311556Srgrimes */ 3321556Srgrimes if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0) 3331556Srgrimes errx(1, "%s", kvm_geterr(kd)); 3341556Srgrimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 3351556Srgrimes err(1, NULL); 3361556Srgrimes for (i = nentries; --i >= 0; ++kp) { 3371556Srgrimes kinfo[i].ki_p = kp; 3381556Srgrimes if (needuser) 3391556Srgrimes saveuser(&kinfo[i]); 34025271Sjkh dynsizevars(&kinfo[i]); 3411556Srgrimes } 34225271Sjkh 34325271Sjkh sizevars(); 34425271Sjkh 3451556Srgrimes /* 3461556Srgrimes * print header 3471556Srgrimes */ 3481556Srgrimes printheader(); 3491556Srgrimes if (nentries == 0) 3501556Srgrimes exit(0); 3511556Srgrimes /* 3521556Srgrimes * sort proc list 3531556Srgrimes */ 3541556Srgrimes qsort(kinfo, nentries, sizeof(KINFO), pscomp); 3551556Srgrimes /* 3561556Srgrimes * for each proc, call each variable output function. 3571556Srgrimes */ 3581556Srgrimes for (i = lineno = 0; i < nentries; i++) { 3591556Srgrimes if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV || 3601556Srgrimes (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0)) 3611556Srgrimes continue; 3621556Srgrimes for (vent = vhead; vent; vent = vent->next) { 3631556Srgrimes (vent->var->oproc)(&kinfo[i], vent); 3641556Srgrimes if (vent->next != NULL) 3651556Srgrimes (void)putchar(' '); 3661556Srgrimes } 3671556Srgrimes (void)putchar('\n'); 3681556Srgrimes if (prtheader && lineno++ == prtheader - 4) { 3691556Srgrimes (void)putchar('\n'); 3701556Srgrimes printheader(); 3711556Srgrimes lineno = 0; 3721556Srgrimes } 3731556Srgrimes } 3741556Srgrimes exit(eval); 3751556Srgrimes} 3761556Srgrimes 3771556Srgrimesstatic void 3781556Srgrimesscanvars() 3791556Srgrimes{ 3801556Srgrimes struct varent *vent; 3811556Srgrimes VAR *v; 38225271Sjkh 38325271Sjkh for (vent = vhead; vent; vent = vent->next) { 38425271Sjkh v = vent->var; 38525271Sjkh if (v->flag & DSIZ) { 38625271Sjkh v->dwidth = v->width; 38725271Sjkh v->width = 0; 38825271Sjkh } 38925271Sjkh if (v->flag & USER) 39025271Sjkh needuser = 1; 39125271Sjkh if (v->flag & COMM) 39225271Sjkh needcomm = 1; 39325271Sjkh } 39425271Sjkh} 39525271Sjkh 39625271Sjkhstatic void 39725271Sjkhdynsizevars(ki) 39825271Sjkh KINFO *ki; 39925271Sjkh{ 40025271Sjkh struct varent *vent; 40125271Sjkh VAR *v; 4021556Srgrimes int i; 4031556Srgrimes 4041556Srgrimes for (vent = vhead; vent; vent = vent->next) { 4051556Srgrimes v = vent->var; 40625271Sjkh if (!(v->flag & DSIZ)) 40725271Sjkh continue; 40825271Sjkh i = (v->sproc)( ki); 40925271Sjkh if (v->width < i) 41025271Sjkh v->width = i; 41125271Sjkh if (v->width > v->dwidth) 41225271Sjkh v->width = v->dwidth; 41325271Sjkh } 41425271Sjkh} 41525271Sjkh 41625271Sjkhstatic void 41725271Sjkhsizevars() 41825271Sjkh{ 41925271Sjkh struct varent *vent; 42025271Sjkh VAR *v; 42125271Sjkh int i; 42225271Sjkh 42325271Sjkh for (vent = vhead; vent; vent = vent->next) { 42425271Sjkh v = vent->var; 4251556Srgrimes i = strlen(v->header); 4261556Srgrimes if (v->width < i) 4271556Srgrimes v->width = i; 4281556Srgrimes totwidth += v->width + 1; /* +1 for space */ 4291556Srgrimes } 4301556Srgrimes totwidth--; 4311556Srgrimes} 4321556Srgrimes 4331556Srgrimesstatic char * 4341556Srgrimesfmt(fn, ki, comm, maxlen) 4351556Srgrimes char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int)); 4361556Srgrimes KINFO *ki; 4371556Srgrimes char *comm; 4381556Srgrimes int maxlen; 4391556Srgrimes{ 4401556Srgrimes char *s; 4411556Srgrimes 4421556Srgrimes if ((s = 4431556Srgrimes fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL) 4441556Srgrimes err(1, NULL); 4451556Srgrimes return (s); 4461556Srgrimes} 4471556Srgrimes 44831552Sdyson#define UREADOK(ki) (forceuread || (KI_PROC(ki)->p_flag & P_INMEM)) 44931552Sdyson 4501556Srgrimesstatic void 4511556Srgrimessaveuser(ki) 4521556Srgrimes KINFO *ki; 4531556Srgrimes{ 4541556Srgrimes struct pstats pstats; 4551556Srgrimes struct usave *usp; 4563686Sdg struct user *u_addr = (struct user *)USRSTACK; 4571556Srgrimes 4581556Srgrimes usp = &ki->ki_u; 45931552Sdyson if (UREADOK(ki) && kvm_uread(kd, KI_PROC(ki), (unsigned long)&u_addr->u_stats, 4601556Srgrimes (char *)&pstats, sizeof(pstats)) == sizeof(pstats)) { 4611556Srgrimes /* 4621556Srgrimes * The u-area might be swapped out, and we can't get 4631556Srgrimes * at it because we have a crashdump and no swap. 4641556Srgrimes * If it's here fill in these fields, otherwise, just 4651556Srgrimes * leave them 0. 4661556Srgrimes */ 4671556Srgrimes usp->u_start = pstats.p_start; 4681556Srgrimes usp->u_ru = pstats.p_ru; 4691556Srgrimes usp->u_cru = pstats.p_cru; 4701556Srgrimes usp->u_valid = 1; 4711556Srgrimes } else 4721556Srgrimes usp->u_valid = 0; 4731556Srgrimes /* 4741556Srgrimes * save arguments if needed 4751556Srgrimes */ 47631552Sdyson if (needcomm && UREADOK(ki)) { 47731552Sdyson ki->ki_args = fmt(kvm_getargv, ki, KI_PROC(ki)->p_comm, 47831552Sdyson MAXCOMLEN); 47931552Sdyson } else if (needcomm) { 48031552Sdyson ki->ki_args = malloc(strlen(KI_PROC(ki)->p_comm) + 3); 48131552Sdyson sprintf(ki->ki_args, "(%s)", KI_PROC(ki)->p_comm); 48231552Sdyson } else { 48331552Sdyson ki->ki_args = NULL; 48431552Sdyson } 48531552Sdyson if (needenv && UREADOK(ki)) { 48631552Sdyson ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0); 48731552Sdyson } else if (needenv) { 48831552Sdyson ki->ki_env = malloc(3); 48931552Sdyson strcpy(ki->ki_env, "()"); 49031552Sdyson } else { 49131552Sdyson ki->ki_env = NULL; 49231552Sdyson } 4931556Srgrimes} 4941556Srgrimes 4951556Srgrimesstatic int 4961556Srgrimespscomp(a, b) 4971556Srgrimes const void *a, *b; 4981556Srgrimes{ 4991556Srgrimes int i; 5001556Srgrimes#define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \ 5011556Srgrimes KI_EPROC(k)->e_vm.vm_tsize) 5021556Srgrimes 5031556Srgrimes if (sortby == SORTCPU) 5041556Srgrimes return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a)); 5051556Srgrimes if (sortby == SORTMEM) 5061556Srgrimes return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a)); 5071556Srgrimes i = KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev; 5081556Srgrimes if (i == 0) 5091556Srgrimes i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid; 5101556Srgrimes return (i); 5111556Srgrimes} 5121556Srgrimes 5131556Srgrimes/* 5141556Srgrimes * ICK (all for getopt), would rather hide the ugliness 5151556Srgrimes * here than taint the main code. 5161556Srgrimes * 5171556Srgrimes * ps foo -> ps -foo 5181556Srgrimes * ps 34 -> ps -p34 5191556Srgrimes * 5201556Srgrimes * The old convention that 't' with no trailing tty arg means the users 5211556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 5221556Srgrimes * feature is available with the option 'T', which takes no argument. 5231556Srgrimes */ 5241556Srgrimesstatic char * 5251556Srgrimeskludge_oldps_options(s) 5261556Srgrimes char *s; 5271556Srgrimes{ 5281556Srgrimes size_t len; 5291556Srgrimes char *newopts, *ns, *cp; 5301556Srgrimes 5311556Srgrimes len = strlen(s); 5321556Srgrimes if ((newopts = ns = malloc(len + 2)) == NULL) 5331556Srgrimes err(1, NULL); 5341556Srgrimes /* 5351556Srgrimes * options begin with '-' 5361556Srgrimes */ 5371556Srgrimes if (*s != '-') 5381556Srgrimes *ns++ = '-'; /* add option flag */ 5391556Srgrimes /* 5401556Srgrimes * gaze to end of argv[1] 5411556Srgrimes */ 5421556Srgrimes cp = s + len - 1; 5431556Srgrimes /* 5441556Srgrimes * if last letter is a 't' flag with no argument (in the context 5451556Srgrimes * of the oldps options -- option string NOT starting with a '-' -- 5461556Srgrimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 5471556Srgrimes */ 5481556Srgrimes if (*cp == 't' && *s != '-') 5491556Srgrimes *cp = 'T'; 5501556Srgrimes else { 5511556Srgrimes /* 5521556Srgrimes * otherwise check for trailing number, which *may* be a 5531556Srgrimes * pid. 5541556Srgrimes */ 5551556Srgrimes while (cp >= s && isdigit(*cp)) 5561556Srgrimes --cp; 5571556Srgrimes } 5581556Srgrimes cp++; 5591556Srgrimes memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 5601556Srgrimes ns += cp - s; 5611556Srgrimes /* 5621556Srgrimes * if there's a trailing number, and not a preceding 'p' (pid) or 5631556Srgrimes * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 5641556Srgrimes */ 5658855Srgrimes if (isdigit(*cp) && 5667165Sjoerg (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) && 5677165Sjoerg (cp - 1 == s || cp[-2] != 't')) 5681556Srgrimes *ns++ = 'p'; 5691556Srgrimes (void)strcpy(ns, cp); /* and append the number */ 5701556Srgrimes 5711556Srgrimes return (newopts); 5721556Srgrimes} 5731556Srgrimes 5741556Srgrimesstatic void 5751556Srgrimesusage() 5761556Srgrimes{ 5771556Srgrimes 57826465Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 57926465Scharnier "usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty] [-U user]", 58026465Scharnier " [-M core] [-N system] [-W swap]", 58126465Scharnier " ps [-L]"); 5821556Srgrimes exit(1); 5831556Srgrimes} 584