Fix: dynamically fetch the session daemon port
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngAgent.java
index c1834f5869a551b3d61a5ddeb752d85b18b5777e..2485be2deb1781c05451eafaddd0a284ad89ec3b 100644 (file)
 package org.lttng.ust.jul;
 
 import java.io.IOException;
-import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.BufferedReader;
 import java.io.FileReader;
 import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.FileHandler;
 import java.util.logging.Handler;
 import java.util.logging.Level;
@@ -31,10 +31,15 @@ import java.util.logging.LogManager;
 import java.util.Enumeration;
 
 public class LTTngAgent {
-       private static LTTngLogHandler lttngHandler;
        private static LogManager logManager;
-       private static LTTngThread lttngThread;
-       private static Thread sessiondTh;
+
+       /* Possible that we have to threads handling two sessiond. */
+       private static LTTngLogHandler lttngHandlerRoot;
+       private static LTTngLogHandler lttngHandlerUser;
+       private static LTTngThread lttngThreadRoot;
+       private static LTTngThread lttngThreadUser;
+       private static Thread sessiondThRoot;
+       private static Thread sessiondThUser;
 
        /* Singleton agent object. */
        private static LTTngAgent curAgent = null;
@@ -43,12 +48,13 @@ public class LTTngAgent {
        private static boolean initialized = false;
 
        private static Semaphore registerSem;
+       private final static int semTimeout = 3; /* Seconds */
 
+       /*
+        * Default value to connect to session daemon. Port number is dynamically
+        * fetched from the port file that is created by a running session daemon.
+        */
        private static final String sessiondAddr = "127.0.0.1";
-       private static final int sessiondPort = 5345;
-
-       private static final String rootPortFile = "/var/run/lttng/jul.port";
-       private static final String userPortFile = "/.lttng/jul.port";
 
        /*
         * Constructor is private. This is a singleton and a reference should be
@@ -56,7 +62,9 @@ public class LTTngAgent {
         */
        private LTTngAgent() throws IOException {
                this.logManager = LogManager.getLogManager();
-               this.lttngHandler = new LTTngLogHandler(this.logManager);
+               this.lttngHandlerUser = new LTTngLogHandler(this.logManager);
+               this.lttngHandlerRoot = new LTTngLogHandler(this.logManager);
+               this.lttngHandlerRoot.is_root = 1;
                this.registerSem = new Semaphore(0, true);
        }
 
@@ -73,7 +81,8 @@ public class LTTngAgent {
                        }
 
                        logger = this.logManager.getLogger(loggerName);
-                       logger.removeHandler(this.lttngHandler);
+                       logger.removeHandler(this.lttngHandlerUser);
+                       logger.removeHandler(this.lttngHandlerRoot);
                }
        }
 
@@ -92,38 +101,6 @@ public class LTTngAgent {
                return uid;
        }
 
-       private String getHomePath() {
-               return System.getProperty("user.home");
-       }
-
-       private int getPortFromFile() throws IOException {
-               int port;
-               int uid = getUID();
-               String path;
-               BufferedReader br;
-
-               /* Check if root or not, it tells where to get the port file. */
-               if (uid == 0) {
-                       path = rootPortFile;
-               } else {
-                       path = new String(getHomePath() + userPortFile);
-               }
-
-               try {
-                       br = new BufferedReader(new FileReader(path));
-                       String line = br.readLine();
-                       port = Integer.parseInt(line, 10);
-                       if (port < 0 || port > 65535) {
-                               port = sessiondPort;
-                       }
-                       br.close();
-               } catch (FileNotFoundException e) {
-                       port = sessiondPort;
-               }
-
-               return port;
-       }
-
        /*
         * Public getter to acquire a reference to this singleton object.
         */
@@ -141,33 +118,53 @@ public class LTTngAgent {
         * returned by the logManager.
         */
        private synchronized void init() throws SecurityException, IOException {
+               int nr_acquires = 0;
+
                if (this.initialized) {
                        return;
                }
 
-               this.lttngThread = new LTTngThread(this.sessiondAddr,
-                               getPortFromFile(), this.lttngHandler, this.registerSem);
-               this.sessiondTh = new Thread(lttngThread);
-               this.sessiondTh.start();
-
-               this.initialized = true;
-
-               /* Wait for the registration to end. */
+               /* Handle user session daemon if any. */
+               this.lttngThreadUser = new LTTngThread(this.sessiondAddr,
+                               this.lttngHandlerUser, this.registerSem);
+               this.sessiondThUser = new Thread(lttngThreadUser);
+               this.sessiondThUser.start();
+               /* Wait for registration done of per-user sessiond */
+               nr_acquires++;
+
+               /* Handle root session daemon. */
+               this.lttngThreadRoot = new LTTngThread(this.sessiondAddr,
+                               this.lttngHandlerRoot, this.registerSem);
+               this.sessiondThRoot = new Thread(lttngThreadRoot);
+               this.sessiondThRoot.start();
+               /* Wait for registration done of system-wide sessiond */
+               nr_acquires++;
+
+               /* Wait for each registration to end. */
                try {
-                       this.registerSem.acquire();
+                       this.registerSem.tryAcquire(nr_acquires, semTimeout,
+                                       TimeUnit.SECONDS);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
+
+               this.initialized = true;
        }
 
        public void dispose() throws IOException {
-               this.lttngThread.dispose();
+               this.lttngThreadUser.dispose();
+               if (this.lttngThreadRoot != null) {
+                       this.lttngThreadRoot.dispose();
+               }
 
                /* Make sure there is no more LTTng handler attach to logger(s). */
                this.removeHandlers();
 
                try {
-                       this.sessiondTh.join();
+                       this.sessiondThUser.join();
+                       if (this.sessiondThRoot != null) {
+                               this.sessiondThRoot.join();
+                       }
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
This page took 0.030071 seconds and 4 git commands to generate.