Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
/*	$NetBSD: fstat.c,v 1.117 2022/10/28 05:27:16 ozaki-r Exp $	*/

/*-
 * Copyright (c) 1988, 1993
 *	The Regents of the University of California.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1988, 1993\
 The Regents of the University of California.  All rights reserved.");
#endif /* not lint */

#ifndef lint
#if 0
static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
#else
__RCSID("$NetBSD: fstat.c,v 1.117 2022/10/28 05:27:16 ozaki-r Exp $");
#endif
#endif /* not lint */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/proc.h>
#include <sys/stat.h>
#include <sys/vnode.h>
#include <sys/socket.h>
#include <sys/socketvar.h>
#include <sys/domain.h>
#include <sys/protosw.h>
#include <sys/unpcb.h>
#include <sys/sysctl.h>
#include <sys/filedesc.h>
#include <sys/pipe.h>
#define _KERNEL
#include <sys/mount.h>
#undef _KERNEL
#define	_KERNEL
#include <sys/file.h>
#include <ufs/ufs/inode.h>
#include <ufs/ufs/ufsmount.h>
#undef _KERNEL
#define NFS
#include <nfs/nfsproto.h>
#include <nfs/rpcv2.h>
#include <nfs/nfs.h>
#include <nfs/nfsnode.h>
#undef NFS
#include <msdosfs/denode.h>
#include <msdosfs/bpb.h>
#define	_KERNEL
#include <msdosfs/msdosfsmount.h>
#undef _KERNEL
#define	_KERNEL
#include <miscfs/genfs/layer.h>
#undef _KERNEL

#include <net/route.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/in_pcb.h>

#ifdef INET6
#include <netinet/ip6.h>
#include <netinet6/in6.h>
#include <netinet6/ip6_var.h>
#include <netinet6/in6_pcb.h>
#endif

#include <netatalk/at.h>
#include <netatalk/ddp_var.h>

#include <netdb.h>
#include <arpa/inet.h>

#include <ctype.h>
#include <errno.h>
#include <kvm.h>
#include <limits.h>
#include <nlist.h>
#include <paths.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include <util.h>

#include "fstat.h"

#define	TEXT	-1
#define	CDIR	-2
#define	RDIR	-3
#define	TRACE	-4

typedef struct devs {
	struct	devs *next;
	long	fsid;
	ino_t	ino;
	const char *name;
} DEVS;
static DEVS *devs;

static int 	fsflg,	/* show files on same filesystem as file(s) argument */
	pflg,	/* show files open by a particular pid */
	uflg;	/* show files open by a particular (effective) user */
static int 	checkfile; /* true if restricting to particular files or filesystems */
static int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
static int	Aflg;	/* prefix with address of file structure */
static int	Oflg;	/* print offset instead of size */
int	vflg;	/* display errors in locating kernel data objects etc... */

static fdfile_t **ofiles; /* buffer of pointers to file structures */
static int fstat_maxfiles;
#define ALLOC_OFILES(d)	\
	if ((d) > fstat_maxfiles) { \
		size_t len = (d) * sizeof(fdfile_t *); \
		free(ofiles); \
		ofiles = malloc(len); \
		if (ofiles == NULL) { \
			err(1, "malloc(%zu)", len);	\
		} \
		fstat_maxfiles = (d); \
	}

kvm_t *kd;

static const char *const dtypes[] = {
	DTYPE_NAMES
};

static void	dofiles(struct kinfo_proc2 *);
static int	ext2fs_filestat(struct vnode *, struct filestat *);
static int	getfname(const char *);
static void	getinetproto(char *, size_t, int);
static void	getatproto(char *, size_t, int);
static char   *getmnton(struct mount *);
static const char   *layer_filestat(struct vnode *, struct filestat *);
static int	msdosfs_filestat(struct vnode *, struct filestat *);
static int	nfs_filestat(struct vnode *, struct filestat *);
static const char *inet_addrstr(char *, size_t, const struct in_addr *,
    uint16_t, bool);
#ifdef INET6
static const char *inet6_addrstr(char *, size_t, const struct in6_addr *,
    uint16_t, bool);
#endif
static const char *at_addrstr(char *, size_t, const struct sockaddr_at *);
static void	socktrans(struct file *, struct socket *, int);
static void	misctrans(struct file *, int);
static int	ufs_filestat(struct vnode *, struct filestat *);
static void	usage(void) __dead;
static const char   *vfilestat(struct vnode *, struct filestat *);
static void	vtrans(struct file *, struct vnode *, int, int, long);
static void	ftrans(fdfile_t *, int);
static void	ptrans(struct file *, struct pipe *, int);
static void	kdriver_init(void);
static void	check_privs(void);

