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
.jul
;
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
;
34 import java
.util
.logging
.Logger
;
36 class USTRegisterMsg
{
37 public static int pid
;
40 public class LTTngTCPSessiondClient
{
41 /* Command header from the session deamon. */
42 private LTTngSessiondCmd2_4
.sessiond_hdr headerCmd
=
43 new LTTngSessiondCmd2_4
.sessiond_hdr();
45 private final String sessiondHost
;
46 private Socket sessiondSock
;
47 private boolean quit
= false;
49 private DataInputStream inFromSessiond
;
50 private DataOutputStream outToSessiond
;
52 private LTTngLogHandler handler
;
54 private Semaphore registerSem
;
56 private static final String rootPortFile
= "/var/run/lttng/jul.port";
57 private static final String userPortFile
= "/.lttng/jul.port";
59 /* Indicate if we've already release the semaphore. */
60 private boolean sem_posted
= false;
62 public LTTngTCPSessiondClient(String host
, Semaphore sem
) {
63 this.sessiondHost
= host
;
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;
80 * Cleanup Agent state.
82 private void cleanupState() {
83 if (this.handler
!= null) {
88 public void init(LTTngLogHandler handler
) throws InterruptedException
{
89 this.handler
= handler
;
96 /* Cleanup Agent state before trying to connect or reconnect. */
102 * Connect to the session daemon before anything else.
107 * Register to the session daemon as the Java component of the
110 registerToSessiond();
113 * Block on socket receive and wait for command from the
114 * session daemon. This will return if and only if there is a
115 * fatal error or the socket closes.
118 } catch (UnknownHostException uhe
) {
120 System
.out
.println(uhe
);
121 } catch (IOException ioe
) {
124 } catch (Exception e
) {
131 public void destroy() {
135 if (this.sessiondSock
!= null) {
136 this.sessiondSock
.close();
138 } catch (Exception e
) {
144 * Receive header data from the session daemon using the LTTng command
145 * static buffer of the right size.
147 private void recvHeader() throws Exception
{
149 byte data
[] = new byte[this.headerCmd
.SIZE
];
151 read_len
= this.inFromSessiond
.read(data
, 0, data
.length
);
152 if (read_len
!= data
.length
) {
153 throw new IOException();
155 this.headerCmd
.populate(data
);
159 * Receive payload from the session daemon. This MUST be done after a
160 * recvHeader() so the header value of a command are known.
162 * The caller SHOULD use isPayload() before which returns true if a payload
163 * is expected after the header.
165 private byte[] recvPayload() throws Exception
{
166 byte payload
[] = new byte[(int) this.headerCmd
.data_size
];
168 /* Failsafe check so we don't waste our time reading 0 bytes. */
169 if (payload
.length
== 0) {
173 this.inFromSessiond
.read(payload
, 0, payload
.length
);
178 * Handle session command from the session daemon.
180 private void handleSessiondCmd() throws Exception
{
185 /* Get header from session daemon. */
188 if (headerCmd
.data_size
> 0) {
189 data
= recvPayload();
192 switch (headerCmd
.cmd
) {
196 * Release semaphore so meaning registration is done and we
197 * can proceed to continue tracing.
201 * We don't send any reply to the registration done command.
202 * This just marks the end of the initial session setup.
208 LTTngSessiondCmd2_4
.sessiond_list_logger listLoggerCmd
=
209 new LTTngSessiondCmd2_4
.sessiond_list_logger();
210 listLoggerCmd
.execute(this.handler
);
211 data
= listLoggerCmd
.getBytes();
217 LTTngSessiondCmd2_4
.sessiond_enable_handler enableCmd
=
218 new LTTngSessiondCmd2_4
.sessiond_enable_handler();
220 enableCmd
.code
= LTTngSessiondCmd2_4
.lttng_jul_ret_code
.CODE_INVALID_CMD
;
223 enableCmd
.populate(data
);
224 enableCmd
.execute(this.handler
);
225 data
= enableCmd
.getBytes();
230 LTTngSessiondCmd2_4
.sessiond_disable_handler disableCmd
=
231 new LTTngSessiondCmd2_4
.sessiond_disable_handler();
233 disableCmd
.code
= LTTngSessiondCmd2_4
.lttng_jul_ret_code
.CODE_INVALID_CMD
;
236 disableCmd
.populate(data
);
237 disableCmd
.execute(this.handler
);
238 data
= disableCmd
.getBytes();
244 ByteBuffer buf
= ByteBuffer
.wrap(data
);
245 buf
.order(ByteOrder
.BIG_ENDIAN
);
246 LTTngSessiondCmd2_4
.lttng_jul_ret_code code
=
247 LTTngSessiondCmd2_4
.lttng_jul_ret_code
.CODE_INVALID_CMD
;
248 buf
.putInt(code
.getCode());
253 /* Send payload to session daemon. */
254 this.outToSessiond
.write(data
, 0, data
.length
);
255 this.outToSessiond
.flush();
259 private String
getHomePath() {
260 return System
.getProperty("user.home");
264 * Read port number from file created by the session daemon.
266 * @return port value if found else 0.
268 private int getPortFromFile(String path
) throws IOException
{
273 br
= new BufferedReader(new FileReader(path
));
274 String line
= br
.readLine();
275 port
= Integer
.parseInt(line
, 10);
276 if (port
< 0 || port
> 65535) {
277 /* Invalid value. Ignore. */
281 } catch (FileNotFoundException e
) {
282 /* No port available. */
289 private void connectToSessiond() throws Exception
{
292 if (this.handler
.is_root
== 1) {
293 port
= getPortFromFile(rootPortFile
);
295 /* No session daemon available. Stop and retry later. */
296 throw new IOException();
299 port
= getPortFromFile(getHomePath() + userPortFile
);
301 /* No session daemon available. Stop and retry later. */
302 throw new IOException();
306 this.sessiondSock
= new Socket(this.sessiondHost
, port
);
307 this.inFromSessiond
= new DataInputStream(
308 sessiondSock
.getInputStream());
309 this.outToSessiond
= new DataOutputStream(
310 sessiondSock
.getOutputStream());
313 private void registerToSessiond() throws Exception
{
314 byte data
[] = new byte[4];
315 ByteBuffer buf
= ByteBuffer
.wrap(data
);
316 String pid
= ManagementFactory
.getRuntimeMXBean().getName().split("@")[0];
318 buf
.putInt(Integer
.parseInt(pid
));
319 this.outToSessiond
.write(data
, 0, data
.length
);
320 this.outToSessiond
.flush();