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