ps.c revision 109502
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 3590143Smarkmstatic const char 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 4090143Smarkm#if 0 411556Srgrimes#ifndef lint 4236049Scharnierstatic char sccsid[] = "@(#)ps.c 8.4 (Berkeley) 4/2/94"; 4390143Smarkm#endif /* not lint */ 4436049Scharnier#endif 4599110Sobrien#include <sys/cdefs.h> 4699110Sobrien__FBSDID("$FreeBSD: head/bin/ps/ps.c 109502 2003-01-19 00:22:34Z jmallett $"); 471556Srgrimes 481556Srgrimes#include <sys/param.h> 493296Sdg#include <sys/user.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 <fcntl.h> 571556Srgrimes#include <kvm.h> 5813514Smpp#include <limits.h> 5973367Sache#include <locale.h> 601556Srgrimes#include <paths.h> 6190143Smarkm#include <pwd.h> 621556Srgrimes#include <stdio.h> 631556Srgrimes#include <stdlib.h> 641556Srgrimes#include <string.h> 651556Srgrimes#include <unistd.h> 661556Srgrimes 671556Srgrimes#include "ps.h" 681556Srgrimes 6966377Sbrian#define SEP ", \t" /* username separators */ 7066377Sbrian 7190143Smarkmstatic KINFO *kinfo; 7290143Smarkmstruct varent *vhead; 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 8197966Sjmalletttime_t now; /* current time(3) value */ 8297966Sjmallett 831556Srgrimesstatic int needuser, needcomm, needenv; 8431552Sdyson#if defined(LAZY_PS) 8531552Sdysonstatic int forceuread=0; 8631552Sdyson#else 8731552Sdysonstatic int forceuread=1; 8831552Sdyson#endif 891556Srgrimes 9090143Smarkmstatic enum sort { DEFAULT, SORTMEM, SORTCPU } sortby = DEFAULT; 911556Srgrimes 9290143Smarkmstatic const char *fmt(char **(*)(kvm_t *, const struct kinfo_proc *, int), 9390110Simp KINFO *, char *, int); 9490110Simpstatic char *kludge_oldps_options(char *); 9590110Simpstatic int pscomp(const void *, const void *); 9690110Simpstatic void saveuser(KINFO *); 9790110Simpstatic void scanvars(void); 9890110Simpstatic void dynsizevars(KINFO *); 9990110Simpstatic void sizevars(void); 10090110Simpstatic void usage(void); 10166377Sbrianstatic uid_t *getuids(const char *, int *); 1021556Srgrimes 10397875Sjmallettstatic char dfmt[] = "pid,tt,state,time,command"; 10497875Sjmallettstatic char jfmt[] = "user,pid,ppid,pgid,jobc,state,tt,time,command"; 10597875Sjmallettstatic char lfmt[] = "uid,pid,ppid,cpu,pri,nice,vsz,rss,mwchan,state,tt,time,command"; 10690143Smarkmstatic char o1[] = "pid"; 10797875Sjmallettstatic char o2[] = "tt,state,time,command"; 10897875Sjmallettstatic char ufmt[] = "user,pid,%cpu,%mem,vsz,rss,tt,state,start,time,command"; 10997875Sjmallettstatic char vfmt[] = "pid,state,time,sl,re,pagein,vsz,rss,lim,tsiz,%cpu,%mem,command"; 110105831Srwatsonstatic char Zfmt[] = "label"; 1111556Srgrimes 11290143Smarkmstatic kvm_t *kd; 1131556Srgrimes 11498494Ssobomax#if defined(LAZY_PS) 11598494Ssobomax#define PS_ARGS "aCcefghjLlM:mN:O:o:p:rSTt:U:uvwxZ" 11698494Ssobomax#else 11798494Ssobomax#define PS_ARGS "aCceghjLlM:mN:O:o:p:rSTt:U:uvwxZ" 11898494Ssobomax#endif 11998494Ssobomax 1201556Srgrimesint 12190110Simpmain(int argc, 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; 12998494Ssobomax int all, ch, flag, i, _fmt, lineno, nentries, nocludge, dropgid; 13066377Sbrian int prtheader, wflag, what, xflg, uid, nuids; 13197804Stjr char *cols; 13290143Smarkm char errbuf[_POSIX2_LINE_MAX]; 13398494Ssobomax const char *cp, *nlistf, *memf; 1341556Srgrimes 13511809Sache (void) setlocale(LC_ALL, ""); 13697966Sjmallett /* Set the time to what it is right now. */ 13797966Sjmallett time(&now); 13811809Sache 13997804Stjr if ((cols = getenv("COLUMNS")) != NULL && *cols != '\0') 14097804Stjr termwidth = atoi(cols); 14197804Stjr else if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1421556Srgrimes ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 && 1431556Srgrimes ioctl(STDIN_FILENO, TIOCGWINSZ, (char *)&ws) == -1) || 1441556Srgrimes ws.ws_col == 0) 1451556Srgrimes termwidth = 79; 1461556Srgrimes else 1471556Srgrimes termwidth = ws.ws_col - 1; 1481556Srgrimes 14998494Ssobomax /* 15098494Ssobomax * Don't apply a kludge if the first argument is an option taking an 15198494Ssobomax * argument 15298494Ssobomax */ 15398494Ssobomax if (argc > 1) { 15498494Ssobomax nocludge = 0; 15598494Ssobomax if (argv[1][0] == '-') { 15698494Ssobomax for (cp = PS_ARGS; *cp != '\0'; cp++) { 15798494Ssobomax if (*cp != ':') 15898494Ssobomax continue; 15998494Ssobomax if (*(cp - 1) == argv[1][1]) { 16098494Ssobomax nocludge = 1; 16198494Ssobomax break; 16298494Ssobomax } 16398494Ssobomax } 16498494Ssobomax } 16598494Ssobomax if (nocludge == 0) 16698494Ssobomax argv[1] = kludge_oldps_options(argv[1]); 16798494Ssobomax } 1681556Srgrimes 16990143Smarkm all = _fmt = prtheader = wflag = xflg = 0; 1701556Srgrimes pid = -1; 17166377Sbrian nuids = 0; 17266377Sbrian uids = NULL; 1731556Srgrimes ttydev = NODEV; 17437317Sphk dropgid = 0; 17589909Sru memf = nlistf = _PATH_DEVNULL; 17698494Ssobomax while ((ch = getopt(argc, argv, PS_ARGS)) != -1) 1771556Srgrimes switch((char)ch) { 1781556Srgrimes case 'a': 1791556Srgrimes all = 1; 1801556Srgrimes break; 18119068Speter case 'C': 18219068Speter rawcpu = 1; 18319068Speter break; 18419068Speter case 'c': 18519068Speter cflag = 1; 18619068Speter break; 1871556Srgrimes case 'e': /* XXX set ufmt */ 1881556Srgrimes needenv = 1; 1891556Srgrimes break; 1901556Srgrimes case 'g': 1911556Srgrimes break; /* no-op */ 1921556Srgrimes case 'h': 1931556Srgrimes prtheader = ws.ws_row > 5 ? ws.ws_row : 22; 1941556Srgrimes break; 1951556Srgrimes case 'j': 196109502Sjmallett parsefmt(jfmt, 0); 19790143Smarkm _fmt = 1; 1981556Srgrimes jfmt[0] = '\0'; 1991556Srgrimes break; 2001556Srgrimes case 'L': 2011556Srgrimes showkey(); 2021556Srgrimes exit(0); 2031556Srgrimes case 'l': 204109502Sjmallett parsefmt(lfmt, 0); 20590143Smarkm _fmt = 1; 2061556Srgrimes lfmt[0] = '\0'; 2071556Srgrimes break; 2081556Srgrimes case 'M': 2091556Srgrimes memf = optarg; 21037317Sphk dropgid = 1; 2111556Srgrimes break; 2121556Srgrimes case 'm': 2131556Srgrimes sortby = SORTMEM; 2141556Srgrimes break; 2151556Srgrimes case 'N': 2161556Srgrimes nlistf = optarg; 21737317Sphk dropgid = 1; 2181556Srgrimes break; 2191556Srgrimes case 'O': 220109502Sjmallett parsefmt(o1, 1); 221109502Sjmallett parsefmt(optarg, 1); 222109502Sjmallett parsefmt(o2, 1); 2231556Srgrimes o1[0] = o2[0] = '\0'; 22490143Smarkm _fmt = 1; 2251556Srgrimes break; 2261556Srgrimes case 'o': 227109502Sjmallett parsefmt(optarg, 1); 22890143Smarkm _fmt = 1; 2291556Srgrimes break; 23031552Sdyson#if defined(LAZY_PS) 23131552Sdyson case 'f': 23231552Sdyson if (getuid() == 0 || getgid() == 0) 23331552Sdyson forceuread = 1; 23431552Sdyson break; 23531552Sdyson#endif 2361556Srgrimes case 'p': 2371556Srgrimes pid = atol(optarg); 2381556Srgrimes xflg = 1; 2391556Srgrimes break; 2401556Srgrimes case 'r': 2411556Srgrimes sortby = SORTCPU; 2421556Srgrimes break; 2431556Srgrimes case 'S': 2441556Srgrimes sumrusage = 1; 2451556Srgrimes break; 2461556Srgrimes case 'T': 2471556Srgrimes if ((optarg = ttyname(STDIN_FILENO)) == NULL) 2481556Srgrimes errx(1, "stdin: not a terminal"); 2491556Srgrimes /* FALLTHROUGH */ 2501556Srgrimes case 't': { 2511556Srgrimes struct stat sb; 25277459Simp char *ttypath, pathbuf[PATH_MAX]; 2531556Srgrimes 2541556Srgrimes if (strcmp(optarg, "co") == 0) 25590143Smarkm ttypath = strdup(_PATH_CONSOLE); 2561556Srgrimes else if (*optarg != '/') 2571556Srgrimes (void)snprintf(ttypath = pathbuf, 2581556Srgrimes sizeof(pathbuf), "%s%s", _PATH_TTY, optarg); 2591556Srgrimes else 2601556Srgrimes ttypath = optarg; 2611556Srgrimes if (stat(ttypath, &sb) == -1) 2621556Srgrimes err(1, "%s", ttypath); 2631556Srgrimes if (!S_ISCHR(sb.st_mode)) 2641556Srgrimes errx(1, "%s: not a terminal", ttypath); 2651556Srgrimes ttydev = sb.st_rdev; 2661556Srgrimes break; 2671556Srgrimes } 26813020Speter case 'U': 26966377Sbrian uids = getuids(optarg, &nuids); 27013020Speter xflg++; /* XXX: intuitive? */ 27113020Speter break; 2721556Srgrimes case 'u': 273109502Sjmallett parsefmt(ufmt, 0); 2741556Srgrimes sortby = SORTCPU; 27590143Smarkm _fmt = 1; 2761556Srgrimes ufmt[0] = '\0'; 2771556Srgrimes break; 2781556Srgrimes case 'v': 279109502Sjmallett parsefmt(vfmt, 0); 2801556Srgrimes sortby = SORTMEM; 28190143Smarkm _fmt = 1; 2821556Srgrimes vfmt[0] = '\0'; 2831556Srgrimes break; 2841556Srgrimes case 'w': 2851556Srgrimes if (wflag) 2861556Srgrimes termwidth = UNLIMITED; 2871556Srgrimes else if (termwidth < 131) 2881556Srgrimes termwidth = 131; 2891556Srgrimes wflag++; 2901556Srgrimes break; 2911556Srgrimes case 'x': 2921556Srgrimes xflg = 1; 2931556Srgrimes break; 29486922Sgreen case 'Z': 295109502Sjmallett parsefmt(Zfmt, 0); 29686922Sgreen Zfmt[0] = '\0'; 29786922Sgreen break; 2981556Srgrimes case '?': 2991556Srgrimes default: 3001556Srgrimes usage(); 3011556Srgrimes } 3021556Srgrimes argc -= optind; 3031556Srgrimes argv += optind; 3041556Srgrimes 3051556Srgrimes#define BACKWARD_COMPATIBILITY 3061556Srgrimes#ifdef BACKWARD_COMPATIBILITY 3071556Srgrimes if (*argv) { 3081556Srgrimes nlistf = *argv; 3091556Srgrimes if (*++argv) { 3101556Srgrimes memf = *argv; 3111556Srgrimes } 3121556Srgrimes } 3131556Srgrimes#endif 3141556Srgrimes /* 3151556Srgrimes * Discard setgid privileges if not the running kernel so that bad 3161556Srgrimes * guys can't print interesting stuff from kernel memory. 3171556Srgrimes */ 31837317Sphk if (dropgid) { 3191556Srgrimes setgid(getgid()); 32037317Sphk setuid(getuid()); 32137317Sphk } 3221556Srgrimes 32389909Sru kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf); 3241556Srgrimes if (kd == 0) 3251556Srgrimes errx(1, "%s", errbuf); 3261556Srgrimes 32790143Smarkm if (!_fmt) 328109502Sjmallett parsefmt(dfmt, 0); 3291556Srgrimes 33013020Speter /* XXX - should be cleaner */ 33166377Sbrian if (!all && ttydev == NODEV && pid == -1 && !nuids) { 33266377Sbrian if ((uids = malloc(sizeof (*uids))) == NULL) 33397877Sjmallett errx(1, "malloc failed"); 33466377Sbrian nuids = 1; 33566377Sbrian *uids = getuid(); 33666377Sbrian } 3371556Srgrimes 3381556Srgrimes /* 3391556Srgrimes * scan requested variables, noting what structures are needed, 34053170Skris * and adjusting header widths as appropriate. 3411556Srgrimes */ 3421556Srgrimes scanvars(); 3431556Srgrimes /* 3441556Srgrimes * get proc list 3451556Srgrimes */ 34666377Sbrian if (nuids == 1) { 3471556Srgrimes what = KERN_PROC_UID; 34866377Sbrian flag = *uids; 3491556Srgrimes } else if (ttydev != NODEV) { 3501556Srgrimes what = KERN_PROC_TTY; 3511556Srgrimes flag = ttydev; 3521556Srgrimes } else if (pid != -1) { 3531556Srgrimes what = KERN_PROC_PID; 3541556Srgrimes flag = pid; 3551556Srgrimes } else { 3561556Srgrimes what = KERN_PROC_ALL; 3571556Srgrimes flag = 0; 3581556Srgrimes } 3591556Srgrimes /* 3601556Srgrimes * select procs 3611556Srgrimes */ 36282267Speter if ((kp = kvm_getprocs(kd, what, flag, &nentries)) == 0 || nentries < 0) 3631556Srgrimes errx(1, "%s", kvm_geterr(kd)); 3641556Srgrimes if ((kinfo = malloc(nentries * sizeof(*kinfo))) == NULL) 36597877Sjmallett errx(1, "malloc failed"); 3661556Srgrimes for (i = nentries; --i >= 0; ++kp) { 3671556Srgrimes kinfo[i].ki_p = kp; 3681556Srgrimes if (needuser) 3691556Srgrimes saveuser(&kinfo[i]); 37025271Sjkh dynsizevars(&kinfo[i]); 3711556Srgrimes } 37225271Sjkh 37325271Sjkh sizevars(); 37425271Sjkh 3751556Srgrimes /* 3761556Srgrimes * print header 3771556Srgrimes */ 3781556Srgrimes printheader(); 3791556Srgrimes if (nentries == 0) 38062803Swill exit(1); 3811556Srgrimes /* 3821556Srgrimes * sort proc list 3831556Srgrimes */ 3841556Srgrimes qsort(kinfo, nentries, sizeof(KINFO), pscomp); 3851556Srgrimes /* 3861556Srgrimes * for each proc, call each variable output function. 3871556Srgrimes */ 3881556Srgrimes for (i = lineno = 0; i < nentries; i++) { 38969896Smckusick if (xflg == 0 && ((&kinfo[i])->ki_p->ki_tdev == NODEV || 39069896Smckusick ((&kinfo[i])->ki_p->ki_flag & P_CONTROLT ) == 0)) 3911556Srgrimes continue; 39266377Sbrian if (nuids > 1) { 39366377Sbrian for (uid = 0; uid < nuids; uid++) 39469896Smckusick if ((&kinfo[i])->ki_p->ki_uid == uids[uid]) 39566377Sbrian break; 39666377Sbrian if (uid == nuids) 39766377Sbrian continue; 39866377Sbrian } 3991556Srgrimes for (vent = vhead; vent; vent = vent->next) { 4001556Srgrimes (vent->var->oproc)(&kinfo[i], vent); 4011556Srgrimes if (vent->next != NULL) 4021556Srgrimes (void)putchar(' '); 4031556Srgrimes } 4041556Srgrimes (void)putchar('\n'); 4051556Srgrimes if (prtheader && lineno++ == prtheader - 4) { 4061556Srgrimes (void)putchar('\n'); 4071556Srgrimes printheader(); 4081556Srgrimes lineno = 0; 4091556Srgrimes } 4101556Srgrimes } 41166377Sbrian free(uids); 41266377Sbrian 4131556Srgrimes exit(eval); 4141556Srgrimes} 4151556Srgrimes 41666377Sbrianuid_t * 41766377Sbriangetuids(const char *arg, int *nuids) 41866377Sbrian{ 41999580Srobert char name[MAXLOGNAME]; 42066377Sbrian struct passwd *pwd; 42166377Sbrian uid_t *uids, *moreuids; 42290143Smarkm int alloc; 42390143Smarkm size_t l; 42466377Sbrian 42566377Sbrian 42666377Sbrian alloc = 0; 42766377Sbrian *nuids = 0; 42866377Sbrian uids = NULL; 42966377Sbrian for (; (l = strcspn(arg, SEP)) > 0; arg += l + strspn(arg + l, SEP)) { 43066377Sbrian if (l >= sizeof name) { 43190143Smarkm warnx("%.*s: name too long", (int)l, arg); 43266377Sbrian continue; 43366377Sbrian } 43466377Sbrian strncpy(name, arg, l); 43566377Sbrian name[l] = '\0'; 43666377Sbrian if ((pwd = getpwnam(name)) == NULL) { 43766377Sbrian warnx("%s: no such user", name); 43866377Sbrian continue; 43966377Sbrian } 44066377Sbrian if (*nuids >= alloc) { 44166377Sbrian alloc = (alloc + 1) << 1; 44266377Sbrian moreuids = realloc(uids, alloc * sizeof (*uids)); 44366377Sbrian if (moreuids == NULL) { 44466377Sbrian free(uids); 44597877Sjmallett errx(1, "realloc failed"); 44666377Sbrian } 44766377Sbrian uids = moreuids; 44866377Sbrian } 44966377Sbrian uids[(*nuids)++] = pwd->pw_uid; 45066377Sbrian } 45166377Sbrian endpwent(); 45266377Sbrian 45366377Sbrian if (!*nuids) 45466377Sbrian errx(1, "No users specified"); 45566377Sbrian 45666377Sbrian return uids; 45766377Sbrian} 45866377Sbrian 459109502SjmallettVARENT * 460109502Sjmallettfind_varentry(VAR *v) 461109502Sjmallett{ 462109502Sjmallett struct varent *vent; 463109502Sjmallett 464109502Sjmallett for (vent = vhead; vent; vent = vent->next) { 465109502Sjmallett if (strcmp(vent->var->name, v->name) == 0) 466109502Sjmallett return vent; 467109502Sjmallett } 468109502Sjmallett return NULL; 469109502Sjmallett} 470109502Sjmallett 4711556Srgrimesstatic void 47290110Simpscanvars(void) 4731556Srgrimes{ 4741556Srgrimes struct varent *vent; 4751556Srgrimes VAR *v; 47625271Sjkh 47725271Sjkh for (vent = vhead; vent; vent = vent->next) { 47825271Sjkh v = vent->var; 47925271Sjkh if (v->flag & DSIZ) { 48025271Sjkh v->dwidth = v->width; 48125271Sjkh v->width = 0; 48225271Sjkh } 48325271Sjkh if (v->flag & USER) 48425271Sjkh needuser = 1; 48525271Sjkh if (v->flag & COMM) 48625271Sjkh needcomm = 1; 48725271Sjkh } 48825271Sjkh} 48925271Sjkh 49025271Sjkhstatic void 49190110Simpdynsizevars(KINFO *ki) 49225271Sjkh{ 49325271Sjkh struct varent *vent; 49425271Sjkh VAR *v; 4951556Srgrimes int i; 4961556Srgrimes 4971556Srgrimes for (vent = vhead; vent; vent = vent->next) { 4981556Srgrimes v = vent->var; 49925271Sjkh if (!(v->flag & DSIZ)) 50025271Sjkh continue; 50125271Sjkh i = (v->sproc)( ki); 50225271Sjkh if (v->width < i) 50325271Sjkh v->width = i; 50425271Sjkh if (v->width > v->dwidth) 50525271Sjkh v->width = v->dwidth; 50625271Sjkh } 50725271Sjkh} 50825271Sjkh 50925271Sjkhstatic void 51090110Simpsizevars(void) 51125271Sjkh{ 51225271Sjkh struct varent *vent; 51325271Sjkh VAR *v; 51425271Sjkh int i; 51525271Sjkh 51625271Sjkh for (vent = vhead; vent; vent = vent->next) { 51725271Sjkh v = vent->var; 5181556Srgrimes i = strlen(v->header); 5191556Srgrimes if (v->width < i) 5201556Srgrimes v->width = i; 5211556Srgrimes totwidth += v->width + 1; /* +1 for space */ 5221556Srgrimes } 5231556Srgrimes totwidth--; 5241556Srgrimes} 5251556Srgrimes 52690143Smarkmstatic const char * 52790110Simpfmt(char **(*fn)(kvm_t *, const struct kinfo_proc *, int), KINFO *ki, 52890110Simp char *comm, int maxlen) 5291556Srgrimes{ 53090143Smarkm const char *s; 5311556Srgrimes 53290143Smarkm s = fmt_argv((*fn)(kd, ki->ki_p, termwidth), comm, maxlen); 5331556Srgrimes return (s); 5341556Srgrimes} 5351556Srgrimes 53671578Sjhb#define UREADOK(ki) (forceuread || (ki->ki_p->ki_sflag & PS_INMEM)) 53731552Sdyson 5381556Srgrimesstatic void 53990110Simpsaveuser(KINFO *ki) 5401556Srgrimes{ 5411556Srgrimes 54271578Sjhb if (ki->ki_p->ki_sflag & PS_INMEM) { 5431556Srgrimes /* 5441556Srgrimes * The u-area might be swapped out, and we can't get 5451556Srgrimes * at it because we have a crashdump and no swap. 5461556Srgrimes * If it's here fill in these fields, otherwise, just 5471556Srgrimes * leave them 0. 5481556Srgrimes */ 54969896Smckusick ki->ki_valid = 1; 5501556Srgrimes } else 55169896Smckusick ki->ki_valid = 0; 5521556Srgrimes /* 5531556Srgrimes * save arguments if needed 5541556Srgrimes */ 55569896Smckusick if (needcomm && (UREADOK(ki) || (ki->ki_p->ki_args != NULL))) { 55690143Smarkm ki->ki_args = strdup(fmt(kvm_getargv, ki, ki->ki_p->ki_comm, 55790143Smarkm MAXCOMLEN)); 55831552Sdyson } else if (needcomm) { 55990143Smarkm asprintf(&ki->ki_args, "(%s)", ki->ki_p->ki_comm); 56053276Speter } else { 56153276Speter ki->ki_args = NULL; 56253276Speter } 56353276Speter if (needenv && UREADOK(ki)) { 56490143Smarkm ki->ki_env = strdup(fmt(kvm_getenvv, ki, (char *)NULL, 0)); 56553276Speter } else if (needenv) { 56653276Speter ki->ki_env = malloc(3); 56753276Speter strcpy(ki->ki_env, "()"); 56853276Speter } else { 56953276Speter ki->ki_env = NULL; 57053276Speter } 5711556Srgrimes} 5721556Srgrimes 5731556Srgrimesstatic int 57490110Simppscomp(const void *a, const void *b) 5751556Srgrimes{ 5761556Srgrimes int i; 57769896Smckusick#define VSIZE(k) ((k)->ki_p->ki_dsize + (k)->ki_p->ki_ssize + \ 57869896Smckusick (k)->ki_p->ki_tsize) 5791556Srgrimes 5801556Srgrimes if (sortby == SORTCPU) 58190143Smarkm return (getpcpu((const KINFO *)b) - getpcpu((const KINFO *)a)); 5821556Srgrimes if (sortby == SORTMEM) 58390143Smarkm return (VSIZE((const KINFO *)b) - VSIZE((const KINFO *)a)); 58490143Smarkm i = (int)((const KINFO *)a)->ki_p->ki_tdev - (int)((const KINFO *)b)->ki_p->ki_tdev; 5851556Srgrimes if (i == 0) 58690143Smarkm i = ((const KINFO *)a)->ki_p->ki_pid - ((const KINFO *)b)->ki_p->ki_pid; 5871556Srgrimes return (i); 5881556Srgrimes} 5891556Srgrimes 5901556Srgrimes/* 5911556Srgrimes * ICK (all for getopt), would rather hide the ugliness 5921556Srgrimes * here than taint the main code. 5931556Srgrimes * 5941556Srgrimes * ps foo -> ps -foo 5951556Srgrimes * ps 34 -> ps -p34 5961556Srgrimes * 5971556Srgrimes * The old convention that 't' with no trailing tty arg means the users 5981556Srgrimes * tty, is only supported if argv[1] doesn't begin with a '-'. This same 5991556Srgrimes * feature is available with the option 'T', which takes no argument. 6001556Srgrimes */ 6011556Srgrimesstatic char * 60290110Simpkludge_oldps_options(char *s) 6031556Srgrimes{ 604102886Sjmallett int have_fmt; 6051556Srgrimes size_t len; 6061556Srgrimes char *newopts, *ns, *cp; 6071556Srgrimes 608102886Sjmallett /* 609102886Sjmallett * If we have an 'o' option, then note it, since we don't want to do 610102886Sjmallett * some types of munging. 611102886Sjmallett */ 612102886Sjmallett have_fmt = index(s, 'o') != NULL; 613102886Sjmallett 6141556Srgrimes len = strlen(s); 6151556Srgrimes if ((newopts = ns = malloc(len + 2)) == NULL) 61697877Sjmallett errx(1, "malloc failed"); 6171556Srgrimes /* 6181556Srgrimes * options begin with '-' 6191556Srgrimes */ 6201556Srgrimes if (*s != '-') 6211556Srgrimes *ns++ = '-'; /* add option flag */ 6221556Srgrimes /* 6231556Srgrimes * gaze to end of argv[1] 6241556Srgrimes */ 6251556Srgrimes cp = s + len - 1; 6261556Srgrimes /* 6271556Srgrimes * if last letter is a 't' flag with no argument (in the context 6281556Srgrimes * of the oldps options -- option string NOT starting with a '-' -- 6291556Srgrimes * then convert to 'T' (meaning *this* terminal, i.e. ttyname(0)). 63081743Sbrian * 63181743Sbrian * However, if a flag accepting a string argument is found in the 63281743Sbrian * option string, the remainder of the string is the argument to 63381743Sbrian * that flag; do not modify that argument. 6341556Srgrimes */ 63589909Sru if (strcspn(s, "MNOoU") == len && *cp == 't' && *s != '-') 6361556Srgrimes *cp = 'T'; 6371556Srgrimes else { 6381556Srgrimes /* 6391556Srgrimes * otherwise check for trailing number, which *may* be a 6401556Srgrimes * pid. 6411556Srgrimes */ 6421556Srgrimes while (cp >= s && isdigit(*cp)) 6431556Srgrimes --cp; 6441556Srgrimes } 6451556Srgrimes cp++; 6461556Srgrimes memmove(ns, s, (size_t)(cp - s)); /* copy up to trailing number */ 6471556Srgrimes ns += cp - s; 6481556Srgrimes /* 6491556Srgrimes * if there's a trailing number, and not a preceding 'p' (pid) or 6501556Srgrimes * 't' (tty) flag, then assume it's a pid and insert a 'p' flag. 6511556Srgrimes */ 6528855Srgrimes if (isdigit(*cp) && 6537165Sjoerg (cp == s || (cp[-1] != 't' && cp[-1] != 'p')) && 654102886Sjmallett (cp - 1 == s || cp[-2] != 't') && !have_fmt) 6551556Srgrimes *ns++ = 'p'; 6561556Srgrimes (void)strcpy(ns, cp); /* and append the number */ 6571556Srgrimes 6581556Srgrimes return (newopts); 6591556Srgrimes} 6601556Srgrimes 6611556Srgrimesstatic void 66290110Simpusage(void) 6631556Srgrimes{ 6641556Srgrimes 66526465Scharnier (void)fprintf(stderr, "%s\n%s\n%s\n", 666105831Srwatson "usage: ps [-aChjlmrSTuvwxZ] [-O|o fmt] [-p pid] [-t tty] [-U user]", 66789909Sru " [-M core] [-N system]", 66826465Scharnier " ps [-L]"); 6691556Srgrimes exit(1); 6701556Srgrimes} 671