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
;
20 import java
.util
.concurrent
.Semaphore
;
21 import java
.nio
.ByteBuffer
;
22 import java
.nio
.ByteOrder
;
23 import java
.lang
.Integer
;
24 import java
.io
.IOException
;
25 import java
.io
.BufferedOutputStream
;
26 import java
.io
.BufferedReader
;
27 import java
.io
.ByteArrayOutputStream
;
28 import java
.io
.DataOutputStream
;
29 import java
.io
.DataInputStream
;
30 import java
.io
.FileReader
;
31 import java
.io
.FileNotFoundException
;
33 import java
.lang
.management
.ManagementFactory
;
35 class LTTngTCPSessiondClient
implements Runnable
{
37 /* Command header from the session deamon. */
38 private LTTngSessiondCmd2_6
.sessiond_hdr headerCmd
=
39 new LTTngSessiondCmd2_6
.sessiond_hdr();
41 private Socket sessiondSock
;
42 private volatile boolean quit
= false;
44 private DataInputStream inFromSessiond
;
45 private DataOutputStream outToSessiond
;
47 private LogFramework log
;
49 private Semaphore registerSem
;
51 private static final String sessiondHost
= "127.0.0.1";
52 private static final String rootPortFile
= "/var/run/lttng/agent.port";
53 private static final String userPortFile
= "/.lttng/agent.port";
55 private static Integer protocolMajorVersion
= 1;
56 private static Integer protocolMinorVersion
= 0;
58 private LTTngAgent
.Domain agentDomain
;
60 /* Indicate if we've already release the semaphore. */
61 private boolean sem_posted
= false;
63 public LTTngTCPSessiondClient(LTTngAgent
.Domain domain
, LogFramework log
, Semaphore sem
) {
64 this.agentDomain
= domain
;
66 this.registerSem
= sem
;
70 * Try to release the registerSem if it's not already done.
72 private void tryReleaseSem()
74 /* Release semaphore so we unblock the agent. */
75 if (!this.sem_posted
) {
76 this.registerSem
.release();
77 this.sem_posted
= true;
88 /* Cleanup Agent state before trying to connect or reconnect. */
94 * Connect to the session daemon before anything else.
99 * Register to the session daemon as the Java component of the
102 registerToSessiond();
105 * Block on socket receive and wait for command from the
106 * session daemon. This will return if and only if there is a
107 * fatal error or the socket closes.
110 } catch (UnknownHostException uhe
) {
112 System
.out
.println(uhe
);
113 } catch (IOException ioe
) {
117 } catch (InterruptedException e
) {
120 } catch (Exception e
) {
127 public void destroy() {
131 if (this.sessiondSock
!= null) {
132 this.sessiondSock
.close();
134 } catch (Exception e
) {
140 * Receive header data from the session daemon using the LTTng command
141 * static buffer of the right size.
143 private void recvHeader() throws Exception
{
145 byte data
[] = new byte[this.headerCmd
.SIZE
];
147 read_len
= this.inFromSessiond
.read(data
, 0, data
.length
);
148 if (read_len
!= data
.length
) {
149 throw new IOException();
151 this.headerCmd
.populate(data
);
155 * Receive payload from the session daemon. This MUST be done after a
156 * recvHeader() so the header value of a command are known.
158 * The caller SHOULD use isPayload() before which returns true if a payload
159 * is expected after the header.
161 private byte[] recvPayload() throws Exception
{
162 byte payload
[] = new byte[(int) this.headerCmd
.data_size
];
164 /* Failsafe check so we don't waste our time reading 0 bytes. */
165 if (payload
.length
== 0) {
169 this.inFromSessiond
.read(payload
, 0, payload
.length
);
174 * Handle session command from the session daemon.
176 private void handleSessiondCmd() throws Exception
{
181 /* Get header from session daemon. */
184 if (headerCmd
.data_size
> 0) {
185 data
= recvPayload();
188 switch (headerCmd
.cmd
) {
192 * Release semaphore so meaning registration is done and we
193 * can proceed to continue tracing.
197 * We don't send any reply to the registration done command.
198 * This just marks the end of the initial session setup.
204 LTTngSessiondCmd2_6
.sessiond_list_logger listLoggerCmd
=
205 new LTTngSessiondCmd2_6
.sessiond_list_logger();
206 listLoggerCmd
.execute(this.log
);
207 data
= listLoggerCmd
.getBytes();
212 LTTngSessiondCmd2_6
.sessiond_enable_handler enableCmd
=
213 new LTTngSessiondCmd2_6
.sessiond_enable_handler();
215 enableCmd
.code
= LTTngSessiondCmd2_6
.lttng_agent_ret_code
.CODE_INVALID_CMD
;
218 enableCmd
.populate(data
);
219 enableCmd
.execute(this.log
);
220 data
= enableCmd
.getBytes();
225 LTTngSessiondCmd2_6
.sessiond_disable_handler disableCmd
=
226 new LTTngSessiondCmd2_6
.sessiond_disable_handler();
228 disableCmd
.code
= LTTngSessiondCmd2_6
.lttng_agent_ret_code
.CODE_INVALID_CMD
;
231 disableCmd
.populate(data
);
232 disableCmd
.execute(this.log
);
233 data
= disableCmd
.getBytes();
239 ByteBuffer buf
= ByteBuffer
.wrap(data
);
240 buf
.order(ByteOrder
.BIG_ENDIAN
);
241 LTTngSessiondCmd2_6
.lttng_agent_ret_code code
=
242 LTTngSessiondCmd2_6
.lttng_agent_ret_code
.CODE_INVALID_CMD
;
247 /* Send payload to session daemon. */
248 this.outToSessiond
.write(data
, 0, data
.length
);
249 this.outToSessiond
.flush();
253 private String
getHomePath() {
254 return System
.getProperty("user.home");
258 * Read port number from file created by the session daemon.
260 * @return port value if found else 0.
262 private int getPortFromFile(String path
) throws IOException
{
267 br
= new BufferedReader(new FileReader(path
));
268 String line
= br
.readLine();
269 port
= Integer
.parseInt(line
, 10);
270 if (port
< 0 || port
> 65535) {
271 /* Invalid value. Ignore. */
275 } catch (FileNotFoundException e
) {
276 /* No port available. */
283 private void connectToSessiond() throws Exception
{
286 if (this.log
.isRoot()) {
287 port
= getPortFromFile(rootPortFile
);
289 /* No session daemon available. Stop and retry later. */
290 throw new IOException();
293 port
= getPortFromFile(getHomePath() + userPortFile
);
295 /* No session daemon available. Stop and retry later. */
296 throw new IOException();
300 this.sessiondSock
= new Socket(this.sessiondHost
, port
);
301 this.inFromSessiond
= new DataInputStream(
302 sessiondSock
.getInputStream());
303 this.outToSessiond
= new DataOutputStream(
304 sessiondSock
.getOutputStream());
307 private void registerToSessiond() throws Exception
{
308 byte data
[] = new byte[16];
309 ByteBuffer buf
= ByteBuffer
.wrap(data
);
310 String pid
= ManagementFactory
.getRuntimeMXBean().getName().split("@")[0];
312 buf
.putInt(this.agentDomain
.value());
313 buf
.putInt(Integer
.parseInt(pid
));
314 buf
.putInt(this.protocolMajorVersion
);
315 buf
.putInt(this.protocolMinorVersion
);
316 this.outToSessiond
.write(data
, 0, data
.length
);
317 this.outToSessiond
.flush();