diff mbox series

[ovs-dev,RFC,21/52] stream-unix: Give accepted sockets distinct names for log messages.

Message ID 20170919220125.32535-22-blp@ovn.org
State RFC
Headers show
Series clustering implementation | expand

Commit Message

Ben Pfaff Sept. 19, 2017, 10 p.m. UTC
At least on Linux, when process A connects to process B over a Unix
domain socket, unless process A bound its socket to a name before
it made the connection, process B gets an empty peer name.  Until
now, OVS has just reported the name of the connection as "unix".
This is not meaningful, of course.  I do not know of a good general
solution to this problem, but this commit attempts a step in the
right direction by at least giving each connection of this kind a
number: "unix#1", "unix#2", and so on.  That way, in log messages
one can at least see which messages are related to a particular
connection.

Signed-off-by: Ben Pfaff <blp@ovn.org>
---
 lib/stream-unix.c       | 15 ++++++++++++---
 tests/ofproto-macros.at |  1 +
 tests/ofproto.at        |  2 +-
 3 files changed, 14 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/lib/stream-unix.c b/lib/stream-unix.c
index 40ded19526f2..9261958fce35 100644
--- a/lib/stream-unix.c
+++ b/lib/stream-unix.c
@@ -26,6 +26,7 @@ 
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include "ovs-atomic.h"
 #include "packets.h"
 #include "poll-loop.h"
 #include "socket-util.h"
@@ -112,9 +113,17 @@  punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len,
 {
     const struct sockaddr_un *sun = (const struct sockaddr_un *) ss;
     int name_len = get_unix_name_len(sun, ss_len);
-    char *bound_name = (name_len > 0
-                        ? xasprintf("unix:%.*s", name_len, sun->sun_path)
-                        : xstrdup("unix"));
+    char *bound_name;
+
+    if (name_len > 0) {
+        bound_name = xasprintf("unix:%.*s", name_len, sun->sun_path);
+    } else {
+        /* When a Unix socket connects to us without first binding a name, we
+         * don't get any name for it.  It's useful nevertheless to be able to
+         * distinguish separate sockets in log messages, so use a counter. */
+        static atomic_count next_idx = ATOMIC_COUNT_INIT(0);
+        bound_name = xasprintf("unix#%u", atomic_count_inc(&next_idx));
+    }
     return new_fd_stream(bound_name, fd, 0, AF_UNIX, streamp);
 }
 
diff --git a/tests/ofproto-macros.at b/tests/ofproto-macros.at
index 38449db32ee0..17f85e703ccf 100644
--- a/tests/ofproto-macros.at
+++ b/tests/ofproto-macros.at
@@ -32,6 +32,7 @@  prt==1 { sub(/[ \t]*$/, ""); print $0 }
 vconn_sub() {
     sed '
 s/tcp:127.0.0.1:[0-9][0-9]*:/unix:/
+s/unix#[0-9]:/unix:/
 '
 }
 ]
diff --git a/tests/ofproto.at b/tests/ofproto.at
index 31cb5208fc1c..b8daa1018fd6 100644
--- a/tests/ofproto.at
+++ b/tests/ofproto.at
@@ -5217,7 +5217,7 @@  vconn|DBG|unix: sent (Success): NXST_FLOW reply:
  in_port=2,dl_src=00:66:77:88:99:aa actions=drop
 ])
 
-AT_CHECK([grep " flow_mods in the last " ovs-vswitchd.log | sed -e 's/^.*connmgr|INFO|//'], [0], [dnl
+AT_CHECK([grep " flow_mods in the last " ovs-vswitchd.log | sed -e 's/^.*connmgr|INFO|//' | vconn_sub], [0], [dnl
 br0<->unix: 1 flow_mods in the last 0 s (1 deletes)
 br0<->unix: 9 flow_mods in the last 0 s (7 adds, 2 deletes)
 br0<->unix: 2 flow_mods in the last 0 s (2 modifications)