int
main(int argc, char **argv)
{
	struct passwd *passwd;
	struct kinfo_proc2 *p, *plast;
	int arg, ch, what;
	char *memf, *nlistf;
	char buf[_POSIX2_LINE_MAX];
	int cnt;
	gid_t egid = getegid();

	(void)setegid(getgid());
	arg = 0;
	what = KERN_PROC_ALL;
	nlistf = memf = NULL;
	while ((ch = getopt(argc, argv, "fnAOp:u:vN:M:")) != -1)
		switch((char)ch) {
		case 'f':
			fsflg = 1;
			break;
		case 'M':
			memf = optarg;
			break;
		case 'N':
			nlistf = optarg;
			break;
		case 'n':
			nflg = 1;
			break;
		case 'A':
			Aflg = 1;
			break;
		case 'O':
			Oflg = 1;
			break;
		case 'p':
			if (pflg++)
				usage();
			if (!isdigit((unsigned char)*optarg)) {
				warnx("-p requires a process id");
				usage();
			}
			what = KERN_PROC_PID;
			arg = atoi(optarg);
			break;
		case 'u':
			if (uflg++)
				usage();
			if (!(passwd = getpwnam(optarg))) {
				errx(1, "%s: unknown uid", optarg);
			}
			what = KERN_PROC_UID;
			arg = passwd->pw_uid;
			break;
		case 'v':
			vflg = 1;
			break;
		case '?':
		default:
			usage();
		}

	check_privs();

	kdriver_init();

	if (*(argv += optind)) {
		for (; *argv; ++argv) {
			if (getfname(*argv))
				checkfile = 1;
		}
		if (!checkfile)	/* file(s) specified, but none accessible */
			exit(1);
	}

	ALLOC_OFILES(256);	/* reserve space for file pointers */

	if (fsflg && !checkfile) {	
		/* -f with no files means use wd */
		if (getfname(".") == 0)
			exit(1);
		checkfile = 1;
	}

	/*
	 * Discard setgid privileges.  If not the running kernel, we toss
	 * them away totally so that bad guys can't print interesting stuff
	 * from kernel memory, otherwise switch back to kmem for the
	 * duration of the kvm_openfiles() call.
	 */
	if (nlistf != NULL || memf != NULL)
		(void)setgid(getgid());
	else
		(void)setegid(egid);

	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
		errx(1, "%s", buf);

	/* get rid of it now anyway */
	if (nlistf == NULL && memf == NULL)
		(void)setgid(getgid());

	if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) {
		errx(1, "%s", kvm_geterr(kd));
	}
	if (Aflg)
		(void)printf("%-*s ", 2*(int)(sizeof(void*)), "ADDR");
	if (nflg)
		(void)printf(
"USER     CMD          PID   FD    DEV       INUM   MODE  %s R/W",
    Oflg ? " OFFS" : "SZ|DV" );
	else
		(void)printf(
"USER     CMD          PID   FD  MOUNT         INUM MODE         %s R/W",
    Oflg ? " OFFS" : "SZ|DV" );

	if (checkfile && fsflg == 0)
		(void)printf(" NAME\n");
	else
		(void)putchar('\n');

	for (plast = &p[cnt]; p < plast; ++p) {
		if (p->p_stat == SZOMB)
			continue;
		dofiles(p);
	}
	return 0;
}

static void
check_privs(void)
{
	int expaddr;
	size_t expsize = sizeof(expaddr);
	const char *expname = "kern.expose_address";

	if (geteuid() == 0)
		return;

	if (sysctlbyname(expname, &expaddr, &expsize, NULL, 0) == -1)
		err(EXIT_FAILURE, "Can't get sysctl `%s'", expname);
	if (expaddr == 0)
		errx(EXIT_FAILURE, "This program does not work without "
		    "sysctl `%s' being set", expname);
}

static const	char *Uname, *Comm;
pid_t	Pid;

#define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
	switch(i) { \
	case TEXT: \
		(void)printf(" text"); \
		break; \
	case CDIR: \
		(void)printf("   wd"); \
		break; \
	case RDIR: \
		(void)printf(" root"); \
		break; \
	case TRACE: \
		(void)printf("   tr"); \
		break; \
	default: \
		(void)printf(" %4d", i); \
		break; \
	}

static struct kinfo_drivers *kdriver;
static size_t kdriverlen;

static int
kdriver_comp(const void *a, const void *b)
{
	const struct kinfo_drivers *ka = a;
	const struct kinfo_drivers *kb = b;
	int kac = ka->d_cmajor == -1 ? 0 : ka->d_cmajor;
	int kbc = kb->d_cmajor == -1 ? 0 : kb->d_cmajor;
	int kab = ka->d_bmajor == -1 ? 0 : ka->d_bmajor;
	int kbb = kb->d_bmajor == -1 ? 0 : kb->d_bmajor;
	int c = kac - kbc;
	if (c == 0)
		return kab - kbb;
	else
		return c;
}

