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