| 1 | /* |
| 2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | package org.lttng.ust.jul; |
| 19 | |
| 20 | import java.io.IOException; |
| 21 | import java.io.FileNotFoundException; |
| 22 | import java.io.InputStream; |
| 23 | import java.io.BufferedReader; |
| 24 | import java.io.FileReader; |
| 25 | import java.util.concurrent.Semaphore; |
| 26 | import java.util.logging.FileHandler; |
| 27 | import java.util.logging.Handler; |
| 28 | import java.util.logging.Level; |
| 29 | import java.util.logging.Logger; |
| 30 | import java.util.logging.LogManager; |
| 31 | import java.util.Enumeration; |
| 32 | |
| 33 | public class LTTngAgent { |
| 34 | private static LTTngLogHandler lttngHandler; |
| 35 | private static LogManager logManager; |
| 36 | private static LTTngThread lttngThread; |
| 37 | private static Thread sessiondTh; |
| 38 | |
| 39 | /* Singleton agent object. */ |
| 40 | private static LTTngAgent curAgent = null; |
| 41 | |
| 42 | /* Indicate if this object has been initialized. */ |
| 43 | private static boolean initialized = false; |
| 44 | |
| 45 | private static Semaphore registerSem; |
| 46 | |
| 47 | private static final String sessiondAddr = "127.0.0.1"; |
| 48 | private static final int sessiondPort = 5345; |
| 49 | |
| 50 | private static final String rootPortFile = "/var/run/lttng/jul.port"; |
| 51 | private static final String userPortFile = "/.lttng/jul.port"; |
| 52 | |
| 53 | /* |
| 54 | * Constructor is private. This is a singleton and a reference should be |
| 55 | * acquired using getLTTngAgent(). |
| 56 | */ |
| 57 | private LTTngAgent() throws IOException { |
| 58 | this.logManager = LogManager.getLogManager(); |
| 59 | this.lttngHandler = new LTTngLogHandler(this.logManager); |
| 60 | this.registerSem = new Semaphore(0, true); |
| 61 | } |
| 62 | |
| 63 | private void removeHandlers() throws SecurityException, IOException { |
| 64 | String loggerName; |
| 65 | Logger logger; |
| 66 | |
| 67 | Enumeration list = this.logManager.getLoggerNames(); |
| 68 | while (list.hasMoreElements()) { |
| 69 | loggerName = list.nextElement().toString(); |
| 70 | /* Somehow there is always an empty string at the end. */ |
| 71 | if (loggerName == "") { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | logger = this.logManager.getLogger(loggerName); |
| 76 | logger.removeHandler(this.lttngHandler); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private int getUID() throws IOException { |
| 81 | int uid; |
| 82 | byte b[] = new byte[4]; |
| 83 | String userName = System.getProperty("user.name"); |
| 84 | String command = "id -u " + userName; |
| 85 | Process child = Runtime.getRuntime().exec(command); |
| 86 | InputStream in = child.getInputStream(); |
| 87 | |
| 88 | in.read(b); |
| 89 | uid = Integer.parseInt(new String(b).trim(), 10); |
| 90 | in.close(); |
| 91 | |
| 92 | return uid; |
| 93 | } |
| 94 | |
| 95 | private String getHomePath() { |
| 96 | return System.getProperty("user.home"); |
| 97 | } |
| 98 | |
| 99 | private int getPortFromFile() throws IOException { |
| 100 | int port; |
| 101 | int uid = getUID(); |
| 102 | String path; |
| 103 | BufferedReader br; |
| 104 | |
| 105 | /* Check if root or not, it tells where to get the port file. */ |
| 106 | if (uid == 0) { |
| 107 | path = rootPortFile; |
| 108 | } else { |
| 109 | path = new String(getHomePath() + userPortFile); |
| 110 | } |
| 111 | |
| 112 | try { |
| 113 | br = new BufferedReader(new FileReader(path)); |
| 114 | String line = br.readLine(); |
| 115 | port = Integer.parseInt(line, 10); |
| 116 | if (port < 0 || port > 65535) { |
| 117 | port = sessiondPort; |
| 118 | } |
| 119 | br.close(); |
| 120 | } catch (FileNotFoundException e) { |
| 121 | port = sessiondPort; |
| 122 | } |
| 123 | |
| 124 | return port; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Public getter to acquire a reference to this singleton object. |
| 129 | */ |
| 130 | public static synchronized LTTngAgent getLTTngAgent() throws IOException { |
| 131 | if (curAgent == null) { |
| 132 | curAgent = new LTTngAgent(); |
| 133 | curAgent.init(); |
| 134 | } |
| 135 | |
| 136 | return curAgent; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Initialize LTTngAgent. This will attach the log handler to all Logger |
| 141 | * returned by the logManager. |
| 142 | */ |
| 143 | private synchronized void init() throws SecurityException, IOException { |
| 144 | if (this.initialized) { |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | this.lttngThread = new LTTngThread(this.sessiondAddr, |
| 149 | getPortFromFile(), this.lttngHandler, this.registerSem); |
| 150 | this.sessiondTh = new Thread(lttngThread); |
| 151 | this.sessiondTh.start(); |
| 152 | |
| 153 | this.initialized = true; |
| 154 | |
| 155 | /* Wait for the registration to end. */ |
| 156 | try { |
| 157 | this.registerSem.acquire(); |
| 158 | } catch (InterruptedException e) { |
| 159 | e.printStackTrace(); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | public void dispose() throws IOException { |
| 164 | this.lttngThread.dispose(); |
| 165 | |
| 166 | /* Make sure there is no more LTTng handler attach to logger(s). */ |
| 167 | this.removeHandlers(); |
| 168 | |
| 169 | try { |
| 170 | this.sessiondTh.join(); |
| 171 | } catch (InterruptedException e) { |
| 172 | e.printStackTrace(); |
| 173 | } |
| 174 | } |
| 175 | } |