Fix: Handle both agent config files pointing to same port
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / client / LttngTcpSessiondClient.java
index 5b5d50c4e3f360b563fb3fd2062934bdb47afac0..793a5f8e0b42cc12038117528376c96caa0134ff 100644 (file)
@@ -21,6 +21,7 @@ package org.lttng.ust.agent.client;
 import java.io.BufferedReader;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
@@ -32,6 +33,8 @@ import java.nio.ByteOrder;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
+import org.lttng.ust.agent.utils.LttngUstAgentLogger;
+
 /**
  * Client for agents to connect to a local session daemon, using a TCP socket.
  *
@@ -109,12 +112,14 @@ public class LttngTcpSessiondClient implements Runnable {
                                /*
                                 * Connect to the session daemon before anything else.
                                 */
+                               log("Connecting to sessiond");
                                connectToSessiond();
 
                                /*
                                 * Register to the session daemon as the Java component of the
                                 * UST application.
                                 */
+                               log("Registering to sessiond");
                                registerToSessiond();
 
                                /*
@@ -122,6 +127,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                 * session daemon. This will return if and only if there is a
                                 * fatal error or the socket closes.
                                 */
+                               log("Waiting on sessiond commands...");
                                handleSessiondCmd();
                        } catch (UnknownHostException uhe) {
                                uhe.printStackTrace();
@@ -139,6 +145,7 @@ public class LttngTcpSessiondClient implements Runnable {
         * Dispose this client and close any socket connection it may hold.
         */
        public void close() {
+               log("Closing client");
                this.quit = true;
 
                try {
@@ -151,29 +158,42 @@ 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);
+
+               /*
+                * 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();
+               }
 
-               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();
-                       }
+               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());
        }
 
        private static String getHomePath() {
-               return System.getProperty("user.home");
+               /*
+                * The environment variable LTTNG_HOME overrides HOME if
+                * defined.
+                */
+               String homePath = System.getenv("LTTNG_HOME");
+
+               if (homePath == null) {
+                       homePath = System.getProperty("user.home");
+               }
+               return homePath;
        }
 
        /**
@@ -184,9 +204,10 @@ public class LttngTcpSessiondClient implements Runnable {
        private static int getPortFromFile(String path) throws IOException {
                int port;
                BufferedReader br = null;
+               File file = new File(path);
 
                try {
-                       br = new BufferedReader(new FileReader(path));
+                       br = new BufferedReader(new FileReader(file));
                        String line = br.readLine();
                        port = Integer.parseInt(line, 10);
                        if (port < 0 || port > 65535) {
@@ -247,6 +268,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                 * We don't send any reply to the registration done command.
                                 * This just marks the end of the initial session setup.
                                 */
+                               log("Registration done");
                                continue;
                        }
                        case CMD_LIST:
@@ -254,6 +276,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                SessiondCommand listLoggerCmd = new SessiondListLoggersCommand();
                                LttngAgentResponse response = listLoggerCmd.execute(logAgent);
                                responseData = response.getBytes();
+                               log("Received list loggers command");
                                break;
                        }
                        case CMD_EVENT_ENABLE:
@@ -266,6 +289,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData);
                                LttngAgentResponse response = enableEventCmd.execute(logAgent);
                                responseData = response.getBytes();
+                               log("Received enable event command");
                                break;
                        }
                        case CMD_EVENT_DISABLE:
@@ -278,6 +302,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                SessiondCommand disableEventCmd = new SessiondDisableEventCommand(inputData);
                                LttngAgentResponse response = disableEventCmd.execute(logAgent);
                                responseData = response.getBytes();
+                               log("Received disable event command");
                                break;
                        }
                        case CMD_APP_CTX_ENABLE:
@@ -290,6 +315,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData);
                                LttngAgentResponse response = enableAppCtxCmd.execute(logAgent);
                                responseData = response.getBytes();
+                               log("Received enable app-context command");
                                break;
                        }
                        case CMD_APP_CTX_DISABLE:
@@ -302,6 +328,7 @@ public class LttngTcpSessiondClient implements Runnable {
                                SessiondCommand disableAppCtxCmd = new SessiondDisableAppContextCommand(inputData);
                                LttngAgentResponse response = disableAppCtxCmd.execute(logAgent);
                                responseData = response.getBytes();
+                               log("Received disable app-context command");
                                break;
                        }
                        default:
@@ -310,11 +337,13 @@ public class LttngTcpSessiondClient implements Runnable {
                                responseData = new byte[4];
                                ByteBuffer buf = ByteBuffer.wrap(responseData);
                                buf.order(ByteOrder.BIG_ENDIAN);
+                               log("Received unknown command, ignoring");
                                break;
                        }
                        }
 
                        /* Send response to the session daemon. */
+                       log("Sending response");
                        this.outToSessiond.write(responseData, 0, responseData.length);
                        this.outToSessiond.flush();
                }
@@ -349,8 +378,19 @@ public class LttngTcpSessiondClient implements Runnable {
                        return null;
                }
 
-               this.inFromSessiond.read(payload, 0, payload.length);
+               int read = inFromSessiond.read(payload, 0, payload.length);
+               if (read != payload.length) {
+                       throw new IOException("Unexpected number of bytes read in sessiond command payload");
+               }
                return payload;
        }
 
+       /**
+        * Wrapper for this class's logging, adds the connection's characteristics
+        * to help differentiate between multiple TCP clients.
+        */
+       private void log(String message) {
+               LttngUstAgentLogger.log(getClass(),
+                               "(root=" + isRoot + ", domain=" + domainValue + ") " + message);
+       }
 }
This page took 0.032425 seconds and 4 git commands to generate.