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