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 #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 rcu_read_lock();
64 agt = trace_ust_find_agent(session->ust_session, app->domain);
65 if (agt) {
66 agent_update(agt, app->sock->fd);
67 }
68 rcu_read_unlock();
69 }
70 session_unlock(session);
71 }
72 session_unlock_list();
73 }
74
75 /*
76 * Destroy a agent application by socket.
77 */
78 static void destroy_agent_app(int sock)
79 {
80 struct agent_app *app;
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();
90 app = agent_find_app_by_sock(sock);
91 assert(app);
92
93 /* RCU read side lock is assumed to be held by this function. */
94 agent_delete_app(app);
95
96 /* The application is freed in a RCU call but the socket is closed here. */
97 agent_destroy_app(app);
98 rcu_read_unlock();
99 }
100
101 /*
102 * Cleanup remaining agent apps in the hash table. This should only be called in
103 * the exit path of the thread.
104 */
105 static void clean_agent_apps_ht(void)
106 {
107 struct lttng_ht_node_ulong *node;
108 struct lttng_ht_iter iter;
109
110 DBG3("[agent-thread] Cleaning agent apps ht");
111
112 rcu_read_lock();
113 cds_lfht_for_each_entry(agent_apps_ht_by_sock->ht, &iter.iter, node, node) {
114 struct agent_app *app;
115
116 app = caa_container_of(node, struct agent_app, node);
117 destroy_agent_app(app->sock->fd);
118 }
119 rcu_read_unlock();
120 }
121
122 /*
123 * Create and init socket from uri.
124 */
125 static 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);
137 assert(agent_tcp_port);
138 uri->port = agent_tcp_port;
139
140 sock = lttcomm_alloc_sock_from_uri(uri);
141 uri_free(uri);
142 if (sock == NULL) {
143 ERR("[agent-thread] agent allocating TCP socket");
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) {
154 WARN("Another session daemon is using this agent port. Agent support "
155 "will be deactivated to prevent interfering with the tracing.");
156 goto error;
157 }
158
159 ret = sock->ops->listen(sock, -1);
160 if (ret < 0) {
161 goto error;
162 }
163
164 DBG("[agent-thread] Listening on TCP port %u and socket %d",
165 agent_tcp_port, sock->fd);
166
167 return sock;
168
169 error:
170 if (sock) {
171 lttcomm_destroy_sock(sock);
172 }
173 return NULL;
174 }
175
176 /*
177 * Close and destroy the given TCP socket.
178 */
179 static void destroy_tcp_socket(struct lttcomm_sock *sock)
180 {
181 assert(sock);
182
183 DBG3("[agent-thread] Destroy TCP socket on port %u", agent_tcp_port);
184
185 /* This will return gracefully if fd is invalid. */
186 sock->ops->close(sock);
187 lttcomm_destroy_sock(sock);
188 }
189
190 /*
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.
194 *
195 * Return the new FD created upon accept() on success or else a negative errno
196 * value.
197 */
198 static int handle_registration(struct lttcomm_sock *reg_sock,
199 struct agent_app **r_app)
200 {
201 int ret;
202 pid_t pid;
203 uint32_t major_version, minor_version;
204 ssize_t size;
205 enum lttng_domain_type domain;
206 struct agent_app *app;
207 struct agent_register_msg msg;
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)) {
220 ret = -EINVAL;
221 goto error_socket;
222 }
223 domain = be32toh(msg.domain);
224 pid = be32toh(msg.pid);
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 }
237
238 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
239 pid, domain, new_sock->fd);
240
241 app = agent_create_app(pid, domain, new_sock);
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 */
251 agent_add_app(app);
252
253 /*
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.
257 */
258
259 if (r_app) {
260 *r_app = app;
261 }
262
263 return new_sock->fd;
264
265 error_socket:
266 new_sock->ops->close(new_sock);
267 lttcomm_destroy_sock(new_sock);
268 error:
269 return ret;
270 }
271
272 /*
273 * This thread manage application notify communication.
274 */
275 void *agent_thread_manage_registration(void *data)
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
282 DBG("[agent-thread] Manage agent application registration.");
283
284 rcu_register_thread();
285 rcu_thread_online();
286
287 /* Agent initialization call MUST be called before starting the thread. */
288 assert(agent_apps_ht_by_sock);
289
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) {
309 DBG3("[agent-thread] Manage agent polling");
310
311 /* Inifinite blocking call, waiting for transmission */
312 restart:
313 ret = lttng_poll_wait(&events, -1);
314 DBG3("[agent-thread] Manage agent return from poll on %d fds",
315 LTTNG_POLL_GETNB(&events));
316 if (ret < 0) {
317 /*
318 * Restart interrupted system call.
319 */
320 if (errno == EINTR) {
321 goto restart;
322 }
323 goto error;
324 }
325 nb_fd = ret;
326 DBG3("[agent-thread] %d fd ready", nb_fd);
327
328 for (i = 0; i < nb_fd; i++) {
329 /* Fetch once the poll data */
330 revents = LTTNG_POLL_GETEV(&events, i);
331 pollfd = LTTNG_POLL_GETFD(&events, i);
332
333 if (!revents) {
334 /* No activity for this FD (poll implementation). */
335 continue;
336 }
337
338 /* Thread quit pipe has been closed. Killing thread. */
339 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
340 if (ret) {
341 goto exit;
342 }
343
344 /*
345 * Check first if this is a POLLERR since POLLIN is also included
346 * in an error value thus checking first.
347 */
348 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
349 /* Removing from the poll set */
350 ret = lttng_poll_del(&events, pollfd);
351 if (ret < 0) {
352 goto error;
353 }
354
355 destroy_agent_app(pollfd);
356 } else if (revents & (LPOLLIN)) {
357 int new_fd;
358 struct agent_app *app = NULL;
359
360 /* Pollin event of agent app socket should NEVER happen. */
361 assert(pollfd == reg_sock->fd);
362
363 new_fd = handle_registration(reg_sock, &app);
364 if (new_fd < 0) {
365 WARN("[agent-thread] agent registration failed. Ignoring.");
366 /* Somehow the communication failed. Just continue. */
367 continue;
368 }
369 /* Should not have a NULL app on success. */
370 assert(app);
371
372 /* Only add poll error event to only detect shutdown. */
373 ret = lttng_poll_add(&events, new_fd,
374 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
375 if (ret < 0) {
376 destroy_agent_app(new_fd);
377 continue;
378 }
379
380 /* Update newly registered app. */
381 update_agent_app(app);
382
383 /* On failure, the poll will detect it and clean it up. */
384 (void) agent_send_registration_done(app);
385 } else {
386 ERR("Unknown poll events %u for sock %d", revents, pollfd);
387 continue;
388 }
389 }
390 }
391
392 exit:
393 /* Whatever happens, try to delete it and exit. */
394 (void) lttng_poll_del(&events, reg_sock->fd);
395 error:
396 destroy_tcp_socket(reg_sock);
397 error_tcp_socket:
398 lttng_poll_clean(&events);
399 error_poll_create:
400 DBG("[agent-thread] is cleaning up and stopping.");
401
402 if (agent_apps_ht_by_sock) {
403 clean_agent_apps_ht();
404 lttng_ht_destroy(agent_apps_ht_by_sock);
405 }
406
407 rcu_thread_offline();
408 rcu_unregister_thread();
409 return NULL;
410 }
This page took 0.039814 seconds and 4 git commands to generate.