2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
11 #include <common/common.h>
12 #include <common/sessiond-comm/sessiond-comm.h>
13 #include <common/uri.h>
14 #include <common/utils.h>
16 #include <common/compat/endian.h>
19 #include "agent-thread.h"
21 #include "lttng-sessiond.h"
26 struct thread_notifiers
{
27 struct lttng_pipe
*quit_pipe
;
33 enum lttng_domain_type domain
;
36 struct agent_protocol_version
{
37 unsigned int major
, minor
;
40 static int agent_tracing_enabled
= -1;
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.
47 static const char *default_reg_uri
=
48 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
51 * Update agent application using the given socket. This is done just after
52 * registration was successful.
54 * This will acquire the various sessions' lock; none must be held by the
56 * The caller must hold the session list lock.
58 static void update_agent_app(const struct agent_app
*app
)
60 struct ltt_session
*session
, *stmp
;
61 struct ltt_session_list
*list
;
62 struct agent
*trigger_agent
;
63 struct lttng_ht_iter iter
;
65 list
= session_get_list();
68 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
69 if (!session_get(session
)) {
73 session_lock(session
);
74 if (session
->ust_session
) {
75 const struct agent
*agt
;
78 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
80 agent_update(agt
, app
);
84 session_unlock(session
);
90 * We are protected against the addition of new events by the session
91 * list lock being held.
93 cds_lfht_for_each_entry(the_trigger_agents_ht_by_domain
->ht
,
94 &iter
.iter
, trigger_agent
, node
.node
) {
95 agent_update(trigger_agent
, app
);
101 * Create and init socket from uri.
103 static struct lttcomm_sock
*init_tcp_socket(void)
106 struct lttng_uri
*uri
= NULL
;
107 struct lttcomm_sock
*sock
= NULL
;
109 bool bind_succeeded
= false;
112 * This should never fail since the URI is hardcoded and the port is set
113 * before this thread is launched.
115 ret
= uri_parse(default_reg_uri
, &uri
);
117 assert(the_config
.agent_tcp_port
.begin
> 0);
118 uri
->port
= the_config
.agent_tcp_port
.begin
;
120 sock
= lttcomm_alloc_sock_from_uri(uri
);
123 ERR("agent allocating TCP socket");
127 ret
= lttcomm_create_sock(sock
);
132 for (port
= the_config
.agent_tcp_port
.begin
;
133 port
<= the_config
.agent_tcp_port
.end
; port
++) {
134 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
136 ERR("Failed to set port %u on socket",
140 DBG3("Trying to bind on port %u", port
);
141 ret
= sock
->ops
->bind(sock
);
143 bind_succeeded
= true;
147 if (errno
== EADDRINUSE
) {
148 DBG("Failed to bind to port %u since it is already in use",
151 PERROR("Failed to bind to port %u", port
);
156 if (!bind_succeeded
) {
157 if (the_config
.agent_tcp_port
.begin
==
158 the_config
.agent_tcp_port
.end
) {
159 WARN("Another process is already using the agent port %i. "
160 "Agent support will be deactivated.",
161 the_config
.agent_tcp_port
.begin
);
164 WARN("All ports in the range [%i, %i] are already in use. "
165 "Agent support will be deactivated.",
166 the_config
.agent_tcp_port
.begin
,
167 the_config
.agent_tcp_port
.end
);
172 ret
= sock
->ops
->listen(sock
, -1);
177 DBG("Listening on TCP port %u and socket %d",
184 lttcomm_destroy_sock(sock
);
190 * Close and destroy the given TCP socket.
192 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
199 ret
= lttcomm_sock_get_port(sock
, &port
);
201 ERR("Failed to get port of agent TCP socket");
205 DBG3("Destroy TCP socket on port %" PRIu16
,
208 /* This will return gracefully if fd is invalid. */
209 sock
->ops
->close(sock
);
210 lttcomm_destroy_sock(sock
);
213 static const char *domain_type_str(enum lttng_domain_type domain_type
)
215 switch (domain_type
) {
216 case LTTNG_DOMAIN_NONE
:
218 case LTTNG_DOMAIN_KERNEL
:
220 case LTTNG_DOMAIN_UST
:
222 case LTTNG_DOMAIN_JUL
:
224 case LTTNG_DOMAIN_LOG4J
:
226 case LTTNG_DOMAIN_PYTHON
:
233 static bool is_agent_protocol_version_supported(
234 const struct agent_protocol_version
*version
)
236 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
237 version
->minor
== AGENT_MINOR_VERSION
;
240 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
241 version
->major
, version
->minor
,
242 AGENT_MAJOR_VERSION
, AGENT_MINOR_VERSION
);
249 * Handle a new agent connection on the registration socket.
251 * Returns 0 on success, or else a negative errno value.
252 * On success, the resulting socket is returned through `agent_app_socket`
253 * and the application's reported id is updated through `agent_app_id`.
255 static int accept_agent_connection(
256 struct lttcomm_sock
*reg_sock
,
257 struct agent_app_id
*agent_app_id
,
258 struct lttcomm_sock
**agent_app_socket
)
261 struct agent_protocol_version agent_version
;
263 struct agent_register_msg msg
;
264 struct lttcomm_sock
*new_sock
;
268 new_sock
= reg_sock
->ops
->accept(reg_sock
);
274 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
275 if (size
< sizeof(msg
)) {
277 PERROR("Failed to register new agent application");
278 } else if (size
!= 0) {
279 ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd",
282 DBG("Failed to register new agent application: connection closed");
285 goto error_close_socket
;
288 agent_version
= (struct agent_protocol_version
) {
289 be32toh(msg
.major_version
),
290 be32toh(msg
.minor_version
),
293 /* Test communication protocol version of the registering agent. */
294 if (!is_agent_protocol_version_supported(&agent_version
)) {
296 goto error_close_socket
;
299 *agent_app_id
= (struct agent_app_id
) {
300 .domain
= (enum lttng_domain_type
) be32toh(msg
.domain
),
301 .pid
= (pid_t
) be32toh(msg
.pid
),
304 DBG2("New registration for agent application: pid = %ld, domain = %s, socket fd = %d",
305 (long) agent_app_id
->pid
,
306 domain_type_str(agent_app_id
->domain
), new_sock
->fd
);
308 *agent_app_socket
= new_sock
;
314 new_sock
->ops
->close(new_sock
);
315 lttcomm_destroy_sock(new_sock
);
320 bool agent_tracing_is_enabled(void)
324 enabled
= uatomic_read(&agent_tracing_enabled
);
325 assert(enabled
!= -1);
330 * Write agent TCP port using the rundir.
332 static int write_agent_port(uint16_t port
)
334 return utils_create_pid_file(
335 (pid_t
) port
, the_config
.agent_port_file_path
.value
);
339 void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
341 DBG("Marking agent management thread as ready");
342 sem_post(¬ifiers
->ready
);
346 void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
348 DBG("Waiting for agent management thread to be ready");
349 sem_wait(¬ifiers
->ready
);
350 DBG("Agent management thread is ready");
354 * This thread manage application notify communication.
356 static void *thread_agent_management(void *data
)
359 uint32_t revents
, nb_fd
;
360 struct lttng_poll_event events
;
361 struct lttcomm_sock
*reg_sock
;
362 struct thread_notifiers
*notifiers
= data
;
363 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
364 notifiers
->quit_pipe
);
366 DBG("Manage agent application registration.");
368 rcu_register_thread();
371 /* Agent initialization call MUST be called before starting the thread. */
372 assert(the_agent_apps_ht_by_sock
);
374 /* Create pollset with size 2, quit pipe and registration socket. */
375 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
377 goto error_poll_create
;
380 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
383 goto error_tcp_socket
;
386 reg_sock
= init_tcp_socket();
390 ret
= lttcomm_sock_get_port(reg_sock
, &port
);
393 ret
= write_agent_port(port
);
395 ERR("Failed to create agent port file: agent tracing will be unavailable");
396 /* Don't prevent the launch of the sessiond on error. */
397 mark_thread_as_ready(notifiers
);
401 /* Don't prevent the launch of the sessiond on error. */
402 mark_thread_as_ready(notifiers
);
403 goto error_tcp_socket
;
407 * Signal that the agent thread is ready. The command thread
408 * may start to query whether or not agent tracing is enabled.
410 uatomic_set(&agent_tracing_enabled
, 1);
411 mark_thread_as_ready(notifiers
);
413 /* Add TCP socket to the poll set. */
414 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
415 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
421 DBG3("Manage agent polling");
423 /* Inifinite blocking call, waiting for transmission */
425 ret
= lttng_poll_wait(&events
, -1);
426 DBG3("Manage agent return from poll on %d fds",
427 LTTNG_POLL_GETNB(&events
));
430 * Restart interrupted system call.
432 if (errno
== EINTR
) {
438 DBG3("%d fd ready", nb_fd
);
440 for (i
= 0; i
< nb_fd
; i
++) {
441 /* Fetch once the poll data */
442 revents
= LTTNG_POLL_GETEV(&events
, i
);
443 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
445 /* Thread quit pipe has been closed. Killing thread. */
446 if (pollfd
== quit_pipe_read_fd
) {
450 /* Activity on the registration socket. */
451 if (revents
& LPOLLIN
) {
452 struct agent_app_id new_app_id
;
453 struct agent_app
*new_app
= NULL
;
454 struct lttcomm_sock
*new_app_socket
;
455 int new_app_socket_fd
;
457 assert(pollfd
== reg_sock
->fd
);
459 ret
= accept_agent_connection(
460 reg_sock
, &new_app_id
, &new_app_socket
);
462 /* Errors are already logged. */
467 * new_app_socket's ownership has been
468 * transferred to the new agent app.
470 new_app
= agent_create_app(new_app_id
.pid
,
474 new_app_socket
->ops
->close(
478 new_app_socket_fd
= new_app_socket
->fd
;
479 new_app_socket
= NULL
;
482 * Since this is a command socket (write then
483 * read), only add poll error event to only
486 ret
= lttng_poll_add(&events
, new_app_socket_fd
,
487 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
489 agent_destroy_app(new_app
);
494 * Prevent sessions from being modified while
495 * the agent application's configuration is
501 * Update the newly registered applications's
504 update_agent_app(new_app
);
506 ret
= agent_send_registration_done(new_app
);
508 agent_destroy_app(new_app
);
509 /* Removing from the poll set. */
510 ret
= lttng_poll_del(&events
,
513 session_unlock_list();
519 /* Publish the new agent app. */
520 agent_add_app(new_app
);
522 session_unlock_list();
523 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
524 /* Removing from the poll set */
525 ret
= lttng_poll_del(&events
, pollfd
);
529 agent_destroy_app_by_sock(pollfd
);
531 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
538 /* Whatever happens, try to delete it and exit. */
539 (void) lttng_poll_del(&events
, reg_sock
->fd
);
541 destroy_tcp_socket(reg_sock
);
543 lttng_poll_clean(&events
);
545 uatomic_set(&agent_tracing_enabled
, 0);
546 DBG("Cleaning up and stopping.");
547 rcu_thread_offline();
548 rcu_unregister_thread();
552 static bool shutdown_agent_management_thread(void *data
)
554 struct thread_notifiers
*notifiers
= data
;
555 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
557 return notify_thread_pipe(write_fd
) == 1;
560 static void cleanup_agent_management_thread(void *data
)
562 struct thread_notifiers
*notifiers
= data
;
564 lttng_pipe_destroy(notifiers
->quit_pipe
);
565 sem_destroy(¬ifiers
->ready
);
569 bool launch_agent_management_thread(void)
571 struct thread_notifiers
*notifiers
;
572 struct lttng_thread
*thread
;
574 notifiers
= zmalloc(sizeof(*notifiers
));
579 sem_init(¬ifiers
->ready
, 0, 0);
580 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
581 if (!notifiers
->quit_pipe
) {
584 thread
= lttng_thread_create("Agent management",
585 thread_agent_management
,
586 shutdown_agent_management_thread
,
587 cleanup_agent_management_thread
,
592 wait_until_thread_is_ready(notifiers
);
593 lttng_thread_put(thread
);
596 cleanup_agent_management_thread(notifiers
);