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