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