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
.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
.Semaphore
;
33 class LTTngTCPSessiondClient
implements Runnable
{
35 /* Command header from the session deamon. */
36 private LTTngSessiondCmd2_6
.sessiond_hdr headerCmd
=
37 new LTTngSessiondCmd2_6
.sessiond_hdr();
39 private Socket sessiondSock
;
40 private volatile boolean quit
= false;
42 private DataInputStream inFromSessiond
;
43 private DataOutputStream outToSessiond
;
45 private LogFramework log
;
47 private Semaphore registerSem
;
49 private static final String sessiondHost
= "127.0.0.1";
50 private static final String rootPortFile
= "/var/run/lttng/agent.port";
51 private static final String userPortFile
= "/.lttng/agent.port";
53 private static Integer protocolMajorVersion
= 1;
54 private static Integer protocolMinorVersion
= 0;
56 private LTTngAgent
.Domain agentDomain
;
58 /* Indicate if we've already release the semaphore. */
59 private boolean sem_posted
= false;
61 public LTTngTCPSessiondClient(LTTngAgent
.Domain domain
, LogFramework log
, Semaphore sem
) {
62 this.agentDomain
= domain
;
64 this.registerSem
= sem
;
68 * Try to release the registerSem if it's not already done.
70 private void tryReleaseSem()
72 /* Release semaphore so we unblock the agent. */
73 if (!this.sem_posted
) {
74 this.registerSem
.release();
75 this.sem_posted
= true;
86 /* Cleanup Agent state before trying to connect or reconnect. */
92 * Connect to the session daemon before anything else.
97 * Register to the session daemon as the Java component of the
100 registerToSessiond();
103 * Block on socket receive and wait for command from the
104 * session daemon. This will return if and only if there is a
105 * fatal error or the socket closes.
108 } catch (UnknownHostException uhe
) {
110 System
.out
.println(uhe
);
111 } catch (IOException ioe
) {
115 } catch (InterruptedException e
) {
118 } catch (Exception e
) {
125 public void destroy() {
129 if (this.sessiondSock
!= null) {
130 this.sessiondSock
.close();
132 } catch (Exception e
) {
138 * Receive header data from the session daemon using the LTTng command
139 * static buffer of the right size.
141 private void recvHeader() throws Exception
{
143 byte data
[] = new byte[LTTngSessiondCmd2_6
.sessiond_hdr
.SIZE
];
145 read_len
= this.inFromSessiond
.read(data
, 0, data
.length
);
146 if (read_len
!= data
.length
) {
147 throw new IOException();
149 this.headerCmd
.populate(data
);
153 * Receive payload from the session daemon. This MUST be done after a
154 * recvHeader() so the header value of a command are known.
156 * The caller SHOULD use isPayload() before which returns true if a payload
157 * is expected after the header.
159 private byte[] recvPayload() throws Exception
{
160 byte payload
[] = new byte[(int) this.headerCmd
.data_size
];
162 /* Failsafe check so we don't waste our time reading 0 bytes. */
163 if (payload
.length
== 0) {
167 this.inFromSessiond
.read(payload
, 0, payload
.length
);
172 * Handle session command from the session daemon.
174 private void handleSessiondCmd() throws Exception
{
178 /* Get header from session daemon. */
181 if (headerCmd
.data_size
> 0) {
182 data
= recvPayload();
185 switch (headerCmd
.cmd
) {
189 * Release semaphore so meaning registration is done and we
190 * can proceed to continue tracing.
194 * We don't send any reply to the registration done command.
195 * This just marks the end of the initial session setup.
201 LTTngSessiondCmd2_6
.sessiond_list_logger listLoggerCmd
=
202 new LTTngSessiondCmd2_6
.sessiond_list_logger();
203 listLoggerCmd
.execute(this.log
);
204 data
= listLoggerCmd
.getBytes();
209 LTTngSessiondCmd2_6
.sessiond_enable_handler enableCmd
=
210 new LTTngSessiondCmd2_6
.sessiond_enable_handler();
212 enableCmd
.code
= LTTngSessiondCmd2_6
.lttng_agent_ret_code
.CODE_INVALID_CMD
;
215 enableCmd
.populate(data
);
216 enableCmd
.execute(this.log
);
217 data
= enableCmd
.getBytes();
222 LTTngSessiondCmd2_6
.sessiond_disable_handler disableCmd
=
223 new LTTngSessiondCmd2_6
.sessiond_disable_handler();
225 disableCmd
.code
= LTTngSessiondCmd2_6
.lttng_agent_ret_code
.CODE_INVALID_CMD
;
228 disableCmd
.populate(data
);
229 disableCmd
.execute(this.log
);
230 data
= disableCmd
.getBytes();
236 ByteBuffer buf
= ByteBuffer
.wrap(data
);
237 buf
.order(ByteOrder
.BIG_ENDIAN
);
242 /* Send payload to session daemon. */
243 this.outToSessiond
.write(data
, 0, data
.length
);
244 this.outToSessiond
.flush();
248 private static String
getHomePath() {
249 return System
.getProperty("user.home");
253 * Read port number from file created by the session daemon.
255 * @return port value if found else 0.
257 private static int getPortFromFile(String path
) throws IOException
{
262 br
= new BufferedReader(new FileReader(path
));
263 String line
= br
.readLine();
264 port
= Integer
.parseInt(line
, 10);
265 if (port
< 0 || port
> 65535) {
266 /* Invalid value. Ignore. */
270 } catch (FileNotFoundException e
) {
271 /* No port available. */
278 private void connectToSessiond() throws Exception
{
281 if (this.log
.isRoot()) {
282 port
= getPortFromFile(rootPortFile
);
284 /* No session daemon available. Stop and retry later. */
285 throw new IOException();
288 port
= getPortFromFile(getHomePath() + userPortFile
);
290 /* No session daemon available. Stop and retry later. */
291 throw new IOException();
295 this.sessiondSock
= new Socket(sessiondHost
, port
);
296 this.inFromSessiond
= new DataInputStream(
297 sessiondSock
.getInputStream());
298 this.outToSessiond
= new DataOutputStream(
299 sessiondSock
.getOutputStream());
302 private void registerToSessiond() throws Exception
{
303 byte data
[] = new byte[16];
304 ByteBuffer buf
= ByteBuffer
.wrap(data
);
305 String pid
= ManagementFactory
.getRuntimeMXBean().getName().split("@")[0];
307 buf
.putInt(this.agentDomain
.value());
308 buf
.putInt(Integer
.parseInt(pid
));
309 buf
.putInt(protocolMajorVersion
);
310 buf
.putInt(protocolMinorVersion
);
311 this.outToSessiond
.write(data
, 0, data
.length
);
312 this.outToSessiond
.flush();