Bug Summary

File:src/usr.sbin/pstat/pstat.c
Warning:line 855, column 3
Potential leak of memory pointed to by 'bp'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.0 -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name pstat.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/usr.sbin/pstat/obj -resource-dir /usr/local/lib/clang/13.0.0 -internal-isystem /usr/local/lib/clang/13.0.0/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/usr.sbin/pstat/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/vmm/scan-build/2022-01-12-194120-40624-1 -x c /usr/src/usr.sbin/pstat/pstat.c
1/* $OpenBSD: pstat.c,v 1.127 2021/11/15 15:14:24 millert Exp $ */
2/* $NetBSD: pstat.c,v 1.27 1996/10/23 22:50:06 cgd Exp $ */
3
4/*-
5 * Copyright (c) 1980, 1991, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h> /* MAXCOMLEN DEV_BSIZE */
34#include <sys/types.h>
35#include <sys/proc.h>
36#include <sys/time.h>
37#include <sys/vnode.h>
38#include <sys/ucred.h>
39#include <sys/stat.h>
40#define _KERNEL
41#include <sys/file.h>
42#include <ufs/ufs/quota.h>
43#include <ufs/ufs/inode.h>
44#include <sys/mount.h>
45#undef _KERNEL
46#include <nfs/nfsproto.h>
47#include <nfs/rpcv2.h>
48#include <nfs/nfsnode.h>
49#include <sys/ioctl.h>
50#include <sys/tty.h>
51#include <sys/conf.h>
52#include <sys/device.h>
53#include <sys/swap.h>
54
55#include <sys/sysctl.h>
56
57#include <stdint.h>
58#include <endian.h>
59#include <err.h>
60#include <fcntl.h>
61#include <kvm.h>
62#include <limits.h>
63#include <nlist.h>
64#include <paths.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <unistd.h>
69
70struct nlist vnodenl[] = {
71#define FNL_NFILE0 0 /* sysctl */
72 {"_numfiles"},
73#define FNL_MAXFILE1 1 /* sysctl */
74 {"_maxfiles"},
75#define TTY_NTTY2 2 /* sysctl */
76 {"_tty_count"},
77#define V_NUMV3 3 /* sysctl */
78 { "_numvnodes" },
79#define TTY_TTYLIST4 4 /* sysctl */
80 {"_ttylist"},
81#define V_MOUNTLIST5 5 /* no sysctl */
82 { "_mountlist" },
83 { NULL((void *)0) }
84};
85
86struct itty *globalitp;
87struct kinfo_file *kf;
88struct nlist *globalnl;
89
90struct e_vnode {
91 struct vnode *vptr;
92 struct vnode vnode;
93};
94
95int kflag;
96int totalflag;
97int usenumflag;
98int hideroot;
99int maxfile;
100int need_nlist;
101int nfile;
102int ntty;
103int numvnodes;
104char *nlistf = NULL((void *)0);
105char *memf = NULL((void *)0);
106kvm_t *kd = NULL((void *)0);
107
108#define SVAR(var)"var" __STRING(var)"var" /* to force expansion */
109#define KGET(idx, var)if (kvm_read(kd, (u_long)(globalnl[idx].n_value), &var, sizeof
(var)) != sizeof(var)) warnx("cannot read %s: %s", "var", kvm_geterr
(kd))
\
110 KGET1(idx, &var, sizeof(var), SVAR(var))if (kvm_read(kd, (u_long)(globalnl[idx].n_value), &var, sizeof
(var)) != sizeof(var)) warnx("cannot read %s: %s", "var", kvm_geterr
(kd))
111#define KGET1(idx, p, s, msg)if (kvm_read(kd, (u_long)(globalnl[idx].n_value), p, s) != s)
warnx("cannot read %s: %s", msg, kvm_geterr(kd))
\
112 KGET2(globalnl[idx].n_value, p, s, msg)if (kvm_read(kd, (u_long)(globalnl[idx].n_value), p, s) != s)
warnx("cannot read %s: %s", msg, kvm_geterr(kd))
113#define KGET2(addr, p, s, msg)if (kvm_read(kd, (u_long)(addr), p, s) != s) warnx("cannot read %s: %s"
, msg, kvm_geterr(kd))
\
114 if (kvm_read(kd, (u_long)(addr), p, s) != s) \
115 warnx("cannot read %s: %s", msg, kvm_geterr(kd))
116#define KGETRET(addr, p, s, msg)if (kvm_read(kd, (u_long)(addr), p, s) != s) { warnx("cannot read %s: %s"
, msg, kvm_geterr(kd)); return (0); }
\
117 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \
118 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \
119 return (0); \
120 }
121
122void filemode(void);
123void filemodeprep(void);
124struct mount *
125 getmnt(struct mount *);
126struct e_vnode *
127 kinfo_vnodes(void);
128void mount_print(struct mount *);
129void nfs_header(void);
130int nfs_print(struct vnode *);
131void swapmode(void);
132void ttymode(void);
133void ttymodeprep(void);
134void ttyprt(struct itty *);
135void tty2itty(struct tty *tp, struct itty *itp);
136void ufs_header(void);
137int ufs_print(struct vnode *);
138void ext2fs_header(void);
139int ext2fs_print(struct vnode *);
140static void __dead__attribute__((__noreturn__)) usage(void);
141void vnode_header(void);
142void vnode_print(struct vnode *, struct vnode *);
143void vnodemode(void);
144void vnodemodeprep(void);
145
146
147int
148main(int argc, char *argv[])
149{
150 int fileflag = 0, swapflag = 0, ttyflag = 0, vnodeflag = 0, ch;
151 char buf[_POSIX2_LINE_MAX2048];
152 const char *dformat = NULL((void *)0);
153 int i;
154
155 hideroot = getuid();
156
157 while ((ch = getopt(argc, argv, "d:TM:N:fiknstv")) != -1)
1
Assuming the condition is true
2
Loop condition is true. Entering loop body
5
Assuming the condition is false
6
Loop condition is false. Execution continues on line 193
158 switch (ch) {
3
Control jumps to 'case 105:' at line 187
159 case 'd':
160 dformat = optarg;
161 break;
162 case 'f':
163 fileflag = 1;
164 break;
165 case 'M':
166 memf = optarg;
167 break;
168 case 'N':
169 nlistf = optarg;
170 break;
171 case 'n':
172 usenumflag = 1;
173 break;
174 case 's':
175 swapflag = 1;
176 break;
177 case 'T':
178 totalflag = 1;
179 break;
180 case 't':
181 ttyflag = 1;
182 break;
183 case 'k':
184 kflag = 1;
185 break;
186 case 'v':
187 case 'i': /* Backward compatibility. */
188 vnodeflag = 1;
189 break;
4
Execution continues on line 157
190 default:
191 usage();
192 }
193 argc -= optind;
194 argv += optind;
195
196 if (dformat
6.1
'dformat' is null
&& getuid())
197 errx(1, "Only root can use -d");
198
199 if ((dformat
6.2
'dformat' is equal to NULL
== NULL((void *)0) && argc > 0) || (dformat
7.1
'dformat' is null
&& argc == 0))
7
Assuming 'argc' is <= 0
200 usage();
201
202 need_nlist = dformat
7.2
'dformat' is null
|| vnodeflag;
203
204 if (nlistf
7.3
'nlistf' is equal to NULL
!= NULL((void *)0) || memf
7.4
'memf' is equal to NULL
!= NULL((void *)0)) {
8
Taking false branch
205 if (fileflag || totalflag)
206 need_nlist = 1;
207 }
208
209 if (vnodeflag
8.1
'vnodeflag' is 1
|| fileflag || dformat || need_nlist)
210 if ((kd = kvm_openfiles(nlistf, memf, NULL((void *)0),
10
Assuming the condition is false
11
Taking false branch
211 O_RDONLY0x0000 | (need_nlist
8.2
'need_nlist' is 1
? 0 : KVM_NO_FILES0x80000000), buf)) == 0
)
9
'?' condition is true
212 errx(1, "kvm_openfiles: %s", buf);
213
214 if (need_nlist
11.1
'need_nlist' is 1
)
12
Taking true branch
215 if (kvm_nlist(kd, vnodenl) == -1)
13
Assuming the condition is false
14
Taking false branch
216 errx(1, "kvm_nlist: %s", kvm_geterr(kd));
217
218 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag || dformat))
15
Taking false branch
219 usage();
220
221 if(!dformat
15.1
'dformat' is null
) {
16
Taking true branch
222 if (fileflag
16.1
'fileflag' is 0
|| totalflag)
17
Assuming 'totalflag' is 0
18
Taking false branch
223 filemodeprep();
224 if (vnodeflag
18.1
'vnodeflag' is 1
|| totalflag)
225 vnodemodeprep();
226 if (ttyflag
18.2
'ttyflag' is 0
)
19
Taking false branch
227 ttymodeprep();
228 }
229
230 if (unveil(_PATH_DEVDB"/var/run/dev.db", "r") == -1)
20
Assuming the condition is false
21
Taking false branch
231 err(1, "unveil %s", _PATH_DEVDB"/var/run/dev.db");
232 if (pledge("stdio rpath vminfo", NULL((void *)0)) == -1)
22
Assuming the condition is false
23
Taking false branch
233 err(1, "pledge");
234
235 if (dformat
23.1
'dformat' is null
) {
24
Taking false branch
236 struct nlist *nl;
237 int longformat = 0, stringformat = 0, error = 0, n;
238 uint32_t mask = ~0;
239 char format[10], buf[1024];
240
241 n = strlen(dformat);
242 if (n == 0)
243 errx(1, "illegal format");
244
245 /*
246 * Support p, c, s, and {l, ll, h, hh, j, t, z, }[diouxX]
247 */
248 if (strcmp(dformat, "p") == 0)
249 longformat = sizeof(long) == 8;
250 else if (strcmp(dformat, "c") == 0)
251 mask = 0xff;
252 else if (strcmp(dformat, "s") == 0)
253 stringformat = 1;
254 else if (strchr("diouxX", dformat[n - 1])) {
255 char *ptbl[]= {"l", "ll", "h", "hh", "j", "t", "z", ""};
256 int i;
257
258 char *mod;
259 for (i = 0; i < sizeof(ptbl)/sizeof(ptbl[0]); i++) {
260 mod = ptbl[i];
261 if (strlen(mod) == n - 1 &&
262 strncmp(mod, dformat, strlen(mod)) == 0)
263 break;
264 }
265 if (i == sizeof(ptbl)/sizeof(ptbl[0])
266 && dformat[1] != '\0')
267 errx(1, "illegal format");
268 if (strcmp(mod, "l") == 0)
269 longformat = sizeof(long) == sizeof(long long);
270 else if (strcmp(mod, "h") == 0)
271 mask = 0xffff;
272 else if (strcmp(mod, "hh") == 0)
273 mask = 0xff;
274 else if (strcmp(mod, "") != 0)
275 longformat = 1;
276
277 } else
278 errx(1, "illegal format");
279
280 if (*dformat == 's') {
281 stringformat = 1;
282 snprintf(format, sizeof(format), "%%.%zus",
283 sizeof buf);
284 } else
285 snprintf(format, sizeof(format), "%%%s", dformat);
286
287 nl = calloc(argc + 1, sizeof *nl);
288 if (!nl)
289 err(1, "calloc nl: ");
290 for (i = 0; i < argc; i++) {
291 if (asprintf(&nl[i].n_name, "_%s",
292 argv[i]) == -1)
293 warn("asprintf");
294 }
295 kvm_nlist(kd, nl);
296 globalnl = nl;
297 for (i = 0; i < argc; i++) {
298 uint64_t v;
299
300 printf("%s ", argv[i]);
301 if (!nl[i].n_value && argv[i][0] == '0') {
302 nl[i].n_value = strtoul(argv[i], NULL((void *)0), 16);
303 nl[i].n_type = N_DATA0x06;
304 }
305 if (!nl[i].n_value) {
306 printf("not found\n");
307 error++;
308 continue;
309 }
310
311 printf("at %p: ", (void *)nl[i].n_value);
312 if ((nl[i].n_type & N_TYPE0x1e) == N_DATA0x06 ||
313 (nl[i].n_type & N_TYPE0x1e) == N_COMM0x12) {
314 if (stringformat) {
315 KGET1(i, &buf, sizeof(buf), argv[i])if (kvm_read(kd, (u_long)(globalnl[i].n_value), &buf, sizeof
(buf)) != sizeof(buf)) warnx("cannot read %s: %s", argv[i], kvm_geterr
(kd))
;
316 buf[sizeof(buf) - 1] = '\0';
317 } else
318 KGET1(i, &v, sizeof(v), argv[i])if (kvm_read(kd, (u_long)(globalnl[i].n_value), &v, sizeof
(v)) != sizeof(v)) warnx("cannot read %s: %s", argv[i], kvm_geterr
(kd))
;
319 if (stringformat)
320 printf(format, &buf);
321 else if (longformat)
322 printf(format, v);
323 else {
324#if BYTE_ORDER1234 == BIG_ENDIAN4321
325 switch (mask) {
326 case 0xff:
327 v >>= 8;
328 /* FALLTHROUGH */
329 case 0xffff:
330 v >>= 16;
331 /* FALLTHROUGH */
332 case 0xffffffff:
333 v >>= 32;
334 break;
335 }
336#endif
337 printf(format, ((uint32_t)v) & mask);
338 }
339 }
340 printf("\n");
341 }
342 for (i = 0; i < argc; i++)
343 free(nl[i].n_name);
344 free(nl);
345 return error;
346 }
347
348 if (fileflag
24.1
'fileflag' is 0
|| totalflag
24.2
'totalflag' is 0
)
25
Taking false branch
349 filemode();
350 if (vnodeflag
25.1
'vnodeflag' is 1
|| totalflag)
351 vnodemode();
26
Calling 'vnodemode'
352 if (ttyflag)
353 ttymode();
354 if (swapflag || totalflag)
355 swapmode();
356 return 0;
357}
358
359void
360vnodemode(void)
361{
362 struct e_vnode *e_vnodebase, *endvnode, *evp;
363 struct vnode *vp;
364 struct mount *maddr, *mp = NULL((void *)0);
365
366 globalnl = vnodenl;
367
368 e_vnodebase = kinfo_vnodes();
27
Calling 'kinfo_vnodes'
369 if (totalflag) {
370 (void)printf("%7d vnodes\n", numvnodes);
371 return;
372 }
373 if (!e_vnodebase)
374 return;
375 endvnode = e_vnodebase + numvnodes;
376 (void)printf("%d active vnodes\n", numvnodes);
377
378 maddr = NULL((void *)0);
379 for (evp = e_vnodebase; evp < endvnode; evp++) {
380 vp = &evp->vnode;
381 if (vp->v_mount != maddr) {
382 /*
383 * New filesystem
384 */
385 if ((mp = getmnt(vp->v_mount)) == NULL((void *)0))
386 continue;
387 maddr = vp->v_mount;
388 mount_print(mp);
389 vnode_header();
390 if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_FFS"ffs", MFSNAMELEN16) ||
391 !strncmp(mp->mnt_stat.f_fstypename, MOUNT_MFS"mfs", MFSNAMELEN16)) {
392 ufs_header();
393 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_NFS"nfs",
394 MFSNAMELEN16)) {
395 nfs_header();
396 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_EXT2FS"ext2fs",
397 MFSNAMELEN16)) {
398 ext2fs_header();
399 }
400 (void)printf("\n");
401 }
402 vnode_print(evp->vptr, vp);
403
404 /* Syncer vnodes have no associated fs-specific data */
405 if (vp->v_data == NULL((void *)0)) {
406 printf(" %6c %5c %7c\n", '-', '-', '-');
407 continue;
408 }
409
410 if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_FFS"ffs", MFSNAMELEN16) ||
411 !strncmp(mp->mnt_stat.f_fstypename, MOUNT_MFS"mfs", MFSNAMELEN16)) {
412 ufs_print(vp);
413 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_NFS"nfs", MFSNAMELEN16)) {
414 nfs_print(vp);
415 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_EXT2FS"ext2fs",
416 MFSNAMELEN16)) {
417 ext2fs_print(vp);
418 }
419 (void)printf("\n");
420 }
421 free(e_vnodebase);
422}
423
424void
425vnodemodeprep(void)
426{
427 int mib[2];
428 size_t num;
429
430 if (kd == NULL((void *)0)) {
431 mib[0] = CTL_KERN1;
432 mib[1] = KERN_NUMVNODES58;
433 num = sizeof(numvnodes);
434 if (sysctl(mib, 2, &numvnodes, &num, NULL((void *)0), 0) < 0)
435 err(1, "sysctl(KERN_NUMVNODES) failed");
436 }
437}
438
439void
440vnode_header(void)
441{
442 (void)printf("%*s TYP VFLAG USE HOLD", 2 * (int)sizeof(long), "ADDR");
443}
444
445void
446vnode_print(struct vnode *avnode, struct vnode *vp)
447{
448 char *type, flags[16];
449 char *fp;
450 int flag;
451
452 /*
453 * set type
454 */
455 switch (vp->v_type) {
456 case VNON:
457 type = "non"; break;
458 case VREG:
459 type = "reg"; break;
460 case VDIR:
461 type = "dir"; break;
462 case VBLK:
463 type = "blk"; break;
464 case VCHR:
465 type = "chr"; break;
466 case VLNK:
467 type = "lnk"; break;
468 case VSOCK:
469 type = "soc"; break;
470 case VFIFO:
471 type = "fif"; break;
472 case VBAD:
473 type = "bad"; break;
474 default:
475 type = "unk"; break;
476 }
477 /*
478 * gather flags
479 */
480 fp = flags;
481 flag = vp->v_flag;
482 if (flag & VROOT0x0001)
483 *fp++ = 'R';
484 if (flag & VTEXT0x0002)
485 *fp++ = 'T';
486 if (flag & VSYSTEM0x0004)
487 *fp++ = 'S';
488 if (flag & VISTTY0x0008)
489 *fp++ = 'I';
490 if (flag & VXLOCK0x0100)
491 *fp++ = 'L';
492 if (flag & VXWANT0x0200)
493 *fp++ = 'W';
494 if (vp->v_bioflag & VBIOWAIT0x0001)
495 *fp++ = 'B';
496 if (flag & VALIASED0x0800)
497 *fp++ = 'A';
498 if (vp->v_bioflag & VBIOONFREELIST0x0004)
499 *fp++ = 'F';
500 if (flag & VLOCKSWORK0x4000)
501 *fp++ = 'l';
502 if (vp->v_bioflag & VBIOONSYNCLIST0x0002)
503 *fp++ = 's';
504 if (fp == flags)
505 *fp++ = '-';
506 *fp = '\0';
507 (void)printf("%0*lx %s %5s %4d %4u",
508 2 * (int)sizeof(long), hideroot ? 0L : (long)avnode,
509 type, flags, vp->v_usecount, vp->v_holdcnt);
510}
511
512void
513ufs_header(void)
514{
515 (void)printf(" FILEID IFLAG RDEV|SZ");
516}
517
518int
519ufs_print(struct vnode *vp)
520{
521 int flag;
522 struct inode inode, *ip = &inode;
523 struct ufs1_dinode di1;
524 char flagbuf[16], *flags = flagbuf;
525 char *name;
526 mode_t type;
527
528 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode")if (kvm_read(kd, (u_long)(((struct inode *)(vp)->v_data)),
&inode, sizeof(struct inode)) != sizeof(struct inode)) {
warnx("cannot read %s: %s", "vnode's inode", kvm_geterr(kd))
; return (0); }
;
529 KGETRET(inode.i_din1, &di1, sizeof(struct ufs1_dinode),if (kvm_read(kd, (u_long)(inode.dinode_u.ffs1_din), &di1,
sizeof(struct ufs1_dinode)) != sizeof(struct ufs1_dinode)) {
warnx("cannot read %s: %s", "vnode's dinode", kvm_geterr(kd)
); return (0); }
530 "vnode's dinode")if (kvm_read(kd, (u_long)(inode.dinode_u.ffs1_din), &di1,
sizeof(struct ufs1_dinode)) != sizeof(struct ufs1_dinode)) {
warnx("cannot read %s: %s", "vnode's dinode", kvm_geterr(kd)
); return (0); }
;
531
532 inode.i_din1dinode_u.ffs1_din = &di1;
533 flag = ip->i_flag;
534#if 0
535 if (flag & IN_LOCKED)
536 *flags++ = 'L';
537 if (flag & IN_WANTED)
538 *flags++ = 'W';
539 if (flag & IN_LWAIT)
540 *flags++ = 'Z';
541#endif
542 if (flag & IN_ACCESS0x0001)
543 *flags++ = 'A';
544 if (flag & IN_CHANGE0x0002)
545 *flags++ = 'C';
546 if (flag & IN_UPDATE0x0004)
547 *flags++ = 'U';
548 if (flag & IN_MODIFIED0x0008)
549 *flags++ = 'M';
550 if (flag & IN_LAZYMOD0x0080)
551 *flags++ = 'm';
552 if (flag & IN_RENAME0x0010)
553 *flags++ = 'R';
554 if (flag & IN_SHLOCK0x0020)
555 *flags++ = 'S';
556 if (flag & IN_EXLOCK0x0040)
557 *flags++ = 'E';
558 if (flag == 0)
559 *flags++ = '-';
560 *flags = '\0';
561
562 (void)printf(" %6d %5s", ip->i_number, flagbuf);
563 type = ip->i_ffs1_modedinode_u.ffs1_din->di_mode & S_IFMT0170000;
564 if (S_ISCHR(ip->i_ffs1_mode)((ip->dinode_u.ffs1_din->di_mode & 0170000) == 0020000
)
|| S_ISBLK(ip->i_ffs1_mode)((ip->dinode_u.ffs1_din->di_mode & 0170000) == 0060000
)
)
565 if (usenumflag ||
566 ((name = devname(ip->i_ffs1_rdevdinode_u.ffs1_din->di_db[0], type)) == NULL((void *)0)))
567 (void)printf(" %2u,%-2u",
568 major(ip->i_ffs1_rdev)(((unsigned)(ip->dinode_u.ffs1_din->di_db[0]) >> 8
) & 0xff)
, minor(ip->i_ffs1_rdev)((unsigned)((ip->dinode_u.ffs1_din->di_db[0]) & 0xff
) | (((ip->dinode_u.ffs1_din->di_db[0]) & 0xffff0000
) >> 8))
);
569 else
570 (void)printf(" %7s", name);
571 else
572 (void)printf(" %7lld", (long long)ip->i_ffs1_sizedinode_u.ffs1_din->di_size);
573 return (0);
574}
575
576void
577ext2fs_header(void)
578{
579 (void)printf(" FILEID IFLAG SZ");
580}
581
582int
583ext2fs_print(struct vnode *vp)
584{
585 int flag;
586 struct inode inode, *ip = &inode;
587 struct ext2fs_dinode di;
588 char flagbuf[16], *flags = flagbuf;
589
590 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode")if (kvm_read(kd, (u_long)(((struct inode *)(vp)->v_data)),
&inode, sizeof(struct inode)) != sizeof(struct inode)) {
warnx("cannot read %s: %s", "vnode's inode", kvm_geterr(kd))
; return (0); }
;
591 KGETRET(inode.i_e2din, &di, sizeof(struct ext2fs_dinode),if (kvm_read(kd, (u_long)(inode.dinode_u.e2fs_din), &di, sizeof
(struct ext2fs_dinode)) != sizeof(struct ext2fs_dinode)) { warnx
("cannot read %s: %s", "vnode's dinode", kvm_geterr(kd)); return
(0); }
592 "vnode's dinode")if (kvm_read(kd, (u_long)(inode.dinode_u.e2fs_din), &di, sizeof
(struct ext2fs_dinode)) != sizeof(struct ext2fs_dinode)) { warnx
("cannot read %s: %s", "vnode's dinode", kvm_geterr(kd)); return
(0); }
;
593
594 inode.i_e2dindinode_u.e2fs_din = &di;
595 flag = ip->i_flag;
596
597#if 0
598 if (flag & IN_LOCKED)
599 *flags++ = 'L';
600 if (flag & IN_WANTED)
601 *flags++ = 'W';
602 if (flag & IN_LWAIT)
603 *flags++ = 'Z';
604#endif
605 if (flag & IN_ACCESS0x0001)
606 *flags++ = 'A';
607 if (flag & IN_CHANGE0x0002)
608 *flags++ = 'C';
609 if (flag & IN_UPDATE0x0004)
610 *flags++ = 'U';
611 if (flag & IN_MODIFIED0x0008)
612 *flags++ = 'M';
613 if (flag & IN_RENAME0x0010)
614 *flags++ = 'R';
615 if (flag & IN_SHLOCK0x0020)
616 *flags++ = 'S';
617 if (flag & IN_EXLOCK0x0040)
618 *flags++ = 'E';
619 if (flag == 0)
620 *flags++ = '-';
621 *flags = '\0';
622
623 (void)printf(" %6d %5s %2d", ip->i_number, flagbuf, ip->i_e2fs_sizedinode_u.e2fs_din->e2di_size);
624 return (0);
625}
626
627void
628nfs_header(void)
629{
630 (void)printf(" FILEID NFLAG RDEV|SZ");
631}
632
633int
634nfs_print(struct vnode *vp)
635{
636 struct nfsnode nfsnode, *np = &nfsnode;
637 char flagbuf[16], *flags = flagbuf;
638 int flag;
639 char *name;
640 mode_t type;
641
642 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode")if (kvm_read(kd, (u_long)(((struct nfsnode *)(vp)->v_data)
), &nfsnode, sizeof(nfsnode)) != sizeof(nfsnode)) { warnx
("cannot read %s: %s", "vnode's nfsnode", kvm_geterr(kd)); return
(0); }
;
643 flag = np->n_flag;
644 if (flag & NFLUSHWANT0x0001)
645 *flags++ = 'W';
646 if (flag & NFLUSHINPROG0x0002)
647 *flags++ = 'P';
648 if (flag & NMODIFIED0x0004)
649 *flags++ = 'M';
650 if (flag & NWRITEERR0x0008)
651 *flags++ = 'E';
652 if (flag & NACC0x0100)
653 *flags++ = 'A';
654 if (flag & NUPD0x0200)
655 *flags++ = 'U';
656 if (flag & NCHG0x0400)
657 *flags++ = 'C';
658 if (flag == 0)
659 *flags++ = '-';
660 *flags = '\0';
661
662 (void)printf(" %6lld %5s", (long long)np->n_vattr.va_fileid, flagbuf);
663 type = np->n_vattr.va_mode & S_IFMT0170000;
664 if (S_ISCHR(np->n_vattr.va_mode)((np->n_vattr.va_mode & 0170000) == 0020000) || S_ISBLK(np->n_vattr.va_mode)((np->n_vattr.va_mode & 0170000) == 0060000))
665 if (usenumflag ||
666 ((name = devname(np->n_vattr.va_rdev, type)) == NULL((void *)0)))
667 (void)printf(" %2u,%-2u", major(np->n_vattr.va_rdev)(((unsigned)(np->n_vattr.va_rdev) >> 8) & 0xff),
668 minor(np->n_vattr.va_rdev)((unsigned)((np->n_vattr.va_rdev) & 0xff) | (((np->
n_vattr.va_rdev) & 0xffff0000) >> 8))
);
669 else
670 (void)printf(" %7s", name);
671 else
672 (void)printf(" %7lld", (long long)np->n_size);
673 return (0);
674}
675
676/*
677 * Given a pointer to a mount structure in kernel space,
678 * read it in and return a usable pointer to it.
679 */
680struct mount *
681getmnt(struct mount *maddr)
682{
683 static struct mtab {
684 struct mtab *next;
685 struct mount *maddr;
686 struct mount mount;
687 } *mhead = NULL((void *)0);
688 struct mtab *mt;
689
690 for (mt = mhead; mt != NULL((void *)0); mt = mt->next)
691 if (maddr == mt->maddr)
692 return (&mt->mount);
693 if ((mt = malloc(sizeof(struct mtab))) == NULL((void *)0))
694 err(1, "malloc: mount table");
695 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table")if (kvm_read(kd, (u_long)(maddr), &mt->mount, sizeof(struct
mount)) != sizeof(struct mount)) { warnx("cannot read %s: %s"
, "mount table", kvm_geterr(kd)); return (0); }
;
696 mt->maddr = maddr;
697 mt->next = mhead;
698 mhead = mt;
699 return (&mt->mount);
700}
701
702void
703mount_print(struct mount *mp)
704{
705 int flags;
706
707 (void)printf("*** MOUNT ");
708 (void)printf("%.*s %s on %s", MFSNAMELEN16,
709 mp->mnt_stat.f_fstypename, mp->mnt_stat.f_mntfromname,
710 mp->mnt_stat.f_mntonname);
711 if ((flags = mp->mnt_flag)) {
712 char *comma = "(";
713
714 putchar(' ')(!__isthreaded ? __sputc(' ', (&__sF[1])) : (putc)(' ', (
&__sF[1])))
;
715 /* user visible flags */
716 if (flags & MNT_RDONLY0x00000001) {
717 (void)printf("%srdonly", comma);
718 flags &= ~MNT_RDONLY0x00000001;
719 comma = ",";
720 }
721 if (flags & MNT_SYNCHRONOUS0x00000002) {
722 (void)printf("%ssynchronous", comma);
723 flags &= ~MNT_SYNCHRONOUS0x00000002;
724 comma = ",";
725 }
726 if (flags & MNT_NOEXEC0x00000004) {
727 (void)printf("%snoexec", comma);
728 flags &= ~MNT_NOEXEC0x00000004;
729 comma = ",";
730 }
731 if (flags & MNT_NOSUID0x00000008) {
732 (void)printf("%snosuid", comma);
733 flags &= ~MNT_NOSUID0x00000008;
734 comma = ",";
735 }
736 if (flags & MNT_NODEV0x00000010) {
737 (void)printf("%snodev", comma);
738 flags &= ~MNT_NODEV0x00000010;
739 comma = ",";
740 }
741 if (flags & MNT_ASYNC0x00000040) {
742 (void)printf("%sasync", comma);
743 flags &= ~MNT_ASYNC0x00000040;
744 comma = ",";
745 }
746 if (flags & MNT_EXRDONLY0x00000080) {
747 (void)printf("%sexrdonly", comma);
748 flags &= ~MNT_EXRDONLY0x00000080;
749 comma = ",";
750 }
751 if (flags & MNT_EXPORTED0x00000100) {
752 (void)printf("%sexport", comma);
753 flags &= ~MNT_EXPORTED0x00000100;
754 comma = ",";
755 }
756 if (flags & MNT_DEFEXPORTED0x00000200) {
757 (void)printf("%sdefdexported", comma);
758 flags &= ~MNT_DEFEXPORTED0x00000200;
759 comma = ",";
760 }
761 if (flags & MNT_EXPORTANON0x00000400) {
762 (void)printf("%sexportanon", comma);
763 flags &= ~MNT_EXPORTANON0x00000400;
764 comma = ",";
765 }
766 if (flags & MNT_WXALLOWED0x00000800) {
767 (void)printf("%swxallowed", comma);
768 flags &= ~MNT_WXALLOWED0x00000800;
769 comma = ",";
770 }
771 if (flags & MNT_LOCAL0x00001000) {
772 (void)printf("%slocal", comma);
773 flags &= ~MNT_LOCAL0x00001000;
774 comma = ",";
775 }
776 if (flags & MNT_QUOTA0x00002000) {
777 (void)printf("%squota", comma);
778 flags &= ~MNT_QUOTA0x00002000;
779 comma = ",";
780 }
781 if (flags & MNT_ROOTFS0x00004000) {
782 (void)printf("%srootfs", comma);
783 flags &= ~MNT_ROOTFS0x00004000;
784 comma = ",";
785 }
786 if (flags & MNT_NOATIME0x00008000) {
787 (void)printf("%snoatime", comma);
788 flags &= ~MNT_NOATIME0x00008000;
789 comma = ",";
790 }
791 /* filesystem control flags */
792 if (flags & MNT_UPDATE0x00010000) {
793 (void)printf("%supdate", comma);
794 flags &= ~MNT_UPDATE0x00010000;
795 comma = ",";
796 }
797 if (flags & MNT_DELEXPORT0x00020000) {
798 (void)printf("%sdelexport", comma);
799 flags &= ~MNT_DELEXPORT0x00020000;
800 comma = ",";
801 }
802 if (flags & MNT_RELOAD0x00040000) {
803 (void)printf("%sreload", comma);
804 flags &= ~MNT_RELOAD0x00040000;
805 comma = ",";
806 }
807 if (flags & MNT_FORCE0x00080000) {
808 (void)printf("%sforce", comma);
809 flags &= ~MNT_FORCE0x00080000;
810 comma = ",";
811 }
812 if (flags & MNT_WANTRDWR0x02000000) {
813 (void)printf("%swantrdwr", comma);
814 flags &= ~MNT_WANTRDWR0x02000000;
815 comma = ",";
816 }
817 if (flags & MNT_SOFTDEP0x04000000) {
818 (void)printf("%ssoftdep", comma);
819 flags &= ~MNT_SOFTDEP0x04000000;
820 comma = ",";
821 }
822 if (flags)
823 (void)printf("%sunknown_flags:%x", comma, flags);
824 (void)printf(")");
825 }
826 (void)printf("\n");
827}
828
829/*
830 * simulate what a running kernel does in kinfo_vnode
831 */
832struct e_vnode *
833kinfo_vnodes(void)
834{
835 struct mntlist kvm_mountlist;
836 struct mount *mp, mount;
837 struct vnode *vp, vnode;
838 char *vbuf, *evbuf, *bp;
839 size_t num;
840
841 if (kd
27.1
'kd' is not equal to NULL
!= NULL((void *)0))
28
Taking true branch
842 KGET(V_NUMV, numvnodes)if (kvm_read(kd, (u_long)(globalnl[3].n_value), &numvnodes
, sizeof(numvnodes)) != sizeof(numvnodes)) warnx("cannot read %s: %s"
, "numvnodes", kvm_geterr(kd))
;
29
Assuming the condition is false
30
Taking false branch
843 if (totalflag
30.1
'totalflag' is 0
)
31
Taking false branch
844 return NULL((void *)0);
845 if ((vbuf = calloc(numvnodes + 20,
32
Memory is allocated
33
Assuming the condition is false
34
Taking false branch
846 sizeof(struct vnode *) + sizeof(struct vnode))) == NULL((void *)0))
847 err(1, "malloc: vnode buffer");
848 bp = vbuf;
849 evbuf = vbuf + (numvnodes + 20) *
850 (sizeof(struct vnode *) + sizeof(struct vnode));
851 KGET(V_MOUNTLIST, kvm_mountlist)if (kvm_read(kd, (u_long)(globalnl[5].n_value), &kvm_mountlist
, sizeof(kvm_mountlist)) != sizeof(kvm_mountlist)) warnx("cannot read %s: %s"
, "kvm_mountlist", kvm_geterr(kd))
;
35
Assuming the condition is false
36
Taking false branch
852 num = 0;
853 for (mp = TAILQ_FIRST(&kvm_mountlist)((&kvm_mountlist)->tqh_first); mp != NULL((void *)0);
37
Assuming 'mp' is not equal to NULL
38
Loop condition is true. Entering loop body
854 mp = TAILQ_NEXT(&mount, mnt_list)((&mount)->mnt_list.tqe_next)) {
855 KGETRET(mp, &mount, sizeof(mount), "mount entry")if (kvm_read(kd, (u_long)(mp), &mount, sizeof(mount)) != sizeof
(mount)) { warnx("cannot read %s: %s", "mount entry", kvm_geterr
(kd)); return (0); }
;
39
Assuming the condition is true
40
Taking true branch
41
Potential leak of memory pointed to by 'bp'
856 for (vp = TAILQ_FIRST(&mount.mnt_vnodelist)((&mount.mnt_vnodelist)->tqh_first);
857 vp != NULL((void *)0); vp = TAILQ_NEXT(&vnode, v_mntvnodes)((&vnode)->v_mntvnodes.tqe_next)) {
858 KGETRET(vp, &vnode, sizeof(vnode), "vnode")if (kvm_read(kd, (u_long)(vp), &vnode, sizeof(vnode)) != sizeof
(vnode)) { warnx("cannot read %s: %s", "vnode", kvm_geterr(kd
)); return (0); }
;
859 if ((bp + sizeof(struct vnode *) +
860 sizeof(struct vnode)) > evbuf)
861 /* XXX - should realloc */
862 errx(1, "no more room for vnodes");
863 memmove(bp, &vp, sizeof(struct vnode *));
864 bp += sizeof(struct vnode *);
865 memmove(bp, &vnode, sizeof(struct vnode));
866 bp += sizeof(struct vnode);
867 num++;
868 }
869 }
870 numvnodes = num;
871 return ((struct e_vnode *)vbuf);
872}
873
874const char hdr[] =
875" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n";
876
877void
878tty2itty(struct tty *tp, struct itty *itp)
879{
880 itp->t_dev = tp->t_dev;
881 itp->t_rawq_c_cc = tp->t_rawq.c_cc;
882 itp->t_canq_c_cc = tp->t_canq.c_cc;
883 itp->t_outq_c_cc = tp->t_outq.c_cc;
884 itp->t_hiwat = tp->t_hiwat;
885 itp->t_lowat = tp->t_lowat;
886 itp->t_column = tp->t_column;
887 itp->t_state = tp->t_state;
888 itp->t_session = tp->t_session;
889 if (tp->t_pgrp != NULL((void *)0))
890 KGET2(&tp->t_pgrp->pg_id, &itp->t_pgrp_pg_id, sizeof(pid_t), "pgid")if (kvm_read(kd, (u_long)(&tp->t_pgrp->pg_id), &
itp->t_pgrp_pg_id, sizeof(pid_t)) != sizeof(pid_t)) warnx(
"cannot read %s: %s", "pgid", kvm_geterr(kd))
;
891 itp->t_line = tp->t_line;
892}
893
894void
895ttymode(void)
896{
897 struct ttylist_head tty_head;
898 struct tty *tp, tty;
899 int i;
900 struct itty itty;
901
902 if (need_nlist)
903 KGET(TTY_NTTY, ntty)if (kvm_read(kd, (u_long)(globalnl[2].n_value), &ntty, sizeof
(ntty)) != sizeof(ntty)) warnx("cannot read %s: %s", "ntty", kvm_geterr
(kd))
;
904 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
905 (void)printf("%s", hdr);
906 if (!need_nlist) {
907 for (i = 0; i < ntty; i++)
908 ttyprt(&globalitp[i]);
909 free(globalitp);
910 } else {
911 KGET(TTY_TTYLIST, tty_head)if (kvm_read(kd, (u_long)(globalnl[4].n_value), &tty_head
, sizeof(tty_head)) != sizeof(tty_head)) warnx("cannot read %s: %s"
, "tty_head", kvm_geterr(kd))
;
912 for (tp = TAILQ_FIRST(&tty_head)((&tty_head)->tqh_first); tp;
913 tp = TAILQ_NEXT(&tty, tty_link)((&tty)->tty_link.tqe_next)) {
914 KGET2(tp, &tty, sizeof tty, "tty struct")if (kvm_read(kd, (u_long)(tp), &tty, sizeof tty) != sizeof
tty) warnx("cannot read %s: %s", "tty struct", kvm_geterr(kd
))
;
915 tty2itty(&tty, &itty);
916 ttyprt(&itty);
917 }
918 }
919}
920
921void
922ttymodeprep(void)
923{
924 int mib[3];
925 size_t nlen;
926
927 if (!need_nlist) {
928 mib[0] = CTL_KERN1;
929 mib[1] = KERN_TTYCOUNT57;
930 nlen = sizeof(ntty);
931 if (sysctl(mib, 2, &ntty, &nlen, NULL((void *)0), 0) < 0)
932 err(1, "sysctl(KERN_TTYCOUNT) failed");
933
934 mib[0] = CTL_KERN1;
935 mib[1] = KERN_TTY44;
936 mib[2] = KERN_TTY_INFO5;
937 if ((globalitp = reallocarray(NULL((void *)0), ntty, sizeof(struct itty))) == NULL((void *)0))
938 err(1, "malloc");
939 nlen = ntty * sizeof(struct itty);
940 if (sysctl(mib, 3, globalitp, &nlen, NULL((void *)0), 0) < 0)
941 err(1, "sysctl(KERN_TTY_INFO) failed");
942 }
943}
944
945struct {
946 int flag;
947 char val;
948} ttystates[] = {
949 { TS_WOPEN0x00200, 'W'},
950 { TS_ISOPEN0x00020, 'O'},
951 { TS_CARR_ON0x00008, 'C'},
952 { TS_TIMEOUT0x00080, 'T'},
953 { TS_FLUSH0x00010, 'F'},
954 { TS_BUSY0x00004, 'B'},
955 { TS_ASLEEP0x00001, 'A'},
956 { TS_XCLUDE0x00400, 'X'},
957 { TS_TTSTOP0x00100, 'S'},
958 { TS_TBLOCK0x00040, 'K'},
959 { TS_ASYNC0x00002, 'Y'},
960 { TS_BKSL0x00800, 'D'},
961 { TS_ERASE0x02000, 'E'},
962 { TS_LNCH0x04000, 'L'},
963 { TS_TYPEN0x08000, 'P'},
964 { TS_CNTTB0x01000, 'N'},
965 { 0, '\0'},
966};
967
968void
969ttyprt(struct itty *tp)
970{
971 char *name, state[20];
972 int i, j;
973
974 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR0020000)) == NULL((void *)0))
975 (void)printf("%2u,%-3u ", major(tp->t_dev)(((unsigned)(tp->t_dev) >> 8) & 0xff), minor(tp->t_dev)((unsigned)((tp->t_dev) & 0xff) | (((tp->t_dev) &
0xffff0000) >> 8))
);
976 else
977 (void)printf("%7s ", name);
978 (void)printf("%3d %4d ", tp->t_rawq_c_cc, tp->t_canq_c_cc);
979 (void)printf("%4d %4d %3d %6d ", tp->t_outq_c_cc,
980 tp->t_hiwat, tp->t_lowat, tp->t_column);
981 for (i = j = 0; ttystates[i].flag; i++)
982 if (tp->t_state&ttystates[i].flag)
983 state[j++] = ttystates[i].val;
984 if (j == 0)
985 state[j++] = '-';
986 state[j] = '\0';
987 (void)printf("%-6s %8lx", state,
988 hideroot ? 0 : (u_long)tp->t_session & 0xffffffff);
989 (void)printf("%6d ", tp->t_pgrp_pg_id);
990 switch (tp->t_line) {
991 case TTYDISC0:
992 (void)printf("term\n");
993 break;
994 case PPPDISC5:
995 (void)printf("ppp\n");
996 break;
997 case NMEADISC7:
998 (void)printf("nmea\n");
999 break;
1000 default:
1001 (void)printf("%d\n", tp->t_line);
1002 break;
1003 }
1004}
1005
1006void
1007filemode(void)
1008{
1009 char flagbuf[16], *fbp;
1010 static char *dtypes[] = { "???", "inode", "socket", "pipe", "kqueue", "???", "???" };
1011
1012 globalnl = vnodenl;
1013
1014 if (nlistf != NULL((void *)0) || memf != NULL((void *)0)) {
1015 KGET(FNL_MAXFILE, maxfile)if (kvm_read(kd, (u_long)(globalnl[1].n_value), &maxfile,
sizeof(maxfile)) != sizeof(maxfile)) warnx("cannot read %s: %s"
, "maxfile", kvm_geterr(kd))
;
1016 if (totalflag) {
1017 KGET(FNL_NFILE, nfile)if (kvm_read(kd, (u_long)(globalnl[0].n_value), &nfile, sizeof
(nfile)) != sizeof(nfile)) warnx("cannot read %s: %s", "nfile"
, kvm_geterr(kd))
;
1018 (void)printf("%3d/%3d files\n", nfile, maxfile);
1019 return;
1020 }
1021 }
1022
1023 (void)printf("%d/%d open files\n", nfile, maxfile);
1024 if (totalflag)
1025 return;
1026
1027 (void)printf("%*s TYPE FLG CNT MSG %*s OFFSET\n",
1028 2 * (int)sizeof(long), "LOC", 2 * (int)sizeof(long), "DATA");
1029 for (; nfile-- > 0; kf++) {
1030 (void)printf("%0*llx ", 2 * (int)sizeof(long),
1031 hideroot ? 0LL : kf->f_fileaddr);
1032 (void)printf("%-8.8s", dtypes[
1033 (kf->f_type >= (sizeof(dtypes)/sizeof(dtypes[0])))
1034 ? 0 : kf->f_type]);
1035 fbp = flagbuf;
1036 if (kf->f_flag & FREAD0x0001)
1037 *fbp++ = 'R';
1038 if (kf->f_flag & FWRITE0x0002)
1039 *fbp++ = 'W';
1040 if (kf->f_flag & FAPPEND0x0008)
1041 *fbp++ = 'A';
1042 if (kf->f_flag & FASYNC0x0040)
1043 *fbp++ = 'I';
1044
1045 if (kf->f_iflags & FIF_HASLOCK0x01)
1046 *fbp++ = 'L';
1047
1048 *fbp = '\0';
1049 (void)printf("%6s %3ld", flagbuf, (long)kf->f_count);
1050 (void)printf(" %3ld", (long)kf->f_msgcount);
1051 (void)printf(" %0*lx", 2 * (int)sizeof(long),
1052 hideroot ? 0L : (long)kf->f_data);
1053
1054 if (kf->f_offset == (uint64_t)-1)
1055 (void)printf(" *\n");
1056 else if (kf->f_offset > INT64_MAX0x7fffffffffffffffLL) {
1057 /* would have been negative */
1058 (void)printf(" %llx\n",
1059 hideroot ? 0LL : (long long)kf->f_offset);
1060 } else
1061 (void)printf(" %lld\n",
1062 hideroot ? 0LL : (long long)kf->f_offset);
1063 }
1064}
1065
1066void
1067filemodeprep(void)
1068{
1069 int mib[2];
1070 size_t len;
1071
1072 if (nlistf == NULL((void *)0) && memf == NULL((void *)0)) {
1073 mib[0] = CTL_KERN1;
1074 mib[1] = KERN_MAXFILES7;
1075 len = sizeof(maxfile);
1076 if (sysctl(mib, 2, &maxfile, &len, NULL((void *)0), 0) < 0)
1077 err(1, "sysctl(KERN_MAXFILES) failed");
1078 if (totalflag) {
1079 mib[0] = CTL_KERN1;
1080 mib[1] = KERN_NFILES56;
1081 len = sizeof(nfile);
1082 if (sysctl(mib, 2, &nfile, &len, NULL((void *)0), 0) < 0)
1083 err(1, "sysctl(KERN_NFILES) failed");
1084 }
1085 }
1086
1087 if (!totalflag) {
1088 kf = kvm_getfiles(kd, KERN_FILE_BYFILE1, 0, sizeof *kf, &nfile);
1089 if (kf == NULL((void *)0)) {
1090 warnx("kvm_getfiles: %s", kvm_geterr(kd));
1091 return;
1092 }
1093 }
1094}
1095
1096/*
1097 * swapmode is based on a program called swapinfo written
1098 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
1099 */
1100void
1101swapmode(void)
1102{
1103 char *header;
1104 int hlen = 10, nswap;
1105 int bdiv, i, avail, nfree, npfree, used;
1106 long blocksize;
1107 struct swapent *swdev;
1108
1109 if (kflag) {
1110 header = "1K-blocks";
1111 blocksize = 1024;
1112 hlen = strlen(header);
1113 } else
1114 header = getbsize(&hlen, &blocksize);
1115
1116 nswap = swapctl(SWAP_NSWAP3, 0, 0);
1117 if (nswap == 0) {
1118 if (!totalflag)
1119 (void)printf("%-11s %*s %8s %8s %8s %s\n",
1120 "Device", hlen, header,
1121 "Used", "Avail", "Capacity", "Priority");
1122 (void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1123 "Total", hlen, 0, 0, 0, 0.0);
1124 return;
1125 }
1126 if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL((void *)0))
1127 err(1, "malloc");
1128 if (swapctl(SWAP_STATS4, swdev, nswap) == -1)
1129 err(1, "swapctl");
1130
1131 if (!totalflag)
1132 (void)printf("%-11s %*s %8s %8s %8s %s\n",
1133 "Device", hlen, header,
1134 "Used", "Avail", "Capacity", "Priority");
1135
1136 /* Run through swap list, doing the funky monkey. */
1137 bdiv = blocksize / DEV_BSIZE(1 << 9);
1138 avail = nfree = npfree = 0;
1139 for (i = 0; i < nswap; i++) {
1140 int xsize, xfree;
1141
1142 if (!(swdev[i].se_flags & SWF_ENABLE0x00000002))
1143 continue;
1144
1145 if (!totalflag) {
1146 if (usenumflag)
1147 (void)printf("%2u,%-2u %*d ",
1148 major(swdev[i].se_dev)(((unsigned)(swdev[i].se_dev) >> 8) & 0xff),
1149 minor(swdev[i].se_dev)((unsigned)((swdev[i].se_dev) & 0xff) | (((swdev[i].se_dev
) & 0xffff0000) >> 8))
,
1150 hlen, swdev[i].se_nblks / bdiv);
1151 else
1152 (void)printf("%-11s %*d ", swdev[i].se_path,
1153 hlen, swdev[i].se_nblks / bdiv);
1154 }
1155
1156 xsize = swdev[i].se_nblks;
1157 used = swdev[i].se_inuse;
1158 xfree = xsize - used;
1159 nfree += (xsize - used);
1160 npfree++;
1161 avail += xsize;
1162 if (totalflag)
1163 continue;
1164 (void)printf("%8d %8d %5.0f%% %d\n",
1165 used / bdiv, xfree / bdiv,
1166 (double)used / (double)xsize * 100.0,
1167 swdev[i].se_priority);
1168 }
1169 free(swdev);
1170
1171 /*
1172 * If only one partition has been set up via swapon(8), we don't
1173 * need to bother with totals.
1174 */
1175 used = avail - nfree;
1176 if (totalflag) {
1177 (void)printf("%dM/%dM swap space\n",
1178 used / (1048576 / DEV_BSIZE(1 << 9)),
1179 avail / (1048576 / DEV_BSIZE(1 << 9)));
1180 return;
1181 }
1182 if (npfree > 1) {
1183 (void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1184 "Total", hlen, avail / bdiv, used / bdiv, nfree / bdiv,
1185 (double)used / (double)avail * 100.0);
1186 }
1187}
1188
1189static void __dead__attribute__((__noreturn__))
1190usage(void)
1191{
1192 (void)fprintf(stderr(&__sF[2]), "usage: "
1193 "pstat [-fknsTtv] [-M core] [-N system] [-d format symbol ...]\n");
1194 exit(1);
1195}