Commit | Line | Data |
---|---|---|
43e5396b | 1 | /* |
8ab5c06b | 2 | * Copyright (C) 2015-2016 EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> |
43e5396b DG |
3 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
4 | * | |
5 | * This library is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
12 | * for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public License | |
15 | * along with this library; if not, write to the Free Software Foundation, | |
16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
d60dfbe4 | 19 | package org.lttng.ust.agent.client; |
43e5396b | 20 | |
f1fa0535 | 21 | import java.io.BufferedReader; |
43e5396b | 22 | import java.io.DataInputStream; |
bc7de6d9 | 23 | import java.io.DataOutputStream; |
f1fa0535 | 24 | import java.io.FileNotFoundException; |
bc7de6d9 AM |
25 | import java.io.FileReader; |
26 | import java.io.IOException; | |
43e5396b | 27 | import java.lang.management.ManagementFactory; |
bc7de6d9 AM |
28 | import java.net.Socket; |
29 | import java.net.UnknownHostException; | |
30 | import java.nio.ByteBuffer; | |
31 | import java.nio.ByteOrder; | |
d60dfbe4 AM |
32 | import java.util.concurrent.CountDownLatch; |
33 | import java.util.concurrent.TimeUnit; | |
43e5396b | 34 | |
cbe2ebd6 AM |
35 | import org.lttng.ust.agent.utils.LttngUstAgentLogger; |
36 | ||
d60dfbe4 AM |
37 | /** |
38 | * Client for agents to connect to a local session daemon, using a TCP socket. | |
39 | * | |
40 | * @author David Goulet | |
41 | */ | |
42 | public class LttngTcpSessiondClient implements Runnable { | |
43e5396b | 43 | |
08284556 AM |
44 | private static final String SESSION_HOST = "127.0.0.1"; |
45 | private static final String ROOT_PORT_FILE = "/var/run/lttng/agent.port"; | |
46 | private static final String USER_PORT_FILE = "/.lttng/agent.port"; | |
47 | ||
191f4058 AM |
48 | private static final int PROTOCOL_MAJOR_VERSION = 2; |
49 | private static final int PROTOCOL_MINOR_VERSION = 0; | |
08284556 | 50 | |
d60dfbe4 | 51 | /** Command header from the session deamon. */ |
d60dfbe4 | 52 | private final CountDownLatch registrationLatch = new CountDownLatch(1); |
43e5396b | 53 | |
43e5396b | 54 | private Socket sessiondSock; |
501f6777 | 55 | private volatile boolean quit = false; |
43e5396b DG |
56 | |
57 | private DataInputStream inFromSessiond; | |
58 | private DataOutputStream outToSessiond; | |
59 | ||
3165c2f5 AM |
60 | private final ILttngTcpClientListener logAgent; |
61 | private final int domainValue; | |
d60dfbe4 | 62 | private final boolean isRoot; |
501f6777 | 63 | |
d60dfbe4 AM |
64 | /** |
65 | * Constructor | |
66 | * | |
67 | * @param logAgent | |
3165c2f5 AM |
68 | * The listener this client will operate on, typically an LTTng |
69 | * agent. | |
70 | * @param domainValue | |
71 | * The integer to send to the session daemon representing the | |
72 | * tracing domain to handle. | |
d60dfbe4 AM |
73 | * @param isRoot |
74 | * True if this client should connect to the root session daemon, | |
75 | * false if it should connect to the user one. | |
76 | */ | |
3165c2f5 | 77 | public LttngTcpSessiondClient(ILttngTcpClientListener logAgent, int domainValue, boolean isRoot) { |
d60dfbe4 | 78 | this.logAgent = logAgent; |
3165c2f5 | 79 | this.domainValue = domainValue; |
d60dfbe4 | 80 | this.isRoot = isRoot; |
43e5396b DG |
81 | } |
82 | ||
d60dfbe4 AM |
83 | /** |
84 | * Wait until this client has successfully established a connection to its | |
85 | * target session daemon. | |
86 | * | |
87 | * @param seconds | |
88 | * A timeout in seconds after which this method will return | |
89 | * anyway. | |
90 | * @return True if the the client actually established the connection, false | |
91 | * if we returned because the timeout has elapsed or the thread was | |
92 | * interrupted. | |
f1fa0535 | 93 | */ |
d60dfbe4 AM |
94 | public boolean waitForConnection(int seconds) { |
95 | try { | |
96 | return registrationLatch.await(seconds, TimeUnit.SECONDS); | |
97 | } catch (InterruptedException e) { | |
98 | return false; | |
f1fa0535 DG |
99 | } |
100 | } | |
101 | ||
501f6777 CB |
102 | @Override |
103 | public void run() { | |
43e5396b DG |
104 | for (;;) { |
105 | if (this.quit) { | |
106 | break; | |
107 | } | |
108 | ||
109 | try { | |
110 | ||
111 | /* | |
112 | * Connect to the session daemon before anything else. | |
113 | */ | |
cbe2ebd6 | 114 | LttngUstAgentLogger.log(getClass(), "Connecting to sessiond"); |
43e5396b DG |
115 | connectToSessiond(); |
116 | ||
117 | /* | |
118 | * Register to the session daemon as the Java component of the | |
119 | * UST application. | |
120 | */ | |
cbe2ebd6 | 121 | LttngUstAgentLogger.log(getClass(), "Registering to sessiond"); |
43e5396b | 122 | registerToSessiond(); |
43e5396b | 123 | |
43e5396b DG |
124 | /* |
125 | * Block on socket receive and wait for command from the | |
126 | * session daemon. This will return if and only if there is a | |
127 | * fatal error or the socket closes. | |
128 | */ | |
cbe2ebd6 | 129 | LttngUstAgentLogger.log(getClass(), "Waiting on sessiond commands..."); |
43e5396b DG |
130 | handleSessiondCmd(); |
131 | } catch (UnknownHostException uhe) { | |
d60dfbe4 | 132 | uhe.printStackTrace(); |
43e5396b | 133 | } catch (IOException ioe) { |
501f6777 CB |
134 | try { |
135 | Thread.sleep(3000); | |
136 | } catch (InterruptedException e) { | |
137 | e.printStackTrace(); | |
138 | } | |
43e5396b DG |
139 | } |
140 | } | |
141 | } | |
142 | ||
d60dfbe4 AM |
143 | /** |
144 | * Dispose this client and close any socket connection it may hold. | |
145 | */ | |
146 | public void close() { | |
cbe2ebd6 | 147 | LttngUstAgentLogger.log(getClass(), "Closing client"); |
43e5396b | 148 | this.quit = true; |
43e5396b DG |
149 | |
150 | try { | |
151 | if (this.sessiondSock != null) { | |
152 | this.sessiondSock.close(); | |
153 | } | |
d60dfbe4 | 154 | } catch (IOException e) { |
43e5396b DG |
155 | e.printStackTrace(); |
156 | } | |
157 | } | |
158 | ||
301a3ddb AM |
159 | private void connectToSessiond() throws IOException { |
160 | int port; | |
43e5396b | 161 | |
301a3ddb AM |
162 | if (this.isRoot) { |
163 | port = getPortFromFile(ROOT_PORT_FILE); | |
164 | if (port == 0) { | |
165 | /* No session daemon available. Stop and retry later. */ | |
166 | throw new IOException(); | |
167 | } | |
168 | } else { | |
169 | port = getPortFromFile(getHomePath() + USER_PORT_FILE); | |
170 | if (port == 0) { | |
171 | /* No session daemon available. Stop and retry later. */ | |
172 | throw new IOException(); | |
173 | } | |
43e5396b | 174 | } |
301a3ddb AM |
175 | |
176 | this.sessiondSock = new Socket(SESSION_HOST, port); | |
177 | this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream()); | |
178 | this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream()); | |
179 | } | |
180 | ||
181 | private static String getHomePath() { | |
59e3be47 MD |
182 | /* |
183 | * The environment variable LTTNG_HOME overrides HOME if | |
184 | * defined. | |
185 | */ | |
186 | String homePath = System.getenv("LTTNG_HOME"); | |
187 | ||
188 | if (homePath == null) { | |
189 | homePath = System.getProperty("user.home"); | |
190 | } | |
191 | return homePath; | |
43e5396b DG |
192 | } |
193 | ||
d60dfbe4 | 194 | /** |
301a3ddb | 195 | * Read port number from file created by the session daemon. |
43e5396b | 196 | * |
301a3ddb | 197 | * @return port value if found else 0. |
43e5396b | 198 | */ |
301a3ddb AM |
199 | private static int getPortFromFile(String path) throws IOException { |
200 | int port; | |
201 | BufferedReader br = null; | |
43e5396b | 202 | |
301a3ddb AM |
203 | try { |
204 | br = new BufferedReader(new FileReader(path)); | |
205 | String line = br.readLine(); | |
206 | port = Integer.parseInt(line, 10); | |
207 | if (port < 0 || port > 65535) { | |
208 | /* Invalid value. Ignore. */ | |
209 | port = 0; | |
210 | } | |
211 | } catch (FileNotFoundException e) { | |
212 | /* No port available. */ | |
213 | port = 0; | |
214 | } finally { | |
215 | if (br != null) { | |
216 | br.close(); | |
217 | } | |
43e5396b DG |
218 | } |
219 | ||
301a3ddb AM |
220 | return port; |
221 | } | |
222 | ||
223 | private void registerToSessiond() throws IOException { | |
224 | byte data[] = new byte[16]; | |
225 | ByteBuffer buf = ByteBuffer.wrap(data); | |
226 | String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; | |
227 | ||
3165c2f5 | 228 | buf.putInt(domainValue); |
301a3ddb | 229 | buf.putInt(Integer.parseInt(pid)); |
191f4058 AM |
230 | buf.putInt(PROTOCOL_MAJOR_VERSION); |
231 | buf.putInt(PROTOCOL_MINOR_VERSION); | |
301a3ddb AM |
232 | this.outToSessiond.write(data, 0, data.length); |
233 | this.outToSessiond.flush(); | |
43e5396b DG |
234 | } |
235 | ||
d60dfbe4 | 236 | /** |
43e5396b DG |
237 | * Handle session command from the session daemon. |
238 | */ | |
d60dfbe4 | 239 | private void handleSessiondCmd() throws IOException { |
301a3ddb AM |
240 | /* Data read from the socket */ |
241 | byte inputData[] = null; | |
242 | /* Reply data written to the socket, sent to the sessiond */ | |
243 | byte responseData[] = null; | |
43e5396b DG |
244 | |
245 | while (true) { | |
246 | /* Get header from session daemon. */ | |
301a3ddb | 247 | SessiondCommandHeader cmdHeader = recvHeader(); |
43e5396b | 248 | |
301a3ddb AM |
249 | if (cmdHeader.getDataSize() > 0) { |
250 | inputData = recvPayload(cmdHeader); | |
43e5396b DG |
251 | } |
252 | ||
301a3ddb | 253 | switch (cmdHeader.getCommandType()) { |
d60dfbe4 AM |
254 | case CMD_REG_DONE: |
255 | { | |
256 | /* | |
257 | * Countdown the registration latch, meaning registration is | |
258 | * done and we can proceed to continue tracing. | |
259 | */ | |
260 | registrationLatch.countDown(); | |
261 | /* | |
262 | * We don't send any reply to the registration done command. | |
263 | * This just marks the end of the initial session setup. | |
264 | */ | |
cbe2ebd6 | 265 | LttngUstAgentLogger.log(getClass(), "Registration done"); |
d60dfbe4 AM |
266 | continue; |
267 | } | |
268 | case CMD_LIST: | |
269 | { | |
1d193914 | 270 | SessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); |
93253569 | 271 | LttngAgentResponse response = listLoggerCmd.execute(logAgent); |
301a3ddb | 272 | responseData = response.getBytes(); |
cbe2ebd6 | 273 | LttngUstAgentLogger.log(getClass(), "Received list loggers command"); |
d60dfbe4 AM |
274 | break; |
275 | } | |
8ab5c06b | 276 | case CMD_EVENT_ENABLE: |
d60dfbe4 | 277 | { |
301a3ddb AM |
278 | if (inputData == null) { |
279 | /* Invalid command */ | |
93253569 | 280 | responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); |
43e5396b DG |
281 | break; |
282 | } | |
8ab5c06b AM |
283 | SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData); |
284 | LttngAgentResponse response = enableEventCmd.execute(logAgent); | |
301a3ddb | 285 | responseData = response.getBytes(); |
cbe2ebd6 | 286 | LttngUstAgentLogger.log(getClass(), "Received enable event command"); |
d60dfbe4 AM |
287 | break; |
288 | } | |
8ab5c06b | 289 | case CMD_EVENT_DISABLE: |
d60dfbe4 | 290 | { |
301a3ddb AM |
291 | if (inputData == null) { |
292 | /* Invalid command */ | |
93253569 | 293 | responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); |
43e5396b DG |
294 | break; |
295 | } | |
8ab5c06b AM |
296 | SessiondCommand disableEventCmd = new SessiondDisableEventCommand(inputData); |
297 | LttngAgentResponse response = disableEventCmd.execute(logAgent); | |
298 | responseData = response.getBytes(); | |
cbe2ebd6 | 299 | LttngUstAgentLogger.log(getClass(), "Received disable event command"); |
8ab5c06b AM |
300 | break; |
301 | } | |
302 | case CMD_APP_CTX_ENABLE: | |
303 | { | |
304 | if (inputData == null) { | |
305 | /* This commands expects a payload, invalid command */ | |
306 | responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); | |
307 | break; | |
308 | } | |
309 | SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData); | |
310 | LttngAgentResponse response = enableAppCtxCmd.execute(logAgent); | |
311 | responseData = response.getBytes(); | |
cbe2ebd6 | 312 | LttngUstAgentLogger.log(getClass(), "Received enable app-context command"); |
8ab5c06b AM |
313 | break; |
314 | } | |
315 | case CMD_APP_CTX_DISABLE: | |
316 | { | |
317 | if (inputData == null) { | |
318 | /* This commands expects a payload, invalid command */ | |
319 | responseData = LttngAgentResponse.FAILURE_RESPONSE.getBytes(); | |
320 | break; | |
321 | } | |
322 | SessiondCommand disableAppCtxCmd = new SessiondDisableAppContextCommand(inputData); | |
323 | LttngAgentResponse response = disableAppCtxCmd.execute(logAgent); | |
301a3ddb | 324 | responseData = response.getBytes(); |
cbe2ebd6 | 325 | LttngUstAgentLogger.log(getClass(), "Received disable app-context command"); |
d60dfbe4 AM |
326 | break; |
327 | } | |
328 | default: | |
329 | { | |
301a3ddb AM |
330 | /* Unknown command, send empty reply */ |
331 | responseData = new byte[4]; | |
332 | ByteBuffer buf = ByteBuffer.wrap(responseData); | |
d60dfbe4 | 333 | buf.order(ByteOrder.BIG_ENDIAN); |
cbe2ebd6 | 334 | LttngUstAgentLogger.log(getClass(), "Received unknown command, ignoring"); |
d60dfbe4 AM |
335 | break; |
336 | } | |
43e5396b DG |
337 | } |
338 | ||
301a3ddb | 339 | /* Send response to the session daemon. */ |
cbe2ebd6 | 340 | LttngUstAgentLogger.log(getClass(), "Sending response"); |
301a3ddb | 341 | this.outToSessiond.write(responseData, 0, responseData.length); |
43e5396b DG |
342 | this.outToSessiond.flush(); |
343 | } | |
344 | } | |
345 | ||
f1fa0535 | 346 | /** |
301a3ddb AM |
347 | * Receive header data from the session daemon using the LTTng command |
348 | * static buffer of the right size. | |
f1fa0535 | 349 | */ |
301a3ddb AM |
350 | private SessiondCommandHeader recvHeader() throws IOException { |
351 | byte data[] = new byte[SessiondCommandHeader.HEADER_SIZE]; | |
f1fa0535 | 352 | |
301a3ddb AM |
353 | int readLen = this.inFromSessiond.read(data, 0, data.length); |
354 | if (readLen != data.length) { | |
355 | throw new IOException(); | |
f1fa0535 | 356 | } |
301a3ddb | 357 | return new SessiondCommandHeader(data); |
f1fa0535 DG |
358 | } |
359 | ||
301a3ddb AM |
360 | /** |
361 | * Receive payload from the session daemon. This MUST be done after a | |
362 | * recvHeader() so the header value of a command are known. | |
363 | * | |
364 | * The caller SHOULD use isPayload() before which returns true if a payload | |
365 | * is expected after the header. | |
366 | */ | |
367 | private byte[] recvPayload(SessiondCommandHeader headerCmd) throws IOException { | |
368 | byte payload[] = new byte[(int) headerCmd.getDataSize()]; | |
f1fa0535 | 369 | |
301a3ddb AM |
370 | /* Failsafe check so we don't waste our time reading 0 bytes. */ |
371 | if (payload.length == 0) { | |
372 | return null; | |
f1fa0535 DG |
373 | } |
374 | ||
301a3ddb AM |
375 | this.inFromSessiond.read(payload, 0, payload.length); |
376 | return payload; | |
43e5396b DG |
377 | } |
378 | ||
43e5396b | 379 | } |