Build the liblttng-ust-java library using the new M4 macros
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngTCPSessiondClient.java
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * for more details.
12 *
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
16 */
17
18 package org.lttng.ust.jul;
19
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;
32 import java.net.*;
33 import java.lang.management.ManagementFactory;
34 import java.util.logging.Logger;
35
36 class USTRegisterMsg {
37 public static int pid;
38 }
39
40 public class LTTngTCPSessiondClient {
41 /* Command header from the session deamon. */
42 private LTTngSessiondCmd2_4.sessiond_hdr headerCmd =
43 new LTTngSessiondCmd2_4.sessiond_hdr();
44
45 private final String sessiondHost;
46 private Socket sessiondSock;
47 private boolean quit = false;
48
49 private DataInputStream inFromSessiond;
50 private DataOutputStream outToSessiond;
51
52 private LTTngLogHandler handler;
53
54 private Semaphore registerSem;
55
56 private static final String rootPortFile = "/var/run/lttng/agent.port";
57 private static final String userPortFile = "/.lttng/agent.port";
58 /*
59 * This is taken from the lttng/domain.h file which is mapped to
60 * LTTNG_DOMAIN_JUL value for this agent.
61 */
62 private static final int agent_domain = 3;
63
64 /* Indicate if we've already release the semaphore. */
65 private boolean sem_posted = false;
66
67 public LTTngTCPSessiondClient(String host, Semaphore sem) {
68 this.sessiondHost = host;
69 this.registerSem = sem;
70 }
71
72 /*
73 * Try to release the registerSem if it's not already done.
74 */
75 private void tryReleaseSem()
76 {
77 /* Release semaphore so we unblock the agent. */
78 if (!this.sem_posted) {
79 this.registerSem.release();
80 this.sem_posted = true;
81 }
82 }
83
84 /*
85 * Cleanup Agent state.
86 */
87 private void cleanupState() {
88 if (this.handler != null) {
89 this.handler.clear();
90 }
91 }
92
93 public void init(LTTngLogHandler handler) throws InterruptedException {
94 this.handler = handler;
95
96 for (;;) {
97 if (this.quit) {
98 break;
99 }
100
101 /* Cleanup Agent state before trying to connect or reconnect. */
102 cleanupState();
103
104 try {
105
106 /*
107 * Connect to the session daemon before anything else.
108 */
109 connectToSessiond();
110
111 /*
112 * Register to the session daemon as the Java component of the
113 * UST application.
114 */
115 registerToSessiond();
116
117 /*
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.
121 */
122 handleSessiondCmd();
123 } catch (UnknownHostException uhe) {
124 tryReleaseSem();
125 System.out.println(uhe);
126 } catch (IOException ioe) {
127 tryReleaseSem();
128 Thread.sleep(3000);
129 } catch (Exception e) {
130 tryReleaseSem();
131 e.printStackTrace();
132 }
133 }
134 }
135
136 public void destroy() {
137 this.quit = true;
138
139 try {
140 if (this.sessiondSock != null) {
141 this.sessiondSock.close();
142 }
143 } catch (Exception e) {
144 e.printStackTrace();
145 }
146 }
147
148 /*
149 * Receive header data from the session daemon using the LTTng command
150 * static buffer of the right size.
151 */
152 private void recvHeader() throws Exception {
153 int read_len;
154 byte data[] = new byte[this.headerCmd.SIZE];
155
156 read_len = this.inFromSessiond.read(data, 0, data.length);
157 if (read_len != data.length) {
158 throw new IOException();
159 }
160 this.headerCmd.populate(data);
161 }
162
163 /*
164 * Receive payload from the session daemon. This MUST be done after a
165 * recvHeader() so the header value of a command are known.
166 *
167 * The caller SHOULD use isPayload() before which returns true if a payload
168 * is expected after the header.
169 */
170 private byte[] recvPayload() throws Exception {
171 byte payload[] = new byte[(int) this.headerCmd.data_size];
172
173 /* Failsafe check so we don't waste our time reading 0 bytes. */
174 if (payload.length == 0) {
175 return null;
176 }
177
178 this.inFromSessiond.read(payload, 0, payload.length);
179 return payload;
180 }
181
182 /*
183 * Handle session command from the session daemon.
184 */
185 private void handleSessiondCmd() throws Exception {
186 int ret_code;
187 byte data[] = null;
188
189 while (true) {
190 /* Get header from session daemon. */
191 recvHeader();
192
193 if (headerCmd.data_size > 0) {
194 data = recvPayload();
195 }
196
197 switch (headerCmd.cmd) {
198 case CMD_REG_DONE:
199 {
200 /*
201 * Release semaphore so meaning registration is done and we
202 * can proceed to continue tracing.
203 */
204 tryReleaseSem();
205 /*
206 * We don't send any reply to the registration done command.
207 * This just marks the end of the initial session setup.
208 */
209 continue;
210 }
211 case CMD_LIST:
212 {
213 LTTngSessiondCmd2_4.sessiond_list_logger listLoggerCmd =
214 new LTTngSessiondCmd2_4.sessiond_list_logger();
215 listLoggerCmd.execute(this.handler);
216 data = listLoggerCmd.getBytes();
217 break;
218 }
219 case CMD_ENABLE:
220 {
221 LTTngEvent event;
222 LTTngSessiondCmd2_4.sessiond_enable_handler enableCmd =
223 new LTTngSessiondCmd2_4.sessiond_enable_handler();
224 if (data == null) {
225 enableCmd.code = LTTngSessiondCmd2_4.lttng_jul_ret_code.CODE_INVALID_CMD;
226 break;
227 }
228 enableCmd.populate(data);
229 enableCmd.execute(this.handler);
230 data = enableCmd.getBytes();
231 break;
232 }
233 case CMD_DISABLE:
234 {
235 LTTngSessiondCmd2_4.sessiond_disable_handler disableCmd =
236 new LTTngSessiondCmd2_4.sessiond_disable_handler();
237 if (data == null) {
238 disableCmd.code = LTTngSessiondCmd2_4.lttng_jul_ret_code.CODE_INVALID_CMD;
239 break;
240 }
241 disableCmd.populate(data);
242 disableCmd.execute(this.handler);
243 data = disableCmd.getBytes();
244 break;
245 }
246 default:
247 {
248 data = new byte[4];
249 ByteBuffer buf = ByteBuffer.wrap(data);
250 buf.order(ByteOrder.BIG_ENDIAN);
251 LTTngSessiondCmd2_4.lttng_jul_ret_code code =
252 LTTngSessiondCmd2_4.lttng_jul_ret_code.CODE_INVALID_CMD;
253 buf.putInt(code.getCode());
254 break;
255 }
256 }
257
258 /* Send payload to session daemon. */
259 this.outToSessiond.write(data, 0, data.length);
260 this.outToSessiond.flush();
261 }
262 }
263
264 private String getHomePath() {
265 return System.getProperty("user.home");
266 }
267
268 /**
269 * Read port number from file created by the session daemon.
270 *
271 * @return port value if found else 0.
272 */
273 private int getPortFromFile(String path) throws IOException {
274 int port;
275 BufferedReader br;
276
277 try {
278 br = new BufferedReader(new FileReader(path));
279 String line = br.readLine();
280 port = Integer.parseInt(line, 10);
281 if (port < 0 || port > 65535) {
282 /* Invalid value. Ignore. */
283 port = 0;
284 }
285 br.close();
286 } catch (FileNotFoundException e) {
287 /* No port available. */
288 port = 0;
289 }
290
291 return port;
292 }
293
294 private void connectToSessiond() throws Exception {
295 int port;
296
297 if (this.handler.is_root == 1) {
298 port = getPortFromFile(rootPortFile);
299 if (port == 0) {
300 /* No session daemon available. Stop and retry later. */
301 throw new IOException();
302 }
303 } else {
304 port = getPortFromFile(getHomePath() + userPortFile);
305 if (port == 0) {
306 /* No session daemon available. Stop and retry later. */
307 throw new IOException();
308 }
309 }
310
311 this.sessiondSock = new Socket(this.sessiondHost, port);
312 this.inFromSessiond = new DataInputStream(
313 sessiondSock.getInputStream());
314 this.outToSessiond = new DataOutputStream(
315 sessiondSock.getOutputStream());
316 }
317
318 private void registerToSessiond() throws Exception {
319 byte data[] = new byte[8];
320 ByteBuffer buf = ByteBuffer.wrap(data);
321 String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0];
322
323 buf.putInt(this.agent_domain);
324 buf.putInt(Integer.parseInt(pid));
325 this.outToSessiond.write(data, 0, data.length);
326 this.outToSessiond.flush();
327 }
328 }
This page took 0.03601 seconds and 4 git commands to generate.