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