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