[patch] fstat: print the owner process for UNIX domain sockets

From: Sergey Kandaurov <pluknet_at_gmail.com>
Date: Thu, 26 Jan 2012 18:25:24 +0300
Hi.
This patch adds support for fstat(1) to identify which process opened the
specified socket file, to bring in line with regular files. E.g.:

# fstat /var/run/devd.pipe
USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W NAME
root     devd        1259    4* local stream fffffe0002a1a870
/var/run/devd.pipe
# fstat /var/run/logpriv
USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W NAME
root     syslogd     1495    5* local dgram fffffe0002a1a3c0  /var/run/logpriv

Comments are welcome.

Index: usr.bin/fstat/fstat.c
===================================================================
--- usr.bin/fstat/fstat.c       (revision 230505)
+++ usr.bin/fstat/fstat.c       (working copy)
_at__at_ -38,6 +38,7 _at__at_ __FBSDID("$FreeBSD$");
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/queue.h>
+#include <sys/un.h>

 #include <netinet/in.h>

_at__at_ -224,28 +225,53 _at__at_ static void
 print_file_info(struct procstat *procstat, struct filestat *fst,
     const char *uname, const char *cmd, int pid)
 {
+       struct sockstat sock;
        struct vnstat vn;
        DEVS *d;
        const char *filename;
        int error, fsmatch = 0;
        char errbuf[_POSIX2_LINE_MAX];

+       error = 0;
        filename = NULL;
        if (checkfile != 0) {
-               if (fst->fs_type != PS_FST_TYPE_VNODE &&
-                   fst->fs_type != PS_FST_TYPE_FIFO)
+               switch (fst->fs_type) {
+               case PS_FST_TYPE_VNODE:
+               case PS_FST_TYPE_FIFO:
+                       error = procstat_get_vnode_info(procstat, fst,
&vn, errbuf);
+                       break;
+               case PS_FST_TYPE_SOCKET:
+                       error = procstat_get_socket_info(procstat,
fst, &sock, errbuf);
+                       break;
+               default:
                        return;
-               error = procstat_get_vnode_info(procstat, fst, &vn, errbuf);
+               }
                if (error != 0)
                        return;

                for (d = devs; d != NULL; d = d->next)
-                       if (d->fsid == vn.vn_fsid) {
-                               fsmatch = 1;
-                               if ((unsigned)d->ino == vn.vn_fileid) {
-                                       filename = d->name;
-                                       break;
+                       switch (fst->fs_type) {
+                       case PS_FST_TYPE_VNODE:
+                       case PS_FST_TYPE_FIFO:
+                               if (d->fsid == vn.vn_fsid) {
+                                       fsmatch = 1;
+                                       if ((unsigned)d->ino == vn.vn_fileid) {
+                                               filename = d->name;
+                                               break;
+                                       }
                                }
+                               break;
+                       case PS_FST_TYPE_SOCKET:
+                               if (sock.dom_family == AF_UNIX) {
+                                       fsmatch = 1;
+                                       if (strcmp(((struct sockaddr_un *)
+                                           (&sock.sa_local))->sun_path,
+                                           d->name) == 0) {
+                                               filename = d->name;
+                                               break;
+                                       }
+                               }
+                               break;
                        }
                if (fsmatch == 0 || (filename == NULL && fsflg == 0))
                        return;

-- 
wbr,
pluknet

Received on Thu Jan 26 2012 - 14:25:25 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:40:23 UTC