[PATCH:xscope] Add support for process ID and names

Peter Wu peter at lekensteyn.nl
Wed Aug 5 08:17:17 PDT 2015


Adds the process ID and name of the other side. Useful if you have
multiple clients. Support for OpenBSD could be added in theory since
it has a similar "struct sockpeercred" structure (not sure about
process name though).

After this change, the process ID and name (when available) will be
appended between parentheses:

     0.01: Client (pid 23433 xdpyinfo) -->    4 bytes
             ............REQUEST: ListExtensions
     0.01:                                   332 bytes <-- X11 Server (pid 8290 Xorg)
                                             ..............REPLY: ListExtensions
                                                           names: (29)

Signed-off-by: Peter Wu <peter at lekensteyn.nl>
---
Hi,

In presence of multiple clients, it is nice to immediately recognize the origin.
This patch adds that for Unix domain sockets on Linux. It should also be
possible to do this on OpenBSD (add ifdef __OpenBSD__ and use "struct
sockpeercred cred"), but I cannot test that nor do I know how to retrieve the
command name.

The buffer sizes are quite arbitrary (but chosen to be "large enough"). I have
picked the FDDescriptor struct to store these properties since these are per-fd
properties. For clarity, the functions are decomposed this way and put in a
separate file rather than polluting fd.c.

Kind regards.
Peter
---
 Makefile.am |   1 +
 fd.c        |  12 +++++++
 fd.h        |   4 +++
 peerinfo.c  | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 scope.c     |  14 +++++++--
 5 files changed, 131 insertions(+), 3 deletions(-)
 create mode 100644 peerinfo.c

diff --git a/Makefile.am b/Makefile.am
index 8a61eaf..5b4a3fd 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -48,6 +48,7 @@ xscope_SOURCES =	\
 	glxscope.h \
 	lbxscope.h \
 	nas.h \
+	peerinfo.c \
 	print11.c \
 	print_bigreq.c \
 	print_glx.c \
diff --git a/fd.c b/fd.c
index a9f6d4f..d07d3a3 100644
--- a/fd.c
+++ b/fd.c
@@ -136,6 +136,14 @@ InitializeFD(void)
 
 /* ************************************************************ */
 
+const char *
+GetPeerInfo(FD fd)
+{
+    return FDD[fd].peer_info;
+}
+
+/* ************************************************************ */
+
 void
 UsingFD(FD fd,
         void (*Handler) (int),
@@ -152,6 +160,7 @@ UsingFD(FD fd,
 #ifdef USE_XTRANS
     FDD[fd].trans_conn = trans_conn;
 #endif
+    FDD[fd].peer_info = ReadPeerInfo(fd);
     if (Handler == NULL)
         FD_CLR(fd, &ReadDescriptors); /* clear fd bit */
     else
@@ -180,6 +189,9 @@ NotUsingFD(FD fd)
     FDD[fd].Busy = false;
     FD_CLR(fd, &ReadDescriptors); /* clear fd bit */
 
+    free(FDD[fd].peer_info);
+    FDD[fd].peer_info = NULL;
+
     while (!FDD[HighestFD].Busy && HighestFD > 0)
         HighestFD -= 1;
 
diff --git a/fd.h b/fd.h
index 59b4889..a3ddbce 100644
--- a/fd.h
+++ b/fd.h
@@ -75,6 +75,7 @@ struct FDDescriptor {
 #ifdef USE_XTRANS
     XtransConnInfo trans_conn;
 #endif
+    char *peer_info;
 };
 
 extern struct FDDescriptor *FDD;        /* array of FD descriptors */
@@ -103,4 +104,7 @@ extern int MainLoop(void);
 extern XtransConnInfo GetXTransConnInfo(FD fd);
 #endif
 
+extern char *ReadPeerInfo(FD fd);
+extern const char *GetPeerInfo(FD fd);
+
 #endif                          /* XSCOPE_FD_H */
diff --git a/peerinfo.c b/peerinfo.c
new file mode 100644
index 0000000..2e9c8e0
--- /dev/null
+++ b/peerinfo.c
@@ -0,0 +1,103 @@
+/*
+ *    Routine to provide information about the other side of a FD.
+ *
+ * Copyright (C) 2015 Peter Wu <peter at lekensteyn.nl>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
+
+#define _GNU_SOURCE             /* for struct ucred on Linux */
+#include "scope.h"
+
+#include <unistd.h>
+#include <stdio.h>
+#include <sys/types.h>          /* for pid_t */
+
+
+static pid_t
+GetPidFromFd(FD fd)
+{
+#ifdef SO_PEERCRED
+    struct ucred cred;
+    socklen_t cred_len = sizeof(cred);
+
+    if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) == 0 &&
+        cred_len == sizeof(cred)) {
+        return cred.pid;
+    }
+#endif
+    return 0;
+}
+
+static const char *
+GetProcnameForPid(pid_t pid)
+{
+    /* comm is no longer than TASK_COMM_LEN (16) */
+    static char comm[17];
+    char comm_path[32];
+    char comm_fd;
+    unsigned comm_len = 0;
+
+    snprintf(comm_path, sizeof(comm_path), "/proc/%d/comm", pid);
+    comm_fd = open(comm_path, O_RDONLY);
+    if (comm_fd != -1) {
+        comm_len = read(comm_fd, comm, sizeof(comm) - 1);
+        if (comm_len < 0)
+            comm_len = 0;
+        close(comm_fd);
+    }
+
+    if (comm_len > 0 && comm[comm_len - 1] == '\n')
+        comm_len--;
+    comm[comm_len] = '\0';
+    return comm;
+}
+
+/*
+   Retrieves additional information about the other side of the fd.
+   For Unix domain sockets, this results in the process ID and name.
+ */
+char *
+ReadPeerInfo(FD fd)
+{
+    /* should hold enough for "PID <number> <command name>" */
+    char peer_info[40];
+    unsigned pos;
+    pid_t pid;
+    const char *comm;
+
+    pid = GetPidFromFd(fd);
+    /* Use the fact that pid_t is the first field of struct ucred. */
+    if (pid) {
+        pos = snprintf(peer_info, sizeof(peer_info), "pid %d", pid);
+
+        comm = GetProcnameForPid(pid);
+        if (comm)
+            pos += snprintf(peer_info + pos, sizeof(peer_info) - pos,
+                            " %s", comm);
+
+        return strdup(peer_info);
+    }
+
+    /* (Could add port number here for AF_INET if it helps.) */
+
+    return NULL;
+}
diff --git a/scope.c b/scope.c
index 1919569..73a0858 100644
--- a/scope.c
+++ b/scope.c
@@ -1011,11 +1011,19 @@ ServerHalf(FD fd)
 const char *
 ClientName(FD fd)
 {
-    static char name[12];
+    static char name[62];
+    unsigned pos = 0;
+    const char *peer_info;
 
     if (clientNumber <= 1)
-        return ("");
-    snprintf(name, sizeof(name), " %d", FDinfo[fd].ClientNumber);
+        name[0] = '\0';
+    else
+        pos = snprintf(name, sizeof(name), " %d", FDinfo[fd].ClientNumber);
+
+    peer_info = GetPeerInfo(fd);
+    if (peer_info)
+        snprintf(name + pos, sizeof(name) - pos, " (%s)", peer_info);
+
     return (name);
 }
 
-- 
2.5.0



More information about the xorg-devel mailing list