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>
43 #include <netinet/in.h>
48 #include <libprocstat.h>
59 #include "functions.h"
61 int fsflg, /* show files on same filesystem as file(s) argument */
62 pflg, /* show files open by a particular pid */
63 uflg; /* show files open by a particular (effective) user */
64 int checkfile; /* true if restricting to particular files or filesystems */
65 int nflg; /* (numerical) display f.s. and rdev as dev_t */
66 int mflg; /* include memory-mapped files */
67 int vflg; /* be verbose */
79 static int getfname(const char *filename);
80 static void dofiles(struct procstat *procstat, struct kinfo_proc *p);
81 static void print_access_flags(int flags);
82 static void print_file_info(struct procstat *procstat,
83 struct filestat *fst, const char *uname, const char *cmd, int pid);
84 static void print_pipe_info(struct procstat *procstat,
85 struct filestat *fst);
86 static void print_pts_info(struct procstat *procstat,
87 struct filestat *fst);
88 static void print_socket_info(struct procstat *procstat,
89 struct filestat *fst);
90 static void print_vnode_info(struct procstat *procstat,
91 struct filestat *fst);
92 static void usage(void) __dead2;
95 do_fstat(int argc, char **argv)
98 struct passwd *passwd;
99 struct procstat *procstat;
104 what = KERN_PROC_PROC;
105 nlistf = memf = NULL;
106 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
126 if (!isdigit(*optarg)) {
127 warnx("-p requires a process id");
130 what = KERN_PROC_PID;
136 if (!(passwd = getpwnam(optarg)))
137 errx(1, "%s: unknown uid", optarg);
138 what = KERN_PROC_UID;
139 arg = passwd->pw_uid;
149 if (*(argv += optind)) {
150 for (; *argv; ++argv) {
154 if (!checkfile) /* file(s) specified, but none accessable */
158 if (fsflg && !checkfile) {
159 /* -f with no files means use wd */
160 if (getfname(".") == 0)
166 procstat = procstat_open_kvm(nlistf, memf);
168 procstat = procstat_open_sysctl();
169 if (procstat == NULL)
170 errx(1, "procstat_open()");
171 p = procstat_getprocs(procstat, what, arg, &cnt);
173 errx(1, "procstat_getprocs()");
180 "USER CMD PID FD DEV INUM MODE SZ|DV R/W");
183 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W");
184 if (checkfile && fsflg == 0)
190 * Go through the process list.
192 for (i = 0; i < cnt; i++) {
193 if (p[i].ki_stat == SZOMB)
195 dofiles(procstat, &p[i]);
197 procstat_freeprocs(procstat, p);
198 procstat_close(procstat);
203 dofiles(struct procstat *procstat, struct kinfo_proc *kp)
207 struct filestat *fst;
208 struct filestat_list *head;
211 uname = user_from_uid(kp->ki_uid, 0);
215 head = procstat_getfiles(procstat, kp, mflg);
218 STAILQ_FOREACH(fst, head, next)
219 print_file_info(procstat, fst, uname, cmd, pid);
220 procstat_freefiles(procstat, head);
225 print_file_info(struct procstat *procstat, struct filestat *fst,
226 const char *uname, const char *cmd, int pid)
228 struct sockstat sock;
231 const char *filename;
232 int error, fsmatch = 0;
233 char errbuf[_POSIX2_LINE_MAX];
237 if (checkfile != 0) {
238 switch (fst->fs_type) {
239 case PS_FST_TYPE_VNODE:
240 case PS_FST_TYPE_FIFO:
241 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
243 case PS_FST_TYPE_SOCKET:
244 error = procstat_get_socket_info(procstat, fst, &sock, errbuf);
252 for (d = devs; d != NULL; d = d->next)
253 switch (fst->fs_type) {
254 case PS_FST_TYPE_VNODE:
255 case PS_FST_TYPE_FIFO:
256 if (d->fsid == vn.vn_fsid) {
258 if ((unsigned)d->ino == vn.vn_fileid) {
264 case PS_FST_TYPE_SOCKET:
265 if (sock.dom_family == AF_UNIX) {
267 if (strcmp(((struct sockaddr_un *)
268 (&sock.sa_local))->sun_path,
276 if (fsmatch == 0 || (filename == NULL && fsflg == 0))
281 * Print entry prefix.
283 printf("%-8.8s %-10s %5d", uname, cmd, pid);
284 if (fst->fs_uflags & PS_FST_UFLAG_TEXT)
286 else if (fst->fs_uflags & PS_FST_UFLAG_CDIR)
288 else if (fst->fs_uflags & PS_FST_UFLAG_RDIR)
290 else if (fst->fs_uflags & PS_FST_UFLAG_TRACE)
292 else if (fst->fs_uflags & PS_FST_UFLAG_MMAP)
294 else if (fst->fs_uflags & PS_FST_UFLAG_JAIL)
296 else if (fst->fs_uflags & PS_FST_UFLAG_CTTY)
299 printf(" %4d", fst->fs_fd);
302 * Print type-specific data.
304 switch (fst->fs_type) {
305 case PS_FST_TYPE_FIFO:
306 case PS_FST_TYPE_VNODE:
307 print_vnode_info(procstat, fst);
309 case PS_FST_TYPE_SOCKET:
310 print_socket_info(procstat, fst);
312 case PS_FST_TYPE_PIPE:
313 print_pipe_info(procstat, fst);
315 case PS_FST_TYPE_PTS:
316 print_pts_info(procstat, fst);
321 "unknown file type %d for file %d of pid %d\n",
322 fst->fs_type, fst->fs_fd, pid);
324 if (filename && !fsflg)
325 printf(" %s", filename);
330 print_socket_info(struct procstat *procstat, struct filestat *fst)
332 static const char *stypename[] = {
341 struct sockstat sock;
343 char errbuf[_POSIX2_LINE_MAX];
347 error = procstat_get_socket_info(procstat, fst, &sock, errbuf);
352 if (sock.type > STYPEMAX)
353 printf("* %s ?%d", sock.dname, sock.type);
355 printf("* %s %s", sock.dname, stypename[sock.type]);
358 * protocol specific formatting
360 * Try to find interesting things to print. For tcp, the interesting
361 * thing is the address of the tcpcb, for udp and others, just the
362 * inpcb (socket pcb). For unix domain, its the address of the socket
363 * pcb and the address of the connected pcb (if connected). Otherwise
364 * just print the protocol number and address of the socket itself.
365 * The idea is not to duplicate netstat, but to make available enough
366 * information for further analysis.
368 switch (sock.dom_family) {
372 setprotoent(++isopen);
373 if ((pe = getprotobynumber(sock.proto)) != NULL)
374 printf(" %s", pe->p_name);
376 printf(" %d", sock.proto);
377 if (sock.proto == IPPROTO_TCP ) {
378 if (sock.inp_ppcb != 0)
379 printf(" %lx", (u_long)sock.inp_ppcb);
381 else if (sock.so_pcb != 0)
382 printf(" %lx", (u_long)sock.so_pcb);
385 /* print address of pcb and connected pcb */
386 if (sock.so_pcb != 0) {
387 printf(" %lx", (u_long)sock.so_pcb);
389 char shoconn[4], *cp;
392 if (!(sock.so_rcv_sb_state & SBS_CANTRCVMORE))
395 if (!(sock.so_snd_sb_state & SBS_CANTSENDMORE))
398 printf(" %s %lx", shoconn,
399 (u_long)sock.unp_conn);
404 /* print protocol number and socket address */
405 printf(" %d %lx", sock.proto, (u_long)sock.so_addr);
410 print_pipe_info(struct procstat *procstat, struct filestat *fst)
413 char errbuf[_POSIX2_LINE_MAX];
416 error = procstat_get_pipe_info(procstat, fst, &ps, errbuf);
421 printf("* pipe %8lx <-> %8lx", (u_long)ps.addr, (u_long)ps.peer);
422 printf(" %6zd", ps.buffer_cnt);
423 print_access_flags(fst->fs_fflags);
427 print_pts_info(struct procstat *procstat, struct filestat *fst)
430 char errbuf[_POSIX2_LINE_MAX];
433 error = procstat_get_pts_info(procstat, fst, &pts, errbuf);
438 printf("* pseudo-terminal master ");
439 if (nflg || !*pts.devname) {
440 printf("%10d,%-2d", major(pts.dev), minor(pts.dev));
442 printf("%10s", pts.devname);
444 print_access_flags(fst->fs_fflags);
448 print_vnode_info(struct procstat *procstat, struct filestat *fst)
451 char errbuf[_POSIX2_LINE_MAX];
457 error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
460 else if (vn.vn_type == PS_FST_VTYPE_VBAD)
462 else if (vn.vn_type == PS_FST_VTYPE_VNON)
464 if (badtype != NULL) {
465 printf(" - - %10s -", badtype);
470 printf(" %2d,%-2d", major(vn.vn_fsid), minor(vn.vn_fsid));
471 else if (vn.vn_mntdir != NULL)
472 (void)printf(" %-8s", vn.vn_mntdir);
478 (void)snprintf(mode, sizeof(mode), "%o", vn.vn_mode);
480 strmode(vn.vn_mode, mode);
482 (void)printf(" %6jd %10s", (intmax_t)vn.vn_fileid, mode);
484 if (vn.vn_type == PS_FST_VTYPE_VBLK || vn.vn_type == PS_FST_VTYPE_VCHR) {
485 if (nflg || !*vn.vn_devname)
486 printf(" %2d,%-2d", major(vn.vn_dev), minor(vn.vn_dev));
488 printf(" %6s", vn.vn_devname);
491 printf(" %6ju", (uintmax_t)vn.vn_size);
492 print_access_flags(fst->fs_fflags);
496 print_access_flags(int flags)
501 if (flags & PS_FST_FFLAG_READ)
503 if (flags & PS_FST_FFLAG_WRITE)
509 getfname(const char *filename)
514 if (stat(filename, &statbuf)) {
515 warn("%s", filename);
518 if ((cur = malloc(sizeof(DEVS))) == NULL)
523 cur->ino = statbuf.st_ino;
524 cur->fsid = statbuf.st_dev;
525 cur->name = filename;
532 (void)fprintf(stderr,
533 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");