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