fstat.c revision 1.34 1 /* $NetBSD: fstat.c,v 1.34 1999/02/18 06:09:25 lukem 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.34 1999/02/18 06:09:25 lukem 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 proc *p = &kp->kp_proc;
296 struct eproc *ep = &kp->kp_eproc;
297
298 Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
299 Pid = p->p_pid;
300 Comm = p->p_comm;
301
302 if (p->p_fd == NULL)
303 return;
304 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
305 warnx("can't read filedesc at %p for pid %d", p->p_fd, Pid);
306 return;
307 }
308 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
309 filed.fd_freefile > filed.fd_lastfile + 1) {
310 dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid);
311 return;
312 }
313 /*
314 * root directory vnode, if one
315 */
316 if (filed.fd_rdir)
317 vtrans(filed.fd_rdir, RDIR, FREAD);
318 /*
319 * current working directory vnode
320 */
321 vtrans(filed.fd_cdir, CDIR, FREAD);
322 /*
323 * ktrace vnode, if one
324 */
325 if (p->p_tracep)
326 vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
327 /*
328 * open files
329 */
330 #define FPSIZE (sizeof (struct file *))
331 ALLOC_OFILES(filed.fd_lastfile+1);
332 if (filed.fd_nfiles > NDFILE) {
333 if (!KVM_READ(filed.fd_ofiles, ofiles,
334 (filed.fd_lastfile+1) * FPSIZE)) {
335 dprintf("can't read file structures at %p for pid %d",
336 filed.fd_ofiles, Pid);
337 return;
338 }
339 } else
340 memmove(ofiles, filed0.fd_dfiles,
341 (filed.fd_lastfile+1) * FPSIZE);
342 for (i = 0; i <= filed.fd_lastfile; i++) {
343 if (ofiles[i] == NULL)
344 continue;
345 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
346 dprintf("can't read file %d at %p for pid %d",
347 i, ofiles[i], Pid);
348 continue;
349 }
350 if (file.f_type == DTYPE_VNODE)
351 vtrans((struct vnode *)file.f_data, i, file.f_flag);
352 else if (file.f_type == DTYPE_SOCKET) {
353 if (checkfile == 0)
354 socktrans((struct socket *)file.f_data, i);
355 }
356 else {
357 dprintf("unknown file type %d for file %d of pid %d",
358 file.f_type, i, Pid);
359 }
360 }
361 }
362
363 void
364 vtrans(vp, i, flag)
365 struct vnode *vp;
366 int i;
367 int flag;
368 {
369 struct vnode vn;
370 struct filestat fst;
371 char mode[15], rw[3];
372 char *badtype = NULL, *filename;
373
374 filename = badtype = NULL;
375 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
376 dprintf("can't read vnode at %p for pid %d", vp, Pid);
377 return;
378 }
379 if (vn.v_type == VNON || vn.v_tag == VT_NON)
380 badtype = "none";
381 else if (vn.v_type == VBAD)
382 badtype = "bad";
383 else
384 switch (vn.v_tag) {
385 case VT_UFS:
386 if (!ufs_filestat(&vn, &fst))
387 badtype = "error";
388 break;
389 case VT_MFS:
390 if (!ufs_filestat(&vn, &fst))
391 badtype = "error";
392 break;
393 case VT_NFS:
394 if (!nfs_filestat(&vn, &fst))
395 badtype = "error";
396 break;
397 case VT_EXT2FS:
398 if (!ext2fs_filestat(&vn, &fst))
399 badtype = "error";
400 break;
401 case VT_ISOFS:
402 if (!isofs_filestat(&vn, &fst))
403 badtype = "error";
404 break;
405 default: {
406 static char unknown[10];
407 (void)snprintf(badtype = unknown, sizeof unknown,
408 "?(%x)", vn.v_tag);
409 break;;
410 }
411 }
412 if (checkfile) {
413 int fsmatch = 0;
414 DEVS *d;
415
416 if (badtype)
417 return;
418 for (d = devs; d != NULL; d = d->next)
419 if (d->fsid == fst.fsid) {
420 fsmatch = 1;
421 if (d->ino == fst.fileid) {
422 filename = d->name;
423 break;
424 }
425 }
426 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
427 return;
428 }
429 PREFIX(i);
430 if (badtype) {
431 (void)printf(" - - %10s -\n", badtype);
432 return;
433 }
434 if (nflg)
435 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
436 else
437 (void)printf(" %-8s", getmnton(vn.v_mount));
438 if (nflg)
439 (void)snprintf(mode, sizeof mode, "%o", fst.mode);
440 else
441 strmode(fst.mode, mode);
442 (void)printf(" %6ld %10s", (long)fst.fileid, mode);
443 switch (vn.v_type) {
444 case VBLK:
445 case VCHR: {
446 char *name;
447
448 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
449 S_IFCHR : S_IFBLK)) == NULL))
450 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev));
451 else
452 printf(" %6s", name);
453 break;
454 }
455 default:
456 printf(" %6qd", (long long)fst.size);
457 }
458 rw[0] = '\0';
459 if (flag & FREAD)
460 strcat(rw, "r");
461 if (flag & FWRITE)
462 strcat(rw, "w");
463 printf(" %-2s", rw);
464 if (filename && !fsflg)
465 printf(" %s", filename);
466 putchar('\n');
467 }
468
469 int
470 ufs_filestat(vp, fsp)
471 struct vnode *vp;
472 struct filestat *fsp;
473 {
474 struct inode inode;
475
476 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
477 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
478 return 0;
479 }
480 fsp->fsid = inode.i_dev & 0xffff;
481 fsp->fileid = (long)inode.i_number;
482 fsp->mode = (mode_t)inode.i_ffs_mode;
483 fsp->size = inode.i_ffs_size;
484 fsp->rdev = inode.i_ffs_rdev;
485
486 return 1;
487 }
488
489 int
490 ext2fs_filestat(vp, fsp)
491 struct vnode *vp;
492 struct filestat *fsp;
493 {
494 struct inode inode;
495
496 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
497 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
498 return 0;
499 }
500 fsp->fsid = inode.i_dev & 0xffff;
501 fsp->fileid = (long)inode.i_number;
502 fsp->mode = (mode_t)inode.i_e2fs_mode;
503 fsp->size = inode.i_e2fs_size;
504 fsp->rdev = 0; /* XXX */
505 return 1;
506 }
507
508 int
509 nfs_filestat(vp, fsp)
510 struct vnode *vp;
511 struct filestat *fsp;
512 {
513 struct nfsnode nfsnode;
514 struct vattr va;
515 mode_t mode;
516
517 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
518 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
519 Pid);
520 return 0;
521 }
522 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
523 dprintf("can't read vnode attributes at %p for pid %d",
524 nfsnode.n_vattr, Pid);
525 return 0;
526 }
527 fsp->fsid = va.va_fsid;
528 fsp->fileid = va.va_fileid;
529 fsp->size = nfsnode.n_size;
530 fsp->rdev = va.va_rdev;
531 mode = (mode_t)va.va_mode;
532 switch (vp->v_type) {
533 case VREG:
534 mode |= S_IFREG;
535 break;
536 case VDIR:
537 mode |= S_IFDIR;
538 break;
539 case VBLK:
540 mode |= S_IFBLK;
541 break;
542 case VCHR:
543 mode |= S_IFCHR;
544 break;
545 case VLNK:
546 mode |= S_IFLNK;
547 break;
548 case VSOCK:
549 mode |= S_IFSOCK;
550 break;
551 case VFIFO:
552 mode |= S_IFIFO;
553 break;
554 default:
555 break;
556 };
557 fsp->mode = mode;
558
559 return 1;
560 }
561
562
563 char *
564 getmnton(m)
565 struct mount *m;
566 {
567 static struct mount mount;
568 static struct mtab {
569 struct mtab *next;
570 struct mount *m;
571 char mntonname[MNAMELEN];
572 } *mhead = NULL;
573 struct mtab *mt;
574
575 for (mt = mhead; mt != NULL; mt = mt->next)
576 if (m == mt->m)
577 return (mt->mntonname);
578 if (!KVM_READ(m, &mount, sizeof(struct mount))) {
579 warnx("can't read mount table at %p", m);
580 return (NULL);
581 }
582 if ((mt = malloc(sizeof (struct mtab))) == NULL) {
583 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
584 }
585 mt->m = m;
586 memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
587 mt->next = mhead;
588 mhead = mt;
589 return (mt->mntonname);
590 }
591
592 void
593 socktrans(sock, i)
594 struct socket *sock;
595 int i;
596 {
597 static char *stypename[] = {
598 "unused", /* 0 */
599 "stream", /* 1 */
600 "dgram", /* 2 */
601 "raw", /* 3 */
602 "rdm", /* 4 */
603 "seqpak" /* 5 */
604 };
605 #define STYPEMAX 5
606 struct socket so;
607 struct protosw proto;
608 struct domain dom;
609 struct inpcb inpcb;
610 struct unpcb unpcb;
611 int len;
612 char dname[32];
613
614 PREFIX(i);
615
616 /* fill in socket */
617 if (!KVM_READ(sock, &so, sizeof(struct socket))) {
618 dprintf("can't read sock at %p", sock);
619 goto bad;
620 }
621
622 /* fill in protosw entry */
623 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
624 dprintf("can't read protosw at %p", so.so_proto);
625 goto bad;
626 }
627
628 /* fill in domain */
629 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
630 dprintf("can't read domain at %p", proto.pr_domain);
631 goto bad;
632 }
633
634 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
635 sizeof(dname) - 1)) != sizeof(dname) -1) {
636 dprintf("can't read domain name at %p", dom.dom_name);
637 dname[0] = '\0';
638 }
639 else
640 dname[len] = '\0';
641
642 if ((u_short)so.so_type > STYPEMAX)
643 printf("* %s ?%d", dname, so.so_type);
644 else
645 printf("* %s %s", dname, stypename[so.so_type]);
646
647 /*
648 * protocol specific formatting
649 *
650 * Try to find interesting things to print. For TCP, the interesting
651 * thing is the address of the tcpcb, for UDP and others, just the
652 * inpcb (socket pcb). For UNIX domain, its the address of the socket
653 * pcb and the address of the connected pcb (if connected). Otherwise
654 * just print the protocol number and address of the socket itself.
655 * The idea is not to duplicate netstat, but to make available enough
656 * information for further analysis.
657 */
658 switch(dom.dom_family) {
659 case AF_INET:
660 getinetproto(proto.pr_protocol);
661 if (proto.pr_protocol == IPPROTO_TCP) {
662 if (so.so_pcb == NULL)
663 break;
664 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
665 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
666 dprintf("can't read inpcb at %p", so.so_pcb);
667 goto bad;
668 }
669 printf(" %lx", (long)inpcb.inp_ppcb);
670 printf(" %s:%d",
671 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
672 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
673 if (inpcb.inp_fport) {
674 printf(" <-> ");
675 printf("%s:%d",
676 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
677 inet_ntoa(inpcb.inp_faddr),
678 ntohs(inpcb.inp_fport));
679 }
680 } else if (proto.pr_protocol == IPPROTO_UDP) {
681 if (so.so_pcb == NULL)
682 break;
683 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
684 sizeof(struct inpcb)) != sizeof(struct inpcb)) {
685 dprintf("can't read inpcb at %p", so.so_pcb);
686 goto bad;
687 }
688 printf(" %lx", (long)so.so_pcb);
689 printf(" %s:%d",
690 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
691 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
692 if (inpcb.inp_fport)
693 printf(" <-> %s:%d",
694 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
695 inet_ntoa(inpcb.inp_faddr),
696 ntohs(inpcb.inp_fport));
697 } else if (so.so_pcb)
698 printf(" %lx", (long)so.so_pcb);
699 break;
700 case AF_LOCAL:
701 /* print address of pcb and connected pcb */
702 if (so.so_pcb) {
703 printf(" %lx", (long)so.so_pcb);
704 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
705 sizeof(struct unpcb)) != sizeof(struct unpcb)){
706 dprintf("can't read unpcb at %p", so.so_pcb);
707 goto bad;
708 }
709 if (unpcb.unp_conn) {
710 char shoconn[4], *cp;
711
712 cp = shoconn;
713 if (!(so.so_state & SS_CANTRCVMORE))
714 *cp++ = '<';
715 *cp++ = '-';
716 if (!(so.so_state & SS_CANTSENDMORE))
717 *cp++ = '>';
718 *cp = '\0';
719 printf(" %s %lx", shoconn,
720 (long)unpcb.unp_conn);
721 }
722 }
723 break;
724 default:
725 /* print protocol number and socket address */
726 printf(" %d %lx", proto.pr_protocol, (long)sock);
727 }
728 printf("\n");
729 return;
730 bad:
731 printf("* error\n");
732 }
733
734 /*
735 * getinetproto --
736 * print name of protocol number
737 */
738 void
739 getinetproto(number)
740 int number;
741 {
742 char *cp;
743
744 switch (number) {
745 case IPPROTO_IP:
746 cp = "ip"; break;
747 case IPPROTO_ICMP:
748 cp ="icmp"; break;
749 case IPPROTO_GGP:
750 cp ="ggp"; break;
751 case IPPROTO_TCP:
752 cp ="tcp"; break;
753 case IPPROTO_EGP:
754 cp ="egp"; break;
755 case IPPROTO_PUP:
756 cp ="pup"; break;
757 case IPPROTO_UDP:
758 cp ="udp"; break;
759 case IPPROTO_IDP:
760 cp ="idp"; break;
761 case IPPROTO_RAW:
762 cp ="raw"; break;
763 default:
764 printf(" %d", number);
765 return;
766 }
767 printf(" %s", cp);
768 }
769
770 int
771 getfname(filename)
772 char *filename;
773 {
774 struct stat statbuf;
775 DEVS *cur;
776
777 if (stat(filename, &statbuf)) {
778 warn("stat(%s)", filename);
779 return(0);
780 }
781 if ((cur = malloc(sizeof(DEVS))) == NULL) {
782 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
783 }
784 cur->next = devs;
785 devs = cur;
786
787 cur->ino = statbuf.st_ino;
788 cur->fsid = statbuf.st_dev & 0xffff;
789 cur->name = filename;
790 return(1);
791 }
792
793 void
794 usage()
795 {
796 errx(1,
797 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
798 }
799