static const char *
kdriver_search(int type, dev_t num)
{
	struct kinfo_drivers k, *kp;
	static char buf[64];

	if (nflg)
		goto out;

	if (type == VBLK) {
		k.d_bmajor = num;
		k.d_cmajor = -1;
	} else {
		k.d_bmajor = -1;
		k.d_cmajor = num;
	}
	kp = bsearch(&k, kdriver, kdriverlen, sizeof(*kdriver), kdriver_comp);
	if (kp)
		return kp->d_name;
out:	
	snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);
	return buf;
}


static void
kdriver_init(void)
{
	size_t sz;
	int error;
	static const int name[2] = { CTL_KERN, KERN_DRIVERS };

	error = sysctl(name, __arraycount(name), NULL, &sz, NULL, 0);
	if (error == -1) {
		warn("sysctl kern.drivers");
		return;
	}

	if (sz % sizeof(*kdriver)) {
		warnx("bad size %zu for kern.drivers", sz);
		return;
	}

	kdriver = malloc(sz);
	if (kdriver == NULL) {
		warn("malloc");
		return;
	}

	error = sysctl(name, __arraycount(name), kdriver, &sz, NULL, 0);
	if (error == -1) {
		warn("sysctl kern.drivers");
		return;
	}

	kdriverlen = sz / sizeof(*kdriver);
	qsort(kdriver, kdriverlen, sizeof(*kdriver), kdriver_comp);
#ifdef DEBUG
	for (size_t i = 0; i < kdriverlen; i++)
		printf("%d %d %s\n", kdriver[i].d_cmajor, kdriver[i].d_bmajor,
		    kdriver[i].d_name);
#endif
}

/*
 * print open files attributed to this process
 */
static void
dofiles(struct kinfo_proc2 *p)
{
	int i;
	struct filedesc filed;
	struct cwdinfo cwdi;
	struct fdtab dt;

	Uname = user_from_uid(p->p_uid, 0);
	Pid = p->p_pid;
	Comm = p->p_comm;

	if (p->p_fd == 0 || p->p_cwdi == 0)
		return;
	if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
		warnx("can't read filedesc at %p for pid %d",
		    (void *)(uintptr_t)p->p_fd, Pid);
		return;
	}
	if (filed.fd_lastfile == -1)
		return;
	if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
		warnx("can't read cwdinfo at %p for pid %d",
		    (void *)(uintptr_t)p->p_cwdi, Pid);
		return;
	}
	if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) {
		warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid);
		return;
	}
	if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles ||
	    filed.fd_freefile > filed.fd_lastfile + 1) {
		dprintf("filedesc corrupted at %p for pid %d",
		    (void *)(uintptr_t)p->p_fd, Pid);
		return;
	}
	/*
	 * root directory vnode, if one
	 */
	if (cwdi.cwdi_rdir)
		vtrans(NULL, cwdi.cwdi_rdir, RDIR, FREAD, (long)cwdi.cwdi_rdir);
	/*
	 * current working directory vnode
	 */
	vtrans(NULL, cwdi.cwdi_cdir, CDIR, FREAD, (long)cwdi.cwdi_cdir);
#if 0
	/*
	 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
	 * ktrace vnode, if one
	 */
	if (p->p_tracep)
		ftrans(p->p_tracep, TRACE);
#endif
	/*
	 * open files
	 */
#define FPSIZE	(sizeof (fdfile_t *))
	ALLOC_OFILES(filed.fd_lastfile+1);
	if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles,
	    (filed.fd_lastfile+1) * FPSIZE)) {
		dprintf("can't read file structures at %p for pid %d",
		    &filed.fd_dt->dt_ff, Pid);
		return;
	}
	for (i = 0; i <= filed.fd_lastfile; i++) {
		if (ofiles[i] == NULL)
			continue;
		ftrans(ofiles[i], i);
	}
}

static void
ftrans(fdfile_t *fp, int i)
{
	struct file file;
	fdfile_t fdfile;

	if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
		dprintf("can't read file %d at %p for pid %d",
		    i, fp, Pid);
		return;
	}
	if (fdfile.ff_file == NULL) {
		dprintf("null ff_file for %d at %p for pid %d",
		    i, fp, Pid);
		return;
	}
	if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
		dprintf("can't read file %d at %p for pid %d",
		    i, fdfile.ff_file, Pid);
		return;
	}
	if (Aflg && file.f_type != DTYPE_VNODE && checkfile == 0)
		(void)printf("%*lx ",
			2*(int)(sizeof(void*)), (long)fdfile.ff_file);
	switch (file.f_type) {
	case DTYPE_VNODE:
		vtrans(&file, file.f_data, i, file.f_flag, (long)fdfile.ff_file);
		break;
	case DTYPE_SOCKET:
		socktrans(&file, file.f_data, i);
		break;
	case DTYPE_PIPE:
		if (checkfile == 0)
			ptrans(&file, file.f_data, i);
		break;
	case DTYPE_MISC:
	case DTYPE_KQUEUE:
	case DTYPE_CRYPTO:
	case DTYPE_MQUEUE:
	case DTYPE_SEM:
		if (checkfile == 0)
			misctrans(&file, i);
		break;
	default:
		dprintf("unknown file type %d for file %d of pid %d",
		    file.f_type, i, Pid);
		break;
	}
}

