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