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 static int agent_tracing_enabled
= -1;
39 * Note that there is not port here. It's set after this URI is parsed so we
40 * can let the user define a custom one. However, localhost is ALWAYS the
41 * default listening address.
43 static const char *default_reg_uri
=
44 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
47 * Update agent application using the given socket. This is done just after
48 * registration was successful.
50 * This is a quite heavy call in terms of locking since the session list lock
51 * AND session lock are acquired.
53 static void update_agent_app(struct agent_app
*app
)
55 struct ltt_session
*session
, *stmp
;
56 struct ltt_session_list
*list
;
58 list
= session_get_list();
62 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
63 if (!session_get(session
)) {
67 session_lock(session
);
68 if (session
->ust_session
) {
72 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
74 agent_update(agt
, app
->sock
->fd
);
78 session_unlock(session
);
81 session_unlock_list();
85 * Create and init socket from uri.
87 static struct lttcomm_sock
*init_tcp_socket(void)
90 struct lttng_uri
*uri
= NULL
;
91 struct lttcomm_sock
*sock
= NULL
;
93 bool bind_succeeded
= false;
96 * This should never fail since the URI is hardcoded and the port is set
97 * before this thread is launched.
99 ret
= uri_parse(default_reg_uri
, &uri
);
101 assert(config
.agent_tcp_port
.begin
> 0);
102 uri
->port
= config
.agent_tcp_port
.begin
;
104 sock
= lttcomm_alloc_sock_from_uri(uri
);
107 ERR("[agent-thread] agent allocating TCP socket");
111 ret
= lttcomm_create_sock(sock
);
116 for (port
= config
.agent_tcp_port
.begin
;
117 port
<= config
.agent_tcp_port
.end
; port
++) {
118 ret
= lttcomm_sock_set_port(sock
, (uint16_t) port
);
120 ERR("[agent-thread] Failed to set port %u on socket",
124 DBG3("[agent-thread] Trying to bind on port %u", port
);
125 ret
= sock
->ops
->bind(sock
);
127 bind_succeeded
= true;
131 if (errno
== EADDRINUSE
) {
132 DBG("Failed to bind to port %u since it is already in use",
135 PERROR("Failed to bind to port %u", port
);
140 if (!bind_succeeded
) {
141 if (config
.agent_tcp_port
.begin
== config
.agent_tcp_port
.end
) {
142 WARN("Another process is already using the agent port %i. "
143 "Agent support will be deactivated.",
144 config
.agent_tcp_port
.begin
);
147 WARN("All ports in the range [%i, %i] are already in use. "
148 "Agent support will be deactivated.",
149 config
.agent_tcp_port
.begin
,
150 config
.agent_tcp_port
.end
);
155 ret
= sock
->ops
->listen(sock
, -1);
160 DBG("[agent-thread] Listening on TCP port %u and socket %d",
167 lttcomm_destroy_sock(sock
);
173 * Close and destroy the given TCP socket.
175 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
182 ret
= lttcomm_sock_get_port(sock
, &port
);
184 ERR("[agent-thread] Failed to get port of agent TCP socket");
188 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16
,
191 /* This will return gracefully if fd is invalid. */
192 sock
->ops
->close(sock
);
193 lttcomm_destroy_sock(sock
);
197 * Handle a new agent registration using the reg socket. After that, a new
198 * agent application is added to the global hash table and attach to an UST app
199 * object. If r_app is not NULL, the created app is set to the pointer.
201 * Return the new FD created upon accept() on success or else a negative errno
204 static int handle_registration(struct lttcomm_sock
*reg_sock
,
205 struct agent_app
**r_app
)
209 uint32_t major_version
, minor_version
;
211 enum lttng_domain_type domain
;
212 struct agent_app
*app
;
213 struct agent_register_msg msg
;
214 struct lttcomm_sock
*new_sock
;
218 new_sock
= reg_sock
->ops
->accept(reg_sock
);
224 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
225 if (size
< sizeof(msg
)) {
229 domain
= be32toh(msg
.domain
);
230 pid
= be32toh(msg
.pid
);
231 major_version
= be32toh(msg
.major_version
);
232 minor_version
= be32toh(msg
.minor_version
);
234 /* Test communication protocol version of the registring agent. */
235 if (major_version
!= AGENT_MAJOR_VERSION
) {
239 if (minor_version
!= AGENT_MINOR_VERSION
) {
244 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
245 pid
, domain
, new_sock
->fd
);
247 app
= agent_create_app(pid
, domain
, new_sock
);
254 * Add before assigning the socket value to the UST app so it can be found
260 * We don't need to attach the agent app to the app. If we ever do so, we
261 * should consider both registration order of agent before app and app
272 new_sock
->ops
->close(new_sock
);
273 lttcomm_destroy_sock(new_sock
);
278 bool agent_tracing_is_enabled(void)
282 enabled
= uatomic_read(&agent_tracing_enabled
);
283 assert(enabled
!= -1);
288 * Write agent TCP port using the rundir.
290 static int write_agent_port(uint16_t port
)
292 return utils_create_pid_file((pid_t
) port
,
293 config
.agent_port_file_path
.value
);
297 * This thread manage application notify communication.
299 static void *thread_agent_management(void *data
)
302 uint32_t revents
, nb_fd
;
303 struct lttng_poll_event events
;
304 struct lttcomm_sock
*reg_sock
;
305 struct lttng_pipe
*quit_pipe
= data
;
306 const int quit_pipe_read_fd
= lttng_pipe_get_readfd(quit_pipe
);
308 DBG("[agent-thread] Manage agent application registration.");
310 rcu_register_thread();
313 /* Agent initialization call MUST be called before starting the thread. */
314 assert(agent_apps_ht_by_sock
);
316 /* Create pollset with size 2, quit pipe and registration socket. */
317 ret
= lttng_poll_create(&events
, 2, LTTNG_CLOEXEC
);
319 goto error_poll_create
;
322 ret
= lttng_poll_add(&events
, quit_pipe_read_fd
,
325 goto error_tcp_socket
;
328 reg_sock
= init_tcp_socket();
332 assert(lttcomm_sock_get_port(reg_sock
, &port
) == 0);
334 ret
= write_agent_port(port
);
336 ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable");
337 /* Don't prevent the launch of the sessiond on error. */
338 sessiond_notify_ready();
342 /* Don't prevent the launch of the sessiond on error. */
343 sessiond_notify_ready();
344 goto error_tcp_socket
;
348 * Signal that the agent thread is ready. The command thread
349 * may start to query whether or not agent tracing is enabled.
351 uatomic_set(&agent_tracing_enabled
, 1);
352 sessiond_notify_ready();
354 /* Add TCP socket to poll set. */
355 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
356 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
362 DBG3("[agent-thread] Manage agent polling");
364 /* Inifinite blocking call, waiting for transmission */
366 ret
= lttng_poll_wait(&events
, -1);
367 DBG3("[agent-thread] Manage agent return from poll on %d fds",
368 LTTNG_POLL_GETNB(&events
));
371 * Restart interrupted system call.
373 if (errno
== EINTR
) {
379 DBG3("[agent-thread] %d fd ready", nb_fd
);
381 for (i
= 0; i
< nb_fd
; i
++) {
382 /* Fetch once the poll data */
383 revents
= LTTNG_POLL_GETEV(&events
, i
);
384 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
387 /* No activity for this FD (poll implementation). */
391 /* Thread quit pipe has been closed. Killing thread. */
392 if (pollfd
== quit_pipe_read_fd
) {
396 if (revents
& LPOLLIN
) {
398 struct agent_app
*app
= NULL
;
400 assert(pollfd
== reg_sock
->fd
);
401 new_fd
= handle_registration(reg_sock
, &app
);
405 /* Should not have a NULL app on success. */
409 * Since this is a command socket (write then read),
410 * only add poll error event to only detect shutdown.
412 ret
= lttng_poll_add(&events
, new_fd
,
413 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
415 agent_destroy_app_by_sock(new_fd
);
419 /* Update newly registered app. */
420 update_agent_app(app
);
422 /* On failure, the poll will detect it and clean it up. */
423 ret
= agent_send_registration_done(app
);
425 /* Removing from the poll set */
426 ret
= lttng_poll_del(&events
, new_fd
);
430 agent_destroy_app_by_sock(new_fd
);
433 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
434 /* Removing from the poll set */
435 ret
= lttng_poll_del(&events
, pollfd
);
439 agent_destroy_app_by_sock(pollfd
);
441 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
448 /* Whatever happens, try to delete it and exit. */
449 (void) lttng_poll_del(&events
, reg_sock
->fd
);
451 destroy_tcp_socket(reg_sock
);
453 lttng_poll_clean(&events
);
455 uatomic_set(&agent_tracing_enabled
, 0);
456 DBG("[agent-thread] Cleaning up and stopping.");
457 rcu_thread_offline();
458 rcu_unregister_thread();
462 static bool shutdown_agent_management_thread(void *data
)
464 struct lttng_pipe
*quit_pipe
= data
;
465 const int write_fd
= lttng_pipe_get_writefd(quit_pipe
);
467 return notify_thread_pipe(write_fd
) == 1;
470 static void cleanup_agent_management_thread(void *data
)
472 struct lttng_pipe
*quit_pipe
= data
;
474 lttng_pipe_destroy(quit_pipe
);
477 bool launch_agent_management_thread(void)
479 struct lttng_pipe
*quit_pipe
;
480 struct lttng_thread
*thread
;
482 quit_pipe
= lttng_pipe_open(FD_CLOEXEC
);
486 thread
= lttng_thread_create("Agent management",
487 thread_agent_management
,
488 shutdown_agent_management_thread
,
489 cleanup_agent_management_thread
,
495 lttng_thread_put(thread
);
498 cleanup_agent_management_thread(quit_pipe
);