static const char dead[] = "dead";
static const char *vnode_tag[] = {
	VNODE_TAGS
};

static const char *
vfilestat(struct vnode *vp, struct filestat *fsp)
{
	const char *badtype = NULL;

	if (vp->v_type == VNON)
		badtype = "none";
	else if (vp->v_type == VBAD)
		badtype = "bad";
	else
		switch (vp->v_tag) {
		case VT_NON:
			badtype = dead;
			break;
		case VT_UFS:
		case VT_LFS:
		case VT_MFS:
			if (!ufs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_MSDOSFS:
			if (!msdosfs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_NFS:
			if (!nfs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_EXT2FS:
			if (!ext2fs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_ISOFS:
			if (!isofs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_NTFS:
			if (!ntfs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_PTYFS:
			if (!ptyfs_filestat(vp, fsp))
				badtype = "error";
			break;
		case VT_TMPFS:
			if (!tmpfs_filestat(vp, fsp))
				badtype = "error";
			break;
#ifdef HAVE_ZFS
		case VT_ZFS:
			if (!zfs_filestat(vp, fsp))
				badtype = "error";
			break;
#endif
		case VT_NULL:
		case VT_OVERLAY:
		case VT_UMAP:
			badtype = layer_filestat(vp, fsp);
			break;
		default: {
			static char unknown[10];
			(void)snprintf(unknown, sizeof unknown, "%s(%#x)",
			    (size_t)vp->v_tag < __arraycount(vnode_tag) ?
			    vnode_tag[vp->v_tag] : "?", vp->v_tag);
			badtype = unknown;
			break;
		}
	}
	return badtype;
}

static int
checkfs(struct vnode *vp, struct vnode *vn, struct filestat *fst,
    const char **type, const char **fname)
{
	*fname = NULL;
	if (!KVM_READ(vp, vn, sizeof(*vn))) {
		dprintf("can't read vnode at %p for pid %d", vp, Pid);
		return 0;
	}
	*type = vfilestat(vn, fst);
	if (checkfile) {
		int fsmatch = 0;
		DEVS *d;
#if 0
		if (*type && *type != dead)
			return 0;
#endif
		for (d = devs; d != NULL; d = d->next) {
			if (d->fsid == fst->fsid) {
				fsmatch = 1;
				if (d->ino == fst->fileid) {
					*fname = d->name;
					break;
				}
			}
		}
		if (fsmatch == 0 || (*fname == NULL && fsflg == 0))
			return 0;
	}
	return 1;
}

static void
vprint(struct vnode *vn, struct filestat *fst)
{
	switch (vn->v_type) {
	case VBLK:
	case VCHR: {
		const char *name;

		if (nflg || ((name = devname(fst->rdev, vn->v_type == VCHR ? 
		    S_IFCHR : S_IFBLK)) == NULL))
			(void)printf("  %s,%-2llu",
			    kdriver_search(vn->v_type, major(fst->rdev)),
			    (unsigned long long)minor(fst->rdev));
		else
			(void)printf(" %6s", name);
		break;
	}
	default:
		(void)printf(" %6lld", (long long)fst->size);
	}
}

void
oprint(struct file *fp, const char *str)
{
	if (Oflg)
		(void)printf(" %6lld", (long long)(fp ? fp->f_offset : 0));
	fputs(str, stdout);
}

static void
vtrans(struct file *fp, struct vnode *vp, int i, int flag, long addr)
{
	struct vnode vn;
	char mode[15], rw[3];
	const char *badtype, *filename;
	struct filestat fst;

	if (!checkfs(vp, &vn, &fst, &badtype, &filename))
		return;

	if (Aflg)
		(void)printf("%*lx ", 2*(int)(sizeof(void*)), addr);
	PREFIX(i);
	if (badtype == dead) {
		char buf[1024];
		(void)snprintb(buf, sizeof(buf), VNODE_FLAGBITS,
		    vn.v_iflag | vn.v_vflag | vn.v_uflag);
		(void)printf("  flags %s\n", buf);
		return;
	} else if (badtype) {
		(void)printf("  -         -  %10s    -\n", badtype);
		return;
	}
	if (nflg)
		(void)printf("  %3llu,%-2llu",
		    (unsigned long long)major(fst.fsid),
		    (unsigned long long)minor(fst.fsid));
	else
		(void)printf("  %-8s", getmnton(vn.v_mount));
	if (nflg)
		(void)snprintf(mode, sizeof mode, "%6o", fst.mode);
	else
		strmode(fst.mode, mode);
	(void)printf("  %8"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
	if (Oflg) {
		oprint(fp, "");
	} else {
		vprint(&vn, &fst);
	}
	rw[0] = '\0';
	if (flag & FREAD)
		(void)strlcat(rw, "r", sizeof(rw));
	if (flag & FWRITE)
		(void)strlcat(rw, "w", sizeof(rw));
	(void)printf(" %-2s", rw);
	if (filename && !fsflg)
		(void)printf("  %s", filename);
	(void)putchar('\n');
}

static int
ufs_filestat(struct vnode *vp, struct filestat *fsp)
{
	struct inode inode;
	struct ufsmount ufsmount;
	union dinode {
		struct ufs1_dinode dp1;
		struct ufs2_dinode dp2;
	} dip;

	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
		return 0;
	}

	if (!KVM_READ(inode.i_ump, &ufsmount, sizeof (struct ufsmount))) {
		dprintf("can't read ufsmount at %p for pid %d", inode.i_ump, Pid);
		return 0;
	}

	switch (ufsmount.um_fstype) {
	case UFS1:
		if (!KVM_READ(inode.i_din.ffs1_din, &dip,
		    sizeof(struct ufs1_dinode))) {
			dprintf("can't read dinode at %p for pid %d",
				inode.i_din.ffs1_din, Pid);
			return 0;
		}
		fsp->rdev = dip.dp1.di_rdev;
		break;
	case UFS2:
		if (!KVM_READ(inode.i_din.ffs2_din, &dip,
		    sizeof(struct ufs2_dinode))) {
			dprintf("can't read dinode at %p for pid %d",
			    inode.i_din.ffs2_din, Pid);
			return 0;
		}
		fsp->rdev = dip.dp2.di_rdev;
		break;
	default:
		dprintf("unknown ufs type %ld for pid %d",
			ufsmount.um_fstype, Pid);
		break;
	}
	fsp->fsid = inode.i_dev & 0xffff;
	fsp->fileid = inode.i_number;
	fsp->mode = (mode_t)inode.i_mode;
	fsp->size = inode.i_size;

	return 1;
}

static int
ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
{
	struct inode inode;
	struct ext2fs_dinode dinode;

	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
		return 0;
	}
	fsp->fsid = inode.i_dev & 0xffff;
	fsp->fileid = inode.i_number;

	if (!KVM_READ(inode.i_din.e2fs_din, &dinode, sizeof dinode)) {
		dprintf("can't read ext2fs_dinode at %p for pid %d",
			inode.i_din.e2fs_din, Pid);
		return 0;
	}
	fsp->mode = dinode.e2di_mode;
	fsp->size = dinode.e2di_size;
	fsp->rdev = dinode.e2di_rdev;

	return 1;
}

static int
nfs_filestat(struct vnode *vp, struct filestat *fsp)
{
	struct nfsnode nfsnode;
	struct vattr va;

	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
		    Pid);
		return 0;
	}
	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
		dprintf("can't read vnode attributes at %p for pid %d",
		    nfsnode.n_vattr, Pid);
		return 0;
	}
	fsp->fsid = va.va_fsid;
	fsp->fileid = va.va_fileid;
	fsp->size = nfsnode.n_size;
	fsp->rdev = va.va_rdev;
	fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);

	return 1;
}

static int
msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
{
	struct denode de;
	struct msdosfsmount mp;

	if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
		dprintf("can't read denode at %p for pid %d", VTONFS(vp),
		    Pid);
		return 0;
	}
	if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
		dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
		    Pid);
		return 0;
	}

	fsp->fsid = de.de_dev & 0xffff;
	fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
	fsp->size = de.de_FileSize;
	fsp->rdev = 0;	/* msdosfs doesn't support device files */
	fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
	return 1;
}

