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; |
bc7de6d9 | 22 | import java.io.DataOutputStream; |
f1fa0535 | 23 | import java.io.FileNotFoundException; |
bc7de6d9 AM |
24 | import java.io.FileReader; |
25 | import java.io.IOException; | |
43e5396b | 26 | import java.lang.management.ManagementFactory; |
bc7de6d9 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 | |
08284556 AM |
35 | private static final String SESSION_HOST = "127.0.0.1"; |
36 | private static final String ROOT_PORT_FILE = "/var/run/lttng/agent.port"; | |
37 | private static final String USER_PORT_FILE = "/.lttng/agent.port"; | |
38 | ||
39 | private static Integer protocolMajorVersion = 1; | |
40 | private static Integer protocolMinorVersion = 0; | |
41 | ||
43e5396b | 42 | /* Command header from the session deamon. */ |
501f6777 CB |
43 | private LTTngSessiondCmd2_6.sessiond_hdr headerCmd = |
44 | new LTTngSessiondCmd2_6.sessiond_hdr(); | |
43e5396b | 45 | |
43e5396b | 46 | private Socket sessiondSock; |
501f6777 | 47 | private volatile boolean quit = false; |
43e5396b DG |
48 | |
49 | private DataInputStream inFromSessiond; | |
50 | private DataOutputStream outToSessiond; | |
51 | ||
501f6777 | 52 | private LogFramework log; |
43e5396b DG |
53 | |
54 | private Semaphore registerSem; | |
55 | ||
501f6777 CB |
56 | |
57 | private LTTngAgent.Domain agentDomain; | |
f1fa0535 | 58 | |
08284556 AM |
59 | /* Indicate if we've already released the semaphore. */ |
60 | private boolean semPosted = false; | |
f1fa0535 | 61 | |
501f6777 CB |
62 | public LTTngTCPSessiondClient(LTTngAgent.Domain domain, LogFramework log, Semaphore sem) { |
63 | this.agentDomain = domain; | |
64 | this.log = log; | |
43e5396b | 65 | this.registerSem = sem; |
43e5396b DG |
66 | } |
67 | ||
f1fa0535 DG |
68 | /* |
69 | * Try to release the registerSem if it's not already done. | |
70 | */ | |
08284556 | 71 | private void tryReleaseSem() { |
f1fa0535 | 72 | /* Release semaphore so we unblock the agent. */ |
08284556 | 73 | if (!this.semPosted) { |
f1fa0535 | 74 | this.registerSem.release(); |
08284556 | 75 | this.semPosted = true; |
f1fa0535 DG |
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 { | |
bc7de6d9 | 142 | byte data[] = new byte[LTTngSessiondCmd2_6.sessiond_hdr.SIZE]; |
43e5396b | 143 | |
08284556 AM |
144 | int readLen = this.inFromSessiond.read(data, 0, data.length); |
145 | if (readLen != data.length) { | |
43e5396b DG |
146 | throw new IOException(); |
147 | } | |
148 | this.headerCmd.populate(data); | |
149 | } | |
150 | ||
151 | /* | |
152 | * Receive payload from the session daemon. This MUST be done after a | |
153 | * recvHeader() so the header value of a command are known. | |
154 | * | |
155 | * The caller SHOULD use isPayload() before which returns true if a payload | |
156 | * is expected after the header. | |
157 | */ | |
158 | private byte[] recvPayload() throws Exception { | |
08284556 | 159 | byte payload[] = new byte[(int) this.headerCmd.dataSize]; |
43e5396b DG |
160 | |
161 | /* Failsafe check so we don't waste our time reading 0 bytes. */ | |
162 | if (payload.length == 0) { | |
163 | return null; | |
164 | } | |
165 | ||
166 | this.inFromSessiond.read(payload, 0, payload.length); | |
167 | return payload; | |
168 | } | |
169 | ||
170 | /* | |
171 | * Handle session command from the session daemon. | |
172 | */ | |
173 | private void handleSessiondCmd() throws Exception { | |
43e5396b DG |
174 | byte data[] = null; |
175 | ||
176 | while (true) { | |
177 | /* Get header from session daemon. */ | |
178 | recvHeader(); | |
179 | ||
08284556 | 180 | if (headerCmd.dataSize > 0) { |
43e5396b DG |
181 | data = recvPayload(); |
182 | } | |
183 | ||
184 | switch (headerCmd.cmd) { | |
f08bb871 DG |
185 | case CMD_REG_DONE: |
186 | { | |
187 | /* | |
188 | * Release semaphore so meaning registration is done and we | |
189 | * can proceed to continue tracing. | |
190 | */ | |
f1fa0535 | 191 | tryReleaseSem(); |
9aabed2d DG |
192 | /* |
193 | * We don't send any reply to the registration done command. | |
194 | * This just marks the end of the initial session setup. | |
195 | */ | |
196 | continue; | |
f08bb871 | 197 | } |
43e5396b DG |
198 | case CMD_LIST: |
199 | { | |
501f6777 CB |
200 | LTTngSessiondCmd2_6.sessiond_list_logger listLoggerCmd = |
201 | new LTTngSessiondCmd2_6.sessiond_list_logger(); | |
202 | listLoggerCmd.execute(this.log); | |
43e5396b DG |
203 | data = listLoggerCmd.getBytes(); |
204 | break; | |
205 | } | |
206 | case CMD_ENABLE: | |
207 | { | |
501f6777 CB |
208 | LTTngSessiondCmd2_6.sessiond_enable_handler enableCmd = |
209 | new LTTngSessiondCmd2_6.sessiond_enable_handler(); | |
43e5396b | 210 | if (data == null) { |
501f6777 | 211 | enableCmd.code = LTTngSessiondCmd2_6.lttng_agent_ret_code.CODE_INVALID_CMD; |
43e5396b DG |
212 | break; |
213 | } | |
214 | enableCmd.populate(data); | |
501f6777 | 215 | enableCmd.execute(this.log); |
43e5396b DG |
216 | data = enableCmd.getBytes(); |
217 | break; | |
218 | } | |
219 | case CMD_DISABLE: | |
220 | { | |
501f6777 CB |
221 | LTTngSessiondCmd2_6.sessiond_disable_handler disableCmd = |
222 | new LTTngSessiondCmd2_6.sessiond_disable_handler(); | |
43e5396b | 223 | if (data == null) { |
501f6777 | 224 | disableCmd.code = LTTngSessiondCmd2_6.lttng_agent_ret_code.CODE_INVALID_CMD; |
43e5396b DG |
225 | break; |
226 | } | |
227 | disableCmd.populate(data); | |
501f6777 | 228 | disableCmd.execute(this.log); |
43e5396b DG |
229 | data = disableCmd.getBytes(); |
230 | break; | |
231 | } | |
232 | default: | |
233 | { | |
234 | data = new byte[4]; | |
235 | ByteBuffer buf = ByteBuffer.wrap(data); | |
236 | buf.order(ByteOrder.BIG_ENDIAN); | |
43e5396b DG |
237 | break; |
238 | } | |
239 | } | |
240 | ||
241 | /* Send payload to session daemon. */ | |
242 | this.outToSessiond.write(data, 0, data.length); | |
243 | this.outToSessiond.flush(); | |
244 | } | |
245 | } | |
246 | ||
bc7de6d9 | 247 | private static String getHomePath() { |
f1fa0535 DG |
248 | return System.getProperty("user.home"); |
249 | } | |
250 | ||
251 | /** | |
252 | * Read port number from file created by the session daemon. | |
253 | * | |
254 | * @return port value if found else 0. | |
255 | */ | |
bc7de6d9 | 256 | private static int getPortFromFile(String path) throws IOException { |
f1fa0535 DG |
257 | int port; |
258 | BufferedReader br; | |
259 | ||
260 | try { | |
261 | br = new BufferedReader(new FileReader(path)); | |
262 | String line = br.readLine(); | |
263 | port = Integer.parseInt(line, 10); | |
264 | if (port < 0 || port > 65535) { | |
265 | /* Invalid value. Ignore. */ | |
266 | port = 0; | |
267 | } | |
268 | br.close(); | |
269 | } catch (FileNotFoundException e) { | |
270 | /* No port available. */ | |
271 | port = 0; | |
272 | } | |
273 | ||
274 | return port; | |
275 | } | |
276 | ||
43e5396b | 277 | private void connectToSessiond() throws Exception { |
f1fa0535 DG |
278 | int port; |
279 | ||
501f6777 | 280 | if (this.log.isRoot()) { |
08284556 | 281 | port = getPortFromFile(ROOT_PORT_FILE); |
f1fa0535 DG |
282 | if (port == 0) { |
283 | /* No session daemon available. Stop and retry later. */ | |
284 | throw new IOException(); | |
285 | } | |
286 | } else { | |
08284556 | 287 | port = getPortFromFile(getHomePath() + USER_PORT_FILE); |
f1fa0535 DG |
288 | if (port == 0) { |
289 | /* No session daemon available. Stop and retry later. */ | |
290 | throw new IOException(); | |
291 | } | |
292 | } | |
293 | ||
08284556 AM |
294 | this.sessiondSock = new Socket(SESSION_HOST, port); |
295 | this.inFromSessiond = new DataInputStream(sessiondSock.getInputStream()); | |
296 | this.outToSessiond = new DataOutputStream(sessiondSock.getOutputStream()); | |
43e5396b DG |
297 | } |
298 | ||
299 | private void registerToSessiond() throws Exception { | |
501f6777 | 300 | byte data[] = new byte[16]; |
43e5396b DG |
301 | ByteBuffer buf = ByteBuffer.wrap(data); |
302 | String pid = ManagementFactory.getRuntimeMXBean().getName().split("@")[0]; | |
303 | ||
501f6777 | 304 | buf.putInt(this.agentDomain.value()); |
43e5396b | 305 | buf.putInt(Integer.parseInt(pid)); |
bc7de6d9 AM |
306 | buf.putInt(protocolMajorVersion); |
307 | buf.putInt(protocolMinorVersion); | |
43e5396b DG |
308 | this.outToSessiond.write(data, 0, data.length); |
309 | this.outToSessiond.flush(); | |
310 | } | |
311 | } |