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