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