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