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