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