1 /*-
2 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
3 * Copyright (c) 1988, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/user.h>
36 #include <sys/stat.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/queue.h>
41
42 #include <netinet/in.h>
43
44 #include <assert.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <libprocstat.h>
48 #include <limits.h>
49 #include <pwd.h>
50 #include <stdint.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <stddef.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <netdb.h>
57
58 #include "functions.h"
59
60 static int fsflg, /* show files on same filesystem as file(s) argument */
61 pflg, /* show files open by a particular pid */
62 uflg; /* show files open by a particular (effective) user */
63 static int checkfile; /* restrict to particular files or filesystems */
64 static int nflg; /* (numerical) display f.s. and rdev as dev_t */
65 static int mflg; /* include memory-mapped files */
66 static int vflg; /* be verbose */
67
68 typedef struct devs {
69 struct devs *next;
70 uint32_t fsid;
71 uint64_t ino;
72 const char *name;
73 } DEVS;
74
75 static DEVS *devs;
76 static char *memf, *nlistf;
77
78 static int getfname(const char *filename);
79 static void dofiles(struct procstat *procstat, struct kinfo_proc *p);
80 static void print_access_flags(int flags);
81 static void print_file_info(struct procstat *procstat,
82 struct filestat *fst, const char *uname, const char *cmd, int pid);
83 static void print_pipe_info(struct procstat *procstat,
84 struct filestat *fst);
85 static void print_pts_info(struct procstat *procstat,
86 struct filestat *fst);
87 static void print_sem_info(struct procstat *procstat,
88 struct filestat *fst);
89 static void print_shm_info(struct procstat *procstat,
90 struct filestat *fst);
91 static void print_socket_info(struct procstat *procstat,
92 struct filestat *fst);
93 static void print_vnode_info(struct procstat *procstat,
94 struct filestat *fst);
95 static void print_port_info(struct procstat *procstat,
96 struct filestat *fst);
97 static void print_portset_info(struct procstat *procstat,
98 struct filestat *fst);
99 static void usage(void) __dead2;
100
101 int
do_fstat(int argc,char ** argv)102 do_fstat(int argc, char **argv)
103 {
104 struct kinfo_proc *p;
105 struct passwd *passwd;
106 struct procstat *procstat;
107 int arg, ch, what;
108 int cnt, i;
109
110 arg = 0;
111 what = KERN_PROC_PROC;
112 nlistf = memf = NULL;
113 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
114 switch((char)ch) {
115 case 'f':
116 fsflg = 1;
117 break;
118 case 'M':
119 memf = optarg;
120 break;
121 case 'N':
122 nlistf = optarg;
123 break;
124 case 'm':
125 mflg = 1;
126 break;
127 case 'n':
128 nflg = 1;
129 break;
130 case 'p':
131 if (pflg++)
132 usage();
133 if (!isdigit(*optarg)) {
134 warnx("-p requires a process id");
135 usage();
136 }
137 what = KERN_PROC_PID;
138 arg = atoi(optarg);
139 break;
140 case 'u':
141 if (uflg++)
142 usage();
143 if (!(passwd = getpwnam(optarg)))
144 errx(1, "%s: unknown uid", optarg);
145 what = KERN_PROC_UID;
146 arg = passwd->pw_uid;
147 break;
148 case 'v':
149 vflg = 1;
150 break;
151 case '?':
152 default:
153 usage();
154 }
155
156 if (*(argv += optind)) {
157 for (; *argv; ++argv) {
158 if (getfname(*argv))
159 checkfile = 1;
160 }
161 if (!checkfile) /* file(s) specified, but none accessible */
162 exit(1);
163 }
164
165 if (fsflg && !checkfile) {
166 /* -f with no files means use wd */
167 if (getfname(".") == 0)
168 exit(1);
169 checkfile = 1;
170 }
171
172 if (memf != NULL)
173 procstat = procstat_open_kvm(nlistf, memf);
174 else
175 procstat = procstat_open_sysctl();
176 if (procstat == NULL)
177 errx(1, "procstat_open()");
178 p = procstat_getprocs(procstat, what, arg, &cnt);
179 if (p == NULL)
180 errx(1, "procstat_getprocs()");
181
182 /*
183 * Print header.
184 */
185 if (nflg)
186 printf("%s",
187 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
188 else
189 printf("%s",
190 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
191 if (checkfile && fsflg == 0)
192 printf(" NAME\n");
193 else
194 putchar('\n');
195
196 /*
197 * Go through the process list.
198 */
199 for (i = 0; i < cnt; i++) {
200 if (p[i].ki_stat == SZOMB)
201 continue;
202 dofiles(procstat, &p[i]);
203 }
204 procstat_freeprocs(procstat, p);
205 procstat_close(procstat);
206 return (0);
207 }
208
209 static void
dofiles(struct procstat * procstat,struct kinfo_proc * kp)210 dofiles(struct procstat *procstat, struct kinfo_proc *kp)
211 {
212 const char *cmd;
213 const char *uname;
214 struct filestat *fst;
215 struct filestat_list *head;
216 int pid;
217
218 uname = user_from_uid(kp->ki_uid, 0);
219 pid = kp->ki_pid;
220 cmd = kp->ki_comm;
221
222 head = procstat_getfiles(procstat, kp, mflg);
223 if (head == NULL)
224 return;
225 STAILQ_FOREACH(fst, head, next)
226 print_file_info(procstat, fst, uname, cmd, pid);
227 procstat_freefiles(procstat, head);
228 }
229
230
231 static void
print_file_info(struct procstat * procstat,struct filestat * fst,const char * uname,const char * cmd,int pid)232 print_file_info(struct procstat *procstat, struct filestat *fst,
233 const char *uname, const char *cmd, int pid)
234 {
235 struct vnstat vn;
236 DEVS *d;
237 const char *filename;
238 int error, fsmatch = 0;
239 char errbuf[_POSIX2_LINE_MAX];
240
241 filename = NULL;
242 if (checkfile != 0) {
243 if (fst->fs_type != PS_FST_TYPE_VNODE &&
244 fst->fs_type != PS_FST_TYPE_FIFO)
245 return;
246 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
247 if (error != 0)
248 return;
249
250 for (d = devs; d != NULL; d = d->next)
251 if (d->fsid == vn.vn_fsid) {
252 fsmatch = 1;
253 if (d->ino == vn.vn_fileid) {
254 filename = d->name;
255 break;
256 }
257 }
258 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
259 return;
260 }
261
262 /*
263 * Print entry prefix.
264 */
265 printf("%-8.8s %-10s %5d", uname, cmd, pid);
266 if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
267 printf(" text");
268 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
269 printf(" wd");
270 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
271 printf(" root");
272 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
273 printf(" tr");
274 else if (fst->fs_uflags & PS_FST_UFLAG_MMAP)
275 printf(" mmap");
276 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
277 printf(" jail");
278 else if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
279 printf(" ctty");
280 else
281 printf(" %4d", fst->fs_fd);
282
283 /*
284 * Print type-specific data.
285 */
286 switch (fst->fs_type) {
287 case PS_FST_TYPE_FIFO:
288 case PS_FST_TYPE_VNODE:
289 print_vnode_info(procstat, fst);
290 break;
291 case PS_FST_TYPE_SOCKET:
292 print_socket_info(procstat, fst);
293 break;
294 case PS_FST_TYPE_PIPE:
295 print_pipe_info(procstat, fst);
296 break;
297 case PS_FST_TYPE_PTS:
298 print_pts_info(procstat, fst);
299 break;
300 case PS_FST_TYPE_SHM:
301 print_shm_info(procstat, fst);
302 break;
303 case PS_FST_TYPE_SEM:
304 print_sem_info(procstat, fst);
305 break;
306 case PS_FST_TYPE_PORT:
307 print_port_info(procstat, fst);
308 break;
309 case PS_FST_TYPE_PORTSET:
310 print_portset_info(procstat, fst);
311 break;
312 default:
313 if (vflg)
314 fprintf(stderr,
315 "unknown file type %d for file %d of pid %d\n",
316 fst->fs_type, fst->fs_fd, pid);
317 }
318 if (filename && !fsflg)
319 printf(" %s", filename);
320 putchar('\n');
321 }
322
323 static void
print_socket_info(struct procstat * procstat,struct filestat * fst)324 print_socket_info(struct procstat *procstat, struct filestat *fst)
325 {
326 static const char *stypename[] = {
327 "unused", /* 0 */
328 "stream", /* 1 */
329 "dgram", /* 2 */
330 "raw", /* 3 */
331 "rdm", /* 4 */
332 "seqpak" /* 5 */
333 };
334 #define STYPEMAX 5
335 struct sockstat sock;
336 struct protoent *pe;
337 char errbuf[_POSIX2_LINE_MAX];
338 int error;
339 static int isopen;
340
341 error = procstat_get_socket_info(procstat, fst, &sock, errbuf);
342 if (error != 0) {
343 printf("* error");
344 return;
345 }
346 if (sock.type > STYPEMAX)
347 printf("* %s ?%d", sock.dname, sock.type);
348 else
349 printf("* %s %s", sock.dname, stypename[sock.type]);
350
351 /*
352 * protocol specific formatting
353 *
354 * Try to find interesting things to print. For tcp, the interesting
355 * thing is the address of the tcpcb, for udp and others, just the
356 * inpcb (socket pcb). For unix domain, its the address of the socket
357 * pcb and the address of the connected pcb (if connected). Otherwise
358 * just print the protocol number and address of the socket itself.
359 * The idea is not to duplicate netstat, but to make available enough
360 * information for further analysis.
361 */
362 switch (sock.dom_family) {
363 case AF_INET:
364 case AF_INET6:
365 if (!isopen)
366 setprotoent(++isopen);
367 if ((pe = getprotobynumber(sock.proto)) != NULL)
368 printf(" %s", pe->p_name);
369 else
370 printf(" %d", sock.proto);
371 if (sock.proto == IPPROTO_TCP ) {
372 if (sock.inp_ppcb != 0)
373 printf(" %lx", (u_long)sock.inp_ppcb);
374 }
375 else if (sock.so_pcb != 0)
376 printf(" %lx", (u_long)sock.so_pcb);
377 break;
378 case AF_UNIX:
379 /* print address of pcb and connected pcb */
380 if (sock.so_pcb != 0) {
381 printf(" %lx", (u_long)sock.so_pcb);
382 if (sock.unp_conn) {
383 char shoconn[4], *cp;
384
385 cp = shoconn;
386 if (!(sock.so_rcv_sb_state & SBS_CANTRCVMORE))
387 *cp++ = '<';
388 *cp++ = '-';
389 if (!(sock.so_snd_sb_state & SBS_CANTSENDMORE))
390 *cp++ = '>';
391 *cp = '\0';
392 printf(" %s %lx", shoconn,
393 (u_long)sock.unp_conn);
394 }
395 }
396 break;
397 default:
398 /* print protocol number and socket address */
399 printf(" %d %lx", sock.proto, (u_long)sock.so_addr);
400 }
401 }
402
403 static void
print_pipe_info(struct procstat * procstat,struct filestat * fst)404 print_pipe_info(struct procstat *procstat, struct filestat *fst)
405 {
406 struct pipestat ps;
407 char errbuf[_POSIX2_LINE_MAX];
408 int error;
409
410 error = procstat_get_pipe_info(procstat, fst, &ps, errbuf);
411 if (error != 0) {
412 printf("* error");
413 return;
414 }
415 printf("* pipe %8lx <-> %8lx", (u_long)ps.addr, (u_long)ps.peer);
416 printf(" %6zd", ps.buffer_cnt);
417 print_access_flags(fst->fs_fflags);
418 }
419
420 static void
print_pts_info(struct procstat * procstat,struct filestat * fst)421 print_pts_info(struct procstat *procstat, struct filestat *fst)
422 {
423 struct ptsstat pts;
424 char errbuf[_POSIX2_LINE_MAX];
425 int error;
426
427 error = procstat_get_pts_info(procstat, fst, &pts, errbuf);
428 if (error != 0) {
429 printf("* error");
430 return;
431 }
432 printf("* pseudo-terminal master ");
433 if (nflg || !*pts.devname) {
434 printf("%#10jx", (uintmax_t)pts.dev);
435 } else {
436 printf("%10s", pts.devname);
437 }
438 print_access_flags(fst->fs_fflags);
439 }
440
441 static void
print_sem_info(struct procstat * procstat,struct filestat * fst)442 print_sem_info(struct procstat *procstat, struct filestat *fst)
443 {
444 struct semstat sem;
445 char errbuf[_POSIX2_LINE_MAX];
446 char mode[15];
447 int error;
448
449 error = procstat_get_sem_info(procstat, fst, &sem, errbuf);
450 if (error != 0) {
451 printf("* error");
452 return;
453 }
454 if (nflg) {
455 printf(" ");
456 (void)snprintf(mode, sizeof(mode), "%o", sem.mode);
457 } else {
458 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
459 strmode(sem.mode, mode);
460 }
461 printf(" %10s %6u", mode, sem.value);
462 print_access_flags(fst->fs_fflags);
463 }
464
465 static void
print_shm_info(struct procstat * procstat,struct filestat * fst)466 print_shm_info(struct procstat *procstat, struct filestat *fst)
467 {
468 struct shmstat shm;
469 char errbuf[_POSIX2_LINE_MAX];
470 char mode[15];
471 int error;
472
473 error = procstat_get_shm_info(procstat, fst, &shm, errbuf);
474 if (error != 0) {
475 printf("* error");
476 return;
477 }
478 if (nflg) {
479 printf(" ");
480 (void)snprintf(mode, sizeof(mode), "%o", shm.mode);
481 } else {
482 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
483 strmode(shm.mode, mode);
484 }
485 printf(" %10s %6ju", mode, shm.size);
486 print_access_flags(fst->fs_fflags);
487 }
488
489 static void
print_vnode_info(struct procstat * procstat,struct filestat * fst)490 print_vnode_info(struct procstat *procstat, struct filestat *fst)
491 {
492 struct vnstat vn;
493 char errbuf[_POSIX2_LINE_MAX];
494 char mode[15];
495 const char *badtype;
496 int error;
497
498 badtype = NULL;
499 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
500 if (error != 0)
501 badtype = errbuf;
502 else if (vn.vn_type == PS_FST_VTYPE_VBAD)
503 badtype = "bad";
504 else if (vn.vn_type == PS_FST_VTYPE_VNON)
505 badtype = "none";
506 if (badtype != NULL) {
507 printf(" - - %10s -", badtype);
508 return;
509 }
510
511 if (nflg)
512 printf(" %#5jx", (uintmax_t)vn.vn_fsid);
513 else if (vn.vn_mntdir != NULL)
514 (void)printf(" %-8s", vn.vn_mntdir);
515
516 /*
517 * Print access mode.
518 */
519 if (nflg)
520 (void)snprintf(mode, sizeof(mode), "%o", vn.vn_mode);
521 else {
522 strmode(vn.vn_mode, mode);
523 }
524 (void)printf(" %6jd %10s", (intmax_t)vn.vn_fileid, mode);
525
526 if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) {
527 if (nflg || !*vn.vn_devname)
528 printf(" %#6jx", (uintmax_t)vn.vn_dev);
529 else {
530 printf(" %6s", vn.vn_devname);
531 }
532 } else
533 printf(" %6ju", (uintmax_t)vn.vn_size);
534 print_access_flags(fst->fs_fflags);
535 }
536
537
538 static void
print_port_info(struct procstat * procstat __unused,struct filestat * fst __unused)539 print_port_info(struct procstat *procstat __unused, struct filestat *fst __unused)
540 {
541
542 printf(" port");
543 }
544
545 static void
print_portset_info(struct procstat * procstat __unused,struct filestat * fst __unused)546 print_portset_info(struct procstat *procstat __unused, struct filestat *fst __unused)
547 {
548
549 printf(" pset");
550 }
551
552 static void
print_access_flags(int flags)553 print_access_flags(int flags)
554 {
555 char rw[3];
556
557 rw[0] = '\0';
558 if (flags & PS_FST_FFLAG_READ)
559 strcat(rw, "r");
560 if (flags & PS_FST_FFLAG_WRITE)
561 strcat(rw, "w");
562 printf(" %2s", rw);
563 }
564
565 int
getfname(const char * filename)566 getfname(const char *filename)
567 {
568 struct stat statbuf;
569 DEVS *cur;
570
571 if (stat(filename, &statbuf)) {
572 warn("%s", filename);
573 return (0);
574 }
575 if ((cur = malloc(sizeof(DEVS))) == NULL)
576 err(1, NULL);
577 cur->next = devs;
578 devs = cur;
579
580 cur->ino = statbuf.st_ino;
581 cur->fsid = statbuf.st_dev;
582 cur->name = filename;
583 return (1);
584 }
585
586 static void
usage(void)587 usage(void)
588 {
589 (void)fprintf(stderr,
590 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");
591 exit(1);
592 }
593