2 * Copyright (c) 1980, 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technologies, Inc.
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 static const char copyright[] =
43 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
44 The Regents of the University of California. All rights reserved.\n";
49 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95";
51 static const char rcsid[] =
55 #include <sys/param.h>
62 #include <sys/stdint.h>
63 #include <sys/ioctl.h>
64 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */
67 #include <sys/blist.h>
70 #include <sys/sysctl.h>
89 static struct nlist nl[] = {
97 static int usenumflag;
104 static char *usagestr;
106 static void filemode(void);
107 static int getfiles(char **, size_t *);
108 static void swapmode(void);
109 static void ttymode(void);
110 static void ttyprt(struct xtty *);
111 static void usage(void);
114 main(int argc, char *argv[])
116 int ch, i, quit, ret;
117 int fileflag, ttyflag;
118 char buf[_POSIX2_LINE_MAX],*opts;
120 fileflag = swapflag = ttyflag = 0;
122 /* We will behave like good old swapinfo if thus invoked */
123 opts = strrchr(argv[0], '/');
128 if (!strcmp(opts, "swapinfo")) {
131 usagestr = "swapinfo [-k] [-M core] [-N system]";
134 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
137 while ((ch = getopt(argc, argv, opts)) != -1)
143 putenv("BLOCKSIZE=1K");
170 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
172 errx(1, "kvm_openfiles: %s", buf);
173 if ((ret = kvm_nlist(kd, nl)) != 0) {
175 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
177 for (i = 0; nl[i].n_name[0] != '\0'; ++i)
178 if (nl[i].n_value == 0) {
180 warnx("undefined symbol: %s",
187 if (!(fileflag | ttyflag | swapflag | totalflag))
189 if (fileflag || totalflag)
193 if (swapflag || totalflag)
201 fprintf(stderr, "usage: %s\n", usagestr);
205 static const char hdr[] =
206 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n";
211 SLIST_HEAD(, tty) tl;
215 (void)printf("%s", hdr);
216 bzero(&xt, sizeof xt);
217 xt.xt_size = sizeof xt;
218 if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl)
219 errx(1, "kvm_read(): %s", kvm_geterr(kd));
220 tp = SLIST_FIRST(&tl);
222 if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty)
223 errx(1, "kvm_read(): %s", kvm_geterr(kd));
224 xt.xt_rawcc = tty.t_rawq.c_cc;
225 xt.xt_cancc = tty.t_canq.c_cc;
226 xt.xt_outcc = tty.t_outq.c_cc;
227 #define XT_COPY(field) xt.xt_##field = tty.t_##field
237 tp = tty.t_list.sle_next;
244 struct xtty *xt, *end;
248 (void)printf("%s", hdr);
249 if ((xttys = malloc(len = sizeof *xt)) == NULL)
251 while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) {
253 err(1, "sysctlbyname()");
255 if ((xttys = realloc(xttys, len)) == NULL)
259 end = (struct xtty *)((char *)xttys + len);
260 for (xt = xttys; xt < end; xt++)
285 { TS_CONNECTED, 'c'},
294 { TS_SO_OLOWAT, 'A'},
296 #ifdef TS_SO_OCOMPLETE
297 { TS_SO_OCOMPLETE, 'a'},
302 { TS_CAR_OFLOW, 'm'},
305 { TS_CTS_OFLOW, 'o'},
308 { TS_DSR_OFLOW, 'd'},
317 #ifdef TS_CAN_BYPASS_L_RINT
318 { TS_CAN_BYPASS_L_RINT, 'l'},
330 ttyprt(struct xtty *xt)
334 char *name, state[20];
336 if (xt->xt_size != sizeof *xt)
337 errx(1, "struct xtty size mismatch");
338 if (usenumflag || xt->xt_dev == 0 ||
339 (name = devname(xt->xt_dev, S_IFCHR)) == NULL)
340 (void)printf(" %2d,%-2d", major(xt->xt_dev), minor(xt->xt_dev));
342 (void)printf("%7s ", name);
343 (void)printf("%2ld %3ld ", xt->xt_rawcc, xt->xt_cancc);
344 (void)printf("%3ld %5d %5d %4d %3d %7d ", xt->xt_outcc,
345 xt->xt_ihiwat, xt->xt_ilowat, xt->xt_ohiwat, xt->xt_olowat,
347 for (i = j = 0; ttystates[i].flag; i++)
348 if (xt->xt_state & ttystates[i].flag)
349 state[j++] = ttystates[i].val;
353 (void)printf("%-6s %8d", state, xt->xt_sid);
355 (void)printf("%6d ", xt->xt_pgid);
356 switch (xt->xt_line) {
358 (void)printf("term\n");
361 (void)printf("ntty\n");
364 (void)printf("slip\n");
367 (void)printf("ppp\n");
370 (void)printf("%d\n", xt->xt_line);
380 char *buf, flagbuf[16], *fbp;
383 static char *dtypes[] = { "???", "inode", "socket" };
386 if (kvm_read(kd, nl[NL_MAXFILES].n_value,
387 &maxf, sizeof maxf) != sizeof maxf ||
388 kvm_read(kd, nl[NL_NFILES].n_value,
389 &openf, sizeof openf) != sizeof openf)
390 errx(1, "kvm_read(): %s", kvm_geterr(kd));
393 if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 ||
394 sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1)
395 err(1, "sysctlbyname()");
399 (void)printf("%3d/%3d files\n", openf, maxf);
402 if (getfiles(&buf, &len) == -1)
405 * Getfiles returns in malloc'd memory a pointer to the first file
406 * structure, and then an array of file structs (whose addresses are
407 * derivable from the previous entry).
409 addr = LIST_FIRST((struct filelist *)buf);
410 fp = (struct file *)(buf + sizeof(struct filelist));
411 openf = (len - sizeof(struct filelist)) / sizeof(struct file);
413 (void)printf("%d/%d open files\n", openf, maxf);
414 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n");
415 for (; (char *)fp < buf + len; addr = LIST_NEXT(fp, f_list), fp++) {
416 if ((unsigned)fp->f_type > DTYPE_SOCKET)
418 (void)printf("%8lx ", (u_long)(void *)addr);
419 (void)printf("%-8.8s", dtypes[fp->f_type]);
421 if (fp->f_flag & FREAD)
423 if (fp->f_flag & FWRITE)
425 if (fp->f_flag & FAPPEND)
427 if (fp->f_flag & FASYNC)
430 (void)printf("%6s %3d", flagbuf, fp->f_count);
431 (void)printf(" %3d", fp->f_msgcount);
432 (void)printf(" %8lx", (u_long)(void *)fp->f_data);
433 (void)printf(" %jx\n", (uintmax_t)fp->f_offset);
439 getfiles(char **abuf, size_t *alen)
447 * Add emulation of KINFO_FILE here.
450 errx(1, "files on dead kernel, not implemented");
454 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
455 warn("sysctl: KERN_FILE");
458 if ((buf = malloc(len)) == NULL)
460 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
461 warn("sysctl: KERN_FILE");
470 * swapmode is based on a program called swapinfo written
471 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
474 #define CONVERT(v) ((int)((intmax_t)(v) * pagesize / blocksize))
475 static struct kvm_swap swtot;
479 print_swap_header(void)
485 header = getbsize(&hlen, &blocksize);
487 (void)printf("%-15s %*s %8s %8s %8s %s\n",
488 "Device", hlen, header,
489 "Used", "Avail", "Capacity", "Type");
493 print_swap(struct kvm_swap *ksw)
498 pagesize = getpagesize();
499 getbsize(&hlen, &blocksize);
500 swtot.ksw_total += ksw->ksw_total;
501 swtot.ksw_used += ksw->ksw_used;
503 if (totalflag == 0) {
504 (void)printf("%-15s %*d ",
505 ksw->ksw_devname, hlen,
506 CONVERT(ksw->ksw_total));
507 (void)printf("%8d %8d %5.0f%% %s\n",
508 CONVERT(ksw->ksw_used),
509 CONVERT(ksw->ksw_total - ksw->ksw_used),
510 (ksw->ksw_used * 100.0) / ksw->ksw_total,
511 (ksw->ksw_flags & SW_SEQUENTIAL) ?
512 "Sequential" : "Interleaved");
517 print_swap_total(void)
522 pagesize = getpagesize();
523 getbsize(&hlen, &blocksize);
525 blocksize = 1024 * 1024;
526 (void)printf("%dM/%dM swap space\n",
527 CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total));
528 } else if (nswdev > 1) {
529 (void)printf("%-15s %*d %8d %8d %5.0f%%\n",
530 "Total", hlen, CONVERT(swtot.ksw_total),
531 CONVERT(swtot.ksw_used),
532 CONVERT(swtot.ksw_total - swtot.ksw_used),
533 (swtot.ksw_used * 100.0) / swtot.ksw_total);
540 struct kvm_swap kswap[16];
543 n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0],
544 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX);
547 for (i = 0; i < n; ++i)
548 print_swap(&kswap[i]);
553 swapmode_sysctl(void)
557 size_t mibsize, size;
561 mibsize = sizeof mib / sizeof mib[0];
562 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
563 err(1, "sysctlnametomib()");
567 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, NULL) == -1)
569 if (xsw.xsw_version != XSWDEV_VERSION)
570 errx(1, "xswdev version mismatch");
571 snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname,
572 "/dev/%s", devname(xsw.xsw_dev, S_IFCHR));
573 ksw.ksw_used = xsw.xsw_used;
574 ksw.ksw_total = xsw.xsw_nblks;
575 ksw.ksw_flags = xsw.xsw_flags;