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