Fix: file-descriptor: missing include guards
[lttng-tools.git] / src / bin / lttng-sessiond / cmd.cpp
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include "agent-thread.hpp"
11 #include "agent.hpp"
12 #include "buffer-registry.hpp"
13 #include "channel.hpp"
14 #include "cmd.hpp"
15 #include "consumer.hpp"
16 #include "event-notifier-error-accounting.hpp"
17 #include "event.hpp"
18 #include "health-sessiond.hpp"
19 #include "kernel-consumer.hpp"
20 #include "kernel.hpp"
21 #include "lttng-sessiond.hpp"
22 #include "lttng-syscall.hpp"
23 #include "notification-thread-commands.hpp"
24 #include "notification-thread.hpp"
25 #include "rotate.hpp"
26 #include "rotation-thread.hpp"
27 #include "session.hpp"
28 #include "timer.hpp"
29 #include "tracker.hpp"
30 #include "utils.hpp"
31
32 #include <common/buffer-view.hpp>
33 #include <common/common.hpp>
34 #include <common/compat/string.hpp>
35 #include <common/defaults.hpp>
36 #include <common/dynamic-buffer.hpp>
37 #include <common/kernel-ctl/kernel-ctl.hpp>
38 #include <common/payload-view.hpp>
39 #include <common/payload.hpp>
40 #include <common/relayd/relayd.hpp>
41 #include <common/sessiond-comm/sessiond-comm.hpp>
42 #include <common/string-utils/string-utils.hpp>
43 #include <common/trace-chunk.hpp>
44 #include <common/urcu.hpp>
45 #include <common/utils.hpp>
46
47 #include <lttng/action/action-internal.hpp>
48 #include <lttng/action/action.h>
49 #include <lttng/channel-internal.hpp>
50 #include <lttng/channel.h>
51 #include <lttng/condition/condition-internal.hpp>
52 #include <lttng/condition/condition.h>
53 #include <lttng/condition/event-rule-matches-internal.hpp>
54 #include <lttng/condition/event-rule-matches.h>
55 #include <lttng/error-query-internal.hpp>
56 #include <lttng/event-internal.hpp>
57 #include <lttng/event-rule/event-rule-internal.hpp>
58 #include <lttng/event-rule/event-rule.h>
59 #include <lttng/location-internal.hpp>
60 #include <lttng/lttng-error.h>
61 #include <lttng/rotate-internal.hpp>
62 #include <lttng/session-descriptor-internal.hpp>
63 #include <lttng/session-internal.hpp>
64 #include <lttng/tracker.h>
65 #include <lttng/trigger/trigger-internal.hpp>
66 #include <lttng/userspace-probe-internal.hpp>
67
68 #include <algorithm>
69 #include <inttypes.h>
70 #include <stdio.h>
71 #include <sys/stat.h>
72 #include <urcu/list.h>
73 #include <urcu/uatomic.h>
74
75 /* Sleep for 100ms between each check for the shm path's deletion. */
76 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
77
78 namespace lsu = lttng::sessiond::ust;
79
80 static enum lttng_error_code wait_on_path(void *path);
81
82 namespace {
83 struct cmd_destroy_session_reply_context {
84 int reply_sock_fd;
85 bool implicit_rotation_on_destroy;
86 /*
87 * Indicates whether or not an error occurred while launching the
88 * destruction of a session.
89 */
90 enum lttng_error_code destruction_status;
91 };
92
93 /*
94 * Command completion handler that is used by the destroy command
95 * when a session that has a non-default shm_path is being destroyed.
96 *
97 * See comment in cmd_destroy_session() for the rationale.
98 */
99 struct destroy_completion_handler {
100 struct cmd_completion_handler handler;
101 char shm_path[member_sizeof(struct ltt_session, shm_path)];
102 } destroy_completion_handler = {
103 .handler = { .run = wait_on_path, .data = destroy_completion_handler.shm_path },
104 .shm_path = { 0 },
105 };
106
107 /*
108 * Used to keep a unique index for each relayd socket created where this value
109 * is associated with streams on the consumer so it can match the right relayd
110 * to send to. It must be accessed with the relayd_net_seq_idx_lock
111 * held.
112 */
113 pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
114 uint64_t relayd_net_seq_idx;
115 } /* namespace */
116
117 static struct cmd_completion_handler *current_completion_handler;
118 static int validate_ust_event_name(const char *);
119 static int cmd_enable_event_internal(struct ltt_session *session,
120 const struct lttng_domain *domain,
121 char *channel_name,
122 struct lttng_event *event,
123 char *filter_expression,
124 struct lttng_bytecode *filter,
125 struct lttng_event_exclusion *exclusion,
126 int wpipe);
127 static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
128 const struct lttng_domain *domain,
129 const struct lttng_channel *_attr,
130 int wpipe);
131
132 /*
133 * Create a session path used by list_lttng_sessions for the case that the
134 * session consumer is on the network.
135 */
136 static int build_network_session_path(char *dst, size_t size, struct ltt_session *session)
137 {
138 int ret, kdata_port, udata_port;
139 struct lttng_uri *kuri = nullptr, *uuri = nullptr, *uri = nullptr;
140 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
141
142 LTTNG_ASSERT(session);
143 LTTNG_ASSERT(dst);
144
145 memset(tmp_urls, 0, sizeof(tmp_urls));
146 memset(tmp_uurl, 0, sizeof(tmp_uurl));
147
148 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
149
150 if (session->kernel_session && session->kernel_session->consumer) {
151 kuri = &session->kernel_session->consumer->dst.net.control;
152 kdata_port = session->kernel_session->consumer->dst.net.data.port;
153 }
154
155 if (session->ust_session && session->ust_session->consumer) {
156 uuri = &session->ust_session->consumer->dst.net.control;
157 udata_port = session->ust_session->consumer->dst.net.data.port;
158 }
159
160 if (uuri == nullptr && kuri == nullptr) {
161 uri = &session->consumer->dst.net.control;
162 kdata_port = session->consumer->dst.net.data.port;
163 } else if (kuri && uuri) {
164 ret = uri_compare(kuri, uuri);
165 if (ret) {
166 /* Not Equal */
167 uri = kuri;
168 /* Build uuri URL string */
169 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
170 if (ret < 0) {
171 goto error;
172 }
173 } else {
174 uri = kuri;
175 }
176 } else if (kuri && uuri == nullptr) {
177 uri = kuri;
178 } else if (uuri && kuri == nullptr) {
179 uri = uuri;
180 }
181
182 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
183 if (ret < 0) {
184 goto error;
185 }
186
187 /*
188 * Do we have a UST url set. If yes, this means we have both kernel and UST
189 * to print.
190 */
191 if (*tmp_uurl != '\0') {
192 ret = snprintf(dst,
193 size,
194 "[K]: %s [data: %d] -- [U]: %s [data: %d]",
195 tmp_urls,
196 kdata_port,
197 tmp_uurl,
198 udata_port);
199 } else {
200 int dport;
201 if (kuri || (!kuri && !uuri)) {
202 dport = kdata_port;
203 } else {
204 /* No kernel URI, use the UST port. */
205 dport = udata_port;
206 }
207 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
208 }
209
210 error:
211 return ret;
212 }
213
214 /*
215 * Get run-time attributes if the session has been started (discarded events,
216 * lost packets).
217 */
218 static int get_kernel_runtime_stats(struct ltt_session *session,
219 struct ltt_kernel_channel *kchan,
220 uint64_t *discarded_events,
221 uint64_t *lost_packets)
222 {
223 int ret;
224
225 if (!session->has_been_started) {
226 ret = 0;
227 *discarded_events = 0;
228 *lost_packets = 0;
229 goto end;
230 }
231
232 ret = consumer_get_discarded_events(
233 session->id, kchan->key, session->kernel_session->consumer, discarded_events);
234 if (ret < 0) {
235 goto end;
236 }
237
238 ret = consumer_get_lost_packets(
239 session->id, kchan->key, session->kernel_session->consumer, lost_packets);
240 if (ret < 0) {
241 goto end;
242 }
243
244 end:
245 return ret;
246 }
247
248 /*
249 * Get run-time attributes if the session has been started (discarded events,
250 * lost packets).
251 */
252 static int get_ust_runtime_stats(struct ltt_session *session,
253 struct ltt_ust_channel *uchan,
254 uint64_t *discarded_events,
255 uint64_t *lost_packets)
256 {
257 int ret;
258 struct ltt_ust_session *usess;
259
260 if (!discarded_events || !lost_packets) {
261 ret = -1;
262 goto end;
263 }
264
265 usess = session->ust_session;
266 LTTNG_ASSERT(discarded_events);
267 LTTNG_ASSERT(lost_packets);
268
269 if (!usess || !session->has_been_started) {
270 *discarded_events = 0;
271 *lost_packets = 0;
272 ret = 0;
273 goto end;
274 }
275
276 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
277 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
278 &usess->buffer_reg_uid_list,
279 usess->consumer,
280 uchan->id,
281 uchan->attr.overwrite,
282 discarded_events,
283 lost_packets);
284 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
285 ret = ust_app_pid_get_channel_runtime_stats(usess,
286 uchan,
287 usess->consumer,
288 uchan->attr.overwrite,
289 discarded_events,
290 lost_packets);
291 if (ret < 0) {
292 goto end;
293 }
294 *discarded_events += uchan->per_pid_closed_app_discarded;
295 *lost_packets += uchan->per_pid_closed_app_lost;
296 } else {
297 ERR("Unsupported buffer type");
298 abort();
299 ret = -1;
300 goto end;
301 }
302
303 end:
304 return ret;
305 }
306
307 /*
308 * Create a list of agent domain events.
309 *
310 * Return number of events in list on success or else a negative value.
311 */
312 static enum lttng_error_code list_lttng_agent_events(struct agent *agt,
313 struct lttng_payload *reply_payload,
314 unsigned int *nb_events)
315 {
316 enum lttng_error_code ret_code;
317 int ret = 0;
318 unsigned int local_nb_events = 0;
319 struct agent_event *event;
320 struct lttng_ht_iter iter;
321 unsigned long agent_event_count;
322
323 assert(agt);
324 assert(reply_payload);
325
326 DBG3("Listing agent events");
327
328 agent_event_count = lttng_ht_get_count(agt->events);
329 if (agent_event_count == 0) {
330 /* Early exit. */
331 goto end;
332 }
333
334 if (agent_event_count > UINT_MAX) {
335 ret_code = LTTNG_ERR_OVERFLOW;
336 goto error;
337 }
338
339 local_nb_events = (unsigned int) agent_event_count;
340
341 {
342 lttng::urcu::read_lock_guard read_lock;
343
344 cds_lfht_for_each_entry (agt->events->ht, &iter.iter, event, node.node) {
345 struct lttng_event *tmp_event = lttng_event_create();
346
347 if (!tmp_event) {
348 ret_code = LTTNG_ERR_NOMEM;
349 goto error;
350 }
351
352 if (lttng_strncpy(tmp_event->name, event->name, sizeof(tmp_event->name))) {
353 lttng_event_destroy(tmp_event);
354 ret_code = LTTNG_ERR_FATAL;
355 goto error;
356 }
357
358 tmp_event->name[sizeof(tmp_event->name) - 1] = '\0';
359 tmp_event->enabled = !!event->enabled_count;
360 tmp_event->loglevel = event->loglevel_value;
361 tmp_event->loglevel_type = event->loglevel_type;
362
363 ret = lttng_event_serialize(tmp_event,
364 0,
365 nullptr,
366 event->filter_expression,
367 0,
368 nullptr,
369 reply_payload);
370 lttng_event_destroy(tmp_event);
371 if (ret) {
372 ret_code = LTTNG_ERR_FATAL;
373 goto error;
374 }
375 }
376 }
377 end:
378 ret_code = LTTNG_OK;
379 *nb_events = local_nb_events;
380 error:
381 return ret_code;
382 }
383
384 /*
385 * Create a list of ust global domain events.
386 */
387 static enum lttng_error_code list_lttng_ust_global_events(char *channel_name,
388 struct ltt_ust_domain_global *ust_global,
389 struct lttng_payload *reply_payload,
390 unsigned int *nb_events)
391 {
392 enum lttng_error_code ret_code;
393 int ret;
394 struct lttng_ht_iter iter;
395 struct lttng_ht_node_str *node;
396 struct ltt_ust_channel *uchan;
397 struct ltt_ust_event *uevent;
398 unsigned long channel_event_count;
399 unsigned int local_nb_events = 0;
400
401 assert(reply_payload);
402 assert(nb_events);
403
404 DBG("Listing UST global events for channel %s", channel_name);
405
406 lttng::urcu::read_lock_guard read_lock;
407
408 lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
409 node = lttng_ht_iter_get_node_str(&iter);
410 if (node == nullptr) {
411 ret_code = LTTNG_ERR_UST_CHAN_NOT_FOUND;
412 goto error;
413 }
414
415 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
416
417 channel_event_count = lttng_ht_get_count(uchan->events);
418 if (channel_event_count == 0) {
419 /* Early exit. */
420 ret_code = LTTNG_OK;
421 goto end;
422 }
423
424 if (channel_event_count > UINT_MAX) {
425 ret_code = LTTNG_ERR_OVERFLOW;
426 goto error;
427 }
428
429 local_nb_events = (unsigned int) channel_event_count;
430
431 DBG3("Listing UST global %d events", *nb_events);
432
433 cds_lfht_for_each_entry (uchan->events->ht, &iter.iter, uevent, node.node) {
434 struct lttng_event *tmp_event = nullptr;
435
436 if (uevent->internal) {
437 /* This event should remain hidden from clients */
438 local_nb_events--;
439 continue;
440 }
441
442 tmp_event = lttng_event_create();
443 if (!tmp_event) {
444 ret_code = LTTNG_ERR_NOMEM;
445 goto error;
446 }
447
448 if (lttng_strncpy(tmp_event->name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN)) {
449 ret_code = LTTNG_ERR_FATAL;
450 lttng_event_destroy(tmp_event);
451 goto error;
452 }
453
454 tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
455 tmp_event->enabled = uevent->enabled;
456
457 switch (uevent->attr.instrumentation) {
458 case LTTNG_UST_ABI_TRACEPOINT:
459 tmp_event->type = LTTNG_EVENT_TRACEPOINT;
460 break;
461 case LTTNG_UST_ABI_PROBE:
462 tmp_event->type = LTTNG_EVENT_PROBE;
463 break;
464 case LTTNG_UST_ABI_FUNCTION:
465 tmp_event->type = LTTNG_EVENT_FUNCTION;
466 break;
467 }
468
469 tmp_event->loglevel = uevent->attr.loglevel;
470 switch (uevent->attr.loglevel_type) {
471 case LTTNG_UST_ABI_LOGLEVEL_ALL:
472 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
473 break;
474 case LTTNG_UST_ABI_LOGLEVEL_RANGE:
475 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
476 break;
477 case LTTNG_UST_ABI_LOGLEVEL_SINGLE:
478 tmp_event->loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
479 break;
480 }
481 if (uevent->filter) {
482 tmp_event->filter = 1;
483 }
484 if (uevent->exclusion) {
485 tmp_event->exclusion = 1;
486 }
487
488 /*
489 * We do not care about the filter bytecode and the fd from the
490 * userspace_probe_location.
491 */
492 ret = lttng_event_serialize(tmp_event,
493 uevent->exclusion ? uevent->exclusion->count : 0,
494 uevent->exclusion ? (char **) uevent->exclusion->names :
495 nullptr,
496 uevent->filter_expression,
497 0,
498 nullptr,
499 reply_payload);
500 lttng_event_destroy(tmp_event);
501 if (ret) {
502 ret_code = LTTNG_ERR_FATAL;
503 goto error;
504 }
505 }
506
507 end:
508 /* nb_events is already set at this point. */
509 ret_code = LTTNG_OK;
510 *nb_events = local_nb_events;
511 error:
512 return ret_code;
513 }
514
515 /*
516 * Fill lttng_event array of all kernel events in the channel.
517 */
518 static enum lttng_error_code list_lttng_kernel_events(char *channel_name,
519 struct ltt_kernel_session *kernel_session,
520 struct lttng_payload *reply_payload,
521 unsigned int *nb_events)
522 {
523 enum lttng_error_code ret_code;
524 int ret;
525 struct ltt_kernel_event *event;
526 struct ltt_kernel_channel *kchan;
527
528 assert(reply_payload);
529
530 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
531 if (kchan == nullptr) {
532 ret_code = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
533 goto end;
534 }
535
536 *nb_events = kchan->event_count;
537
538 DBG("Listing events for channel %s", kchan->channel->name);
539
540 if (*nb_events == 0) {
541 ret_code = LTTNG_OK;
542 goto end;
543 }
544
545 /* Kernel channels */
546 cds_list_for_each_entry (event, &kchan->events_list.head, list) {
547 struct lttng_event *tmp_event = lttng_event_create();
548
549 if (!tmp_event) {
550 ret_code = LTTNG_ERR_NOMEM;
551 goto end;
552 }
553
554 if (lttng_strncpy(tmp_event->name, event->event->name, LTTNG_SYMBOL_NAME_LEN)) {
555 lttng_event_destroy(tmp_event);
556 ret_code = LTTNG_ERR_FATAL;
557 goto end;
558 }
559
560 tmp_event->name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
561 tmp_event->enabled = event->enabled;
562 tmp_event->filter = (unsigned char) !!event->filter_expression;
563
564 switch (event->event->instrumentation) {
565 case LTTNG_KERNEL_ABI_TRACEPOINT:
566 tmp_event->type = LTTNG_EVENT_TRACEPOINT;
567 break;
568 case LTTNG_KERNEL_ABI_KRETPROBE:
569 tmp_event->type = LTTNG_EVENT_FUNCTION;
570 memcpy(&tmp_event->attr.probe,
571 &event->event->u.kprobe,
572 sizeof(struct lttng_kernel_abi_kprobe));
573 break;
574 case LTTNG_KERNEL_ABI_KPROBE:
575 tmp_event->type = LTTNG_EVENT_PROBE;
576 memcpy(&tmp_event->attr.probe,
577 &event->event->u.kprobe,
578 sizeof(struct lttng_kernel_abi_kprobe));
579 break;
580 case LTTNG_KERNEL_ABI_UPROBE:
581 tmp_event->type = LTTNG_EVENT_USERSPACE_PROBE;
582 break;
583 case LTTNG_KERNEL_ABI_FUNCTION:
584 tmp_event->type = LTTNG_EVENT_FUNCTION;
585 memcpy(&(tmp_event->attr.ftrace),
586 &event->event->u.ftrace,
587 sizeof(struct lttng_kernel_abi_function));
588 break;
589 case LTTNG_KERNEL_ABI_NOOP:
590 tmp_event->type = LTTNG_EVENT_NOOP;
591 break;
592 case LTTNG_KERNEL_ABI_SYSCALL:
593 tmp_event->type = LTTNG_EVENT_SYSCALL;
594 break;
595 case LTTNG_KERNEL_ABI_ALL:
596 /* fall-through. */
597 default:
598 abort();
599 break;
600 }
601
602 if (event->userspace_probe_location) {
603 struct lttng_userspace_probe_location *location_copy =
604 lttng_userspace_probe_location_copy(
605 event->userspace_probe_location);
606
607 if (!location_copy) {
608 lttng_event_destroy(tmp_event);
609 ret_code = LTTNG_ERR_NOMEM;
610 goto end;
611 }
612
613 ret = lttng_event_set_userspace_probe_location(tmp_event, location_copy);
614 if (ret) {
615 lttng_event_destroy(tmp_event);
616 lttng_userspace_probe_location_destroy(location_copy);
617 ret_code = LTTNG_ERR_INVALID;
618 goto end;
619 }
620 }
621
622 ret = lttng_event_serialize(
623 tmp_event, 0, nullptr, event->filter_expression, 0, nullptr, reply_payload);
624 lttng_event_destroy(tmp_event);
625 if (ret) {
626 ret_code = LTTNG_ERR_FATAL;
627 goto end;
628 }
629 }
630
631 ret_code = LTTNG_OK;
632 end:
633 return ret_code;
634 }
635
636 /*
637 * Add URI so the consumer output object. Set the correct path depending on the
638 * domain adding the default trace directory.
639 */
640 static enum lttng_error_code add_uri_to_consumer(const struct ltt_session *session,
641 struct consumer_output *consumer,
642 struct lttng_uri *uri,
643 enum lttng_domain_type domain)
644 {
645 int ret;
646 enum lttng_error_code ret_code = LTTNG_OK;
647
648 LTTNG_ASSERT(uri);
649
650 if (consumer == nullptr) {
651 DBG("No consumer detected. Don't add URI. Stopping.");
652 ret_code = LTTNG_ERR_NO_CONSUMER;
653 goto error;
654 }
655
656 switch (domain) {
657 case LTTNG_DOMAIN_KERNEL:
658 ret = lttng_strncpy(consumer->domain_subdir,
659 DEFAULT_KERNEL_TRACE_DIR,
660 sizeof(consumer->domain_subdir));
661 break;
662 case LTTNG_DOMAIN_UST:
663 ret = lttng_strncpy(consumer->domain_subdir,
664 DEFAULT_UST_TRACE_DIR,
665 sizeof(consumer->domain_subdir));
666 break;
667 default:
668 /*
669 * This case is possible is we try to add the URI to the global
670 * tracing session consumer object which in this case there is
671 * no subdir.
672 */
673 memset(consumer->domain_subdir, 0, sizeof(consumer->domain_subdir));
674 ret = 0;
675 }
676 if (ret) {
677 ERR("Failed to initialize consumer output domain subdirectory");
678 ret_code = LTTNG_ERR_FATAL;
679 goto error;
680 }
681
682 switch (uri->dtype) {
683 case LTTNG_DST_IPV4:
684 case LTTNG_DST_IPV6:
685 DBG2("Setting network URI to consumer");
686
687 if (consumer->type == CONSUMER_DST_NET) {
688 if ((uri->stype == LTTNG_STREAM_CONTROL &&
689 consumer->dst.net.control_isset) ||
690 (uri->stype == LTTNG_STREAM_DATA && consumer->dst.net.data_isset)) {
691 ret_code = LTTNG_ERR_URL_EXIST;
692 goto error;
693 }
694 } else {
695 memset(&consumer->dst, 0, sizeof(consumer->dst));
696 }
697
698 /* Set URI into consumer output object */
699 ret = consumer_set_network_uri(session, consumer, uri);
700 if (ret < 0) {
701 ret_code = (lttng_error_code) -ret;
702 goto error;
703 } else if (ret == 1) {
704 /*
705 * URI was the same in the consumer so we do not append the subdir
706 * again so to not duplicate output dir.
707 */
708 ret_code = LTTNG_OK;
709 goto error;
710 }
711 break;
712 case LTTNG_DST_PATH:
713 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
714 ret_code = LTTNG_ERR_INVALID;
715 goto error;
716 }
717 DBG2("Setting trace directory path from URI to %s", uri->dst.path);
718 memset(&consumer->dst, 0, sizeof(consumer->dst));
719
720 ret = lttng_strncpy(consumer->dst.session_root_path,
721 uri->dst.path,
722 sizeof(consumer->dst.session_root_path));
723 if (ret) {
724 ret_code = LTTNG_ERR_FATAL;
725 goto error;
726 }
727 consumer->type = CONSUMER_DST_LOCAL;
728 break;
729 }
730
731 ret_code = LTTNG_OK;
732 error:
733 return ret_code;
734 }
735
736 /*
737 * Init tracing by creating trace directory and sending fds kernel consumer.
738 */
739 static int init_kernel_tracing(struct ltt_kernel_session *session)
740 {
741 int ret = 0;
742 struct lttng_ht_iter iter;
743 struct consumer_socket *socket;
744
745 LTTNG_ASSERT(session);
746
747 if (session->consumer_fds_sent == 0 && session->consumer != nullptr) {
748 lttng::urcu::read_lock_guard read_lock;
749
750 cds_lfht_for_each_entry (
751 session->consumer->socks->ht, &iter.iter, socket, node.node) {
752 pthread_mutex_lock(socket->lock);
753 ret = kernel_consumer_send_session(socket, session);
754 pthread_mutex_unlock(socket->lock);
755 if (ret < 0) {
756 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
757 goto error;
758 }
759 }
760 }
761
762 error:
763 return ret;
764 }
765
766 /*
767 * Create a socket to the relayd using the URI.
768 *
769 * On success, the relayd_sock pointer is set to the created socket.
770 * Else, it remains untouched and an LTTng error code is returned.
771 */
772 static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
773 struct lttcomm_relayd_sock **relayd_sock,
774 struct consumer_output *consumer)
775 {
776 int ret;
777 enum lttng_error_code status = LTTNG_OK;
778 struct lttcomm_relayd_sock *rsock;
779
780 rsock = lttcomm_alloc_relayd_sock(
781 uri, RELAYD_VERSION_COMM_MAJOR, RELAYD_VERSION_COMM_MINOR);
782 if (!rsock) {
783 status = LTTNG_ERR_FATAL;
784 goto error;
785 }
786
787 /*
788 * Connect to relayd so we can proceed with a session creation. This call
789 * can possibly block for an arbitrary amount of time to set the health
790 * state to be in poll execution.
791 */
792 health_poll_entry();
793 ret = relayd_connect(rsock);
794 health_poll_exit();
795 if (ret < 0) {
796 ERR("Unable to reach lttng-relayd");
797 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
798 goto free_sock;
799 }
800
801 /* Create socket for control stream. */
802 if (uri->stype == LTTNG_STREAM_CONTROL) {
803 uint64_t result_flags;
804
805 DBG3("Creating relayd stream socket from URI");
806
807 /* Check relayd version */
808 ret = relayd_version_check(rsock);
809 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
810 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
811 goto close_sock;
812 } else if (ret < 0) {
813 ERR("Unable to reach lttng-relayd");
814 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
815 goto close_sock;
816 }
817 consumer->relay_major_version = rsock->major;
818 consumer->relay_minor_version = rsock->minor;
819 ret = relayd_get_configuration(rsock, 0, &result_flags);
820 if (ret < 0) {
821 ERR("Unable to get relayd configuration");
822 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
823 goto close_sock;
824 }
825 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
826 consumer->relay_allows_clear = true;
827 }
828 } else if (uri->stype == LTTNG_STREAM_DATA) {
829 DBG3("Creating relayd data socket from URI");
830 } else {
831 /* Command is not valid */
832 ERR("Relayd invalid stream type: %d", uri->stype);
833 status = LTTNG_ERR_INVALID;
834 goto close_sock;
835 }
836
837 *relayd_sock = rsock;
838
839 return status;
840
841 close_sock:
842 /* The returned value is not useful since we are on an error path. */
843 (void) relayd_close(rsock);
844 free_sock:
845 free(rsock);
846 error:
847 return status;
848 }
849
850 /*
851 * Connect to the relayd using URI and send the socket to the right consumer.
852 *
853 * The consumer socket lock must be held by the caller.
854 *
855 * Returns LTTNG_OK on success or an LTTng error code on failure.
856 */
857 static enum lttng_error_code send_consumer_relayd_socket(unsigned int session_id,
858 struct lttng_uri *relayd_uri,
859 struct consumer_output *consumer,
860 struct consumer_socket *consumer_sock,
861 const char *session_name,
862 const char *hostname,
863 const char *base_path,
864 int session_live_timer,
865 const uint64_t *current_chunk_id,
866 time_t session_creation_time,
867 bool session_name_contains_creation_time)
868 {
869 int ret;
870 struct lttcomm_relayd_sock *rsock = nullptr;
871 enum lttng_error_code status;
872
873 /* Connect to relayd and make version check if uri is the control. */
874 status = create_connect_relayd(relayd_uri, &rsock, consumer);
875 if (status != LTTNG_OK) {
876 goto relayd_comm_error;
877 }
878 LTTNG_ASSERT(rsock);
879
880 /* Set the network sequence index if not set. */
881 if (consumer->net_seq_index == (uint64_t) -1ULL) {
882 pthread_mutex_lock(&relayd_net_seq_idx_lock);
883 /*
884 * Increment net_seq_idx because we are about to transfer the
885 * new relayd socket to the consumer.
886 * Assign unique key so the consumer can match streams.
887 */
888 consumer->net_seq_index = ++relayd_net_seq_idx;
889 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
890 }
891
892 /* Send relayd socket to consumer. */
893 ret = consumer_send_relayd_socket(consumer_sock,
894 rsock,
895 consumer,
896 relayd_uri->stype,
897 session_id,
898 session_name,
899 hostname,
900 base_path,
901 session_live_timer,
902 current_chunk_id,
903 session_creation_time,
904 session_name_contains_creation_time);
905 if (ret < 0) {
906 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
907 goto close_sock;
908 }
909
910 /* Flag that the corresponding socket was sent. */
911 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
912 consumer_sock->control_sock_sent = 1;
913 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
914 consumer_sock->data_sock_sent = 1;
915 }
916
917 /*
918 * Close socket which was dup on the consumer side. The session daemon does
919 * NOT keep track of the relayd socket(s) once transfer to the consumer.
920 */
921
922 close_sock:
923 if (status != LTTNG_OK) {
924 /*
925 * The consumer output for this session should not be used anymore
926 * since the relayd connection failed thus making any tracing or/and
927 * streaming not usable.
928 */
929 consumer->enabled = false;
930 }
931 (void) relayd_close(rsock);
932 free(rsock);
933
934 relayd_comm_error:
935 return status;
936 }
937
938 /*
939 * Send both relayd sockets to a specific consumer and domain. This is a
940 * helper function to facilitate sending the information to the consumer for a
941 * session.
942 *
943 * The consumer socket lock must be held by the caller.
944 *
945 * Returns LTTNG_OK, or an LTTng error code on failure.
946 */
947 static enum lttng_error_code send_consumer_relayd_sockets(unsigned int session_id,
948 struct consumer_output *consumer,
949 struct consumer_socket *sock,
950 const char *session_name,
951 const char *hostname,
952 const char *base_path,
953 int session_live_timer,
954 const uint64_t *current_chunk_id,
955 time_t session_creation_time,
956 bool session_name_contains_creation_time)
957 {
958 enum lttng_error_code status = LTTNG_OK;
959
960 LTTNG_ASSERT(consumer);
961 LTTNG_ASSERT(sock);
962
963 /* Sending control relayd socket. */
964 if (!sock->control_sock_sent) {
965 status = send_consumer_relayd_socket(session_id,
966 &consumer->dst.net.control,
967 consumer,
968 sock,
969 session_name,
970 hostname,
971 base_path,
972 session_live_timer,
973 current_chunk_id,
974 session_creation_time,
975 session_name_contains_creation_time);
976 if (status != LTTNG_OK) {
977 goto error;
978 }
979 }
980
981 /* Sending data relayd socket. */
982 if (!sock->data_sock_sent) {
983 status = send_consumer_relayd_socket(session_id,
984 &consumer->dst.net.data,
985 consumer,
986 sock,
987 session_name,
988 hostname,
989 base_path,
990 session_live_timer,
991 current_chunk_id,
992 session_creation_time,
993 session_name_contains_creation_time);
994 if (status != LTTNG_OK) {
995 goto error;
996 }
997 }
998
999 error:
1000 return status;
1001 }
1002
1003 /*
1004 * Setup relayd connections for a tracing session. First creates the socket to
1005 * the relayd and send them to the right domain consumer. Consumer type MUST be
1006 * network.
1007 */
1008 int cmd_setup_relayd(struct ltt_session *session)
1009 {
1010 int ret = LTTNG_OK;
1011 struct ltt_ust_session *usess;
1012 struct ltt_kernel_session *ksess;
1013 struct consumer_socket *socket;
1014 struct lttng_ht_iter iter;
1015 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1016
1017 LTTNG_ASSERT(session);
1018
1019 usess = session->ust_session;
1020 ksess = session->kernel_session;
1021
1022 DBG("Setting relayd for session %s", session->name);
1023
1024 if (session->current_trace_chunk) {
1025 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1026 session->current_trace_chunk, &current_chunk_id.value);
1027
1028 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1029 current_chunk_id.is_set = true;
1030 } else {
1031 ERR("Failed to get current trace chunk id");
1032 ret = LTTNG_ERR_UNK;
1033 goto error;
1034 }
1035 }
1036
1037 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET &&
1038 usess->consumer->enabled) {
1039 /* For each consumer socket, send relayd sockets */
1040 lttng::urcu::read_lock_guard read_lock;
1041
1042 cds_lfht_for_each_entry (
1043 usess->consumer->socks->ht, &iter.iter, socket, node.node) {
1044 pthread_mutex_lock(socket->lock);
1045 ret = send_consumer_relayd_sockets(
1046 session->id,
1047 usess->consumer,
1048 socket,
1049 session->name,
1050 session->hostname,
1051 session->base_path,
1052 session->live_timer,
1053 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
1054 session->creation_time,
1055 session->name_contains_creation_time);
1056 pthread_mutex_unlock(socket->lock);
1057 if (ret != LTTNG_OK) {
1058 goto error;
1059 }
1060 /* Session is now ready for network streaming. */
1061 session->net_handle = 1;
1062 }
1063
1064 session->consumer->relay_major_version = usess->consumer->relay_major_version;
1065 session->consumer->relay_minor_version = usess->consumer->relay_minor_version;
1066 session->consumer->relay_allows_clear = usess->consumer->relay_allows_clear;
1067 }
1068
1069 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET &&
1070 ksess->consumer->enabled) {
1071 lttng::urcu::read_lock_guard read_lock;
1072
1073 cds_lfht_for_each_entry (
1074 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
1075 pthread_mutex_lock(socket->lock);
1076 ret = send_consumer_relayd_sockets(
1077 session->id,
1078 ksess->consumer,
1079 socket,
1080 session->name,
1081 session->hostname,
1082 session->base_path,
1083 session->live_timer,
1084 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
1085 session->creation_time,
1086 session->name_contains_creation_time);
1087 pthread_mutex_unlock(socket->lock);
1088 if (ret != LTTNG_OK) {
1089 goto error;
1090 }
1091 /* Session is now ready for network streaming. */
1092 session->net_handle = 1;
1093 }
1094
1095 session->consumer->relay_major_version = ksess->consumer->relay_major_version;
1096 session->consumer->relay_minor_version = ksess->consumer->relay_minor_version;
1097 session->consumer->relay_allows_clear = ksess->consumer->relay_allows_clear;
1098 }
1099
1100 error:
1101 return ret;
1102 }
1103
1104 /*
1105 * Start a kernel session by opening all necessary streams.
1106 */
1107 int start_kernel_session(struct ltt_kernel_session *ksess)
1108 {
1109 int ret;
1110 struct ltt_kernel_channel *kchan;
1111
1112 /* Open kernel metadata */
1113 if (ksess->metadata == nullptr && ksess->output_traces) {
1114 ret = kernel_open_metadata(ksess);
1115 if (ret < 0) {
1116 ret = LTTNG_ERR_KERN_META_FAIL;
1117 goto error;
1118 }
1119 }
1120
1121 /* Open kernel metadata stream */
1122 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1123 ret = kernel_open_metadata_stream(ksess);
1124 if (ret < 0) {
1125 ERR("Kernel create metadata stream failed");
1126 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1127 goto error;
1128 }
1129 }
1130
1131 /* For each channel */
1132 cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
1133 if (kchan->stream_count == 0) {
1134 ret = kernel_open_channel_stream(kchan);
1135 if (ret < 0) {
1136 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1137 goto error;
1138 }
1139 /* Update the stream global counter */
1140 ksess->stream_count_global += ret;
1141 }
1142 }
1143
1144 /* Setup kernel consumer socket and send fds to it */
1145 ret = init_kernel_tracing(ksess);
1146 if (ret != 0) {
1147 ret = LTTNG_ERR_KERN_START_FAIL;
1148 goto error;
1149 }
1150
1151 /* This start the kernel tracing */
1152 ret = kernel_start_session(ksess);
1153 if (ret < 0) {
1154 ret = LTTNG_ERR_KERN_START_FAIL;
1155 goto error;
1156 }
1157
1158 /* Quiescent wait after starting trace */
1159 kernel_wait_quiescent();
1160
1161 ksess->active = true;
1162
1163 ret = LTTNG_OK;
1164
1165 error:
1166 return ret;
1167 }
1168
1169 int stop_kernel_session(struct ltt_kernel_session *ksess)
1170 {
1171 struct ltt_kernel_channel *kchan;
1172 bool error_occurred = false;
1173 int ret;
1174
1175 if (!ksess || !ksess->active) {
1176 return LTTNG_OK;
1177 }
1178 DBG("Stopping kernel tracing");
1179
1180 ret = kernel_stop_session(ksess);
1181 if (ret < 0) {
1182 ret = LTTNG_ERR_KERN_STOP_FAIL;
1183 goto error;
1184 }
1185
1186 kernel_wait_quiescent();
1187
1188 /* Flush metadata after stopping (if exists) */
1189 if (ksess->metadata_stream_fd >= 0) {
1190 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1191 if (ret < 0) {
1192 ERR("Kernel metadata flush failed");
1193 error_occurred = true;
1194 }
1195 }
1196
1197 /* Flush all buffers after stopping */
1198 cds_list_for_each_entry (kchan, &ksess->channel_list.head, list) {
1199 ret = kernel_flush_buffer(kchan);
1200 if (ret < 0) {
1201 ERR("Kernel flush buffer error");
1202 error_occurred = true;
1203 }
1204 }
1205
1206 ksess->active = false;
1207 if (error_occurred) {
1208 ret = LTTNG_ERR_UNK;
1209 } else {
1210 ret = LTTNG_OK;
1211 }
1212 error:
1213 return ret;
1214 }
1215
1216 /*
1217 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1218 */
1219 int cmd_disable_channel(struct ltt_session *session,
1220 enum lttng_domain_type domain,
1221 char *channel_name)
1222 {
1223 int ret;
1224 struct ltt_ust_session *usess;
1225
1226 usess = session->ust_session;
1227
1228 lttng::urcu::read_lock_guard read_lock;
1229
1230 switch (domain) {
1231 case LTTNG_DOMAIN_KERNEL:
1232 {
1233 ret = channel_kernel_disable(session->kernel_session, channel_name);
1234 if (ret != LTTNG_OK) {
1235 goto error;
1236 }
1237
1238 kernel_wait_quiescent();
1239 break;
1240 }
1241 case LTTNG_DOMAIN_UST:
1242 {
1243 struct ltt_ust_channel *uchan;
1244 struct lttng_ht *chan_ht;
1245
1246 chan_ht = usess->domain_global.channels;
1247
1248 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1249 if (uchan == nullptr) {
1250 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1251 goto error;
1252 }
1253
1254 ret = channel_ust_disable(usess, uchan);
1255 if (ret != LTTNG_OK) {
1256 goto error;
1257 }
1258 break;
1259 }
1260 default:
1261 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1262 goto error;
1263 }
1264
1265 ret = LTTNG_OK;
1266
1267 error:
1268 return ret;
1269 }
1270
1271 /*
1272 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1273 *
1274 * The wpipe arguments is used as a notifier for the kernel thread.
1275 */
1276 int cmd_enable_channel(struct command_ctx *cmd_ctx, int sock, int wpipe)
1277 {
1278 int ret;
1279 size_t channel_len;
1280 ssize_t sock_recv_len;
1281 struct lttng_channel *channel = nullptr;
1282 struct lttng_buffer_view view;
1283 struct lttng_dynamic_buffer channel_buffer;
1284 const struct lttng_domain command_domain = cmd_ctx->lsm.domain;
1285
1286 lttng_dynamic_buffer_init(&channel_buffer);
1287 channel_len = (size_t) cmd_ctx->lsm.u.channel.length;
1288 ret = lttng_dynamic_buffer_set_size(&channel_buffer, channel_len);
1289 if (ret) {
1290 ret = LTTNG_ERR_NOMEM;
1291 goto end;
1292 }
1293
1294 sock_recv_len = lttcomm_recv_unix_sock(sock, channel_buffer.data, channel_len);
1295 if (sock_recv_len < 0 || sock_recv_len != channel_len) {
1296 ERR("Failed to receive \"enable channel\" command payload");
1297 ret = LTTNG_ERR_INVALID;
1298 goto end;
1299 }
1300
1301 view = lttng_buffer_view_from_dynamic_buffer(&channel_buffer, 0, channel_len);
1302 if (!lttng_buffer_view_is_valid(&view)) {
1303 ret = LTTNG_ERR_INVALID;
1304 goto end;
1305 }
1306
1307 if (lttng_channel_create_from_buffer(&view, &channel) != channel_len) {
1308 ERR("Invalid channel payload received in \"enable channel\" command");
1309 ret = LTTNG_ERR_INVALID;
1310 goto end;
1311 }
1312
1313 ret = cmd_enable_channel_internal(cmd_ctx->session, &command_domain, channel, wpipe);
1314
1315 end:
1316 lttng_dynamic_buffer_reset(&channel_buffer);
1317 lttng_channel_destroy(channel);
1318 return ret;
1319 }
1320
1321 static enum lttng_error_code cmd_enable_channel_internal(struct ltt_session *session,
1322 const struct lttng_domain *domain,
1323 const struct lttng_channel *_attr,
1324 int wpipe)
1325 {
1326 enum lttng_error_code ret_code;
1327 struct ltt_ust_session *usess = session->ust_session;
1328 struct lttng_ht *chan_ht;
1329 size_t len;
1330 struct lttng_channel *attr = nullptr;
1331
1332 LTTNG_ASSERT(session);
1333 LTTNG_ASSERT(_attr);
1334 LTTNG_ASSERT(domain);
1335
1336 lttng::urcu::read_lock_guard read_lock;
1337
1338 attr = lttng_channel_copy(_attr);
1339 if (!attr) {
1340 ret_code = LTTNG_ERR_NOMEM;
1341 goto end;
1342 }
1343
1344 len = lttng_strnlen(attr->name, sizeof(attr->name));
1345
1346 /* Validate channel name */
1347 if (attr->name[0] == '.' || memchr(attr->name, '/', len) != nullptr) {
1348 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1349 goto end;
1350 }
1351
1352 DBG("Enabling channel %s for session %s", attr->name, session->name);
1353
1354 /*
1355 * If the session is a live session, remove the switch timer, the
1356 * live timer does the same thing but sends also synchronisation
1357 * beacons for inactive streams.
1358 */
1359 if (session->live_timer > 0) {
1360 attr->attr.live_timer_interval = session->live_timer;
1361 attr->attr.switch_timer_interval = 0;
1362 }
1363
1364 /* Check for feature support */
1365 switch (domain->type) {
1366 case LTTNG_DOMAIN_KERNEL:
1367 {
1368 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1369 /* Sampling position of buffer is not supported */
1370 WARN("Kernel tracer does not support buffer monitoring. "
1371 "Setting the monitor interval timer to 0 "
1372 "(disabled) for channel '%s' of session '%s'",
1373 attr->name,
1374 session->name);
1375 lttng_channel_set_monitor_timer_interval(attr, 0);
1376 }
1377 break;
1378 }
1379 case LTTNG_DOMAIN_UST:
1380 break;
1381 case LTTNG_DOMAIN_JUL:
1382 case LTTNG_DOMAIN_LOG4J:
1383 case LTTNG_DOMAIN_PYTHON:
1384 if (!agent_tracing_is_enabled()) {
1385 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1386 ret_code = LTTNG_ERR_AGENT_TRACING_DISABLED;
1387 goto error;
1388 }
1389 break;
1390 default:
1391 ret_code = LTTNG_ERR_UNKNOWN_DOMAIN;
1392 goto error;
1393 }
1394
1395 switch (domain->type) {
1396 case LTTNG_DOMAIN_KERNEL:
1397 {
1398 struct ltt_kernel_channel *kchan;
1399
1400 kchan = trace_kernel_get_channel_by_name(attr->name, session->kernel_session);
1401 if (kchan == nullptr) {
1402 /*
1403 * Don't try to create a channel if the session has been started at
1404 * some point in time before. The tracer does not allow it.
1405 */
1406 if (session->has_been_started) {
1407 ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED;
1408 goto error;
1409 }
1410
1411 if (session->snapshot.nb_output > 0 || session->snapshot_mode) {
1412 /* Enforce mmap output for snapshot sessions. */
1413 attr->attr.output = LTTNG_EVENT_MMAP;
1414 }
1415 ret_code = channel_kernel_create(session->kernel_session, attr, wpipe);
1416 if (attr->name[0] != '\0') {
1417 session->kernel_session->has_non_default_channel = 1;
1418 }
1419 } else {
1420 ret_code = channel_kernel_enable(session->kernel_session, kchan);
1421 }
1422
1423 if (ret_code != LTTNG_OK) {
1424 goto error;
1425 }
1426
1427 kernel_wait_quiescent();
1428 break;
1429 }
1430 case LTTNG_DOMAIN_UST:
1431 case LTTNG_DOMAIN_JUL:
1432 case LTTNG_DOMAIN_LOG4J:
1433 case LTTNG_DOMAIN_PYTHON:
1434 {
1435 struct ltt_ust_channel *uchan;
1436
1437 /*
1438 * FIXME
1439 *
1440 * Current agent implementation limitations force us to allow
1441 * only one channel at once in "agent" subdomains. Each
1442 * subdomain has a default channel name which must be strictly
1443 * adhered to.
1444 */
1445 if (domain->type == LTTNG_DOMAIN_JUL) {
1446 if (strncmp(attr->name,
1447 DEFAULT_JUL_CHANNEL_NAME,
1448 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
1449 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1450 goto error;
1451 }
1452 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1453 if (strncmp(attr->name,
1454 DEFAULT_LOG4J_CHANNEL_NAME,
1455 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
1456 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1457 goto error;
1458 }
1459 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1460 if (strncmp(attr->name,
1461 DEFAULT_PYTHON_CHANNEL_NAME,
1462 LTTNG_SYMBOL_NAME_LEN - 1) != 0) {
1463 ret_code = LTTNG_ERR_INVALID_CHANNEL_NAME;
1464 goto error;
1465 }
1466 }
1467
1468 chan_ht = usess->domain_global.channels;
1469
1470 uchan = trace_ust_find_channel_by_name(chan_ht, attr->name);
1471 if (uchan == nullptr) {
1472 /*
1473 * Don't try to create a channel if the session has been started at
1474 * some point in time before. The tracer does not allow it.
1475 */
1476 if (session->has_been_started) {
1477 ret_code = LTTNG_ERR_TRACE_ALREADY_STARTED;
1478 goto error;
1479 }
1480
1481 ret_code = channel_ust_create(usess, attr, domain->buf_type);
1482 if (attr->name[0] != '\0') {
1483 usess->has_non_default_channel = 1;
1484 }
1485 } else {
1486 ret_code = channel_ust_enable(usess, uchan);
1487 }
1488 break;
1489 }
1490 default:
1491 ret_code = LTTNG_ERR_UNKNOWN_DOMAIN;
1492 goto error;
1493 }
1494
1495 if (ret_code == LTTNG_OK && attr->attr.output != LTTNG_EVENT_MMAP) {
1496 session->has_non_mmap_channel = true;
1497 }
1498 error:
1499 end:
1500 lttng_channel_destroy(attr);
1501 return ret_code;
1502 }
1503
1504 enum lttng_error_code
1505 cmd_process_attr_tracker_get_tracking_policy(struct ltt_session *session,
1506 enum lttng_domain_type domain,
1507 enum lttng_process_attr process_attr,
1508 enum lttng_tracking_policy *policy)
1509 {
1510 enum lttng_error_code ret_code = LTTNG_OK;
1511 const struct process_attr_tracker *tracker;
1512
1513 switch (domain) {
1514 case LTTNG_DOMAIN_KERNEL:
1515 if (!session->kernel_session) {
1516 ret_code = LTTNG_ERR_INVALID;
1517 goto end;
1518 }
1519 tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr);
1520 break;
1521 case LTTNG_DOMAIN_UST:
1522 if (!session->ust_session) {
1523 ret_code = LTTNG_ERR_INVALID;
1524 goto end;
1525 }
1526 tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr);
1527 break;
1528 default:
1529 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1530 goto end;
1531 }
1532 if (tracker) {
1533 *policy = process_attr_tracker_get_tracking_policy(tracker);
1534 } else {
1535 ret_code = LTTNG_ERR_INVALID;
1536 }
1537 end:
1538 return ret_code;
1539 }
1540
1541 enum lttng_error_code
1542 cmd_process_attr_tracker_set_tracking_policy(struct ltt_session *session,
1543 enum lttng_domain_type domain,
1544 enum lttng_process_attr process_attr,
1545 enum lttng_tracking_policy policy)
1546 {
1547 enum lttng_error_code ret_code = LTTNG_OK;
1548
1549 switch (policy) {
1550 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1551 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1552 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1553 break;
1554 default:
1555 ret_code = LTTNG_ERR_INVALID;
1556 goto end;
1557 }
1558
1559 switch (domain) {
1560 case LTTNG_DOMAIN_KERNEL:
1561 if (!session->kernel_session) {
1562 ret_code = LTTNG_ERR_INVALID;
1563 goto end;
1564 }
1565 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1566 session->kernel_session, process_attr, policy);
1567 break;
1568 case LTTNG_DOMAIN_UST:
1569 if (!session->ust_session) {
1570 ret_code = LTTNG_ERR_INVALID;
1571 goto end;
1572 }
1573 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1574 session->ust_session, process_attr, policy);
1575 break;
1576 default:
1577 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1578 break;
1579 }
1580 end:
1581 return ret_code;
1582 }
1583
1584 enum lttng_error_code
1585 cmd_process_attr_tracker_inclusion_set_add_value(struct ltt_session *session,
1586 enum lttng_domain_type domain,
1587 enum lttng_process_attr process_attr,
1588 const struct process_attr_value *value)
1589 {
1590 enum lttng_error_code ret_code = LTTNG_OK;
1591
1592 switch (domain) {
1593 case LTTNG_DOMAIN_KERNEL:
1594 if (!session->kernel_session) {
1595 ret_code = LTTNG_ERR_INVALID;
1596 goto end;
1597 }
1598 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1599 session->kernel_session, process_attr, value);
1600 break;
1601 case LTTNG_DOMAIN_UST:
1602 if (!session->ust_session) {
1603 ret_code = LTTNG_ERR_INVALID;
1604 goto end;
1605 }
1606 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1607 session->ust_session, process_attr, value);
1608 break;
1609 default:
1610 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1611 break;
1612 }
1613 end:
1614 return ret_code;
1615 }
1616
1617 enum lttng_error_code
1618 cmd_process_attr_tracker_inclusion_set_remove_value(struct ltt_session *session,
1619 enum lttng_domain_type domain,
1620 enum lttng_process_attr process_attr,
1621 const struct process_attr_value *value)
1622 {
1623 enum lttng_error_code ret_code = LTTNG_OK;
1624
1625 switch (domain) {
1626 case LTTNG_DOMAIN_KERNEL:
1627 if (!session->kernel_session) {
1628 ret_code = LTTNG_ERR_INVALID;
1629 goto end;
1630 }
1631 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1632 session->kernel_session, process_attr, value);
1633 break;
1634 case LTTNG_DOMAIN_UST:
1635 if (!session->ust_session) {
1636 ret_code = LTTNG_ERR_INVALID;
1637 goto end;
1638 }
1639 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1640 session->ust_session, process_attr, value);
1641 break;
1642 default:
1643 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1644 break;
1645 }
1646 end:
1647 return ret_code;
1648 }
1649
1650 enum lttng_error_code
1651 cmd_process_attr_tracker_get_inclusion_set(struct ltt_session *session,
1652 enum lttng_domain_type domain,
1653 enum lttng_process_attr process_attr,
1654 struct lttng_process_attr_values **values)
1655 {
1656 enum lttng_error_code ret_code = LTTNG_OK;
1657 const struct process_attr_tracker *tracker;
1658 enum process_attr_tracker_status status;
1659
1660 switch (domain) {
1661 case LTTNG_DOMAIN_KERNEL:
1662 if (!session->kernel_session) {
1663 ret_code = LTTNG_ERR_INVALID;
1664 goto end;
1665 }
1666 tracker = kernel_get_process_attr_tracker(session->kernel_session, process_attr);
1667 break;
1668 case LTTNG_DOMAIN_UST:
1669 if (!session->ust_session) {
1670 ret_code = LTTNG_ERR_INVALID;
1671 goto end;
1672 }
1673 tracker = trace_ust_get_process_attr_tracker(session->ust_session, process_attr);
1674 break;
1675 default:
1676 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1677 goto end;
1678 }
1679
1680 if (!tracker) {
1681 ret_code = LTTNG_ERR_INVALID;
1682 goto end;
1683 }
1684
1685 status = process_attr_tracker_get_inclusion_set(tracker, values);
1686 switch (status) {
1687 case PROCESS_ATTR_TRACKER_STATUS_OK:
1688 ret_code = LTTNG_OK;
1689 break;
1690 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1691 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1692 break;
1693 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1694 ret_code = LTTNG_ERR_NOMEM;
1695 break;
1696 default:
1697 ret_code = LTTNG_ERR_UNK;
1698 break;
1699 }
1700
1701 end:
1702 return ret_code;
1703 }
1704
1705 /*
1706 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1707 */
1708 int cmd_disable_event(struct command_ctx *cmd_ctx,
1709 struct lttng_event *event,
1710 char *filter_expression,
1711 struct lttng_bytecode *bytecode,
1712 struct lttng_event_exclusion *exclusion)
1713 {
1714 int ret;
1715 const char *event_name;
1716 const struct ltt_session *session = cmd_ctx->session;
1717 const char *channel_name = cmd_ctx->lsm.u.disable.channel_name;
1718 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
1719
1720 DBG("Disable event command for event \'%s\'", event->name);
1721
1722 /*
1723 * Filter and exclusions are simply not handled by the
1724 * disable event command at this time.
1725 *
1726 * FIXME
1727 */
1728 (void) filter_expression;
1729 (void) exclusion;
1730
1731 /* Ignore the presence of filter or exclusion for the event */
1732 event->filter = 0;
1733 event->exclusion = 0;
1734
1735 event_name = event->name;
1736
1737 lttng::urcu::read_lock_guard read_lock;
1738
1739 /* Error out on unhandled search criteria */
1740 if (event->loglevel_type || event->loglevel != -1 || event->enabled || event->pid ||
1741 event->filter || event->exclusion) {
1742 ret = LTTNG_ERR_UNK;
1743 goto error;
1744 }
1745
1746 switch (domain) {
1747 case LTTNG_DOMAIN_KERNEL:
1748 {
1749 struct ltt_kernel_channel *kchan;
1750 struct ltt_kernel_session *ksess;
1751
1752 ksess = session->kernel_session;
1753
1754 /*
1755 * If a non-default channel has been created in the
1756 * session, explicitely require that -c chan_name needs
1757 * to be provided.
1758 */
1759 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1760 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1761 goto error_unlock;
1762 }
1763
1764 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1765 if (kchan == nullptr) {
1766 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1767 goto error_unlock;
1768 }
1769
1770 switch (event->type) {
1771 case LTTNG_EVENT_ALL:
1772 case LTTNG_EVENT_TRACEPOINT:
1773 case LTTNG_EVENT_SYSCALL:
1774 case LTTNG_EVENT_PROBE:
1775 case LTTNG_EVENT_FUNCTION:
1776 case LTTNG_EVENT_FUNCTION_ENTRY: /* fall-through */
1777 if (event_name[0] == '\0') {
1778 ret = event_kernel_disable_event(kchan, nullptr, event->type);
1779 } else {
1780 ret = event_kernel_disable_event(kchan, event_name, event->type);
1781 }
1782 if (ret != LTTNG_OK) {
1783 goto error_unlock;
1784 }
1785 break;
1786 default:
1787 ret = LTTNG_ERR_UNK;
1788 goto error_unlock;
1789 }
1790
1791 kernel_wait_quiescent();
1792 break;
1793 }
1794 case LTTNG_DOMAIN_UST:
1795 {
1796 struct ltt_ust_channel *uchan;
1797 struct ltt_ust_session *usess;
1798
1799 usess = session->ust_session;
1800
1801 if (validate_ust_event_name(event_name)) {
1802 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1803 goto error_unlock;
1804 }
1805
1806 /*
1807 * If a non-default channel has been created in the
1808 * session, explicitly require that -c chan_name needs
1809 * to be provided.
1810 */
1811 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1812 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1813 goto error_unlock;
1814 }
1815
1816 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
1817 if (uchan == nullptr) {
1818 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1819 goto error_unlock;
1820 }
1821
1822 switch (event->type) {
1823 case LTTNG_EVENT_ALL:
1824 /*
1825 * An empty event name means that everything
1826 * should be disabled.
1827 */
1828 if (event->name[0] == '\0') {
1829 ret = event_ust_disable_all_tracepoints(usess, uchan);
1830 } else {
1831 ret = event_ust_disable_tracepoint(usess, uchan, event_name);
1832 }
1833 if (ret != LTTNG_OK) {
1834 goto error_unlock;
1835 }
1836 break;
1837 default:
1838 ret = LTTNG_ERR_UNK;
1839 goto error_unlock;
1840 }
1841
1842 DBG3("Disable UST event %s in channel %s completed", event_name, channel_name);
1843 break;
1844 }
1845 case LTTNG_DOMAIN_LOG4J:
1846 case LTTNG_DOMAIN_JUL:
1847 case LTTNG_DOMAIN_PYTHON:
1848 {
1849 struct agent *agt;
1850 struct ltt_ust_session *usess = session->ust_session;
1851
1852 LTTNG_ASSERT(usess);
1853
1854 switch (event->type) {
1855 case LTTNG_EVENT_ALL:
1856 break;
1857 default:
1858 ret = LTTNG_ERR_UNK;
1859 goto error_unlock;
1860 }
1861
1862 agt = trace_ust_find_agent(usess, domain);
1863 if (!agt) {
1864 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1865 goto error_unlock;
1866 }
1867 /*
1868 * An empty event name means that everything
1869 * should be disabled.
1870 */
1871 if (event->name[0] == '\0') {
1872 ret = event_agent_disable_all(usess, agt);
1873 } else {
1874 ret = event_agent_disable(usess, agt, event_name);
1875 }
1876 if (ret != LTTNG_OK) {
1877 goto error_unlock;
1878 }
1879
1880 break;
1881 }
1882 default:
1883 ret = LTTNG_ERR_UND;
1884 goto error_unlock;
1885 }
1886
1887 ret = LTTNG_OK;
1888
1889 error_unlock:
1890 error:
1891 free(exclusion);
1892 free(bytecode);
1893 free(filter_expression);
1894 return ret;
1895 }
1896
1897 /*
1898 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1899 */
1900 int cmd_add_context(struct command_ctx *cmd_ctx,
1901 const struct lttng_event_context *event_context,
1902 int kwpipe)
1903 {
1904 int ret, chan_kern_created = 0, chan_ust_created = 0;
1905 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
1906 const struct ltt_session *session = cmd_ctx->session;
1907 const char *channel_name = cmd_ctx->lsm.u.context.channel_name;
1908
1909 /*
1910 * Don't try to add a context if the session has been started at
1911 * some point in time before. The tracer does not allow it and would
1912 * result in a corrupted trace.
1913 */
1914 if (cmd_ctx->session->has_been_started) {
1915 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1916 goto end;
1917 }
1918
1919 switch (domain) {
1920 case LTTNG_DOMAIN_KERNEL:
1921 LTTNG_ASSERT(session->kernel_session);
1922
1923 if (session->kernel_session->channel_count == 0) {
1924 /* Create default channel */
1925 ret = channel_kernel_create(session->kernel_session, nullptr, kwpipe);
1926 if (ret != LTTNG_OK) {
1927 goto error;
1928 }
1929 chan_kern_created = 1;
1930 }
1931 /* Add kernel context to kernel tracer */
1932 ret = context_kernel_add(session->kernel_session, event_context, channel_name);
1933 if (ret != LTTNG_OK) {
1934 goto error;
1935 }
1936 break;
1937 case LTTNG_DOMAIN_JUL:
1938 case LTTNG_DOMAIN_LOG4J:
1939 {
1940 /*
1941 * Validate channel name.
1942 * If no channel name is given and the domain is JUL or LOG4J,
1943 * set it to the appropriate domain-specific channel name. If
1944 * a name is provided but does not match the expexted channel
1945 * name, return an error.
1946 */
1947 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1948 strcmp(channel_name, DEFAULT_JUL_CHANNEL_NAME) != 0) {
1949 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1950 goto error;
1951 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1952 strcmp(channel_name, DEFAULT_LOG4J_CHANNEL_NAME) != 0) {
1953 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1954 goto error;
1955 }
1956 }
1957 /* fall through */
1958 case LTTNG_DOMAIN_UST:
1959 {
1960 struct ltt_ust_session *usess = session->ust_session;
1961 unsigned int chan_count;
1962
1963 LTTNG_ASSERT(usess);
1964
1965 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1966 if (chan_count == 0) {
1967 struct lttng_channel *attr;
1968 /* Create default channel */
1969 attr = channel_new_default_attr(domain, usess->buffer_type);
1970 if (attr == nullptr) {
1971 ret = LTTNG_ERR_FATAL;
1972 goto error;
1973 }
1974
1975 ret = channel_ust_create(usess, attr, usess->buffer_type);
1976 if (ret != LTTNG_OK) {
1977 free(attr);
1978 goto error;
1979 }
1980 channel_attr_destroy(attr);
1981 chan_ust_created = 1;
1982 }
1983
1984 ret = context_ust_add(usess, domain, event_context, channel_name);
1985 if (ret != LTTNG_OK) {
1986 goto error;
1987 }
1988 break;
1989 }
1990 default:
1991 ret = LTTNG_ERR_UND;
1992 goto error;
1993 }
1994
1995 ret = LTTNG_OK;
1996 goto end;
1997
1998 error:
1999 if (chan_kern_created) {
2000 struct ltt_kernel_channel *kchan = trace_kernel_get_channel_by_name(
2001 DEFAULT_CHANNEL_NAME, session->kernel_session);
2002 /* Created previously, this should NOT fail. */
2003 LTTNG_ASSERT(kchan);
2004 kernel_destroy_channel(kchan);
2005 }
2006
2007 if (chan_ust_created) {
2008 struct ltt_ust_channel *uchan = trace_ust_find_channel_by_name(
2009 session->ust_session->domain_global.channels, DEFAULT_CHANNEL_NAME);
2010 /* Created previously, this should NOT fail. */
2011 LTTNG_ASSERT(uchan);
2012 /* Remove from the channel list of the session. */
2013 trace_ust_delete_channel(session->ust_session->domain_global.channels, uchan);
2014 trace_ust_destroy_channel(uchan);
2015 }
2016 end:
2017 return ret;
2018 }
2019
2020 static inline bool name_starts_with(const char *name, const char *prefix)
2021 {
2022 const size_t max_cmp_len = std::min(strlen(prefix), (size_t) LTTNG_SYMBOL_NAME_LEN);
2023
2024 return !strncmp(name, prefix, max_cmp_len);
2025 }
2026
2027 /* Perform userspace-specific event name validation */
2028 static int validate_ust_event_name(const char *name)
2029 {
2030 int ret = 0;
2031
2032 if (!name) {
2033 ret = -1;
2034 goto end;
2035 }
2036
2037 /*
2038 * Check name against all internal UST event component namespaces used
2039 * by the agents.
2040 */
2041 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2042 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2043 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2044 ret = -1;
2045 }
2046
2047 end:
2048 return ret;
2049 }
2050
2051 /*
2052 * Internal version of cmd_enable_event() with a supplemental
2053 * "internal_event" flag which is used to enable internal events which should
2054 * be hidden from clients. Such events are used in the agent implementation to
2055 * enable the events through which all "agent" events are funeled.
2056 */
2057 static int _cmd_enable_event(struct ltt_session *session,
2058 const struct lttng_domain *domain,
2059 char *channel_name,
2060 struct lttng_event *event,
2061 char *filter_expression,
2062 struct lttng_bytecode *filter,
2063 struct lttng_event_exclusion *exclusion,
2064 int wpipe,
2065 bool internal_event)
2066 {
2067 int ret = 0, channel_created = 0;
2068 struct lttng_channel *attr = nullptr;
2069
2070 LTTNG_ASSERT(session);
2071 LTTNG_ASSERT(event);
2072 LTTNG_ASSERT(channel_name);
2073
2074 /* If we have a filter, we must have its filter expression */
2075 LTTNG_ASSERT(!(!!filter_expression ^ !!filter));
2076
2077 /* Normalize event name as a globbing pattern */
2078 strutils_normalize_star_glob_pattern(event->name);
2079
2080 /* Normalize exclusion names as globbing patterns */
2081 if (exclusion) {
2082 size_t i;
2083
2084 for (i = 0; i < exclusion->count; i++) {
2085 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2086
2087 strutils_normalize_star_glob_pattern(name);
2088 }
2089 }
2090
2091 DBG("Enable event command for event \'%s\'", event->name);
2092
2093 lttng::urcu::read_lock_guard read_lock;
2094
2095 switch (domain->type) {
2096 case LTTNG_DOMAIN_KERNEL:
2097 {
2098 struct ltt_kernel_channel *kchan;
2099
2100 /*
2101 * If a non-default channel has been created in the
2102 * session, explicitely require that -c chan_name needs
2103 * to be provided.
2104 */
2105 if (session->kernel_session->has_non_default_channel && channel_name[0] == '\0') {
2106 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2107 goto error;
2108 }
2109
2110 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
2111 if (kchan == nullptr) {
2112 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, LTTNG_BUFFER_GLOBAL);
2113 if (attr == nullptr) {
2114 ret = LTTNG_ERR_FATAL;
2115 goto error;
2116 }
2117 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
2118 ret = LTTNG_ERR_INVALID;
2119 goto error;
2120 }
2121
2122 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
2123 if (ret != LTTNG_OK) {
2124 goto error;
2125 }
2126 channel_created = 1;
2127 }
2128
2129 /* Get the newly created kernel channel pointer */
2130 kchan = trace_kernel_get_channel_by_name(channel_name, session->kernel_session);
2131 if (kchan == nullptr) {
2132 /* This sould not happen... */
2133 ret = LTTNG_ERR_FATAL;
2134 goto error;
2135 }
2136
2137 switch (event->type) {
2138 case LTTNG_EVENT_ALL:
2139 {
2140 char *filter_expression_a = nullptr;
2141 struct lttng_bytecode *filter_a = nullptr;
2142
2143 /*
2144 * We need to duplicate filter_expression and filter,
2145 * because ownership is passed to first enable
2146 * event.
2147 */
2148 if (filter_expression) {
2149 filter_expression_a = strdup(filter_expression);
2150 if (!filter_expression_a) {
2151 ret = LTTNG_ERR_FATAL;
2152 goto error;
2153 }
2154 }
2155 if (filter) {
2156 filter_a = zmalloc<lttng_bytecode>(sizeof(*filter_a) + filter->len);
2157 if (!filter_a) {
2158 free(filter_expression_a);
2159 ret = LTTNG_ERR_FATAL;
2160 goto error;
2161 }
2162 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2163 }
2164 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2165 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
2166 /* We have passed ownership */
2167 filter_expression = nullptr;
2168 filter = nullptr;
2169 if (ret != LTTNG_OK) {
2170 if (channel_created) {
2171 /* Let's not leak a useless channel. */
2172 kernel_destroy_channel(kchan);
2173 }
2174 free(filter_expression_a);
2175 free(filter_a);
2176 goto error;
2177 }
2178 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2179 ret = event_kernel_enable_event(
2180 kchan, event, filter_expression_a, filter_a);
2181 /* We have passed ownership */
2182 filter_expression_a = nullptr;
2183 filter_a = nullptr;
2184 if (ret != LTTNG_OK) {
2185 goto error;
2186 }
2187 break;
2188 }
2189 case LTTNG_EVENT_PROBE:
2190 case LTTNG_EVENT_USERSPACE_PROBE:
2191 case LTTNG_EVENT_FUNCTION:
2192 case LTTNG_EVENT_FUNCTION_ENTRY:
2193 case LTTNG_EVENT_TRACEPOINT:
2194 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
2195 /* We have passed ownership */
2196 filter_expression = nullptr;
2197 filter = nullptr;
2198 if (ret != LTTNG_OK) {
2199 if (channel_created) {
2200 /* Let's not leak a useless channel. */
2201 kernel_destroy_channel(kchan);
2202 }
2203 goto error;
2204 }
2205 break;
2206 case LTTNG_EVENT_SYSCALL:
2207 ret = event_kernel_enable_event(kchan, event, filter_expression, filter);
2208 /* We have passed ownership */
2209 filter_expression = nullptr;
2210 filter = nullptr;
2211 if (ret != LTTNG_OK) {
2212 goto error;
2213 }
2214 break;
2215 default:
2216 ret = LTTNG_ERR_UNK;
2217 goto error;
2218 }
2219
2220 kernel_wait_quiescent();
2221 break;
2222 }
2223 case LTTNG_DOMAIN_UST:
2224 {
2225 struct ltt_ust_channel *uchan;
2226 struct ltt_ust_session *usess = session->ust_session;
2227
2228 LTTNG_ASSERT(usess);
2229
2230 /*
2231 * If a non-default channel has been created in the
2232 * session, explicitely require that -c chan_name needs
2233 * to be provided.
2234 */
2235 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2236 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2237 goto error;
2238 }
2239
2240 /* Get channel from global UST domain */
2241 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, channel_name);
2242 if (uchan == nullptr) {
2243 /* Create default channel */
2244 attr = channel_new_default_attr(LTTNG_DOMAIN_UST, usess->buffer_type);
2245 if (attr == nullptr) {
2246 ret = LTTNG_ERR_FATAL;
2247 goto error;
2248 }
2249 if (lttng_strncpy(attr->name, channel_name, sizeof(attr->name))) {
2250 ret = LTTNG_ERR_INVALID;
2251 goto error;
2252 }
2253
2254 ret = cmd_enable_channel_internal(session, domain, attr, wpipe);
2255 if (ret != LTTNG_OK) {
2256 goto error;
2257 }
2258
2259 /* Get the newly created channel reference back */
2260 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2261 channel_name);
2262 LTTNG_ASSERT(uchan);
2263 }
2264
2265 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2266 /*
2267 * Don't allow users to add UST events to channels which
2268 * are assigned to a userspace subdomain (JUL, Log4J,
2269 * Python, etc.).
2270 */
2271 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2272 goto error;
2273 }
2274
2275 if (!internal_event) {
2276 /*
2277 * Ensure the event name is not reserved for internal
2278 * use.
2279 */
2280 ret = validate_ust_event_name(event->name);
2281 if (ret) {
2282 WARN("Userspace event name %s failed validation.", event->name);
2283 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2284 goto error;
2285 }
2286 }
2287
2288 /* At this point, the session and channel exist on the tracer */
2289 ret = event_ust_enable_tracepoint(
2290 usess, uchan, event, filter_expression, filter, exclusion, internal_event);
2291 /* We have passed ownership */
2292 filter_expression = nullptr;
2293 filter = nullptr;
2294 exclusion = nullptr;
2295 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2296 goto already_enabled;
2297 } else if (ret != LTTNG_OK) {
2298 goto error;
2299 }
2300 break;
2301 }
2302 case LTTNG_DOMAIN_LOG4J:
2303 case LTTNG_DOMAIN_JUL:
2304 case LTTNG_DOMAIN_PYTHON:
2305 {
2306 const char *default_event_name, *default_chan_name;
2307 struct agent *agt;
2308 struct lttng_event uevent;
2309 struct lttng_domain tmp_dom;
2310 struct ltt_ust_session *usess = session->ust_session;
2311
2312 LTTNG_ASSERT(usess);
2313
2314 if (!agent_tracing_is_enabled()) {
2315 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2316 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2317 goto error;
2318 }
2319
2320 agt = trace_ust_find_agent(usess, domain->type);
2321 if (!agt) {
2322 agt = agent_create(domain->type);
2323 if (!agt) {
2324 ret = LTTNG_ERR_NOMEM;
2325 goto error;
2326 }
2327 agent_add(agt, usess->agents);
2328 }
2329
2330 /* Create the default tracepoint. */
2331 memset(&uevent, 0, sizeof(uevent));
2332 uevent.type = LTTNG_EVENT_TRACEPOINT;
2333 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2334 default_event_name = event_get_default_agent_ust_name(domain->type);
2335 if (!default_event_name) {
2336 ret = LTTNG_ERR_FATAL;
2337 goto error;
2338 }
2339 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2340 uevent.name[sizeof(uevent.name) - 1] = '\0';
2341
2342 /*
2343 * The domain type is changed because we are about to enable the
2344 * default channel and event for the JUL domain that are hardcoded.
2345 * This happens in the UST domain.
2346 */
2347 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2348 tmp_dom.type = LTTNG_DOMAIN_UST;
2349
2350 switch (domain->type) {
2351 case LTTNG_DOMAIN_LOG4J:
2352 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2353 break;
2354 case LTTNG_DOMAIN_JUL:
2355 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2356 break;
2357 case LTTNG_DOMAIN_PYTHON:
2358 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2359 break;
2360 default:
2361 /* The switch/case we are in makes this impossible */
2362 abort();
2363 }
2364
2365 {
2366 char *filter_expression_copy = nullptr;
2367 struct lttng_bytecode *filter_copy = nullptr;
2368
2369 if (filter) {
2370 const size_t filter_size =
2371 sizeof(struct lttng_bytecode) + filter->len;
2372
2373 filter_copy = zmalloc<lttng_bytecode>(filter_size);
2374 if (!filter_copy) {
2375 ret = LTTNG_ERR_NOMEM;
2376 goto error;
2377 }
2378 memcpy(filter_copy, filter, filter_size);
2379
2380 filter_expression_copy = strdup(filter_expression);
2381 if (!filter_expression) {
2382 ret = LTTNG_ERR_NOMEM;
2383 }
2384
2385 if (!filter_expression_copy || !filter_copy) {
2386 free(filter_expression_copy);
2387 free(filter_copy);
2388 goto error;
2389 }
2390 }
2391
2392 ret = cmd_enable_event_internal(session,
2393 &tmp_dom,
2394 (char *) default_chan_name,
2395 &uevent,
2396 filter_expression_copy,
2397 filter_copy,
2398 nullptr,
2399 wpipe);
2400 }
2401
2402 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2403 goto already_enabled;
2404 } else if (ret != LTTNG_OK) {
2405 goto error;
2406 }
2407
2408 /* The wild card * means that everything should be enabled. */
2409 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2410 ret = event_agent_enable_all(usess, agt, event, filter, filter_expression);
2411 } else {
2412 ret = event_agent_enable(usess, agt, event, filter, filter_expression);
2413 }
2414 filter = nullptr;
2415 filter_expression = nullptr;
2416 if (ret != LTTNG_OK) {
2417 goto error;
2418 }
2419
2420 break;
2421 }
2422 default:
2423 ret = LTTNG_ERR_UND;
2424 goto error;
2425 }
2426
2427 ret = LTTNG_OK;
2428
2429 already_enabled:
2430 error:
2431 free(filter_expression);
2432 free(filter);
2433 free(exclusion);
2434 channel_attr_destroy(attr);
2435 return ret;
2436 }
2437
2438 /*
2439 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2440 * We own filter, exclusion, and filter_expression.
2441 */
2442 int cmd_enable_event(struct command_ctx *cmd_ctx,
2443 struct lttng_event *event,
2444 char *filter_expression,
2445 struct lttng_event_exclusion *exclusion,
2446 struct lttng_bytecode *bytecode,
2447 int wpipe)
2448 {
2449 int ret;
2450 /*
2451 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2452 */
2453 const lttng_domain command_domain = cmd_ctx->lsm.domain;
2454
2455 /*
2456 * The ownership of the following parameters is transferred to
2457 * _cmd_enable_event:
2458 *
2459 * - filter_expression,
2460 * - bytecode,
2461 * - exclusion
2462 */
2463 ret = _cmd_enable_event(cmd_ctx->session,
2464 &command_domain,
2465 cmd_ctx->lsm.u.enable.channel_name,
2466 event,
2467 filter_expression,
2468 bytecode,
2469 exclusion,
2470 wpipe,
2471 false);
2472 filter_expression = nullptr;
2473 bytecode = nullptr;
2474 exclusion = nullptr;
2475 return ret;
2476 }
2477
2478 /*
2479 * Enable an event which is internal to LTTng. An internal should
2480 * never be made visible to clients and are immune to checks such as
2481 * reserved names.
2482 */
2483 static int cmd_enable_event_internal(struct ltt_session *session,
2484 const struct lttng_domain *domain,
2485 char *channel_name,
2486 struct lttng_event *event,
2487 char *filter_expression,
2488 struct lttng_bytecode *filter,
2489 struct lttng_event_exclusion *exclusion,
2490 int wpipe)
2491 {
2492 return _cmd_enable_event(session,
2493 domain,
2494 channel_name,
2495 event,
2496 filter_expression,
2497 filter,
2498 exclusion,
2499 wpipe,
2500 true);
2501 }
2502
2503 /*
2504 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2505 */
2506 enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
2507 struct lttng_payload *reply_payload)
2508 {
2509 enum lttng_error_code ret_code;
2510 int ret;
2511 ssize_t i, nb_events = 0;
2512 struct lttng_event *events = nullptr;
2513 struct lttcomm_list_command_header reply_command_header = {};
2514 size_t reply_command_header_offset;
2515
2516 assert(reply_payload);
2517
2518 /* Reserve space for command reply header. */
2519 reply_command_header_offset = reply_payload->buffer.size;
2520 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
2521 reply_command_header_offset +
2522 sizeof(struct lttcomm_list_command_header));
2523 if (ret) {
2524 ret_code = LTTNG_ERR_NOMEM;
2525 goto error;
2526 }
2527
2528 switch (domain) {
2529 case LTTNG_DOMAIN_KERNEL:
2530 nb_events = kernel_list_events(&events);
2531 if (nb_events < 0) {
2532 ret_code = LTTNG_ERR_KERN_LIST_FAIL;
2533 goto error;
2534 }
2535 break;
2536 case LTTNG_DOMAIN_UST:
2537 nb_events = ust_app_list_events(&events);
2538 if (nb_events < 0) {
2539 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2540 goto error;
2541 }
2542 break;
2543 case LTTNG_DOMAIN_LOG4J:
2544 case LTTNG_DOMAIN_JUL:
2545 case LTTNG_DOMAIN_PYTHON:
2546 nb_events = agent_list_events(&events, domain);
2547 if (nb_events < 0) {
2548 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2549 goto error;
2550 }
2551 break;
2552 default:
2553 ret_code = LTTNG_ERR_UND;
2554 goto error;
2555 }
2556
2557 for (i = 0; i < nb_events; i++) {
2558 ret = lttng_event_serialize(
2559 &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
2560 if (ret) {
2561 ret_code = LTTNG_ERR_NOMEM;
2562 goto error;
2563 }
2564 }
2565
2566 if (nb_events > UINT32_MAX) {
2567 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2568 ret_code = LTTNG_ERR_OVERFLOW;
2569 goto error;
2570 }
2571
2572 /* Update command reply header. */
2573 reply_command_header.count = (uint32_t) nb_events;
2574 memcpy(reply_payload->buffer.data + reply_command_header_offset,
2575 &reply_command_header,
2576 sizeof(reply_command_header));
2577
2578 ret_code = LTTNG_OK;
2579 error:
2580 free(events);
2581 return ret_code;
2582 }
2583
2584 /*
2585 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2586 */
2587 enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2588 struct lttng_payload *reply)
2589 {
2590 enum lttng_error_code ret_code;
2591 int ret;
2592 unsigned int i, nb_fields;
2593 struct lttng_event_field *fields = nullptr;
2594 struct lttcomm_list_command_header reply_command_header = {};
2595 size_t reply_command_header_offset;
2596
2597 assert(reply);
2598
2599 /* Reserve space for command reply header. */
2600 reply_command_header_offset = reply->buffer.size;
2601 ret = lttng_dynamic_buffer_set_size(&reply->buffer,
2602 reply_command_header_offset +
2603 sizeof(struct lttcomm_list_command_header));
2604 if (ret) {
2605 ret_code = LTTNG_ERR_NOMEM;
2606 goto error;
2607 }
2608
2609 switch (domain) {
2610 case LTTNG_DOMAIN_UST:
2611 ret = ust_app_list_event_fields(&fields);
2612 if (ret < 0) {
2613 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2614 goto error;
2615 }
2616
2617 break;
2618 case LTTNG_DOMAIN_KERNEL:
2619 default: /* fall-through */
2620 ret_code = LTTNG_ERR_UND;
2621 goto error;
2622 }
2623
2624 nb_fields = ret;
2625
2626 for (i = 0; i < nb_fields; i++) {
2627 ret = lttng_event_field_serialize(&fields[i], reply);
2628 if (ret) {
2629 ret_code = LTTNG_ERR_NOMEM;
2630 goto error;
2631 }
2632 }
2633
2634 if (nb_fields > UINT32_MAX) {
2635 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2636 ret_code = LTTNG_ERR_OVERFLOW;
2637 goto error;
2638 }
2639
2640 /* Update command reply header. */
2641 reply_command_header.count = (uint32_t) nb_fields;
2642
2643 memcpy(reply->buffer.data + reply_command_header_offset,
2644 &reply_command_header,
2645 sizeof(reply_command_header));
2646
2647 ret_code = LTTNG_OK;
2648
2649 error:
2650 free(fields);
2651 return ret_code;
2652 }
2653
2654 enum lttng_error_code cmd_list_syscalls(struct lttng_payload *reply_payload)
2655 {
2656 enum lttng_error_code ret_code;
2657 ssize_t nb_events, i;
2658 int ret;
2659 struct lttng_event *events = nullptr;
2660 struct lttcomm_list_command_header reply_command_header = {};
2661 size_t reply_command_header_offset;
2662
2663 assert(reply_payload);
2664
2665 /* Reserve space for command reply header. */
2666 reply_command_header_offset = reply_payload->buffer.size;
2667 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
2668 reply_command_header_offset +
2669 sizeof(struct lttcomm_list_command_header));
2670 if (ret) {
2671 ret_code = LTTNG_ERR_NOMEM;
2672 goto end;
2673 }
2674
2675 nb_events = syscall_table_list(&events);
2676 if (nb_events < 0) {
2677 ret_code = (enum lttng_error_code) - nb_events;
2678 goto end;
2679 }
2680
2681 for (i = 0; i < nb_events; i++) {
2682 ret = lttng_event_serialize(
2683 &events[i], 0, nullptr, nullptr, 0, nullptr, reply_payload);
2684 if (ret) {
2685 ret_code = LTTNG_ERR_NOMEM;
2686 goto end;
2687 }
2688 }
2689
2690 if (nb_events > UINT32_MAX) {
2691 ERR("Syscall count would overflow the syscall listing command's reply");
2692 ret_code = LTTNG_ERR_OVERFLOW;
2693 goto end;
2694 }
2695
2696 /* Update command reply header. */
2697 reply_command_header.count = (uint32_t) nb_events;
2698 memcpy(reply_payload->buffer.data + reply_command_header_offset,
2699 &reply_command_header,
2700 sizeof(reply_command_header));
2701
2702 ret_code = LTTNG_OK;
2703 end:
2704 free(events);
2705 return ret_code;
2706 }
2707
2708 /*
2709 * Command LTTNG_START_TRACE processed by the client thread.
2710 *
2711 * Called with session mutex held.
2712 */
2713 int cmd_start_trace(struct ltt_session *session)
2714 {
2715 enum lttng_error_code ret;
2716 unsigned long nb_chan = 0;
2717 struct ltt_kernel_session *ksession;
2718 struct ltt_ust_session *usess;
2719 const bool session_rotated_after_last_stop = session->rotated_after_last_stop;
2720 const bool session_cleared_after_last_stop = session->cleared_after_last_stop;
2721
2722 LTTNG_ASSERT(session);
2723
2724 /* Ease our life a bit ;) */
2725 ksession = session->kernel_session;
2726 usess = session->ust_session;
2727
2728 /* Is the session already started? */
2729 if (session->active) {
2730 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2731 /* Perform nothing */
2732 goto end;
2733 }
2734
2735 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2736 !session->current_trace_chunk) {
2737 /*
2738 * A rotation was launched while the session was stopped and
2739 * it has not been completed yet. It is not possible to start
2740 * the session since starting the session here would require a
2741 * rotation from "NULL" to a new trace chunk. That rotation
2742 * would overlap with the ongoing rotation, which is not
2743 * supported.
2744 */
2745 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2746 session->name);
2747 ret = LTTNG_ERR_ROTATION_PENDING;
2748 goto error;
2749 }
2750
2751 /*
2752 * Starting a session without channel is useless since after that it's not
2753 * possible to enable channel thus inform the client.
2754 */
2755 if (usess && usess->domain_global.channels) {
2756 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2757 }
2758 if (ksession) {
2759 nb_chan += ksession->channel_count;
2760 }
2761 if (!nb_chan) {
2762 ret = LTTNG_ERR_NO_CHANNEL;
2763 goto error;
2764 }
2765
2766 session->active = true;
2767 session->rotated_after_last_stop = false;
2768 session->cleared_after_last_stop = false;
2769 if (session->output_traces && !session->current_trace_chunk) {
2770 if (!session->has_been_started) {
2771 struct lttng_trace_chunk *trace_chunk;
2772
2773 DBG("Creating initial trace chunk of session \"%s\"", session->name);
2774 trace_chunk =
2775 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
2776 if (!trace_chunk) {
2777 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2778 goto error;
2779 }
2780 LTTNG_ASSERT(!session->current_trace_chunk);
2781 ret = (lttng_error_code) session_set_trace_chunk(
2782 session, trace_chunk, nullptr);
2783 lttng_trace_chunk_put(trace_chunk);
2784 if (ret) {
2785 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2786 goto error;
2787 }
2788 } else {
2789 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2790 session->name);
2791 /*
2792 * Rotate existing streams into the new chunk.
2793 * This is a "quiet" rotation has no client has
2794 * explicitly requested this operation.
2795 *
2796 * There is also no need to wait for the rotation
2797 * to complete as it will happen immediately. No data
2798 * was produced as the session was stopped, so the
2799 * rotation should happen on reception of the command.
2800 */
2801 ret = (lttng_error_code) cmd_rotate_session(
2802 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2803 if (ret != LTTNG_OK) {
2804 goto error;
2805 }
2806 }
2807 }
2808
2809 /* Kernel tracing */
2810 if (ksession != nullptr) {
2811 DBG("Start kernel tracing session %s", session->name);
2812 ret = (lttng_error_code) start_kernel_session(ksession);
2813 if (ret != LTTNG_OK) {
2814 goto error;
2815 }
2816 }
2817
2818 /* Flag session that trace should start automatically */
2819 if (usess) {
2820 int int_ret = ust_app_start_trace_all(usess);
2821
2822 if (int_ret < 0) {
2823 ret = LTTNG_ERR_UST_START_FAIL;
2824 goto error;
2825 }
2826 }
2827
2828 /*
2829 * Open a packet in every stream of the session to ensure that viewers
2830 * can correctly identify the boundaries of the periods during which
2831 * tracing was active for this session.
2832 */
2833 ret = session_open_packets(session);
2834 if (ret != LTTNG_OK) {
2835 goto error;
2836 }
2837
2838 /*
2839 * Clear the flag that indicates that a rotation was done while the
2840 * session was stopped.
2841 */
2842 session->rotated_after_last_stop = false;
2843
2844 if (session->rotate_timer_period && !session->rotation_schedule_timer_enabled) {
2845 int int_ret = timer_session_rotation_schedule_timer_start(
2846 session, session->rotate_timer_period);
2847
2848 if (int_ret < 0) {
2849 ERR("Failed to enable rotate timer");
2850 ret = LTTNG_ERR_UNK;
2851 goto error;
2852 }
2853 }
2854
2855 ret = LTTNG_OK;
2856
2857 error:
2858 if (ret == LTTNG_OK) {
2859 /* Flag this after a successful start. */
2860 session->has_been_started = true;
2861 } else {
2862 session->active = false;
2863 /* Restore initial state on error. */
2864 session->rotated_after_last_stop = session_rotated_after_last_stop;
2865 session->cleared_after_last_stop = session_cleared_after_last_stop;
2866 }
2867 end:
2868 return ret;
2869 }
2870
2871 /*
2872 * Command LTTNG_STOP_TRACE processed by the client thread.
2873 */
2874 int cmd_stop_trace(struct ltt_session *session)
2875 {
2876 int ret;
2877 struct ltt_kernel_session *ksession;
2878 struct ltt_ust_session *usess;
2879
2880 LTTNG_ASSERT(session);
2881
2882 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2883 /* Short cut */
2884 ksession = session->kernel_session;
2885 usess = session->ust_session;
2886
2887 /* Session is not active. Skip everything and inform the client. */
2888 if (!session->active) {
2889 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2890 goto error;
2891 }
2892
2893 ret = stop_kernel_session(ksession);
2894 if (ret != LTTNG_OK) {
2895 goto error;
2896 }
2897
2898 if (usess && usess->active) {
2899 ret = ust_app_stop_trace_all(usess);
2900 if (ret < 0) {
2901 ret = LTTNG_ERR_UST_STOP_FAIL;
2902 goto error;
2903 }
2904 }
2905
2906 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2907 /* Flag inactive after a successful stop. */
2908 session->active = false;
2909 ret = LTTNG_OK;
2910
2911 error:
2912 return ret;
2913 }
2914
2915 /*
2916 * Set the base_path of the session only if subdir of a control uris is set.
2917 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2918 */
2919 static int
2920 set_session_base_path_from_uris(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
2921 {
2922 int ret;
2923 size_t i;
2924
2925 for (i = 0; i < nb_uri; i++) {
2926 if (uris[i].stype != LTTNG_STREAM_CONTROL || uris[i].subdir[0] == '\0') {
2927 /* Not interested in these URIs */
2928 continue;
2929 }
2930
2931 if (session->base_path != nullptr) {
2932 free(session->base_path);
2933 session->base_path = nullptr;
2934 }
2935
2936 /* Set session base_path */
2937 session->base_path = strdup(uris[i].subdir);
2938 if (!session->base_path) {
2939 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2940 uris[i].subdir,
2941 session->name);
2942 ret = LTTNG_ERR_NOMEM;
2943 goto error;
2944 }
2945 DBG2("Setting base path \"%s\" for session \"%s\"",
2946 session->base_path,
2947 session->name);
2948 }
2949 ret = LTTNG_OK;
2950 error:
2951 return ret;
2952 }
2953
2954 /*
2955 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2956 */
2957 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, struct lttng_uri *uris)
2958 {
2959 int ret, i;
2960 struct ltt_kernel_session *ksess = session->kernel_session;
2961 struct ltt_ust_session *usess = session->ust_session;
2962
2963 LTTNG_ASSERT(session);
2964 LTTNG_ASSERT(uris);
2965 LTTNG_ASSERT(nb_uri > 0);
2966
2967 /* Can't set consumer URI if the session is active. */
2968 if (session->active) {
2969 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2970 goto error;
2971 }
2972
2973 /*
2974 * Set the session base path if any. This is done inside
2975 * cmd_set_consumer_uri to preserve backward compatibility of the
2976 * previous session creation api vs the session descriptor api.
2977 */
2978 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2979 if (ret != LTTNG_OK) {
2980 goto error;
2981 }
2982
2983 /* Set the "global" consumer URIs */
2984 for (i = 0; i < nb_uri; i++) {
2985 ret = add_uri_to_consumer(session, session->consumer, &uris[i], LTTNG_DOMAIN_NONE);
2986 if (ret != LTTNG_OK) {
2987 goto error;
2988 }
2989 }
2990
2991 /* Set UST session URIs */
2992 if (session->ust_session) {
2993 for (i = 0; i < nb_uri; i++) {
2994 ret = add_uri_to_consumer(session,
2995 session->ust_session->consumer,
2996 &uris[i],
2997 LTTNG_DOMAIN_UST);
2998 if (ret != LTTNG_OK) {
2999 goto error;
3000 }
3001 }
3002 }
3003
3004 /* Set kernel session URIs */
3005 if (session->kernel_session) {
3006 for (i = 0; i < nb_uri; i++) {
3007 ret = add_uri_to_consumer(session,
3008 session->kernel_session->consumer,
3009 &uris[i],
3010 LTTNG_DOMAIN_KERNEL);
3011 if (ret != LTTNG_OK) {
3012 goto error;
3013 }
3014 }
3015 }
3016
3017 /*
3018 * Make sure to set the session in output mode after we set URI since a
3019 * session can be created without URL (thus flagged in no output mode).
3020 */
3021 session->output_traces = 1;
3022 if (ksess) {
3023 ksess->output_traces = 1;
3024 }
3025
3026 if (usess) {
3027 usess->output_traces = 1;
3028 }
3029
3030 /* All good! */
3031 ret = LTTNG_OK;
3032
3033 error:
3034 return ret;
3035 }
3036
3037 static enum lttng_error_code
3038 set_session_output_from_descriptor(struct ltt_session *session,
3039 const struct lttng_session_descriptor *descriptor)
3040 {
3041 int ret;
3042 enum lttng_error_code ret_code = LTTNG_OK;
3043 enum lttng_session_descriptor_type session_type =
3044 lttng_session_descriptor_get_type(descriptor);
3045 enum lttng_session_descriptor_output_type output_type =
3046 lttng_session_descriptor_get_output_type(descriptor);
3047 struct lttng_uri uris[2] = {};
3048 size_t uri_count = 0;
3049
3050 switch (output_type) {
3051 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3052 goto end;
3053 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
3054 lttng_session_descriptor_get_local_output_uri(descriptor, &uris[0]);
3055 uri_count = 1;
3056 break;
3057 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
3058 lttng_session_descriptor_get_network_output_uris(descriptor, &uris[0], &uris[1]);
3059 uri_count = 2;
3060 break;
3061 default:
3062 ret_code = LTTNG_ERR_INVALID;
3063 goto end;
3064 }
3065
3066 switch (session_type) {
3067 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3068 {
3069 struct snapshot_output *new_output = nullptr;
3070
3071 new_output = snapshot_output_alloc();
3072 if (!new_output) {
3073 ret_code = LTTNG_ERR_NOMEM;
3074 goto end;
3075 }
3076
3077 ret = snapshot_output_init_with_uri(session,
3078 DEFAULT_SNAPSHOT_MAX_SIZE,
3079 nullptr,
3080 uris,
3081 uri_count,
3082 session->consumer,
3083 new_output,
3084 &session->snapshot);
3085 if (ret < 0) {
3086 ret_code = (ret == -ENOMEM) ? LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
3087 snapshot_output_destroy(new_output);
3088 goto end;
3089 }
3090 snapshot_add_output(&session->snapshot, new_output);
3091 break;
3092 }
3093 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3094 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3095 {
3096 ret_code = (lttng_error_code) cmd_set_consumer_uri(session, uri_count, uris);
3097 break;
3098 }
3099 default:
3100 ret_code = LTTNG_ERR_INVALID;
3101 goto end;
3102 }
3103 end:
3104 return ret_code;
3105 }
3106
3107 static enum lttng_error_code
3108 cmd_create_session_from_descriptor(struct lttng_session_descriptor *descriptor,
3109 const lttng_sock_cred *creds,
3110 const char *home_path)
3111 {
3112 int ret;
3113 enum lttng_error_code ret_code;
3114 const char *session_name;
3115 struct ltt_session *new_session = nullptr;
3116 enum lttng_session_descriptor_status descriptor_status;
3117
3118 session_lock_list();
3119 if (home_path) {
3120 if (*home_path != '/') {
3121 ERR("Home path provided by client is not absolute");
3122 ret_code = LTTNG_ERR_INVALID;
3123 goto end;
3124 }
3125 }
3126
3127 descriptor_status = lttng_session_descriptor_get_session_name(descriptor, &session_name);
3128 switch (descriptor_status) {
3129 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3130 break;
3131 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3132 session_name = nullptr;
3133 break;
3134 default:
3135 ret_code = LTTNG_ERR_INVALID;
3136 goto end;
3137 }
3138
3139 ret_code = session_create(session_name, creds->uid, creds->gid, &new_session);
3140 if (ret_code != LTTNG_OK) {
3141 goto end;
3142 }
3143
3144 ret_code = notification_thread_command_add_session(the_notification_thread_handle,
3145 new_session->id,
3146 new_session->name,
3147 new_session->uid,
3148 new_session->gid);
3149 if (ret_code != LTTNG_OK) {
3150 goto end;
3151 }
3152
3153 /* Announce the session's destruction to the notification thread when it is destroyed. */
3154 ret = session_add_destroy_notifier(
3155 new_session,
3156 [](const struct ltt_session *session, void *user_data __attribute__((unused))) {
3157 (void) notification_thread_command_remove_session(
3158 the_notification_thread_handle, session->id);
3159 },
3160 nullptr);
3161 if (ret) {
3162 PERROR("Failed to add notification thread command to session's destroy notifiers: session name = %s",
3163 new_session->name);
3164 ret = LTTNG_ERR_NOMEM;
3165 goto end;
3166 }
3167
3168 if (!session_name) {
3169 ret = lttng_session_descriptor_set_session_name(descriptor, new_session->name);
3170 if (ret) {
3171 ret_code = LTTNG_ERR_SESSION_FAIL;
3172 goto end;
3173 }
3174 }
3175
3176 if (!lttng_session_descriptor_is_output_destination_initialized(descriptor)) {
3177 /*
3178 * Only include the session's creation time in the output
3179 * destination if the name of the session itself was
3180 * not auto-generated.
3181 */
3182 ret_code = lttng_session_descriptor_set_default_output(
3183 descriptor,
3184 session_name ? &new_session->creation_time : nullptr,
3185 home_path);
3186 if (ret_code != LTTNG_OK) {
3187 goto end;
3188 }
3189 } else {
3190 new_session->has_user_specified_directory =
3191 lttng_session_descriptor_has_output_directory(descriptor);
3192 }
3193
3194 switch (lttng_session_descriptor_get_type(descriptor)) {
3195 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3196 new_session->snapshot_mode = 1;
3197 break;
3198 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3199 new_session->live_timer =
3200 lttng_session_descriptor_live_get_timer_interval(descriptor);
3201 break;
3202 default:
3203 break;
3204 }
3205
3206 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3207 if (ret_code != LTTNG_OK) {
3208 goto end;
3209 }
3210 new_session->consumer->enabled = true;
3211 ret_code = LTTNG_OK;
3212 end:
3213 /* Release reference provided by the session_create function. */
3214 session_put(new_session);
3215 if (ret_code != LTTNG_OK && new_session) {
3216 /* Release the global reference on error. */
3217 session_destroy(new_session);
3218 }
3219 session_unlock_list();
3220 return ret_code;
3221 }
3222
3223 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx,
3224 int sock,
3225 struct lttng_session_descriptor **return_descriptor)
3226 {
3227 int ret;
3228 size_t payload_size;
3229 struct lttng_dynamic_buffer payload;
3230 struct lttng_buffer_view home_dir_view;
3231 struct lttng_buffer_view session_descriptor_view;
3232 struct lttng_session_descriptor *session_descriptor = nullptr;
3233 enum lttng_error_code ret_code;
3234
3235 lttng_dynamic_buffer_init(&payload);
3236 if (cmd_ctx->lsm.u.create_session.home_dir_size >= LTTNG_PATH_MAX) {
3237 ret_code = LTTNG_ERR_INVALID;
3238 goto error;
3239 }
3240 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
3241 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3242 ret_code = LTTNG_ERR_INVALID;
3243 goto error;
3244 }
3245
3246 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3247 cmd_ctx->lsm.u.create_session.session_descriptor_size;
3248 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3249 if (ret) {
3250 ret_code = LTTNG_ERR_NOMEM;
3251 goto error;
3252 }
3253
3254 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3255 if (ret <= 0) {
3256 ERR("Reception of session descriptor failed, aborting.");
3257 ret_code = LTTNG_ERR_SESSION_FAIL;
3258 goto error;
3259 }
3260
3261 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3262 &payload, 0, cmd_ctx->lsm.u.create_session.home_dir_size);
3263 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
3264 !lttng_buffer_view_is_valid(&home_dir_view)) {
3265 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3266 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3267 goto error;
3268 }
3269
3270 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3271 &payload,
3272 cmd_ctx->lsm.u.create_session.home_dir_size,
3273 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3274 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3275 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3276 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3277 goto error;
3278 }
3279
3280 ret = lttng_session_descriptor_create_from_buffer(&session_descriptor_view,
3281 &session_descriptor);
3282 if (ret < 0) {
3283 ERR("Failed to create session descriptor from payload of \"create session\" command");
3284 ret_code = LTTNG_ERR_INVALID;
3285 goto error;
3286 }
3287
3288 /*
3289 * Sets the descriptor's auto-generated properties (name, output) if
3290 * needed.
3291 */
3292 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3293 &cmd_ctx->creds,
3294 home_dir_view.size ? home_dir_view.data :
3295 nullptr);
3296 if (ret_code != LTTNG_OK) {
3297 goto error;
3298 }
3299
3300 ret_code = LTTNG_OK;
3301 *return_descriptor = session_descriptor;
3302 session_descriptor = nullptr;
3303 error:
3304 lttng_dynamic_buffer_reset(&payload);
3305 lttng_session_descriptor_destroy(session_descriptor);
3306 return ret_code;
3307 }
3308
3309 static void cmd_destroy_session_reply(const struct ltt_session *session, void *_reply_context)
3310 {
3311 int ret;
3312 ssize_t comm_ret;
3313 const struct cmd_destroy_session_reply_context *reply_context =
3314 (cmd_destroy_session_reply_context *) _reply_context;
3315 struct lttng_dynamic_buffer payload;
3316 struct lttcomm_session_destroy_command_header cmd_header;
3317 struct lttng_trace_archive_location *location = nullptr;
3318 struct lttcomm_lttng_msg llm = {
3319 .cmd_type = LTTCOMM_SESSIOND_COMMAND_DESTROY_SESSION,
3320 .ret_code = reply_context->destruction_status,
3321 .pid = UINT32_MAX,
3322 .cmd_header_size = sizeof(struct lttcomm_session_destroy_command_header),
3323 .data_size = 0,
3324 .fd_count = 0,
3325 };
3326 size_t payload_size_before_location;
3327
3328 lttng_dynamic_buffer_init(&payload);
3329
3330 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3331 if (ret) {
3332 ERR("Failed to append session destruction message");
3333 goto error;
3334 }
3335
3336 cmd_header.rotation_state = (int32_t) (reply_context->implicit_rotation_on_destroy ?
3337 session->rotation_state :
3338 LTTNG_ROTATION_STATE_NO_ROTATION);
3339 ret = lttng_dynamic_buffer_append(&payload, &cmd_header, sizeof(cmd_header));
3340 if (ret) {
3341 ERR("Failed to append session destruction command header");
3342 goto error;
3343 }
3344
3345 if (!reply_context->implicit_rotation_on_destroy) {
3346 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3347 session->name);
3348 goto send_reply;
3349 }
3350 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3351 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3352 session->name);
3353 goto send_reply;
3354 }
3355
3356 location = session_get_trace_archive_location(session);
3357 if (!location) {
3358 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3359 session->name);
3360 goto error;
3361 }
3362
3363 payload_size_before_location = payload.size;
3364 comm_ret = lttng_trace_archive_location_serialize(location, &payload);
3365 lttng_trace_archive_location_put(location);
3366 if (comm_ret < 0) {
3367 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3368 session->name);
3369 goto error;
3370 }
3371 /* Update the message to indicate the location's length. */
3372 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3373 payload.size - payload_size_before_location;
3374 send_reply:
3375 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, payload.data, payload.size);
3376 if (comm_ret != (ssize_t) payload.size) {
3377 ERR("Failed to send result of the destruction of session \"%s\" to client",
3378 session->name);
3379 }
3380 error:
3381 ret = close(reply_context->reply_sock_fd);
3382 if (ret) {
3383 PERROR("Failed to close client socket in deferred session destroy reply");
3384 }
3385 lttng_dynamic_buffer_reset(&payload);
3386 free(_reply_context);
3387 }
3388
3389 /*
3390 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3391 *
3392 * Called with session lock held.
3393 */
3394 int cmd_destroy_session(struct ltt_session *session,
3395 struct notification_thread_handle *notification_thread_handle,
3396 int *sock_fd)
3397 {
3398 int ret;
3399 enum lttng_error_code destruction_last_error = LTTNG_OK;
3400 struct cmd_destroy_session_reply_context *reply_context = nullptr;
3401
3402 if (sock_fd) {
3403 reply_context = zmalloc<cmd_destroy_session_reply_context>();
3404 if (!reply_context) {
3405 ret = LTTNG_ERR_NOMEM;
3406 goto end;
3407 }
3408
3409 reply_context->reply_sock_fd = *sock_fd;
3410 }
3411
3412 /* Safety net */
3413 LTTNG_ASSERT(session);
3414
3415 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id);
3416 if (session->active) {
3417 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3418 session->name);
3419 ret = cmd_stop_trace(session);
3420 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3421 /* Carry on with the destruction of the session. */
3422 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3423 session->name,
3424 lttng_strerror(-ret));
3425 destruction_last_error = (lttng_error_code) ret;
3426 }
3427 }
3428
3429 if (session->rotation_schedule_timer_enabled) {
3430 if (timer_session_rotation_schedule_timer_stop(session)) {
3431 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3432 session->name);
3433 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
3434 }
3435 }
3436
3437 if (session->rotate_size) {
3438 unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
3439 session->rotate_size = 0;
3440 }
3441
3442 if (session->rotated && session->current_trace_chunk && session->output_traces) {
3443 /*
3444 * Perform a last rotation on destruction if rotations have
3445 * occurred during the session's lifetime.
3446 */
3447 ret = cmd_rotate_session(
3448 session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
3449 if (ret != LTTNG_OK) {
3450 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3451 session->name,
3452 lttng_strerror(-ret));
3453 destruction_last_error = (lttng_error_code) -ret;
3454 }
3455 if (reply_context) {
3456 reply_context->implicit_rotation_on_destroy = true;
3457 }
3458 } else if (session->has_been_started && session->current_trace_chunk) {
3459 /*
3460 * The user has not triggered a session rotation. However, to
3461 * ensure all data has been consumed, the session is rotated
3462 * to a 'null' trace chunk before it is destroyed.
3463 *
3464 * This is a "quiet" rotation meaning that no notification is
3465 * emitted and no renaming of the current trace chunk takes
3466 * place.
3467 */
3468 ret = cmd_rotate_session(
3469 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
3470 /*
3471 * Rotation operations may not be supported by the kernel
3472 * tracer. Hence, do not consider this implicit rotation as
3473 * a session destruction error. The library has already stopped
3474 * the session and waited for pending data; there is nothing
3475 * left to do but complete the destruction of the session.
3476 */
3477 if (ret != LTTNG_OK && ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
3478 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3479 session->name,
3480 lttng_strerror(ret));
3481 destruction_last_error = (lttng_error_code) -ret;
3482 }
3483 }
3484
3485 if (session->shm_path[0]) {
3486 /*
3487 * When a session is created with an explicit shm_path,
3488 * the consumer daemon will create its shared memory files
3489 * at that location and will *not* unlink them. This is normal
3490 * as the intention of that feature is to make it possible
3491 * to retrieve the content of those files should a crash occur.
3492 *
3493 * To ensure the content of those files can be used, the
3494 * sessiond daemon will replicate the content of the metadata
3495 * cache in a metadata file.
3496 *
3497 * On clean-up, it is expected that the consumer daemon will
3498 * unlink the shared memory files and that the session daemon
3499 * will unlink the metadata file. Then, the session's directory
3500 * in the shm path can be removed.
3501 *
3502 * Unfortunately, a flaw in the design of the sessiond's and
3503 * consumerd's tear down of channels makes it impossible to
3504 * determine when the sessiond _and_ the consumerd have both
3505 * destroyed their representation of a channel. For one, the
3506 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3507 * callbacks in both daemons.
3508 *
3509 * However, it is also impossible for the sessiond to know when
3510 * the consumer daemon is done destroying its channel(s) since
3511 * it occurs as a reaction to the closing of the channel's file
3512 * descriptor. There is no resulting communication initiated
3513 * from the consumerd to the sessiond to confirm that the
3514 * operation is completed (and was successful).
3515 *
3516 * Until this is all fixed, the session daemon checks for the
3517 * removal of the session's shm path which makes it possible
3518 * to safely advertise a session as having been destroyed.
3519 *
3520 * Prior to this fix, it was not possible to reliably save
3521 * a session making use of the --shm-path option, destroy it,
3522 * and load it again. This is because the creation of the
3523 * session would fail upon seeing the session's shm path
3524 * already in existence.
3525 *
3526 * Note that none of the error paths in the check for the
3527 * directory's existence return an error. This is normal
3528 * as there isn't much that can be done. The session will
3529 * be destroyed properly, except that we can't offer the
3530 * guarantee that the same session can be re-created.
3531 */
3532 current_completion_handler = &destroy_completion_handler.handler;
3533 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3534 session->shm_path,
3535 sizeof(destroy_completion_handler.shm_path));
3536 LTTNG_ASSERT(!ret);
3537 }
3538
3539 /*
3540 * The session is destroyed. However, note that the command context
3541 * still holds a reference to the session, thus delaying its destruction
3542 * _at least_ up to the point when that reference is released.
3543 */
3544 session_destroy(session);
3545 if (reply_context) {
3546 reply_context->destruction_status = destruction_last_error;
3547 ret = session_add_destroy_notifier(
3548 session, cmd_destroy_session_reply, (void *) reply_context);
3549 if (ret) {
3550 ret = LTTNG_ERR_FATAL;
3551 goto end;
3552 } else {
3553 *sock_fd = -1;
3554 }
3555 }
3556 ret = LTTNG_OK;
3557 end:
3558 return ret;
3559 }
3560
3561 /*
3562 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3563 */
3564 int cmd_register_consumer(struct ltt_session *session,
3565 enum lttng_domain_type domain,
3566 const char *sock_path,
3567 struct consumer_data *cdata)
3568 {
3569 int ret, sock;
3570 struct consumer_socket *socket = nullptr;
3571
3572 LTTNG_ASSERT(session);
3573 LTTNG_ASSERT(cdata);
3574 LTTNG_ASSERT(sock_path);
3575
3576 switch (domain) {
3577 case LTTNG_DOMAIN_KERNEL:
3578 {
3579 struct ltt_kernel_session *ksess = session->kernel_session;
3580
3581 LTTNG_ASSERT(ksess);
3582
3583 /* Can't register a consumer if there is already one */
3584 if (ksess->consumer_fds_sent != 0) {
3585 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3586 goto error;
3587 }
3588
3589 sock = lttcomm_connect_unix_sock(sock_path);
3590 if (sock < 0) {
3591 ret = LTTNG_ERR_CONNECT_FAIL;
3592 goto error;
3593 }
3594 cdata->cmd_sock = sock;
3595
3596 socket = consumer_allocate_socket(&cdata->cmd_sock);
3597 if (socket == nullptr) {
3598 ret = close(sock);
3599 if (ret < 0) {
3600 PERROR("close register consumer");
3601 }
3602 cdata->cmd_sock = -1;
3603 ret = LTTNG_ERR_FATAL;
3604 goto error;
3605 }
3606
3607 socket->lock = zmalloc<pthread_mutex_t>();
3608 if (socket->lock == nullptr) {
3609 PERROR("zmalloc pthread mutex");
3610 ret = LTTNG_ERR_FATAL;
3611 goto error;
3612 }
3613
3614 pthread_mutex_init(socket->lock, nullptr);
3615 socket->registered = 1;
3616
3617 lttng::urcu::read_lock_guard read_lock;
3618 consumer_add_socket(socket, ksess->consumer);
3619
3620 pthread_mutex_lock(&cdata->pid_mutex);
3621 cdata->pid = -1;
3622 pthread_mutex_unlock(&cdata->pid_mutex);
3623
3624 break;
3625 }
3626 default:
3627 /* TODO: Userspace tracing */
3628 ret = LTTNG_ERR_UND;
3629 goto error;
3630 }
3631
3632 return LTTNG_OK;
3633
3634 error:
3635 if (socket) {
3636 consumer_destroy_socket(socket);
3637 }
3638 return ret;
3639 }
3640
3641 /*
3642 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3643 */
3644 ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains)
3645 {
3646 int ret, index = 0;
3647 ssize_t nb_dom = 0;
3648 struct agent *agt;
3649 struct lttng_ht_iter iter;
3650
3651 if (session->kernel_session != nullptr) {
3652 DBG3("Listing domains found kernel domain");
3653 nb_dom++;
3654 }
3655
3656 if (session->ust_session != nullptr) {
3657 DBG3("Listing domains found UST global domain");
3658 nb_dom++;
3659
3660 lttng::urcu::read_lock_guard read_lock;
3661
3662 cds_lfht_for_each_entry (
3663 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3664 if (agt->being_used) {
3665 nb_dom++;
3666 }
3667 }
3668 }
3669
3670 if (!nb_dom) {
3671 goto end;
3672 }
3673
3674 *domains = calloc<lttng_domain>(nb_dom);
3675 if (*domains == nullptr) {
3676 ret = LTTNG_ERR_FATAL;
3677 goto error;
3678 }
3679
3680 if (session->kernel_session != nullptr) {
3681 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3682
3683 /* Kernel session buffer type is always GLOBAL */
3684 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3685
3686 index++;
3687 }
3688
3689 if (session->ust_session != nullptr) {
3690 (*domains)[index].type = LTTNG_DOMAIN_UST;
3691 (*domains)[index].buf_type = session->ust_session->buffer_type;
3692 index++;
3693
3694 {
3695 lttng::urcu::read_lock_guard read_lock;
3696
3697 cds_lfht_for_each_entry (
3698 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3699 if (agt->being_used) {
3700 (*domains)[index].type = agt->domain;
3701 (*domains)[index].buf_type =
3702 session->ust_session->buffer_type;
3703 index++;
3704 }
3705 }
3706 }
3707 }
3708 end:
3709 return nb_dom;
3710
3711 error:
3712 /* Return negative value to differentiate return code */
3713 return -ret;
3714 }
3715
3716 /*
3717 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3718 */
3719 enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
3720 struct ltt_session *session,
3721 struct lttng_payload *payload)
3722 {
3723 int ret = 0;
3724 unsigned int i = 0;
3725 struct lttcomm_list_command_header cmd_header = {};
3726 size_t cmd_header_offset;
3727 enum lttng_error_code ret_code;
3728
3729 assert(session);
3730 assert(payload);
3731
3732 DBG("Listing channels for session %s", session->name);
3733
3734 cmd_header_offset = payload->buffer.size;
3735
3736 /* Reserve space for command reply header. */
3737 ret = lttng_dynamic_buffer_set_size(&payload->buffer,
3738 cmd_header_offset + sizeof(cmd_header));
3739 if (ret) {
3740 ret_code = LTTNG_ERR_NOMEM;
3741 goto end;
3742 }
3743
3744 switch (domain) {
3745 case LTTNG_DOMAIN_KERNEL:
3746 {
3747 /* Kernel channels */
3748 struct ltt_kernel_channel *kchan;
3749 if (session->kernel_session != nullptr) {
3750 cds_list_for_each_entry (
3751 kchan, &session->kernel_session->channel_list.head, list) {
3752 uint64_t discarded_events, lost_packets;
3753 struct lttng_channel_extended *extended;
3754
3755 extended = (struct lttng_channel_extended *)
3756 kchan->channel->attr.extended.ptr;
3757
3758 ret = get_kernel_runtime_stats(
3759 session, kchan, &discarded_events, &lost_packets);
3760 if (ret < 0) {
3761 ret_code = LTTNG_ERR_UNK;
3762 goto end;
3763 }
3764
3765 /*
3766 * Update the discarded_events and lost_packets
3767 * count for the channel
3768 */
3769 extended->discarded_events = discarded_events;
3770 extended->lost_packets = lost_packets;
3771
3772 ret = lttng_channel_serialize(kchan->channel, &payload->buffer);
3773 if (ret) {
3774 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3775 kchan->channel->name);
3776 ret_code = LTTNG_ERR_UNK;
3777 goto end;
3778 }
3779
3780 i++;
3781 }
3782 }
3783 break;
3784 }
3785 case LTTNG_DOMAIN_UST:
3786 {
3787 struct lttng_ht_iter iter;
3788 struct ltt_ust_channel *uchan;
3789
3790 {
3791 lttng::urcu::read_lock_guard read_lock;
3792
3793 cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht,
3794 &iter.iter,
3795 uchan,
3796 node.node) {
3797 uint64_t discarded_events = 0, lost_packets = 0;
3798 struct lttng_channel *channel = nullptr;
3799 struct lttng_channel_extended *extended;
3800
3801 channel = trace_ust_channel_to_lttng_channel(uchan);
3802 if (!channel) {
3803 ret_code = LTTNG_ERR_NOMEM;
3804 goto end;
3805 }
3806
3807 extended = (struct lttng_channel_extended *)
3808 channel->attr.extended.ptr;
3809
3810 ret = get_ust_runtime_stats(
3811 session, uchan, &discarded_events, &lost_packets);
3812 if (ret < 0) {
3813 lttng_channel_destroy(channel);
3814 ret_code = LTTNG_ERR_UNK;
3815 goto end;
3816 }
3817
3818 extended->discarded_events = discarded_events;
3819 extended->lost_packets = lost_packets;
3820
3821 ret = lttng_channel_serialize(channel, &payload->buffer);
3822 if (ret) {
3823 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3824 channel->name);
3825 lttng_channel_destroy(channel);
3826 ret_code = LTTNG_ERR_UNK;
3827 goto end;
3828 }
3829
3830 lttng_channel_destroy(channel);
3831 i++;
3832 }
3833 }
3834
3835 break;
3836 }
3837 default:
3838 break;
3839 }
3840
3841 if (i > UINT32_MAX) {
3842 ERR("Channel count would overflow the channel listing command's reply");
3843 ret_code = LTTNG_ERR_OVERFLOW;
3844 goto end;
3845 }
3846
3847 /* Update command reply header. */
3848 cmd_header.count = (uint32_t) i;
3849 memcpy(payload->buffer.data + cmd_header_offset, &cmd_header, sizeof(cmd_header));
3850 ret_code = LTTNG_OK;
3851
3852 end:
3853 return ret_code;
3854 }
3855
3856 /*
3857 * Command LTTNG_LIST_EVENTS processed by the client thread.
3858 */
3859 enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
3860 struct ltt_session *session,
3861 char *channel_name,
3862 struct lttng_payload *reply_payload)
3863 {
3864 int buffer_resize_ret;
3865 enum lttng_error_code ret_code = LTTNG_OK;
3866 struct lttcomm_list_command_header reply_command_header = {};
3867 size_t reply_command_header_offset;
3868 unsigned int nb_events = 0;
3869
3870 assert(reply_payload);
3871
3872 /* Reserve space for command reply header. */
3873 reply_command_header_offset = reply_payload->buffer.size;
3874 buffer_resize_ret = lttng_dynamic_buffer_set_size(
3875 &reply_payload->buffer,
3876 reply_command_header_offset + sizeof(struct lttcomm_list_command_header));
3877 if (buffer_resize_ret) {
3878 ret_code = LTTNG_ERR_NOMEM;
3879 goto end;
3880 }
3881
3882 switch (domain) {
3883 case LTTNG_DOMAIN_KERNEL:
3884 if (session->kernel_session != nullptr) {
3885 ret_code = list_lttng_kernel_events(
3886 channel_name, session->kernel_session, reply_payload, &nb_events);
3887 }
3888
3889 break;
3890 case LTTNG_DOMAIN_UST:
3891 {
3892 if (session->ust_session != nullptr) {
3893 ret_code =
3894 list_lttng_ust_global_events(channel_name,
3895 &session->ust_session->domain_global,
3896 reply_payload,
3897 &nb_events);
3898 }
3899
3900 break;
3901 }
3902 case LTTNG_DOMAIN_LOG4J:
3903 case LTTNG_DOMAIN_JUL:
3904 case LTTNG_DOMAIN_PYTHON:
3905 if (session->ust_session) {
3906 struct lttng_ht_iter iter;
3907 struct agent *agt;
3908
3909 lttng::urcu::read_lock_guard read_lock;
3910
3911 cds_lfht_for_each_entry (
3912 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3913 if (agt->domain == domain) {
3914 ret_code = list_lttng_agent_events(
3915 agt, reply_payload, &nb_events);
3916 break;
3917 }
3918 }
3919 }
3920 break;
3921 default:
3922 ret_code = LTTNG_ERR_UND;
3923 break;
3924 }
3925
3926 if (nb_events > UINT32_MAX) {
3927 ret_code = LTTNG_ERR_OVERFLOW;
3928 goto end;
3929 }
3930
3931 /* Update command reply header. */
3932 reply_command_header.count = (uint32_t) nb_events;
3933 memcpy(reply_payload->buffer.data + reply_command_header_offset,
3934 &reply_command_header,
3935 sizeof(reply_command_header));
3936
3937 end:
3938 return ret_code;
3939 }
3940
3941 /*
3942 * Using the session list, filled a lttng_session array to send back to the
3943 * client for session listing.
3944 *
3945 * The session list lock MUST be acquired before calling this function. Use
3946 * session_lock_list() and session_unlock_list().
3947 */
3948 void cmd_list_lttng_sessions(struct lttng_session *sessions,
3949 size_t session_count,
3950 uid_t uid,
3951 gid_t gid)
3952 {
3953 int ret;
3954 unsigned int i = 0;
3955 struct ltt_session *session;
3956 struct ltt_session_list *list = session_get_list();
3957 struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]);
3958
3959 DBG("Getting all available session for UID %d GID %d", uid, gid);
3960 /*
3961 * Iterate over session list and append data after the control struct in
3962 * the buffer.
3963 */
3964 cds_list_for_each_entry (session, &list->head, list) {
3965 if (!session_get(session)) {
3966 continue;
3967 }
3968 /*
3969 * Only list the sessions the user can control.
3970 */
3971 if (!session_access_ok(session, uid) || session->destroyed) {
3972 session_put(session);
3973 continue;
3974 }
3975
3976 struct ltt_kernel_session *ksess = session->kernel_session;
3977 struct ltt_ust_session *usess = session->ust_session;
3978
3979 if (session->consumer->type == CONSUMER_DST_NET ||
3980 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3981 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3982 ret = build_network_session_path(
3983 sessions[i].path, sizeof(sessions[i].path), session);
3984 } else {
3985 ret = snprintf(sessions[i].path,
3986 sizeof(sessions[i].path),
3987 "%s",
3988 session->consumer->dst.session_root_path);
3989 }
3990 if (ret < 0) {
3991 PERROR("snprintf session path");
3992 session_put(session);
3993 continue;
3994 }
3995
3996 strncpy(sessions[i].name, session->name, NAME_MAX);
3997 sessions[i].name[NAME_MAX - 1] = '\0';
3998 sessions[i].enabled = session->active;
3999 sessions[i].snapshot_mode = session->snapshot_mode;
4000 sessions[i].live_timer_interval = session->live_timer;
4001 extended[i].creation_time.value = (uint64_t) session->creation_time;
4002 extended[i].creation_time.is_set = 1;
4003 i++;
4004 session_put(session);
4005 }
4006 }
4007
4008 /*
4009 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4010 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4011 */
4012 int cmd_data_pending(struct ltt_session *session)
4013 {
4014 int ret;
4015 struct ltt_kernel_session *ksess = session->kernel_session;
4016 struct ltt_ust_session *usess = session->ust_session;
4017
4018 LTTNG_ASSERT(session);
4019
4020 DBG("Data pending for session %s", session->name);
4021
4022 /* Session MUST be stopped to ask for data availability. */
4023 if (session->active) {
4024 ret = LTTNG_ERR_SESSION_STARTED;
4025 goto error;
4026 } else {
4027 /*
4028 * If stopped, just make sure we've started before else the above call
4029 * will always send that there is data pending.
4030 *
4031 * The consumer assumes that when the data pending command is received,
4032 * the trace has been started before or else no output data is written
4033 * by the streams which is a condition for data pending. So, this is
4034 * *VERY* important that we don't ask the consumer before a start
4035 * trace.
4036 */
4037 if (!session->has_been_started) {
4038 ret = 0;
4039 goto error;
4040 }
4041 }
4042
4043 /* A rotation is still pending, we have to wait. */
4044 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
4045 DBG("Rotate still pending for session %s", session->name);
4046 ret = 1;
4047 goto error;
4048 }
4049
4050 if (ksess && ksess->consumer) {
4051 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
4052 if (ret == 1) {
4053 /* Data is still being extracted for the kernel. */
4054 goto error;
4055 }
4056 }
4057
4058 if (usess && usess->consumer) {
4059 ret = consumer_is_data_pending(usess->id, usess->consumer);
4060 if (ret == 1) {
4061 /* Data is still being extracted for the kernel. */
4062 goto error;
4063 }
4064 }
4065
4066 /* Data is ready to be read by a viewer */
4067 ret = 0;
4068
4069 error:
4070 return ret;
4071 }
4072
4073 /*
4074 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4075 *
4076 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4077 */
4078 int cmd_snapshot_add_output(struct ltt_session *session,
4079 const struct lttng_snapshot_output *output,
4080 uint32_t *id)
4081 {
4082 int ret;
4083 struct snapshot_output *new_output;
4084
4085 LTTNG_ASSERT(session);
4086 LTTNG_ASSERT(output);
4087
4088 DBG("Cmd snapshot add output for session %s", session->name);
4089
4090 /*
4091 * Can't create an output if the session is not set in no-output mode.
4092 */
4093 if (session->output_traces) {
4094 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4095 goto error;
4096 }
4097
4098 if (session->has_non_mmap_channel) {
4099 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
4100 goto error;
4101 }
4102
4103 /* Only one output is allowed until we have the "tee" feature. */
4104 if (session->snapshot.nb_output == 1) {
4105 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
4106 goto error;
4107 }
4108
4109 new_output = snapshot_output_alloc();
4110 if (!new_output) {
4111 ret = LTTNG_ERR_NOMEM;
4112 goto error;
4113 }
4114
4115 ret = snapshot_output_init(session,
4116 output->max_size,
4117 output->name,
4118 output->ctrl_url,
4119 output->data_url,
4120 session->consumer,
4121 new_output,
4122 &session->snapshot);
4123 if (ret < 0) {
4124 if (ret == -ENOMEM) {
4125 ret = LTTNG_ERR_NOMEM;
4126 } else {
4127 ret = LTTNG_ERR_INVALID;
4128 }
4129 goto free_error;
4130 }
4131
4132 snapshot_add_output(&session->snapshot, new_output);
4133 if (id) {
4134 *id = new_output->id;
4135 }
4136
4137 return LTTNG_OK;
4138
4139 free_error:
4140 snapshot_output_destroy(new_output);
4141 error:
4142 return ret;
4143 }
4144
4145 /*
4146 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4147 *
4148 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4149 */
4150 int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output)
4151 {
4152 int ret;
4153 struct snapshot_output *sout = nullptr;
4154
4155 LTTNG_ASSERT(session);
4156 LTTNG_ASSERT(output);
4157
4158 lttng::urcu::read_lock_guard read_lock;
4159
4160 /*
4161 * Permission denied to create an output if the session is not
4162 * set in no output mode.
4163 */
4164 if (session->output_traces) {
4165 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4166 goto error;
4167 }
4168
4169 if (output->id) {
4170 DBG("Cmd snapshot del output id %" PRIu32 " for session %s",
4171 output->id,
4172 session->name);
4173 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
4174 } else if (*output->name != '\0') {
4175 DBG("Cmd snapshot del output name %s for session %s", output->name, session->name);
4176 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
4177 }
4178 if (!sout) {
4179 ret = LTTNG_ERR_INVALID;
4180 goto error;
4181 }
4182
4183 snapshot_delete_output(&session->snapshot, sout);
4184 snapshot_output_destroy(sout);
4185 ret = LTTNG_OK;
4186
4187 error:
4188 return ret;
4189 }
4190
4191 /*
4192 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4193 *
4194 * If no output is available, outputs is untouched and 0 is returned.
4195 *
4196 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4197 */
4198 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
4199 struct lttng_snapshot_output **outputs)
4200 {
4201 int ret, idx = 0;
4202 struct lttng_snapshot_output *list = nullptr;
4203 struct lttng_ht_iter iter;
4204 struct snapshot_output *output;
4205
4206 LTTNG_ASSERT(session);
4207 LTTNG_ASSERT(outputs);
4208
4209 DBG("Cmd snapshot list outputs for session %s", session->name);
4210
4211 /*
4212 * Permission denied to create an output if the session is not
4213 * set in no output mode.
4214 */
4215 if (session->output_traces) {
4216 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4217 goto end;
4218 }
4219
4220 if (session->snapshot.nb_output == 0) {
4221 ret = 0;
4222 goto end;
4223 }
4224
4225 list = calloc<lttng_snapshot_output>(session->snapshot.nb_output);
4226 if (!list) {
4227 ret = -LTTNG_ERR_NOMEM;
4228 goto end;
4229 }
4230
4231 /* Copy list from session to the new list object. */
4232 {
4233 lttng::urcu::read_lock_guard read_lock;
4234
4235 cds_lfht_for_each_entry (
4236 session->snapshot.output_ht->ht, &iter.iter, output, node.node) {
4237 LTTNG_ASSERT(output->consumer);
4238 list[idx].id = output->id;
4239 list[idx].max_size = output->max_size;
4240 if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) {
4241 ret = -LTTNG_ERR_INVALID;
4242 goto error;
4243 }
4244
4245 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4246 if (lttng_strncpy(list[idx].ctrl_url,
4247 output->consumer->dst.session_root_path,
4248 sizeof(list[idx].ctrl_url))) {
4249 ret = -LTTNG_ERR_INVALID;
4250 goto error;
4251 }
4252 } else {
4253 /* Control URI. */
4254 ret = uri_to_str_url(&output->consumer->dst.net.control,
4255 list[idx].ctrl_url,
4256 sizeof(list[idx].ctrl_url));
4257 if (ret < 0) {
4258 ret = -LTTNG_ERR_NOMEM;
4259 goto error;
4260 }
4261
4262 /* Data URI. */
4263 ret = uri_to_str_url(&output->consumer->dst.net.data,
4264 list[idx].data_url,
4265 sizeof(list[idx].data_url));
4266 if (ret < 0) {
4267 ret = -LTTNG_ERR_NOMEM;
4268 goto error;
4269 }
4270 }
4271
4272 idx++;
4273 }
4274 }
4275
4276 *outputs = list;
4277 list = nullptr;
4278 ret = session->snapshot.nb_output;
4279 error:
4280 free(list);
4281 end:
4282 return ret;
4283 }
4284
4285 /*
4286 * Check if we can regenerate the metadata for this session.
4287 * Only kernel, UST per-uid and non-live sessions are supported.
4288 *
4289 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4290 */
4291 static int check_regenerate_metadata_support(struct ltt_session *session)
4292 {
4293 int ret;
4294
4295 LTTNG_ASSERT(session);
4296
4297 if (session->live_timer != 0) {
4298 ret = LTTNG_ERR_LIVE_SESSION;
4299 goto end;
4300 }
4301 if (!session->active) {
4302 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4303 goto end;
4304 }
4305 if (session->ust_session) {
4306 switch (session->ust_session->buffer_type) {
4307 case LTTNG_BUFFER_PER_UID:
4308 break;
4309 case LTTNG_BUFFER_PER_PID:
4310 ret = LTTNG_ERR_PER_PID_SESSION;
4311 goto end;
4312 default:
4313 abort();
4314 ret = LTTNG_ERR_UNK;
4315 goto end;
4316 }
4317 }
4318 if (session->consumer->type == CONSUMER_DST_NET &&
4319 session->consumer->relay_minor_version < 8) {
4320 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4321 goto end;
4322 }
4323 ret = 0;
4324
4325 end:
4326 return ret;
4327 }
4328
4329 /*
4330 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4331 *
4332 * Ask the consumer to truncate the existing metadata file(s) and
4333 * then regenerate the metadata. Live and per-pid sessions are not
4334 * supported and return an error.
4335 *
4336 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4337 */
4338 int cmd_regenerate_metadata(struct ltt_session *session)
4339 {
4340 int ret;
4341
4342 LTTNG_ASSERT(session);
4343
4344 ret = check_regenerate_metadata_support(session);
4345 if (ret) {
4346 goto end;
4347 }
4348
4349 if (session->kernel_session) {
4350 ret = kernctl_session_regenerate_metadata(session->kernel_session->fd);
4351 if (ret < 0) {
4352 ERR("Failed to regenerate the kernel metadata");
4353 goto end;
4354 }
4355 }
4356
4357 if (session->ust_session) {
4358 ret = trace_ust_regenerate_metadata(session->ust_session);
4359 if (ret < 0) {
4360 ERR("Failed to regenerate the UST metadata");
4361 goto end;
4362 }
4363 }
4364 DBG("Cmd metadata regenerate for session %s", session->name);
4365 ret = LTTNG_OK;
4366
4367 end:
4368 return ret;
4369 }
4370
4371 /*
4372 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4373 *
4374 * Ask the tracer to regenerate a new statedump.
4375 *
4376 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4377 */
4378 int cmd_regenerate_statedump(struct ltt_session *session)
4379 {
4380 int ret;
4381
4382 LTTNG_ASSERT(session);
4383
4384 if (!session->active) {
4385 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4386 goto end;
4387 }
4388
4389 if (session->kernel_session) {
4390 ret = kernctl_session_regenerate_statedump(session->kernel_session->fd);
4391 /*
4392 * Currently, the statedump in kernel can only fail if out
4393 * of memory.
4394 */
4395 if (ret < 0) {
4396 if (ret == -ENOMEM) {
4397 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4398 } else {
4399 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4400 }
4401 ERR("Failed to regenerate the kernel statedump");
4402 goto end;
4403 }
4404 }
4405
4406 if (session->ust_session) {
4407 ret = ust_app_regenerate_statedump_all(session->ust_session);
4408 /*
4409 * Currently, the statedump in UST always returns 0.
4410 */
4411 if (ret < 0) {
4412 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4413 ERR("Failed to regenerate the UST statedump");
4414 goto end;
4415 }
4416 }
4417 DBG("Cmd regenerate statedump for session %s", session->name);
4418 ret = LTTNG_OK;
4419
4420 end:
4421 return ret;
4422 }
4423
4424 static enum lttng_error_code
4425 synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread,
4426 struct lttng_trigger *trigger,
4427 const struct lttng_credentials *cmd_creds)
4428 {
4429 enum lttng_error_code ret_code;
4430 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
4431 const char *trigger_name;
4432 uid_t trigger_owner;
4433 enum lttng_trigger_status trigger_status;
4434 const enum lttng_domain_type trigger_domain =
4435 lttng_trigger_get_underlying_domain_type_restriction(trigger);
4436
4437 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4438 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4439
4440 LTTNG_ASSERT(condition);
4441 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4442 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4443
4444 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4445 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4446
4447 session_lock_list();
4448 switch (trigger_domain) {
4449 case LTTNG_DOMAIN_KERNEL:
4450 {
4451 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4452 if (ret_code != LTTNG_OK) {
4453 enum lttng_error_code notif_thread_unregister_ret;
4454
4455 notif_thread_unregister_ret =
4456 notification_thread_command_unregister_trigger(notification_thread,
4457 trigger);
4458
4459 if (notif_thread_unregister_ret != LTTNG_OK) {
4460 /* Return the original error code. */
4461 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4462 trigger_name,
4463 (int) trigger_owner,
4464 ret_code);
4465 }
4466 }
4467 break;
4468 }
4469 case LTTNG_DOMAIN_UST:
4470 ust_app_global_update_all_event_notifier_rules();
4471 break;
4472 case LTTNG_DOMAIN_JUL:
4473 case LTTNG_DOMAIN_LOG4J:
4474 case LTTNG_DOMAIN_PYTHON:
4475 {
4476 /* Agent domains. */
4477 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
4478
4479 if (!agt) {
4480 agt = agent_create(trigger_domain);
4481 if (!agt) {
4482 ret_code = LTTNG_ERR_NOMEM;
4483 goto end_unlock_session_list;
4484 }
4485
4486 agent_add(agt, the_trigger_agents_ht_by_domain);
4487 }
4488
4489 ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
4490 if (ret_code != LTTNG_OK) {
4491 goto end_unlock_session_list;
4492 }
4493
4494 break;
4495 }
4496 case LTTNG_DOMAIN_NONE:
4497 default:
4498 abort();
4499 }
4500
4501 ret_code = LTTNG_OK;
4502 end_unlock_session_list:
4503 session_unlock_list();
4504 return ret_code;
4505 }
4506
4507 enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
4508 struct lttng_trigger *trigger,
4509 bool is_trigger_anonymous,
4510 struct notification_thread_handle *notification_thread,
4511 struct lttng_trigger **return_trigger)
4512 {
4513 enum lttng_error_code ret_code;
4514 const char *trigger_name;
4515 uid_t trigger_owner;
4516 enum lttng_trigger_status trigger_status;
4517
4518 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4519 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4520
4521 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4522 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4523
4524 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4525 trigger_name,
4526 (int) trigger_owner,
4527 (int) lttng_credentials_get_uid(cmd_creds));
4528
4529 /*
4530 * Validate the trigger credentials against the command credentials.
4531 * Only the root user can register a trigger with non-matching
4532 * credentials.
4533 */
4534 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
4535 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4536 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4537 trigger_name,
4538 (int) trigger_owner,
4539 (int) lttng_credentials_get_uid(cmd_creds));
4540 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4541 goto end;
4542 }
4543 }
4544
4545 /*
4546 * The bytecode generation also serves as a validation step for the
4547 * bytecode expressions.
4548 */
4549 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4550 if (ret_code != LTTNG_OK) {
4551 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4552 trigger_name,
4553 (int) trigger_owner,
4554 ret_code);
4555 goto end;
4556 }
4557
4558 /*
4559 * A reference to the trigger is acquired by the notification thread.
4560 * It is safe to return the same trigger to the caller since it the
4561 * other user holds a reference.
4562 *
4563 * The trigger is modified during the execution of the
4564 * "register trigger" command. However, by the time the command returns,
4565 * it is safe to use without any locking as its properties are
4566 * immutable.
4567 */
4568 ret_code = notification_thread_command_register_trigger(
4569 notification_thread, trigger, is_trigger_anonymous);
4570 if (ret_code != LTTNG_OK) {
4571 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4572 trigger_name,
4573 (int) trigger_owner,
4574 ret_code);
4575 goto end;
4576 }
4577
4578 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4579 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4580
4581 /*
4582 * Synchronize tracers if the trigger adds an event notifier.
4583 */
4584 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4585 ret_code = synchronize_tracer_notifier_register(
4586 notification_thread, trigger, cmd_creds);
4587 if (ret_code != LTTNG_OK) {
4588 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
4589 goto end;
4590 }
4591 }
4592
4593 /*
4594 * Return an updated trigger to the client.
4595 *
4596 * Since a modified version of the same trigger is returned, acquire a
4597 * reference to the trigger so the caller doesn't have to care if those
4598 * are distinct instances or not.
4599 */
4600 if (ret_code == LTTNG_OK) {
4601 lttng_trigger_get(trigger);
4602 *return_trigger = trigger;
4603 /* Ownership of trigger was transferred to caller. */
4604 trigger = nullptr;
4605 }
4606 end:
4607 return ret_code;
4608 }
4609
4610 static enum lttng_error_code
4611 synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
4612 {
4613 enum lttng_error_code ret_code;
4614 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
4615 const enum lttng_domain_type trigger_domain =
4616 lttng_trigger_get_underlying_domain_type_restriction(trigger);
4617
4618 LTTNG_ASSERT(condition);
4619 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4620 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4621
4622 session_lock_list();
4623 switch (trigger_domain) {
4624 case LTTNG_DOMAIN_KERNEL:
4625 ret_code = kernel_unregister_event_notifier(trigger);
4626 if (ret_code != LTTNG_OK) {
4627 goto end_unlock_session_list;
4628 }
4629
4630 break;
4631 case LTTNG_DOMAIN_UST:
4632 ust_app_global_update_all_event_notifier_rules();
4633 break;
4634 case LTTNG_DOMAIN_JUL:
4635 case LTTNG_DOMAIN_LOG4J:
4636 case LTTNG_DOMAIN_PYTHON:
4637 {
4638 /* Agent domains. */
4639 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
4640
4641 /*
4642 * This trigger was never registered in the first place. Calling
4643 * this function under those circumstances is an internal error.
4644 */
4645 LTTNG_ASSERT(agt);
4646 ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
4647 if (ret_code != LTTNG_OK) {
4648 goto end_unlock_session_list;
4649 }
4650
4651 break;
4652 }
4653 case LTTNG_DOMAIN_NONE:
4654 default:
4655 abort();
4656 }
4657
4658 ret_code = LTTNG_OK;
4659
4660 end_unlock_session_list:
4661 session_unlock_list();
4662 return ret_code;
4663 }
4664
4665 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
4666 const struct lttng_trigger *trigger,
4667 struct notification_thread_handle *notification_thread)
4668 {
4669 enum lttng_error_code ret_code;
4670 const char *trigger_name;
4671 uid_t trigger_owner;
4672 enum lttng_trigger_status trigger_status;
4673 struct lttng_trigger *sessiond_trigger = nullptr;
4674
4675 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4676 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4677 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4678 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4679
4680 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4681 trigger_name,
4682 (int) trigger_owner,
4683 (int) lttng_credentials_get_uid(cmd_creds));
4684
4685 /*
4686 * Validate the trigger credentials against the command credentials.
4687 * Only the root user can unregister a trigger with non-matching
4688 * credentials.
4689 */
4690 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
4691 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4692 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4693 trigger_name,
4694 (int) trigger_owner,
4695 (int) lttng_credentials_get_uid(cmd_creds));
4696 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4697 goto end;
4698 }
4699 }
4700
4701 /* Fetch the sessiond side trigger object. */
4702 ret_code = notification_thread_command_get_trigger(
4703 notification_thread, trigger, &sessiond_trigger);
4704 if (ret_code != LTTNG_OK) {
4705 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4706 trigger_name,
4707 (int) trigger_owner,
4708 ret_code);
4709 goto end;
4710 }
4711
4712 LTTNG_ASSERT(sessiond_trigger);
4713
4714 /*
4715 * From this point on, no matter what, consider the trigger
4716 * unregistered.
4717 *
4718 * We set the unregistered state of the sessiond side trigger object in
4719 * the client thread since we want to minimize the possibility of the
4720 * notification thread being stalled due to a long execution of an
4721 * action that required the trigger lock.
4722 */
4723 lttng_trigger_set_as_unregistered(sessiond_trigger);
4724
4725 ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger);
4726 if (ret_code != LTTNG_OK) {
4727 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4728 trigger_name,
4729 (int) trigger_owner,
4730 ret_code);
4731 goto end;
4732 }
4733
4734 /*
4735 * Synchronize tracers if the trigger removes an event notifier.
4736 * Do this even if the trigger unregistration failed to at least stop
4737 * the tracers from producing notifications associated with this
4738 * event notifier.
4739 */
4740 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4741 ret_code = synchronize_tracer_notifier_unregister(trigger);
4742 if (ret_code != LTTNG_OK) {
4743 ERR("Error unregistering trigger to tracer.");
4744 goto end;
4745 }
4746 }
4747
4748 end:
4749 lttng_trigger_put(sessiond_trigger);
4750 return ret_code;
4751 }
4752
4753 enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
4754 struct notification_thread_handle *notification_thread,
4755 struct lttng_triggers **return_triggers)
4756 {
4757 int ret;
4758 enum lttng_error_code ret_code;
4759 struct lttng_triggers *triggers = nullptr;
4760
4761 /* Get the set of triggers from the notification thread. */
4762 ret_code = notification_thread_command_list_triggers(
4763 notification_thread, cmd_ctx->creds.uid, &triggers);
4764 if (ret_code != LTTNG_OK) {
4765 goto end;
4766 }
4767
4768 ret = lttng_triggers_remove_hidden_triggers(triggers);
4769 if (ret) {
4770 ret_code = LTTNG_ERR_UNK;
4771 goto end;
4772 }
4773
4774 *return_triggers = triggers;
4775 triggers = nullptr;
4776 ret_code = LTTNG_OK;
4777 end:
4778 lttng_triggers_destroy(triggers);
4779 return ret_code;
4780 }
4781
4782 enum lttng_error_code
4783 cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4784 const struct lttng_error_query *query,
4785 struct lttng_error_query_results **_results,
4786 struct notification_thread_handle *notification_thread)
4787 {
4788 enum lttng_error_code ret_code;
4789 const struct lttng_trigger *query_target_trigger;
4790 const struct lttng_action *query_target_action = nullptr;
4791 struct lttng_trigger *matching_trigger = nullptr;
4792 const char *trigger_name;
4793 uid_t trigger_owner;
4794 enum lttng_trigger_status trigger_status;
4795 struct lttng_error_query_results *results = nullptr;
4796
4797 switch (lttng_error_query_get_target_type(query)) {
4798 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4799 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4800 break;
4801 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4802 query_target_trigger = lttng_error_query_condition_borrow_target(query);
4803 break;
4804 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4805 query_target_trigger = lttng_error_query_action_borrow_trigger_target(query);
4806 break;
4807 default:
4808 abort();
4809 }
4810
4811 LTTNG_ASSERT(query_target_trigger);
4812
4813 ret_code = notification_thread_command_get_trigger(
4814 notification_thread, query_target_trigger, &matching_trigger);
4815 if (ret_code != LTTNG_OK) {
4816 goto end;
4817 }
4818
4819 /* No longer needed. */
4820 query_target_trigger = nullptr;
4821
4822 if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4823 /* Get the sessiond-side version of the target action. */
4824 query_target_action =
4825 lttng_error_query_action_borrow_action_target(query, matching_trigger);
4826 }
4827
4828 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4829 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4830 trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner);
4831 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4832
4833 results = lttng_error_query_results_create();
4834 if (!results) {
4835 ret_code = LTTNG_ERR_NOMEM;
4836 goto end;
4837 }
4838
4839 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4840 trigger_name,
4841 (int) trigger_owner,
4842 (int) lttng_credentials_get_uid(cmd_creds));
4843
4844 /*
4845 * Validate the trigger credentials against the command credentials.
4846 * Only the root user can target a trigger with non-matching
4847 * credentials.
4848 */
4849 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger),
4850 cmd_creds)) {
4851 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4852 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4853 trigger_name,
4854 (int) trigger_owner,
4855 (int) lttng_credentials_get_uid(cmd_creds));
4856 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4857 goto end;
4858 }
4859 }
4860
4861 switch (lttng_error_query_get_target_type(query)) {
4862 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4863 trigger_status = lttng_trigger_add_error_results(matching_trigger, results);
4864
4865 switch (trigger_status) {
4866 case LTTNG_TRIGGER_STATUS_OK:
4867 break;
4868 default:
4869 ret_code = LTTNG_ERR_UNK;
4870 goto end;
4871 }
4872
4873 break;
4874 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4875 {
4876 trigger_status =
4877 lttng_trigger_condition_add_error_results(matching_trigger, results);
4878
4879 switch (trigger_status) {
4880 case LTTNG_TRIGGER_STATUS_OK:
4881 break;
4882 default:
4883 ret_code = LTTNG_ERR_UNK;
4884 goto end;
4885 }
4886
4887 break;
4888 }
4889 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4890 {
4891 const enum lttng_action_status action_status =
4892 lttng_action_add_error_query_results(query_target_action, results);
4893
4894 switch (action_status) {
4895 case LTTNG_ACTION_STATUS_OK:
4896 break;
4897 default:
4898 ret_code = LTTNG_ERR_UNK;
4899 goto end;
4900 }
4901
4902 break;
4903 }
4904 default:
4905 abort();
4906 break;
4907 }
4908
4909 *_results = results;
4910 results = nullptr;
4911 ret_code = LTTNG_OK;
4912 end:
4913 lttng_trigger_put(matching_trigger);
4914 lttng_error_query_results_destroy(results);
4915 return ret_code;
4916 }
4917
4918 /*
4919 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4920 * snapshot output is *not* set with a remote destination.
4921 *
4922 * Return LTTNG_OK on success or a LTTNG_ERR code.
4923 */
4924 static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output,
4925 const struct ltt_session *session)
4926 {
4927 enum lttng_error_code status = LTTNG_OK;
4928 struct lttng_ht_iter iter;
4929 struct consumer_socket *socket;
4930 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
4931 const char *base_path;
4932
4933 LTTNG_ASSERT(output);
4934 LTTNG_ASSERT(session);
4935
4936 DBG2("Set relayd object from snapshot output");
4937
4938 if (session->current_trace_chunk) {
4939 enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
4940 session->current_trace_chunk, &current_chunk_id.value);
4941
4942 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
4943 current_chunk_id.is_set = true;
4944 } else {
4945 ERR("Failed to get current trace chunk id");
4946 status = LTTNG_ERR_UNK;
4947 goto error;
4948 }
4949 }
4950
4951 /* Ignore if snapshot consumer output is not network. */
4952 if (output->type != CONSUMER_DST_NET) {
4953 goto error;
4954 }
4955
4956 /*
4957 * The snapshot record URI base path overrides the session
4958 * base path.
4959 */
4960 if (output->dst.net.control.subdir[0] != '\0') {
4961 base_path = output->dst.net.control.subdir;
4962 } else {
4963 base_path = session->base_path;
4964 }
4965
4966 /*
4967 * For each consumer socket, create and send the relayd object of the
4968 * snapshot output.
4969 */
4970 {
4971 lttng::urcu::read_lock_guard read_lock;
4972
4973 cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
4974 pthread_mutex_lock(socket->lock);
4975 status = send_consumer_relayd_sockets(
4976 session->id,
4977 output,
4978 socket,
4979 session->name,
4980 session->hostname,
4981 base_path,
4982 session->live_timer,
4983 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
4984 session->creation_time,
4985 session->name_contains_creation_time);
4986 pthread_mutex_unlock(socket->lock);
4987 if (status != LTTNG_OK) {
4988 goto error;
4989 }
4990 }
4991 }
4992
4993 error:
4994 return status;
4995 }
4996
4997 /*
4998 * Record a kernel snapshot.
4999 *
5000 * Return LTTNG_OK on success or a LTTNG_ERR code.
5001 */
5002 static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
5003 const struct consumer_output *output,
5004 const struct ltt_session *session,
5005 uint64_t nb_packets_per_stream)
5006 {
5007 enum lttng_error_code status;
5008
5009 LTTNG_ASSERT(ksess);
5010 LTTNG_ASSERT(output);
5011 LTTNG_ASSERT(session);
5012
5013 status = kernel_snapshot_record(ksess, output, nb_packets_per_stream);
5014 return status;
5015 }
5016
5017 /*
5018 * Record a UST snapshot.
5019 *
5020 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5021 */
5022 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
5023 const struct consumer_output *output,
5024 const struct ltt_session *session,
5025 uint64_t nb_packets_per_stream)
5026 {
5027 enum lttng_error_code status;
5028
5029 LTTNG_ASSERT(usess);
5030 LTTNG_ASSERT(output);
5031 LTTNG_ASSERT(session);
5032
5033 status = ust_app_snapshot_record(usess, output, nb_packets_per_stream);
5034 return status;
5035 }
5036
5037 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session,
5038 uint64_t cur_nr_packets)
5039 {
5040 uint64_t tot_size = 0;
5041
5042 if (session->kernel_session) {
5043 struct ltt_kernel_channel *chan;
5044 const struct ltt_kernel_session *ksess = session->kernel_session;
5045
5046 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
5047 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5048 /*
5049 * Don't take channel into account if we
5050 * already grab all its packets.
5051 */
5052 continue;
5053 }
5054 tot_size += chan->channel->attr.subbuf_size * chan->stream_count;
5055 }
5056 }
5057
5058 if (session->ust_session) {
5059 const struct ltt_ust_session *usess = session->ust_session;
5060
5061 tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets);
5062 }
5063
5064 return tot_size;
5065 }
5066
5067 /*
5068 * Calculate the number of packets we can grab from each stream that
5069 * fits within the overall snapshot max size.
5070 *
5071 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5072 * the number of packets per stream.
5073 *
5074 * TODO: this approach is not perfect: we consider the worse case
5075 * (packet filling the sub-buffers) as an upper bound, but we could do
5076 * better if we do this calculation while we actually grab the packet
5077 * content: we would know how much padding we don't actually store into
5078 * the file.
5079 *
5080 * This algorithm is currently bounded by the number of packets per
5081 * stream.
5082 *
5083 * Since we call this algorithm before actually grabbing the data, it's
5084 * an approximation: for instance, applications could appear/disappear
5085 * in between this call and actually grabbing data.
5086 */
5087 static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5088 uint64_t max_size)
5089 {
5090 int64_t size_left;
5091 uint64_t cur_nb_packets = 0;
5092
5093 if (!max_size) {
5094 return 0; /* Infinite */
5095 }
5096
5097 size_left = max_size;
5098 for (;;) {
5099 uint64_t one_more_packet_tot_size;
5100
5101 one_more_packet_tot_size =
5102 get_session_size_one_more_packet_per_stream(session, cur_nb_packets);
5103 if (!one_more_packet_tot_size) {
5104 /* We are already grabbing all packets. */
5105 break;
5106 }
5107 size_left -= one_more_packet_tot_size;
5108 if (size_left < 0) {
5109 break;
5110 }
5111 cur_nb_packets++;
5112 }
5113 if (!cur_nb_packets && size_left != max_size) {
5114 /* Not enough room to grab one packet of each stream, error. */
5115 return -1;
5116 }
5117 return cur_nb_packets;
5118 }
5119
5120 static enum lttng_error_code snapshot_record(struct ltt_session *session,
5121 const struct snapshot_output *snapshot_output)
5122 {
5123 int64_t nb_packets_per_stream;
5124 char snapshot_chunk_name[LTTNG_NAME_MAX];
5125 int ret;
5126 enum lttng_error_code ret_code = LTTNG_OK;
5127 struct lttng_trace_chunk *snapshot_trace_chunk;
5128 struct consumer_output *original_ust_consumer_output = nullptr;
5129 struct consumer_output *original_kernel_consumer_output = nullptr;
5130 struct consumer_output *snapshot_ust_consumer_output = nullptr;
5131 struct consumer_output *snapshot_kernel_consumer_output = nullptr;
5132
5133 ret = snprintf(snapshot_chunk_name,
5134 sizeof(snapshot_chunk_name),
5135 "%s-%s-%" PRIu64,
5136 snapshot_output->name,
5137 snapshot_output->datetime,
5138 snapshot_output->nb_snapshot);
5139 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
5140 ERR("Failed to format snapshot name");
5141 ret_code = LTTNG_ERR_INVALID;
5142 goto error;
5143 }
5144 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5145 snapshot_output->name,
5146 session->name,
5147 snapshot_chunk_name);
5148 if (!session->kernel_session && !session->ust_session) {
5149 ERR("Failed to record snapshot as no channels exist");
5150 ret_code = LTTNG_ERR_NO_CHANNEL;
5151 goto error;
5152 }
5153
5154 if (session->kernel_session) {
5155 original_kernel_consumer_output = session->kernel_session->consumer;
5156 snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer);
5157 strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name);
5158
5159 /* Copy the original domain subdir. */
5160 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5161 original_kernel_consumer_output->domain_subdir);
5162
5163 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5164 original_kernel_consumer_output);
5165 if (ret < 0) {
5166 ERR("Failed to copy consumer sockets from snapshot output configuration");
5167 ret_code = LTTNG_ERR_NOMEM;
5168 goto error;
5169 }
5170 ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session);
5171 if (ret_code != LTTNG_OK) {
5172 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5173 goto error;
5174 }
5175 session->kernel_session->consumer = snapshot_kernel_consumer_output;
5176 }
5177 if (session->ust_session) {
5178 original_ust_consumer_output = session->ust_session->consumer;
5179 snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer);
5180 strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name);
5181
5182 /* Copy the original domain subdir. */
5183 strcpy(snapshot_ust_consumer_output->domain_subdir,
5184 original_ust_consumer_output->domain_subdir);
5185
5186 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5187 original_ust_consumer_output);
5188 if (ret < 0) {
5189 ERR("Failed to copy consumer sockets from snapshot output configuration");
5190 ret_code = LTTNG_ERR_NOMEM;
5191 goto error;
5192 }
5193 ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session);
5194 if (ret_code != LTTNG_OK) {
5195 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5196 goto error;
5197 }
5198 session->ust_session->consumer = snapshot_ust_consumer_output;
5199 }
5200
5201 snapshot_trace_chunk = session_create_new_trace_chunk(
5202 session,
5203 snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output,
5204 consumer_output_get_base_path(snapshot_output->consumer),
5205 snapshot_chunk_name);
5206 if (!snapshot_trace_chunk) {
5207 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5208 session->name);
5209 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5210 goto error;
5211 }
5212 LTTNG_ASSERT(!session->current_trace_chunk);
5213 ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr);
5214 lttng_trace_chunk_put(snapshot_trace_chunk);
5215 snapshot_trace_chunk = nullptr;
5216 if (ret) {
5217 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5218 session->name);
5219 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5220 goto error;
5221 }
5222
5223 nb_packets_per_stream =
5224 get_session_nb_packets_per_stream(session, snapshot_output->max_size);
5225 if (nb_packets_per_stream < 0) {
5226 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5227 goto error_close_trace_chunk;
5228 }
5229
5230 if (session->kernel_session) {
5231 ret_code = record_kernel_snapshot(session->kernel_session,
5232 snapshot_kernel_consumer_output,
5233 session,
5234 nb_packets_per_stream);
5235 if (ret_code != LTTNG_OK) {
5236 goto error_close_trace_chunk;
5237 }
5238 }
5239
5240 if (session->ust_session) {
5241 ret_code = record_ust_snapshot(session->ust_session,
5242 snapshot_ust_consumer_output,
5243 session,
5244 nb_packets_per_stream);
5245 if (ret_code != LTTNG_OK) {
5246 goto error_close_trace_chunk;
5247 }
5248 }
5249
5250 error_close_trace_chunk:
5251 if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) {
5252 ERR("Failed to release the current trace chunk of session \"%s\"", session->name);
5253 ret_code = LTTNG_ERR_UNK;
5254 }
5255
5256 if (session_close_trace_chunk(session,
5257 snapshot_trace_chunk,
5258 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION,
5259 nullptr)) {
5260 /*
5261 * Don't goto end; make sure the chunk is closed for the session
5262 * to allow future snapshots.
5263 */
5264 ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name);
5265 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5266 }
5267
5268 lttng_trace_chunk_put(snapshot_trace_chunk);
5269 snapshot_trace_chunk = nullptr;
5270 error:
5271 if (original_ust_consumer_output) {
5272 session->ust_session->consumer = original_ust_consumer_output;
5273 }
5274 if (original_kernel_consumer_output) {
5275 session->kernel_session->consumer = original_kernel_consumer_output;
5276 }
5277 consumer_output_put(snapshot_ust_consumer_output);
5278 consumer_output_put(snapshot_kernel_consumer_output);
5279 return ret_code;
5280 }
5281
5282 /*
5283 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5284 *
5285 * The wait parameter is ignored so this call always wait for the snapshot to
5286 * complete before returning.
5287 *
5288 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5289 */
5290 int cmd_snapshot_record(struct ltt_session *session,
5291 const struct lttng_snapshot_output *output,
5292 int wait __attribute__((unused)))
5293 {
5294 enum lttng_error_code cmd_ret = LTTNG_OK;
5295 int ret;
5296 unsigned int snapshot_success = 0;
5297 char datetime[16];
5298 struct snapshot_output *tmp_output = nullptr;
5299
5300 LTTNG_ASSERT(session);
5301 LTTNG_ASSERT(output);
5302
5303 DBG("Cmd snapshot record for session %s", session->name);
5304
5305 /* Get the datetime for the snapshot output directory. */
5306 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime));
5307 if (!ret) {
5308 cmd_ret = LTTNG_ERR_INVALID;
5309 goto error;
5310 }
5311
5312 /*
5313 * Permission denied to create an output if the session is not
5314 * set in no output mode.
5315 */
5316 if (session->output_traces) {
5317 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
5318 goto error;
5319 }
5320
5321 /* The session needs to be started at least once. */
5322 if (!session->has_been_started) {
5323 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5324 goto error;
5325 }
5326
5327 /* Use temporary output for the session. */
5328 if (*output->ctrl_url != '\0') {
5329 tmp_output = snapshot_output_alloc();
5330 if (!tmp_output) {
5331 cmd_ret = LTTNG_ERR_NOMEM;
5332 goto error;
5333 }
5334
5335 ret = snapshot_output_init(session,
5336 output->max_size,
5337 output->name,
5338 output->ctrl_url,
5339 output->data_url,
5340 session->consumer,
5341 tmp_output,
5342 nullptr);
5343 if (ret < 0) {
5344 if (ret == -ENOMEM) {
5345 cmd_ret = LTTNG_ERR_NOMEM;
5346 } else {
5347 cmd_ret = LTTNG_ERR_INVALID;
5348 }
5349 goto error;
5350 }
5351 /* Use the global session count for the temporary snapshot. */
5352 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
5353
5354 /* Use the global datetime */
5355 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5356 cmd_ret = snapshot_record(session, tmp_output);
5357 if (cmd_ret != LTTNG_OK) {
5358 goto error;
5359 }
5360 snapshot_success = 1;
5361 } else {
5362 struct snapshot_output *sout;
5363 struct lttng_ht_iter iter;
5364
5365 lttng::urcu::read_lock_guard read_lock;
5366
5367 cds_lfht_for_each_entry (
5368 session->snapshot.output_ht->ht, &iter.iter, sout, node.node) {
5369 struct snapshot_output output_copy;
5370
5371 /*
5372 * Make a local copy of the output and override output
5373 * parameters with those provided as part of the
5374 * command.
5375 */
5376 memcpy(&output_copy, sout, sizeof(output_copy));
5377
5378 if (output->max_size != (uint64_t) -1ULL) {
5379 output_copy.max_size = output->max_size;
5380 }
5381
5382 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5383 memcpy(output_copy.datetime, datetime, sizeof(datetime));
5384
5385 /* Use temporary name. */
5386 if (*output->name != '\0') {
5387 if (lttng_strncpy(output_copy.name,
5388 output->name,
5389 sizeof(output_copy.name))) {
5390 cmd_ret = LTTNG_ERR_INVALID;
5391 goto error;
5392 }
5393 }
5394
5395 cmd_ret = snapshot_record(session, &output_copy);
5396 if (cmd_ret != LTTNG_OK) {
5397 goto error;
5398 }
5399
5400 snapshot_success = 1;
5401 }
5402 }
5403
5404 if (snapshot_success) {
5405 session->snapshot.nb_snapshot++;
5406 } else {
5407 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
5408 }
5409
5410 error:
5411 if (tmp_output) {
5412 snapshot_output_destroy(tmp_output);
5413 }
5414
5415 return cmd_ret;
5416 }
5417
5418 /*
5419 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5420 */
5421 int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
5422 {
5423 /* Safety net */
5424 LTTNG_ASSERT(session);
5425
5426 /*
5427 * Can only set shm path before session is started.
5428 */
5429 if (session->has_been_started) {
5430 return LTTNG_ERR_SESSION_STARTED;
5431 }
5432
5433 strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
5434 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5435
5436 return LTTNG_OK;
5437 }
5438
5439 /*
5440 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5441 *
5442 * Ask the consumer to rotate the session output directory.
5443 * The session lock must be held.
5444 *
5445 * Returns LTTNG_OK on success or else a negative LTTng error code.
5446 */
5447 int cmd_rotate_session(struct ltt_session *session,
5448 struct lttng_rotate_session_return *rotate_return,
5449 bool quiet_rotation,
5450 enum lttng_trace_chunk_command_type command)
5451 {
5452 int ret;
5453 uint64_t ongoing_rotation_chunk_id;
5454 enum lttng_error_code cmd_ret = LTTNG_OK;
5455 struct lttng_trace_chunk *chunk_being_archived = nullptr;
5456 struct lttng_trace_chunk *new_trace_chunk = nullptr;
5457 enum lttng_trace_chunk_status chunk_status;
5458 bool failed_to_rotate = false;
5459 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5460
5461 LTTNG_ASSERT(session);
5462
5463 if (!session->has_been_started) {
5464 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5465 goto end;
5466 }
5467
5468 /*
5469 * Explicit rotation is not supported for live sessions.
5470 * However, live sessions can perform a quiet rotation on
5471 * destroy.
5472 * Rotation is not supported for snapshot traces (no output).
5473 */
5474 if ((!quiet_rotation && session->live_timer) || !session->output_traces) {
5475 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5476 goto end;
5477 }
5478
5479 /* Unsupported feature in lttng-relayd before 2.11. */
5480 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5481 (session->consumer->relay_major_version == 2 &&
5482 session->consumer->relay_minor_version < 11)) {
5483 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
5484 goto end;
5485 }
5486
5487 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5488 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5489 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5490 goto end;
5491 }
5492
5493 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5494 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5495 session->name);
5496 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
5497 goto end;
5498 }
5499
5500 /*
5501 * After a stop, we only allow one rotation to occur, the other ones are
5502 * useless until a new start.
5503 */
5504 if (session->rotated_after_last_stop) {
5505 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5506 session->name);
5507 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
5508 goto end;
5509 }
5510
5511 /*
5512 * After a stop followed by a clear, disallow following rotations a they would
5513 * generate empty chunks.
5514 */
5515 if (session->cleared_after_last_stop) {
5516 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5517 session->name);
5518 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5519 goto end;
5520 }
5521
5522 if (session->active) {
5523 new_trace_chunk =
5524 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
5525 if (!new_trace_chunk) {
5526 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5527 goto error;
5528 }
5529 }
5530
5531 /*
5532 * The current trace chunk becomes the chunk being archived.
5533 *
5534 * After this point, "chunk_being_archived" must absolutely
5535 * be closed on the consumer(s), otherwise it will never be
5536 * cleaned-up, which will result in a leak.
5537 */
5538 ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived);
5539 if (ret) {
5540 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5541 goto error;
5542 }
5543
5544 if (session->kernel_session) {
5545 cmd_ret = kernel_rotate_session(session);
5546 if (cmd_ret != LTTNG_OK) {
5547 failed_to_rotate = true;
5548 rotation_fail_code = cmd_ret;
5549 }
5550 }
5551 if (session->ust_session) {
5552 cmd_ret = ust_app_rotate_session(session);
5553 if (cmd_ret != LTTNG_OK) {
5554 failed_to_rotate = true;
5555 rotation_fail_code = cmd_ret;
5556 }
5557 }
5558
5559 if (!session->active) {
5560 session->rotated_after_last_stop = true;
5561 }
5562
5563 if (!chunk_being_archived) {
5564 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5565 session->name);
5566 if (failed_to_rotate) {
5567 cmd_ret = rotation_fail_code;
5568 goto error;
5569 }
5570 cmd_ret = LTTNG_OK;
5571 goto end;
5572 }
5573
5574 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5575 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id);
5576 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5577
5578 ret = session_close_trace_chunk(
5579 session, chunk_being_archived, command, session->last_chunk_path);
5580 if (ret) {
5581 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5582 goto error;
5583 }
5584
5585 if (failed_to_rotate) {
5586 cmd_ret = rotation_fail_code;
5587 goto error;
5588 }
5589
5590 session->quiet_rotation = quiet_rotation;
5591 ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER);
5592 if (ret) {
5593 cmd_ret = LTTNG_ERR_UNK;
5594 goto error;
5595 }
5596
5597 if (rotate_return) {
5598 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5599 }
5600
5601 session->chunk_being_archived = chunk_being_archived;
5602 chunk_being_archived = nullptr;
5603 if (!quiet_rotation) {
5604 ret = notification_thread_command_session_rotation_ongoing(
5605 the_notification_thread_handle, session->id, ongoing_rotation_chunk_id);
5606 if (ret != LTTNG_OK) {
5607 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5608 session->name);
5609 cmd_ret = (lttng_error_code) ret;
5610 }
5611 }
5612
5613 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5614 session->name,
5615 ongoing_rotation_chunk_id);
5616 end:
5617 lttng_trace_chunk_put(new_trace_chunk);
5618 lttng_trace_chunk_put(chunk_being_archived);
5619 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5620 return ret;
5621 error:
5622 if (session_reset_rotation_state(session, LTTNG_ROTATION_STATE_ERROR)) {
5623 ERR("Failed to reset rotation state of session \"%s\"", session->name);
5624 }
5625 goto end;
5626 }
5627
5628 /*
5629 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5630 *
5631 * Check if the session has finished its rotation.
5632 *
5633 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5634 */
5635 int cmd_rotate_get_info(struct ltt_session *session,
5636 struct lttng_rotation_get_info_return *info_return,
5637 uint64_t rotation_id)
5638 {
5639 enum lttng_error_code cmd_ret = LTTNG_OK;
5640 enum lttng_rotation_state rotation_state;
5641
5642 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64,
5643 session->name,
5644 session->most_recent_chunk_id.value);
5645
5646 if (session->chunk_being_archived) {
5647 enum lttng_trace_chunk_status chunk_status;
5648 uint64_t chunk_id;
5649
5650 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
5651 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5652
5653 rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING :
5654 LTTNG_ROTATION_STATE_EXPIRED;
5655 } else {
5656 if (session->last_archived_chunk_id.is_set &&
5657 rotation_id != session->last_archived_chunk_id.value) {
5658 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5659 } else {
5660 rotation_state = session->rotation_state;
5661 }
5662 }
5663
5664 switch (rotation_state) {
5665 case LTTNG_ROTATION_STATE_NO_ROTATION:
5666 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5667 session->name);
5668 goto end;
5669 case LTTNG_ROTATION_STATE_EXPIRED:
5670 DBG("Reporting that the rotation state of rotation id %" PRIu64
5671 " of session \"%s\" has expired",
5672 rotation_id,
5673 session->name);
5674 break;
5675 case LTTNG_ROTATION_STATE_ONGOING:
5676 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5677 rotation_id,
5678 session->name);
5679 break;
5680 case LTTNG_ROTATION_STATE_COMPLETED:
5681 {
5682 int fmt_ret;
5683 char *chunk_path;
5684 char *current_tracing_path_reply;
5685 size_t current_tracing_path_reply_len;
5686
5687 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5688 rotation_id,
5689 session->name);
5690
5691 switch (session_get_consumer_destination_type(session)) {
5692 case CONSUMER_DST_LOCAL:
5693 current_tracing_path_reply = info_return->location.local.absolute_path;
5694 current_tracing_path_reply_len =
5695 sizeof(info_return->location.local.absolute_path);
5696 info_return->location_type =
5697 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5698 fmt_ret = asprintf(&chunk_path,
5699 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5700 session_get_base_path(session),
5701 session->last_archived_chunk_name);
5702 if (fmt_ret == -1) {
5703 PERROR("Failed to format the path of the last archived trace chunk");
5704 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5705 cmd_ret = LTTNG_ERR_UNK;
5706 goto end;
5707 }
5708 break;
5709 case CONSUMER_DST_NET:
5710 {
5711 uint16_t ctrl_port, data_port;
5712
5713 current_tracing_path_reply = info_return->location.relay.relative_path;
5714 current_tracing_path_reply_len =
5715 sizeof(info_return->location.relay.relative_path);
5716 /* Currently the only supported relay protocol. */
5717 info_return->location.relay.protocol =
5718 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5719
5720 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5721 session_get_net_consumer_hostname(session),
5722 sizeof(info_return->location.relay.host));
5723 if (fmt_ret) {
5724 ERR("Failed to copy host name to rotate_get_info reply");
5725 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5726 cmd_ret = LTTNG_ERR_SET_URL;
5727 goto end;
5728 }
5729
5730 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5731 info_return->location.relay.ports.control = ctrl_port;
5732 info_return->location.relay.ports.data = data_port;
5733 info_return->location_type =
5734 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5735 chunk_path = strdup(session->last_chunk_path);
5736 if (!chunk_path) {
5737 ERR("Failed to allocate the path of the last archived trace chunk");
5738 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5739 cmd_ret = LTTNG_ERR_UNK;
5740 goto end;
5741 }
5742 break;
5743 }
5744 default:
5745 abort();
5746 }
5747
5748 fmt_ret = lttng_strncpy(
5749 current_tracing_path_reply, chunk_path, current_tracing_path_reply_len);
5750 free(chunk_path);
5751 if (fmt_ret) {
5752 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5753 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5754 cmd_ret = LTTNG_ERR_UNK;
5755 goto end;
5756 }
5757
5758 break;
5759 }
5760 case LTTNG_ROTATION_STATE_ERROR:
5761 DBG("Reporting that an error occurred during rotation %" PRIu64
5762 " of session \"%s\"",
5763 rotation_id,
5764 session->name);
5765 break;
5766 default:
5767 abort();
5768 }
5769
5770 cmd_ret = LTTNG_OK;
5771 end:
5772 info_return->status = (int32_t) rotation_state;
5773 return cmd_ret;
5774 }
5775
5776 /*
5777 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5778 *
5779 * Configure the automatic rotation parameters.
5780 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5781 * 'activate' to false means deactivate the rotation schedule and validate that
5782 * 'new_value' has the same value as the currently active value.
5783 *
5784 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5785 */
5786 int cmd_rotation_set_schedule(struct ltt_session *session,
5787 bool activate,
5788 enum lttng_rotation_schedule_type schedule_type,
5789 uint64_t new_value,
5790 struct notification_thread_handle *notification_thread_handle)
5791 {
5792 int ret;
5793 uint64_t *parameter_value;
5794
5795 LTTNG_ASSERT(session);
5796
5797 DBG("Cmd rotate set schedule session %s", session->name);
5798
5799 if (session->live_timer || !session->output_traces) {
5800 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5801 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5802 goto end;
5803 }
5804
5805 switch (schedule_type) {
5806 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5807 parameter_value = &session->rotate_size;
5808 break;
5809 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5810 parameter_value = &session->rotate_timer_period;
5811 if (new_value >= UINT_MAX) {
5812 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5813 " > %u (UINT_MAX)",
5814 new_value,
5815 UINT_MAX);
5816 ret = LTTNG_ERR_INVALID;
5817 goto end;
5818 }
5819 break;
5820 default:
5821 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5822 ret = LTTNG_ERR_INVALID;
5823 goto end;
5824 }
5825
5826 /* Improper use of the API. */
5827 if (new_value == -1ULL) {
5828 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5829 ret = LTTNG_ERR_INVALID;
5830 goto end;
5831 }
5832
5833 /*
5834 * As indicated in struct ltt_session's comments, a value of == 0 means
5835 * this schedule rotation type is not in use.
5836 *
5837 * Reject the command if we were asked to activate a schedule that was
5838 * already active.
5839 */
5840 if (activate && *parameter_value != 0) {
5841 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5842 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5843 goto end;
5844 }
5845
5846 /*
5847 * Reject the command if we were asked to deactivate a schedule that was
5848 * not active.
5849 */
5850 if (!activate && *parameter_value == 0) {
5851 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5852 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5853 goto end;
5854 }
5855
5856 /*
5857 * Reject the command if we were asked to deactivate a schedule that
5858 * doesn't exist.
5859 */
5860 if (!activate && *parameter_value != new_value) {
5861 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5862 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5863 goto end;
5864 }
5865
5866 *parameter_value = activate ? new_value : 0;
5867
5868 switch (schedule_type) {
5869 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5870 if (activate && session->active) {
5871 /*
5872 * Only start the timer if the session is active,
5873 * otherwise it will be started when the session starts.
5874 */
5875 ret = timer_session_rotation_schedule_timer_start(session, new_value);
5876 if (ret) {
5877 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5878 ret = LTTNG_ERR_UNK;
5879 goto end;
5880 }
5881 } else {
5882 ret = timer_session_rotation_schedule_timer_stop(session);
5883 if (ret) {
5884 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5885 ret = LTTNG_ERR_UNK;
5886 goto end;
5887 }
5888 }
5889 break;
5890 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5891 if (activate) {
5892 ret = subscribe_session_consumed_size_rotation(
5893 session, new_value, notification_thread_handle);
5894 if (ret) {
5895 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5896 ret = LTTNG_ERR_UNK;
5897 goto end;
5898 }
5899 } else {
5900 ret = unsubscribe_session_consumed_size_rotation(
5901 session, notification_thread_handle);
5902 if (ret) {
5903 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5904 ret = LTTNG_ERR_UNK;
5905 goto end;
5906 }
5907 }
5908 break;
5909 default:
5910 /* Would have been caught before. */
5911 abort();
5912 }
5913
5914 ret = LTTNG_OK;
5915
5916 goto end;
5917
5918 end:
5919 return ret;
5920 }
5921
5922 /* Wait for a given path to be removed before continuing. */
5923 static enum lttng_error_code wait_on_path(void *path_data)
5924 {
5925 const char *shm_path = (const char *) path_data;
5926
5927 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5928 shm_path);
5929 while (true) {
5930 int ret;
5931 struct stat st;
5932
5933 ret = stat(shm_path, &st);
5934 if (ret) {
5935 if (errno != ENOENT) {
5936 PERROR("stat() returned an error while checking for the existence of the shm path");
5937 } else {
5938 DBG("shm path no longer exists, completing the destruction of session");
5939 }
5940 break;
5941 } else {
5942 if (!S_ISDIR(st.st_mode)) {
5943 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5944 shm_path);
5945 break;
5946 }
5947 }
5948 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5949 }
5950 return LTTNG_OK;
5951 }
5952
5953 /*
5954 * Returns a pointer to a handler to run on completion of a command.
5955 * Returns NULL if no handler has to be run for the last command executed.
5956 */
5957 const struct cmd_completion_handler *cmd_pop_completion_handler()
5958 {
5959 struct cmd_completion_handler *handler = current_completion_handler;
5960
5961 current_completion_handler = nullptr;
5962 return handler;
5963 }
5964
5965 /*
5966 * Init command subsystem.
5967 */
5968 void cmd_init()
5969 {
5970 /*
5971 * Set network sequence index to 1 for streams to match a relayd
5972 * socket on the consumer side.
5973 */
5974 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5975 relayd_net_seq_idx = 1;
5976 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
5977
5978 DBG("Command subsystem initialized");
5979 }
This page took 0.151922 seconds and 4 git commands to generate.