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"
30 #include "lttng-sessiond.h"
35 * Note that there is not port here. It's set after this URI is parsed so we
36 * can let the user define a custom one. However, localhost is ALWAYS the
37 * default listening address.
39 static const char *default_reg_uri
=
40 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS
;
43 * Update agent application using the given socket. This is done just after
44 * registration was successful.
46 * This is a quite heavy call in terms of locking since the session list lock
47 * AND session lock are acquired.
49 static void update_agent_app(struct agent_app
*app
)
51 struct ltt_session
*session
, *stmp
;
52 struct ltt_session_list
*list
;
54 list
= session_get_list();
58 cds_list_for_each_entry_safe(session
, stmp
, &list
->head
, list
) {
59 session_lock(session
);
60 if (session
->ust_session
) {
64 agt
= trace_ust_find_agent(session
->ust_session
, app
->domain
);
66 agent_update(agt
, app
->sock
->fd
);
70 session_unlock(session
);
72 session_unlock_list();
76 * Destroy a agent application by socket.
78 static void destroy_agent_app(int sock
)
80 struct agent_app
*app
;
85 * Not finding an application is a very important error that should NEVER
86 * happen. The hash table deletion is ONLY done through this call even on
90 app
= agent_find_app_by_sock(sock
);
94 /* RCU read side lock is taken in this function call. */
95 agent_delete_app(app
);
97 /* The application is freed in a RCU call but the socket is closed here. */
98 agent_destroy_app(app
);
102 * Cleanup remaining agent apps in the hash table. This should only be called in
103 * the exit path of the thread.
105 static void clean_agent_apps_ht(void)
107 struct lttng_ht_node_ulong
*node
;
108 struct lttng_ht_iter iter
;
110 DBG3("[agent-thread] Cleaning agent apps ht");
113 cds_lfht_for_each_entry(agent_apps_ht_by_sock
->ht
, &iter
.iter
, node
, node
) {
114 struct agent_app
*app
;
116 app
= caa_container_of(node
, struct agent_app
, node
);
117 destroy_agent_app(app
->sock
->fd
);
123 * Create and init socket from uri.
125 static struct lttcomm_sock
*init_tcp_socket(void)
128 struct lttng_uri
*uri
= NULL
;
129 struct lttcomm_sock
*sock
= NULL
;
132 * This should never fail since the URI is hardcoded and the port is set
133 * before this thread is launched.
135 ret
= uri_parse(default_reg_uri
, &uri
);
137 assert(agent_tcp_port
);
138 uri
->port
= agent_tcp_port
;
140 sock
= lttcomm_alloc_sock_from_uri(uri
);
143 ERR("[agent-thread] agent allocating TCP socket");
147 ret
= lttcomm_create_sock(sock
);
152 ret
= sock
->ops
->bind(sock
);
154 WARN("Another session daemon is using this agent port. Agent support "
155 "will be deactivated to prevent interfering with the tracing.");
159 ret
= sock
->ops
->listen(sock
, -1);
164 DBG("[agent-thread] Listening on TCP port %u and socket %d",
165 agent_tcp_port
, sock
->fd
);
171 lttcomm_destroy_sock(sock
);
177 * Close and destroy the given TCP socket.
179 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
183 DBG3("[agent-thread] Destroy TCP socket on port %u", agent_tcp_port
);
185 /* This will return gracefully if fd is invalid. */
186 sock
->ops
->close(sock
);
187 lttcomm_destroy_sock(sock
);
191 * Handle a new agent registration using the reg socket. After that, a new
192 * agent application is added to the global hash table and attach to an UST app
193 * object. If r_app is not NULL, the created app is set to the pointer.
195 * Return the new FD created upon accept() on success or else a negative errno
198 static int handle_registration(struct lttcomm_sock
*reg_sock
,
199 struct agent_app
**r_app
)
203 uint32_t major_version
, minor_version
;
205 enum lttng_domain_type domain
;
206 struct agent_app
*app
;
207 struct agent_register_msg msg
;
208 struct lttcomm_sock
*new_sock
;
212 new_sock
= reg_sock
->ops
->accept(reg_sock
);
218 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
219 if (size
< sizeof(msg
)) {
223 domain
= be32toh(msg
.domain
);
224 pid
= be32toh(msg
.pid
);
225 major_version
= be32toh(msg
.major_version
);
226 minor_version
= be32toh(msg
.minor_version
);
228 /* Test communication protocol version of the registring agent. */
229 if (major_version
!= AGENT_MAJOR_VERSION
) {
233 if (minor_version
!= AGENT_MINOR_VERSION
) {
238 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
239 pid
, domain
, new_sock
->fd
);
241 app
= agent_create_app(pid
, domain
, new_sock
);
248 * Add before assigning the socket value to the UST app so it can be found
254 * We don't need to attach the agent app to the app. If we ever do so, we
255 * should consider both registration order of agent before app and app
266 new_sock
->ops
->close(new_sock
);
267 lttcomm_destroy_sock(new_sock
);
273 * This thread manage application notify communication.
275 void *agent_thread_manage_registration(void *data
)
278 uint32_t revents
, nb_fd
;
279 struct lttng_poll_event events
;
280 struct lttcomm_sock
*reg_sock
;
282 DBG("[agent-thread] Manage agent application registration.");
284 rcu_register_thread();
287 /* Agent initialization call MUST be called before starting the thread. */
288 assert(agent_apps_ht_by_sock
);
290 /* Create pollset with size 2, quit pipe and socket. */
291 ret
= sessiond_set_thread_pollset(&events
, 2);
293 goto error_poll_create
;
296 reg_sock
= init_tcp_socket();
298 goto error_tcp_socket
;
301 /* Add create valid TCP socket to poll set. */
302 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
303 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
309 DBG3("[agent-thread] Manage agent polling",
310 LTTNG_POLL_GETNB(&events
));
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();