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