Fix: agent may not be ready on launch
[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
6c1c0768 18#define _LGPL_SOURCE
4d076222
DG
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"
6a4e4039 30#include "agent.h"
4d076222 31#include "lttng-sessiond.h"
f20baf8e
DG
32#include "session.h"
33#include "utils.h"
4d076222
DG
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 */
fa91dc52
MD
40static const char *default_reg_uri =
41 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
4d076222 42
f20baf8e 43/*
022d91ba 44 * Update agent application using the given socket. This is done just after
f20baf8e
DG
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 */
fefd409b 50static void update_agent_app(struct agent_app *app)
f20baf8e
DG
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) {
fefd409b
DG
62 struct agent *agt;
63
4da703ad 64 rcu_read_lock();
fefd409b
DG
65 agt = trace_ust_find_agent(session->ust_session, app->domain);
66 if (agt) {
67 agent_update(agt, app->sock->fd);
68 }
4da703ad 69 rcu_read_unlock();
f20baf8e
DG
70 }
71 session_unlock(session);
72 }
73 session_unlock_list();
74}
75
4d076222
DG
76/*
77 * Create and init socket from uri.
78 */
79static struct lttcomm_sock *init_tcp_socket(void)
80{
81 int ret;
82 struct lttng_uri *uri = NULL;
83 struct lttcomm_sock *sock = NULL;
84
85 /*
86 * This should never fail since the URI is hardcoded and the port is set
87 * before this thread is launched.
88 */
89 ret = uri_parse(default_reg_uri, &uri);
90 assert(ret);
e6142f2e
JG
91 assert(config.agent_tcp_port);
92 uri->port = config.agent_tcp_port;
4d076222
DG
93
94 sock = lttcomm_alloc_sock_from_uri(uri);
95 uri_free(uri);
96 if (sock == NULL) {
022d91ba 97 ERR("[agent-thread] agent allocating TCP socket");
4d076222
DG
98 goto error;
99 }
100
101 ret = lttcomm_create_sock(sock);
102 if (ret < 0) {
103 goto error;
104 }
105
106 ret = sock->ops->bind(sock);
107 if (ret < 0) {
022d91ba 108 WARN("Another session daemon is using this agent port. Agent support "
5368d366 109 "will be deactivated to prevent interfering with the tracing.");
4d076222
DG
110 goto error;
111 }
112
113 ret = sock->ops->listen(sock, -1);
114 if (ret < 0) {
115 goto error;
116 }
117
022d91ba 118 DBG("[agent-thread] Listening on TCP port %u and socket %d",
e6142f2e 119 config.agent_tcp_port, sock->fd);
4d076222
DG
120
121 return sock;
122
123error:
124 if (sock) {
125 lttcomm_destroy_sock(sock);
126 }
127 return NULL;
128}
129
130/*
131 * Close and destroy the given TCP socket.
132 */
133static void destroy_tcp_socket(struct lttcomm_sock *sock)
134{
135 assert(sock);
136
e6142f2e 137 DBG3("[agent-thread] Destroy TCP socket on port %u", config.agent_tcp_port);
4d076222
DG
138
139 /* This will return gracefully if fd is invalid. */
140 sock->ops->close(sock);
141 lttcomm_destroy_sock(sock);
142}
143
f20baf8e 144/*
022d91ba
DG
145 * Handle a new agent registration using the reg socket. After that, a new
146 * agent application is added to the global hash table and attach to an UST app
1b500e7a 147 * object. If r_app is not NULL, the created app is set to the pointer.
f20baf8e
DG
148 *
149 * Return the new FD created upon accept() on success or else a negative errno
150 * value.
151 */
1b500e7a 152static int handle_registration(struct lttcomm_sock *reg_sock,
022d91ba 153 struct agent_app **r_app)
f20baf8e
DG
154{
155 int ret;
156 pid_t pid;
9474416f 157 uint32_t major_version, minor_version;
f20baf8e 158 ssize_t size;
fefd409b 159 enum lttng_domain_type domain;
022d91ba
DG
160 struct agent_app *app;
161 struct agent_register_msg msg;
f20baf8e
DG
162 struct lttcomm_sock *new_sock;
163
164 assert(reg_sock);
165
166 new_sock = reg_sock->ops->accept(reg_sock);
167 if (!new_sock) {
168 ret = -ENOTCONN;
169 goto error;
170 }
171
172 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
173 if (size < sizeof(msg)) {
79865500 174 ret = -EINVAL;
f20baf8e
DG
175 goto error_socket;
176 }
fefd409b 177 domain = be32toh(msg.domain);
f20baf8e 178 pid = be32toh(msg.pid);
9474416f
DG
179 major_version = be32toh(msg.major_version);
180 minor_version = be32toh(msg.minor_version);
181
182 /* Test communication protocol version of the registring agent. */
183 if (major_version != AGENT_MAJOR_VERSION) {
184 ret = -EINVAL;
185 goto error_socket;
186 }
187 if (minor_version != AGENT_MINOR_VERSION) {
188 ret = -EINVAL;
189 goto error_socket;
190 }
f20baf8e 191
fefd409b
DG
192 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
193 pid, domain, new_sock->fd);
f20baf8e 194
fefd409b 195 app = agent_create_app(pid, domain, new_sock);
f20baf8e
DG
196 if (!app) {
197 ret = -ENOMEM;
198 goto error_socket;
199 }
200
201 /*
202 * Add before assigning the socket value to the UST app so it can be found
203 * concurrently.
204 */
022d91ba 205 agent_add_app(app);
f20baf8e
DG
206
207 /*
022d91ba
DG
208 * We don't need to attach the agent app to the app. If we ever do so, we
209 * should consider both registration order of agent before app and app
210 * before agent.
f20baf8e 211 */
f20baf8e 212
1b500e7a
DG
213 if (r_app) {
214 *r_app = app;
215 }
216
f20baf8e
DG
217 return new_sock->fd;
218
219error_socket:
220 new_sock->ops->close(new_sock);
221 lttcomm_destroy_sock(new_sock);
222error:
223 return ret;
224}
225
4d076222
DG
226/*
227 * This thread manage application notify communication.
228 */
022d91ba 229void *agent_thread_manage_registration(void *data)
4d076222
DG
230{
231 int i, ret, pollfd;
232 uint32_t revents, nb_fd;
233 struct lttng_poll_event events;
234 struct lttcomm_sock *reg_sock;
235
022d91ba 236 DBG("[agent-thread] Manage agent application registration.");
4d076222
DG
237
238 rcu_register_thread();
239 rcu_thread_online();
240
022d91ba
DG
241 /* Agent initialization call MUST be called before starting the thread. */
242 assert(agent_apps_ht_by_sock);
f20baf8e 243
4d076222
DG
244 /* Create pollset with size 2, quit pipe and socket. */
245 ret = sessiond_set_thread_pollset(&events, 2);
246 if (ret < 0) {
247 goto error_poll_create;
248 }
249
250 reg_sock = init_tcp_socket();
7eac7803 251 sessiond_notify_ready();
4d076222
DG
252 if (!reg_sock) {
253 goto error_tcp_socket;
254 }
255
427392b4 256 /* Add TCP socket to poll set. */
4d076222
DG
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
03e43155 299 if (revents & LPOLLIN) {
f20baf8e 300 int new_fd;
022d91ba 301 struct agent_app *app = NULL;
f20baf8e 302
f20baf8e 303 assert(pollfd == reg_sock->fd);
1b500e7a 304 new_fd = handle_registration(reg_sock, &app);
f20baf8e 305 if (new_fd < 0) {
f20baf8e
DG
306 continue;
307 }
1b500e7a
DG
308 /* Should not have a NULL app on success. */
309 assert(app);
f20baf8e 310
03e43155
MD
311 /*
312 * Since this is a command socket (write then read),
313 * only add poll error event to only detect shutdown.
314 */
f20baf8e
DG
315 ret = lttng_poll_add(&events, new_fd,
316 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
317 if (ret < 0) {
6a4e4039 318 agent_destroy_app_by_sock(new_fd);
f20baf8e
DG
319 continue;
320 }
321
322 /* Update newly registered app. */
fefd409b 323 update_agent_app(app);
1b500e7a
DG
324
325 /* On failure, the poll will detect it and clean it up. */
03e43155
MD
326 ret = agent_send_registration_done(app);
327 if (ret < 0) {
328 /* Removing from the poll set */
329 ret = lttng_poll_del(&events, new_fd);
330 if (ret < 0) {
331 goto error;
332 }
333 agent_destroy_app_by_sock(new_fd);
334 continue;
335 }
336 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
337 /* Removing from the poll set */
338 ret = lttng_poll_del(&events, pollfd);
339 if (ret < 0) {
340 goto error;
341 }
342 agent_destroy_app_by_sock(pollfd);
4d076222 343 } else {
03e43155
MD
344 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
345 goto error;
4d076222
DG
346 }
347 }
348 }
349
350exit:
f20baf8e
DG
351 /* Whatever happens, try to delete it and exit. */
352 (void) lttng_poll_del(&events, reg_sock->fd);
4d076222
DG
353error:
354 destroy_tcp_socket(reg_sock);
355error_tcp_socket:
356 lttng_poll_clean(&events);
357error_poll_create:
022d91ba 358 DBG("[agent-thread] is cleaning up and stopping.");
4d076222
DG
359
360 rcu_thread_offline();
361 rcu_unregister_thread();
362 return NULL;
363}
This page took 0.105555 seconds and 5 git commands to generate.