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