Fix: statements with side-effects in assert statements
[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
7d849b29
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 *
7d849b29
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 */
7d849b29 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) {
7d849b29 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) {
7d849b29 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
7d849b29
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/*
7d849b29 235 * Handle a new agent connection on the registration socket.
f20baf8e 236 *
7d849b29
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 */
7d849b29
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;
7d849b29 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;
7d849b29 257 goto end;
f20baf8e
DG
258 }
259
260 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
261 if (size < sizeof(msg)) {
7d849b29
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;
7d849b29 271 goto error_close_socket;
9474416f 272 }
f20baf8e 273
7d849b29
JG
274 agent_version = (struct agent_protocol_version) {
275 be32toh(msg.major_version),
276 be32toh(msg.minor_version),
277 };
f20baf8e 278
7d849b29
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
7d849b29
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
7d849b29
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
7d849b29
JG
294 *agent_app_socket = new_sock;
295 new_sock = NULL;
296 ret = 0;
297 goto end;
f20baf8e 298
7d849b29 299error_close_socket:
f20baf8e
DG
300 new_sock->ops->close(new_sock);
301 lttcomm_destroy_sock(new_sock);
7d849b29 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
e0ad3e31
FD
376 ret = lttcomm_sock_get_port(reg_sock, &port);
377 assert(ret == 0);
2288467f
JG
378
379 ret = write_agent_port(port);
380 if (ret) {
381 ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable");
382 /* Don't prevent the launch of the sessiond on error. */
c78d8e86 383 mark_thread_as_ready(notifiers);
2288467f
JG
384 goto error;
385 }
386 } else {
387 /* Don't prevent the launch of the sessiond on error. */
c78d8e86 388 mark_thread_as_ready(notifiers);
2288467f
JG
389 goto error_tcp_socket;
390 }
f28f9e44
JG
391
392 /*
393 * Signal that the agent thread is ready. The command thread
394 * may start to query whether or not agent tracing is enabled.
395 */
2288467f 396 uatomic_set(&agent_tracing_enabled, 1);
c78d8e86 397 mark_thread_as_ready(notifiers);
4d076222 398
7d849b29 399 /* Add TCP socket to the poll set. */
4d076222
DG
400 ret = lttng_poll_add(&events, reg_sock->fd,
401 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
402 if (ret < 0) {
403 goto error;
404 }
405
406 while (1) {
546f19b5 407 DBG3("[agent-thread] Manage agent polling");
4d076222
DG
408
409 /* Inifinite blocking call, waiting for transmission */
410restart:
411 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
412 DBG3("[agent-thread] Manage agent return from poll on %d fds",
413 LTTNG_POLL_GETNB(&events));
4d076222
DG
414 if (ret < 0) {
415 /*
416 * Restart interrupted system call.
417 */
418 if (errno == EINTR) {
419 goto restart;
420 }
421 goto error;
422 }
423 nb_fd = ret;
022d91ba 424 DBG3("[agent-thread] %d fd ready", nb_fd);
4d076222
DG
425
426 for (i = 0; i < nb_fd; i++) {
427 /* Fetch once the poll data */
428 revents = LTTNG_POLL_GETEV(&events, i);
429 pollfd = LTTNG_POLL_GETFD(&events, i);
430
431 /* Thread quit pipe has been closed. Killing thread. */
8a7e4590 432 if (pollfd == quit_pipe_read_fd) {
4d076222
DG
433 goto exit;
434 }
435
7d849b29 436 /* Activity on the registration socket. */
03e43155 437 if (revents & LPOLLIN) {
7d849b29
JG
438 struct agent_app_id new_app_id;
439 struct agent_app *new_app = NULL;
440 struct lttcomm_sock *new_app_socket;
441 int new_app_socket_fd;
f20baf8e 442
f20baf8e 443 assert(pollfd == reg_sock->fd);
7d849b29
JG
444
445 ret = accept_agent_connection(
446 reg_sock, &new_app_id, &new_app_socket);
447 if (ret < 0) {
448 /* Errors are already logged. */
f20baf8e
DG
449 continue;
450 }
451
03e43155 452 /*
7d849b29
JG
453 * new_app_socket's ownership has been
454 * transferred to the new agent app.
03e43155 455 */
7d849b29
JG
456 new_app = agent_create_app(new_app_id.pid,
457 new_app_id.domain,
458 new_app_socket);
459 if (!new_app) {
460 new_app_socket->ops->close(
461 new_app_socket);
462 continue;
463 }
464 new_app_socket_fd = new_app_socket->fd;
465 new_app_socket = NULL;
466
467 /*
468 * Since this is a command socket (write then
469 * read), only add poll error event to only
470 * detect shutdown.
471 */
472 ret = lttng_poll_add(&events, new_app_socket_fd,
f20baf8e
DG
473 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
474 if (ret < 0) {
7d849b29 475 agent_destroy_app(new_app);
f20baf8e
DG
476 continue;
477 }
478
7d849b29
JG
479 /*
480 * Prevent sessions from being modified while
481 * the agent application's configuration is
482 * updated.
483 */
484 session_lock_list();
485
486 /*
487 * Update the newly registered applications's
488 * configuration.
489 */
490 update_agent_app(new_app);
1b500e7a 491
7d849b29 492 ret = agent_send_registration_done(new_app);
03e43155 493 if (ret < 0) {
7d849b29
JG
494 agent_destroy_app(new_app);
495 /* Removing from the poll set. */
496 ret = lttng_poll_del(&events,
497 new_app_socket_fd);
03e43155 498 if (ret < 0) {
7d849b29 499 session_unlock_list();
03e43155
MD
500 goto error;
501 }
03e43155
MD
502 continue;
503 }
7d849b29
JG
504
505 /* Publish the new agent app. */
506 agent_add_app(new_app);
507
508 session_unlock_list();
03e43155
MD
509 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
510 /* Removing from the poll set */
511 ret = lttng_poll_del(&events, pollfd);
512 if (ret < 0) {
513 goto error;
514 }
515 agent_destroy_app_by_sock(pollfd);
4d076222 516 } else {
03e43155
MD
517 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
518 goto error;
4d076222
DG
519 }
520 }
521 }
522
523exit:
f20baf8e
DG
524 /* Whatever happens, try to delete it and exit. */
525 (void) lttng_poll_del(&events, reg_sock->fd);
4d076222
DG
526error:
527 destroy_tcp_socket(reg_sock);
528error_tcp_socket:
529 lttng_poll_clean(&events);
530error_poll_create:
2288467f 531 uatomic_set(&agent_tracing_enabled, 0);
8a7e4590 532 DBG("[agent-thread] Cleaning up and stopping.");
4d076222
DG
533 rcu_thread_offline();
534 rcu_unregister_thread();
535 return NULL;
536}
8a7e4590
JG
537
538static bool shutdown_agent_management_thread(void *data)
539{
c78d8e86
JG
540 struct thread_notifiers *notifiers = data;
541 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
8a7e4590
JG
542
543 return notify_thread_pipe(write_fd) == 1;
544}
545
5b093681
JG
546static void cleanup_agent_management_thread(void *data)
547{
c78d8e86 548 struct thread_notifiers *notifiers = data;
5b093681 549
c78d8e86
JG
550 lttng_pipe_destroy(notifiers->quit_pipe);
551 sem_destroy(&notifiers->ready);
552 free(notifiers);
5b093681
JG
553}
554
555bool launch_agent_management_thread(void)
8a7e4590 556{
c78d8e86 557 struct thread_notifiers *notifiers;
8a7e4590
JG
558 struct lttng_thread *thread;
559
c78d8e86
JG
560 notifiers = zmalloc(sizeof(*notifiers));
561 if (!notifiers) {
21fa020e 562 goto error_alloc;
c78d8e86
JG
563 }
564
565 sem_init(&notifiers->ready, 0, 0);
566 notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
567 if (!notifiers->quit_pipe) {
8a7e4590
JG
568 goto error;
569 }
570 thread = lttng_thread_create("Agent management",
571 thread_agent_management,
572 shutdown_agent_management_thread,
5b093681 573 cleanup_agent_management_thread,
c78d8e86 574 notifiers);
8a7e4590
JG
575 if (!thread) {
576 goto error;
577 }
c78d8e86 578 wait_until_thread_is_ready(notifiers);
8a7e4590
JG
579 lttng_thread_put(thread);
580 return true;
581error:
c78d8e86 582 cleanup_agent_management_thread(notifiers);
21fa020e 583error_alloc:
8a7e4590
JG
584 return false;
585}
This page took 0.092737 seconds and 5 git commands to generate.