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