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