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