Annotation of src/usr.bin/fstat/fstat.c, Revision 1.87
1.87 ! tedu 1: /* $OpenBSD: fstat.c,v 1.86 2016/01/02 13:22:52 semarie 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.85 mestre 99: int cflg; /* fuser only */
1.68 millert 100:
1.85 mestre 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);
1.85 mestre 110: __dead void usage(void);
1.35 millert 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 vtrans(struct kinfo_file *);
1.47 deraadt 124: const char *inet6_addrstr(struct in6_addr *);
1.68 millert 125: int signame_to_signum(char *);
1.74 deraadt 126: void hide(void *p);
127:
128: int hideroot;
129:
130: void
131: hide(void *p)
132: {
133: printf("%p", hideroot ? NULL : p);
134: }
1.1 deraadt 135:
1.11 millert 136: int
1.40 deraadt 137: main(int argc, char *argv[])
1.1 deraadt 138: {
1.32 mpech 139: struct passwd *passwd;
1.76 guenther 140: struct kinfo_file *kf, *kflast;
1.1 deraadt 141: int arg, ch, what;
1.68 millert 142: char *memf, *nlistf, *optstr;
1.2 niklas 143: char buf[_POSIX2_LINE_MAX];
1.58 tedu 144: const char *errstr;
1.65 millert 145: int cnt, flags;
1.1 deraadt 146:
1.74 deraadt 147: hideroot = getuid();
148:
1.65 millert 149: arg = -1;
150: what = KERN_FILE_BYPID;
1.1 deraadt 151: nlistf = memf = NULL;
1.28 hugh 152: oflg = 0;
1.68 millert 153:
154: /* are we fstat(1) or fuser(1)? */
155: if (strcmp(__progname, "fuser") == 0) {
156: fuser = 1;
157: optstr = "cfks:uM:N:";
158: } else {
159: fuser = 0;
160: optstr = "fnop:su:vN:M:";
161: }
162:
163: /*
164: * fuser and fstat share three flags: -f, -s and -u. In both cases
165: * -f is a boolean, but for -u fstat wants an argument while fuser
166: * does not and for -s fuser wants an argument whereas fstat does not.
167: */
168: while ((ch = getopt(argc, argv, optstr)) != -1)
1.60 sobrado 169: switch ((char)ch) {
1.68 millert 170: case 'c':
171: if (fsflg)
172: usage();
173: cflg = 1;
174: break;
1.1 deraadt 175: case 'f':
1.68 millert 176: if (cflg)
177: usage();
1.1 deraadt 178: fsflg = 1;
179: break;
1.68 millert 180: case 'k':
181: sflg = 1;
182: signo = SIGKILL;
183: break;
1.1 deraadt 184: case 'M':
185: memf = optarg;
186: break;
187: case 'N':
188: nlistf = optarg;
189: break;
190: case 'n':
191: nflg = 1;
192: break;
1.28 hugh 193: case 'o':
194: oflg = 1;
195: break;
1.1 deraadt 196: case 'p':
197: if (pflg++)
198: usage();
1.58 tedu 199: arg = strtonum(optarg, 0, INT_MAX, &errstr);
200: if (errstr != NULL) {
1.74 deraadt 201: warnx("-p requires a process id, %s: %s",
1.58 tedu 202: errstr, optarg);
1.1 deraadt 203: usage();
204: }
1.65 millert 205: what = KERN_FILE_BYPID;
1.1 deraadt 206: break;
1.56 mickey 207: case 's':
208: sflg = 1;
1.68 millert 209: if (fuser) {
210: signo = signame_to_signum(optarg);
211: if (signo == -1) {
212: warnx("invalid signal %s", optarg);
213: usage();
214: }
215: }
1.56 mickey 216: break;
1.1 deraadt 217: case 'u':
218: if (uflg++)
219: usage();
1.68 millert 220: if (!fuser) {
1.69 nicm 221: if (!(passwd = getpwnam(optarg))) {
1.74 deraadt 222: arg = strtonum(optarg, 0, UID_MAX,
1.69 nicm 223: &errstr);
224: if (errstr != NULL) {
225: errx(1, "%s: unknown uid",
226: optarg);
227: }
228: } else
229: arg = passwd->pw_uid;
1.68 millert 230: what = KERN_FILE_BYUID;
231: }
1.1 deraadt 232: break;
233: case 'v':
234: vflg = 1;
235: break;
236: default:
237: usage();
238: }
239:
1.41 deraadt 240: /*
1.62 deraadt 241: * get the uid, for oflg and sflg
242: */
243: uid = getuid();
244:
245: /*
1.65 millert 246: * Use sysctl unless inspecting an alternate kernel.
1.41 deraadt 247: */
1.65 millert 248: if (nlistf == NULL || memf == NULL)
249: flags = KVM_NO_FILES;
250: else
251: flags = O_RDONLY;
1.41 deraadt 252:
1.65 millert 253: if ((kd = kvm_openfiles(nlistf, memf, NULL, flags, buf)) == NULL)
1.41 deraadt 254: errx(1, "%s", buf);
255:
1.1 deraadt 256: if (*(argv += optind)) {
257: for (; *argv; ++argv) {
258: if (getfname(*argv))
259: checkfile = 1;
260: }
1.68 millert 261: /* file(s) specified, but none accessible */
262: if (!checkfile)
1.1 deraadt 263: exit(1);
1.68 millert 264: } else if (fuser)
265: usage();
1.1 deraadt 266:
1.68 millert 267: if (!fuser && fsflg && !checkfile) {
268: /* fstat -f with no files means use wd */
1.1 deraadt 269: if (getfname(".") == 0)
270: exit(1);
271: checkfile = 1;
272: }
1.16 deraadt 273:
1.76 guenther 274: if ((kf = kvm_getfiles(kd, what, arg, sizeof(*kf), &cnt)) == NULL)
1.21 deraadt 275: errx(1, "%s", kvm_geterr(kd));
1.81 deraadt 276:
1.82 deraadt 277: if (fuser) {
1.86 semarie 278: /*
279: * fuser
280: * uflg: need "getpw"
281: * sflg: need "proc" (might call kill(2))
282: */
283: if (uflg && sflg) {
284: if (pledge("stdio rpath getpw proc", NULL) == -1)
285: err(1, "pledge");
286: } else if (uflg) {
287: if (pledge("stdio rpath getpw", NULL) == -1)
288: err(1, "pledge");
289: } else if (sflg) {
1.82 deraadt 290: if (pledge("stdio rpath proc", NULL) == -1)
291: err(1, "pledge");
292: } else {
293: if (pledge("stdio rpath", NULL) == -1)
294: err(1, "pledge");
295: }
296: } else {
1.86 semarie 297: /* fstat */
1.84 deraadt 298: if (pledge("stdio rpath getpw", NULL) == -1)
1.82 deraadt 299: err(1, "pledge");
300: }
1.68 millert 301:
1.72 guenther 302: find_splices(kf, cnt);
1.68 millert 303: if (!fuser)
304: fstat_header();
305: for (kflast = &kf[cnt]; kf < kflast; ++kf) {
306: if (fuser)
307: fuser_check(kf);
308: else
309: fstat_dofile(kf);
310: }
311: if (fuser)
312: fuser_run();
313:
314: exit(0);
315: }
316:
317: void
318: fstat_header(void)
319: {
1.1 deraadt 320: if (nflg)
321: printf("%s",
1.60 sobrado 322: "USER CMD PID FD DEV INUM MODE R/W SZ|DV");
1.1 deraadt 323: else
324: printf("%s",
1.60 sobrado 325: "USER CMD PID FD MOUNT INUM MODE R/W SZ|DV");
1.28 hugh 326: if (oflg)
327: printf("%s", ":OFFSET ");
1.1 deraadt 328: if (checkfile && fsflg == 0)
1.56 mickey 329: printf(" NAME");
330: if (sflg)
331: printf(" XFERS KBYTES");
332: putchar('\n');
1.1 deraadt 333: }
334:
335: char *Uname, *Comm;
1.62 deraadt 336: uid_t *procuid;
1.14 deraadt 337: pid_t Pid;
1.1 deraadt 338:
1.34 deraadt 339: #define PREFIX(i) do { \
1.39 mpech 340: printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
1.60 sobrado 341: switch (i) { \
1.65 millert 342: case KERN_FILE_TEXT: \
1.1 deraadt 343: printf(" text"); \
344: break; \
1.65 millert 345: case KERN_FILE_CDIR: \
1.1 deraadt 346: printf(" wd"); \
347: break; \
1.65 millert 348: case KERN_FILE_RDIR: \
1.1 deraadt 349: printf(" root"); \
350: break; \
1.65 millert 351: case KERN_FILE_TRACE: \
1.1 deraadt 352: printf(" tr"); \
353: break; \
354: default: \
355: printf(" %4d", i); \
356: break; \
1.34 deraadt 357: } \
358: } while (0)
1.1 deraadt 359:
360: /*
361: * print open files attributed to this process
362: */
363: void
1.76 guenther 364: fstat_dofile(struct kinfo_file *kf)
1.1 deraadt 365: {
366:
1.65 millert 367: Uname = user_from_uid(kf->p_uid, 0);
368: procuid = &kf->p_uid;
369: Pid = kf->p_pid;
370: Comm = kf->p_comm;
1.56 mickey 371:
1.65 millert 372: switch (kf->f_type) {
373: case DTYPE_VNODE:
374: vtrans(kf);
375: break;
376: case DTYPE_SOCKET:
1.67 miod 377: if (checkfile == 0)
378: socktrans(kf);
1.65 millert 379: break;
380: case DTYPE_PIPE:
1.67 miod 381: if (checkfile == 0)
382: pipetrans(kf);
1.65 millert 383: break;
384: case DTYPE_KQUEUE:
1.67 miod 385: if (checkfile == 0)
386: kqueuetrans(kf);
1.65 millert 387: break;
388: default:
389: if (vflg) {
390: warnx("unknown file type %d for file %d of pid %ld",
391: kf->f_type, kf->fd_fd, (long)Pid);
1.1 deraadt 392: }
1.65 millert 393: break;
1.1 deraadt 394: }
395: }
396:
397: void
1.76 guenther 398: vtrans(struct kinfo_file *kf)
1.1 deraadt 399: {
1.65 millert 400: const char *badtype = NULL;
401: char rw[3], mode[12];
402: char *filename = NULL;
403:
404: if (kf->v_type == VNON)
1.1 deraadt 405: badtype = "none";
1.65 millert 406: else if (kf->v_type == VBAD)
1.1 deraadt 407: badtype = "bad";
1.65 millert 408: else if (kf->v_tag == VT_NON && !(kf->v_flag & VCLONE))
409: badtype = "none"; /* not a clone */
410:
1.1 deraadt 411: if (checkfile) {
412: int fsmatch = 0;
1.68 millert 413: struct filearg *fa;
1.1 deraadt 414:
415: if (badtype)
416: return;
1.68 millert 417: SLIST_FOREACH(fa, &fileargs, next) {
418: if (fa->dev == kf->va_fsid) {
1.1 deraadt 419: fsmatch = 1;
1.68 millert 420: if (fa->ino == kf->va_fileid) {
421: filename = fa->name;
1.1 deraadt 422: break;
423: }
424: }
1.65 millert 425: }
1.1 deraadt 426: if (fsmatch == 0 || (filename == NULL && fsflg == 0))
427: return;
428: }
1.65 millert 429: PREFIX(kf->fd_fd);
1.1 deraadt 430: if (badtype) {
1.53 mickey 431: (void)printf(" - - %10s -\n", badtype);
1.1 deraadt 432: return;
433: }
1.61 thib 434:
1.1 deraadt 435: if (nflg)
1.65 millert 436: (void)printf(" %2ld,%-2ld", (long)major(kf->va_fsid),
437: (long)minor(kf->va_fsid));
438: else if (!(kf->v_flag & VCLONE))
439: (void)printf(" %-8s", kf->f_mntonname);
1.1 deraadt 440: else
1.73 mikeb 441: (void)printf(" clone ");
1.1 deraadt 442: if (nflg)
1.65 millert 443: (void)snprintf(mode, sizeof(mode), "%o", kf->va_mode);
1.1 deraadt 444: else
1.65 millert 445: strmode(kf->va_mode, mode);
446: printf(" %8llu %11s", kf->va_fileid, mode);
1.28 hugh 447: rw[0] = '\0';
1.65 millert 448: if (kf->f_flag & FREAD)
1.34 deraadt 449: strlcat(rw, "r", sizeof rw);
1.65 millert 450: if (kf->f_flag & FWRITE)
1.34 deraadt 451: strlcat(rw, "w", sizeof rw);
1.28 hugh 452: printf(" %2s", rw);
1.65 millert 453: switch (kf->v_type) {
1.1 deraadt 454: case VBLK:
455: case VCHR: {
456: char *name;
457:
1.65 millert 458: if (nflg || ((name = devname(kf->va_rdev,
459: kf->v_type == VCHR ? S_IFCHR : S_IFBLK)) == NULL))
460: printf(" %2d,%-3d", major(kf->va_rdev), minor(kf->va_rdev));
1.1 deraadt 461: else
1.28 hugh 462: printf(" %7s", name);
463: if (oflg)
464: printf(" ");
1.1 deraadt 465: break;
466: }
467: default:
1.65 millert 468: printf(" %8llu", kf->va_size);
1.62 deraadt 469: if (oflg) {
470: if (uid == 0 || uid == *procuid)
1.65 millert 471: printf(":%-8llu", kf->f_offset);
1.74 deraadt 472: else
1.62 deraadt 473: printf(":%-8s", "*");
474: }
475: }
476: if (sflg) {
477: if (uid == 0 || uid == *procuid) {
1.65 millert 478: printf(" %8llu %8llu",
1.74 deraadt 479: (kf->f_rxfer + kf->f_rwfer),
480: (kf->f_rbytes + kf->f_wbytes) / 1024);
1.62 deraadt 481: } else {
482: printf(" %8s %8s", "*", "*");
483: }
1.1 deraadt 484: }
485: if (filename && !fsflg)
1.28 hugh 486: printf(" %s", filename);
1.1 deraadt 487: putchar('\n');
488: }
489:
1.22 art 490: void
1.76 guenther 491: pipetrans(struct kinfo_file *kf)
1.22 art 492: {
493: void *maxaddr;
494:
1.65 millert 495: PREFIX(kf->fd_fd);
1.22 art 496:
497: printf(" ");
498:
499: /*
500: * We don't have enough space to fit both peer and own address, so
501: * we select the higher address so both ends of the pipe have the
502: * same visible addr. (it's the higher address because when the other
503: * end closes, it becomes 0)
504: */
1.80 deraadt 505: maxaddr = (void *)(uintptr_t)MAXIMUM(kf->f_data, kf->pipe_peer);
1.22 art 506:
1.74 deraadt 507: printf("pipe ");
508: hide(maxaddr);
1.75 bluhm 509: printf(" state: %s%s%s",
1.65 millert 510: (kf->pipe_state & PIPE_WANTR) ? "R" : "",
511: (kf->pipe_state & PIPE_WANTW) ? "W" : "",
512: (kf->pipe_state & PIPE_EOF) ? "E" : "");
1.56 mickey 513: if (sflg)
1.65 millert 514: printf("\t%8llu %8llu",
515: (kf->f_rxfer + kf->f_rwfer),
516: (kf->f_rbytes + kf->f_wbytes) / 1024);
1.56 mickey 517: printf("\n");
1.37 deraadt 518: return;
519: }
520:
521: void
1.76 guenther 522: kqueuetrans(struct kinfo_file *kf)
1.37 deraadt 523: {
1.65 millert 524: PREFIX(kf->fd_fd);
1.37 deraadt 525:
526: printf(" ");
527:
1.74 deraadt 528: printf("kqueue ");
529: hide((void *)(uintptr_t)kf->f_data);
530: printf(" %d state: %s%s\n",
1.65 millert 531: kf->kq_count,
532: (kf->kq_state & KQ_SEL) ? "S" : "",
533: (kf->kq_state & KQ_SLEEP) ? "W" : "");
1.22 art 534: return;
1.1 deraadt 535: }
536:
1.26 itojun 537: #ifdef INET6
538: const char *
1.40 deraadt 539: inet6_addrstr(struct in6_addr *p)
1.26 itojun 540: {
541: struct sockaddr_in6 sin6;
542: static char hbuf[NI_MAXHOST];
1.50 itojun 543: const int niflags = NI_NUMERICHOST;
1.26 itojun 544:
545: memset(&sin6, 0, sizeof(sin6));
546: sin6.sin6_family = AF_INET6;
547: sin6.sin6_len = sizeof(struct sockaddr_in6);
548: sin6.sin6_addr = *p;
549: if (IN6_IS_ADDR_LINKLOCAL(p) &&
550: *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
551: sin6.sin6_scope_id =
1.38 deraadt 552: ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
1.26 itojun 553: sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
554: }
555:
556: if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
1.38 deraadt 557: hbuf, sizeof(hbuf), NULL, 0, niflags))
1.26 itojun 558: return "invalid";
559:
560: return hbuf;
561: }
562: #endif
563:
1.1 deraadt 564: void
1.76 guenther 565: splice_insert(char type, u_int64_t ptr, struct kinfo_file *data)
1.72 guenther 566: {
567: ENTRY entry, *found;
568:
1.74 deraadt 569: if (asprintf(&entry.key, "%c%llx", type, hideroot ? 0 : ptr) == -1)
1.72 guenther 570: err(1, NULL);
571: entry.data = data;
572: if ((found = hsearch(entry, ENTER)) == NULL)
573: err(1, "hsearch");
574: /* if it's ambiguous, set the data to NULL */
575: if (found->data != data)
576: found->data = NULL;
577: }
578:
1.76 guenther 579: struct kinfo_file *
1.72 guenther 580: splice_find(char type, u_int64_t ptr)
581: {
582: ENTRY entry, *found;
583: char buf[20];
584:
1.74 deraadt 585: snprintf(buf, sizeof(buf), "%c%llx", type, hideroot ? 0 : ptr);
1.72 guenther 586: entry.key = buf;
587: found = hsearch(entry, FIND);
588: return (found != NULL ? found->data : NULL);
589: }
590:
591: void
1.76 guenther 592: find_splices(struct kinfo_file *kf, int cnt)
1.72 guenther 593: {
594: int i, created;
595:
596: created = 0;
597: for (i = 0; i < cnt; i++) {
598: if (kf[i].f_type != DTYPE_SOCKET ||
599: (kf[i].so_splice == 0 && kf[i].so_splicelen != -1))
600: continue;
601: if (created++ == 0) {
602: if (hcreate(1000) == 0)
603: err(1, "hcreate");
604: }
605: splice_insert('>', kf[i].f_data, &kf[i]);
606: if (kf[i].so_splice != 0)
607: splice_insert('<', kf[i].so_splice, &kf[i]);
608: }
609: }
610:
611: void
1.76 guenther 612: print_inet_details(struct kinfo_file *kf)
1.72 guenther 613: {
614: struct in_addr laddr, faddr;
615:
616: memcpy(&laddr, kf->inp_laddru, sizeof(laddr));
617: memcpy(&faddr, kf->inp_faddru, sizeof(faddr));
618: if (kf->so_protocol == IPPROTO_TCP) {
1.74 deraadt 619: printf(" ");
620: hide((void *)(uintptr_t)kf->inp_ppcb);
1.72 guenther 621: printf(" %s:%d", laddr.s_addr == INADDR_ANY ? "*" :
622: inet_ntoa(laddr), ntohs(kf->inp_lport));
623: if (kf->inp_fport) {
624: if (kf->so_state & SS_CONNECTOUT)
625: printf(" --> ");
626: else
627: printf(" <-- ");
628: printf("%s:%d",
629: faddr.s_addr == INADDR_ANY ? "*" :
630: inet_ntoa(faddr), ntohs(kf->inp_fport));
631: }
632: } else if (kf->so_protocol == IPPROTO_UDP) {
633: printf(" %s:%d", laddr.s_addr == INADDR_ANY ? "*" :
634: inet_ntoa(laddr), ntohs(kf->inp_lport));
635: if (kf->inp_fport) {
636: printf(" <-> %s:%d",
637: faddr.s_addr == INADDR_ANY ? "*" :
638: inet_ntoa(faddr), ntohs(kf->inp_fport));
639: }
1.74 deraadt 640: } else if (kf->so_pcb) {
641: printf(" ");
642: hide((void *)(uintptr_t)kf->so_pcb);
643: }
1.72 guenther 644: }
645:
646: #ifdef INET6
647: void
1.76 guenther 648: print_inet6_details(struct kinfo_file *kf)
1.72 guenther 649: {
650: char xaddrbuf[NI_MAXHOST + 2];
651: struct in6_addr laddr6, faddr6;
652:
653: memcpy(&laddr6, kf->inp_laddru, sizeof(laddr6));
654: memcpy(&faddr6, kf->inp_faddru, sizeof(faddr6));
655: if (kf->so_protocol == IPPROTO_TCP) {
1.74 deraadt 656: printf(" ");
657: hide((void *)(uintptr_t)kf->inp_ppcb);
1.72 guenther 658: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
659: inet6_addrstr(&laddr6));
660: printf(" %s:%d",
661: IN6_IS_ADDR_UNSPECIFIED(&laddr6) ? "*" :
662: xaddrbuf, ntohs(kf->inp_lport));
663: if (kf->inp_fport) {
664: if (kf->so_state & SS_CONNECTOUT)
665: printf(" --> ");
666: else
667: printf(" <-- ");
668: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
669: inet6_addrstr(&faddr6));
670: printf("%s:%d",
671: IN6_IS_ADDR_UNSPECIFIED(&faddr6) ? "*" :
672: xaddrbuf, ntohs(kf->inp_fport));
673: }
674: } else if (kf->so_protocol == IPPROTO_UDP) {
675: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
676: inet6_addrstr(&laddr6));
677: printf(" %s:%d",
678: IN6_IS_ADDR_UNSPECIFIED(&laddr6) ? "*" :
679: xaddrbuf, ntohs(kf->inp_lport));
680: if (kf->inp_fport) {
681: snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
682: inet6_addrstr(&faddr6));
683: printf(" <-> %s:%d",
684: IN6_IS_ADDR_UNSPECIFIED(&faddr6) ? "*" :
685: xaddrbuf, ntohs(kf->inp_fport));
686: }
1.74 deraadt 687: } else if (kf->so_pcb) {
688: printf(" ");
689: hide((void *)(uintptr_t)kf->so_pcb);
690: }
1.72 guenther 691: }
692: #endif
693:
694: void
1.76 guenther 695: print_sock_details(struct kinfo_file *kf)
1.72 guenther 696: {
697: if (kf->so_family == AF_INET)
698: print_inet_details(kf);
699: #ifdef INET6
700: else if (kf->so_family == AF_INET6)
701: print_inet6_details(kf);
702: #endif
703: }
704:
705: void
1.76 guenther 706: socktrans(struct kinfo_file *kf)
1.1 deraadt 707: {
708: static char *stypename[] = {
709: "unused", /* 0 */
1.38 deraadt 710: "stream", /* 1 */
1.1 deraadt 711: "dgram", /* 2 */
712: "raw", /* 3 */
713: "rdm", /* 4 */
714: "seqpak" /* 5 */
715: };
716: #define STYPEMAX 5
1.66 chl 717: char *stype, stypebuf[24];
1.1 deraadt 718:
1.65 millert 719: PREFIX(kf->fd_fd);
1.1 deraadt 720:
1.65 millert 721: if (kf->so_type > STYPEMAX) {
722: snprintf(stypebuf, sizeof(stypebuf), "?%d", kf->so_type);
723: stype = stypebuf;
724: } else {
725: stype = stypename[kf->so_type];
1.1 deraadt 726: }
727:
1.34 deraadt 728: /*
1.1 deraadt 729: * protocol specific formatting
730: *
731: * Try to find interesting things to print. For tcp, the interesting
732: * thing is the address of the tcpcb, for udp and others, just the
733: * inpcb (socket pcb). For unix domain, its the address of the socket
734: * pcb and the address of the connected pcb (if connected). Otherwise
735: * just print the protocol number and address of the socket itself.
736: * The idea is not to duplicate netstat, but to make available enough
737: * information for further analysis.
738: */
1.65 millert 739: switch (kf->so_family) {
1.1 deraadt 740: case AF_INET:
1.65 millert 741: printf("* internet %s", stype);
742: getinetproto(kf->so_protocol);
1.72 guenther 743: print_inet_details(kf);
1.1 deraadt 744: break;
1.26 itojun 745: #ifdef INET6
746: case AF_INET6:
1.65 millert 747: printf("* internet6 %s", stype);
748: getinetproto(kf->so_protocol);
1.72 guenther 749: print_inet6_details(kf);
1.26 itojun 750: break;
751: #endif
1.1 deraadt 752: case AF_UNIX:
753: /* print address of pcb and connected pcb */
1.65 millert 754: printf("* unix %s", stype);
755: if (kf->so_pcb) {
1.74 deraadt 756: printf(" ");
757: hide((void *)(uintptr_t)kf->so_pcb);
1.65 millert 758: if (kf->unp_conn) {
1.1 deraadt 759: char shoconn[4], *cp;
760:
761: cp = shoconn;
1.65 millert 762: if (!(kf->so_state & SS_CANTRCVMORE))
1.1 deraadt 763: *cp++ = '<';
764: *cp++ = '-';
1.65 millert 765: if (!(kf->so_state & SS_CANTSENDMORE))
1.1 deraadt 766: *cp++ = '>';
767: *cp = '\0';
1.74 deraadt 768: printf(" %s ", shoconn);
769: hide((void *)(uintptr_t)kf->unp_conn);
1.1 deraadt 770: }
771: }
772: break;
1.65 millert 773: case AF_MPLS:
774: /* print protocol number and socket address */
775: printf("* mpls %s", stype);
1.74 deraadt 776: printf(" %d ", kf->so_protocol);
777: hide((void *)(uintptr_t)kf->f_data);
1.65 millert 778: break;
779: case AF_ROUTE:
780: /* print protocol number and socket address */
781: printf("* route %s", stype);
1.74 deraadt 782: printf(" %d ", kf->so_protocol);
783: hide((void *)(uintptr_t)kf->f_data);
1.65 millert 784: break;
1.1 deraadt 785: default:
786: /* print protocol number and socket address */
1.65 millert 787: printf("* %d %s", kf->so_family, stype);
1.74 deraadt 788: printf(" %d ", kf->so_protocol);
789: hide((void *)(uintptr_t)kf->f_data);
1.72 guenther 790: }
791: if (kf->so_splice != 0 || kf->so_splicelen == -1) {
1.76 guenther 792: struct kinfo_file *from, *to;
1.72 guenther 793:
794: from = splice_find('<', kf->f_data);
795: to = NULL;
796: if (kf->so_splice != 0)
797: to = splice_find('>', kf->so_splice);
798:
799: if (to != NULL && from == to) {
800: printf(" <==>");
801: print_sock_details(to);
802: } else if (kf->so_splice != 0) {
803: printf(" ==>");
804: if (to != NULL)
805: print_sock_details(to);
806: } else if (kf->so_splicelen == -1) {
807: printf(" <==");
808: if (from != NULL)
809: print_sock_details(from);
810: }
1.1 deraadt 811: }
1.56 mickey 812: if (sflg)
1.65 millert 813: printf("\t%8llu %8llu",
814: (kf->f_rxfer + kf->f_rwfer),
815: (kf->f_rbytes + kf->f_wbytes) / 1024);
1.1 deraadt 816: printf("\n");
817: }
818:
819: /*
820: * getinetproto --
821: * print name of protocol number
822: */
823: void
1.85 mestre 824: getinetproto(int number)
1.1 deraadt 825: {
1.24 deraadt 826: static int isopen;
1.32 mpech 827: struct protoent *pe;
1.1 deraadt 828:
1.24 deraadt 829: if (!isopen)
830: setprotoent(++isopen);
831: if ((pe = getprotobynumber(number)) != NULL)
832: printf(" %s", pe->p_name);
833: else
1.1 deraadt 834: printf(" %d", number);
835: }
836:
1.11 millert 837: int
1.40 deraadt 838: getfname(char *filename)
1.1 deraadt 839: {
1.68 millert 840: static struct statfs *mntbuf;
841: static int nmounts;
842: int i;
843: struct stat sb;
844: struct filearg *cur;
1.1 deraadt 845:
1.68 millert 846: if (stat(filename, &sb)) {
1.27 millert 847: warn("%s", filename);
1.68 millert 848: return (0);
1.1 deraadt 849: }
850:
1.68 millert 851: /*
852: * POSIX specifies "For block special devices, all processes using any
853: * file on that device are listed". However the -f flag description
854: * states "The report shall be only for the named files", so we only
855: * look up a block device if the -f flag has not be specified.
856: */
857: if (fuser && !fsflg && S_ISBLK(sb.st_mode)) {
858: if (mntbuf == NULL) {
859: nmounts = getmntinfo(&mntbuf, MNT_NOWAIT);
860: if (nmounts == -1)
861: err(1, "getmntinfo");
862: }
863: for (i = 0; i < nmounts; i++) {
864: if (!strcmp(mntbuf[i].f_mntfromname, filename)) {
865: if (stat(mntbuf[i].f_mntonname, &sb) == -1) {
866: warn("%s", filename);
867: return (0);
868: }
869: cflg = 1;
870: break;
871: }
872: }
873: }
874:
875: if ((cur = malloc(sizeof(*cur))) == NULL)
876: err(1, NULL);
877:
878: cur->ino = sb.st_ino;
879: cur->dev = sb.st_dev & 0xffff;
1.1 deraadt 880: cur->name = filename;
1.68 millert 881: TAILQ_INIT(&cur->fusers);
882: SLIST_INSERT_HEAD(&fileargs, cur, next);
883: return (1);
884: }
885:
886: int
887: signame_to_signum(char *sig)
888: {
889: int n;
890: const char *errstr = NULL;
891:
892: if (isdigit((unsigned char)*sig)) {
893: n = strtonum(sig, 0, NSIG - 1, &errstr);
894: return (errstr ? -1 : n);
895: }
896: if (!strncasecmp(sig, "sig", 3))
897: sig += 3;
898: for (n = 1; n < NSIG; n++) {
899: if (!strcasecmp(sys_signame[n], sig))
900: return (n);
901: }
902: return (-1);
1.1 deraadt 903: }
904:
905: void
1.40 deraadt 906: usage(void)
1.1 deraadt 907: {
1.68 millert 908: if (fuser) {
909: fprintf(stderr, "usage: fuser [-cfku] [-M core] "
910: "[-N system] [-s signal] file ...\n");
911: } else {
912: fprintf(stderr, "usage: fstat [-fnosv] [-M core] [-N system] "
913: "[-p pid] [-u user] [file ...]\n");
914: }
1.1 deraadt 915: exit(1);
916: }