ps.c revision 53276
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. 321556Srgrimes */ 331556Srgrimes 341556Srgrimes#ifndef lint 3520420Sstevestatic char const copyright[] = 361556Srgrimes"@(#) Copyright (c) 1990, 1993, 1994\n\ 371556Srgrimes The Regents of the University of California. All rights reserved.\n"; 381556Srgrimes#endif /* not lint */ 391556Srgrimes 401556Srgrimes#ifndef lint 4136049Scharnier#if 0 4236049Scharnierstatic char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 4336049Scharnier#endif 4436049Scharnierstatic const char rcsid[] = 4550471Speter "$FreeBSD: head/bin/ps/ps.c 53276 1999-11-17 12:52:42Z peter $"; 461556Srgrimes#endif /* not lint */ 471556Srgrimes 481556Srgrimes#include <sys/param.h> 493296Sdg#include <sys/user.h> 501556Srgrimes#include <sys/time.h> 511556Srgrimes#include <sys/resource.h> 521556Srgrimes#include <sys/stat.h> 531556Srgrimes#include <sys/ioctl.h> 541556Srgrimes#include <sys/sysctl.h> 551556Srgrimes 561556Srgrimes#include <ctype.h> 571556Srgrimes#include <err.h> 581556Srgrimes#include <errno.h> 591556Srgrimes#include <fcntl.h> 601556Srgrimes#include <kvm.h> 6113514Smpp#include <limits.h> 621556Srgrimes#include <nlist.h> 631556Srgrimes#include <paths.h> 641556Srgrimes#include <stdio.h> 651556Srgrimes#include <stdlib.h> 661556Srgrimes#include <string.h> 671556Srgrimes#include <unistd.h> 6811744Sache#include <locale.h> 6913020Speter#include <pwd.h> 701556Srgrimes 711556Srgrimes#include "ps.h" 721556Srgrimes 731556SrgrimesKINFO *kinfo; 741556Srgrimesstruct varent *vhead, *vtail; 751556Srgrimes 761556Srgrimesint eval; /* exit value */ 7719068Speterint cflag; /* -c */ 781556Srgrimesint rawcpu; /* -C */ 791556Srgrimesint sumrusage; /* -S */ 801556Srgrimesint termwidth; /* width of screen (0 == infinity) */ 811556Srgrimesint totwidth; /* calculated width of requested variables */ 821556Srgrimes 831556Srgrimesstatic int needuser, needcomm, needenv; 8431552Sdyson#if defined(LAZY_PS) 8531552Sdysonstatic int forceuread=0; 8631552Sdyson#else 8731552Sdysonstatic int forceuread=1; 8831552Sdyson#endif 891556Srgrimes 901556Srgrimesenum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 911556Srgrimes 921556Srgrimesstatic char *fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int), 931556Srgrimes KINFO *, char *, int)); 941556Srgrimesstatic char *kludge_oldps_options __P((char *)); 951556Srgrimesstatic int pscomp __P((const void *, const void *)); 961556Srgrimesstatic void saveuser __P((KINFO *)); 971556Srgrimesstatic void scanvars __P((void)); 9825271Sjkhstatic void dynsizevars __P((KINFO *)); 9925271Sjkhstatic void sizevars __P((void)); 1001556Srgrimesstatic void usage __P((void)); 1011556Srgrimes 1021556Srgrimeschar dfmt[] = "pid tt state time command"; 1031556Srgrimeschar jfmt[] = "user pid ppid pgid sess jobc state tt time command"; 1041556Srgrimeschar lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command"; 1051556Srgrimeschar o1[] = "pid"; 1061556Srgrimeschar o2[] = "tt state time command"; 1071556Srgrimeschar ufmt[] = "user pid %cpu %mem vsz rss tt state start time command"; 1081556Srgrimeschar vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command"; 1091556Srgrimes 1101556Srgrimeskvm_t *kd; 1111556Srgrimes 1121556Srgrimesint 1131556Srgrimesmain(argc, argv) 1141556Srgrimes int argc; 1151556Srgrimes char *argv[]; 1161556Srgrimes{ 1171556Srgrimes struct kinfo_proc *kp; 1181556Srgrimes struct varent *vent; 1191556Srgrimes struct winsize ws; 12013020Speter struct passwd *pwd; 1211556Srgrimes dev_t ttydev; 1221556Srgrimes pid_t pid; 1231556Srgrimes uid_t uid; 12437317Sphk int all, ch, flag, i, fmt, lineno, nentries, dropgid; 1251556Srgrimes int prtheader, wflag, what, xflg; 12613514Smpp char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX]; 1271556Srgrimes 12811809Sache (void) setlocale(LC_ALL, ""); 12911809Sache 1301556Srgrimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1311556Srgrimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1321556Srgrimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1331556Srgrimes ws.ws_col == 0) 1341556Srgrimes termwidth = 79; 1351556Srgrimes else 1361556Srgrimes termwidth = ws.ws_col - 1; 1371556Srgrimes 1381556Srgrimes if (argc > 1) 1391556Srgrimes argv[1] = kludge_oldps_options(argv[1]); 1401556Srgrimes 1411556Srgrimes all = fmt = prtheader = wflag = xflg = 0; 1421556Srgrimes pid = -1; 1431556Srgrimes uid = (uid_t) -1; 1441556Srgrimes ttydev = NODEV; 14537317Sphk dropgid = 0; 14637317Sphk memf = nlistf = swapf = _PATH_DEVNULL; 1471556Srgrimes while ((ch = getopt(argc, argv, 14831552Sdyson#if defined(LAZY_PS) 14933591Sdima "aCcefghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) 15031552Sdyson#else 15124348Simp "aCceghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) 15231552Sdyson#endif 1531556Srgrimes switch((char)ch) { 1541556Srgrimes case 'a': 1551556Srgrimes all = 1; 1561556Srgrimes break; 15719068Speter case 'C': 15819068Speter rawcpu = 1; 15919068Speter break; 16019068Speter case 'c': 16119068Speter cflag = 1; 16219068Speter break; 1631556Srgrimes case 'e': /* XXX set ufmt */ 1641556Srgrimes needenv = 1; 1651556Srgrimes break; 1661556Srgrimes case 'g': 1671556Srgrimes break; /* no-op */ 1681556Srgrimes case 'h': 1691556Srgrimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 1701556Srgrimes break; 1711556Srgrimes case 'j': 1721556Srgrimes parsefmt(jfmt); 1731556Srgrimes fmt = 1; 1741556Srgrimes jfmt[0] = '\0'; 1751556Srgrimes break; 1761556Srgrimes case 'L': 1771556Srgrimes showkey(); 1781556Srgrimes exit(0); 1791556Srgrimes case 'l': 1801556Srgrimes parsefmt(lfmt); 1811556Srgrimes fmt = 1; 1821556Srgrimes lfmt[0] = '\0'; 1831556Srgrimes break; 1841556Srgrimes case 'M': 1851556Srgrimes memf = optarg; 18637317Sphk dropgid = 1; 1871556Srgrimes break; 1881556Srgrimes case 'm': 1891556Srgrimes sortby = SORTMEM; 1901556Srgrimes break; 1911556Srgrimes case 'N': 1921556Srgrimes nlistf = optarg; 19337317Sphk dropgid = 1; 1941556Srgrimes break; 1951556Srgrimes case 'O': 1961556Srgrimes parsefmt(o1); 1971556Srgrimes parsefmt(optarg); 1981556Srgrimes parsefmt(o2); 1991556Srgrimes o1[0] = o2[0] = '\0'; 2001556Srgrimes fmt = 1; 2011556Srgrimes break; 2021556Srgrimes case 'o': 2031556Srgrimes parsefmt(optarg); 2041556Srgrimes fmt = 1; 2051556Srgrimes break; 20631552Sdyson#if defined(LAZY_PS) 20731552Sdyson case 'f': 20831552Sdyson if (getuid() == 0 || getgid() == 0) 20931552Sdyson forceuread = 1; 21031552Sdyson break; 21131552Sdyson#endif 2121556Srgrimes case 'p': 2131556Srgrimes pid = atol(optarg); 2141556Srgrimes xflg = 1; 2151556Srgrimes break; 2161556Srgrimes case 'r': 2171556Srgrimes sortby = SORTCPU; 2181556Srgrimes break; 2191556Srgrimes case 'S': 2201556Srgrimes sumrusage = 1; 2211556Srgrimes break; 2221556Srgrimes case 'T': 2231556Srgrimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 2241556Srgrimes errx(1, "stdin: not a terminal"); 2251556Srgrimes /* FALLTHROUGH */ 2261556Srgrimes case 't': { 2271556Srgrimes struct stat sb; 2281556Srgrimes char *ttypath, pathbuf[MAXPATHLEN]; 2291556Srgrimes 2301556Srgrimes if (strcmp(optarg, "co") == 0) 2311556Srgrimes ttypath = _PATH_CONSOLE; 2321556Srgrimes else if (*optarg != '/') 2331556Srgrimes (void)snprintf(ttypath = pathbuf, 2341556Srgrimes sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); 2351556Srgrimes else 2361556Srgrimes ttypath = optarg; 2371556Srgrimes if (stat(ttypath, &sb) == -1) 2381556Srgrimes err(1, "%s", ttypath); 2391556Srgrimes if (!S_ISCHR(sb.st_mode)) 2401556Srgrimes errx(1, "%s: not a terminal", ttypath); 2411556Srgrimes ttydev = sb.st_rdev; 2421556Srgrimes break; 2431556Srgrimes } 24413020Speter case 'U': 24513020Speter pwd = getpwnam(optarg); 24613020Speter if (pwd == NULL) 24713020Speter errx(1, "%s: no such user", optarg); 24813020Speter uid = pwd->pw_uid; 24913020Speter endpwent(); 25013020Speter xflg++; /* XXX: intuitive? */ 25113020Speter break; 2521556Srgrimes case 'u': 2531556Srgrimes parsefmt(ufmt); 2541556Srgrimes sortby = SORTCPU; 2551556Srgrimes fmt = 1; 2561556Srgrimes ufmt[0] = '\0'; 2571556Srgrimes break; 2581556Srgrimes case 'v': 2591556Srgrimes parsefmt(vfmt); 2601556Srgrimes sortby = SORTMEM; 2611556Srgrimes fmt = 1; 2621556Srgrimes vfmt[0] = '\0'; 2631556Srgrimes break; 2641556Srgrimes case 'W': 2651556Srgrimes swapf = optarg; 26637317Sphk dropgid = 1; 2671556Srgrimes break; 2681556Srgrimes case 'w': 2691556Srgrimes if (wflag) 2701556Srgrimes termwidth = UNLIMITED; 2711556Srgrimes else if (termwidth < 131) 2721556Srgrimes termwidth = 131; 2731556Srgrimes wflag++; 2741556Srgrimes break; 2751556Srgrimes case 'x': 2761556Srgrimes xflg = 1; 2771556Srgrimes break; 2781556Srgrimes case '?': 2791556Srgrimes default: 2801556Srgrimes usage(); 2811556Srgrimes } 2821556Srgrimes argc -= optind; 2831556Srgrimes argv += optind; 2841556Srgrimes 2851556Srgrimes#define BACKWARD_COMPATIBILITY 2861556Srgrimes#ifdef BACKWARD_COMPATIBILITY 2871556Srgrimes if (*argv) { 2881556Srgrimes nlistf = *argv; 2891556Srgrimes if (*++argv) { 2901556Srgrimes memf = *argv; 2911556Srgrimes if (*++argv) 2921556Srgrimes swapf = *argv; 2931556Srgrimes } 2941556Srgrimes } 2951556Srgrimes#endif 2961556Srgrimes /* 2971556Srgrimes * Discard setgid privileges if not the running kernel so that bad 2981556Srgrimes * guys can't print interesting stuff from kernel memory. 2991556Srgrimes */ 30037317Sphk if (dropgid) { 3011556Srgrimes setgid(getgid()); 30237317Sphk setuid(getuid()); 30337317Sphk } 3041556Srgrimes 3051556Srgrimes kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf); 3061556Srgrimes if (kd == 0) 3071556Srgrimes errx(1, "%s", errbuf); 3081556Srgrimes 3091556Srgrimes if (!fmt) 3101556Srgrimes parsefmt(dfmt); 3111556Srgrimes 31213020Speter /* XXX - should be cleaner */ 31313020Speter if (!all && ttydev == NODEV && pid == -1 && uid == (uid_t)-1) 3141556Srgrimes uid = getuid(); 3151556Srgrimes 3161556Srgrimes /* 3171556Srgrimes * scan requested variables, noting what structures are needed, 31853170Skris * and adjusting header widths as appropriate. 3191556Srgrimes */ 3201556Srgrimes scanvars(); 3211556Srgrimes /* 3221556Srgrimes * get proc list 3231556Srgrimes */ 3241556Srgrimes if (uid != (uid_t) -1) { 3251556Srgrimes what = KERN_PROC_UID; 3261556Srgrimes flag = uid; 3271556Srgrimes } else if (ttydev != NODEV) { 3281556Srgrimes what = KERN_PROC_TTY; 3291556Srgrimes flag = ttydev; 3301556Srgrimes } else if (pid != -1) { 3311556Srgrimes what = KERN_PROC_PID; 3321556Srgrimes flag = pid; 3331556Srgrimes } else { 3341556Srgrimes what = KERN_PROC_ALL; 3351556Srgrimes flag = 0; 3361556Srgrimes } 3371556Srgrimes /* 3381556Srgrimes * select procs 3391556Srgrimes */ 3401556Srgrimes if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0) 3411556Srgrimes errx(1, "%s", kvm_geterr(kd)); 3421556Srgrimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 3431556Srgrimes err(1, NULL); 3441556Srgrimes for (i = nentries; --i >= 0; ++kp) { 3451556Srgrimes kinfo[i].ki_p = kp; 3461556Srgrimes if (needuser) 3471556Srgrimes saveuser(&kinfo[i]); 34825271Sjkh dynsizevars(&kinfo[i]); 3491556Srgrimes } 35025271Sjkh 35125271Sjkh sizevars(); 35225271Sjkh 3531556Srgrimes /* 3541556Srgrimes * print header 3551556Srgrimes */ 3561556Srgrimes printheader(); 3571556Srgrimes if (nentries == 0) 3581556Srgrimes exit(0); 3591556Srgrimes /* 3601556Srgrimes * sort proc list 3611556Srgrimes */ 3621556Srgrimes qsort(kinfo, nentries, sizeof(KINFO), pscomp); 3631556Srgrimes /* 3641556Srgrimes * for each proc, call each variable output function. 3651556Srgrimes */ 3661556Srgrimes for (i = lineno = 0; i < nentries; i++) { 3671556Srgrimes if (xflg == 0 && (KI_EPROC(&kinfo[i])->e_tdev == NODEV || 3681556Srgrimes (KI_PROC(&kinfo[i])->p_flag & P_CONTROLT ) == 0)) 3691556Srgrimes continue; 3701556Srgrimes for (vent = vhead; vent; vent = vent->next) { 3711556Srgrimes (vent->var->oproc)(&kinfo[i], vent); 3721556Srgrimes if (vent->next != NULL) 3731556Srgrimes (void)putchar(' '); 3741556Srgrimes } 3751556Srgrimes (void)putchar('\n'); 3761556Srgrimes if (prtheader && lineno++ == prtheader - 4) { 3771556Srgrimes (void)putchar('\n'); 3781556Srgrimes printheader(); 3791556Srgrimes lineno = 0; 3801556Srgrimes } 3811556Srgrimes } 3821556Srgrimes exit(eval); 3831556Srgrimes} 3841556Srgrimes 3851556Srgrimesstatic void 3861556Srgrimesscanvars() 3871556Srgrimes{ 3881556Srgrimes struct varent *vent; 3891556Srgrimes VAR *v; 39025271Sjkh 39125271Sjkh for (vent = vhead; vent; vent = vent->next) { 39225271Sjkh v = vent->var; 39325271Sjkh if (v->flag & DSIZ) { 39425271Sjkh v->dwidth = v->width; 39525271Sjkh v->width = 0; 39625271Sjkh } 39725271Sjkh if (v->flag & USER) 39825271Sjkh needuser = 1; 39925271Sjkh if (v->flag & COMM) 40025271Sjkh needcomm = 1; 40125271Sjkh } 40225271Sjkh} 40325271Sjkh 40425271Sjkhstatic void 40525271Sjkhdynsizevars(ki) 40625271Sjkh KINFO *ki; 40725271Sjkh{ 40825271Sjkh struct varent *vent; 40925271Sjkh VAR *v; 4101556Srgrimes int i; 4111556Srgrimes 4121556Srgrimes for (vent = vhead; vent; vent = vent->next) { 4131556Srgrimes v = vent->var; 41425271Sjkh if (!(v->flag & DSIZ)) 41525271Sjkh continue; 41625271Sjkh i = (v->sproc)( ki); 41725271Sjkh if (v->width < i) 41825271Sjkh v->width = i; 41925271Sjkh if (v->width > v->dwidth) 42025271Sjkh v->width = v->dwidth; 42125271Sjkh } 42225271Sjkh} 42325271Sjkh 42425271Sjkhstatic void 42525271Sjkhsizevars() 42625271Sjkh{ 42725271Sjkh struct varent *vent; 42825271Sjkh VAR *v; 42925271Sjkh int i; 43025271Sjkh 43125271Sjkh for (vent = vhead; vent; vent = vent->next) { 43225271Sjkh v = vent->var; 4331556Srgrimes i = strlen(v->header); 4341556Srgrimes if (v->width < i) 4351556Srgrimes v->width = i; 4361556Srgrimes totwidth += v->width + 1; /* +1 for space */ 4371556Srgrimes } 4381556Srgrimes totwidth--; 4391556Srgrimes} 4401556Srgrimes 4411556Srgrimesstatic char * 4421556Srgrimesfmt(fn, ki, comm, maxlen) 4431556Srgrimes char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int)); 4441556Srgrimes KINFO *ki; 4451556Srgrimes char *comm; 4461556Srgrimes int maxlen; 4471556Srgrimes{ 4481556Srgrimes char *s; 4491556Srgrimes 4501556Srgrimes if ((s = 4511556Srgrimes fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL) 4521556Srgrimes err(1, NULL); 4531556Srgrimes return (s); 4541556Srgrimes} 4551556Srgrimes 45631552Sdyson#define UREADOK(ki) (forceuread || (KI_PROC(ki)->p_flag & P_INMEM)) 45731552Sdyson 4581556Srgrimesstatic void 4591556Srgrimessaveuser(ki) 4601556Srgrimes KINFO *ki; 4611556Srgrimes{ 4621556Srgrimes struct usave *usp; 4631556Srgrimes 4641556Srgrimes usp = &ki->ki_u; 46545227Swpaul 46653276Speter if (KI_PROC(ki)->p_flag & P_INMEM) { 4671556Srgrimes /* 4681556Srgrimes * The u-area might be swapped out, and we can't get 4691556Srgrimes * at it because we have a crashdump and no swap. 4701556Srgrimes * If it's here fill in these fields, otherwise, just 4711556Srgrimes * leave them 0. 4721556Srgrimes */ 47353276Speter usp->u_start = KI_EPROC(ki)->e_stats.p_start; 47453276Speter usp->u_ru = KI_EPROC(ki)->e_stats.p_ru; 47553276Speter usp->u_cru = KI_EPROC(ki)->e_stats.p_cru; 4761556Srgrimes usp->u_valid = 1; 4771556Srgrimes } else 4781556Srgrimes usp->u_valid = 0; 4791556Srgrimes /* 4801556Srgrimes * save arguments if needed 4811556Srgrimes */ 48253239Sphk if (needcomm && (UREADOK(ki) || (KI_PROC(ki)->p_args != NULL))) { 48353276Speter ki->ki_args = fmt(kvm_getargv, ki, KI_PROC(ki)->p_comm, 48453276Speter MAXCOMLEN); 48531552Sdyson } else if (needcomm) { 48653276Speter ki->ki_args = malloc(strlen(KI_PROC(ki)->p_comm) + 3); 48753276Speter sprintf(ki->ki_args, "(%s)", KI_PROC(ki)->p_comm); 48853276Speter } else { 48953276Speter ki->ki_args = NULL; 49053276Speter } 49153276Speter if (needenv && UREADOK(ki)) { 49253276Speter ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0); 49353276Speter } else if (needenv) { 49453276Speter ki->ki_env = malloc(3); 49553276Speter strcpy(ki->ki_env, "()"); 49653276Speter } else { 49753276Speter ki->ki_env = NULL; 49853276Speter } 4991556Srgrimes} 5001556Srgrimes 5011556Srgrimesstatic int 5021556Srgrimespscomp(a, b) 5031556Srgrimes const void *a, *b; 5041556Srgrimes{ 5051556Srgrimes int i; 5061556Srgrimes#define VSIZE(k) (KI_EPROC(k)->e_vm.vm_dsize + KI_EPROC(k)->e_vm.vm_ssize + \ 5071556Srgrimes KI_EPROC(k)->e_vm.vm_tsize) 5081556Srgrimes 5091556Srgrimes if (sortby == SORTCPU) 5101556Srgrimes return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a)); 5111556Srgrimes if (sortby == SORTMEM) 5121556Srgrimes return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a)); 5131556Srgrimes i = KI_EPROC((KINFO *)a)->e_tdev - KI_EPROC((KINFO *)b)->e_tdev; 5141556Srgrimes if (i == 0) 5151556Srgrimes i = KI_PROC((KINFO *)a)->p_pid - KI_PROC((KINFO *)b)->p_pid; 5161556Srgrimes return (i); 5171556Srgrimes} 5181556Srgrimes 5191556Srgrimes/* 5201556Srgrimes * ICK (all for getopt), would rather hide the ugliness 5211556Srgrimes * here than taint the main code. 5221556Srgrimes * 5231556Srgrimes * ps foo -> ps -foo 5241556Srgrimes * ps 34 -> ps -p34 5251556Srgrimes * 5261556Srgrimes * The old convention that 't' with no trailing tty arg means the users 5271556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 5281556Srgrimes * feature is available with the option 'T', which takes no argument. 5291556Srgrimes */ 5301556Srgrimesstatic char * 5311556Srgrimeskludge_oldps_options(s) 5321556Srgrimes char *s; 5331556Srgrimes{ 5341556Srgrimes size_t len; 5351556Srgrimes char *newopts, *ns, *cp; 5361556Srgrimes 5371556Srgrimes len = strlen(s); 5381556Srgrimes if ((newopts = ns = malloc(len + 2)) == NULL) 5391556Srgrimes err(1, NULL); 5401556Srgrimes /* 5411556Srgrimes * options begin with '-' 5421556Srgrimes */ 5431556Srgrimes if (*s != '-') 5441556Srgrimes *ns++ = '-'; /* add option flag */ 5451556Srgrimes /* 5461556Srgrimes * gaze to end of argv[1] 5471556Srgrimes */ 5481556Srgrimes cp = s + len - 1; 5491556Srgrimes /* 5501556Srgrimes * if last letter is a 't' flag with no argument (in the context 5511556Srgrimes * of the oldps options -- option string NOT starting with a '-' -- 5521556Srgrimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 5531556Srgrimes */ 5541556Srgrimes if (*cp == 't' && *s != '-') 5551556Srgrimes *cp = 'T'; 5561556Srgrimes else { 5571556Srgrimes /* 5581556Srgrimes * otherwise check for trailing number, which *may* be a 5591556Srgrimes * pid. 5601556Srgrimes */ 5611556Srgrimes while (cp >= s && isdigit(*cp)) 5621556Srgrimes --cp; 5631556Srgrimes } 5641556Srgrimes cp++; 5651556Srgrimes memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 5661556Srgrimes ns += cp - s; 5671556Srgrimes /* 5681556Srgrimes * if there's a trailing number, and not a preceding 'p' (pid) or 5691556Srgrimes * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 5701556Srgrimes */ 5718855Srgrimes if (isdigit(*cp) && 5727165Sjoerg (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) && 5737165Sjoerg (cp - 1 == s || cp[-2] != 't')) 5741556Srgrimes *ns++ = 'p'; 5751556Srgrimes (void)strcpy(ns, cp); /* and append the number */ 5761556Srgrimes 5771556Srgrimes return (newopts); 5781556Srgrimes} 5791556Srgrimes 5801556Srgrimesstatic void 5811556Srgrimesusage() 5821556Srgrimes{ 5831556Srgrimes 58426465Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 58526465Scharnier "usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty] [-U user]", 58626465Scharnier " [-M core] [-N system] [-W swap]", 58726465Scharnier " ps [-L]"); 5881556Srgrimes exit(1); 5891556Srgrimes} 590