Home | History | Annotate | Line # | Download | only in ps
ps.c revision 1.49
      1  1.49       dsl /*	$NetBSD: ps.c,v 1.49 2003/03/06 09:02:16 dsl Exp $	*/
      2  1.13       cgd 
      3  1.40    simonb /*
      4  1.40    simonb  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  1.40    simonb  * All rights reserved.
      6  1.40    simonb  *
      7  1.40    simonb  * This code is derived from software contributed to The NetBSD Foundation
      8  1.40    simonb  * by Simon Burge.
      9  1.40    simonb  *
     10  1.40    simonb  * Redistribution and use in source and binary forms, with or without
     11  1.40    simonb  * modification, are permitted provided that the following conditions
     12  1.40    simonb  * are met:
     13  1.40    simonb  * 1. Redistributions of source code must retain the above copyright
     14  1.40    simonb  *    notice, this list of conditions and the following disclaimer.
     15  1.40    simonb  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.40    simonb  *    notice, this list of conditions and the following disclaimer in the
     17  1.40    simonb  *    documentation and/or other materials provided with the distribution.
     18  1.40    simonb  * 3. All advertising materials mentioning features or use of this software
     19  1.40    simonb  *    must display the following acknowledgement:
     20  1.40    simonb  *        This product includes software developed by the NetBSD
     21  1.40    simonb  *        Foundation, Inc. and its contributors.
     22  1.40    simonb  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.40    simonb  *    contributors may be used to endorse or promote products derived
     24  1.40    simonb  *    from this software without specific prior written permission.
     25  1.40    simonb  *
     26  1.40    simonb  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.40    simonb  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.40    simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.40    simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.40    simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.40    simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.40    simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.40    simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.40    simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.40    simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.40    simonb  * POSSIBILITY OF SUCH DAMAGE.
     37  1.40    simonb  */
     38  1.40    simonb 
     39  1.40    simonb /*
     40  1.11       cgd  * Copyright (c) 1990, 1993, 1994
     41  1.11       cgd  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.1       cgd  *    must display the following acknowledgement:
     53   1.1       cgd  *	This product includes software developed by the University of
     54   1.1       cgd  *	California, Berkeley and its contributors.
     55   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.1       cgd  *    may be used to endorse or promote products derived from this software
     57   1.1       cgd  *    without specific prior written permission.
     58   1.1       cgd  *
     59   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1       cgd  * SUCH DAMAGE.
     70   1.1       cgd  */
     71   1.1       cgd 
     72  1.19  christos #include <sys/cdefs.h>
     73   1.1       cgd #ifndef lint
     74  1.19  christos __COPYRIGHT("@(#) Copyright (c) 1990, 1993, 1994\n\
     75  1.19  christos 	The Regents of the University of California.  All rights reserved.\n");
     76   1.1       cgd #endif /* not lint */
     77   1.1       cgd 
     78   1.1       cgd #ifndef lint
     79  1.13       cgd #if 0
     80  1.11       cgd static char sccsid[] = "@(#)ps.c	8.4 (Berkeley) 4/2/94";
     81  1.13       cgd #else
     82  1.49       dsl __RCSID("$NetBSD: ps.c,v 1.49 2003/03/06 09:02:16 dsl Exp $");
     83  1.13       cgd #endif
     84   1.1       cgd #endif /* not lint */
     85   1.1       cgd 
     86   1.1       cgd #include <sys/param.h>
     87   1.1       cgd #include <sys/user.h>
     88   1.1       cgd #include <sys/time.h>
     89   1.1       cgd #include <sys/resource.h>
     90  1.48   thorpej #include <sys/lwp.h>
     91   1.1       cgd #include <sys/proc.h>
     92   1.1       cgd #include <sys/stat.h>
     93   1.1       cgd #include <sys/ioctl.h>
     94  1.11       cgd #include <sys/sysctl.h>
     95  1.11       cgd 
     96  1.49       dsl #include <stddef.h>
     97  1.11       cgd #include <ctype.h>
     98  1.11       cgd #include <err.h>
     99  1.11       cgd #include <errno.h>
    100  1.11       cgd #include <fcntl.h>
    101  1.11       cgd #include <kvm.h>
    102  1.17        pk #include <limits.h>
    103   1.1       cgd #include <nlist.h>
    104  1.11       cgd #include <paths.h>
    105  1.25   mycroft #include <pwd.h>
    106   1.1       cgd #include <stdio.h>
    107   1.1       cgd #include <stdlib.h>
    108   1.1       cgd #include <string.h>
    109  1.11       cgd #include <unistd.h>
    110  1.11       cgd 
    111   1.1       cgd #include "ps.h"
    112   1.1       cgd 
    113  1.40    simonb /*
    114  1.40    simonb  * ARGOPTS must contain all option characters that take arguments
    115  1.40    simonb  * (except for 't'!) - it is used in kludge_oldps_options()
    116  1.40    simonb  */
    117  1.49       dsl #define	GETOPTSTR	"acCeghjk:LlM:mN:O:o:p:rSsTt:U:uvW:wx"
    118  1.49       dsl #define	ARGOPTS		"kMNOopUW"
    119  1.40    simonb 
    120  1.38    simonb struct kinfo_proc2 *kinfo;
    121  1.49       dsl struct varent *vhead, *sorthead;
    122   1.1       cgd 
    123   1.1       cgd int	eval;			/* exit value */
    124   1.1       cgd int	rawcpu;			/* -C */
    125   1.1       cgd int	sumrusage;		/* -S */
    126   1.1       cgd int	termwidth;		/* width of screen (0 == infinity) */
    127   1.1       cgd int	totwidth;		/* calculated width of requested variables */
    128   1.1       cgd 
    129  1.48   thorpej int	needcomm, needenv, commandonly;
    130  1.33    simonb uid_t	myuid;
    131   1.1       cgd 
    132  1.48   thorpej static struct kinfo_lwp
    133  1.48   thorpej 		*pick_representative_lwp __P((struct kinfo_proc2 *,
    134  1.48   thorpej 		    struct kinfo_lwp *, int));
    135  1.38    simonb static struct kinfo_proc2
    136  1.38    simonb 		*getkinfo_kvm __P((kvm_t *, int, int, int *));
    137  1.11       cgd static char	*kludge_oldps_options __P((char *));
    138  1.11       cgd static int	 pscomp __P((const void *, const void *));
    139  1.11       cgd static void	 scanvars __P((void));
    140  1.11       cgd static void	 usage __P((void));
    141  1.19  christos int		 main __P((int, char *[]));
    142   1.1       cgd 
    143   1.1       cgd char dfmt[] = "pid tt state time command";
    144   1.1       cgd char jfmt[] = "user pid ppid pgid sess jobc state tt time command";
    145   1.1       cgd char lfmt[] = "uid pid ppid cpu pri nice vsz rss wchan state tt time command";
    146   1.1       cgd char   o1[] = "pid";
    147   1.1       cgd char   o2[] = "tt state time command";
    148  1.48   thorpej char sfmt[] = "uid pid ppid cpu lid nlwp pri nice vsz rss wchan lstate tt time command";
    149   1.1       cgd char ufmt[] = "user pid %cpu %mem vsz rss tt state start time command";
    150  1.11       cgd char vfmt[] = "pid state time sl re pagein vsz rss lim tsiz %cpu %mem command";
    151  1.11       cgd 
    152  1.11       cgd kvm_t *kd;
    153   1.1       cgd 
    154  1.11       cgd int
    155   1.1       cgd main(argc, argv)
    156   1.1       cgd 	int argc;
    157  1.11       cgd 	char *argv[];
    158   1.1       cgd {
    159  1.11       cgd 	struct varent *vent;
    160   1.1       cgd 	struct winsize ws;
    161  1.48   thorpej 	struct kinfo_lwp *kl, *l;
    162  1.48   thorpej 	int ch, flag, i, j, fmt, lineno, nentries, nlwps;
    163  1.48   thorpej 	int prtheader, wflag, what, xflg, mode, showlwps;
    164  1.17        pk 	char *nlistf, *memf, *swapf, errbuf[_POSIX2_LINE_MAX];
    165  1.26       kim 	char *ttname;
    166   1.1       cgd 
    167  1.38    simonb 	if ((ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
    168  1.38    simonb 	     ioctl(STDERR_FILENO, TIOCGWINSZ, (char *)&ws) == -1 &&
    169  1.38    simonb 	     ioctl(STDIN_FILENO,  TIOCGWINSZ, (char *)&ws) == -1) ||
    170   1.1       cgd 	     ws.ws_col == 0)
    171   1.1       cgd 		termwidth = 79;
    172   1.1       cgd 	else
    173   1.1       cgd 		termwidth = ws.ws_col - 1;
    174   1.1       cgd 
    175   1.1       cgd 	if (argc > 1)
    176   1.1       cgd 		argv[1] = kludge_oldps_options(argv[1]);
    177   1.1       cgd 
    178  1.48   thorpej 	fmt = prtheader = wflag = xflg = showlwps = 0;
    179  1.25   mycroft 	what = KERN_PROC_UID;
    180  1.33    simonb 	flag = myuid = getuid();
    181   1.1       cgd 	memf = nlistf = swapf = NULL;
    182  1.39    simonb 	mode = PRINTMODE;
    183  1.40    simonb 	while ((ch = getopt(argc, argv, GETOPTSTR)) != -1)
    184   1.1       cgd 		switch((char)ch) {
    185   1.1       cgd 		case 'a':
    186  1.25   mycroft 			what = KERN_PROC_ALL;
    187  1.25   mycroft 			flag = 0;
    188   1.1       cgd 			break;
    189  1.12   mycroft 		case 'c':
    190  1.12   mycroft 			commandonly = 1;
    191  1.12   mycroft 			break;
    192  1.11       cgd 		case 'e':			/* XXX set ufmt */
    193  1.11       cgd 			needenv = 1;
    194  1.11       cgd 			break;
    195   1.1       cgd 		case 'C':
    196   1.1       cgd 			rawcpu = 1;
    197   1.1       cgd 			break;
    198   1.1       cgd 		case 'g':
    199  1.11       cgd 			break;			/* no-op */
    200   1.1       cgd 		case 'h':
    201   1.1       cgd 			prtheader = ws.ws_row > 5 ? ws.ws_row : 22;
    202   1.1       cgd 			break;
    203   1.1       cgd 		case 'j':
    204   1.1       cgd 			parsefmt(jfmt);
    205  1.15   mycroft 			fmt = 1;
    206   1.1       cgd 			jfmt[0] = '\0';
    207   1.1       cgd 			break;
    208  1.49       dsl 		case 'k':
    209  1.49       dsl 			parsesort(optarg);
    210  1.49       dsl 			break;
    211  1.28  bgrayson 		case 'K':
    212  1.47  jdolecek 			break;			/* no-op - was dontuseprocfs */
    213  1.11       cgd 		case 'L':
    214   1.1       cgd 			showkey();
    215   1.1       cgd 			exit(0);
    216  1.23   mycroft 			/* NOTREACHED */
    217   1.1       cgd 		case 'l':
    218   1.1       cgd 			parsefmt(lfmt);
    219  1.15   mycroft 			fmt = 1;
    220   1.1       cgd 			lfmt[0] = '\0';
    221   1.1       cgd 			break;
    222   1.1       cgd 		case 'M':
    223   1.1       cgd 			memf = optarg;
    224   1.1       cgd 			break;
    225   1.1       cgd 		case 'm':
    226  1.49       dsl 			parsesort("vsz");
    227   1.1       cgd 			break;
    228   1.1       cgd 		case 'N':
    229   1.1       cgd 			nlistf = optarg;
    230   1.1       cgd 			break;
    231   1.1       cgd 		case 'O':
    232   1.1       cgd 			parsefmt(o1);
    233   1.1       cgd 			parsefmt(optarg);
    234   1.1       cgd 			parsefmt(o2);
    235   1.1       cgd 			o1[0] = o2[0] = '\0';
    236  1.15   mycroft 			fmt = 1;
    237   1.1       cgd 			break;
    238   1.1       cgd 		case 'o':
    239   1.1       cgd 			parsefmt(optarg);
    240  1.15   mycroft 			fmt = 1;
    241   1.1       cgd 			break;
    242   1.1       cgd 		case 'p':
    243  1.25   mycroft 			what = KERN_PROC_PID;
    244  1.25   mycroft 			flag = atol(optarg);
    245   1.1       cgd 			xflg = 1;
    246   1.1       cgd 			break;
    247   1.1       cgd 		case 'r':
    248  1.49       dsl 			parsesort("%cpu");
    249   1.1       cgd 			break;
    250   1.1       cgd 		case 'S':
    251   1.1       cgd 			sumrusage = 1;
    252   1.1       cgd 			break;
    253  1.48   thorpej 		case 's':
    254  1.48   thorpej 			/* -L was already taken... */
    255  1.48   thorpej 			showlwps = 1;
    256  1.48   thorpej 			parsefmt(sfmt);
    257  1.48   thorpej 			fmt = 1;
    258  1.48   thorpej 			sfmt[0] = '\0';
    259  1.48   thorpej 			break;
    260   1.1       cgd 		case 'T':
    261  1.22   mycroft 			if ((ttname = ttyname(STDIN_FILENO)) == NULL)
    262  1.11       cgd 				errx(1, "stdin: not a terminal");
    263  1.22   mycroft 			goto tty;
    264  1.22   mycroft 		case 't':
    265  1.22   mycroft 			ttname = optarg;
    266  1.22   mycroft 		tty: {
    267  1.11       cgd 			struct stat sb;
    268  1.11       cgd 			char *ttypath, pathbuf[MAXPATHLEN];
    269   1.1       cgd 
    270  1.36    simonb 			flag = 0;
    271  1.36    simonb 			if (strcmp(ttname, "?") == 0)
    272  1.36    simonb 				flag = KERN_PROC_TTY_NODEV;
    273  1.36    simonb 			else if (strcmp(ttname, "-") == 0)
    274  1.36    simonb 				flag = KERN_PROC_TTY_REVOKE;
    275  1.36    simonb 			else if (strcmp(ttname, "co") == 0)
    276   1.1       cgd 				ttypath = _PATH_CONSOLE;
    277  1.26       kim 			else if (*ttname != '/')
    278  1.11       cgd 				(void)snprintf(ttypath = pathbuf,
    279  1.26       kim 				    sizeof(pathbuf), "%s%s", _PATH_TTY, ttname);
    280   1.1       cgd 			else
    281  1.26       kim 				ttypath = ttname;
    282  1.25   mycroft 			what = KERN_PROC_TTY;
    283  1.36    simonb 			if (flag == 0) {
    284  1.36    simonb 				if (stat(ttypath, &sb) == -1)
    285  1.36    simonb 					err(1, "%s", ttypath);
    286  1.36    simonb 				if (!S_ISCHR(sb.st_mode))
    287  1.36    simonb 					errx(1, "%s: not a terminal", ttypath);
    288  1.36    simonb 				flag = sb.st_rdev;
    289  1.36    simonb 			}
    290   1.1       cgd 			break;
    291   1.1       cgd 		}
    292  1.25   mycroft 		case 'U':
    293  1.25   mycroft 			if (*optarg != '\0') {
    294  1.25   mycroft 				struct passwd *pw;
    295  1.25   mycroft 				char *ep;
    296  1.25   mycroft 
    297  1.25   mycroft 				what = KERN_PROC_UID;
    298  1.25   mycroft 				pw = getpwnam(optarg);
    299  1.25   mycroft 				if (pw == NULL) {
    300  1.25   mycroft 					errno = 0;
    301  1.25   mycroft 					flag = strtoul(optarg, &ep, 10);
    302  1.25   mycroft 					if (errno)
    303  1.25   mycroft 						err(1, "%s", optarg);
    304  1.25   mycroft 					if (*ep != '\0')
    305  1.25   mycroft 						errx(1, "%s: illegal user name",
    306  1.25   mycroft 						    optarg);
    307  1.25   mycroft 				} else
    308  1.25   mycroft 					flag = pw->pw_uid;
    309  1.25   mycroft 			}
    310  1.25   mycroft 			break;
    311   1.1       cgd 		case 'u':
    312   1.1       cgd 			parsefmt(ufmt);
    313  1.49       dsl 			parsesort("%cpu");
    314  1.15   mycroft 			fmt = 1;
    315   1.1       cgd 			ufmt[0] = '\0';
    316   1.1       cgd 			break;
    317   1.1       cgd 		case 'v':
    318   1.1       cgd 			parsefmt(vfmt);
    319  1.49       dsl 			parsesort("vsz");
    320  1.15   mycroft 			fmt = 1;
    321   1.1       cgd 			vfmt[0] = '\0';
    322   1.1       cgd 			break;
    323   1.1       cgd 		case 'W':
    324   1.1       cgd 			swapf = optarg;
    325   1.1       cgd 			break;
    326   1.1       cgd 		case 'w':
    327   1.6       cgd 			if (wflag)
    328   1.6       cgd 				termwidth = UNLIMITED;
    329   1.6       cgd 			else if (termwidth < 131)
    330   1.1       cgd 				termwidth = 131;
    331  1.11       cgd 			wflag++;
    332   1.1       cgd 			break;
    333   1.1       cgd 		case 'x':
    334   1.1       cgd 			xflg = 1;
    335   1.1       cgd 			break;
    336   1.1       cgd 		case '?':
    337   1.1       cgd 		default:
    338   1.1       cgd 			usage();
    339   1.1       cgd 		}
    340   1.1       cgd 	argc -= optind;
    341   1.1       cgd 	argv += optind;
    342   1.1       cgd 
    343   1.1       cgd #define	BACKWARD_COMPATIBILITY
    344   1.1       cgd #ifdef	BACKWARD_COMPATIBILITY
    345   1.1       cgd 	if (*argv) {
    346   1.1       cgd 		nlistf = *argv;
    347   1.1       cgd 		if (*++argv) {
    348   1.1       cgd 			memf = *argv;
    349   1.1       cgd 			if (*++argv)
    350   1.1       cgd 				swapf = *argv;
    351   1.1       cgd 		}
    352   1.1       cgd 	}
    353   1.1       cgd #endif
    354  1.11       cgd 
    355  1.49       dsl 	if (memf == NULL) {
    356  1.49       dsl 		kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
    357  1.38    simonb 		donlist_sysctl();
    358  1.38    simonb 	} else
    359  1.38    simonb 		kd = kvm_openfiles(nlistf, memf, swapf, O_RDONLY, errbuf);
    360  1.38    simonb 
    361  1.47  jdolecek 	if (kd == 0)
    362  1.47  jdolecek 		errx(1, "%s", errbuf);
    363  1.21       mrg 
    364  1.15   mycroft 	if (!fmt)
    365   1.1       cgd 		parsefmt(dfmt);
    366   1.1       cgd 
    367  1.49       dsl 	/* Add default sort criteria */
    368  1.49       dsl 	parsesort("tdev,pid");
    369  1.49       dsl 	for (vent = sorthead; vent; vent = vent->next) {
    370  1.49       dsl 		if (vent->var->flag & LWP || vent->var->type == UNSPECIFIED)
    371  1.49       dsl 			warnx("Cannot sort on %s, sort key ignored\n",
    372  1.49       dsl 				vent->var->name);
    373  1.49       dsl 	}
    374  1.49       dsl 
    375   1.1       cgd 	/*
    376  1.39    simonb 	 * scan requested variables, noting what structures are needed.
    377   1.1       cgd 	 */
    378   1.1       cgd 	scanvars();
    379  1.29  jdolecek 
    380   1.1       cgd 	/*
    381   1.1       cgd 	 * select procs
    382   1.1       cgd 	 */
    383  1.48   thorpej 	if (!(kinfo = getkinfo_kvm(kd, what, flag, &nentries)))
    384  1.48   thorpej 		err(1, "%s", kvm_geterr(kd));
    385  1.39    simonb 	if (nentries == 0) {
    386  1.39    simonb 		printheader();
    387  1.42     cyber 		exit(1);
    388  1.39    simonb 	}
    389   1.1       cgd 	/*
    390   1.1       cgd 	 * sort proc list
    391   1.1       cgd 	 */
    392  1.38    simonb 	qsort(kinfo, nentries, sizeof(struct kinfo_proc2), pscomp);
    393   1.1       cgd 	/*
    394  1.39    simonb 	 * For each proc, call each variable output function in
    395  1.39    simonb 	 * "setwidth" mode to determine the widest element of
    396  1.39    simonb 	 * the column.
    397  1.39    simonb 	 */
    398  1.39    simonb 	if (mode == PRINTMODE)
    399  1.39    simonb 		for (i = 0; i < nentries; i++) {
    400  1.39    simonb 			struct kinfo_proc2 *ki = &kinfo[i];
    401  1.39    simonb 
    402  1.39    simonb 			if (xflg == 0 && (ki->p_tdev == NODEV ||
    403  1.39    simonb 			    (ki->p_flag & P_CONTROLT) == 0))
    404  1.39    simonb 				continue;
    405  1.48   thorpej 
    406  1.48   thorpej 			kl = kvm_getlwps(kd, ki->p_pid, ki->p_paddr,
    407  1.48   thorpej 			    sizeof(struct kinfo_lwp), &nlwps);
    408  1.48   thorpej 			if (kl == 0)
    409  1.48   thorpej 				nlwps = 0;
    410  1.48   thorpej 			if (showlwps == 0) {
    411  1.48   thorpej 				l = pick_representative_lwp(ki, kl, nlwps);
    412  1.48   thorpej 				for (vent = vhead; vent; vent = vent->next)
    413  1.48   thorpej 					OUTPUT(vent, ki, l, WIDTHMODE);
    414  1.48   thorpej 			} else {
    415  1.48   thorpej 				/* The printing is done with the loops
    416  1.48   thorpej 				 * reversed, but here we don't need that,
    417  1.48   thorpej 				 * and this improves the code locality a bit.
    418  1.48   thorpej 				 */
    419  1.48   thorpej 				for (vent = vhead; vent; vent = vent->next)
    420  1.48   thorpej 					for (j = 0; j < nlwps; j++)
    421  1.48   thorpej 						OUTPUT(vent, ki, &kl[j],
    422  1.48   thorpej 						    WIDTHMODE);
    423  1.48   thorpej 			}
    424  1.39    simonb 		}
    425  1.39    simonb 	/*
    426  1.39    simonb 	 * Print header - AFTER determining process field widths.
    427  1.39    simonb 	 * printheader() also adds up the total width of all
    428  1.39    simonb 	 * fields the first time it's called.
    429  1.39    simonb 	 */
    430  1.39    simonb 	printheader();
    431  1.39    simonb 	/*
    432  1.39    simonb 	 * For each proc, call each variable output function in
    433  1.39    simonb 	 * print mode.
    434   1.1       cgd 	 */
    435   1.1       cgd 	for (i = lineno = 0; i < nentries; i++) {
    436  1.38    simonb 		struct kinfo_proc2 *ki = &kinfo[i];
    437  1.14   mycroft 
    438  1.38    simonb 		if (xflg == 0 && (ki->p_tdev == NODEV ||
    439  1.38    simonb 		    (ki->p_flag & P_CONTROLT ) == 0))
    440   1.1       cgd 			continue;
    441  1.48   thorpej 		kl = kvm_getlwps(kd, ki->p_pid, (u_long)ki->p_paddr,
    442  1.48   thorpej 		    sizeof(struct kinfo_lwp), &nlwps);
    443  1.48   thorpej 		if (kl == 0)
    444  1.48   thorpej 			nlwps = 0;
    445  1.48   thorpej 		if (showlwps == 0) {
    446  1.48   thorpej 			l = pick_representative_lwp(ki, kl, nlwps);
    447  1.48   thorpej 			for (vent = vhead; vent; vent = vent->next) {
    448  1.48   thorpej 				OUTPUT(vent, ki, l, mode);
    449  1.48   thorpej 				if (vent->next != NULL)
    450  1.48   thorpej 					(void)putchar(' ');
    451  1.48   thorpej 			}
    452  1.11       cgd 			(void)putchar('\n');
    453  1.48   thorpej 			if (prtheader && lineno++ == prtheader - 4) {
    454  1.48   thorpej 				(void)putchar('\n');
    455  1.48   thorpej 				printheader();
    456  1.48   thorpej 				lineno = 0;
    457  1.48   thorpej 			}
    458  1.48   thorpej 		} else {
    459  1.48   thorpej 			for (j = 0; j < nlwps; j++) {
    460  1.48   thorpej 				for (vent = vhead; vent; vent = vent->next) {
    461  1.48   thorpej 					OUTPUT(vent, ki, &kl[j], mode);
    462  1.48   thorpej 					if (vent->next != NULL)
    463  1.48   thorpej 						(void)putchar(' ');
    464  1.48   thorpej 				}
    465  1.48   thorpej 				(void)putchar('\n');
    466  1.48   thorpej 				if (prtheader && lineno++ == prtheader - 4) {
    467  1.48   thorpej 					(void)putchar('\n');
    468  1.48   thorpej 					printheader();
    469  1.48   thorpej 					lineno = 0;
    470  1.48   thorpej 				}
    471  1.48   thorpej 			}
    472   1.1       cgd 		}
    473   1.1       cgd 	}
    474   1.1       cgd 	exit(eval);
    475  1.23   mycroft 	/* NOTREACHED */
    476   1.1       cgd }
    477   1.1       cgd 
    478  1.48   thorpej static struct kinfo_lwp *
    479  1.48   thorpej pick_representative_lwp(ki, kl, nlwps)
    480  1.48   thorpej 	struct kinfo_proc2 *ki;
    481  1.48   thorpej 	struct kinfo_lwp *kl;
    482  1.48   thorpej 	int nlwps;
    483  1.48   thorpej {
    484  1.48   thorpej 	int i, onproc, running, sleeping, stopped, suspended;
    485  1.48   thorpej 	static struct kinfo_lwp zero_lwp;
    486  1.48   thorpej 
    487  1.48   thorpej 	if (kl == 0)
    488  1.48   thorpej 		return &zero_lwp;
    489  1.48   thorpej 
    490  1.48   thorpej 	/* Trivial case: only one LWP */
    491  1.48   thorpej 	if (nlwps == 1)
    492  1.48   thorpej 		return kl;
    493  1.48   thorpej 
    494  1.48   thorpej 	switch (ki->p_realstat) {
    495  1.48   thorpej 	case SSTOP:
    496  1.48   thorpej 	case SACTIVE:
    497  1.48   thorpej 		/* Pick the most live LWP */
    498  1.48   thorpej 		onproc = running = sleeping = stopped = suspended = -1;
    499  1.48   thorpej 		for (i = 0; i < nlwps; i++) {
    500  1.48   thorpej 			switch (kl[i].l_stat) {
    501  1.48   thorpej 			case LSONPROC:
    502  1.48   thorpej 				onproc = i;
    503  1.48   thorpej 				break;
    504  1.48   thorpej 			case LSRUN:
    505  1.48   thorpej 				running = i;
    506  1.48   thorpej 				break;
    507  1.48   thorpej 			case LSSLEEP:
    508  1.48   thorpej 				sleeping = i;
    509  1.48   thorpej 				break;
    510  1.48   thorpej 			case LSSTOP:
    511  1.48   thorpej 				stopped = i;
    512  1.48   thorpej 				break;
    513  1.48   thorpej 			case LSSUSPENDED:
    514  1.48   thorpej 				suspended = i;
    515  1.48   thorpej 				break;
    516  1.48   thorpej 			}
    517  1.48   thorpej 		}
    518  1.48   thorpej 		if (onproc != -1)
    519  1.48   thorpej 			return &kl[onproc];
    520  1.48   thorpej 		if (running != -1)
    521  1.48   thorpej 			return &kl[running];
    522  1.48   thorpej 		if (sleeping != -1)
    523  1.48   thorpej 			return &kl[sleeping];
    524  1.48   thorpej 		if (stopped != -1)
    525  1.48   thorpej 			return &kl[stopped];
    526  1.48   thorpej 		if (suspended != -1)
    527  1.48   thorpej 			return &kl[suspended];
    528  1.48   thorpej 		break;
    529  1.48   thorpej 	case SDEAD:
    530  1.48   thorpej 	case SZOMB:
    531  1.48   thorpej 		/* First will do */
    532  1.48   thorpej 		return kl;
    533  1.48   thorpej 		break;
    534  1.48   thorpej 	}
    535  1.48   thorpej 	/* Error condition! */
    536  1.48   thorpej 	warnx("Inconsistent LWP state for process %d\n", ki->p_pid);
    537  1.48   thorpej 	return kl;
    538  1.48   thorpej }
    539  1.48   thorpej 
    540  1.48   thorpej 
    541  1.38    simonb static struct kinfo_proc2 *
    542  1.45     lukem getkinfo_kvm(kdp, what, flag, nentriesp)
    543  1.45     lukem 	kvm_t *kdp;
    544  1.38    simonb 	int what, flag, *nentriesp;
    545  1.29  jdolecek {
    546  1.45     lukem 	return (kvm_getproc2(kdp, what, flag, sizeof(struct kinfo_proc2),
    547  1.38    simonb 	    nentriesp));
    548  1.29  jdolecek }
    549  1.29  jdolecek 
    550  1.11       cgd static void
    551   1.1       cgd scanvars()
    552   1.1       cgd {
    553  1.11       cgd 	struct varent *vent;
    554  1.11       cgd 	VAR *v;
    555   1.1       cgd 
    556   1.1       cgd 	for (vent = vhead; vent; vent = vent->next) {
    557   1.1       cgd 		v = vent->var;
    558  1.43      matt 		if (v->flag & COMM) {
    559   1.1       cgd 			needcomm = 1;
    560  1.43      matt 			break;
    561  1.43      matt 		}
    562   1.1       cgd 	}
    563  1.11       cgd }
    564   1.1       cgd 
    565  1.11       cgd static int
    566  1.49       dsl pscomp(const void *a, const void *b)
    567   1.1       cgd {
    568  1.38    simonb 	struct kinfo_proc2 *ka = (struct kinfo_proc2 *)a;
    569  1.38    simonb 	struct kinfo_proc2 *kb = (struct kinfo_proc2 *)b;
    570  1.38    simonb 
    571   1.1       cgd 	int i;
    572  1.49       dsl 	int64_t i64;
    573  1.49       dsl 	VAR *v;
    574  1.49       dsl 	struct varent *ve;
    575  1.49       dsl 	sigset_t *sa, *sb;
    576  1.49       dsl 
    577  1.49       dsl #define V_SIZE(k) (k->p_vm_dsize + k->p_vm_ssize + k->p_vm_tsize)
    578  1.49       dsl #define RDIFF_N(t, n) \
    579  1.49       dsl 	if (((t *)((char *)ka + v->off))[n] > ((t *)((char *)kb + v->off))[n]) \
    580  1.49       dsl 		return 1; \
    581  1.49       dsl 	if (((t *)((char *)ka + v->off))[n] < ((t *)((char *)kb + v->off))[n]) \
    582  1.49       dsl 		return -1;
    583  1.49       dsl 
    584  1.49       dsl #define RDIFF(type) RDIFF_N(type, 0); continue
    585  1.49       dsl 
    586  1.49       dsl 	for (ve = sorthead; ve != NULL; ve = ve->next) {
    587  1.49       dsl 		v = ve->var;
    588  1.49       dsl 		if (v->flag & LWP)
    589  1.49       dsl 			/* LWP structure not available (yet) */
    590  1.49       dsl 			continue;
    591  1.49       dsl 		/* Sort on pvar() fields, + a few others */
    592  1.49       dsl 		switch (v->type) {
    593  1.49       dsl 		case CHAR:
    594  1.49       dsl 			RDIFF(char);
    595  1.49       dsl 		case UCHAR:
    596  1.49       dsl 			RDIFF(u_char);
    597  1.49       dsl 		case SHORT:
    598  1.49       dsl 			RDIFF(short);
    599  1.49       dsl 		case USHORT:
    600  1.49       dsl 			RDIFF(ushort);
    601  1.49       dsl 		case INT:
    602  1.49       dsl 			RDIFF(int);
    603  1.49       dsl 		case UINT:
    604  1.49       dsl 			RDIFF(uint);
    605  1.49       dsl 		case LONG:
    606  1.49       dsl 			RDIFF(long);
    607  1.49       dsl 		case ULONG:
    608  1.49       dsl 			RDIFF(ulong);
    609  1.49       dsl 		case INT32:
    610  1.49       dsl 			RDIFF(int32_t);
    611  1.49       dsl 		case UINT32:
    612  1.49       dsl 			RDIFF(uint32_t);
    613  1.49       dsl 		case SIGLIST:
    614  1.49       dsl 			sa = (void *)((char *)a + v->off);
    615  1.49       dsl 			sb = (void *)((char *)b + v->off);
    616  1.49       dsl 			i = 0;
    617  1.49       dsl 			do {
    618  1.49       dsl 				if (sa->__bits[i] > sb->__bits[i])
    619  1.49       dsl 					return 1;
    620  1.49       dsl 				if (sa->__bits[i] < sb->__bits[i])
    621  1.49       dsl 					return -1;
    622  1.49       dsl 				i++;
    623  1.49       dsl 			} while (i < sizeof sa->__bits / sizeof sa->__bits[0]);
    624  1.49       dsl 			continue;
    625  1.49       dsl 		case INT64:
    626  1.49       dsl 			RDIFF(int64_t);
    627  1.49       dsl 		case KPTR:
    628  1.49       dsl 		case KPTR24:
    629  1.49       dsl 		case UINT64:
    630  1.49       dsl 			RDIFF(uint64_t);
    631  1.49       dsl 		case TIMEVAL:
    632  1.49       dsl 			/* compare xxx_sec then xxx_usec */
    633  1.49       dsl 			RDIFF_N(uint32_t, 0);
    634  1.49       dsl 			RDIFF_N(uint32_t, 1);
    635  1.49       dsl 			continue;
    636  1.49       dsl 		case CPUTIME:
    637  1.49       dsl 			i64 = ka->p_rtime_sec * 1000000 + ka->p_rtime_usec;
    638  1.49       dsl 			i64 -= kb->p_rtime_sec * 1000000 + kb->p_rtime_usec;
    639  1.49       dsl 			if (sumrusage) {
    640  1.49       dsl 				i64 += ka->p_uctime_sec * 1000000
    641  1.49       dsl 				    + ka->p_uctime_usec;
    642  1.49       dsl 				i64 -= kb->p_uctime_sec * 1000000
    643  1.49       dsl 				    + kb->p_uctime_usec;
    644  1.49       dsl 			}
    645  1.49       dsl 			if (i64 != 0)
    646  1.49       dsl 				return i64 > 0 ? 1 : -1;
    647  1.49       dsl 			continue;
    648  1.49       dsl 		case PCPU:
    649  1.49       dsl 			i = getpcpu(kb) - getpcpu(ka);
    650  1.49       dsl 			if (i != 0)
    651  1.49       dsl 				return i;
    652  1.49       dsl 			continue;
    653  1.49       dsl 		case VSIZE:
    654  1.49       dsl 			i = V_SIZE(kb) - V_SIZE(ka);
    655  1.49       dsl 			if (i != 0)
    656  1.49       dsl 				return i;
    657  1.49       dsl 			continue;
    658  1.49       dsl 
    659  1.49       dsl 		default:
    660  1.49       dsl 			/* Ignore everything else */
    661  1.49       dsl 			break;
    662  1.49       dsl 		}
    663  1.49       dsl 	}
    664  1.49       dsl 	return 0;
    665   1.1       cgd 
    666  1.49       dsl #undef VSIZE
    667   1.1       cgd }
    668   1.1       cgd 
    669   1.1       cgd /*
    670   1.1       cgd  * ICK (all for getopt), would rather hide the ugliness
    671   1.1       cgd  * here than taint the main code.
    672   1.1       cgd  *
    673   1.1       cgd  *  ps foo -> ps -foo
    674   1.1       cgd  *  ps 34 -> ps -p34
    675   1.1       cgd  *
    676  1.25   mycroft  * The old convention that 't' with no trailing tty arg means the user's
    677   1.1       cgd  * tty, is only supported if argv[1] doesn't begin with a '-'.  This same
    678   1.1       cgd  * feature is available with the option 'T', which takes no argument.
    679   1.1       cgd  */
    680  1.11       cgd static char *
    681   1.1       cgd kludge_oldps_options(s)
    682   1.1       cgd 	char *s;
    683   1.1       cgd {
    684   1.1       cgd 	size_t len;
    685   1.1       cgd 	char *newopts, *ns, *cp;
    686   1.1       cgd 
    687   1.1       cgd 	len = strlen(s);
    688  1.16   thorpej 	if ((newopts = ns = malloc(len + 3)) == NULL)
    689  1.32  drochner 		err(1, NULL);
    690   1.1       cgd 	/*
    691   1.1       cgd 	 * options begin with '-'
    692   1.1       cgd 	 */
    693   1.1       cgd 	if (*s != '-')
    694   1.1       cgd 		*ns++ = '-';	/* add option flag */
    695   1.1       cgd 	/*
    696   1.1       cgd 	 * gaze to end of argv[1]
    697   1.1       cgd 	 */
    698   1.1       cgd 	cp = s + len - 1;
    699   1.1       cgd 	/*
    700  1.40    simonb 	 * if the last letter is a 't' flag and there are no other option
    701  1.40    simonb 	 * characters that take arguments (eg U, p, o) in the option
    702  1.40    simonb 	 * string and the option string doesn't start with a '-' then
    703  1.40    simonb 	 * convert to 'T' (meaning *this* terminal, i.e. ttyname(0)).
    704   1.1       cgd 	 */
    705  1.40    simonb 	if (*cp == 't' && *s != '-' && strpbrk(s, ARGOPTS) == NULL)
    706   1.1       cgd 		*cp = 'T';
    707   1.1       cgd 	else {
    708   1.1       cgd 		/*
    709   1.1       cgd 		 * otherwise check for trailing number, which *may* be a
    710   1.1       cgd 		 * pid.
    711   1.1       cgd 		 */
    712   1.1       cgd 		while (cp >= s && isdigit(*cp))
    713   1.1       cgd 			--cp;
    714   1.1       cgd 	}
    715   1.1       cgd 	cp++;
    716  1.11       cgd 	memmove(ns, s, (size_t)(cp - s));	/* copy up to trailing number */
    717   1.1       cgd 	ns += cp - s;
    718   1.1       cgd 	/*
    719   1.1       cgd 	 * if there's a trailing number, and not a preceding 'p' (pid) or
    720   1.1       cgd 	 * 't' (tty) flag, then assume it's a pid and insert a 'p' flag.
    721   1.1       cgd 	 */
    722  1.25   mycroft 	if (isdigit(*cp) &&
    723  1.25   mycroft 	    (cp == s || (cp[-1] != 'U' && cp[-1] != 't' && cp[-1] != 'p' &&
    724  1.25   mycroft 	     (cp - 1 == s || cp[-2] != 't'))))
    725   1.1       cgd 		*ns++ = 'p';
    726  1.18       mrg 	/* and append the number */
    727  1.40    simonb 	(void)strcpy(ns, cp);		/* XXX strcpy is safe here */
    728   1.1       cgd 
    729   1.1       cgd 	return (newopts);
    730   1.1       cgd }
    731   1.1       cgd 
    732  1.11       cgd static void
    733  1.11       cgd usage()
    734   1.1       cgd {
    735   1.1       cgd 
    736  1.11       cgd 	(void)fprintf(stderr,
    737  1.11       cgd 	    "usage:\t%s\n\t   %s\n\t%s\n",
    738  1.49       dsl 	    "ps [-acCehjlmrsSTuvwx] [-k key] [-O|o fmt] [-p pid] [-t tty]",
    739  1.34   hubertf 	    "[-M core] [-N system] [-W swap] [-U username]",
    740  1.11       cgd 	    "ps [-L]");
    741   1.1       cgd 	exit(1);
    742  1.23   mycroft 	/* NOTREACHED */
    743   1.1       cgd }
    744