static const char *
layer_filestat(struct vnode *vp, struct filestat *fsp)
{
	struct layer_node layer_node;
	struct mount mount;
	struct vnode vn;
	const char *badtype;

	if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
		dprintf("can't read layer_node at %p for pid %d",
		    VTOLAYER(vp), Pid);
		return "error";
	}
	if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
		dprintf("can't read mount struct at %p for pid %d",
		    vp->v_mount, Pid);
		return "error";
	}
	vp = layer_node.layer_lowervp;
	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
		dprintf("can't read vnode at %p for pid %d", vp, Pid);
		return "error";
	}
	if ((badtype = vfilestat(&vn, fsp)) == NULL)
		fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
	return badtype;
}

static char *
getmnton(struct mount *m)
{
	static struct mount mount;
	static struct mtab {
		struct mtab *next;
		struct mount *m;
		char mntonname[MNAMELEN];
	} *mhead = NULL;
	struct mtab *mt;

	for (mt = mhead; mt != NULL; mt = mt->next)
		if (m == mt->m)
			return mt->mntonname;
	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
		warnx("can't read mount table at %p", m);
		return NULL;
	}
	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
	}
	mt->m = m;
	(void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
	    MNAMELEN);
	mt->next = mhead;
	mhead = mt;
	return mt->mntonname;
}

