Fix: sessiond: ODR violation results in memory corruption
[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 struct 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, NULL,
4353 registry->major, registry->minor);
4354 if (ret) {
4355 pthread_mutex_unlock(&registry->lock);
4356 ERR("Failed to generate session metadata (err = %d)",
4357 ret);
4358 goto end;
4359 }
4360 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
4361 chan, node.node) {
4362 struct ust_registry_event *event;
4363 struct lttng_ht_iter iter_event;
4364
4365 chan->metadata_dumped = 0;
4366
4367 ret = ust_metadata_channel_statedump(registry, chan);
4368 if (ret) {
4369 pthread_mutex_unlock(&registry->lock);
4370 ERR("Failed to generate channel metadata "
4371 "(err = %d)", ret);
4372 goto end;
4373 }
4374 cds_lfht_for_each_entry(chan->events->ht, &iter_event.iter,
4375 event, node.node) {
4376 event->metadata_dumped = 0;
4377 ret = ust_metadata_event_statedump(registry,
4378 chan, event);
4379 if (ret) {
4380 pthread_mutex_unlock(&registry->lock);
4381 ERR("Failed to generate event metadata "
4382 "(err = %d)", ret);
4383 goto end;
4384 }
4385 }
4386 }
4387 pthread_mutex_unlock(&registry->lock);
4388 }
4389
4390 end:
4391 rcu_read_unlock();
4392 return ret;
4393 }
4394
4395 /*
4396 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4397 *
4398 * Ask the consumer to truncate the existing metadata file(s) and
4399 * then regenerate the metadata. Live and per-pid sessions are not
4400 * supported and return an error.
4401 *
4402 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4403 */
4404 int cmd_regenerate_metadata(struct ltt_session *session)
4405 {
4406 int ret;
4407
4408 LTTNG_ASSERT(session);
4409
4410 ret = check_regenerate_metadata_support(session);
4411 if (ret) {
4412 goto end;
4413 }
4414
4415 if (session->kernel_session) {
4416 ret = kernctl_session_regenerate_metadata(
4417 session->kernel_session->fd);
4418 if (ret < 0) {
4419 ERR("Failed to regenerate the kernel metadata");
4420 goto end;
4421 }
4422 }
4423
4424 if (session->ust_session) {
4425 ret = ust_regenerate_metadata(session->ust_session);
4426 if (ret < 0) {
4427 ERR("Failed to regenerate the UST metadata");
4428 goto end;
4429 }
4430 }
4431 DBG("Cmd metadata regenerate for session %s", session->name);
4432 ret = LTTNG_OK;
4433
4434 end:
4435 return ret;
4436 }
4437
4438 /*
4439 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4440 *
4441 * Ask the tracer to regenerate a new statedump.
4442 *
4443 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4444 */
4445 int cmd_regenerate_statedump(struct ltt_session *session)
4446 {
4447 int ret;
4448
4449 LTTNG_ASSERT(session);
4450
4451 if (!session->active) {
4452 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4453 goto end;
4454 }
4455
4456 if (session->kernel_session) {
4457 ret = kernctl_session_regenerate_statedump(
4458 session->kernel_session->fd);
4459 /*
4460 * Currently, the statedump in kernel can only fail if out
4461 * of memory.
4462 */
4463 if (ret < 0) {
4464 if (ret == -ENOMEM) {
4465 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4466 } else {
4467 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4468 }
4469 ERR("Failed to regenerate the kernel statedump");
4470 goto end;
4471 }
4472 }
4473
4474 if (session->ust_session) {
4475 ret = ust_app_regenerate_statedump_all(session->ust_session);
4476 /*
4477 * Currently, the statedump in UST always returns 0.
4478 */
4479 if (ret < 0) {
4480 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4481 ERR("Failed to regenerate the UST statedump");
4482 goto end;
4483 }
4484 }
4485 DBG("Cmd regenerate statedump for session %s", session->name);
4486 ret = LTTNG_OK;
4487
4488 end:
4489 return ret;
4490 }
4491
4492 static
4493 enum lttng_error_code synchronize_tracer_notifier_register(
4494 struct notification_thread_handle *notification_thread,
4495 struct lttng_trigger *trigger, const struct lttng_credentials *cmd_creds)
4496 {
4497 enum lttng_error_code ret_code;
4498 const struct lttng_condition *condition =
4499 lttng_trigger_get_const_condition(trigger);
4500 const char *trigger_name;
4501 uid_t trigger_owner;
4502 enum lttng_trigger_status trigger_status;
4503 const enum lttng_domain_type trigger_domain =
4504 lttng_trigger_get_underlying_domain_type_restriction(
4505 trigger);
4506
4507 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4508 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4509
4510 LTTNG_ASSERT(condition);
4511 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4512 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4513
4514 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4515 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4516 trigger_name : "(anonymous)";
4517
4518 session_lock_list();
4519 switch (trigger_domain) {
4520 case LTTNG_DOMAIN_KERNEL:
4521 {
4522 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4523 if (ret_code != LTTNG_OK) {
4524 enum lttng_error_code notif_thread_unregister_ret;
4525
4526 notif_thread_unregister_ret =
4527 notification_thread_command_unregister_trigger(
4528 notification_thread, trigger);
4529
4530 if (notif_thread_unregister_ret != LTTNG_OK) {
4531 /* Return the original error code. */
4532 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4533 trigger_name,
4534 (int) trigger_owner,
4535 ret_code);
4536 }
4537 }
4538 break;
4539 }
4540 case LTTNG_DOMAIN_UST:
4541 ust_app_global_update_all_event_notifier_rules();
4542 break;
4543 case LTTNG_DOMAIN_JUL:
4544 case LTTNG_DOMAIN_LOG4J:
4545 case LTTNG_DOMAIN_PYTHON:
4546 {
4547 /* Agent domains. */
4548 struct agent *agt = agent_find_by_event_notifier_domain(
4549 trigger_domain);
4550
4551 if (!agt) {
4552 agt = agent_create(trigger_domain);
4553 if (!agt) {
4554 ret_code = LTTNG_ERR_NOMEM;
4555 goto end_unlock_session_list;
4556 }
4557
4558 agent_add(agt, the_trigger_agents_ht_by_domain);
4559 }
4560
4561 ret_code = (lttng_error_code) trigger_agent_enable(trigger, agt);
4562 if (ret_code != LTTNG_OK) {
4563 goto end_unlock_session_list;
4564 }
4565
4566 break;
4567 }
4568 case LTTNG_DOMAIN_NONE:
4569 default:
4570 abort();
4571 }
4572
4573 ret_code = LTTNG_OK;
4574 end_unlock_session_list:
4575 session_unlock_list();
4576 return ret_code;
4577 }
4578
4579 enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
4580 struct lttng_trigger *trigger,
4581 bool is_trigger_anonymous,
4582 struct notification_thread_handle *notification_thread,
4583 struct lttng_trigger **return_trigger)
4584 {
4585 enum lttng_error_code ret_code;
4586 const char *trigger_name;
4587 uid_t trigger_owner;
4588 enum lttng_trigger_status trigger_status;
4589
4590 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4591 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4592 trigger_name : "(anonymous)";
4593
4594 trigger_status = lttng_trigger_get_owner_uid(
4595 trigger, &trigger_owner);
4596 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4597
4598 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4599 trigger_name, (int) trigger_owner,
4600 (int) lttng_credentials_get_uid(cmd_creds));
4601
4602 /*
4603 * Validate the trigger credentials against the command credentials.
4604 * Only the root user can register a trigger with non-matching
4605 * credentials.
4606 */
4607 if (!lttng_credentials_is_equal_uid(
4608 lttng_trigger_get_credentials(trigger),
4609 cmd_creds)) {
4610 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4611 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4612 trigger_name, (int) trigger_owner,
4613 (int) lttng_credentials_get_uid(cmd_creds));
4614 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4615 goto end;
4616 }
4617 }
4618
4619 /*
4620 * The bytecode generation also serves as a validation step for the
4621 * bytecode expressions.
4622 */
4623 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4624 if (ret_code != LTTNG_OK) {
4625 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4626 trigger_name, (int) trigger_owner, ret_code);
4627 goto end;
4628 }
4629
4630 /*
4631 * A reference to the trigger is acquired by the notification thread.
4632 * It is safe to return the same trigger to the caller since it the
4633 * other user holds a reference.
4634 *
4635 * The trigger is modified during the execution of the
4636 * "register trigger" command. However, by the time the command returns,
4637 * it is safe to use without any locking as its properties are
4638 * immutable.
4639 */
4640 ret_code = notification_thread_command_register_trigger(
4641 notification_thread, trigger, is_trigger_anonymous);
4642 if (ret_code != LTTNG_OK) {
4643 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4644 trigger_name, (int) trigger_owner, ret_code);
4645 goto end;
4646 }
4647
4648 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4649 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4650 trigger_name : "(anonymous)";
4651
4652 /*
4653 * Synchronize tracers if the trigger adds an event notifier.
4654 */
4655 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4656 ret_code = synchronize_tracer_notifier_register(notification_thread,
4657 trigger, cmd_creds);
4658 if (ret_code != LTTNG_OK) {
4659 ERR("Error registering tracer notifier: %s",
4660 lttng_strerror(-ret_code));
4661 goto end;
4662 }
4663 }
4664
4665 /*
4666 * Return an updated trigger to the client.
4667 *
4668 * Since a modified version of the same trigger is returned, acquire a
4669 * reference to the trigger so the caller doesn't have to care if those
4670 * are distinct instances or not.
4671 */
4672 if (ret_code == LTTNG_OK) {
4673 lttng_trigger_get(trigger);
4674 *return_trigger = trigger;
4675 /* Ownership of trigger was transferred to caller. */
4676 trigger = NULL;
4677 }
4678 end:
4679 return ret_code;
4680 }
4681
4682 static
4683 enum lttng_error_code synchronize_tracer_notifier_unregister(
4684 const struct lttng_trigger *trigger)
4685 {
4686 enum lttng_error_code ret_code;
4687 const struct lttng_condition *condition =
4688 lttng_trigger_get_const_condition(trigger);
4689 const enum lttng_domain_type trigger_domain =
4690 lttng_trigger_get_underlying_domain_type_restriction(
4691 trigger);
4692
4693 LTTNG_ASSERT(condition);
4694 LTTNG_ASSERT(lttng_condition_get_type(condition) ==
4695 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4696
4697 session_lock_list();
4698 switch (trigger_domain) {
4699 case LTTNG_DOMAIN_KERNEL:
4700 ret_code = kernel_unregister_event_notifier(trigger);
4701 if (ret_code != LTTNG_OK) {
4702 goto end_unlock_session_list;
4703 }
4704
4705 break;
4706 case LTTNG_DOMAIN_UST:
4707 ust_app_global_update_all_event_notifier_rules();
4708 break;
4709 case LTTNG_DOMAIN_JUL:
4710 case LTTNG_DOMAIN_LOG4J:
4711 case LTTNG_DOMAIN_PYTHON:
4712 {
4713 /* Agent domains. */
4714 struct agent *agt = agent_find_by_event_notifier_domain(
4715 trigger_domain);
4716
4717 /*
4718 * This trigger was never registered in the first place. Calling
4719 * this function under those circumstances is an internal error.
4720 */
4721 LTTNG_ASSERT(agt);
4722 ret_code = (lttng_error_code) trigger_agent_disable(trigger, agt);
4723 if (ret_code != LTTNG_OK) {
4724 goto end_unlock_session_list;
4725 }
4726
4727 break;
4728 }
4729 case LTTNG_DOMAIN_NONE:
4730 default:
4731 abort();
4732 }
4733
4734 ret_code = LTTNG_OK;
4735
4736 end_unlock_session_list:
4737 session_unlock_list();
4738 return ret_code;
4739 }
4740
4741 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
4742 const struct lttng_trigger *trigger,
4743 struct notification_thread_handle *notification_thread)
4744 {
4745 enum lttng_error_code ret_code;
4746 const char *trigger_name;
4747 uid_t trigger_owner;
4748 enum lttng_trigger_status trigger_status;
4749 struct lttng_trigger *sessiond_trigger = NULL;
4750
4751 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4752 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4753 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4754 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4755
4756 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4757 trigger_name, (int) trigger_owner,
4758 (int) lttng_credentials_get_uid(cmd_creds));
4759
4760 /*
4761 * Validate the trigger credentials against the command credentials.
4762 * Only the root user can unregister a trigger with non-matching
4763 * credentials.
4764 */
4765 if (!lttng_credentials_is_equal_uid(
4766 lttng_trigger_get_credentials(trigger),
4767 cmd_creds)) {
4768 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4769 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4770 trigger_name, (int) trigger_owner,
4771 (int) lttng_credentials_get_uid(cmd_creds));
4772 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4773 goto end;
4774 }
4775 }
4776
4777 /* Fetch the sessiond side trigger object. */
4778 ret_code = notification_thread_command_get_trigger(
4779 notification_thread, trigger, &sessiond_trigger);
4780 if (ret_code != LTTNG_OK) {
4781 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4782 trigger_name, (int) trigger_owner, ret_code);
4783 goto end;
4784 }
4785
4786 LTTNG_ASSERT(sessiond_trigger);
4787
4788 /*
4789 * From this point on, no matter what, consider the trigger
4790 * unregistered.
4791 *
4792 * We set the unregistered state of the sessiond side trigger object in
4793 * the client thread since we want to minimize the possibility of the
4794 * notification thread being stalled due to a long execution of an
4795 * action that required the trigger lock.
4796 */
4797 lttng_trigger_set_as_unregistered(sessiond_trigger);
4798
4799 ret_code = notification_thread_command_unregister_trigger(notification_thread,
4800 trigger);
4801 if (ret_code != LTTNG_OK) {
4802 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4803 trigger_name, (int) trigger_owner, ret_code);
4804 goto end;
4805 }
4806
4807 /*
4808 * Synchronize tracers if the trigger removes an event notifier.
4809 * Do this even if the trigger unregistration failed to at least stop
4810 * the tracers from producing notifications associated with this
4811 * event notifier.
4812 */
4813 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4814 ret_code = synchronize_tracer_notifier_unregister(trigger);
4815 if (ret_code != LTTNG_OK) {
4816 ERR("Error unregistering trigger to tracer.");
4817 goto end;
4818 }
4819
4820 }
4821
4822 end:
4823 lttng_trigger_put(sessiond_trigger);
4824 return ret_code;
4825 }
4826
4827 enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
4828 struct notification_thread_handle *notification_thread,
4829 struct lttng_triggers **return_triggers)
4830 {
4831 int ret;
4832 enum lttng_error_code ret_code;
4833 struct lttng_triggers *triggers = NULL;
4834
4835 /* Get the set of triggers from the notification thread. */
4836 ret_code = notification_thread_command_list_triggers(
4837 notification_thread, cmd_ctx->creds.uid, &triggers);
4838 if (ret_code != LTTNG_OK) {
4839 goto end;
4840 }
4841
4842 ret = lttng_triggers_remove_hidden_triggers(triggers);
4843 if (ret) {
4844 ret_code = LTTNG_ERR_UNK;
4845 goto end;
4846 }
4847
4848 *return_triggers = triggers;
4849 triggers = NULL;
4850 ret_code = LTTNG_OK;
4851 end:
4852 lttng_triggers_destroy(triggers);
4853 return ret_code;
4854 }
4855
4856 enum lttng_error_code cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4857 const struct lttng_error_query *query,
4858 struct lttng_error_query_results **_results,
4859 struct notification_thread_handle *notification_thread)
4860 {
4861 enum lttng_error_code ret_code;
4862 const struct lttng_trigger *query_target_trigger;
4863 const struct lttng_action *query_target_action = NULL;
4864 struct lttng_trigger *matching_trigger = NULL;
4865 const char *trigger_name;
4866 uid_t trigger_owner;
4867 enum lttng_trigger_status trigger_status;
4868 struct lttng_error_query_results *results = NULL;
4869
4870 switch (lttng_error_query_get_target_type(query)) {
4871 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4872 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4873 break;
4874 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4875 query_target_trigger =
4876 lttng_error_query_condition_borrow_target(query);
4877 break;
4878 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4879 query_target_trigger = lttng_error_query_action_borrow_trigger_target(
4880 query);
4881 break;
4882 default:
4883 abort();
4884 }
4885
4886 LTTNG_ASSERT(query_target_trigger);
4887
4888 ret_code = notification_thread_command_get_trigger(notification_thread,
4889 query_target_trigger, &matching_trigger);
4890 if (ret_code != LTTNG_OK) {
4891 goto end;
4892 }
4893
4894 /* No longer needed. */
4895 query_target_trigger = NULL;
4896
4897 if (lttng_error_query_get_target_type(query) ==
4898 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4899 /* Get the sessiond-side version of the target action. */
4900 query_target_action =
4901 lttng_error_query_action_borrow_action_target(
4902 query, matching_trigger);
4903 }
4904
4905 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4906 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4907 trigger_name : "(anonymous)";
4908 trigger_status = lttng_trigger_get_owner_uid(matching_trigger,
4909 &trigger_owner);
4910 LTTNG_ASSERT(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4911
4912 results = lttng_error_query_results_create();
4913 if (!results) {
4914 ret_code = LTTNG_ERR_NOMEM;
4915 goto end;
4916 }
4917
4918 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4919 trigger_name, (int) trigger_owner,
4920 (int) lttng_credentials_get_uid(cmd_creds));
4921
4922 /*
4923 * Validate the trigger credentials against the command credentials.
4924 * Only the root user can target a trigger with non-matching
4925 * credentials.
4926 */
4927 if (!lttng_credentials_is_equal_uid(
4928 lttng_trigger_get_credentials(matching_trigger),
4929 cmd_creds)) {
4930 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4931 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4932 trigger_name, (int) trigger_owner,
4933 (int) lttng_credentials_get_uid(cmd_creds));
4934 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4935 goto end;
4936 }
4937 }
4938
4939 switch (lttng_error_query_get_target_type(query)) {
4940 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4941 trigger_status = lttng_trigger_add_error_results(
4942 matching_trigger, results);
4943
4944 switch (trigger_status) {
4945 case LTTNG_TRIGGER_STATUS_OK:
4946 break;
4947 default:
4948 ret_code = LTTNG_ERR_UNK;
4949 goto end;
4950 }
4951
4952 break;
4953 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4954 {
4955 trigger_status = lttng_trigger_condition_add_error_results(
4956 matching_trigger, results);
4957
4958 switch (trigger_status) {
4959 case LTTNG_TRIGGER_STATUS_OK:
4960 break;
4961 default:
4962 ret_code = LTTNG_ERR_UNK;
4963 goto end;
4964 }
4965
4966 break;
4967 }
4968 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4969 {
4970 const enum lttng_action_status action_status =
4971 lttng_action_add_error_query_results(
4972 query_target_action, results);
4973
4974 switch (action_status) {
4975 case LTTNG_ACTION_STATUS_OK:
4976 break;
4977 default:
4978 ret_code = LTTNG_ERR_UNK;
4979 goto end;
4980 }
4981
4982 break;
4983 }
4984 default:
4985 abort();
4986 break;
4987 }
4988
4989 *_results = results;
4990 results = NULL;
4991 ret_code = LTTNG_OK;
4992 end:
4993 lttng_trigger_put(matching_trigger);
4994 lttng_error_query_results_destroy(results);
4995 return ret_code;
4996 }
4997
4998 /*
4999 * Send relayd sockets from snapshot output to consumer. Ignore request if the
5000 * snapshot output is *not* set with a remote destination.
5001 *
5002 * Return LTTNG_OK on success or a LTTNG_ERR code.
5003 */
5004 static enum lttng_error_code set_relayd_for_snapshot(
5005 struct consumer_output *output,
5006 const struct ltt_session *session)
5007 {
5008 enum lttng_error_code status = LTTNG_OK;
5009 struct lttng_ht_iter iter;
5010 struct consumer_socket *socket;
5011 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
5012 const char *base_path;
5013
5014 LTTNG_ASSERT(output);
5015 LTTNG_ASSERT(session);
5016
5017 DBG2("Set relayd object from snapshot output");
5018
5019 if (session->current_trace_chunk) {
5020 enum lttng_trace_chunk_status chunk_status =
5021 lttng_trace_chunk_get_id(
5022 session->current_trace_chunk,
5023 &current_chunk_id.value);
5024
5025 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
5026 current_chunk_id.is_set = true;
5027 } else {
5028 ERR("Failed to get current trace chunk id");
5029 status = LTTNG_ERR_UNK;
5030 goto error;
5031 }
5032 }
5033
5034 /* Ignore if snapshot consumer output is not network. */
5035 if (output->type != CONSUMER_DST_NET) {
5036 goto error;
5037 }
5038
5039 /*
5040 * The snapshot record URI base path overrides the session
5041 * base path.
5042 */
5043 if (output->dst.net.control.subdir[0] != '\0') {
5044 base_path = output->dst.net.control.subdir;
5045 } else {
5046 base_path = session->base_path;
5047 }
5048
5049 /*
5050 * For each consumer socket, create and send the relayd object of the
5051 * snapshot output.
5052 */
5053 rcu_read_lock();
5054 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
5055 socket, node.node) {
5056 pthread_mutex_lock(socket->lock);
5057 status = send_consumer_relayd_sockets(session->id,
5058 output, socket,
5059 session->name, session->hostname,
5060 base_path,
5061 session->live_timer,
5062 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
5063 session->creation_time,
5064 session->name_contains_creation_time);
5065 pthread_mutex_unlock(socket->lock);
5066 if (status != LTTNG_OK) {
5067 rcu_read_unlock();
5068 goto error;
5069 }
5070 }
5071 rcu_read_unlock();
5072
5073 error:
5074 return status;
5075 }
5076
5077 /*
5078 * Record a kernel snapshot.
5079 *
5080 * Return LTTNG_OK on success or a LTTNG_ERR code.
5081 */
5082 static enum lttng_error_code record_kernel_snapshot(
5083 struct ltt_kernel_session *ksess,
5084 const struct consumer_output *output,
5085 const struct ltt_session *session,
5086 uint64_t nb_packets_per_stream)
5087 {
5088 enum lttng_error_code status;
5089
5090 LTTNG_ASSERT(ksess);
5091 LTTNG_ASSERT(output);
5092 LTTNG_ASSERT(session);
5093
5094 status = kernel_snapshot_record(
5095 ksess, output, nb_packets_per_stream);
5096 return status;
5097 }
5098
5099 /*
5100 * Record a UST snapshot.
5101 *
5102 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5103 */
5104 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
5105 const struct consumer_output *output,
5106 const struct ltt_session *session,
5107 uint64_t nb_packets_per_stream)
5108 {
5109 enum lttng_error_code status;
5110
5111 LTTNG_ASSERT(usess);
5112 LTTNG_ASSERT(output);
5113 LTTNG_ASSERT(session);
5114
5115 status = ust_app_snapshot_record(
5116 usess, output, nb_packets_per_stream);
5117 return status;
5118 }
5119
5120 static
5121 uint64_t get_session_size_one_more_packet_per_stream(
5122 const struct ltt_session *session, uint64_t cur_nr_packets)
5123 {
5124 uint64_t tot_size = 0;
5125
5126 if (session->kernel_session) {
5127 struct ltt_kernel_channel *chan;
5128 const struct ltt_kernel_session *ksess =
5129 session->kernel_session;
5130
5131 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
5132 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5133 /*
5134 * Don't take channel into account if we
5135 * already grab all its packets.
5136 */
5137 continue;
5138 }
5139 tot_size += chan->channel->attr.subbuf_size
5140 * chan->stream_count;
5141 }
5142 }
5143
5144 if (session->ust_session) {
5145 const struct ltt_ust_session *usess = session->ust_session;
5146
5147 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
5148 cur_nr_packets);
5149 }
5150
5151 return tot_size;
5152 }
5153
5154 /*
5155 * Calculate the number of packets we can grab from each stream that
5156 * fits within the overall snapshot max size.
5157 *
5158 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5159 * the number of packets per stream.
5160 *
5161 * TODO: this approach is not perfect: we consider the worse case
5162 * (packet filling the sub-buffers) as an upper bound, but we could do
5163 * better if we do this calculation while we actually grab the packet
5164 * content: we would know how much padding we don't actually store into
5165 * the file.
5166 *
5167 * This algorithm is currently bounded by the number of packets per
5168 * stream.
5169 *
5170 * Since we call this algorithm before actually grabbing the data, it's
5171 * an approximation: for instance, applications could appear/disappear
5172 * in between this call and actually grabbing data.
5173 */
5174 static
5175 int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5176 uint64_t max_size)
5177 {
5178 int64_t size_left;
5179 uint64_t cur_nb_packets = 0;
5180
5181 if (!max_size) {
5182 return 0; /* Infinite */
5183 }
5184
5185 size_left = max_size;
5186 for (;;) {
5187 uint64_t one_more_packet_tot_size;
5188
5189 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
5190 session, cur_nb_packets);
5191 if (!one_more_packet_tot_size) {
5192 /* We are already grabbing all packets. */
5193 break;
5194 }
5195 size_left -= one_more_packet_tot_size;
5196 if (size_left < 0) {
5197 break;
5198 }
5199 cur_nb_packets++;
5200 }
5201 if (!cur_nb_packets && size_left != max_size) {
5202 /* Not enough room to grab one packet of each stream, error. */
5203 return -1;
5204 }
5205 return cur_nb_packets;
5206 }
5207
5208 static
5209 enum lttng_error_code snapshot_record(struct ltt_session *session,
5210 const struct snapshot_output *snapshot_output)
5211 {
5212 int64_t nb_packets_per_stream;
5213 char snapshot_chunk_name[LTTNG_NAME_MAX];
5214 int ret;
5215 enum lttng_error_code ret_code = LTTNG_OK;
5216 struct lttng_trace_chunk *snapshot_trace_chunk;
5217 struct consumer_output *original_ust_consumer_output = NULL;
5218 struct consumer_output *original_kernel_consumer_output = NULL;
5219 struct consumer_output *snapshot_ust_consumer_output = NULL;
5220 struct consumer_output *snapshot_kernel_consumer_output = NULL;
5221
5222 ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
5223 "%s-%s-%" PRIu64,
5224 snapshot_output->name,
5225 snapshot_output->datetime,
5226 snapshot_output->nb_snapshot);
5227 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
5228 ERR("Failed to format snapshot name");
5229 ret_code = LTTNG_ERR_INVALID;
5230 goto error;
5231 }
5232 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5233 snapshot_output->name, session->name,
5234 snapshot_chunk_name);
5235 if (!session->kernel_session && !session->ust_session) {
5236 ERR("Failed to record snapshot as no channels exist");
5237 ret_code = LTTNG_ERR_NO_CHANNEL;
5238 goto error;
5239 }
5240
5241 if (session->kernel_session) {
5242 original_kernel_consumer_output =
5243 session->kernel_session->consumer;
5244 snapshot_kernel_consumer_output =
5245 consumer_copy_output(snapshot_output->consumer);
5246 strcpy(snapshot_kernel_consumer_output->chunk_path,
5247 snapshot_chunk_name);
5248
5249 /* Copy the original domain subdir. */
5250 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5251 original_kernel_consumer_output->domain_subdir);
5252
5253 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5254 original_kernel_consumer_output);
5255 if (ret < 0) {
5256 ERR("Failed to copy consumer sockets from snapshot output configuration");
5257 ret_code = LTTNG_ERR_NOMEM;
5258 goto error;
5259 }
5260 ret_code = set_relayd_for_snapshot(
5261 snapshot_kernel_consumer_output, session);
5262 if (ret_code != LTTNG_OK) {
5263 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5264 goto error;
5265 }
5266 session->kernel_session->consumer =
5267 snapshot_kernel_consumer_output;
5268 }
5269 if (session->ust_session) {
5270 original_ust_consumer_output = session->ust_session->consumer;
5271 snapshot_ust_consumer_output =
5272 consumer_copy_output(snapshot_output->consumer);
5273 strcpy(snapshot_ust_consumer_output->chunk_path,
5274 snapshot_chunk_name);
5275
5276 /* Copy the original domain subdir. */
5277 strcpy(snapshot_ust_consumer_output->domain_subdir,
5278 original_ust_consumer_output->domain_subdir);
5279
5280 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5281 original_ust_consumer_output);
5282 if (ret < 0) {
5283 ERR("Failed to copy consumer sockets from snapshot output configuration");
5284 ret_code = LTTNG_ERR_NOMEM;
5285 goto error;
5286 }
5287 ret_code = set_relayd_for_snapshot(
5288 snapshot_ust_consumer_output, session);
5289 if (ret_code != LTTNG_OK) {
5290 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5291 goto error;
5292 }
5293 session->ust_session->consumer =
5294 snapshot_ust_consumer_output;
5295 }
5296
5297 snapshot_trace_chunk = session_create_new_trace_chunk(session,
5298 snapshot_kernel_consumer_output ?:
5299 snapshot_ust_consumer_output,
5300 consumer_output_get_base_path(
5301 snapshot_output->consumer),
5302 snapshot_chunk_name);
5303 if (!snapshot_trace_chunk) {
5304 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5305 session->name);
5306 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5307 goto error;
5308 }
5309 LTTNG_ASSERT(!session->current_trace_chunk);
5310 ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
5311 lttng_trace_chunk_put(snapshot_trace_chunk);
5312 snapshot_trace_chunk = NULL;
5313 if (ret) {
5314 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5315 session->name);
5316 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5317 goto error;
5318 }
5319
5320 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
5321 snapshot_output->max_size);
5322 if (nb_packets_per_stream < 0) {
5323 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5324 goto error_close_trace_chunk;
5325 }
5326
5327 if (session->kernel_session) {
5328 ret_code = record_kernel_snapshot(session->kernel_session,
5329 snapshot_kernel_consumer_output, session,
5330 nb_packets_per_stream);
5331 if (ret_code != LTTNG_OK) {
5332 goto error_close_trace_chunk;
5333 }
5334 }
5335
5336 if (session->ust_session) {
5337 ret_code = record_ust_snapshot(session->ust_session,
5338 snapshot_ust_consumer_output, session,
5339 nb_packets_per_stream);
5340 if (ret_code != LTTNG_OK) {
5341 goto error_close_trace_chunk;
5342 }
5343 }
5344
5345 error_close_trace_chunk:
5346 if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
5347 ERR("Failed to release the current trace chunk of session \"%s\"",
5348 session->name);
5349 ret_code = LTTNG_ERR_UNK;
5350 }
5351
5352 if (session_close_trace_chunk(session, snapshot_trace_chunk,
5353 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) {
5354 /*
5355 * Don't goto end; make sure the chunk is closed for the session
5356 * to allow future snapshots.
5357 */
5358 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5359 session->name);
5360 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5361 }
5362
5363 lttng_trace_chunk_put(snapshot_trace_chunk);
5364 snapshot_trace_chunk = NULL;
5365 error:
5366 if (original_ust_consumer_output) {
5367 session->ust_session->consumer = original_ust_consumer_output;
5368 }
5369 if (original_kernel_consumer_output) {
5370 session->kernel_session->consumer =
5371 original_kernel_consumer_output;
5372 }
5373 consumer_output_put(snapshot_ust_consumer_output);
5374 consumer_output_put(snapshot_kernel_consumer_output);
5375 return ret_code;
5376 }
5377
5378 /*
5379 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5380 *
5381 * The wait parameter is ignored so this call always wait for the snapshot to
5382 * complete before returning.
5383 *
5384 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5385 */
5386 int cmd_snapshot_record(struct ltt_session *session,
5387 const struct lttng_snapshot_output *output,
5388 int wait __attribute__((unused)))
5389 {
5390 enum lttng_error_code cmd_ret = LTTNG_OK;
5391 int ret;
5392 unsigned int snapshot_success = 0;
5393 char datetime[16];
5394 struct snapshot_output *tmp_output = NULL;
5395
5396 LTTNG_ASSERT(session);
5397 LTTNG_ASSERT(output);
5398
5399 DBG("Cmd snapshot record for session %s", session->name);
5400
5401 /* Get the datetime for the snapshot output directory. */
5402 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
5403 sizeof(datetime));
5404 if (!ret) {
5405 cmd_ret = LTTNG_ERR_INVALID;
5406 goto error;
5407 }
5408
5409 /*
5410 * Permission denied to create an output if the session is not
5411 * set in no output mode.
5412 */
5413 if (session->output_traces) {
5414 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
5415 goto error;
5416 }
5417
5418 /* The session needs to be started at least once. */
5419 if (!session->has_been_started) {
5420 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5421 goto error;
5422 }
5423
5424 /* Use temporary output for the session. */
5425 if (*output->ctrl_url != '\0') {
5426 tmp_output = snapshot_output_alloc();
5427 if (!tmp_output) {
5428 cmd_ret = LTTNG_ERR_NOMEM;
5429 goto error;
5430 }
5431
5432 ret = snapshot_output_init(session, output->max_size,
5433 output->name,
5434 output->ctrl_url, output->data_url,
5435 session->consumer,
5436 tmp_output, NULL);
5437 if (ret < 0) {
5438 if (ret == -ENOMEM) {
5439 cmd_ret = LTTNG_ERR_NOMEM;
5440 } else {
5441 cmd_ret = LTTNG_ERR_INVALID;
5442 }
5443 goto error;
5444 }
5445 /* Use the global session count for the temporary snapshot. */
5446 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
5447
5448 /* Use the global datetime */
5449 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5450 cmd_ret = snapshot_record(session, tmp_output);
5451 if (cmd_ret != LTTNG_OK) {
5452 goto error;
5453 }
5454 snapshot_success = 1;
5455 } else {
5456 struct snapshot_output *sout;
5457 struct lttng_ht_iter iter;
5458
5459 rcu_read_lock();
5460 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
5461 &iter.iter, sout, node.node) {
5462 struct snapshot_output output_copy;
5463
5464 /*
5465 * Make a local copy of the output and override output
5466 * parameters with those provided as part of the
5467 * command.
5468 */
5469 memcpy(&output_copy, sout, sizeof(output_copy));
5470
5471 if (output->max_size != (uint64_t) -1ULL) {
5472 output_copy.max_size = output->max_size;
5473 }
5474
5475 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5476 memcpy(output_copy.datetime, datetime,
5477 sizeof(datetime));
5478
5479 /* Use temporary name. */
5480 if (*output->name != '\0') {
5481 if (lttng_strncpy(output_copy.name,
5482 output->name,
5483 sizeof(output_copy.name))) {
5484 cmd_ret = LTTNG_ERR_INVALID;
5485 rcu_read_unlock();
5486 goto error;
5487 }
5488 }
5489
5490 cmd_ret = snapshot_record(session, &output_copy);
5491 if (cmd_ret != LTTNG_OK) {
5492 rcu_read_unlock();
5493 goto error;
5494 }
5495 snapshot_success = 1;
5496 }
5497 rcu_read_unlock();
5498 }
5499
5500 if (snapshot_success) {
5501 session->snapshot.nb_snapshot++;
5502 } else {
5503 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
5504 }
5505
5506 error:
5507 if (tmp_output) {
5508 snapshot_output_destroy(tmp_output);
5509 }
5510 return cmd_ret;
5511 }
5512
5513 /*
5514 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5515 */
5516 int cmd_set_session_shm_path(struct ltt_session *session,
5517 const char *shm_path)
5518 {
5519 /* Safety net */
5520 LTTNG_ASSERT(session);
5521
5522 /*
5523 * Can only set shm path before session is started.
5524 */
5525 if (session->has_been_started) {
5526 return LTTNG_ERR_SESSION_STARTED;
5527 }
5528
5529 strncpy(session->shm_path, shm_path,
5530 sizeof(session->shm_path));
5531 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5532
5533 return LTTNG_OK;
5534 }
5535
5536 /*
5537 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5538 *
5539 * Ask the consumer to rotate the session output directory.
5540 * The session lock must be held.
5541 *
5542 * Returns LTTNG_OK on success or else a negative LTTng error code.
5543 */
5544 int cmd_rotate_session(struct ltt_session *session,
5545 struct lttng_rotate_session_return *rotate_return,
5546 bool quiet_rotation,
5547 enum lttng_trace_chunk_command_type command)
5548 {
5549 int ret;
5550 uint64_t ongoing_rotation_chunk_id;
5551 enum lttng_error_code cmd_ret = LTTNG_OK;
5552 struct lttng_trace_chunk *chunk_being_archived = NULL;
5553 struct lttng_trace_chunk *new_trace_chunk = NULL;
5554 enum lttng_trace_chunk_status chunk_status;
5555 bool failed_to_rotate = false;
5556 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5557
5558 LTTNG_ASSERT(session);
5559
5560 if (!session->has_been_started) {
5561 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5562 goto end;
5563 }
5564
5565 /*
5566 * Explicit rotation is not supported for live sessions.
5567 * However, live sessions can perform a quiet rotation on
5568 * destroy.
5569 * Rotation is not supported for snapshot traces (no output).
5570 */
5571 if ((!quiet_rotation && session->live_timer) ||
5572 !session->output_traces) {
5573 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5574 goto end;
5575 }
5576
5577 /* Unsupported feature in lttng-relayd before 2.11. */
5578 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5579 (session->consumer->relay_major_version == 2 &&
5580 session->consumer->relay_minor_version < 11)) {
5581 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
5582 goto end;
5583 }
5584
5585 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5586 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5587 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5588 goto end;
5589 }
5590
5591 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5592 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5593 session->name);
5594 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
5595 goto end;
5596 }
5597
5598 /*
5599 * After a stop, we only allow one rotation to occur, the other ones are
5600 * useless until a new start.
5601 */
5602 if (session->rotated_after_last_stop) {
5603 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5604 session->name);
5605 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
5606 goto end;
5607 }
5608
5609 /*
5610 * After a stop followed by a clear, disallow following rotations a they would
5611 * generate empty chunks.
5612 */
5613 if (session->cleared_after_last_stop) {
5614 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5615 session->name);
5616 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5617 goto end;
5618 }
5619
5620 if (session->active) {
5621 new_trace_chunk = session_create_new_trace_chunk(session, NULL,
5622 NULL, NULL);
5623 if (!new_trace_chunk) {
5624 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5625 goto error;
5626 }
5627 }
5628
5629 /*
5630 * The current trace chunk becomes the chunk being archived.
5631 *
5632 * After this point, "chunk_being_archived" must absolutely
5633 * be closed on the consumer(s), otherwise it will never be
5634 * cleaned-up, which will result in a leak.
5635 */
5636 ret = session_set_trace_chunk(session, new_trace_chunk,
5637 &chunk_being_archived);
5638 if (ret) {
5639 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5640 goto error;
5641 }
5642
5643 if (session->kernel_session) {
5644 cmd_ret = kernel_rotate_session(session);
5645 if (cmd_ret != LTTNG_OK) {
5646 failed_to_rotate = true;
5647 rotation_fail_code = cmd_ret;
5648 }
5649 }
5650 if (session->ust_session) {
5651 cmd_ret = ust_app_rotate_session(session);
5652 if (cmd_ret != LTTNG_OK) {
5653 failed_to_rotate = true;
5654 rotation_fail_code = cmd_ret;
5655 }
5656 }
5657
5658 if (!session->active) {
5659 session->rotated_after_last_stop = true;
5660 }
5661
5662 if (!chunk_being_archived) {
5663 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5664 session->name);
5665 if (failed_to_rotate) {
5666 cmd_ret = rotation_fail_code;
5667 goto error;
5668 }
5669 cmd_ret = LTTNG_OK;
5670 goto end;
5671 }
5672
5673 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5674 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
5675 &ongoing_rotation_chunk_id);
5676 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5677
5678 ret = session_close_trace_chunk(session, chunk_being_archived,
5679 command, session->last_chunk_path);
5680 if (ret) {
5681 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5682 goto error;
5683 }
5684
5685 if (failed_to_rotate) {
5686 cmd_ret = rotation_fail_code;
5687 goto error;
5688 }
5689
5690 session->quiet_rotation = quiet_rotation;
5691 ret = timer_session_rotation_pending_check_start(session,
5692 DEFAULT_ROTATE_PENDING_TIMER);
5693 if (ret) {
5694 cmd_ret = LTTNG_ERR_UNK;
5695 goto error;
5696 }
5697
5698 if (rotate_return) {
5699 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5700 }
5701
5702 session->chunk_being_archived = chunk_being_archived;
5703 chunk_being_archived = NULL;
5704 if (!quiet_rotation) {
5705 ret = notification_thread_command_session_rotation_ongoing(
5706 the_notification_thread_handle, session->name,
5707 session->uid, session->gid,
5708 ongoing_rotation_chunk_id);
5709 if (ret != LTTNG_OK) {
5710 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5711 session->name);
5712 cmd_ret = (lttng_error_code) ret;
5713 }
5714 }
5715
5716 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5717 session->name, ongoing_rotation_chunk_id);
5718 end:
5719 lttng_trace_chunk_put(new_trace_chunk);
5720 lttng_trace_chunk_put(chunk_being_archived);
5721 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5722 return ret;
5723 error:
5724 if (session_reset_rotation_state(session,
5725 LTTNG_ROTATION_STATE_ERROR)) {
5726 ERR("Failed to reset rotation state of session \"%s\"",
5727 session->name);
5728 }
5729 goto end;
5730 }
5731
5732 /*
5733 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5734 *
5735 * Check if the session has finished its rotation.
5736 *
5737 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5738 */
5739 int cmd_rotate_get_info(struct ltt_session *session,
5740 struct lttng_rotation_get_info_return *info_return,
5741 uint64_t rotation_id)
5742 {
5743 enum lttng_error_code cmd_ret = LTTNG_OK;
5744 enum lttng_rotation_state rotation_state;
5745
5746 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
5747 session->most_recent_chunk_id.value);
5748
5749 if (session->chunk_being_archived) {
5750 enum lttng_trace_chunk_status chunk_status;
5751 uint64_t chunk_id;
5752
5753 chunk_status = lttng_trace_chunk_get_id(
5754 session->chunk_being_archived,
5755 &chunk_id);
5756 LTTNG_ASSERT(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5757
5758 rotation_state = rotation_id == chunk_id ?
5759 LTTNG_ROTATION_STATE_ONGOING :
5760 LTTNG_ROTATION_STATE_EXPIRED;
5761 } else {
5762 if (session->last_archived_chunk_id.is_set &&
5763 rotation_id != session->last_archived_chunk_id.value) {
5764 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5765 } else {
5766 rotation_state = session->rotation_state;
5767 }
5768 }
5769
5770 switch (rotation_state) {
5771 case LTTNG_ROTATION_STATE_NO_ROTATION:
5772 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5773 session->name);
5774 goto end;
5775 case LTTNG_ROTATION_STATE_EXPIRED:
5776 DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
5777 rotation_id, session->name);
5778 break;
5779 case LTTNG_ROTATION_STATE_ONGOING:
5780 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5781 rotation_id, session->name);
5782 break;
5783 case LTTNG_ROTATION_STATE_COMPLETED:
5784 {
5785 int fmt_ret;
5786 char *chunk_path;
5787 char *current_tracing_path_reply;
5788 size_t current_tracing_path_reply_len;
5789
5790 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5791 rotation_id, session->name);
5792
5793 switch (session_get_consumer_destination_type(session)) {
5794 case CONSUMER_DST_LOCAL:
5795 current_tracing_path_reply =
5796 info_return->location.local.absolute_path;
5797 current_tracing_path_reply_len =
5798 sizeof(info_return->location.local.absolute_path);
5799 info_return->location_type =
5800 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5801 fmt_ret = asprintf(&chunk_path,
5802 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5803 session_get_base_path(session),
5804 session->last_archived_chunk_name);
5805 if (fmt_ret == -1) {
5806 PERROR("Failed to format the path of the last archived trace chunk");
5807 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5808 cmd_ret = LTTNG_ERR_UNK;
5809 goto end;
5810 }
5811 break;
5812 case CONSUMER_DST_NET:
5813 {
5814 uint16_t ctrl_port, data_port;
5815
5816 current_tracing_path_reply =
5817 info_return->location.relay.relative_path;
5818 current_tracing_path_reply_len =
5819 sizeof(info_return->location.relay.relative_path);
5820 /* Currently the only supported relay protocol. */
5821 info_return->location.relay.protocol =
5822 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5823
5824 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5825 session_get_net_consumer_hostname(session),
5826 sizeof(info_return->location.relay.host));
5827 if (fmt_ret) {
5828 ERR("Failed to copy host name to rotate_get_info reply");
5829 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5830 cmd_ret = LTTNG_ERR_SET_URL;
5831 goto end;
5832 }
5833
5834 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5835 info_return->location.relay.ports.control = ctrl_port;
5836 info_return->location.relay.ports.data = data_port;
5837 info_return->location_type =
5838 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5839 chunk_path = strdup(session->last_chunk_path);
5840 if (!chunk_path) {
5841 ERR("Failed to allocate the path of the last archived trace chunk");
5842 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5843 cmd_ret = LTTNG_ERR_UNK;
5844 goto end;
5845 }
5846 break;
5847 }
5848 default:
5849 abort();
5850 }
5851
5852 fmt_ret = lttng_strncpy(current_tracing_path_reply,
5853 chunk_path, current_tracing_path_reply_len);
5854 free(chunk_path);
5855 if (fmt_ret) {
5856 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5857 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5858 cmd_ret = LTTNG_ERR_UNK;
5859 goto end;
5860 }
5861
5862 break;
5863 }
5864 case LTTNG_ROTATION_STATE_ERROR:
5865 DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
5866 rotation_id, session->name);
5867 break;
5868 default:
5869 abort();
5870 }
5871
5872 cmd_ret = LTTNG_OK;
5873 end:
5874 info_return->status = (int32_t) rotation_state;
5875 return cmd_ret;
5876 }
5877
5878 /*
5879 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5880 *
5881 * Configure the automatic rotation parameters.
5882 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5883 * 'activate' to false means deactivate the rotation schedule and validate that
5884 * 'new_value' has the same value as the currently active value.
5885 *
5886 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5887 */
5888 int cmd_rotation_set_schedule(struct ltt_session *session,
5889 bool activate, enum lttng_rotation_schedule_type schedule_type,
5890 uint64_t new_value,
5891 struct notification_thread_handle *notification_thread_handle)
5892 {
5893 int ret;
5894 uint64_t *parameter_value;
5895
5896 LTTNG_ASSERT(session);
5897
5898 DBG("Cmd rotate set schedule session %s", session->name);
5899
5900 if (session->live_timer || !session->output_traces) {
5901 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5902 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5903 goto end;
5904 }
5905
5906 switch (schedule_type) {
5907 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5908 parameter_value = &session->rotate_size;
5909 break;
5910 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5911 parameter_value = &session->rotate_timer_period;
5912 if (new_value >= UINT_MAX) {
5913 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
5914 new_value, UINT_MAX);
5915 ret = LTTNG_ERR_INVALID;
5916 goto end;
5917 }
5918 break;
5919 default:
5920 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5921 ret = LTTNG_ERR_INVALID;
5922 goto end;
5923 }
5924
5925 /* Improper use of the API. */
5926 if (new_value == -1ULL) {
5927 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5928 ret = LTTNG_ERR_INVALID;
5929 goto end;
5930 }
5931
5932 /*
5933 * As indicated in struct ltt_session's comments, a value of == 0 means
5934 * this schedule rotation type is not in use.
5935 *
5936 * Reject the command if we were asked to activate a schedule that was
5937 * already active.
5938 */
5939 if (activate && *parameter_value != 0) {
5940 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5941 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5942 goto end;
5943 }
5944
5945 /*
5946 * Reject the command if we were asked to deactivate a schedule that was
5947 * not active.
5948 */
5949 if (!activate && *parameter_value == 0) {
5950 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5951 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5952 goto end;
5953 }
5954
5955 /*
5956 * Reject the command if we were asked to deactivate a schedule that
5957 * doesn't exist.
5958 */
5959 if (!activate && *parameter_value != new_value) {
5960 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5961 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5962 goto end;
5963 }
5964
5965 *parameter_value = activate ? new_value : 0;
5966
5967 switch (schedule_type) {
5968 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5969 if (activate && session->active) {
5970 /*
5971 * Only start the timer if the session is active,
5972 * otherwise it will be started when the session starts.
5973 */
5974 ret = timer_session_rotation_schedule_timer_start(
5975 session, new_value);
5976 if (ret) {
5977 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5978 ret = LTTNG_ERR_UNK;
5979 goto end;
5980 }
5981 } else {
5982 ret = timer_session_rotation_schedule_timer_stop(
5983 session);
5984 if (ret) {
5985 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5986 ret = LTTNG_ERR_UNK;
5987 goto end;
5988 }
5989 }
5990 break;
5991 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5992 if (activate) {
5993 ret = subscribe_session_consumed_size_rotation(session,
5994 new_value, notification_thread_handle);
5995 if (ret) {
5996 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5997 ret = LTTNG_ERR_UNK;
5998 goto end;
5999 }
6000 } else {
6001 ret = unsubscribe_session_consumed_size_rotation(session,
6002 notification_thread_handle);
6003 if (ret) {
6004 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
6005 ret = LTTNG_ERR_UNK;
6006 goto end;
6007 }
6008
6009 }
6010 break;
6011 default:
6012 /* Would have been caught before. */
6013 abort();
6014 }
6015
6016 ret = LTTNG_OK;
6017
6018 goto end;
6019
6020 end:
6021 return ret;
6022 }
6023
6024 /* Wait for a given path to be removed before continuing. */
6025 static enum lttng_error_code wait_on_path(void *path_data)
6026 {
6027 const char *shm_path = (const char *) path_data;
6028
6029 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6030 shm_path);
6031 while (true) {
6032 int ret;
6033 struct stat st;
6034
6035 ret = stat(shm_path, &st);
6036 if (ret) {
6037 if (errno != ENOENT) {
6038 PERROR("stat() returned an error while checking for the existence of the shm path");
6039 } else {
6040 DBG("shm path no longer exists, completing the destruction of session");
6041 }
6042 break;
6043 } else {
6044 if (!S_ISDIR(st.st_mode)) {
6045 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6046 shm_path);
6047 break;
6048 }
6049 }
6050 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
6051 }
6052 return LTTNG_OK;
6053 }
6054
6055 /*
6056 * Returns a pointer to a handler to run on completion of a command.
6057 * Returns NULL if no handler has to be run for the last command executed.
6058 */
6059 const struct cmd_completion_handler *cmd_pop_completion_handler(void)
6060 {
6061 struct cmd_completion_handler *handler = current_completion_handler;
6062
6063 current_completion_handler = NULL;
6064 return handler;
6065 }
6066
6067 /*
6068 * Init command subsystem.
6069 */
6070 void cmd_init(void)
6071 {
6072 /*
6073 * Set network sequence index to 1 for streams to match a relayd
6074 * socket on the consumer side.
6075 */
6076 pthread_mutex_lock(&relayd_net_seq_idx_lock);
6077 relayd_net_seq_idx = 1;
6078 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
6079
6080 DBG("Command subsystem initialized");
6081 }
This page took 0.185074 seconds and 5 git commands to generate.