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