Add the support for multiple agent
[lttng-tools.git] / src / bin / lttng-sessiond / agent-thread.c
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #define _GNU_SOURCE
19 #include <assert.h>
20
21 #include <common/common.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/uri.h>
24 #include <common/utils.h>
25
26 #include <common/compat/endian.h>
27
28 #include "fd-limit.h"
29 #include "agent-thread.h"
30 #include "lttng-sessiond.h"
31 #include "session.h"
32 #include "utils.h"
33
34 /*
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.
38 */
39 static const char *default_reg_uri =
40 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
41
42 /*
43 * Update agent application using the given socket. This is done just after
44 * registration was successful.
45 *
46 * This is a quite heavy call in terms of locking since the session list lock
47 * AND session lock are acquired.
48 */
49 static void update_agent_app(struct agent_app *app)
50 {
51 struct ltt_session *session, *stmp;
52 struct ltt_session_list *list;
53
54 list = session_get_list();
55 assert(list);
56
57 session_lock_list();
58 cds_list_for_each_entry_safe(session, stmp, &list->head, list) {
59 session_lock(session);
60 if (session->ust_session) {
61 struct agent *agt;
62
63 agt = trace_ust_find_agent(session->ust_session, app->domain);
64 if (agt) {
65 agent_update(agt, app->sock->fd);
66 }
67 }
68 session_unlock(session);
69 }
70 session_unlock_list();
71 }
72
73 /*
74 * Destroy a agent application by socket.
75 */
76 static void destroy_agent_app(int sock)
77 {
78 struct agent_app *app;
79
80 assert(sock >= 0);
81
82 /*
83 * Not finding an application is a very important error that should NEVER
84 * happen. The hash table deletion is ONLY done through this call even on
85 * thread cleanup.
86 */
87 rcu_read_lock();
88 app = agent_find_app_by_sock(sock);
89 assert(app);
90 rcu_read_unlock();
91
92 /* RCU read side lock is taken in this function call. */
93 agent_delete_app(app);
94
95 /* The application is freed in a RCU call but the socket is closed here. */
96 agent_destroy_app(app);
97 }
98
99 /*
100 * Cleanup remaining agent apps in the hash table. This should only be called in
101 * the exit path of the thread.
102 */
103 static void clean_agent_apps_ht(void)
104 {
105 struct lttng_ht_node_ulong *node;
106 struct lttng_ht_iter iter;
107
108 DBG3("[agent-thread] Cleaning agent apps ht");
109
110 rcu_read_lock();
111 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
112 struct agent_app *app;
113
114 app = caa_container_of(node, struct agent_app, node);
115 destroy_agent_app(app->sock->fd);
116 }
117 rcu_read_unlock();
118 }
119
120 /*
121 * Create and init socket from uri.
122 */
123 static struct lttcomm_sock *init_tcp_socket(void)
124 {
125 int ret;
126 struct lttng_uri *uri = NULL;
127 struct lttcomm_sock *sock = NULL;
128
129 /*
130 * This should never fail since the URI is hardcoded and the port is set
131 * before this thread is launched.
132 */
133 ret = uri_parse(default_reg_uri, &uri);
134 assert(ret);
135 assert(agent_tcp_port);
136 uri->port = agent_tcp_port;
137
138 sock = lttcomm_alloc_sock_from_uri(uri);
139 uri_free(uri);
140 if (sock == NULL) {
141 ERR("[agent-thread] agent allocating TCP socket");
142 goto error;
143 }
144
145 ret = lttcomm_create_sock(sock);
146 if (ret < 0) {
147 goto error;
148 }
149
150 ret = sock->ops->bind(sock);
151 if (ret < 0) {
152 WARN("Another session daemon is using this agent port. Agent support "
153 "will be deactivated to prevent interfering with the tracing.");
154 goto error;
155 }
156
157 ret = sock->ops->listen(sock, -1);
158 if (ret < 0) {
159 goto error;
160 }
161
162 DBG("[agent-thread] Listening on TCP port %u and socket %d",
163 agent_tcp_port, sock->fd);
164
165 return sock;
166
167 error:
168 if (sock) {
169 lttcomm_destroy_sock(sock);
170 }
171 return NULL;
172 }
173
174 /*
175 * Close and destroy the given TCP socket.
176 */
177 static void destroy_tcp_socket(struct lttcomm_sock *sock)
178 {
179 assert(sock);
180
181 DBG3("[agent-thread] Destroy TCP socket on port %u", agent_tcp_port);
182
183 /* This will return gracefully if fd is invalid. */
184 sock->ops->close(sock);
185 lttcomm_destroy_sock(sock);
186 }
187
188 /*
189 * Handle a new agent registration using the reg socket. After that, a new
190 * agent application is added to the global hash table and attach to an UST app
191 * object. If r_app is not NULL, the created app is set to the pointer.
192 *
193 * Return the new FD created upon accept() on success or else a negative errno
194 * value.
195 */
196 static int handle_registration(struct lttcomm_sock *reg_sock,
197 struct agent_app **r_app)
198 {
199 int ret;
200 pid_t pid;
201 ssize_t size;
202 enum lttng_domain_type domain;
203 struct agent_app *app;
204 struct agent_register_msg msg;
205 struct lttcomm_sock *new_sock;
206
207 assert(reg_sock);
208
209 new_sock = reg_sock->ops->accept(reg_sock);
210 if (!new_sock) {
211 ret = -ENOTCONN;
212 goto error;
213 }
214
215 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
216 if (size < sizeof(msg)) {
217 ret = -errno;
218 goto error_socket;
219 }
220 domain = be32toh(msg.domain);
221 pid = be32toh(msg.pid);
222
223 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
224 pid, domain, new_sock->fd);
225
226 app = agent_create_app(pid, domain, new_sock);
227 if (!app) {
228 ret = -ENOMEM;
229 goto error_socket;
230 }
231
232 /*
233 * Add before assigning the socket value to the UST app so it can be found
234 * concurrently.
235 */
236 agent_add_app(app);
237
238 /*
239 * We don't need to attach the agent app to the app. If we ever do so, we
240 * should consider both registration order of agent before app and app
241 * before agent.
242 */
243
244 if (r_app) {
245 *r_app = app;
246 }
247
248 return new_sock->fd;
249
250 error_socket:
251 new_sock->ops->close(new_sock);
252 lttcomm_destroy_sock(new_sock);
253 error:
254 return ret;
255 }
256
257 /*
258 * This thread manage application notify communication.
259 */
260 void *agent_thread_manage_registration(void *data)
261 {
262 int i, ret, pollfd;
263 uint32_t revents, nb_fd;
264 struct lttng_poll_event events;
265 struct lttcomm_sock *reg_sock;
266
267 DBG("[agent-thread] Manage agent application registration.");
268
269 rcu_register_thread();
270 rcu_thread_online();
271
272 /* Agent initialization call MUST be called before starting the thread. */
273 assert(agent_apps_ht_by_sock);
274
275 /* Create pollset with size 2, quit pipe and socket. */
276 ret = sessiond_set_thread_pollset(&events, 2);
277 if (ret < 0) {
278 goto error_poll_create;
279 }
280
281 reg_sock = init_tcp_socket();
282 if (!reg_sock) {
283 goto error_tcp_socket;
284 }
285
286 /* Add create valid TCP socket to poll set. */
287 ret = lttng_poll_add(&events, reg_sock->fd,
288 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
289 if (ret < 0) {
290 goto error;
291 }
292
293 while (1) {
294 DBG3("[agent-thread] Manage agent polling on %d fds",
295 LTTNG_POLL_GETNB(&events));
296
297 /* Inifinite blocking call, waiting for transmission */
298 restart:
299 ret = lttng_poll_wait(&events, -1);
300 if (ret < 0) {
301 /*
302 * Restart interrupted system call.
303 */
304 if (errno == EINTR) {
305 goto restart;
306 }
307 goto error;
308 }
309 nb_fd = ret;
310 DBG3("[agent-thread] %d fd ready", nb_fd);
311
312 for (i = 0; i < nb_fd; i++) {
313 /* Fetch once the poll data */
314 revents = LTTNG_POLL_GETEV(&events, i);
315 pollfd = LTTNG_POLL_GETFD(&events, i);
316
317 /* Thread quit pipe has been closed. Killing thread. */
318 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
319 if (ret) {
320 goto exit;
321 }
322
323 /*
324 * Check first if this is a POLLERR since POLLIN is also included
325 * in an error value thus checking first.
326 */
327 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
328 /* Removing from the poll set */
329 ret = lttng_poll_del(&events, pollfd);
330 if (ret < 0) {
331 goto error;
332 }
333
334 destroy_agent_app(pollfd);
335 } else if (revents & (LPOLLIN)) {
336 int new_fd;
337 struct agent_app *app = NULL;
338
339 /* Pollin event of agent app socket should NEVER happen. */
340 assert(pollfd == reg_sock->fd);
341
342 new_fd = handle_registration(reg_sock, &app);
343 if (new_fd < 0) {
344 WARN("[agent-thread] agent registration failed. Ignoring.");
345 /* Somehow the communication failed. Just continue. */
346 continue;
347 }
348 /* Should not have a NULL app on success. */
349 assert(app);
350
351 /* Only add poll error event to only detect shutdown. */
352 ret = lttng_poll_add(&events, new_fd,
353 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
354 if (ret < 0) {
355 destroy_agent_app(new_fd);
356 continue;
357 }
358
359 /* Update newly registered app. */
360 update_agent_app(app);
361
362 /* On failure, the poll will detect it and clean it up. */
363 (void) agent_send_registration_done(app);
364 } else {
365 ERR("Unknown poll events %u for sock %d", revents, pollfd);
366 continue;
367 }
368 }
369 }
370
371 exit:
372 /* Whatever happens, try to delete it and exit. */
373 (void) lttng_poll_del(&events, reg_sock->fd);
374 error:
375 destroy_tcp_socket(reg_sock);
376 error_tcp_socket:
377 lttng_poll_clean(&events);
378 error_poll_create:
379 DBG("[agent-thread] is cleaning up and stopping.");
380
381 if (agent_apps_ht_by_sock) {
382 clean_agent_apps_ht();
383 lttng_ht_destroy(agent_apps_ht_by_sock);
384 }
385
386 rcu_thread_offline();
387 rcu_unregister_thread();
388 return NULL;
389 }
This page took 0.03868 seconds and 6 git commands to generate.