From: Alexandre Montplaisir Date: Thu, 2 Jun 2016 07:05:31 +0000 (-0400) Subject: Fix: Handle both agent config files pointing to same port X-Git-Tag: v2.9.0-rc1~71 X-Git-Url: http://git.liburcu.org/?p=lttng-ust.git;a=commitdiff_plain;h=e0c010a92d16f71a26a1e076a07ca52d60d4e810 Fix: Handle both agent config files pointing to same port The expected locations for the user and root agent sessiond configuration files are ~/.lttng/agent.port and /var/run/lttng/agent.port, respectively. These files indicate which port an agent should connect to to reach its respective sessiond. If by some bad luck both files indicate the same port, then both Java TCP clients would end up connecting to the same sessiond, resulting in weird results, like "lttng list" listing all events twice. Make sure the target ports are different, and avoid duplicate connections in case there are not. Signed-off-by: Alexandre Montplaisir Signed-off-by: Mathieu Desnoyers --- diff --git a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java index 84ad973f..793a5f8e 100644 --- a/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java +++ b/liblttng-ust-java-agent/java/lttng-ust-agent-common/org/lttng/ust/agent/client/LttngTcpSessiondClient.java @@ -158,23 +158,27 @@ public class LttngTcpSessiondClient implements Runnable { } private void connectToSessiond() throws IOException { - int port; + int rootPort = getPortFromFile(ROOT_PORT_FILE); + int userPort = getPortFromFile(getHomePath() + USER_PORT_FILE); - if (this.isRoot) { - port = getPortFromFile(ROOT_PORT_FILE); - if (port == 0) { - /* No session daemon available. Stop and retry later. */ - throw new IOException(); - } - } else { - port = getPortFromFile(getHomePath() + USER_PORT_FILE); - if (port == 0) { - /* No session daemon available. Stop and retry later. */ - throw new IOException(); - } + /* + * Check for the edge case of both files existing but pointing to the + * same port. In this case, let the root client handle it. + */ + if ((rootPort != 0) && (rootPort == userPort) && (!isRoot)) { + log("User and root config files both point to port " + rootPort + + ". Letting the root client handle it."); + throw new IOException(); + } + + int portToUse = (isRoot ? rootPort : userPort); + + if (portToUse == 0) { + /* No session daemon available. Stop and retry later. */ + throw new IOException(); } - this.sessiondSock = new Socket(SESSION_HOST, port); + this.sessiondSock = new Socket(SESSION_HOST, portToUse); this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream()); this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream()); }