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