]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/pstat/pstat.c
Use struct xfile, not struct file.
[FreeBSD/FreeBSD.git] / usr.sbin / pstat / pstat.c
1 /*-
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.
5  * All rights reserved.
6  *
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.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
27  *
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
38  * SUCH DAMAGE.
39  */
40
41 #ifndef lint
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";
45 #endif /* not lint */
46
47 #ifndef lint
48 #if 0
49 static char sccsid[] = "@(#)pstat.c     8.16 (Berkeley) 5/9/95";
50 #endif
51 static const char rcsid[] =
52   "$FreeBSD$";
53 #endif /* not lint */
54
55 #include <sys/param.h>
56 #include <sys/time.h>
57 #include <sys/file.h>
58 #include <sys/stat.h>
59 #include <sys/stdint.h>
60 #include <sys/ioctl.h>
61 #include <sys/ioctl_compat.h>   /* XXX NTTYDISC is too well hidden */
62 #include <sys/tty.h>
63 #include <sys/conf.h>
64 #include <sys/blist.h>
65
66 #include <sys/user.h>
67 #include <sys/sysctl.h>
68
69 #include <err.h>
70 #include <fcntl.h>
71 #include <kvm.h>
72 #include <limits.h>
73 #include <nlist.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <string.h>
77 #include <unistd.h>
78
79 enum {
80         NL_CONSTTY,
81         NL_MAXFILES,
82         NL_NFILES,
83         NL_TTY_LIST
84 };
85
86 static struct nlist nl[] = {
87         { "_constty", 0 },
88         { "_maxfiles", 0 },
89         { "_nfiles", 0 },
90         { "_tty_list", 0 },
91         { "" }
92 };
93
94 static int      usenumflag;
95 static int      totalflag;
96 static int      swapflag;
97 static char     *nlistf;
98 static char     *memf;
99 static kvm_t    *kd;
100
101 static char     *usagestr;
102
103 static void     filemode(void);
104 static int      getfiles(char **, size_t *);
105 static void     swapmode(void);
106 static void     ttymode(void);
107 static void     ttyprt(struct xtty *);
108 static void     usage(void);
109
110 int
111 main(int argc, char *argv[])
112 {
113         int ch, i, quit, ret;
114         int fileflag, ttyflag;
115         char buf[_POSIX2_LINE_MAX],*opts;
116
117         fileflag = swapflag = ttyflag = 0;
118
119         /* We will behave like good old swapinfo if thus invoked */
120         opts = strrchr(argv[0], '/');
121         if (opts)
122                 opts++;
123         else
124                 opts = argv[0];
125         if (!strcmp(opts, "swapinfo")) {
126                 swapflag = 1;
127                 opts = "kM:N:";
128                 usagestr = "swapinfo [-k] [-M core] [-N system]";
129         } else {
130                 opts = "TM:N:fknst";
131                 usagestr = "pstat [-Tfknst] [-M core] [-N system]";
132         }
133
134         while ((ch = getopt(argc, argv, opts)) != -1)
135                 switch (ch) {
136                 case 'f':
137                         fileflag = 1;
138                         break;
139                 case 'k':
140                         putenv("BLOCKSIZE=1K");
141                         break;
142                 case 'M':
143                         memf = optarg;
144                         break;
145                 case 'N':
146                         nlistf = optarg;
147                         break;
148                 case 'n':
149                         usenumflag = 1;
150                         break;
151                 case 's':
152                         ++swapflag;
153                         break;
154                 case 'T':
155                         totalflag = 1;
156                         break;
157                 case 't':
158                         ttyflag = 1;
159                         break;
160                 default:
161                         usage();
162                 }
163         argc -= optind;
164         argv += optind;
165
166         if (memf != NULL) {
167                 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf);
168                 if (kd == NULL)
169                         errx(1, "kvm_openfiles: %s", buf);
170                 if ((ret = kvm_nlist(kd, nl)) != 0) {
171                         if (ret == -1)
172                                 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
173                         quit = 0;
174                         for (i = 0; nl[i].n_name[0] != '\0'; ++i)
175                                 if (nl[i].n_value == 0) {
176                                         quit = 1;
177                                         warnx("undefined symbol: %s",
178                                             nl[i].n_name);
179                                 }
180                         if (quit)
181                                 exit(1);
182                 }
183         }
184         if (!(fileflag | ttyflag | swapflag | totalflag))
185                 usage();
186         if (fileflag || totalflag)
187                 filemode();
188         if (ttyflag)
189                 ttymode();
190         if (swapflag || totalflag)
191                 swapmode();
192         exit (0);
193 }
194
195 static void
196 usage(void)
197 {
198         fprintf(stderr, "usage: %s\n", usagestr);
199         exit (1);
200 }
201
202 static const char hdr[] =
203 "  LINE RAW CAN OUT IHIWT ILOWT OHWT LWT     COL STATE  SESS      PGID DISC\n";
204
205 static void
206 ttymode_kvm(void)
207 {
208         SLIST_HEAD(, tty) tl;
209         struct tty *tp, tty;
210         struct xtty xt;
211
212         (void)printf("%s", hdr);
213         bzero(&xt, sizeof xt);
214         xt.xt_size = sizeof xt;
215         if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl)
216                 errx(1, "kvm_read(): %s", kvm_geterr(kd));
217         tp = SLIST_FIRST(&tl);
218         while (tp != NULL) {
219                 if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty)
220                         errx(1, "kvm_read(): %s", kvm_geterr(kd));
221                 xt.xt_rawcc = tty.t_rawq.c_cc;
222                 xt.xt_cancc = tty.t_canq.c_cc;
223                 xt.xt_outcc = tty.t_outq.c_cc;
224 #define XT_COPY(field) xt.xt_##field = tty.t_##field
225                 XT_COPY(line);
226                 XT_COPY(state);
227                 XT_COPY(column);
228                 XT_COPY(ihiwat);
229                 XT_COPY(ilowat);
230                 XT_COPY(ohiwat);
231                 XT_COPY(olowat);
232 #undef XT_COPY
233                 ttyprt(&xt);
234                 tp = tty.t_list.sle_next;
235         }
236 }
237
238 static void
239 ttymode_sysctl(void)
240 {
241         struct xtty *xt, *end;
242         void *xttys;
243         size_t len;
244
245         (void)printf("%s", hdr);
246         if ((xttys = malloc(len = sizeof *xt)) == NULL)
247                 err(1, "malloc()");
248         while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) {
249                 if (errno != ENOMEM)
250                         err(1, "sysctlbyname()");
251                 len *= 2;
252                 if ((xttys = realloc(xttys, len)) == NULL)
253                         err(1, "realloc()");
254         }
255         if (len > 0) {
256                 end = (struct xtty *)((char *)xttys + len);
257                 for (xt = xttys; xt < end; xt++)
258                         ttyprt(xt);
259         }
260 }
261
262 static void
263 ttymode(void)
264 {
265
266         if (kd != NULL)
267                 ttymode_kvm();
268         else
269                 ttymode_sysctl();
270 }
271
272 static struct {
273         int flag;
274         char val;
275 } ttystates[] = {
276 #ifdef TS_WOPEN
277         { TS_WOPEN,     'W'},
278 #endif
279         { TS_ISOPEN,    'O'},
280         { TS_CARR_ON,   'C'},
281 #ifdef TS_CONNECTED
282         { TS_CONNECTED, 'c'},
283 #endif
284         { TS_TIMEOUT,   'T'},
285         { TS_FLUSH,     'F'},
286         { TS_BUSY,      'B'},
287 #ifdef TS_ASLEEP
288         { TS_ASLEEP,    'A'},
289 #endif
290 #ifdef TS_SO_OLOWAT
291         { TS_SO_OLOWAT, 'A'},
292 #endif
293 #ifdef TS_SO_OCOMPLETE
294         { TS_SO_OCOMPLETE, 'a'},
295 #endif
296         { TS_XCLUDE,    'X'},
297         { TS_TTSTOP,    'S'},
298 #ifdef TS_CAR_OFLOW
299         { TS_CAR_OFLOW, 'm'},
300 #endif
301 #ifdef TS_CTS_OFLOW
302         { TS_CTS_OFLOW, 'o'},
303 #endif
304 #ifdef TS_DSR_OFLOW
305         { TS_DSR_OFLOW, 'd'},
306 #endif
307         { TS_TBLOCK,    'K'},
308         { TS_ASYNC,     'Y'},
309         { TS_BKSL,      'D'},
310         { TS_ERASE,     'E'},
311         { TS_LNCH,      'L'},
312         { TS_TYPEN,     'P'},
313         { TS_CNTTB,     'N'},
314 #ifdef TS_CAN_BYPASS_L_RINT
315         { TS_CAN_BYPASS_L_RINT, 'l'},
316 #endif
317 #ifdef TS_SNOOP
318         { TS_SNOOP,     's'},
319 #endif
320 #ifdef TS_ZOMBIE
321         { TS_ZOMBIE,    'Z'},
322 #endif
323         { 0,           '\0'},
324 };
325
326 static void
327 ttyprt(struct xtty *xt)
328 {
329         int i, j;
330         pid_t pgid;
331         char *name, state[20];
332
333         if (xt->xt_size != sizeof *xt)
334                 errx(1, "struct xtty size mismatch");
335         if (usenumflag || xt->xt_dev == 0 ||
336            (name = devname(xt->xt_dev, S_IFCHR)) == NULL)
337                 (void)printf("   %2d,%-2d", major(xt->xt_dev), minor(xt->xt_dev));
338         else
339                 (void)printf("%7s ", name);
340         (void)printf("%2ld %3ld ", xt->xt_rawcc, xt->xt_cancc);
341         (void)printf("%3ld %5d %5d %4d %3d %7d ", xt->xt_outcc,
342                 xt->xt_ihiwat, xt->xt_ilowat, xt->xt_ohiwat, xt->xt_olowat,
343                 xt->xt_column);
344         for (i = j = 0; ttystates[i].flag; i++)
345                 if (xt->xt_state & ttystates[i].flag)
346                         state[j++] = ttystates[i].val;
347         if (j == 0)
348                 state[j++] = '-';
349         state[j] = '\0';
350         (void)printf("%-6s %8d", state, xt->xt_sid);
351         pgid = 0;
352         (void)printf("%6d ", xt->xt_pgid);
353         switch (xt->xt_line) {
354         case TTYDISC:
355                 (void)printf("term\n");
356                 break;
357         case NTTYDISC:
358                 (void)printf("ntty\n");
359                 break;
360         case SLIPDISC:
361                 (void)printf("slip\n");
362                 break;
363         case PPPDISC:
364                 (void)printf("ppp\n");
365                 break;
366         default:
367                 (void)printf("%d\n", xt->xt_line);
368                 break;
369         }
370 }
371
372 static void
373 filemode(void)
374 {
375         struct xfile *fp;
376         char *buf, flagbuf[16], *fbp;
377         int maxf, openf;
378         size_t len;
379         static char *dtypes[] = { "???", "inode", "socket" };
380         int i;
381
382         if (kd != NULL) {
383                 if (kvm_read(kd, nl[NL_MAXFILES].n_value,
384                         &maxf, sizeof maxf) != sizeof maxf ||
385                     kvm_read(kd, nl[NL_NFILES].n_value,
386                         &openf, sizeof openf) != sizeof openf)
387                         errx(1, "kvm_read(): %s", kvm_geterr(kd));
388         } else {
389                 len = sizeof(int);
390                 if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 ||
391                     sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1)
392                         err(1, "sysctlbyname()");
393         }
394
395         if (totalflag) {
396                 (void)printf("%3d/%3d files\n", openf, maxf);
397                 return;
398         }
399         if (getfiles(&buf, &len) == -1)
400                 return;
401         openf = len / sizeof *fp;
402         (void)printf("%d/%d open files\n", openf, maxf);
403         (void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
404         for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) {
405                 if ((unsigned)fp->xf_type > DTYPE_SOCKET)
406                         continue;
407                 (void)printf("%8jx ", (uintmax_t)(uintptr_t)fp->xf_file);
408                 (void)printf("%-8.8s", dtypes[fp->xf_type]);
409                 fbp = flagbuf;
410                 if (fp->xf_flag & FREAD)
411                         *fbp++ = 'R';
412                 if (fp->xf_flag & FWRITE)
413                         *fbp++ = 'W';
414                 if (fp->xf_flag & FAPPEND)
415                         *fbp++ = 'A';
416                 if (fp->xf_flag & FASYNC)
417                         *fbp++ = 'I';
418                 *fbp = '\0';
419                 (void)printf("%6s  %3d", flagbuf, fp->xf_count);
420                 (void)printf("  %3d", fp->xf_msgcount);
421                 (void)printf("  %8lx", (u_long)(void *)fp->xf_data);
422                 (void)printf("  %jx\n", (uintmax_t)fp->xf_offset);
423         }
424         free(buf);
425 }
426
427 static int
428 getfiles(char **abuf, size_t *alen)
429 {
430         size_t len;
431         int mib[2];
432         char *buf;
433
434         /*
435          * XXX
436          * Add emulation of KINFO_FILE here.
437          */
438         if (kd != NULL)
439                 errx(1, "files on dead kernel, not implemented");
440
441         mib[0] = CTL_KERN;
442         mib[1] = KERN_FILE;
443         if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
444                 warn("sysctl: KERN_FILE");
445                 return (-1);
446         }
447         if ((buf = malloc(len)) == NULL)
448                 errx(1, "malloc");
449         if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
450                 warn("sysctl: KERN_FILE");
451                 return (-1);
452         }
453         *abuf = buf;
454         *alen = len;
455         return (0);
456 }
457
458 /*
459  * swapmode is based on a program called swapinfo written
460  * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
461  */
462
463 #define CONVERT(v)      ((int)((intmax_t)(v) * pagesize / blocksize))
464 static struct kvm_swap swtot;
465 static int nswdev;
466
467 static void
468 print_swap_header(void)
469 {
470         int hlen;
471         long blocksize;
472         const char *header;
473
474         header = getbsize(&hlen, &blocksize);
475         if (totalflag == 0)
476                 (void)printf("%-15s %*s %8s %8s %8s  %s\n",
477                     "Device", hlen, header,
478                     "Used", "Avail", "Capacity", "Type");
479 }
480
481 static void
482 print_swap(struct kvm_swap *ksw)
483 {
484         int hlen, pagesize;
485         long blocksize;
486
487         pagesize = getpagesize();
488         getbsize(&hlen, &blocksize);
489         swtot.ksw_total += ksw->ksw_total;
490         swtot.ksw_used += ksw->ksw_used;
491         ++nswdev;
492         if (totalflag == 0) {
493                 (void)printf("%-15s %*d ",
494                     ksw->ksw_devname, hlen,
495                     CONVERT(ksw->ksw_total));
496                 (void)printf("%8d %8d %5.0f%%    %s\n",
497                     CONVERT(ksw->ksw_used),
498                     CONVERT(ksw->ksw_total - ksw->ksw_used),
499                     (ksw->ksw_used * 100.0) / ksw->ksw_total,
500                     (ksw->ksw_flags & SW_SEQUENTIAL) ?
501                     "Sequential" : "Interleaved");
502         }
503 }
504
505 static void
506 print_swap_total(void)
507 {
508         int hlen, pagesize;
509         long blocksize;
510
511         pagesize = getpagesize();
512         getbsize(&hlen, &blocksize);
513         if (totalflag) {
514                 blocksize = 1024 * 1024;
515                 (void)printf("%dM/%dM swap space\n",
516                     CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total));
517         } else if (nswdev > 1) {
518                 (void)printf("%-15s %*d %8d %8d %5.0f%%\n",
519                     "Total", hlen, CONVERT(swtot.ksw_total),
520                     CONVERT(swtot.ksw_used),
521                     CONVERT(swtot.ksw_total - swtot.ksw_used),
522                     (swtot.ksw_used * 100.0) / swtot.ksw_total);
523         }
524 }
525
526 static void
527 swapmode_kvm(void)
528 {
529         struct kvm_swap kswap[16];
530         int i, n;
531
532         n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0],
533             ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX);
534
535         print_swap_header();
536         for (i = 0; i < n; ++i)
537                 print_swap(&kswap[i]);
538         print_swap_total();
539 }
540
541 static void
542 swapmode_sysctl(void)
543 {
544         struct kvm_swap ksw;
545         struct xswdev xsw;
546         size_t mibsize, size;
547         int mib[16], n;
548
549         print_swap_header();
550         mibsize = sizeof mib / sizeof mib[0];
551         if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1)
552                 err(1, "sysctlnametomib()");
553         for (n = 0; ; ++n) {
554                 mib[mibsize] = n;
555                 size = sizeof xsw;
556                 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, NULL) == -1)
557                         break;
558                 if (xsw.xsw_version != XSWDEV_VERSION)
559                         errx(1, "xswdev version mismatch");
560                 snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname,
561                     "/dev/%s", devname(xsw.xsw_dev, S_IFCHR));
562                 ksw.ksw_used = xsw.xsw_used;
563                 ksw.ksw_total = xsw.xsw_nblks;
564                 ksw.ksw_flags = xsw.xsw_flags;
565                 print_swap(&ksw);
566         }
567         if (errno != ENOENT)
568                 err(1, "sysctl()");
569         print_swap_total();
570 }
571
572 static void
573 swapmode(void)
574 {
575         if (kd != NULL)
576                 swapmode_kvm();
577         else
578                 swapmode_sysctl();
579 }