ps.c revision 73367
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 73367 2001-03-02 23:53:36Z ache $"; 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> 6173367Sache#include <langinfo.h> 6213514Smpp#include <limits.h> 6373367Sache#include <locale.h> 641556Srgrimes#include <nlist.h> 651556Srgrimes#include <paths.h> 661556Srgrimes#include <stdio.h> 671556Srgrimes#include <stdlib.h> 681556Srgrimes#include <string.h> 691556Srgrimes#include <unistd.h> 7013020Speter#include <pwd.h> 7166377Sbrian#include <utmp.h> 721556Srgrimes 731556Srgrimes#include "ps.h" 741556Srgrimes 7566377Sbrian#define SEP ", \t" /* username separators */ 7666377Sbrian 771556SrgrimesKINFO *kinfo; 781556Srgrimesstruct varent *vhead, *vtail; 791556Srgrimes 801556Srgrimesint eval; /* exit value */ 8119068Speterint cflag; /* -c */ 821556Srgrimesint rawcpu; /* -C */ 831556Srgrimesint sumrusage; /* -S */ 841556Srgrimesint termwidth; /* width of screen (0 == infinity) */ 851556Srgrimesint totwidth; /* calculated width of requested variables */ 8673367Sacheint use_ampm; /* use AM/PM time */ 871556Srgrimes 881556Srgrimesstatic int needuser, needcomm, needenv; 8931552Sdyson#if defined(LAZY_PS) 9031552Sdysonstatic int forceuread=0; 9131552Sdyson#else 9231552Sdysonstatic int forceuread=1; 9331552Sdyson#endif 941556Srgrimes 951556Srgrimesenum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 961556Srgrimes 971556Srgrimesstatic char *fmt __P((char **(*)(kvm_t *, const struct kinfo_proc *, int), 981556Srgrimes KINFO *, char *, int)); 991556Srgrimesstatic char *kludge_oldps_options __P((char *)); 1001556Srgrimesstatic int pscomp __P((const void *, const void *)); 1011556Srgrimesstatic void saveuser __P((KINFO *)); 1021556Srgrimesstatic void scanvars __P((void)); 10325271Sjkhstatic void dynsizevars __P((KINFO *)); 10425271Sjkhstatic void sizevars __P((void)); 1051556Srgrimesstatic void usage __P((void)); 10666377Sbrianstatic uid_t *getuids(const char *, int *); 1071556Srgrimes 1081556Srgrimeschar dfmt[] = "pid tt state time command"; 1091556Srgrimeschar jfmt[] = "user pid ppid pgid sess jobc state tt time command"; 1101556Srgrimeschar lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command"; 1111556Srgrimeschar o1[] = "pid"; 1121556Srgrimeschar o2[] = "tt state time command"; 1131556Srgrimeschar ufmt[] = "user pid %cpu %mem vsz rss tt state start time command"; 1141556Srgrimeschar vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command"; 1151556Srgrimes 1161556Srgrimeskvm_t *kd; 1171556Srgrimes 1181556Srgrimesint 1191556Srgrimesmain(argc, argv) 1201556Srgrimes int argc; 1211556Srgrimes char *argv[]; 1221556Srgrimes{ 1231556Srgrimes struct kinfo_proc *kp; 1241556Srgrimes struct varent *vent; 1251556Srgrimes struct winsize ws; 1261556Srgrimes dev_t ttydev; 1271556Srgrimes pid_t pid; 12866377Sbrian uid_t *uids; 12937317Sphk int all, ch, flag, i, fmt, lineno, nentries, dropgid; 13066377Sbrian int prtheader, wflag, what, xflg, uid, nuids; 13113514Smpp char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX]; 1321556Srgrimes 13311809Sache (void) setlocale(LC_ALL, ""); 13473367Sache use_ampm = (*nl_langinfo(T_FMT_AMPM) != '\0'); 13511809Sache 1361556Srgrimes if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1371556Srgrimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1381556Srgrimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1391556Srgrimes ws.ws_col == 0) 1401556Srgrimes termwidth = 79; 1411556Srgrimes else 1421556Srgrimes termwidth = ws.ws_col - 1; 1431556Srgrimes 1441556Srgrimes if (argc > 1) 1451556Srgrimes argv[1] = kludge_oldps_options(argv[1]); 1461556Srgrimes 1471556Srgrimes all = fmt = prtheader = wflag = xflg = 0; 1481556Srgrimes pid = -1; 14966377Sbrian nuids = 0; 15066377Sbrian uids = NULL; 1511556Srgrimes ttydev = NODEV; 15237317Sphk dropgid = 0; 15337317Sphk memf = nlistf = swapf = _PATH_DEVNULL; 1541556Srgrimes while ((ch = getopt(argc, argv, 15531552Sdyson#if defined(LAZY_PS) 15633591Sdima "aCcefghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) 15731552Sdyson#else 15824348Simp "aCceghjLlM:mN:O:o:p:rSTt:U:uvW:wx")) != -1) 15931552Sdyson#endif 1601556Srgrimes switch((char)ch) { 1611556Srgrimes case 'a': 1621556Srgrimes all = 1; 1631556Srgrimes break; 16419068Speter case 'C': 16519068Speter rawcpu = 1; 16619068Speter break; 16719068Speter case 'c': 16819068Speter cflag = 1; 16919068Speter break; 1701556Srgrimes case 'e': /* XXX set ufmt */ 1711556Srgrimes needenv = 1; 1721556Srgrimes break; 1731556Srgrimes case 'g': 1741556Srgrimes break; /* no-op */ 1751556Srgrimes case 'h': 1761556Srgrimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 1771556Srgrimes break; 1781556Srgrimes case 'j': 1791556Srgrimes parsefmt(jfmt); 1801556Srgrimes fmt = 1; 1811556Srgrimes jfmt[0] = '\0'; 1821556Srgrimes break; 1831556Srgrimes case 'L': 1841556Srgrimes showkey(); 1851556Srgrimes exit(0); 1861556Srgrimes case 'l': 1871556Srgrimes parsefmt(lfmt); 1881556Srgrimes fmt = 1; 1891556Srgrimes lfmt[0] = '\0'; 1901556Srgrimes break; 1911556Srgrimes case 'M': 1921556Srgrimes memf = optarg; 19337317Sphk dropgid = 1; 1941556Srgrimes break; 1951556Srgrimes case 'm': 1961556Srgrimes sortby = SORTMEM; 1971556Srgrimes break; 1981556Srgrimes case 'N': 1991556Srgrimes nlistf = optarg; 20037317Sphk dropgid = 1; 2011556Srgrimes break; 2021556Srgrimes case 'O': 2031556Srgrimes parsefmt(o1); 2041556Srgrimes parsefmt(optarg); 2051556Srgrimes parsefmt(o2); 2061556Srgrimes o1[0] = o2[0] = '\0'; 2071556Srgrimes fmt = 1; 2081556Srgrimes break; 2091556Srgrimes case 'o': 2101556Srgrimes parsefmt(optarg); 2111556Srgrimes fmt = 1; 2121556Srgrimes break; 21331552Sdyson#if defined(LAZY_PS) 21431552Sdyson case 'f': 21531552Sdyson if (getuid() == 0 || getgid() == 0) 21631552Sdyson forceuread = 1; 21731552Sdyson break; 21831552Sdyson#endif 2191556Srgrimes case 'p': 2201556Srgrimes pid = atol(optarg); 2211556Srgrimes xflg = 1; 2221556Srgrimes break; 2231556Srgrimes case 'r': 2241556Srgrimes sortby = SORTCPU; 2251556Srgrimes break; 2261556Srgrimes case 'S': 2271556Srgrimes sumrusage = 1; 2281556Srgrimes break; 2291556Srgrimes case 'T': 2301556Srgrimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 2311556Srgrimes errx(1, "stdin: not a terminal"); 2321556Srgrimes /* FALLTHROUGH */ 2331556Srgrimes case 't': { 2341556Srgrimes struct stat sb; 2351556Srgrimes char *ttypath, pathbuf[MAXPATHLEN]; 2361556Srgrimes 2371556Srgrimes if (strcmp(optarg, "co") == 0) 2381556Srgrimes ttypath = _PATH_CONSOLE; 2391556Srgrimes else if (*optarg != '/') 2401556Srgrimes (void)snprintf(ttypath = pathbuf, 2411556Srgrimes sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); 2421556Srgrimes else 2431556Srgrimes ttypath = optarg; 2441556Srgrimes if (stat(ttypath, &sb) == -1) 2451556Srgrimes err(1, "%s", ttypath); 2461556Srgrimes if (!S_ISCHR(sb.st_mode)) 2471556Srgrimes errx(1, "%s: not a terminal", ttypath); 2481556Srgrimes ttydev = sb.st_rdev; 2491556Srgrimes break; 2501556Srgrimes } 25113020Speter case 'U': 25266377Sbrian uids = getuids(optarg, &nuids); 25313020Speter xflg++; /* XXX: intuitive? */ 25413020Speter break; 2551556Srgrimes case 'u': 2561556Srgrimes parsefmt(ufmt); 2571556Srgrimes sortby = SORTCPU; 2581556Srgrimes fmt = 1; 2591556Srgrimes ufmt[0] = '\0'; 2601556Srgrimes break; 2611556Srgrimes case 'v': 2621556Srgrimes parsefmt(vfmt); 2631556Srgrimes sortby = SORTMEM; 2641556Srgrimes fmt = 1; 2651556Srgrimes vfmt[0] = '\0'; 2661556Srgrimes break; 2671556Srgrimes case 'W': 2681556Srgrimes swapf = optarg; 26937317Sphk dropgid = 1; 2701556Srgrimes break; 2711556Srgrimes case 'w': 2721556Srgrimes if (wflag) 2731556Srgrimes termwidth = UNLIMITED; 2741556Srgrimes else if (termwidth < 131) 2751556Srgrimes termwidth = 131; 2761556Srgrimes wflag++; 2771556Srgrimes break; 2781556Srgrimes case 'x': 2791556Srgrimes xflg = 1; 2801556Srgrimes break; 2811556Srgrimes case '?': 2821556Srgrimes default: 2831556Srgrimes usage(); 2841556Srgrimes } 2851556Srgrimes argc -= optind; 2861556Srgrimes argv += optind; 2871556Srgrimes 2881556Srgrimes#define BACKWARD_COMPATIBILITY 2891556Srgrimes#ifdef BACKWARD_COMPATIBILITY 2901556Srgrimes if (*argv) { 2911556Srgrimes nlistf = *argv; 2921556Srgrimes if (*++argv) { 2931556Srgrimes memf = *argv; 2941556Srgrimes if (*++argv) 2951556Srgrimes swapf = *argv; 2961556Srgrimes } 2971556Srgrimes } 2981556Srgrimes#endif 2991556Srgrimes /* 3001556Srgrimes * Discard setgid privileges if not the running kernel so that bad 3011556Srgrimes * guys can't print interesting stuff from kernel memory. 3021556Srgrimes */ 30337317Sphk if (dropgid) { 3041556Srgrimes setgid(getgid()); 30537317Sphk setuid(getuid()); 30637317Sphk } 3071556Srgrimes 3081556Srgrimes kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf); 3091556Srgrimes if (kd == 0) 3101556Srgrimes errx(1, "%s", errbuf); 3111556Srgrimes 3121556Srgrimes if (!fmt) 3131556Srgrimes parsefmt(dfmt); 3141556Srgrimes 31513020Speter /* XXX - should be cleaner */ 31666377Sbrian if (!all && ttydev == NODEV && pid == -1 && !nuids) { 31766377Sbrian if ((uids = malloc(sizeof (*uids))) == NULL) 31866377Sbrian errx(1, "malloc: %s", strerror(errno)); 31966377Sbrian nuids = 1; 32066377Sbrian *uids = getuid(); 32166377Sbrian } 3221556Srgrimes 3231556Srgrimes /* 3241556Srgrimes * scan requested variables, noting what structures are needed, 32553170Skris * and adjusting header widths as appropriate. 3261556Srgrimes */ 3271556Srgrimes scanvars(); 3281556Srgrimes /* 3291556Srgrimes * get proc list 3301556Srgrimes */ 33166377Sbrian if (nuids == 1) { 3321556Srgrimes what = KERN_PROC_UID; 33366377Sbrian flag = *uids; 3341556Srgrimes } else if (ttydev != NODEV) { 3351556Srgrimes what = KERN_PROC_TTY; 3361556Srgrimes flag = ttydev; 3371556Srgrimes } else if (pid != -1) { 3381556Srgrimes what = KERN_PROC_PID; 3391556Srgrimes flag = pid; 3401556Srgrimes } else { 3411556Srgrimes what = KERN_PROC_ALL; 3421556Srgrimes flag = 0; 3431556Srgrimes } 3441556Srgrimes /* 3451556Srgrimes * select procs 3461556Srgrimes */ 3471556Srgrimes if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0) 3481556Srgrimes errx(1, "%s", kvm_geterr(kd)); 3491556Srgrimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 3501556Srgrimes err(1, NULL); 3511556Srgrimes for (i = nentries; --i >= 0; ++kp) { 3521556Srgrimes kinfo[i].ki_p = kp; 3531556Srgrimes if (needuser) 3541556Srgrimes saveuser(&kinfo[i]); 35525271Sjkh dynsizevars(&kinfo[i]); 3561556Srgrimes } 35725271Sjkh 35825271Sjkh sizevars(); 35925271Sjkh 3601556Srgrimes /* 3611556Srgrimes * print header 3621556Srgrimes */ 3631556Srgrimes printheader(); 3641556Srgrimes if (nentries == 0) 36562803Swill exit(1); 3661556Srgrimes /* 3671556Srgrimes * sort proc list 3681556Srgrimes */ 3691556Srgrimes qsort(kinfo, nentries, sizeof(KINFO), pscomp); 3701556Srgrimes /* 3711556Srgrimes * for each proc, call each variable output function. 3721556Srgrimes */ 3731556Srgrimes for (i = lineno = 0; i < nentries; i++) { 37469896Smckusick if (xflg == 0 && ((&kinfo[i])->ki_p->ki_tdev == NODEV || 37569896Smckusick ((&kinfo[i])->ki_p->ki_flag & P_CONTROLT ) == 0)) 3761556Srgrimes continue; 37766377Sbrian if (nuids > 1) { 37866377Sbrian for (uid = 0; uid < nuids; uid++) 37969896Smckusick if ((&kinfo[i])->ki_p->ki_uid == uids[uid]) 38066377Sbrian break; 38166377Sbrian if (uid == nuids) 38266377Sbrian continue; 38366377Sbrian } 3841556Srgrimes for (vent = vhead; vent; vent = vent->next) { 3851556Srgrimes (vent->var->oproc)(&kinfo[i], vent); 3861556Srgrimes if (vent->next != NULL) 3871556Srgrimes (void)putchar(' '); 3881556Srgrimes } 3891556Srgrimes (void)putchar('\n'); 3901556Srgrimes if (prtheader && lineno++ == prtheader - 4) { 3911556Srgrimes (void)putchar('\n'); 3921556Srgrimes printheader(); 3931556Srgrimes lineno = 0; 3941556Srgrimes } 3951556Srgrimes } 39666377Sbrian free(uids); 39766377Sbrian 3981556Srgrimes exit(eval); 3991556Srgrimes} 4001556Srgrimes 40166377Sbrianuid_t * 40266377Sbriangetuids(const char *arg, int *nuids) 40366377Sbrian{ 40466377Sbrian char name[UT_NAMESIZE + 1]; 40566377Sbrian struct passwd *pwd; 40666377Sbrian uid_t *uids, *moreuids; 40766377Sbrian int l, alloc; 40866377Sbrian 40966377Sbrian 41066377Sbrian alloc = 0; 41166377Sbrian *nuids = 0; 41266377Sbrian uids = NULL; 41366377Sbrian for (; (l = strcspn(arg, SEP)) > 0; arg += l + strspn(arg + l, SEP)) { 41466377Sbrian if (l >= sizeof name) { 41566377Sbrian warnx("%.*s: name too long", l, arg); 41666377Sbrian continue; 41766377Sbrian } 41866377Sbrian strncpy(name, arg, l); 41966377Sbrian name[l] = '\0'; 42066377Sbrian if ((pwd = getpwnam(name)) == NULL) { 42166377Sbrian warnx("%s: no such user", name); 42266377Sbrian continue; 42366377Sbrian } 42466377Sbrian if (*nuids >= alloc) { 42566377Sbrian alloc = (alloc + 1) << 1; 42666377Sbrian moreuids = realloc(uids, alloc * sizeof (*uids)); 42766377Sbrian if (moreuids == NULL) { 42866377Sbrian free(uids); 42966377Sbrian errx(1, "realloc: %s", strerror(errno)); 43066377Sbrian } 43166377Sbrian uids = moreuids; 43266377Sbrian } 43366377Sbrian uids[(*nuids)++] = pwd->pw_uid; 43466377Sbrian } 43566377Sbrian endpwent(); 43666377Sbrian 43766377Sbrian if (!*nuids) 43866377Sbrian errx(1, "No users specified"); 43966377Sbrian 44066377Sbrian return uids; 44166377Sbrian} 44266377Sbrian 4431556Srgrimesstatic void 4441556Srgrimesscanvars() 4451556Srgrimes{ 4461556Srgrimes struct varent *vent; 4471556Srgrimes VAR *v; 44825271Sjkh 44925271Sjkh for (vent = vhead; vent; vent = vent->next) { 45025271Sjkh v = vent->var; 45125271Sjkh if (v->flag & DSIZ) { 45225271Sjkh v->dwidth = v->width; 45325271Sjkh v->width = 0; 45425271Sjkh } 45525271Sjkh if (v->flag & USER) 45625271Sjkh needuser = 1; 45725271Sjkh if (v->flag & COMM) 45825271Sjkh needcomm = 1; 45925271Sjkh } 46025271Sjkh} 46125271Sjkh 46225271Sjkhstatic void 46325271Sjkhdynsizevars(ki) 46425271Sjkh KINFO *ki; 46525271Sjkh{ 46625271Sjkh struct varent *vent; 46725271Sjkh VAR *v; 4681556Srgrimes int i; 4691556Srgrimes 4701556Srgrimes for (vent = vhead; vent; vent = vent->next) { 4711556Srgrimes v = vent->var; 47225271Sjkh if (!(v->flag & DSIZ)) 47325271Sjkh continue; 47425271Sjkh i = (v->sproc)( ki); 47525271Sjkh if (v->width < i) 47625271Sjkh v->width = i; 47725271Sjkh if (v->width > v->dwidth) 47825271Sjkh v->width = v->dwidth; 47925271Sjkh } 48025271Sjkh} 48125271Sjkh 48225271Sjkhstatic void 48325271Sjkhsizevars() 48425271Sjkh{ 48525271Sjkh struct varent *vent; 48625271Sjkh VAR *v; 48725271Sjkh int i; 48825271Sjkh 48925271Sjkh for (vent = vhead; vent; vent = vent->next) { 49025271Sjkh v = vent->var; 4911556Srgrimes i = strlen(v->header); 4921556Srgrimes if (v->width < i) 4931556Srgrimes v->width = i; 4941556Srgrimes totwidth += v->width + 1; /* +1 for space */ 4951556Srgrimes } 4961556Srgrimes totwidth--; 4971556Srgrimes} 4981556Srgrimes 4991556Srgrimesstatic char * 5001556Srgrimesfmt(fn, ki, comm, maxlen) 5011556Srgrimes char **(*fn) __P((kvm_t *, const struct kinfo_proc *, int)); 5021556Srgrimes KINFO *ki; 5031556Srgrimes char *comm; 5041556Srgrimes int maxlen; 5051556Srgrimes{ 5061556Srgrimes char *s; 5071556Srgrimes 5081556Srgrimes if ((s = 5091556Srgrimes fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen)) == NULL) 5101556Srgrimes err(1, NULL); 5111556Srgrimes return (s); 5121556Srgrimes} 5131556Srgrimes 51471578Sjhb#define UREADOK(ki) (forceuread || (ki->ki_p->ki_sflag & PS_INMEM)) 51531552Sdyson 5161556Srgrimesstatic void 5171556Srgrimessaveuser(ki) 5181556Srgrimes KINFO *ki; 5191556Srgrimes{ 5201556Srgrimes 52171578Sjhb if (ki->ki_p->ki_sflag & PS_INMEM) { 5221556Srgrimes /* 5231556Srgrimes * The u-area might be swapped out, and we can't get 5241556Srgrimes * at it because we have a crashdump and no swap. 5251556Srgrimes * If it's here fill in these fields, otherwise, just 5261556Srgrimes * leave them 0. 5271556Srgrimes */ 52869896Smckusick ki->ki_valid = 1; 5291556Srgrimes } else 53069896Smckusick ki->ki_valid = 0; 5311556Srgrimes /* 5321556Srgrimes * save arguments if needed 5331556Srgrimes */ 53469896Smckusick if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) { 53569896Smckusick ki->ki_args = fmt(kvm_getargv, ki, ki->ki_p->ki_comm, 53653276Speter MAXCOMLEN); 53731552Sdyson } else if (needcomm) { 53869896Smckusick ki->ki_args = malloc(strlen(ki->ki_p->ki_comm) + 3); 53969896Smckusick sprintf(ki->ki_args, "(%s)", ki->ki_p->ki_comm); 54053276Speter } else { 54153276Speter ki->ki_args = NULL; 54253276Speter } 54353276Speter if (needenv && UREADOK(ki)) { 54453276Speter ki->ki_env = fmt(kvm_getenvv, ki, (char *)NULL, 0); 54553276Speter } else if (needenv) { 54653276Speter ki->ki_env = malloc(3); 54753276Speter strcpy(ki->ki_env, "()"); 54853276Speter } else { 54953276Speter ki->ki_env = NULL; 55053276Speter } 5511556Srgrimes} 5521556Srgrimes 5531556Srgrimesstatic int 5541556Srgrimespscomp(a, b) 5551556Srgrimes const void *a, *b; 5561556Srgrimes{ 5571556Srgrimes int i; 55869896Smckusick#define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \ 55969896Smckusick (k)->ki_p->ki_tsize) 5601556Srgrimes 5611556Srgrimes if (sortby == SORTCPU) 5621556Srgrimes return (getpcpu((KINFO *)b) - getpcpu((KINFO *)a)); 5631556Srgrimes if (sortby == SORTMEM) 5641556Srgrimes return (VSIZE((KINFO *)b) - VSIZE((KINFO *)a)); 56569896Smckusick i = ((KINFO *)a)->ki_p->ki_tdev - ((KINFO *)b)->ki_p->ki_tdev; 5661556Srgrimes if (i == 0) 56769896Smckusick i = ((KINFO *)a)->ki_p->ki_pid - ((KINFO *)b)->ki_p->ki_pid; 5681556Srgrimes return (i); 5691556Srgrimes} 5701556Srgrimes 5711556Srgrimes/* 5721556Srgrimes * ICK (all for getopt), would rather hide the ugliness 5731556Srgrimes * here than taint the main code. 5741556Srgrimes * 5751556Srgrimes * ps foo -> ps -foo 5761556Srgrimes * ps 34 -> ps -p34 5771556Srgrimes * 5781556Srgrimes * The old convention that 't' with no trailing tty arg means the users 5791556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 5801556Srgrimes * feature is available with the option 'T', which takes no argument. 5811556Srgrimes */ 5821556Srgrimesstatic char * 5831556Srgrimeskludge_oldps_options(s) 5841556Srgrimes char *s; 5851556Srgrimes{ 5861556Srgrimes size_t len; 5871556Srgrimes char *newopts, *ns, *cp; 5881556Srgrimes 5891556Srgrimes len = strlen(s); 5901556Srgrimes if ((newopts = ns = malloc(len + 2)) == NULL) 5911556Srgrimes err(1, NULL); 5921556Srgrimes /* 5931556Srgrimes * options begin with '-' 5941556Srgrimes */ 5951556Srgrimes if (*s != '-') 5961556Srgrimes *ns++ = '-'; /* add option flag */ 5971556Srgrimes /* 5981556Srgrimes * gaze to end of argv[1] 5991556Srgrimes */ 6001556Srgrimes cp = s + len - 1; 6011556Srgrimes /* 6021556Srgrimes * if last letter is a 't' flag with no argument (in the context 6031556Srgrimes * of the oldps options -- option string NOT starting with a '-' -- 6041556Srgrimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 6051556Srgrimes */ 6061556Srgrimes if (*cp == 't' && *s != '-') 6071556Srgrimes *cp = 'T'; 6081556Srgrimes else { 6091556Srgrimes /* 6101556Srgrimes * otherwise check for trailing number, which *may* be a 6111556Srgrimes * pid. 6121556Srgrimes */ 6131556Srgrimes while (cp >= s && isdigit(*cp)) 6141556Srgrimes --cp; 6151556Srgrimes } 6161556Srgrimes cp++; 6171556Srgrimes memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 6181556Srgrimes ns += cp - s; 6191556Srgrimes /* 6201556Srgrimes * if there's a trailing number, and not a preceding 'p' (pid) or 6211556Srgrimes * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 6221556Srgrimes */ 6238855Srgrimes if (isdigit(*cp) && 6247165Sjoerg (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) && 6257165Sjoerg (cp - 1 == s || cp[-2] != 't')) 6261556Srgrimes *ns++ = 'p'; 6271556Srgrimes (void)strcpy(ns, cp); /* and append the number */ 6281556Srgrimes 6291556Srgrimes return (newopts); 6301556Srgrimes} 6311556Srgrimes 6321556Srgrimesstatic void 6331556Srgrimesusage() 6341556Srgrimes{ 6351556Srgrimes 63626465Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 63726465Scharnier "usage: ps [-aChjlmrSTuvwx] [-O|o fmt] [-p pid] [-t tty] [-U user]", 63826465Scharnier " [-M core] [-N system] [-W swap]", 63926465Scharnier " ps [-L]"); 6401556Srgrimes exit(1); 6411556Srgrimes} 642