Commit | Line | Data |
---|---|---|
43e5396b DG |
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 | ||
501f6777 | 18 | package org.lttng.ust.agent; |
43e5396b | 19 | |
f1fa0535 | 20 | import java.io.BufferedReader; |
43e5396b | 21 | import java.io.DataInputStream; |
57224e35 | 22 | import java.io.DataOutputStream; |
f1fa0535 | 23 | import java.io.FileNotFoundException; |
57224e35 AM |
24 | import java.io.FileReader; |
25 | import java.io.IOException; | |
43e5396b | 26 | import java.lang.management.ManagementFactory; |
57224e35 AM |
27 | import java.net.Socket; |
28 | import java.net.UnknownHostException; | |
29 | import java.nio.ByteBuffer; | |
30 | import java.nio.ByteOrder; | |
31 | import java.util.concurrent.Semaphore; | |
43e5396b | 32 | |
501f6777 | 33 | class LTTngTCPSessiondClient implements Runnable { |
43e5396b | 34 | |
43e5396b | 35 | /* Command header from the session deamon. */ |
501f6777 CB |
36 | private LTTngSessiondCmd2_6.sessiond_hdr headerCmd = |
37 | new LTTngSessiondCmd2_6.sessiond_hdr(); | |
43e5396b | 38 | |
43e5396b | 39 | private Socket sessiondSock; |
501f6777 | 40 | private volatile boolean quit = false; |
43e5396b DG |
41 | |
42 | private DataInputStream inFromSessiond; | |
43 | private DataOutputStream outToSessiond; | |
44 | ||
501f6777 | 45 | private LogFramework log; |
43e5396b DG |
46 | |
47 | private Semaphore registerSem; | |
48 | ||
501f6777 | 49 | private static final String sessiondHost = "127.0.0.1"; |
2b8f5235 DG |
50 | private static final String rootPortFile = "/var/run/lttng/agent.port"; |
51 | private static final String userPortFile = "/.lttng/agent.port"; | |
501f6777 CB |
52 | |
53 | private static Integer protocolMajorVersion = 1; | |
54 | private static Integer protocolMinorVersion = 0; | |
55 | ||
56 | private LTTngAgent.Domain agentDomain; | |
f1fa0535 DG |
57 | |
58 | /* Indicate if we've already release the semaphore. */ | |
59 | private boolean sem_posted = false; | |
60 | ||
501f6777 CB |
61 | public LTTngTCPSessiondClient(LTTngAgent.Domain domain, LogFramework log, Semaphore sem) { |
62 | this.agentDomain = domain; | |
63 | this.log = log; | |
43e5396b | 64 | this.registerSem = sem; |
43e5396b DG |
65 | } |
66 | ||
f1fa0535 DG |
67 | /* |
68 | * Try to release the registerSem if it's not already done. | |
69 | */ | |
70 | private void tryReleaseSem() | |
71 | { | |
72 | /* Release semaphore so we unblock the agent. */ | |
73 | if (!this.sem_posted) { | |
74 | this.registerSem.release(); | |
75 | this.sem_posted = true; | |
76 | } | |
77 | } | |
78 | ||
501f6777 CB |
79 | @Override |
80 | public void run() { | |
43e5396b DG |
81 | for (;;) { |
82 | if (this.quit) { | |
83 | break; | |
84 | } | |
85 | ||
87d64abb | 86 | /* Cleanup Agent state before trying to connect or reconnect. */ |
501f6777 | 87 | this.log.reset(); |
87d64abb | 88 | |
43e5396b DG |
89 | try { |
90 | ||
91 | /* | |
92 | * Connect to the session daemon before anything else. | |
93 | */ | |
94 | connectToSessiond(); | |
95 | ||
96 | /* | |
97 | * Register to the session daemon as the Java component of the | |
98 | * UST application. | |
99 | */ | |
100 | registerToSessiond(); | |
43e5396b | 101 | |
43e5396b DG |
102 | /* |
103 | * Block on socket receive and wait for command from the | |
104 | * session daemon. This will return if and only if there is a | |
105 | * fatal error or the socket closes. | |
106 | */ | |
107 | handleSessiondCmd(); | |
108 | } catch (UnknownHostException uhe) { | |
f1fa0535 | 109 | tryReleaseSem(); |
43e5396b DG |
110 | System.out.println(uhe); |
111 | } catch (IOException ioe) { | |
f1fa0535 | 112 | tryReleaseSem(); |
501f6777 CB |
113 | try { |
114 | Thread.sleep(3000); | |
115 | } catch (InterruptedException e) { | |
116 | e.printStackTrace(); | |
117 | } | |
43e5396b | 118 | } catch (Exception e) { |
f1fa0535 | 119 | tryReleaseSem(); |
43e5396b DG |
120 | e.printStackTrace(); |
121 | } | |
122 | } | |
123 | } | |
124 | ||
125 | public void destroy() { | |
126 | this.quit = true; | |
43e5396b DG |
127 | |
128 | try { | |
129 | if (this.sessiondSock != null) { | |
130 | this.sessiondSock.close(); | |
131 | } | |
132 | } catch (Exception e) { | |
133 | e.printStackTrace(); | |
134 | } | |
135 | } | |
136 | ||
137 | /* | |
138 | * Receive header data from the session daemon using the LTTng command | |
139 | * static buffer of the right size. | |
140 | */ | |
141 | private void recvHeader() throws Exception { | |
142 | int read_len; | |
57224e35 | 143 | byte data[] = new byte[LTTngSessiondCmd2_6.sessiond_hdr.SIZE]; |
43e5396b DG |
144 | |
145 | read_len = this.inFromSessiond.read(data, 0, data.length); | |
146 | if (read_len != data.length) { | |
147 | throw new IOException(); | |
148 | } | |
149 | this.headerCmd.populate(data); | |
150 | } | |
151 | ||
152 | /* | |
153 | * Receive payload from the session daemon. This MUST be done after a | |
154 | * recvHeader() so the header value of a command are known. | |
155 | * | |
156 | * The caller SHOULD use isPayload() before which returns true if a payload | |
157 | * is expected after the header. | |
158 | */ | |
159 | private byte[] recvPayload() throws Exception { | |
160 | byte payload[] = new byte[(int) this.headerCmd.data_size]; | |
161 | ||
162 | /* Failsafe check so we don't waste our time reading 0 bytes. */ | |
163 | if (payload.length == 0) { | |
164 | return null; | |
165 | } | |
166 | ||
167 | this.inFromSessiond.read(payload, 0, payload.length); | |
168 | return payload; | |
169 | } | |
170 | ||
171 | /* | |
172 | * Handle session command from the session daemon. | |
173 | */ | |
174 | private void handleSessiondCmd() throws Exception { | |
43e5396b DG |
175 | byte data[] = null; |
176 | ||
177 | while (true) { | |
178 | /* Get header from session daemon. */ | |
179 | recvHeader(); | |
180 | ||
181 | if (headerCmd.data_size > 0) { | |
182 | data = recvPayload(); | |
183 | } | |
184 | ||
185 | switch (headerCmd.cmd) { | |
f08bb871 DG |
186 | case CMD_REG_DONE: |
187 | { | |
d09aa6cd PP |
188 | /* |
189 | * Check command version: | |
190 | * | |
191 | * * 0: Connected to a non-fixed session daemon, | |
192 | * which could send multiple disable | |
193 | * event commands: do not decrement | |
194 | * reference count on disable event command | |
195 | * (original behaviour). | |
196 | * * >0: Connected to a fixed session daemon: | |
197 | * do decrement reference count on disable | |
198 | * event command. | |
199 | */ | |
200 | if (headerCmd.cmd_version > 0) { | |
201 | this.log.setEnableRefCountDecrement(true); | |
202 | } | |
203 | ||
f08bb871 DG |
204 | /* |
205 | * Release semaphore so meaning registration is done and we | |
206 | * can proceed to continue tracing. | |
207 | */ | |
f1fa0535 | 208 | tryReleaseSem(); |
9aabed2d DG |
209 | /* |
210 | * We don't send any reply to the registration done command. | |
211 | * This just marks the end of the initial session setup. | |
212 | */ | |
213 | continue; | |
f08bb871 | 214 | } |
43e5396b DG |
215 | case CMD_LIST: |
216 | { | |
501f6777 CB |
217 | LTTngSessiondCmd2_6.sessiond_list_logger listLoggerCmd = |
218 | new LTTngSessiondCmd2_6.sessiond_list_logger(); | |
219 | listLoggerCmd.execute(this.log); | |
43e5396b DG |
220 | data = listLoggerCmd.getBytes(); |
221 | break; | |
222 | } | |
223 | case CMD_ENABLE: | |
224 | { | |
501f6777 CB |
225 | LTTngSessiondCmd2_6.sessiond_enable_handler enableCmd = |
226 | new LTTngSessiondCmd2_6.sessiond_enable_handler(); | |
43e5396b | 227 | if (data == null) { |
501f6777 | 228 | enableCmd.code = LTTngSessiondCmd2_6.lttng_agent_ret_code.CODE_INVALID_CMD; |
43e5396b DG |
229 | break; |
230 | } | |
231 | enableCmd.populate(data); | |
501f6777 | 232 | enableCmd.execute(this.log); |
43e5396b DG |
233 | data = enableCmd.getBytes(); |
234 | break; | |
235 | } | |
236 | case CMD_DISABLE: | |
237 | { | |
501f6777 CB |
238 | LTTngSessiondCmd2_6.sessiond_disable_handler disableCmd = |
239 | new LTTngSessiondCmd2_6.sessiond_disable_handler(); | |
43e5396b | 240 | if (data == null) { |
501f6777 | 241 | disableCmd.code = LTTngSessiondCmd2_6.lttng_agent_ret_code.CODE_INVALID_CMD; |
43e5396b DG |
242 | break; |
243 | } | |
244 | disableCmd.populate(data); | |
501f6777 | 245 | disableCmd.execute(this.log); |
43e5396b DG |
246 | data = disableCmd.getBytes(); |
247 | break; | |
248 | } | |
249 | default: | |
250 | { | |
251 | data = new byte[4]; | |
252 | ByteBuffer buf = ByteBuffer.wrap(data); | |
253 | buf.order(ByteOrder.BIG_ENDIAN); | |
43e5396b DG |
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 | ||
57224e35 | 264 | private static String getHomePath() { |
364e7df4 MD |
265 | /* |
266 | * The environment variable LTTNG_HOME overrides HOME if | |
267 | * defined. | |
268 | */ | |
269 | String homePath = System.getenv("LTTNG_HOME"); | |
270 | ||
271 | if (homePath == null) { | |
272 | homePath = System.getProperty("user.home"); | |
273 | } | |
274 | return homePath; | |
f1fa0535 DG |
275 | } |
276 | ||
277 | /** | |
278 | * Read port number from file created by the session daemon. | |
279 | * | |
280 | * @return port value if found else 0. | |
281 | */ | |
57224e35 | 282 | private static int getPortFromFile(String path) throws IOException { |
f1fa0535 DG |
283 | int port; |
284 | BufferedReader br; | |
285 | ||
286 | try { | |
287 | br = new BufferedReader(new FileReader(path)); | |
288 | String line = br.readLine(); | |
289 | port = Integer.parseInt(line, 10); | |
290 | if (port < 0 || port > 65535) { | |
291 | /* Invalid value. Ignore. */ | |
292 | port = 0; | |
293 | } | |
294 | br.close(); | |
295 | } catch (FileNotFoundException e) { | |
296 | /* No port available. */ | |
297 | port = 0; | |
298 | } | |
299 | ||
300 | return port; | |
301 | } | |
302 | ||
43e5396b | 303 | private void connectToSessiond() throws Exception { |
f1fa0535 DG |
304 | int port; |
305 | ||
501f6777 | 306 | if (this.log.isRoot()) { |
f1fa0535 DG |
307 | port = getPortFromFile(rootPortFile); |
308 | if (port == 0) { | |
309 | /* No session daemon available. Stop and retry later. */ | |
310 | throw new IOException(); | |
311 | } | |
312 | } else { | |
313 | port = getPortFromFile(getHomePath() + userPortFile); | |
314 | if (port == 0) { | |
315 | /* No session daemon available. Stop and retry later. */ | |
316 | throw new IOException(); | |
317 | } | |
318 | } | |
319 | ||
57224e35 | 320 | this.sessiondSock = new Socket(sessiondHost, port); |
43e5396b DG |
321 | this.inFromSessiond = new DataInputStream( |
322 | sessiondSock.getInputStream()); | |
323 | this.outToSessiond = new DataOutputStream( | |
324 | sessiondSock.getOutputStream()); | |
325 | } | |
326 | ||
327 | private void registerToSessiond() throws Exception { | |
501f6777 | 328 | byte data[] = new byte[16]; |
43e5396b DG |
329 | ByteBuffer buf = ByteBuffer.wrap(data); |
330 | String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; | |
331 | ||
501f6777 | 332 | buf.putInt(this.agentDomain.value()); |
43e5396b | 333 | buf.putInt(Integer.parseInt(pid)); |
57224e35 AM |
334 | buf.putInt(protocolMajorVersion); |
335 | buf.putInt(protocolMinorVersion); | |
43e5396b DG |
336 | this.outToSessiond.write(data, 0, data.length); |
337 | this.outToSessiond.flush(); | |
338 | } | |
339 | } |