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