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