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