Fix: list_lttng_agent_events: unbalanced RCU read-side lock on error
[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 * Don't try to enable a channel if the session has been started at
1512 * some point in time before. The tracer does not allow it.
1513 */
1514 if (session->has_been_started) {
1515 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1516 goto error;
1517 }
1518
1519 /*
1520 * If the session is a live session, remove the switch timer, the
1521 * live timer does the same thing but sends also synchronisation
1522 * beacons for inactive streams.
1523 */
1524 if (session->live_timer > 0) {
1525 attr.attr.live_timer_interval = session->live_timer;
1526 attr.attr.switch_timer_interval = 0;
1527 }
1528
1529 /* Check for feature support */
1530 switch (domain->type) {
1531 case LTTNG_DOMAIN_KERNEL:
1532 {
1533 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1534 /* Sampling position of buffer is not supported */
1535 WARN("Kernel tracer does not support buffer monitoring. "
1536 "Setting the monitor interval timer to 0 "
1537 "(disabled) for channel '%s' of session '%s'",
1538 attr.name, session->name);
1539 lttng_channel_set_monitor_timer_interval(&attr, 0);
1540 }
1541 break;
1542 }
1543 case LTTNG_DOMAIN_UST:
1544 break;
1545 case LTTNG_DOMAIN_JUL:
1546 case LTTNG_DOMAIN_LOG4J:
1547 case LTTNG_DOMAIN_PYTHON:
1548 if (!agent_tracing_is_enabled()) {
1549 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1550 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1551 goto error;
1552 }
1553 break;
1554 default:
1555 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1556 goto error;
1557 }
1558
1559 switch (domain->type) {
1560 case LTTNG_DOMAIN_KERNEL:
1561 {
1562 struct ltt_kernel_channel *kchan;
1563
1564 kchan = trace_kernel_get_channel_by_name(attr.name,
1565 session->kernel_session);
1566 if (kchan == NULL) {
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 ret = channel_ust_create(usess, &attr, domain->buf_type);
1627 if (attr.name[0] != '\0') {
1628 usess->has_non_default_channel = 1;
1629 }
1630 } else {
1631 ret = channel_ust_enable(usess, uchan);
1632 }
1633 break;
1634 }
1635 default:
1636 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1637 goto error;
1638 }
1639
1640 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
1641 session->has_non_mmap_channel = true;
1642 }
1643 error:
1644 rcu_read_unlock();
1645 end:
1646 return ret;
1647 }
1648
1649 /*
1650 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1651 */
1652 int cmd_disable_event(struct ltt_session *session,
1653 enum lttng_domain_type domain, const char *channel_name,
1654 const struct lttng_event *event)
1655 {
1656 int ret;
1657 const char *event_name;
1658
1659 DBG("Disable event command for event \'%s\'", event->name);
1660
1661 event_name = event->name;
1662
1663 /* Error out on unhandled search criteria */
1664 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1665 || event->pid || event->filter || event->exclusion) {
1666 ret = LTTNG_ERR_UNK;
1667 goto error;
1668 }
1669
1670 rcu_read_lock();
1671
1672 switch (domain) {
1673 case LTTNG_DOMAIN_KERNEL:
1674 {
1675 struct ltt_kernel_channel *kchan;
1676 struct ltt_kernel_session *ksess;
1677
1678 ksess = session->kernel_session;
1679
1680 /*
1681 * If a non-default channel has been created in the
1682 * session, explicitely require that -c chan_name needs
1683 * to be provided.
1684 */
1685 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1686 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1687 goto error_unlock;
1688 }
1689
1690 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1691 if (kchan == NULL) {
1692 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1693 goto error_unlock;
1694 }
1695
1696 switch (event->type) {
1697 case LTTNG_EVENT_ALL:
1698 case LTTNG_EVENT_TRACEPOINT:
1699 case LTTNG_EVENT_SYSCALL:
1700 case LTTNG_EVENT_PROBE:
1701 case LTTNG_EVENT_FUNCTION:
1702 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1703 if (event_name[0] == '\0') {
1704 ret = event_kernel_disable_event(kchan,
1705 NULL, event->type);
1706 } else {
1707 ret = event_kernel_disable_event(kchan,
1708 event_name, event->type);
1709 }
1710 if (ret != LTTNG_OK) {
1711 goto error_unlock;
1712 }
1713 break;
1714 default:
1715 ret = LTTNG_ERR_UNK;
1716 goto error_unlock;
1717 }
1718
1719 kernel_wait_quiescent();
1720 break;
1721 }
1722 case LTTNG_DOMAIN_UST:
1723 {
1724 struct ltt_ust_channel *uchan;
1725 struct ltt_ust_session *usess;
1726
1727 usess = session->ust_session;
1728
1729 if (validate_ust_event_name(event_name)) {
1730 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1731 goto error_unlock;
1732 }
1733
1734 /*
1735 * If a non-default channel has been created in the
1736 * session, explicitly require that -c chan_name needs
1737 * to be provided.
1738 */
1739 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1740 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1741 goto error_unlock;
1742 }
1743
1744 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1745 channel_name);
1746 if (uchan == NULL) {
1747 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1748 goto error_unlock;
1749 }
1750
1751 switch (event->type) {
1752 case LTTNG_EVENT_ALL:
1753 /*
1754 * An empty event name means that everything
1755 * should be disabled.
1756 */
1757 if (event->name[0] == '\0') {
1758 ret = event_ust_disable_all_tracepoints(usess, uchan);
1759 } else {
1760 ret = event_ust_disable_tracepoint(usess, uchan,
1761 event_name);
1762 }
1763 if (ret != LTTNG_OK) {
1764 goto error_unlock;
1765 }
1766 break;
1767 default:
1768 ret = LTTNG_ERR_UNK;
1769 goto error_unlock;
1770 }
1771
1772 DBG3("Disable UST event %s in channel %s completed", event_name,
1773 channel_name);
1774 break;
1775 }
1776 case LTTNG_DOMAIN_LOG4J:
1777 case LTTNG_DOMAIN_JUL:
1778 case LTTNG_DOMAIN_PYTHON:
1779 {
1780 struct agent *agt;
1781 struct ltt_ust_session *usess = session->ust_session;
1782
1783 assert(usess);
1784
1785 switch (event->type) {
1786 case LTTNG_EVENT_ALL:
1787 break;
1788 default:
1789 ret = LTTNG_ERR_UNK;
1790 goto error_unlock;
1791 }
1792
1793 agt = trace_ust_find_agent(usess, domain);
1794 if (!agt) {
1795 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1796 goto error_unlock;
1797 }
1798 /*
1799 * An empty event name means that everything
1800 * should be disabled.
1801 */
1802 if (event->name[0] == '\0') {
1803 ret = event_agent_disable_all(usess, agt);
1804 } else {
1805 ret = event_agent_disable(usess, agt, event_name);
1806 }
1807 if (ret != LTTNG_OK) {
1808 goto error_unlock;
1809 }
1810
1811 break;
1812 }
1813 default:
1814 ret = LTTNG_ERR_UND;
1815 goto error_unlock;
1816 }
1817
1818 ret = LTTNG_OK;
1819
1820 error_unlock:
1821 rcu_read_unlock();
1822 error:
1823 return ret;
1824 }
1825
1826 /*
1827 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1828 */
1829 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1830 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
1831 {
1832 int ret, chan_kern_created = 0, chan_ust_created = 0;
1833 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1834
1835 /*
1836 * Don't try to add a context if the session has been started at
1837 * some point in time before. The tracer does not allow it and would
1838 * result in a corrupted trace.
1839 */
1840 if (session->has_been_started) {
1841 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1842 goto end;
1843 }
1844
1845 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1846 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1847 app_ctx_name = ctx->u.app_ctx.ctx_name;
1848 }
1849
1850 switch (domain) {
1851 case LTTNG_DOMAIN_KERNEL:
1852 assert(session->kernel_session);
1853
1854 if (session->kernel_session->channel_count == 0) {
1855 /* Create default channel */
1856 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1857 if (ret != LTTNG_OK) {
1858 goto error;
1859 }
1860 chan_kern_created = 1;
1861 }
1862 /* Add kernel context to kernel tracer */
1863 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1864 if (ret != LTTNG_OK) {
1865 goto error;
1866 }
1867 break;
1868 case LTTNG_DOMAIN_JUL:
1869 case LTTNG_DOMAIN_LOG4J:
1870 {
1871 /*
1872 * Validate channel name.
1873 * If no channel name is given and the domain is JUL or LOG4J,
1874 * set it to the appropriate domain-specific channel name. If
1875 * a name is provided but does not match the expexted channel
1876 * name, return an error.
1877 */
1878 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1879 strcmp(channel_name,
1880 DEFAULT_JUL_CHANNEL_NAME)) {
1881 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1882 goto error;
1883 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1884 strcmp(channel_name,
1885 DEFAULT_LOG4J_CHANNEL_NAME)) {
1886 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1887 goto error;
1888 }
1889 /* break is _not_ missing here. */
1890 }
1891 case LTTNG_DOMAIN_UST:
1892 {
1893 struct ltt_ust_session *usess = session->ust_session;
1894 unsigned int chan_count;
1895
1896 assert(usess);
1897
1898 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1899 if (chan_count == 0) {
1900 struct lttng_channel *attr;
1901 /* Create default channel */
1902 attr = channel_new_default_attr(domain, usess->buffer_type);
1903 if (attr == NULL) {
1904 ret = LTTNG_ERR_FATAL;
1905 goto error;
1906 }
1907
1908 ret = channel_ust_create(usess, attr, usess->buffer_type);
1909 if (ret != LTTNG_OK) {
1910 free(attr);
1911 goto error;
1912 }
1913 channel_attr_destroy(attr);
1914 chan_ust_created = 1;
1915 }
1916
1917 ret = context_ust_add(usess, domain, ctx, channel_name);
1918 free(app_ctx_provider_name);
1919 free(app_ctx_name);
1920 app_ctx_name = NULL;
1921 app_ctx_provider_name = NULL;
1922 if (ret != LTTNG_OK) {
1923 goto error;
1924 }
1925 break;
1926 }
1927 default:
1928 ret = LTTNG_ERR_UND;
1929 goto error;
1930 }
1931
1932 ret = LTTNG_OK;
1933 goto end;
1934
1935 error:
1936 if (chan_kern_created) {
1937 struct ltt_kernel_channel *kchan =
1938 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1939 session->kernel_session);
1940 /* Created previously, this should NOT fail. */
1941 assert(kchan);
1942 kernel_destroy_channel(kchan);
1943 }
1944
1945 if (chan_ust_created) {
1946 struct ltt_ust_channel *uchan =
1947 trace_ust_find_channel_by_name(
1948 session->ust_session->domain_global.channels,
1949 DEFAULT_CHANNEL_NAME);
1950 /* Created previously, this should NOT fail. */
1951 assert(uchan);
1952 /* Remove from the channel list of the session. */
1953 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1954 uchan);
1955 trace_ust_destroy_channel(uchan);
1956 }
1957 end:
1958 free(app_ctx_provider_name);
1959 free(app_ctx_name);
1960 return ret;
1961 }
1962
1963 static inline bool name_starts_with(const char *name, const char *prefix)
1964 {
1965 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
1966
1967 return !strncmp(name, prefix, max_cmp_len);
1968 }
1969
1970 /* Perform userspace-specific event name validation */
1971 static int validate_ust_event_name(const char *name)
1972 {
1973 int ret = 0;
1974
1975 if (!name) {
1976 ret = -1;
1977 goto end;
1978 }
1979
1980 /*
1981 * Check name against all internal UST event component namespaces used
1982 * by the agents.
1983 */
1984 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
1985 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
1986 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
1987 ret = -1;
1988 }
1989
1990 end:
1991 return ret;
1992 }
1993
1994 /*
1995 * Internal version of cmd_enable_event() with a supplemental
1996 * "internal_event" flag which is used to enable internal events which should
1997 * be hidden from clients. Such events are used in the agent implementation to
1998 * enable the events through which all "agent" events are funeled.
1999 */
2000 static int _cmd_enable_event(struct ltt_session *session,
2001 const struct lttng_domain *domain,
2002 char *channel_name, struct lttng_event *event,
2003 char *filter_expression,
2004 struct lttng_filter_bytecode *filter,
2005 struct lttng_event_exclusion *exclusion,
2006 int wpipe, bool internal_event)
2007 {
2008 int ret = 0, channel_created = 0;
2009 struct lttng_channel *attr = NULL;
2010
2011 assert(session);
2012 assert(event);
2013 assert(channel_name);
2014
2015 /* If we have a filter, we must have its filter expression */
2016 assert(!(!!filter_expression ^ !!filter));
2017
2018 /* Normalize event name as a globbing pattern */
2019 strutils_normalize_star_glob_pattern(event->name);
2020
2021 /* Normalize exclusion names as globbing patterns */
2022 if (exclusion) {
2023 size_t i;
2024
2025 for (i = 0; i < exclusion->count; i++) {
2026 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2027
2028 strutils_normalize_star_glob_pattern(name);
2029 }
2030 }
2031
2032 DBG("Enable event command for event \'%s\'", event->name);
2033
2034 rcu_read_lock();
2035
2036 switch (domain->type) {
2037 case LTTNG_DOMAIN_KERNEL:
2038 {
2039 struct ltt_kernel_channel *kchan;
2040
2041 /*
2042 * If a non-default channel has been created in the
2043 * session, explicitely require that -c chan_name needs
2044 * to be provided.
2045 */
2046 if (session->kernel_session->has_non_default_channel
2047 && channel_name[0] == '\0') {
2048 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2049 goto error;
2050 }
2051
2052 kchan = trace_kernel_get_channel_by_name(channel_name,
2053 session->kernel_session);
2054 if (kchan == NULL) {
2055 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2056 LTTNG_BUFFER_GLOBAL);
2057 if (attr == NULL) {
2058 ret = LTTNG_ERR_FATAL;
2059 goto error;
2060 }
2061 if (lttng_strncpy(attr->name, channel_name,
2062 sizeof(attr->name))) {
2063 ret = LTTNG_ERR_INVALID;
2064 goto error;
2065 }
2066
2067 ret = cmd_enable_channel(session, domain, attr, wpipe);
2068 if (ret != LTTNG_OK) {
2069 goto error;
2070 }
2071 channel_created = 1;
2072 }
2073
2074 /* Get the newly created kernel channel pointer */
2075 kchan = trace_kernel_get_channel_by_name(channel_name,
2076 session->kernel_session);
2077 if (kchan == NULL) {
2078 /* This sould not happen... */
2079 ret = LTTNG_ERR_FATAL;
2080 goto error;
2081 }
2082
2083 switch (event->type) {
2084 case LTTNG_EVENT_ALL:
2085 {
2086 char *filter_expression_a = NULL;
2087 struct lttng_filter_bytecode *filter_a = NULL;
2088
2089 /*
2090 * We need to duplicate filter_expression and filter,
2091 * because ownership is passed to first enable
2092 * event.
2093 */
2094 if (filter_expression) {
2095 filter_expression_a = strdup(filter_expression);
2096 if (!filter_expression_a) {
2097 ret = LTTNG_ERR_FATAL;
2098 goto error;
2099 }
2100 }
2101 if (filter) {
2102 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2103 if (!filter_a) {
2104 free(filter_expression_a);
2105 ret = LTTNG_ERR_FATAL;
2106 goto error;
2107 }
2108 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2109 }
2110 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2111 ret = event_kernel_enable_event(kchan, event,
2112 filter_expression, filter);
2113 /* We have passed ownership */
2114 filter_expression = NULL;
2115 filter = NULL;
2116 if (ret != LTTNG_OK) {
2117 if (channel_created) {
2118 /* Let's not leak a useless channel. */
2119 kernel_destroy_channel(kchan);
2120 }
2121 free(filter_expression_a);
2122 free(filter_a);
2123 goto error;
2124 }
2125 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2126 ret = event_kernel_enable_event(kchan, event,
2127 filter_expression_a, filter_a);
2128 /* We have passed ownership */
2129 filter_expression_a = NULL;
2130 filter_a = NULL;
2131 if (ret != LTTNG_OK) {
2132 goto error;
2133 }
2134 break;
2135 }
2136 case LTTNG_EVENT_PROBE:
2137 case LTTNG_EVENT_USERSPACE_PROBE:
2138 case LTTNG_EVENT_FUNCTION:
2139 case LTTNG_EVENT_FUNCTION_ENTRY:
2140 case LTTNG_EVENT_TRACEPOINT:
2141 ret = event_kernel_enable_event(kchan, event,
2142 filter_expression, filter);
2143 /* We have passed ownership */
2144 filter_expression = NULL;
2145 filter = NULL;
2146 if (ret != LTTNG_OK) {
2147 if (channel_created) {
2148 /* Let's not leak a useless channel. */
2149 kernel_destroy_channel(kchan);
2150 }
2151 goto error;
2152 }
2153 break;
2154 case LTTNG_EVENT_SYSCALL:
2155 ret = event_kernel_enable_event(kchan, event,
2156 filter_expression, filter);
2157 /* We have passed ownership */
2158 filter_expression = NULL;
2159 filter = NULL;
2160 if (ret != LTTNG_OK) {
2161 goto error;
2162 }
2163 break;
2164 default:
2165 ret = LTTNG_ERR_UNK;
2166 goto error;
2167 }
2168
2169 kernel_wait_quiescent();
2170 break;
2171 }
2172 case LTTNG_DOMAIN_UST:
2173 {
2174 struct ltt_ust_channel *uchan;
2175 struct ltt_ust_session *usess = session->ust_session;
2176
2177 assert(usess);
2178
2179 /*
2180 * If a non-default channel has been created in the
2181 * session, explicitely require that -c chan_name needs
2182 * to be provided.
2183 */
2184 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2185 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2186 goto error;
2187 }
2188
2189 /* Get channel from global UST domain */
2190 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2191 channel_name);
2192 if (uchan == NULL) {
2193 /* Create default channel */
2194 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2195 usess->buffer_type);
2196 if (attr == NULL) {
2197 ret = LTTNG_ERR_FATAL;
2198 goto error;
2199 }
2200 if (lttng_strncpy(attr->name, channel_name,
2201 sizeof(attr->name))) {
2202 ret = LTTNG_ERR_INVALID;
2203 goto error;
2204 }
2205
2206 ret = cmd_enable_channel(session, domain, attr, wpipe);
2207 if (ret != LTTNG_OK) {
2208 goto error;
2209 }
2210
2211 /* Get the newly created channel reference back */
2212 uchan = trace_ust_find_channel_by_name(
2213 usess->domain_global.channels, channel_name);
2214 assert(uchan);
2215 }
2216
2217 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2218 /*
2219 * Don't allow users to add UST events to channels which
2220 * are assigned to a userspace subdomain (JUL, Log4J,
2221 * Python, etc.).
2222 */
2223 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2224 goto error;
2225 }
2226
2227 if (!internal_event) {
2228 /*
2229 * Ensure the event name is not reserved for internal
2230 * use.
2231 */
2232 ret = validate_ust_event_name(event->name);
2233 if (ret) {
2234 WARN("Userspace event name %s failed validation.",
2235 event->name);
2236 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2237 goto error;
2238 }
2239 }
2240
2241 /* At this point, the session and channel exist on the tracer */
2242 ret = event_ust_enable_tracepoint(usess, uchan, event,
2243 filter_expression, filter, exclusion,
2244 internal_event);
2245 /* We have passed ownership */
2246 filter_expression = NULL;
2247 filter = NULL;
2248 exclusion = NULL;
2249 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2250 goto already_enabled;
2251 } else if (ret != LTTNG_OK) {
2252 goto error;
2253 }
2254 break;
2255 }
2256 case LTTNG_DOMAIN_LOG4J:
2257 case LTTNG_DOMAIN_JUL:
2258 case LTTNG_DOMAIN_PYTHON:
2259 {
2260 const char *default_event_name, *default_chan_name;
2261 struct agent *agt;
2262 struct lttng_event uevent;
2263 struct lttng_domain tmp_dom;
2264 struct ltt_ust_session *usess = session->ust_session;
2265
2266 assert(usess);
2267
2268 if (!agent_tracing_is_enabled()) {
2269 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2270 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2271 goto error;
2272 }
2273
2274 agt = trace_ust_find_agent(usess, domain->type);
2275 if (!agt) {
2276 agt = agent_create(domain->type);
2277 if (!agt) {
2278 ret = LTTNG_ERR_NOMEM;
2279 goto error;
2280 }
2281 agent_add(agt, usess->agents);
2282 }
2283
2284 /* Create the default tracepoint. */
2285 memset(&uevent, 0, sizeof(uevent));
2286 uevent.type = LTTNG_EVENT_TRACEPOINT;
2287 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2288 default_event_name = event_get_default_agent_ust_name(
2289 domain->type);
2290 if (!default_event_name) {
2291 ret = LTTNG_ERR_FATAL;
2292 goto error;
2293 }
2294 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2295 uevent.name[sizeof(uevent.name) - 1] = '\0';
2296
2297 /*
2298 * The domain type is changed because we are about to enable the
2299 * default channel and event for the JUL domain that are hardcoded.
2300 * This happens in the UST domain.
2301 */
2302 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2303 tmp_dom.type = LTTNG_DOMAIN_UST;
2304
2305 switch (domain->type) {
2306 case LTTNG_DOMAIN_LOG4J:
2307 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2308 break;
2309 case LTTNG_DOMAIN_JUL:
2310 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2311 break;
2312 case LTTNG_DOMAIN_PYTHON:
2313 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2314 break;
2315 default:
2316 /* The switch/case we are in makes this impossible */
2317 assert(0);
2318 }
2319
2320 {
2321 char *filter_expression_copy = NULL;
2322 struct lttng_filter_bytecode *filter_copy = NULL;
2323
2324 if (filter) {
2325 const size_t filter_size = sizeof(
2326 struct lttng_filter_bytecode)
2327 + filter->len;
2328
2329 filter_copy = zmalloc(filter_size);
2330 if (!filter_copy) {
2331 ret = LTTNG_ERR_NOMEM;
2332 goto error;
2333 }
2334 memcpy(filter_copy, filter, filter_size);
2335
2336 filter_expression_copy =
2337 strdup(filter_expression);
2338 if (!filter_expression) {
2339 ret = LTTNG_ERR_NOMEM;
2340 }
2341
2342 if (!filter_expression_copy || !filter_copy) {
2343 free(filter_expression_copy);
2344 free(filter_copy);
2345 goto error;
2346 }
2347 }
2348
2349 ret = cmd_enable_event_internal(session, &tmp_dom,
2350 (char *) default_chan_name,
2351 &uevent, filter_expression_copy,
2352 filter_copy, NULL, wpipe);
2353 }
2354
2355 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2356 goto already_enabled;
2357 } else if (ret != LTTNG_OK) {
2358 goto error;
2359 }
2360
2361 /* The wild card * means that everything should be enabled. */
2362 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2363 ret = event_agent_enable_all(usess, agt, event, filter,
2364 filter_expression);
2365 } else {
2366 ret = event_agent_enable(usess, agt, event, filter,
2367 filter_expression);
2368 }
2369 filter = NULL;
2370 filter_expression = NULL;
2371 if (ret != LTTNG_OK) {
2372 goto error;
2373 }
2374
2375 break;
2376 }
2377 default:
2378 ret = LTTNG_ERR_UND;
2379 goto error;
2380 }
2381
2382 ret = LTTNG_OK;
2383
2384 already_enabled:
2385 error:
2386 free(filter_expression);
2387 free(filter);
2388 free(exclusion);
2389 channel_attr_destroy(attr);
2390 rcu_read_unlock();
2391 return ret;
2392 }
2393
2394 /*
2395 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2396 * We own filter, exclusion, and filter_expression.
2397 */
2398 int cmd_enable_event(struct ltt_session *session,
2399 const struct lttng_domain *domain,
2400 char *channel_name, struct lttng_event *event,
2401 char *filter_expression,
2402 struct lttng_filter_bytecode *filter,
2403 struct lttng_event_exclusion *exclusion,
2404 int wpipe)
2405 {
2406 return _cmd_enable_event(session, domain, channel_name, event,
2407 filter_expression, filter, exclusion, wpipe, false);
2408 }
2409
2410 /*
2411 * Enable an event which is internal to LTTng. An internal should
2412 * never be made visible to clients and are immune to checks such as
2413 * reserved names.
2414 */
2415 static int cmd_enable_event_internal(struct ltt_session *session,
2416 const struct lttng_domain *domain,
2417 char *channel_name, struct lttng_event *event,
2418 char *filter_expression,
2419 struct lttng_filter_bytecode *filter,
2420 struct lttng_event_exclusion *exclusion,
2421 int wpipe)
2422 {
2423 return _cmd_enable_event(session, domain, channel_name, event,
2424 filter_expression, filter, exclusion, wpipe, true);
2425 }
2426
2427 /*
2428 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2429 */
2430 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2431 struct lttng_event **events)
2432 {
2433 int ret;
2434 ssize_t nb_events = 0;
2435
2436 switch (domain) {
2437 case LTTNG_DOMAIN_KERNEL:
2438 nb_events = kernel_list_events(events);
2439 if (nb_events < 0) {
2440 ret = LTTNG_ERR_KERN_LIST_FAIL;
2441 goto error;
2442 }
2443 break;
2444 case LTTNG_DOMAIN_UST:
2445 nb_events = ust_app_list_events(events);
2446 if (nb_events < 0) {
2447 ret = LTTNG_ERR_UST_LIST_FAIL;
2448 goto error;
2449 }
2450 break;
2451 case LTTNG_DOMAIN_LOG4J:
2452 case LTTNG_DOMAIN_JUL:
2453 case LTTNG_DOMAIN_PYTHON:
2454 nb_events = agent_list_events(events, domain);
2455 if (nb_events < 0) {
2456 ret = LTTNG_ERR_UST_LIST_FAIL;
2457 goto error;
2458 }
2459 break;
2460 default:
2461 ret = LTTNG_ERR_UND;
2462 goto error;
2463 }
2464
2465 return nb_events;
2466
2467 error:
2468 /* Return negative value to differentiate return code */
2469 return -ret;
2470 }
2471
2472 /*
2473 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2474 */
2475 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2476 struct lttng_event_field **fields)
2477 {
2478 int ret;
2479 ssize_t nb_fields = 0;
2480
2481 switch (domain) {
2482 case LTTNG_DOMAIN_UST:
2483 nb_fields = ust_app_list_event_fields(fields);
2484 if (nb_fields < 0) {
2485 ret = LTTNG_ERR_UST_LIST_FAIL;
2486 goto error;
2487 }
2488 break;
2489 case LTTNG_DOMAIN_KERNEL:
2490 default: /* fall-through */
2491 ret = LTTNG_ERR_UND;
2492 goto error;
2493 }
2494
2495 return nb_fields;
2496
2497 error:
2498 /* Return negative value to differentiate return code */
2499 return -ret;
2500 }
2501
2502 ssize_t cmd_list_syscalls(struct lttng_event **events)
2503 {
2504 return syscall_table_list(events);
2505 }
2506
2507 /*
2508 * Command LTTNG_LIST_TRACKER_PIDS processed by the client thread.
2509 *
2510 * Called with session lock held.
2511 */
2512 ssize_t cmd_list_tracker_pids(struct ltt_session *session,
2513 enum lttng_domain_type domain, int32_t **pids)
2514 {
2515 int ret;
2516 ssize_t nr_pids = 0;
2517
2518 switch (domain) {
2519 case LTTNG_DOMAIN_KERNEL:
2520 {
2521 struct ltt_kernel_session *ksess;
2522
2523 ksess = session->kernel_session;
2524 nr_pids = kernel_list_tracker_pids(ksess, pids);
2525 if (nr_pids < 0) {
2526 ret = LTTNG_ERR_KERN_LIST_FAIL;
2527 goto error;
2528 }
2529 break;
2530 }
2531 case LTTNG_DOMAIN_UST:
2532 {
2533 struct ltt_ust_session *usess;
2534
2535 usess = session->ust_session;
2536 nr_pids = trace_ust_list_tracker_pids(usess, pids);
2537 if (nr_pids < 0) {
2538 ret = LTTNG_ERR_UST_LIST_FAIL;
2539 goto error;
2540 }
2541 break;
2542 }
2543 case LTTNG_DOMAIN_LOG4J:
2544 case LTTNG_DOMAIN_JUL:
2545 case LTTNG_DOMAIN_PYTHON:
2546 default:
2547 ret = LTTNG_ERR_UND;
2548 goto error;
2549 }
2550
2551 return nr_pids;
2552
2553 error:
2554 /* Return negative value to differentiate return code */
2555 return -ret;
2556 }
2557
2558 /*
2559 * Command LTTNG_START_TRACE processed by the client thread.
2560 *
2561 * Called with session mutex held.
2562 */
2563 int cmd_start_trace(struct ltt_session *session)
2564 {
2565 enum lttng_error_code ret;
2566 unsigned long nb_chan = 0;
2567 struct ltt_kernel_session *ksession;
2568 struct ltt_ust_session *usess;
2569 const bool session_rotated_after_last_stop =
2570 session->rotated_after_last_stop;
2571
2572 assert(session);
2573
2574 /* Ease our life a bit ;) */
2575 ksession = session->kernel_session;
2576 usess = session->ust_session;
2577
2578 /* Is the session already started? */
2579 if (session->active) {
2580 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2581 goto error;
2582 }
2583
2584 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2585 !session->current_trace_chunk) {
2586 /*
2587 * A rotation was launched while the session was stopped and
2588 * it has not been completed yet. It is not possible to start
2589 * the session since starting the session here would require a
2590 * rotation from "NULL" to a new trace chunk. That rotation
2591 * would overlap with the ongoing rotation, which is not
2592 * supported.
2593 */
2594 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2595 session->name);
2596 ret = LTTNG_ERR_ROTATION_PENDING;
2597 goto error;
2598 }
2599
2600 /*
2601 * Starting a session without channel is useless since after that it's not
2602 * possible to enable channel thus inform the client.
2603 */
2604 if (usess && usess->domain_global.channels) {
2605 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2606 }
2607 if (ksession) {
2608 nb_chan += ksession->channel_count;
2609 }
2610 if (!nb_chan) {
2611 ret = LTTNG_ERR_NO_CHANNEL;
2612 goto error;
2613 }
2614
2615 session->active = 1;
2616 session->rotated_after_last_stop = false;
2617 if (session->output_traces && !session->current_trace_chunk) {
2618 if (!session->has_been_started) {
2619 struct lttng_trace_chunk *trace_chunk;
2620
2621 DBG("Creating initial trace chunk of session \"%s\"",
2622 session->name);
2623 trace_chunk = session_create_new_trace_chunk(
2624 session, NULL, NULL, NULL);
2625 if (!trace_chunk) {
2626 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2627 goto error;
2628 }
2629 assert(!session->current_trace_chunk);
2630 ret = session_set_trace_chunk(session, trace_chunk,
2631 NULL);
2632 lttng_trace_chunk_put(trace_chunk);
2633 if (ret) {
2634 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2635 goto error;
2636 }
2637 } else {
2638 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2639 session->name);
2640 /*
2641 * Rotate existing streams into the new chunk.
2642 * This is a "quiet" rotation has no client has
2643 * explicitly requested this operation.
2644 *
2645 * There is also no need to wait for the rotation
2646 * to complete as it will happen immediately. No data
2647 * was produced as the session was stopped, so the
2648 * rotation should happen on reception of the command.
2649 */
2650 ret = cmd_rotate_session(session, NULL, true);
2651 if (ret != LTTNG_OK) {
2652 goto error;
2653 }
2654 }
2655 }
2656
2657 /* Kernel tracing */
2658 if (ksession != NULL) {
2659 DBG("Start kernel tracing session %s", session->name);
2660 ret = start_kernel_session(ksession);
2661 if (ret != LTTNG_OK) {
2662 goto error;
2663 }
2664 }
2665
2666 /* Flag session that trace should start automatically */
2667 if (usess) {
2668 int int_ret = ust_app_start_trace_all(usess);
2669
2670 if (int_ret < 0) {
2671 ret = LTTNG_ERR_UST_START_FAIL;
2672 goto error;
2673 }
2674 }
2675
2676 /*
2677 * Clear the flag that indicates that a rotation was done while the
2678 * session was stopped.
2679 */
2680 session->rotated_after_last_stop = false;
2681
2682 if (session->rotate_timer_period) {
2683 int int_ret = timer_session_rotation_schedule_timer_start(
2684 session, session->rotate_timer_period);
2685
2686 if (int_ret < 0) {
2687 ERR("Failed to enable rotate timer");
2688 ret = LTTNG_ERR_UNK;
2689 goto error;
2690 }
2691 }
2692
2693 ret = LTTNG_OK;
2694
2695 error:
2696 if (ret == LTTNG_OK) {
2697 /* Flag this after a successful start. */
2698 session->has_been_started |= 1;
2699 } else {
2700 session->active = 0;
2701 /* Restore initial state on error. */
2702 session->rotated_after_last_stop =
2703 session_rotated_after_last_stop;
2704 }
2705 return ret;
2706 }
2707
2708 /*
2709 * Command LTTNG_STOP_TRACE processed by the client thread.
2710 */
2711 int cmd_stop_trace(struct ltt_session *session)
2712 {
2713 int ret;
2714 struct ltt_kernel_channel *kchan;
2715 struct ltt_kernel_session *ksession;
2716 struct ltt_ust_session *usess;
2717 bool error_occurred = false;
2718
2719 assert(session);
2720
2721 DBG("Begin stop session %s (id %" PRIu64 ")", session->name, session->id);
2722 /* Short cut */
2723 ksession = session->kernel_session;
2724 usess = session->ust_session;
2725
2726 /* Session is not active. Skip everythong and inform the client. */
2727 if (!session->active) {
2728 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2729 goto error;
2730 }
2731
2732 /* Kernel tracer */
2733 if (ksession && ksession->active) {
2734 DBG("Stop kernel tracing");
2735
2736 ret = kernel_stop_session(ksession);
2737 if (ret < 0) {
2738 ret = LTTNG_ERR_KERN_STOP_FAIL;
2739 goto error;
2740 }
2741
2742 kernel_wait_quiescent();
2743
2744 /* Flush metadata after stopping (if exists) */
2745 if (ksession->metadata_stream_fd >= 0) {
2746 ret = kernel_metadata_flush_buffer(ksession->metadata_stream_fd);
2747 if (ret < 0) {
2748 ERR("Kernel metadata flush failed");
2749 error_occurred = true;
2750 }
2751 }
2752
2753 /* Flush all buffers after stopping */
2754 cds_list_for_each_entry(kchan, &ksession->channel_list.head, list) {
2755 ret = kernel_flush_buffer(kchan);
2756 if (ret < 0) {
2757 ERR("Kernel flush buffer error");
2758 error_occurred = true;
2759 }
2760 }
2761
2762 ksession->active = 0;
2763 DBG("Kernel session stopped %s (id %" PRIu64 ")", session->name,
2764 session->id);
2765 }
2766
2767 if (usess && usess->active) {
2768 ret = ust_app_stop_trace_all(usess);
2769 if (ret < 0) {
2770 ret = LTTNG_ERR_UST_STOP_FAIL;
2771 goto error;
2772 }
2773 }
2774
2775 /* Flag inactive after a successful stop. */
2776 session->active = 0;
2777 ret = !error_occurred ? LTTNG_OK : LTTNG_ERR_UNK;
2778
2779 error:
2780 return ret;
2781 }
2782
2783 /*
2784 * Set the base_path of the session only if subdir of a control uris is set.
2785 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2786 */
2787 static int set_session_base_path_from_uris(struct ltt_session *session,
2788 size_t nb_uri,
2789 struct lttng_uri *uris)
2790 {
2791 int ret;
2792 size_t i;
2793
2794 for (i = 0; i < nb_uri; i++) {
2795 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2796 uris[i].subdir[0] == '\0') {
2797 /* Not interested in these URIs */
2798 continue;
2799 }
2800
2801 if (session->base_path != NULL) {
2802 free(session->base_path);
2803 session->base_path = NULL;
2804 }
2805
2806 /* Set session base_path */
2807 session->base_path = strdup(uris[i].subdir);
2808 if (!session->base_path) {
2809 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2810 uris[i].subdir, session->name);
2811 ret = LTTNG_ERR_NOMEM;
2812 goto error;
2813 }
2814 DBG2("Setting base path \"%s\" for session \"%s\"",
2815 session->base_path, session->name);
2816 }
2817 ret = LTTNG_OK;
2818 error:
2819 return ret;
2820 }
2821
2822 /*
2823 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2824 */
2825 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2826 struct lttng_uri *uris)
2827 {
2828 int ret, i;
2829 struct ltt_kernel_session *ksess = session->kernel_session;
2830 struct ltt_ust_session *usess = session->ust_session;
2831
2832 assert(session);
2833 assert(uris);
2834 assert(nb_uri > 0);
2835
2836 /* Can't set consumer URI if the session is active. */
2837 if (session->active) {
2838 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2839 goto error;
2840 }
2841
2842 /*
2843 * Set the session base path if any. This is done inside
2844 * cmd_set_consumer_uri to preserve backward compatibility of the
2845 * previous session creation api vs the session descriptor api.
2846 */
2847 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2848 if (ret != LTTNG_OK) {
2849 goto error;
2850 }
2851
2852 /* Set the "global" consumer URIs */
2853 for (i = 0; i < nb_uri; i++) {
2854 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2855 LTTNG_DOMAIN_NONE);
2856 if (ret != LTTNG_OK) {
2857 goto error;
2858 }
2859 }
2860
2861 /* Set UST session URIs */
2862 if (session->ust_session) {
2863 for (i = 0; i < nb_uri; i++) {
2864 ret = add_uri_to_consumer(session,
2865 session->ust_session->consumer,
2866 &uris[i], LTTNG_DOMAIN_UST);
2867 if (ret != LTTNG_OK) {
2868 goto error;
2869 }
2870 }
2871 }
2872
2873 /* Set kernel session URIs */
2874 if (session->kernel_session) {
2875 for (i = 0; i < nb_uri; i++) {
2876 ret = add_uri_to_consumer(session,
2877 session->kernel_session->consumer,
2878 &uris[i], LTTNG_DOMAIN_KERNEL);
2879 if (ret != LTTNG_OK) {
2880 goto error;
2881 }
2882 }
2883 }
2884
2885 /*
2886 * Make sure to set the session in output mode after we set URI since a
2887 * session can be created without URL (thus flagged in no output mode).
2888 */
2889 session->output_traces = 1;
2890 if (ksess) {
2891 ksess->output_traces = 1;
2892 }
2893
2894 if (usess) {
2895 usess->output_traces = 1;
2896 }
2897
2898 /* All good! */
2899 ret = LTTNG_OK;
2900
2901 error:
2902 return ret;
2903 }
2904
2905 static
2906 enum lttng_error_code set_session_output_from_descriptor(
2907 struct ltt_session *session,
2908 const struct lttng_session_descriptor *descriptor)
2909 {
2910 int ret;
2911 enum lttng_error_code ret_code = LTTNG_OK;
2912 enum lttng_session_descriptor_type session_type =
2913 lttng_session_descriptor_get_type(descriptor);
2914 enum lttng_session_descriptor_output_type output_type =
2915 lttng_session_descriptor_get_output_type(descriptor);
2916 struct lttng_uri uris[2] = {};
2917 size_t uri_count = 0;
2918
2919 switch (output_type) {
2920 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
2921 goto end;
2922 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
2923 lttng_session_descriptor_get_local_output_uri(descriptor,
2924 &uris[0]);
2925 uri_count = 1;
2926 break;
2927 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
2928 lttng_session_descriptor_get_network_output_uris(descriptor,
2929 &uris[0], &uris[1]);
2930 uri_count = 2;
2931 break;
2932 default:
2933 ret_code = LTTNG_ERR_INVALID;
2934 goto end;
2935 }
2936
2937 switch (session_type) {
2938 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
2939 {
2940 struct snapshot_output *new_output = NULL;
2941
2942 new_output = snapshot_output_alloc();
2943 if (!new_output) {
2944 ret_code = LTTNG_ERR_NOMEM;
2945 goto end;
2946 }
2947
2948 ret = snapshot_output_init_with_uri(session,
2949 DEFAULT_SNAPSHOT_MAX_SIZE,
2950 NULL, uris, uri_count, session->consumer,
2951 new_output, &session->snapshot);
2952 if (ret < 0) {
2953 ret_code = (ret == -ENOMEM) ?
2954 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
2955 snapshot_output_destroy(new_output);
2956 goto end;
2957 }
2958 snapshot_add_output(&session->snapshot, new_output);
2959 break;
2960 }
2961 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
2962 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
2963 {
2964 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
2965 break;
2966 }
2967 default:
2968 ret_code = LTTNG_ERR_INVALID;
2969 goto end;
2970 }
2971 end:
2972 return ret_code;
2973 }
2974
2975 static
2976 enum lttng_error_code cmd_create_session_from_descriptor(
2977 struct lttng_session_descriptor *descriptor,
2978 const lttng_sock_cred *creds,
2979 const char *home_path)
2980 {
2981 int ret;
2982 enum lttng_error_code ret_code;
2983 const char *session_name;
2984 struct ltt_session *new_session = NULL;
2985 enum lttng_session_descriptor_status descriptor_status;
2986
2987 session_lock_list();
2988 if (home_path) {
2989 if (*home_path != '/') {
2990 ERR("Home path provided by client is not absolute");
2991 ret_code = LTTNG_ERR_INVALID;
2992 goto end;
2993 }
2994 }
2995
2996 descriptor_status = lttng_session_descriptor_get_session_name(
2997 descriptor, &session_name);
2998 switch (descriptor_status) {
2999 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3000 break;
3001 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3002 session_name = NULL;
3003 break;
3004 default:
3005 ret_code = LTTNG_ERR_INVALID;
3006 goto end;
3007 }
3008
3009 ret_code = session_create(session_name, creds->uid, creds->gid,
3010 &new_session);
3011 if (ret_code != LTTNG_OK) {
3012 goto end;
3013 }
3014
3015 if (!session_name) {
3016 ret = lttng_session_descriptor_set_session_name(descriptor,
3017 new_session->name);
3018 if (ret) {
3019 ret_code = LTTNG_ERR_SESSION_FAIL;
3020 goto end;
3021 }
3022 }
3023
3024 if (!lttng_session_descriptor_is_output_destination_initialized(
3025 descriptor)) {
3026 /*
3027 * Only include the session's creation time in the output
3028 * destination if the name of the session itself was
3029 * not auto-generated.
3030 */
3031 ret_code = lttng_session_descriptor_set_default_output(
3032 descriptor,
3033 session_name ? &new_session->creation_time : NULL,
3034 home_path);
3035 if (ret_code != LTTNG_OK) {
3036 goto end;
3037 }
3038 } else {
3039 new_session->has_user_specified_directory =
3040 lttng_session_descriptor_has_output_directory(
3041 descriptor);
3042 }
3043
3044 switch (lttng_session_descriptor_get_type(descriptor)) {
3045 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3046 new_session->snapshot_mode = 1;
3047 break;
3048 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3049 new_session->live_timer =
3050 lttng_session_descriptor_live_get_timer_interval(
3051 descriptor);
3052 break;
3053 default:
3054 break;
3055 }
3056
3057 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3058 if (ret_code != LTTNG_OK) {
3059 goto end;
3060 }
3061 new_session->consumer->enabled = 1;
3062 ret_code = LTTNG_OK;
3063 end:
3064 /* Release reference provided by the session_create function. */
3065 session_put(new_session);
3066 if (ret_code != LTTNG_OK && new_session) {
3067 /* Release the global reference on error. */
3068 session_destroy(new_session);
3069 }
3070 session_unlock_list();
3071 return ret_code;
3072 }
3073
3074 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3075 struct lttng_session_descriptor **return_descriptor)
3076 {
3077 int ret;
3078 size_t payload_size;
3079 struct lttng_dynamic_buffer payload;
3080 struct lttng_buffer_view home_dir_view;
3081 struct lttng_buffer_view session_descriptor_view;
3082 struct lttng_session_descriptor *session_descriptor = NULL;
3083 enum lttng_error_code ret_code;
3084
3085 lttng_dynamic_buffer_init(&payload);
3086 if (cmd_ctx->lsm->u.create_session.home_dir_size >=
3087 LTTNG_PATH_MAX) {
3088 ret_code = LTTNG_ERR_INVALID;
3089 goto error;
3090 }
3091 if (cmd_ctx->lsm->u.create_session.session_descriptor_size >
3092 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3093 ret_code = LTTNG_ERR_INVALID;
3094 goto error;
3095 }
3096
3097 payload_size = cmd_ctx->lsm->u.create_session.home_dir_size +
3098 cmd_ctx->lsm->u.create_session.session_descriptor_size;
3099 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3100 if (ret) {
3101 ret_code = LTTNG_ERR_NOMEM;
3102 goto error;
3103 }
3104
3105 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3106 if (ret <= 0) {
3107 ERR("Reception of session descriptor failed, aborting.");
3108 ret_code = LTTNG_ERR_SESSION_FAIL;
3109 goto error;
3110 }
3111
3112 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3113 &payload,
3114 0,
3115 cmd_ctx->lsm->u.create_session.home_dir_size);
3116 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3117 &payload,
3118 cmd_ctx->lsm->u.create_session.home_dir_size,
3119 cmd_ctx->lsm->u.create_session.session_descriptor_size);
3120
3121 ret = lttng_session_descriptor_create_from_buffer(
3122 &session_descriptor_view, &session_descriptor);
3123 if (ret < 0) {
3124 ERR("Failed to create session descriptor from payload of \"create session\" command");
3125 ret_code = LTTNG_ERR_INVALID;
3126 goto error;
3127 }
3128
3129 /*
3130 * Sets the descriptor's auto-generated properties (name, output) if
3131 * needed.
3132 */
3133 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3134 &cmd_ctx->creds,
3135 home_dir_view.size ? home_dir_view.data : NULL);
3136 if (ret_code != LTTNG_OK) {
3137 goto error;
3138 }
3139
3140 ret_code = LTTNG_OK;
3141 *return_descriptor = session_descriptor;
3142 session_descriptor = NULL;
3143 error:
3144 lttng_dynamic_buffer_reset(&payload);
3145 lttng_session_descriptor_destroy(session_descriptor);
3146 return ret_code;
3147 }
3148
3149 static
3150 void cmd_destroy_session_reply(const struct ltt_session *session,
3151 void *_reply_context)
3152 {
3153 int ret;
3154 ssize_t comm_ret;
3155 const struct cmd_destroy_session_reply_context *reply_context =
3156 _reply_context;
3157 struct lttng_dynamic_buffer payload;
3158 struct lttcomm_session_destroy_command_header cmd_header;
3159 struct lttng_trace_archive_location *location = NULL;
3160 struct lttcomm_lttng_msg llm = {
3161 .cmd_type = LTTNG_DESTROY_SESSION,
3162 .ret_code = reply_context->destruction_status,
3163 .pid = UINT32_MAX,
3164 .cmd_header_size =
3165 sizeof(struct lttcomm_session_destroy_command_header),
3166 .data_size = 0,
3167 };
3168 size_t payload_size_before_location;
3169
3170 lttng_dynamic_buffer_init(&payload);
3171
3172 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3173 if (ret) {
3174 ERR("Failed to append session destruction message");
3175 goto error;
3176 }
3177
3178 cmd_header.rotation_state =
3179 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3180 session->rotation_state :
3181 LTTNG_ROTATION_STATE_NO_ROTATION);
3182 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3183 sizeof(cmd_header));
3184 if (ret) {
3185 ERR("Failed to append session destruction command header");
3186 goto error;
3187 }
3188
3189 if (!reply_context->implicit_rotation_on_destroy) {
3190 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3191 session->name);
3192 goto send_reply;
3193 }
3194 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3195 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3196 session->name);
3197 goto send_reply;
3198 }
3199
3200 location = session_get_trace_archive_location(session);
3201 if (!location) {
3202 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3203 session->name);
3204 goto error;
3205 }
3206
3207 payload_size_before_location = payload.size;
3208 comm_ret = lttng_trace_archive_location_serialize(location,
3209 &payload);
3210 if (comm_ret < 0) {
3211 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3212 session->name);
3213 goto error;
3214 }
3215 /* Update the message to indicate the location's length. */
3216 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3217 payload.size - payload_size_before_location;
3218 send_reply:
3219 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd,
3220 payload.data, payload.size);
3221 if (comm_ret != (ssize_t) payload.size) {
3222 ERR("Failed to send result of the destruction of session \"%s\" to client",
3223 session->name);
3224 }
3225 error:
3226 ret = close(reply_context->reply_sock_fd);
3227 if (ret) {
3228 PERROR("Failed to close client socket in deferred session destroy reply");
3229 }
3230 lttng_dynamic_buffer_reset(&payload);
3231 free(_reply_context);
3232 }
3233
3234 /*
3235 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3236 *
3237 * Called with session lock held.
3238 */
3239 int cmd_destroy_session(struct ltt_session *session,
3240 struct notification_thread_handle *notification_thread_handle,
3241 int *sock_fd)
3242 {
3243 int ret;
3244 enum lttng_error_code destruction_last_error = LTTNG_OK;
3245 struct cmd_destroy_session_reply_context *reply_context = NULL;
3246
3247 if (sock_fd) {
3248 reply_context = zmalloc(sizeof(*reply_context));
3249 if (!reply_context) {
3250 ret = LTTNG_ERR_NOMEM;
3251 goto end;
3252 }
3253 reply_context->reply_sock_fd = *sock_fd;
3254 }
3255
3256 /* Safety net */
3257 assert(session);
3258
3259 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name,
3260 session->id);
3261 if (session->active) {
3262 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3263 session->name);
3264 ret = cmd_stop_trace(session);
3265 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3266 /* Carry on with the destruction of the session. */
3267 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3268 session->name, lttng_strerror(-ret));
3269 destruction_last_error = ret;
3270 }
3271 }
3272
3273 if (session->rotation_schedule_timer_enabled) {
3274 if (timer_session_rotation_schedule_timer_stop(
3275 session)) {
3276 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3277 session->name);
3278 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
3279 }
3280 }
3281
3282 if (session->rotate_size) {
3283 unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
3284 session->rotate_size = 0;
3285 }
3286
3287 if (session->most_recent_chunk_id.is_set &&
3288 session->most_recent_chunk_id.value != 0 &&
3289 session->current_trace_chunk && session->output_traces) {
3290 /*
3291 * Perform a last rotation on destruction if rotations have
3292 * occurred during the session's lifetime.
3293 */
3294 ret = cmd_rotate_session(session, NULL, false);
3295 if (ret != LTTNG_OK) {
3296 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3297 session->name, lttng_strerror(-ret));
3298 destruction_last_error = -ret;
3299 }
3300 if (reply_context) {
3301 reply_context->implicit_rotation_on_destroy = true;
3302 }
3303 } else if (session->has_been_started && session->current_trace_chunk) {
3304 /*
3305 * The user has not triggered a session rotation. However, to
3306 * ensure all data has been consumed, the session is rotated
3307 * to a 'null' trace chunk before it is destroyed.
3308 *
3309 * This is a "quiet" rotation meaning that no notification is
3310 * emitted and no renaming of the current trace chunk takes
3311 * place.
3312 */
3313 ret = cmd_rotate_session(session, NULL, true);
3314 if (ret != LTTNG_OK) {
3315 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3316 session->name, lttng_strerror(-ret));
3317 destruction_last_error = -ret;
3318 }
3319 }
3320
3321 if (session->shm_path[0]) {
3322 /*
3323 * When a session is created with an explicit shm_path,
3324 * the consumer daemon will create its shared memory files
3325 * at that location and will *not* unlink them. This is normal
3326 * as the intention of that feature is to make it possible
3327 * to retrieve the content of those files should a crash occur.
3328 *
3329 * To ensure the content of those files can be used, the
3330 * sessiond daemon will replicate the content of the metadata
3331 * cache in a metadata file.
3332 *
3333 * On clean-up, it is expected that the consumer daemon will
3334 * unlink the shared memory files and that the session daemon
3335 * will unlink the metadata file. Then, the session's directory
3336 * in the shm path can be removed.
3337 *
3338 * Unfortunately, a flaw in the design of the sessiond's and
3339 * consumerd's tear down of channels makes it impossible to
3340 * determine when the sessiond _and_ the consumerd have both
3341 * destroyed their representation of a channel. For one, the
3342 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3343 * callbacks in both daemons.
3344 *
3345 * However, it is also impossible for the sessiond to know when
3346 * the consumer daemon is done destroying its channel(s) since
3347 * it occurs as a reaction to the closing of the channel's file
3348 * descriptor. There is no resulting communication initiated
3349 * from the consumerd to the sessiond to confirm that the
3350 * operation is completed (and was successful).
3351 *
3352 * Until this is all fixed, the session daemon checks for the
3353 * removal of the session's shm path which makes it possible
3354 * to safely advertise a session as having been destroyed.
3355 *
3356 * Prior to this fix, it was not possible to reliably save
3357 * a session making use of the --shm-path option, destroy it,
3358 * and load it again. This is because the creation of the
3359 * session would fail upon seeing the session's shm path
3360 * already in existence.
3361 *
3362 * Note that none of the error paths in the check for the
3363 * directory's existence return an error. This is normal
3364 * as there isn't much that can be done. The session will
3365 * be destroyed properly, except that we can't offer the
3366 * guarantee that the same session can be re-created.
3367 */
3368 current_completion_handler = &destroy_completion_handler.handler;
3369 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3370 session->shm_path,
3371 sizeof(destroy_completion_handler.shm_path));
3372 assert(!ret);
3373 }
3374
3375 /*
3376 * The session is destroyed. However, note that the command context
3377 * still holds a reference to the session, thus delaying its destruction
3378 * _at least_ up to the point when that reference is released.
3379 */
3380 session_destroy(session);
3381 if (reply_context) {
3382 reply_context->destruction_status = destruction_last_error;
3383 ret = session_add_destroy_notifier(session,
3384 cmd_destroy_session_reply,
3385 (void *) reply_context);
3386 if (ret) {
3387 ret = LTTNG_ERR_FATAL;
3388 goto end;
3389 } else {
3390 *sock_fd = -1;
3391 }
3392 }
3393 ret = LTTNG_OK;
3394 end:
3395 return ret;
3396 }
3397
3398 /*
3399 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3400 */
3401 int cmd_register_consumer(struct ltt_session *session,
3402 enum lttng_domain_type domain, const char *sock_path,
3403 struct consumer_data *cdata)
3404 {
3405 int ret, sock;
3406 struct consumer_socket *socket = NULL;
3407
3408 assert(session);
3409 assert(cdata);
3410 assert(sock_path);
3411
3412 switch (domain) {
3413 case LTTNG_DOMAIN_KERNEL:
3414 {
3415 struct ltt_kernel_session *ksess = session->kernel_session;
3416
3417 assert(ksess);
3418
3419 /* Can't register a consumer if there is already one */
3420 if (ksess->consumer_fds_sent != 0) {
3421 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3422 goto error;
3423 }
3424
3425 sock = lttcomm_connect_unix_sock(sock_path);
3426 if (sock < 0) {
3427 ret = LTTNG_ERR_CONNECT_FAIL;
3428 goto error;
3429 }
3430 cdata->cmd_sock = sock;
3431
3432 socket = consumer_allocate_socket(&cdata->cmd_sock);
3433 if (socket == NULL) {
3434 ret = close(sock);
3435 if (ret < 0) {
3436 PERROR("close register consumer");
3437 }
3438 cdata->cmd_sock = -1;
3439 ret = LTTNG_ERR_FATAL;
3440 goto error;
3441 }
3442
3443 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3444 if (socket->lock == NULL) {
3445 PERROR("zmalloc pthread mutex");
3446 ret = LTTNG_ERR_FATAL;
3447 goto error;
3448 }
3449 pthread_mutex_init(socket->lock, NULL);
3450 socket->registered = 1;
3451
3452 rcu_read_lock();
3453 consumer_add_socket(socket, ksess->consumer);
3454 rcu_read_unlock();
3455
3456 pthread_mutex_lock(&cdata->pid_mutex);
3457 cdata->pid = -1;
3458 pthread_mutex_unlock(&cdata->pid_mutex);
3459
3460 break;
3461 }
3462 default:
3463 /* TODO: Userspace tracing */
3464 ret = LTTNG_ERR_UND;
3465 goto error;
3466 }
3467
3468 return LTTNG_OK;
3469
3470 error:
3471 if (socket) {
3472 consumer_destroy_socket(socket);
3473 }
3474 return ret;
3475 }
3476
3477 /*
3478 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3479 */
3480 ssize_t cmd_list_domains(struct ltt_session *session,
3481 struct lttng_domain **domains)
3482 {
3483 int ret, index = 0;
3484 ssize_t nb_dom = 0;
3485 struct agent *agt;
3486 struct lttng_ht_iter iter;
3487
3488 if (session->kernel_session != NULL) {
3489 DBG3("Listing domains found kernel domain");
3490 nb_dom++;
3491 }
3492
3493 if (session->ust_session != NULL) {
3494 DBG3("Listing domains found UST global domain");
3495 nb_dom++;
3496
3497 rcu_read_lock();
3498 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3499 agt, node.node) {
3500 if (agt->being_used) {
3501 nb_dom++;
3502 }
3503 }
3504 rcu_read_unlock();
3505 }
3506
3507 if (!nb_dom) {
3508 goto end;
3509 }
3510
3511 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3512 if (*domains == NULL) {
3513 ret = LTTNG_ERR_FATAL;
3514 goto error;
3515 }
3516
3517 if (session->kernel_session != NULL) {
3518 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3519
3520 /* Kernel session buffer type is always GLOBAL */
3521 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3522
3523 index++;
3524 }
3525
3526 if (session->ust_session != NULL) {
3527 (*domains)[index].type = LTTNG_DOMAIN_UST;
3528 (*domains)[index].buf_type = session->ust_session->buffer_type;
3529 index++;
3530
3531 rcu_read_lock();
3532 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3533 agt, node.node) {
3534 if (agt->being_used) {
3535 (*domains)[index].type = agt->domain;
3536 (*domains)[index].buf_type = session->ust_session->buffer_type;
3537 index++;
3538 }
3539 }
3540 rcu_read_unlock();
3541 }
3542 end:
3543 return nb_dom;
3544
3545 error:
3546 /* Return negative value to differentiate return code */
3547 return -ret;
3548 }
3549
3550
3551 /*
3552 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3553 */
3554 ssize_t cmd_list_channels(enum lttng_domain_type domain,
3555 struct ltt_session *session, struct lttng_channel **channels)
3556 {
3557 ssize_t nb_chan = 0, payload_size = 0, ret;
3558
3559 switch (domain) {
3560 case LTTNG_DOMAIN_KERNEL:
3561 if (session->kernel_session != NULL) {
3562 nb_chan = session->kernel_session->channel_count;
3563 }
3564 DBG3("Number of kernel channels %zd", nb_chan);
3565 if (nb_chan <= 0) {
3566 ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND;
3567 goto end;
3568 }
3569 break;
3570 case LTTNG_DOMAIN_UST:
3571 if (session->ust_session != NULL) {
3572 rcu_read_lock();
3573 nb_chan = lttng_ht_get_count(
3574 session->ust_session->domain_global.channels);
3575 rcu_read_unlock();
3576 }
3577 DBG3("Number of UST global channels %zd", nb_chan);
3578 if (nb_chan < 0) {
3579 ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND;
3580 goto end;
3581 }
3582 break;
3583 default:
3584 ret = -LTTNG_ERR_UND;
3585 goto end;
3586 }
3587
3588 if (nb_chan > 0) {
3589 const size_t channel_size = sizeof(struct lttng_channel) +
3590 sizeof(struct lttng_channel_extended);
3591 struct lttng_channel_extended *channel_exts;
3592
3593 payload_size = nb_chan * channel_size;
3594 *channels = zmalloc(payload_size);
3595 if (*channels == NULL) {
3596 ret = -LTTNG_ERR_FATAL;
3597 goto end;
3598 }
3599
3600 channel_exts = ((void *) *channels) +
3601 (nb_chan * sizeof(struct lttng_channel));
3602 ret = list_lttng_channels(domain, session, *channels, channel_exts);
3603 if (ret != LTTNG_OK) {
3604 free(*channels);
3605 *channels = NULL;
3606 goto end;
3607 }
3608 } else {
3609 *channels = NULL;
3610 }
3611
3612 ret = payload_size;
3613 end:
3614 return ret;
3615 }
3616
3617 /*
3618 * Command LTTNG_LIST_EVENTS processed by the client thread.
3619 */
3620 ssize_t cmd_list_events(enum lttng_domain_type domain,
3621 struct ltt_session *session, char *channel_name,
3622 struct lttng_event **events, size_t *total_size)
3623 {
3624 int ret = 0;
3625 ssize_t nb_event = 0;
3626
3627 switch (domain) {
3628 case LTTNG_DOMAIN_KERNEL:
3629 if (session->kernel_session != NULL) {
3630 nb_event = list_lttng_kernel_events(channel_name,
3631 session->kernel_session, events,
3632 total_size);
3633 }
3634 break;
3635 case LTTNG_DOMAIN_UST:
3636 {
3637 if (session->ust_session != NULL) {
3638 nb_event = list_lttng_ust_global_events(channel_name,
3639 &session->ust_session->domain_global, events,
3640 total_size);
3641 }
3642 break;
3643 }
3644 case LTTNG_DOMAIN_LOG4J:
3645 case LTTNG_DOMAIN_JUL:
3646 case LTTNG_DOMAIN_PYTHON:
3647 if (session->ust_session) {
3648 struct lttng_ht_iter iter;
3649 struct agent *agt;
3650
3651 rcu_read_lock();
3652 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3653 &iter.iter, agt, node.node) {
3654 if (agt->domain == domain) {
3655 nb_event = list_lttng_agent_events(
3656 agt, events,
3657 total_size);
3658 break;
3659 }
3660 }
3661 rcu_read_unlock();
3662 }
3663 break;
3664 default:
3665 ret = LTTNG_ERR_UND;
3666 goto error;
3667 }
3668
3669 return nb_event;
3670
3671 error:
3672 /* Return negative value to differentiate return code */
3673 return -ret;
3674 }
3675
3676 /*
3677 * Using the session list, filled a lttng_session array to send back to the
3678 * client for session listing.
3679 *
3680 * The session list lock MUST be acquired before calling this function. Use
3681 * session_lock_list() and session_unlock_list().
3682 */
3683 void cmd_list_lttng_sessions(struct lttng_session *sessions,
3684 size_t session_count, uid_t uid, gid_t gid)
3685 {
3686 int ret;
3687 unsigned int i = 0;
3688 struct ltt_session *session;
3689 struct ltt_session_list *list = session_get_list();
3690 struct lttng_session_extended *extended =
3691 (typeof(extended)) (&sessions[session_count]);
3692
3693 DBG("Getting all available session for UID %d GID %d",
3694 uid, gid);
3695 /*
3696 * Iterate over session list and append data after the control struct in
3697 * the buffer.
3698 */
3699 cds_list_for_each_entry(session, &list->head, list) {
3700 if (!session_get(session)) {
3701 continue;
3702 }
3703 /*
3704 * Only list the sessions the user can control.
3705 */
3706 if (!session_access_ok(session, uid, gid) ||
3707 session->destroyed) {
3708 session_put(session);
3709 continue;
3710 }
3711
3712 struct ltt_kernel_session *ksess = session->kernel_session;
3713 struct ltt_ust_session *usess = session->ust_session;
3714
3715 if (session->consumer->type == CONSUMER_DST_NET ||
3716 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3717 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3718 ret = build_network_session_path(sessions[i].path,
3719 sizeof(sessions[i].path), session);
3720 } else {
3721 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
3722 session->consumer->dst.session_root_path);
3723 }
3724 if (ret < 0) {
3725 PERROR("snprintf session path");
3726 session_put(session);
3727 continue;
3728 }
3729
3730 strncpy(sessions[i].name, session->name, NAME_MAX);
3731 sessions[i].name[NAME_MAX - 1] = '\0';
3732 sessions[i].enabled = session->active;
3733 sessions[i].snapshot_mode = session->snapshot_mode;
3734 sessions[i].live_timer_interval = session->live_timer;
3735 extended[i].creation_time.value = (uint64_t) session->creation_time;
3736 extended[i].creation_time.is_set = 1;
3737 i++;
3738 session_put(session);
3739 }
3740 }
3741
3742 /*
3743 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3744 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3745 */
3746 int cmd_data_pending(struct ltt_session *session)
3747 {
3748 int ret;
3749 struct ltt_kernel_session *ksess = session->kernel_session;
3750 struct ltt_ust_session *usess = session->ust_session;
3751
3752 assert(session);
3753
3754 DBG("Data pending for session %s", session->name);
3755
3756 /* Session MUST be stopped to ask for data availability. */
3757 if (session->active) {
3758 ret = LTTNG_ERR_SESSION_STARTED;
3759 goto error;
3760 } else {
3761 /*
3762 * If stopped, just make sure we've started before else the above call
3763 * will always send that there is data pending.
3764 *
3765 * The consumer assumes that when the data pending command is received,
3766 * the trace has been started before or else no output data is written
3767 * by the streams which is a condition for data pending. So, this is
3768 * *VERY* important that we don't ask the consumer before a start
3769 * trace.
3770 */
3771 if (!session->has_been_started) {
3772 ret = 0;
3773 goto error;
3774 }
3775 }
3776
3777 /* A rotation is still pending, we have to wait. */
3778 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
3779 DBG("Rotate still pending for session %s", session->name);
3780 ret = 1;
3781 goto error;
3782 }
3783
3784 if (ksess && ksess->consumer) {
3785 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
3786 if (ret == 1) {
3787 /* Data is still being extracted for the kernel. */
3788 goto error;
3789 }
3790 }
3791
3792 if (usess && usess->consumer) {
3793 ret = consumer_is_data_pending(usess->id, usess->consumer);
3794 if (ret == 1) {
3795 /* Data is still being extracted for the kernel. */
3796 goto error;
3797 }
3798 }
3799
3800 /* Data is ready to be read by a viewer */
3801 ret = 0;
3802
3803 error:
3804 return ret;
3805 }
3806
3807 /*
3808 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
3809 *
3810 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3811 */
3812 int cmd_snapshot_add_output(struct ltt_session *session,
3813 const struct lttng_snapshot_output *output, uint32_t *id)
3814 {
3815 int ret;
3816 struct snapshot_output *new_output;
3817
3818 assert(session);
3819 assert(output);
3820
3821 DBG("Cmd snapshot add output for session %s", session->name);
3822
3823 /*
3824 * Can't create an output if the session is not set in no-output mode.
3825 */
3826 if (session->output_traces) {
3827 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3828 goto error;
3829 }
3830
3831 if (session->has_non_mmap_channel) {
3832 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
3833 goto error;
3834 }
3835
3836 /* Only one output is allowed until we have the "tee" feature. */
3837 if (session->snapshot.nb_output == 1) {
3838 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
3839 goto error;
3840 }
3841
3842 new_output = snapshot_output_alloc();
3843 if (!new_output) {
3844 ret = LTTNG_ERR_NOMEM;
3845 goto error;
3846 }
3847
3848 ret = snapshot_output_init(session, output->max_size, output->name,
3849 output->ctrl_url, output->data_url, session->consumer, new_output,
3850 &session->snapshot);
3851 if (ret < 0) {
3852 if (ret == -ENOMEM) {
3853 ret = LTTNG_ERR_NOMEM;
3854 } else {
3855 ret = LTTNG_ERR_INVALID;
3856 }
3857 goto free_error;
3858 }
3859
3860 rcu_read_lock();
3861 snapshot_add_output(&session->snapshot, new_output);
3862 if (id) {
3863 *id = new_output->id;
3864 }
3865 rcu_read_unlock();
3866
3867 return LTTNG_OK;
3868
3869 free_error:
3870 snapshot_output_destroy(new_output);
3871 error:
3872 return ret;
3873 }
3874
3875 /*
3876 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
3877 *
3878 * Return LTTNG_OK on success or else a LTTNG_ERR code.
3879 */
3880 int cmd_snapshot_del_output(struct ltt_session *session,
3881 const struct lttng_snapshot_output *output)
3882 {
3883 int ret;
3884 struct snapshot_output *sout = NULL;
3885
3886 assert(session);
3887 assert(output);
3888
3889 rcu_read_lock();
3890
3891 /*
3892 * Permission denied to create an output if the session is not
3893 * set in no output mode.
3894 */
3895 if (session->output_traces) {
3896 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3897 goto error;
3898 }
3899
3900 if (output->id) {
3901 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
3902 session->name);
3903 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
3904 } else if (*output->name != '\0') {
3905 DBG("Cmd snapshot del output name %s for session %s", output->name,
3906 session->name);
3907 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
3908 }
3909 if (!sout) {
3910 ret = LTTNG_ERR_INVALID;
3911 goto error;
3912 }
3913
3914 snapshot_delete_output(&session->snapshot, sout);
3915 snapshot_output_destroy(sout);
3916 ret = LTTNG_OK;
3917
3918 error:
3919 rcu_read_unlock();
3920 return ret;
3921 }
3922
3923 /*
3924 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
3925 *
3926 * If no output is available, outputs is untouched and 0 is returned.
3927 *
3928 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
3929 */
3930 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
3931 struct lttng_snapshot_output **outputs)
3932 {
3933 int ret, idx = 0;
3934 struct lttng_snapshot_output *list = NULL;
3935 struct lttng_ht_iter iter;
3936 struct snapshot_output *output;
3937
3938 assert(session);
3939 assert(outputs);
3940
3941 DBG("Cmd snapshot list outputs for session %s", session->name);
3942
3943 /*
3944 * Permission denied to create an output if the session is not
3945 * set in no output mode.
3946 */
3947 if (session->output_traces) {
3948 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
3949 goto end;
3950 }
3951
3952 if (session->snapshot.nb_output == 0) {
3953 ret = 0;
3954 goto end;
3955 }
3956
3957 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
3958 if (!list) {
3959 ret = -LTTNG_ERR_NOMEM;
3960 goto end;
3961 }
3962
3963 /* Copy list from session to the new list object. */
3964 rcu_read_lock();
3965 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
3966 output, node.node) {
3967 assert(output->consumer);
3968 list[idx].id = output->id;
3969 list[idx].max_size = output->max_size;
3970 if (lttng_strncpy(list[idx].name, output->name,
3971 sizeof(list[idx].name))) {
3972 ret = -LTTNG_ERR_INVALID;
3973 goto error;
3974 }
3975 if (output->consumer->type == CONSUMER_DST_LOCAL) {
3976 if (lttng_strncpy(list[idx].ctrl_url,
3977 output->consumer->dst.session_root_path,
3978 sizeof(list[idx].ctrl_url))) {
3979 ret = -LTTNG_ERR_INVALID;
3980 goto error;
3981 }
3982 } else {
3983 /* Control URI. */
3984 ret = uri_to_str_url(&output->consumer->dst.net.control,
3985 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
3986 if (ret < 0) {
3987 ret = -LTTNG_ERR_NOMEM;
3988 goto error;
3989 }
3990
3991 /* Data URI. */
3992 ret = uri_to_str_url(&output->consumer->dst.net.data,
3993 list[idx].data_url, sizeof(list[idx].data_url));
3994 if (ret < 0) {
3995 ret = -LTTNG_ERR_NOMEM;
3996 goto error;
3997 }
3998 }
3999 idx++;
4000 }
4001
4002 *outputs = list;
4003 list = NULL;
4004 ret = session->snapshot.nb_output;
4005 error:
4006 rcu_read_unlock();
4007 free(list);
4008 end:
4009 return ret;
4010 }
4011
4012 /*
4013 * Check if we can regenerate the metadata for this session.
4014 * Only kernel, UST per-uid and non-live sessions are supported.
4015 *
4016 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4017 */
4018 static
4019 int check_regenerate_metadata_support(struct ltt_session *session)
4020 {
4021 int ret;
4022
4023 assert(session);
4024
4025 if (session->live_timer != 0) {
4026 ret = LTTNG_ERR_LIVE_SESSION;
4027 goto end;
4028 }
4029 if (!session->active) {
4030 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4031 goto end;
4032 }
4033 if (session->ust_session) {
4034 switch (session->ust_session->buffer_type) {
4035 case LTTNG_BUFFER_PER_UID:
4036 break;
4037 case LTTNG_BUFFER_PER_PID:
4038 ret = LTTNG_ERR_PER_PID_SESSION;
4039 goto end;
4040 default:
4041 assert(0);
4042 ret = LTTNG_ERR_UNK;
4043 goto end;
4044 }
4045 }
4046 if (session->consumer->type == CONSUMER_DST_NET &&
4047 session->consumer->relay_minor_version < 8) {
4048 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4049 goto end;
4050 }
4051 ret = 0;
4052
4053 end:
4054 return ret;
4055 }
4056
4057 static
4058 int clear_metadata_file(int fd)
4059 {
4060 int ret;
4061 off_t lseek_ret;
4062
4063 lseek_ret = lseek(fd, 0, SEEK_SET);
4064 if (lseek_ret < 0) {
4065 PERROR("lseek");
4066 ret = -1;
4067 goto end;
4068 }
4069
4070 ret = ftruncate(fd, 0);
4071 if (ret < 0) {
4072 PERROR("ftruncate");
4073 goto end;
4074 }
4075
4076 end:
4077 return ret;
4078 }
4079
4080 static
4081 int ust_regenerate_metadata(struct ltt_ust_session *usess)
4082 {
4083 int ret = 0;
4084 struct buffer_reg_uid *uid_reg = NULL;
4085 struct buffer_reg_session *session_reg = NULL;
4086
4087 rcu_read_lock();
4088 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
4089 struct ust_registry_session *registry;
4090 struct ust_registry_channel *chan;
4091 struct lttng_ht_iter iter_chan;
4092
4093 session_reg = uid_reg->registry;
4094 registry = session_reg->reg.ust;
4095
4096 pthread_mutex_lock(&registry->lock);
4097 registry->metadata_len_sent = 0;
4098 memset(registry->metadata, 0, registry->metadata_alloc_len);
4099 registry->metadata_len = 0;
4100 registry->metadata_version++;
4101 if (registry->metadata_fd > 0) {
4102 /* Clear the metadata file's content. */
4103 ret = clear_metadata_file(registry->metadata_fd);
4104 if (ret) {
4105 pthread_mutex_unlock(&registry->lock);
4106 goto end;
4107 }
4108 }
4109
4110 ret = ust_metadata_session_statedump(registry, NULL,
4111 registry->major, registry->minor);
4112 if (ret) {
4113 pthread_mutex_unlock(&registry->lock);
4114 ERR("Failed to generate session metadata (err = %d)",
4115 ret);
4116 goto end;
4117 }
4118 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
4119 chan, node.node) {
4120 struct ust_registry_event *event;
4121 struct lttng_ht_iter iter_event;
4122
4123 ret = ust_metadata_channel_statedump(registry, chan);
4124 if (ret) {
4125 pthread_mutex_unlock(&registry->lock);
4126 ERR("Failed to generate channel metadata "
4127 "(err = %d)", ret);
4128 goto end;
4129 }
4130 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
4131 event, node.node) {
4132 ret = ust_metadata_event_statedump(registry,
4133 chan, event);
4134 if (ret) {
4135 pthread_mutex_unlock(&registry->lock);
4136 ERR("Failed to generate event metadata "
4137 "(err = %d)", ret);
4138 goto end;
4139 }
4140 }
4141 }
4142 pthread_mutex_unlock(&registry->lock);
4143 }
4144
4145 end:
4146 rcu_read_unlock();
4147 return ret;
4148 }
4149
4150 /*
4151 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4152 *
4153 * Ask the consumer to truncate the existing metadata file(s) and
4154 * then regenerate the metadata. Live and per-pid sessions are not
4155 * supported and return an error.
4156 *
4157 * Return 0 on success or else a LTTNG_ERR code.
4158 */
4159 int cmd_regenerate_metadata(struct ltt_session *session)
4160 {
4161 int ret;
4162
4163 assert(session);
4164
4165 ret = check_regenerate_metadata_support(session);
4166 if (ret) {
4167 goto end;
4168 }
4169
4170 if (session->kernel_session) {
4171 ret = kernctl_session_regenerate_metadata(
4172 session->kernel_session->fd);
4173 if (ret < 0) {
4174 ERR("Failed to regenerate the kernel metadata");
4175 goto end;
4176 }
4177 }
4178
4179 if (session->ust_session) {
4180 ret = ust_regenerate_metadata(session->ust_session);
4181 if (ret < 0) {
4182 ERR("Failed to regenerate the UST metadata");
4183 goto end;
4184 }
4185 }
4186 DBG("Cmd metadata regenerate for session %s", session->name);
4187 ret = LTTNG_OK;
4188
4189 end:
4190 return ret;
4191 }
4192
4193 /*
4194 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4195 *
4196 * Ask the tracer to regenerate a new statedump.
4197 *
4198 * Return 0 on success or else a LTTNG_ERR code.
4199 */
4200 int cmd_regenerate_statedump(struct ltt_session *session)
4201 {
4202 int ret;
4203
4204 assert(session);
4205
4206 if (!session->active) {
4207 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4208 goto end;
4209 }
4210
4211 if (session->kernel_session) {
4212 ret = kernctl_session_regenerate_statedump(
4213 session->kernel_session->fd);
4214 /*
4215 * Currently, the statedump in kernel can only fail if out
4216 * of memory.
4217 */
4218 if (ret < 0) {
4219 if (ret == -ENOMEM) {
4220 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4221 } else {
4222 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4223 }
4224 ERR("Failed to regenerate the kernel statedump");
4225 goto end;
4226 }
4227 }
4228
4229 if (session->ust_session) {
4230 ret = ust_app_regenerate_statedump_all(session->ust_session);
4231 /*
4232 * Currently, the statedump in UST always returns 0.
4233 */
4234 if (ret < 0) {
4235 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4236 ERR("Failed to regenerate the UST statedump");
4237 goto end;
4238 }
4239 }
4240 DBG("Cmd regenerate statedump for session %s", session->name);
4241 ret = LTTNG_OK;
4242
4243 end:
4244 return ret;
4245 }
4246
4247 int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock,
4248 struct notification_thread_handle *notification_thread)
4249 {
4250 int ret;
4251 size_t trigger_len;
4252 ssize_t sock_recv_len;
4253 struct lttng_trigger *trigger = NULL;
4254 struct lttng_buffer_view view;
4255 struct lttng_dynamic_buffer trigger_buffer;
4256
4257 lttng_dynamic_buffer_init(&trigger_buffer);
4258 trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length;
4259 ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len);
4260 if (ret) {
4261 ret = LTTNG_ERR_NOMEM;
4262 goto end;
4263 }
4264
4265 sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data,
4266 trigger_len);
4267 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
4268 ERR("Failed to receive \"register trigger\" command payload");
4269 /* TODO: should this be a new error enum ? */
4270 ret = LTTNG_ERR_INVALID_TRIGGER;
4271 goto end;
4272 }
4273
4274 view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1);
4275 if (lttng_trigger_create_from_buffer(&view, &trigger) !=
4276 trigger_len) {
4277 ERR("Invalid trigger payload received in \"register trigger\" command");
4278 ret = LTTNG_ERR_INVALID_TRIGGER;
4279 goto end;
4280 }
4281
4282 ret = notification_thread_command_register_trigger(notification_thread,
4283 trigger);
4284 /* Ownership of trigger was transferred. */
4285 trigger = NULL;
4286 end:
4287 lttng_trigger_destroy(trigger);
4288 lttng_dynamic_buffer_reset(&trigger_buffer);
4289 return ret;
4290 }
4291
4292 int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock,
4293 struct notification_thread_handle *notification_thread)
4294 {
4295 int ret;
4296 size_t trigger_len;
4297 ssize_t sock_recv_len;
4298 struct lttng_trigger *trigger = NULL;
4299 struct lttng_buffer_view view;
4300 struct lttng_dynamic_buffer trigger_buffer;
4301
4302 lttng_dynamic_buffer_init(&trigger_buffer);
4303 trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length;
4304 ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len);
4305 if (ret) {
4306 ret = LTTNG_ERR_NOMEM;
4307 goto end;
4308 }
4309
4310 sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data,
4311 trigger_len);
4312 if (sock_recv_len < 0 || sock_recv_len != trigger_len) {
4313 ERR("Failed to receive \"unregister trigger\" command payload");
4314 /* TODO: should this be a new error enum ? */
4315 ret = LTTNG_ERR_INVALID_TRIGGER;
4316 goto end;
4317 }
4318
4319 view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1);
4320 if (lttng_trigger_create_from_buffer(&view, &trigger) !=
4321 trigger_len) {
4322 ERR("Invalid trigger payload received in \"unregister trigger\" command");
4323 ret = LTTNG_ERR_INVALID_TRIGGER;
4324 goto end;
4325 }
4326
4327 ret = notification_thread_command_unregister_trigger(notification_thread,
4328 trigger);
4329 end:
4330 lttng_trigger_destroy(trigger);
4331 lttng_dynamic_buffer_reset(&trigger_buffer);
4332 return ret;
4333 }
4334
4335 /*
4336 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4337 * snapshot output is *not* set with a remote destination.
4338 *
4339 * Return LTTNG_OK on success or a LTTNG_ERR code.
4340 */
4341 static enum lttng_error_code set_relayd_for_snapshot(
4342 struct consumer_output *output,
4343 const struct ltt_session *session)
4344 {
4345 enum lttng_error_code status = LTTNG_OK;
4346 struct lttng_ht_iter iter;
4347 struct consumer_socket *socket;
4348 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
4349 const char *base_path;
4350
4351 assert(output);
4352 assert(session);
4353
4354 DBG2("Set relayd object from snapshot output");
4355
4356 if (session->current_trace_chunk) {
4357 enum lttng_trace_chunk_status chunk_status =
4358 lttng_trace_chunk_get_id(
4359 session->current_trace_chunk,
4360 &current_chunk_id.value);
4361
4362 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
4363 current_chunk_id.is_set = true;
4364 } else {
4365 ERR("Failed to get current trace chunk id");
4366 status = LTTNG_ERR_UNK;
4367 goto error;
4368 }
4369 }
4370
4371 /* Ignore if snapshot consumer output is not network. */
4372 if (output->type != CONSUMER_DST_NET) {
4373 goto error;
4374 }
4375
4376 /*
4377 * The snapshot record URI base path overrides the session
4378 * base path.
4379 */
4380 if (output->dst.net.control.subdir[0] != '\0') {
4381 base_path = output->dst.net.control.subdir;
4382 } else {
4383 base_path = session->base_path;
4384 }
4385
4386 /*
4387 * For each consumer socket, create and send the relayd object of the
4388 * snapshot output.
4389 */
4390 rcu_read_lock();
4391 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
4392 socket, node.node) {
4393 pthread_mutex_lock(socket->lock);
4394 status = send_consumer_relayd_sockets(0, session->id,
4395 output, socket,
4396 session->name, session->hostname,
4397 base_path,
4398 session->live_timer,
4399 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
4400 session->creation_time,
4401 session->name_contains_creation_time);
4402 pthread_mutex_unlock(socket->lock);
4403 if (status != LTTNG_OK) {
4404 rcu_read_unlock();
4405 goto error;
4406 }
4407 }
4408 rcu_read_unlock();
4409
4410 error:
4411 return status;
4412 }
4413
4414 /*
4415 * Record a kernel snapshot.
4416 *
4417 * Return LTTNG_OK on success or a LTTNG_ERR code.
4418 */
4419 static enum lttng_error_code record_kernel_snapshot(
4420 struct ltt_kernel_session *ksess,
4421 const struct consumer_output *output,
4422 const struct ltt_session *session,
4423 int wait, uint64_t nb_packets_per_stream)
4424 {
4425 enum lttng_error_code status;
4426
4427 assert(ksess);
4428 assert(output);
4429 assert(session);
4430
4431 status = kernel_snapshot_record(
4432 ksess, output, wait, nb_packets_per_stream);
4433 return status;
4434 }
4435
4436 /*
4437 * Record a UST snapshot.
4438 *
4439 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
4440 */
4441 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
4442 const struct consumer_output *output,
4443 const struct ltt_session *session,
4444 int wait, uint64_t nb_packets_per_stream)
4445 {
4446 enum lttng_error_code status;
4447
4448 assert(usess);
4449 assert(output);
4450 assert(session);
4451
4452 status = ust_app_snapshot_record(
4453 usess, output, wait, nb_packets_per_stream);
4454 return status;
4455 }
4456
4457 static
4458 uint64_t get_session_size_one_more_packet_per_stream(
4459 const struct ltt_session *session, uint64_t cur_nr_packets)
4460 {
4461 uint64_t tot_size = 0;
4462
4463 if (session->kernel_session) {
4464 struct ltt_kernel_channel *chan;
4465 const struct ltt_kernel_session *ksess =
4466 session->kernel_session;
4467
4468 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
4469 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
4470 /*
4471 * Don't take channel into account if we
4472 * already grab all its packets.
4473 */
4474 continue;
4475 }
4476 tot_size += chan->channel->attr.subbuf_size
4477 * chan->stream_count;
4478 }
4479 }
4480
4481 if (session->ust_session) {
4482 const struct ltt_ust_session *usess = session->ust_session;
4483
4484 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
4485 cur_nr_packets);
4486 }
4487
4488 return tot_size;
4489 }
4490
4491 /*
4492 * Calculate the number of packets we can grab from each stream that
4493 * fits within the overall snapshot max size.
4494 *
4495 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
4496 * the number of packets per stream.
4497 *
4498 * TODO: this approach is not perfect: we consider the worse case
4499 * (packet filling the sub-buffers) as an upper bound, but we could do
4500 * better if we do this calculation while we actually grab the packet
4501 * content: we would know how much padding we don't actually store into
4502 * the file.
4503 *
4504 * This algorithm is currently bounded by the number of packets per
4505 * stream.
4506 *
4507 * Since we call this algorithm before actually grabbing the data, it's
4508 * an approximation: for instance, applications could appear/disappear
4509 * in between this call and actually grabbing data.
4510 */
4511 static
4512 int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
4513 uint64_t max_size)
4514 {
4515 int64_t size_left;
4516 uint64_t cur_nb_packets = 0;
4517
4518 if (!max_size) {
4519 return 0; /* Infinite */
4520 }
4521
4522 size_left = max_size;
4523 for (;;) {
4524 uint64_t one_more_packet_tot_size;
4525
4526 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
4527 session, cur_nb_packets);
4528 if (!one_more_packet_tot_size) {
4529 /* We are already grabbing all packets. */
4530 break;
4531 }
4532 size_left -= one_more_packet_tot_size;
4533 if (size_left < 0) {
4534 break;
4535 }
4536 cur_nb_packets++;
4537 }
4538 if (!cur_nb_packets && size_left != max_size) {
4539 /* Not enough room to grab one packet of each stream, error. */
4540 return -1;
4541 }
4542 return cur_nb_packets;
4543 }
4544
4545 static
4546 enum lttng_error_code snapshot_record(struct ltt_session *session,
4547 const struct snapshot_output *snapshot_output, int wait)
4548 {
4549 int64_t nb_packets_per_stream;
4550 char snapshot_chunk_name[LTTNG_NAME_MAX];
4551 int ret;
4552 enum lttng_error_code ret_code = LTTNG_OK;
4553 struct lttng_trace_chunk *snapshot_trace_chunk;
4554 struct consumer_output *original_ust_consumer_output = NULL;
4555 struct consumer_output *original_kernel_consumer_output = NULL;
4556 struct consumer_output *snapshot_ust_consumer_output = NULL;
4557 struct consumer_output *snapshot_kernel_consumer_output = NULL;
4558
4559 ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
4560 "%s-%s-%" PRIu64,
4561 snapshot_output->name,
4562 snapshot_output->datetime,
4563 snapshot_output->nb_snapshot);
4564 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
4565 ERR("Failed to format snapshot name");
4566 ret_code = LTTNG_ERR_INVALID;
4567 goto error;
4568 }
4569 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
4570 snapshot_output->name, session->name,
4571 snapshot_chunk_name);
4572 if (!session->kernel_session && !session->ust_session) {
4573 ERR("Failed to record snapshot as no channels exist");
4574 ret_code = LTTNG_ERR_NO_CHANNEL;
4575 goto error;
4576 }
4577
4578 if (session->kernel_session) {
4579 original_kernel_consumer_output =
4580 session->kernel_session->consumer;
4581 snapshot_kernel_consumer_output =
4582 consumer_copy_output(snapshot_output->consumer);
4583 strcpy(snapshot_kernel_consumer_output->chunk_path,
4584 snapshot_chunk_name);
4585 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
4586 original_kernel_consumer_output);
4587 if (ret < 0) {
4588 ERR("Failed to copy consumer sockets from snapshot output configuration");
4589 ret_code = LTTNG_ERR_NOMEM;
4590 goto error;
4591 }
4592 ret_code = set_relayd_for_snapshot(
4593 snapshot_kernel_consumer_output, session);
4594 if (ret_code != LTTNG_OK) {
4595 ERR("Failed to setup relay daemon for kernel tracer snapshot");
4596 goto error;
4597 }
4598 session->kernel_session->consumer =
4599 snapshot_kernel_consumer_output;
4600 }
4601 if (session->ust_session) {
4602 original_ust_consumer_output = session->ust_session->consumer;
4603 snapshot_ust_consumer_output =
4604 consumer_copy_output(snapshot_output->consumer);
4605 strcpy(snapshot_ust_consumer_output->chunk_path,
4606 snapshot_chunk_name);
4607 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
4608 original_ust_consumer_output);
4609 if (ret < 0) {
4610 ERR("Failed to copy consumer sockets from snapshot output configuration");
4611 ret_code = LTTNG_ERR_NOMEM;
4612 goto error;
4613 }
4614 ret_code = set_relayd_for_snapshot(
4615 snapshot_ust_consumer_output, session);
4616 if (ret_code != LTTNG_OK) {
4617 ERR("Failed to setup relay daemon for userspace tracer snapshot");
4618 goto error;
4619 }
4620 session->ust_session->consumer =
4621 snapshot_ust_consumer_output;
4622 }
4623
4624 snapshot_trace_chunk = session_create_new_trace_chunk(session,
4625 snapshot_kernel_consumer_output ?:
4626 snapshot_ust_consumer_output,
4627 consumer_output_get_base_path(
4628 snapshot_output->consumer),
4629 snapshot_chunk_name);
4630 if (!snapshot_trace_chunk) {
4631 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
4632 session->name);
4633 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
4634 goto error;
4635 }
4636 assert(!session->current_trace_chunk);
4637 ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
4638 lttng_trace_chunk_put(snapshot_trace_chunk);
4639 snapshot_trace_chunk = NULL;
4640 if (ret) {
4641 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
4642 session->name);
4643 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
4644 goto error;
4645 }
4646
4647 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
4648 snapshot_output->max_size);
4649 if (nb_packets_per_stream < 0) {
4650 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
4651 goto error_close_trace_chunk;
4652 }
4653
4654 if (session->kernel_session) {
4655 ret_code = record_kernel_snapshot(session->kernel_session,
4656 snapshot_kernel_consumer_output, session,
4657 wait, nb_packets_per_stream);
4658 if (ret_code != LTTNG_OK) {
4659 goto error_close_trace_chunk;
4660 }
4661 }
4662
4663 if (session->ust_session) {
4664 ret_code = record_ust_snapshot(session->ust_session,
4665 snapshot_ust_consumer_output, session,
4666 wait, nb_packets_per_stream);
4667 if (ret_code != LTTNG_OK) {
4668 goto error_close_trace_chunk;
4669 }
4670 }
4671 error_close_trace_chunk:
4672 if (session_close_trace_chunk(
4673 session, session->current_trace_chunk, NULL, NULL)) {
4674 /*
4675 * Don't goto end; make sure the chunk is closed for the session
4676 * to allow future snapshots.
4677 */
4678 ERR("Failed to close snapshot trace chunk of session \"%s\"",
4679 session->name);
4680 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
4681 }
4682 if (session_set_trace_chunk(session, NULL, NULL)) {
4683 ERR("Failed to release the current trace chunk of session \"%s\"",
4684 session->name);
4685 ret_code = LTTNG_ERR_UNK;
4686 }
4687 error:
4688 if (original_ust_consumer_output) {
4689 session->ust_session->consumer = original_ust_consumer_output;
4690 }
4691 if (original_kernel_consumer_output) {
4692 session->kernel_session->consumer =
4693 original_kernel_consumer_output;
4694 }
4695 consumer_output_put(snapshot_ust_consumer_output);
4696 consumer_output_put(snapshot_kernel_consumer_output);
4697 return ret_code;
4698 }
4699
4700 /*
4701 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
4702 *
4703 * The wait parameter is ignored so this call always wait for the snapshot to
4704 * complete before returning.
4705 *
4706 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4707 */
4708 int cmd_snapshot_record(struct ltt_session *session,
4709 const struct lttng_snapshot_output *output, int wait)
4710 {
4711 enum lttng_error_code cmd_ret = LTTNG_OK;
4712 int ret;
4713 unsigned int snapshot_success = 0;
4714 char datetime[16];
4715 struct snapshot_output *tmp_output = NULL;
4716
4717 assert(session);
4718 assert(output);
4719
4720 DBG("Cmd snapshot record for session %s", session->name);
4721
4722 /* Get the datetime for the snapshot output directory. */
4723 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
4724 sizeof(datetime));
4725 if (!ret) {
4726 cmd_ret = LTTNG_ERR_INVALID;
4727 goto error;
4728 }
4729
4730 /*
4731 * Permission denied to create an output if the session is not
4732 * set in no output mode.
4733 */
4734 if (session->output_traces) {
4735 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4736 goto error;
4737 }
4738
4739 /* The session needs to be started at least once. */
4740 if (!session->has_been_started) {
4741 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
4742 goto error;
4743 }
4744
4745 /* Use temporary output for the session. */
4746 if (*output->ctrl_url != '\0') {
4747 tmp_output = snapshot_output_alloc();
4748 if (!tmp_output) {
4749 cmd_ret = LTTNG_ERR_NOMEM;
4750 goto error;
4751 }
4752
4753 ret = snapshot_output_init(session, output->max_size,
4754 output->name,
4755 output->ctrl_url, output->data_url,
4756 session->consumer,
4757 tmp_output, NULL);
4758 if (ret < 0) {
4759 if (ret == -ENOMEM) {
4760 cmd_ret = LTTNG_ERR_NOMEM;
4761 } else {
4762 cmd_ret = LTTNG_ERR_INVALID;
4763 }
4764 goto error;
4765 }
4766 /* Use the global session count for the temporary snapshot. */
4767 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
4768
4769 /* Use the global datetime */
4770 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
4771 cmd_ret = snapshot_record(session, tmp_output, wait);
4772 if (cmd_ret != LTTNG_OK) {
4773 goto error;
4774 }
4775 snapshot_success = 1;
4776 } else {
4777 struct snapshot_output *sout;
4778 struct lttng_ht_iter iter;
4779
4780 rcu_read_lock();
4781 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
4782 &iter.iter, sout, node.node) {
4783 struct snapshot_output output_copy;
4784
4785 /*
4786 * Make a local copy of the output and override output
4787 * parameters with those provided as part of the
4788 * command.
4789 */
4790 memcpy(&output_copy, sout, sizeof(output_copy));
4791
4792 if (output->max_size != (uint64_t) -1ULL) {
4793 output_copy.max_size = output->max_size;
4794 }
4795
4796 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
4797 memcpy(output_copy.datetime, datetime,
4798 sizeof(datetime));
4799
4800 /* Use temporary name. */
4801 if (*output->name != '\0') {
4802 if (lttng_strncpy(output_copy.name,
4803 output->name,
4804 sizeof(output_copy.name))) {
4805 cmd_ret = LTTNG_ERR_INVALID;
4806 rcu_read_unlock();
4807 goto error;
4808 }
4809 }
4810
4811 cmd_ret = snapshot_record(session, &output_copy, wait);
4812 if (cmd_ret != LTTNG_OK) {
4813 rcu_read_unlock();
4814 goto error;
4815 }
4816 snapshot_success = 1;
4817 }
4818 rcu_read_unlock();
4819 }
4820
4821 if (snapshot_success) {
4822 session->snapshot.nb_snapshot++;
4823 } else {
4824 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
4825 }
4826
4827 error:
4828 if (tmp_output) {
4829 snapshot_output_destroy(tmp_output);
4830 }
4831 return cmd_ret;
4832 }
4833
4834 /*
4835 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
4836 */
4837 int cmd_set_session_shm_path(struct ltt_session *session,
4838 const char *shm_path)
4839 {
4840 /* Safety net */
4841 assert(session);
4842
4843 /*
4844 * Can only set shm path before session is started.
4845 */
4846 if (session->has_been_started) {
4847 return LTTNG_ERR_SESSION_STARTED;
4848 }
4849
4850 strncpy(session->shm_path, shm_path,
4851 sizeof(session->shm_path));
4852 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
4853
4854 return 0;
4855 }
4856
4857 /*
4858 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
4859 *
4860 * Ask the consumer to rotate the session output directory.
4861 * The session lock must be held.
4862 *
4863 * Returns LTTNG_OK on success or else a negative LTTng error code.
4864 */
4865 int cmd_rotate_session(struct ltt_session *session,
4866 struct lttng_rotate_session_return *rotate_return,
4867 bool quiet_rotation)
4868 {
4869 int ret;
4870 uint64_t ongoing_rotation_chunk_id;
4871 enum lttng_error_code cmd_ret = LTTNG_OK;
4872 struct lttng_trace_chunk *chunk_being_archived = NULL;
4873 struct lttng_trace_chunk *new_trace_chunk = NULL;
4874 enum lttng_trace_chunk_status chunk_status;
4875 bool failed_to_rotate = false;
4876 enum lttng_error_code rotation_fail_code = LTTNG_OK;
4877
4878 assert(session);
4879
4880 if (!session->has_been_started) {
4881 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
4882 goto end;
4883 }
4884
4885 /*
4886 * Explicit rotation is not supported for live sessions.
4887 * However, live sessions can perform a quiet rotation on
4888 * destroy.
4889 * Rotation is not supported for snapshot traces (no output).
4890 */
4891 if ((!quiet_rotation && session->live_timer) ||
4892 !session->output_traces) {
4893 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
4894 goto end;
4895 }
4896
4897 /* Unsupported feature in lttng-relayd before 2.11. */
4898 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
4899 (session->consumer->relay_major_version == 2 &&
4900 session->consumer->relay_minor_version < 11)) {
4901 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
4902 goto end;
4903 }
4904
4905 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
4906 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
4907 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
4908 goto end;
4909 }
4910
4911 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
4912 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
4913 session->name);
4914 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
4915 goto end;
4916 }
4917
4918 /*
4919 * After a stop, we only allow one rotation to occur, the other ones are
4920 * useless until a new start.
4921 */
4922 if (session->rotated_after_last_stop) {
4923 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
4924 session->name);
4925 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
4926 goto end;
4927 }
4928 if (session->active) {
4929 new_trace_chunk = session_create_new_trace_chunk(session, NULL,
4930 NULL, NULL);
4931 if (!new_trace_chunk) {
4932 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
4933 goto error;
4934 }
4935 }
4936
4937 /*
4938 * The current trace chunk becomes the chunk being archived.
4939 *
4940 * After this point, "chunk_being_archived" must absolutely
4941 * be closed on the consumer(s), otherwise it will never be
4942 * cleaned-up, which will result in a leak.
4943 */
4944 ret = session_set_trace_chunk(session, new_trace_chunk,
4945 &chunk_being_archived);
4946 if (ret) {
4947 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
4948 goto error;
4949 }
4950
4951 if (session->kernel_session) {
4952 cmd_ret = kernel_rotate_session(session);
4953 if (cmd_ret != LTTNG_OK) {
4954 failed_to_rotate = true;
4955 rotation_fail_code = cmd_ret;
4956 }
4957 }
4958 if (session->ust_session) {
4959 cmd_ret = ust_app_rotate_session(session);
4960 if (cmd_ret != LTTNG_OK) {
4961 failed_to_rotate = true;
4962 rotation_fail_code = cmd_ret;
4963 }
4964 }
4965
4966 if (!session->active) {
4967 session->rotated_after_last_stop = true;
4968 }
4969
4970 if (!chunk_being_archived) {
4971 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
4972 session->name);
4973 if (failed_to_rotate) {
4974 cmd_ret = rotation_fail_code;
4975 goto error;
4976 }
4977 cmd_ret = LTTNG_OK;
4978 goto end;
4979 }
4980
4981 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
4982 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
4983 &ongoing_rotation_chunk_id);
4984 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
4985
4986 ret = session_close_trace_chunk(session, chunk_being_archived,
4987 quiet_rotation ?
4988 NULL :
4989 &((enum lttng_trace_chunk_command_type){
4990 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED}),
4991 session->last_chunk_path);
4992 if (ret) {
4993 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
4994 goto error;
4995 }
4996
4997 if (failed_to_rotate) {
4998 cmd_ret = rotation_fail_code;
4999 goto error;
5000 }
5001
5002 session->quiet_rotation = quiet_rotation;
5003 ret = timer_session_rotation_pending_check_start(session,
5004 DEFAULT_ROTATE_PENDING_TIMER);
5005 if (ret) {
5006 cmd_ret = LTTNG_ERR_UNK;
5007 goto error;
5008 }
5009
5010 if (rotate_return) {
5011 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5012 }
5013
5014 session->chunk_being_archived = chunk_being_archived;
5015 chunk_being_archived = NULL;
5016 if (!quiet_rotation) {
5017 ret = notification_thread_command_session_rotation_ongoing(
5018 notification_thread_handle,
5019 session->name, session->uid, session->gid,
5020 ongoing_rotation_chunk_id);
5021 if (ret != LTTNG_OK) {
5022 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5023 session->name);
5024 cmd_ret = ret;
5025 }
5026 }
5027
5028 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5029 session->name, ongoing_rotation_chunk_id);
5030 end:
5031 lttng_trace_chunk_put(new_trace_chunk);
5032 lttng_trace_chunk_put(chunk_being_archived);
5033 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5034 return ret;
5035 error:
5036 if (session_reset_rotation_state(session,
5037 LTTNG_ROTATION_STATE_ERROR)) {
5038 ERR("Failed to reset rotation state of session \"%s\"",
5039 session->name);
5040 }
5041 goto end;
5042 }
5043
5044 /*
5045 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5046 *
5047 * Check if the session has finished its rotation.
5048 *
5049 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5050 */
5051 int cmd_rotate_get_info(struct ltt_session *session,
5052 struct lttng_rotation_get_info_return *info_return,
5053 uint64_t rotation_id)
5054 {
5055 enum lttng_error_code cmd_ret = LTTNG_OK;
5056 enum lttng_rotation_state rotation_state;
5057
5058 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
5059 session->most_recent_chunk_id.value);
5060
5061 if (session->chunk_being_archived) {
5062 enum lttng_trace_chunk_status chunk_status;
5063 uint64_t chunk_id;
5064
5065 chunk_status = lttng_trace_chunk_get_id(
5066 session->chunk_being_archived,
5067 &chunk_id);
5068 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5069
5070 rotation_state = rotation_id == chunk_id ?
5071 LTTNG_ROTATION_STATE_ONGOING :
5072 LTTNG_ROTATION_STATE_EXPIRED;
5073 } else {
5074 if (session->last_archived_chunk_id.is_set &&
5075 rotation_id != session->last_archived_chunk_id.value) {
5076 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5077 } else {
5078 rotation_state = session->rotation_state;
5079 }
5080 }
5081
5082 switch (rotation_state) {
5083 case LTTNG_ROTATION_STATE_NO_ROTATION:
5084 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5085 session->name);
5086 goto end;
5087 case LTTNG_ROTATION_STATE_EXPIRED:
5088 DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
5089 rotation_id, session->name);
5090 break;
5091 case LTTNG_ROTATION_STATE_ONGOING:
5092 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5093 rotation_id, session->name);
5094 break;
5095 case LTTNG_ROTATION_STATE_COMPLETED:
5096 {
5097 int fmt_ret;
5098 char *chunk_path;
5099 char *current_tracing_path_reply;
5100 size_t current_tracing_path_reply_len;
5101
5102 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5103 rotation_id, session->name);
5104
5105 switch (session_get_consumer_destination_type(session)) {
5106 case CONSUMER_DST_LOCAL:
5107 current_tracing_path_reply =
5108 info_return->location.local.absolute_path;
5109 current_tracing_path_reply_len =
5110 sizeof(info_return->location.local.absolute_path);
5111 info_return->location_type =
5112 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5113 fmt_ret = asprintf(&chunk_path,
5114 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5115 session_get_base_path(session),
5116 session->last_archived_chunk_name);
5117 if (fmt_ret == -1) {
5118 PERROR("Failed to format the path of the last archived trace chunk");
5119 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5120 cmd_ret = LTTNG_ERR_UNK;
5121 goto end;
5122 }
5123 break;
5124 case CONSUMER_DST_NET:
5125 {
5126 uint16_t ctrl_port, data_port;
5127
5128 current_tracing_path_reply =
5129 info_return->location.relay.relative_path;
5130 current_tracing_path_reply_len =
5131 sizeof(info_return->location.relay.relative_path);
5132 /* Currently the only supported relay protocol. */
5133 info_return->location.relay.protocol =
5134 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5135
5136 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5137 session_get_net_consumer_hostname(session),
5138 sizeof(info_return->location.relay.host));
5139 if (fmt_ret) {
5140 ERR("Failed to copy host name to rotate_get_info reply");
5141 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5142 cmd_ret = LTTNG_ERR_SET_URL;
5143 goto end;
5144 }
5145
5146 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5147 info_return->location.relay.ports.control = ctrl_port;
5148 info_return->location.relay.ports.data = data_port;
5149 info_return->location_type =
5150 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5151 chunk_path = strdup(session->last_chunk_path);
5152 if (!chunk_path) {
5153 ERR("Failed to allocate the path of the last archived trace chunk");
5154 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5155 cmd_ret = LTTNG_ERR_UNK;
5156 goto end;
5157 }
5158 break;
5159 }
5160 default:
5161 abort();
5162 }
5163
5164 fmt_ret = lttng_strncpy(current_tracing_path_reply,
5165 chunk_path, current_tracing_path_reply_len);
5166 free(chunk_path);
5167 if (fmt_ret) {
5168 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5169 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5170 cmd_ret = LTTNG_ERR_UNK;
5171 goto end;
5172 }
5173
5174 break;
5175 }
5176 case LTTNG_ROTATION_STATE_ERROR:
5177 DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
5178 rotation_id, session->name);
5179 break;
5180 default:
5181 abort();
5182 }
5183
5184 cmd_ret = LTTNG_OK;
5185 end:
5186 info_return->status = (int32_t) rotation_state;
5187 return cmd_ret;
5188 }
5189
5190 /*
5191 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5192 *
5193 * Configure the automatic rotation parameters.
5194 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5195 * 'activate' to false means deactivate the rotation schedule and validate that
5196 * 'new_value' has the same value as the currently active value.
5197 *
5198 * Return 0 on success or else a positive LTTNG_ERR code.
5199 */
5200 int cmd_rotation_set_schedule(struct ltt_session *session,
5201 bool activate, enum lttng_rotation_schedule_type schedule_type,
5202 uint64_t new_value,
5203 struct notification_thread_handle *notification_thread_handle)
5204 {
5205 int ret;
5206 uint64_t *parameter_value;
5207
5208 assert(session);
5209
5210 DBG("Cmd rotate set schedule session %s", session->name);
5211
5212 if (session->live_timer || !session->output_traces) {
5213 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5214 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5215 goto end;
5216 }
5217
5218 switch (schedule_type) {
5219 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5220 parameter_value = &session->rotate_size;
5221 break;
5222 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5223 parameter_value = &session->rotate_timer_period;
5224 if (new_value >= UINT_MAX) {
5225 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
5226 new_value, UINT_MAX);
5227 ret = LTTNG_ERR_INVALID;
5228 goto end;
5229 }
5230 break;
5231 default:
5232 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5233 ret = LTTNG_ERR_INVALID;
5234 goto end;
5235 }
5236
5237 /* Improper use of the API. */
5238 if (new_value == -1ULL) {
5239 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5240 ret = LTTNG_ERR_INVALID;
5241 goto end;
5242 }
5243
5244 /*
5245 * As indicated in struct ltt_session's comments, a value of == 0 means
5246 * this schedule rotation type is not in use.
5247 *
5248 * Reject the command if we were asked to activate a schedule that was
5249 * already active.
5250 */
5251 if (activate && *parameter_value != 0) {
5252 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5253 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5254 goto end;
5255 }
5256
5257 /*
5258 * Reject the command if we were asked to deactivate a schedule that was
5259 * not active.
5260 */
5261 if (!activate && *parameter_value == 0) {
5262 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5263 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5264 goto end;
5265 }
5266
5267 /*
5268 * Reject the command if we were asked to deactivate a schedule that
5269 * doesn't exist.
5270 */
5271 if (!activate && *parameter_value != new_value) {
5272 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5273 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5274 goto end;
5275 }
5276
5277 *parameter_value = activate ? new_value : 0;
5278
5279 switch (schedule_type) {
5280 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5281 if (activate && session->active) {
5282 /*
5283 * Only start the timer if the session is active,
5284 * otherwise it will be started when the session starts.
5285 */
5286 ret = timer_session_rotation_schedule_timer_start(
5287 session, new_value);
5288 if (ret) {
5289 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5290 ret = LTTNG_ERR_UNK;
5291 goto end;
5292 }
5293 } else {
5294 ret = timer_session_rotation_schedule_timer_stop(
5295 session);
5296 if (ret) {
5297 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5298 ret = LTTNG_ERR_UNK;
5299 goto end;
5300 }
5301 }
5302 break;
5303 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5304 if (activate) {
5305 ret = subscribe_session_consumed_size_rotation(session,
5306 new_value, notification_thread_handle);
5307 if (ret) {
5308 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5309 ret = LTTNG_ERR_UNK;
5310 goto end;
5311 }
5312 } else {
5313 ret = unsubscribe_session_consumed_size_rotation(session,
5314 notification_thread_handle);
5315 if (ret) {
5316 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5317 ret = LTTNG_ERR_UNK;
5318 goto end;
5319 }
5320
5321 }
5322 break;
5323 default:
5324 /* Would have been caught before. */
5325 abort();
5326 }
5327
5328 ret = LTTNG_OK;
5329
5330 goto end;
5331
5332 end:
5333 return ret;
5334 }
5335
5336 /* Wait for a given path to be removed before continuing. */
5337 static enum lttng_error_code wait_on_path(void *path_data)
5338 {
5339 const char *shm_path = path_data;
5340
5341 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
5342 shm_path);
5343 while (true) {
5344 int ret;
5345 struct stat st;
5346
5347 ret = stat(shm_path, &st);
5348 if (ret) {
5349 if (errno != ENOENT) {
5350 PERROR("stat() returned an error while checking for the existence of the shm path");
5351 } else {
5352 DBG("shm path no longer exists, completing the destruction of session");
5353 }
5354 break;
5355 } else {
5356 if (!S_ISDIR(st.st_mode)) {
5357 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
5358 shm_path);
5359 break;
5360 }
5361 }
5362 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
5363 }
5364 return LTTNG_OK;
5365 }
5366
5367 /*
5368 * Returns a pointer to a handler to run on completion of a command.
5369 * Returns NULL if no handler has to be run for the last command executed.
5370 */
5371 const struct cmd_completion_handler *cmd_pop_completion_handler(void)
5372 {
5373 struct cmd_completion_handler *handler = current_completion_handler;
5374
5375 current_completion_handler = NULL;
5376 return handler;
5377 }
5378
5379 /*
5380 * Init command subsystem.
5381 */
5382 void cmd_init(void)
5383 {
5384 /*
5385 * Set network sequence index to 1 for streams to match a relayd
5386 * socket on the consumer side.
5387 */
5388 pthread_mutex_lock(&relayd_net_seq_idx_lock);
5389 relayd_net_seq_idx = 1;
5390 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
5391
5392 DBG("Command subsystem initialized");
5393 }
This page took 0.185832 seconds and 4 git commands to generate.