Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 26 Jan 2012 18:25:24 +0300
From:      Sergey Kandaurov <pluknet@gmail.com>
To:        FreeBSD Current <freebsd-current@freebsd.org>
Subject:   [patch] fstat: print the owner process for UNIX domain sockets
Message-ID:  <CAE-mSOLuDTRgjGZcrSqi8o9JqPLggO0bjW9nvcP6nZ5g%2B3Ot8w@mail.gmail.com>

next in thread | raw e-mail | index | archive | help
--f46d043c077e3e1d7004b76ffde2
Content-Type: text/plain; charset=ISO-8859-1

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)
@@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
 #include <sys/socketvar.h>
 #include <sys/sysctl.h>
 #include <sys/queue.h>
+#include <sys/un.h>

 #include <netinet/in.h>

@@ -224,28 +225,53 @@ 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

--f46d043c077e3e1d7004b76ffde2
Content-Type: application/octet-stream; name="fstat_un.diff"
Content-Disposition: attachment; filename="fstat_un.diff"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gxvxo1v60

SW5kZXg6IHVzci5iaW4vZnN0YXQvZnN0YXQuYwo9PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci0tLSB1c3IuYmluL2ZzdGF0
L2ZzdGF0LmMJKHJldmlzaW9uIDIzMDUwNSkKKysrIHVzci5iaW4vZnN0YXQvZnN0YXQuYwkod29y
a2luZyBjb3B5KQpAQCAtMzgsNiArMzgsNyBAQCBfX0ZCU0RJRCgiJEZyZWVCU0QkIik7CiAjaW5j
bHVkZSA8c3lzL3NvY2tldHZhci5oPgogI2luY2x1ZGUgPHN5cy9zeXNjdG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wogCQkJfQogCQlpZiAoZnNt
YXRjaCA9PSAwIHx8IChmaWxlbmFtZSA9PSBOVUxMICYmIGZzZmxnID09IDApKQogCQkJcmV0dXJu
Owo=
--f46d043c077e3e1d7004b76ffde2--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAE-mSOLuDTRgjGZcrSqi8o9JqPLggO0bjW9nvcP6nZ5g%2B3Ot8w>