SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[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;
2463b787
JR
62 struct agent *trigger_agent;
63 struct lttng_ht_iter iter;
f20baf8e
DG
64
65 list = session_get_list();
66 assert(list);
67
f20baf8e 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
4da703ad 77 rcu_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 }
4da703ad 82 rcu_read_unlock();
f20baf8e
DG
83 }
84 session_unlock(session);
e32d7f27 85 session_put(session);
f20baf8e 86 }
2463b787
JR
87
88 /* Do we need more locking here? maybe against trigger add? */
89 rcu_read_lock();
90 cds_lfht_for_each_entry (trigger_agents_ht_by_domain->ht, &iter.iter,
91 trigger_agent, node.node) {
92 agent_update(trigger_agent, app);
93 }
94 rcu_read_unlock();
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
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
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/*
733c9165 245 * Handle a new agent connection on the registration socket.
f20baf8e 246 *
733c9165
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 */
733c9165
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;
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
262 assert(reg_sock);
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",
276 sizeof(msg), size);
277 } else {
278 DBG("Failed to register new agent application: connection closed");
279 }
79865500 280 ret = -EINVAL;
733c9165 281 goto error_close_socket;
9474416f 282 }
f20baf8e 283
733c9165
JG
284 agent_version = (struct agent_protocol_version) {
285 be32toh(msg.major_version),
286 be32toh(msg.minor_version),
287 };
f20baf8e 288
733c9165
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
733c9165
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
733c9165
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
733c9165
JG
304 *agent_app_socket = new_sock;
305 new_sock = NULL;
306 ret = 0;
307 goto end;
f20baf8e 308
733c9165 309error_close_socket:
f20baf8e
DG
310 new_sock->ops->close(new_sock);
311 lttcomm_destroy_sock(new_sock);
733c9165 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
c78d8e86
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 */
8a7e4590 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;
c78d8e86
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
8a7e4590
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
8a7e4590
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. */
c78d8e86 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. */
c78d8e86 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);
c78d8e86 406 mark_thread_as_ready(notifiers);
4d076222 407
733c9165 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. */
8a7e4590 441 if (pollfd == quit_pipe_read_fd) {
4d076222
DG
442 goto exit;
443 }
444
733c9165 445 /* Activity on the registration socket. */
03e43155 446 if (revents & LPOLLIN) {
733c9165
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);
733c9165
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 /*
733c9165
JG
462 * new_app_socket's ownership has been
463 * transferred to the new agent app.
03e43155 464 */
733c9165
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) {
733c9165 484 agent_destroy_app(new_app);
f20baf8e
DG
485 continue;
486 }
487
733c9165
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
733c9165 501 ret = agent_send_registration_done(new_app);
03e43155 502 if (ret < 0) {
733c9165
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) {
733c9165 508 session_unlock_list();
03e43155
MD
509 goto error;
510 }
03e43155
MD
511 continue;
512 }
733c9165
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);
8a7e4590 541 DBG("[agent-thread] Cleaning up and stopping.");
4d076222
DG
542 rcu_thread_offline();
543 rcu_unregister_thread();
544 return NULL;
545}
8a7e4590
JG
546
547static bool shutdown_agent_management_thread(void *data)
548{
c78d8e86
JG
549 struct thread_notifiers *notifiers = data;
550 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
8a7e4590
JG
551
552 return notify_thread_pipe(write_fd) == 1;
553}
554
5b093681
JG
555static void cleanup_agent_management_thread(void *data)
556{
c78d8e86 557 struct thread_notifiers *notifiers = data;
5b093681 558
c78d8e86
JG
559 lttng_pipe_destroy(notifiers->quit_pipe);
560 sem_destroy(&notifiers->ready);
561 free(notifiers);
5b093681
JG
562}
563
564bool launch_agent_management_thread(void)
8a7e4590 565{
c78d8e86 566 struct thread_notifiers *notifiers;
8a7e4590
JG
567 struct lttng_thread *thread;
568
c78d8e86
JG
569 notifiers = zmalloc(sizeof(*notifiers));
570 if (!notifiers) {
21fa020e 571 goto error_alloc;
c78d8e86
JG
572 }
573
574 sem_init(&notifiers->ready, 0, 0);
575 notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
576 if (!notifiers->quit_pipe) {
8a7e4590
JG
577 goto error;
578 }
579 thread = lttng_thread_create("Agent management",
580 thread_agent_management,
581 shutdown_agent_management_thread,
5b093681 582 cleanup_agent_management_thread,
c78d8e86 583 notifiers);
8a7e4590
JG
584 if (!thread) {
585 goto error;
586 }
c78d8e86 587 wait_until_thread_is_ready(notifiers);
8a7e4590
JG
588 lttng_thread_put(thread);
589 return true;
590error:
c78d8e86 591 cleanup_agent_management_thread(notifiers);
21fa020e 592error_alloc:
8a7e4590
JG
593 return false;
594}
This page took 0.139927 seconds and 5 git commands to generate.