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 * 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 * Create and init socket from uri.
79 static struct lttcomm_sock
*init_tcp_socket(void)
82 struct lttng_uri
*uri
= NULL
;
83 struct lttcomm_sock
*sock
= NULL
;
86 * This should never fail since the URI is hardcoded and the port is set
87 * before this thread is launched.
89 ret
= uri_parse(default_reg_uri
, &uri
);
91 assert(config
.agent_tcp_port
);
92 uri
->port
= config
.agent_tcp_port
;
94 sock
= lttcomm_alloc_sock_from_uri(uri
);
97 ERR("[agent-thread] agent allocating TCP socket");
101 ret
= lttcomm_create_sock(sock
);
106 ret
= sock
->ops
->bind(sock
);
108 WARN("Another session daemon is using this agent port. Agent support "
109 "will be deactivated to prevent interfering with the tracing.");
113 ret
= sock
->ops
->listen(sock
, -1);
118 DBG("[agent-thread] Listening on TCP port %u and socket %d",
119 config
.agent_tcp_port
, sock
->fd
);
125 lttcomm_destroy_sock(sock
);
131 * Close and destroy the given TCP socket.
133 static void destroy_tcp_socket(struct lttcomm_sock
*sock
)
137 DBG3("[agent-thread] Destroy TCP socket on port %u", config
.agent_tcp_port
);
139 /* This will return gracefully if fd is invalid. */
140 sock
->ops
->close(sock
);
141 lttcomm_destroy_sock(sock
);
145 * Handle a new agent registration using the reg socket. After that, a new
146 * agent application is added to the global hash table and attach to an UST app
147 * object. If r_app is not NULL, the created app is set to the pointer.
149 * Return the new FD created upon accept() on success or else a negative errno
152 static int handle_registration(struct lttcomm_sock
*reg_sock
,
153 struct agent_app
**r_app
)
157 uint32_t major_version
, minor_version
;
159 enum lttng_domain_type domain
;
160 struct agent_app
*app
;
161 struct agent_register_msg msg
;
162 struct lttcomm_sock
*new_sock
;
166 new_sock
= reg_sock
->ops
->accept(reg_sock
);
172 size
= new_sock
->ops
->recvmsg(new_sock
, &msg
, sizeof(msg
), 0);
173 if (size
< sizeof(msg
)) {
177 domain
= be32toh(msg
.domain
);
178 pid
= be32toh(msg
.pid
);
179 major_version
= be32toh(msg
.major_version
);
180 minor_version
= be32toh(msg
.minor_version
);
182 /* Test communication protocol version of the registring agent. */
183 if (major_version
!= AGENT_MAJOR_VERSION
) {
187 if (minor_version
!= AGENT_MINOR_VERSION
) {
192 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
193 pid
, domain
, new_sock
->fd
);
195 app
= agent_create_app(pid
, domain
, new_sock
);
202 * Add before assigning the socket value to the UST app so it can be found
208 * We don't need to attach the agent app to the app. If we ever do so, we
209 * should consider both registration order of agent before app and app
220 new_sock
->ops
->close(new_sock
);
221 lttcomm_destroy_sock(new_sock
);
227 * This thread manage application notify communication.
229 void *agent_thread_manage_registration(void *data
)
232 uint32_t revents
, nb_fd
;
233 struct lttng_poll_event events
;
234 struct lttcomm_sock
*reg_sock
;
236 DBG("[agent-thread] Manage agent application registration.");
238 rcu_register_thread();
241 /* Agent initialization call MUST be called before starting the thread. */
242 assert(agent_apps_ht_by_sock
);
244 /* Create pollset with size 2, quit pipe and socket. */
245 ret
= sessiond_set_thread_pollset(&events
, 2);
247 goto error_poll_create
;
250 reg_sock
= init_tcp_socket();
252 goto error_tcp_socket
;
255 /* Add TCP socket to poll set. */
256 ret
= lttng_poll_add(&events
, reg_sock
->fd
,
257 LPOLLIN
| LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
263 DBG3("[agent-thread] Manage agent polling");
265 /* Inifinite blocking call, waiting for transmission */
267 ret
= lttng_poll_wait(&events
, -1);
268 DBG3("[agent-thread] Manage agent return from poll on %d fds",
269 LTTNG_POLL_GETNB(&events
));
272 * Restart interrupted system call.
274 if (errno
== EINTR
) {
280 DBG3("[agent-thread] %d fd ready", nb_fd
);
282 for (i
= 0; i
< nb_fd
; i
++) {
283 /* Fetch once the poll data */
284 revents
= LTTNG_POLL_GETEV(&events
, i
);
285 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
288 /* No activity for this FD (poll implementation). */
292 /* Thread quit pipe has been closed. Killing thread. */
293 ret
= sessiond_check_thread_quit_pipe(pollfd
, revents
);
298 if (revents
& LPOLLIN
) {
300 struct agent_app
*app
= NULL
;
302 assert(pollfd
== reg_sock
->fd
);
303 new_fd
= handle_registration(reg_sock
, &app
);
307 /* Should not have a NULL app on success. */
311 * Since this is a command socket (write then read),
312 * only add poll error event to only detect shutdown.
314 ret
= lttng_poll_add(&events
, new_fd
,
315 LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
);
317 agent_destroy_app_by_sock(new_fd
);
321 /* Update newly registered app. */
322 update_agent_app(app
);
324 /* On failure, the poll will detect it and clean it up. */
325 ret
= agent_send_registration_done(app
);
327 /* Removing from the poll set */
328 ret
= lttng_poll_del(&events
, new_fd
);
332 agent_destroy_app_by_sock(new_fd
);
335 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
336 /* Removing from the poll set */
337 ret
= lttng_poll_del(&events
, pollfd
);
341 agent_destroy_app_by_sock(pollfd
);
343 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
350 /* Whatever happens, try to delete it and exit. */
351 (void) lttng_poll_del(&events
, reg_sock
->fd
);
353 destroy_tcp_socket(reg_sock
);
355 lttng_poll_clean(&events
);
357 DBG("[agent-thread] is cleaning up and stopping.");
359 rcu_thread_offline();
360 rcu_unregister_thread();