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