fstat.c revision 1.94 1 1.94 mrg /* $NetBSD: fstat.c,v 1.94 2011/09/23 07:31:39 mrg 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.64 agc * 3. Neither the name of the University nor the names of its contributors
16 1.1 cgd * may be used to endorse or promote products derived from this software
17 1.1 cgd * without specific prior written permission.
18 1.1 cgd *
19 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cgd * SUCH DAMAGE.
30 1.1 cgd */
31 1.1 cgd
32 1.23 lukem #include <sys/cdefs.h>
33 1.1 cgd #ifndef lint
34 1.85 lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
35 1.85 lukem The Regents of the University of California. All rights reserved.");
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.1 cgd #ifndef lint
39 1.21 mrg #if 0
40 1.21 mrg static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95";
41 1.21 mrg #else
42 1.94 mrg __RCSID("$NetBSD: fstat.c,v 1.94 2011/09/23 07:31:39 mrg Exp $");
43 1.21 mrg #endif
44 1.1 cgd #endif /* not lint */
45 1.1 cgd
46 1.78 ad #include <sys/types.h>
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/stat.h>
51 1.1 cgd #include <sys/vnode.h>
52 1.1 cgd #include <sys/socket.h>
53 1.1 cgd #include <sys/socketvar.h>
54 1.1 cgd #include <sys/domain.h>
55 1.1 cgd #include <sys/protosw.h>
56 1.1 cgd #include <sys/unpcb.h>
57 1.8 cgd #include <sys/sysctl.h>
58 1.1 cgd #include <sys/filedesc.h>
59 1.47 jdolecek #include <sys/pipe.h>
60 1.14 jtc #define _KERNEL
61 1.1 cgd #include <sys/file.h>
62 1.10 cgd #include <ufs/ufs/inode.h>
63 1.14 jtc #undef _KERNEL
64 1.41 jdolecek #define _KERNEL
65 1.41 jdolecek #include <sys/mount.h>
66 1.41 jdolecek #undef _KERNEL
67 1.8 cgd #define NFS
68 1.15 fvdl #include <nfs/nfsproto.h>
69 1.8 cgd #include <nfs/rpcv2.h>
70 1.1 cgd #include <nfs/nfs.h>
71 1.1 cgd #include <nfs/nfsnode.h>
72 1.8 cgd #undef NFS
73 1.41 jdolecek #include <msdosfs/denode.h>
74 1.41 jdolecek #include <msdosfs/bpb.h>
75 1.41 jdolecek #define _KERNEL
76 1.41 jdolecek #include <msdosfs/msdosfsmount.h>
77 1.41 jdolecek #undef _KERNEL
78 1.43 enami #define _KERNEL
79 1.43 enami #include <miscfs/genfs/layer.h>
80 1.43 enami #undef _KERNEL
81 1.1 cgd
82 1.1 cgd #include <net/route.h>
83 1.1 cgd #include <netinet/in.h>
84 1.1 cgd #include <netinet/in_systm.h>
85 1.1 cgd #include <netinet/ip.h>
86 1.1 cgd #include <netinet/in_pcb.h>
87 1.1 cgd
88 1.38 itojun #ifdef INET6
89 1.38 itojun #include <netinet/ip6.h>
90 1.38 itojun #include <netinet6/ip6_var.h>
91 1.38 itojun #include <netinet6/in6_pcb.h>
92 1.38 itojun #endif
93 1.38 itojun
94 1.93 christos #include <netatalk/at.h>
95 1.93 christos #include <netatalk/ddp_var.h>
96 1.93 christos
97 1.38 itojun #include <netdb.h>
98 1.34 lukem #include <arpa/inet.h>
99 1.34 lukem
100 1.8 cgd #include <ctype.h>
101 1.1 cgd #include <errno.h>
102 1.8 cgd #include <kvm.h>
103 1.15 fvdl #include <limits.h>
104 1.1 cgd #include <nlist.h>
105 1.8 cgd #include <paths.h>
106 1.1 cgd #include <pwd.h>
107 1.1 cgd #include <stdio.h>
108 1.1 cgd #include <stdlib.h>
109 1.1 cgd #include <string.h>
110 1.23 lukem #include <unistd.h>
111 1.27 fair #include <err.h>
112 1.92 christos #include <util.h>
113 1.1 cgd
114 1.34 lukem #include "fstat.h"
115 1.34 lukem
116 1.1 cgd #define TEXT -1
117 1.1 cgd #define CDIR -2
118 1.1 cgd #define RDIR -3
119 1.1 cgd #define TRACE -4
120 1.1 cgd
121 1.1 cgd typedef struct devs {
122 1.1 cgd struct devs *next;
123 1.1 cgd long fsid;
124 1.1 cgd ino_t ino;
125 1.72 christos const char *name;
126 1.1 cgd } DEVS;
127 1.72 christos static DEVS *devs;
128 1.1 cgd
129 1.72 christos static int fsflg, /* show files on same filesystem as file(s) argument */
130 1.1 cgd pflg, /* show files open by a particular pid */
131 1.1 cgd uflg; /* show files open by a particular (effective) user */
132 1.72 christos static int checkfile; /* true if restricting to particular files or filesystems */
133 1.72 christos static int nflg; /* (numerical) display f.s. and rdev as dev_t */
134 1.1 cgd int vflg; /* display errors in locating kernel data objects etc... */
135 1.1 cgd
136 1.80 christos static fdfile_t **ofiles; /* buffer of pointers to file structures */
137 1.74 mrg static int fstat_maxfiles;
138 1.1 cgd #define ALLOC_OFILES(d) \
139 1.74 mrg if ((d) > fstat_maxfiles) { \
140 1.80 christos size_t len = (d) * sizeof(fdfile_t *); \
141 1.1 cgd free(ofiles); \
142 1.80 christos ofiles = malloc(len); \
143 1.1 cgd if (ofiles == NULL) { \
144 1.80 christos err(1, "malloc(%zu)", len); \
145 1.1 cgd } \
146 1.74 mrg fstat_maxfiles = (d); \
147 1.1 cgd }
148 1.1 cgd
149 1.8 cgd kvm_t *kd;
150 1.1 cgd
151 1.81 christos static const char *const dtypes[] = {
152 1.81 christos DTYPE_NAMES
153 1.81 christos };
154 1.81 christos
155 1.72 christos static void dofiles(struct kinfo_proc2 *);
156 1.72 christos static int ext2fs_filestat(struct vnode *, struct filestat *);
157 1.72 christos static int getfname(const char *);
158 1.72 christos static void getinetproto(int);
159 1.93 christos static void getatproto(int);
160 1.72 christos static char *getmnton(struct mount *);
161 1.72 christos static const char *layer_filestat(struct vnode *, struct filestat *);
162 1.72 christos static int msdosfs_filestat(struct vnode *, struct filestat *);
163 1.72 christos static int nfs_filestat(struct vnode *, struct filestat *);
164 1.93 christos static const char *inet_addrstr(char *, size_t, const struct in_addr *,
165 1.93 christos uint16_t);
166 1.38 itojun #ifdef INET6
167 1.93 christos static const char *inet6_addrstr(char *, size_t, const struct in6_addr *,
168 1.93 christos uint16_t);
169 1.38 itojun #endif
170 1.93 christos static const char *at_addrstr(char *, size_t, const struct sockaddr_at *);
171 1.72 christos static void socktrans(struct socket *, int);
172 1.84 christos static void misctrans(struct file *);
173 1.72 christos static int ufs_filestat(struct vnode *, struct filestat *);
174 1.77 perry static void usage(void) __dead;
175 1.72 christos static const char *vfilestat(struct vnode *, struct filestat *);
176 1.72 christos static void vtrans(struct vnode *, int, int);
177 1.80 christos static void ftrans(fdfile_t *, int);
178 1.72 christos static void ptrans(struct file *, struct pipe *, int);
179 1.1 cgd
180 1.23 lukem int
181 1.72 christos main(int argc, char **argv)
182 1.1 cgd {
183 1.23 lukem struct passwd *passwd;
184 1.58 dsl struct kinfo_proc2 *p, *plast;
185 1.1 cgd int arg, ch, what;
186 1.8 cgd char *memf, *nlistf;
187 1.15 fvdl char buf[_POSIX2_LINE_MAX];
188 1.8 cgd int cnt;
189 1.26 mrg gid_t egid = getegid();
190 1.1 cgd
191 1.26 mrg (void)setegid(getgid());
192 1.1 cgd arg = 0;
193 1.8 cgd what = KERN_PROC_ALL;
194 1.8 cgd nlistf = memf = NULL;
195 1.22 lukem while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
196 1.1 cgd switch((char)ch) {
197 1.1 cgd case 'f':
198 1.1 cgd fsflg = 1;
199 1.1 cgd break;
200 1.8 cgd case 'M':
201 1.8 cgd memf = optarg;
202 1.8 cgd break;
203 1.8 cgd case 'N':
204 1.8 cgd nlistf = optarg;
205 1.8 cgd break;
206 1.1 cgd case 'n':
207 1.1 cgd nflg = 1;
208 1.1 cgd break;
209 1.1 cgd case 'p':
210 1.1 cgd if (pflg++)
211 1.1 cgd usage();
212 1.68 dsl if (!isdigit((unsigned char)*optarg)) {
213 1.34 lukem warnx("-p requires a process id");
214 1.1 cgd usage();
215 1.1 cgd }
216 1.8 cgd what = KERN_PROC_PID;
217 1.1 cgd arg = atoi(optarg);
218 1.1 cgd break;
219 1.1 cgd case 'u':
220 1.1 cgd if (uflg++)
221 1.1 cgd usage();
222 1.1 cgd if (!(passwd = getpwnam(optarg))) {
223 1.34 lukem errx(1, "%s: unknown uid", optarg);
224 1.1 cgd }
225 1.8 cgd what = KERN_PROC_UID;
226 1.1 cgd arg = passwd->pw_uid;
227 1.1 cgd break;
228 1.1 cgd case 'v':
229 1.1 cgd vflg = 1;
230 1.1 cgd break;
231 1.1 cgd case '?':
232 1.1 cgd default:
233 1.1 cgd usage();
234 1.1 cgd }
235 1.1 cgd
236 1.1 cgd if (*(argv += optind)) {
237 1.1 cgd for (; *argv; ++argv) {
238 1.1 cgd if (getfname(*argv))
239 1.1 cgd checkfile = 1;
240 1.1 cgd }
241 1.49 wiz if (!checkfile) /* file(s) specified, but none accessible */
242 1.1 cgd exit(1);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd ALLOC_OFILES(256); /* reserve space for file pointers */
246 1.1 cgd
247 1.1 cgd if (fsflg && !checkfile) {
248 1.1 cgd /* -f with no files means use wd */
249 1.1 cgd if (getfname(".") == 0)
250 1.1 cgd exit(1);
251 1.1 cgd checkfile = 1;
252 1.1 cgd }
253 1.1 cgd
254 1.8 cgd /*
255 1.26 mrg * Discard setgid privileges. If not the running kernel, we toss
256 1.26 mrg * them away totally so that bad guys can't print interesting stuff
257 1.26 mrg * from kernel memory, otherwise switch back to kmem for the
258 1.26 mrg * duration of the kvm_openfiles() call.
259 1.8 cgd */
260 1.8 cgd if (nlistf != NULL || memf != NULL)
261 1.26 mrg (void)setgid(getgid());
262 1.26 mrg else
263 1.26 mrg (void)setegid(egid);
264 1.26 mrg
265 1.26 mrg if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
266 1.26 mrg errx(1, "%s", buf);
267 1.26 mrg
268 1.26 mrg /* get rid of it now anyway */
269 1.26 mrg if (nlistf == NULL && memf == NULL)
270 1.26 mrg (void)setgid(getgid());
271 1.8 cgd
272 1.58 dsl if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) {
273 1.34 lukem errx(1, "%s", kvm_geterr(kd));
274 1.1 cgd }
275 1.1 cgd if (nflg)
276 1.72 christos (void)printf("%s",
277 1.55 simonb "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
278 1.1 cgd else
279 1.72 christos (void)printf("%s",
280 1.55 simonb "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
281 1.1 cgd if (checkfile && fsflg == 0)
282 1.72 christos (void)printf(" NAME\n");
283 1.1 cgd else
284 1.72 christos (void)putchar('\n');
285 1.1 cgd
286 1.8 cgd for (plast = &p[cnt]; p < plast; ++p) {
287 1.58 dsl if (p->p_stat == SZOMB)
288 1.1 cgd continue;
289 1.1 cgd dofiles(p);
290 1.1 cgd }
291 1.72 christos return 0;
292 1.1 cgd }
293 1.1 cgd
294 1.72 christos static const char *Uname, *Comm;
295 1.34 lukem pid_t Pid;
296 1.1 cgd
297 1.72 christos #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
298 1.1 cgd switch(i) { \
299 1.1 cgd case TEXT: \
300 1.72 christos (void)printf(" text"); \
301 1.1 cgd break; \
302 1.1 cgd case CDIR: \
303 1.72 christos (void)printf(" wd"); \
304 1.1 cgd break; \
305 1.1 cgd case RDIR: \
306 1.72 christos (void)printf(" root"); \
307 1.1 cgd break; \
308 1.1 cgd case TRACE: \
309 1.72 christos (void)printf(" tr"); \
310 1.1 cgd break; \
311 1.1 cgd default: \
312 1.72 christos (void)printf(" %4d", i); \
313 1.1 cgd break; \
314 1.1 cgd }
315 1.1 cgd
316 1.1 cgd /*
317 1.1 cgd * print open files attributed to this process
318 1.1 cgd */
319 1.72 christos static void
320 1.72 christos dofiles(struct kinfo_proc2 *p)
321 1.1 cgd {
322 1.23 lukem int i;
323 1.78 ad struct filedesc filed;
324 1.35 thorpej struct cwdinfo cwdi;
325 1.89 ad struct fdtab dt;
326 1.1 cgd
327 1.58 dsl Uname = user_from_uid(p->p_uid, 0);
328 1.1 cgd Pid = p->p_pid;
329 1.1 cgd Comm = p->p_comm;
330 1.1 cgd
331 1.65 fvdl if (p->p_fd == 0 || p->p_cwdi == 0)
332 1.1 cgd return;
333 1.78 ad if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
334 1.91 christos warnx("can't read filedesc at %p for pid %d",
335 1.91 christos (void *)(uintptr_t)p->p_fd, Pid);
336 1.13 mycroft return;
337 1.13 mycroft }
338 1.91 christos if (filed.fd_lastfile == -1)
339 1.91 christos return;
340 1.35 thorpej if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
341 1.91 christos warnx("can't read cwdinfo at %p for pid %d",
342 1.91 christos (void *)(uintptr_t)p->p_cwdi, Pid);
343 1.35 thorpej return;
344 1.35 thorpej }
345 1.89 ad if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) {
346 1.89 ad warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid);
347 1.89 ad return;
348 1.89 ad }
349 1.89 ad if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles ||
350 1.13 mycroft filed.fd_freefile > filed.fd_lastfile + 1) {
351 1.91 christos dprintf("filedesc corrupted at %p for pid %d",
352 1.91 christos (void *)(uintptr_t)p->p_fd, Pid);
353 1.1 cgd return;
354 1.1 cgd }
355 1.1 cgd /*
356 1.1 cgd * root directory vnode, if one
357 1.1 cgd */
358 1.35 thorpej if (cwdi.cwdi_rdir)
359 1.35 thorpej vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
360 1.1 cgd /*
361 1.1 cgd * current working directory vnode
362 1.1 cgd */
363 1.35 thorpej vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
364 1.80 christos #if 0
365 1.1 cgd /*
366 1.80 christos * Disable for now, since p->p_tracep appears to point to a ktr_desc *
367 1.1 cgd * ktrace vnode, if one
368 1.1 cgd */
369 1.1 cgd if (p->p_tracep)
370 1.92 christos ftrans(p->p_tracep, TRACE);
371 1.80 christos #endif
372 1.1 cgd /*
373 1.1 cgd * open files
374 1.1 cgd */
375 1.80 christos #define FPSIZE (sizeof (fdfile_t *))
376 1.8 cgd ALLOC_OFILES(filed.fd_lastfile+1);
377 1.89 ad if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles,
378 1.76 dyoung (filed.fd_lastfile+1) * FPSIZE)) {
379 1.76 dyoung dprintf("can't read file structures at %p for pid %d",
380 1.89 ad &filed.fd_dt->dt_ff, Pid);
381 1.76 dyoung return;
382 1.76 dyoung }
383 1.1 cgd for (i = 0; i <= filed.fd_lastfile; i++) {
384 1.1 cgd if (ofiles[i] == NULL)
385 1.1 cgd continue;
386 1.42 sommerfe ftrans(ofiles[i], i);
387 1.42 sommerfe }
388 1.42 sommerfe }
389 1.42 sommerfe
390 1.72 christos static void
391 1.80 christos ftrans(fdfile_t *fp, int i)
392 1.42 sommerfe {
393 1.42 sommerfe struct file file;
394 1.80 christos fdfile_t fdfile;
395 1.42 sommerfe
396 1.80 christos if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
397 1.42 sommerfe dprintf("can't read file %d at %p for pid %d",
398 1.42 sommerfe i, fp, Pid);
399 1.42 sommerfe return;
400 1.42 sommerfe }
401 1.82 christos if (fdfile.ff_file == NULL)
402 1.82 christos return;
403 1.80 christos if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
404 1.80 christos dprintf("can't read file %d at %p for pid %d",
405 1.80 christos i, fdfile.ff_file, Pid);
406 1.80 christos return;
407 1.80 christos }
408 1.48 lukem switch (file.f_type) {
409 1.48 lukem case DTYPE_VNODE:
410 1.92 christos vtrans(file.f_data, i, file.f_flag);
411 1.48 lukem break;
412 1.48 lukem case DTYPE_SOCKET:
413 1.42 sommerfe if (checkfile == 0)
414 1.92 christos socktrans(file.f_data, i);
415 1.48 lukem break;
416 1.48 lukem case DTYPE_PIPE:
417 1.47 jdolecek if (checkfile == 0)
418 1.92 christos ptrans(&file, file.f_data, i);
419 1.48 lukem break;
420 1.84 christos case DTYPE_MISC:
421 1.56 jdolecek case DTYPE_KQUEUE:
422 1.81 christos case DTYPE_CRYPTO:
423 1.81 christos case DTYPE_MQUEUE:
424 1.90 rmind case DTYPE_SEM:
425 1.56 jdolecek if (checkfile == 0)
426 1.84 christos misctrans(&file);
427 1.56 jdolecek break;
428 1.48 lukem default:
429 1.42 sommerfe dprintf("unknown file type %d for file %d of pid %d",
430 1.42 sommerfe file.f_type, i, Pid);
431 1.48 lukem break;
432 1.1 cgd }
433 1.1 cgd }
434 1.1 cgd
435 1.92 christos static const char dead[] = "dead";
436 1.92 christos
437 1.72 christos static const char *
438 1.72 christos vfilestat(struct vnode *vp, struct filestat *fsp)
439 1.1 cgd {
440 1.72 christos const char *badtype = NULL;
441 1.1 cgd
442 1.92 christos if (vp->v_type == VNON)
443 1.1 cgd badtype = "none";
444 1.44 enami else if (vp->v_type == VBAD)
445 1.1 cgd badtype = "bad";
446 1.1 cgd else
447 1.44 enami switch (vp->v_tag) {
448 1.92 christos case VT_NON:
449 1.92 christos badtype = dead;
450 1.92 christos break;
451 1.1 cgd case VT_UFS:
452 1.57 yamt case VT_LFS:
453 1.1 cgd case VT_MFS:
454 1.44 enami if (!ufs_filestat(vp, fsp))
455 1.8 cgd badtype = "error";
456 1.1 cgd break;
457 1.41 jdolecek case VT_MSDOSFS:
458 1.44 enami if (!msdosfs_filestat(vp, fsp))
459 1.41 jdolecek badtype = "error";
460 1.41 jdolecek break;
461 1.1 cgd case VT_NFS:
462 1.44 enami if (!nfs_filestat(vp, fsp))
463 1.8 cgd badtype = "error";
464 1.1 cgd break;
465 1.20 bouyer case VT_EXT2FS:
466 1.44 enami if (!ext2fs_filestat(vp, fsp))
467 1.20 bouyer badtype = "error";
468 1.20 bouyer break;
469 1.34 lukem case VT_ISOFS:
470 1.44 enami if (!isofs_filestat(vp, fsp))
471 1.37 jdolecek badtype = "error";
472 1.37 jdolecek break;
473 1.37 jdolecek case VT_NTFS:
474 1.44 enami if (!ntfs_filestat(vp, fsp))
475 1.34 lukem badtype = "error";
476 1.34 lukem break;
477 1.70 christos case VT_PTYFS:
478 1.70 christos if (!ptyfs_filestat(vp, fsp))
479 1.70 christos badtype = "error";
480 1.70 christos break;
481 1.73 christos case VT_TMPFS:
482 1.73 christos if (!tmpfs_filestat(vp, fsp))
483 1.73 christos badtype = "error";
484 1.73 christos break;
485 1.43 enami case VT_NULL:
486 1.43 enami case VT_OVERLAY:
487 1.43 enami case VT_UMAP:
488 1.44 enami badtype = layer_filestat(vp, fsp);
489 1.43 enami break;
490 1.1 cgd default: {
491 1.1 cgd static char unknown[10];
492 1.72 christos (void)snprintf(unknown, sizeof unknown,
493 1.44 enami "?(%x)", vp->v_tag);
494 1.72 christos badtype = unknown;
495 1.44 enami break;
496 1.1 cgd }
497 1.1 cgd }
498 1.91 christos return badtype;
499 1.44 enami }
500 1.44 enami
501 1.72 christos static void
502 1.72 christos vtrans(struct vnode *vp, int i, int flag)
503 1.44 enami {
504 1.44 enami struct vnode vn;
505 1.44 enami struct filestat fst;
506 1.44 enami char mode[15], rw[3];
507 1.72 christos const char *badtype, *filename;
508 1.44 enami
509 1.44 enami filename = NULL;
510 1.44 enami if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
511 1.44 enami dprintf("can't read vnode at %p for pid %d", vp, Pid);
512 1.44 enami return;
513 1.44 enami }
514 1.44 enami badtype = vfilestat(&vn, &fst);
515 1.1 cgd if (checkfile) {
516 1.1 cgd int fsmatch = 0;
517 1.23 lukem DEVS *d;
518 1.1 cgd
519 1.92 christos if (badtype && badtype != dead)
520 1.1 cgd return;
521 1.1 cgd for (d = devs; d != NULL; d = d->next)
522 1.1 cgd if (d->fsid == fst.fsid) {
523 1.1 cgd fsmatch = 1;
524 1.1 cgd if (d->ino == fst.fileid) {
525 1.1 cgd filename = d->name;
526 1.1 cgd break;
527 1.1 cgd }
528 1.1 cgd }
529 1.1 cgd if (fsmatch == 0 || (filename == NULL && fsflg == 0))
530 1.1 cgd return;
531 1.1 cgd }
532 1.1 cgd PREFIX(i);
533 1.92 christos if (badtype == dead) {
534 1.92 christos char buf[1024];
535 1.92 christos (void)snprintb(buf, sizeof(buf), VNODE_FLAGBITS,
536 1.92 christos vn.v_iflag | vn.v_vflag | vn.v_uflag);
537 1.92 christos (void)printf(" flags %s\n", buf);
538 1.92 christos return;
539 1.92 christos } else if (badtype) {
540 1.1 cgd (void)printf(" - - %10s -\n", badtype);
541 1.1 cgd return;
542 1.1 cgd }
543 1.1 cgd if (nflg)
544 1.87 christos (void)printf(" %2llu,%-2llu",
545 1.87 christos (unsigned long long)major(fst.fsid),
546 1.87 christos (unsigned long long)minor(fst.fsid));
547 1.1 cgd else
548 1.1 cgd (void)printf(" %-8s", getmnton(vn.v_mount));
549 1.1 cgd if (nflg)
550 1.18 mrg (void)snprintf(mode, sizeof mode, "%o", fst.mode);
551 1.1 cgd else
552 1.1 cgd strmode(fst.mode, mode);
553 1.88 lukem (void)printf(" %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
554 1.1 cgd switch (vn.v_type) {
555 1.1 cgd case VBLK:
556 1.1 cgd case VCHR: {
557 1.1 cgd char *name;
558 1.1 cgd
559 1.1 cgd if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
560 1.1 cgd S_IFCHR : S_IFBLK)) == NULL))
561 1.87 christos (void)printf(" %2llu,%-2llu",
562 1.87 christos (unsigned long long)major(fst.rdev),
563 1.87 christos (unsigned long long)minor(fst.rdev));
564 1.1 cgd else
565 1.72 christos (void)printf(" %6s", name);
566 1.1 cgd break;
567 1.1 cgd }
568 1.1 cgd default:
569 1.72 christos (void)printf(" %6lld", (long long)fst.size);
570 1.1 cgd }
571 1.34 lukem rw[0] = '\0';
572 1.8 cgd if (flag & FREAD)
573 1.72 christos (void)strlcat(rw, "r", sizeof(rw));
574 1.8 cgd if (flag & FWRITE)
575 1.72 christos (void)strlcat(rw, "w", sizeof(rw));
576 1.72 christos (void)printf(" %-2s", rw);
577 1.1 cgd if (filename && !fsflg)
578 1.72 christos (void)printf(" %s", filename);
579 1.72 christos (void)putchar('\n');
580 1.1 cgd }
581 1.1 cgd
582 1.72 christos static int
583 1.72 christos ufs_filestat(struct vnode *vp, struct filestat *fsp)
584 1.1 cgd {
585 1.8 cgd struct inode inode;
586 1.62 fvdl union dinode {
587 1.62 fvdl struct ufs1_dinode dp1;
588 1.62 fvdl struct ufs2_dinode dp2;
589 1.62 fvdl } dip;
590 1.8 cgd
591 1.8 cgd if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
592 1.34 lukem dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
593 1.8 cgd return 0;
594 1.8 cgd }
595 1.62 fvdl
596 1.62 fvdl if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) {
597 1.62 fvdl dprintf("can't read dinode at %p for pid %d",
598 1.62 fvdl inode.i_din.ffs1_din, Pid);
599 1.62 fvdl return 0;
600 1.62 fvdl }
601 1.62 fvdl if (inode.i_size == dip.dp1.di_size)
602 1.62 fvdl fsp->rdev = dip.dp1.di_rdev;
603 1.62 fvdl else {
604 1.62 fvdl if (!KVM_READ(inode.i_din.ffs1_din, &dip,
605 1.62 fvdl sizeof(struct ufs2_dinode))) {
606 1.62 fvdl dprintf("can't read dinode at %p for pid %d",
607 1.62 fvdl inode.i_din.ffs1_din, Pid);
608 1.62 fvdl return 0;
609 1.62 fvdl }
610 1.62 fvdl fsp->rdev = dip.dp2.di_rdev;
611 1.62 fvdl }
612 1.62 fvdl
613 1.8 cgd fsp->fsid = inode.i_dev & 0xffff;
614 1.88 lukem fsp->fileid = inode.i_number;
615 1.62 fvdl fsp->mode = (mode_t)inode.i_mode;
616 1.62 fvdl fsp->size = inode.i_size;
617 1.20 bouyer
618 1.20 bouyer return 1;
619 1.20 bouyer }
620 1.20 bouyer
621 1.72 christos static int
622 1.72 christos ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
623 1.20 bouyer {
624 1.20 bouyer struct inode inode;
625 1.66 aymeric u_int16_t mode;
626 1.66 aymeric u_int32_t size;
627 1.1 cgd
628 1.20 bouyer if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
629 1.34 lukem dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
630 1.20 bouyer return 0;
631 1.20 bouyer }
632 1.20 bouyer fsp->fsid = inode.i_dev & 0xffff;
633 1.88 lukem fsp->fileid = inode.i_number;
634 1.66 aymeric
635 1.66 aymeric if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) {
636 1.66 aymeric dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp),
637 1.66 aymeric &inode.i_e2fs_mode, Pid);
638 1.66 aymeric return 0;
639 1.66 aymeric }
640 1.66 aymeric fsp->mode = mode;
641 1.66 aymeric
642 1.66 aymeric if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) {
643 1.66 aymeric dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp),
644 1.66 aymeric &inode.i_e2fs_size, Pid);
645 1.66 aymeric return 0;
646 1.66 aymeric }
647 1.66 aymeric fsp->size = size;
648 1.20 bouyer fsp->rdev = 0; /* XXX */
649 1.8 cgd return 1;
650 1.1 cgd }
651 1.1 cgd
652 1.72 christos static int
653 1.72 christos nfs_filestat(struct vnode *vp, struct filestat *fsp)
654 1.1 cgd {
655 1.8 cgd struct nfsnode nfsnode;
656 1.24 thorpej struct vattr va;
657 1.1 cgd
658 1.8 cgd if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
659 1.34 lukem dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
660 1.34 lukem Pid);
661 1.8 cgd return 0;
662 1.8 cgd }
663 1.24 thorpej if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
664 1.34 lukem dprintf("can't read vnode attributes at %p for pid %d",
665 1.34 lukem nfsnode.n_vattr, Pid);
666 1.24 thorpej return 0;
667 1.24 thorpej }
668 1.24 thorpej fsp->fsid = va.va_fsid;
669 1.24 thorpej fsp->fileid = va.va_fileid;
670 1.8 cgd fsp->size = nfsnode.n_size;
671 1.24 thorpej fsp->rdev = va.va_rdev;
672 1.40 jdolecek fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
673 1.8 cgd
674 1.8 cgd return 1;
675 1.1 cgd }
676 1.1 cgd
677 1.72 christos static int
678 1.72 christos msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
679 1.41 jdolecek {
680 1.41 jdolecek struct denode de;
681 1.41 jdolecek struct msdosfsmount mp;
682 1.41 jdolecek
683 1.41 jdolecek if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
684 1.41 jdolecek dprintf("can't read denode at %p for pid %d", VTONFS(vp),
685 1.41 jdolecek Pid);
686 1.41 jdolecek return 0;
687 1.41 jdolecek }
688 1.41 jdolecek if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
689 1.41 jdolecek dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
690 1.41 jdolecek Pid);
691 1.41 jdolecek return 0;
692 1.41 jdolecek }
693 1.41 jdolecek
694 1.41 jdolecek fsp->fsid = de.de_dev & 0xffff;
695 1.41 jdolecek fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
696 1.41 jdolecek fsp->size = de.de_FileSize;
697 1.41 jdolecek fsp->rdev = 0; /* msdosfs doesn't support device files */
698 1.41 jdolecek fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
699 1.41 jdolecek return 1;
700 1.43 enami }
701 1.43 enami
702 1.72 christos static const char *
703 1.72 christos layer_filestat(struct vnode *vp, struct filestat *fsp)
704 1.43 enami {
705 1.43 enami struct layer_node layer_node;
706 1.44 enami struct mount mount;
707 1.44 enami struct vnode vn;
708 1.72 christos const char *badtype;
709 1.43 enami
710 1.43 enami if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
711 1.43 enami dprintf("can't read layer_node at %p for pid %d",
712 1.43 enami VTOLAYER(vp), Pid);
713 1.91 christos return "error";
714 1.44 enami }
715 1.44 enami if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
716 1.44 enami dprintf("can't read mount struct at %p for pid %d",
717 1.44 enami vp->v_mount, Pid);
718 1.91 christos return "error";
719 1.44 enami }
720 1.44 enami vp = layer_node.layer_lowervp;
721 1.44 enami if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
722 1.44 enami dprintf("can't read vnode at %p for pid %d", vp, Pid);
723 1.91 christos return "error";
724 1.43 enami }
725 1.44 enami if ((badtype = vfilestat(&vn, fsp)) == NULL)
726 1.67 christos fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
727 1.91 christos return badtype;
728 1.41 jdolecek }
729 1.1 cgd
730 1.72 christos static char *
731 1.72 christos getmnton(struct mount *m)
732 1.1 cgd {
733 1.1 cgd static struct mount mount;
734 1.1 cgd static struct mtab {
735 1.1 cgd struct mtab *next;
736 1.1 cgd struct mount *m;
737 1.1 cgd char mntonname[MNAMELEN];
738 1.1 cgd } *mhead = NULL;
739 1.23 lukem struct mtab *mt;
740 1.1 cgd
741 1.1 cgd for (mt = mhead; mt != NULL; mt = mt->next)
742 1.1 cgd if (m == mt->m)
743 1.91 christos return mt->mntonname;
744 1.1 cgd if (!KVM_READ(m, &mount, sizeof(struct mount))) {
745 1.34 lukem warnx("can't read mount table at %p", m);
746 1.91 christos return NULL;
747 1.1 cgd }
748 1.1 cgd if ((mt = malloc(sizeof (struct mtab))) == NULL) {
749 1.30 ross err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
750 1.1 cgd }
751 1.1 cgd mt->m = m;
752 1.72 christos (void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
753 1.72 christos MNAMELEN);
754 1.1 cgd mt->next = mhead;
755 1.1 cgd mhead = mt;
756 1.91 christos return mt->mntonname;
757 1.1 cgd }
758 1.1 cgd
759 1.93 christos static const char *
760 1.93 christos inet_addrstr(char *buf, size_t len, const struct in_addr *a, uint16_t p)
761 1.93 christos {
762 1.93 christos char addr[256];
763 1.93 christos
764 1.93 christos if (a->s_addr == INADDR_ANY) {
765 1.93 christos if (p == 0)
766 1.93 christos addr[0] = '\0';
767 1.93 christos else
768 1.93 christos strlcpy(addr, "*", sizeof(addr));
769 1.93 christos } else {
770 1.93 christos struct sockaddr_in sin;
771 1.93 christos const int niflags = NI_NUMERICHOST;
772 1.93 christos
773 1.93 christos (void)memset(&sin, 0, sizeof(sin));
774 1.93 christos sin.sin_family = AF_INET6;
775 1.93 christos sin.sin_len = sizeof(sin);
776 1.93 christos sin.sin_addr = *a;
777 1.93 christos
778 1.93 christos if (getnameinfo((struct sockaddr *)&sin, sin.sin_len,
779 1.93 christos addr, sizeof(addr), NULL, 0, niflags))
780 1.93 christos if (inet_ntop(AF_INET, a, addr, sizeof(addr)) == NULL)
781 1.93 christos strlcpy(addr, "invalid", sizeof(addr));
782 1.93 christos }
783 1.93 christos if (addr[0])
784 1.93 christos snprintf(buf, len, "%s:%u", addr, p);
785 1.93 christos else
786 1.93 christos strlcpy(buf, addr, len);
787 1.93 christos return buf;
788 1.93 christos }
789 1.93 christos
790 1.38 itojun #ifdef INET6
791 1.38 itojun static const char *
792 1.93 christos inet6_addrstr(char *buf, size_t len, const struct in6_addr *a, uint16_t p)
793 1.38 itojun {
794 1.93 christos char addr[256];
795 1.38 itojun
796 1.93 christos if (IN6_IS_ADDR_UNSPECIFIED(a)) {
797 1.93 christos if (p == 0)
798 1.93 christos addr[0] = '\0';
799 1.93 christos else
800 1.93 christos strlcpy(addr, "*", sizeof(addr));
801 1.93 christos } else {
802 1.93 christos struct sockaddr_in6 sin6;
803 1.93 christos const int niflags = NI_NUMERICHOST;
804 1.93 christos
805 1.93 christos (void)memset(&sin6, 0, sizeof(sin6));
806 1.93 christos sin6.sin6_family = AF_INET6;
807 1.93 christos sin6.sin6_len = sizeof(sin6);
808 1.93 christos sin6.sin6_addr = *a;
809 1.93 christos
810 1.93 christos if (IN6_IS_ADDR_LINKLOCAL(a) &&
811 1.93 christos *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
812 1.93 christos sin6.sin6_scope_id =
813 1.93 christos ntohs(*(uint16_t *)&sin6.sin6_addr.s6_addr[2]);
814 1.93 christos sin6.sin6_addr.s6_addr[2] = 0;
815 1.93 christos sin6.sin6_addr.s6_addr[3] = 0;
816 1.93 christos }
817 1.93 christos
818 1.93 christos if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
819 1.93 christos addr, sizeof(addr), NULL, 0, niflags))
820 1.93 christos if (inet_ntop(AF_INET6, a, addr, sizeof(addr)) == NULL)
821 1.93 christos strlcpy(addr, "invalid", sizeof(addr));
822 1.93 christos }
823 1.93 christos if (addr[0])
824 1.93 christos snprintf(buf, len, "[%s]:%u", addr, p);
825 1.93 christos else
826 1.93 christos strlcpy(buf, addr, len);
827 1.93 christos
828 1.93 christos return buf;
829 1.38 itojun }
830 1.38 itojun #endif
831 1.38 itojun
832 1.93 christos static const char *
833 1.93 christos at_addrstr(char *buf, size_t len, const struct sockaddr_at *sat)
834 1.93 christos {
835 1.93 christos const struct netrange *nr = &sat->sat_range.r_netrange;
836 1.93 christos const struct at_addr *at = &sat->sat_addr;
837 1.93 christos char addr[64], phase[64], range[64];
838 1.93 christos
839 1.93 christos if (sat->sat_port || at->s_net || at->s_node) {
840 1.93 christos if (at->s_net || at->s_node)
841 1.93 christos snprintf(addr, sizeof(addr), "%u.%u:%u",
842 1.93 christos ntohs(at->s_net), at->s_node, sat->sat_port);
843 1.93 christos else
844 1.93 christos snprintf(addr, sizeof(addr), "*:%u", sat->sat_port);
845 1.93 christos } else
846 1.93 christos addr[0] = '\0';
847 1.93 christos
848 1.93 christos if (nr->nr_phase)
849 1.93 christos snprintf(phase, sizeof(phase), " phase %u", nr->nr_phase);
850 1.93 christos else
851 1.93 christos phase[0] = '\0';
852 1.93 christos
853 1.93 christos if (nr->nr_firstnet || nr->nr_lastnet)
854 1.93 christos snprintf(range, sizeof(range), " range [%u-%u]",
855 1.93 christos ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet));
856 1.93 christos else
857 1.93 christos range[0] = '\0';
858 1.93 christos
859 1.93 christos snprintf(buf, len, "%s%s%s", addr, phase, range);
860 1.93 christos return buf;
861 1.93 christos }
862 1.93 christos
863 1.72 christos static void
864 1.72 christos socktrans(struct socket *sock, int i)
865 1.1 cgd {
866 1.72 christos static const char *stypename[] = {
867 1.1 cgd "unused", /* 0 */
868 1.1 cgd "stream", /* 1 */
869 1.1 cgd "dgram", /* 2 */
870 1.1 cgd "raw", /* 3 */
871 1.1 cgd "rdm", /* 4 */
872 1.1 cgd "seqpak" /* 5 */
873 1.1 cgd };
874 1.1 cgd #define STYPEMAX 5
875 1.1 cgd struct socket so;
876 1.1 cgd struct protosw proto;
877 1.1 cgd struct domain dom;
878 1.1 cgd struct inpcb inpcb;
879 1.38 itojun #ifdef INET6
880 1.38 itojun struct in6pcb in6pcb;
881 1.38 itojun #endif
882 1.1 cgd struct unpcb unpcb;
883 1.93 christos struct ddpcb ddpcb;
884 1.1 cgd int len;
885 1.18 mrg char dname[32];
886 1.93 christos char lbuf[512], fbuf[512];
887 1.1 cgd PREFIX(i);
888 1.1 cgd
889 1.1 cgd /* fill in socket */
890 1.1 cgd if (!KVM_READ(sock, &so, sizeof(struct socket))) {
891 1.34 lukem dprintf("can't read sock at %p", sock);
892 1.1 cgd goto bad;
893 1.1 cgd }
894 1.1 cgd
895 1.1 cgd /* fill in protosw entry */
896 1.1 cgd if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
897 1.34 lukem dprintf("can't read protosw at %p", so.so_proto);
898 1.1 cgd goto bad;
899 1.1 cgd }
900 1.1 cgd
901 1.1 cgd /* fill in domain */
902 1.1 cgd if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
903 1.34 lukem dprintf("can't read domain at %p", proto.pr_domain);
904 1.1 cgd goto bad;
905 1.1 cgd }
906 1.1 cgd
907 1.8 cgd if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
908 1.25 msaitoh sizeof(dname) - 1)) != sizeof(dname) -1) {
909 1.34 lukem dprintf("can't read domain name at %p", dom.dom_name);
910 1.1 cgd dname[0] = '\0';
911 1.1 cgd }
912 1.1 cgd else
913 1.1 cgd dname[len] = '\0';
914 1.1 cgd
915 1.1 cgd if ((u_short)so.so_type > STYPEMAX)
916 1.72 christos (void)printf("* %s ?%d", dname, so.so_type);
917 1.1 cgd else
918 1.72 christos (void)printf("* %s %s", dname, stypename[so.so_type]);
919 1.1 cgd
920 1.1 cgd /*
921 1.1 cgd * protocol specific formatting
922 1.1 cgd *
923 1.34 lukem * Try to find interesting things to print. For TCP, the interesting
924 1.34 lukem * thing is the address of the tcpcb, for UDP and others, just the
925 1.34 lukem * inpcb (socket pcb). For UNIX domain, its the address of the socket
926 1.1 cgd * pcb and the address of the connected pcb (if connected). Otherwise
927 1.1 cgd * just print the protocol number and address of the socket itself.
928 1.1 cgd * The idea is not to duplicate netstat, but to make available enough
929 1.1 cgd * information for further analysis.
930 1.1 cgd */
931 1.93 christos fbuf[0] = '\0';
932 1.93 christos lbuf[0] = '\0';
933 1.1 cgd switch(dom.dom_family) {
934 1.1 cgd case AF_INET:
935 1.1 cgd getinetproto(proto.pr_protocol);
936 1.93 christos switch (proto.pr_protocol) {
937 1.93 christos case IPPROTO_TCP:
938 1.93 christos case IPPROTO_UDP:
939 1.34 lukem if (so.so_pcb == NULL)
940 1.34 lukem break;
941 1.34 lukem if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
942 1.93 christos sizeof(inpcb)) != sizeof(inpcb)) {
943 1.34 lukem dprintf("can't read inpcb at %p", so.so_pcb);
944 1.34 lukem goto bad;
945 1.34 lukem }
946 1.93 christos inet_addrstr(lbuf, sizeof(lbuf), &inpcb.inp_laddr,
947 1.93 christos ntohs(inpcb.inp_lport));
948 1.93 christos inet_addrstr(fbuf, sizeof(fbuf), &inpcb.inp_faddr,
949 1.93 christos ntohs(inpcb.inp_fport));
950 1.93 christos break;
951 1.93 christos default:
952 1.93 christos break;
953 1.93 christos }
954 1.1 cgd break;
955 1.38 itojun #ifdef INET6
956 1.38 itojun case AF_INET6:
957 1.38 itojun getinetproto(proto.pr_protocol);
958 1.93 christos switch (proto.pr_protocol) {
959 1.93 christos case IPPROTO_TCP:
960 1.93 christos case IPPROTO_UDP:
961 1.38 itojun if (so.so_pcb == NULL)
962 1.38 itojun break;
963 1.38 itojun if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
964 1.93 christos sizeof(in6pcb)) != sizeof(in6pcb)) {
965 1.38 itojun dprintf("can't read in6pcb at %p", so.so_pcb);
966 1.38 itojun goto bad;
967 1.38 itojun }
968 1.93 christos inet6_addrstr(lbuf, sizeof(lbuf), &in6pcb.in6p_laddr,
969 1.93 christos in6pcb.in6p_lport);
970 1.93 christos inet6_addrstr(fbuf, sizeof(fbuf), &in6pcb.in6p_faddr,
971 1.93 christos in6pcb.in6p_fport);
972 1.93 christos break;
973 1.93 christos default:
974 1.93 christos break;
975 1.93 christos }
976 1.38 itojun break;
977 1.38 itojun #endif
978 1.31 lukem case AF_LOCAL:
979 1.1 cgd /* print address of pcb and connected pcb */
980 1.1 cgd if (so.so_pcb) {
981 1.91 christos char shoconn[4], *cp;
982 1.91 christos
983 1.8 cgd if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
984 1.1 cgd sizeof(struct unpcb)) != sizeof(struct unpcb)){
985 1.34 lukem dprintf("can't read unpcb at %p", so.so_pcb);
986 1.1 cgd goto bad;
987 1.1 cgd }
988 1.91 christos
989 1.91 christos cp = shoconn;
990 1.91 christos if (!(so.so_state & SS_CANTRCVMORE))
991 1.91 christos *cp++ = '<';
992 1.91 christos *cp++ = '-';
993 1.91 christos if (!(so.so_state & SS_CANTSENDMORE))
994 1.91 christos *cp++ = '>';
995 1.91 christos *cp = '\0';
996 1.91 christos if (unpcb.unp_addr) {
997 1.91 christos struct sockaddr_un *sun =
998 1.91 christos malloc(unpcb.unp_addrlen);
999 1.91 christos if (sun == NULL)
1000 1.91 christos err(1, "malloc(%zu)",
1001 1.91 christos unpcb.unp_addrlen);
1002 1.91 christos if (kvm_read(kd, (u_long)unpcb.unp_addr,
1003 1.91 christos sun, unpcb.unp_addrlen) !=
1004 1.91 christos (ssize_t)unpcb.unp_addrlen) {
1005 1.91 christos dprintf("can't read sun at %p",
1006 1.91 christos unpcb.unp_addr);
1007 1.91 christos free(sun);
1008 1.91 christos } else {
1009 1.93 christos snprintf(fbuf, sizeof(fbuf), " %s %s",
1010 1.91 christos shoconn, sun->sun_path);
1011 1.91 christos free(sun);
1012 1.91 christos break;
1013 1.91 christos }
1014 1.91 christos }
1015 1.93 christos if (unpcb.unp_conn)
1016 1.93 christos snprintf(fbuf, sizeof(fbuf), " %s %lx", shoconn,
1017 1.12 cgd (long)unpcb.unp_conn);
1018 1.93 christos }
1019 1.93 christos break;
1020 1.93 christos case AF_APPLETALK:
1021 1.93 christos getatproto(proto.pr_protocol);
1022 1.93 christos if (so.so_pcb) {
1023 1.93 christos if (kvm_read(kd, (u_long)so.so_pcb, (char *)&ddpcb,
1024 1.93 christos sizeof(ddpcb)) != sizeof(ddpcb)){
1025 1.93 christos dprintf("can't read ddpcb at %p", so.so_pcb);
1026 1.93 christos goto bad;
1027 1.1 cgd }
1028 1.93 christos at_addrstr(fbuf, sizeof(fbuf), &ddpcb.ddp_fsat);
1029 1.93 christos at_addrstr(lbuf, sizeof(lbuf), &ddpcb.ddp_lsat);
1030 1.1 cgd }
1031 1.1 cgd break;
1032 1.1 cgd default:
1033 1.1 cgd /* print protocol number and socket address */
1034 1.93 christos snprintf(fbuf, sizeof(fbuf), " %d %jx", proto.pr_protocol,
1035 1.94 mrg (uintmax_t)(uintptr_t)sock);
1036 1.93 christos break;
1037 1.1 cgd }
1038 1.93 christos if (fbuf[0] || lbuf[0])
1039 1.93 christos printf(" %s%s%s", fbuf, (fbuf[0] && lbuf[0]) ? " <-> " : "",
1040 1.93 christos lbuf);
1041 1.93 christos else if (so.so_pcb)
1042 1.94 mrg printf(" %jx", (uintmax_t)(uintptr_t)so.so_pcb);
1043 1.72 christos (void)printf("\n");
1044 1.47 jdolecek return;
1045 1.47 jdolecek bad:
1046 1.72 christos (void)printf("* error\n");
1047 1.47 jdolecek }
1048 1.47 jdolecek
1049 1.72 christos static void
1050 1.72 christos ptrans(struct file *fp, struct pipe *cpipe, int i)
1051 1.47 jdolecek {
1052 1.47 jdolecek struct pipe cp;
1053 1.47 jdolecek
1054 1.47 jdolecek PREFIX(i);
1055 1.47 jdolecek
1056 1.50 jdolecek /* fill in pipe */
1057 1.47 jdolecek if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
1058 1.47 jdolecek dprintf("can't read pipe at %p", cpipe);
1059 1.47 jdolecek goto bad;
1060 1.47 jdolecek }
1061 1.47 jdolecek
1062 1.52 jdolecek /* pipe descriptor is either read or write, never both */
1063 1.72 christos (void)printf("* pipe %p %s %p %s%s%s", cpipe,
1064 1.51 jdolecek (fp->f_flag & FWRITE) ? "->" : "<-",
1065 1.52 jdolecek cp.pipe_peer,
1066 1.54 jdolecek (fp->f_flag & FWRITE) ? "w" : "r",
1067 1.54 jdolecek (fp->f_flag & FNONBLOCK) ? "n" : "",
1068 1.54 jdolecek (cp.pipe_state & PIPE_ASYNC) ? "a" : "");
1069 1.72 christos (void)printf("\n");
1070 1.1 cgd return;
1071 1.1 cgd bad:
1072 1.72 christos (void)printf("* error\n");
1073 1.56 jdolecek }
1074 1.56 jdolecek
1075 1.72 christos static void
1076 1.84 christos misctrans(struct file *file)
1077 1.56 jdolecek {
1078 1.56 jdolecek
1079 1.86 gmcgarry PREFIX((int)file->f_type);
1080 1.84 christos pmisc(file, dtypes[file->f_type]);
1081 1.1 cgd }
1082 1.1 cgd
1083 1.1 cgd /*
1084 1.1 cgd * getinetproto --
1085 1.1 cgd * print name of protocol number
1086 1.1 cgd */
1087 1.72 christos static void
1088 1.72 christos getinetproto(int number)
1089 1.1 cgd {
1090 1.72 christos const char *cp;
1091 1.1 cgd
1092 1.26 mrg switch (number) {
1093 1.1 cgd case IPPROTO_IP:
1094 1.1 cgd cp = "ip"; break;
1095 1.1 cgd case IPPROTO_ICMP:
1096 1.1 cgd cp ="icmp"; break;
1097 1.1 cgd case IPPROTO_GGP:
1098 1.1 cgd cp ="ggp"; break;
1099 1.1 cgd case IPPROTO_TCP:
1100 1.1 cgd cp ="tcp"; break;
1101 1.1 cgd case IPPROTO_EGP:
1102 1.1 cgd cp ="egp"; break;
1103 1.1 cgd case IPPROTO_PUP:
1104 1.1 cgd cp ="pup"; break;
1105 1.1 cgd case IPPROTO_UDP:
1106 1.1 cgd cp ="udp"; break;
1107 1.1 cgd case IPPROTO_IDP:
1108 1.1 cgd cp ="idp"; break;
1109 1.1 cgd case IPPROTO_RAW:
1110 1.1 cgd cp ="raw"; break;
1111 1.38 itojun case IPPROTO_ICMPV6:
1112 1.38 itojun cp ="icmp6"; break;
1113 1.1 cgd default:
1114 1.72 christos (void)printf(" %d", number);
1115 1.1 cgd return;
1116 1.1 cgd }
1117 1.72 christos (void)printf(" %s", cp);
1118 1.1 cgd }
1119 1.1 cgd
1120 1.93 christos /*
1121 1.93 christos * getatproto --
1122 1.93 christos * print name of protocol number
1123 1.93 christos */
1124 1.93 christos static void
1125 1.93 christos getatproto(int number)
1126 1.93 christos {
1127 1.93 christos const char *cp;
1128 1.93 christos
1129 1.93 christos switch (number) {
1130 1.93 christos case ATPROTO_DDP:
1131 1.93 christos cp = "ddp"; break;
1132 1.93 christos case ATPROTO_AARP:
1133 1.93 christos cp ="aarp"; break;
1134 1.93 christos default:
1135 1.93 christos (void)printf(" %d", number);
1136 1.93 christos return;
1137 1.93 christos }
1138 1.93 christos (void)printf(" %s", cp);
1139 1.93 christos }
1140 1.93 christos
1141 1.72 christos static int
1142 1.72 christos getfname(const char *filename)
1143 1.1 cgd {
1144 1.1 cgd struct stat statbuf;
1145 1.1 cgd DEVS *cur;
1146 1.1 cgd
1147 1.1 cgd if (stat(filename, &statbuf)) {
1148 1.28 fair warn("stat(%s)", filename);
1149 1.72 christos return 0;
1150 1.1 cgd }
1151 1.91 christos if ((cur = malloc(sizeof(*cur))) == NULL) {
1152 1.91 christos err(1, "malloc(%zu)", sizeof(*cur));
1153 1.1 cgd }
1154 1.1 cgd cur->next = devs;
1155 1.1 cgd devs = cur;
1156 1.1 cgd
1157 1.1 cgd cur->ino = statbuf.st_ino;
1158 1.1 cgd cur->fsid = statbuf.st_dev & 0xffff;
1159 1.1 cgd cur->name = filename;
1160 1.72 christos return 1;
1161 1.40 jdolecek }
1162 1.40 jdolecek
1163 1.40 jdolecek mode_t
1164 1.72 christos getftype(enum vtype v_type)
1165 1.40 jdolecek {
1166 1.40 jdolecek mode_t ftype;
1167 1.40 jdolecek
1168 1.40 jdolecek switch (v_type) {
1169 1.40 jdolecek case VREG:
1170 1.40 jdolecek ftype = S_IFREG;
1171 1.40 jdolecek break;
1172 1.40 jdolecek case VDIR:
1173 1.40 jdolecek ftype = S_IFDIR;
1174 1.40 jdolecek break;
1175 1.40 jdolecek case VBLK:
1176 1.40 jdolecek ftype = S_IFBLK;
1177 1.40 jdolecek break;
1178 1.40 jdolecek case VCHR:
1179 1.40 jdolecek ftype = S_IFCHR;
1180 1.40 jdolecek break;
1181 1.40 jdolecek case VLNK:
1182 1.40 jdolecek ftype = S_IFLNK;
1183 1.40 jdolecek break;
1184 1.40 jdolecek case VSOCK:
1185 1.40 jdolecek ftype = S_IFSOCK;
1186 1.40 jdolecek break;
1187 1.40 jdolecek case VFIFO:
1188 1.40 jdolecek ftype = S_IFIFO;
1189 1.40 jdolecek break;
1190 1.40 jdolecek default:
1191 1.40 jdolecek ftype = 0;
1192 1.40 jdolecek break;
1193 1.40 jdolecek };
1194 1.40 jdolecek
1195 1.40 jdolecek return ftype;
1196 1.1 cgd }
1197 1.1 cgd
1198 1.72 christos static void
1199 1.70 christos usage(void)
1200 1.1 cgd {
1201 1.70 christos (void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
1202 1.70 christos "[-N system] [-M core] [file ...]\n", getprogname());
1203 1.70 christos exit(1);
1204 1.1 cgd }
1205