fstat.c revision 1.80 1 1.80 christos /* $NetBSD: fstat.c,v 1.80 2008/04/04 18:27:00 christos 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.23 lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
35 1.23 lukem The Regents of the University of California. All rights reserved.\n");
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.80 christos __RCSID("$NetBSD: fstat.c,v 1.80 2008/04/04 18:27:00 christos 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.38 itojun #include <netdb.h>
95 1.34 lukem #include <arpa/inet.h>
96 1.34 lukem
97 1.8 cgd #include <ctype.h>
98 1.1 cgd #include <errno.h>
99 1.8 cgd #include <kvm.h>
100 1.15 fvdl #include <limits.h>
101 1.1 cgd #include <nlist.h>
102 1.8 cgd #include <paths.h>
103 1.1 cgd #include <pwd.h>
104 1.1 cgd #include <stdio.h>
105 1.1 cgd #include <stdlib.h>
106 1.1 cgd #include <string.h>
107 1.23 lukem #include <unistd.h>
108 1.27 fair #include <err.h>
109 1.1 cgd
110 1.34 lukem #include "fstat.h"
111 1.34 lukem
112 1.1 cgd #define TEXT -1
113 1.1 cgd #define CDIR -2
114 1.1 cgd #define RDIR -3
115 1.1 cgd #define TRACE -4
116 1.1 cgd
117 1.1 cgd typedef struct devs {
118 1.1 cgd struct devs *next;
119 1.1 cgd long fsid;
120 1.1 cgd ino_t ino;
121 1.72 christos const char *name;
122 1.1 cgd } DEVS;
123 1.72 christos static DEVS *devs;
124 1.1 cgd
125 1.72 christos static int fsflg, /* show files on same filesystem as file(s) argument */
126 1.1 cgd pflg, /* show files open by a particular pid */
127 1.1 cgd uflg; /* show files open by a particular (effective) user */
128 1.72 christos static int checkfile; /* true if restricting to particular files or filesystems */
129 1.72 christos static int nflg; /* (numerical) display f.s. and rdev as dev_t */
130 1.1 cgd int vflg; /* display errors in locating kernel data objects etc... */
131 1.1 cgd
132 1.80 christos static fdfile_t **ofiles; /* buffer of pointers to file structures */
133 1.74 mrg static int fstat_maxfiles;
134 1.1 cgd #define ALLOC_OFILES(d) \
135 1.74 mrg if ((d) > fstat_maxfiles) { \
136 1.80 christos size_t len = (d) * sizeof(fdfile_t *); \
137 1.1 cgd free(ofiles); \
138 1.80 christos ofiles = malloc(len); \
139 1.1 cgd if (ofiles == NULL) { \
140 1.80 christos err(1, "malloc(%zu)", len); \
141 1.1 cgd } \
142 1.74 mrg fstat_maxfiles = (d); \
143 1.1 cgd }
144 1.1 cgd
145 1.8 cgd kvm_t *kd;
146 1.1 cgd
147 1.72 christos static void dofiles(struct kinfo_proc2 *);
148 1.72 christos static int ext2fs_filestat(struct vnode *, struct filestat *);
149 1.72 christos static int getfname(const char *);
150 1.72 christos static void getinetproto(int);
151 1.72 christos static char *getmnton(struct mount *);
152 1.72 christos static const char *layer_filestat(struct vnode *, struct filestat *);
153 1.72 christos static int msdosfs_filestat(struct vnode *, struct filestat *);
154 1.72 christos static int nfs_filestat(struct vnode *, struct filestat *);
155 1.38 itojun #ifdef INET6
156 1.72 christos static const char *inet6_addrstr(struct in6_addr *);
157 1.38 itojun #endif
158 1.72 christos static void socktrans(struct socket *, int);
159 1.72 christos static void kqueuetrans(void *, int);
160 1.72 christos static int ufs_filestat(struct vnode *, struct filestat *);
161 1.77 perry static void usage(void) __dead;
162 1.72 christos static const char *vfilestat(struct vnode *, struct filestat *);
163 1.72 christos static void vtrans(struct vnode *, int, int);
164 1.80 christos static void ftrans(fdfile_t *, int);
165 1.72 christos static void ptrans(struct file *, struct pipe *, int);
166 1.1 cgd
167 1.23 lukem int
168 1.72 christos main(int argc, char **argv)
169 1.1 cgd {
170 1.23 lukem struct passwd *passwd;
171 1.58 dsl struct kinfo_proc2 *p, *plast;
172 1.1 cgd int arg, ch, what;
173 1.8 cgd char *memf, *nlistf;
174 1.15 fvdl char buf[_POSIX2_LINE_MAX];
175 1.8 cgd int cnt;
176 1.26 mrg gid_t egid = getegid();
177 1.1 cgd
178 1.26 mrg (void)setegid(getgid());
179 1.1 cgd arg = 0;
180 1.8 cgd what = KERN_PROC_ALL;
181 1.8 cgd nlistf = memf = NULL;
182 1.22 lukem while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
183 1.1 cgd switch((char)ch) {
184 1.1 cgd case 'f':
185 1.1 cgd fsflg = 1;
186 1.1 cgd break;
187 1.8 cgd case 'M':
188 1.8 cgd memf = optarg;
189 1.8 cgd break;
190 1.8 cgd case 'N':
191 1.8 cgd nlistf = optarg;
192 1.8 cgd break;
193 1.1 cgd case 'n':
194 1.1 cgd nflg = 1;
195 1.1 cgd break;
196 1.1 cgd case 'p':
197 1.1 cgd if (pflg++)
198 1.1 cgd usage();
199 1.68 dsl if (!isdigit((unsigned char)*optarg)) {
200 1.34 lukem warnx("-p requires a process id");
201 1.1 cgd usage();
202 1.1 cgd }
203 1.8 cgd what = KERN_PROC_PID;
204 1.1 cgd arg = atoi(optarg);
205 1.1 cgd break;
206 1.1 cgd case 'u':
207 1.1 cgd if (uflg++)
208 1.1 cgd usage();
209 1.1 cgd if (!(passwd = getpwnam(optarg))) {
210 1.34 lukem errx(1, "%s: unknown uid", optarg);
211 1.1 cgd }
212 1.8 cgd what = KERN_PROC_UID;
213 1.1 cgd arg = passwd->pw_uid;
214 1.1 cgd break;
215 1.1 cgd case 'v':
216 1.1 cgd vflg = 1;
217 1.1 cgd break;
218 1.1 cgd case '?':
219 1.1 cgd default:
220 1.1 cgd usage();
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd if (*(argv += optind)) {
224 1.1 cgd for (; *argv; ++argv) {
225 1.1 cgd if (getfname(*argv))
226 1.1 cgd checkfile = 1;
227 1.1 cgd }
228 1.49 wiz if (!checkfile) /* file(s) specified, but none accessible */
229 1.1 cgd exit(1);
230 1.1 cgd }
231 1.1 cgd
232 1.1 cgd ALLOC_OFILES(256); /* reserve space for file pointers */
233 1.1 cgd
234 1.1 cgd if (fsflg && !checkfile) {
235 1.1 cgd /* -f with no files means use wd */
236 1.1 cgd if (getfname(".") == 0)
237 1.1 cgd exit(1);
238 1.1 cgd checkfile = 1;
239 1.1 cgd }
240 1.1 cgd
241 1.8 cgd /*
242 1.26 mrg * Discard setgid privileges. If not the running kernel, we toss
243 1.26 mrg * them away totally so that bad guys can't print interesting stuff
244 1.26 mrg * from kernel memory, otherwise switch back to kmem for the
245 1.26 mrg * duration of the kvm_openfiles() call.
246 1.8 cgd */
247 1.8 cgd if (nlistf != NULL || memf != NULL)
248 1.26 mrg (void)setgid(getgid());
249 1.26 mrg else
250 1.26 mrg (void)setegid(egid);
251 1.26 mrg
252 1.26 mrg if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
253 1.26 mrg errx(1, "%s", buf);
254 1.26 mrg
255 1.26 mrg /* get rid of it now anyway */
256 1.26 mrg if (nlistf == NULL && memf == NULL)
257 1.26 mrg (void)setgid(getgid());
258 1.8 cgd
259 1.58 dsl if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) {
260 1.34 lukem errx(1, "%s", kvm_geterr(kd));
261 1.1 cgd }
262 1.1 cgd if (nflg)
263 1.72 christos (void)printf("%s",
264 1.55 simonb "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
265 1.1 cgd else
266 1.72 christos (void)printf("%s",
267 1.55 simonb "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
268 1.1 cgd if (checkfile && fsflg == 0)
269 1.72 christos (void)printf(" NAME\n");
270 1.1 cgd else
271 1.72 christos (void)putchar('\n');
272 1.1 cgd
273 1.8 cgd for (plast = &p[cnt]; p < plast; ++p) {
274 1.58 dsl if (p->p_stat == SZOMB)
275 1.1 cgd continue;
276 1.1 cgd dofiles(p);
277 1.1 cgd }
278 1.72 christos return 0;
279 1.1 cgd }
280 1.1 cgd
281 1.72 christos static const char *Uname, *Comm;
282 1.34 lukem pid_t Pid;
283 1.1 cgd
284 1.72 christos #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
285 1.1 cgd switch(i) { \
286 1.1 cgd case TEXT: \
287 1.72 christos (void)printf(" text"); \
288 1.1 cgd break; \
289 1.1 cgd case CDIR: \
290 1.72 christos (void)printf(" wd"); \
291 1.1 cgd break; \
292 1.1 cgd case RDIR: \
293 1.72 christos (void)printf(" root"); \
294 1.1 cgd break; \
295 1.1 cgd case TRACE: \
296 1.72 christos (void)printf(" tr"); \
297 1.1 cgd break; \
298 1.1 cgd default: \
299 1.72 christos (void)printf(" %4d", i); \
300 1.1 cgd break; \
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd /*
304 1.1 cgd * print open files attributed to this process
305 1.1 cgd */
306 1.72 christos static void
307 1.72 christos dofiles(struct kinfo_proc2 *p)
308 1.1 cgd {
309 1.23 lukem int i;
310 1.78 ad struct filedesc filed;
311 1.35 thorpej struct cwdinfo cwdi;
312 1.1 cgd
313 1.58 dsl Uname = user_from_uid(p->p_uid, 0);
314 1.1 cgd Pid = p->p_pid;
315 1.1 cgd Comm = p->p_comm;
316 1.1 cgd
317 1.65 fvdl if (p->p_fd == 0 || p->p_cwdi == 0)
318 1.1 cgd return;
319 1.78 ad if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
320 1.59 martin warnx("can't read filedesc at %#llx for pid %d", (unsigned long long)p->p_fd, Pid);
321 1.13 mycroft return;
322 1.13 mycroft }
323 1.35 thorpej if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
324 1.59 martin warnx("can't read cwdinfo at %#llx for pid %d", (unsigned long long)p->p_cwdi, Pid);
325 1.35 thorpej return;
326 1.35 thorpej }
327 1.13 mycroft if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
328 1.13 mycroft filed.fd_freefile > filed.fd_lastfile + 1) {
329 1.59 martin dprintf("filedesc corrupted at %#llx for pid %d", (unsigned long long)p->p_fd, Pid);
330 1.1 cgd return;
331 1.1 cgd }
332 1.1 cgd /*
333 1.1 cgd * root directory vnode, if one
334 1.1 cgd */
335 1.35 thorpej if (cwdi.cwdi_rdir)
336 1.35 thorpej vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
337 1.1 cgd /*
338 1.1 cgd * current working directory vnode
339 1.1 cgd */
340 1.35 thorpej vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
341 1.80 christos #if 0
342 1.1 cgd /*
343 1.80 christos * Disable for now, since p->p_tracep appears to point to a ktr_desc *
344 1.1 cgd * ktrace vnode, if one
345 1.1 cgd */
346 1.1 cgd if (p->p_tracep)
347 1.58 dsl ftrans((struct file *)(intptr_t)p->p_tracep, TRACE);
348 1.80 christos #endif
349 1.1 cgd /*
350 1.1 cgd * open files
351 1.1 cgd */
352 1.80 christos #define FPSIZE (sizeof (fdfile_t *))
353 1.8 cgd ALLOC_OFILES(filed.fd_lastfile+1);
354 1.76 dyoung if (!KVM_READ(filed.fd_ofiles, ofiles,
355 1.76 dyoung (filed.fd_lastfile+1) * FPSIZE)) {
356 1.76 dyoung dprintf("can't read file structures at %p for pid %d",
357 1.76 dyoung filed.fd_ofiles, Pid);
358 1.76 dyoung return;
359 1.76 dyoung }
360 1.1 cgd for (i = 0; i <= filed.fd_lastfile; i++) {
361 1.1 cgd if (ofiles[i] == NULL)
362 1.1 cgd continue;
363 1.42 sommerfe ftrans(ofiles[i], i);
364 1.42 sommerfe }
365 1.42 sommerfe }
366 1.42 sommerfe
367 1.72 christos static void
368 1.80 christos ftrans(fdfile_t *fp, int i)
369 1.42 sommerfe {
370 1.42 sommerfe struct file file;
371 1.80 christos fdfile_t fdfile;
372 1.42 sommerfe
373 1.80 christos if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
374 1.42 sommerfe dprintf("can't read file %d at %p for pid %d",
375 1.42 sommerfe i, fp, Pid);
376 1.42 sommerfe return;
377 1.42 sommerfe }
378 1.80 christos if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
379 1.80 christos dprintf("can't read file %d at %p for pid %d",
380 1.80 christos i, fdfile.ff_file, Pid);
381 1.80 christos return;
382 1.80 christos }
383 1.48 lukem switch (file.f_type) {
384 1.48 lukem case DTYPE_VNODE:
385 1.42 sommerfe vtrans((struct vnode *)file.f_data, i, file.f_flag);
386 1.48 lukem break;
387 1.48 lukem case DTYPE_SOCKET:
388 1.42 sommerfe if (checkfile == 0)
389 1.42 sommerfe socktrans((struct socket *)file.f_data, i);
390 1.48 lukem break;
391 1.48 lukem case DTYPE_PIPE:
392 1.47 jdolecek if (checkfile == 0)
393 1.47 jdolecek ptrans(&file, (struct pipe *)file.f_data, i);
394 1.48 lukem break;
395 1.56 jdolecek case DTYPE_KQUEUE:
396 1.56 jdolecek if (checkfile == 0)
397 1.56 jdolecek kqueuetrans((void *)file.f_data, i);
398 1.56 jdolecek break;
399 1.48 lukem default:
400 1.42 sommerfe dprintf("unknown file type %d for file %d of pid %d",
401 1.42 sommerfe file.f_type, i, Pid);
402 1.48 lukem break;
403 1.1 cgd }
404 1.1 cgd }
405 1.1 cgd
406 1.72 christos static const char *
407 1.72 christos vfilestat(struct vnode *vp, struct filestat *fsp)
408 1.1 cgd {
409 1.72 christos const char *badtype = NULL;
410 1.1 cgd
411 1.44 enami if (vp->v_type == VNON || vp->v_tag == VT_NON)
412 1.1 cgd badtype = "none";
413 1.44 enami else if (vp->v_type == VBAD)
414 1.1 cgd badtype = "bad";
415 1.1 cgd else
416 1.44 enami switch (vp->v_tag) {
417 1.1 cgd case VT_UFS:
418 1.57 yamt case VT_LFS:
419 1.1 cgd case VT_MFS:
420 1.44 enami if (!ufs_filestat(vp, fsp))
421 1.8 cgd badtype = "error";
422 1.1 cgd break;
423 1.41 jdolecek case VT_MSDOSFS:
424 1.44 enami if (!msdosfs_filestat(vp, fsp))
425 1.41 jdolecek badtype = "error";
426 1.41 jdolecek break;
427 1.1 cgd case VT_NFS:
428 1.44 enami if (!nfs_filestat(vp, fsp))
429 1.8 cgd badtype = "error";
430 1.1 cgd break;
431 1.20 bouyer case VT_EXT2FS:
432 1.44 enami if (!ext2fs_filestat(vp, fsp))
433 1.20 bouyer badtype = "error";
434 1.20 bouyer break;
435 1.34 lukem case VT_ISOFS:
436 1.44 enami if (!isofs_filestat(vp, fsp))
437 1.37 jdolecek badtype = "error";
438 1.37 jdolecek break;
439 1.37 jdolecek case VT_NTFS:
440 1.44 enami if (!ntfs_filestat(vp, fsp))
441 1.34 lukem badtype = "error";
442 1.34 lukem break;
443 1.70 christos case VT_PTYFS:
444 1.70 christos if (!ptyfs_filestat(vp, fsp))
445 1.70 christos badtype = "error";
446 1.70 christos break;
447 1.73 christos case VT_TMPFS:
448 1.73 christos if (!tmpfs_filestat(vp, fsp))
449 1.73 christos badtype = "error";
450 1.73 christos break;
451 1.43 enami case VT_NULL:
452 1.43 enami case VT_OVERLAY:
453 1.43 enami case VT_UMAP:
454 1.44 enami badtype = layer_filestat(vp, fsp);
455 1.43 enami break;
456 1.1 cgd default: {
457 1.1 cgd static char unknown[10];
458 1.72 christos (void)snprintf(unknown, sizeof unknown,
459 1.44 enami "?(%x)", vp->v_tag);
460 1.72 christos badtype = unknown;
461 1.44 enami break;
462 1.1 cgd }
463 1.1 cgd }
464 1.44 enami return (badtype);
465 1.44 enami }
466 1.44 enami
467 1.72 christos static void
468 1.72 christos vtrans(struct vnode *vp, int i, int flag)
469 1.44 enami {
470 1.44 enami struct vnode vn;
471 1.44 enami struct filestat fst;
472 1.44 enami char mode[15], rw[3];
473 1.72 christos const char *badtype, *filename;
474 1.44 enami
475 1.44 enami filename = NULL;
476 1.44 enami if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
477 1.44 enami dprintf("can't read vnode at %p for pid %d", vp, Pid);
478 1.44 enami return;
479 1.44 enami }
480 1.44 enami badtype = vfilestat(&vn, &fst);
481 1.1 cgd if (checkfile) {
482 1.1 cgd int fsmatch = 0;
483 1.23 lukem DEVS *d;
484 1.1 cgd
485 1.1 cgd if (badtype)
486 1.1 cgd return;
487 1.1 cgd for (d = devs; d != NULL; d = d->next)
488 1.1 cgd if (d->fsid == fst.fsid) {
489 1.1 cgd fsmatch = 1;
490 1.1 cgd if (d->ino == fst.fileid) {
491 1.1 cgd filename = d->name;
492 1.1 cgd break;
493 1.1 cgd }
494 1.1 cgd }
495 1.1 cgd if (fsmatch == 0 || (filename == NULL && fsflg == 0))
496 1.1 cgd return;
497 1.1 cgd }
498 1.1 cgd PREFIX(i);
499 1.1 cgd if (badtype) {
500 1.1 cgd (void)printf(" - - %10s -\n", badtype);
501 1.1 cgd return;
502 1.1 cgd }
503 1.1 cgd if (nflg)
504 1.1 cgd (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
505 1.1 cgd else
506 1.1 cgd (void)printf(" %-8s", getmnton(vn.v_mount));
507 1.1 cgd if (nflg)
508 1.18 mrg (void)snprintf(mode, sizeof mode, "%o", fst.mode);
509 1.1 cgd else
510 1.1 cgd strmode(fst.mode, mode);
511 1.71 atatat (void)printf(" %7lu %*s", (unsigned long)fst.fileid, nflg ? 5 : 10, mode);
512 1.1 cgd switch (vn.v_type) {
513 1.1 cgd case VBLK:
514 1.1 cgd case VCHR: {
515 1.1 cgd char *name;
516 1.1 cgd
517 1.1 cgd if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
518 1.1 cgd S_IFCHR : S_IFBLK)) == NULL))
519 1.72 christos (void)printf(" %2d,%-2d", major(fst.rdev),
520 1.72 christos minor(fst.rdev));
521 1.1 cgd else
522 1.72 christos (void)printf(" %6s", name);
523 1.1 cgd break;
524 1.1 cgd }
525 1.1 cgd default:
526 1.72 christos (void)printf(" %6lld", (long long)fst.size);
527 1.1 cgd }
528 1.34 lukem rw[0] = '\0';
529 1.8 cgd if (flag & FREAD)
530 1.72 christos (void)strlcat(rw, "r", sizeof(rw));
531 1.8 cgd if (flag & FWRITE)
532 1.72 christos (void)strlcat(rw, "w", sizeof(rw));
533 1.72 christos (void)printf(" %-2s", rw);
534 1.1 cgd if (filename && !fsflg)
535 1.72 christos (void)printf(" %s", filename);
536 1.72 christos (void)putchar('\n');
537 1.1 cgd }
538 1.1 cgd
539 1.72 christos static int
540 1.72 christos ufs_filestat(struct vnode *vp, struct filestat *fsp)
541 1.1 cgd {
542 1.8 cgd struct inode inode;
543 1.62 fvdl union dinode {
544 1.62 fvdl struct ufs1_dinode dp1;
545 1.62 fvdl struct ufs2_dinode dp2;
546 1.62 fvdl } dip;
547 1.8 cgd
548 1.8 cgd if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
549 1.34 lukem dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
550 1.8 cgd return 0;
551 1.8 cgd }
552 1.62 fvdl
553 1.62 fvdl if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) {
554 1.62 fvdl dprintf("can't read dinode at %p for pid %d",
555 1.62 fvdl inode.i_din.ffs1_din, Pid);
556 1.62 fvdl return 0;
557 1.62 fvdl }
558 1.62 fvdl if (inode.i_size == dip.dp1.di_size)
559 1.62 fvdl fsp->rdev = dip.dp1.di_rdev;
560 1.62 fvdl else {
561 1.62 fvdl if (!KVM_READ(inode.i_din.ffs1_din, &dip,
562 1.62 fvdl sizeof(struct ufs2_dinode))) {
563 1.62 fvdl dprintf("can't read dinode at %p for pid %d",
564 1.62 fvdl inode.i_din.ffs1_din, Pid);
565 1.62 fvdl return 0;
566 1.62 fvdl }
567 1.62 fvdl fsp->rdev = dip.dp2.di_rdev;
568 1.62 fvdl }
569 1.62 fvdl
570 1.8 cgd fsp->fsid = inode.i_dev & 0xffff;
571 1.8 cgd fsp->fileid = (long)inode.i_number;
572 1.62 fvdl fsp->mode = (mode_t)inode.i_mode;
573 1.62 fvdl fsp->size = inode.i_size;
574 1.20 bouyer
575 1.20 bouyer return 1;
576 1.20 bouyer }
577 1.20 bouyer
578 1.72 christos static int
579 1.72 christos ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
580 1.20 bouyer {
581 1.20 bouyer struct inode inode;
582 1.66 aymeric u_int16_t mode;
583 1.66 aymeric u_int32_t size;
584 1.1 cgd
585 1.20 bouyer if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
586 1.34 lukem dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
587 1.20 bouyer return 0;
588 1.20 bouyer }
589 1.20 bouyer fsp->fsid = inode.i_dev & 0xffff;
590 1.20 bouyer fsp->fileid = (long)inode.i_number;
591 1.66 aymeric
592 1.66 aymeric if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) {
593 1.66 aymeric dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp),
594 1.66 aymeric &inode.i_e2fs_mode, Pid);
595 1.66 aymeric return 0;
596 1.66 aymeric }
597 1.66 aymeric fsp->mode = mode;
598 1.66 aymeric
599 1.66 aymeric if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) {
600 1.66 aymeric dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp),
601 1.66 aymeric &inode.i_e2fs_size, Pid);
602 1.66 aymeric return 0;
603 1.66 aymeric }
604 1.66 aymeric fsp->size = size;
605 1.20 bouyer fsp->rdev = 0; /* XXX */
606 1.8 cgd return 1;
607 1.1 cgd }
608 1.1 cgd
609 1.72 christos static int
610 1.72 christos nfs_filestat(struct vnode *vp, struct filestat *fsp)
611 1.1 cgd {
612 1.8 cgd struct nfsnode nfsnode;
613 1.24 thorpej struct vattr va;
614 1.1 cgd
615 1.8 cgd if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
616 1.34 lukem dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
617 1.34 lukem Pid);
618 1.8 cgd return 0;
619 1.8 cgd }
620 1.24 thorpej if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
621 1.34 lukem dprintf("can't read vnode attributes at %p for pid %d",
622 1.34 lukem nfsnode.n_vattr, Pid);
623 1.24 thorpej return 0;
624 1.24 thorpej }
625 1.24 thorpej fsp->fsid = va.va_fsid;
626 1.24 thorpej fsp->fileid = va.va_fileid;
627 1.8 cgd fsp->size = nfsnode.n_size;
628 1.24 thorpej fsp->rdev = va.va_rdev;
629 1.40 jdolecek fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
630 1.8 cgd
631 1.8 cgd return 1;
632 1.1 cgd }
633 1.1 cgd
634 1.72 christos static int
635 1.72 christos msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
636 1.41 jdolecek {
637 1.41 jdolecek struct denode de;
638 1.41 jdolecek struct msdosfsmount mp;
639 1.41 jdolecek
640 1.41 jdolecek if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
641 1.41 jdolecek dprintf("can't read denode at %p for pid %d", VTONFS(vp),
642 1.41 jdolecek Pid);
643 1.41 jdolecek return 0;
644 1.41 jdolecek }
645 1.41 jdolecek if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
646 1.41 jdolecek dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
647 1.41 jdolecek Pid);
648 1.41 jdolecek return 0;
649 1.41 jdolecek }
650 1.41 jdolecek
651 1.41 jdolecek fsp->fsid = de.de_dev & 0xffff;
652 1.41 jdolecek fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
653 1.41 jdolecek fsp->size = de.de_FileSize;
654 1.41 jdolecek fsp->rdev = 0; /* msdosfs doesn't support device files */
655 1.41 jdolecek fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
656 1.41 jdolecek return 1;
657 1.43 enami }
658 1.43 enami
659 1.72 christos static const char *
660 1.72 christos layer_filestat(struct vnode *vp, struct filestat *fsp)
661 1.43 enami {
662 1.43 enami struct layer_node layer_node;
663 1.44 enami struct mount mount;
664 1.44 enami struct vnode vn;
665 1.72 christos const char *badtype;
666 1.43 enami
667 1.43 enami if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
668 1.43 enami dprintf("can't read layer_node at %p for pid %d",
669 1.43 enami VTOLAYER(vp), Pid);
670 1.44 enami return ("error");
671 1.44 enami }
672 1.44 enami if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
673 1.44 enami dprintf("can't read mount struct at %p for pid %d",
674 1.44 enami vp->v_mount, Pid);
675 1.44 enami return ("error");
676 1.44 enami }
677 1.44 enami vp = layer_node.layer_lowervp;
678 1.44 enami if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
679 1.44 enami dprintf("can't read vnode at %p for pid %d", vp, Pid);
680 1.44 enami return ("error");
681 1.43 enami }
682 1.44 enami if ((badtype = vfilestat(&vn, fsp)) == NULL)
683 1.67 christos fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
684 1.44 enami return (badtype);
685 1.41 jdolecek }
686 1.1 cgd
687 1.72 christos static char *
688 1.72 christos getmnton(struct mount *m)
689 1.1 cgd {
690 1.1 cgd static struct mount mount;
691 1.1 cgd static struct mtab {
692 1.1 cgd struct mtab *next;
693 1.1 cgd struct mount *m;
694 1.1 cgd char mntonname[MNAMELEN];
695 1.1 cgd } *mhead = NULL;
696 1.23 lukem struct mtab *mt;
697 1.1 cgd
698 1.1 cgd for (mt = mhead; mt != NULL; mt = mt->next)
699 1.1 cgd if (m == mt->m)
700 1.1 cgd return (mt->mntonname);
701 1.1 cgd if (!KVM_READ(m, &mount, sizeof(struct mount))) {
702 1.34 lukem warnx("can't read mount table at %p", m);
703 1.1 cgd return (NULL);
704 1.1 cgd }
705 1.1 cgd if ((mt = malloc(sizeof (struct mtab))) == NULL) {
706 1.30 ross err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
707 1.1 cgd }
708 1.1 cgd mt->m = m;
709 1.72 christos (void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
710 1.72 christos MNAMELEN);
711 1.1 cgd mt->next = mhead;
712 1.1 cgd mhead = mt;
713 1.1 cgd return (mt->mntonname);
714 1.1 cgd }
715 1.1 cgd
716 1.38 itojun #ifdef INET6
717 1.38 itojun static const char *
718 1.72 christos inet6_addrstr(struct in6_addr *p)
719 1.38 itojun {
720 1.38 itojun struct sockaddr_in6 sin6;
721 1.38 itojun static char hbuf[NI_MAXHOST];
722 1.39 jdolecek const int niflags = NI_NUMERICHOST;
723 1.38 itojun
724 1.72 christos (void)memset(&sin6, 0, sizeof(sin6));
725 1.38 itojun sin6.sin6_family = AF_INET6;
726 1.38 itojun sin6.sin6_len = sizeof(struct sockaddr_in6);
727 1.38 itojun sin6.sin6_addr = *p;
728 1.38 itojun if (IN6_IS_ADDR_LINKLOCAL(p) &&
729 1.38 itojun *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
730 1.38 itojun sin6.sin6_scope_id =
731 1.38 itojun ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
732 1.38 itojun sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
733 1.38 itojun }
734 1.38 itojun
735 1.38 itojun if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
736 1.38 itojun hbuf, sizeof(hbuf), NULL, 0, niflags))
737 1.38 itojun return "invalid";
738 1.38 itojun
739 1.38 itojun return hbuf;
740 1.38 itojun }
741 1.38 itojun #endif
742 1.38 itojun
743 1.72 christos static void
744 1.72 christos socktrans(struct socket *sock, int i)
745 1.1 cgd {
746 1.72 christos static const char *stypename[] = {
747 1.1 cgd "unused", /* 0 */
748 1.1 cgd "stream", /* 1 */
749 1.1 cgd "dgram", /* 2 */
750 1.1 cgd "raw", /* 3 */
751 1.1 cgd "rdm", /* 4 */
752 1.1 cgd "seqpak" /* 5 */
753 1.1 cgd };
754 1.1 cgd #define STYPEMAX 5
755 1.1 cgd struct socket so;
756 1.1 cgd struct protosw proto;
757 1.1 cgd struct domain dom;
758 1.1 cgd struct inpcb inpcb;
759 1.38 itojun #ifdef INET6
760 1.38 itojun struct in6pcb in6pcb;
761 1.38 itojun #endif
762 1.1 cgd struct unpcb unpcb;
763 1.1 cgd int len;
764 1.18 mrg char dname[32];
765 1.38 itojun #ifdef INET6
766 1.38 itojun char xaddrbuf[NI_MAXHOST + 2];
767 1.38 itojun #endif
768 1.1 cgd
769 1.1 cgd PREFIX(i);
770 1.1 cgd
771 1.1 cgd /* fill in socket */
772 1.1 cgd if (!KVM_READ(sock, &so, sizeof(struct socket))) {
773 1.34 lukem dprintf("can't read sock at %p", sock);
774 1.1 cgd goto bad;
775 1.1 cgd }
776 1.1 cgd
777 1.1 cgd /* fill in protosw entry */
778 1.1 cgd if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
779 1.34 lukem dprintf("can't read protosw at %p", so.so_proto);
780 1.1 cgd goto bad;
781 1.1 cgd }
782 1.1 cgd
783 1.1 cgd /* fill in domain */
784 1.1 cgd if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
785 1.34 lukem dprintf("can't read domain at %p", proto.pr_domain);
786 1.1 cgd goto bad;
787 1.1 cgd }
788 1.1 cgd
789 1.8 cgd if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
790 1.25 msaitoh sizeof(dname) - 1)) != sizeof(dname) -1) {
791 1.34 lukem dprintf("can't read domain name at %p", dom.dom_name);
792 1.1 cgd dname[0] = '\0';
793 1.1 cgd }
794 1.1 cgd else
795 1.1 cgd dname[len] = '\0';
796 1.1 cgd
797 1.1 cgd if ((u_short)so.so_type > STYPEMAX)
798 1.72 christos (void)printf("* %s ?%d", dname, so.so_type);
799 1.1 cgd else
800 1.72 christos (void)printf("* %s %s", dname, stypename[so.so_type]);
801 1.1 cgd
802 1.1 cgd /*
803 1.1 cgd * protocol specific formatting
804 1.1 cgd *
805 1.34 lukem * Try to find interesting things to print. For TCP, the interesting
806 1.34 lukem * thing is the address of the tcpcb, for UDP and others, just the
807 1.34 lukem * inpcb (socket pcb). For UNIX domain, its the address of the socket
808 1.1 cgd * pcb and the address of the connected pcb (if connected). Otherwise
809 1.1 cgd * just print the protocol number and address of the socket itself.
810 1.1 cgd * The idea is not to duplicate netstat, but to make available enough
811 1.1 cgd * information for further analysis.
812 1.1 cgd */
813 1.1 cgd switch(dom.dom_family) {
814 1.1 cgd case AF_INET:
815 1.1 cgd getinetproto(proto.pr_protocol);
816 1.34 lukem if (proto.pr_protocol == IPPROTO_TCP) {
817 1.34 lukem if (so.so_pcb == NULL)
818 1.34 lukem break;
819 1.34 lukem if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
820 1.34 lukem sizeof(struct inpcb)) != sizeof(struct inpcb)) {
821 1.34 lukem dprintf("can't read inpcb at %p", so.so_pcb);
822 1.34 lukem goto bad;
823 1.34 lukem }
824 1.72 christos (void)printf(" %lx", (long)inpcb.inp_ppcb);
825 1.72 christos (void)printf(" %s:%d",
826 1.34 lukem inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
827 1.34 lukem inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
828 1.34 lukem if (inpcb.inp_fport) {
829 1.72 christos (void)printf(" <-> %s:%d",
830 1.34 lukem inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
831 1.34 lukem inet_ntoa(inpcb.inp_faddr),
832 1.34 lukem ntohs(inpcb.inp_fport));
833 1.34 lukem }
834 1.34 lukem } else if (proto.pr_protocol == IPPROTO_UDP) {
835 1.34 lukem if (so.so_pcb == NULL)
836 1.34 lukem break;
837 1.34 lukem if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
838 1.34 lukem sizeof(struct inpcb)) != sizeof(struct inpcb)) {
839 1.34 lukem dprintf("can't read inpcb at %p", so.so_pcb);
840 1.34 lukem goto bad;
841 1.1 cgd }
842 1.72 christos (void)printf(" %lx", (long)so.so_pcb);
843 1.72 christos (void)printf(" %s:%d",
844 1.34 lukem inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
845 1.34 lukem inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
846 1.34 lukem if (inpcb.inp_fport)
847 1.72 christos (void)printf(" <-> %s:%d",
848 1.34 lukem inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
849 1.34 lukem inet_ntoa(inpcb.inp_faddr),
850 1.34 lukem ntohs(inpcb.inp_fport));
851 1.34 lukem } else if (so.so_pcb)
852 1.72 christos (void)printf(" %lx", (long)so.so_pcb);
853 1.1 cgd break;
854 1.38 itojun #ifdef INET6
855 1.38 itojun case AF_INET6:
856 1.38 itojun getinetproto(proto.pr_protocol);
857 1.38 itojun if (proto.pr_protocol == IPPROTO_TCP) {
858 1.38 itojun if (so.so_pcb == NULL)
859 1.38 itojun break;
860 1.38 itojun if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
861 1.38 itojun sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
862 1.38 itojun dprintf("can't read in6pcb at %p", so.so_pcb);
863 1.38 itojun goto bad;
864 1.38 itojun }
865 1.72 christos (void)printf(" %lx", (long)in6pcb.in6p_ppcb);
866 1.72 christos (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
867 1.38 itojun inet6_addrstr(&in6pcb.in6p_laddr));
868 1.72 christos (void)printf(" %s:%d",
869 1.38 itojun IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
870 1.38 itojun xaddrbuf,
871 1.38 itojun ntohs(in6pcb.in6p_lport));
872 1.38 itojun if (in6pcb.in6p_fport) {
873 1.72 christos (void)snprintf(xaddrbuf, sizeof(xaddrbuf),
874 1.72 christos "[%s]", inet6_addrstr(&in6pcb.in6p_faddr));
875 1.72 christos (void)printf(" <-> %s:%d",
876 1.38 itojun IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
877 1.38 itojun xaddrbuf,
878 1.38 itojun ntohs(in6pcb.in6p_fport));
879 1.38 itojun }
880 1.38 itojun } else if (proto.pr_protocol == IPPROTO_UDP) {
881 1.38 itojun if (so.so_pcb == NULL)
882 1.38 itojun break;
883 1.38 itojun if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
884 1.38 itojun sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
885 1.38 itojun dprintf("can't read inpcb at %p", so.so_pcb);
886 1.38 itojun goto bad;
887 1.38 itojun }
888 1.72 christos (void)printf(" %lx", (long)so.so_pcb);
889 1.72 christos (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
890 1.38 itojun inet6_addrstr(&in6pcb.in6p_laddr));
891 1.72 christos (void)printf(" %s:%d",
892 1.38 itojun IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
893 1.38 itojun xaddrbuf,
894 1.38 itojun ntohs(in6pcb.in6p_lport));
895 1.38 itojun if (in6pcb.in6p_fport) {
896 1.72 christos (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
897 1.38 itojun inet6_addrstr(&in6pcb.in6p_faddr));
898 1.72 christos (void)printf(" <-> %s:%d",
899 1.38 itojun IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
900 1.38 itojun xaddrbuf,
901 1.38 itojun ntohs(in6pcb.in6p_fport));
902 1.38 itojun }
903 1.38 itojun } else if (so.so_pcb)
904 1.72 christos (void)printf(" %lx", (long)so.so_pcb);
905 1.38 itojun break;
906 1.38 itojun #endif
907 1.31 lukem case AF_LOCAL:
908 1.1 cgd /* print address of pcb and connected pcb */
909 1.1 cgd if (so.so_pcb) {
910 1.72 christos (void)printf(" %lx", (long)so.so_pcb);
911 1.8 cgd if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
912 1.1 cgd sizeof(struct unpcb)) != sizeof(struct unpcb)){
913 1.34 lukem dprintf("can't read unpcb at %p", so.so_pcb);
914 1.1 cgd goto bad;
915 1.1 cgd }
916 1.1 cgd if (unpcb.unp_conn) {
917 1.1 cgd char shoconn[4], *cp;
918 1.1 cgd
919 1.1 cgd cp = shoconn;
920 1.1 cgd if (!(so.so_state & SS_CANTRCVMORE))
921 1.1 cgd *cp++ = '<';
922 1.1 cgd *cp++ = '-';
923 1.1 cgd if (!(so.so_state & SS_CANTSENDMORE))
924 1.1 cgd *cp++ = '>';
925 1.1 cgd *cp = '\0';
926 1.72 christos (void)printf(" %s %lx", shoconn,
927 1.12 cgd (long)unpcb.unp_conn);
928 1.1 cgd }
929 1.1 cgd }
930 1.1 cgd break;
931 1.1 cgd default:
932 1.1 cgd /* print protocol number and socket address */
933 1.72 christos (void)printf(" %d %lx", proto.pr_protocol, (long)sock);
934 1.1 cgd }
935 1.72 christos (void)printf("\n");
936 1.47 jdolecek return;
937 1.47 jdolecek bad:
938 1.72 christos (void)printf("* error\n");
939 1.47 jdolecek }
940 1.47 jdolecek
941 1.72 christos static void
942 1.72 christos ptrans(struct file *fp, struct pipe *cpipe, int i)
943 1.47 jdolecek {
944 1.47 jdolecek struct pipe cp;
945 1.47 jdolecek
946 1.47 jdolecek PREFIX(i);
947 1.47 jdolecek
948 1.50 jdolecek /* fill in pipe */
949 1.47 jdolecek if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
950 1.47 jdolecek dprintf("can't read pipe at %p", cpipe);
951 1.47 jdolecek goto bad;
952 1.47 jdolecek }
953 1.47 jdolecek
954 1.52 jdolecek /* pipe descriptor is either read or write, never both */
955 1.72 christos (void)printf("* pipe %p %s %p %s%s%s", cpipe,
956 1.51 jdolecek (fp->f_flag & FWRITE) ? "->" : "<-",
957 1.52 jdolecek cp.pipe_peer,
958 1.54 jdolecek (fp->f_flag & FWRITE) ? "w" : "r",
959 1.54 jdolecek (fp->f_flag & FNONBLOCK) ? "n" : "",
960 1.54 jdolecek (cp.pipe_state & PIPE_ASYNC) ? "a" : "");
961 1.72 christos (void)printf("\n");
962 1.1 cgd return;
963 1.1 cgd bad:
964 1.72 christos (void)printf("* error\n");
965 1.56 jdolecek }
966 1.56 jdolecek
967 1.72 christos static void
968 1.72 christos kqueuetrans(void *kq, int i)
969 1.56 jdolecek {
970 1.56 jdolecek
971 1.56 jdolecek PREFIX(i);
972 1.72 christos (void)printf("* kqueue %lx", (long)kq);
973 1.72 christos (void)printf("\n");
974 1.1 cgd }
975 1.1 cgd
976 1.1 cgd /*
977 1.1 cgd * getinetproto --
978 1.1 cgd * print name of protocol number
979 1.1 cgd */
980 1.72 christos static void
981 1.72 christos getinetproto(int number)
982 1.1 cgd {
983 1.72 christos const char *cp;
984 1.1 cgd
985 1.26 mrg switch (number) {
986 1.1 cgd case IPPROTO_IP:
987 1.1 cgd cp = "ip"; break;
988 1.1 cgd case IPPROTO_ICMP:
989 1.1 cgd cp ="icmp"; break;
990 1.1 cgd case IPPROTO_GGP:
991 1.1 cgd cp ="ggp"; break;
992 1.1 cgd case IPPROTO_TCP:
993 1.1 cgd cp ="tcp"; break;
994 1.1 cgd case IPPROTO_EGP:
995 1.1 cgd cp ="egp"; break;
996 1.1 cgd case IPPROTO_PUP:
997 1.1 cgd cp ="pup"; break;
998 1.1 cgd case IPPROTO_UDP:
999 1.1 cgd cp ="udp"; break;
1000 1.1 cgd case IPPROTO_IDP:
1001 1.1 cgd cp ="idp"; break;
1002 1.1 cgd case IPPROTO_RAW:
1003 1.1 cgd cp ="raw"; break;
1004 1.38 itojun case IPPROTO_ICMPV6:
1005 1.38 itojun cp ="icmp6"; break;
1006 1.1 cgd default:
1007 1.72 christos (void)printf(" %d", number);
1008 1.1 cgd return;
1009 1.1 cgd }
1010 1.72 christos (void)printf(" %s", cp);
1011 1.1 cgd }
1012 1.1 cgd
1013 1.72 christos static int
1014 1.72 christos getfname(const char *filename)
1015 1.1 cgd {
1016 1.1 cgd struct stat statbuf;
1017 1.1 cgd DEVS *cur;
1018 1.1 cgd
1019 1.1 cgd if (stat(filename, &statbuf)) {
1020 1.28 fair warn("stat(%s)", filename);
1021 1.72 christos return 0;
1022 1.1 cgd }
1023 1.1 cgd if ((cur = malloc(sizeof(DEVS))) == NULL) {
1024 1.30 ross err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
1025 1.1 cgd }
1026 1.1 cgd cur->next = devs;
1027 1.1 cgd devs = cur;
1028 1.1 cgd
1029 1.1 cgd cur->ino = statbuf.st_ino;
1030 1.1 cgd cur->fsid = statbuf.st_dev & 0xffff;
1031 1.1 cgd cur->name = filename;
1032 1.72 christos return 1;
1033 1.40 jdolecek }
1034 1.40 jdolecek
1035 1.40 jdolecek mode_t
1036 1.72 christos getftype(enum vtype v_type)
1037 1.40 jdolecek {
1038 1.40 jdolecek mode_t ftype;
1039 1.40 jdolecek
1040 1.40 jdolecek switch (v_type) {
1041 1.40 jdolecek case VREG:
1042 1.40 jdolecek ftype = S_IFREG;
1043 1.40 jdolecek break;
1044 1.40 jdolecek case VDIR:
1045 1.40 jdolecek ftype = S_IFDIR;
1046 1.40 jdolecek break;
1047 1.40 jdolecek case VBLK:
1048 1.40 jdolecek ftype = S_IFBLK;
1049 1.40 jdolecek break;
1050 1.40 jdolecek case VCHR:
1051 1.40 jdolecek ftype = S_IFCHR;
1052 1.40 jdolecek break;
1053 1.40 jdolecek case VLNK:
1054 1.40 jdolecek ftype = S_IFLNK;
1055 1.40 jdolecek break;
1056 1.40 jdolecek case VSOCK:
1057 1.40 jdolecek ftype = S_IFSOCK;
1058 1.40 jdolecek break;
1059 1.40 jdolecek case VFIFO:
1060 1.40 jdolecek ftype = S_IFIFO;
1061 1.40 jdolecek break;
1062 1.40 jdolecek default:
1063 1.40 jdolecek ftype = 0;
1064 1.40 jdolecek break;
1065 1.40 jdolecek };
1066 1.40 jdolecek
1067 1.40 jdolecek return ftype;
1068 1.1 cgd }
1069 1.1 cgd
1070 1.72 christos static void
1071 1.70 christos usage(void)
1072 1.1 cgd {
1073 1.70 christos (void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
1074 1.70 christos "[-N system] [-M core] [file ...]\n", getprogname());
1075 1.70 christos exit(1);
1076 1.1 cgd }
1077