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