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.
22 #include <common/common.h>
23 #include <common/sessiond-comm/sessiond-comm.h>
24 #include <common/uri.h>
25 #include <common/utils.h>
27 #include <common/compat/endian.h>
30 #include "agent-thread.h"
31 #include "lttng-sessiond.h"
36 * Note that there is not port here. It's set after this URI is parsed so we
37 * can let the user define a custom one. However, localhost is ALWAYS the
38 * default listening address.
40 static const char *default_reg_uri
=
41 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
44 * Update agent application using the given socket. This is done just after
45 * registration was successful.
47 * This is a quite heavy call in terms of locking since the session list lock
48 * AND session lock are acquired.
50 static void update_agent_app(struct agent_app
*app
)
52 struct ltt_session
*session
, *stmp
;
53 struct ltt_session_list
*list
;
55 list
= session_get_list();
59 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
60 session_lock(session
);
61 if (session
->ust_session
) {
65 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
67 agent_update(agt
, app
->sock
->fd
);
71 session_unlock(session
);
73 session_unlock_list();
77 * Destroy a agent application by socket.
79 static void destroy_agent_app(int sock
)
81 struct agent_app
*app
;
86 * Not finding an application is a very important error that should NEVER
87 * happen. The hash table deletion is ONLY done through this call even on
91 app
= agent_find_app_by_sock(sock
);
95 /* RCU read side lock is taken in this function call. */
96 agent_delete_app(app
);
98 /* The application is freed in a RCU call but the socket is closed here. */
99 agent_destroy_app(app
);
103 * Cleanup remaining agent apps in the hash table. This should only be called in
104 * the exit path of the thread.
106 static void clean_agent_apps_ht(void)
108 struct lttng_ht_node_ulong
*node
;
109 struct lttng_ht_iter iter
;
111 DBG3("[agent-thread] Cleaning agent apps ht");
114 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
115 struct agent_app
*app
;
117 app
= caa_container_of(node
, struct agent_app
, node
);
118 destroy_agent_app(app
->sock
->fd
);
124 * Create and init socket from uri.
126 static struct lttcomm_sock
*init_tcp_socket(void)
129 struct lttng_uri
*uri
= NULL
;
130 struct lttcomm_sock
*sock
= NULL
;
133 * This should never fail since the URI is hardcoded and the port is set
134 * before this thread is launched.
136 ret
= uri_parse(default_reg_uri
, &uri
);
138 assert(agent_tcp_port
);
139 uri
->port
= agent_tcp_port
;
141 sock
= lttcomm_alloc_sock_from_uri(uri
);
144 ERR("[agent-thread] agent allocating TCP socket");
148 ret
= lttcomm_create_sock(sock
);
153 ret
= sock
->ops
->bind(sock
);
155 WARN("Another session daemon is using this agent port. Agent support "
156 "will be deactivated to prevent interfering with the tracing.");
160 ret
= sock
->ops
->listen(sock
, -1);
165 DBG("[agent-thread] Listening on TCP port %u and socket %d",
166 agent_tcp_port
, sock
->fd
);
172 lttcomm_destroy_sock(sock
);
178 * Close and destroy the given TCP socket.
180 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
184 DBG3("[agent-thread] Destroy TCP socket on port %u", agent_tcp_port
);
186 /* This will return gracefully if fd is invalid. */
187 sock
->ops
->close(sock
);
188 lttcomm_destroy_sock(sock
);
192 * Handle a new agent registration using the reg socket. After that, a new
193 * agent application is added to the global hash table and attach to an UST app
194 * object. If r_app is not NULL, the created app is set to the pointer.
196 * Return the new FD created upon accept() on success or else a negative errno
199 static int handle_registration(struct lttcomm_sock
*reg_sock
,
200 struct agent_app
**r_app
)
204 uint32_t major_version
, minor_version
;
206 enum lttng_domain_type domain
;
207 struct agent_app
*app
;
208 struct agent_register_msg msg
;
209 struct lttcomm_sock
*new_sock
;
213 new_sock
= reg_sock
->ops
->accept(reg_sock
);
219 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
220 if (size
< sizeof(msg
)) {
224 domain
= be32toh(msg
.domain
);
225 pid
= be32toh(msg
.pid
);
226 major_version
= be32toh(msg
.major_version
);
227 minor_version
= be32toh(msg
.minor_version
);
229 /* Test communication protocol version of the registring agent. */
230 if (major_version
!= AGENT_MAJOR_VERSION
) {
234 if (minor_version
!= AGENT_MINOR_VERSION
) {
239 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
240 pid
, domain
, new_sock
->fd
);
242 app
= agent_create_app(pid
, domain
, new_sock
);
249 * Add before assigning the socket value to the UST app so it can be found
255 * We don't need to attach the agent app to the app. If we ever do so, we
256 * should consider both registration order of agent before app and app
267 new_sock
->ops
->close(new_sock
);
268 lttcomm_destroy_sock(new_sock
);
274 * This thread manage application notify communication.
276 void *agent_thread_manage_registration(void *data
)
279 uint32_t revents
, nb_fd
;
280 struct lttng_poll_event events
;
281 struct lttcomm_sock
*reg_sock
;
283 DBG("[agent-thread] Manage agent application registration.");
285 rcu_register_thread();
288 /* Agent initialization call MUST be called before starting the thread. */
289 assert(agent_apps_ht_by_sock
);
291 /* Create pollset with size 2, quit pipe and socket. */
292 ret
= sessiond_set_thread_pollset(&events
, 2);
294 goto error_poll_create
;
297 reg_sock
= init_tcp_socket();
299 goto error_tcp_socket
;
302 /* Add create valid TCP socket to poll set. */
303 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
304 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
310 DBG3("[agent-thread] Manage agent polling");
312 /* Inifinite blocking call, waiting for transmission */
314 ret
= lttng_poll_wait(&events
, -1);
315 DBG3("[agent-thread] Manage agent return from poll on %d fds",
316 LTTNG_POLL_GETNB(&events
));
319 * Restart interrupted system call.
321 if (errno
== EINTR
) {
327 DBG3("[agent-thread] %d fd ready", nb_fd
);
329 for (i
= 0; i
< nb_fd
; i
++) {
330 /* Fetch once the poll data */
331 revents
= LTTNG_POLL_GETEV(&events
, i
);
332 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
335 /* No activity for this FD (poll implementation). */
339 /* Thread quit pipe has been closed. Killing thread. */
340 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
346 * Check first if this is a POLLERR since POLLIN is also included
347 * in an error value thus checking first.
349 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
350 /* Removing from the poll set */
351 ret
= lttng_poll_del(&events
, pollfd
);
356 destroy_agent_app(pollfd
);
357 } else if (revents
& (LPOLLIN
)) {
359 struct agent_app
*app
= NULL
;
361 /* Pollin event of agent app socket should NEVER happen. */
362 assert(pollfd
== reg_sock
->fd
);
364 new_fd
= handle_registration(reg_sock
, &app
);
366 WARN("[agent-thread] agent registration failed. Ignoring.");
367 /* Somehow the communication failed. Just continue. */
370 /* Should not have a NULL app on success. */
373 /* Only add poll error event to only detect shutdown. */
374 ret
= lttng_poll_add(&events
, new_fd
,
375 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
377 destroy_agent_app(new_fd
);
381 /* Update newly registered app. */
382 update_agent_app(app
);
384 /* On failure, the poll will detect it and clean it up. */
385 (void) agent_send_registration_done(app
);
387 ERR("Unknown poll events %u for sock %d", revents
, pollfd
);
394 /* Whatever happens, try to delete it and exit. */
395 (void) lttng_poll_del(&events
, reg_sock
->fd
);
397 destroy_tcp_socket(reg_sock
);
399 lttng_poll_clean(&events
);
401 DBG("[agent-thread] is cleaning up and stopping.");
403 if (agent_apps_ht_by_sock
) {
404 clean_agent_apps_ht();
405 lttng_ht_destroy(agent_apps_ht_by_sock
);
408 rcu_thread_offline();
409 rcu_unregister_thread();