X-Git-Url: http://git.liburcu.org/?a=blobdiff_plain;f=liblttng-ust-java-agent%2Fjava%2Flttng-ust-agent-common%2Forg%2Flttng%2Fust%2Fagent%2Fclient%2FLttngTcpSessiondClient.java;h=37f4ec4e59289c0f146d1766573af02e97254492;hb=6b8cdda9e4d87e9dee0b887ac733577b26be0ad3;hp=3e9e24dd65463d2100d7d8095595919a24cddbcb;hpb=301a3ddb302c9c2767f41f3b47d2f3e8ca8b9067;p=lttng-ust.git 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 3e9e24dd..37f4ec4e 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 @@ -1,4 +1,5 @@ /* + * Copyright (C) 2015-2016 EfficiOS Inc., Alexandre Montplaisir * Copyright (C) 2013 - David Goulet * * This library is free software; you can redistribute it and/or modify it @@ -20,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; @@ -31,7 +33,7 @@ import java.nio.ByteOrder; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; -import org.lttng.ust.agent.AbstractLttngAgent; +import org.lttng.ust.agent.utils.LttngUstAgentLogger; /** * Client for agents to connect to a local session daemon, using a TCP socket. @@ -44,8 +46,8 @@ public class LttngTcpSessiondClient implements Runnable { private static final String ROOT_PORT_FILE = "/var/run/lttng/agent.port"; private static final String USER_PORT_FILE = "/.lttng/agent.port"; - private static int protocolMajorVersion = 1; - private static int protocolMinorVersion = 0; + private static final int PROTOCOL_MAJOR_VERSION = 2; + private static final int PROTOCOL_MINOR_VERSION = 0; /** Command header from the session deamon. */ private final CountDownLatch registrationLatch = new CountDownLatch(1); @@ -56,21 +58,26 @@ public class LttngTcpSessiondClient implements Runnable { private DataInputStream inFromSessiond; private DataOutputStream outToSessiond; - private final AbstractLttngAgent logAgent; + private final ILttngTcpClientListener logAgent; + private final int domainValue; private final boolean isRoot; - /** * Constructor * * @param logAgent - * The logging agent this client will operate on. + * The listener this client will operate on, typically an LTTng + * agent. + * @param domainValue + * The integer to send to the session daemon representing the + * tracing domain to handle. * @param isRoot * True if this client should connect to the root session daemon, * false if it should connect to the user one. */ - public LttngTcpSessiondClient(AbstractLttngAgent logAgent, boolean isRoot) { + public LttngTcpSessiondClient(ILttngTcpClientListener logAgent, int domainValue, boolean isRoot) { this.logAgent = logAgent; + this.domainValue = domainValue; this.isRoot = isRoot; } @@ -105,12 +112,14 @@ public class LttngTcpSessiondClient implements Runnable { /* * Connect to the session daemon before anything else. */ + LttngUstAgentLogger.log(getClass(), "Connecting to sessiond"); connectToSessiond(); /* * Register to the session daemon as the Java component of the * UST application. */ + LttngUstAgentLogger.log(getClass(), "Registering to sessiond"); registerToSessiond(); /* @@ -118,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. */ + LttngUstAgentLogger.log(getClass(), "Waiting on sessiond commands..."); handleSessiondCmd(); } catch (UnknownHostException uhe) { uhe.printStackTrace(); @@ -135,6 +145,7 @@ public class LttngTcpSessiondClient implements Runnable { * Dispose this client and close any socket connection it may hold. */ public void close() { + LttngUstAgentLogger.log(getClass(), "Closing client"); this.quit = true; try { @@ -169,7 +180,16 @@ public class LttngTcpSessiondClient implements Runnable { } 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; } /** @@ -180,9 +200,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) { @@ -206,10 +227,10 @@ public class LttngTcpSessiondClient implements Runnable { ByteBuffer buf = ByteBuffer.wrap(data); String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; - buf.putInt(logAgent.getDomain().value()); + buf.putInt(domainValue); buf.putInt(Integer.parseInt(pid)); - buf.putInt(protocolMajorVersion); - buf.putInt(protocolMinorVersion); + buf.putInt(PROTOCOL_MAJOR_VERSION); + buf.putInt(PROTOCOL_MINOR_VERSION); this.outToSessiond.write(data, 0, data.length); this.outToSessiond.flush(); } @@ -243,37 +264,67 @@ 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. */ + LttngUstAgentLogger.log(getClass(), "Registration done"); continue; } case CMD_LIST: { - ISessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); - ILttngAgentResponse response = listLoggerCmd.execute(logAgent); + SessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); + LttngAgentResponse response = listLoggerCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received list loggers command"); break; } - case CMD_ENABLE: + case CMD_EVENT_ENABLE: { if (inputData == null) { /* Invalid command */ - responseData = ILttngAgentResponse.FAILURE_RESPONSE.getBytes(); + responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); break; } - ISessiondCommand enableCmd = new SessiondEnableEventCommand(inputData); - ILttngAgentResponse response = enableCmd.execute(logAgent); + SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData); + LttngAgentResponse response = enableEventCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received enable event command"); break; } - case CMD_DISABLE: + case CMD_EVENT_DISABLE: { if (inputData == null) { /* Invalid command */ - responseData = ILttngAgentResponse.FAILURE_RESPONSE.getBytes(); + responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); + break; + } + SessiondCommand disableEventCmd = new SessiondDisableEventCommand(inputData); + LttngAgentResponse response = disableEventCmd.execute(logAgent); + responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received disable event command"); + break; + } + case CMD_APP_CTX_ENABLE: + { + if (inputData == null) { + /* This commands expects a payload, invalid command */ + responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); break; } - ISessiondCommand disableCmd = new SessiondDisableEventCommand(inputData); - ILttngAgentResponse response = disableCmd.execute(logAgent); + SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData); + LttngAgentResponse response = enableAppCtxCmd.execute(logAgent); responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received enable app-context command"); + break; + } + case CMD_APP_CTX_DISABLE: + { + if (inputData == null) { + /* This commands expects a payload, invalid command */ + responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); + break; + } + SessiondCommand disableAppCtxCmd = new SessiondDisableAppContextCommand(inputData); + LttngAgentResponse response = disableAppCtxCmd.execute(logAgent); + responseData = response.getBytes(); + LttngUstAgentLogger.log(getClass(), "Received disable app-context command"); break; } default: @@ -282,11 +333,13 @@ public class LttngTcpSessiondClient implements Runnable { responseData = new byte[4]; ByteBuffer buf = ByteBuffer.wrap(responseData); buf.order(ByteOrder.BIG_ENDIAN); + LttngUstAgentLogger.log(getClass(), "Received unknown command, ignoring"); break; } } /* Send response to the session daemon. */ + LttngUstAgentLogger.log(getClass(), "Sending response"); this.outToSessiond.write(responseData, 0, responseData.length); this.outToSessiond.flush(); } @@ -321,7 +374,10 @@ 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; }