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.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
37 #include <sys/socket.h>
38 #include <sys/socketvar.h>
39 #include <sys/sysctl.h>
40 #include <sys/queue.h>
42 #include <netinet/in.h>
47 #include <libprocstat.h>
58 #include "functions.h"
60 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 int checkfile; /* true if restricting to particular files or filesystems */
64 int nflg; /* (numerical) display f.s. and rdev as dev_t */
65 int mflg; /* include memory-mapped files */
66 int vflg; /* be verbose */
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_shm_info(struct procstat *procstat,
88 struct filestat *fst);
89 static void print_socket_info(struct procstat *procstat,
90 struct filestat *fst);
91 static void print_vnode_info(struct procstat *procstat,
92 struct filestat *fst);
93 static void usage(void) __dead2;
96 do_fstat(int argc, char **argv)
99 struct passwd *passwd;
100 struct procstat *procstat;
105 what = KERN_PROC_PROC;
106 nlistf = memf = NULL;
107 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
127 if (!isdigit(*optarg)) {
128 warnx("-p requires a process id");
131 what = KERN_PROC_PID;
137 if (!(passwd = getpwnam(optarg)))
138 errx(1, "%s: unknown uid", optarg);
139 what = KERN_PROC_UID;
140 arg = passwd->pw_uid;
150 if (*(argv += optind)) {
151 for (; *argv; ++argv) {
155 if (!checkfile) /* file(s) specified, but none accessable */
159 if (fsflg && !checkfile) {
160 /* -f with no files means use wd */
161 if (getfname(".") == 0)
167 procstat = procstat_open_kvm(nlistf, memf);
169 procstat = procstat_open_sysctl();
170 if (procstat == NULL)
171 errx(1, "procstat_open()");
172 p = procstat_getprocs(procstat, what, arg, &cnt);
174 errx(1, "procstat_getprocs()");
181 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
184 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
185 if (checkfile && fsflg == 0)
191 * Go through the process list.
193 for (i = 0; i < cnt; i++) {
194 if (p[i].ki_stat == SZOMB)
196 dofiles(procstat, &p[i]);
198 procstat_freeprocs(procstat, p);
199 procstat_close(procstat);
204 dofiles(struct procstat *procstat, struct kinfo_proc *kp)
208 struct filestat *fst;
209 struct filestat_list *head;
212 uname = user_from_uid(kp->ki_uid, 0);
216 head = procstat_getfiles(procstat, kp, mflg);
219 STAILQ_FOREACH(fst, head, next)
220 print_file_info(procstat, fst, uname, cmd, pid);
221 procstat_freefiles(procstat, head);
226 print_file_info(struct procstat *procstat, struct filestat *fst,
227 const char *uname, const char *cmd, int pid)
231 const char *filename;
232 int error, fsmatch = 0;
233 char errbuf[_POSIX2_LINE_MAX];
236 if (checkfile != 0) {
237 if (fst->fs_type != PS_FST_TYPE_VNODE &&
238 fst->fs_type != PS_FST_TYPE_FIFO)
240 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
244 for (d = devs; d != NULL; d = d->next)
245 if (d->fsid == vn.vn_fsid) {
247 if ((unsigned)d->ino == vn.vn_fileid) {
252 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
257 * Print entry prefix.
259 printf("%-8.8s %-10s %5d", uname, cmd, pid);
260 if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
262 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
264 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
266 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
268 else if (fst->fs_uflags & PS_FST_UFLAG_MMAP)
270 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
272 else if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
275 printf(" %4d", fst->fs_fd);
278 * Print type-specific data.
280 switch (fst->fs_type) {
281 case PS_FST_TYPE_FIFO:
282 case PS_FST_TYPE_VNODE:
283 print_vnode_info(procstat, fst);
285 case PS_FST_TYPE_SOCKET:
286 print_socket_info(procstat, fst);
288 case PS_FST_TYPE_PIPE:
289 print_pipe_info(procstat, fst);
291 case PS_FST_TYPE_PTS:
292 print_pts_info(procstat, fst);
294 case PS_FST_TYPE_SHM:
295 print_shm_info(procstat, fst);
300 "unknown file type %d for file %d of pid %d\n",
301 fst->fs_type, fst->fs_fd, pid);
303 if (filename && !fsflg)
304 printf(" %s", filename);
309 print_socket_info(struct procstat *procstat, struct filestat *fst)
311 static const char *stypename[] = {
320 struct sockstat sock;
322 char errbuf[_POSIX2_LINE_MAX];
326 error = procstat_get_socket_info(procstat, fst, &sock, errbuf);
331 if (sock.type > STYPEMAX)
332 printf("* %s ?%d", sock.dname, sock.type);
334 printf("* %s %s", sock.dname, stypename[sock.type]);
337 * protocol specific formatting
339 * Try to find interesting things to print. For tcp, the interesting
340 * thing is the address of the tcpcb, for udp and others, just the
341 * inpcb (socket pcb). For unix domain, its the address of the socket
342 * pcb and the address of the connected pcb (if connected). Otherwise
343 * just print the protocol number and address of the socket itself.
344 * The idea is not to duplicate netstat, but to make available enough
345 * information for further analysis.
347 switch (sock.dom_family) {
351 setprotoent(++isopen);
352 if ((pe = getprotobynumber(sock.proto)) != NULL)
353 printf(" %s", pe->p_name);
355 printf(" %d", sock.proto);
356 if (sock.proto == IPPROTO_TCP ) {
357 if (sock.inp_ppcb != 0)
358 printf(" %lx", (u_long)sock.inp_ppcb);
360 else if (sock.so_pcb != 0)
361 printf(" %lx", (u_long)sock.so_pcb);
364 /* print address of pcb and connected pcb */
365 if (sock.so_pcb != 0) {
366 printf(" %lx", (u_long)sock.so_pcb);
368 char shoconn[4], *cp;
371 if (!(sock.so_rcv_sb_state & SBS_CANTRCVMORE))
374 if (!(sock.so_snd_sb_state & SBS_CANTSENDMORE))
377 printf(" %s %lx", shoconn,
378 (u_long)sock.unp_conn);
383 /* print protocol number and socket address */
384 printf(" %d %lx", sock.proto, (u_long)sock.so_addr);
389 print_pipe_info(struct procstat *procstat, struct filestat *fst)
392 char errbuf[_POSIX2_LINE_MAX];
395 error = procstat_get_pipe_info(procstat, fst, &ps, errbuf);
400 printf("* pipe %8lx <-> %8lx", (u_long)ps.addr, (u_long)ps.peer);
401 printf(" %6zd", ps.buffer_cnt);
402 print_access_flags(fst->fs_fflags);
406 print_pts_info(struct procstat *procstat, struct filestat *fst)
409 char errbuf[_POSIX2_LINE_MAX];
412 error = procstat_get_pts_info(procstat, fst, &pts, errbuf);
417 printf("* pseudo-terminal master ");
418 if (nflg || !*pts.devname) {
419 printf("%10d,%-2d", major(pts.dev), minor(pts.dev));
421 printf("%10s", pts.devname);
423 print_access_flags(fst->fs_fflags);
427 print_shm_info(struct procstat *procstat, struct filestat *fst)
430 char errbuf[_POSIX2_LINE_MAX];
434 error = procstat_get_shm_info(procstat, fst, &shm, errbuf);
441 (void)snprintf(mode, sizeof(mode), "%o", shm.mode);
443 printf(" %-15s", fst->fs_path != NULL ? fst->fs_path : "-");
444 strmode(shm.mode, mode);
446 printf(" %10s %6ju", mode, shm.size);
447 print_access_flags(fst->fs_fflags);
451 print_vnode_info(struct procstat *procstat, struct filestat *fst)
454 char errbuf[_POSIX2_LINE_MAX];
460 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
463 else if (vn.vn_type == PS_FST_VTYPE_VBAD)
465 else if (vn.vn_type == PS_FST_VTYPE_VNON)
467 if (badtype != NULL) {
468 printf(" - - %10s -", badtype);
473 printf(" %2d,%-2d", major(vn.vn_fsid), minor(vn.vn_fsid));
474 else if (vn.vn_mntdir != NULL)
475 (void)printf(" %-8s", vn.vn_mntdir);
481 (void)snprintf(mode, sizeof(mode), "%o", vn.vn_mode);
483 strmode(vn.vn_mode, mode);
485 (void)printf(" %6jd %10s", (intmax_t)vn.vn_fileid, mode);
487 if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) {
488 if (nflg || !*vn.vn_devname)
489 printf(" %2d,%-2d", major(vn.vn_dev), minor(vn.vn_dev));
491 printf(" %6s", vn.vn_devname);
494 printf(" %6ju", (uintmax_t)vn.vn_size);
495 print_access_flags(fst->fs_fflags);
499 print_access_flags(int flags)
504 if (flags & PS_FST_FFLAG_READ)
506 if (flags & PS_FST_FFLAG_WRITE)
512 getfname(const char *filename)
517 if (stat(filename, &statbuf)) {
518 warn("%s", filename);
521 if ((cur = malloc(sizeof(DEVS))) == NULL)
526 cur->ino = statbuf.st_ino;
527 cur->fsid = statbuf.st_dev;
528 cur->name = filename;
535 (void)fprintf(stderr,
536 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");