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