SoW-2020-0002: Trace Hit Counters: trigger error reporting integration
[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/condition/condition-internal.h>
33 #include <lttng/condition/event-rule.h>
34 #include <lttng/condition/event-rule-internal.h>
35 #include <lttng/event-rule/event-rule.h>
36 #include <lttng/event-rule/event-rule-internal.h>
37 #include <lttng/event-rule/uprobe-internal.h>
38 #include <lttng/event-rule/tracepoint.h>
39 #include <lttng/action/action.h>
40 #include <lttng/channel.h>
41 #include <lttng/channel-internal.h>
42 #include <lttng/rotate-internal.h>
43 #include <lttng/location-internal.h>
44 #include <lttng/session-internal.h>
45 #include <lttng/userspace-probe-internal.h>
46 #include <lttng/session-descriptor-internal.h>
47 #include <lttng/lttng-error.h>
48 #include <lttng/tracker.h>
49 #include <common/string-utils/string-utils.h>
50
51 #include "channel.h"
52 #include "consumer.h"
53 #include "event.h"
54 #include "health-sessiond.h"
55 #include "kernel.h"
56 #include "kernel-consumer.h"
57 #include "lttng-sessiond.h"
58 #include "utils.h"
59 #include "lttng-syscall.h"
60 #include "agent.h"
61 #include "buffer-registry.h"
62 #include "notification-thread.h"
63 #include "notification-thread-commands.h"
64 #include "rotate.h"
65 #include "rotation-thread.h"
66 #include "timer.h"
67 #include "agent-thread.h"
68 #include "tracker.h"
69
70 #include "cmd.h"
71
72 /* Sleep for 100ms between each check for the shm path's deletion. */
73 #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000
74
75 struct cmd_destroy_session_reply_context {
76 int reply_sock_fd;
77 bool implicit_rotation_on_destroy;
78 /*
79 * Indicates whether or not an error occurred while launching the
80 * destruction of a session.
81 */
82 enum lttng_error_code destruction_status;
83 };
84
85 static enum lttng_error_code wait_on_path(void *path);
86
87 /*
88 * Command completion handler that is used by the destroy command
89 * when a session that has a non-default shm_path is being destroyed.
90 *
91 * See comment in cmd_destroy_session() for the rationale.
92 */
93 static struct destroy_completion_handler {
94 struct cmd_completion_handler handler;
95 char shm_path[member_sizeof(struct ltt_session, shm_path)];
96 } destroy_completion_handler = {
97 .handler = {
98 .run = wait_on_path,
99 .data = destroy_completion_handler.shm_path
100 },
101 .shm_path = { 0 },
102 };
103
104 static struct cmd_completion_handler *current_completion_handler;
105
106 /*
107 * Used to keep a unique index for each relayd socket created where this value
108 * is associated with streams on the consumer so it can match the right relayd
109 * to send to. It must be accessed with the relayd_net_seq_idx_lock
110 * held.
111 */
112 static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER;
113 static uint64_t relayd_net_seq_idx;
114
115 static int validate_ust_event_name(const char *);
116 static int cmd_enable_event_internal(struct ltt_session *session,
117 const struct lttng_domain *domain,
118 char *channel_name, struct lttng_event *event,
119 char *filter_expression,
120 struct lttng_bytecode *filter,
121 struct lttng_event_exclusion *exclusion,
122 int wpipe);
123
124 /*
125 * Create a session path used by list_lttng_sessions for the case that the
126 * session consumer is on the network.
127 */
128 static int build_network_session_path(char *dst, size_t size,
129 struct ltt_session *session)
130 {
131 int ret, kdata_port, udata_port;
132 struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL;
133 char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX];
134
135 assert(session);
136 assert(dst);
137
138 memset(tmp_urls, 0, sizeof(tmp_urls));
139 memset(tmp_uurl, 0, sizeof(tmp_uurl));
140
141 kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT;
142
143 if (session->kernel_session && session->kernel_session->consumer) {
144 kuri = &session->kernel_session->consumer->dst.net.control;
145 kdata_port = session->kernel_session->consumer->dst.net.data.port;
146 }
147
148 if (session->ust_session && session->ust_session->consumer) {
149 uuri = &session->ust_session->consumer->dst.net.control;
150 udata_port = session->ust_session->consumer->dst.net.data.port;
151 }
152
153 if (uuri == NULL && kuri == NULL) {
154 uri = &session->consumer->dst.net.control;
155 kdata_port = session->consumer->dst.net.data.port;
156 } else if (kuri && uuri) {
157 ret = uri_compare(kuri, uuri);
158 if (ret) {
159 /* Not Equal */
160 uri = kuri;
161 /* Build uuri URL string */
162 ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl));
163 if (ret < 0) {
164 goto error;
165 }
166 } else {
167 uri = kuri;
168 }
169 } else if (kuri && uuri == NULL) {
170 uri = kuri;
171 } else if (uuri && kuri == NULL) {
172 uri = uuri;
173 }
174
175 ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls));
176 if (ret < 0) {
177 goto error;
178 }
179
180 /*
181 * Do we have a UST url set. If yes, this means we have both kernel and UST
182 * to print.
183 */
184 if (*tmp_uurl != '\0') {
185 ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]",
186 tmp_urls, kdata_port, tmp_uurl, udata_port);
187 } else {
188 int dport;
189 if (kuri || (!kuri && !uuri)) {
190 dport = kdata_port;
191 } else {
192 /* No kernel URI, use the UST port. */
193 dport = udata_port;
194 }
195 ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport);
196 }
197
198 error:
199 return ret;
200 }
201
202 /*
203 * Get run-time attributes if the session has been started (discarded events,
204 * lost packets).
205 */
206 static int get_kernel_runtime_stats(struct ltt_session *session,
207 struct ltt_kernel_channel *kchan, uint64_t *discarded_events,
208 uint64_t *lost_packets)
209 {
210 int ret;
211
212 if (!session->has_been_started) {
213 ret = 0;
214 *discarded_events = 0;
215 *lost_packets = 0;
216 goto end;
217 }
218
219 ret = consumer_get_discarded_events(session->id, kchan->key,
220 session->kernel_session->consumer,
221 discarded_events);
222 if (ret < 0) {
223 goto end;
224 }
225
226 ret = consumer_get_lost_packets(session->id, kchan->key,
227 session->kernel_session->consumer,
228 lost_packets);
229 if (ret < 0) {
230 goto end;
231 }
232
233 end:
234 return ret;
235 }
236
237 /*
238 * Get run-time attributes if the session has been started (discarded events,
239 * lost packets).
240 */
241 static int get_ust_runtime_stats(struct ltt_session *session,
242 struct ltt_ust_channel *uchan, uint64_t *discarded_events,
243 uint64_t *lost_packets)
244 {
245 int ret;
246 struct ltt_ust_session *usess;
247
248 if (!discarded_events || !lost_packets) {
249 ret = -1;
250 goto end;
251 }
252
253 usess = session->ust_session;
254 assert(discarded_events);
255 assert(lost_packets);
256
257 if (!usess || !session->has_been_started) {
258 *discarded_events = 0;
259 *lost_packets = 0;
260 ret = 0;
261 goto end;
262 }
263
264 if (usess->buffer_type == LTTNG_BUFFER_PER_UID) {
265 ret = ust_app_uid_get_channel_runtime_stats(usess->id,
266 &usess->buffer_reg_uid_list,
267 usess->consumer, uchan->id,
268 uchan->attr.overwrite,
269 discarded_events,
270 lost_packets);
271 } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) {
272 ret = ust_app_pid_get_channel_runtime_stats(usess,
273 uchan, usess->consumer,
274 uchan->attr.overwrite,
275 discarded_events,
276 lost_packets);
277 if (ret < 0) {
278 goto end;
279 }
280 *discarded_events += uchan->per_pid_closed_app_discarded;
281 *lost_packets += uchan->per_pid_closed_app_lost;
282 } else {
283 ERR("Unsupported buffer type");
284 assert(0);
285 ret = -1;
286 goto end;
287 }
288
289 end:
290 return ret;
291 }
292
293 /*
294 * Fill lttng_channel array of all channels.
295 */
296 static ssize_t list_lttng_channels(enum lttng_domain_type domain,
297 struct ltt_session *session, struct lttng_channel *channels,
298 struct lttng_channel_extended *chan_exts)
299 {
300 int i = 0, ret = 0;
301 struct ltt_kernel_channel *kchan;
302
303 DBG("Listing channels for session %s", session->name);
304
305 switch (domain) {
306 case LTTNG_DOMAIN_KERNEL:
307 /* Kernel channels */
308 if (session->kernel_session != NULL) {
309 cds_list_for_each_entry(kchan,
310 &session->kernel_session->channel_list.head, list) {
311 uint64_t discarded_events, lost_packets;
312 struct lttng_channel_extended *extended;
313
314 extended = (struct lttng_channel_extended *)
315 kchan->channel->attr.extended.ptr;
316
317 ret = get_kernel_runtime_stats(session, kchan,
318 &discarded_events, &lost_packets);
319 if (ret < 0) {
320 goto end;
321 }
322 /* Copy lttng_channel struct to array */
323 memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel));
324 channels[i].enabled = kchan->enabled;
325 chan_exts[i].discarded_events =
326 discarded_events;
327 chan_exts[i].lost_packets = lost_packets;
328 chan_exts[i].monitor_timer_interval =
329 extended->monitor_timer_interval;
330 chan_exts[i].blocking_timeout = 0;
331 i++;
332 }
333 }
334 break;
335 case LTTNG_DOMAIN_UST:
336 {
337 struct lttng_ht_iter iter;
338 struct ltt_ust_channel *uchan;
339
340 rcu_read_lock();
341 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
342 &iter.iter, uchan, node.node) {
343 uint64_t discarded_events = 0, lost_packets = 0;
344
345 if (lttng_strncpy(channels[i].name, uchan->name,
346 LTTNG_SYMBOL_NAME_LEN)) {
347 break;
348 }
349 channels[i].attr.overwrite = uchan->attr.overwrite;
350 channels[i].attr.subbuf_size = uchan->attr.subbuf_size;
351 channels[i].attr.num_subbuf = uchan->attr.num_subbuf;
352 channels[i].attr.switch_timer_interval =
353 uchan->attr.switch_timer_interval;
354 channels[i].attr.read_timer_interval =
355 uchan->attr.read_timer_interval;
356 channels[i].enabled = uchan->enabled;
357 channels[i].attr.tracefile_size = uchan->tracefile_size;
358 channels[i].attr.tracefile_count = uchan->tracefile_count;
359
360 /*
361 * Map enum lttng_ust_output to enum lttng_event_output.
362 */
363 switch (uchan->attr.output) {
364 case LTTNG_UST_MMAP:
365 channels[i].attr.output = LTTNG_EVENT_MMAP;
366 break;
367 default:
368 /*
369 * LTTNG_UST_MMAP is the only supported UST
370 * output mode.
371 */
372 assert(0);
373 break;
374 }
375
376 chan_exts[i].monitor_timer_interval =
377 uchan->monitor_timer_interval;
378 chan_exts[i].blocking_timeout =
379 uchan->attr.u.s.blocking_timeout;
380
381 ret = get_ust_runtime_stats(session, uchan,
382 &discarded_events, &lost_packets);
383 if (ret < 0) {
384 break;
385 }
386 chan_exts[i].discarded_events = discarded_events;
387 chan_exts[i].lost_packets = lost_packets;
388 i++;
389 }
390 rcu_read_unlock();
391 break;
392 }
393 default:
394 break;
395 }
396
397 end:
398 if (ret < 0) {
399 return -LTTNG_ERR_FATAL;
400 } else {
401 return LTTNG_OK;
402 }
403 }
404
405 static int append_extended_info(const char *filter_expression,
406 struct lttng_event_exclusion *exclusion,
407 struct lttng_userspace_probe_location *probe_location,
408 struct lttng_payload *payload)
409 {
410 int ret = 0;
411 size_t filter_len = 0;
412 size_t nb_exclusions = 0;
413 size_t userspace_probe_location_len = 0;
414 struct lttcomm_event_extended_header extended_header = {};
415 struct lttcomm_event_extended_header *p_extended_header;
416 const size_t original_payload_size = payload->buffer.size;
417
418 ret = lttng_dynamic_buffer_append(&payload->buffer, &extended_header,
419 sizeof(extended_header));
420 if (ret) {
421 goto end;
422 }
423
424 if (filter_expression) {
425 filter_len = strlen(filter_expression) + 1;
426 ret = lttng_dynamic_buffer_append(&payload->buffer,
427 filter_expression, filter_len);
428 if (ret) {
429 goto end;
430 }
431 }
432
433 if (exclusion) {
434 const size_t len = exclusion->count * LTTNG_SYMBOL_NAME_LEN;
435
436 nb_exclusions = exclusion->count;
437
438 ret = lttng_dynamic_buffer_append(
439 &payload->buffer, &exclusion->names, len);
440 if (ret) {
441 goto end;
442 }
443 }
444
445 if (probe_location) {
446 const size_t size_before_probe = payload->buffer.size;
447
448 ret = lttng_userspace_probe_location_serialize(probe_location,
449 payload);
450 if (ret < 0) {
451 ret = -1;
452 goto end;
453 }
454
455 userspace_probe_location_len =
456 payload->buffer.size - size_before_probe;
457 }
458
459 /* Set header fields */
460 p_extended_header = (struct lttcomm_event_extended_header *)
461 (payload->buffer.data + original_payload_size);
462
463 p_extended_header->filter_len = filter_len;
464 p_extended_header->nb_exclusions = nb_exclusions;
465 p_extended_header->userspace_probe_location_len =
466 userspace_probe_location_len;
467
468 ret = 0;
469 end:
470 return ret;
471 }
472
473 /*
474 * Create a list of agent domain events.
475 *
476 * Return number of events in list on success or else a negative value.
477 */
478 static int list_lttng_agent_events(struct agent *agt,
479 struct lttng_payload *payload)
480 {
481 int nb_events = 0, ret = 0;
482 const struct agent_event *agent_event;
483 struct lttng_ht_iter iter;
484
485 assert(agt);
486
487 DBG3("Listing agent events");
488
489 rcu_read_lock();
490 cds_lfht_for_each_entry (
491 agt->events->ht, &iter.iter, agent_event, node.node) {
492 struct lttng_event event = {
493 .enabled = agent_event->enabled,
494 .loglevel = agent_event->loglevel_value,
495 .loglevel_type = agent_event->loglevel_type,
496 };
497
498 strncpy(event.name, agent_event->name, sizeof(event.name));
499 event.name[sizeof(event.name) - 1] = '\0';
500
501 ret = lttng_dynamic_buffer_append(
502 &payload->buffer, &event, sizeof(event));
503 if (ret) {
504 ERR("Failed to append event to payload");
505 ret = -LTTNG_ERR_NOMEM;
506 goto end;
507 }
508
509 nb_events++;
510 }
511
512 cds_lfht_for_each_entry (
513 agt->events->ht, &iter.iter, agent_event, node.node) {
514 /* Append extended info. */
515 ret = append_extended_info(agent_event->filter_expression, NULL,
516 NULL, payload);
517 if (ret) {
518 ERR("Failed to append extended event info to payload");
519 ret = -LTTNG_ERR_NOMEM;
520 goto end;
521 }
522 }
523
524 ret = nb_events;
525 end:
526 rcu_read_unlock();
527 return ret;
528 }
529
530 /*
531 * Create a list of ust global domain events.
532 */
533 static int list_lttng_ust_global_events(char *channel_name,
534 struct ltt_ust_domain_global *ust_global,
535 struct lttng_payload *payload)
536 {
537 int ret = 0;
538 unsigned int nb_events = 0;
539 struct lttng_ht_iter iter;
540 const struct lttng_ht_node_str *node;
541 const struct ltt_ust_channel *uchan;
542 const struct ltt_ust_event *uevent;
543
544 DBG("Listing UST global events for channel %s", channel_name);
545
546 rcu_read_lock();
547
548 lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter);
549 node = lttng_ht_iter_get_node_str(&iter);
550 if (node == NULL) {
551 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
552 goto end;
553 }
554
555 uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node);
556
557 DBG3("Listing UST global events");
558
559 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
560 struct lttng_event event = {};
561
562 if (uevent->internal) {
563 continue;
564 }
565
566 strncpy(event.name, uevent->attr.name, sizeof(event.name));
567 event.name[sizeof(event.name) - 1] = '\0';
568
569 event.enabled = uevent->enabled;
570
571 switch (uevent->attr.instrumentation) {
572 case LTTNG_UST_TRACEPOINT:
573 event.type = LTTNG_EVENT_TRACEPOINT;
574 break;
575 case LTTNG_UST_PROBE:
576 event.type = LTTNG_EVENT_PROBE;
577 break;
578 case LTTNG_UST_FUNCTION:
579 event.type = LTTNG_EVENT_FUNCTION;
580 break;
581 }
582
583 event.loglevel = uevent->attr.loglevel;
584 switch (uevent->attr.loglevel_type) {
585 case LTTNG_UST_LOGLEVEL_ALL:
586 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
587 break;
588 case LTTNG_UST_LOGLEVEL_RANGE:
589 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE;
590 break;
591 case LTTNG_UST_LOGLEVEL_SINGLE:
592 event.loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE;
593 break;
594 }
595
596 if (uevent->filter) {
597 event.filter = 1;
598 }
599
600 if (uevent->exclusion) {
601 event.exclusion = 1;
602 }
603
604 ret = lttng_dynamic_buffer_append(&payload->buffer, &event, sizeof(event));
605 if (ret) {
606 ERR("Failed to append event to payload");
607 ret = -LTTNG_ERR_NOMEM;
608 goto end;
609 }
610
611 nb_events++;
612 }
613
614 cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) {
615 /* Append extended info. */
616 ret = append_extended_info(uevent->filter_expression,
617 uevent->exclusion, NULL, payload);
618 if (ret) {
619 ERR("Failed to append extended event info to payload");
620 ret = -LTTNG_ERR_FATAL;
621 goto end;
622 }
623 }
624
625 ret = nb_events;
626 end:
627 rcu_read_unlock();
628 return ret;
629 }
630
631 /*
632 * Fill lttng_event array of all kernel events in the channel.
633 */
634 static int list_lttng_kernel_events(char *channel_name,
635 struct ltt_kernel_session *kernel_session,
636 struct lttng_payload *payload)
637 {
638 int ret;
639 unsigned int nb_event;
640 const struct ltt_kernel_event *kevent;
641 const struct ltt_kernel_channel *kchan;
642
643 kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session);
644 if (kchan == NULL) {
645 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
646 goto error;
647 }
648
649 nb_event = kchan->event_count;
650
651 DBG("Listing events for channel %s", kchan->channel->name);
652
653 /* Kernel channels */
654 cds_list_for_each_entry(kevent, &kchan->events_list.head , list) {
655 struct lttng_event event = {};
656
657 strncpy(event.name, kevent->event->name, sizeof(event.name));
658 event.name[sizeof(event.name) - 1] = '\0';
659 event.enabled = kevent->enabled;
660 event.filter = (unsigned char) !!kevent->filter_expression;
661
662 switch (kevent->event->instrumentation) {
663 case LTTNG_KERNEL_TRACEPOINT:
664 event.type = LTTNG_EVENT_TRACEPOINT;
665 break;
666 case LTTNG_KERNEL_KRETPROBE:
667 event.type = LTTNG_EVENT_FUNCTION;
668 memcpy(&event.attr.probe, &kevent->event->u.kprobe,
669 sizeof(struct lttng_kernel_kprobe));
670 break;
671 case LTTNG_KERNEL_KPROBE:
672 event.type = LTTNG_EVENT_PROBE;
673 memcpy(&event.attr.probe, &kevent->event->u.kprobe,
674 sizeof(struct lttng_kernel_kprobe));
675 break;
676 case LTTNG_KERNEL_UPROBE:
677 event.type = LTTNG_EVENT_USERSPACE_PROBE;
678 break;
679 case LTTNG_KERNEL_FUNCTION:
680 event.type = LTTNG_EVENT_FUNCTION;
681 memcpy(&event.attr.ftrace, &kevent->event->u.ftrace,
682 sizeof(struct lttng_kernel_function));
683 break;
684 case LTTNG_KERNEL_NOOP:
685 event.type = LTTNG_EVENT_NOOP;
686 break;
687 case LTTNG_KERNEL_SYSCALL:
688 event.type = LTTNG_EVENT_SYSCALL;
689 break;
690 case LTTNG_KERNEL_ALL:
691 /* fall-through. */
692 default:
693 assert(0);
694 break;
695 }
696
697 ret = lttng_dynamic_buffer_append(
698 &payload->buffer, &event, sizeof(event));
699 if (ret) {
700 ERR("Failed to append event to payload");
701 ret = -LTTNG_ERR_NOMEM;
702 goto end;
703 }
704 }
705
706 cds_list_for_each_entry(kevent, &kchan->events_list.head , list) {
707 /* Append extended info. */
708 ret = append_extended_info(kevent->filter_expression, NULL,
709 kevent->userspace_probe_location, payload);
710 if (ret) {
711 DBG("Error appending extended info message");
712 ret = -LTTNG_ERR_FATAL;
713 goto error;
714 }
715 }
716
717 end:
718 return nb_event;
719 error:
720 return ret;
721 }
722
723 /*
724 * Add URI so the consumer output object. Set the correct path depending on the
725 * domain adding the default trace directory.
726 */
727 static enum lttng_error_code add_uri_to_consumer(
728 const struct ltt_session *session,
729 struct consumer_output *consumer,
730 struct lttng_uri *uri, enum lttng_domain_type domain)
731 {
732 int ret;
733 enum lttng_error_code ret_code = LTTNG_OK;
734
735 assert(uri);
736
737 if (consumer == NULL) {
738 DBG("No consumer detected. Don't add URI. Stopping.");
739 ret_code = LTTNG_ERR_NO_CONSUMER;
740 goto error;
741 }
742
743 switch (domain) {
744 case LTTNG_DOMAIN_KERNEL:
745 ret = lttng_strncpy(consumer->domain_subdir,
746 DEFAULT_KERNEL_TRACE_DIR,
747 sizeof(consumer->domain_subdir));
748 break;
749 case LTTNG_DOMAIN_UST:
750 ret = lttng_strncpy(consumer->domain_subdir,
751 DEFAULT_UST_TRACE_DIR,
752 sizeof(consumer->domain_subdir));
753 break;
754 default:
755 /*
756 * This case is possible is we try to add the URI to the global
757 * tracing session consumer object which in this case there is
758 * no subdir.
759 */
760 memset(consumer->domain_subdir, 0,
761 sizeof(consumer->domain_subdir));
762 ret = 0;
763 }
764 if (ret) {
765 ERR("Failed to initialize consumer output domain subdirectory");
766 ret_code = LTTNG_ERR_FATAL;
767 goto error;
768 }
769
770 switch (uri->dtype) {
771 case LTTNG_DST_IPV4:
772 case LTTNG_DST_IPV6:
773 DBG2("Setting network URI to consumer");
774
775 if (consumer->type == CONSUMER_DST_NET) {
776 if ((uri->stype == LTTNG_STREAM_CONTROL &&
777 consumer->dst.net.control_isset) ||
778 (uri->stype == LTTNG_STREAM_DATA &&
779 consumer->dst.net.data_isset)) {
780 ret_code = LTTNG_ERR_URL_EXIST;
781 goto error;
782 }
783 } else {
784 memset(&consumer->dst, 0, sizeof(consumer->dst));
785 }
786
787 /* Set URI into consumer output object */
788 ret = consumer_set_network_uri(session, consumer, uri);
789 if (ret < 0) {
790 ret_code = -ret;
791 goto error;
792 } else if (ret == 1) {
793 /*
794 * URI was the same in the consumer so we do not append the subdir
795 * again so to not duplicate output dir.
796 */
797 ret_code = LTTNG_OK;
798 goto error;
799 }
800 break;
801 case LTTNG_DST_PATH:
802 if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) {
803 ret_code = LTTNG_ERR_INVALID;
804 goto error;
805 }
806 DBG2("Setting trace directory path from URI to %s",
807 uri->dst.path);
808 memset(&consumer->dst, 0, sizeof(consumer->dst));
809
810 ret = lttng_strncpy(consumer->dst.session_root_path,
811 uri->dst.path,
812 sizeof(consumer->dst.session_root_path));
813 if (ret) {
814 ret_code = LTTNG_ERR_FATAL;
815 goto error;
816 }
817 consumer->type = CONSUMER_DST_LOCAL;
818 break;
819 }
820
821 ret_code = LTTNG_OK;
822 error:
823 return ret_code;
824 }
825
826 /*
827 * Init tracing by creating trace directory and sending fds kernel consumer.
828 */
829 static int init_kernel_tracing(struct ltt_kernel_session *session)
830 {
831 int ret = 0;
832 struct lttng_ht_iter iter;
833 struct consumer_socket *socket;
834
835 assert(session);
836
837 rcu_read_lock();
838
839 if (session->consumer_fds_sent == 0 && session->consumer != NULL) {
840 cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter,
841 socket, node.node) {
842 pthread_mutex_lock(socket->lock);
843 ret = kernel_consumer_send_session(socket, session);
844 pthread_mutex_unlock(socket->lock);
845 if (ret < 0) {
846 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
847 goto error;
848 }
849 }
850 }
851
852 error:
853 rcu_read_unlock();
854 return ret;
855 }
856
857 /*
858 * Create a socket to the relayd using the URI.
859 *
860 * On success, the relayd_sock pointer is set to the created socket.
861 * Else, it remains untouched and an LTTng error code is returned.
862 */
863 static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri,
864 struct lttcomm_relayd_sock **relayd_sock,
865 struct consumer_output *consumer)
866 {
867 int ret;
868 enum lttng_error_code status = LTTNG_OK;
869 struct lttcomm_relayd_sock *rsock;
870
871 rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR,
872 RELAYD_VERSION_COMM_MINOR);
873 if (!rsock) {
874 status = LTTNG_ERR_FATAL;
875 goto error;
876 }
877
878 /*
879 * Connect to relayd so we can proceed with a session creation. This call
880 * can possibly block for an arbitrary amount of time to set the health
881 * state to be in poll execution.
882 */
883 health_poll_entry();
884 ret = relayd_connect(rsock);
885 health_poll_exit();
886 if (ret < 0) {
887 ERR("Unable to reach lttng-relayd");
888 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
889 goto free_sock;
890 }
891
892 /* Create socket for control stream. */
893 if (uri->stype == LTTNG_STREAM_CONTROL) {
894 uint64_t result_flags;
895
896 DBG3("Creating relayd stream socket from URI");
897
898 /* Check relayd version */
899 ret = relayd_version_check(rsock);
900 if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) {
901 status = LTTNG_ERR_RELAYD_VERSION_FAIL;
902 goto close_sock;
903 } else if (ret < 0) {
904 ERR("Unable to reach lttng-relayd");
905 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
906 goto close_sock;
907 }
908 consumer->relay_major_version = rsock->major;
909 consumer->relay_minor_version = rsock->minor;
910 ret = relayd_get_configuration(rsock, 0,
911 &result_flags);
912 if (ret < 0) {
913 ERR("Unable to get relayd configuration");
914 status = LTTNG_ERR_RELAYD_CONNECT_FAIL;
915 goto close_sock;
916 }
917 if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) {
918 consumer->relay_allows_clear = true;
919 }
920 } else if (uri->stype == LTTNG_STREAM_DATA) {
921 DBG3("Creating relayd data socket from URI");
922 } else {
923 /* Command is not valid */
924 ERR("Relayd invalid stream type: %d", uri->stype);
925 status = LTTNG_ERR_INVALID;
926 goto close_sock;
927 }
928
929 *relayd_sock = rsock;
930
931 return status;
932
933 close_sock:
934 /* The returned value is not useful since we are on an error path. */
935 (void) relayd_close(rsock);
936 free_sock:
937 free(rsock);
938 error:
939 return status;
940 }
941
942 /*
943 * Connect to the relayd using URI and send the socket to the right consumer.
944 *
945 * The consumer socket lock must be held by the caller.
946 *
947 * Returns LTTNG_OK on success or an LTTng error code on failure.
948 */
949 static enum lttng_error_code send_consumer_relayd_socket(
950 unsigned int session_id,
951 struct lttng_uri *relayd_uri,
952 struct consumer_output *consumer,
953 struct consumer_socket *consumer_sock,
954 const char *session_name, const char *hostname,
955 const char *base_path, int session_live_timer,
956 const uint64_t *current_chunk_id,
957 time_t session_creation_time,
958 bool session_name_contains_creation_time)
959 {
960 int ret;
961 struct lttcomm_relayd_sock *rsock = NULL;
962 enum lttng_error_code status;
963
964 /* Connect to relayd and make version check if uri is the control. */
965 status = create_connect_relayd(relayd_uri, &rsock, consumer);
966 if (status != LTTNG_OK) {
967 goto relayd_comm_error;
968 }
969 assert(rsock);
970
971 /* Set the network sequence index if not set. */
972 if (consumer->net_seq_index == (uint64_t) -1ULL) {
973 pthread_mutex_lock(&relayd_net_seq_idx_lock);
974 /*
975 * Increment net_seq_idx because we are about to transfer the
976 * new relayd socket to the consumer.
977 * Assign unique key so the consumer can match streams.
978 */
979 consumer->net_seq_index = ++relayd_net_seq_idx;
980 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
981 }
982
983 /* Send relayd socket to consumer. */
984 ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer,
985 relayd_uri->stype, session_id,
986 session_name, hostname, base_path,
987 session_live_timer, current_chunk_id,
988 session_creation_time, session_name_contains_creation_time);
989 if (ret < 0) {
990 status = LTTNG_ERR_ENABLE_CONSUMER_FAIL;
991 goto close_sock;
992 }
993
994 /* Flag that the corresponding socket was sent. */
995 if (relayd_uri->stype == LTTNG_STREAM_CONTROL) {
996 consumer_sock->control_sock_sent = 1;
997 } else if (relayd_uri->stype == LTTNG_STREAM_DATA) {
998 consumer_sock->data_sock_sent = 1;
999 }
1000
1001 /*
1002 * Close socket which was dup on the consumer side. The session daemon does
1003 * NOT keep track of the relayd socket(s) once transfer to the consumer.
1004 */
1005
1006 close_sock:
1007 if (status != LTTNG_OK) {
1008 /*
1009 * The consumer output for this session should not be used anymore
1010 * since the relayd connection failed thus making any tracing or/and
1011 * streaming not usable.
1012 */
1013 consumer->enabled = 0;
1014 }
1015 (void) relayd_close(rsock);
1016 free(rsock);
1017
1018 relayd_comm_error:
1019 return status;
1020 }
1021
1022 /*
1023 * Send both relayd sockets to a specific consumer and domain. This is a
1024 * helper function to facilitate sending the information to the consumer for a
1025 * session.
1026 *
1027 * The consumer socket lock must be held by the caller.
1028 *
1029 * Returns LTTNG_OK, or an LTTng error code on failure.
1030 */
1031 static enum lttng_error_code send_consumer_relayd_sockets(
1032 enum lttng_domain_type domain,
1033 unsigned int session_id, struct consumer_output *consumer,
1034 struct consumer_socket *sock, const char *session_name,
1035 const char *hostname, const char *base_path, int session_live_timer,
1036 const uint64_t *current_chunk_id, time_t session_creation_time,
1037 bool session_name_contains_creation_time)
1038 {
1039 enum lttng_error_code status = LTTNG_OK;
1040
1041 assert(consumer);
1042 assert(sock);
1043
1044 /* Sending control relayd socket. */
1045 if (!sock->control_sock_sent) {
1046 status = send_consumer_relayd_socket(session_id,
1047 &consumer->dst.net.control, consumer, sock,
1048 session_name, hostname, base_path, session_live_timer,
1049 current_chunk_id, session_creation_time,
1050 session_name_contains_creation_time);
1051 if (status != LTTNG_OK) {
1052 goto error;
1053 }
1054 }
1055
1056 /* Sending data relayd socket. */
1057 if (!sock->data_sock_sent) {
1058 status = send_consumer_relayd_socket(session_id,
1059 &consumer->dst.net.data, consumer, sock,
1060 session_name, hostname, base_path, session_live_timer,
1061 current_chunk_id, session_creation_time,
1062 session_name_contains_creation_time);
1063 if (status != LTTNG_OK) {
1064 goto error;
1065 }
1066 }
1067
1068 error:
1069 return status;
1070 }
1071
1072 /*
1073 * Setup relayd connections for a tracing session. First creates the socket to
1074 * the relayd and send them to the right domain consumer. Consumer type MUST be
1075 * network.
1076 */
1077 int cmd_setup_relayd(struct ltt_session *session)
1078 {
1079 int ret = LTTNG_OK;
1080 struct ltt_ust_session *usess;
1081 struct ltt_kernel_session *ksess;
1082 struct consumer_socket *socket;
1083 struct lttng_ht_iter iter;
1084 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
1085
1086 assert(session);
1087
1088 usess = session->ust_session;
1089 ksess = session->kernel_session;
1090
1091 DBG("Setting relayd for session %s", session->name);
1092
1093 rcu_read_lock();
1094 if (session->current_trace_chunk) {
1095 enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id(
1096 session->current_trace_chunk, &current_chunk_id.value);
1097
1098 if (status == LTTNG_TRACE_CHUNK_STATUS_OK) {
1099 current_chunk_id.is_set = true;
1100 } else {
1101 ERR("Failed to get current trace chunk id");
1102 ret = LTTNG_ERR_UNK;
1103 goto error;
1104 }
1105 }
1106
1107 if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET
1108 && usess->consumer->enabled) {
1109 /* For each consumer socket, send relayd sockets */
1110 cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter,
1111 socket, node.node) {
1112 pthread_mutex_lock(socket->lock);
1113 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id,
1114 usess->consumer, socket,
1115 session->name, session->hostname,
1116 session->base_path,
1117 session->live_timer,
1118 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1119 session->creation_time,
1120 session->name_contains_creation_time);
1121 pthread_mutex_unlock(socket->lock);
1122 if (ret != LTTNG_OK) {
1123 goto error;
1124 }
1125 /* Session is now ready for network streaming. */
1126 session->net_handle = 1;
1127 }
1128 session->consumer->relay_major_version =
1129 usess->consumer->relay_major_version;
1130 session->consumer->relay_minor_version =
1131 usess->consumer->relay_minor_version;
1132 session->consumer->relay_allows_clear =
1133 usess->consumer->relay_allows_clear;
1134 }
1135
1136 if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET
1137 && ksess->consumer->enabled) {
1138 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1139 socket, node.node) {
1140 pthread_mutex_lock(socket->lock);
1141 ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id,
1142 ksess->consumer, socket,
1143 session->name, session->hostname,
1144 session->base_path,
1145 session->live_timer,
1146 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
1147 session->creation_time,
1148 session->name_contains_creation_time);
1149 pthread_mutex_unlock(socket->lock);
1150 if (ret != LTTNG_OK) {
1151 goto error;
1152 }
1153 /* Session is now ready for network streaming. */
1154 session->net_handle = 1;
1155 }
1156 session->consumer->relay_major_version =
1157 ksess->consumer->relay_major_version;
1158 session->consumer->relay_minor_version =
1159 ksess->consumer->relay_minor_version;
1160 session->consumer->relay_allows_clear =
1161 ksess->consumer->relay_allows_clear;
1162 }
1163
1164 error:
1165 rcu_read_unlock();
1166 return ret;
1167 }
1168
1169 /*
1170 * Start a kernel session by opening all necessary streams.
1171 */
1172 int start_kernel_session(struct ltt_kernel_session *ksess)
1173 {
1174 int ret;
1175 struct ltt_kernel_channel *kchan;
1176
1177 /* Open kernel metadata */
1178 if (ksess->metadata == NULL && ksess->output_traces) {
1179 ret = kernel_open_metadata(ksess);
1180 if (ret < 0) {
1181 ret = LTTNG_ERR_KERN_META_FAIL;
1182 goto error;
1183 }
1184 }
1185
1186 /* Open kernel metadata stream */
1187 if (ksess->metadata && ksess->metadata_stream_fd < 0) {
1188 ret = kernel_open_metadata_stream(ksess);
1189 if (ret < 0) {
1190 ERR("Kernel create metadata stream failed");
1191 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1192 goto error;
1193 }
1194 }
1195
1196 /* For each channel */
1197 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1198 if (kchan->stream_count == 0) {
1199 ret = kernel_open_channel_stream(kchan);
1200 if (ret < 0) {
1201 ret = LTTNG_ERR_KERN_STREAM_FAIL;
1202 goto error;
1203 }
1204 /* Update the stream global counter */
1205 ksess->stream_count_global += ret;
1206 }
1207 }
1208
1209 /* Setup kernel consumer socket and send fds to it */
1210 ret = init_kernel_tracing(ksess);
1211 if (ret != 0) {
1212 ret = LTTNG_ERR_KERN_START_FAIL;
1213 goto error;
1214 }
1215
1216 /* This start the kernel tracing */
1217 ret = kernel_start_session(ksess);
1218 if (ret < 0) {
1219 ret = LTTNG_ERR_KERN_START_FAIL;
1220 goto error;
1221 }
1222
1223 /* Quiescent wait after starting trace */
1224 kernel_wait_quiescent();
1225
1226 ksess->active = 1;
1227
1228 ret = LTTNG_OK;
1229
1230 error:
1231 return ret;
1232 }
1233
1234 int stop_kernel_session(struct ltt_kernel_session *ksess)
1235 {
1236 struct ltt_kernel_channel *kchan;
1237 bool error_occurred = false;
1238 int ret;
1239
1240 if (!ksess || !ksess->active) {
1241 return LTTNG_OK;
1242 }
1243 DBG("Stopping kernel tracing");
1244
1245 ret = kernel_stop_session(ksess);
1246 if (ret < 0) {
1247 ret = LTTNG_ERR_KERN_STOP_FAIL;
1248 goto error;
1249 }
1250
1251 kernel_wait_quiescent();
1252
1253 /* Flush metadata after stopping (if exists) */
1254 if (ksess->metadata_stream_fd >= 0) {
1255 ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd);
1256 if (ret < 0) {
1257 ERR("Kernel metadata flush failed");
1258 error_occurred = true;
1259 }
1260 }
1261
1262 /* Flush all buffers after stopping */
1263 cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) {
1264 ret = kernel_flush_buffer(kchan);
1265 if (ret < 0) {
1266 ERR("Kernel flush buffer error");
1267 error_occurred = true;
1268 }
1269 }
1270
1271 ksess->active = 0;
1272 if (error_occurred) {
1273 ret = LTTNG_ERR_UNK;
1274 } else {
1275 ret = LTTNG_OK;
1276 }
1277 error:
1278 return ret;
1279 }
1280
1281 /*
1282 * Command LTTNG_DISABLE_CHANNEL processed by the client thread.
1283 */
1284 int cmd_disable_channel(struct ltt_session *session,
1285 enum lttng_domain_type domain, char *channel_name)
1286 {
1287 int ret;
1288 struct ltt_ust_session *usess;
1289
1290 usess = session->ust_session;
1291
1292 rcu_read_lock();
1293
1294 switch (domain) {
1295 case LTTNG_DOMAIN_KERNEL:
1296 {
1297 ret = channel_kernel_disable(session->kernel_session,
1298 channel_name);
1299 if (ret != LTTNG_OK) {
1300 goto error;
1301 }
1302
1303 kernel_wait_quiescent();
1304 break;
1305 }
1306 case LTTNG_DOMAIN_UST:
1307 {
1308 struct ltt_ust_channel *uchan;
1309 struct lttng_ht *chan_ht;
1310
1311 chan_ht = usess->domain_global.channels;
1312
1313 uchan = trace_ust_find_channel_by_name(chan_ht, channel_name);
1314 if (uchan == NULL) {
1315 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1316 goto error;
1317 }
1318
1319 ret = channel_ust_disable(usess, uchan);
1320 if (ret != LTTNG_OK) {
1321 goto error;
1322 }
1323 break;
1324 }
1325 default:
1326 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1327 goto error;
1328 }
1329
1330 ret = LTTNG_OK;
1331
1332 error:
1333 rcu_read_unlock();
1334 return ret;
1335 }
1336
1337 /*
1338 * Command LTTNG_ENABLE_CHANNEL processed by the client thread.
1339 *
1340 * The wpipe arguments is used as a notifier for the kernel thread.
1341 */
1342 int cmd_enable_channel(struct ltt_session *session,
1343 const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe)
1344 {
1345 int ret;
1346 struct ltt_ust_session *usess = session->ust_session;
1347 struct lttng_ht *chan_ht;
1348 size_t len;
1349 struct lttng_channel attr;
1350
1351 assert(session);
1352 assert(_attr);
1353 assert(domain);
1354
1355 attr = *_attr;
1356 len = lttng_strnlen(attr.name, sizeof(attr.name));
1357
1358 /* Validate channel name */
1359 if (attr.name[0] == '.' ||
1360 memchr(attr.name, '/', len) != NULL) {
1361 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1362 goto end;
1363 }
1364
1365 DBG("Enabling channel %s for session %s", attr.name, session->name);
1366
1367 rcu_read_lock();
1368
1369 /*
1370 * Don't try to enable a channel if the session has been started at
1371 * some point in time before. The tracer does not allow it.
1372 */
1373 if (session->has_been_started) {
1374 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1375 goto error;
1376 }
1377
1378 /*
1379 * If the session is a live session, remove the switch timer, the
1380 * live timer does the same thing but sends also synchronisation
1381 * beacons for inactive streams.
1382 */
1383 if (session->live_timer > 0) {
1384 attr.attr.live_timer_interval = session->live_timer;
1385 attr.attr.switch_timer_interval = 0;
1386 }
1387
1388 /* Check for feature support */
1389 switch (domain->type) {
1390 case LTTNG_DOMAIN_KERNEL:
1391 {
1392 if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) {
1393 /* Sampling position of buffer is not supported */
1394 WARN("Kernel tracer does not support buffer monitoring. "
1395 "Setting the monitor interval timer to 0 "
1396 "(disabled) for channel '%s' of session '%s'",
1397 attr.name, session->name);
1398 lttng_channel_set_monitor_timer_interval(&attr, 0);
1399 }
1400 break;
1401 }
1402 case LTTNG_DOMAIN_UST:
1403 break;
1404 case LTTNG_DOMAIN_JUL:
1405 case LTTNG_DOMAIN_LOG4J:
1406 case LTTNG_DOMAIN_PYTHON:
1407 if (!agent_tracing_is_enabled()) {
1408 DBG("Attempted to enable a channel in an agent domain but the agent thread is not running");
1409 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
1410 goto error;
1411 }
1412 break;
1413 default:
1414 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1415 goto error;
1416 }
1417
1418 switch (domain->type) {
1419 case LTTNG_DOMAIN_KERNEL:
1420 {
1421 struct ltt_kernel_channel *kchan;
1422
1423 kchan = trace_kernel_get_channel_by_name(attr.name,
1424 session->kernel_session);
1425 if (kchan == NULL) {
1426 if (session->snapshot.nb_output > 0 ||
1427 session->snapshot_mode) {
1428 /* Enforce mmap output for snapshot sessions. */
1429 attr.attr.output = LTTNG_EVENT_MMAP;
1430 }
1431 ret = channel_kernel_create(session->kernel_session, &attr, wpipe);
1432 if (attr.name[0] != '\0') {
1433 session->kernel_session->has_non_default_channel = 1;
1434 }
1435 } else {
1436 ret = channel_kernel_enable(session->kernel_session, kchan);
1437 }
1438
1439 if (ret != LTTNG_OK) {
1440 goto error;
1441 }
1442
1443 kernel_wait_quiescent();
1444 break;
1445 }
1446 case LTTNG_DOMAIN_UST:
1447 case LTTNG_DOMAIN_JUL:
1448 case LTTNG_DOMAIN_LOG4J:
1449 case LTTNG_DOMAIN_PYTHON:
1450 {
1451 struct ltt_ust_channel *uchan;
1452
1453 /*
1454 * FIXME
1455 *
1456 * Current agent implementation limitations force us to allow
1457 * only one channel at once in "agent" subdomains. Each
1458 * subdomain has a default channel name which must be strictly
1459 * adhered to.
1460 */
1461 if (domain->type == LTTNG_DOMAIN_JUL) {
1462 if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME,
1463 LTTNG_SYMBOL_NAME_LEN)) {
1464 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1465 goto error;
1466 }
1467 } else if (domain->type == LTTNG_DOMAIN_LOG4J) {
1468 if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME,
1469 LTTNG_SYMBOL_NAME_LEN)) {
1470 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1471 goto error;
1472 }
1473 } else if (domain->type == LTTNG_DOMAIN_PYTHON) {
1474 if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME,
1475 LTTNG_SYMBOL_NAME_LEN)) {
1476 ret = LTTNG_ERR_INVALID_CHANNEL_NAME;
1477 goto error;
1478 }
1479 }
1480
1481 chan_ht = usess->domain_global.channels;
1482
1483 uchan = trace_ust_find_channel_by_name(chan_ht, attr.name);
1484 if (uchan == NULL) {
1485 ret = channel_ust_create(usess, &attr, domain->buf_type);
1486 if (attr.name[0] != '\0') {
1487 usess->has_non_default_channel = 1;
1488 }
1489 } else {
1490 ret = channel_ust_enable(usess, uchan);
1491 }
1492 break;
1493 }
1494 default:
1495 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
1496 goto error;
1497 }
1498
1499 if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) {
1500 session->has_non_mmap_channel = true;
1501 }
1502 error:
1503 rcu_read_unlock();
1504 end:
1505 return ret;
1506 }
1507
1508 enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy(
1509 struct ltt_session *session,
1510 enum lttng_domain_type domain,
1511 enum lttng_process_attr process_attr,
1512 enum lttng_tracking_policy *policy)
1513 {
1514 enum lttng_error_code ret_code = LTTNG_OK;
1515 const struct process_attr_tracker *tracker;
1516
1517 switch (domain) {
1518 case LTTNG_DOMAIN_KERNEL:
1519 if (!session->kernel_session) {
1520 ret_code = LTTNG_ERR_INVALID;
1521 goto end;
1522 }
1523 tracker = kernel_get_process_attr_tracker(
1524 session->kernel_session, process_attr);
1525 break;
1526 case LTTNG_DOMAIN_UST:
1527 if (!session->ust_session) {
1528 ret_code = LTTNG_ERR_INVALID;
1529 goto end;
1530 }
1531 tracker = trace_ust_get_process_attr_tracker(
1532 session->ust_session, process_attr);
1533 break;
1534 default:
1535 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1536 goto end;
1537 }
1538 if (tracker) {
1539 *policy = process_attr_tracker_get_tracking_policy(tracker);
1540 } else {
1541 ret_code = LTTNG_ERR_INVALID;
1542 }
1543 end:
1544 return ret_code;
1545 }
1546
1547 enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy(
1548 struct ltt_session *session,
1549 enum lttng_domain_type domain,
1550 enum lttng_process_attr process_attr,
1551 enum lttng_tracking_policy policy)
1552 {
1553 enum lttng_error_code ret_code = LTTNG_OK;
1554
1555 switch (policy) {
1556 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
1557 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
1558 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
1559 break;
1560 default:
1561 ret_code = LTTNG_ERR_INVALID;
1562 goto end;
1563 }
1564
1565 switch (domain) {
1566 case LTTNG_DOMAIN_KERNEL:
1567 if (!session->kernel_session) {
1568 ret_code = LTTNG_ERR_INVALID;
1569 goto end;
1570 }
1571 ret_code = kernel_process_attr_tracker_set_tracking_policy(
1572 session->kernel_session, process_attr, policy);
1573 break;
1574 case LTTNG_DOMAIN_UST:
1575 if (!session->ust_session) {
1576 ret_code = LTTNG_ERR_INVALID;
1577 goto end;
1578 }
1579 ret_code = trace_ust_process_attr_tracker_set_tracking_policy(
1580 session->ust_session, process_attr, policy);
1581 break;
1582 default:
1583 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1584 break;
1585 }
1586 end:
1587 return ret_code;
1588 }
1589
1590 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value(
1591 struct ltt_session *session,
1592 enum lttng_domain_type domain,
1593 enum lttng_process_attr process_attr,
1594 const struct process_attr_value *value)
1595 {
1596 enum lttng_error_code ret_code = LTTNG_OK;
1597
1598 switch (domain) {
1599 case LTTNG_DOMAIN_KERNEL:
1600 if (!session->kernel_session) {
1601 ret_code = LTTNG_ERR_INVALID;
1602 goto end;
1603 }
1604 ret_code = kernel_process_attr_tracker_inclusion_set_add_value(
1605 session->kernel_session, process_attr, value);
1606 break;
1607 case LTTNG_DOMAIN_UST:
1608 if (!session->ust_session) {
1609 ret_code = LTTNG_ERR_INVALID;
1610 goto end;
1611 }
1612 ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value(
1613 session->ust_session, process_attr, value);
1614 break;
1615 default:
1616 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1617 break;
1618 }
1619 end:
1620 return ret_code;
1621 }
1622
1623 enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value(
1624 struct ltt_session *session,
1625 enum lttng_domain_type domain,
1626 enum lttng_process_attr process_attr,
1627 const struct process_attr_value *value)
1628 {
1629 enum lttng_error_code ret_code = LTTNG_OK;
1630
1631 switch (domain) {
1632 case LTTNG_DOMAIN_KERNEL:
1633 if (!session->kernel_session) {
1634 ret_code = LTTNG_ERR_INVALID;
1635 goto end;
1636 }
1637 ret_code = kernel_process_attr_tracker_inclusion_set_remove_value(
1638 session->kernel_session, process_attr, value);
1639 break;
1640 case LTTNG_DOMAIN_UST:
1641 if (!session->ust_session) {
1642 ret_code = LTTNG_ERR_INVALID;
1643 goto end;
1644 }
1645 ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value(
1646 session->ust_session, process_attr, value);
1647 break;
1648 default:
1649 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1650 break;
1651 }
1652 end:
1653 return ret_code;
1654 }
1655
1656 enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set(
1657 struct ltt_session *session,
1658 enum lttng_domain_type domain,
1659 enum lttng_process_attr process_attr,
1660 struct lttng_process_attr_values **values)
1661 {
1662 enum lttng_error_code ret_code = LTTNG_OK;
1663 const struct process_attr_tracker *tracker;
1664 enum process_attr_tracker_status status;
1665
1666 switch (domain) {
1667 case LTTNG_DOMAIN_KERNEL:
1668 if (!session->kernel_session) {
1669 ret_code = LTTNG_ERR_INVALID;
1670 goto end;
1671 }
1672 tracker = kernel_get_process_attr_tracker(
1673 session->kernel_session, process_attr);
1674 break;
1675 case LTTNG_DOMAIN_UST:
1676 if (!session->ust_session) {
1677 ret_code = LTTNG_ERR_INVALID;
1678 goto end;
1679 }
1680 tracker = trace_ust_get_process_attr_tracker(
1681 session->ust_session, process_attr);
1682 break;
1683 default:
1684 ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN;
1685 goto end;
1686 }
1687
1688 if (!tracker) {
1689 ret_code = LTTNG_ERR_INVALID;
1690 goto end;
1691 }
1692
1693 status = process_attr_tracker_get_inclusion_set(tracker, values);
1694 switch (status) {
1695 case PROCESS_ATTR_TRACKER_STATUS_OK:
1696 ret_code = LTTNG_OK;
1697 break;
1698 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1699 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1700 break;
1701 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1702 ret_code = LTTNG_ERR_NOMEM;
1703 break;
1704 default:
1705 ret_code = LTTNG_ERR_UNK;
1706 break;
1707 }
1708
1709 end:
1710 return ret_code;
1711 }
1712
1713 /*
1714 * Command LTTNG_DISABLE_EVENT processed by the client thread.
1715 */
1716 int cmd_disable_event(struct ltt_session *session,
1717 enum lttng_domain_type domain, const char *channel_name,
1718 const struct lttng_event *event)
1719 {
1720 int ret;
1721 const char *event_name;
1722
1723 DBG("Disable event command for event \'%s\'", event->name);
1724
1725 event_name = event->name;
1726
1727 /* Error out on unhandled search criteria */
1728 if (event->loglevel_type || event->loglevel != -1 || event->enabled
1729 || event->pid || event->filter || event->exclusion) {
1730 ret = LTTNG_ERR_UNK;
1731 goto error;
1732 }
1733
1734 rcu_read_lock();
1735
1736 switch (domain) {
1737 case LTTNG_DOMAIN_KERNEL:
1738 {
1739 struct ltt_kernel_channel *kchan;
1740 struct ltt_kernel_session *ksess;
1741
1742 ksess = session->kernel_session;
1743
1744 /*
1745 * If a non-default channel has been created in the
1746 * session, explicitely require that -c chan_name needs
1747 * to be provided.
1748 */
1749 if (ksess->has_non_default_channel && channel_name[0] == '\0') {
1750 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1751 goto error_unlock;
1752 }
1753
1754 kchan = trace_kernel_get_channel_by_name(channel_name, ksess);
1755 if (kchan == NULL) {
1756 ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND;
1757 goto error_unlock;
1758 }
1759
1760 switch (event->type) {
1761 case LTTNG_EVENT_ALL:
1762 case LTTNG_EVENT_TRACEPOINT:
1763 case LTTNG_EVENT_SYSCALL:
1764 case LTTNG_EVENT_PROBE:
1765 case LTTNG_EVENT_FUNCTION:
1766 case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */
1767 if (event_name[0] == '\0') {
1768 ret = event_kernel_disable_event(kchan,
1769 NULL, event->type);
1770 } else {
1771 ret = event_kernel_disable_event(kchan,
1772 event_name, event->type);
1773 }
1774 if (ret != LTTNG_OK) {
1775 goto error_unlock;
1776 }
1777 break;
1778 default:
1779 ret = LTTNG_ERR_UNK;
1780 goto error_unlock;
1781 }
1782
1783 kernel_wait_quiescent();
1784 break;
1785 }
1786 case LTTNG_DOMAIN_UST:
1787 {
1788 struct ltt_ust_channel *uchan;
1789 struct ltt_ust_session *usess;
1790
1791 usess = session->ust_session;
1792
1793 if (validate_ust_event_name(event_name)) {
1794 ret = LTTNG_ERR_INVALID_EVENT_NAME;
1795 goto error_unlock;
1796 }
1797
1798 /*
1799 * If a non-default channel has been created in the
1800 * session, explicitly require that -c chan_name needs
1801 * to be provided.
1802 */
1803 if (usess->has_non_default_channel && channel_name[0] == '\0') {
1804 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
1805 goto error_unlock;
1806 }
1807
1808 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
1809 channel_name);
1810 if (uchan == NULL) {
1811 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1812 goto error_unlock;
1813 }
1814
1815 switch (event->type) {
1816 case LTTNG_EVENT_ALL:
1817 /*
1818 * An empty event name means that everything
1819 * should be disabled.
1820 */
1821 if (event->name[0] == '\0') {
1822 ret = event_ust_disable_all_tracepoints(usess, uchan);
1823 } else {
1824 ret = event_ust_disable_tracepoint(usess, uchan,
1825 event_name);
1826 }
1827 if (ret != LTTNG_OK) {
1828 goto error_unlock;
1829 }
1830 break;
1831 default:
1832 ret = LTTNG_ERR_UNK;
1833 goto error_unlock;
1834 }
1835
1836 DBG3("Disable UST event %s in channel %s completed", event_name,
1837 channel_name);
1838 break;
1839 }
1840 case LTTNG_DOMAIN_LOG4J:
1841 case LTTNG_DOMAIN_JUL:
1842 case LTTNG_DOMAIN_PYTHON:
1843 {
1844 struct agent *agt;
1845 struct ltt_ust_session *usess = session->ust_session;
1846
1847 assert(usess);
1848
1849 switch (event->type) {
1850 case LTTNG_EVENT_ALL:
1851 break;
1852 default:
1853 ret = LTTNG_ERR_UNK;
1854 goto error_unlock;
1855 }
1856
1857 agt = trace_ust_find_agent(usess, domain);
1858 if (!agt) {
1859 ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND;
1860 goto error_unlock;
1861 }
1862 /*
1863 * An empty event name means that everything
1864 * should be disabled.
1865 */
1866 if (event->name[0] == '\0') {
1867 ret = event_agent_disable_all(usess, agt);
1868 } else {
1869 ret = event_agent_disable(usess, agt, event_name);
1870 }
1871 if (ret != LTTNG_OK) {
1872 goto error_unlock;
1873 }
1874
1875 break;
1876 }
1877 default:
1878 ret = LTTNG_ERR_UND;
1879 goto error_unlock;
1880 }
1881
1882 ret = LTTNG_OK;
1883
1884 error_unlock:
1885 rcu_read_unlock();
1886 error:
1887 return ret;
1888 }
1889
1890 /*
1891 * Command LTTNG_ADD_CONTEXT processed by the client thread.
1892 */
1893 int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain,
1894 char *channel_name, const struct lttng_event_context *ctx, int kwpipe)
1895 {
1896 int ret, chan_kern_created = 0, chan_ust_created = 0;
1897 char *app_ctx_provider_name = NULL, *app_ctx_name = NULL;
1898
1899 /*
1900 * Don't try to add a context if the session has been started at
1901 * some point in time before. The tracer does not allow it and would
1902 * result in a corrupted trace.
1903 */
1904 if (session->has_been_started) {
1905 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1906 goto end;
1907 }
1908
1909 if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) {
1910 app_ctx_provider_name = ctx->u.app_ctx.provider_name;
1911 app_ctx_name = ctx->u.app_ctx.ctx_name;
1912 }
1913
1914 switch (domain) {
1915 case LTTNG_DOMAIN_KERNEL:
1916 assert(session->kernel_session);
1917
1918 if (session->kernel_session->channel_count == 0) {
1919 /* Create default channel */
1920 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1921 if (ret != LTTNG_OK) {
1922 goto error;
1923 }
1924 chan_kern_created = 1;
1925 }
1926 /* Add kernel context to kernel tracer */
1927 ret = context_kernel_add(session->kernel_session, ctx, channel_name);
1928 if (ret != LTTNG_OK) {
1929 goto error;
1930 }
1931 break;
1932 case LTTNG_DOMAIN_JUL:
1933 case LTTNG_DOMAIN_LOG4J:
1934 {
1935 /*
1936 * Validate channel name.
1937 * If no channel name is given and the domain is JUL or LOG4J,
1938 * set it to the appropriate domain-specific channel name. If
1939 * a name is provided but does not match the expexted channel
1940 * name, return an error.
1941 */
1942 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1943 strcmp(channel_name,
1944 DEFAULT_JUL_CHANNEL_NAME)) {
1945 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1946 goto error;
1947 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1948 strcmp(channel_name,
1949 DEFAULT_LOG4J_CHANNEL_NAME)) {
1950 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1951 goto error;
1952 }
1953 /* break is _not_ missing here. */
1954 }
1955 case LTTNG_DOMAIN_UST:
1956 {
1957 struct ltt_ust_session *usess = session->ust_session;
1958 unsigned int chan_count;
1959
1960 assert(usess);
1961
1962 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1963 if (chan_count == 0) {
1964 struct lttng_channel *attr;
1965 /* Create default channel */
1966 attr = channel_new_default_attr(domain, usess->buffer_type);
1967 if (attr == NULL) {
1968 ret = LTTNG_ERR_FATAL;
1969 goto error;
1970 }
1971
1972 ret = channel_ust_create(usess, attr, usess->buffer_type);
1973 if (ret != LTTNG_OK) {
1974 free(attr);
1975 goto error;
1976 }
1977 channel_attr_destroy(attr);
1978 chan_ust_created = 1;
1979 }
1980
1981 ret = context_ust_add(usess, domain, ctx, channel_name);
1982 free(app_ctx_provider_name);
1983 free(app_ctx_name);
1984 app_ctx_name = NULL;
1985 app_ctx_provider_name = NULL;
1986 if (ret != LTTNG_OK) {
1987 goto error;
1988 }
1989 break;
1990 }
1991 default:
1992 ret = LTTNG_ERR_UND;
1993 goto error;
1994 }
1995
1996 ret = LTTNG_OK;
1997 goto end;
1998
1999 error:
2000 if (chan_kern_created) {
2001 struct ltt_kernel_channel *kchan =
2002 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
2003 session->kernel_session);
2004 /* Created previously, this should NOT fail. */
2005 assert(kchan);
2006 kernel_destroy_channel(kchan);
2007 }
2008
2009 if (chan_ust_created) {
2010 struct ltt_ust_channel *uchan =
2011 trace_ust_find_channel_by_name(
2012 session->ust_session->domain_global.channels,
2013 DEFAULT_CHANNEL_NAME);
2014 /* Created previously, this should NOT fail. */
2015 assert(uchan);
2016 /* Remove from the channel list of the session. */
2017 trace_ust_delete_channel(session->ust_session->domain_global.channels,
2018 uchan);
2019 trace_ust_destroy_channel(uchan);
2020 }
2021 end:
2022 free(app_ctx_provider_name);
2023 free(app_ctx_name);
2024 return ret;
2025 }
2026
2027 static inline bool name_starts_with(const char *name, const char *prefix)
2028 {
2029 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
2030
2031 return !strncmp(name, prefix, max_cmp_len);
2032 }
2033
2034 /* Perform userspace-specific event name validation */
2035 static int validate_ust_event_name(const char *name)
2036 {
2037 int ret = 0;
2038
2039 if (!name) {
2040 ret = -1;
2041 goto end;
2042 }
2043
2044 /*
2045 * Check name against all internal UST event component namespaces used
2046 * by the agents.
2047 */
2048 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2049 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2050 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2051 ret = -1;
2052 }
2053
2054 end:
2055 return ret;
2056 }
2057
2058 /*
2059 * Internal version of cmd_enable_event() with a supplemental
2060 * "internal_event" flag which is used to enable internal events which should
2061 * be hidden from clients. Such events are used in the agent implementation to
2062 * enable the events through which all "agent" events are funeled.
2063 */
2064 static int _cmd_enable_event(struct ltt_session *session,
2065 const struct lttng_domain *domain,
2066 char *channel_name, struct lttng_event *event,
2067 char *filter_expression,
2068 struct lttng_bytecode *filter,
2069 struct lttng_event_exclusion *exclusion,
2070 int wpipe, bool internal_event)
2071 {
2072 int ret = 0, channel_created = 0;
2073 struct lttng_channel *attr = NULL;
2074
2075 assert(session);
2076 assert(event);
2077 assert(channel_name);
2078
2079 /* If we have a filter, we must have its filter expression */
2080 assert(!(!!filter_expression ^ !!filter));
2081
2082 /* Normalize event name as a globbing pattern */
2083 strutils_normalize_star_glob_pattern(event->name);
2084
2085 /* Normalize exclusion names as globbing patterns */
2086 if (exclusion) {
2087 size_t i;
2088
2089 for (i = 0; i < exclusion->count; i++) {
2090 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2091
2092 strutils_normalize_star_glob_pattern(name);
2093 }
2094 }
2095
2096 DBG("Enable event command for event \'%s\'", event->name);
2097
2098 rcu_read_lock();
2099
2100 switch (domain->type) {
2101 case LTTNG_DOMAIN_KERNEL:
2102 {
2103 struct ltt_kernel_channel *kchan;
2104
2105 /*
2106 * If a non-default channel has been created in the
2107 * session, explicitely require that -c chan_name needs
2108 * to be provided.
2109 */
2110 if (session->kernel_session->has_non_default_channel
2111 && channel_name[0] == '\0') {
2112 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2113 goto error;
2114 }
2115
2116 kchan = trace_kernel_get_channel_by_name(channel_name,
2117 session->kernel_session);
2118 if (kchan == NULL) {
2119 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2120 LTTNG_BUFFER_GLOBAL);
2121 if (attr == NULL) {
2122 ret = LTTNG_ERR_FATAL;
2123 goto error;
2124 }
2125 if (lttng_strncpy(attr->name, channel_name,
2126 sizeof(attr->name))) {
2127 ret = LTTNG_ERR_INVALID;
2128 goto error;
2129 }
2130
2131 ret = cmd_enable_channel(session, domain, attr, wpipe);
2132 if (ret != LTTNG_OK) {
2133 goto error;
2134 }
2135 channel_created = 1;
2136 }
2137
2138 /* Get the newly created kernel channel pointer */
2139 kchan = trace_kernel_get_channel_by_name(channel_name,
2140 session->kernel_session);
2141 if (kchan == NULL) {
2142 /* This sould not happen... */
2143 ret = LTTNG_ERR_FATAL;
2144 goto error;
2145 }
2146
2147 switch (event->type) {
2148 case LTTNG_EVENT_ALL:
2149 {
2150 char *filter_expression_a = NULL;
2151 struct lttng_bytecode *filter_a = NULL;
2152
2153 /*
2154 * We need to duplicate filter_expression and filter,
2155 * because ownership is passed to first enable
2156 * event.
2157 */
2158 if (filter_expression) {
2159 filter_expression_a = strdup(filter_expression);
2160 if (!filter_expression_a) {
2161 ret = LTTNG_ERR_FATAL;
2162 goto error;
2163 }
2164 }
2165 if (filter) {
2166 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2167 if (!filter_a) {
2168 free(filter_expression_a);
2169 ret = LTTNG_ERR_FATAL;
2170 goto error;
2171 }
2172 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2173 }
2174 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2175 ret = event_kernel_enable_event(kchan, event,
2176 filter_expression, filter);
2177 /* We have passed ownership */
2178 filter_expression = NULL;
2179 filter = NULL;
2180 if (ret != LTTNG_OK) {
2181 if (channel_created) {
2182 /* Let's not leak a useless channel. */
2183 kernel_destroy_channel(kchan);
2184 }
2185 free(filter_expression_a);
2186 free(filter_a);
2187 goto error;
2188 }
2189 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2190 ret = event_kernel_enable_event(kchan, event,
2191 filter_expression_a, filter_a);
2192 /* We have passed ownership */
2193 filter_expression_a = NULL;
2194 filter_a = NULL;
2195 if (ret != LTTNG_OK) {
2196 goto error;
2197 }
2198 break;
2199 }
2200 case LTTNG_EVENT_PROBE:
2201 case LTTNG_EVENT_USERSPACE_PROBE:
2202 case LTTNG_EVENT_FUNCTION:
2203 case LTTNG_EVENT_FUNCTION_ENTRY:
2204 case LTTNG_EVENT_TRACEPOINT:
2205 ret = event_kernel_enable_event(kchan, event,
2206 filter_expression, filter);
2207 /* We have passed ownership */
2208 filter_expression = NULL;
2209 filter = NULL;
2210 if (ret != LTTNG_OK) {
2211 if (channel_created) {
2212 /* Let's not leak a useless channel. */
2213 kernel_destroy_channel(kchan);
2214 }
2215 goto error;
2216 }
2217 break;
2218 case LTTNG_EVENT_SYSCALL:
2219 ret = event_kernel_enable_event(kchan, event,
2220 filter_expression, filter);
2221 /* We have passed ownership */
2222 filter_expression = NULL;
2223 filter = NULL;
2224 if (ret != LTTNG_OK) {
2225 goto error;
2226 }
2227 break;
2228 default:
2229 ret = LTTNG_ERR_UNK;
2230 goto error;
2231 }
2232
2233 kernel_wait_quiescent();
2234 break;
2235 }
2236 case LTTNG_DOMAIN_UST:
2237 {
2238 struct ltt_ust_channel *uchan;
2239 struct ltt_ust_session *usess = session->ust_session;
2240
2241 assert(usess);
2242
2243 /*
2244 * If a non-default channel has been created in the
2245 * session, explicitely require that -c chan_name needs
2246 * to be provided.
2247 */
2248 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2249 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2250 goto error;
2251 }
2252
2253 /* Get channel from global UST domain */
2254 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2255 channel_name);
2256 if (uchan == NULL) {
2257 /* Create default channel */
2258 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2259 usess->buffer_type);
2260 if (attr == NULL) {
2261 ret = LTTNG_ERR_FATAL;
2262 goto error;
2263 }
2264 if (lttng_strncpy(attr->name, channel_name,
2265 sizeof(attr->name))) {
2266 ret = LTTNG_ERR_INVALID;
2267 goto error;
2268 }
2269
2270 ret = cmd_enable_channel(session, domain, attr, wpipe);
2271 if (ret != LTTNG_OK) {
2272 goto error;
2273 }
2274
2275 /* Get the newly created channel reference back */
2276 uchan = trace_ust_find_channel_by_name(
2277 usess->domain_global.channels, channel_name);
2278 assert(uchan);
2279 }
2280
2281 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2282 /*
2283 * Don't allow users to add UST events to channels which
2284 * are assigned to a userspace subdomain (JUL, Log4J,
2285 * Python, etc.).
2286 */
2287 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2288 goto error;
2289 }
2290
2291 if (!internal_event) {
2292 /*
2293 * Ensure the event name is not reserved for internal
2294 * use.
2295 */
2296 ret = validate_ust_event_name(event->name);
2297 if (ret) {
2298 WARN("Userspace event name %s failed validation.",
2299 event->name);
2300 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2301 goto error;
2302 }
2303 }
2304
2305 /* At this point, the session and channel exist on the tracer */
2306 ret = event_ust_enable_tracepoint(usess, uchan, event,
2307 filter_expression, filter, exclusion,
2308 internal_event);
2309 /* We have passed ownership */
2310 filter_expression = NULL;
2311 filter = NULL;
2312 exclusion = NULL;
2313 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2314 goto already_enabled;
2315 } else if (ret != LTTNG_OK) {
2316 goto error;
2317 }
2318 break;
2319 }
2320 case LTTNG_DOMAIN_LOG4J:
2321 case LTTNG_DOMAIN_JUL:
2322 case LTTNG_DOMAIN_PYTHON:
2323 {
2324 const char *default_event_name, *default_chan_name;
2325 struct agent *agt;
2326 struct lttng_event uevent;
2327 struct lttng_domain tmp_dom;
2328 struct ltt_ust_session *usess = session->ust_session;
2329
2330 assert(usess);
2331
2332 if (!agent_tracing_is_enabled()) {
2333 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2334 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2335 goto error;
2336 }
2337
2338 agt = trace_ust_find_agent(usess, domain->type);
2339 if (!agt) {
2340 agt = agent_create(domain->type);
2341 if (!agt) {
2342 ret = LTTNG_ERR_NOMEM;
2343 goto error;
2344 }
2345 agent_add(agt, usess->agents);
2346 }
2347
2348 /* Create the default tracepoint. */
2349 memset(&uevent, 0, sizeof(uevent));
2350 uevent.type = LTTNG_EVENT_TRACEPOINT;
2351 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2352 default_event_name = event_get_default_agent_ust_name(
2353 domain->type);
2354 if (!default_event_name) {
2355 ret = LTTNG_ERR_FATAL;
2356 goto error;
2357 }
2358 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2359 uevent.name[sizeof(uevent.name) - 1] = '\0';
2360
2361 /*
2362 * The domain type is changed because we are about to enable the
2363 * default channel and event for the JUL domain that are hardcoded.
2364 * This happens in the UST domain.
2365 */
2366 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2367 tmp_dom.type = LTTNG_DOMAIN_UST;
2368
2369 switch (domain->type) {
2370 case LTTNG_DOMAIN_LOG4J:
2371 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2372 break;
2373 case LTTNG_DOMAIN_JUL:
2374 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2375 break;
2376 case LTTNG_DOMAIN_PYTHON:
2377 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2378 break;
2379 default:
2380 /* The switch/case we are in makes this impossible */
2381 assert(0);
2382 }
2383
2384 {
2385 char *filter_expression_copy = NULL;
2386 struct lttng_bytecode *filter_copy = NULL;
2387
2388 if (filter) {
2389 const size_t filter_size = sizeof(
2390 struct lttng_bytecode)
2391 + filter->len;
2392
2393 filter_copy = zmalloc(filter_size);
2394 if (!filter_copy) {
2395 ret = LTTNG_ERR_NOMEM;
2396 goto error;
2397 }
2398 memcpy(filter_copy, filter, filter_size);
2399
2400 filter_expression_copy =
2401 strdup(filter_expression);
2402 if (!filter_expression) {
2403 ret = LTTNG_ERR_NOMEM;
2404 }
2405
2406 if (!filter_expression_copy || !filter_copy) {
2407 free(filter_expression_copy);
2408 free(filter_copy);
2409 goto error;
2410 }
2411 }
2412
2413 ret = cmd_enable_event_internal(session, &tmp_dom,
2414 (char *) default_chan_name,
2415 &uevent, filter_expression_copy,
2416 filter_copy, NULL, wpipe);
2417 }
2418
2419 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2420 goto already_enabled;
2421 } else if (ret != LTTNG_OK) {
2422 goto error;
2423 }
2424
2425 /* The wild card * means that everything should be enabled. */
2426 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2427 ret = event_agent_enable_all(usess, agt, event, filter,
2428 filter_expression);
2429 } else {
2430 ret = event_agent_enable(usess, agt, event, filter,
2431 filter_expression);
2432 }
2433 filter = NULL;
2434 filter_expression = NULL;
2435 if (ret != LTTNG_OK) {
2436 goto error;
2437 }
2438
2439 break;
2440 }
2441 default:
2442 ret = LTTNG_ERR_UND;
2443 goto error;
2444 }
2445
2446 ret = LTTNG_OK;
2447
2448 already_enabled:
2449 error:
2450 free(filter_expression);
2451 free(filter);
2452 free(exclusion);
2453 channel_attr_destroy(attr);
2454 rcu_read_unlock();
2455 return ret;
2456 }
2457
2458 /*
2459 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2460 * We own filter, exclusion, and filter_expression.
2461 */
2462 int cmd_enable_event(struct ltt_session *session,
2463 const struct lttng_domain *domain,
2464 char *channel_name, struct lttng_event *event,
2465 char *filter_expression,
2466 struct lttng_bytecode *filter,
2467 struct lttng_event_exclusion *exclusion,
2468 int wpipe)
2469 {
2470 return _cmd_enable_event(session, domain, channel_name, event,
2471 filter_expression, filter, exclusion, wpipe, false);
2472 }
2473
2474 /*
2475 * Enable an event which is internal to LTTng. An internal should
2476 * never be made visible to clients and are immune to checks such as
2477 * reserved names.
2478 */
2479 static int cmd_enable_event_internal(struct ltt_session *session,
2480 const struct lttng_domain *domain,
2481 char *channel_name, struct lttng_event *event,
2482 char *filter_expression,
2483 struct lttng_bytecode *filter,
2484 struct lttng_event_exclusion *exclusion,
2485 int wpipe)
2486 {
2487 return _cmd_enable_event(session, domain, channel_name, event,
2488 filter_expression, filter, exclusion, wpipe, true);
2489 }
2490
2491 /*
2492 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2493 */
2494 ssize_t cmd_list_tracepoints(enum lttng_domain_type domain,
2495 struct lttng_event **events)
2496 {
2497 int ret;
2498 ssize_t nb_events = 0;
2499
2500 switch (domain) {
2501 case LTTNG_DOMAIN_KERNEL:
2502 nb_events = kernel_list_events(events);
2503 if (nb_events < 0) {
2504 ret = LTTNG_ERR_KERN_LIST_FAIL;
2505 goto error;
2506 }
2507 break;
2508 case LTTNG_DOMAIN_UST:
2509 nb_events = ust_app_list_events(events);
2510 if (nb_events < 0) {
2511 ret = LTTNG_ERR_UST_LIST_FAIL;
2512 goto error;
2513 }
2514 break;
2515 case LTTNG_DOMAIN_LOG4J:
2516 case LTTNG_DOMAIN_JUL:
2517 case LTTNG_DOMAIN_PYTHON:
2518 nb_events = agent_list_events(events, domain);
2519 if (nb_events < 0) {
2520 ret = LTTNG_ERR_UST_LIST_FAIL;
2521 goto error;
2522 }
2523 break;
2524 default:
2525 ret = LTTNG_ERR_UND;
2526 goto error;
2527 }
2528
2529 return nb_events;
2530
2531 error:
2532 /* Return negative value to differentiate return code */
2533 return -ret;
2534 }
2535
2536 /*
2537 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2538 */
2539 ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2540 struct lttng_event_field **fields)
2541 {
2542 int ret;
2543 ssize_t nb_fields = 0;
2544
2545 switch (domain) {
2546 case LTTNG_DOMAIN_UST:
2547 nb_fields = ust_app_list_event_fields(fields);
2548 if (nb_fields < 0) {
2549 ret = LTTNG_ERR_UST_LIST_FAIL;
2550 goto error;
2551 }
2552 break;
2553 case LTTNG_DOMAIN_KERNEL:
2554 default: /* fall-through */
2555 ret = LTTNG_ERR_UND;
2556 goto error;
2557 }
2558
2559 return nb_fields;
2560
2561 error:
2562 /* Return negative value to differentiate return code */
2563 return -ret;
2564 }
2565
2566 ssize_t cmd_list_syscalls(struct lttng_event **events)
2567 {
2568 return syscall_table_list(events);
2569 }
2570
2571 /*
2572 * Command LTTNG_START_TRACE processed by the client thread.
2573 *
2574 * Called with session mutex held.
2575 */
2576 int cmd_start_trace(struct ltt_session *session)
2577 {
2578 enum lttng_error_code ret;
2579 unsigned long nb_chan = 0;
2580 struct ltt_kernel_session *ksession;
2581 struct ltt_ust_session *usess;
2582 const bool session_rotated_after_last_stop =
2583 session->rotated_after_last_stop;
2584 const bool session_cleared_after_last_stop =
2585 session->cleared_after_last_stop;
2586
2587 assert(session);
2588
2589 /* Ease our life a bit ;) */
2590 ksession = session->kernel_session;
2591 usess = session->ust_session;
2592
2593 /* Is the session already started? */
2594 if (session->active) {
2595 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2596 /* Perform nothing */
2597 goto end;
2598 }
2599
2600 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2601 !session->current_trace_chunk) {
2602 /*
2603 * A rotation was launched while the session was stopped and
2604 * it has not been completed yet. It is not possible to start
2605 * the session since starting the session here would require a
2606 * rotation from "NULL" to a new trace chunk. That rotation
2607 * would overlap with the ongoing rotation, which is not
2608 * supported.
2609 */
2610 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2611 session->name);
2612 ret = LTTNG_ERR_ROTATION_PENDING;
2613 goto error;
2614 }
2615
2616 /*
2617 * Starting a session without channel is useless since after that it's not
2618 * possible to enable channel thus inform the client.
2619 */
2620 if (usess && usess->domain_global.channels) {
2621 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2622 }
2623 if (ksession) {
2624 nb_chan += ksession->channel_count;
2625 }
2626 if (!nb_chan) {
2627 ret = LTTNG_ERR_NO_CHANNEL;
2628 goto error;
2629 }
2630
2631 session->active = 1;
2632 session->rotated_after_last_stop = false;
2633 session->cleared_after_last_stop = false;
2634 if (session->output_traces && !session->current_trace_chunk) {
2635 if (!session->has_been_started) {
2636 struct lttng_trace_chunk *trace_chunk;
2637
2638 DBG("Creating initial trace chunk of session \"%s\"",
2639 session->name);
2640 trace_chunk = session_create_new_trace_chunk(
2641 session, NULL, NULL, NULL);
2642 if (!trace_chunk) {
2643 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2644 goto error;
2645 }
2646 assert(!session->current_trace_chunk);
2647 ret = session_set_trace_chunk(session, trace_chunk,
2648 NULL);
2649 lttng_trace_chunk_put(trace_chunk);
2650 if (ret) {
2651 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2652 goto error;
2653 }
2654 } else {
2655 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2656 session->name);
2657 /*
2658 * Rotate existing streams into the new chunk.
2659 * This is a "quiet" rotation has no client has
2660 * explicitly requested this operation.
2661 *
2662 * There is also no need to wait for the rotation
2663 * to complete as it will happen immediately. No data
2664 * was produced as the session was stopped, so the
2665 * rotation should happen on reception of the command.
2666 */
2667 ret = cmd_rotate_session(session, NULL, true,
2668 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2669 if (ret != LTTNG_OK) {
2670 goto error;
2671 }
2672 }
2673 }
2674
2675 /* Kernel tracing */
2676 if (ksession != NULL) {
2677 DBG("Start kernel tracing session %s", session->name);
2678 ret = start_kernel_session(ksession);
2679 if (ret != LTTNG_OK) {
2680 goto error;
2681 }
2682 }
2683
2684 /* Flag session that trace should start automatically */
2685 if (usess) {
2686 int int_ret = ust_app_start_trace_all(usess);
2687
2688 if (int_ret < 0) {
2689 ret = LTTNG_ERR_UST_START_FAIL;
2690 goto error;
2691 }
2692 }
2693
2694 /*
2695 * Open a packet in every stream of the session to ensure that viewers
2696 * can correctly identify the boundaries of the periods during which
2697 * tracing was active for this session.
2698 */
2699 ret = session_open_packets(session);
2700 if (ret != LTTNG_OK) {
2701 goto error;
2702 }
2703
2704 /*
2705 * Clear the flag that indicates that a rotation was done while the
2706 * session was stopped.
2707 */
2708 session->rotated_after_last_stop = false;
2709
2710 if (session->rotate_timer_period) {
2711 int int_ret = timer_session_rotation_schedule_timer_start(
2712 session, session->rotate_timer_period);
2713
2714 if (int_ret < 0) {
2715 ERR("Failed to enable rotate timer");
2716 ret = LTTNG_ERR_UNK;
2717 goto error;
2718 }
2719 }
2720
2721 ret = LTTNG_OK;
2722
2723 error:
2724 if (ret == LTTNG_OK) {
2725 /* Flag this after a successful start. */
2726 session->has_been_started |= 1;
2727 } else {
2728 session->active = 0;
2729 /* Restore initial state on error. */
2730 session->rotated_after_last_stop =
2731 session_rotated_after_last_stop;
2732 session->cleared_after_last_stop =
2733 session_cleared_after_last_stop;
2734 }
2735 end:
2736 return ret;
2737 }
2738
2739 /*
2740 * Command LTTNG_STOP_TRACE processed by the client thread.
2741 */
2742 int cmd_stop_trace(struct ltt_session *session)
2743 {
2744 int ret;
2745 struct ltt_kernel_session *ksession;
2746 struct ltt_ust_session *usess;
2747
2748 assert(session);
2749
2750 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2751 /* Short cut */
2752 ksession = session->kernel_session;
2753 usess = session->ust_session;
2754
2755 /* Session is not active. Skip everythong and inform the client. */
2756 if (!session->active) {
2757 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2758 goto error;
2759 }
2760
2761 ret = stop_kernel_session(ksession);
2762 if (ret != LTTNG_OK) {
2763 goto error;
2764 }
2765
2766 if (usess && usess->active) {
2767 ret = ust_app_stop_trace_all(usess);
2768 if (ret < 0) {
2769 ret = LTTNG_ERR_UST_STOP_FAIL;
2770 goto error;
2771 }
2772 }
2773
2774 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2775 session->id);
2776 /* Flag inactive after a successful stop. */
2777 session->active = 0;
2778 ret = LTTNG_OK;
2779
2780 error:
2781 return ret;
2782 }
2783
2784 /*
2785 * Set the base_path of the session only if subdir of a control uris is set.
2786 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2787 */
2788 static int set_session_base_path_from_uris(struct ltt_session *session,
2789 size_t nb_uri,
2790 struct lttng_uri *uris)
2791 {
2792 int ret;
2793 size_t i;
2794
2795 for (i = 0; i < nb_uri; i++) {
2796 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2797 uris[i].subdir[0] == '\0') {
2798 /* Not interested in these URIs */
2799 continue;
2800 }
2801
2802 if (session->base_path != NULL) {
2803 free(session->base_path);
2804 session->base_path = NULL;
2805 }
2806
2807 /* Set session base_path */
2808 session->base_path = strdup(uris[i].subdir);
2809 if (!session->base_path) {
2810 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2811 uris[i].subdir, session->name);
2812 ret = LTTNG_ERR_NOMEM;
2813 goto error;
2814 }
2815 DBG2("Setting base path \"%s\" for session \"%s\"",
2816 session->base_path, session->name);
2817 }
2818 ret = LTTNG_OK;
2819 error:
2820 return ret;
2821 }
2822
2823 /*
2824 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2825 */
2826 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2827 struct lttng_uri *uris)
2828 {
2829 int ret, i;
2830 struct ltt_kernel_session *ksess = session->kernel_session;
2831 struct ltt_ust_session *usess = session->ust_session;
2832
2833 assert(session);
2834 assert(uris);
2835 assert(nb_uri > 0);
2836
2837 /* Can't set consumer URI if the session is active. */
2838 if (session->active) {
2839 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2840 goto error;
2841 }
2842
2843 /*
2844 * Set the session base path if any. This is done inside
2845 * cmd_set_consumer_uri to preserve backward compatibility of the
2846 * previous session creation api vs the session descriptor api.
2847 */
2848 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2849 if (ret != LTTNG_OK) {
2850 goto error;
2851 }
2852
2853 /* Set the "global" consumer URIs */
2854 for (i = 0; i < nb_uri; i++) {
2855 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2856 LTTNG_DOMAIN_NONE);
2857 if (ret != LTTNG_OK) {
2858 goto error;
2859 }
2860 }
2861
2862 /* Set UST session URIs */
2863 if (session->ust_session) {
2864 for (i = 0; i < nb_uri; i++) {
2865 ret = add_uri_to_consumer(session,
2866 session->ust_session->consumer,
2867 &uris[i], LTTNG_DOMAIN_UST);
2868 if (ret != LTTNG_OK) {
2869 goto error;
2870 }
2871 }
2872 }
2873
2874 /* Set kernel session URIs */
2875 if (session->kernel_session) {
2876 for (i = 0; i < nb_uri; i++) {
2877 ret = add_uri_to_consumer(session,
2878 session->kernel_session->consumer,
2879 &uris[i], LTTNG_DOMAIN_KERNEL);
2880 if (ret != LTTNG_OK) {
2881 goto error;
2882 }
2883 }
2884 }
2885
2886 /*
2887 * Make sure to set the session in output mode after we set URI since a
2888 * session can be created without URL (thus flagged in no output mode).
2889 */
2890 session->output_traces = 1;
2891 if (ksess) {
2892 ksess->output_traces = 1;
2893 }
2894
2895 if (usess) {
2896 usess->output_traces = 1;
2897 }
2898
2899 /* All good! */
2900 ret = LTTNG_OK;
2901
2902 error:
2903 return ret;
2904 }
2905
2906 static
2907 enum lttng_error_code set_session_output_from_descriptor(
2908 struct ltt_session *session,
2909 const struct lttng_session_descriptor *descriptor)
2910 {
2911 int ret;
2912 enum lttng_error_code ret_code = LTTNG_OK;
2913 enum lttng_session_descriptor_type session_type =
2914 lttng_session_descriptor_get_type(descriptor);
2915 enum lttng_session_descriptor_output_type output_type =
2916 lttng_session_descriptor_get_output_type(descriptor);
2917 struct lttng_uri uris[2] = {};
2918 size_t uri_count = 0;
2919
2920 switch (output_type) {
2921 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
2922 goto end;
2923 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
2924 lttng_session_descriptor_get_local_output_uri(descriptor,
2925 &uris[0]);
2926 uri_count = 1;
2927 break;
2928 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
2929 lttng_session_descriptor_get_network_output_uris(descriptor,
2930 &uris[0], &uris[1]);
2931 uri_count = 2;
2932 break;
2933 default:
2934 ret_code = LTTNG_ERR_INVALID;
2935 goto end;
2936 }
2937
2938 switch (session_type) {
2939 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
2940 {
2941 struct snapshot_output *new_output = NULL;
2942
2943 new_output = snapshot_output_alloc();
2944 if (!new_output) {
2945 ret_code = LTTNG_ERR_NOMEM;
2946 goto end;
2947 }
2948
2949 ret = snapshot_output_init_with_uri(session,
2950 DEFAULT_SNAPSHOT_MAX_SIZE,
2951 NULL, uris, uri_count, session->consumer,
2952 new_output, &session->snapshot);
2953 if (ret < 0) {
2954 ret_code = (ret == -ENOMEM) ?
2955 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
2956 snapshot_output_destroy(new_output);
2957 goto end;
2958 }
2959 snapshot_add_output(&session->snapshot, new_output);
2960 break;
2961 }
2962 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
2963 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
2964 {
2965 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
2966 break;
2967 }
2968 default:
2969 ret_code = LTTNG_ERR_INVALID;
2970 goto end;
2971 }
2972 end:
2973 return ret_code;
2974 }
2975
2976 static
2977 enum lttng_error_code cmd_create_session_from_descriptor(
2978 struct lttng_session_descriptor *descriptor,
2979 const lttng_sock_cred *creds,
2980 const char *home_path)
2981 {
2982 int ret;
2983 enum lttng_error_code ret_code;
2984 const char *session_name;
2985 struct ltt_session *new_session = NULL;
2986 enum lttng_session_descriptor_status descriptor_status;
2987
2988 session_lock_list();
2989 if (home_path) {
2990 if (*home_path != '/') {
2991 ERR("Home path provided by client is not absolute");
2992 ret_code = LTTNG_ERR_INVALID;
2993 goto end;
2994 }
2995 }
2996
2997 descriptor_status = lttng_session_descriptor_get_session_name(
2998 descriptor, &session_name);
2999 switch (descriptor_status) {
3000 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3001 break;
3002 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3003 session_name = NULL;
3004 break;
3005 default:
3006 ret_code = LTTNG_ERR_INVALID;
3007 goto end;
3008 }
3009
3010 ret_code = session_create(session_name, creds->uid, creds->gid,
3011 &new_session);
3012 if (ret_code != LTTNG_OK) {
3013 goto end;
3014 }
3015
3016 if (!session_name) {
3017 ret = lttng_session_descriptor_set_session_name(descriptor,
3018 new_session->name);
3019 if (ret) {
3020 ret_code = LTTNG_ERR_SESSION_FAIL;
3021 goto end;
3022 }
3023 }
3024
3025 if (!lttng_session_descriptor_is_output_destination_initialized(
3026 descriptor)) {
3027 /*
3028 * Only include the session's creation time in the output
3029 * destination if the name of the session itself was
3030 * not auto-generated.
3031 */
3032 ret_code = lttng_session_descriptor_set_default_output(
3033 descriptor,
3034 session_name ? &new_session->creation_time : NULL,
3035 home_path);
3036 if (ret_code != LTTNG_OK) {
3037 goto end;
3038 }
3039 } else {
3040 new_session->has_user_specified_directory =
3041 lttng_session_descriptor_has_output_directory(
3042 descriptor);
3043 }
3044
3045 switch (lttng_session_descriptor_get_type(descriptor)) {
3046 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3047 new_session->snapshot_mode = 1;
3048 break;
3049 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3050 new_session->live_timer =
3051 lttng_session_descriptor_live_get_timer_interval(
3052 descriptor);
3053 break;
3054 default:
3055 break;
3056 }
3057
3058 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3059 if (ret_code != LTTNG_OK) {
3060 goto end;
3061 }
3062 new_session->consumer->enabled = 1;
3063 ret_code = LTTNG_OK;
3064 end:
3065 /* Release reference provided by the session_create function. */
3066 session_put(new_session);
3067 if (ret_code != LTTNG_OK && new_session) {
3068 /* Release the global reference on error. */
3069 session_destroy(new_session);
3070 }
3071 session_unlock_list();
3072 return ret_code;
3073 }
3074
3075 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3076 struct lttng_session_descriptor **return_descriptor)
3077 {
3078 int ret;
3079 size_t payload_size;
3080 struct lttng_dynamic_buffer payload;
3081 struct lttng_buffer_view home_dir_view;
3082 struct lttng_buffer_view session_descriptor_view;
3083 struct lttng_session_descriptor *session_descriptor = NULL;
3084 enum lttng_error_code ret_code;
3085
3086 lttng_dynamic_buffer_init(&payload);
3087 if (cmd_ctx->lsm.u.create_session.home_dir_size >=
3088 LTTNG_PATH_MAX) {
3089 ret_code = LTTNG_ERR_INVALID;
3090 goto error;
3091 }
3092 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
3093 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3094 ret_code = LTTNG_ERR_INVALID;
3095 goto error;
3096 }
3097
3098 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3099 cmd_ctx->lsm.u.create_session.session_descriptor_size;
3100 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3101 if (ret) {
3102 ret_code = LTTNG_ERR_NOMEM;
3103 goto error;
3104 }
3105
3106 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3107 if (ret <= 0) {
3108 ERR("Reception of session descriptor failed, aborting.");
3109 ret_code = LTTNG_ERR_SESSION_FAIL;
3110 goto error;
3111 }
3112
3113 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3114 &payload,
3115 0,
3116 cmd_ctx->lsm.u.create_session.home_dir_size);
3117 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3118 &payload,
3119 cmd_ctx->lsm.u.create_session.home_dir_size,
3120 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3121
3122 ret = lttng_session_descriptor_create_from_buffer(
3123 &session_descriptor_view, &session_descriptor);
3124 if (ret < 0) {
3125 ERR("Failed to create session descriptor from payload of \"create session\" command");
3126 ret_code = LTTNG_ERR_INVALID;
3127 goto error;
3128 }
3129
3130 /*
3131 * Sets the descriptor's auto-generated properties (name, output) if
3132 * needed.
3133 */
3134 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3135 &cmd_ctx->creds,
3136 home_dir_view.size ? home_dir_view.data : NULL);
3137 if (ret_code != LTTNG_OK) {
3138 goto error;
3139 }
3140
3141 ret_code = LTTNG_OK;
3142 *return_descriptor = session_descriptor;
3143 session_descriptor = NULL;
3144 error:
3145 lttng_dynamic_buffer_reset(&payload);
3146 lttng_session_descriptor_destroy(session_descriptor);
3147 return ret_code;
3148 }
3149
3150 static
3151 void cmd_destroy_session_reply(const struct ltt_session *session,
3152 void *_reply_context)
3153 {
3154 int ret;
3155 ssize_t comm_ret;
3156 const struct cmd_destroy_session_reply_context *reply_context =
3157 _reply_context;
3158 struct lttng_dynamic_buffer payload;
3159 struct lttcomm_session_destroy_command_header cmd_header;
3160 struct lttng_trace_archive_location *location = NULL;
3161 struct lttcomm_lttng_msg llm = {
3162 .cmd_type = LTTNG_DESTROY_SESSION,
3163 .ret_code = reply_context->destruction_status,
3164 .pid = UINT32_MAX,
3165 .cmd_header_size =
3166 sizeof(struct lttcomm_session_destroy_command_header),
3167 .data_size = 0,
3168 };
3169 size_t payload_size_before_location;
3170
3171 lttng_dynamic_buffer_init(&payload);
3172
3173 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3174 if (ret) {
3175 ERR("Failed to append session destruction message");
3176 goto error;
3177 }
3178
3179 cmd_header.rotation_state =
3180 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3181 session->rotation_state :
3182 LTTNG_ROTATION_STATE_NO_ROTATION);
3183 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3184 sizeof(cmd_header));
3185 if (ret) {
3186 ERR("Failed to append session destruction command header");
3187 goto error;
3188 }
3189
3190 if (!reply_context->implicit_rotation_on_destroy) {
3191 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3192 session->name);
3193 goto send_reply;
3194 }
3195 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3196 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3197 session->name);
3198 goto send_reply;
3199 }
3200
3201 location = session_get_trace_archive_location(session);
3202 if (!location) {
3203 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3204 session->name);
3205 goto error;
3206 }
3207
3208 payload_size_before_location = payload.size;
3209 comm_ret = lttng_trace_archive_location_serialize(location,
3210 &payload);
3211 if (comm_ret < 0) {
3212 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3213 session->name);
3214 goto error;
3215 }
3216 /* Update the message to indicate the location's length. */
3217 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3218 payload.size - payload_size_before_location;
3219 send_reply:
3220 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd,
3221 payload.data, payload.size);
3222 if (comm_ret != (ssize_t) payload.size) {
3223 ERR("Failed to send result of the destruction of session \"%s\" to client",
3224 session->name);
3225 }
3226 error:
3227 ret = close(reply_context->reply_sock_fd);
3228 if (ret) {
3229 PERROR("Failed to close client socket in deferred session destroy reply");
3230 }
3231 lttng_dynamic_buffer_reset(&payload);
3232 free(_reply_context);
3233 }
3234
3235 /*
3236 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3237 *
3238 * Called with session lock held.
3239 */
3240 int cmd_destroy_session(struct ltt_session *session,
3241 struct notification_thread_handle *notification_thread_handle,
3242 int *sock_fd)
3243 {
3244 int ret;
3245 enum lttng_error_code destruction_last_error = LTTNG_OK;
3246 struct cmd_destroy_session_reply_context *reply_context = NULL;