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