2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include "agent-thread.hpp"
12 #include "fd-limit.hpp"
13 #include "lttng-sessiond.hpp"
14 #include "session.hpp"
18 #include <common/common.hpp>
19 #include <common/compat/endian.hpp>
20 #include <common/sessiond-comm/sessiond-comm.hpp>
21 #include <common/urcu.hpp>
22 #include <common/uri.hpp>
23 #include <common/utils.hpp>
28 struct thread_notifiers
{
29 struct lttng_pipe
*quit_pipe
;
35 enum lttng_domain_type domain
;
38 struct agent_protocol_version
{
39 unsigned int major
, minor
;
42 int agent_tracing_enabled
= -1;
45 * Note that there is not port here. It's set after this URI is parsed so we
46 * can let the user define a custom one. However, localhost is ALWAYS the
47 * default listening address.
49 const char *default_reg_uri
= "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
53 * Update agent application using the given socket. This is done just after
54 * registration was successful.
56 * This will acquire the various sessions' lock; none must be held by the
58 * The caller must hold the session list lock.
60 static void update_agent_app(const struct agent_app
*app
)
62 struct ltt_session_list
*list
;
64 list
= session_get_list();
68 lttng::urcu::list_iteration_adapter
<ltt_session
, <t_session::list
>(list
->head
)) {
69 if (!session_get(session
)) {
73 session_lock(session
);
74 if (session
->ust_session
) {
75 const struct agent
*agt
;
77 const lttng::urcu::read_lock_guard read_lock
;
78 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
80 agent_update(agt
, app
);
83 session_unlock(session
);
88 * We are protected against the addition of new events by the session
89 * list lock being held.
91 for (auto *trigger_agent
:
92 lttng::urcu::lfht_iteration_adapter
<agent
, decltype(agent::node
), &agent::node
>(
93 *the_trigger_agents_ht_by_domain
->ht
)) {
94 agent_update(trigger_agent
, app
);
99 * Create and init socket from uri.
101 static struct lttcomm_sock
*init_tcp_socket()
104 struct lttng_uri
*uri
= nullptr;
105 struct lttcomm_sock
*sock
= nullptr;
107 bool bind_succeeded
= false;
110 * This should never fail since the URI is hardcoded and the port is set
111 * before this thread is launched.
113 ret
= uri_parse(default_reg_uri
, &uri
);
115 LTTNG_ASSERT(the_config
.agent_tcp_port
.begin
> 0);
116 uri
->port
= the_config
.agent_tcp_port
.begin
;
118 sock
= lttcomm_alloc_sock_from_uri(uri
);
120 if (sock
== nullptr) {
121 ERR("agent allocating TCP socket");
125 ret
= lttcomm_create_sock(sock
);
130 for (port
= the_config
.agent_tcp_port
.begin
; port
<= the_config
.agent_tcp_port
.end
;
132 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
134 ERR("Failed to set port %u on socket", port
);
137 DBG3("Trying to bind on port %u", port
);
138 ret
= sock
->ops
->bind(sock
);
140 bind_succeeded
= true;
144 if (errno
== EADDRINUSE
) {
145 DBG("Failed to bind to port %u since it is already in use", port
);
147 PERROR("Failed to bind to port %u", port
);
152 if (!bind_succeeded
) {
153 if (the_config
.agent_tcp_port
.begin
== the_config
.agent_tcp_port
.end
) {
154 WARN("Another process is already using the agent port %i. "
155 "Agent support will be deactivated.",
156 the_config
.agent_tcp_port
.begin
);
159 WARN("All ports in the range [%i, %i] are already in use. "
160 "Agent support will be deactivated.",
161 the_config
.agent_tcp_port
.begin
,
162 the_config
.agent_tcp_port
.end
);
167 ret
= sock
->ops
->listen(sock
, -1);
172 DBG("Listening on TCP port %u and socket %d", port
, sock
->fd
);
178 lttcomm_destroy_sock(sock
);
184 * Close and destroy the given TCP socket.
186 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
193 ret
= lttcomm_sock_get_port(sock
, &port
);
195 ERR("Failed to get port of agent TCP socket");
199 DBG3("Destroy TCP socket on port %" PRIu16
, port
);
201 /* This will return gracefully if fd is invalid. */
202 sock
->ops
->close(sock
);
203 lttcomm_destroy_sock(sock
);
206 static const char *domain_type_str(enum lttng_domain_type domain_type
)
208 switch (domain_type
) {
209 case LTTNG_DOMAIN_NONE
:
211 case LTTNG_DOMAIN_KERNEL
:
213 case LTTNG_DOMAIN_UST
:
215 case LTTNG_DOMAIN_JUL
:
217 case LTTNG_DOMAIN_LOG4J
:
219 case LTTNG_DOMAIN_LOG4J2
:
221 case LTTNG_DOMAIN_PYTHON
:
228 static bool is_agent_protocol_version_supported(const struct agent_protocol_version
*version
)
230 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
231 version
->minor
== AGENT_MINOR_VERSION
;
234 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
238 AGENT_MINOR_VERSION
);
245 * Handle a new agent connection on the registration socket.
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`.
251 static int accept_agent_connection(struct lttcomm_sock
*reg_sock
,
252 struct agent_app_id
*agent_app_id
,
253 struct lttcomm_sock
**agent_app_socket
)
256 struct agent_protocol_version agent_version
;
258 struct agent_register_msg msg
;
259 struct lttcomm_sock
*new_sock
;
261 LTTNG_ASSERT(reg_sock
);
263 new_sock
= reg_sock
->ops
->accept(reg_sock
);
269 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
270 if (size
< sizeof(msg
)) {
272 PERROR("Failed to register new agent application");
273 } else if (size
!= 0) {
274 ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd",
278 DBG("Failed to register new agent application: connection closed");
281 goto error_close_socket
;
284 agent_version
= (struct agent_protocol_version
){
285 be32toh(msg
.major_version
),
286 be32toh(msg
.minor_version
),
289 /* Test communication protocol version of the registering agent. */
290 if (!is_agent_protocol_version_supported(&agent_version
)) {
292 goto error_close_socket
;
295 *agent_app_id
= (struct agent_app_id
){
296 .pid
= (pid_t
) be32toh(msg
.pid
),
297 .domain
= (lttng_domain_type
) be32toh(msg
.domain
),
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
),
305 *agent_app_socket
= new_sock
;
311 new_sock
->ops
->close(new_sock
);
312 lttcomm_destroy_sock(new_sock
);
317 bool agent_tracing_is_enabled()
321 enabled
= uatomic_read(&agent_tracing_enabled
);
322 LTTNG_ASSERT(enabled
!= -1);
327 * Write agent TCP port using the rundir.
329 static int write_agent_port(uint16_t port
)
331 return utils_create_pid_file((pid_t
) port
, the_config
.agent_port_file_path
.value
);
334 static void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
336 DBG("Marking agent management thread as ready");
337 sem_post(¬ifiers
->ready
);
340 static void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
342 DBG("Waiting for agent management thread to be ready");
343 sem_wait(¬ifiers
->ready
);
344 DBG("Agent management thread is ready");
348 * This thread manage application notify communication.
350 static void *thread_agent_management(void *data
)
354 struct lttng_poll_event events
;
355 struct lttcomm_sock
*reg_sock
;
356 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
357 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
359 DBG("Manage agent application registration.");
361 rcu_register_thread();
364 /* Agent initialization call MUST be called before starting the thread. */
365 LTTNG_ASSERT(the_agent_apps_ht_by_sock
);
367 /* Create pollset with size 2, quit pipe and registration socket. */
368 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
370 goto error_poll_create
;
373 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
375 goto error_tcp_socket
;
378 reg_sock
= init_tcp_socket();
382 ret
= lttcomm_sock_get_port(reg_sock
, &port
);
383 LTTNG_ASSERT(ret
== 0);
385 ret
= write_agent_port(port
);
387 ERR("Failed to create agent port file: agent tracing will be unavailable");
388 /* Don't prevent the launch of the sessiond on error. */
389 mark_thread_as_ready(notifiers
);
393 /* Don't prevent the launch of the sessiond on error. */
394 mark_thread_as_ready(notifiers
);
395 goto error_tcp_socket
;
399 * Signal that the agent thread is ready. The command thread
400 * may start to query whether or not agent tracing is enabled.
402 uatomic_set(&agent_tracing_enabled
, 1);
403 mark_thread_as_ready(notifiers
);
405 /* Add TCP socket to the poll set. */
406 ret
= lttng_poll_add(&events
, reg_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
412 DBG3("Manage agent polling");
414 /* Inifinite blocking call, waiting for transmission */
416 ret
= lttng_poll_wait(&events
, -1);
417 DBG3("Manage agent return from poll on %d fds", LTTNG_POLL_GETNB(&events
));
420 * Restart interrupted system call.
422 if (errno
== EINTR
) {
428 DBG3("%d fd ready", nb_fd
);
430 for (i
= 0; i
< nb_fd
; i
++) {
431 /* Fetch once the poll data */
432 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
433 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
435 /* Activity on thread quit pipe, exiting. */
436 if (pollfd
== thread_quit_pipe_fd
) {
437 DBG("Activity on thread quit pipe");
441 /* Activity on the registration socket. */
442 if (revents
& LPOLLIN
) {
443 struct agent_app_id new_app_id
;
444 struct agent_app
*new_app
= nullptr;
445 struct lttcomm_sock
*new_app_socket
;
446 int new_app_socket_fd
;
448 LTTNG_ASSERT(pollfd
== reg_sock
->fd
);
450 ret
= accept_agent_connection(
451 reg_sock
, &new_app_id
, &new_app_socket
);
453 /* Errors are already logged. */
458 * new_app_socket's ownership has been
459 * transferred to the new agent app.
461 new_app
= agent_create_app(
462 new_app_id
.pid
, new_app_id
.domain
, new_app_socket
);
464 new_app_socket
->ops
->close(new_app_socket
);
467 new_app_socket_fd
= new_app_socket
->fd
;
468 new_app_socket
= nullptr;
471 * Since this is a command socket (write then
472 * read), only add poll error event to only
475 ret
= lttng_poll_add(&events
, new_app_socket_fd
, LPOLLRDHUP
);
477 agent_destroy_app(new_app
);
482 * Prevent sessions from being modified while
483 * the agent application's configuration is
486 const auto list_lock
= lttng::sessiond::lock_session_list();
489 * Update the newly registered applications's
492 update_agent_app(new_app
);
494 ret
= agent_send_registration_done(new_app
);
496 agent_destroy_app(new_app
);
497 /* Removing from the poll set. */
498 ret
= lttng_poll_del(&events
, new_app_socket_fd
);
505 /* Publish the new agent app. */
506 agent_add_app(new_app
);
507 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
508 /* Removing from the poll set */
509 ret
= lttng_poll_del(&events
, pollfd
);
513 agent_destroy_app_by_sock(pollfd
);
515 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
522 /* Whatever happens, try to delete it and exit. */
523 (void) lttng_poll_del(&events
, reg_sock
->fd
);
525 destroy_tcp_socket(reg_sock
);
527 lttng_poll_clean(&events
);
529 uatomic_set(&agent_tracing_enabled
, 0);
530 DBG("Cleaning up and stopping.");
531 rcu_thread_offline();
532 rcu_unregister_thread();
536 static bool shutdown_agent_management_thread(void *data
)
538 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
539 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
541 return notify_thread_pipe(write_fd
) == 1;
544 static void cleanup_agent_management_thread(void *data
)
546 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
548 lttng_pipe_destroy(notifiers
->quit_pipe
);
549 sem_destroy(¬ifiers
->ready
);
553 bool launch_agent_management_thread()
555 struct thread_notifiers
*notifiers
;
556 struct lttng_thread
*thread
;
558 notifiers
= zmalloc
<thread_notifiers
>();
563 sem_init(¬ifiers
->ready
, 0, 0);
564 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
565 if (!notifiers
->quit_pipe
) {
568 thread
= lttng_thread_create("Agent management",
569 thread_agent_management
,
570 shutdown_agent_management_thread
,
571 cleanup_agent_management_thread
,
576 wait_until_thread_is_ready(notifiers
);
577 lttng_thread_put(thread
);
580 cleanup_agent_management_thread(notifiers
);