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