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" | |
7baf7871 | 34 | #include "thread.h" |
4d076222 | 35 | |
9d2bf9ed JG |
36 | struct thread_notifiers { |
37 | struct lttng_pipe *quit_pipe; | |
38 | sem_t ready; | |
39 | }; | |
40 | ||
f28f9e44 JG |
41 | static int agent_tracing_enabled = -1; |
42 | ||
4d076222 DG |
43 | /* |
44 | * Note that there is not port here. It's set after this URI is parsed so we | |
45 | * can let the user define a custom one. However, localhost is ALWAYS the | |
46 | * default listening address. | |
47 | */ | |
fa91dc52 MD |
48 | static const char *default_reg_uri = |
49 | "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS; | |
4d076222 | 50 | |
f20baf8e | 51 | /* |
022d91ba | 52 | * Update agent application using the given socket. This is done just after |
f20baf8e DG |
53 | * registration was successful. |
54 | * | |
55 | * This is a quite heavy call in terms of locking since the session list lock | |
56 | * AND session lock are acquired. | |
57 | */ | |
fefd409b | 58 | static void update_agent_app(struct agent_app *app) |
f20baf8e DG |
59 | { |
60 | struct ltt_session *session, *stmp; | |
61 | struct ltt_session_list *list; | |
62 | ||
63 | list = session_get_list(); | |
64 | assert(list); | |
65 | ||
66 | session_lock_list(); | |
67 | cds_list_for_each_entry_safe(session, stmp, &list->head, list) { | |
48a86f68 JG |
68 | if (!session_get(session)) { |
69 | continue; | |
70 | } | |
71 | ||
f20baf8e DG |
72 | session_lock(session); |
73 | if (session->ust_session) { | |
fefd409b DG |
74 | struct agent *agt; |
75 | ||
4da703ad | 76 | rcu_read_lock(); |
fefd409b DG |
77 | agt = trace_ust_find_agent(session->ust_session, app->domain); |
78 | if (agt) { | |
79 | agent_update(agt, app->sock->fd); | |
80 | } | |
4da703ad | 81 | rcu_read_unlock(); |
f20baf8e DG |
82 | } |
83 | session_unlock(session); | |
48a86f68 | 84 | session_put(session); |
f20baf8e DG |
85 | } |
86 | session_unlock_list(); | |
87 | } | |
88 | ||
4d076222 DG |
89 | /* |
90 | * Create and init socket from uri. | |
91 | */ | |
92 | static struct lttcomm_sock *init_tcp_socket(void) | |
93 | { | |
94 | int ret; | |
95 | struct lttng_uri *uri = NULL; | |
96 | struct lttcomm_sock *sock = NULL; | |
2288467f JG |
97 | unsigned int port; |
98 | bool bind_succeeded = false; | |
4d076222 DG |
99 | |
100 | /* | |
101 | * This should never fail since the URI is hardcoded and the port is set | |
102 | * before this thread is launched. | |
103 | */ | |
104 | ret = uri_parse(default_reg_uri, &uri); | |
105 | assert(ret); | |
2288467f JG |
106 | assert(config.agent_tcp_port.begin > 0); |
107 | uri->port = config.agent_tcp_port.begin; | |
4d076222 DG |
108 | |
109 | sock = lttcomm_alloc_sock_from_uri(uri); | |
110 | uri_free(uri); | |
111 | if (sock == NULL) { | |
022d91ba | 112 | ERR("[agent-thread] agent allocating TCP socket"); |
4d076222 DG |
113 | goto error; |
114 | } | |
115 | ||
116 | ret = lttcomm_create_sock(sock); | |
117 | if (ret < 0) { | |
118 | goto error; | |
119 | } | |
120 | ||
2288467f JG |
121 | for (port = config.agent_tcp_port.begin; |
122 | port <= config.agent_tcp_port.end; port++) { | |
123 | ret = lttcomm_sock_set_port(sock, (uint16_t) port); | |
124 | if (ret) { | |
125 | ERR("[agent-thread] Failed to set port %u on socket", | |
126 | port); | |
127 | goto error; | |
128 | } | |
129 | DBG3("[agent-thread] Trying to bind on port %u", port); | |
130 | ret = sock->ops->bind(sock); | |
131 | if (!ret) { | |
132 | bind_succeeded = true; | |
133 | break; | |
134 | } | |
135 | ||
136 | if (errno == EADDRINUSE) { | |
137 | DBG("Failed to bind to port %u since it is already in use", | |
138 | port); | |
139 | } else { | |
140 | PERROR("Failed to bind to port %u", port); | |
141 | goto error; | |
142 | } | |
143 | } | |
144 | ||
145 | if (!bind_succeeded) { | |
146 | if (config.agent_tcp_port.begin == config.agent_tcp_port.end) { | |
147 | WARN("Another process is already using the agent port %i. " | |
148 | "Agent support will be deactivated.", | |
149 | config.agent_tcp_port.begin); | |
150 | goto error; | |
151 | } else { | |
152 | WARN("All ports in the range [%i, %i] are already in use. " | |
153 | "Agent support will be deactivated.", | |
154 | config.agent_tcp_port.begin, | |
155 | config.agent_tcp_port.end); | |
156 | goto error; | |
157 | } | |
4d076222 DG |
158 | } |
159 | ||
160 | ret = sock->ops->listen(sock, -1); | |
161 | if (ret < 0) { | |
162 | goto error; | |
163 | } | |
164 | ||
022d91ba | 165 | DBG("[agent-thread] Listening on TCP port %u and socket %d", |
2288467f | 166 | port, sock->fd); |
4d076222 DG |
167 | |
168 | return sock; | |
169 | ||
170 | error: | |
171 | if (sock) { | |
172 | lttcomm_destroy_sock(sock); | |
173 | } | |
174 | return NULL; | |
175 | } | |
176 | ||
177 | /* | |
178 | * Close and destroy the given TCP socket. | |
179 | */ | |
180 | static void destroy_tcp_socket(struct lttcomm_sock *sock) | |
181 | { | |
2288467f JG |
182 | int ret; |
183 | uint16_t port; | |
184 | ||
4d076222 DG |
185 | assert(sock); |
186 | ||
2288467f JG |
187 | ret = lttcomm_sock_get_port(sock, &port); |
188 | if (ret) { | |
189 | ERR("[agent-thread] Failed to get port of agent TCP socket"); | |
190 | port = 0; | |
191 | } | |
192 | ||
193 | DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16, | |
194 | port); | |
4d076222 DG |
195 | |
196 | /* This will return gracefully if fd is invalid. */ | |
197 | sock->ops->close(sock); | |
198 | lttcomm_destroy_sock(sock); | |
199 | } | |
200 | ||
f20baf8e | 201 | /* |
022d91ba DG |
202 | * Handle a new agent registration using the reg socket. After that, a new |
203 | * agent application is added to the global hash table and attach to an UST app | |
1b500e7a | 204 | * object. If r_app is not NULL, the created app is set to the pointer. |
f20baf8e DG |
205 | * |
206 | * Return the new FD created upon accept() on success or else a negative errno | |
207 | * value. | |
208 | */ | |
1b500e7a | 209 | static int handle_registration(struct lttcomm_sock *reg_sock, |
022d91ba | 210 | struct agent_app **r_app) |
f20baf8e DG |
211 | { |
212 | int ret; | |
213 | pid_t pid; | |
9474416f | 214 | uint32_t major_version, minor_version; |
f20baf8e | 215 | ssize_t size; |
fefd409b | 216 | enum lttng_domain_type domain; |
022d91ba DG |
217 | struct agent_app *app; |
218 | struct agent_register_msg msg; | |
f20baf8e DG |
219 | struct lttcomm_sock *new_sock; |
220 | ||
221 | assert(reg_sock); | |
222 | ||
223 | new_sock = reg_sock->ops->accept(reg_sock); | |
224 | if (!new_sock) { | |
225 | ret = -ENOTCONN; | |
226 | goto error; | |
227 | } | |
228 | ||
229 | size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0); | |
230 | if (size < sizeof(msg)) { | |
79865500 | 231 | ret = -EINVAL; |
f20baf8e DG |
232 | goto error_socket; |
233 | } | |
fefd409b | 234 | domain = be32toh(msg.domain); |
f20baf8e | 235 | pid = be32toh(msg.pid); |
9474416f DG |
236 | major_version = be32toh(msg.major_version); |
237 | minor_version = be32toh(msg.minor_version); | |
238 | ||
239 | /* Test communication protocol version of the registring agent. */ | |
240 | if (major_version != AGENT_MAJOR_VERSION) { | |
241 | ret = -EINVAL; | |
242 | goto error_socket; | |
243 | } | |
244 | if (minor_version != AGENT_MINOR_VERSION) { | |
245 | ret = -EINVAL; | |
246 | goto error_socket; | |
247 | } | |
f20baf8e | 248 | |
fefd409b DG |
249 | DBG2("[agent-thread] New registration for pid %d domain %d on socket %d", |
250 | pid, domain, new_sock->fd); | |
f20baf8e | 251 | |
fefd409b | 252 | app = agent_create_app(pid, domain, new_sock); |
f20baf8e DG |
253 | if (!app) { |
254 | ret = -ENOMEM; | |
255 | goto error_socket; | |
256 | } | |
257 | ||
258 | /* | |
259 | * Add before assigning the socket value to the UST app so it can be found | |
260 | * concurrently. | |
261 | */ | |
022d91ba | 262 | agent_add_app(app); |
f20baf8e DG |
263 | |
264 | /* | |
022d91ba DG |
265 | * We don't need to attach the agent app to the app. If we ever do so, we |
266 | * should consider both registration order of agent before app and app | |
267 | * before agent. | |
f20baf8e | 268 | */ |
f20baf8e | 269 | |
1b500e7a DG |
270 | if (r_app) { |
271 | *r_app = app; | |
272 | } | |
273 | ||
f20baf8e DG |
274 | return new_sock->fd; |
275 | ||
276 | error_socket: | |
277 | new_sock->ops->close(new_sock); | |
278 | lttcomm_destroy_sock(new_sock); | |
279 | error: | |
280 | return ret; | |
281 | } | |
282 | ||
f28f9e44 JG |
283 | bool agent_tracing_is_enabled(void) |
284 | { | |
285 | int enabled; | |
286 | ||
287 | enabled = uatomic_read(&agent_tracing_enabled); | |
288 | assert(enabled != -1); | |
289 | return enabled == 1; | |
290 | } | |
291 | ||
2288467f JG |
292 | /* |
293 | * Write agent TCP port using the rundir. | |
294 | */ | |
295 | static int write_agent_port(uint16_t port) | |
296 | { | |
297 | return utils_create_pid_file((pid_t) port, | |
298 | config.agent_port_file_path.value); | |
299 | } | |
300 | ||
9d2bf9ed JG |
301 | static |
302 | void mark_thread_as_ready(struct thread_notifiers *notifiers) | |
303 | { | |
304 | DBG("Marking agent management thread as ready"); | |
305 | sem_post(¬ifiers->ready); | |
306 | } | |
307 | ||
308 | static | |
309 | void wait_until_thread_is_ready(struct thread_notifiers *notifiers) | |
310 | { | |
311 | DBG("Waiting for agent management thread to be ready"); | |
312 | sem_wait(¬ifiers->ready); | |
313 | DBG("Agent management thread is ready"); | |
314 | } | |
315 | ||
4d076222 DG |
316 | /* |
317 | * This thread manage application notify communication. | |
318 | */ | |
7baf7871 | 319 | static void *thread_agent_management(void *data) |
4d076222 DG |
320 | { |
321 | int i, ret, pollfd; | |
322 | uint32_t revents, nb_fd; | |
323 | struct lttng_poll_event events; | |
324 | struct lttcomm_sock *reg_sock; | |
9d2bf9ed JG |
325 | struct thread_notifiers *notifiers = data; |
326 | const int quit_pipe_read_fd = lttng_pipe_get_readfd( | |
327 | notifiers->quit_pipe); | |
4d076222 | 328 | |
022d91ba | 329 | DBG("[agent-thread] Manage agent application registration."); |
4d076222 DG |
330 | |
331 | rcu_register_thread(); | |
332 | rcu_thread_online(); | |
333 | ||
022d91ba DG |
334 | /* Agent initialization call MUST be called before starting the thread. */ |
335 | assert(agent_apps_ht_by_sock); | |
f20baf8e | 336 | |
7baf7871 JG |
337 | /* Create pollset with size 2, quit pipe and registration socket. */ |
338 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
4d076222 DG |
339 | if (ret < 0) { |
340 | goto error_poll_create; | |
341 | } | |
342 | ||
7baf7871 JG |
343 | ret = lttng_poll_add(&events, quit_pipe_read_fd, |
344 | LPOLLIN | LPOLLERR); | |
345 | if (ret < 0) { | |
346 | goto error_tcp_socket; | |
347 | } | |
348 | ||
4d076222 | 349 | reg_sock = init_tcp_socket(); |
2288467f JG |
350 | if (reg_sock) { |
351 | uint16_t port; | |
352 | ||
353 | assert(lttcomm_sock_get_port(reg_sock, &port) == 0); | |
354 | ||
355 | ret = write_agent_port(port); | |
356 | if (ret) { | |
357 | ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable"); | |
358 | /* Don't prevent the launch of the sessiond on error. */ | |
9d2bf9ed | 359 | mark_thread_as_ready(notifiers); |
2288467f JG |
360 | goto error; |
361 | } | |
362 | } else { | |
363 | /* Don't prevent the launch of the sessiond on error. */ | |
9d2bf9ed | 364 | mark_thread_as_ready(notifiers); |
2288467f JG |
365 | goto error_tcp_socket; |
366 | } | |
f28f9e44 JG |
367 | |
368 | /* | |
369 | * Signal that the agent thread is ready. The command thread | |
370 | * may start to query whether or not agent tracing is enabled. | |
371 | */ | |
2288467f | 372 | uatomic_set(&agent_tracing_enabled, 1); |
9d2bf9ed | 373 | mark_thread_as_ready(notifiers); |
4d076222 | 374 | |
427392b4 | 375 | /* Add TCP socket to poll set. */ |
4d076222 DG |
376 | ret = lttng_poll_add(&events, reg_sock->fd, |
377 | LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
378 | if (ret < 0) { | |
379 | goto error; | |
380 | } | |
381 | ||
382 | while (1) { | |
546f19b5 | 383 | DBG3("[agent-thread] Manage agent polling"); |
4d076222 DG |
384 | |
385 | /* Inifinite blocking call, waiting for transmission */ | |
386 | restart: | |
387 | ret = lttng_poll_wait(&events, -1); | |
7fa2082e MD |
388 | DBG3("[agent-thread] Manage agent return from poll on %d fds", |
389 | LTTNG_POLL_GETNB(&events)); | |
4d076222 DG |
390 | if (ret < 0) { |
391 | /* | |
392 | * Restart interrupted system call. | |
393 | */ | |
394 | if (errno == EINTR) { | |
395 | goto restart; | |
396 | } | |
397 | goto error; | |
398 | } | |
399 | nb_fd = ret; | |
022d91ba | 400 | DBG3("[agent-thread] %d fd ready", nb_fd); |
4d076222 DG |
401 | |
402 | for (i = 0; i < nb_fd; i++) { | |
403 | /* Fetch once the poll data */ | |
404 | revents = LTTNG_POLL_GETEV(&events, i); | |
405 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
406 | ||
407 | /* Thread quit pipe has been closed. Killing thread. */ | |
7baf7871 | 408 | if (pollfd == quit_pipe_read_fd) { |
4d076222 DG |
409 | goto exit; |
410 | } | |
411 | ||
03e43155 | 412 | if (revents & LPOLLIN) { |
f20baf8e | 413 | int new_fd; |
022d91ba | 414 | struct agent_app *app = NULL; |
f20baf8e | 415 | |
f20baf8e | 416 | assert(pollfd == reg_sock->fd); |
1b500e7a | 417 | new_fd = handle_registration(reg_sock, &app); |
f20baf8e | 418 | if (new_fd < 0) { |
f20baf8e DG |
419 | continue; |
420 | } | |
1b500e7a DG |
421 | /* Should not have a NULL app on success. */ |
422 | assert(app); | |
f20baf8e | 423 | |
03e43155 MD |
424 | /* |
425 | * Since this is a command socket (write then read), | |
426 | * only add poll error event to only detect shutdown. | |
427 | */ | |
f20baf8e DG |
428 | ret = lttng_poll_add(&events, new_fd, |
429 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
430 | if (ret < 0) { | |
6a4e4039 | 431 | agent_destroy_app_by_sock(new_fd); |
f20baf8e DG |
432 | continue; |
433 | } | |
434 | ||
435 | /* Update newly registered app. */ | |
fefd409b | 436 | update_agent_app(app); |
1b500e7a DG |
437 | |
438 | /* On failure, the poll will detect it and clean it up. */ | |
03e43155 MD |
439 | ret = agent_send_registration_done(app); |
440 | if (ret < 0) { | |
441 | /* Removing from the poll set */ | |
442 | ret = lttng_poll_del(&events, new_fd); | |
443 | if (ret < 0) { | |
444 | goto error; | |
445 | } | |
446 | agent_destroy_app_by_sock(new_fd); | |
447 | continue; | |
448 | } | |
449 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
450 | /* Removing from the poll set */ | |
451 | ret = lttng_poll_del(&events, pollfd); | |
452 | if (ret < 0) { | |
453 | goto error; | |
454 | } | |
455 | agent_destroy_app_by_sock(pollfd); | |
4d076222 | 456 | } else { |
03e43155 MD |
457 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
458 | goto error; | |
4d076222 DG |
459 | } |
460 | } | |
461 | } | |
462 | ||
463 | exit: | |
f20baf8e DG |
464 | /* Whatever happens, try to delete it and exit. */ |
465 | (void) lttng_poll_del(&events, reg_sock->fd); | |
4d076222 DG |
466 | error: |
467 | destroy_tcp_socket(reg_sock); | |
468 | error_tcp_socket: | |
469 | lttng_poll_clean(&events); | |
470 | error_poll_create: | |
2288467f | 471 | uatomic_set(&agent_tracing_enabled, 0); |
7baf7871 | 472 | DBG("[agent-thread] Cleaning up and stopping."); |
4d076222 DG |
473 | rcu_thread_offline(); |
474 | rcu_unregister_thread(); | |
475 | return NULL; | |
476 | } | |
7baf7871 JG |
477 | |
478 | static bool shutdown_agent_management_thread(void *data) | |
479 | { | |
9d2bf9ed JG |
480 | struct thread_notifiers *notifiers = data; |
481 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); | |
7baf7871 JG |
482 | |
483 | return notify_thread_pipe(write_fd) == 1; | |
484 | } | |
485 | ||
45f6525f JG |
486 | static void cleanup_agent_management_thread(void *data) |
487 | { | |
9d2bf9ed | 488 | struct thread_notifiers *notifiers = data; |
45f6525f | 489 | |
9d2bf9ed JG |
490 | lttng_pipe_destroy(notifiers->quit_pipe); |
491 | sem_destroy(¬ifiers->ready); | |
492 | free(notifiers); | |
45f6525f JG |
493 | } |
494 | ||
495 | bool launch_agent_management_thread(void) | |
7baf7871 | 496 | { |
9d2bf9ed | 497 | struct thread_notifiers *notifiers; |
7baf7871 JG |
498 | struct lttng_thread *thread; |
499 | ||
9d2bf9ed JG |
500 | notifiers = zmalloc(sizeof(*notifiers)); |
501 | if (!notifiers) { | |
a7f04300 | 502 | goto error_alloc; |
9d2bf9ed JG |
503 | } |
504 | ||
505 | sem_init(¬ifiers->ready, 0, 0); | |
506 | notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC); | |
507 | if (!notifiers->quit_pipe) { | |
7baf7871 JG |
508 | goto error; |
509 | } | |
510 | thread = lttng_thread_create("Agent management", | |
511 | thread_agent_management, | |
512 | shutdown_agent_management_thread, | |
45f6525f | 513 | cleanup_agent_management_thread, |
9d2bf9ed | 514 | notifiers); |
7baf7871 JG |
515 | if (!thread) { |
516 | goto error; | |
517 | } | |
9d2bf9ed | 518 | wait_until_thread_is_ready(notifiers); |
7baf7871 JG |
519 | lttng_thread_put(thread); |
520 | return true; | |
521 | error: | |
9d2bf9ed | 522 | cleanup_agent_management_thread(notifiers); |
a7f04300 | 523 | error_alloc: |
7baf7871 JG |
524 | return false; |
525 | } |