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