Clean-up: run format-cpp on the tree
[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, int *sock_fd)
3394 {
3395 int ret;
3396 enum lttng_error_code destruction_last_error = LTTNG_OK;
3397 struct cmd_destroy_session_reply_context *reply_context = nullptr;
3398
3399 if (sock_fd) {
3400 reply_context = zmalloc<cmd_destroy_session_reply_context>();
3401 if (!reply_context) {
3402 ret = LTTNG_ERR_NOMEM;
3403 goto end;
3404 }
3405
3406 reply_context->reply_sock_fd = *sock_fd;
3407 }
3408
3409 /* Safety net */
3410 LTTNG_ASSERT(session);
3411
3412 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, session->id);
3413 if (session->active) {
3414 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3415 session->name);
3416 ret = cmd_stop_trace(session);
3417 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3418 /* Carry on with the destruction of the session. */
3419 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3420 session->name,
3421 lttng_strerror(-ret));
3422 destruction_last_error = (lttng_error_code) ret;
3423 }
3424 }
3425
3426 if (session->rotation_schedule_timer_enabled) {
3427 if (timer_session_rotation_schedule_timer_stop(session)) {
3428 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3429 session->name);
3430 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
3431 }
3432 }
3433
3434 if (session->rotate_size) {
3435 try {
3436 the_rotation_thread_handle->unsubscribe_session_consumed_size_rotation(
3437 *session);
3438 } catch (std::exception& e) {
3439 /* Continue the destruction of the session anyway. */
3440 ERR("Failed to unsubscribe rotation thread notification channel from consumed size condition during session destruction: %s",
3441 e.what());
3442 }
3443
3444 session->rotate_size = 0;
3445 }
3446
3447 if (session->rotated && session->current_trace_chunk && session->output_traces) {
3448 /*
3449 * Perform a last rotation on destruction if rotations have
3450 * occurred during the session's lifetime.
3451 */
3452 ret = cmd_rotate_session(
3453 session, nullptr, false, LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
3454 if (ret != LTTNG_OK) {
3455 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3456 session->name,
3457 lttng_strerror(-ret));
3458 destruction_last_error = (lttng_error_code) -ret;
3459 }
3460 if (reply_context) {
3461 reply_context->implicit_rotation_on_destroy = true;
3462 }
3463 } else if (session->has_been_started && session->current_trace_chunk) {
3464 /*
3465 * The user has not triggered a session rotation. However, to
3466 * ensure all data has been consumed, the session is rotated
3467 * to a 'null' trace chunk before it is destroyed.
3468 *
3469 * This is a "quiet" rotation meaning that no notification is
3470 * emitted and no renaming of the current trace chunk takes
3471 * place.
3472 */
3473 ret = cmd_rotate_session(
3474 session, nullptr, true, LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
3475 /*
3476 * Rotation operations may not be supported by the kernel
3477 * tracer. Hence, do not consider this implicit rotation as
3478 * a session destruction error. The library has already stopped
3479 * the session and waited for pending data; there is nothing
3480 * left to do but complete the destruction of the session.
3481 */
3482 if (ret != LTTNG_OK && ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
3483 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3484 session->name,
3485 lttng_strerror(ret));
3486 destruction_last_error = (lttng_error_code) -ret;
3487 }
3488 }
3489
3490 if (session->shm_path[0]) {
3491 /*
3492 * When a session is created with an explicit shm_path,
3493 * the consumer daemon will create its shared memory files
3494 * at that location and will *not* unlink them. This is normal
3495 * as the intention of that feature is to make it possible
3496 * to retrieve the content of those files should a crash occur.
3497 *
3498 * To ensure the content of those files can be used, the
3499 * sessiond daemon will replicate the content of the metadata
3500 * cache in a metadata file.
3501 *
3502 * On clean-up, it is expected that the consumer daemon will
3503 * unlink the shared memory files and that the session daemon
3504 * will unlink the metadata file. Then, the session's directory
3505 * in the shm path can be removed.
3506 *
3507 * Unfortunately, a flaw in the design of the sessiond's and
3508 * consumerd's tear down of channels makes it impossible to
3509 * determine when the sessiond _and_ the consumerd have both
3510 * destroyed their representation of a channel. For one, the
3511 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3512 * callbacks in both daemons.
3513 *
3514 * However, it is also impossible for the sessiond to know when
3515 * the consumer daemon is done destroying its channel(s) since
3516 * it occurs as a reaction to the closing of the channel's file
3517 * descriptor. There is no resulting communication initiated
3518 * from the consumerd to the sessiond to confirm that the
3519 * operation is completed (and was successful).
3520 *
3521 * Until this is all fixed, the session daemon checks for the
3522 * removal of the session's shm path which makes it possible
3523 * to safely advertise a session as having been destroyed.
3524 *
3525 * Prior to this fix, it was not possible to reliably save
3526 * a session making use of the --shm-path option, destroy it,
3527 * and load it again. This is because the creation of the
3528 * session would fail upon seeing the session's shm path
3529 * already in existence.
3530 *
3531 * Note that none of the error paths in the check for the
3532 * directory's existence return an error. This is normal
3533 * as there isn't much that can be done. The session will
3534 * be destroyed properly, except that we can't offer the
3535 * guarantee that the same session can be re-created.
3536 */
3537 current_completion_handler = &destroy_completion_handler.handler;
3538 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3539 session->shm_path,
3540 sizeof(destroy_completion_handler.shm_path));
3541 LTTNG_ASSERT(!ret);
3542 }
3543
3544 /*
3545 * The session is destroyed. However, note that the command context
3546 * still holds a reference to the session, thus delaying its destruction
3547 * _at least_ up to the point when that reference is released.
3548 */
3549 session_destroy(session);
3550 if (reply_context) {
3551 reply_context->destruction_status = destruction_last_error;
3552 ret = session_add_destroy_notifier(
3553 session, cmd_destroy_session_reply, (void *) reply_context);
3554 if (ret) {
3555 ret = LTTNG_ERR_FATAL;
3556 goto end;
3557 } else {
3558 *sock_fd = -1;
3559 }
3560 }
3561 ret = LTTNG_OK;
3562 end:
3563 return ret;
3564 }
3565
3566 /*
3567 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3568 */
3569 int cmd_register_consumer(struct ltt_session *session,
3570 enum lttng_domain_type domain,
3571 const char *sock_path,
3572 struct consumer_data *cdata)
3573 {
3574 int ret, sock;
3575 struct consumer_socket *socket = nullptr;
3576
3577 LTTNG_ASSERT(session);
3578 LTTNG_ASSERT(cdata);
3579 LTTNG_ASSERT(sock_path);
3580
3581 switch (domain) {
3582 case LTTNG_DOMAIN_KERNEL:
3583 {
3584 struct ltt_kernel_session *ksess = session->kernel_session;
3585
3586 LTTNG_ASSERT(ksess);
3587
3588 /* Can't register a consumer if there is already one */
3589 if (ksess->consumer_fds_sent != 0) {
3590 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3591 goto error;
3592 }
3593
3594 sock = lttcomm_connect_unix_sock(sock_path);
3595 if (sock < 0) {
3596 ret = LTTNG_ERR_CONNECT_FAIL;
3597 goto error;
3598 }
3599 cdata->cmd_sock = sock;
3600
3601 socket = consumer_allocate_socket(&cdata->cmd_sock);
3602 if (socket == nullptr) {
3603 ret = close(sock);
3604 if (ret < 0) {
3605 PERROR("close register consumer");
3606 }
3607 cdata->cmd_sock = -1;
3608 ret = LTTNG_ERR_FATAL;
3609 goto error;
3610 }
3611
3612 socket->lock = zmalloc<pthread_mutex_t>();
3613 if (socket->lock == nullptr) {
3614 PERROR("zmalloc pthread mutex");
3615 ret = LTTNG_ERR_FATAL;
3616 goto error;
3617 }
3618
3619 pthread_mutex_init(socket->lock, nullptr);
3620 socket->registered = 1;
3621
3622 lttng::urcu::read_lock_guard read_lock;
3623 consumer_add_socket(socket, ksess->consumer);
3624
3625 pthread_mutex_lock(&cdata->pid_mutex);
3626 cdata->pid = -1;
3627 pthread_mutex_unlock(&cdata->pid_mutex);
3628
3629 break;
3630 }
3631 default:
3632 /* TODO: Userspace tracing */
3633 ret = LTTNG_ERR_UND;
3634 goto error;
3635 }
3636
3637 return LTTNG_OK;
3638
3639 error:
3640 if (socket) {
3641 consumer_destroy_socket(socket);
3642 }
3643 return ret;
3644 }
3645
3646 /*
3647 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3648 */
3649 ssize_t cmd_list_domains(struct ltt_session *session, struct lttng_domain **domains)
3650 {
3651 int ret, index = 0;
3652 ssize_t nb_dom = 0;
3653 struct agent *agt;
3654 struct lttng_ht_iter iter;
3655
3656 if (session->kernel_session != nullptr) {
3657 DBG3("Listing domains found kernel domain");
3658 nb_dom++;
3659 }
3660
3661 if (session->ust_session != nullptr) {
3662 DBG3("Listing domains found UST global domain");
3663 nb_dom++;
3664
3665 lttng::urcu::read_lock_guard read_lock;
3666
3667 cds_lfht_for_each_entry (
3668 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3669 if (agt->being_used) {
3670 nb_dom++;
3671 }
3672 }
3673 }
3674
3675 if (!nb_dom) {
3676 goto end;
3677 }
3678
3679 *domains = calloc<lttng_domain>(nb_dom);
3680 if (*domains == nullptr) {
3681 ret = LTTNG_ERR_FATAL;
3682 goto error;
3683 }
3684
3685 if (session->kernel_session != nullptr) {
3686 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3687
3688 /* Kernel session buffer type is always GLOBAL */
3689 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3690
3691 index++;
3692 }
3693
3694 if (session->ust_session != nullptr) {
3695 (*domains)[index].type = LTTNG_DOMAIN_UST;
3696 (*domains)[index].buf_type = session->ust_session->buffer_type;
3697 index++;
3698
3699 {
3700 lttng::urcu::read_lock_guard read_lock;
3701
3702 cds_lfht_for_each_entry (
3703 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3704 if (agt->being_used) {
3705 (*domains)[index].type = agt->domain;
3706 (*domains)[index].buf_type =
3707 session->ust_session->buffer_type;
3708 index++;
3709 }
3710 }
3711 }
3712 }
3713 end:
3714 return nb_dom;
3715
3716 error:
3717 /* Return negative value to differentiate return code */
3718 return -ret;
3719 }
3720
3721 /*
3722 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3723 */
3724 enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
3725 struct ltt_session *session,
3726 struct lttng_payload *payload)
3727 {
3728 int ret = 0;
3729 unsigned int i = 0;
3730 struct lttcomm_list_command_header cmd_header = {};
3731 size_t cmd_header_offset;
3732 enum lttng_error_code ret_code;
3733
3734 assert(session);
3735 assert(payload);
3736
3737 DBG("Listing channels for session %s", session->name);
3738
3739 cmd_header_offset = payload->buffer.size;
3740
3741 /* Reserve space for command reply header. */
3742 ret = lttng_dynamic_buffer_set_size(&payload->buffer,
3743 cmd_header_offset + sizeof(cmd_header));
3744 if (ret) {
3745 ret_code = LTTNG_ERR_NOMEM;
3746 goto end;
3747 }
3748
3749 switch (domain) {
3750 case LTTNG_DOMAIN_KERNEL:
3751 {
3752 /* Kernel channels */
3753 struct ltt_kernel_channel *kchan;
3754 if (session->kernel_session != nullptr) {
3755 cds_list_for_each_entry (
3756 kchan, &session->kernel_session->channel_list.head, list) {
3757 uint64_t discarded_events, lost_packets;
3758 struct lttng_channel_extended *extended;
3759
3760 extended = (struct lttng_channel_extended *)
3761 kchan->channel->attr.extended.ptr;
3762
3763 ret = get_kernel_runtime_stats(
3764 session, kchan, &discarded_events, &lost_packets);
3765 if (ret < 0) {
3766 ret_code = LTTNG_ERR_UNK;
3767 goto end;
3768 }
3769
3770 /*
3771 * Update the discarded_events and lost_packets
3772 * count for the channel
3773 */
3774 extended->discarded_events = discarded_events;
3775 extended->lost_packets = lost_packets;
3776
3777 ret = lttng_channel_serialize(kchan->channel, &payload->buffer);
3778 if (ret) {
3779 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3780 kchan->channel->name);
3781 ret_code = LTTNG_ERR_UNK;
3782 goto end;
3783 }
3784
3785 i++;
3786 }
3787 }
3788 break;
3789 }
3790 case LTTNG_DOMAIN_UST:
3791 {
3792 struct lttng_ht_iter iter;
3793 struct ltt_ust_channel *uchan;
3794
3795 {
3796 lttng::urcu::read_lock_guard read_lock;
3797
3798 cds_lfht_for_each_entry (session->ust_session->domain_global.channels->ht,
3799 &iter.iter,
3800 uchan,
3801 node.node) {
3802 uint64_t discarded_events = 0, lost_packets = 0;
3803 struct lttng_channel *channel = nullptr;
3804 struct lttng_channel_extended *extended;
3805
3806 channel = trace_ust_channel_to_lttng_channel(uchan);
3807 if (!channel) {
3808 ret_code = LTTNG_ERR_NOMEM;
3809 goto end;
3810 }
3811
3812 extended = (struct lttng_channel_extended *)
3813 channel->attr.extended.ptr;
3814
3815 ret = get_ust_runtime_stats(
3816 session, uchan, &discarded_events, &lost_packets);
3817 if (ret < 0) {
3818 lttng_channel_destroy(channel);
3819 ret_code = LTTNG_ERR_UNK;
3820 goto end;
3821 }
3822
3823 extended->discarded_events = discarded_events;
3824 extended->lost_packets = lost_packets;
3825
3826 ret = lttng_channel_serialize(channel, &payload->buffer);
3827 if (ret) {
3828 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3829 channel->name);
3830 lttng_channel_destroy(channel);
3831 ret_code = LTTNG_ERR_UNK;
3832 goto end;
3833 }
3834
3835 lttng_channel_destroy(channel);
3836 i++;
3837 }
3838 }
3839
3840 break;
3841 }
3842 default:
3843 break;
3844 }
3845
3846 if (i > UINT32_MAX) {
3847 ERR("Channel count would overflow the channel listing command's reply");
3848 ret_code = LTTNG_ERR_OVERFLOW;
3849 goto end;
3850 }
3851
3852 /* Update command reply header. */
3853 cmd_header.count = (uint32_t) i;
3854 memcpy(payload->buffer.data + cmd_header_offset, &cmd_header, sizeof(cmd_header));
3855 ret_code = LTTNG_OK;
3856
3857 end:
3858 return ret_code;
3859 }
3860
3861 /*
3862 * Command LTTNG_LIST_EVENTS processed by the client thread.
3863 */
3864 enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
3865 struct ltt_session *session,
3866 char *channel_name,
3867 struct lttng_payload *reply_payload)
3868 {
3869 int buffer_resize_ret;
3870 enum lttng_error_code ret_code = LTTNG_OK;
3871 struct lttcomm_list_command_header reply_command_header = {};
3872 size_t reply_command_header_offset;
3873 unsigned int nb_events = 0;
3874
3875 assert(reply_payload);
3876
3877 /* Reserve space for command reply header. */
3878 reply_command_header_offset = reply_payload->buffer.size;
3879 buffer_resize_ret = lttng_dynamic_buffer_set_size(
3880 &reply_payload->buffer,
3881 reply_command_header_offset + sizeof(struct lttcomm_list_command_header));
3882 if (buffer_resize_ret) {
3883 ret_code = LTTNG_ERR_NOMEM;
3884 goto end;
3885 }
3886
3887 switch (domain) {
3888 case LTTNG_DOMAIN_KERNEL:
3889 if (session->kernel_session != nullptr) {
3890 ret_code = list_lttng_kernel_events(
3891 channel_name, session->kernel_session, reply_payload, &nb_events);
3892 }
3893
3894 break;
3895 case LTTNG_DOMAIN_UST:
3896 {
3897 if (session->ust_session != nullptr) {
3898 ret_code =
3899 list_lttng_ust_global_events(channel_name,
3900 &session->ust_session->domain_global,
3901 reply_payload,
3902 &nb_events);
3903 }
3904
3905 break;
3906 }
3907 case LTTNG_DOMAIN_LOG4J:
3908 case LTTNG_DOMAIN_JUL:
3909 case LTTNG_DOMAIN_PYTHON:
3910 if (session->ust_session) {
3911 struct lttng_ht_iter iter;
3912 struct agent *agt;
3913
3914 lttng::urcu::read_lock_guard read_lock;
3915
3916 cds_lfht_for_each_entry (
3917 session->ust_session->agents->ht, &iter.iter, agt, node.node) {
3918 if (agt->domain == domain) {
3919 ret_code = list_lttng_agent_events(
3920 agt, reply_payload, &nb_events);
3921 break;
3922 }
3923 }
3924 }
3925 break;
3926 default:
3927 ret_code = LTTNG_ERR_UND;
3928 break;
3929 }
3930
3931 if (nb_events > UINT32_MAX) {
3932 ret_code = LTTNG_ERR_OVERFLOW;
3933 goto end;
3934 }
3935
3936 /* Update command reply header. */
3937 reply_command_header.count = (uint32_t) nb_events;
3938 memcpy(reply_payload->buffer.data + reply_command_header_offset,
3939 &reply_command_header,
3940 sizeof(reply_command_header));
3941
3942 end:
3943 return ret_code;
3944 }
3945
3946 /*
3947 * Using the session list, filled a lttng_session array to send back to the
3948 * client for session listing.
3949 *
3950 * The session list lock MUST be acquired before calling this function. Use
3951 * session_lock_list() and session_unlock_list().
3952 */
3953 void cmd_list_lttng_sessions(struct lttng_session *sessions,
3954 size_t session_count,
3955 uid_t uid,
3956 gid_t gid)
3957 {
3958 int ret;
3959 unsigned int i = 0;
3960 struct ltt_session *session;
3961 struct ltt_session_list *list = session_get_list();
3962 struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]);
3963
3964 DBG("Getting all available session for UID %d GID %d", uid, gid);
3965 /*
3966 * Iterate over session list and append data after the control struct in
3967 * the buffer.
3968 */
3969 cds_list_for_each_entry (session, &list->head, list) {
3970 if (!session_get(session)) {
3971 continue;
3972 }
3973 /*
3974 * Only list the sessions the user can control.
3975 */
3976 if (!session_access_ok(session, uid) || session->destroyed) {
3977 session_put(session);
3978 continue;
3979 }
3980
3981 struct ltt_kernel_session *ksess = session->kernel_session;
3982 struct ltt_ust_session *usess = session->ust_session;
3983
3984 if (session->consumer->type == CONSUMER_DST_NET ||
3985 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3986 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3987 ret = build_network_session_path(
3988 sessions[i].path, sizeof(sessions[i].path), session);
3989 } else {
3990 ret = snprintf(sessions[i].path,
3991 sizeof(sessions[i].path),
3992 "%s",
3993 session->consumer->dst.session_root_path);
3994 }
3995 if (ret < 0) {
3996 PERROR("snprintf session path");
3997 session_put(session);
3998 continue;
3999 }
4000
4001 strncpy(sessions[i].name, session->name, NAME_MAX);
4002 sessions[i].name[NAME_MAX - 1] = '\0';
4003 sessions[i].enabled = session->active;
4004 sessions[i].snapshot_mode = session->snapshot_mode;
4005 sessions[i].live_timer_interval = session->live_timer;
4006 extended[i].creation_time.value = (uint64_t) session->creation_time;
4007 extended[i].creation_time.is_set = 1;
4008 i++;
4009 session_put(session);
4010 }
4011 }
4012
4013 /*
4014 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
4015 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
4016 */
4017 int cmd_data_pending(struct ltt_session *session)
4018 {
4019 int ret;
4020 struct ltt_kernel_session *ksess = session->kernel_session;
4021 struct ltt_ust_session *usess = session->ust_session;
4022
4023 LTTNG_ASSERT(session);
4024
4025 DBG("Data pending for session %s", session->name);
4026
4027 /* Session MUST be stopped to ask for data availability. */
4028 if (session->active) {
4029 ret = LTTNG_ERR_SESSION_STARTED;
4030 goto error;
4031 } else {
4032 /*
4033 * If stopped, just make sure we've started before else the above call
4034 * will always send that there is data pending.
4035 *
4036 * The consumer assumes that when the data pending command is received,
4037 * the trace has been started before or else no output data is written
4038 * by the streams which is a condition for data pending. So, this is
4039 * *VERY* important that we don't ask the consumer before a start
4040 * trace.
4041 */
4042 if (!session->has_been_started) {
4043 ret = 0;
4044 goto error;
4045 }
4046 }
4047
4048 /* A rotation is still pending, we have to wait. */
4049 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
4050 DBG("Rotate still pending for session %s", session->name);
4051 ret = 1;
4052 goto error;
4053 }
4054
4055 if (ksess && ksess->consumer) {
4056 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
4057 if (ret == 1) {
4058 /* Data is still being extracted for the kernel. */
4059 goto error;
4060 }
4061 }
4062
4063 if (usess && usess->consumer) {
4064 ret = consumer_is_data_pending(usess->id, usess->consumer);
4065 if (ret == 1) {
4066 /* Data is still being extracted for the kernel. */
4067 goto error;
4068 }
4069 }
4070
4071 /* Data is ready to be read by a viewer */
4072 ret = 0;
4073
4074 error:
4075 return ret;
4076 }
4077
4078 /*
4079 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4080 *
4081 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4082 */
4083 int cmd_snapshot_add_output(struct ltt_session *session,
4084 const struct lttng_snapshot_output *output,
4085 uint32_t *id)
4086 {
4087 int ret;
4088 struct snapshot_output *new_output;
4089
4090 LTTNG_ASSERT(session);
4091 LTTNG_ASSERT(output);
4092
4093 DBG("Cmd snapshot add output for session %s", session->name);
4094
4095 /*
4096 * Can't create an output if the session is not set in no-output mode.
4097 */
4098 if (session->output_traces) {
4099 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4100 goto error;
4101 }
4102
4103 if (session->has_non_mmap_channel) {
4104 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
4105 goto error;
4106 }
4107
4108 /* Only one output is allowed until we have the "tee" feature. */
4109 if (session->snapshot.nb_output == 1) {
4110 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
4111 goto error;
4112 }
4113
4114 new_output = snapshot_output_alloc();
4115 if (!new_output) {
4116 ret = LTTNG_ERR_NOMEM;
4117 goto error;
4118 }
4119
4120 ret = snapshot_output_init(session,
4121 output->max_size,
4122 output->name,
4123 output->ctrl_url,
4124 output->data_url,
4125 session->consumer,
4126 new_output,
4127 &session->snapshot);
4128 if (ret < 0) {
4129 if (ret == -ENOMEM) {
4130 ret = LTTNG_ERR_NOMEM;
4131 } else {
4132 ret = LTTNG_ERR_INVALID;
4133 }
4134 goto free_error;
4135 }
4136
4137 snapshot_add_output(&session->snapshot, new_output);
4138 if (id) {
4139 *id = new_output->id;
4140 }
4141
4142 return LTTNG_OK;
4143
4144 free_error:
4145 snapshot_output_destroy(new_output);
4146 error:
4147 return ret;
4148 }
4149
4150 /*
4151 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4152 *
4153 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4154 */
4155 int cmd_snapshot_del_output(struct ltt_session *session, const struct lttng_snapshot_output *output)
4156 {
4157 int ret;
4158 struct snapshot_output *sout = nullptr;
4159
4160 LTTNG_ASSERT(session);
4161 LTTNG_ASSERT(output);
4162
4163 lttng::urcu::read_lock_guard read_lock;
4164
4165 /*
4166 * Permission denied to create an output if the session is not
4167 * set in no output mode.
4168 */
4169 if (session->output_traces) {
4170 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4171 goto error;
4172 }
4173
4174 if (output->id) {
4175 DBG("Cmd snapshot del output id %" PRIu32 " for session %s",
4176 output->id,
4177 session->name);
4178 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
4179 } else if (*output->name != '\0') {
4180 DBG("Cmd snapshot del output name %s for session %s", output->name, session->name);
4181 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
4182 }
4183 if (!sout) {
4184 ret = LTTNG_ERR_INVALID;
4185 goto error;
4186 }
4187
4188 snapshot_delete_output(&session->snapshot, sout);
4189 snapshot_output_destroy(sout);
4190 ret = LTTNG_OK;
4191
4192 error:
4193 return ret;
4194 }
4195
4196 /*
4197 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4198 *
4199 * If no output is available, outputs is untouched and 0 is returned.
4200 *
4201 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4202 */
4203 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
4204 struct lttng_snapshot_output **outputs)
4205 {
4206 int ret, idx = 0;
4207 struct lttng_snapshot_output *list = nullptr;
4208 struct lttng_ht_iter iter;
4209 struct snapshot_output *output;
4210
4211 LTTNG_ASSERT(session);
4212 LTTNG_ASSERT(outputs);
4213
4214 DBG("Cmd snapshot list outputs for session %s", session->name);
4215
4216 /*
4217 * Permission denied to create an output if the session is not
4218 * set in no output mode.
4219 */
4220 if (session->output_traces) {
4221 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4222 goto end;
4223 }
4224
4225 if (session->snapshot.nb_output == 0) {
4226 ret = 0;
4227 goto end;
4228 }
4229
4230 list = calloc<lttng_snapshot_output>(session->snapshot.nb_output);
4231 if (!list) {
4232 ret = -LTTNG_ERR_NOMEM;
4233 goto end;
4234 }
4235
4236 /* Copy list from session to the new list object. */
4237 {
4238 lttng::urcu::read_lock_guard read_lock;
4239
4240 cds_lfht_for_each_entry (
4241 session->snapshot.output_ht->ht, &iter.iter, output, node.node) {
4242 LTTNG_ASSERT(output->consumer);
4243 list[idx].id = output->id;
4244 list[idx].max_size = output->max_size;
4245 if (lttng_strncpy(list[idx].name, output->name, sizeof(list[idx].name))) {
4246 ret = -LTTNG_ERR_INVALID;
4247 goto error;
4248 }
4249
4250 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4251 if (lttng_strncpy(list[idx].ctrl_url,
4252 output->consumer->dst.session_root_path,
4253 sizeof(list[idx].ctrl_url))) {
4254 ret = -LTTNG_ERR_INVALID;
4255 goto error;
4256 }
4257 } else {
4258 /* Control URI. */
4259 ret = uri_to_str_url(&output->consumer->dst.net.control,
4260 list[idx].ctrl_url,
4261 sizeof(list[idx].ctrl_url));
4262 if (ret < 0) {
4263 ret = -LTTNG_ERR_NOMEM;
4264 goto error;
4265 }
4266
4267 /* Data URI. */
4268 ret = uri_to_str_url(&output->consumer->dst.net.data,
4269 list[idx].data_url,
4270 sizeof(list[idx].data_url));
4271 if (ret < 0) {
4272 ret = -LTTNG_ERR_NOMEM;
4273 goto error;
4274 }
4275 }
4276
4277 idx++;
4278 }
4279 }
4280
4281 *outputs = list;
4282 list = nullptr;
4283 ret = session->snapshot.nb_output;
4284 error:
4285 free(list);
4286 end:
4287 return ret;
4288 }
4289
4290 /*
4291 * Check if we can regenerate the metadata for this session.
4292 * Only kernel, UST per-uid and non-live sessions are supported.
4293 *
4294 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4295 */
4296 static int check_regenerate_metadata_support(struct ltt_session *session)
4297 {
4298 int ret;
4299
4300 LTTNG_ASSERT(session);
4301
4302 if (session->live_timer != 0) {
4303 ret = LTTNG_ERR_LIVE_SESSION;
4304 goto end;
4305 }
4306 if (!session->active) {
4307 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4308 goto end;
4309 }
4310 if (session->ust_session) {
4311 switch (session->ust_session->buffer_type) {
4312 case LTTNG_BUFFER_PER_UID:
4313 break;
4314 case LTTNG_BUFFER_PER_PID:
4315 ret = LTTNG_ERR_PER_PID_SESSION;
4316 goto end;
4317 default:
4318 abort();
4319 ret = LTTNG_ERR_UNK;
4320 goto end;
4321 }
4322 }
4323 if (session->consumer->type == CONSUMER_DST_NET &&
4324 session->consumer->relay_minor_version < 8) {
4325 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4326 goto end;
4327 }
4328 ret = 0;
4329
4330 end:
4331 return ret;
4332 }
4333
4334 /*
4335 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4336 *
4337 * Ask the consumer to truncate the existing metadata file(s) and
4338 * then regenerate the metadata. Live and per-pid sessions are not
4339 * supported and return an error.
4340 *
4341 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4342 */
4343 int cmd_regenerate_metadata(struct ltt_session *session)
4344 {
4345 int ret;
4346
4347 LTTNG_ASSERT(session);
4348
4349 ret = check_regenerate_metadata_support(session);
4350 if (ret) {
4351 goto end;
4352 }
4353
4354 if (session->kernel_session) {
4355 ret = kernctl_session_regenerate_metadata(session->kernel_session->fd);
4356 if (ret < 0) {
4357 ERR("Failed to regenerate the kernel metadata");
4358 goto end;
4359 }
4360 }
4361
4362 if (session->ust_session) {
4363 ret = trace_ust_regenerate_metadata(session->ust_session);
4364 if (ret < 0) {
4365 ERR("Failed to regenerate the UST metadata");
4366 goto end;
4367 }
4368 }
4369 DBG("Cmd metadata regenerate for session %s", session->name);
4370 ret = LTTNG_OK;
4371
4372 end:
4373 return ret;
4374 }
4375
4376 /*
4377 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4378 *
4379 * Ask the tracer to regenerate a new statedump.
4380 *
4381 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4382 */
4383 int cmd_regenerate_statedump(struct ltt_session *session)
4384 {
4385 int ret;
4386
4387 LTTNG_ASSERT(session);
4388
4389 if (!session->active) {
4390 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4391 goto end;
4392 }
4393
4394 if (session->kernel_session) {
4395 ret = kernctl_session_regenerate_statedump(session->kernel_session->fd);
4396 /*
4397 * Currently, the statedump in kernel can only fail if out
4398 * of memory.
4399 */
4400 if (ret < 0) {
4401 if (ret == -ENOMEM) {
4402 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4403 } else {
4404 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4405 }
4406 ERR("Failed to regenerate the kernel statedump");
4407 goto end;
4408 }
4409 }
4410
4411 if (session->ust_session) {
4412 ret = ust_app_regenerate_statedump_all(session->ust_session);
4413 /*
4414 * Currently, the statedump in UST always returns 0.
4415 */
4416 if (ret < 0) {
4417 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4418 ERR("Failed to regenerate the UST statedump");
4419 goto end;
4420 }
4421 }
4422 DBG("Cmd regenerate statedump for session %s", session->name);
4423 ret = LTTNG_OK;
4424
4425 end:
4426 return ret;
4427 }
4428
4429 static enum lttng_error_code
4430 synchronize_tracer_notifier_register(struct notification_thread_handle *notification_thread,
4431 struct lttng_trigger *trigger,
4432 const struct lttng_credentials *cmd_creds)
4433 {
4434 enum lttng_error_code ret_code;
4435 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
4436 const char *trigger_name;
4437 uid_t trigger_owner;
4438 enum lttng_trigger_status trigger_status;
4439 const enum lttng_domain_type trigger_domain =
4440 lttng_trigger_get_underlying_domain_type_restriction(trigger);
4441
4442 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4443 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4444
4445 LTTNG_ASSERT(condition);
4446 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4447 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4448
4449 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4450 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4451
4452 session_lock_list();
4453 switch (trigger_domain) {
4454 case LTTNG_DOMAIN_KERNEL:
4455 {
4456 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4457 if (ret_code != LTTNG_OK) {
4458 enum lttng_error_code notif_thread_unregister_ret;
4459
4460 notif_thread_unregister_ret =
4461 notification_thread_command_unregister_trigger(notification_thread,
4462 trigger);
4463
4464 if (notif_thread_unregister_ret != LTTNG_OK) {
4465 /* Return the original error code. */
4466 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4467 trigger_name,
4468 (int) trigger_owner,
4469 ret_code);
4470 }
4471 }
4472 break;
4473 }
4474 case LTTNG_DOMAIN_UST:
4475 ust_app_global_update_all_event_notifier_rules();
4476 break;
4477 case LTTNG_DOMAIN_JUL:
4478 case LTTNG_DOMAIN_LOG4J:
4479 case LTTNG_DOMAIN_PYTHON:
4480 {
4481 /* Agent domains. */
4482 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
4483
4484 if (!agt) {
4485 agt = agent_create(trigger_domain);
4486 if (!agt) {
4487 ret_code = LTTNG_ERR_NOMEM;
4488 goto end_unlock_session_list;
4489 }
4490
4491 agent_add(agt, the_trigger_agents_ht_by_domain);
4492 }
4493
4494 ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
4495 if (ret_code != LTTNG_OK) {
4496 goto end_unlock_session_list;
4497 }
4498
4499 break;
4500 }
4501 case LTTNG_DOMAIN_NONE:
4502 default:
4503 abort();
4504 }
4505
4506 ret_code = LTTNG_OK;
4507 end_unlock_session_list:
4508 session_unlock_list();
4509 return ret_code;
4510 }
4511
4512 enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
4513 struct lttng_trigger *trigger,
4514 bool is_trigger_anonymous,
4515 struct notification_thread_handle *notification_thread,
4516 struct lttng_trigger **return_trigger)
4517 {
4518 enum lttng_error_code ret_code;
4519 const char *trigger_name;
4520 uid_t trigger_owner;
4521 enum lttng_trigger_status trigger_status;
4522
4523 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4524 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4525
4526 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4527 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4528
4529 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4530 trigger_name,
4531 (int) trigger_owner,
4532 (int) lttng_credentials_get_uid(cmd_creds));
4533
4534 /*
4535 * Validate the trigger credentials against the command credentials.
4536 * Only the root user can register a trigger with non-matching
4537 * credentials.
4538 */
4539 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
4540 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4541 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4542 trigger_name,
4543 (int) trigger_owner,
4544 (int) lttng_credentials_get_uid(cmd_creds));
4545 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4546 goto end;
4547 }
4548 }
4549
4550 /*
4551 * The bytecode generation also serves as a validation step for the
4552 * bytecode expressions.
4553 */
4554 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4555 if (ret_code != LTTNG_OK) {
4556 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4557 trigger_name,
4558 (int) trigger_owner,
4559 ret_code);
4560 goto end;
4561 }
4562
4563 /*
4564 * A reference to the trigger is acquired by the notification thread.
4565 * It is safe to return the same trigger to the caller since it the
4566 * other user holds a reference.
4567 *
4568 * The trigger is modified during the execution of the
4569 * "register trigger" command. However, by the time the command returns,
4570 * it is safe to use without any locking as its properties are
4571 * immutable.
4572 */
4573 ret_code = notification_thread_command_register_trigger(
4574 notification_thread, trigger, is_trigger_anonymous);
4575 if (ret_code != LTTNG_OK) {
4576 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4577 trigger_name,
4578 (int) trigger_owner,
4579 ret_code);
4580 goto end;
4581 }
4582
4583 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4584 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4585
4586 /*
4587 * Synchronize tracers if the trigger adds an event notifier.
4588 */
4589 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4590 ret_code = synchronize_tracer_notifier_register(
4591 notification_thread, trigger, cmd_creds);
4592 if (ret_code != LTTNG_OK) {
4593 ERR("Error registering tracer notifier: %s", lttng_strerror(-ret_code));
4594 goto end;
4595 }
4596 }
4597
4598 /*
4599 * Return an updated trigger to the client.
4600 *
4601 * Since a modified version of the same trigger is returned, acquire a
4602 * reference to the trigger so the caller doesn't have to care if those
4603 * are distinct instances or not.
4604 */
4605 if (ret_code == LTTNG_OK) {
4606 lttng_trigger_get(trigger);
4607 *return_trigger = trigger;
4608 /* Ownership of trigger was transferred to caller. */
4609 trigger = nullptr;
4610 }
4611 end:
4612 return ret_code;
4613 }
4614
4615 static enum lttng_error_code
4616 synchronize_tracer_notifier_unregister(const struct lttng_trigger *trigger)
4617 {
4618 enum lttng_error_code ret_code;
4619 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
4620 const enum lttng_domain_type trigger_domain =
4621 lttng_trigger_get_underlying_domain_type_restriction(trigger);
4622
4623 LTTNG_ASSERT(condition);
4624 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4625 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4626
4627 session_lock_list();
4628 switch (trigger_domain) {
4629 case LTTNG_DOMAIN_KERNEL:
4630 ret_code = kernel_unregister_event_notifier(trigger);
4631 if (ret_code != LTTNG_OK) {
4632 goto end_unlock_session_list;
4633 }
4634
4635 break;
4636 case LTTNG_DOMAIN_UST:
4637 ust_app_global_update_all_event_notifier_rules();
4638 break;
4639 case LTTNG_DOMAIN_JUL:
4640 case LTTNG_DOMAIN_LOG4J:
4641 case LTTNG_DOMAIN_PYTHON:
4642 {
4643 /* Agent domains. */
4644 struct agent *agt = agent_find_by_event_notifier_domain(trigger_domain);
4645
4646 /*
4647 * This trigger was never registered in the first place. Calling
4648 * this function under those circumstances is an internal error.
4649 */
4650 LTTNG_ASSERT(agt);
4651 ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
4652 if (ret_code != LTTNG_OK) {
4653 goto end_unlock_session_list;
4654 }
4655
4656 break;
4657 }
4658 case LTTNG_DOMAIN_NONE:
4659 default:
4660 abort();
4661 }
4662
4663 ret_code = LTTNG_OK;
4664
4665 end_unlock_session_list:
4666 session_unlock_list();
4667 return ret_code;
4668 }
4669
4670 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
4671 const struct lttng_trigger *trigger,
4672 struct notification_thread_handle *notification_thread)
4673 {
4674 enum lttng_error_code ret_code;
4675 const char *trigger_name;
4676 uid_t trigger_owner;
4677 enum lttng_trigger_status trigger_status;
4678 struct lttng_trigger *sessiond_trigger = nullptr;
4679
4680 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4681 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4682 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4683 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4684
4685 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4686 trigger_name,
4687 (int) trigger_owner,
4688 (int) lttng_credentials_get_uid(cmd_creds));
4689
4690 /*
4691 * Validate the trigger credentials against the command credentials.
4692 * Only the root user can unregister a trigger with non-matching
4693 * credentials.
4694 */
4695 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(trigger), cmd_creds)) {
4696 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4697 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4698 trigger_name,
4699 (int) trigger_owner,
4700 (int) lttng_credentials_get_uid(cmd_creds));
4701 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4702 goto end;
4703 }
4704 }
4705
4706 /* Fetch the sessiond side trigger object. */
4707 ret_code = notification_thread_command_get_trigger(
4708 notification_thread, trigger, &sessiond_trigger);
4709 if (ret_code != LTTNG_OK) {
4710 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4711 trigger_name,
4712 (int) trigger_owner,
4713 ret_code);
4714 goto end;
4715 }
4716
4717 LTTNG_ASSERT(sessiond_trigger);
4718
4719 /*
4720 * From this point on, no matter what, consider the trigger
4721 * unregistered.
4722 *
4723 * We set the unregistered state of the sessiond side trigger object in
4724 * the client thread since we want to minimize the possibility of the
4725 * notification thread being stalled due to a long execution of an
4726 * action that required the trigger lock.
4727 */
4728 lttng_trigger_set_as_unregistered(sessiond_trigger);
4729
4730 ret_code = notification_thread_command_unregister_trigger(notification_thread, trigger);
4731 if (ret_code != LTTNG_OK) {
4732 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4733 trigger_name,
4734 (int) trigger_owner,
4735 ret_code);
4736 goto end;
4737 }
4738
4739 /*
4740 * Synchronize tracers if the trigger removes an event notifier.
4741 * Do this even if the trigger unregistration failed to at least stop
4742 * the tracers from producing notifications associated with this
4743 * event notifier.
4744 */
4745 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4746 ret_code = synchronize_tracer_notifier_unregister(trigger);
4747 if (ret_code != LTTNG_OK) {
4748 ERR("Error unregistering trigger to tracer.");
4749 goto end;
4750 }
4751 }
4752
4753 end:
4754 lttng_trigger_put(sessiond_trigger);
4755 return ret_code;
4756 }
4757
4758 enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
4759 struct notification_thread_handle *notification_thread,
4760 struct lttng_triggers **return_triggers)
4761 {
4762 int ret;
4763 enum lttng_error_code ret_code;
4764 struct lttng_triggers *triggers = nullptr;
4765
4766 /* Get the set of triggers from the notification thread. */
4767 ret_code = notification_thread_command_list_triggers(
4768 notification_thread, cmd_ctx->creds.uid, &triggers);
4769 if (ret_code != LTTNG_OK) {
4770 goto end;
4771 }
4772
4773 ret = lttng_triggers_remove_hidden_triggers(triggers);
4774 if (ret) {
4775 ret_code = LTTNG_ERR_UNK;
4776 goto end;
4777 }
4778
4779 *return_triggers = triggers;
4780 triggers = nullptr;
4781 ret_code = LTTNG_OK;
4782 end:
4783 lttng_triggers_destroy(triggers);
4784 return ret_code;
4785 }
4786
4787 enum lttng_error_code
4788 cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4789 const struct lttng_error_query *query,
4790 struct lttng_error_query_results **_results,
4791 struct notification_thread_handle *notification_thread)
4792 {
4793 enum lttng_error_code ret_code;
4794 const struct lttng_trigger *query_target_trigger;
4795 const struct lttng_action *query_target_action = nullptr;
4796 struct lttng_trigger *matching_trigger = nullptr;
4797 const char *trigger_name;
4798 uid_t trigger_owner;
4799 enum lttng_trigger_status trigger_status;
4800 struct lttng_error_query_results *results = nullptr;
4801
4802 switch (lttng_error_query_get_target_type(query)) {
4803 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4804 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4805 break;
4806 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4807 query_target_trigger = lttng_error_query_condition_borrow_target(query);
4808 break;
4809 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4810 query_target_trigger = lttng_error_query_action_borrow_trigger_target(query);
4811 break;
4812 default:
4813 abort();
4814 }
4815
4816 LTTNG_ASSERT(query_target_trigger);
4817
4818 ret_code = notification_thread_command_get_trigger(
4819 notification_thread, query_target_trigger, &matching_trigger);
4820 if (ret_code != LTTNG_OK) {
4821 goto end;
4822 }
4823
4824 /* No longer needed. */
4825 query_target_trigger = nullptr;
4826
4827 if (lttng_error_query_get_target_type(query) == LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4828 /* Get the sessiond-side version of the target action. */
4829 query_target_action =
4830 lttng_error_query_action_borrow_action_target(query, matching_trigger);
4831 }
4832
4833 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4834 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4835 trigger_status = lttng_trigger_get_owner_uid(matching_trigger, &trigger_owner);
4836 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4837
4838 results = lttng_error_query_results_create();
4839 if (!results) {
4840 ret_code = LTTNG_ERR_NOMEM;
4841 goto end;
4842 }
4843
4844 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4845 trigger_name,
4846 (int) trigger_owner,
4847 (int) lttng_credentials_get_uid(cmd_creds));
4848
4849 /*
4850 * Validate the trigger credentials against the command credentials.
4851 * Only the root user can target a trigger with non-matching
4852 * credentials.
4853 */
4854 if (!lttng_credentials_is_equal_uid(lttng_trigger_get_credentials(matching_trigger),
4855 cmd_creds)) {
4856 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4857 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4858 trigger_name,
4859 (int) trigger_owner,
4860 (int) lttng_credentials_get_uid(cmd_creds));
4861 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4862 goto end;
4863 }
4864 }
4865
4866 switch (lttng_error_query_get_target_type(query)) {
4867 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4868 trigger_status = lttng_trigger_add_error_results(matching_trigger, results);
4869
4870 switch (trigger_status) {
4871 case LTTNG_TRIGGER_STATUS_OK:
4872 break;
4873 default:
4874 ret_code = LTTNG_ERR_UNK;
4875 goto end;
4876 }
4877
4878 break;
4879 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4880 {
4881 trigger_status =
4882 lttng_trigger_condition_add_error_results(matching_trigger, results);
4883
4884 switch (trigger_status) {
4885 case LTTNG_TRIGGER_STATUS_OK:
4886 break;
4887 default:
4888 ret_code = LTTNG_ERR_UNK;
4889 goto end;
4890 }
4891
4892 break;
4893 }
4894 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4895 {
4896 const enum lttng_action_status action_status =
4897 lttng_action_add_error_query_results(query_target_action, results);
4898
4899 switch (action_status) {
4900 case LTTNG_ACTION_STATUS_OK:
4901 break;
4902 default:
4903 ret_code = LTTNG_ERR_UNK;
4904 goto end;
4905 }
4906
4907 break;
4908 }
4909 default:
4910 abort();
4911 break;
4912 }
4913
4914 *_results = results;
4915 results = nullptr;
4916 ret_code = LTTNG_OK;
4917 end:
4918 lttng_trigger_put(matching_trigger);
4919 lttng_error_query_results_destroy(results);
4920 return ret_code;
4921 }
4922
4923 /*
4924 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4925 * snapshot output is *not* set with a remote destination.
4926 *
4927 * Return LTTNG_OK on success or a LTTNG_ERR code.
4928 */
4929 static enum lttng_error_code set_relayd_for_snapshot(struct consumer_output *output,
4930 const struct ltt_session *session)
4931 {
4932 enum lttng_error_code status = LTTNG_OK;
4933 struct lttng_ht_iter iter;
4934 struct consumer_socket *socket;
4935 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
4936 const char *base_path;
4937
4938 LTTNG_ASSERT(output);
4939 LTTNG_ASSERT(session);
4940
4941 DBG2("Set relayd object from snapshot output");
4942
4943 if (session->current_trace_chunk) {
4944 enum lttng_trace_chunk_status chunk_status = lttng_trace_chunk_get_id(
4945 session->current_trace_chunk, &current_chunk_id.value);
4946
4947 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
4948 current_chunk_id.is_set = true;
4949 } else {
4950 ERR("Failed to get current trace chunk id");
4951 status = LTTNG_ERR_UNK;
4952 goto error;
4953 }
4954 }
4955
4956 /* Ignore if snapshot consumer output is not network. */
4957 if (output->type != CONSUMER_DST_NET) {
4958 goto error;
4959 }
4960
4961 /*
4962 * The snapshot record URI base path overrides the session
4963 * base path.
4964 */
4965 if (output->dst.net.control.subdir[0] != '\0') {
4966 base_path = output->dst.net.control.subdir;
4967 } else {
4968 base_path = session->base_path;
4969 }
4970
4971 /*
4972 * For each consumer socket, create and send the relayd object of the
4973 * snapshot output.
4974 */
4975 {
4976 lttng::urcu::read_lock_guard read_lock;
4977
4978 cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
4979 pthread_mutex_lock(socket->lock);
4980 status = send_consumer_relayd_sockets(
4981 session->id,
4982 output,
4983 socket,
4984 session->name,
4985 session->hostname,
4986 base_path,
4987 session->live_timer,
4988 current_chunk_id.is_set ? &current_chunk_id.value : nullptr,
4989 session->creation_time,
4990 session->name_contains_creation_time);
4991 pthread_mutex_unlock(socket->lock);
4992 if (status != LTTNG_OK) {
4993 goto error;
4994 }
4995 }
4996 }
4997
4998 error:
4999 return status;
5000 }
5001
5002 /*
5003 * Record a kernel snapshot.
5004 *
5005 * Return LTTNG_OK on success or a LTTNG_ERR code.
5006 */
5007 static enum lttng_error_code record_kernel_snapshot(struct ltt_kernel_session *ksess,
5008 const struct consumer_output *output,
5009 const struct ltt_session *session,
5010 uint64_t nb_packets_per_stream)
5011 {
5012 enum lttng_error_code status;
5013
5014 LTTNG_ASSERT(ksess);
5015 LTTNG_ASSERT(output);
5016 LTTNG_ASSERT(session);
5017
5018 status = kernel_snapshot_record(ksess, output, nb_packets_per_stream);
5019 return status;
5020 }
5021
5022 /*
5023 * Record a UST snapshot.
5024 *
5025 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5026 */
5027 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
5028 const struct consumer_output *output,
5029 const struct ltt_session *session,
5030 uint64_t nb_packets_per_stream)
5031 {
5032 enum lttng_error_code status;
5033
5034 LTTNG_ASSERT(usess);
5035 LTTNG_ASSERT(output);
5036 LTTNG_ASSERT(session);
5037
5038 status = ust_app_snapshot_record(usess, output, nb_packets_per_stream);
5039 return status;
5040 }
5041
5042 static uint64_t get_session_size_one_more_packet_per_stream(const struct ltt_session *session,
5043 uint64_t cur_nr_packets)
5044 {
5045 uint64_t tot_size = 0;
5046
5047 if (session->kernel_session) {
5048 struct ltt_kernel_channel *chan;
5049 const struct ltt_kernel_session *ksess = session->kernel_session;
5050
5051 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
5052 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5053 /*
5054 * Don't take channel into account if we
5055 * already grab all its packets.
5056 */
5057 continue;
5058 }
5059 tot_size += chan->channel->attr.subbuf_size * chan->stream_count;
5060 }
5061 }
5062
5063 if (session->ust_session) {
5064 const struct ltt_ust_session *usess = session->ust_session;
5065
5066 tot_size += ust_app_get_size_one_more_packet_per_stream(usess, cur_nr_packets);
5067 }
5068
5069 return tot_size;
5070 }
5071
5072 /*
5073 * Calculate the number of packets we can grab from each stream that
5074 * fits within the overall snapshot max size.
5075 *
5076 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5077 * the number of packets per stream.
5078 *
5079 * TODO: this approach is not perfect: we consider the worse case
5080 * (packet filling the sub-buffers) as an upper bound, but we could do
5081 * better if we do this calculation while we actually grab the packet
5082 * content: we would know how much padding we don't actually store into
5083 * the file.
5084 *
5085 * This algorithm is currently bounded by the number of packets per
5086 * stream.
5087 *
5088 * Since we call this algorithm before actually grabbing the data, it's
5089 * an approximation: for instance, applications could appear/disappear
5090 * in between this call and actually grabbing data.
5091 */
5092 static int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5093 uint64_t max_size)
5094 {
5095 int64_t size_left;
5096 uint64_t cur_nb_packets = 0;
5097
5098 if (!max_size) {
5099 return 0; /* Infinite */
5100 }
5101
5102 size_left = max_size;
5103 for (;;) {
5104 uint64_t one_more_packet_tot_size;
5105
5106 one_more_packet_tot_size =
5107 get_session_size_one_more_packet_per_stream(session, cur_nb_packets);
5108 if (!one_more_packet_tot_size) {
5109 /* We are already grabbing all packets. */
5110 break;
5111 }
5112 size_left -= one_more_packet_tot_size;
5113 if (size_left < 0) {
5114 break;
5115 }
5116 cur_nb_packets++;
5117 }
5118 if (!cur_nb_packets && size_left != max_size) {
5119 /* Not enough room to grab one packet of each stream, error. */
5120 return -1;
5121 }
5122 return cur_nb_packets;
5123 }
5124
5125 static enum lttng_error_code snapshot_record(struct ltt_session *session,
5126 const struct snapshot_output *snapshot_output)
5127 {
5128 int64_t nb_packets_per_stream;
5129 char snapshot_chunk_name[LTTNG_NAME_MAX];
5130 int ret;
5131 enum lttng_error_code ret_code = LTTNG_OK;
5132 struct lttng_trace_chunk *snapshot_trace_chunk;
5133 struct consumer_output *original_ust_consumer_output = nullptr;
5134 struct consumer_output *original_kernel_consumer_output = nullptr;
5135 struct consumer_output *snapshot_ust_consumer_output = nullptr;
5136 struct consumer_output *snapshot_kernel_consumer_output = nullptr;
5137
5138 ret = snprintf(snapshot_chunk_name,
5139 sizeof(snapshot_chunk_name),
5140 "%s-%s-%" PRIu64,
5141 snapshot_output->name,
5142 snapshot_output->datetime,
5143 snapshot_output->nb_snapshot);
5144 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
5145 ERR("Failed to format snapshot name");
5146 ret_code = LTTNG_ERR_INVALID;
5147 goto error;
5148 }
5149 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5150 snapshot_output->name,
5151 session->name,
5152 snapshot_chunk_name);
5153 if (!session->kernel_session && !session->ust_session) {
5154 ERR("Failed to record snapshot as no channels exist");
5155 ret_code = LTTNG_ERR_NO_CHANNEL;
5156 goto error;
5157 }
5158
5159 if (session->kernel_session) {
5160 original_kernel_consumer_output = session->kernel_session->consumer;
5161 snapshot_kernel_consumer_output = consumer_copy_output(snapshot_output->consumer);
5162 strcpy(snapshot_kernel_consumer_output->chunk_path, snapshot_chunk_name);
5163
5164 /* Copy the original domain subdir. */
5165 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5166 original_kernel_consumer_output->domain_subdir);
5167
5168 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5169 original_kernel_consumer_output);
5170 if (ret < 0) {
5171 ERR("Failed to copy consumer sockets from snapshot output configuration");
5172 ret_code = LTTNG_ERR_NOMEM;
5173 goto error;
5174 }
5175 ret_code = set_relayd_for_snapshot(snapshot_kernel_consumer_output, session);
5176 if (ret_code != LTTNG_OK) {
5177 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5178 goto error;
5179 }
5180 session->kernel_session->consumer = snapshot_kernel_consumer_output;
5181 }
5182 if (session->ust_session) {
5183 original_ust_consumer_output = session->ust_session->consumer;
5184 snapshot_ust_consumer_output = consumer_copy_output(snapshot_output->consumer);
5185 strcpy(snapshot_ust_consumer_output->chunk_path, snapshot_chunk_name);
5186
5187 /* Copy the original domain subdir. */
5188 strcpy(snapshot_ust_consumer_output->domain_subdir,
5189 original_ust_consumer_output->domain_subdir);
5190
5191 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5192 original_ust_consumer_output);
5193 if (ret < 0) {
5194 ERR("Failed to copy consumer sockets from snapshot output configuration");
5195 ret_code = LTTNG_ERR_NOMEM;
5196 goto error;
5197 }
5198 ret_code = set_relayd_for_snapshot(snapshot_ust_consumer_output, session);
5199 if (ret_code != LTTNG_OK) {
5200 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5201 goto error;
5202 }
5203 session->ust_session->consumer = snapshot_ust_consumer_output;
5204 }
5205
5206 snapshot_trace_chunk = session_create_new_trace_chunk(
5207 session,
5208 snapshot_kernel_consumer_output ?: snapshot_ust_consumer_output,
5209 consumer_output_get_base_path(snapshot_output->consumer),
5210 snapshot_chunk_name);
5211 if (!snapshot_trace_chunk) {
5212 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5213 session->name);
5214 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5215 goto error;
5216 }
5217 LTTNG_ASSERT(!session->current_trace_chunk);
5218 ret = session_set_trace_chunk(session, snapshot_trace_chunk, nullptr);
5219 lttng_trace_chunk_put(snapshot_trace_chunk);
5220 snapshot_trace_chunk = nullptr;
5221 if (ret) {
5222 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5223 session->name);
5224 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5225 goto error;
5226 }
5227
5228 nb_packets_per_stream =
5229 get_session_nb_packets_per_stream(session, snapshot_output->max_size);
5230 if (nb_packets_per_stream < 0) {
5231 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5232 goto error_close_trace_chunk;
5233 }
5234
5235 if (session->kernel_session) {
5236 ret_code = record_kernel_snapshot(session->kernel_session,
5237 snapshot_kernel_consumer_output,
5238 session,
5239 nb_packets_per_stream);
5240 if (ret_code != LTTNG_OK) {
5241 goto error_close_trace_chunk;
5242 }
5243 }
5244
5245 if (session->ust_session) {
5246 ret_code = record_ust_snapshot(session->ust_session,
5247 snapshot_ust_consumer_output,
5248 session,
5249 nb_packets_per_stream);
5250 if (ret_code != LTTNG_OK) {
5251 goto error_close_trace_chunk;
5252 }
5253 }
5254
5255 error_close_trace_chunk:
5256 if (session_set_trace_chunk(session, nullptr, &snapshot_trace_chunk)) {
5257 ERR("Failed to release the current trace chunk of session \"%s\"", session->name);
5258 ret_code = LTTNG_ERR_UNK;
5259 }
5260
5261 if (session_close_trace_chunk(session,
5262 snapshot_trace_chunk,
5263 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION,
5264 nullptr)) {
5265 /*
5266 * Don't goto end; make sure the chunk is closed for the session
5267 * to allow future snapshots.
5268 */
5269 ERR("Failed to close snapshot trace chunk of session \"%s\"", session->name);
5270 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5271 }
5272
5273 lttng_trace_chunk_put(snapshot_trace_chunk);
5274 snapshot_trace_chunk = nullptr;
5275 error:
5276 if (original_ust_consumer_output) {
5277 session->ust_session->consumer = original_ust_consumer_output;
5278 }
5279 if (original_kernel_consumer_output) {
5280 session->kernel_session->consumer = original_kernel_consumer_output;
5281 }
5282 consumer_output_put(snapshot_ust_consumer_output);
5283 consumer_output_put(snapshot_kernel_consumer_output);
5284 return ret_code;
5285 }
5286
5287 /*
5288 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5289 *
5290 * The wait parameter is ignored so this call always wait for the snapshot to
5291 * complete before returning.
5292 *
5293 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5294 */
5295 int cmd_snapshot_record(struct ltt_session *session,
5296 const struct lttng_snapshot_output *output,
5297 int wait __attribute__((unused)))
5298 {
5299 enum lttng_error_code cmd_ret = LTTNG_OK;
5300 int ret;
5301 unsigned int snapshot_success = 0;
5302 char datetime[16];
5303 struct snapshot_output *tmp_output = nullptr;
5304
5305 LTTNG_ASSERT(session);
5306 LTTNG_ASSERT(output);
5307
5308 DBG("Cmd snapshot record for session %s", session->name);
5309
5310 /* Get the datetime for the snapshot output directory. */
5311 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, sizeof(datetime));
5312 if (!ret) {
5313 cmd_ret = LTTNG_ERR_INVALID;
5314 goto error;
5315 }
5316
5317 /*
5318 * Permission denied to create an output if the session is not
5319 * set in no output mode.
5320 */
5321 if (session->output_traces) {
5322 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
5323 goto error;
5324 }
5325
5326 /* The session needs to be started at least once. */
5327 if (!session->has_been_started) {
5328 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5329 goto error;
5330 }
5331
5332 /* Use temporary output for the session. */
5333 if (*output->ctrl_url != '\0') {
5334 tmp_output = snapshot_output_alloc();
5335 if (!tmp_output) {
5336 cmd_ret = LTTNG_ERR_NOMEM;
5337 goto error;
5338 }
5339
5340 ret = snapshot_output_init(session,
5341 output->max_size,
5342 output->name,
5343 output->ctrl_url,
5344 output->data_url,
5345 session->consumer,
5346 tmp_output,
5347 nullptr);
5348 if (ret < 0) {
5349 if (ret == -ENOMEM) {
5350 cmd_ret = LTTNG_ERR_NOMEM;
5351 } else {
5352 cmd_ret = LTTNG_ERR_INVALID;
5353 }
5354 goto error;
5355 }
5356 /* Use the global session count for the temporary snapshot. */
5357 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
5358
5359 /* Use the global datetime */
5360 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5361 cmd_ret = snapshot_record(session, tmp_output);
5362 if (cmd_ret != LTTNG_OK) {
5363 goto error;
5364 }
5365 snapshot_success = 1;
5366 } else {
5367 struct snapshot_output *sout;
5368 struct lttng_ht_iter iter;
5369
5370 lttng::urcu::read_lock_guard read_lock;
5371
5372 cds_lfht_for_each_entry (
5373 session->snapshot.output_ht->ht, &iter.iter, sout, node.node) {
5374 struct snapshot_output output_copy;
5375
5376 /*
5377 * Make a local copy of the output and override output
5378 * parameters with those provided as part of the
5379 * command.
5380 */
5381 memcpy(&output_copy, sout, sizeof(output_copy));
5382
5383 if (output->max_size != (uint64_t) -1ULL) {
5384 output_copy.max_size = output->max_size;
5385 }
5386
5387 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5388 memcpy(output_copy.datetime, datetime, sizeof(datetime));
5389
5390 /* Use temporary name. */
5391 if (*output->name != '\0') {
5392 if (lttng_strncpy(output_copy.name,
5393 output->name,
5394 sizeof(output_copy.name))) {
5395 cmd_ret = LTTNG_ERR_INVALID;
5396 goto error;
5397 }
5398 }
5399
5400 cmd_ret = snapshot_record(session, &output_copy);
5401 if (cmd_ret != LTTNG_OK) {
5402 goto error;
5403 }
5404
5405 snapshot_success = 1;
5406 }
5407 }
5408
5409 if (snapshot_success) {
5410 session->snapshot.nb_snapshot++;
5411 } else {
5412 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
5413 }
5414
5415 error:
5416 if (tmp_output) {
5417 snapshot_output_destroy(tmp_output);
5418 }
5419
5420 return cmd_ret;
5421 }
5422
5423 /*
5424 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5425 */
5426 int cmd_set_session_shm_path(struct ltt_session *session, const char *shm_path)
5427 {
5428 /* Safety net */
5429 LTTNG_ASSERT(session);
5430
5431 /*
5432 * Can only set shm path before session is started.
5433 */
5434 if (session->has_been_started) {
5435 return LTTNG_ERR_SESSION_STARTED;
5436 }
5437
5438 strncpy(session->shm_path, shm_path, sizeof(session->shm_path));
5439 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5440
5441 return LTTNG_OK;
5442 }
5443
5444 /*
5445 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5446 *
5447 * Ask the consumer to rotate the session output directory.
5448 * The session lock must be held.
5449 *
5450 * Returns LTTNG_OK on success or else a negative LTTng error code.
5451 */
5452 int cmd_rotate_session(struct ltt_session *session,
5453 struct lttng_rotate_session_return *rotate_return,
5454 bool quiet_rotation,
5455 enum lttng_trace_chunk_command_type command)
5456 {
5457 int ret;
5458 uint64_t ongoing_rotation_chunk_id;
5459 enum lttng_error_code cmd_ret = LTTNG_OK;
5460 struct lttng_trace_chunk *chunk_being_archived = nullptr;
5461 struct lttng_trace_chunk *new_trace_chunk = nullptr;
5462 enum lttng_trace_chunk_status chunk_status;
5463 bool failed_to_rotate = false;
5464 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5465
5466 LTTNG_ASSERT(session);
5467
5468 if (!session->has_been_started) {
5469 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5470 goto end;
5471 }
5472
5473 /*
5474 * Explicit rotation is not supported for live sessions.
5475 * However, live sessions can perform a quiet rotation on
5476 * destroy.
5477 * Rotation is not supported for snapshot traces (no output).
5478 */
5479 if ((!quiet_rotation && session->live_timer) || !session->output_traces) {
5480 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5481 goto end;
5482 }
5483
5484 /* Unsupported feature in lttng-relayd before 2.11. */
5485 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5486 (session->consumer->relay_major_version == 2 &&
5487 session->consumer->relay_minor_version < 11)) {
5488 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
5489 goto end;
5490 }
5491
5492 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5493 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5494 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5495 goto end;
5496 }
5497
5498 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5499 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5500 session->name);
5501 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
5502 goto end;
5503 }
5504
5505 /*
5506 * After a stop, we only allow one rotation to occur, the other ones are
5507 * useless until a new start.
5508 */
5509 if (session->rotated_after_last_stop) {
5510 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5511 session->name);
5512 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
5513 goto end;
5514 }
5515
5516 /*
5517 * After a stop followed by a clear, disallow following rotations a they would
5518 * generate empty chunks.
5519 */
5520 if (session->cleared_after_last_stop) {
5521 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5522 session->name);
5523 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5524 goto end;
5525 }
5526
5527 if (session->active) {
5528 new_trace_chunk =
5529 session_create_new_trace_chunk(session, nullptr, nullptr, nullptr);
5530 if (!new_trace_chunk) {
5531 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5532 goto error;
5533 }
5534 }
5535
5536 /*
5537 * The current trace chunk becomes the chunk being archived.
5538 *
5539 * After this point, "chunk_being_archived" must absolutely
5540 * be closed on the consumer(s), otherwise it will never be
5541 * cleaned-up, which will result in a leak.
5542 */
5543 ret = session_set_trace_chunk(session, new_trace_chunk, &chunk_being_archived);
5544 if (ret) {
5545 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5546 goto error;
5547 }
5548
5549 if (session->kernel_session) {
5550 cmd_ret = kernel_rotate_session(session);
5551 if (cmd_ret != LTTNG_OK) {
5552 failed_to_rotate = true;
5553 rotation_fail_code = cmd_ret;
5554 }
5555 }
5556 if (session->ust_session) {
5557 cmd_ret = ust_app_rotate_session(session);
5558 if (cmd_ret != LTTNG_OK) {
5559 failed_to_rotate = true;
5560 rotation_fail_code = cmd_ret;
5561 }
5562 }
5563
5564 if (!session->active) {
5565 session->rotated_after_last_stop = true;
5566 }
5567
5568 if (!chunk_being_archived) {
5569 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5570 session->name);
5571 if (failed_to_rotate) {
5572 cmd_ret = rotation_fail_code;
5573 goto error;
5574 }
5575 cmd_ret = LTTNG_OK;
5576 goto end;
5577 }
5578
5579 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5580 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, &ongoing_rotation_chunk_id);
5581 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5582
5583 ret = session_close_trace_chunk(
5584 session, chunk_being_archived, command, session->last_chunk_path);
5585 if (ret) {
5586 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5587 goto error;
5588 }
5589
5590 if (failed_to_rotate) {
5591 cmd_ret = rotation_fail_code;
5592 goto error;
5593 }
5594
5595 session->quiet_rotation = quiet_rotation;
5596 ret = timer_session_rotation_pending_check_start(session, DEFAULT_ROTATE_PENDING_TIMER);
5597 if (ret) {
5598 cmd_ret = LTTNG_ERR_UNK;
5599 goto error;
5600 }
5601
5602 if (rotate_return) {
5603 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5604 }
5605
5606 session->chunk_being_archived = chunk_being_archived;
5607 chunk_being_archived = nullptr;
5608 if (!quiet_rotation) {
5609 ret = notification_thread_command_session_rotation_ongoing(
5610 the_notification_thread_handle, session->id, ongoing_rotation_chunk_id);
5611 if (ret != LTTNG_OK) {
5612 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5613 session->name);
5614 cmd_ret = (lttng_error_code) ret;
5615 }
5616 }
5617
5618 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5619 session->name,
5620 ongoing_rotation_chunk_id);
5621 end:
5622 lttng_trace_chunk_put(new_trace_chunk);
5623 lttng_trace_chunk_put(chunk_being_archived);
5624 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5625 return ret;
5626 error:
5627 if (session_reset_rotation_state(*session, LTTNG_ROTATION_STATE_ERROR)) {
5628 ERR("Failed to reset rotation state of session \"%s\"", session->name);
5629 }
5630 goto end;
5631 }
5632
5633 /*
5634 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5635 *
5636 * Check if the session has finished its rotation.
5637 *
5638 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5639 */
5640 int cmd_rotate_get_info(struct ltt_session *session,
5641 struct lttng_rotation_get_info_return *info_return,
5642 uint64_t rotation_id)
5643 {
5644 enum lttng_error_code cmd_ret = LTTNG_OK;
5645 enum lttng_rotation_state rotation_state;
5646
5647 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64,
5648 session->name,
5649 session->most_recent_chunk_id.value);
5650
5651 if (session->chunk_being_archived) {
5652 enum lttng_trace_chunk_status chunk_status;
5653 uint64_t chunk_id;
5654
5655 chunk_status = lttng_trace_chunk_get_id(session->chunk_being_archived, &chunk_id);
5656 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5657
5658 rotation_state = rotation_id == chunk_id ? LTTNG_ROTATION_STATE_ONGOING :
5659 LTTNG_ROTATION_STATE_EXPIRED;
5660 } else {
5661 if (session->last_archived_chunk_id.is_set &&
5662 rotation_id != session->last_archived_chunk_id.value) {
5663 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5664 } else {
5665 rotation_state = session->rotation_state;
5666 }
5667 }
5668
5669 switch (rotation_state) {
5670 case LTTNG_ROTATION_STATE_NO_ROTATION:
5671 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5672 session->name);
5673 goto end;
5674 case LTTNG_ROTATION_STATE_EXPIRED:
5675 DBG("Reporting that the rotation state of rotation id %" PRIu64
5676 " of session \"%s\" has expired",
5677 rotation_id,
5678 session->name);
5679 break;
5680 case LTTNG_ROTATION_STATE_ONGOING:
5681 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5682 rotation_id,
5683 session->name);
5684 break;
5685 case LTTNG_ROTATION_STATE_COMPLETED:
5686 {
5687 int fmt_ret;
5688 char *chunk_path;
5689 char *current_tracing_path_reply;
5690 size_t current_tracing_path_reply_len;
5691
5692 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5693 rotation_id,
5694 session->name);
5695
5696 switch (session_get_consumer_destination_type(session)) {
5697 case CONSUMER_DST_LOCAL:
5698 current_tracing_path_reply = info_return->location.local.absolute_path;
5699 current_tracing_path_reply_len =
5700 sizeof(info_return->location.local.absolute_path);
5701 info_return->location_type =
5702 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5703 fmt_ret = asprintf(&chunk_path,
5704 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5705 session_get_base_path(session),
5706 session->last_archived_chunk_name);
5707 if (fmt_ret == -1) {
5708 PERROR("Failed to format the path of the last archived trace chunk");
5709 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5710 cmd_ret = LTTNG_ERR_UNK;
5711 goto end;
5712 }
5713 break;
5714 case CONSUMER_DST_NET:
5715 {
5716 uint16_t ctrl_port, data_port;
5717
5718 current_tracing_path_reply = info_return->location.relay.relative_path;
5719 current_tracing_path_reply_len =
5720 sizeof(info_return->location.relay.relative_path);
5721 /* Currently the only supported relay protocol. */
5722 info_return->location.relay.protocol =
5723 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5724
5725 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5726 session_get_net_consumer_hostname(session),
5727 sizeof(info_return->location.relay.host));
5728 if (fmt_ret) {
5729 ERR("Failed to copy host name to rotate_get_info reply");
5730 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5731 cmd_ret = LTTNG_ERR_SET_URL;
5732 goto end;
5733 }
5734
5735 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5736 info_return->location.relay.ports.control = ctrl_port;
5737 info_return->location.relay.ports.data = data_port;
5738 info_return->location_type =
5739 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5740 chunk_path = strdup(session->last_chunk_path);
5741 if (!chunk_path) {
5742 ERR("Failed to allocate the path of the last archived trace chunk");
5743 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5744 cmd_ret = LTTNG_ERR_UNK;
5745 goto end;
5746 }
5747 break;
5748 }
5749 default:
5750 abort();
5751 }
5752
5753 fmt_ret = lttng_strncpy(
5754 current_tracing_path_reply, chunk_path, current_tracing_path_reply_len);
5755 free(chunk_path);
5756 if (fmt_ret) {
5757 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5758 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5759 cmd_ret = LTTNG_ERR_UNK;
5760 goto end;
5761 }
5762
5763 break;
5764 }
5765 case LTTNG_ROTATION_STATE_ERROR:
5766 DBG("Reporting that an error occurred during rotation %" PRIu64
5767 " of session \"%s\"",
5768 rotation_id,
5769 session->name);
5770 break;
5771 default:
5772 abort();
5773 }
5774
5775 cmd_ret = LTTNG_OK;
5776 end:
5777 info_return->status = (int32_t) rotation_state;
5778 return cmd_ret;
5779 }
5780
5781 /*
5782 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5783 *
5784 * Configure the automatic rotation parameters.
5785 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5786 * 'activate' to false means deactivate the rotation schedule and validate that
5787 * 'new_value' has the same value as the currently active value.
5788 *
5789 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5790 */
5791 int cmd_rotation_set_schedule(struct ltt_session *session,
5792 bool activate,
5793 enum lttng_rotation_schedule_type schedule_type,
5794 uint64_t new_value)
5795 {
5796 int ret;
5797 uint64_t *parameter_value;
5798
5799 LTTNG_ASSERT(session);
5800
5801 DBG("Cmd rotate set schedule session %s", session->name);
5802
5803 if (session->live_timer || !session->output_traces) {
5804 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5805 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5806 goto end;
5807 }
5808
5809 switch (schedule_type) {
5810 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5811 parameter_value = &session->rotate_size;
5812 break;
5813 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5814 parameter_value = &session->rotate_timer_period;
5815 if (new_value >= UINT_MAX) {
5816 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64
5817 " > %u (UINT_MAX)",
5818 new_value,
5819 UINT_MAX);
5820 ret = LTTNG_ERR_INVALID;
5821 goto end;
5822 }
5823 break;
5824 default:
5825 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5826 ret = LTTNG_ERR_INVALID;
5827 goto end;
5828 }
5829
5830 /* Improper use of the API. */
5831 if (new_value == -1ULL) {
5832 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5833 ret = LTTNG_ERR_INVALID;
5834 goto end;
5835 }
5836
5837 /*
5838 * As indicated in struct ltt_session's comments, a value of == 0 means
5839 * this schedule rotation type is not in use.
5840 *
5841 * Reject the command if we were asked to activate a schedule that was
5842 * already active.
5843 */
5844 if (activate && *parameter_value != 0) {
5845 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5846 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5847 goto end;
5848 }
5849
5850 /*
5851 * Reject the command if we were asked to deactivate a schedule that was
5852 * not active.
5853 */
5854 if (!activate && *parameter_value == 0) {
5855 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5856 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5857 goto end;
5858 }
5859
5860 /*
5861 * Reject the command if we were asked to deactivate a schedule that
5862 * doesn't exist.
5863 */
5864 if (!activate && *parameter_value != new_value) {
5865 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5866 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5867 goto end;
5868 }
5869
5870 *parameter_value = activate ? new_value : 0;
5871
5872 switch (schedule_type) {
5873 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5874 if (activate && session->active) {
5875 /*
5876 * Only start the timer if the session is active,
5877 * otherwise it will be started when the session starts.
5878 */
5879 ret = timer_session_rotation_schedule_timer_start(session, new_value);
5880 if (ret) {
5881 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5882 ret = LTTNG_ERR_UNK;
5883 goto end;
5884 }
5885 } else {
5886 ret = timer_session_rotation_schedule_timer_stop(session);
5887 if (ret) {
5888 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5889 ret = LTTNG_ERR_UNK;
5890 goto end;
5891 }
5892 }
5893 break;
5894 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5895 if (activate) {
5896 try {
5897 the_rotation_thread_handle->subscribe_session_consumed_size_rotation(
5898 *session, new_value);
5899 } catch (std::exception& e) {
5900 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5901 e.what());
5902 ret = LTTNG_ERR_UNK;
5903 goto end;
5904 }
5905 } else {
5906 try {
5907 the_rotation_thread_handle
5908 ->unsubscribe_session_consumed_size_rotation(*session);
5909 } catch (std::exception& e) {
5910 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command: %s",
5911 e.what());
5912 ret = LTTNG_ERR_UNK;
5913 goto end;
5914 }
5915 }
5916 break;
5917 default:
5918 /* Would have been caught before. */
5919 abort();
5920 }
5921
5922 ret = LTTNG_OK;
5923
5924 goto end;
5925
5926 end:
5927 return ret;
5928 }
5929
5930 /* Wait for a given path to be removed before continuing. */
5931 static enum lttng_error_code wait_on_path(void *path_data)
5932 {
5933 const char *shm_path = (const char *) path_data;
5934
5935 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5936 shm_path);
5937 while (true) {
5938 int ret;
5939 struct stat st;
5940
5941 ret = stat(shm_path, &st);
5942 if (ret) {
5943 if (errno != ENOENT) {
5944 PERROR("stat() returned an error while checking for the existence of the shm path");
5945 } else {
5946 DBG("shm path no longer exists, completing the destruction of session");
5947 }
5948 break;
5949 } else {
5950 if (!S_ISDIR(st.st_mode)) {
5951 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5952 shm_path);
5953 break;
5954 }
5955 }
5956 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5957 }
5958 return LTTNG_OK;
5959 }
5960
5961 /*
5962 * Returns a pointer to a handler to run on completion of a command.
5963 * Returns NULL if no handler has to be run for the last command executed.
5964 */
5965 const struct cmd_completion_handler *cmd_pop_completion_handler()
5966 {
5967 struct cmd_completion_handler *handler = current_completion_handler;
5968
5969 current_completion_handler = nullptr;
5970 return handler;
5971 }
5972
5973 /*
5974 * Init command subsystem.
5975 */
5976 void cmd_init()
5977 {
5978 /*
5979 * Set network sequence index to 1 for streams to match a relayd
5980 * socket on the consumer side.
5981 */
5982 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5983 relayd_net_seq_idx = 1;
5984 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
5985
5986 DBG("Command subsystem initialized");
5987 }
This page took 0.189382 seconds and 4 git commands to generate.