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
;
63 list
= session_get_list();
66 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
67 if (!session_get(session
)) {
71 session_lock(session
);
72 if (session
->ust_session
) {
73 const struct agent
*agt
;
76 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
78 agent_update(agt
, app
);
82 session_unlock(session
);
88 * Create and init socket from uri.
90 static struct lttcomm_sock
*init_tcp_socket(void)
93 struct lttng_uri
*uri
= NULL
;
94 struct lttcomm_sock
*sock
= NULL
;
96 bool bind_succeeded
= false;
99 * This should never fail since the URI is hardcoded and the port is set
100 * before this thread is launched.
102 ret
= uri_parse(default_reg_uri
, &uri
);
104 assert(config
.agent_tcp_port
.begin
> 0);
105 uri
->port
= config
.agent_tcp_port
.begin
;
107 sock
= lttcomm_alloc_sock_from_uri(uri
);
110 ERR("[agent-thread] agent allocating TCP socket");
114 ret
= lttcomm_create_sock(sock
);
119 for (port
= config
.agent_tcp_port
.begin
;
120 port
<= config
.agent_tcp_port
.end
; port
++) {
121 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
123 ERR("[agent-thread] Failed to set port %u on socket",
127 DBG3("[agent-thread] Trying to bind on port %u", port
);
128 ret
= sock
->ops
->bind(sock
);
130 bind_succeeded
= true;
134 if (errno
== EADDRINUSE
) {
135 DBG("Failed to bind to port %u since it is already in use",
138 PERROR("Failed to bind to port %u", port
);
143 if (!bind_succeeded
) {
144 if (config
.agent_tcp_port
.begin
== config
.agent_tcp_port
.end
) {
145 WARN("Another process is already using the agent port %i. "
146 "Agent support will be deactivated.",
147 config
.agent_tcp_port
.begin
);
150 WARN("All ports in the range [%i, %i] are already in use. "
151 "Agent support will be deactivated.",
152 config
.agent_tcp_port
.begin
,
153 config
.agent_tcp_port
.end
);
158 ret
= sock
->ops
->listen(sock
, -1);
163 DBG("[agent-thread] Listening on TCP port %u and socket %d",
170 lttcomm_destroy_sock(sock
);
176 * Close and destroy the given TCP socket.
178 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
185 ret
= lttcomm_sock_get_port(sock
, &port
);
187 ERR("[agent-thread] Failed to get port of agent TCP socket");
191 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16
,
194 /* This will return gracefully if fd is invalid. */
195 sock
->ops
->close(sock
);
196 lttcomm_destroy_sock(sock
);
199 static const char *domain_type_str(enum lttng_domain_type domain_type
)
201 switch (domain_type
) {
202 case LTTNG_DOMAIN_NONE
:
204 case LTTNG_DOMAIN_KERNEL
:
206 case LTTNG_DOMAIN_UST
:
208 case LTTNG_DOMAIN_JUL
:
210 case LTTNG_DOMAIN_LOG4J
:
212 case LTTNG_DOMAIN_PYTHON
:
219 static bool is_agent_protocol_version_supported(
220 const struct agent_protocol_version
*version
)
222 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
223 version
->minor
== AGENT_MINOR_VERSION
;
226 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
227 version
->major
, version
->minor
,
228 AGENT_MAJOR_VERSION
, AGENT_MINOR_VERSION
);
235 * Handle a new agent connection on the registration socket.
237 * Returns 0 on success, or else a negative errno value.
238 * On success, the resulting socket is returned through `agent_app_socket`
239 * and the application's reported id is updated through `agent_app_id`.
241 static int accept_agent_connection(
242 struct lttcomm_sock
*reg_sock
,
243 struct agent_app_id
*agent_app_id
,
244 struct lttcomm_sock
**agent_app_socket
)
247 struct agent_protocol_version agent_version
;
249 struct agent_register_msg msg
;
250 struct lttcomm_sock
*new_sock
;
254 new_sock
= reg_sock
->ops
->accept(reg_sock
);
260 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
261 if (size
< sizeof(msg
)) {
263 PERROR("Failed to register new agent application");
264 } else if (size
!= 0) {
265 ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd",
268 DBG("Failed to register new agent application: connection closed");
271 goto error_close_socket
;
274 agent_version
= (struct agent_protocol_version
) {
275 be32toh(msg
.major_version
),
276 be32toh(msg
.minor_version
),
279 /* Test communication protocol version of the registering agent. */
280 if (!is_agent_protocol_version_supported(&agent_version
)) {
282 goto error_close_socket
;
285 *agent_app_id
= (struct agent_app_id
) {
286 .domain
= (enum lttng_domain_type
) be32toh(msg
.domain
),
287 .pid
= (pid_t
) be32toh(msg
.pid
),
290 DBG2("New registration for agent application: pid = %ld, domain = %s, socket fd = %d",
291 (long) agent_app_id
->pid
,
292 domain_type_str(agent_app_id
->domain
), new_sock
->fd
);
294 *agent_app_socket
= new_sock
;
300 new_sock
->ops
->close(new_sock
);
301 lttcomm_destroy_sock(new_sock
);
306 bool agent_tracing_is_enabled(void)
310 enabled
= uatomic_read(&agent_tracing_enabled
);
311 assert(enabled
!= -1);
316 * Write agent TCP port using the rundir.
318 static int write_agent_port(uint16_t port
)
320 return utils_create_pid_file((pid_t
) port
,
321 config
.agent_port_file_path
.value
);
325 void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
327 DBG("Marking agent management thread as ready");
328 sem_post(¬ifiers
->ready
);
332 void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
334 DBG("Waiting for agent management thread to be ready");
335 sem_wait(¬ifiers
->ready
);
336 DBG("Agent management thread is ready");
340 * This thread manage application notify communication.
342 static void *thread_agent_management(void *data
)
345 uint32_t revents
, nb_fd
;
346 struct lttng_poll_event events
;
347 struct lttcomm_sock
*reg_sock
;
348 struct thread_notifiers
*notifiers
= data
;
349 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
350 notifiers
->quit_pipe
);
352 DBG("[agent-thread] Manage agent application registration.");
354 rcu_register_thread();
357 /* Agent initialization call MUST be called before starting the thread. */
358 assert(agent_apps_ht_by_sock
);
360 /* Create pollset with size 2, quit pipe and registration socket. */
361 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
363 goto error_poll_create
;
366 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
369 goto error_tcp_socket
;
372 reg_sock
= init_tcp_socket();
376 assert(lttcomm_sock_get_port(reg_sock
, &port
) == 0);
378 ret
= write_agent_port(port
);
380 ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable");
381 /* Don't prevent the launch of the sessiond on error. */
382 mark_thread_as_ready(notifiers
);
386 /* Don't prevent the launch of the sessiond on error. */
387 mark_thread_as_ready(notifiers
);
388 goto error_tcp_socket
;
392 * Signal that the agent thread is ready. The command thread
393 * may start to query whether or not agent tracing is enabled.
395 uatomic_set(&agent_tracing_enabled
, 1);
396 mark_thread_as_ready(notifiers
);
398 /* Add TCP socket to the poll set. */
399 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
400 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
406 DBG3("[agent-thread] Manage agent polling");
408 /* Inifinite blocking call, waiting for transmission */
410 ret
= lttng_poll_wait(&events
, -1);
411 DBG3("[agent-thread] Manage agent return from poll on %d fds",
412 LTTNG_POLL_GETNB(&events
));
415 * Restart interrupted system call.
417 if (errno
== EINTR
) {
423 DBG3("[agent-thread] %d fd ready", nb_fd
);
425 for (i
= 0; i
< nb_fd
; i
++) {
426 /* Fetch once the poll data */
427 revents
= LTTNG_POLL_GETEV(&events
, i
);
428 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
430 /* Thread quit pipe has been closed. Killing thread. */
431 if (pollfd
== quit_pipe_read_fd
) {
435 /* Activity on the registration socket. */
436 if (revents
& LPOLLIN
) {
437 struct agent_app_id new_app_id
;
438 struct agent_app
*new_app
= NULL
;
439 struct lttcomm_sock
*new_app_socket
;
440 int new_app_socket_fd
;
442 assert(pollfd
== reg_sock
->fd
);
444 ret
= accept_agent_connection(
445 reg_sock
, &new_app_id
, &new_app_socket
);
447 /* Errors are already logged. */
452 * new_app_socket's ownership has been
453 * transferred to the new agent app.
455 new_app
= agent_create_app(new_app_id
.pid
,
459 new_app_socket
->ops
->close(
463 new_app_socket_fd
= new_app_socket
->fd
;
464 new_app_socket
= NULL
;
467 * Since this is a command socket (write then
468 * read), only add poll error event to only
471 ret
= lttng_poll_add(&events
, new_app_socket_fd
,
472 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
474 agent_destroy_app(new_app
);
479 * Prevent sessions from being modified while
480 * the agent application's configuration is
486 * Update the newly registered applications's
489 update_agent_app(new_app
);
491 ret
= agent_send_registration_done(new_app
);
493 agent_destroy_app(new_app
);
494 /* Removing from the poll set. */
495 ret
= lttng_poll_del(&events
,
498 session_unlock_list();
504 /* Publish the new agent app. */
505 agent_add_app(new_app
);
507 session_unlock_list();
508 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
509 /* Removing from the poll set */
510 ret
= lttng_poll_del(&events
, pollfd
);
514 agent_destroy_app_by_sock(pollfd
);
516 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
523 /* Whatever happens, try to delete it and exit. */
524 (void) lttng_poll_del(&events
, reg_sock
->fd
);
526 destroy_tcp_socket(reg_sock
);
528 lttng_poll_clean(&events
);
530 uatomic_set(&agent_tracing_enabled
, 0);
531 DBG("[agent-thread] Cleaning up and stopping.");
532 rcu_thread_offline();
533 rcu_unregister_thread();
537 static bool shutdown_agent_management_thread(void *data
)
539 struct thread_notifiers
*notifiers
= data
;
540 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
542 return notify_thread_pipe(write_fd
) == 1;
545 static void cleanup_agent_management_thread(void *data
)
547 struct thread_notifiers
*notifiers
= data
;
549 lttng_pipe_destroy(notifiers
->quit_pipe
);
550 sem_destroy(¬ifiers
->ready
);
554 bool launch_agent_management_thread(void)
556 struct thread_notifiers
*notifiers
;
557 struct lttng_thread
*thread
;
559 notifiers
= zmalloc(sizeof(*notifiers
));
564 sem_init(¬ifiers
->ready
, 0, 0);
565 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
566 if (!notifiers
->quit_pipe
) {
569 thread
= lttng_thread_create("Agent management",
570 thread_agent_management
,
571 shutdown_agent_management_thread
,
572 cleanup_agent_management_thread
,
577 wait_until_thread_is_ready(notifiers
);
578 lttng_thread_put(thread
);
581 cleanup_agent_management_thread(notifiers
);