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/uri.hpp>
22 #include <common/utils.hpp>
25 struct thread_notifiers
{
26 struct lttng_pipe
*quit_pipe
;
32 enum lttng_domain_type domain
;
35 struct agent_protocol_version
{
36 unsigned int major
, minor
;
39 int agent_tracing_enabled
= -1;
42 * Note that there is not port here. It's set after this URI is parsed so we
43 * can let the user define a custom one. However, localhost is ALWAYS the
44 * default listening address.
46 const char *default_reg_uri
= "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
50 * Update agent application using the given socket. This is done just after
51 * registration was successful.
53 * This will acquire the various sessions' lock; none must be held by the
55 * The caller must hold the session list lock.
57 static void update_agent_app(const struct agent_app
*app
)
59 struct ltt_session
*session
, *stmp
;
60 struct ltt_session_list
*list
;
61 struct agent
*trigger_agent
;
62 struct lttng_ht_iter iter
;
64 list
= session_get_list();
67 cds_list_for_each_entry_safe (session
, stmp
, &list
->head
, list
) {
68 if (!session_get(session
)) {
72 session_lock(session
);
73 if (session
->ust_session
) {
74 const struct agent
*agt
;
77 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
79 agent_update(agt
, app
);
83 session_unlock(session
);
89 * We are protected against the addition of new events by the session
90 * list lock being held.
92 cds_lfht_for_each_entry (
93 the_trigger_agents_ht_by_domain
->ht
, &iter
.iter
, trigger_agent
, node
.node
) {
94 agent_update(trigger_agent
, app
);
100 * Create and init socket from uri.
102 static struct lttcomm_sock
*init_tcp_socket()
105 struct lttng_uri
*uri
= nullptr;
106 struct lttcomm_sock
*sock
= nullptr;
108 bool bind_succeeded
= false;
111 * This should never fail since the URI is hardcoded and the port is set
112 * before this thread is launched.
114 ret
= uri_parse(default_reg_uri
, &uri
);
116 LTTNG_ASSERT(the_config
.agent_tcp_port
.begin
> 0);
117 uri
->port
= the_config
.agent_tcp_port
.begin
;
119 sock
= lttcomm_alloc_sock_from_uri(uri
);
121 if (sock
== nullptr) {
122 ERR("agent allocating TCP socket");
126 ret
= lttcomm_create_sock(sock
);
131 for (port
= the_config
.agent_tcp_port
.begin
; port
<= the_config
.agent_tcp_port
.end
;
133 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
135 ERR("Failed to set port %u on socket", port
);
138 DBG3("Trying to bind on port %u", port
);
139 ret
= sock
->ops
->bind(sock
);
141 bind_succeeded
= true;
145 if (errno
== EADDRINUSE
) {
146 DBG("Failed to bind to port %u since it is already in use", port
);
148 PERROR("Failed to bind to port %u", port
);
153 if (!bind_succeeded
) {
154 if (the_config
.agent_tcp_port
.begin
== the_config
.agent_tcp_port
.end
) {
155 WARN("Another process is already using the agent port %i. "
156 "Agent support will be deactivated.",
157 the_config
.agent_tcp_port
.begin
);
160 WARN("All ports in the range [%i, %i] are already in use. "
161 "Agent support will be deactivated.",
162 the_config
.agent_tcp_port
.begin
,
163 the_config
.agent_tcp_port
.end
);
168 ret
= sock
->ops
->listen(sock
, -1);
173 DBG("Listening on TCP port %u and socket %d", port
, sock
->fd
);
179 lttcomm_destroy_sock(sock
);
185 * Close and destroy the given TCP socket.
187 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
194 ret
= lttcomm_sock_get_port(sock
, &port
);
196 ERR("Failed to get port of agent TCP socket");
200 DBG3("Destroy TCP socket on port %" PRIu16
, port
);
202 /* This will return gracefully if fd is invalid. */
203 sock
->ops
->close(sock
);
204 lttcomm_destroy_sock(sock
);
207 static const char *domain_type_str(enum lttng_domain_type domain_type
)
209 switch (domain_type
) {
210 case LTTNG_DOMAIN_NONE
:
212 case LTTNG_DOMAIN_KERNEL
:
214 case LTTNG_DOMAIN_UST
:
216 case LTTNG_DOMAIN_JUL
:
218 case LTTNG_DOMAIN_LOG4J
:
220 case LTTNG_DOMAIN_PYTHON
:
227 static bool is_agent_protocol_version_supported(const struct agent_protocol_version
*version
)
229 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
230 version
->minor
== AGENT_MINOR_VERSION
;
233 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
237 AGENT_MINOR_VERSION
);
244 * Handle a new agent connection on the registration socket.
246 * Returns 0 on success, or else a negative errno value.
247 * On success, the resulting socket is returned through `agent_app_socket`
248 * and the application's reported id is updated through `agent_app_id`.
250 static int accept_agent_connection(struct lttcomm_sock
*reg_sock
,
251 struct agent_app_id
*agent_app_id
,
252 struct lttcomm_sock
**agent_app_socket
)
255 struct agent_protocol_version agent_version
;
257 struct agent_register_msg msg
;
258 struct lttcomm_sock
*new_sock
;
260 LTTNG_ASSERT(reg_sock
);
262 new_sock
= reg_sock
->ops
->accept(reg_sock
);
268 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
269 if (size
< sizeof(msg
)) {
271 PERROR("Failed to register new agent application");
272 } else if (size
!= 0) {
273 ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd",
277 DBG("Failed to register new agent application: connection closed");
280 goto error_close_socket
;
283 agent_version
= (struct agent_protocol_version
){
284 be32toh(msg
.major_version
),
285 be32toh(msg
.minor_version
),
288 /* Test communication protocol version of the registering agent. */
289 if (!is_agent_protocol_version_supported(&agent_version
)) {
291 goto error_close_socket
;
294 *agent_app_id
= (struct agent_app_id
){
295 .pid
= (pid_t
) be32toh(msg
.pid
),
296 .domain
= (lttng_domain_type
) be32toh(msg
.domain
),
299 DBG2("New registration for agent application: pid = %ld, domain = %s, socket fd = %d",
300 (long) agent_app_id
->pid
,
301 domain_type_str(agent_app_id
->domain
),
304 *agent_app_socket
= new_sock
;
310 new_sock
->ops
->close(new_sock
);
311 lttcomm_destroy_sock(new_sock
);
316 bool agent_tracing_is_enabled()
320 enabled
= uatomic_read(&agent_tracing_enabled
);
321 LTTNG_ASSERT(enabled
!= -1);
326 * Write agent TCP port using the rundir.
328 static int write_agent_port(uint16_t port
)
330 return utils_create_pid_file((pid_t
) port
, the_config
.agent_port_file_path
.value
);
333 static void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
335 DBG("Marking agent management thread as ready");
336 sem_post(¬ifiers
->ready
);
339 static void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
341 DBG("Waiting for agent management thread to be ready");
342 sem_wait(¬ifiers
->ready
);
343 DBG("Agent management thread is ready");
347 * This thread manage application notify communication.
349 static void *thread_agent_management(void *data
)
353 struct lttng_poll_event events
;
354 struct lttcomm_sock
*reg_sock
;
355 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
356 const auto thread_quit_pipe_fd
= lttng_pipe_get_readfd(notifiers
->quit_pipe
);
358 DBG("Manage agent application registration.");
360 rcu_register_thread();
363 /* Agent initialization call MUST be called before starting the thread. */
364 LTTNG_ASSERT(the_agent_apps_ht_by_sock
);
366 /* Create pollset with size 2, quit pipe and registration socket. */
367 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
369 goto error_poll_create
;
372 ret
= lttng_poll_add(&events
, thread_quit_pipe_fd
, LPOLLIN
);
374 goto error_tcp_socket
;
377 reg_sock
= init_tcp_socket();
381 ret
= lttcomm_sock_get_port(reg_sock
, &port
);
382 LTTNG_ASSERT(ret
== 0);
384 ret
= write_agent_port(port
);
386 ERR("Failed to create agent port file: agent tracing will be unavailable");
387 /* Don't prevent the launch of the sessiond on error. */
388 mark_thread_as_ready(notifiers
);
392 /* Don't prevent the launch of the sessiond on error. */
393 mark_thread_as_ready(notifiers
);
394 goto error_tcp_socket
;
398 * Signal that the agent thread is ready. The command thread
399 * may start to query whether or not agent tracing is enabled.
401 uatomic_set(&agent_tracing_enabled
, 1);
402 mark_thread_as_ready(notifiers
);
404 /* Add TCP socket to the poll set. */
405 ret
= lttng_poll_add(&events
, reg_sock
->fd
, LPOLLIN
| LPOLLRDHUP
);
411 DBG3("Manage agent polling");
413 /* Inifinite blocking call, waiting for transmission */
415 ret
= lttng_poll_wait(&events
, -1);
416 DBG3("Manage agent return from poll on %d fds", LTTNG_POLL_GETNB(&events
));
419 * Restart interrupted system call.
421 if (errno
== EINTR
) {
427 DBG3("%d fd ready", nb_fd
);
429 for (i
= 0; i
< nb_fd
; i
++) {
430 /* Fetch once the poll data */
431 const auto revents
= LTTNG_POLL_GETEV(&events
, i
);
432 const auto pollfd
= LTTNG_POLL_GETFD(&events
, i
);
434 /* Activity on thread quit pipe, exiting. */
435 if (pollfd
== thread_quit_pipe_fd
) {
436 DBG("Activity on thread quit pipe");
440 /* Activity on the registration socket. */
441 if (revents
& LPOLLIN
) {
442 struct agent_app_id new_app_id
;
443 struct agent_app
*new_app
= nullptr;
444 struct lttcomm_sock
*new_app_socket
;
445 int new_app_socket_fd
;
447 LTTNG_ASSERT(pollfd
== reg_sock
->fd
);
449 ret
= accept_agent_connection(
450 reg_sock
, &new_app_id
, &new_app_socket
);
452 /* Errors are already logged. */
457 * new_app_socket's ownership has been
458 * transferred to the new agent app.
460 new_app
= agent_create_app(
461 new_app_id
.pid
, new_app_id
.domain
, new_app_socket
);
463 new_app_socket
->ops
->close(new_app_socket
);
466 new_app_socket_fd
= new_app_socket
->fd
;
467 new_app_socket
= nullptr;
470 * Since this is a command socket (write then
471 * read), only add poll error event to only
474 ret
= lttng_poll_add(&events
, new_app_socket_fd
, LPOLLRDHUP
);
476 agent_destroy_app(new_app
);
481 * Prevent sessions from being modified while
482 * the agent application's configuration is
488 * Update the newly registered applications's
491 update_agent_app(new_app
);
493 ret
= agent_send_registration_done(new_app
);
495 agent_destroy_app(new_app
);
496 /* Removing from the poll set. */
497 ret
= lttng_poll_del(&events
, new_app_socket_fd
);
499 session_unlock_list();
505 /* Publish the new agent app. */
506 agent_add_app(new_app
);
508 session_unlock_list();
509 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
510 /* Removing from the poll set */
511 ret
= lttng_poll_del(&events
, pollfd
);
515 agent_destroy_app_by_sock(pollfd
);
517 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
524 /* Whatever happens, try to delete it and exit. */
525 (void) lttng_poll_del(&events
, reg_sock
->fd
);
527 destroy_tcp_socket(reg_sock
);
529 lttng_poll_clean(&events
);
531 uatomic_set(&agent_tracing_enabled
, 0);
532 DBG("Cleaning up and stopping.");
533 rcu_thread_offline();
534 rcu_unregister_thread();
538 static bool shutdown_agent_management_thread(void *data
)
540 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
541 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
543 return notify_thread_pipe(write_fd
) == 1;
546 static void cleanup_agent_management_thread(void *data
)
548 struct thread_notifiers
*notifiers
= (thread_notifiers
*) data
;
550 lttng_pipe_destroy(notifiers
->quit_pipe
);
551 sem_destroy(¬ifiers
->ready
);
555 bool launch_agent_management_thread()
557 struct thread_notifiers
*notifiers
;
558 struct lttng_thread
*thread
;
560 notifiers
= zmalloc
<thread_notifiers
>();
565 sem_init(¬ifiers
->ready
, 0, 0);
566 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
567 if (!notifiers
->quit_pipe
) {
570 thread
= lttng_thread_create("Agent management",
571 thread_agent_management
,
572 shutdown_agent_management_thread
,
573 cleanup_agent_management_thread
,
578 wait_until_thread_is_ready(notifiers
);
579 lttng_thread_put(thread
);
582 cleanup_agent_management_thread(notifiers
);