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