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