/*
+ * Copyright (C) 2015-2016 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
* Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
*
* This library is free software; you can redistribute it and/or modify it
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.
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 SessiondHeaderCommand headerCmd = new SessiondHeaderCommand();
private final CountDownLatch registrationLatch = new CountDownLatch(1);
private Socket sessiondSock;
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;
}
/*
* 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();
/*
* 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();
* Dispose this client and close any socket connection it may hold.
*/
public void close() {
+ LttngUstAgentLogger.log(getClass(), "Closing client");
this.quit = true;
try {
}
}
- /**
- * Receive header data from the session daemon using the LTTng command
- * static buffer of the right size.
- */
- private void recvHeader() throws IOException {
- byte data[] = new byte[SessiondHeaderCommand.HEADER_SIZE];
+ private void connectToSessiond() throws IOException {
+ int port;
- int readLen = this.inFromSessiond.read(data, 0, data.length);
- if (readLen != data.length) {
- 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();
+ }
}
- this.headerCmd.populate(data);
+
+ this.sessiondSock = new Socket(SESSION_HOST, port);
+ this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream());
+ this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream());
+ }
+
+ private static String getHomePath() {
+ /*
+ * 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;
}
/**
- * Receive payload from the session daemon. This MUST be done after a
- * recvHeader() so the header value of a command are known.
+ * Read port number from file created by the session daemon.
*
- * The caller SHOULD use isPayload() before which returns true if a payload
- * is expected after the header.
+ * @return port value if found else 0.
*/
- private byte[] recvPayload() throws IOException {
- byte payload[] = new byte[(int) this.headerCmd.getDataSize()];
+ private static int getPortFromFile(String path) throws IOException {
+ int port;
+ BufferedReader br = null;
- /* Failsafe check so we don't waste our time reading 0 bytes. */
- if (payload.length == 0) {
- return null;
+ try {
+ br = new BufferedReader(new FileReader(path));
+ String line = br.readLine();
+ port = Integer.parseInt(line, 10);
+ if (port < 0 || port > 65535) {
+ /* Invalid value. Ignore. */
+ port = 0;
+ }
+ } catch (FileNotFoundException e) {
+ /* No port available. */
+ port = 0;
+ } finally {
+ if (br != null) {
+ br.close();
+ }
}
- this.inFromSessiond.read(payload, 0, payload.length);
- return payload;
+ return port;
+ }
+
+ private void registerToSessiond() throws IOException {
+ byte data[] = new byte[16];
+ ByteBuffer buf = ByteBuffer.wrap(data);
+ String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
+
+ buf.putInt(domainValue);
+ buf.putInt(Integer.parseInt(pid));
+ buf.putInt(PROTOCOL_MAJOR_VERSION);
+ buf.putInt(PROTOCOL_MINOR_VERSION);
+ this.outToSessiond.write(data, 0, data.length);
+ this.outToSessiond.flush();
}
/**
* Handle session command from the session daemon.
*/
private void handleSessiondCmd() throws IOException {
- byte data[] = null;
+ /* Data read from the socket */
+ byte inputData[] = null;
+ /* Reply data written to the socket, sent to the sessiond */
+ byte responseData[] = null;
while (true) {
/* Get header from session daemon. */
- recvHeader();
+ SessiondCommandHeader cmdHeader = recvHeader();
- if (headerCmd.getDataSize() > 0) {
- data = recvPayload();
+ if (cmdHeader.getDataSize() > 0) {
+ inputData = recvPayload(cmdHeader);
}
- switch (headerCmd.getCommandType()) {
+ switch (cmdHeader.getCommandType()) {
case CMD_REG_DONE:
{
/*
* 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:
{
- SessiondListLoggersResponse listLoggerCmd = new SessiondListLoggersResponse();
- listLoggerCmd.execute(logAgent);
- data = listLoggerCmd.getBytes();
+ SessiondCommand listLoggerCmd = new SessiondListLoggersCommand();
+ LttngAgentResponse response = listLoggerCmd.execute(logAgent);
+ responseData = response.getBytes();
+ LttngUstAgentLogger.log(getClass(), "Received list loggers command");
+ break;
+ }
+ case CMD_EVENT_ENABLE:
+ {
+ if (inputData == null) {
+ /* Invalid command */
+ responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes();
+ break;
+ }
+ SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData);
+ LttngAgentResponse response = enableEventCmd.execute(logAgent);
+ responseData = response.getBytes();
+ LttngUstAgentLogger.log(getClass(), "Received enable event command");
+ break;
+ }
+ case CMD_EVENT_DISABLE:
+ {
+ if (inputData == null) {
+ /* Invalid command */
+ 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_ENABLE:
+ case CMD_APP_CTX_ENABLE:
{
- SessiondEnableHandler enableCmd = new SessiondEnableHandler();
- if (data == null) {
- enableCmd.code = ISessiondResponse.LttngAgentRetCode.CODE_INVALID_CMD;
+ if (inputData == null) {
+ /* This commands expects a payload, invalid command */
+ responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes();
break;
}
- enableCmd.populate(data);
- enableCmd.execute(logAgent);
- data = enableCmd.getBytes();
+ SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData);
+ LttngAgentResponse response = enableAppCtxCmd.execute(logAgent);
+ responseData = response.getBytes();
+ LttngUstAgentLogger.log(getClass(), "Received enable app-context command");
break;
}
- case CMD_DISABLE:
+ case CMD_APP_CTX_DISABLE:
{
- SessiondDisableHandler disableCmd = new SessiondDisableHandler();
- if (data == null) {
- disableCmd.setRetCode(ISessiondResponse.LttngAgentRetCode.CODE_INVALID_CMD);
+ if (inputData == null) {
+ /* This commands expects a payload, invalid command */
+ responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes();
break;
}
- disableCmd.populate(data);
- disableCmd.execute(logAgent);
- data = disableCmd.getBytes();
+ SessiondCommand disableAppCtxCmd = new SessiondDisableAppContextCommand(inputData);
+ LttngAgentResponse response = disableAppCtxCmd.execute(logAgent);
+ responseData = response.getBytes();
+ LttngUstAgentLogger.log(getClass(), "Received disable app-context command");
break;
}
default:
{
- data = new byte[4];
- ByteBuffer buf = ByteBuffer.wrap(data);
+ /* Unknown command, send empty reply */
+ responseData = new byte[4];
+ ByteBuffer buf = ByteBuffer.wrap(responseData);
buf.order(ByteOrder.BIG_ENDIAN);
+ LttngUstAgentLogger.log(getClass(), "Received unknown command, ignoring");
break;
}
}
- if (data == null) {
- /*
- * Simply used to silence a potential null access warning below.
- *
- * The flow analysis gets confused here and thinks "data" may be
- * null at this point. It should not happen according to program
- * logic, if it does we've done something wrong.
- */
- throw new IllegalStateException();
- }
- /* Send payload to session daemon. */
- this.outToSessiond.write(data, 0, data.length);
+ /* Send response to the session daemon. */
+ LttngUstAgentLogger.log(getClass(), "Sending response");
+ this.outToSessiond.write(responseData, 0, responseData.length);
this.outToSessiond.flush();
}
}
- private static String getHomePath() {
- return System.getProperty("user.home");
- }
-
/**
- * Read port number from file created by the session daemon.
- *
- * @return port value if found else 0.
+ * Receive header data from the session daemon using the LTTng command
+ * static buffer of the right size.
*/
- private static int getPortFromFile(String path) throws IOException {
- int port;
- BufferedReader br = null;
+ private SessiondCommandHeader recvHeader() throws IOException {
+ byte data[] = new byte[SessiondCommandHeader.HEADER_SIZE];
- try {
- br = new BufferedReader(new FileReader(path));
- String line = br.readLine();
- port = Integer.parseInt(line, 10);
- if (port < 0 || port > 65535) {
- /* Invalid value. Ignore. */
- port = 0;
- }
- } catch (FileNotFoundException e) {
- /* No port available. */
- port = 0;
- } finally {
- if (br != null) {
- br.close();
- }
+ int readLen = this.inFromSessiond.read(data, 0, data.length);
+ if (readLen != data.length) {
+ throw new IOException();
}
-
- return port;
+ return new SessiondCommandHeader(data);
}
- private void connectToSessiond() throws IOException {
- int port;
+ /**
+ * Receive payload from the session daemon. This MUST be done after a
+ * recvHeader() so the header value of a command are known.
+ *
+ * The caller SHOULD use isPayload() before which returns true if a payload
+ * is expected after the header.
+ */
+ private byte[] recvPayload(SessiondCommandHeader headerCmd) throws IOException {
+ byte payload[] = new byte[(int) headerCmd.getDataSize()];
- 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();
- }
+ /* Failsafe check so we don't waste our time reading 0 bytes. */
+ if (payload.length == 0) {
+ return null;
}
- this.sessiondSock = new Socket(SESSION_HOST, port);
- this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream());
- this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream());
+ this.inFromSessiond.read(payload, 0, payload.length);
+ return payload;
}
- private void registerToSessiond() throws IOException {
- byte data[] = new byte[16];
- ByteBuffer buf = ByteBuffer.wrap(data);
- String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
-
- buf.putInt(logAgent.getDomain().value());
- buf.putInt(Integer.parseInt(pid));
- buf.putInt(protocolMajorVersion);
- buf.putInt(protocolMinorVersion);
- this.outToSessiond.write(data, 0, data.length);
- this.outToSessiond.flush();
- }
}