Load session configurations from lttng-sessiond's main thread
[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"
8a7e4590 34#include "thread.h"
4d076222 35
f28f9e44
JG
36static int agent_tracing_enabled = -1;
37
4d076222
DG
38/*
39 * Note that there is not port here. It's set after this URI is parsed so we
40 * can let the user define a custom one. However, localhost is ALWAYS the
41 * default listening address.
42 */
fa91dc52
MD
43static const char *default_reg_uri =
44 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
4d076222 45
f20baf8e 46/*
022d91ba 47 * Update agent application using the given socket. This is done just after
f20baf8e
DG
48 * registration was successful.
49 *
50 * This is a quite heavy call in terms of locking since the session list lock
51 * AND session lock are acquired.
52 */
fefd409b 53static void update_agent_app(struct agent_app *app)
f20baf8e
DG
54{
55 struct ltt_session *session, *stmp;
56 struct ltt_session_list *list;
57
58 list = session_get_list();
59 assert(list);
60
61 session_lock_list();
62 cds_list_for_each_entry_safe(session, stmp, &list->head, list) {
e32d7f27
JG
63 if (!session_get(session)) {
64 continue;
65 }
66
f20baf8e
DG
67 session_lock(session);
68 if (session->ust_session) {
fefd409b
DG
69 struct agent *agt;
70
4da703ad 71 rcu_read_lock();
fefd409b
DG
72 agt = trace_ust_find_agent(session->ust_session, app->domain);
73 if (agt) {
74 agent_update(agt, app->sock->fd);
75 }
4da703ad 76 rcu_read_unlock();
f20baf8e
DG
77 }
78 session_unlock(session);
e32d7f27 79 session_put(session);
f20baf8e
DG
80 }
81 session_unlock_list();
82}
83
4d076222
DG
84/*
85 * Create and init socket from uri.
86 */
87static struct lttcomm_sock *init_tcp_socket(void)
88{
89 int ret;
90 struct lttng_uri *uri = NULL;
91 struct lttcomm_sock *sock = NULL;
2288467f
JG
92 unsigned int port;
93 bool bind_succeeded = false;
4d076222
DG
94
95 /*
96 * This should never fail since the URI is hardcoded and the port is set
97 * before this thread is launched.
98 */
99 ret = uri_parse(default_reg_uri, &uri);
100 assert(ret);
2288467f
JG
101 assert(config.agent_tcp_port.begin > 0);
102 uri->port = config.agent_tcp_port.begin;
4d076222
DG
103
104 sock = lttcomm_alloc_sock_from_uri(uri);
105 uri_free(uri);
106 if (sock == NULL) {
022d91ba 107 ERR("[agent-thread] agent allocating TCP socket");
4d076222
DG
108 goto error;
109 }
110
111 ret = lttcomm_create_sock(sock);
112 if (ret < 0) {
113 goto error;
114 }
115
2288467f
JG
116 for (port = config.agent_tcp_port.begin;
117 port <= config.agent_tcp_port.end; port++) {
118 ret = lttcomm_sock_set_port(sock, (uint16_t) port);
119 if (ret) {
120 ERR("[agent-thread] Failed to set port %u on socket",
121 port);
122 goto error;
123 }
124 DBG3("[agent-thread] Trying to bind on port %u", port);
125 ret = sock->ops->bind(sock);
126 if (!ret) {
127 bind_succeeded = true;
128 break;
129 }
130
131 if (errno == EADDRINUSE) {
132 DBG("Failed to bind to port %u since it is already in use",
133 port);
134 } else {
135 PERROR("Failed to bind to port %u", port);
136 goto error;
137 }
138 }
139
140 if (!bind_succeeded) {
141 if (config.agent_tcp_port.begin == config.agent_tcp_port.end) {
142 WARN("Another process is already using the agent port %i. "
143 "Agent support will be deactivated.",
144 config.agent_tcp_port.begin);
145 goto error;
146 } else {
147 WARN("All ports in the range [%i, %i] are already in use. "
148 "Agent support will be deactivated.",
149 config.agent_tcp_port.begin,
150 config.agent_tcp_port.end);
151 goto error;
152 }
4d076222
DG
153 }
154
155 ret = sock->ops->listen(sock, -1);
156 if (ret < 0) {
157 goto error;
158 }
159
022d91ba 160 DBG("[agent-thread] Listening on TCP port %u and socket %d",
2288467f 161 port, sock->fd);
4d076222
DG
162
163 return sock;
164
165error:
166 if (sock) {
167 lttcomm_destroy_sock(sock);
168 }
169 return NULL;
170}
171
172/*
173 * Close and destroy the given TCP socket.
174 */
175static void destroy_tcp_socket(struct lttcomm_sock *sock)
176{
2288467f
JG
177 int ret;
178 uint16_t port;
179
4d076222
DG
180 assert(sock);
181
2288467f
JG
182 ret = lttcomm_sock_get_port(sock, &port);
183 if (ret) {
184 ERR("[agent-thread] Failed to get port of agent TCP socket");
185 port = 0;
186 }
187
188 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16,
189 port);
4d076222
DG
190
191 /* This will return gracefully if fd is invalid. */
192 sock->ops->close(sock);
193 lttcomm_destroy_sock(sock);
194}
195
f20baf8e 196/*
022d91ba
DG
197 * Handle a new agent registration using the reg socket. After that, a new
198 * agent application is added to the global hash table and attach to an UST app
1b500e7a 199 * object. If r_app is not NULL, the created app is set to the pointer.
f20baf8e
DG
200 *
201 * Return the new FD created upon accept() on success or else a negative errno
202 * value.
203 */
1b500e7a 204static int handle_registration(struct lttcomm_sock *reg_sock,
022d91ba 205 struct agent_app **r_app)
f20baf8e
DG
206{
207 int ret;
208 pid_t pid;
9474416f 209 uint32_t major_version, minor_version;
f20baf8e 210 ssize_t size;
fefd409b 211 enum lttng_domain_type domain;
022d91ba
DG
212 struct agent_app *app;
213 struct agent_register_msg msg;
f20baf8e
DG
214 struct lttcomm_sock *new_sock;
215
216 assert(reg_sock);
217
218 new_sock = reg_sock->ops->accept(reg_sock);
219 if (!new_sock) {
220 ret = -ENOTCONN;
221 goto error;
222 }
223
224 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
225 if (size < sizeof(msg)) {
79865500 226 ret = -EINVAL;
f20baf8e
DG
227 goto error_socket;
228 }
fefd409b 229 domain = be32toh(msg.domain);
f20baf8e 230 pid = be32toh(msg.pid);
9474416f
DG
231 major_version = be32toh(msg.major_version);
232 minor_version = be32toh(msg.minor_version);
233
234 /* Test communication protocol version of the registring agent. */
235 if (major_version != AGENT_MAJOR_VERSION) {
236 ret = -EINVAL;
237 goto error_socket;
238 }
239 if (minor_version != AGENT_MINOR_VERSION) {
240 ret = -EINVAL;
241 goto error_socket;
242 }
f20baf8e 243
fefd409b
DG
244 DBG2("[agent-thread] New registration for pid %d domain %d on socket %d",
245 pid, domain, new_sock->fd);
f20baf8e 246
fefd409b 247 app = agent_create_app(pid, domain, new_sock);
f20baf8e
DG
248 if (!app) {
249 ret = -ENOMEM;
250 goto error_socket;
251 }
252
253 /*
254 * Add before assigning the socket value to the UST app so it can be found
255 * concurrently.
256 */
022d91ba 257 agent_add_app(app);
f20baf8e
DG
258
259 /*
022d91ba
DG
260 * We don't need to attach the agent app to the app. If we ever do so, we
261 * should consider both registration order of agent before app and app
262 * before agent.
f20baf8e 263 */
f20baf8e 264
1b500e7a
DG
265 if (r_app) {
266 *r_app = app;
267 }
268
f20baf8e
DG
269 return new_sock->fd;
270
271error_socket:
272 new_sock->ops->close(new_sock);
273 lttcomm_destroy_sock(new_sock);
274error:
275 return ret;
276}
277
f28f9e44
JG
278bool agent_tracing_is_enabled(void)
279{
280 int enabled;
281
282 enabled = uatomic_read(&agent_tracing_enabled);
283 assert(enabled != -1);
284 return enabled == 1;
285}
286
2288467f
JG
287/*
288 * Write agent TCP port using the rundir.
289 */
290static int write_agent_port(uint16_t port)
291{
292 return utils_create_pid_file((pid_t) port,
293 config.agent_port_file_path.value);
294}
295
4d076222
DG
296/*
297 * This thread manage application notify communication.
298 */
8a7e4590 299static void *thread_agent_management(void *data)
4d076222
DG
300{
301 int i, ret, pollfd;
302 uint32_t revents, nb_fd;
303 struct lttng_poll_event events;
304 struct lttcomm_sock *reg_sock;
8a7e4590
JG
305 struct lttng_pipe *quit_pipe = data;
306 const int quit_pipe_read_fd = lttng_pipe_get_readfd(quit_pipe);
4d076222 307
022d91ba 308 DBG("[agent-thread] Manage agent application registration.");
4d076222
DG
309
310 rcu_register_thread();
311 rcu_thread_online();
312
022d91ba
DG
313 /* Agent initialization call MUST be called before starting the thread. */
314 assert(agent_apps_ht_by_sock);
f20baf8e 315
8a7e4590
JG
316 /* Create pollset with size 2, quit pipe and registration socket. */
317 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
4d076222
DG
318 if (ret < 0) {
319 goto error_poll_create;
320 }
321
8a7e4590
JG
322 ret = lttng_poll_add(&events, quit_pipe_read_fd,
323 LPOLLIN | LPOLLERR);
324 if (ret < 0) {
325 goto error_tcp_socket;
326 }
327
4d076222 328 reg_sock = init_tcp_socket();
2288467f
JG
329 if (reg_sock) {
330 uint16_t port;
331
332 assert(lttcomm_sock_get_port(reg_sock, &port) == 0);
333
334 ret = write_agent_port(port);
335 if (ret) {
336 ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable");
337 /* Don't prevent the launch of the sessiond on error. */
338 sessiond_notify_ready();
339 goto error;
340 }
341 } else {
342 /* Don't prevent the launch of the sessiond on error. */
343 sessiond_notify_ready();
344 goto error_tcp_socket;
345 }
f28f9e44
JG
346
347 /*
348 * Signal that the agent thread is ready. The command thread
349 * may start to query whether or not agent tracing is enabled.
350 */
2288467f 351 uatomic_set(&agent_tracing_enabled, 1);
7eac7803 352 sessiond_notify_ready();
4d076222 353
427392b4 354 /* Add TCP socket to poll set. */
4d076222
DG
355 ret = lttng_poll_add(&events, reg_sock->fd,
356 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
357 if (ret < 0) {
358 goto error;
359 }
360
361 while (1) {
546f19b5 362 DBG3("[agent-thread] Manage agent polling");
4d076222
DG
363
364 /* Inifinite blocking call, waiting for transmission */
365restart:
366 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
367 DBG3("[agent-thread] Manage agent return from poll on %d fds",
368 LTTNG_POLL_GETNB(&events));
4d076222
DG
369 if (ret < 0) {
370 /*
371 * Restart interrupted system call.
372 */
373 if (errno == EINTR) {
374 goto restart;
375 }
376 goto error;
377 }
378 nb_fd = ret;
022d91ba 379 DBG3("[agent-thread] %d fd ready", nb_fd);
4d076222
DG
380
381 for (i = 0; i < nb_fd; i++) {
382 /* Fetch once the poll data */
383 revents = LTTNG_POLL_GETEV(&events, i);
384 pollfd = LTTNG_POLL_GETFD(&events, i);
385
fd20dac9
MD
386 if (!revents) {
387 /* No activity for this FD (poll implementation). */
388 continue;
389 }
390
4d076222 391 /* Thread quit pipe has been closed. Killing thread. */
8a7e4590 392 if (pollfd == quit_pipe_read_fd) {
4d076222
DG
393 goto exit;
394 }
395
03e43155 396 if (revents & LPOLLIN) {
f20baf8e 397 int new_fd;
022d91ba 398 struct agent_app *app = NULL;
f20baf8e 399
f20baf8e 400 assert(pollfd == reg_sock->fd);
1b500e7a 401 new_fd = handle_registration(reg_sock, &app);
f20baf8e 402 if (new_fd < 0) {
f20baf8e
DG
403 continue;
404 }
1b500e7a
DG
405 /* Should not have a NULL app on success. */
406 assert(app);
f20baf8e 407
03e43155
MD
408 /*
409 * Since this is a command socket (write then read),
410 * only add poll error event to only detect shutdown.
411 */
f20baf8e
DG
412 ret = lttng_poll_add(&events, new_fd,
413 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
414 if (ret < 0) {
6a4e4039 415 agent_destroy_app_by_sock(new_fd);
f20baf8e
DG
416 continue;
417 }
418
419 /* Update newly registered app. */
fefd409b 420 update_agent_app(app);
1b500e7a
DG
421
422 /* On failure, the poll will detect it and clean it up. */
03e43155
MD
423 ret = agent_send_registration_done(app);
424 if (ret < 0) {
425 /* Removing from the poll set */
426 ret = lttng_poll_del(&events, new_fd);
427 if (ret < 0) {
428 goto error;
429 }
430 agent_destroy_app_by_sock(new_fd);
431 continue;
432 }
433 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
434 /* Removing from the poll set */
435 ret = lttng_poll_del(&events, pollfd);
436 if (ret < 0) {
437 goto error;
438 }
439 agent_destroy_app_by_sock(pollfd);
4d076222 440 } else {
03e43155
MD
441 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
442 goto error;
4d076222
DG
443 }
444 }
445 }
446
447exit:
f20baf8e
DG
448 /* Whatever happens, try to delete it and exit. */
449 (void) lttng_poll_del(&events, reg_sock->fd);
4d076222
DG
450error:
451 destroy_tcp_socket(reg_sock);
452error_tcp_socket:
453 lttng_poll_clean(&events);
454error_poll_create:
2288467f 455 uatomic_set(&agent_tracing_enabled, 0);
8a7e4590 456 DBG("[agent-thread] Cleaning up and stopping.");
4d076222
DG
457 rcu_thread_offline();
458 rcu_unregister_thread();
459 return NULL;
460}
8a7e4590
JG
461
462static bool shutdown_agent_management_thread(void *data)
463{
464 struct lttng_pipe *quit_pipe = data;
465 const int write_fd = lttng_pipe_get_writefd(quit_pipe);
466
467 return notify_thread_pipe(write_fd) == 1;
468}
469
5b093681
JG
470static void cleanup_agent_management_thread(void *data)
471{
472 struct lttng_pipe *quit_pipe = data;
473
474 lttng_pipe_destroy(quit_pipe);
475}
476
477bool launch_agent_management_thread(void)
8a7e4590
JG
478{
479 struct lttng_pipe *quit_pipe;
480 struct lttng_thread *thread;
481
482 quit_pipe = lttng_pipe_open(FD_CLOEXEC);
483 if (!quit_pipe) {
484 goto error;
485 }
486 thread = lttng_thread_create("Agent management",
487 thread_agent_management,
488 shutdown_agent_management_thread,
5b093681 489 cleanup_agent_management_thread,
8a7e4590
JG
490 quit_pipe);
491 if (!thread) {
492 goto error;
493 }
494
495 lttng_thread_put(thread);
496 return true;
497error:
5b093681 498 cleanup_agent_management_thread(quit_pipe);
8a7e4590
JG
499 return false;
500}
This page took 0.114006 seconds and 5 git commands to generate.