2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/uri.h>
24 #include <common/utils.h>
26 #include <common/compat/endian.h>
29 #include "agent-thread.h"
31 #include "lttng-sessiond.h"
36 struct thread_notifiers
{
37 struct lttng_pipe
*quit_pipe
;
43 enum lttng_domain_type domain
;
46 struct agent_protocol_version
{
47 unsigned int major
, minor
;
50 static int agent_tracing_enabled
= -1;
53 * Note that there is not port here. It's set after this URI is parsed so we
54 * can let the user define a custom one. However, localhost is ALWAYS the
55 * default listening address.
57 static const char *default_reg_uri
=
58 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
61 * Update agent application using the given socket. This is done just after
62 * registration was successful.
64 * This will acquire the various sessions' lock; none must be held by the
66 * The caller must hold the session list lock.
68 static void update_agent_app(const struct agent_app
*app
)
70 struct ltt_session
*session
, *stmp
;
71 struct ltt_session_list
*list
;
73 list
= session_get_list();
76 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
77 if (!session_get(session
)) {
81 session_lock(session
);
82 if (session
->ust_session
) {
83 const struct agent
*agt
;
86 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
88 agent_update(agt
, app
);
92 session_unlock(session
);
98 * Create and init socket from uri.
100 static struct lttcomm_sock
*init_tcp_socket(void)
103 struct lttng_uri
*uri
= NULL
;
104 struct lttcomm_sock
*sock
= NULL
;
106 bool bind_succeeded
= false;
109 * This should never fail since the URI is hardcoded and the port is set
110 * before this thread is launched.
112 ret
= uri_parse(default_reg_uri
, &uri
);
114 assert(config
.agent_tcp_port
.begin
> 0);
115 uri
->port
= config
.agent_tcp_port
.begin
;
117 sock
= lttcomm_alloc_sock_from_uri(uri
);
120 ERR("[agent-thread] agent allocating TCP socket");
124 ret
= lttcomm_create_sock(sock
);
129 for (port
= config
.agent_tcp_port
.begin
;
130 port
<= config
.agent_tcp_port
.end
; port
++) {
131 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
133 ERR("[agent-thread] Failed to set port %u on socket",
137 DBG3("[agent-thread] 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",
148 PERROR("Failed to bind to port %u", port
);
153 if (!bind_succeeded
) {
154 if (config
.agent_tcp_port
.begin
== config
.agent_tcp_port
.end
) {
155 WARN("Another process is already using the agent port %i. "
156 "Agent support will be deactivated.",
157 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 config
.agent_tcp_port
.begin
,
163 config
.agent_tcp_port
.end
);
168 ret
= sock
->ops
->listen(sock
, -1);
173 DBG("[agent-thread] Listening on TCP port %u and socket %d",
180 lttcomm_destroy_sock(sock
);
186 * Close and destroy the given TCP socket.
188 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
195 ret
= lttcomm_sock_get_port(sock
, &port
);
197 ERR("[agent-thread] Failed to get port of agent TCP socket");
201 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16
,
204 /* This will return gracefully if fd is invalid. */
205 sock
->ops
->close(sock
);
206 lttcomm_destroy_sock(sock
);
209 static const char *domain_type_str(enum lttng_domain_type domain_type
)
211 switch (domain_type
) {
212 case LTTNG_DOMAIN_NONE
:
214 case LTTNG_DOMAIN_KERNEL
:
216 case LTTNG_DOMAIN_UST
:
218 case LTTNG_DOMAIN_JUL
:
220 case LTTNG_DOMAIN_LOG4J
:
222 case LTTNG_DOMAIN_PYTHON
:
229 static bool is_agent_protocol_version_supported(
230 const struct agent_protocol_version
*version
)
232 const bool is_supported
= version
->major
== AGENT_MAJOR_VERSION
&&
233 version
->minor
== AGENT_MINOR_VERSION
;
236 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
237 version
->major
, version
->minor
,
238 AGENT_MAJOR_VERSION
, 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(
252 struct lttcomm_sock
*reg_sock
,
253 struct agent_app_id
*agent_app_id
,
254 struct lttcomm_sock
**agent_app_socket
)
257 struct agent_protocol_version agent_version
;
259 struct agent_register_msg msg
;
260 struct lttcomm_sock
*new_sock
;
264 new_sock
= reg_sock
->ops
->accept(reg_sock
);
270 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
271 if (size
< sizeof(msg
)) {
273 PERROR("Failed to register new agent application");
274 } else if (size
!= 0) {
275 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 .domain
= (enum lttng_domain_type
) be32toh(msg
.domain
),
297 .pid
= (pid_t
) be32toh(msg
.pid
),
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
), new_sock
->fd
);
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(void)
320 enabled
= uatomic_read(&agent_tracing_enabled
);
321 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
,
331 config
.agent_port_file_path
.value
);
335 void mark_thread_as_ready(struct thread_notifiers
*notifiers
)
337 DBG("Marking agent management thread as ready");
338 sem_post(¬ifiers
->ready
);
342 void wait_until_thread_is_ready(struct thread_notifiers
*notifiers
)
344 DBG("Waiting for agent management thread to be ready");
345 sem_wait(¬ifiers
->ready
);
346 DBG("Agent management thread is ready");
350 * This thread manage application notify communication.
352 static void *thread_agent_management(void *data
)
355 uint32_t revents
, nb_fd
;
356 struct lttng_poll_event events
;
357 struct lttcomm_sock
*reg_sock
;
358 struct thread_notifiers
*notifiers
= data
;
359 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(
360 notifiers
->quit_pipe
);
362 DBG("[agent-thread] Manage agent application registration.");
364 rcu_register_thread();
367 /* Agent initialization call MUST be called before starting the thread. */
368 assert(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
, quit_pipe_read_fd
,
379 goto error_tcp_socket
;
382 reg_sock
= init_tcp_socket();
386 assert(lttcomm_sock_get_port(reg_sock
, &port
) == 0);
388 ret
= write_agent_port(port
);
390 ERR("[agent-thread] 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
,
410 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
416 DBG3("[agent-thread] Manage agent polling");
418 /* Inifinite blocking call, waiting for transmission */
420 ret
= lttng_poll_wait(&events
, -1);
421 DBG3("[agent-thread] Manage agent return from poll on %d fds",
422 LTTNG_POLL_GETNB(&events
));
425 * Restart interrupted system call.
427 if (errno
== EINTR
) {
433 DBG3("[agent-thread] %d fd ready", nb_fd
);
435 for (i
= 0; i
< nb_fd
; i
++) {
436 /* Fetch once the poll data */
437 revents
= LTTNG_POLL_GETEV(&events
, i
);
438 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
440 /* Thread quit pipe has been closed. Killing thread. */
441 if (pollfd
== quit_pipe_read_fd
) {
445 /* Activity on the registration socket. */
446 if (revents
& LPOLLIN
) {
447 struct agent_app_id new_app_id
;
448 struct agent_app
*new_app
= NULL
;
449 struct lttcomm_sock
*new_app_socket
;
450 int new_app_socket_fd
;
452 assert(pollfd
== reg_sock
->fd
);
454 ret
= accept_agent_connection(
455 reg_sock
, &new_app_id
, &new_app_socket
);
457 /* Errors are already logged. */
462 * new_app_socket's ownership has been
463 * transferred to the new agent app.
465 new_app
= agent_create_app(new_app_id
.pid
,
469 new_app_socket
->ops
->close(
473 new_app_socket_fd
= new_app_socket
->fd
;
474 new_app_socket
= NULL
;
477 * Since this is a command socket (write then
478 * read), only add poll error event to only
481 ret
= lttng_poll_add(&events
, new_app_socket_fd
,
482 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
484 agent_destroy_app(new_app
);
489 * Prevent sessions from being modified while
490 * the agent application's configuration is
496 * Update the newly registered applications's
499 update_agent_app(new_app
);
501 ret
= agent_send_registration_done(new_app
);
503 agent_destroy_app(new_app
);
504 /* Removing from the poll set. */
505 ret
= lttng_poll_del(&events
,
508 session_unlock_list();
514 /* Publish the new agent app. */
515 agent_add_app(new_app
);
517 session_unlock_list();
518 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
519 /* Removing from the poll set */
520 ret
= lttng_poll_del(&events
, pollfd
);
524 agent_destroy_app_by_sock(pollfd
);
526 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
533 /* Whatever happens, try to delete it and exit. */
534 (void) lttng_poll_del(&events
, reg_sock
->fd
);
536 destroy_tcp_socket(reg_sock
);
538 lttng_poll_clean(&events
);
540 uatomic_set(&agent_tracing_enabled
, 0);
541 DBG("[agent-thread] Cleaning up and stopping.");
542 rcu_thread_offline();
543 rcu_unregister_thread();
547 static bool shutdown_agent_management_thread(void *data
)
549 struct thread_notifiers
*notifiers
= data
;
550 const int write_fd
= lttng_pipe_get_writefd(notifiers
->quit_pipe
);
552 return notify_thread_pipe(write_fd
) == 1;
555 static void cleanup_agent_management_thread(void *data
)
557 struct thread_notifiers
*notifiers
= data
;
559 lttng_pipe_destroy(notifiers
->quit_pipe
);
560 sem_destroy(¬ifiers
->ready
);
564 bool launch_agent_management_thread(void)
566 struct thread_notifiers
*notifiers
;
567 struct lttng_thread
*thread
;
569 notifiers
= zmalloc(sizeof(*notifiers
));
574 sem_init(¬ifiers
->ready
, 0, 0);
575 notifiers
->quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
576 if (!notifiers
->quit_pipe
) {
579 thread
= lttng_thread_create("Agent management",
580 thread_agent_management
,
581 shutdown_agent_management_thread
,
582 cleanup_agent_management_thread
,
587 wait_until_thread_is_ready(notifiers
);
588 lttng_thread_put(thread
);
591 cleanup_agent_management_thread(notifiers
);