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