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