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