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
*session
, *stmp
;
63 struct ltt_session_list
*list
;
64 struct agent
*trigger_agent
;
65 struct lttng_ht_iter iter
;
67 list
= session_get_list();
70 cds_list_for_each_entry_safe (session
, stmp
, &list
->head
, list
) {
71 if (!session_get(session
)) {
75 session_lock(session
);
76 if (session
->ust_session
) {
77 const struct agent
*agt
;
79 lttng::urcu::read_lock_guard read_lock
;
80 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
82 agent_update(agt
, app
);
85 session_unlock(session
);
91 * We are protected against the addition of new events by the session
92 * list lock being held.
94 lttng::urcu::read_lock_guard read_lock
;
96 cds_lfht_for_each_entry (
97 the_trigger_agents_ht_by_domain
->ht
, &iter
.iter
, trigger_agent
, node
.node
) {
98 agent_update(trigger_agent
, app
);
104 * Create and init socket from uri.
106 static struct lttcomm_sock
*init_tcp_socket()
109 struct lttng_uri
*uri
= nullptr;
110 struct lttcomm_sock
*sock
= nullptr;
112 bool bind_succeeded
= false;
115 * This should never fail since the URI is hardcoded and the port is set
116 * before this thread is launched.
118 ret
= uri_parse(default_reg_uri
, &uri
);
120 LTTNG_ASSERT(the_config
.agent_tcp_port
.begin
> 0);
121 uri
->port
= the_config
.agent_tcp_port
.begin
;
123 sock
= lttcomm_alloc_sock_from_uri(uri
);
125 if (sock
== nullptr) {
126 ERR("agent allocating TCP socket");
130 ret
= lttcomm_create_sock(sock
);
135 for (port
= the_config
.agent_tcp_port
.begin
; port
<= the_config
.agent_tcp_port
.end
;
137 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
139 ERR("Failed to set port %u on socket", port
);
142 DBG3("Trying to bind on port %u", port
);
143 ret
= sock
->ops
->bind(sock
);
145 bind_succeeded
= true;
149 if (errno
== EADDRINUSE
) {
150 DBG("Failed to bind to port %u since it is already in use", port
);
152 PERROR("Failed to bind to port %u", port
);
157 if (!bind_succeeded
) {
158 if (the_config
.agent_tcp_port
.begin
== 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", port
, sock
->fd
);
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("Failed to get port of agent TCP socket");
204 DBG3("Destroy TCP socket on port %" PRIu16
, port
);
206 /* This will return gracefully if fd is invalid. */
207 sock
->ops
->close(sock
);
208 lttcomm_destroy_sock(sock
);
211 static const char *domain_type_str(enum lttng_domain_type domain_type
)
213 switch (domain_type
) {
214 case LTTNG_DOMAIN_NONE
:
216 case LTTNG_DOMAIN_KERNEL
:
218 case LTTNG_DOMAIN_UST
:
220 case LTTNG_DOMAIN_JUL
:
222 case LTTNG_DOMAIN_LOG4J
:
224 case LTTNG_DOMAIN_PYTHON
:
231 static bool is_agent_protocol_version_supported(const struct agent_protocol_version
*version
)
233 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
234 version
->minor
== AGENT_MINOR_VERSION
;
237 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
241 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(struct lttcomm_sock
*reg_sock
,
255 struct agent_app_id
*agent_app_id
,
256 struct lttcomm_sock
**agent_app_socket
)
259 struct agent_protocol_version agent_version
;
261 struct agent_register_msg msg
;
262 struct lttcomm_sock
*new_sock
;
264 LTTNG_ASSERT(reg_sock
);
266 new_sock
= reg_sock
->ops
->accept(reg_sock
);
272 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
273 if (size
< sizeof(msg
)) {
275 PERROR("Failed to register new agent application");
276 } else if (size
!= 0) {
277 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 .pid
= (pid_t
) be32toh(msg
.pid
),
300 .domain
= (lttng_domain_type
) be32toh(msg
.domain
),
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
),
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()
324 enabled
= uatomic_read(&agent_tracing_enabled
);
325 LTTNG_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((pid_t
) port
, the_config
.agent_port_file_path
.value
);
337 static void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
339 DBG("Marking agent management thread as ready");
340 sem_post(¬ifiers
->ready
);
343 static void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
345 DBG("Waiting for agent management thread to be ready");
346 sem_wait(¬ifiers
->ready
);
347 DBG("Agent management thread is ready");
351 * This thread manage application notify communication.
353 static void *thread_agent_management(void *data
)
357 struct lttng_poll_event events
;
358 struct lttcomm_sock
*reg_sock
;
359 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
360 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
362 DBG("Manage agent application registration.");
364 rcu_register_thread();
367 /* Agent initialization call MUST be called before starting the thread. */
368 LTTNG_ASSERT(the_agent_apps_ht_by_sock
);
370 /* Create pollset with size 2, quit pipe and registration socket. */
371 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
373 goto error_poll_create
;
376 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
378 goto error_tcp_socket
;
381 reg_sock
= init_tcp_socket();
385 ret
= lttcomm_sock_get_port(reg_sock
, &port
);
386 LTTNG_ASSERT(ret
== 0);
388 ret
= write_agent_port(port
);
390 ERR("Failed to create agent port file: agent tracing will be unavailable");
391 /* Don't prevent the launch of the sessiond on error. */
392 mark_thread_as_ready(notifiers
);
396 /* Don't prevent the launch of the sessiond on error. */
397 mark_thread_as_ready(notifiers
);
398 goto error_tcp_socket
;
402 * Signal that the agent thread is ready. The command thread
403 * may start to query whether or not agent tracing is enabled.
405 uatomic_set(&agent_tracing_enabled
, 1);
406 mark_thread_as_ready(notifiers
);
408 /* Add TCP socket to the poll set. */
409 ret
= lttng_poll_add(&events
, reg_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
415 DBG3("Manage agent polling");
417 /* Inifinite blocking call, waiting for transmission */
419 ret
= lttng_poll_wait(&events
, -1);
420 DBG3("Manage agent return from poll on %d fds", LTTNG_POLL_GETNB(&events
));
423 * Restart interrupted system call.
425 if (errno
== EINTR
) {
431 DBG3("%d fd ready", nb_fd
);
433 for (i
= 0; i
< nb_fd
; i
++) {
434 /* Fetch once the poll data */
435 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
436 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
438 /* Activity on thread quit pipe, exiting. */
439 if (pollfd
== thread_quit_pipe_fd
) {
440 DBG("Activity on thread quit pipe");
444 /* Activity on the registration socket. */
445 if (revents
& LPOLLIN
) {
446 struct agent_app_id new_app_id
;
447 struct agent_app
*new_app
= nullptr;
448 struct lttcomm_sock
*new_app_socket
;
449 int new_app_socket_fd
;
451 LTTNG_ASSERT(pollfd
== reg_sock
->fd
);
453 ret
= accept_agent_connection(
454 reg_sock
, &new_app_id
, &new_app_socket
);
456 /* Errors are already logged. */
461 * new_app_socket's ownership has been
462 * transferred to the new agent app.
464 new_app
= agent_create_app(
465 new_app_id
.pid
, new_app_id
.domain
, new_app_socket
);
467 new_app_socket
->ops
->close(new_app_socket
);
470 new_app_socket_fd
= new_app_socket
->fd
;
471 new_app_socket
= nullptr;
474 * Since this is a command socket (write then
475 * read), only add poll error event to only
478 ret
= lttng_poll_add(&events
, new_app_socket_fd
, LPOLLRDHUP
);
480 agent_destroy_app(new_app
);
485 * Prevent sessions from being modified while
486 * the agent application's configuration is
489 const auto list_lock
= lttng::sessiond::lock_session_list();
492 * Update the newly registered applications's
495 update_agent_app(new_app
);
497 ret
= agent_send_registration_done(new_app
);
499 agent_destroy_app(new_app
);
500 /* Removing from the poll set. */
501 ret
= lttng_poll_del(&events
, new_app_socket_fd
);
508 /* Publish the new agent app. */
509 agent_add_app(new_app
);
510 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
511 /* Removing from the poll set */
512 ret
= lttng_poll_del(&events
, pollfd
);
516 agent_destroy_app_by_sock(pollfd
);
518 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
525 /* Whatever happens, try to delete it and exit. */
526 (void) lttng_poll_del(&events
, reg_sock
->fd
);
528 destroy_tcp_socket(reg_sock
);
530 lttng_poll_clean(&events
);
532 uatomic_set(&agent_tracing_enabled
, 0);
533 DBG("Cleaning up and stopping.");
534 rcu_thread_offline();
535 rcu_unregister_thread();
539 static bool shutdown_agent_management_thread(void *data
)
541 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
542 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
544 return notify_thread_pipe(write_fd
) == 1;
547 static void cleanup_agent_management_thread(void *data
)
549 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
551 lttng_pipe_destroy(notifiers
->quit_pipe
);
552 sem_destroy(¬ifiers
->ready
);
556 bool launch_agent_management_thread()
558 struct thread_notifiers
*notifiers
;
559 struct lttng_thread
*thread
;
561 notifiers
= zmalloc
<thread_notifiers
>();
566 sem_init(¬ifiers
->ready
, 0, 0);
567 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
568 if (!notifiers
->quit_pipe
) {
571 thread
= lttng_thread_create("Agent management",
572 thread_agent_management
,
573 shutdown_agent_management_thread
,
574 cleanup_agent_management_thread
,
579 wait_until_thread_is_ready(notifiers
);
580 lttng_thread_put(thread
);
583 cleanup_agent_management_thread(notifiers
);