Annotation of src/usr.bin/fstat/fstat.c, Revision 1.80
1.80 ! deraadt 1: /* $OpenBSD: fstat.c,v 1.79 2014/08/20 11:23:42 mikeb Exp $ */
1.65 millert 2:
3: /*
4: * Copyright (c) 2009 Todd C. Miller <Todd.Miller@courtesan.com>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
1.2 niklas 18:
1.1 deraadt 19: /*-
20: * Copyright (c) 1988, 1993
21: * The Regents of the University of California. All rights reserved.
22: *
23: * Redistribution and use in source and binary forms, with or without
24: * modification, are permitted provided that the following conditions
25: * are met:
26: * 1. Redistributions of source code must retain the above copyright
27: * notice, this list of conditions and the following disclaimer.
28: * 2. Redistributions in binary form must reproduce the above copyright
29: * notice, this list of conditions and the following disclaimer in the
30: * documentation and/or other materials provided with the distribution.
1.43 millert 31: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 32: * may be used to endorse or promote products derived from this software
33: * without specific prior written permission.
34: *
35: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45: * SUCH DAMAGE.
46: */
47:
1.80 ! deraadt 48: #include <sys/types.h>
1.68 millert 49: #include <sys/queue.h>
50: #include <sys/mount.h>
1.1 deraadt 51: #include <sys/stat.h>
52: #include <sys/vnode.h>
53: #include <sys/socket.h>
54: #include <sys/socketvar.h>
1.37 deraadt 55: #include <sys/eventvar.h>
1.1 deraadt 56: #include <sys/sysctl.h>
1.65 millert 57: #define _KERNEL /* for DTYPE_* */
1.1 deraadt 58: #include <sys/file.h>
59: #undef _KERNEL
1.61 thib 60:
1.1 deraadt 61: #include <net/route.h>
62: #include <netinet/in.h>
1.26 itojun 63:
64: #include <netdb.h>
1.5 deraadt 65: #include <arpa/inet.h>
66:
1.22 art 67: #include <sys/pipe.h>
68:
1.1 deraadt 69: #include <ctype.h>
70: #include <errno.h>
1.65 millert 71: #include <fcntl.h>
1.1 deraadt 72: #include <kvm.h>
1.2 niklas 73: #include <limits.h>
1.1 deraadt 74: #include <nlist.h>
75: #include <pwd.h>
1.72 guenther 76: #include <search.h>
1.68 millert 77: #include <signal.h>
1.1 deraadt 78: #include <stdio.h>
1.65 millert 79: #include <stdint.h>
1.1 deraadt 80: #include <stdlib.h>
81: #include <string.h>
1.11 millert 82: #include <unistd.h>
1.17 mickey 83: #include <err.h>
1.1 deraadt 84:
1.68 millert 85: #include "fstat.h"
86:
1.80 ! deraadt 87: #define MAXIMUM(a, b) (((a) > (b)) ? (a) : (b))
! 88:
1.68 millert 89: struct fileargs fileargs = SLIST_HEAD_INITIALIZER(fileargs);
1.1 deraadt 90:
1.46 deraadt 91: int fsflg; /* show files on same filesystem as file(s) argument */
92: int pflg; /* show files open by a particular pid */
93: int uflg; /* show files open by a particular (effective) user */
1.38 deraadt 94: int checkfile; /* true if restricting to particular files or filesystems */
1.1 deraadt 95: int nflg; /* (numerical) display f.s. and rdev as dev_t */
1.28 hugh 96: int oflg; /* display file offset */
1.56 mickey 97: int sflg; /* display file xfer/bytes counters */
1.1 deraadt 98: int vflg; /* display errors in locating kernel data objects etc... */
1.68 millert 99: int cflg; /* fuser only */
100:
101: int fuser; /* 1 if we are fuser, 0 if we are fstat */
102: int signo; /* signal to send (fuser only) */
1.1 deraadt 103:
104: kvm_t *kd;
1.62 deraadt 105: uid_t uid;
1.1 deraadt 106:
1.76 guenther 107: void fstat_dofile(struct kinfo_file *);
1.68 millert 108: void fstat_header(void);
1.35 millert 109: void getinetproto(int);
110: void usage(void);
111: int getfname(char *);
1.76 guenther 112: void kqueuetrans(struct kinfo_file *);
113: void pipetrans(struct kinfo_file *);
114: struct kinfo_file *splice_find(char, u_int64_t);
115: void splice_insert(char, u_int64_t, struct kinfo_file *);
116: void find_splices(struct kinfo_file *, int);
117: void print_inet_details(struct kinfo_file *);
1.72 guenther 118: #ifdef INET6
1.76 guenther 119: void print_inet6_details(struct kinfo_file *);
1.72 guenther 120: #endif
1.76 guenther 121: void print_sock_details(struct kinfo_file *);
122: void socktrans(struct kinfo_file *);
123: void systracetrans(struct kinfo_file *);
124: void vtrans(struct kinfo_file *);
1.47 deraadt 125: const char *inet6_addrstr(struct in6_addr *);
1.68 millert 126: int signame_to_signum(char *);
1.74 deraadt 127: void hide(void *p);
128:
129: int hideroot;
130:
131: void
132: hide(void *p)
133: {
134: printf("%p", hideroot ? NULL : p);
135: }
1.1 deraadt 136:
1.11 millert 137: int
1.40 deraadt 138: main(int argc, char *argv[])
1.1 deraadt 139: {
1.32 mpech 140: struct passwd *passwd;
1.76 guenther 141: struct kinfo_file *kf, *kflast;
1.1 deraadt 142: int arg, ch, what;
1.68 millert 143: char *memf, *nlistf, *optstr;
1.2 niklas 144: char buf[_POSIX2_LINE_MAX];
1.58 tedu 145: const char *errstr;
1.65 millert 146: int cnt, flags;
1.1 deraadt 147:
1.74 deraadt 148: hideroot = getuid();
149:
1.65 millert 150: arg = -1;
151: what = KERN_FILE_BYPID;
1.1 deraadt 152: nlistf = memf = NULL;
1.28 hugh 153: oflg = 0;
1.68 millert 154:
155: /* are we fstat(1) or fuser(1)? */
156: if (strcmp(__progname, "fuser") == 0) {
157: fuser = 1;
158: optstr = "cfks:uM:N:";
159: } else {
160: fuser = 0;
161: optstr = "fnop:su:vN:M:";
162: }
163:
164: /*
165: * fuser and fstat share three flags: -f, -s and -u. In both cases
166: * -f is a boolean, but for -u fstat wants an argument while fuser
167: * does not and for -s fuser wants an argument whereas fstat does not.
168: */
169: while ((ch = getopt(argc, argv, optstr)) != -1)
1.60 sobrado 170: switch ((char)ch) {
1.68 millert 171: case 'c':
172: if (fsflg)
173: usage();
174: cflg = 1;
175: break;
1.1 deraadt 176: case 'f':
1.68 millert 177: if (cflg)
178: usage();
1.1 deraadt 179: fsflg = 1;
180: break;
1.68 millert 181: case 'k':
182: sflg = 1;
183: signo = SIGKILL;
184: break;
1.1 deraadt 185: case 'M':
186: memf = optarg;
187: break;
188: case 'N':
189: nlistf = optarg;
190: break;
191: case 'n':
192: nflg = 1;
193: break;
1.28 hugh 194: case 'o':
195: oflg = 1;
196: break;
1.1 deraadt 197: case 'p':
198: if (pflg++)
199: usage();
1.58 tedu 200: arg = strtonum(optarg, 0, INT_MAX, &errstr);
201: if (errstr != NULL) {
1.74 deraadt 202: warnx("-p requires a process id, %s: %s",
1.58 tedu 203: errstr, optarg);
1.1 deraadt 204: usage();
205: }
1.65 millert 206: what = KERN_FILE_BYPID;
1.1 deraadt 207: break;
1.56 mickey 208: case 's':
209: sflg = 1;
1.68 millert 210: if (fuser) {
211: signo = signame_to_signum(optarg);
212: if (signo == -1) {
213: warnx("invalid signal %s", optarg);
214: usage();
215: }
216: }
1.56 mickey 217: break;
1.1 deraadt 218: case 'u':
219: if (uflg++)
220: usage();
1.68 millert 221: if (!fuser) {
1.69 nicm 222: if (!(passwd = getpwnam(optarg))) {
1.74 deraadt 223: arg = strtonum(optarg, 0, UID_MAX,
1.69 nicm 224: &errstr);
225: if (errstr != NULL) {
226: errx(1, "%s: unknown uid",
227: optarg);
228: }
229: } else
230: arg = passwd->pw_uid;
1.68 millert 231: what = KERN_FILE_BYUID;
232: }
1.1 deraadt 233: break;
234: case 'v':
235: vflg = 1;
236: break;
237: default:
238: usage();
239: }
240:
1.41 deraadt 241: /*
1.62 deraadt 242: * get the uid, for oflg and sflg
243: */
244: uid = getuid();
245:
246: /*
1.65 millert 247: * Use sysctl unless inspecting an alternate kernel.
1.41 deraadt 248: */
1.65 millert 249: if (nlistf == NULL || memf == NULL)
250: flags = KVM_NO_FILES;
251: else
252: flags = O_RDONLY;
1.41 deraadt 253:
1.65 millert 254: if ((kd = kvm_openfiles(nlistf, memf, NULL, flags, buf)) == NULL)
1.41 deraadt 255: errx(1, "%s", buf);
256:
1.1 deraadt 257: if (*(argv += optind)) {
258: for (; *argv; ++argv) {
259: if (getfname(*argv))
260: checkfile = 1;
261: }
1.68 millert 262: /* file(s) specified, but none accessible */
263: if (!checkfile)
1.1 deraadt 264: exit(1);
1.68 millert 265: } else if (fuser)
266: usage();
1.1 deraadt 267:
1.68 millert 268: if (!fuser && fsflg && !checkfile) {
269: /* fstat -f with no files means use wd */
1.1 deraadt 270: if (getfname(".") == 0)
271: exit(1);
272: checkfile = 1;
273: }
1.16 deraadt 274:
1.76 guenther 275: if ((kf = kvm_getfiles(kd, what, arg, sizeof(*kf), &cnt)) == NULL)
1.21 deraadt 276: errx(1, "%s", kvm_geterr(kd));
1.68 millert 277:
1.72 guenther 278: find_splices(kf, cnt);
1.68 millert 279: if (!fuser)
280: fstat_header();
281: for (kflast = &kf[cnt]; kf < kflast; ++kf) {
282: if (fuser)
283: fuser_check(kf);
284: else
285: fstat_dofile(kf);
286: }
287: if (fuser)
288: fuser_run();
289:
290: exit(0);
291: }
292:
293: void
294: fstat_header(void)
295: {
1.1 deraadt 296: if (nflg)
297: printf("%s",
1.60 sobrado 298: "USER CMD PID FD DEV INUM MODE R/W SZ|DV");
1.1 deraadt 299: else
300: printf("%s",
1.60 sobrado 301: "USER CMD PID FD MOUNT INUM MODE R/W SZ|DV");
1.28 hugh 302: if (oflg)
303: printf("%s", ":OFFSET ");
1.1 deraadt 304: if (checkfile && fsflg == 0)
1.56 mickey 305: printf(" NAME");
306: if (sflg)
307: printf(" XFERS KBYTES");
308: putchar('\n');
1.1 deraadt 309: }
310:
311: char *Uname, *Comm;
1.62 deraadt 312: uid_t *procuid;
1.14 deraadt 313: pid_t Pid;
1.1 deraadt 314:
1.34 deraadt 315: #define PREFIX(i) do { \
1.39 mpech 316: printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
1.60 sobrado 317: switch (i) { \
1.65 millert 318: case KERN_FILE_TEXT: \
1.1 deraadt 319: printf(" text"); \
320: break; \
1.65 millert 321: case KERN_FILE_CDIR: \
1.1 deraadt 322: printf(" wd"); \
323: break; \
1.65 millert 324: case KERN_FILE_RDIR: \
1.1 deraadt 325: printf(" root"); \
326: break; \
1.65 millert 327: case KERN_FILE_TRACE: \
1.1 deraadt 328: printf(" tr"); \
329: break; \
330: default: \
331: printf(" %4d", i); \
332: break; \
1.34 deraadt 333: } \
334: } while (0)
1.1 deraadt 335:
336: /*
337: * print open files attributed to this process
338: */
339: void
1.76 guenther 340: fstat_dofile(struct kinfo_file *kf)
1.1 deraadt 341: {
342:
1.65 millert 343: Uname = user_from_uid(kf->p_uid, 0);
344: procuid = &kf->p_uid;
345: Pid = kf->p_pid;
346: Comm = kf->p_comm;
1.56 mickey 347:
1.65 millert 348: switch (kf->f_type) {
349: case DTYPE_VNODE:
350: vtrans(kf);
351: break;
352: case DTYPE_SOCKET:
1.67 miod 353: if (checkfile == 0)
354: socktrans(kf);
1.65 millert 355: break;
356: case DTYPE_PIPE:
1.67 miod 357: if (checkfile == 0)
358: pipetrans(kf);
1.65 millert 359: break;
360: case DTYPE_KQUEUE:
1.67 miod 361: if (checkfile == 0)
362: kqueuetrans(kf);
1.65 millert 363: break;
364: case DTYPE_SYSTRACE:
1.67 miod 365: if (checkfile == 0)
366: systracetrans(kf);
1.65 millert 367: break;
368: default:
369: if (vflg) {
370: warnx("unknown file type %d for file %d of pid %ld",
371: kf->f_type, kf->fd_fd, (long)Pid);
1.1 deraadt 372: }
1.65 millert 373: break;
1.1 deraadt 374: }
375: }
376:
377: void
1.76 guenther 378: vtrans(struct kinfo_file *kf)
1.1 deraadt 379: {
1.65 millert 380: const char *badtype = NULL;
381: char rw[3], mode[12];
382: char *filename = NULL;
383:
384: if (kf->v_type == VNON)
1.1 deraadt 385: badtype = "none";
1.65 millert 386: else if (kf->v_type == VBAD)
1.1 deraadt 387: badtype = "bad";
1.65 millert 388: else if (kf->v_tag == VT_NON && !(kf->v_flag & VCLONE))
389: badtype = "none"; /* not a clone */
390:
1.1 deraadt 391: if (checkfile) {
392: int fsmatch = 0;
1.68 millert 393: struct filearg *fa;
1.1 deraadt 394:
395: if (badtype)
396: return;
1.68 millert 397: SLIST_FOREACH(fa, &fileargs, next) {
398: if (fa->dev == kf->va_fsid) {
1.1 deraadt 399: fsmatch = 1;
1.68 millert 400: if (fa->ino == kf->va_fileid) {
401: filename = fa->name;
1.1 deraadt 402: break;
403: }
404: }
1.65 millert 405: }
1.1 deraadt 406: if (fsmatch == 0 || (filename == NULL && fsflg == 0))
407: return;
408: }
1.65 millert 409: PREFIX(kf->fd_fd);
1.1 deraadt 410: if (badtype) {
1.53 mickey 411: (void)printf(" - - %10s -\n", badtype);
1.1 deraadt 412: return;
413: }
1.61 thib 414:
1.1 deraadt 415: if (nflg)
1.65 millert 416: (void)printf(" %2ld,%-2ld", (long)major(kf->va_fsid),
417: (long)minor(kf->va_fsid));
418: else if (!(kf->v_flag & VCLONE))
419: (void)printf(" %-8s", kf->f_mntonname);
1.1 deraadt 420: else
1.73 mikeb 421: (void)printf(" clone ");
1.1 deraadt 422: if (nflg)
1.65 millert 423: (void)snprintf(mode, sizeof(mode), "%o", kf->va_mode);
1.1 deraadt 424: else
1.65 millert 425: strmode(kf->va_mode, mode);
426: printf(" %8llu %11s", kf->va_fileid, mode);
1.28 hugh 427: rw[0] = '\0';
1.65 millert 428: if (kf->f_flag & FREAD)
1.34 deraadt 429: strlcat(rw, "r", sizeof rw);
1.65 millert 430: if (kf->f_flag & FWRITE)
1.34 deraadt 431: strlcat(rw, "w", sizeof rw);
1.28 hugh 432: printf(" %2s", rw);
1.65 millert 433: switch (kf->v_type) {
1.1 deraadt 434: case VBLK:
435: case VCHR: {
436: char *name;
437:
1.65 millert 438: if (nflg || ((name = devname(kf->va_rdev,
439: kf->v_type == VCHR ? S_IFCHR : S_IFBLK)) == NULL))
440: printf(" %2d,%-3d", major(kf->va_rdev), minor(kf->va_rdev));
1.1 deraadt 441: else
1.28 hugh 442: printf(" %7s", name);
443: if (oflg)
444: printf(" ");
1.1 deraadt 445: break;
446: }
447: default:
1.65 millert 448: printf(" %8llu", kf->va_size);
1.62 deraadt 449: if (oflg) {
450: if (uid == 0 || uid == *procuid)
1.65 millert 451: printf(":%-8llu", kf->f_offset);
1.74 deraadt 452: else
1.62 deraadt 453: printf(":%-8s", "*");
454: }
455: }
456: if (sflg) {
457: if (uid == 0 || uid == *procuid) {
1.65 millert 458: printf(" %8llu %8llu",
1.74 deraadt 459: (kf->f_rxfer + kf->f_rwfer),
460: (kf->f_rbytes + kf->f_wbytes) / 1024);
1.62 deraadt 461: } else {
462: printf(" %8s %8s", "*", "*");
463: }
1.1 deraadt 464: }
465: if (filename && !fsflg)
1.28 hugh 466: printf(" %s", filename);
1.1 deraadt 467: putchar('\n');
468: }
469:
1.22 art 470: void
1.76 guenther 471: pipetrans(struct kinfo_file *kf)
1.22 art 472: {
473: void *maxaddr;
474:
1.65 millert 475: PREFIX(kf->fd_fd);
1.22 art 476:
477: printf(" ");
478:
479: /*
480: * We don't have enough space to fit both peer and own address, so
481: * we select the higher address so both ends of the pipe have the
482: * same visible addr. (it's the higher address because when the other
483: * end closes, it becomes 0)
484: */
1.80 ! deraadt 485: maxaddr = (void *)(uintptr_t)MAXIMUM(kf->f_data, kf->pipe_peer);
1.22 art 486:
1.74 deraadt 487: printf("pipe ");
488: hide(maxaddr);
1.75 bluhm 489: printf(" state: %s%s%s",
1.65 millert 490: (kf->pipe_state & PIPE_WANTR) ? "R" : "",
491: (kf->pipe_state & PIPE_WANTW) ? "W" : "",
492: (kf->pipe_state & PIPE_EOF) ? "E" : "");
1.56 mickey 493: if (sflg)
1.65 millert 494: printf("\t%8llu %8llu",
495: (kf->f_rxfer + kf->f_rwfer),
496: (kf->f_rbytes + kf->f_wbytes) / 1024);
1.56 mickey 497: printf("\n");
1.37 deraadt 498: return;
499: }
500:
501: void
1.76 guenther 502: kqueuetrans(struct kinfo_file *kf)
1.37 deraadt 503: {
1.65 millert 504: PREFIX(kf->fd_fd);
1.37 deraadt 505:
506: printf(" ");
507:
1.74 deraadt 508: printf("kqueue ");
509: hide((void *)(uintptr_t)kf->f_data);
510: printf(" %d state: %s%s\n",
1.65 millert 511: kf->kq_count,
512: (kf->kq_state & KQ_SEL) ? "S" : "",
513: (kf->kq_state & KQ_SLEEP) ? "W" : "");
1.37 deraadt 514: return;
515: }
516:
517: void
1.76 guenther 518: systracetrans(struct kinfo_file *kf)
1.37 deraadt 519: {
1.65 millert 520: PREFIX(kf->fd_fd);
1.37 deraadt 521:
522: printf(" ");
523:
1.74 deraadt 524: printf("systrace ");
525: hide((void *)(uintptr_t)kf->f_data);
526: printf(" npol %d\n", kf->str_npolicies);
1.22 art 527: return;
1.1 deraadt 528: }
529:
1.26 itojun 530: #ifdef INET6
531: const char *
1.40 deraadt 532: inet6_addrstr(struct in6_addr *p)
1.26 itojun 533: {
534: struct sockaddr_in6 sin6;
535: static char hbuf[NI_MAXHOST];
1.50 itojun 536: const int niflags = NI_NUMERICHOST;
1.26 itojun 537:
538: memset(&sin6, 0, sizeof(sin6));
539: sin6.sin6_family = AF_INET6;
540: sin6.sin6_len = sizeof(struct sockaddr_in6);
541: sin6.sin6_addr = *p;
542: if (IN6_IS_ADDR_LINKLOCAL(p) &&
543: *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
544: sin6.sin6_scope_id =
1.38 deraadt 545: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
1.26 itojun 546: sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
547: }
548:
549: if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.38 deraadt 550: hbuf, sizeof(hbuf), NULL, 0, niflags))
1.26 itojun 551: return "invalid";
552:
553: return hbuf;
554: }
555: #endif
556:
1.1 deraadt 557: void
1.76 guenther 558: splice_insert(char type, u_int64_t ptr, struct kinfo_file *data)
1.72 guenther 559: {
560: ENTRY entry, *found;
561:
1.74 deraadt 562: if (asprintf(&entry.key, "%c%llx", type, hideroot ? 0 : ptr) == -1)
1.72 guenther 563: err(1, NULL);
564: entry.data = data;
565: if ((found = hsearch(entry, ENTER)) == NULL)
566: err(1, "hsearch");
567: /* if it's ambiguous, set the data to NULL */
568: if (found->data != data)
569: found->data = NULL;
570: }
571:
1.76 guenther 572: struct kinfo_file *
1.72 guenther 573: splice_find(char type, u_int64_t ptr)
574: {
575: ENTRY entry, *found;
576: char buf[20];
577:
1.74 deraadt 578: snprintf(buf, sizeof(buf), "%c%llx", type, hideroot ? 0 : ptr);
1.72 guenther 579: entry.key = buf;
580: found = hsearch(entry, FIND);
581: return (found != NULL ? found->data : NULL);
582: }
583:
584: void
1.76 guenther 585: find_splices(struct kinfo_file *kf, int cnt)
1.72 guenther 586: {
587: int i, created;
588:
589: created = 0;
590: for (i = 0; i < cnt; i++) {
591: if (kf[i].f_type != DTYPE_SOCKET ||
592: (kf[i].so_splice == 0 && kf[i].so_splicelen != -1))
593: continue;
594: if (created++ == 0) {
595: if (hcreate(1000) == 0)
596: err(1, "hcreate");
597: }
598: splice_insert('>', kf[i].f_data, &kf[i]);
599: if (kf[i].so_splice != 0)
600: splice_insert('<', kf[i].so_splice, &kf[i]);
601: }
602: }
603:
604: void
1.76 guenther 605: print_inet_details(struct kinfo_file *kf)
1.72 guenther 606: {
607: struct in_addr laddr, faddr;
608:
609: memcpy(&laddr, kf->inp_laddru, sizeof(laddr));
610: memcpy(&faddr, kf->inp_faddru, sizeof(faddr));
611: if (kf->so_protocol == IPPROTO_TCP) {
1.74 deraadt 612: printf(" ");
613: hide((void *)(uintptr_t)kf->inp_ppcb);
1.72 guenther 614: printf(" %s:%d", laddr.s_addr == INADDR_ANY ? "*" :
615: inet_ntoa(laddr), ntohs(kf->inp_lport));
616: if (kf->inp_fport) {
617: if (kf->so_state & SS_CONNECTOUT)
618: printf(" --> ");
619: else
620: printf(" <-- ");
621: printf("%s:%d",
622: faddr.s_addr == INADDR_ANY ? "*" :
623: inet_ntoa(faddr), ntohs(kf->inp_fport));
624: }
625: } else if (kf->so_protocol == IPPROTO_UDP) {
626: printf(" %s:%d", laddr.s_addr == INADDR_ANY ? "*" :
627: inet_ntoa(laddr), ntohs(kf->inp_lport));
628: if (kf->inp_fport) {
629: printf(" <-> %s:%d",
630: faddr.s_addr == INADDR_ANY ? "*" :
631: inet_ntoa(faddr), ntohs(kf->inp_fport));
632: }
1.74 deraadt 633: } else if (kf->so_pcb) {
634: printf(" ");
635: hide((void *)(uintptr_t)kf->so_pcb);
636: }
1.72 guenther 637: }
638:
639: #ifdef INET6
640: void
1.76 guenther 641: print_inet6_details(struct kinfo_file *kf)
1.72 guenther 642: {
643: char xaddrbuf[NI_MAXHOST + 2];
644: struct in6_addr laddr6, faddr6;
645:
646: memcpy(&laddr6, kf->inp_laddru, sizeof(laddr6));
647: memcpy(&faddr6, kf->inp_faddru, sizeof(faddr6));
648: if (kf->so_protocol == IPPROTO_TCP) {
1.74 deraadt 649: printf(" ");
650: hide((void *)(uintptr_t)kf->inp_ppcb);
1.72 guenther 651: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
652: inet6_addrstr(&laddr6));
653: printf(" %s:%d",
654: IN6_IS_ADDR_UNSPECIFIED(&laddr6) ? "*" :
655: xaddrbuf, ntohs(kf->inp_lport));
656: if (kf->inp_fport) {
657: if (kf->so_state & SS_CONNECTOUT)
658: printf(" --> ");
659: else
660: printf(" <-- ");
661: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
662: inet6_addrstr(&faddr6));
663: printf("%s:%d",
664: IN6_IS_ADDR_UNSPECIFIED(&faddr6) ? "*" :
665: xaddrbuf, ntohs(kf->inp_fport));
666: }
667: } else if (kf->so_protocol == IPPROTO_UDP) {
668: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
669: inet6_addrstr(&laddr6));
670: printf(" %s:%d",
671: IN6_IS_ADDR_UNSPECIFIED(&laddr6) ? "*" :
672: xaddrbuf, ntohs(kf->inp_lport));
673: if (kf->inp_fport) {
674: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
675: inet6_addrstr(&faddr6));
676: printf(" <-> %s:%d",
677: IN6_IS_ADDR_UNSPECIFIED(&faddr6) ? "*" :
678: xaddrbuf, ntohs(kf->inp_fport));
679: }
1.74 deraadt 680: } else if (kf->so_pcb) {
681: printf(" ");
682: hide((void *)(uintptr_t)kf->so_pcb);
683: }
1.72 guenther 684: }
685: #endif
686:
687: void
1.76 guenther 688: print_sock_details(struct kinfo_file *kf)
1.72 guenther 689: {
690: if (kf->so_family == AF_INET)
691: print_inet_details(kf);
692: #ifdef INET6
693: else if (kf->so_family == AF_INET6)
694: print_inet6_details(kf);
695: #endif
696: }
697:
698: void
1.76 guenther 699: socktrans(struct kinfo_file *kf)
1.1 deraadt 700: {
701: static char *stypename[] = {
702: "unused", /* 0 */
1.38 deraadt 703: "stream", /* 1 */
1.1 deraadt 704: "dgram", /* 2 */
705: "raw", /* 3 */
706: "rdm", /* 4 */
707: "seqpak" /* 5 */
708: };
709: #define STYPEMAX 5
1.66 chl 710: char *stype, stypebuf[24];
1.1 deraadt 711:
1.65 millert 712: PREFIX(kf->fd_fd);
1.1 deraadt 713:
1.65 millert 714: if (kf->so_type > STYPEMAX) {
715: snprintf(stypebuf, sizeof(stypebuf), "?%d", kf->so_type);
716: stype = stypebuf;
717: } else {
718: stype = stypename[kf->so_type];
1.1 deraadt 719: }
720:
1.34 deraadt 721: /*
1.1 deraadt 722: * protocol specific formatting
723: *
724: * Try to find interesting things to print. For tcp, the interesting
725: * thing is the address of the tcpcb, for udp and others, just the
726: * inpcb (socket pcb). For unix domain, its the address of the socket
727: * pcb and the address of the connected pcb (if connected). Otherwise
728: * just print the protocol number and address of the socket itself.
729: * The idea is not to duplicate netstat, but to make available enough
730: * information for further analysis.
731: */
1.65 millert 732: switch (kf->so_family) {
1.1 deraadt 733: case AF_INET:
1.65 millert 734: printf("* internet %s", stype);
735: getinetproto(kf->so_protocol);
1.72 guenther 736: print_inet_details(kf);
1.1 deraadt 737: break;
1.26 itojun 738: #ifdef INET6
739: case AF_INET6:
1.65 millert 740: printf("* internet6 %s", stype);
741: getinetproto(kf->so_protocol);
1.72 guenther 742: print_inet6_details(kf);
1.26 itojun 743: break;
744: #endif
1.1 deraadt 745: case AF_UNIX:
746: /* print address of pcb and connected pcb */
1.65 millert 747: printf("* unix %s", stype);
748: if (kf->so_pcb) {
1.74 deraadt 749: printf(" ");
750: hide((void *)(uintptr_t)kf->so_pcb);
1.65 millert 751: if (kf->unp_conn) {
1.1 deraadt 752: char shoconn[4], *cp;
753:
754: cp = shoconn;
1.65 millert 755: if (!(kf->so_state & SS_CANTRCVMORE))
1.1 deraadt 756: *cp++ = '<';
757: *cp++ = '-';
1.65 millert 758: if (!(kf->so_state & SS_CANTSENDMORE))
1.1 deraadt 759: *cp++ = '>';
760: *cp = '\0';
1.74 deraadt 761: printf(" %s ", shoconn);
762: hide((void *)(uintptr_t)kf->unp_conn);
1.1 deraadt 763: }
764: }
765: break;
1.65 millert 766: case AF_MPLS:
767: /* print protocol number and socket address */
768: printf("* mpls %s", stype);
1.74 deraadt 769: printf(" %d ", kf->so_protocol);
770: hide((void *)(uintptr_t)kf->f_data);
1.65 millert 771: break;
772: case AF_ROUTE:
773: /* print protocol number and socket address */
774: printf("* route %s", stype);
1.74 deraadt 775: printf(" %d ", kf->so_protocol);
776: hide((void *)(uintptr_t)kf->f_data);
1.65 millert 777: break;
1.1 deraadt 778: default:
779: /* print protocol number and socket address */
1.65 millert 780: printf("* %d %s", kf->so_family, stype);
1.74 deraadt 781: printf(" %d ", kf->so_protocol);
782: hide((void *)(uintptr_t)kf->f_data);
1.72 guenther 783: }
784: if (kf->so_splice != 0 || kf->so_splicelen == -1) {
1.76 guenther 785: struct kinfo_file *from, *to;
1.72 guenther 786:
787: from = splice_find('<', kf->f_data);
788: to = NULL;
789: if (kf->so_splice != 0)
790: to = splice_find('>', kf->so_splice);
791:
792: if (to != NULL && from == to) {
793: printf(" <==>");
794: print_sock_details(to);
795: } else if (kf->so_splice != 0) {
796: printf(" ==>");
797: if (to != NULL)
798: print_sock_details(to);
799: } else if (kf->so_splicelen == -1) {
800: printf(" <==");
801: if (from != NULL)
802: print_sock_details(from);
803: }
1.1 deraadt 804: }
1.56 mickey 805: if (sflg)
1.65 millert 806: printf("\t%8llu %8llu",
807: (kf->f_rxfer + kf->f_rwfer),
808: (kf->f_rbytes + kf->f_wbytes) / 1024);
1.1 deraadt 809: printf("\n");
810: }
811:
812: /*
813: * getinetproto --
814: * print name of protocol number
815: */
816: void
817: getinetproto(number)
818: int number;
819: {
1.24 deraadt 820: static int isopen;
1.32 mpech 821: struct protoent *pe;
1.1 deraadt 822:
1.24 deraadt 823: if (!isopen)
824: setprotoent(++isopen);
825: if ((pe = getprotobynumber(number)) != NULL)
826: printf(" %s", pe->p_name);
827: else
1.1 deraadt 828: printf(" %d", number);
829: }
830:
1.11 millert 831: int
1.40 deraadt 832: getfname(char *filename)
1.1 deraadt 833: {
1.68 millert 834: static struct statfs *mntbuf;
835: static int nmounts;
836: int i;
837: struct stat sb;
838: struct filearg *cur;
1.1 deraadt 839:
1.68 millert 840: if (stat(filename, &sb)) {
1.27 millert 841: warn("%s", filename);
1.68 millert 842: return (0);
1.1 deraadt 843: }
844:
1.68 millert 845: /*
846: * POSIX specifies "For block special devices, all processes using any
847: * file on that device are listed". However the -f flag description
848: * states "The report shall be only for the named files", so we only
849: * look up a block device if the -f flag has not be specified.
850: */
851: if (fuser && !fsflg && S_ISBLK(sb.st_mode)) {
852: if (mntbuf == NULL) {
853: nmounts = getmntinfo(&mntbuf, MNT_NOWAIT);
854: if (nmounts == -1)
855: err(1, "getmntinfo");
856: }
857: for (i = 0; i < nmounts; i++) {
858: if (!strcmp(mntbuf[i].f_mntfromname, filename)) {
859: if (stat(mntbuf[i].f_mntonname, &sb) == -1) {
860: warn("%s", filename);
861: return (0);
862: }
863: cflg = 1;
864: break;
865: }
866: }
867: }
868:
869: if ((cur = malloc(sizeof(*cur))) == NULL)
870: err(1, NULL);
871:
872: cur->ino = sb.st_ino;
873: cur->dev = sb.st_dev & 0xffff;
1.1 deraadt 874: cur->name = filename;
1.68 millert 875: TAILQ_INIT(&cur->fusers);
876: SLIST_INSERT_HEAD(&fileargs, cur, next);
877: return (1);
878: }
879:
880: int
881: signame_to_signum(char *sig)
882: {
883: int n;
884: const char *errstr = NULL;
885:
886: if (isdigit((unsigned char)*sig)) {
887: n = strtonum(sig, 0, NSIG - 1, &errstr);
888: return (errstr ? -1 : n);
889: }
890: if (!strncasecmp(sig, "sig", 3))
891: sig += 3;
892: for (n = 1; n < NSIG; n++) {
893: if (!strcasecmp(sys_signame[n], sig))
894: return (n);
895: }
896: return (-1);
1.1 deraadt 897: }
898:
899: void
1.40 deraadt 900: usage(void)
1.1 deraadt 901: {
1.68 millert 902: if (fuser) {
903: fprintf(stderr, "usage: fuser [-cfku] [-M core] "
904: "[-N system] [-s signal] file ...\n");
905: } else {
906: fprintf(stderr, "usage: fstat [-fnosv] [-M core] [-N system] "
907: "[-p pid] [-u user] [file ...]\n");
908: }
1.1 deraadt 909: exit(1);
910: }