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