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