static const char *
inet_addrstr(char *buf, size_t len, const struct in_addr *a, uint16_t p, bool isdg)
{
	char addr[256], serv[256];
	struct sockaddr_in sin;
	const int niflags =
		(nflg ? (NI_NUMERICHOST|NI_NUMERICSERV) : 0) |
		(isdg ? NI_DGRAM : 0);


	(void)memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_len = sizeof(sin);
	sin.sin_addr = *a;
	sin.sin_port = htons(p);

	serv[0] = '\0';

	if (getnameinfo((struct sockaddr *)&sin, sin.sin_len,
	    addr, sizeof(addr), serv, sizeof(serv), niflags)) {
		if (inet_ntop(AF_INET, a, addr, sizeof(addr)) == NULL)
			strlcpy(addr, "invalid", sizeof(addr));
	}

	if (serv[0] == '\0')
		snprintf(serv, sizeof(serv), "%u", p);

	if (a->s_addr == INADDR_ANY) {
		if (p == 0)
			buf[0] = '\0';
		else
			snprintf(buf, len, "*:%s", serv);
		return buf;
	}

	snprintf(buf, len, "%s:%s", addr, serv);
	return buf;
}

#ifdef INET6
static const char *
inet6_addrstr(char *buf, size_t len, const struct in6_addr *a, uint16_t p, bool isdg)
{
	char addr[256], serv[256];
	struct sockaddr_in6 sin6;
	const int niflags =
		(nflg ? (NI_NUMERICHOST|NI_NUMERICSERV) : 0) |
		(isdg ? NI_DGRAM : 0);

	(void)memset(&sin6, 0, sizeof(sin6));
	sin6.sin6_family = AF_INET6;
	sin6.sin6_len = sizeof(sin6);
	sin6.sin6_addr = *a;
	sin6.sin6_port = htons(p);

	inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL);
	serv[0] = '\0';

	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
	    addr, sizeof(addr), serv, sizeof(serv), niflags)) {
		if (inet_ntop(AF_INET6, a, addr, sizeof(addr)) == NULL)
			strlcpy(addr, "invalid", sizeof(addr));
	}

	if (serv[0] == '\0')
		snprintf(serv, sizeof(serv), "%u", p);

	if (IN6_IS_ADDR_UNSPECIFIED(a)) {
		if (p == 0)
			buf[0] = '\0';
		else
			snprintf(buf, len, "*:%s", serv);
		return buf;
	}

	if (strchr(addr, ':') == NULL)
		snprintf(buf, len, "%s:%s", addr, serv);
	else
		snprintf(buf, len, "[%s]:%s", addr, serv);

	return buf;
}
#endif

static const char *
at_addrstr(char *buf, size_t len, const struct sockaddr_at *sat)
{
	const struct netrange *nr = &sat->sat_range.r_netrange;
	const struct at_addr *at = &sat->sat_addr;
	char addr[64], phase[64], range[64];

	if (sat->sat_port || at->s_net || at->s_node) {
		if (at->s_net || at->s_node)
			snprintf(addr, sizeof(addr), "%u.%u:%u",
			    ntohs(at->s_net), at->s_node, sat->sat_port);
		else
			snprintf(addr, sizeof(addr), "*:%u", sat->sat_port);
	} else
		addr[0] = '\0';

	if (nr->nr_phase)
		snprintf(phase, sizeof(phase), " phase %u", nr->nr_phase);
	else
		phase[0] = '\0';

	if (nr->nr_firstnet || nr->nr_lastnet)
		snprintf(range, sizeof(range), " range [%u-%u]",
		    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet));
	else
		range[0] = '\0';

	snprintf(buf, len, "%s%s%s", addr, phase, range);
	return buf;
}

