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/param.h>
36 #include <sys/socket.h>
37 #include <sys/socketvar.h>
38 #include <sys/sysctl.h>
39 #include <sys/queue.h>
42 #include <netinet/in.h>
44 #include <arpa/inet.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 sflg, /* show socket details */
65 uflg; /* show files open by a particular (effective) user */
66 static int checkfile; /* restrict to particular files or filesystems */
67 static int nflg; /* (numerical) display f.s. and rdev as dev_t */
68 static int mflg; /* include memory-mapped files */
69 static int vflg; /* be verbose */
79 static char *memf, *nlistf;
81 static int getfname(const char *filename);
82 static void dofiles(struct procstat *procstat, struct kinfo_proc *p);
83 static void print_access_flags(int flags);
84 static void print_file_info(struct procstat *procstat,
85 struct filestat *fst, const char *uname, const char *cmd, int pid);
86 static void print_pipe_info(struct procstat *procstat,
87 struct filestat *fst);
88 static void print_pts_info(struct procstat *procstat,
89 struct filestat *fst);
90 static void print_sem_info(struct procstat *procstat,
91 struct filestat *fst);
92 static void print_shm_info(struct procstat *procstat,
93 struct filestat *fst);
94 static void print_socket_info(struct procstat *procstat,
95 struct filestat *fst);
96 static void print_vnode_info(struct procstat *procstat,
97 struct filestat *fst);
98 static void usage(void) __dead2;
101 do_fstat(int argc, char **argv)
103 struct kinfo_proc *p;
104 struct passwd *passwd;
105 struct procstat *procstat;
110 what = KERN_PROC_PROC;
111 nlistf = memf = NULL;
112 while ((ch = getopt(argc, argv, "fmnp:su:vN:M:")) != -1)
132 if (!isdigit(*optarg)) {
133 warnx("-p requires a process id");
136 what = KERN_PROC_PID;
145 if (!(passwd = getpwnam(optarg)))
146 errx(1, "%s: unknown uid", optarg);
147 what = KERN_PROC_UID;
148 arg = passwd->pw_uid;
158 if (*(argv += optind)) {
159 for (; *argv; ++argv) {
163 if (!checkfile) /* file(s) specified, but none accessible */
167 if (fsflg && !checkfile) {
168 /* -f with no files means use wd */
169 if (getfname(".") == 0)
175 procstat = procstat_open_kvm(nlistf, memf);
177 procstat = procstat_open_sysctl();
178 if (procstat == NULL)
179 errx(1, "procstat_open()");
180 p = procstat_getprocs(procstat, what, arg, &cnt);
182 errx(1, "procstat_getprocs()");
189 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
192 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
193 if (checkfile && fsflg == 0)
199 * Go through the process list.
201 for (i = 0; i < cnt; i++) {
202 if (p[i].ki_stat == SZOMB)
204 dofiles(procstat, &p[i]);
206 procstat_freeprocs(procstat, p);
207 procstat_close(procstat);
212 dofiles(struct procstat *procstat, struct kinfo_proc *kp)
216 struct filestat *fst;
217 struct filestat_list *head;
220 uname = user_from_uid(kp->ki_uid, 0);
224 head = procstat_getfiles(procstat, kp, mflg);
227 STAILQ_FOREACH(fst, head, next)
228 print_file_info(procstat, fst, uname, cmd, pid);
229 procstat_freefiles(procstat, head);
234 print_file_info(struct procstat *procstat, struct filestat *fst,
235 const char *uname, const char *cmd, int pid)
239 const char *filename;
240 int error, fsmatch = 0;
241 char errbuf[_POSIX2_LINE_MAX];
244 if (checkfile != 0) {
245 if (fst->fs_type != PS_FST_TYPE_VNODE &&
246 fst->fs_type != PS_FST_TYPE_FIFO)
248 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
252 for (d = devs; d != NULL; d = d->next)
253 if (d->fsid == vn.vn_fsid) {
255 if (d->ino == vn.vn_fileid) {
260 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
265 * Print entry prefix.
267 printf("%-8.8s %-10s %5d", uname, cmd, pid);
268 if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
270 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
272 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
274 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
276 else if (fst->fs_uflags & PS_FST_UFLAG_MMAP)
278 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
280 else if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
283 printf(" %4d", fst->fs_fd);
286 * Print type-specific data.
288 switch (fst->fs_type) {
289 case PS_FST_TYPE_FIFO:
290 case PS_FST_TYPE_VNODE:
291 print_vnode_info(procstat, fst);
293 case PS_FST_TYPE_SOCKET:
294 print_socket_info(procstat, fst);
296 case PS_FST_TYPE_PIPE:
297 print_pipe_info(procstat, fst);
299 case PS_FST_TYPE_PTS:
300 print_pts_info(procstat, fst);
302 case PS_FST_TYPE_SHM:
303 print_shm_info(procstat, fst);
305 case PS_FST_TYPE_SEM:
306 print_sem_info(procstat, fst);
308 case PS_FST_TYPE_DEV:
313 "unknown file type %d for file %d of pid %d\n",
314 fst->fs_type, fst->fs_fd, pid);
316 if (filename && !fsflg)
317 printf(" %s", filename);
322 addr_to_string(struct sockaddr_storage *ss, char *buffer, int buflen)
324 char buffer2[INET6_ADDRSTRLEN];
325 struct sockaddr_in6 *sin6;
326 struct sockaddr_in *sin;
327 struct sockaddr_un *sun;
329 switch (ss->ss_family) {
331 sun = (struct sockaddr_un *)ss;
332 if (strlen(sun->sun_path) == 0)
333 strlcpy(buffer, "-", buflen);
335 strlcpy(buffer, sun->sun_path, buflen);
339 sin = (struct sockaddr_in *)ss;
340 if (sin->sin_addr.s_addr == INADDR_ANY)
341 snprintf(buffer, buflen, "%s:%d", "*",
342 ntohs(sin->sin_port));
343 else if (inet_ntop(AF_INET, &sin->sin_addr, buffer2,
344 sizeof(buffer2)) != NULL)
345 snprintf(buffer, buflen, "%s:%d", buffer2,
346 ntohs(sin->sin_port));
350 sin6 = (struct sockaddr_in6 *)ss;
351 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
352 snprintf(buffer, buflen, "%s.%d", "*",
353 ntohs(sin6->sin6_port));
354 else if (inet_ntop(AF_INET6, &sin6->sin6_addr, buffer2,
355 sizeof(buffer2)) != NULL)
356 snprintf(buffer, buflen, "%s.%d", buffer2,
357 ntohs(sin6->sin6_port));
359 strlcpy(buffer, "-", buflen);
363 strlcpy(buffer, "", buflen);
371 print_socket_info(struct procstat *procstat, struct filestat *fst)
373 static const char *stypename[] = {
382 struct sockstat sock;
384 char errbuf[_POSIX2_LINE_MAX];
385 char src_addr[PATH_MAX], dst_addr[PATH_MAX];
386 struct sockaddr_un *sun;
390 error = procstat_get_socket_info(procstat, fst, &sock, errbuf);
395 if (sock.type > STYPEMAX)
396 printf("* %s ?%d", sock.dname, sock.type);
398 printf("* %s %s", sock.dname, stypename[sock.type]);
401 * protocol specific formatting
403 * Try to find interesting things to print. For tcp, the interesting
404 * thing is the address of the tcpcb, for udp and others, just the
405 * inpcb (socket pcb). For unix domain, its the address of the socket
406 * pcb and the address of the connected pcb (if connected). Otherwise
407 * just print the protocol number and address of the socket itself.
408 * The idea is not to duplicate netstat, but to make available enough
409 * information for further analysis.
411 switch (sock.dom_family) {
415 setprotoent(++isopen);
416 if ((pe = getprotobynumber(sock.proto)) != NULL)
417 printf(" %s", pe->p_name);
419 printf(" %d", sock.proto);
420 if (sock.proto == IPPROTO_TCP ) {
421 if (sock.inp_ppcb != 0)
422 printf(" %lx", (u_long)sock.inp_ppcb);
424 else if (sock.so_pcb != 0)
425 printf(" %lx", (u_long)sock.so_pcb);
429 addr_to_string(&sock.sa_local, src_addr, sizeof(src_addr)),
430 addr_to_string(&sock.sa_peer, dst_addr, sizeof(dst_addr)));
433 /* print address of pcb and connected pcb */
434 if (sock.so_pcb != 0) {
435 printf(" %lx", (u_long)sock.so_pcb);
437 char shoconn[4], *cp;
440 if (!(sock.so_rcv_sb_state & SBS_CANTRCVMORE))
443 if (!(sock.so_snd_sb_state & SBS_CANTSENDMORE))
446 printf(" %s %lx", shoconn,
447 (u_long)sock.unp_conn);
452 sun = (struct sockaddr_un *)&sock.sa_local;
454 * While generally we like to print two addresses,
455 * local and peer, for sockets, it turns out to be
456 * more useful to print the first non-null address for
457 * local sockets, as typically they aren't bound and
458 * connected, and the path strings can get long.
460 if (sun->sun_path[0] != 0)
461 addr_to_string(&sock.sa_local,
462 src_addr, sizeof(src_addr));
464 addr_to_string(&sock.sa_peer,
465 src_addr, sizeof(src_addr));
466 printf(" %s", src_addr);
469 /* print protocol number and socket address */
470 printf(" %d %lx", sock.proto, (u_long)sock.so_addr);
475 print_pipe_info(struct procstat *procstat, struct filestat *fst)
478 char errbuf[_POSIX2_LINE_MAX];
481 error = procstat_get_pipe_info(procstat, fst, &ps, errbuf);
486 printf("* pipe %8lx <-> %8lx", (u_long)ps.addr, (u_long)ps.peer);
487 printf(" %6zd", ps.buffer_cnt);
488 print_access_flags(fst->fs_fflags);
492 print_pts_info(struct procstat *procstat, struct filestat *fst)
495 char errbuf[_POSIX2_LINE_MAX];
498 error = procstat_get_pts_info(procstat, fst, &pts, errbuf);
503 printf("* pseudo-terminal master ");
504 if (nflg || !*pts.devname) {
505 printf("%#10jx", (uintmax_t)pts.dev);
507 printf("%10s", pts.devname);
509 print_access_flags(fst->fs_fflags);
513 print_sem_info(struct procstat *procstat, struct filestat *fst)
516 char errbuf[_POSIX2_LINE_MAX];
520 error = procstat_get_sem_info(procstat, fst, &sem, errbuf);
527 (void)snprintf(mode, sizeof(mode), "%o", sem.mode);
529 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
530 strmode(sem.mode, mode);
532 printf(" %10s %6u", mode, sem.value);
533 print_access_flags(fst->fs_fflags);
537 print_shm_info(struct procstat *procstat, struct filestat *fst)
540 char errbuf[_POSIX2_LINE_MAX];
544 error = procstat_get_shm_info(procstat, fst, &shm, errbuf);
551 (void)snprintf(mode, sizeof(mode), "%o", shm.mode);
553 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
554 strmode(shm.mode, mode);
556 printf(" %10s %6ju", mode, shm.size);
557 print_access_flags(fst->fs_fflags);
561 print_vnode_info(struct procstat *procstat, struct filestat *fst)
564 char errbuf[_POSIX2_LINE_MAX];
570 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
573 else if (vn.vn_type == PS_FST_VTYPE_VBAD)
575 else if (vn.vn_type == PS_FST_VTYPE_VNON)
577 if (badtype != NULL) {
578 printf(" - - %10s -", badtype);
583 printf(" %#5jx", (uintmax_t)vn.vn_fsid);
584 else if (vn.vn_mntdir != NULL)
585 (void)printf(" %-8s", vn.vn_mntdir);
591 (void)snprintf(mode, sizeof(mode), "%o", vn.vn_mode);
593 strmode(vn.vn_mode, mode);
595 (void)printf(" %6jd %10s", (intmax_t)vn.vn_fileid, mode);
597 if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) {
598 if (nflg || !*vn.vn_devname)
599 printf(" %#6jx", (uintmax_t)vn.vn_dev);
601 printf(" %6s", vn.vn_devname);
604 printf(" %6ju", (uintmax_t)vn.vn_size);
605 print_access_flags(fst->fs_fflags);
609 print_access_flags(int flags)
614 if (flags & PS_FST_FFLAG_READ)
616 if (flags & PS_FST_FFLAG_WRITE)
622 getfname(const char *filename)
627 if (stat(filename, &statbuf)) {
628 warn("%s", filename);
631 if ((cur = malloc(sizeof(DEVS))) == NULL)
636 cur->ino = statbuf.st_ino;
637 cur->fsid = statbuf.st_dev;
638 cur->name = filename;
645 (void)fprintf(stderr,
646 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");