Generalize set_ust_event_exclusion to support multiple types of ust object
[lttng-tools.git] / src / bin / lttng-sessiond / agent-thread.c
1 /*
2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
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
16 #include <common/compat/endian.h>
17
18 #include "fd-limit.h"
19 #include "agent-thread.h"
20 #include "agent.h"
21 #include "lttng-sessiond.h"
22 #include "session.h"
23 #include "utils.h"
24 #include "thread.h"
25
26 struct thread_notifiers {
27 struct lttng_pipe *quit_pipe;
28 sem_t ready;
29 };
30
31 struct agent_app_id {
32 pid_t pid;
33 enum lttng_domain_type domain;
34 };
35
36 struct agent_protocol_version {
37 unsigned int major, minor;
38 };
39
40 static int agent_tracing_enabled = -1;
41
42 /*
43 * Note that there is not port here. It's set after this URI is parsed so we
44 * can let the user define a custom one. However, localhost is ALWAYS the
45 * default listening address.
46 */
47 static const char *default_reg_uri =
48 "tcp://" DEFAULT_NETWORK_VIEWER_BIND_ADDRESS;
49
50 /*
51 * Update agent application using the given socket. This is done just after
52 * registration was successful.
53 *
54 * This will acquire the various sessions' lock; none must be held by the
55 * caller.
56 * The caller must hold the session list lock.
57 */
58 static void update_agent_app(const struct agent_app *app)
59 {
60 struct ltt_session *session, *stmp;
61 struct ltt_session_list *list;
62
63 list = session_get_list();
64 assert(list);
65
66 cds_list_for_each_entry_safe(session, stmp, &list->head, list) {
67 if (!session_get(session)) {
68 continue;
69 }
70
71 session_lock(session);
72 if (session->ust_session) {
73 const struct agent *agt;
74
75 rcu_read_lock();
76 agt = trace_ust_find_agent(session->ust_session, app->domain);
77 if (agt) {
78 agent_update(agt, app);
79 }
80 rcu_read_unlock();
81 }
82 session_unlock(session);
83 session_put(session);
84 }
85 }
86
87 /*
88 * Create and init socket from uri.
89 */
90 static struct lttcomm_sock *init_tcp_socket(void)
91 {
92 int ret;
93 struct lttng_uri *uri = NULL;
94 struct lttcomm_sock *sock = NULL;
95 unsigned int port;
96 bool bind_succeeded = false;
97
98 /*
99 * This should never fail since the URI is hardcoded and the port is set
100 * before this thread is launched.
101 */
102 ret = uri_parse(default_reg_uri, &uri);
103 assert(ret);
104 assert(config.agent_tcp_port.begin > 0);
105 uri->port = config.agent_tcp_port.begin;
106
107 sock = lttcomm_alloc_sock_from_uri(uri);
108 uri_free(uri);
109 if (sock == NULL) {
110 ERR("[agent-thread] agent allocating TCP socket");
111 goto error;
112 }
113
114 ret = lttcomm_create_sock(sock);
115 if (ret < 0) {
116 goto error;
117 }
118
119 for (port = config.agent_tcp_port.begin;
120 port <= config.agent_tcp_port.end; port++) {
121 ret = lttcomm_sock_set_port(sock, (uint16_t) port);
122 if (ret) {
123 ERR("[agent-thread] Failed to set port %u on socket",
124 port);
125 goto error;
126 }
127 DBG3("[agent-thread] Trying to bind on port %u", port);
128 ret = sock->ops->bind(sock);
129 if (!ret) {
130 bind_succeeded = true;
131 break;
132 }
133
134 if (errno == EADDRINUSE) {
135 DBG("Failed to bind to port %u since it is already in use",
136 port);
137 } else {
138 PERROR("Failed to bind to port %u", port);
139 goto error;
140 }
141 }
142
143 if (!bind_succeeded) {
144 if (config.agent_tcp_port.begin == config.agent_tcp_port.end) {
145 WARN("Another process is already using the agent port %i. "
146 "Agent support will be deactivated.",
147 config.agent_tcp_port.begin);
148 goto error;
149 } else {
150 WARN("All ports in the range [%i, %i] are already in use. "
151 "Agent support will be deactivated.",
152 config.agent_tcp_port.begin,
153 config.agent_tcp_port.end);
154 goto error;
155 }
156 }
157
158 ret = sock->ops->listen(sock, -1);
159 if (ret < 0) {
160 goto error;
161 }
162
163 DBG("[agent-thread] Listening on TCP port %u and socket %d",
164 port, sock->fd);
165
166 return sock;
167
168 error:
169 if (sock) {
170 lttcomm_destroy_sock(sock);
171 }
172 return NULL;
173 }
174
175 /*
176 * Close and destroy the given TCP socket.
177 */
178 static void destroy_tcp_socket(struct lttcomm_sock *sock)
179 {
180 int ret;
181 uint16_t port;
182
183 assert(sock);
184
185 ret = lttcomm_sock_get_port(sock, &port);
186 if (ret) {
187 ERR("[agent-thread] Failed to get port of agent TCP socket");
188 port = 0;
189 }
190
191 DBG3("[agent-thread] Destroy TCP socket on port %" PRIu16,
192 port);
193
194 /* This will return gracefully if fd is invalid. */
195 sock->ops->close(sock);
196 lttcomm_destroy_sock(sock);
197 }
198
199 static const char *domain_type_str(enum lttng_domain_type domain_type)
200 {
201 switch (domain_type) {
202 case LTTNG_DOMAIN_NONE:
203 return "none";
204 case LTTNG_DOMAIN_KERNEL:
205 return "kernel";
206 case LTTNG_DOMAIN_UST:
207 return "ust";
208 case LTTNG_DOMAIN_JUL:
209 return "jul";
210 case LTTNG_DOMAIN_LOG4J:
211 return "log4j";
212 case LTTNG_DOMAIN_PYTHON:
213 return "python";
214 default:
215 return "unknown";
216 }
217 }
218
219 static bool is_agent_protocol_version_supported(
220 const struct agent_protocol_version *version)
221 {
222 const bool is_supported = version->major == AGENT_MAJOR_VERSION &&
223 version->minor == AGENT_MINOR_VERSION;
224
225 if (!is_supported) {
226 WARN("Refusing agent connection: unsupported protocol version %ui.%ui, expected %i.%i",
227 version->major, version->minor,
228 AGENT_MAJOR_VERSION, AGENT_MINOR_VERSION);
229 }
230
231 return is_supported;
232 }
233
234 /*
235 * Handle a new agent connection on the registration socket.
236 *
237 * Returns 0 on success, or else a negative errno value.
238 * On success, the resulting socket is returned through `agent_app_socket`
239 * and the application's reported id is updated through `agent_app_id`.
240 */
241 static int accept_agent_connection(
242 struct lttcomm_sock *reg_sock,
243 struct agent_app_id *agent_app_id,
244 struct lttcomm_sock **agent_app_socket)
245 {
246 int ret;
247 struct agent_protocol_version agent_version;
248 ssize_t size;
249 struct agent_register_msg msg;
250 struct lttcomm_sock *new_sock;
251
252 assert(reg_sock);
253
254 new_sock = reg_sock->ops->accept(reg_sock);
255 if (!new_sock) {
256 ret = -ENOTCONN;
257 goto end;
258 }
259
260 size = new_sock->ops->recvmsg(new_sock, &msg, sizeof(msg), 0);
261 if (size < sizeof(msg)) {
262 if (size < 0) {
263 PERROR("Failed to register new agent application");
264 } else if (size != 0) {
265 ERR("Failed to register new agent application: invalid registration message length: expected length = %zu, message length = %zd",
266 sizeof(msg), size);
267 } else {
268 DBG("Failed to register new agent application: connection closed");
269 }
270 ret = -EINVAL;
271 goto error_close_socket;
272 }
273
274 agent_version = (struct agent_protocol_version) {
275 be32toh(msg.major_version),
276 be32toh(msg.minor_version),
277 };
278
279 /* Test communication protocol version of the registering agent. */
280 if (!is_agent_protocol_version_supported(&agent_version)) {
281 ret = -EINVAL;
282 goto error_close_socket;
283 }
284
285 *agent_app_id = (struct agent_app_id) {
286 .domain = (enum lttng_domain_type) be32toh(msg.domain),
287 .pid = (pid_t) be32toh(msg.pid),
288 };
289
290 DBG2("New registration for agent application: pid = %ld, domain = %s, socket fd = %d",
291 (long) agent_app_id->pid,
292 domain_type_str(agent_app_id->domain), new_sock->fd);
293
294 *agent_app_socket = new_sock;
295 new_sock = NULL;
296 ret = 0;
297 goto end;
298
299 error_close_socket:
300 new_sock->ops->close(new_sock);
301 lttcomm_destroy_sock(new_sock);
302 end:
303 return ret;
304 }
305
306 bool agent_tracing_is_enabled(void)
307 {
308 int enabled;
309
310 enabled = uatomic_read(&agent_tracing_enabled);
311 assert(enabled != -1);
312 return enabled == 1;
313 }
314
315 /*
316 * Write agent TCP port using the rundir.
317 */
318 static int write_agent_port(uint16_t port)
319 {
320 return utils_create_pid_file((pid_t) port,
321 config.agent_port_file_path.value);
322 }
323
324 static
325 void mark_thread_as_ready(struct thread_notifiers *notifiers)
326 {
327 DBG("Marking agent management thread as ready");
328 sem_post(&notifiers->ready);
329 }
330
331 static
332 void wait_until_thread_is_ready(struct thread_notifiers *notifiers)
333 {
334 DBG("Waiting for agent management thread to be ready");
335 sem_wait(&notifiers->ready);
336 DBG("Agent management thread is ready");
337 }
338
339 /*
340 * This thread manage application notify communication.
341 */
342 static void *thread_agent_management(void *data)
343 {
344 int i, ret, pollfd;
345 uint32_t revents, nb_fd;
346 struct lttng_poll_event events;
347 struct lttcomm_sock *reg_sock;
348 struct thread_notifiers *notifiers = data;
349 const int quit_pipe_read_fd = lttng_pipe_get_readfd(
350 notifiers->quit_pipe);
351
352 DBG("[agent-thread] Manage agent application registration.");
353
354 rcu_register_thread();
355 rcu_thread_online();
356
357 /* Agent initialization call MUST be called before starting the thread. */
358 assert(agent_apps_ht_by_sock);
359
360 /* Create pollset with size 2, quit pipe and registration socket. */
361 ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC);
362 if (ret < 0) {
363 goto error_poll_create;
364 }
365
366 ret = lttng_poll_add(&events, quit_pipe_read_fd,
367 LPOLLIN | LPOLLERR);
368 if (ret < 0) {
369 goto error_tcp_socket;
370 }
371
372 reg_sock = init_tcp_socket();
373 if (reg_sock) {
374 uint16_t port;
375
376 assert(lttcomm_sock_get_port(reg_sock, &port) == 0);
377
378 ret = write_agent_port(port);
379 if (ret) {
380 ERR("[agent-thread] Failed to create agent port file: agent tracing will be unavailable");
381 /* Don't prevent the launch of the sessiond on error. */
382 mark_thread_as_ready(notifiers);
383 goto error;
384 }
385 } else {
386 /* Don't prevent the launch of the sessiond on error. */
387 mark_thread_as_ready(notifiers);
388 goto error_tcp_socket;
389 }
390
391 /*
392 * Signal that the agent thread is ready. The command thread
393 * may start to query whether or not agent tracing is enabled.
394 */
395 uatomic_set(&agent_tracing_enabled, 1);
396 mark_thread_as_ready(notifiers);
397
398 /* Add TCP socket to the poll set. */
399 ret = lttng_poll_add(&events, reg_sock->fd,
400 LPOLLIN | LPOLLERR | LPOLLHUP | LPOLLRDHUP);
401 if (ret < 0) {
402 goto error;
403 }
404
405 while (1) {
406 DBG3("[agent-thread] Manage agent polling");
407
408 /* Inifinite blocking call, waiting for transmission */
409 restart:
410 ret = lttng_poll_wait(&events, -1);
411 DBG3("[agent-thread] Manage agent return from poll on %d fds",
412 LTTNG_POLL_GETNB(&events));
413 if (ret < 0) {
414 /*
415 * Restart interrupted system call.
416 */
417 if (errno == EINTR) {
418 goto restart;
419 }
420 goto error;
421 }
422 nb_fd = ret;
423 DBG3("[agent-thread] %d fd ready", nb_fd);
424
425 for (i = 0; i < nb_fd; i++) {
426 /* Fetch once the poll data */
427 revents = LTTNG_POLL_GETEV(&events, i);
428 pollfd = LTTNG_POLL_GETFD(&events, i);
429
430 /* Thread quit pipe has been closed. Killing thread. */
431 if (pollfd == quit_pipe_read_fd) {
432 goto exit;
433 }
434
435 /* Activity on the registration socket. */
436 if (revents & LPOLLIN) {
437 struct agent_app_id new_app_id;
438 struct agent_app *new_app = NULL;
439 struct lttcomm_sock *new_app_socket;
440 int new_app_socket_fd;
441
442 assert(pollfd == reg_sock->fd);
443
444 ret = accept_agent_connection(
445 reg_sock, &new_app_id, &new_app_socket);
446 if (ret < 0) {
447 /* Errors are already logged. */
448 continue;
449 }
450
451 /*
452 * new_app_socket's ownership has been
453 * transferred to the new agent app.
454 */
455 new_app = agent_create_app(new_app_id.pid,
456 new_app_id.domain,
457 new_app_socket);
458 if (!new_app) {
459 new_app_socket->ops->close(
460 new_app_socket);
461 continue;
462 }
463 new_app_socket_fd = new_app_socket->fd;
464 new_app_socket = NULL;
465
466 /*
467 * Since this is a command socket (write then
468 * read), only add poll error event to only
469 * detect shutdown.
470 */
471 ret = lttng_poll_add(&events, new_app_socket_fd,
472 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
473 if (ret < 0) {
474 agent_destroy_app(new_app);
475 continue;
476 }
477
478 /*
479 * Prevent sessions from being modified while
480 * the agent application's configuration is
481 * updated.
482 */
483 session_lock_list();
484
485 /*
486 * Update the newly registered applications's
487 * configuration.
488 */
489 update_agent_app(new_app);
490
491 ret = agent_send_registration_done(new_app);
492 if (ret < 0) {
493 agent_destroy_app(new_app);
494 /* Removing from the poll set. */
495 ret = lttng_poll_del(&events,
496 new_app_socket_fd);
497 if (ret < 0) {
498 session_unlock_list();
499 goto error;
500 }
501 continue;
502 }
503
504 /* Publish the new agent app. */
505 agent_add_app(new_app);
506
507 session_unlock_list();
508 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
509 /* Removing from the poll set */
510 ret = lttng_poll_del(&events, pollfd);
511 if (ret < 0) {
512 goto error;
513 }
514 agent_destroy_app_by_sock(pollfd);
515 } else {
516 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
517 goto error;
518 }
519 }
520 }
521
522 exit:
523 /* Whatever happens, try to delete it and exit. */
524 (void) lttng_poll_del(&events, reg_sock->fd);
525 error:
526 destroy_tcp_socket(reg_sock);
527 error_tcp_socket:
528 lttng_poll_clean(&events);
529 error_poll_create:
530 uatomic_set(&agent_tracing_enabled, 0);
531 DBG("[agent-thread] Cleaning up and stopping.");
532 rcu_thread_offline();
533 rcu_unregister_thread();
534 return NULL;
535 }
536
537 static bool shutdown_agent_management_thread(void *data)
538 {
539 struct thread_notifiers *notifiers = data;
540 const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe);
541
542 return notify_thread_pipe(write_fd) == 1;
543 }
544
545 static void cleanup_agent_management_thread(void *data)
546 {
547 struct thread_notifiers *notifiers = data;
548
549 lttng_pipe_destroy(notifiers->quit_pipe);
550 sem_destroy(&notifiers->ready);
551 free(notifiers);
552 }
553
554 bool launch_agent_management_thread(void)
555 {
556 struct thread_notifiers *notifiers;
557 struct lttng_thread *thread;
558
559 notifiers = zmalloc(sizeof(*notifiers));
560 if (!notifiers) {
561 goto error_alloc;
562 }
563
564 sem_init(&notifiers->ready, 0, 0);
565 notifiers->quit_pipe = lttng_pipe_open(FD_CLOEXEC);
566 if (!notifiers->quit_pipe) {
567 goto error;
568 }
569 thread = lttng_thread_create("Agent management",
570 thread_agent_management,
571 shutdown_agent_management_thread,
572 cleanup_agent_management_thread,
573 notifiers);
574 if (!thread) {
575 goto error;
576 }
577 wait_until_thread_is_ready(notifiers);
578 lttng_thread_put(thread);
579 return true;
580 error:
581 cleanup_agent_management_thread(notifiers);
582 error_alloc:
583 return false;
584 }
This page took 0.050705 seconds and 4 git commands to generate.