static void
socktrans(struct file *f, struct socket *sock, int i)
{
	static const char *stypename[] = {
		"unused",	/* 0 */
		"stream", 	/* 1 */
		"dgram",	/* 2 */
		"raw",		/* 3 */
		"rdm",		/* 4 */
		"seqpak"	/* 5 */
	};
#define	STYPEMAX 5
	struct socket	so;
	struct protosw	proto;
	struct domain	dom;
	struct in4pcb	in4pcb;
	struct in6pcb	in6pcb;
	struct unpcb	unpcb;
	struct ddpcb	ddpcb;
	int len;
	char dname[32];
	char lbuf[512], fbuf[512], pbuf[24];
	bool isdgram;

	pbuf[0] = '\0';
	/* fill in socket */
	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
		dprintf("can't read sock at %p", sock);
		goto bad;
	}

	/* fill in protosw entry */
	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
		dprintf("can't read protosw at %p", so.so_proto);
		goto bad;
	}

	/* fill in domain */
	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
		dprintf("can't read domain at %p", proto.pr_domain);
		goto bad;
	}

	if (checkfile && dom.dom_family != AF_LOCAL)
		return;

	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
	    sizeof(dname) - 1)) != sizeof(dname) -1) {
		dprintf("can't read domain name at %p", dom.dom_name);
		dname[0] = '\0';
	}
	else
		dname[len] = '\0';

	/* 
	 * protocol specific formatting
	 *
	 * Try to find interesting things to print.  For TCP, the interesting
	 * thing is the address of the tcpcb, for UDP and others, just the
	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
	 * pcb and the address of the connected pcb (if connected).  Otherwise
	 * just print the protocol number and address of the socket itself.
	 * The idea is not to duplicate netstat, but to make available enough
	 * information for further analysis.
	 */
	fbuf[0] = '\0';
	lbuf[0] = '\0';
	isdgram = false;
	switch(dom.dom_family) {
	case AF_INET:
		getinetproto(pbuf, sizeof(pbuf), proto.pr_protocol);
		switch (proto.pr_protocol) {
		case IPPROTO_UDP:
			isdgram = true;
			/* FALLTHROUGH */
		case IPPROTO_TCP:
			if (so.so_pcb == NULL)
				break;
			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in4pcb,
			    sizeof(in4pcb)) != sizeof(in4pcb)) {
				dprintf("can't read in4pcb at %p", so.so_pcb);
				goto bad;
			}
			struct inpcb *inp = (struct inpcb *)&in4pcb;
			inet_addrstr(lbuf, sizeof(lbuf), &in4p_laddr(inp),
			    ntohs(inp->inp_lport), isdgram);
			inet_addrstr(fbuf, sizeof(fbuf), &in4p_faddr(inp),
			    ntohs(inp->inp_fport), isdgram);
			break;
		default:
			break;
		}
		break;
#ifdef INET6
	case AF_INET6:
		getinetproto(pbuf, sizeof(pbuf), proto.pr_protocol);
		switch (proto.pr_protocol) {
		case IPPROTO_UDP:
			isdgram = true;
			/* FALLTHROUGH */
		case IPPROTO_TCP:
			if (so.so_pcb == NULL)
				break;
			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
			    sizeof(in6pcb)) != sizeof(in6pcb)) {
				dprintf("can't read in6pcb at %p", so.so_pcb);
				goto bad;
			}
			struct inpcb *inp = (struct inpcb *)&in6pcb;
			inet6_addrstr(lbuf, sizeof(lbuf), &in6p_laddr(inp),
			    ntohs(inp->inp_lport), isdgram);
			inet6_addrstr(fbuf, sizeof(fbuf), &in6p_faddr(inp),
			    ntohs(inp->inp_fport), isdgram);
			break;
		default:
			break;
		}
		break;
#endif
	case AF_LOCAL:
		/* print address of pcb and connected pcb */
		if (so.so_pcb) {
			char shoconn[4], *cp;
			void *pcb[2];
			size_t p = 0;

			pcb[0] = so.so_pcb;

			cp = shoconn;
			if (!(so.so_state & SS_CANTRCVMORE))
				*cp++ = '<';
			*cp++ = '-';
			if (!(so.so_state & SS_CANTSENDMORE))
				*cp++ = '>';
			*cp = '\0';
again:
			if (kvm_read(kd, (u_long)pcb[p], (char *)&unpcb,
			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
				dprintf("can't read unpcb at %p", so.so_pcb);
				goto bad;
			}
			if (checkfile) {
				struct vnode vn;
				struct filestat fst;
				const char *badtype, *filename;
				if (unpcb.unp_vnode == NULL)
					return;
				if (!checkfs(unpcb.unp_vnode, &vn, &fst,
				    &badtype, &filename))
					return;
			}

			if (unpcb.unp_addr) {
				struct sockaddr_un *sun = 
					malloc(unpcb.unp_addrlen);
				if (sun == NULL)
				    err(1, "malloc(%zu)",
					unpcb.unp_addrlen);
				if (kvm_read(kd, (u_long)unpcb.unp_addr,
				    sun, unpcb.unp_addrlen) !=
				    (ssize_t)unpcb.unp_addrlen) {
					dprintf("can't read sun at %p",
					    unpcb.unp_addr);
					free(sun);
				} else {
					snprintf(fbuf, sizeof(fbuf), " %s %s %s",
					    shoconn, sun->sun_path,
					    p == 0 ? "[creat]" : "[using]");
					free(sun);
					break;
				}
			}
			if (unpcb.unp_conn) {
				if (p == 0) {
					pcb[++p] = unpcb.unp_conn;
					goto again;
				} else
					snprintf(fbuf, sizeof(fbuf),
					    " %p %s %p", pcb[0], shoconn,
					    pcb[1]);
			}
		}
		break;
	case AF_APPLETALK:
		getatproto(pbuf, sizeof(pbuf), proto.pr_protocol);
		if (so.so_pcb) {
			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&ddpcb,
			    sizeof(ddpcb)) != sizeof(ddpcb)){
				dprintf("can't read ddpcb at %p", so.so_pcb);
				goto bad;
			}
			at_addrstr(fbuf, sizeof(fbuf), &ddpcb.ddp_fsat);
			at_addrstr(lbuf, sizeof(lbuf), &ddpcb.ddp_lsat);
		}
		break;
	default:
		/* print protocol number and socket address */
		snprintf(fbuf, sizeof(fbuf), " %d %jx", proto.pr_protocol,
		    (uintmax_t)(uintptr_t)sock);
		break;
	}
	PREFIX(i);
	if ((u_short)so.so_type > STYPEMAX)
		(void)printf("* %s ?%d", dname, so.so_type);
	else
		(void)printf("* %s %s", dname, stypename[so.so_type]);

	if (pbuf[0])
		printf("%s", pbuf);
	if (fbuf[0] || lbuf[0])
		printf(" %s%s%s", fbuf, (fbuf[0] && lbuf[0]) ? " <-> " : "",
		    lbuf);
	else if (so.so_pcb)
		printf(" %jx", (uintmax_t)(uintptr_t)so.so_pcb);
	oprint(f, "\n");
	return;
