Fix: Specify encoding when reading agent port file
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / client / LttngTcpSessiondClient.java
index 84ad973f3cea5f0cdc56194eac4604fbb0dc2141..f78fdbd0f7c515d6bbecb589ae3454f2d2c83636 100644 (file)
@@ -21,15 +21,16 @@ 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.FileInputStream;
 import java.io.FileNotFoundException;
-import java.io.FileReader;
 import java.io.IOException;
+import java.io.InputStreamReader;
 import java.lang.management.ManagementFactory;
 import java.net.Socket;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
+import java.nio.charset.Charset;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -45,6 +46,7 @@ public class LttngTcpSessiondClient implements Runnable {
        private static final String SESSION_HOST = "127.0.0.1";
        private static final String ROOT_PORT_FILE = "/var/run/lttng/agent.port";
        private static final String USER_PORT_FILE = "/.lttng/agent.port";
+       private static final Charset PORT_FILE_ENCODING = Charset.forName("UTF-8");
 
        private static final int PROTOCOL_MAJOR_VERSION = 2;
        private static final int PROTOCOL_MINOR_VERSION = 0;
@@ -158,23 +160,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());
        }
@@ -200,10 +206,9 @@ 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(file));
+                       br = new BufferedReader(new InputStreamReader(new FileInputStream(path), PORT_FILE_ENCODING));
                        String line = br.readLine();
                        port = Integer.parseInt(line, 10);
                        if (port < 0 || port > 65535) {
This page took 0.0243 seconds and 4 git commands to generate.