Commit | Line | Data |
---|---|---|
43e5396b | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
43e5396b | 3 | * |
c0c0989a MJ |
4 | * Copyright (C) 2015-2016 EfficiOS Inc. |
5 | * Copyright (C) 2015-2016 Alexandre Montplaisir <alexmonthy@efficios.com> | |
6 | * Copyright (C) 2013 David Goulet <dgoulet@efficios.com> | |
43e5396b DG |
7 | */ |
8 | ||
d60dfbe4 | 9 | package org.lttng.ust.agent.client; |
43e5396b | 10 | |
f1fa0535 | 11 | import java.io.BufferedReader; |
43e5396b | 12 | import java.io.DataInputStream; |
bc7de6d9 | 13 | import java.io.DataOutputStream; |
9f84d546 | 14 | import java.io.FileInputStream; |
f1fa0535 | 15 | import java.io.FileNotFoundException; |
bc7de6d9 | 16 | import java.io.IOException; |
9f84d546 | 17 | import java.io.InputStreamReader; |
43e5396b | 18 | import java.lang.management.ManagementFactory; |
bc7de6d9 AM |
19 | import java.net.Socket; |
20 | import java.net.UnknownHostException; | |
21 | import java.nio.ByteBuffer; | |
22 | import java.nio.ByteOrder; | |
9f84d546 | 23 | import java.nio.charset.Charset; |
d60dfbe4 AM |
24 | import java.util.concurrent.CountDownLatch; |
25 | import java.util.concurrent.TimeUnit; | |
43e5396b | 26 | |
cbe2ebd6 AM |
27 | import org.lttng.ust.agent.utils.LttngUstAgentLogger; |
28 | ||
d60dfbe4 AM |
29 | /** |
30 | * Client for agents to connect to a local session daemon, using a TCP socket. | |
31 | * | |
32 | * @author David Goulet | |
33 | */ | |
34 | public class LttngTcpSessiondClient implements Runnable { | |
43e5396b | 35 | |
08284556 AM |
36 | private static final String SESSION_HOST = "127.0.0.1"; |
37 | private static final String ROOT_PORT_FILE = "/var/run/lttng/agent.port"; | |
38 | private static final String USER_PORT_FILE = "/.lttng/agent.port"; | |
9f84d546 | 39 | private static final Charset PORT_FILE_ENCODING = Charset.forName("UTF-8"); |
08284556 | 40 | |
191f4058 AM |
41 | private static final int PROTOCOL_MAJOR_VERSION = 2; |
42 | private static final int PROTOCOL_MINOR_VERSION = 0; | |
08284556 | 43 | |
2fbda51c | 44 | /** Command header from the session daemon. */ |
d60dfbe4 | 45 | private final CountDownLatch registrationLatch = new CountDownLatch(1); |
43e5396b | 46 | |
43e5396b | 47 | private Socket sessiondSock; |
501f6777 | 48 | private volatile boolean quit = false; |
43e5396b DG |
49 | |
50 | private DataInputStream inFromSessiond; | |
51 | private DataOutputStream outToSessiond; | |
52 | ||
3165c2f5 AM |
53 | private final ILttngTcpClientListener logAgent; |
54 | private final int domainValue; | |
d60dfbe4 | 55 | private final boolean isRoot; |
501f6777 | 56 | |
d60dfbe4 AM |
57 | /** |
58 | * Constructor | |
59 | * | |
60 | * @param logAgent | |
3165c2f5 AM |
61 | * The listener this client will operate on, typically an LTTng |
62 | * agent. | |
63 | * @param domainValue | |
64 | * The integer to send to the session daemon representing the | |
65 | * tracing domain to handle. | |
d60dfbe4 AM |
66 | * @param isRoot |
67 | * True if this client should connect to the root session daemon, | |
68 | * false if it should connect to the user one. | |
69 | */ | |
3165c2f5 | 70 | public LttngTcpSessiondClient(ILttngTcpClientListener logAgent, int domainValue, boolean isRoot) { |
d60dfbe4 | 71 | this.logAgent = logAgent; |
3165c2f5 | 72 | this.domainValue = domainValue; |
d60dfbe4 | 73 | this.isRoot = isRoot; |
43e5396b DG |
74 | } |
75 | ||
d60dfbe4 AM |
76 | /** |
77 | * Wait until this client has successfully established a connection to its | |
78 | * target session daemon. | |
79 | * | |
80 | * @param seconds | |
81 | * A timeout in seconds after which this method will return | |
82 | * anyway. | |
83 | * @return True if the the client actually established the connection, false | |
84 | * if we returned because the timeout has elapsed or the thread was | |
85 | * interrupted. | |
f1fa0535 | 86 | */ |
d60dfbe4 AM |
87 | public boolean waitForConnection(int seconds) { |
88 | try { | |
89 | return registrationLatch.await(seconds, TimeUnit.SECONDS); | |
90 | } catch (InterruptedException e) { | |
91 | return false; | |
f1fa0535 DG |
92 | } |
93 | } | |
94 | ||
501f6777 CB |
95 | @Override |
96 | public void run() { | |
43e5396b DG |
97 | for (;;) { |
98 | if (this.quit) { | |
99 | break; | |
100 | } | |
101 | ||
102 | try { | |
103 | ||
104 | /* | |
105 | * Connect to the session daemon before anything else. | |
106 | */ | |
6e1fdc3a | 107 | log("Connecting to sessiond"); |
43e5396b DG |
108 | connectToSessiond(); |
109 | ||
110 | /* | |
111 | * Register to the session daemon as the Java component of the | |
112 | * UST application. | |
113 | */ | |
6e1fdc3a | 114 | log("Registering to sessiond"); |
43e5396b | 115 | registerToSessiond(); |
43e5396b | 116 | |
43e5396b DG |
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 | */ | |
6e1fdc3a | 122 | log("Waiting on sessiond commands..."); |
43e5396b DG |
123 | handleSessiondCmd(); |
124 | } catch (UnknownHostException uhe) { | |
d60dfbe4 | 125 | uhe.printStackTrace(); |
35cbacdb MD |
126 | /* |
127 | * Terminate agent thread. | |
128 | */ | |
129 | close(); | |
43e5396b | 130 | } catch (IOException ioe) { |
35cbacdb MD |
131 | /* |
132 | * I/O exception may have been triggered by a session daemon | |
133 | * closing the socket. Close our own socket and | |
134 | * retry connecting after a delay. | |
135 | */ | |
501f6777 | 136 | try { |
35cbacdb MD |
137 | if (this.sessiondSock != null) { |
138 | this.sessiondSock.close(); | |
139 | } | |
501f6777 CB |
140 | Thread.sleep(3000); |
141 | } catch (InterruptedException e) { | |
35cbacdb MD |
142 | /* |
143 | * Retry immediately if sleep is interrupted. | |
144 | */ | |
145 | } catch (IOException closeioe) { | |
146 | closeioe.printStackTrace(); | |
147 | /* | |
148 | * Terminate agent thread. | |
149 | */ | |
150 | close(); | |
501f6777 | 151 | } |
43e5396b DG |
152 | } |
153 | } | |
154 | } | |
155 | ||
d60dfbe4 AM |
156 | /** |
157 | * Dispose this client and close any socket connection it may hold. | |
158 | */ | |
159 | public void close() { | |
6e1fdc3a | 160 | log("Closing client"); |
43e5396b | 161 | this.quit = true; |
43e5396b DG |
162 | |
163 | try { | |
164 | if (this.sessiondSock != null) { | |
165 | this.sessiondSock.close(); | |
166 | } | |
d60dfbe4 | 167 | } catch (IOException e) { |
43e5396b DG |
168 | e.printStackTrace(); |
169 | } | |
170 | } | |
171 | ||
301a3ddb | 172 | private void connectToSessiond() throws IOException { |
c0f6fb05 | 173 | int portToUse; |
e0c010a9 AM |
174 | |
175 | /* | |
c0f6fb05 MD |
176 | * The environment variable LTTNG_UST_APP_PATH disables |
177 | * connection to per-user and root session daemons. | |
e0c010a9 | 178 | */ |
c0f6fb05 MD |
179 | String lttngUstAppPath = getUstAppPath(); |
180 | ||
181 | if (lttngUstAppPath != null) { | |
182 | portToUse = getPortFromFile(lttngUstAppPath + USER_PORT_FILE); | |
183 | } else { | |
184 | int rootPort = getPortFromFile(ROOT_PORT_FILE); | |
185 | int userPort = getPortFromFile(getHomePath() + USER_PORT_FILE); | |
186 | ||
187 | /* | |
188 | * Check for the edge case of both files existing but pointing to the | |
189 | * same port. In this case, let the root client handle it. | |
190 | */ | |
191 | if ((rootPort != 0) && (rootPort == userPort) && (!isRoot)) { | |
192 | log("User and root config files both point to port " + rootPort + | |
193 | ". Letting the root client handle it."); | |
194 | throw new IOException(); | |
195 | } | |
43e5396b | 196 | |
c0f6fb05 MD |
197 | portToUse = (isRoot ? rootPort : userPort); |
198 | } | |
e0c010a9 AM |
199 | |
200 | if (portToUse == 0) { | |
201 | /* No session daemon available. Stop and retry later. */ | |
202 | throw new IOException(); | |
43e5396b | 203 | } |
301a3ddb | 204 | |
e0c010a9 | 205 | this.sessiondSock = new Socket(SESSION_HOST, portToUse); |
301a3ddb AM |
206 | this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream()); |
207 | this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream()); | |
208 | } | |
209 | ||
c0f6fb05 MD |
210 | private static String getUstAppPath() { |
211 | return System.getenv("LTTNG_UST_APP_PATH"); | |
212 | } | |
213 | ||
301a3ddb | 214 | private static String getHomePath() { |
59e3be47 MD |
215 | /* |
216 | * The environment variable LTTNG_HOME overrides HOME if | |
c0f6fb05 | 217 | * set. |
59e3be47 | 218 | */ |
c0f6fb05 MD |
219 | String lttngHomePath = System.getenv("LTTNG_HOME"); |
220 | if (lttngHomePath != null) { | |
221 | return lttngHomePath; | |
59e3be47 | 222 | } |
c0f6fb05 | 223 | return System.getProperty("user.home"); |
43e5396b DG |
224 | } |
225 | ||
d60dfbe4 | 226 | /** |
301a3ddb | 227 | * Read port number from file created by the session daemon. |
43e5396b | 228 | * |
301a3ddb | 229 | * @return port value if found else 0. |
43e5396b | 230 | */ |
301a3ddb | 231 | private static int getPortFromFile(String path) throws IOException { |
301a3ddb | 232 | BufferedReader br = null; |
43e5396b | 233 | |
301a3ddb | 234 | try { |
9f84d546 | 235 | br = new BufferedReader(new InputStreamReader(new FileInputStream(path), PORT_FILE_ENCODING)); |
301a3ddb | 236 | String line = br.readLine(); |
8d8c99c9 AM |
237 | if (line == null) { |
238 | /* File exists but is empty. */ | |
239 | return 0; | |
240 | } | |
241 | ||
242 | int port = Integer.parseInt(line, 10); | |
301a3ddb AM |
243 | if (port < 0 || port > 65535) { |
244 | /* Invalid value. Ignore. */ | |
245 | port = 0; | |
246 | } | |
8d8c99c9 AM |
247 | return port; |
248 | ||
249 | } catch (NumberFormatException e) { | |
250 | /* File contained something that was not a number. */ | |
251 | return 0; | |
301a3ddb AM |
252 | } catch (FileNotFoundException e) { |
253 | /* No port available. */ | |
8d8c99c9 | 254 | return 0; |
301a3ddb AM |
255 | } finally { |
256 | if (br != null) { | |
257 | br.close(); | |
258 | } | |
43e5396b | 259 | } |
301a3ddb AM |
260 | } |
261 | ||
262 | private void registerToSessiond() throws IOException { | |
263 | byte data[] = new byte[16]; | |
264 | ByteBuffer buf = ByteBuffer.wrap(data); | |
265 | String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; | |
266 | ||
3165c2f5 | 267 | buf.putInt(domainValue); |
301a3ddb | 268 | buf.putInt(Integer.parseInt(pid)); |
191f4058 AM |
269 | buf.putInt(PROTOCOL_MAJOR_VERSION); |
270 | buf.putInt(PROTOCOL_MINOR_VERSION); | |
301a3ddb AM |
271 | this.outToSessiond.write(data, 0, data.length); |
272 | this.outToSessiond.flush(); | |
43e5396b DG |
273 | } |
274 | ||
d60dfbe4 | 275 | /** |
43e5396b DG |
276 | * Handle session command from the session daemon. |
277 | */ | |
d60dfbe4 | 278 | private void handleSessiondCmd() throws IOException { |
301a3ddb AM |
279 | /* Data read from the socket */ |
280 | byte inputData[] = null; | |
281 | /* Reply data written to the socket, sent to the sessiond */ | |
f35c6aa0 | 282 | LttngAgentResponse response; |
43e5396b DG |
283 | |
284 | while (true) { | |
285 | /* Get header from session daemon. */ | |
301a3ddb | 286 | SessiondCommandHeader cmdHeader = recvHeader(); |
43e5396b | 287 | |
301a3ddb AM |
288 | if (cmdHeader.getDataSize() > 0) { |
289 | inputData = recvPayload(cmdHeader); | |
43e5396b DG |
290 | } |
291 | ||
301a3ddb | 292 | switch (cmdHeader.getCommandType()) { |
d60dfbe4 AM |
293 | case CMD_REG_DONE: |
294 | { | |
295 | /* | |
296 | * Countdown the registration latch, meaning registration is | |
297 | * done and we can proceed to continue tracing. | |
298 | */ | |
299 | registrationLatch.countDown(); | |
300 | /* | |
301 | * We don't send any reply to the registration done command. | |
302 | * This just marks the end of the initial session setup. | |
303 | */ | |
6e1fdc3a | 304 | log("Registration done"); |
d60dfbe4 AM |
305 | continue; |
306 | } | |
307 | case CMD_LIST: | |
308 | { | |
1d193914 | 309 | SessiondCommand listLoggerCmd = new SessiondListLoggersCommand(); |
f35c6aa0 | 310 | response = listLoggerCmd.execute(logAgent); |
6e1fdc3a | 311 | log("Received list loggers command"); |
d60dfbe4 AM |
312 | break; |
313 | } | |
8ab5c06b | 314 | case CMD_EVENT_ENABLE: |
d60dfbe4 | 315 | { |
301a3ddb AM |
316 | if (inputData == null) { |
317 | /* Invalid command */ | |
f35c6aa0 | 318 | response = LttngAgentResponse.FAILURE_RESPONSE; |
43e5396b DG |
319 | break; |
320 | } | |
8ab5c06b | 321 | SessiondCommand enableEventCmd = new SessiondEnableEventCommand(inputData); |
f35c6aa0 AM |
322 | response = enableEventCmd.execute(logAgent); |
323 | log("Received enable event command: " + enableEventCmd.toString()); | |
d60dfbe4 AM |
324 | break; |
325 | } | |
8ab5c06b | 326 | case CMD_EVENT_DISABLE: |
d60dfbe4 | 327 | { |
301a3ddb AM |
328 | if (inputData == null) { |
329 | /* Invalid command */ | |
f35c6aa0 | 330 | response = LttngAgentResponse.FAILURE_RESPONSE; |
43e5396b DG |
331 | break; |
332 | } | |
8ab5c06b | 333 | SessiondCommand disableEventCmd = new SessiondDisableEventCommand(inputData); |
f35c6aa0 AM |
334 | response = disableEventCmd.execute(logAgent); |
335 | log("Received disable event command: " + disableEventCmd.toString()); | |
8ab5c06b AM |
336 | break; |
337 | } | |
338 | case CMD_APP_CTX_ENABLE: | |
339 | { | |
340 | if (inputData == null) { | |
341 | /* This commands expects a payload, invalid command */ | |
f35c6aa0 | 342 | response = LttngAgentResponse.FAILURE_RESPONSE; |
8ab5c06b AM |
343 | break; |
344 | } | |
345 | SessiondCommand enableAppCtxCmd = new SessiondEnableAppContextCommand(inputData); | |
f35c6aa0 | 346 | response = enableAppCtxCmd.execute(logAgent); |
6e1fdc3a | 347 | log("Received enable app-context command"); |
8ab5c06b AM |
348 | break; |
349 | } | |
350 | case CMD_APP_CTX_DISABLE: | |
351 | { | |
352 | if (inputData == null) { | |
353 | /* This commands expects a payload, invalid command */ | |
f35c6aa0 | 354 | response = LttngAgentResponse.FAILURE_RESPONSE; |
8ab5c06b AM |
355 | break; |
356 | } | |
357 | SessiondCommand disableAppCtxCmd = new SessiondDisableAppContextCommand(inputData); | |
f35c6aa0 | 358 | response = disableAppCtxCmd.execute(logAgent); |
6e1fdc3a | 359 | log("Received disable app-context command"); |
d60dfbe4 AM |
360 | break; |
361 | } | |
362 | default: | |
363 | { | |
301a3ddb | 364 | /* Unknown command, send empty reply */ |
f35c6aa0 | 365 | response = null; |
6e1fdc3a | 366 | log("Received unknown command, ignoring"); |
d60dfbe4 AM |
367 | break; |
368 | } | |
43e5396b DG |
369 | } |
370 | ||
301a3ddb | 371 | /* Send response to the session daemon. */ |
f35c6aa0 AM |
372 | byte[] responseData; |
373 | if (response == null) { | |
374 | responseData = new byte[4]; | |
375 | ByteBuffer buf = ByteBuffer.wrap(responseData); | |
376 | buf.order(ByteOrder.BIG_ENDIAN); | |
377 | } else { | |
378 | log("Sending response: " + response.toString()); | |
379 | responseData = response.getBytes(); | |
380 | } | |
301a3ddb | 381 | this.outToSessiond.write(responseData, 0, responseData.length); |
43e5396b DG |
382 | this.outToSessiond.flush(); |
383 | } | |
384 | } | |
385 | ||
f1fa0535 | 386 | /** |
301a3ddb AM |
387 | * Receive header data from the session daemon using the LTTng command |
388 | * static buffer of the right size. | |
f1fa0535 | 389 | */ |
301a3ddb AM |
390 | private SessiondCommandHeader recvHeader() throws IOException { |
391 | byte data[] = new byte[SessiondCommandHeader.HEADER_SIZE]; | |
32974710 MD |
392 | int bytesLeft = data.length; |
393 | int bytesOffset = 0; | |
f1fa0535 | 394 | |
dcd9a9d7 | 395 | while (bytesLeft > 0) { |
32974710 MD |
396 | int bytesRead = this.inFromSessiond.read(data, bytesOffset, bytesLeft); |
397 | ||
398 | if (bytesRead < 0) { | |
399 | throw new IOException(); | |
400 | } | |
401 | bytesLeft -= bytesRead; | |
402 | bytesOffset += bytesRead; | |
f1fa0535 | 403 | } |
301a3ddb | 404 | return new SessiondCommandHeader(data); |
f1fa0535 DG |
405 | } |
406 | ||
301a3ddb AM |
407 | /** |
408 | * Receive payload from the session daemon. This MUST be done after a | |
409 | * recvHeader() so the header value of a command are known. | |
410 | * | |
411 | * The caller SHOULD use isPayload() before which returns true if a payload | |
412 | * is expected after the header. | |
413 | */ | |
414 | private byte[] recvPayload(SessiondCommandHeader headerCmd) throws IOException { | |
415 | byte payload[] = new byte[(int) headerCmd.getDataSize()]; | |
32974710 MD |
416 | int bytesLeft = payload.length; |
417 | int bytesOffset = 0; | |
f1fa0535 | 418 | |
301a3ddb | 419 | /* Failsafe check so we don't waste our time reading 0 bytes. */ |
32974710 | 420 | if (bytesLeft == 0) { |
301a3ddb | 421 | return null; |
f1fa0535 DG |
422 | } |
423 | ||
dcd9a9d7 | 424 | while (bytesLeft > 0) { |
32974710 MD |
425 | int bytesRead = inFromSessiond.read(payload, bytesOffset, bytesLeft); |
426 | ||
427 | if (bytesRead < 0) { | |
428 | throw new IOException(); | |
429 | } | |
430 | bytesLeft -= bytesRead; | |
431 | bytesOffset += bytesRead; | |
1e111005 | 432 | } |
301a3ddb | 433 | return payload; |
43e5396b DG |
434 | } |
435 | ||
6e1fdc3a AM |
436 | /** |
437 | * Wrapper for this class's logging, adds the connection's characteristics | |
438 | * to help differentiate between multiple TCP clients. | |
439 | */ | |
440 | private void log(String message) { | |
441 | LttngUstAgentLogger.log(getClass(), | |
442 | "(root=" + isRoot + ", domain=" + domainValue + ") " + message); | |
443 | } | |
43e5396b | 444 | } |