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