2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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
18 package org
.lttng
.ust
.agent
.client
;
20 import java
.io
.BufferedReader
;
21 import java
.io
.DataInputStream
;
22 import java
.io
.DataOutputStream
;
23 import java
.io
.FileNotFoundException
;
24 import java
.io
.FileReader
;
25 import java
.io
.IOException
;
26 import java
.lang
.management
.ManagementFactory
;
27 import java
.net
.Socket
;
28 import java
.net
.UnknownHostException
;
29 import java
.nio
.ByteBuffer
;
30 import java
.nio
.ByteOrder
;
31 import java
.util
.concurrent
.CountDownLatch
;
32 import java
.util
.concurrent
.TimeUnit
;
34 import org
.lttng
.ust
.agent
.AbstractLttngAgent
;
37 * Client for agents to connect to a local session daemon, using a TCP socket.
39 * @author David Goulet
41 public class LttngTcpSessiondClient
implements Runnable
{
43 private static final String SESSION_HOST
= "127.0.0.1";
44 private static final String ROOT_PORT_FILE
= "/var/run/lttng/agent.port";
45 private static final String USER_PORT_FILE
= "/.lttng/agent.port";
47 private static int protocolMajorVersion
= 1;
48 private static int protocolMinorVersion
= 0;
50 /** Command header from the session deamon. */
51 private final SessiondHeaderCommand headerCmd
= new SessiondHeaderCommand();
52 private final CountDownLatch registrationLatch
= new CountDownLatch(1);
54 private Socket sessiondSock
;
55 private volatile boolean quit
= false;
57 private DataInputStream inFromSessiond
;
58 private DataOutputStream outToSessiond
;
60 private final AbstractLttngAgent
<?
> logAgent
;
61 private final boolean isRoot
;
68 * The logging agent this client will operate on.
70 * True if this client should connect to the root session daemon,
71 * false if it should connect to the user one.
73 public LttngTcpSessiondClient(AbstractLttngAgent
<?
> logAgent
, boolean isRoot
) {
74 this.logAgent
= logAgent
;
79 * Wait until this client has successfully established a connection to its
80 * target session daemon.
83 * A timeout in seconds after which this method will return
85 * @return True if the the client actually established the connection, false
86 * if we returned because the timeout has elapsed or the thread was
89 public boolean waitForConnection(int seconds
) {
91 return registrationLatch
.await(seconds
, TimeUnit
.SECONDS
);
92 } catch (InterruptedException e
) {
107 * Connect to the session daemon before anything else.
112 * Register to the session daemon as the Java component of the
115 registerToSessiond();
118 * Block on socket receive and wait for command from the
119 * session daemon. This will return if and only if there is a
120 * fatal error or the socket closes.
123 } catch (UnknownHostException uhe
) {
124 uhe
.printStackTrace();
125 } catch (IOException ioe
) {
128 } catch (InterruptedException e
) {
136 * Dispose this client and close any socket connection it may hold.
138 public void close() {
142 if (this.sessiondSock
!= null) {
143 this.sessiondSock
.close();
145 } catch (IOException e
) {
151 * Receive header data from the session daemon using the LTTng command
152 * static buffer of the right size.
154 private void recvHeader() throws IOException
{
155 byte data
[] = new byte[SessiondHeaderCommand
.HEADER_SIZE
];
157 int readLen
= this.inFromSessiond
.read(data
, 0, data
.length
);
158 if (readLen
!= data
.length
) {
159 throw new IOException();
161 this.headerCmd
.populate(data
);
165 * Receive payload from the session daemon. This MUST be done after a
166 * recvHeader() so the header value of a command are known.
168 * The caller SHOULD use isPayload() before which returns true if a payload
169 * is expected after the header.
171 private byte[] recvPayload() throws IOException
{
172 byte payload
[] = new byte[(int) this.headerCmd
.getDataSize()];
174 /* Failsafe check so we don't waste our time reading 0 bytes. */
175 if (payload
.length
== 0) {
179 this.inFromSessiond
.read(payload
, 0, payload
.length
);
184 * Handle session command from the session daemon.
186 private void handleSessiondCmd() throws IOException
{
190 /* Get header from session daemon. */
193 if (headerCmd
.getDataSize() > 0) {
194 data
= recvPayload();
197 switch (headerCmd
.getCommandType()) {
201 * Countdown the registration latch, meaning registration is
202 * done and we can proceed to continue tracing.
204 registrationLatch
.countDown();
206 * We don't send any reply to the registration done command.
207 * This just marks the end of the initial session setup.
213 SessiondListLoggersResponse listLoggerCmd
= new SessiondListLoggersResponse();
214 listLoggerCmd
.execute(logAgent
);
215 data
= listLoggerCmd
.getBytes();
220 SessiondEnableHandler enableCmd
= new SessiondEnableHandler();
222 enableCmd
.code
= ISessiondResponse
.LttngAgentRetCode
.CODE_INVALID_CMD
;
225 enableCmd
.populate(data
);
226 enableCmd
.execute(logAgent
);
227 data
= enableCmd
.getBytes();
232 SessiondDisableHandler disableCmd
= new SessiondDisableHandler();
234 disableCmd
.setRetCode(ISessiondResponse
.LttngAgentRetCode
.CODE_INVALID_CMD
);
237 disableCmd
.populate(data
);
238 disableCmd
.execute(logAgent
);
239 data
= disableCmd
.getBytes();
245 ByteBuffer buf
= ByteBuffer
.wrap(data
);
246 buf
.order(ByteOrder
.BIG_ENDIAN
);
253 * Simply used to silence a potential null access warning below.
255 * The flow analysis gets confused here and thinks "data" may be
256 * null at this point. It should not happen according to program
257 * logic, if it does we've done something wrong.
259 throw new IllegalStateException();
261 /* Send payload to session daemon. */
262 this.outToSessiond
.write(data
, 0, data
.length
);
263 this.outToSessiond
.flush();
267 private static String
getHomePath() {
268 return System
.getProperty("user.home");
272 * Read port number from file created by the session daemon.
274 * @return port value if found else 0.
276 private static int getPortFromFile(String path
) throws IOException
{
278 BufferedReader br
= null;
281 br
= new BufferedReader(new FileReader(path
));
282 String line
= br
.readLine();
283 port
= Integer
.parseInt(line
, 10);
284 if (port
< 0 || port
> 65535) {
285 /* Invalid value. Ignore. */
288 } catch (FileNotFoundException e
) {
289 /* No port available. */
300 private void connectToSessiond() throws IOException
{
304 port
= getPortFromFile(ROOT_PORT_FILE
);
306 /* No session daemon available. Stop and retry later. */
307 throw new IOException();
310 port
= getPortFromFile(getHomePath() + USER_PORT_FILE
);
312 /* No session daemon available. Stop and retry later. */
313 throw new IOException();
317 this.sessiondSock
= new Socket(SESSION_HOST
, port
);
318 this.inFromSessiond
= new DataInputStream(sessiondSock
.getInputStream());
319 this.outToSessiond
= new DataOutputStream(sessiondSock
.getOutputStream());
322 private void registerToSessiond() throws IOException
{
323 byte data
[] = new byte[16];
324 ByteBuffer buf
= ByteBuffer
.wrap(data
);
325 String pid
= ManagementFactory
.getRuntimeMXBean().getName().split("@")[0];
327 buf
.putInt(logAgent
.getDomain().value());
328 buf
.putInt(Integer
.parseInt(pid
));
329 buf
.putInt(protocolMajorVersion
);
330 buf
.putInt(protocolMinorVersion
);
331 this.outToSessiond
.write(data
, 0, data
.length
);
332 this.outToSessiond
.flush();