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