fstat.c revision 1.35 1 /* $NetBSD: fstat.c,v 1.35 1999/05/02 22:50:19 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95";
45 #else
46 __RCSID("$NetBSD: fstat.c,v 1.35 1999/05/02 22:50:19 thorpej Exp $");
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/proc.h>
53 #include <sys/user.h>
54 #include <sys/stat.h>
55 #include <sys/vnode.h>
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 #include <sys/domain.h>
59 #include <sys/protosw.h>
60 #include <sys/unpcb.h>
61 #include <sys/sysctl.h>
62 #include <sys/filedesc.h>
63 #define _KERNEL
64 #include <sys/file.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/inode.h>
67 #undef _KERNEL
68 #define NFS
69 #include <sys/mount.h>
70 #include <nfs/nfsproto.h>
71 #include <nfs/rpcv2.h>
72 #include <nfs/nfs.h>
73 #include <nfs/nfsnode.h>
74 #undef NFS
75
76 #include <net/route.h>
77 #include <netinet/in.h>
78 #include <netinet/in_systm.h>
79 #include <netinet/ip.h>
80 #include <netinet/in_pcb.h>
81
82 #include <arpa/inet.h>
83
84 #include <ctype.h>
85 #include <errno.h>
86 #include <kvm.h>
87 #include <limits.h>
88 #include <nlist.h>
89 #include <paths.h>
90 #include <pwd.h>
91 #include <stdio.h>
92 #include <stdlib.h>
93 #include <string.h>
94 #include <unistd.h>
95 #include <err.h>
96
97 #include "fstat.h"
98
99 #define TEXT -1
100 #define CDIR -2
101 #define RDIR -3
102 #define TRACE -4
103
104 typedef struct devs {
105 struct devs *next;
106 long fsid;
107 ino_t ino;
108 char *name;
109 } DEVS;
110 DEVS *devs;
111
112 int fsflg, /* show files on same filesystem as file(s) argument */
113 pflg, /* show files open by a particular pid */
114 uflg; /* show files open by a particular (effective) user */
115 int checkfile; /* true if restricting to particular files or filesystems */
116 int nflg; /* (numerical) display f.s. and rdev as dev_t */
117 int vflg; /* display errors in locating kernel data objects etc... */
118
119 struct file **ofiles; /* buffer of pointers to file structures */
120 int maxfiles;
121 #define ALLOC_OFILES(d) \
122 if ((d) > maxfiles) { \
123 free(ofiles); \
124 ofiles = malloc((d) * sizeof(struct file *)); \
125 if (ofiles == NULL) { \
126 err(1, "malloc(%u)", (d) * \
127 (unsigned int)sizeof(struct file *)); \
128 } \
129 maxfiles = (d); \
130 }
131
132 kvm_t *kd;
133
134 void dofiles __P((struct kinfo_proc *));
135 int ext2fs_filestat __P((struct vnode *, struct filestat *));
136 int getfname __P((char *));
137 void getinetproto __P((int));
138 char *getmnton __P((struct mount *));
139 int main __P((int, char **));
140 int nfs_filestat __P((struct vnode *, struct filestat *));
141 void socktrans __P((struct socket *, int));
142 int ufs_filestat __P((struct vnode *, struct filestat *));
143 void usage __P((void));
144 void vtrans __P((struct vnode *, int, int));
145
146 int
147 main(argc, argv)
148 int argc;
149 char **argv;
150 {
151 struct passwd *passwd;
152 struct kinfo_proc *p, *plast;
153 int arg, ch, what;
154 char *memf, *nlistf;
155 char buf[_POSIX2_LINE_MAX];
156 int cnt;
157 gid_t egid = getegid();
158
159 (void)setegid(getgid());
160 arg = 0;
161 what = KERN_PROC_ALL;
162 nlistf = memf = NULL;
163 while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
164 switch((char)ch) {
165 case 'f':
166 fsflg = 1;
167 break;
168 case 'M':
169 memf = optarg;
170 break;
171 case 'N':
172 nlistf = optarg;
173 break;
174 case 'n':
175 nflg = 1;
176 break;
177 case 'p':
178 if (pflg++)
179 usage();
180 if (!isdigit(*optarg)) {
181 warnx("-p requires a process id");
182 usage();
183 }
184 what = KERN_PROC_PID;
185 arg = atoi(optarg);
186 break;
187 case 'u':
188 if (uflg++)
189 usage();
190 if (!(passwd = getpwnam(optarg))) {
191 errx(1, "%s: unknown uid", optarg);
192 }
193 what = KERN_PROC_UID;
194 arg = passwd->pw_uid;
195 break;
196 case 'v':
197 vflg = 1;
198 break;
199 case '?':
200 default:
201 usage();
202 }
203
204 if (*(argv += optind)) {
205 for (; *argv; ++argv) {
206 if (getfname(*argv))
207 checkfile = 1;
208 }
209 if (!checkfile) /* file(s) specified, but none accessable */
210 exit(1);
211 }
212
213 ALLOC_OFILES(256); /* reserve space for file pointers */
214
215 if (fsflg && !checkfile) {
216 /* -f with no files means use wd */
217 if (getfname(".") == 0)
218 exit(1);
219 checkfile = 1;
220 }
221
222 /*
223 * Discard setgid privileges. If not the running kernel, we toss
224 * them away totally so that bad guys can't print interesting stuff
225 * from kernel memory, otherwise switch back to kmem for the
226 * duration of the kvm_openfiles() call.
227 */
228 if (nlistf != NULL || memf != NULL)
229 (void)setgid(getgid());
230 else
231 (void)setegid(egid);
232
233 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
234 errx(1, "%s", buf);
235
236 /* get rid of it now anyway */
237 if (nlistf == NULL && memf == NULL)
238 (void)setgid(getgid());
239
240 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
241 errx(1, "%s", kvm_geterr(kd));
242 }
243 if (nflg)
244 printf("%s",
245 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
246 else
247 printf("%s",
248 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
249 if (checkfile && fsflg == 0)
250 printf(" NAME\n");
251 else
252 putchar('\n');
253
254 for (plast = &p[cnt]; p < plast; ++p) {
255 if (p->kp_proc.p_stat == SZOMB)
256 continue;
257 dofiles(p);
258 }
259 exit(0);
260 }
261
262 const char *Uname, *Comm;
263 pid_t Pid;
264
265 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
266 switch(i) { \
267 case TEXT: \
268 printf(" text"); \
269 break; \
270 case CDIR: \
271 printf(" wd"); \
272 break; \
273 case RDIR: \
274 printf(" root"); \
275 break; \
276 case TRACE: \
277 printf(" tr"); \
278 break; \
279 default: \
280 printf(" %4d", i); \
281 break; \
282 }
283
284 /*
285 * print open files attributed to this process
286 */
287 void
288 dofiles(kp)
289 struct kinfo_proc *kp;
290 {
291 int i;
292 struct file file;
293 struct filedesc0 filed0;
294 #define filed filed0.fd_fd
295 struct cwdinfo cwdi;
296 struct proc *p = &kp->kp_proc;
297 struct eproc *ep = &kp->kp_eproc;
298
299 Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
300 Pid = p->p_pid;
301 Comm = p->p_comm;
302
303 if (p->p_fd == NULL || p->p_cwdi == NULL)
304 return;
305 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
306 warnx("can't read filedesc at %p for pid %d", p->p_fd, Pid);
307 return;
308 }
309 if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
310 warnx("can't read cwdinfo at %p for pid %d", p->p_cwdi, Pid);
311 return;
312 }
313 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
314 filed.fd_freefile > filed.fd_lastfile + 1) {
315 dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid);
316 return;
317 }
318 /*
319 * root directory vnode, if one
320 */
321 if (cwdi.cwdi_rdir)
322 vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
323 /*
324 * current working directory vnode
325 */
326 vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
327 /*
328 * ktrace vnode, if one
329 */
330 if (p->p_tracep)
331 vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
332 /*
333 * open files
334 */
335 #define FPSIZE (sizeof (struct file *))
336 ALLOC_OFILES(filed.fd_lastfile+1);
337 if (filed.fd_nfiles > NDFILE) {
338 if (!KVM_READ(filed.fd_ofiles, ofiles,
339 (filed.fd_lastfile+1) * FPSIZE)) {
340 dprintf("can't read file structures at %p for pid %d",
341 filed.fd_ofiles, Pid);
342 return;
343 }
344 } else
345 memmove(ofiles, filed0.fd_dfiles,
346 (filed.fd_lastfile+1) * FPSIZE);
347 for (i = 0; i <= filed.fd_lastfile; i++) {
348 if (ofiles[i] == NULL)
349 continue;
350 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
351 dprintf("can't read file %d at %p for pid %d",
352 i, ofiles[i], Pid);
353 continue;
354 }
355 if (file.f_type == DTYPE_VNODE)
356 vtrans((struct vnode *)file.f_data, i, file.f_flag);
357 else if (file.f_type == DTYPE_SOCKET) {
358 if (checkfile == 0)
359 socktrans((struct socket *)file.f_data, i);
360 }
361 else {
362 dprintf("unknown file type %d for file %d of pid %d",
363 file.f_type, i, Pid);
364 }
365 }
366 }
367
368 void
369 vtrans(vp, i, flag)
370 struct vnode *vp;
371 int i;
372 int flag;
373 {
374 struct vnode vn;
375 struct filestat fst;
376 char mode[15], rw[3];
377 char *badtype = NULL, *filename;
378
379 filename = badtype = NULL;
380 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
381 dprintf("can't read vnode at %p for pid %d", vp, Pid);
382 return;
383 }
384 if (vn.v_type == VNON || vn.v_tag == VT_NON)
385 badtype = "none";
386 else if (vn.v_type == VBAD)
387 badtype = "bad";
388 else
389 switch (vn.v_tag) {
390 case VT_UFS:
391 if (!ufs_filestat(&vn, &fst))
392 badtype = "error";
393 break;
394 case VT_MFS:
395 if (!ufs_filestat(&vn, &fst))
396 badtype = "error";
397 break;
398 case VT_NFS:
399 if (!nfs_filestat(&vn, &fst))
400 badtype = "error";
401 break;
402 case VT_EXT2FS:
403 if (!ext2fs_filestat(&vn, &fst))
404 badtype = "error";
405 break;
406 case VT_ISOFS:
407 if (!isofs_filestat(&vn, &fst))
408 badtype = "error";
409 break;
410 default: {
411 static char unknown[10];
412 (void)snprintf(badtype = unknown, sizeof unknown,
413 "?(%x)", vn.v_tag);
414 break;;
415 }
416 }
417 if (checkfile) {
418 int fsmatch = 0;
419 DEVS *d;
420
421 if (badtype)
422 return;
423 for (d = devs; d != NULL; d = d->next)
424 if (d->fsid == fst.fsid) {
425 fsmatch = 1;
426 if (d->ino == fst.fileid) {
427 filename = d->name;
428 break;
429 }
430 }
431 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
432 return;
433 }
434 PREFIX(i);
435 if (badtype) {
436 (void)printf(" - - %10s -\n", badtype);
437 return;
438 }
439 if (nflg)
440 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
441 else
442 (void)printf(" %-8s", getmnton(vn.v_mount));
443 if (nflg)
444 (void)snprintf(mode, sizeof mode, "%o", fst.mode);
445 else
446 strmode(fst.mode, mode);
447 (void)printf(" %6ld %10s", (long)fst.fileid, mode);
448 switch (vn.v_type) {
449 case VBLK:
450 case VCHR: {
451 char *name;
452
453 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
454 S_IFCHR : S_IFBLK)) == NULL))
455 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
456 else
457 printf(" %6s", name);
458 break;
459 }
460 default:
461 printf(" %6qd", (long long)fst.size);
462 }
463 rw[0] = '\0';
464 if (flag & FREAD)
465 strcat(rw, "r");
466 if (flag & FWRITE)
467 strcat(rw, "w");
468 printf(" %-2s", rw);
469 if (filename && !fsflg)
470 printf(" %s", filename);
471 putchar('\n');
472 }
473
474 int
475 ufs_filestat(vp, fsp)
476 struct vnode *vp;
477 struct filestat *fsp;
478 {
479 struct inode inode;
480
481 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
482 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
483 return 0;
484 }
485 fsp->fsid = inode.i_dev & 0xffff;
486 fsp->fileid = (long)inode.i_number;
487 fsp->mode = (mode_t)inode.i_ffs_mode;
488 fsp->size = inode.i_ffs_size;
489 fsp->rdev = inode.i_ffs_rdev;
490
491 return 1;
492 }
493
494 int
495 ext2fs_filestat(vp, fsp)
496 struct vnode *vp;
497 struct filestat *fsp;
498 {
499 struct inode inode;
500
501 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
502 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
503 return 0;
504 }
505 fsp->fsid = inode.i_dev & 0xffff;
506 fsp->fileid = (long)inode.i_number;
507 fsp->mode = (mode_t)inode.i_e2fs_mode;
508 fsp->size = inode.i_e2fs_size;
509 fsp->rdev = 0; /* XXX */
510 return 1;
511 }
512
513 int
514 nfs_filestat(vp, fsp)
515 struct vnode *vp;
516 struct filestat *fsp;
517 {
518 struct nfsnode nfsnode;
519 struct vattr va;
520 mode_t mode;
521
522 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
523 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
524 Pid);
525 return 0;
526 }
527 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
528 dprintf("can't read vnode attributes at %p for pid %d",
529 nfsnode.n_vattr, Pid);
530 return 0;
531 }
532 fsp->fsid = va.va_fsid;
533 fsp->fileid = va.va_fileid;
534 fsp->size = nfsnode.n_size;
535 fsp->rdev = va.va_rdev;
536 mode = (mode_t)va.va_mode;
537 switch (vp->v_type) {
538 case VREG:
539 mode |= S_IFREG;
540 break;
541 case VDIR:
542 mode |= S_IFDIR;
543 break;
544 case VBLK:
545 mode |= S_IFBLK;
546 break;
547 case VCHR:
548 mode |= S_IFCHR;
549 break;
550 case VLNK:
551 mode |= S_IFLNK;
552 break;
553 case VSOCK:
554 mode |= S_IFSOCK;
555 break;
556 case VFIFO:
557 mode |= S_IFIFO;
558 break;
559 default:
560 break;
561 };
562 fsp->mode = mode;
563
564 return 1;
565 }
566
567
568 char *
569 getmnton(m)
570 struct mount *m;
571 {
572 static struct mount mount;
573 static struct mtab {
574 struct mtab *next;
575 struct mount *m;
576 char mntonname[MNAMELEN];
577 } *mhead = NULL;
578 struct mtab *mt;
579
580 for (mt = mhead; mt != NULL; mt = mt->next)
581 if (m == mt->m)
582 return (mt->mntonname);
583 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
584 warnx("can't read mount table at %p", m);
585 return (NULL);
586 }
587 if ((mt = malloc(sizeof (struct mtab))) == NULL) {
588 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
589 }
590 mt->m = m;
591 memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
592 mt->next = mhead;
593 mhead = mt;
594 return (mt->mntonname);
595 }
596
597 void
598 socktrans(sock, i)
599 struct socket *sock;
600 int i;
601 {
602 static char *stypename[] = {
603 "unused", /* 0 */
604 "stream", /* 1 */
605 "dgram", /* 2 */
606 "raw", /* 3 */
607 "rdm", /* 4 */
608 "seqpak" /* 5 */
609 };
610 #define STYPEMAX 5
611 struct socket so;
612 struct protosw proto;
613 struct domain dom;
614 struct inpcb inpcb;
615 struct unpcb unpcb;
616 int len;
617 char dname[32];
618
619 PREFIX(i);
620
621 /* fill in socket */
622 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
623 dprintf("can't read sock at %p", sock);
624 goto bad;
625 }
626
627 /* fill in protosw entry */
628 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
629 dprintf("can't read protosw at %p", so.so_proto);
630 goto bad;
631 }
632
633 /* fill in domain */
634 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
635 dprintf("can't read domain at %p", proto.pr_domain);
636 goto bad;
637 }
638
639 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
640 sizeof(dname) - 1)) != sizeof(dname) -1) {
641 dprintf("can't read domain name at %p", dom.dom_name);
642 dname[0] = '\0';
643 }
644 else
645 dname[len] = '\0';
646
647 if ((u_short)so.so_type > STYPEMAX)
648 printf("* %s ?%d", dname, so.so_type);
649 else
650 printf("* %s %s", dname, stypename[so.so_type]);
651
652 /*
653 * protocol specific formatting
654 *
655 * Try to find interesting things to print. For TCP, the interesting
656 * thing is the address of the tcpcb, for UDP and others, just the
657 * inpcb (socket pcb). For UNIX domain, its the address of the socket
658 * pcb and the address of the connected pcb (if connected). Otherwise
659 * just print the protocol number and address of the socket itself.
660 * The idea is not to duplicate netstat, but to make available enough
661 * information for further analysis.
662 */
663 switch(dom.dom_family) {
664 case AF_INET:
665 getinetproto(proto.pr_protocol);
666 if (proto.pr_protocol == IPPROTO_TCP) {
667 if (so.so_pcb == NULL)
668 break;
669 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
670 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
671 dprintf("can't read inpcb at %p", so.so_pcb);
672 goto bad;
673 }
674 printf(" %lx", (long)inpcb.inp_ppcb);
675 printf(" %s:%d",
676 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
677 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
678 if (inpcb.inp_fport) {
679 printf(" <-> ");
680 printf("%s:%d",
681 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
682 inet_ntoa(inpcb.inp_faddr),
683 ntohs(inpcb.inp_fport));
684 }
685 } else if (proto.pr_protocol == IPPROTO_UDP) {
686 if (so.so_pcb == NULL)
687 break;
688 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
689 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
690 dprintf("can't read inpcb at %p", so.so_pcb);
691 goto bad;
692 }
693 printf(" %lx", (long)so.so_pcb);
694 printf(" %s:%d",
695 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
696 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
697 if (inpcb.inp_fport)
698 printf(" <-> %s:%d",
699 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
700 inet_ntoa(inpcb.inp_faddr),
701 ntohs(inpcb.inp_fport));
702 } else if (so.so_pcb)
703 printf(" %lx", (long)so.so_pcb);
704 break;
705 case AF_LOCAL:
706 /* print address of pcb and connected pcb */
707 if (so.so_pcb) {
708 printf(" %lx", (long)so.so_pcb);
709 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
710 sizeof(struct unpcb)) != sizeof(struct unpcb)){
711 dprintf("can't read unpcb at %p", so.so_pcb);
712 goto bad;
713 }
714 if (unpcb.unp_conn) {
715 char shoconn[4], *cp;
716
717 cp = shoconn;
718 if (!(so.so_state & SS_CANTRCVMORE))
719 *cp++ = '<';
720 *cp++ = '-';
721 if (!(so.so_state & SS_CANTSENDMORE))
722 *cp++ = '>';
723 *cp = '\0';
724 printf(" %s %lx", shoconn,
725 (long)unpcb.unp_conn);
726 }
727 }
728 break;
729 default:
730 /* print protocol number and socket address */
731 printf(" %d %lx", proto.pr_protocol, (long)sock);
732 }
733 printf("\n");
734 return;
735 bad:
736 printf("* error\n");
737 }
738
739 /*
740 * getinetproto --
741 * print name of protocol number
742 */
743 void
744 getinetproto(number)
745 int number;
746 {
747 char *cp;
748
749 switch (number) {
750 case IPPROTO_IP:
751 cp = "ip"; break;
752 case IPPROTO_ICMP:
753 cp ="icmp"; break;
754 case IPPROTO_GGP:
755 cp ="ggp"; break;
756 case IPPROTO_TCP:
757 cp ="tcp"; break;
758 case IPPROTO_EGP:
759 cp ="egp"; break;
760 case IPPROTO_PUP:
761 cp ="pup"; break;
762 case IPPROTO_UDP:
763 cp ="udp"; break;
764 case IPPROTO_IDP:
765 cp ="idp"; break;
766 case IPPROTO_RAW:
767 cp ="raw"; break;
768 default:
769 printf(" %d", number);
770 return;
771 }
772 printf(" %s", cp);
773 }
774
775 int
776 getfname(filename)
777 char *filename;
778 {
779 struct stat statbuf;
780 DEVS *cur;
781
782 if (stat(filename, &statbuf)) {
783 warn("stat(%s)", filename);
784 return(0);
785 }
786 if ((cur = malloc(sizeof(DEVS))) == NULL) {
787 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
788 }
789 cur->next = devs;
790 devs = cur;
791
792 cur->ino = statbuf.st_ino;
793 cur->fsid = statbuf.st_dev & 0xffff;
794 cur->name = filename;
795 return(1);
796 }
797
798 void
799 usage()
800 {
801 errx(1,
802 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
803 }
804