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 (trigger_agents_ht_by_domain
->ht
, &iter
.iter
,
94 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(config
.agent_tcp_port
.begin
> 0);
118 uri
->port
= config
.agent_tcp_port
.begin
;
120 sock
= lttcomm_alloc_sock_from_uri(uri
);
123 ERR("[agent-thread] agent allocating TCP socket");
127 ret
= lttcomm_create_sock(sock
);
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
);
136 ERR("[agent-thread] Failed to set port %u on socket",
140 DBG3("[agent-thread] 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 (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
);
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
);
171 ret
= sock
->ops
->listen(sock
, -1);
176 DBG("[agent-thread] Listening on TCP port %u and socket %d",
183 lttcomm_destroy_sock(sock
);
189 * Close and destroy the given TCP socket.
191 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
198 ret
= lttcomm_sock_get_port(sock
, &port
);
200 ERR("[agent-thread] Failed to get port of agent TCP socket");
204 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16
,
207 /* This will return gracefully if fd is invalid. */
208 sock
->ops
->close(sock
);
209 lttcomm_destroy_sock(sock
);
212 static const char *domain_type_str(enum lttng_domain_type domain_type
)
214 switch (domain_type
) {
215 case LTTNG_DOMAIN_NONE
:
217 case LTTNG_DOMAIN_KERNEL
:
219 case LTTNG_DOMAIN_UST
:
221 case LTTNG_DOMAIN_JUL
:
223 case LTTNG_DOMAIN_LOG4J
:
225 case LTTNG_DOMAIN_PYTHON
:
232 static bool is_agent_protocol_version_supported(
233 const struct agent_protocol_version
*version
)
235 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
236 version
->minor
== AGENT_MINOR_VERSION
;
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
);
248 * Handle a new agent connection on the registration socket.
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`.
254 static 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
)
260 struct agent_protocol_version agent_version
;
262 struct agent_register_msg msg
;
263 struct lttcomm_sock
*new_sock
;
267 new_sock
= reg_sock
->ops
->accept(reg_sock
);
273 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
274 if (size
< sizeof(msg
)) {
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",
281 DBG("Failed to register new agent application: connection closed");
284 goto error_close_socket
;
287 agent_version
= (struct agent_protocol_version
) {
288 be32toh(msg
.major_version
),
289 be32toh(msg
.minor_version
),
292 /* Test communication protocol version of the registering agent. */
293 if (!is_agent_protocol_version_supported(&agent_version
)) {
295 goto error_close_socket
;
298 *agent_app_id
= (struct agent_app_id
) {
299 .domain
= (enum lttng_domain_type
) be32toh(msg
.domain
),
300 .pid
= (pid_t
) be32toh(msg
.pid
),
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
);
307 *agent_app_socket
= new_sock
;
313 new_sock
->ops
->close(new_sock
);
314 lttcomm_destroy_sock(new_sock
);
319 bool agent_tracing_is_enabled(void)
323 enabled
= uatomic_read(&agent_tracing_enabled
);
324 assert(enabled
!= -1);
329 * Write agent TCP port using the rundir.
331 static int write_agent_port(uint16_t port
)
333 return utils_create_pid_file((pid_t
) port
,
334 config
.agent_port_file_path
.value
);
338 void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
340 DBG("Marking agent management thread as ready");
341 sem_post(¬ifiers
->ready
);
345 void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
347 DBG("Waiting for agent management thread to be ready");
348 sem_wait(¬ifiers
->ready
);
349 DBG("Agent management thread is ready");
353 * This thread manage application notify communication.
355 static void *thread_agent_management(void *data
)
358 uint32_t revents
, nb_fd
;
359 struct lttng_poll_event events
;
360 struct lttcomm_sock
*reg_sock
;
361 struct thread_notifiers
*notifiers
= data
;
362 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
363 notifiers
->quit_pipe
);
365 DBG("[agent-thread] Manage agent application registration.");
367 rcu_register_thread();
370 /* Agent initialization call MUST be called before starting the thread. */
371 assert(agent_apps_ht_by_sock
);
373 /* Create pollset with size 2, quit pipe and registration socket. */
374 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
376 goto error_poll_create
;
379 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
382 goto error_tcp_socket
;
385 reg_sock
= init_tcp_socket();
389 assert(lttcomm_sock_get_port(reg_sock
, &port
) == 0);
391 ret
= write_agent_port(port
);
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. */
395 mark_thread_as_ready(notifiers
);
399 /* Don't prevent the launch of the sessiond on error. */
400 mark_thread_as_ready(notifiers
);
401 goto error_tcp_socket
;
405 * Signal that the agent thread is ready. The command thread
406 * may start to query whether or not agent tracing is enabled.
408 uatomic_set(&agent_tracing_enabled
, 1);
409 mark_thread_as_ready(notifiers
);
411 /* Add TCP socket to the poll set. */
412 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
413 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
419 DBG3("[agent-thread] Manage agent polling");
421 /* Inifinite blocking call, waiting for transmission */
423 ret
= lttng_poll_wait(&events
, -1);
424 DBG3("[agent-thread] Manage agent return from poll on %d fds",
425 LTTNG_POLL_GETNB(&events
));
428 * Restart interrupted system call.
430 if (errno
== EINTR
) {
436 DBG3("[agent-thread] %d fd ready", nb_fd
);
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
);
443 /* Thread quit pipe has been closed. Killing thread. */
444 if (pollfd
== quit_pipe_read_fd
) {
448 /* Activity on the registration socket. */
449 if (revents
& LPOLLIN
) {
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
;
455 assert(pollfd
== reg_sock
->fd
);
457 ret
= accept_agent_connection(
458 reg_sock
, &new_app_id
, &new_app_socket
);
460 /* Errors are already logged. */
465 * new_app_socket's ownership has been
466 * transferred to the new agent app.
468 new_app
= agent_create_app(new_app_id
.pid
,
472 new_app_socket
->ops
->close(
476 new_app_socket_fd
= new_app_socket
->fd
;
477 new_app_socket
= NULL
;
480 * Since this is a command socket (write then
481 * read), only add poll error event to only
484 ret
= lttng_poll_add(&events
, new_app_socket_fd
,
485 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
487 agent_destroy_app(new_app
);
492 * Prevent sessions from being modified while
493 * the agent application's configuration is
499 * Update the newly registered applications's
502 update_agent_app(new_app
);
504 ret
= agent_send_registration_done(new_app
);
506 agent_destroy_app(new_app
);
507 /* Removing from the poll set. */
508 ret
= lttng_poll_del(&events
,
511 session_unlock_list();
517 /* Publish the new agent app. */
518 agent_add_app(new_app
);
520 session_unlock_list();
521 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
522 /* Removing from the poll set */
523 ret
= lttng_poll_del(&events
, pollfd
);
527 agent_destroy_app_by_sock(pollfd
);
529 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
536 /* Whatever happens, try to delete it and exit. */
537 (void) lttng_poll_del(&events
, reg_sock
->fd
);
539 destroy_tcp_socket(reg_sock
);
541 lttng_poll_clean(&events
);
543 uatomic_set(&agent_tracing_enabled
, 0);
544 DBG("[agent-thread] Cleaning up and stopping.");
545 rcu_thread_offline();
546 rcu_unregister_thread();
550 static bool shutdown_agent_management_thread(void *data
)
552 struct thread_notifiers
*notifiers
= data
;
553 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
555 return notify_thread_pipe(write_fd
) == 1;
558 static void cleanup_agent_management_thread(void *data
)
560 struct thread_notifiers
*notifiers
= data
;
562 lttng_pipe_destroy(notifiers
->quit_pipe
);
563 sem_destroy(¬ifiers
->ready
);
567 bool launch_agent_management_thread(void)
569 struct thread_notifiers
*notifiers
;
570 struct lttng_thread
*thread
;
572 notifiers
= zmalloc(sizeof(*notifiers
));
577 sem_init(¬ifiers
->ready
, 0, 0);
578 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
579 if (!notifiers
->quit_pipe
) {
582 thread
= lttng_thread_create("Agent management",
583 thread_agent_management
,
584 shutdown_agent_management_thread
,
585 cleanup_agent_management_thread
,
590 wait_until_thread_is_ready(notifiers
);
591 lttng_thread_put(thread
);
594 cleanup_agent_management_thread(notifiers
);