Commit | Line | Data |
---|---|---|
4d076222 DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program 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 General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
4d076222 DG |
19 | #include <assert.h> |
20 | ||
21 | #include <common/common.h> | |
22 | #include <common/sessiond-comm/sessiond-comm.h> | |
23 | #include <common/uri.h> | |
24 | #include <common/utils.h> | |
25 | ||
f263b7fd JD |
26 | #include <common/compat/endian.h> |
27 | ||
4d076222 | 28 | #include "fd-limit.h" |
022d91ba | 29 | #include "agent-thread.h" |
6a4e4039 | 30 | #include "agent.h" |
4d076222 | 31 | #include "lttng-sessiond.h" |
f20baf8e DG |
32 | #include "session.h" |
33 | #include "utils.h" | |
4d076222 | 34 | |
f28f9e44 JG |
35 | static int agent_tracing_enabled = -1; |
36 | ||
4d076222 DG |
37 | /* |
38 | * Note that there is not port here. It's set after this URI is parsed so we | |
39 | * can let the user define a custom one. However, localhost is ALWAYS the | |
40 | * default listening address. | |
41 | */ | |
fa91dc52 MD |
42 | static const char *default_reg_uri = |
43 | "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS; | |
4d076222 | 44 | |
f20baf8e | 45 | /* |
022d91ba | 46 | * Update agent application using the given socket. This is done just after |
f20baf8e DG |
47 | * registration was successful. |
48 | * | |
49 | * This is a quite heavy call in terms of locking since the session list lock | |
50 | * AND session lock are acquired. | |
51 | */ | |
fefd409b | 52 | static void update_agent_app(struct agent_app *app) |
f20baf8e DG |
53 | { |
54 | struct ltt_session *session, *stmp; | |
55 | struct ltt_session_list *list; | |
56 | ||
57 | list = session_get_list(); | |
58 | assert(list); | |
59 | ||
60 | session_lock_list(); | |
61 | cds_list_for_each_entry_safe(session, stmp, &list->head, list) { | |
62 | session_lock(session); | |
63 | if (session->ust_session) { | |
fefd409b DG |
64 | struct agent *agt; |
65 | ||
4da703ad | 66 | rcu_read_lock(); |
fefd409b DG |
67 | agt = trace_ust_find_agent(session->ust_session, app->domain); |
68 | if (agt) { | |
69 | agent_update(agt, app->sock->fd); | |
70 | } | |
4da703ad | 71 | rcu_read_unlock(); |
f20baf8e DG |
72 | } |
73 | session_unlock(session); | |
74 | } | |
75 | session_unlock_list(); | |
76 | } | |
77 | ||
4d076222 DG |
78 | /* |
79 | * Create and init socket from uri. | |
80 | */ | |
81 | static struct lttcomm_sock *init_tcp_socket(void) | |
82 | { | |
83 | int ret; | |
84 | struct lttng_uri *uri = NULL; | |
85 | struct lttcomm_sock *sock = NULL; | |
86 | ||
87 | /* | |
88 | * This should never fail since the URI is hardcoded and the port is set | |
89 | * before this thread is launched. | |
90 | */ | |
91 | ret = uri_parse(default_reg_uri, &uri); | |
92 | assert(ret); | |
e6142f2e JG |
93 | assert(config.agent_tcp_port); |
94 | uri->port = config.agent_tcp_port; | |
4d076222 DG |
95 | |
96 | sock = lttcomm_alloc_sock_from_uri(uri); | |
97 | uri_free(uri); | |
98 | if (sock == NULL) { | |
022d91ba | 99 | ERR("[agent-thread] agent allocating TCP socket"); |
4d076222 DG |
100 | goto error; |
101 | } | |
102 | ||
103 | ret = lttcomm_create_sock(sock); | |
104 | if (ret < 0) { | |
105 | goto error; | |
106 | } | |
107 | ||
108 | ret = sock->ops->bind(sock); | |
109 | if (ret < 0) { | |
022d91ba | 110 | WARN("Another session daemon is using this agent port. Agent support " |
5368d366 | 111 | "will be deactivated to prevent interfering with the tracing."); |
4d076222 DG |
112 | goto error; |
113 | } | |
114 | ||
115 | ret = sock->ops->listen(sock, -1); | |
116 | if (ret < 0) { | |
117 | goto error; | |
118 | } | |
119 | ||
022d91ba | 120 | DBG("[agent-thread] Listening on TCP port %u and socket %d", |
e6142f2e | 121 | config.agent_tcp_port, sock->fd); |
4d076222 DG |
122 | |
123 | return sock; | |
124 | ||
125 | error: | |
126 | if (sock) { | |
127 | lttcomm_destroy_sock(sock); | |
128 | } | |
129 | return NULL; | |
130 | } | |
131 | ||
132 | /* | |
133 | * Close and destroy the given TCP socket. | |
134 | */ | |
135 | static void destroy_tcp_socket(struct lttcomm_sock *sock) | |
136 | { | |
137 | assert(sock); | |
138 | ||
e6142f2e | 139 | DBG3("[agent-thread] Destroy TCP socket on port %u", config.agent_tcp_port); |
4d076222 DG |
140 | |
141 | /* This will return gracefully if fd is invalid. */ | |
142 | sock->ops->close(sock); | |
143 | lttcomm_destroy_sock(sock); | |
144 | } | |
145 | ||
f20baf8e | 146 | /* |
022d91ba DG |
147 | * Handle a new agent registration using the reg socket. After that, a new |
148 | * agent application is added to the global hash table and attach to an UST app | |
1b500e7a | 149 | * object. If r_app is not NULL, the created app is set to the pointer. |
f20baf8e DG |
150 | * |
151 | * Return the new FD created upon accept() on success or else a negative errno | |
152 | * value. | |
153 | */ | |
1b500e7a | 154 | static int handle_registration(struct lttcomm_sock *reg_sock, |
022d91ba | 155 | struct agent_app **r_app) |
f20baf8e DG |
156 | { |
157 | int ret; | |
158 | pid_t pid; | |
9474416f | 159 | uint32_t major_version, minor_version; |
f20baf8e | 160 | ssize_t size; |
fefd409b | 161 | enum lttng_domain_type domain; |
022d91ba DG |
162 | struct agent_app *app; |
163 | struct agent_register_msg msg; | |
f20baf8e DG |
164 | struct lttcomm_sock *new_sock; |
165 | ||
166 | assert(reg_sock); | |
167 | ||
168 | new_sock = reg_sock->ops->accept(reg_sock); | |
169 | if (!new_sock) { | |
170 | ret = -ENOTCONN; | |
171 | goto error; | |
172 | } | |
173 | ||
174 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); | |
175 | if (size < sizeof(msg)) { | |
79865500 | 176 | ret = -EINVAL; |
f20baf8e DG |
177 | goto error_socket; |
178 | } | |
fefd409b | 179 | domain = be32toh(msg.domain); |
f20baf8e | 180 | pid = be32toh(msg.pid); |
9474416f DG |
181 | major_version = be32toh(msg.major_version); |
182 | minor_version = be32toh(msg.minor_version); | |
183 | ||
184 | /* Test communication protocol version of the registring agent. */ | |
185 | if (major_version != AGENT_MAJOR_VERSION) { | |
186 | ret = -EINVAL; | |
187 | goto error_socket; | |
188 | } | |
189 | if (minor_version != AGENT_MINOR_VERSION) { | |
190 | ret = -EINVAL; | |
191 | goto error_socket; | |
192 | } | |
f20baf8e | 193 | |
fefd409b DG |
194 | DBG2("[agent-thread] New registration for pid %d domain %d on socket %d", |
195 | pid, domain, new_sock->fd); | |
f20baf8e | 196 | |
fefd409b | 197 | app = agent_create_app(pid, domain, new_sock); |
f20baf8e DG |
198 | if (!app) { |
199 | ret = -ENOMEM; | |
200 | goto error_socket; | |
201 | } | |
202 | ||
203 | /* | |
204 | * Add before assigning the socket value to the UST app so it can be found | |
205 | * concurrently. | |
206 | */ | |
022d91ba | 207 | agent_add_app(app); |
f20baf8e DG |
208 | |
209 | /* | |
022d91ba DG |
210 | * We don't need to attach the agent app to the app. If we ever do so, we |
211 | * should consider both registration order of agent before app and app | |
212 | * before agent. | |
f20baf8e | 213 | */ |
f20baf8e | 214 | |
1b500e7a DG |
215 | if (r_app) { |
216 | *r_app = app; | |
217 | } | |
218 | ||
f20baf8e DG |
219 | return new_sock->fd; |
220 | ||
221 | error_socket: | |
222 | new_sock->ops->close(new_sock); | |
223 | lttcomm_destroy_sock(new_sock); | |
224 | error: | |
225 | return ret; | |
226 | } | |
227 | ||
f28f9e44 JG |
228 | bool agent_tracing_is_enabled(void) |
229 | { | |
230 | int enabled; | |
231 | ||
232 | enabled = uatomic_read(&agent_tracing_enabled); | |
233 | assert(enabled != -1); | |
234 | return enabled == 1; | |
235 | } | |
236 | ||
4d076222 DG |
237 | /* |
238 | * This thread manage application notify communication. | |
239 | */ | |
022d91ba | 240 | void *agent_thread_manage_registration(void *data) |
4d076222 DG |
241 | { |
242 | int i, ret, pollfd; | |
243 | uint32_t revents, nb_fd; | |
244 | struct lttng_poll_event events; | |
245 | struct lttcomm_sock *reg_sock; | |
246 | ||
022d91ba | 247 | DBG("[agent-thread] Manage agent application registration."); |
4d076222 DG |
248 | |
249 | rcu_register_thread(); | |
250 | rcu_thread_online(); | |
251 | ||
022d91ba DG |
252 | /* Agent initialization call MUST be called before starting the thread. */ |
253 | assert(agent_apps_ht_by_sock); | |
f20baf8e | 254 | |
4d076222 DG |
255 | /* Create pollset with size 2, quit pipe and socket. */ |
256 | ret = sessiond_set_thread_pollset(&events, 2); | |
257 | if (ret < 0) { | |
258 | goto error_poll_create; | |
259 | } | |
260 | ||
261 | reg_sock = init_tcp_socket(); | |
f28f9e44 JG |
262 | uatomic_set(&agent_tracing_enabled, !!reg_sock); |
263 | ||
264 | /* | |
265 | * Signal that the agent thread is ready. The command thread | |
266 | * may start to query whether or not agent tracing is enabled. | |
267 | */ | |
7eac7803 | 268 | sessiond_notify_ready(); |
4d076222 DG |
269 | if (!reg_sock) { |
270 | goto error_tcp_socket; | |
271 | } | |
272 | ||
427392b4 | 273 | /* Add TCP socket to poll set. */ |
4d076222 DG |
274 | ret = lttng_poll_add(&events, reg_sock->fd, |
275 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
276 | if (ret < 0) { | |
277 | goto error; | |
278 | } | |
279 | ||
280 | while (1) { | |
546f19b5 | 281 | DBG3("[agent-thread] Manage agent polling"); |
4d076222 DG |
282 | |
283 | /* Inifinite blocking call, waiting for transmission */ | |
284 | restart: | |
285 | ret = lttng_poll_wait(&events, -1); | |
7fa2082e MD |
286 | DBG3("[agent-thread] Manage agent return from poll on %d fds", |
287 | LTTNG_POLL_GETNB(&events)); | |
4d076222 DG |
288 | if (ret < 0) { |
289 | /* | |
290 | * Restart interrupted system call. | |
291 | */ | |
292 | if (errno == EINTR) { | |
293 | goto restart; | |
294 | } | |
295 | goto error; | |
296 | } | |
297 | nb_fd = ret; | |
022d91ba | 298 | DBG3("[agent-thread] %d fd ready", nb_fd); |
4d076222 DG |
299 | |
300 | for (i = 0; i < nb_fd; i++) { | |
301 | /* Fetch once the poll data */ | |
302 | revents = LTTNG_POLL_GETEV(&events, i); | |
303 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
304 | ||
fd20dac9 MD |
305 | if (!revents) { |
306 | /* No activity for this FD (poll implementation). */ | |
307 | continue; | |
308 | } | |
309 | ||
4d076222 DG |
310 | /* Thread quit pipe has been closed. Killing thread. */ |
311 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); | |
312 | if (ret) { | |
313 | goto exit; | |
314 | } | |
315 | ||
03e43155 | 316 | if (revents & LPOLLIN) { |
f20baf8e | 317 | int new_fd; |
022d91ba | 318 | struct agent_app *app = NULL; |
f20baf8e | 319 | |
f20baf8e | 320 | assert(pollfd == reg_sock->fd); |
1b500e7a | 321 | new_fd = handle_registration(reg_sock, &app); |
f20baf8e | 322 | if (new_fd < 0) { |
f20baf8e DG |
323 | continue; |
324 | } | |
1b500e7a DG |
325 | /* Should not have a NULL app on success. */ |
326 | assert(app); | |
f20baf8e | 327 | |
03e43155 MD |
328 | /* |
329 | * Since this is a command socket (write then read), | |
330 | * only add poll error event to only detect shutdown. | |
331 | */ | |
f20baf8e DG |
332 | ret = lttng_poll_add(&events, new_fd, |
333 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
334 | if (ret < 0) { | |
6a4e4039 | 335 | agent_destroy_app_by_sock(new_fd); |
f20baf8e DG |
336 | continue; |
337 | } | |
338 | ||
339 | /* Update newly registered app. */ | |
fefd409b | 340 | update_agent_app(app); |
1b500e7a DG |
341 | |
342 | /* On failure, the poll will detect it and clean it up. */ | |
03e43155 MD |
343 | ret = agent_send_registration_done(app); |
344 | if (ret < 0) { | |
345 | /* Removing from the poll set */ | |
346 | ret = lttng_poll_del(&events, new_fd); | |
347 | if (ret < 0) { | |
348 | goto error; | |
349 | } | |
350 | agent_destroy_app_by_sock(new_fd); | |
351 | continue; | |
352 | } | |
353 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
354 | /* Removing from the poll set */ | |
355 | ret = lttng_poll_del(&events, pollfd); | |
356 | if (ret < 0) { | |
357 | goto error; | |
358 | } | |
359 | agent_destroy_app_by_sock(pollfd); | |
4d076222 | 360 | } else { |
03e43155 MD |
361 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
362 | goto error; | |
4d076222 DG |
363 | } |
364 | } | |
365 | } | |
366 | ||
367 | exit: | |
f28f9e44 | 368 | uatomic_set(&agent_tracing_enabled, 0); |
f20baf8e DG |
369 | /* Whatever happens, try to delete it and exit. */ |
370 | (void) lttng_poll_del(&events, reg_sock->fd); | |
4d076222 DG |
371 | error: |
372 | destroy_tcp_socket(reg_sock); | |
373 | error_tcp_socket: | |
374 | lttng_poll_clean(&events); | |
375 | error_poll_create: | |
022d91ba | 376 | DBG("[agent-thread] is cleaning up and stopping."); |
4d076222 DG |
377 | |
378 | rcu_thread_offline(); | |
379 | rcu_unregister_thread(); | |
380 | return NULL; | |
381 | } |