/*
+ * 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.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
+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;
+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 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;
/*
* Connect to the session daemon before anything else.
*/
+ log("Connecting to sessiond");
connectToSessiond();
/*
* Register to the session daemon as the Java component of the
* UST application.
*/
+ log("Registering to sessiond");
registerToSessiond();
/*
* session daemon. This will return if and only if there is a
* fatal error or the socket closes.
*/
+ log("Waiting on sessiond commands...");
handleSessiondCmd();
} catch (UnknownHostException uhe) {
uhe.printStackTrace();
* Dispose this client and close any socket connection it may hold.
*/
public void close() {
+ log("Closing client");
this.quit = true;
try {
}
private void connectToSessiond() throws IOException {
- int port;
+ int rootPort = getPortFromFile(ROOT_PORT_FILE);
+ int userPort = getPortFromFile(getHomePath() + USER_PORT_FILE);
+
+ /*
+ * 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();
+ }
- 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();
- }
+ 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());
}
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;
}
/**
BufferedReader br = null;
try {
- br = new BufferedReader(new FileReader(path));
+ 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) {
* We don't send any reply to the registration done command.
* This just marks the end of the initial session setup.
*/
+ log("Registration done");
continue;
}
case CMD_LIST:
SessiondCommand listLoggerCmd = new SessiondListLoggersCommand();
LttngAgentResponse response = listLoggerCmd.execute(logAgent);
responseData = response.getBytes();
+ log("Received list loggers command");
break;
}
- case CMD_ENABLE:
+ case CMD_EVENT_ENABLE:
{
if (inputData == null) {
/* Invalid command */
responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes();
break;
}
- SessiondCommand enableCmd = new SessiondEnableEventCommand(inputData);
- LttngAgentResponse response = enableCmd.execute(logAgent);
+ SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData);
+ LttngAgentResponse response = enableEventCmd.execute(logAgent);
responseData = response.getBytes();
+ log("Received enable event command");
break;
}
- case CMD_DISABLE:
+ case CMD_EVENT_DISABLE:
{
if (inputData == null) {
/* Invalid command */
responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes();
break;
}
- SessiondCommand disableCmd = new SessiondDisableEventCommand(inputData);
- LttngAgentResponse response = disableCmd.execute(logAgent);
+ SessiondCommand disableEventCmd = new SessiondDisableEventCommand(inputData);
+ LttngAgentResponse response = disableEventCmd.execute(logAgent);
responseData = response.getBytes();
+ log("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;
+ }
+ SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData);
+ LttngAgentResponse response = enableAppCtxCmd.execute(logAgent);
+ responseData = response.getBytes();
+ log("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();
+ log("Received disable app-context command");
break;
}
default:
responseData = new byte[4];
ByteBuffer buf = ByteBuffer.wrap(responseData);
buf.order(ByteOrder.BIG_ENDIAN);
+ log("Received unknown command, ignoring");
break;
}
}
/* Send response to the session daemon. */
+ log("Sending response");
this.outToSessiond.write(responseData, 0, responseData.length);
this.outToSessiond.flush();
}
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;
}
+ /**
+ * Wrapper for this class's logging, adds the connection's characteristics
+ * to help differentiate between multiple TCP clients.
+ */
+ private void log(String message) {
+ LttngUstAgentLogger.log(getClass(),
+ "(root=" + isRoot + ", domain=" + domainValue + ") " + message);
+ }
}