bad:
	(void)printf("* error\n");
}

static void
ptrans(struct file *fp, struct pipe *cpipe, int i)
{
	struct pipe cp;

	PREFIX(i);
	
	/* fill in pipe */
	if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
		dprintf("can't read pipe at %p", cpipe);
		goto bad;
	}

	/* pipe descriptor is either read or write, never both */
	(void)printf("* pipe %p %s %p %s%s%s", cpipe,
		(fp->f_flag & FWRITE) ? "->" : "<-",
		cp.pipe_peer,
		(fp->f_flag & FWRITE) ? "w" : "r",
		(fp->f_flag & FNONBLOCK) ? "n" : "",
		(cp.pipe_state & PIPE_ASYNC) ? "a" : "");
	oprint(fp, "\n");
	return;
bad:
	(void)printf("* error\n");
}

static void
misctrans(struct file *file, int i)
{

	PREFIX(i);
	pmisc(file, dtypes[file->f_type]);
}

/*
 * getinetproto --
 *	print name of protocol number
 */
static void
getinetproto(char *buf, size_t len, int number)
{
	const char *cp;

	switch (number) {
	case IPPROTO_IP:
		cp = "ip"; break;
	case IPPROTO_ICMP:
		cp ="icmp"; break;
	case IPPROTO_GGP:
		cp ="ggp"; break;
	case IPPROTO_TCP:
		cp ="tcp"; break;
	case IPPROTO_EGP:
		cp ="egp"; break;
	case IPPROTO_PUP:
		cp ="pup"; break;
	case IPPROTO_UDP:
		cp ="udp"; break;
	case IPPROTO_IDP:
		cp ="idp"; break;
	case IPPROTO_RAW:
		cp ="raw"; break;
	case IPPROTO_ICMPV6:
		cp ="icmp6"; break;
	default:
		(void)snprintf(buf, len, " %d", number);
		return;
	}
	(void)snprintf(buf, len, " %s", cp);
}

/*
 * getatproto --
 *	print name of protocol number
 */
static void
getatproto(char *buf, size_t len, int number)
{
	const char *cp;

	switch (number) {
	case ATPROTO_DDP:
		cp = "ddp"; break;
	case ATPROTO_AARP:
		cp ="aarp"; break;
	default:
		(void)snprintf(buf, len, " %d", number);
		return;
	}
	(void)snprintf(buf, len, " %s", cp);
}

static int
getfname(const char *filename)
{
	struct stat statbuf;
	DEVS *cur;

	if (stat(filename, &statbuf)) {
		warn("stat(%s)", filename);
		return 0;
	}
	if ((cur = malloc(sizeof(*cur))) == NULL) {
		err(1, "malloc(%zu)", sizeof(*cur));
	}
	cur->next = devs;
	devs = cur;

	cur->ino = statbuf.st_ino;
	cur->fsid = statbuf.st_dev & 0xffff;
	cur->name = filename;
	return 1;
}

mode_t
getftype(enum vtype v_type)
{
	mode_t ftype;

	switch (v_type) {
	case VREG:
		ftype = S_IFREG;
		break;
	case VDIR:
		ftype = S_IFDIR;
		break;
	case VBLK:
		ftype = S_IFBLK;
		break;
	case VCHR:
		ftype = S_IFCHR;
		break;
	case VLNK:
		ftype = S_IFLNK;
		break;
	case VSOCK:
		ftype = S_IFSOCK;
		break;
	case VFIFO:
		ftype = S_IFIFO;
		break;
	default:
		ftype = 0;
		break;
	};

	return ftype;
}

static void
usage(void)
{
	(void)fprintf(stderr, "Usage: %s [-Afnv] [-M core] [-N system] "
	    "[-p pid] [-u user] [file ...]\n", getprogname());
	exit(1);
}