2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
5 * Copyright (c) 1988, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/socketvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/queue.h>
44 #include <netinet/in.h>
49 #include <libprocstat.h>
60 #include "functions.h"
62 static int fsflg, /* show files on same filesystem as file(s) argument */
63 pflg, /* show files open by a particular pid */
64 uflg; /* show files open by a particular (effective) user */
65 static int checkfile; /* restrict to particular files or filesystems */
66 static int nflg; /* (numerical) display f.s. and rdev as dev_t */
67 static int mflg; /* include memory-mapped files */
68 static int vflg; /* be verbose */
78 static char *memf, *nlistf;
80 static int getfname(const char *filename);
81 static void dofiles(struct procstat *procstat, struct kinfo_proc *p);
82 static void print_access_flags(int flags);
83 static void print_file_info(struct procstat *procstat,
84 struct filestat *fst, const char *uname, const char *cmd, int pid);
85 static void print_pipe_info(struct procstat *procstat,
86 struct filestat *fst);
87 static void print_pts_info(struct procstat *procstat,
88 struct filestat *fst);
89 static void print_sem_info(struct procstat *procstat,
90 struct filestat *fst);
91 static void print_shm_info(struct procstat *procstat,
92 struct filestat *fst);
93 static void print_socket_info(struct procstat *procstat,
94 struct filestat *fst);
95 static void print_vnode_info(struct procstat *procstat,
96 struct filestat *fst);
97 static void usage(void) __dead2;
100 do_fstat(int argc, char **argv)
102 struct kinfo_proc *p;
103 struct passwd *passwd;
104 struct procstat *procstat;
109 what = KERN_PROC_PROC;
110 nlistf = memf = NULL;
111 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
131 if (!isdigit(*optarg)) {
132 warnx("-p requires a process id");
135 what = KERN_PROC_PID;
141 if (!(passwd = getpwnam(optarg)))
142 errx(1, "%s: unknown uid", optarg);
143 what = KERN_PROC_UID;
144 arg = passwd->pw_uid;
154 if (*(argv += optind)) {
155 for (; *argv; ++argv) {
159 if (!checkfile) /* file(s) specified, but none accessible */
163 if (fsflg && !checkfile) {
164 /* -f with no files means use wd */
165 if (getfname(".") == 0)
171 procstat = procstat_open_kvm(nlistf, memf);
173 procstat = procstat_open_sysctl();
174 if (procstat == NULL)
175 errx(1, "procstat_open()");
176 p = procstat_getprocs(procstat, what, arg, &cnt);
178 errx(1, "procstat_getprocs()");
185 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
188 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
189 if (checkfile && fsflg == 0)
195 * Go through the process list.
197 for (i = 0; i < cnt; i++) {
198 if (p[i].ki_stat == SZOMB)
200 dofiles(procstat, &p[i]);
202 procstat_freeprocs(procstat, p);
203 procstat_close(procstat);
208 dofiles(struct procstat *procstat, struct kinfo_proc *kp)
212 struct filestat *fst;
213 struct filestat_list *head;
216 uname = user_from_uid(kp->ki_uid, 0);
220 head = procstat_getfiles(procstat, kp, mflg);
223 STAILQ_FOREACH(fst, head, next)
224 print_file_info(procstat, fst, uname, cmd, pid);
225 procstat_freefiles(procstat, head);
230 print_file_info(struct procstat *procstat, struct filestat *fst,
231 const char *uname, const char *cmd, int pid)
235 const char *filename;
236 int error, fsmatch = 0;
237 char errbuf[_POSIX2_LINE_MAX];
240 if (checkfile != 0) {
241 if (fst->fs_type != PS_FST_TYPE_VNODE &&
242 fst->fs_type != PS_FST_TYPE_FIFO)
244 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
248 for (d = devs; d != NULL; d = d->next)
249 if (d->fsid == vn.vn_fsid) {
251 if (d->ino == vn.vn_fileid) {
256 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
261 * Print entry prefix.
263 printf("%-8.8s %-10s %5d", uname, cmd, pid);
264 if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
266 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
268 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
270 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
272 else if (fst->fs_uflags & PS_FST_UFLAG_MMAP)
274 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
276 else if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
279 printf(" %4d", fst->fs_fd);
282 * Print type-specific data.
284 switch (fst->fs_type) {
285 case PS_FST_TYPE_FIFO:
286 case PS_FST_TYPE_VNODE:
287 print_vnode_info(procstat, fst);
289 case PS_FST_TYPE_SOCKET:
290 print_socket_info(procstat, fst);
292 case PS_FST_TYPE_PIPE:
293 print_pipe_info(procstat, fst);
295 case PS_FST_TYPE_PTS:
296 print_pts_info(procstat, fst);
298 case PS_FST_TYPE_SHM:
299 print_shm_info(procstat, fst);
301 case PS_FST_TYPE_SEM:
302 print_sem_info(procstat, fst);
307 "unknown file type %d for file %d of pid %d\n",
308 fst->fs_type, fst->fs_fd, pid);
310 if (filename && !fsflg)
311 printf(" %s", filename);
316 print_socket_info(struct procstat *procstat, struct filestat *fst)
318 static const char *stypename[] = {
327 struct sockstat sock;
329 char errbuf[_POSIX2_LINE_MAX];
333 error = procstat_get_socket_info(procstat, fst, &sock, errbuf);
338 if (sock.type > STYPEMAX)
339 printf("* %s ?%d", sock.dname, sock.type);
341 printf("* %s %s", sock.dname, stypename[sock.type]);
344 * protocol specific formatting
346 * Try to find interesting things to print. For tcp, the interesting
347 * thing is the address of the tcpcb, for udp and others, just the
348 * inpcb (socket pcb). For unix domain, its the address of the socket
349 * pcb and the address of the connected pcb (if connected). Otherwise
350 * just print the protocol number and address of the socket itself.
351 * The idea is not to duplicate netstat, but to make available enough
352 * information for further analysis.
354 switch (sock.dom_family) {
358 setprotoent(++isopen);
359 if ((pe = getprotobynumber(sock.proto)) != NULL)
360 printf(" %s", pe->p_name);
362 printf(" %d", sock.proto);
363 if (sock.proto == IPPROTO_TCP ) {
364 if (sock.inp_ppcb != 0)
365 printf(" %lx", (u_long)sock.inp_ppcb);
367 else if (sock.so_pcb != 0)
368 printf(" %lx", (u_long)sock.so_pcb);
371 /* print address of pcb and connected pcb */
372 if (sock.so_pcb != 0) {
373 printf(" %lx", (u_long)sock.so_pcb);
375 char shoconn[4], *cp;
378 if (!(sock.so_rcv_sb_state & SBS_CANTRCVMORE))
381 if (!(sock.so_snd_sb_state & SBS_CANTSENDMORE))
384 printf(" %s %lx", shoconn,
385 (u_long)sock.unp_conn);
390 /* print protocol number and socket address */
391 printf(" %d %lx", sock.proto, (u_long)sock.so_addr);
396 print_pipe_info(struct procstat *procstat, struct filestat *fst)
399 char errbuf[_POSIX2_LINE_MAX];
402 error = procstat_get_pipe_info(procstat, fst, &ps, errbuf);
407 printf("* pipe %8lx <-> %8lx", (u_long)ps.addr, (u_long)ps.peer);
408 printf(" %6zd", ps.buffer_cnt);
409 print_access_flags(fst->fs_fflags);
413 print_pts_info(struct procstat *procstat, struct filestat *fst)
416 char errbuf[_POSIX2_LINE_MAX];
419 error = procstat_get_pts_info(procstat, fst, &pts, errbuf);
424 printf("* pseudo-terminal master ");
425 if (nflg || !*pts.devname) {
426 printf("%#10jx", (uintmax_t)pts.dev);
428 printf("%10s", pts.devname);
430 print_access_flags(fst->fs_fflags);
434 print_sem_info(struct procstat *procstat, struct filestat *fst)
437 char errbuf[_POSIX2_LINE_MAX];
441 error = procstat_get_sem_info(procstat, fst, &sem, errbuf);
448 (void)snprintf(mode, sizeof(mode), "%o", sem.mode);
450 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
451 strmode(sem.mode, mode);
453 printf(" %10s %6u", mode, sem.value);
454 print_access_flags(fst->fs_fflags);
458 print_shm_info(struct procstat *procstat, struct filestat *fst)
461 char errbuf[_POSIX2_LINE_MAX];
465 error = procstat_get_shm_info(procstat, fst, &shm, errbuf);
472 (void)snprintf(mode, sizeof(mode), "%o", shm.mode);
474 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
475 strmode(shm.mode, mode);
477 printf(" %10s %6ju", mode, shm.size);
478 print_access_flags(fst->fs_fflags);
482 print_vnode_info(struct procstat *procstat, struct filestat *fst)
485 char errbuf[_POSIX2_LINE_MAX];
491 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
494 else if (vn.vn_type == PS_FST_VTYPE_VBAD)
496 else if (vn.vn_type == PS_FST_VTYPE_VNON)
498 if (badtype != NULL) {
499 printf(" - - %10s -", badtype);
504 printf(" %#5jx", (uintmax_t)vn.vn_fsid);
505 else if (vn.vn_mntdir != NULL)
506 (void)printf(" %-8s", vn.vn_mntdir);
512 (void)snprintf(mode, sizeof(mode), "%o", vn.vn_mode);
514 strmode(vn.vn_mode, mode);
516 (void)printf(" %6jd %10s", (intmax_t)vn.vn_fileid, mode);
518 if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) {
519 if (nflg || !*vn.vn_devname)
520 printf(" %#6jx", (uintmax_t)vn.vn_dev);
522 printf(" %6s", vn.vn_devname);
525 printf(" %6ju", (uintmax_t)vn.vn_size);
526 print_access_flags(fst->fs_fflags);
530 print_access_flags(int flags)
535 if (flags & PS_FST_FFLAG_READ)
537 if (flags & PS_FST_FFLAG_WRITE)
543 getfname(const char *filename)
548 if (stat(filename, &statbuf)) {
549 warn("%s", filename);
552 if ((cur = malloc(sizeof(DEVS))) == NULL)
557 cur->ino = statbuf.st_ino;
558 cur->fsid = statbuf.st_dev;
559 cur->name = filename;
566 (void)fprintf(stderr,
567 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");