9552bbb8680f363477b2d56811196862f7e324c4
[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 command_ctx *cmd_ctx,
1869 const struct lttng_event_context *event_context, int kwpipe)
1870 {
1871 int ret, chan_kern_created = 0, chan_ust_created = 0;
1872 const enum lttng_domain_type domain = cmd_ctx->lsm.domain.type;
1873 const struct ltt_session *session = cmd_ctx->session;
1874 const char *channel_name = cmd_ctx->lsm.u.context.channel_name;
1875
1876 /*
1877 * Don't try to add a context if the session has been started at
1878 * some point in time before. The tracer does not allow it and would
1879 * result in a corrupted trace.
1880 */
1881 if (cmd_ctx->session->has_been_started) {
1882 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
1883 goto end;
1884 }
1885
1886 switch (domain) {
1887 case LTTNG_DOMAIN_KERNEL:
1888 assert(session->kernel_session);
1889
1890 if (session->kernel_session->channel_count == 0) {
1891 /* Create default channel */
1892 ret = channel_kernel_create(session->kernel_session, NULL, kwpipe);
1893 if (ret != LTTNG_OK) {
1894 goto error;
1895 }
1896 chan_kern_created = 1;
1897 }
1898 /* Add kernel context to kernel tracer */
1899 ret = context_kernel_add(session->kernel_session,
1900 event_context, channel_name);
1901 if (ret != LTTNG_OK) {
1902 goto error;
1903 }
1904 break;
1905 case LTTNG_DOMAIN_JUL:
1906 case LTTNG_DOMAIN_LOG4J:
1907 {
1908 /*
1909 * Validate channel name.
1910 * If no channel name is given and the domain is JUL or LOG4J,
1911 * set it to the appropriate domain-specific channel name. If
1912 * a name is provided but does not match the expexted channel
1913 * name, return an error.
1914 */
1915 if (domain == LTTNG_DOMAIN_JUL && *channel_name &&
1916 strcmp(channel_name,
1917 DEFAULT_JUL_CHANNEL_NAME)) {
1918 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1919 goto error;
1920 } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name &&
1921 strcmp(channel_name,
1922 DEFAULT_LOG4J_CHANNEL_NAME)) {
1923 ret = LTTNG_ERR_UST_CHAN_NOT_FOUND;
1924 goto error;
1925 }
1926 /* break is _not_ missing here. */
1927 }
1928 case LTTNG_DOMAIN_UST:
1929 {
1930 struct ltt_ust_session *usess = session->ust_session;
1931 unsigned int chan_count;
1932
1933 assert(usess);
1934
1935 chan_count = lttng_ht_get_count(usess->domain_global.channels);
1936 if (chan_count == 0) {
1937 struct lttng_channel *attr;
1938 /* Create default channel */
1939 attr = channel_new_default_attr(domain, usess->buffer_type);
1940 if (attr == NULL) {
1941 ret = LTTNG_ERR_FATAL;
1942 goto error;
1943 }
1944
1945 ret = channel_ust_create(usess, attr, usess->buffer_type);
1946 if (ret != LTTNG_OK) {
1947 free(attr);
1948 goto error;
1949 }
1950 channel_attr_destroy(attr);
1951 chan_ust_created = 1;
1952 }
1953
1954 ret = context_ust_add(usess, domain, event_context,
1955 channel_name);
1956 if (ret != LTTNG_OK) {
1957 goto error;
1958 }
1959 break;
1960 }
1961 default:
1962 ret = LTTNG_ERR_UND;
1963 goto error;
1964 }
1965
1966 ret = LTTNG_OK;
1967 goto end;
1968
1969 error:
1970 if (chan_kern_created) {
1971 struct ltt_kernel_channel *kchan =
1972 trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME,
1973 session->kernel_session);
1974 /* Created previously, this should NOT fail. */
1975 assert(kchan);
1976 kernel_destroy_channel(kchan);
1977 }
1978
1979 if (chan_ust_created) {
1980 struct ltt_ust_channel *uchan =
1981 trace_ust_find_channel_by_name(
1982 session->ust_session->domain_global.channels,
1983 DEFAULT_CHANNEL_NAME);
1984 /* Created previously, this should NOT fail. */
1985 assert(uchan);
1986 /* Remove from the channel list of the session. */
1987 trace_ust_delete_channel(session->ust_session->domain_global.channels,
1988 uchan);
1989 trace_ust_destroy_channel(uchan);
1990 }
1991 end:
1992 return ret;
1993 }
1994
1995 static inline bool name_starts_with(const char *name, const char *prefix)
1996 {
1997 const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN);
1998
1999 return !strncmp(name, prefix, max_cmp_len);
2000 }
2001
2002 /* Perform userspace-specific event name validation */
2003 static int validate_ust_event_name(const char *name)
2004 {
2005 int ret = 0;
2006
2007 if (!name) {
2008 ret = -1;
2009 goto end;
2010 }
2011
2012 /*
2013 * Check name against all internal UST event component namespaces used
2014 * by the agents.
2015 */
2016 if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) ||
2017 name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) ||
2018 name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) {
2019 ret = -1;
2020 }
2021
2022 end:
2023 return ret;
2024 }
2025
2026 /*
2027 * Internal version of cmd_enable_event() with a supplemental
2028 * "internal_event" flag which is used to enable internal events which should
2029 * be hidden from clients. Such events are used in the agent implementation to
2030 * enable the events through which all "agent" events are funeled.
2031 */
2032 static int _cmd_enable_event(struct ltt_session *session,
2033 const struct lttng_domain *domain,
2034 char *channel_name, struct lttng_event *event,
2035 char *filter_expression,
2036 struct lttng_bytecode *filter,
2037 struct lttng_event_exclusion *exclusion,
2038 int wpipe, bool internal_event)
2039 {
2040 int ret = 0, channel_created = 0;
2041 struct lttng_channel *attr = NULL;
2042
2043 assert(session);
2044 assert(event);
2045 assert(channel_name);
2046
2047 /* If we have a filter, we must have its filter expression */
2048 assert(!(!!filter_expression ^ !!filter));
2049
2050 /* Normalize event name as a globbing pattern */
2051 strutils_normalize_star_glob_pattern(event->name);
2052
2053 /* Normalize exclusion names as globbing patterns */
2054 if (exclusion) {
2055 size_t i;
2056
2057 for (i = 0; i < exclusion->count; i++) {
2058 char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i);
2059
2060 strutils_normalize_star_glob_pattern(name);
2061 }
2062 }
2063
2064 DBG("Enable event command for event \'%s\'", event->name);
2065
2066 rcu_read_lock();
2067
2068 switch (domain->type) {
2069 case LTTNG_DOMAIN_KERNEL:
2070 {
2071 struct ltt_kernel_channel *kchan;
2072
2073 /*
2074 * If a non-default channel has been created in the
2075 * session, explicitely require that -c chan_name needs
2076 * to be provided.
2077 */
2078 if (session->kernel_session->has_non_default_channel
2079 && channel_name[0] == '\0') {
2080 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2081 goto error;
2082 }
2083
2084 kchan = trace_kernel_get_channel_by_name(channel_name,
2085 session->kernel_session);
2086 if (kchan == NULL) {
2087 attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL,
2088 LTTNG_BUFFER_GLOBAL);
2089 if (attr == NULL) {
2090 ret = LTTNG_ERR_FATAL;
2091 goto error;
2092 }
2093 if (lttng_strncpy(attr->name, channel_name,
2094 sizeof(attr->name))) {
2095 ret = LTTNG_ERR_INVALID;
2096 goto error;
2097 }
2098
2099 ret = cmd_enable_channel_internal(
2100 session, domain, attr, wpipe);
2101 if (ret != LTTNG_OK) {
2102 goto error;
2103 }
2104 channel_created = 1;
2105 }
2106
2107 /* Get the newly created kernel channel pointer */
2108 kchan = trace_kernel_get_channel_by_name(channel_name,
2109 session->kernel_session);
2110 if (kchan == NULL) {
2111 /* This sould not happen... */
2112 ret = LTTNG_ERR_FATAL;
2113 goto error;
2114 }
2115
2116 switch (event->type) {
2117 case LTTNG_EVENT_ALL:
2118 {
2119 char *filter_expression_a = NULL;
2120 struct lttng_bytecode *filter_a = NULL;
2121
2122 /*
2123 * We need to duplicate filter_expression and filter,
2124 * because ownership is passed to first enable
2125 * event.
2126 */
2127 if (filter_expression) {
2128 filter_expression_a = strdup(filter_expression);
2129 if (!filter_expression_a) {
2130 ret = LTTNG_ERR_FATAL;
2131 goto error;
2132 }
2133 }
2134 if (filter) {
2135 filter_a = zmalloc(sizeof(*filter_a) + filter->len);
2136 if (!filter_a) {
2137 free(filter_expression_a);
2138 ret = LTTNG_ERR_FATAL;
2139 goto error;
2140 }
2141 memcpy(filter_a, filter, sizeof(*filter_a) + filter->len);
2142 }
2143 event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */
2144 ret = event_kernel_enable_event(kchan, event,
2145 filter_expression, filter);
2146 /* We have passed ownership */
2147 filter_expression = NULL;
2148 filter = NULL;
2149 if (ret != LTTNG_OK) {
2150 if (channel_created) {
2151 /* Let's not leak a useless channel. */
2152 kernel_destroy_channel(kchan);
2153 }
2154 free(filter_expression_a);
2155 free(filter_a);
2156 goto error;
2157 }
2158 event->type = LTTNG_EVENT_SYSCALL; /* Hack */
2159 ret = event_kernel_enable_event(kchan, event,
2160 filter_expression_a, filter_a);
2161 /* We have passed ownership */
2162 filter_expression_a = NULL;
2163 filter_a = NULL;
2164 if (ret != LTTNG_OK) {
2165 goto error;
2166 }
2167 break;
2168 }
2169 case LTTNG_EVENT_PROBE:
2170 case LTTNG_EVENT_USERSPACE_PROBE:
2171 case LTTNG_EVENT_FUNCTION:
2172 case LTTNG_EVENT_FUNCTION_ENTRY:
2173 case LTTNG_EVENT_TRACEPOINT:
2174 ret = event_kernel_enable_event(kchan, event,
2175 filter_expression, filter);
2176 /* We have passed ownership */
2177 filter_expression = NULL;
2178 filter = NULL;
2179 if (ret != LTTNG_OK) {
2180 if (channel_created) {
2181 /* Let's not leak a useless channel. */
2182 kernel_destroy_channel(kchan);
2183 }
2184 goto error;
2185 }
2186 break;
2187 case LTTNG_EVENT_SYSCALL:
2188 ret = event_kernel_enable_event(kchan, event,
2189 filter_expression, filter);
2190 /* We have passed ownership */
2191 filter_expression = NULL;
2192 filter = NULL;
2193 if (ret != LTTNG_OK) {
2194 goto error;
2195 }
2196 break;
2197 default:
2198 ret = LTTNG_ERR_UNK;
2199 goto error;
2200 }
2201
2202 kernel_wait_quiescent();
2203 break;
2204 }
2205 case LTTNG_DOMAIN_UST:
2206 {
2207 struct ltt_ust_channel *uchan;
2208 struct ltt_ust_session *usess = session->ust_session;
2209
2210 assert(usess);
2211
2212 /*
2213 * If a non-default channel has been created in the
2214 * session, explicitely require that -c chan_name needs
2215 * to be provided.
2216 */
2217 if (usess->has_non_default_channel && channel_name[0] == '\0') {
2218 ret = LTTNG_ERR_NEED_CHANNEL_NAME;
2219 goto error;
2220 }
2221
2222 /* Get channel from global UST domain */
2223 uchan = trace_ust_find_channel_by_name(usess->domain_global.channels,
2224 channel_name);
2225 if (uchan == NULL) {
2226 /* Create default channel */
2227 attr = channel_new_default_attr(LTTNG_DOMAIN_UST,
2228 usess->buffer_type);
2229 if (attr == NULL) {
2230 ret = LTTNG_ERR_FATAL;
2231 goto error;
2232 }
2233 if (lttng_strncpy(attr->name, channel_name,
2234 sizeof(attr->name))) {
2235 ret = LTTNG_ERR_INVALID;
2236 goto error;
2237 }
2238
2239 ret = cmd_enable_channel_internal(
2240 session, domain, attr, wpipe);
2241 if (ret != LTTNG_OK) {
2242 goto error;
2243 }
2244
2245 /* Get the newly created channel reference back */
2246 uchan = trace_ust_find_channel_by_name(
2247 usess->domain_global.channels, channel_name);
2248 assert(uchan);
2249 }
2250
2251 if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) {
2252 /*
2253 * Don't allow users to add UST events to channels which
2254 * are assigned to a userspace subdomain (JUL, Log4J,
2255 * Python, etc.).
2256 */
2257 ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN;
2258 goto error;
2259 }
2260
2261 if (!internal_event) {
2262 /*
2263 * Ensure the event name is not reserved for internal
2264 * use.
2265 */
2266 ret = validate_ust_event_name(event->name);
2267 if (ret) {
2268 WARN("Userspace event name %s failed validation.",
2269 event->name);
2270 ret = LTTNG_ERR_INVALID_EVENT_NAME;
2271 goto error;
2272 }
2273 }
2274
2275 /* At this point, the session and channel exist on the tracer */
2276 ret = event_ust_enable_tracepoint(usess, uchan, event,
2277 filter_expression, filter, exclusion,
2278 internal_event);
2279 /* We have passed ownership */
2280 filter_expression = NULL;
2281 filter = NULL;
2282 exclusion = NULL;
2283 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2284 goto already_enabled;
2285 } else if (ret != LTTNG_OK) {
2286 goto error;
2287 }
2288 break;
2289 }
2290 case LTTNG_DOMAIN_LOG4J:
2291 case LTTNG_DOMAIN_JUL:
2292 case LTTNG_DOMAIN_PYTHON:
2293 {
2294 const char *default_event_name, *default_chan_name;
2295 struct agent *agt;
2296 struct lttng_event uevent;
2297 struct lttng_domain tmp_dom;
2298 struct ltt_ust_session *usess = session->ust_session;
2299
2300 assert(usess);
2301
2302 if (!agent_tracing_is_enabled()) {
2303 DBG("Attempted to enable an event in an agent domain but the agent thread is not running");
2304 ret = LTTNG_ERR_AGENT_TRACING_DISABLED;
2305 goto error;
2306 }
2307
2308 agt = trace_ust_find_agent(usess, domain->type);
2309 if (!agt) {
2310 agt = agent_create(domain->type);
2311 if (!agt) {
2312 ret = LTTNG_ERR_NOMEM;
2313 goto error;
2314 }
2315 agent_add(agt, usess->agents);
2316 }
2317
2318 /* Create the default tracepoint. */
2319 memset(&uevent, 0, sizeof(uevent));
2320 uevent.type = LTTNG_EVENT_TRACEPOINT;
2321 uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
2322 default_event_name = event_get_default_agent_ust_name(
2323 domain->type);
2324 if (!default_event_name) {
2325 ret = LTTNG_ERR_FATAL;
2326 goto error;
2327 }
2328 strncpy(uevent.name, default_event_name, sizeof(uevent.name));
2329 uevent.name[sizeof(uevent.name) - 1] = '\0';
2330
2331 /*
2332 * The domain type is changed because we are about to enable the
2333 * default channel and event for the JUL domain that are hardcoded.
2334 * This happens in the UST domain.
2335 */
2336 memcpy(&tmp_dom, domain, sizeof(tmp_dom));
2337 tmp_dom.type = LTTNG_DOMAIN_UST;
2338
2339 switch (domain->type) {
2340 case LTTNG_DOMAIN_LOG4J:
2341 default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME;
2342 break;
2343 case LTTNG_DOMAIN_JUL:
2344 default_chan_name = DEFAULT_JUL_CHANNEL_NAME;
2345 break;
2346 case LTTNG_DOMAIN_PYTHON:
2347 default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME;
2348 break;
2349 default:
2350 /* The switch/case we are in makes this impossible */
2351 assert(0);
2352 }
2353
2354 {
2355 char *filter_expression_copy = NULL;
2356 struct lttng_bytecode *filter_copy = NULL;
2357
2358 if (filter) {
2359 const size_t filter_size = sizeof(
2360 struct lttng_bytecode)
2361 + filter->len;
2362
2363 filter_copy = zmalloc(filter_size);
2364 if (!filter_copy) {
2365 ret = LTTNG_ERR_NOMEM;
2366 goto error;
2367 }
2368 memcpy(filter_copy, filter, filter_size);
2369
2370 filter_expression_copy =
2371 strdup(filter_expression);
2372 if (!filter_expression) {
2373 ret = LTTNG_ERR_NOMEM;
2374 }
2375
2376 if (!filter_expression_copy || !filter_copy) {
2377 free(filter_expression_copy);
2378 free(filter_copy);
2379 goto error;
2380 }
2381 }
2382
2383 ret = cmd_enable_event_internal(session, &tmp_dom,
2384 (char *) default_chan_name,
2385 &uevent, filter_expression_copy,
2386 filter_copy, NULL, wpipe);
2387 }
2388
2389 if (ret == LTTNG_ERR_UST_EVENT_ENABLED) {
2390 goto already_enabled;
2391 } else if (ret != LTTNG_OK) {
2392 goto error;
2393 }
2394
2395 /* The wild card * means that everything should be enabled. */
2396 if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) {
2397 ret = event_agent_enable_all(usess, agt, event, filter,
2398 filter_expression);
2399 } else {
2400 ret = event_agent_enable(usess, agt, event, filter,
2401 filter_expression);
2402 }
2403 filter = NULL;
2404 filter_expression = NULL;
2405 if (ret != LTTNG_OK) {
2406 goto error;
2407 }
2408
2409 break;
2410 }
2411 default:
2412 ret = LTTNG_ERR_UND;
2413 goto error;
2414 }
2415
2416 ret = LTTNG_OK;
2417
2418 already_enabled:
2419 error:
2420 free(filter_expression);
2421 free(filter);
2422 free(exclusion);
2423 channel_attr_destroy(attr);
2424 rcu_read_unlock();
2425 return ret;
2426 }
2427
2428 /*
2429 * Command LTTNG_ENABLE_EVENT processed by the client thread.
2430 * We own filter, exclusion, and filter_expression.
2431 */
2432 int cmd_enable_event(struct command_ctx *cmd_ctx,
2433 struct lttng_event *event,
2434 char *filter_expression,
2435 struct lttng_event_exclusion *exclusion,
2436 struct lttng_bytecode *bytecode,
2437 int wpipe)
2438 {
2439 int ret;
2440 /*
2441 * Copied to ensure proper alignment since 'lsm' is a packed structure.
2442 */
2443 const struct lttng_domain command_domain = cmd_ctx->lsm.domain;
2444
2445 /*
2446 * The ownership of the following parameters is transferred to
2447 * _cmd_enable_event:
2448 *
2449 * - filter_expression,
2450 * - bytecode,
2451 * - exclusion
2452 */
2453 ret = _cmd_enable_event(cmd_ctx->session,
2454 &command_domain,
2455 cmd_ctx->lsm.u.enable.channel_name, event,
2456 filter_expression, bytecode, exclusion, wpipe, false);
2457 filter_expression = NULL;
2458 bytecode = NULL;
2459 exclusion = NULL;
2460 return ret;
2461 }
2462
2463 /*
2464 * Enable an event which is internal to LTTng. An internal should
2465 * never be made visible to clients and are immune to checks such as
2466 * reserved names.
2467 */
2468 static int cmd_enable_event_internal(struct ltt_session *session,
2469 const struct lttng_domain *domain,
2470 char *channel_name, struct lttng_event *event,
2471 char *filter_expression,
2472 struct lttng_bytecode *filter,
2473 struct lttng_event_exclusion *exclusion,
2474 int wpipe)
2475 {
2476 return _cmd_enable_event(session, domain, channel_name, event,
2477 filter_expression, filter, exclusion, wpipe, true);
2478 }
2479
2480 /*
2481 * Command LTTNG_LIST_TRACEPOINTS processed by the client thread.
2482 */
2483 enum lttng_error_code cmd_list_tracepoints(enum lttng_domain_type domain,
2484 struct lttng_payload *reply_payload)
2485 {
2486 enum lttng_error_code ret_code;
2487 int ret;
2488 ssize_t i, nb_events = 0;
2489 struct lttng_event *events = NULL;
2490 struct lttcomm_list_command_header reply_command_header = {};
2491 size_t reply_command_header_offset;
2492
2493 assert(reply_payload);
2494
2495 /* Reserve space for command reply header. */
2496 reply_command_header_offset = reply_payload->buffer.size;
2497 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
2498 reply_command_header_offset +
2499 sizeof(struct lttcomm_list_command_header));
2500 if (ret) {
2501 ret_code = LTTNG_ERR_NOMEM;
2502 goto error;
2503 }
2504
2505 switch (domain) {
2506 case LTTNG_DOMAIN_KERNEL:
2507 nb_events = kernel_list_events(&events);
2508 if (nb_events < 0) {
2509 ret_code = LTTNG_ERR_KERN_LIST_FAIL;
2510 goto error;
2511 }
2512 break;
2513 case LTTNG_DOMAIN_UST:
2514 nb_events = ust_app_list_events(&events);
2515 if (nb_events < 0) {
2516 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2517 goto error;
2518 }
2519 break;
2520 case LTTNG_DOMAIN_LOG4J:
2521 case LTTNG_DOMAIN_JUL:
2522 case LTTNG_DOMAIN_PYTHON:
2523 nb_events = agent_list_events(&events, domain);
2524 if (nb_events < 0) {
2525 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2526 goto error;
2527 }
2528 break;
2529 default:
2530 ret_code = LTTNG_ERR_UND;
2531 goto error;
2532 }
2533
2534 for (i = 0; i < nb_events; i++) {
2535 ret = lttng_event_serialize(&events[i], 0, NULL, NULL, 0, NULL,
2536 reply_payload);
2537 if (ret) {
2538 ret_code = LTTNG_ERR_NOMEM;
2539 goto error;
2540 }
2541 }
2542
2543 if (nb_events > UINT32_MAX) {
2544 ERR("Tracepoint count would overflow the tracepoint listing command's reply");
2545 ret_code = LTTNG_ERR_OVERFLOW;
2546 goto error;
2547 }
2548
2549 /* Update command reply header. */
2550 reply_command_header.count = (uint32_t) nb_events;
2551 memcpy(reply_payload->buffer.data + reply_command_header_offset, &reply_command_header,
2552 sizeof(reply_command_header));
2553
2554 ret_code = LTTNG_OK;
2555 error:
2556 free(events);
2557 return ret_code;
2558 }
2559
2560 /*
2561 * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread.
2562 */
2563 enum lttng_error_code cmd_list_tracepoint_fields(enum lttng_domain_type domain,
2564 struct lttng_payload *reply)
2565 {
2566 enum lttng_error_code ret_code;
2567 int ret;
2568 unsigned int i, nb_fields;
2569 struct lttng_event_field *fields = NULL;
2570 struct lttcomm_list_command_header reply_command_header = {};
2571 size_t reply_command_header_offset;
2572
2573 assert(reply);
2574
2575 /* Reserve space for command reply header. */
2576 reply_command_header_offset = reply->buffer.size;
2577 ret = lttng_dynamic_buffer_set_size(&reply->buffer,
2578 reply_command_header_offset +
2579 sizeof(struct lttcomm_list_command_header));
2580 if (ret) {
2581 ret_code = LTTNG_ERR_NOMEM;
2582 goto error;
2583 }
2584
2585 switch (domain) {
2586 case LTTNG_DOMAIN_UST:
2587 ret = ust_app_list_event_fields(&fields);
2588 if (ret < 0) {
2589 ret_code = LTTNG_ERR_UST_LIST_FAIL;
2590 goto error;
2591 }
2592
2593 break;
2594 case LTTNG_DOMAIN_KERNEL:
2595 default: /* fall-through */
2596 ret_code = LTTNG_ERR_UND;
2597 goto error;
2598 }
2599
2600 nb_fields = ret;
2601
2602 for (i = 0; i < nb_fields; i++) {
2603 ret = lttng_event_field_serialize(&fields[i], reply);
2604 if (ret) {
2605 ret_code = LTTNG_ERR_NOMEM;
2606 goto error;
2607 }
2608 }
2609
2610 if (nb_fields > UINT32_MAX) {
2611 ERR("Tracepoint field count would overflow the tracepoint field listing command's reply");
2612 ret_code = LTTNG_ERR_OVERFLOW;
2613 goto error;
2614 }
2615
2616 /* Update command reply header. */
2617 reply_command_header.count = (uint32_t) nb_fields;
2618
2619 memcpy(reply->buffer.data + reply_command_header_offset, &reply_command_header,
2620 sizeof(reply_command_header));
2621
2622 ret_code = LTTNG_OK;
2623
2624 error:
2625 free(fields);
2626 return ret_code;
2627 }
2628
2629 enum lttng_error_code cmd_list_syscalls(
2630 struct lttng_payload *reply_payload)
2631 {
2632 enum lttng_error_code ret_code;
2633 ssize_t nb_events, i;
2634 int ret;
2635 struct lttng_event *events = NULL;
2636 struct lttcomm_list_command_header reply_command_header = {};
2637 size_t reply_command_header_offset;
2638
2639 assert(reply_payload);
2640
2641 /* Reserve space for command reply header. */
2642 reply_command_header_offset = reply_payload->buffer.size;
2643 ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
2644 reply_command_header_offset +
2645 sizeof(struct lttcomm_list_command_header));
2646 if (ret) {
2647 ret_code = LTTNG_ERR_NOMEM;
2648 goto end;
2649 }
2650
2651 nb_events = syscall_table_list(&events);
2652 if (nb_events < 0) {
2653 ret_code = (enum lttng_error_code) -nb_events;
2654 goto end;
2655 }
2656
2657 for (i = 0; i < nb_events; i++) {
2658 ret = lttng_event_serialize(&events[i], 0, NULL, NULL, 0, NULL,
2659 reply_payload);
2660 if (ret) {
2661 ret_code = LTTNG_ERR_NOMEM;
2662 goto end;
2663 }
2664 }
2665
2666 if (nb_events > UINT32_MAX) {
2667 ERR("Syscall count would overflow the syscall listing command's reply");
2668 ret_code = LTTNG_ERR_OVERFLOW;
2669 goto end;
2670 }
2671
2672 /* Update command reply header. */
2673 reply_command_header.count = (uint32_t) nb_events;
2674 memcpy(reply_payload->buffer.data + reply_command_header_offset, &reply_command_header,
2675 sizeof(reply_command_header));
2676
2677 ret_code = LTTNG_OK;
2678 end:
2679 free(events);
2680 return ret_code;
2681 }
2682
2683 /*
2684 * Command LTTNG_START_TRACE processed by the client thread.
2685 *
2686 * Called with session mutex held.
2687 */
2688 int cmd_start_trace(struct ltt_session *session)
2689 {
2690 enum lttng_error_code ret;
2691 unsigned long nb_chan = 0;
2692 struct ltt_kernel_session *ksession;
2693 struct ltt_ust_session *usess;
2694 const bool session_rotated_after_last_stop =
2695 session->rotated_after_last_stop;
2696 const bool session_cleared_after_last_stop =
2697 session->cleared_after_last_stop;
2698
2699 assert(session);
2700
2701 /* Ease our life a bit ;) */
2702 ksession = session->kernel_session;
2703 usess = session->ust_session;
2704
2705 /* Is the session already started? */
2706 if (session->active) {
2707 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2708 /* Perform nothing */
2709 goto end;
2710 }
2711
2712 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING &&
2713 !session->current_trace_chunk) {
2714 /*
2715 * A rotation was launched while the session was stopped and
2716 * it has not been completed yet. It is not possible to start
2717 * the session since starting the session here would require a
2718 * rotation from "NULL" to a new trace chunk. That rotation
2719 * would overlap with the ongoing rotation, which is not
2720 * supported.
2721 */
2722 WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing",
2723 session->name);
2724 ret = LTTNG_ERR_ROTATION_PENDING;
2725 goto error;
2726 }
2727
2728 /*
2729 * Starting a session without channel is useless since after that it's not
2730 * possible to enable channel thus inform the client.
2731 */
2732 if (usess && usess->domain_global.channels) {
2733 nb_chan += lttng_ht_get_count(usess->domain_global.channels);
2734 }
2735 if (ksession) {
2736 nb_chan += ksession->channel_count;
2737 }
2738 if (!nb_chan) {
2739 ret = LTTNG_ERR_NO_CHANNEL;
2740 goto error;
2741 }
2742
2743 session->active = 1;
2744 session->rotated_after_last_stop = false;
2745 session->cleared_after_last_stop = false;
2746 if (session->output_traces && !session->current_trace_chunk) {
2747 if (!session->has_been_started) {
2748 struct lttng_trace_chunk *trace_chunk;
2749
2750 DBG("Creating initial trace chunk of session \"%s\"",
2751 session->name);
2752 trace_chunk = session_create_new_trace_chunk(
2753 session, NULL, NULL, NULL);
2754 if (!trace_chunk) {
2755 ret = LTTNG_ERR_CREATE_DIR_FAIL;
2756 goto error;
2757 }
2758 assert(!session->current_trace_chunk);
2759 ret = session_set_trace_chunk(session, trace_chunk,
2760 NULL);
2761 lttng_trace_chunk_put(trace_chunk);
2762 if (ret) {
2763 ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
2764 goto error;
2765 }
2766 } else {
2767 DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk",
2768 session->name);
2769 /*
2770 * Rotate existing streams into the new chunk.
2771 * This is a "quiet" rotation has no client has
2772 * explicitly requested this operation.
2773 *
2774 * There is also no need to wait for the rotation
2775 * to complete as it will happen immediately. No data
2776 * was produced as the session was stopped, so the
2777 * rotation should happen on reception of the command.
2778 */
2779 ret = cmd_rotate_session(session, NULL, true,
2780 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
2781 if (ret != LTTNG_OK) {
2782 goto error;
2783 }
2784 }
2785 }
2786
2787 /* Kernel tracing */
2788 if (ksession != NULL) {
2789 DBG("Start kernel tracing session %s", session->name);
2790 ret = start_kernel_session(ksession);
2791 if (ret != LTTNG_OK) {
2792 goto error;
2793 }
2794 }
2795
2796 /* Flag session that trace should start automatically */
2797 if (usess) {
2798 int int_ret = ust_app_start_trace_all(usess);
2799
2800 if (int_ret < 0) {
2801 ret = LTTNG_ERR_UST_START_FAIL;
2802 goto error;
2803 }
2804 }
2805
2806 /*
2807 * Open a packet in every stream of the session to ensure that viewers
2808 * can correctly identify the boundaries of the periods during which
2809 * tracing was active for this session.
2810 */
2811 ret = session_open_packets(session);
2812 if (ret != LTTNG_OK) {
2813 goto error;
2814 }
2815
2816 /*
2817 * Clear the flag that indicates that a rotation was done while the
2818 * session was stopped.
2819 */
2820 session->rotated_after_last_stop = false;
2821
2822 if (session->rotate_timer_period) {
2823 int int_ret = timer_session_rotation_schedule_timer_start(
2824 session, session->rotate_timer_period);
2825
2826 if (int_ret < 0) {
2827 ERR("Failed to enable rotate timer");
2828 ret = LTTNG_ERR_UNK;
2829 goto error;
2830 }
2831 }
2832
2833 ret = LTTNG_OK;
2834
2835 error:
2836 if (ret == LTTNG_OK) {
2837 /* Flag this after a successful start. */
2838 session->has_been_started |= 1;
2839 } else {
2840 session->active = 0;
2841 /* Restore initial state on error. */
2842 session->rotated_after_last_stop =
2843 session_rotated_after_last_stop;
2844 session->cleared_after_last_stop =
2845 session_cleared_after_last_stop;
2846 }
2847 end:
2848 return ret;
2849 }
2850
2851 /*
2852 * Command LTTNG_STOP_TRACE processed by the client thread.
2853 */
2854 int cmd_stop_trace(struct ltt_session *session)
2855 {
2856 int ret;
2857 struct ltt_kernel_session *ksession;
2858 struct ltt_ust_session *usess;
2859
2860 assert(session);
2861
2862 DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id);
2863 /* Short cut */
2864 ksession = session->kernel_session;
2865 usess = session->ust_session;
2866
2867 /* Session is not active. Skip everythong and inform the client. */
2868 if (!session->active) {
2869 ret = LTTNG_ERR_TRACE_ALREADY_STOPPED;
2870 goto error;
2871 }
2872
2873 ret = stop_kernel_session(ksession);
2874 if (ret != LTTNG_OK) {
2875 goto error;
2876 }
2877
2878 if (usess && usess->active) {
2879 ret = ust_app_stop_trace_all(usess);
2880 if (ret < 0) {
2881 ret = LTTNG_ERR_UST_STOP_FAIL;
2882 goto error;
2883 }
2884 }
2885
2886 DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name,
2887 session->id);
2888 /* Flag inactive after a successful stop. */
2889 session->active = 0;
2890 ret = LTTNG_OK;
2891
2892 error:
2893 return ret;
2894 }
2895
2896 /*
2897 * Set the base_path of the session only if subdir of a control uris is set.
2898 * Return LTTNG_OK on success, otherwise LTTNG_ERR_*.
2899 */
2900 static int set_session_base_path_from_uris(struct ltt_session *session,
2901 size_t nb_uri,
2902 struct lttng_uri *uris)
2903 {
2904 int ret;
2905 size_t i;
2906
2907 for (i = 0; i < nb_uri; i++) {
2908 if (uris[i].stype != LTTNG_STREAM_CONTROL ||
2909 uris[i].subdir[0] == '\0') {
2910 /* Not interested in these URIs */
2911 continue;
2912 }
2913
2914 if (session->base_path != NULL) {
2915 free(session->base_path);
2916 session->base_path = NULL;
2917 }
2918
2919 /* Set session base_path */
2920 session->base_path = strdup(uris[i].subdir);
2921 if (!session->base_path) {
2922 PERROR("Failed to copy base path \"%s\" to session \"%s\"",
2923 uris[i].subdir, session->name);
2924 ret = LTTNG_ERR_NOMEM;
2925 goto error;
2926 }
2927 DBG2("Setting base path \"%s\" for session \"%s\"",
2928 session->base_path, session->name);
2929 }
2930 ret = LTTNG_OK;
2931 error:
2932 return ret;
2933 }
2934
2935 /*
2936 * Command LTTNG_SET_CONSUMER_URI processed by the client thread.
2937 */
2938 int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri,
2939 struct lttng_uri *uris)
2940 {
2941 int ret, i;
2942 struct ltt_kernel_session *ksess = session->kernel_session;
2943 struct ltt_ust_session *usess = session->ust_session;
2944
2945 assert(session);
2946 assert(uris);
2947 assert(nb_uri > 0);
2948
2949 /* Can't set consumer URI if the session is active. */
2950 if (session->active) {
2951 ret = LTTNG_ERR_TRACE_ALREADY_STARTED;
2952 goto error;
2953 }
2954
2955 /*
2956 * Set the session base path if any. This is done inside
2957 * cmd_set_consumer_uri to preserve backward compatibility of the
2958 * previous session creation api vs the session descriptor api.
2959 */
2960 ret = set_session_base_path_from_uris(session, nb_uri, uris);
2961 if (ret != LTTNG_OK) {
2962 goto error;
2963 }
2964
2965 /* Set the "global" consumer URIs */
2966 for (i = 0; i < nb_uri; i++) {
2967 ret = add_uri_to_consumer(session, session->consumer, &uris[i],
2968 LTTNG_DOMAIN_NONE);
2969 if (ret != LTTNG_OK) {
2970 goto error;
2971 }
2972 }
2973
2974 /* Set UST session URIs */
2975 if (session->ust_session) {
2976 for (i = 0; i < nb_uri; i++) {
2977 ret = add_uri_to_consumer(session,
2978 session->ust_session->consumer,
2979 &uris[i], LTTNG_DOMAIN_UST);
2980 if (ret != LTTNG_OK) {
2981 goto error;
2982 }
2983 }
2984 }
2985
2986 /* Set kernel session URIs */
2987 if (session->kernel_session) {
2988 for (i = 0; i < nb_uri; i++) {
2989 ret = add_uri_to_consumer(session,
2990 session->kernel_session->consumer,
2991 &uris[i], LTTNG_DOMAIN_KERNEL);
2992 if (ret != LTTNG_OK) {
2993 goto error;
2994 }
2995 }
2996 }
2997
2998 /*
2999 * Make sure to set the session in output mode after we set URI since a
3000 * session can be created without URL (thus flagged in no output mode).
3001 */
3002 session->output_traces = 1;
3003 if (ksess) {
3004 ksess->output_traces = 1;
3005 }
3006
3007 if (usess) {
3008 usess->output_traces = 1;
3009 }
3010
3011 /* All good! */
3012 ret = LTTNG_OK;
3013
3014 error:
3015 return ret;
3016 }
3017
3018 static
3019 enum lttng_error_code set_session_output_from_descriptor(
3020 struct ltt_session *session,
3021 const struct lttng_session_descriptor *descriptor)
3022 {
3023 int ret;
3024 enum lttng_error_code ret_code = LTTNG_OK;
3025 enum lttng_session_descriptor_type session_type =
3026 lttng_session_descriptor_get_type(descriptor);
3027 enum lttng_session_descriptor_output_type output_type =
3028 lttng_session_descriptor_get_output_type(descriptor);
3029 struct lttng_uri uris[2] = {};
3030 size_t uri_count = 0;
3031
3032 switch (output_type) {
3033 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE:
3034 goto end;
3035 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL:
3036 lttng_session_descriptor_get_local_output_uri(descriptor,
3037 &uris[0]);
3038 uri_count = 1;
3039 break;
3040 case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK:
3041 lttng_session_descriptor_get_network_output_uris(descriptor,
3042 &uris[0], &uris[1]);
3043 uri_count = 2;
3044 break;
3045 default:
3046 ret_code = LTTNG_ERR_INVALID;
3047 goto end;
3048 }
3049
3050 switch (session_type) {
3051 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3052 {
3053 struct snapshot_output *new_output = NULL;
3054
3055 new_output = snapshot_output_alloc();
3056 if (!new_output) {
3057 ret_code = LTTNG_ERR_NOMEM;
3058 goto end;
3059 }
3060
3061 ret = snapshot_output_init_with_uri(session,
3062 DEFAULT_SNAPSHOT_MAX_SIZE,
3063 NULL, uris, uri_count, session->consumer,
3064 new_output, &session->snapshot);
3065 if (ret < 0) {
3066 ret_code = (ret == -ENOMEM) ?
3067 LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID;
3068 snapshot_output_destroy(new_output);
3069 goto end;
3070 }
3071 snapshot_add_output(&session->snapshot, new_output);
3072 break;
3073 }
3074 case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR:
3075 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3076 {
3077 ret_code = cmd_set_consumer_uri(session, uri_count, uris);
3078 break;
3079 }
3080 default:
3081 ret_code = LTTNG_ERR_INVALID;
3082 goto end;
3083 }
3084 end:
3085 return ret_code;
3086 }
3087
3088 static
3089 enum lttng_error_code cmd_create_session_from_descriptor(
3090 struct lttng_session_descriptor *descriptor,
3091 const lttng_sock_cred *creds,
3092 const char *home_path)
3093 {
3094 int ret;
3095 enum lttng_error_code ret_code;
3096 const char *session_name;
3097 struct ltt_session *new_session = NULL;
3098 enum lttng_session_descriptor_status descriptor_status;
3099
3100 session_lock_list();
3101 if (home_path) {
3102 if (*home_path != '/') {
3103 ERR("Home path provided by client is not absolute");
3104 ret_code = LTTNG_ERR_INVALID;
3105 goto end;
3106 }
3107 }
3108
3109 descriptor_status = lttng_session_descriptor_get_session_name(
3110 descriptor, &session_name);
3111 switch (descriptor_status) {
3112 case LTTNG_SESSION_DESCRIPTOR_STATUS_OK:
3113 break;
3114 case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET:
3115 session_name = NULL;
3116 break;
3117 default:
3118 ret_code = LTTNG_ERR_INVALID;
3119 goto end;
3120 }
3121
3122 ret_code = session_create(session_name, creds->uid, creds->gid,
3123 &new_session);
3124 if (ret_code != LTTNG_OK) {
3125 goto end;
3126 }
3127
3128 if (!session_name) {
3129 ret = lttng_session_descriptor_set_session_name(descriptor,
3130 new_session->name);
3131 if (ret) {
3132 ret_code = LTTNG_ERR_SESSION_FAIL;
3133 goto end;
3134 }
3135 }
3136
3137 if (!lttng_session_descriptor_is_output_destination_initialized(
3138 descriptor)) {
3139 /*
3140 * Only include the session's creation time in the output
3141 * destination if the name of the session itself was
3142 * not auto-generated.
3143 */
3144 ret_code = lttng_session_descriptor_set_default_output(
3145 descriptor,
3146 session_name ? &new_session->creation_time : NULL,
3147 home_path);
3148 if (ret_code != LTTNG_OK) {
3149 goto end;
3150 }
3151 } else {
3152 new_session->has_user_specified_directory =
3153 lttng_session_descriptor_has_output_directory(
3154 descriptor);
3155 }
3156
3157 switch (lttng_session_descriptor_get_type(descriptor)) {
3158 case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT:
3159 new_session->snapshot_mode = 1;
3160 break;
3161 case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE:
3162 new_session->live_timer =
3163 lttng_session_descriptor_live_get_timer_interval(
3164 descriptor);
3165 break;
3166 default:
3167 break;
3168 }
3169
3170 ret_code = set_session_output_from_descriptor(new_session, descriptor);
3171 if (ret_code != LTTNG_OK) {
3172 goto end;
3173 }
3174 new_session->consumer->enabled = 1;
3175 ret_code = LTTNG_OK;
3176 end:
3177 /* Release reference provided by the session_create function. */
3178 session_put(new_session);
3179 if (ret_code != LTTNG_OK && new_session) {
3180 /* Release the global reference on error. */
3181 session_destroy(new_session);
3182 }
3183 session_unlock_list();
3184 return ret_code;
3185 }
3186
3187 enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock,
3188 struct lttng_session_descriptor **return_descriptor)
3189 {
3190 int ret;
3191 size_t payload_size;
3192 struct lttng_dynamic_buffer payload;
3193 struct lttng_buffer_view home_dir_view;
3194 struct lttng_buffer_view session_descriptor_view;
3195 struct lttng_session_descriptor *session_descriptor = NULL;
3196 enum lttng_error_code ret_code;
3197
3198 lttng_dynamic_buffer_init(&payload);
3199 if (cmd_ctx->lsm.u.create_session.home_dir_size >=
3200 LTTNG_PATH_MAX) {
3201 ret_code = LTTNG_ERR_INVALID;
3202 goto error;
3203 }
3204 if (cmd_ctx->lsm.u.create_session.session_descriptor_size >
3205 LTTNG_SESSION_DESCRIPTOR_MAX_LEN) {
3206 ret_code = LTTNG_ERR_INVALID;
3207 goto error;
3208 }
3209
3210 payload_size = cmd_ctx->lsm.u.create_session.home_dir_size +
3211 cmd_ctx->lsm.u.create_session.session_descriptor_size;
3212 ret = lttng_dynamic_buffer_set_size(&payload, payload_size);
3213 if (ret) {
3214 ret_code = LTTNG_ERR_NOMEM;
3215 goto error;
3216 }
3217
3218 ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size);
3219 if (ret <= 0) {
3220 ERR("Reception of session descriptor failed, aborting.");
3221 ret_code = LTTNG_ERR_SESSION_FAIL;
3222 goto error;
3223 }
3224
3225 home_dir_view = lttng_buffer_view_from_dynamic_buffer(
3226 &payload,
3227 0,
3228 cmd_ctx->lsm.u.create_session.home_dir_size);
3229 if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 &&
3230 !lttng_buffer_view_is_valid(&home_dir_view)) {
3231 ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory");
3232 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3233 goto error;
3234 }
3235
3236 session_descriptor_view = lttng_buffer_view_from_dynamic_buffer(
3237 &payload,
3238 cmd_ctx->lsm.u.create_session.home_dir_size,
3239 cmd_ctx->lsm.u.create_session.session_descriptor_size);
3240 if (!lttng_buffer_view_is_valid(&session_descriptor_view)) {
3241 ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor");
3242 ret_code = LTTNG_ERR_INVALID_PROTOCOL;
3243 goto error;
3244 }
3245
3246 ret = lttng_session_descriptor_create_from_buffer(
3247 &session_descriptor_view, &session_descriptor);
3248 if (ret < 0) {
3249 ERR("Failed to create session descriptor from payload of \"create session\" command");
3250 ret_code = LTTNG_ERR_INVALID;
3251 goto error;
3252 }
3253
3254 /*
3255 * Sets the descriptor's auto-generated properties (name, output) if
3256 * needed.
3257 */
3258 ret_code = cmd_create_session_from_descriptor(session_descriptor,
3259 &cmd_ctx->creds,
3260 home_dir_view.size ? home_dir_view.data : NULL);
3261 if (ret_code != LTTNG_OK) {
3262 goto error;
3263 }
3264
3265 ret_code = LTTNG_OK;
3266 *return_descriptor = session_descriptor;
3267 session_descriptor = NULL;
3268 error:
3269 lttng_dynamic_buffer_reset(&payload);
3270 lttng_session_descriptor_destroy(session_descriptor);
3271 return ret_code;
3272 }
3273
3274 static
3275 void cmd_destroy_session_reply(const struct ltt_session *session,
3276 void *_reply_context)
3277 {
3278 int ret;
3279 ssize_t comm_ret;
3280 const struct cmd_destroy_session_reply_context *reply_context =
3281 _reply_context;
3282 struct lttng_dynamic_buffer payload;
3283 struct lttcomm_session_destroy_command_header cmd_header;
3284 struct lttng_trace_archive_location *location = NULL;
3285 struct lttcomm_lttng_msg llm = {
3286 .cmd_type = LTTNG_DESTROY_SESSION,
3287 .ret_code = reply_context->destruction_status,
3288 .pid = UINT32_MAX,
3289 .cmd_header_size =
3290 sizeof(struct lttcomm_session_destroy_command_header),
3291 .data_size = 0,
3292 };
3293 size_t payload_size_before_location;
3294
3295 lttng_dynamic_buffer_init(&payload);
3296
3297 ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm));
3298 if (ret) {
3299 ERR("Failed to append session destruction message");
3300 goto error;
3301 }
3302
3303 cmd_header.rotation_state =
3304 (int32_t) (reply_context->implicit_rotation_on_destroy ?
3305 session->rotation_state :
3306 LTTNG_ROTATION_STATE_NO_ROTATION);
3307 ret = lttng_dynamic_buffer_append(&payload, &cmd_header,
3308 sizeof(cmd_header));
3309 if (ret) {
3310 ERR("Failed to append session destruction command header");
3311 goto error;
3312 }
3313
3314 if (!reply_context->implicit_rotation_on_destroy) {
3315 DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply",
3316 session->name);
3317 goto send_reply;
3318 }
3319 if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) {
3320 DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply",
3321 session->name);
3322 goto send_reply;
3323 }
3324
3325 location = session_get_trace_archive_location(session);
3326 if (!location) {
3327 ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"",
3328 session->name);
3329 goto error;
3330 }
3331
3332 payload_size_before_location = payload.size;
3333 comm_ret = lttng_trace_archive_location_serialize(location,
3334 &payload);
3335 lttng_trace_archive_location_put(location);
3336 if (comm_ret < 0) {
3337 ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"",
3338 session->name);
3339 goto error;
3340 }
3341 /* Update the message to indicate the location's length. */
3342 ((struct lttcomm_lttng_msg *) payload.data)->data_size =
3343 payload.size - payload_size_before_location;
3344 send_reply:
3345 comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd,
3346 payload.data, payload.size);
3347 if (comm_ret != (ssize_t) payload.size) {
3348 ERR("Failed to send result of the destruction of session \"%s\" to client",
3349 session->name);
3350 }
3351 error:
3352 ret = close(reply_context->reply_sock_fd);
3353 if (ret) {
3354 PERROR("Failed to close client socket in deferred session destroy reply");
3355 }
3356 lttng_dynamic_buffer_reset(&payload);
3357 free(_reply_context);
3358 }
3359
3360 /*
3361 * Command LTTNG_DESTROY_SESSION processed by the client thread.
3362 *
3363 * Called with session lock held.
3364 */
3365 int cmd_destroy_session(struct ltt_session *session,
3366 struct notification_thread_handle *notification_thread_handle,
3367 int *sock_fd)
3368 {
3369 int ret;
3370 enum lttng_error_code destruction_last_error = LTTNG_OK;
3371 struct cmd_destroy_session_reply_context *reply_context = NULL;
3372
3373 if (sock_fd) {
3374 reply_context = zmalloc(sizeof(*reply_context));
3375 if (!reply_context) {
3376 ret = LTTNG_ERR_NOMEM;
3377 goto end;
3378 }
3379 reply_context->reply_sock_fd = *sock_fd;
3380 }
3381
3382 /* Safety net */
3383 assert(session);
3384
3385 DBG("Begin destroy session %s (id %" PRIu64 ")", session->name,
3386 session->id);
3387 if (session->active) {
3388 DBG("Session \"%s\" is active, attempting to stop it before destroying it",
3389 session->name);
3390 ret = cmd_stop_trace(session);
3391 if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) {
3392 /* Carry on with the destruction of the session. */
3393 ERR("Failed to stop session \"%s\" as part of its destruction: %s",
3394 session->name, lttng_strerror(-ret));
3395 destruction_last_error = ret;
3396 }
3397 }
3398
3399 if (session->rotation_schedule_timer_enabled) {
3400 if (timer_session_rotation_schedule_timer_stop(
3401 session)) {
3402 ERR("Failed to stop the \"rotation schedule\" timer of session %s",
3403 session->name);
3404 destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR;
3405 }
3406 }
3407
3408 if (session->rotate_size) {
3409 unsubscribe_session_consumed_size_rotation(session, notification_thread_handle);
3410 session->rotate_size = 0;
3411 }
3412
3413 if (session->rotated && session->current_trace_chunk && session->output_traces) {
3414 /*
3415 * Perform a last rotation on destruction if rotations have
3416 * occurred during the session's lifetime.
3417 */
3418 ret = cmd_rotate_session(session, NULL, false,
3419 LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED);
3420 if (ret != LTTNG_OK) {
3421 ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s",
3422 session->name, lttng_strerror(-ret));
3423 destruction_last_error = -ret;
3424 }
3425 if (reply_context) {
3426 reply_context->implicit_rotation_on_destroy = true;
3427 }
3428 } else if (session->has_been_started && session->current_trace_chunk) {
3429 /*
3430 * The user has not triggered a session rotation. However, to
3431 * ensure all data has been consumed, the session is rotated
3432 * to a 'null' trace chunk before it is destroyed.
3433 *
3434 * This is a "quiet" rotation meaning that no notification is
3435 * emitted and no renaming of the current trace chunk takes
3436 * place.
3437 */
3438 ret = cmd_rotate_session(session, NULL, true,
3439 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION);
3440 /*
3441 * Rotation operations may not be supported by the kernel
3442 * tracer. Hence, do not consider this implicit rotation as
3443 * a session destruction error. The library has already stopped
3444 * the session and waited for pending data; there is nothing
3445 * left to do but complete the destruction of the session.
3446 */
3447 if (ret != LTTNG_OK &&
3448 ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) {
3449 ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s",
3450 session->name, lttng_strerror(ret));
3451 destruction_last_error = -ret;
3452 }
3453 }
3454
3455 if (session->shm_path[0]) {
3456 /*
3457 * When a session is created with an explicit shm_path,
3458 * the consumer daemon will create its shared memory files
3459 * at that location and will *not* unlink them. This is normal
3460 * as the intention of that feature is to make it possible
3461 * to retrieve the content of those files should a crash occur.
3462 *
3463 * To ensure the content of those files can be used, the
3464 * sessiond daemon will replicate the content of the metadata
3465 * cache in a metadata file.
3466 *
3467 * On clean-up, it is expected that the consumer daemon will
3468 * unlink the shared memory files and that the session daemon
3469 * will unlink the metadata file. Then, the session's directory
3470 * in the shm path can be removed.
3471 *
3472 * Unfortunately, a flaw in the design of the sessiond's and
3473 * consumerd's tear down of channels makes it impossible to
3474 * determine when the sessiond _and_ the consumerd have both
3475 * destroyed their representation of a channel. For one, the
3476 * unlinking, close, and rmdir happen in deferred 'call_rcu'
3477 * callbacks in both daemons.
3478 *
3479 * However, it is also impossible for the sessiond to know when
3480 * the consumer daemon is done destroying its channel(s) since
3481 * it occurs as a reaction to the closing of the channel's file
3482 * descriptor. There is no resulting communication initiated
3483 * from the consumerd to the sessiond to confirm that the
3484 * operation is completed (and was successful).
3485 *
3486 * Until this is all fixed, the session daemon checks for the
3487 * removal of the session's shm path which makes it possible
3488 * to safely advertise a session as having been destroyed.
3489 *
3490 * Prior to this fix, it was not possible to reliably save
3491 * a session making use of the --shm-path option, destroy it,
3492 * and load it again. This is because the creation of the
3493 * session would fail upon seeing the session's shm path
3494 * already in existence.
3495 *
3496 * Note that none of the error paths in the check for the
3497 * directory's existence return an error. This is normal
3498 * as there isn't much that can be done. The session will
3499 * be destroyed properly, except that we can't offer the
3500 * guarantee that the same session can be re-created.
3501 */
3502 current_completion_handler = &destroy_completion_handler.handler;
3503 ret = lttng_strncpy(destroy_completion_handler.shm_path,
3504 session->shm_path,
3505 sizeof(destroy_completion_handler.shm_path));
3506 assert(!ret);
3507 }
3508
3509 /*
3510 * The session is destroyed. However, note that the command context
3511 * still holds a reference to the session, thus delaying its destruction
3512 * _at least_ up to the point when that reference is released.
3513 */
3514 session_destroy(session);
3515 if (reply_context) {
3516 reply_context->destruction_status = destruction_last_error;
3517 ret = session_add_destroy_notifier(session,
3518 cmd_destroy_session_reply,
3519 (void *) reply_context);
3520 if (ret) {
3521 ret = LTTNG_ERR_FATAL;
3522 goto end;
3523 } else {
3524 *sock_fd = -1;
3525 }
3526 }
3527 ret = LTTNG_OK;
3528 end:
3529 return ret;
3530 }
3531
3532 /*
3533 * Command LTTNG_REGISTER_CONSUMER processed by the client thread.
3534 */
3535 int cmd_register_consumer(struct ltt_session *session,
3536 enum lttng_domain_type domain, const char *sock_path,
3537 struct consumer_data *cdata)
3538 {
3539 int ret, sock;
3540 struct consumer_socket *socket = NULL;
3541
3542 assert(session);
3543 assert(cdata);
3544 assert(sock_path);
3545
3546 switch (domain) {
3547 case LTTNG_DOMAIN_KERNEL:
3548 {
3549 struct ltt_kernel_session *ksess = session->kernel_session;
3550
3551 assert(ksess);
3552
3553 /* Can't register a consumer if there is already one */
3554 if (ksess->consumer_fds_sent != 0) {
3555 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
3556 goto error;
3557 }
3558
3559 sock = lttcomm_connect_unix_sock(sock_path);
3560 if (sock < 0) {
3561 ret = LTTNG_ERR_CONNECT_FAIL;
3562 goto error;
3563 }
3564 cdata->cmd_sock = sock;
3565
3566 socket = consumer_allocate_socket(&cdata->cmd_sock);
3567 if (socket == NULL) {
3568 ret = close(sock);
3569 if (ret < 0) {
3570 PERROR("close register consumer");
3571 }
3572 cdata->cmd_sock = -1;
3573 ret = LTTNG_ERR_FATAL;
3574 goto error;
3575 }
3576
3577 socket->lock = zmalloc(sizeof(pthread_mutex_t));
3578 if (socket->lock == NULL) {
3579 PERROR("zmalloc pthread mutex");
3580 ret = LTTNG_ERR_FATAL;
3581 goto error;
3582 }
3583 pthread_mutex_init(socket->lock, NULL);
3584 socket->registered = 1;
3585
3586 rcu_read_lock();
3587 consumer_add_socket(socket, ksess->consumer);
3588 rcu_read_unlock();
3589
3590 pthread_mutex_lock(&cdata->pid_mutex);
3591 cdata->pid = -1;
3592 pthread_mutex_unlock(&cdata->pid_mutex);
3593
3594 break;
3595 }
3596 default:
3597 /* TODO: Userspace tracing */
3598 ret = LTTNG_ERR_UND;
3599 goto error;
3600 }
3601
3602 return LTTNG_OK;
3603
3604 error:
3605 if (socket) {
3606 consumer_destroy_socket(socket);
3607 }
3608 return ret;
3609 }
3610
3611 /*
3612 * Command LTTNG_LIST_DOMAINS processed by the client thread.
3613 */
3614 ssize_t cmd_list_domains(struct ltt_session *session,
3615 struct lttng_domain **domains)
3616 {
3617 int ret, index = 0;
3618 ssize_t nb_dom = 0;
3619 struct agent *agt;
3620 struct lttng_ht_iter iter;
3621
3622 if (session->kernel_session != NULL) {
3623 DBG3("Listing domains found kernel domain");
3624 nb_dom++;
3625 }
3626
3627 if (session->ust_session != NULL) {
3628 DBG3("Listing domains found UST global domain");
3629 nb_dom++;
3630
3631 rcu_read_lock();
3632 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3633 agt, node.node) {
3634 if (agt->being_used) {
3635 nb_dom++;
3636 }
3637 }
3638 rcu_read_unlock();
3639 }
3640
3641 if (!nb_dom) {
3642 goto end;
3643 }
3644
3645 *domains = zmalloc(nb_dom * sizeof(struct lttng_domain));
3646 if (*domains == NULL) {
3647 ret = LTTNG_ERR_FATAL;
3648 goto error;
3649 }
3650
3651 if (session->kernel_session != NULL) {
3652 (*domains)[index].type = LTTNG_DOMAIN_KERNEL;
3653
3654 /* Kernel session buffer type is always GLOBAL */
3655 (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL;
3656
3657 index++;
3658 }
3659
3660 if (session->ust_session != NULL) {
3661 (*domains)[index].type = LTTNG_DOMAIN_UST;
3662 (*domains)[index].buf_type = session->ust_session->buffer_type;
3663 index++;
3664
3665 rcu_read_lock();
3666 cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter,
3667 agt, node.node) {
3668 if (agt->being_used) {
3669 (*domains)[index].type = agt->domain;
3670 (*domains)[index].buf_type = session->ust_session->buffer_type;
3671 index++;
3672 }
3673 }
3674 rcu_read_unlock();
3675 }
3676 end:
3677 return nb_dom;
3678
3679 error:
3680 /* Return negative value to differentiate return code */
3681 return -ret;
3682 }
3683
3684
3685 /*
3686 * Command LTTNG_LIST_CHANNELS processed by the client thread.
3687 */
3688 enum lttng_error_code cmd_list_channels(enum lttng_domain_type domain,
3689 struct ltt_session *session,
3690 struct lttng_payload *payload)
3691 {
3692 int ret = 0;
3693 unsigned int i = 0;
3694 struct lttcomm_list_command_header cmd_header = {};
3695 size_t cmd_header_offset;
3696 enum lttng_error_code ret_code;
3697
3698 assert(session);
3699 assert(payload);
3700
3701 DBG("Listing channels for session %s", session->name);
3702
3703 cmd_header_offset = payload->buffer.size;
3704
3705 /* Reserve space for command reply header. */
3706 ret = lttng_dynamic_buffer_set_size(&payload->buffer,
3707 cmd_header_offset + sizeof(cmd_header));
3708 if (ret) {
3709 ret_code = LTTNG_ERR_NOMEM;
3710 goto end;
3711 }
3712
3713 switch (domain) {
3714 case LTTNG_DOMAIN_KERNEL:
3715 {
3716 /* Kernel channels */
3717 struct ltt_kernel_channel *kchan;
3718 if (session->kernel_session != NULL) {
3719 cds_list_for_each_entry(kchan,
3720 &session->kernel_session->channel_list.head, list) {
3721 uint64_t discarded_events, lost_packets;
3722 struct lttng_channel_extended *extended;
3723
3724 extended = (struct lttng_channel_extended *)
3725 kchan->channel->attr.extended.ptr;
3726
3727 ret = get_kernel_runtime_stats(session, kchan,
3728 &discarded_events, &lost_packets);
3729 if (ret < 0) {
3730 ret_code = LTTNG_ERR_UNK;
3731 goto end;
3732 }
3733
3734 /*
3735 * Update the discarded_events and lost_packets
3736 * count for the channel
3737 */
3738 extended->discarded_events = discarded_events;
3739 extended->lost_packets = lost_packets;
3740
3741 ret = lttng_channel_serialize(
3742 kchan->channel, &payload->buffer);
3743 if (ret) {
3744 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3745 kchan->channel->name);
3746 ret_code = LTTNG_ERR_UNK;
3747 goto end;
3748 }
3749
3750 i++;
3751 }
3752 }
3753 break;
3754 }
3755 case LTTNG_DOMAIN_UST:
3756 {
3757 struct lttng_ht_iter iter;
3758 struct ltt_ust_channel *uchan;
3759
3760 rcu_read_lock();
3761 cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht,
3762 &iter.iter, uchan, node.node) {
3763 uint64_t discarded_events = 0, lost_packets = 0;
3764 struct lttng_channel *channel = NULL;
3765 struct lttng_channel_extended *extended;
3766
3767 channel = trace_ust_channel_to_lttng_channel(uchan);
3768 if (!channel) {
3769 ret = LTTNG_ERR_NOMEM;
3770 break;
3771 }
3772
3773 extended = (struct lttng_channel_extended *)
3774 channel->attr.extended.ptr;
3775
3776 ret = get_ust_runtime_stats(session, uchan,
3777 &discarded_events, &lost_packets);
3778 if (ret < 0) {
3779 lttng_channel_destroy(channel);
3780 ret_code = LTTNG_ERR_UNK;
3781 break;
3782 }
3783
3784 extended->discarded_events = discarded_events;
3785 extended->lost_packets = lost_packets;
3786
3787 ret = lttng_channel_serialize(
3788 channel, &payload->buffer);
3789 if (ret) {
3790 ERR("Failed to serialize lttng_channel: channel name = '%s'",
3791 channel->name);
3792 ret_code = LTTNG_ERR_UNK;
3793 ret = -1;
3794 break;
3795 }
3796
3797 i++;
3798 }
3799 rcu_read_unlock();
3800 break;
3801 }
3802 default:
3803 break;
3804 }
3805
3806 if (i > UINT32_MAX) {
3807 ERR("Channel count would overflow the channel listing command's reply");
3808 ret_code = LTTNG_ERR_OVERFLOW;
3809 goto end;
3810 }
3811
3812 /* Update command reply header. */
3813 cmd_header.count = (uint32_t) i;
3814 memcpy(payload->buffer.data + cmd_header_offset, &cmd_header,
3815 sizeof(cmd_header));
3816 ret_code = LTTNG_OK;
3817
3818 end:
3819 return ret_code;
3820 }
3821
3822 /*
3823 * Command LTTNG_LIST_EVENTS processed by the client thread.
3824 */
3825 enum lttng_error_code cmd_list_events(enum lttng_domain_type domain,
3826 struct ltt_session *session,
3827 char *channel_name,
3828 struct lttng_payload *reply_payload)
3829 {
3830 int buffer_resize_ret;
3831 enum lttng_error_code ret_code = LTTNG_OK;
3832 struct lttcomm_list_command_header reply_command_header = {};
3833 size_t reply_command_header_offset;
3834 unsigned int nb_events = 0;
3835
3836 assert(reply_payload);
3837
3838 /* Reserve space for command reply header. */
3839 reply_command_header_offset = reply_payload->buffer.size;
3840 buffer_resize_ret = lttng_dynamic_buffer_set_size(&reply_payload->buffer,
3841 reply_command_header_offset +
3842 sizeof(struct lttcomm_list_command_header));
3843 if (buffer_resize_ret) {
3844 ret_code = LTTNG_ERR_NOMEM;
3845 goto end;
3846 }
3847
3848 switch (domain) {
3849 case LTTNG_DOMAIN_KERNEL:
3850 if (session->kernel_session != NULL) {
3851 ret_code = list_lttng_kernel_events(channel_name,
3852 session->kernel_session, reply_payload, &nb_events);
3853 }
3854
3855 break;
3856 case LTTNG_DOMAIN_UST:
3857 {
3858 if (session->ust_session != NULL) {
3859 ret_code = list_lttng_ust_global_events(channel_name,
3860 &session->ust_session->domain_global,
3861 reply_payload, &nb_events);
3862 }
3863
3864 break;
3865 }
3866 case LTTNG_DOMAIN_LOG4J:
3867 case LTTNG_DOMAIN_JUL:
3868 case LTTNG_DOMAIN_PYTHON:
3869 if (session->ust_session) {
3870 struct lttng_ht_iter iter;
3871 struct agent *agt;
3872
3873 rcu_read_lock();
3874 cds_lfht_for_each_entry(session->ust_session->agents->ht,
3875 &iter.iter, agt, node.node) {
3876 if (agt->domain == domain) {
3877 ret_code = list_lttng_agent_events(
3878 agt, reply_payload, &nb_events);
3879 break;
3880 }
3881 }
3882
3883 rcu_read_unlock();
3884 }
3885 break;
3886 default:
3887 ret_code = LTTNG_ERR_UND;
3888 break;
3889 }
3890
3891 if (nb_events > UINT32_MAX) {
3892 ret_code = LTTNG_ERR_OVERFLOW;
3893 goto end;
3894 }
3895
3896 /* Update command reply header. */
3897 reply_command_header.count = (uint32_t) nb_events;
3898 memcpy(reply_payload->buffer.data + reply_command_header_offset, &reply_command_header,
3899 sizeof(reply_command_header));
3900
3901 end:
3902 return ret_code;
3903 }
3904
3905 /*
3906 * Using the session list, filled a lttng_session array to send back to the
3907 * client for session listing.
3908 *
3909 * The session list lock MUST be acquired before calling this function. Use
3910 * session_lock_list() and session_unlock_list().
3911 */
3912 void cmd_list_lttng_sessions(struct lttng_session *sessions,
3913 size_t session_count, uid_t uid, gid_t gid)
3914 {
3915 int ret;
3916 unsigned int i = 0;
3917 struct ltt_session *session;
3918 struct ltt_session_list *list = session_get_list();
3919 struct lttng_session_extended *extended =
3920 (typeof(extended)) (&sessions[session_count]);
3921
3922 DBG("Getting all available session for UID %d GID %d",
3923 uid, gid);
3924 /*
3925 * Iterate over session list and append data after the control struct in
3926 * the buffer.
3927 */
3928 cds_list_for_each_entry(session, &list->head, list) {
3929 if (!session_get(session)) {
3930 continue;
3931 }
3932 /*
3933 * Only list the sessions the user can control.
3934 */
3935 if (!session_access_ok(session, uid) ||
3936 session->destroyed) {
3937 session_put(session);
3938 continue;
3939 }
3940
3941 struct ltt_kernel_session *ksess = session->kernel_session;
3942 struct ltt_ust_session *usess = session->ust_session;
3943
3944 if (session->consumer->type == CONSUMER_DST_NET ||
3945 (ksess && ksess->consumer->type == CONSUMER_DST_NET) ||
3946 (usess && usess->consumer->type == CONSUMER_DST_NET)) {
3947 ret = build_network_session_path(sessions[i].path,
3948 sizeof(sessions[i].path), session);
3949 } else {
3950 ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s",
3951 session->consumer->dst.session_root_path);
3952 }
3953 if (ret < 0) {
3954 PERROR("snprintf session path");
3955 session_put(session);
3956 continue;
3957 }
3958
3959 strncpy(sessions[i].name, session->name, NAME_MAX);
3960 sessions[i].name[NAME_MAX - 1] = '\0';
3961 sessions[i].enabled = session->active;
3962 sessions[i].snapshot_mode = session->snapshot_mode;
3963 sessions[i].live_timer_interval = session->live_timer;
3964 extended[i].creation_time.value = (uint64_t) session->creation_time;
3965 extended[i].creation_time.is_set = 1;
3966 i++;
3967 session_put(session);
3968 }
3969 }
3970
3971 /*
3972 * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning
3973 * ready for trace analysis (or any kind of reader) or else 1 for pending data.
3974 */
3975 int cmd_data_pending(struct ltt_session *session)
3976 {
3977 int ret;
3978 struct ltt_kernel_session *ksess = session->kernel_session;
3979 struct ltt_ust_session *usess = session->ust_session;
3980
3981 assert(session);
3982
3983 DBG("Data pending for session %s", session->name);
3984
3985 /* Session MUST be stopped to ask for data availability. */
3986 if (session->active) {
3987 ret = LTTNG_ERR_SESSION_STARTED;
3988 goto error;
3989 } else {
3990 /*
3991 * If stopped, just make sure we've started before else the above call
3992 * will always send that there is data pending.
3993 *
3994 * The consumer assumes that when the data pending command is received,
3995 * the trace has been started before or else no output data is written
3996 * by the streams which is a condition for data pending. So, this is
3997 * *VERY* important that we don't ask the consumer before a start
3998 * trace.
3999 */
4000 if (!session->has_been_started) {
4001 ret = 0;
4002 goto error;
4003 }
4004 }
4005
4006 /* A rotation is still pending, we have to wait. */
4007 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
4008 DBG("Rotate still pending for session %s", session->name);
4009 ret = 1;
4010 goto error;
4011 }
4012
4013 if (ksess && ksess->consumer) {
4014 ret = consumer_is_data_pending(ksess->id, ksess->consumer);
4015 if (ret == 1) {
4016 /* Data is still being extracted for the kernel. */
4017 goto error;
4018 }
4019 }
4020
4021 if (usess && usess->consumer) {
4022 ret = consumer_is_data_pending(usess->id, usess->consumer);
4023 if (ret == 1) {
4024 /* Data is still being extracted for the kernel. */
4025 goto error;
4026 }
4027 }
4028
4029 /* Data is ready to be read by a viewer */
4030 ret = 0;
4031
4032 error:
4033 return ret;
4034 }
4035
4036 /*
4037 * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library.
4038 *
4039 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4040 */
4041 int cmd_snapshot_add_output(struct ltt_session *session,
4042 const struct lttng_snapshot_output *output, uint32_t *id)
4043 {
4044 int ret;
4045 struct snapshot_output *new_output;
4046
4047 assert(session);
4048 assert(output);
4049
4050 DBG("Cmd snapshot add output for session %s", session->name);
4051
4052 /*
4053 * Can't create an output if the session is not set in no-output mode.
4054 */
4055 if (session->output_traces) {
4056 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4057 goto error;
4058 }
4059
4060 if (session->has_non_mmap_channel) {
4061 ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED;
4062 goto error;
4063 }
4064
4065 /* Only one output is allowed until we have the "tee" feature. */
4066 if (session->snapshot.nb_output == 1) {
4067 ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST;
4068 goto error;
4069 }
4070
4071 new_output = snapshot_output_alloc();
4072 if (!new_output) {
4073 ret = LTTNG_ERR_NOMEM;
4074 goto error;
4075 }
4076
4077 ret = snapshot_output_init(session, output->max_size, output->name,
4078 output->ctrl_url, output->data_url, session->consumer, new_output,
4079 &session->snapshot);
4080 if (ret < 0) {
4081 if (ret == -ENOMEM) {
4082 ret = LTTNG_ERR_NOMEM;
4083 } else {
4084 ret = LTTNG_ERR_INVALID;
4085 }
4086 goto free_error;
4087 }
4088
4089 rcu_read_lock();
4090 snapshot_add_output(&session->snapshot, new_output);
4091 if (id) {
4092 *id = new_output->id;
4093 }
4094 rcu_read_unlock();
4095
4096 return LTTNG_OK;
4097
4098 free_error:
4099 snapshot_output_destroy(new_output);
4100 error:
4101 return ret;
4102 }
4103
4104 /*
4105 * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl.
4106 *
4107 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4108 */
4109 int cmd_snapshot_del_output(struct ltt_session *session,
4110 const struct lttng_snapshot_output *output)
4111 {
4112 int ret;
4113 struct snapshot_output *sout = NULL;
4114
4115 assert(session);
4116 assert(output);
4117
4118 rcu_read_lock();
4119
4120 /*
4121 * Permission denied to create an output if the session is not
4122 * set in no output mode.
4123 */
4124 if (session->output_traces) {
4125 ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4126 goto error;
4127 }
4128
4129 if (output->id) {
4130 DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id,
4131 session->name);
4132 sout = snapshot_find_output_by_id(output->id, &session->snapshot);
4133 } else if (*output->name != '\0') {
4134 DBG("Cmd snapshot del output name %s for session %s", output->name,
4135 session->name);
4136 sout = snapshot_find_output_by_name(output->name, &session->snapshot);
4137 }
4138 if (!sout) {
4139 ret = LTTNG_ERR_INVALID;
4140 goto error;
4141 }
4142
4143 snapshot_delete_output(&session->snapshot, sout);
4144 snapshot_output_destroy(sout);
4145 ret = LTTNG_OK;
4146
4147 error:
4148 rcu_read_unlock();
4149 return ret;
4150 }
4151
4152 /*
4153 * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl.
4154 *
4155 * If no output is available, outputs is untouched and 0 is returned.
4156 *
4157 * Return the size of the newly allocated outputs or a negative LTTNG_ERR code.
4158 */
4159 ssize_t cmd_snapshot_list_outputs(struct ltt_session *session,
4160 struct lttng_snapshot_output **outputs)
4161 {
4162 int ret, idx = 0;
4163 struct lttng_snapshot_output *list = NULL;
4164 struct lttng_ht_iter iter;
4165 struct snapshot_output *output;
4166
4167 assert(session);
4168 assert(outputs);
4169
4170 DBG("Cmd snapshot list outputs for session %s", session->name);
4171
4172 /*
4173 * Permission denied to create an output if the session is not
4174 * set in no output mode.
4175 */
4176 if (session->output_traces) {
4177 ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION;
4178 goto end;
4179 }
4180
4181 if (session->snapshot.nb_output == 0) {
4182 ret = 0;
4183 goto end;
4184 }
4185
4186 list = zmalloc(session->snapshot.nb_output * sizeof(*list));
4187 if (!list) {
4188 ret = -LTTNG_ERR_NOMEM;
4189 goto end;
4190 }
4191
4192 /* Copy list from session to the new list object. */
4193 rcu_read_lock();
4194 cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter,
4195 output, node.node) {
4196 assert(output->consumer);
4197 list[idx].id = output->id;
4198 list[idx].max_size = output->max_size;
4199 if (lttng_strncpy(list[idx].name, output->name,
4200 sizeof(list[idx].name))) {
4201 ret = -LTTNG_ERR_INVALID;
4202 goto error;
4203 }
4204 if (output->consumer->type == CONSUMER_DST_LOCAL) {
4205 if (lttng_strncpy(list[idx].ctrl_url,
4206 output->consumer->dst.session_root_path,
4207 sizeof(list[idx].ctrl_url))) {
4208 ret = -LTTNG_ERR_INVALID;
4209 goto error;
4210 }
4211 } else {
4212 /* Control URI. */
4213 ret = uri_to_str_url(&output->consumer->dst.net.control,
4214 list[idx].ctrl_url, sizeof(list[idx].ctrl_url));
4215 if (ret < 0) {
4216 ret = -LTTNG_ERR_NOMEM;
4217 goto error;
4218 }
4219
4220 /* Data URI. */
4221 ret = uri_to_str_url(&output->consumer->dst.net.data,
4222 list[idx].data_url, sizeof(list[idx].data_url));
4223 if (ret < 0) {
4224 ret = -LTTNG_ERR_NOMEM;
4225 goto error;
4226 }
4227 }
4228 idx++;
4229 }
4230
4231 *outputs = list;
4232 list = NULL;
4233 ret = session->snapshot.nb_output;
4234 error:
4235 rcu_read_unlock();
4236 free(list);
4237 end:
4238 return ret;
4239 }
4240
4241 /*
4242 * Check if we can regenerate the metadata for this session.
4243 * Only kernel, UST per-uid and non-live sessions are supported.
4244 *
4245 * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise.
4246 */
4247 static
4248 int check_regenerate_metadata_support(struct ltt_session *session)
4249 {
4250 int ret;
4251
4252 assert(session);
4253
4254 if (session->live_timer != 0) {
4255 ret = LTTNG_ERR_LIVE_SESSION;
4256 goto end;
4257 }
4258 if (!session->active) {
4259 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4260 goto end;
4261 }
4262 if (session->ust_session) {
4263 switch (session->ust_session->buffer_type) {
4264 case LTTNG_BUFFER_PER_UID:
4265 break;
4266 case LTTNG_BUFFER_PER_PID:
4267 ret = LTTNG_ERR_PER_PID_SESSION;
4268 goto end;
4269 default:
4270 assert(0);
4271 ret = LTTNG_ERR_UNK;
4272 goto end;
4273 }
4274 }
4275 if (session->consumer->type == CONSUMER_DST_NET &&
4276 session->consumer->relay_minor_version < 8) {
4277 ret = LTTNG_ERR_RELAYD_VERSION_FAIL;
4278 goto end;
4279 }
4280 ret = 0;
4281
4282 end:
4283 return ret;
4284 }
4285
4286 static
4287 int clear_metadata_file(int fd)
4288 {
4289 int ret;
4290 off_t lseek_ret;
4291
4292 lseek_ret = lseek(fd, 0, SEEK_SET);
4293 if (lseek_ret < 0) {
4294 PERROR("lseek");
4295 ret = -1;
4296 goto end;
4297 }
4298
4299 ret = ftruncate(fd, 0);
4300 if (ret < 0) {
4301 PERROR("ftruncate");
4302 goto end;
4303 }
4304
4305 end:
4306 return ret;
4307 }
4308
4309 static
4310 int ust_regenerate_metadata(struct ltt_ust_session *usess)
4311 {
4312 int ret = 0;
4313 struct buffer_reg_uid *uid_reg = NULL;
4314 struct buffer_reg_session *session_reg = NULL;
4315
4316 rcu_read_lock();
4317 cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) {
4318 struct ust_registry_session *registry;
4319 struct ust_registry_channel *chan;
4320 struct lttng_ht_iter iter_chan;
4321
4322 session_reg = uid_reg->registry;
4323 registry = session_reg->reg.ust;
4324
4325 pthread_mutex_lock(&registry->lock);
4326 registry->metadata_len_sent = 0;
4327 memset(registry->metadata, 0, registry->metadata_alloc_len);
4328 registry->metadata_len = 0;
4329 registry->metadata_version++;
4330 if (registry->metadata_fd > 0) {
4331 /* Clear the metadata file's content. */
4332 ret = clear_metadata_file(registry->metadata_fd);
4333 if (ret) {
4334 pthread_mutex_unlock(&registry->lock);
4335 goto end;
4336 }
4337 }
4338
4339 ret = ust_metadata_session_statedump(registry, NULL,
4340 registry->major, registry->minor);
4341 if (ret) {
4342 pthread_mutex_unlock(&registry->lock);
4343 ERR("Failed to generate session metadata (err = %d)",
4344 ret);
4345 goto end;
4346 }
4347 cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter,
4348 chan, node.node) {
4349 struct ust_registry_event *event;
4350 struct lttng_ht_iter iter_event;
4351
4352 ret = ust_metadata_channel_statedump(registry, chan);
4353 if (ret) {
4354 pthread_mutex_unlock(&registry->lock);
4355 ERR("Failed to generate channel metadata "
4356 "(err = %d)", ret);
4357 goto end;
4358 }
4359 cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter,
4360 event, node.node) {
4361 ret = ust_metadata_event_statedump(registry,
4362 chan, event);
4363 if (ret) {
4364 pthread_mutex_unlock(&registry->lock);
4365 ERR("Failed to generate event metadata "
4366 "(err = %d)", ret);
4367 goto end;
4368 }
4369 }
4370 }
4371 pthread_mutex_unlock(&registry->lock);
4372 }
4373
4374 end:
4375 rcu_read_unlock();
4376 return ret;
4377 }
4378
4379 /*
4380 * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library.
4381 *
4382 * Ask the consumer to truncate the existing metadata file(s) and
4383 * then regenerate the metadata. Live and per-pid sessions are not
4384 * supported and return an error.
4385 *
4386 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4387 */
4388 int cmd_regenerate_metadata(struct ltt_session *session)
4389 {
4390 int ret;
4391
4392 assert(session);
4393
4394 ret = check_regenerate_metadata_support(session);
4395 if (ret) {
4396 goto end;
4397 }
4398
4399 if (session->kernel_session) {
4400 ret = kernctl_session_regenerate_metadata(
4401 session->kernel_session->fd);
4402 if (ret < 0) {
4403 ERR("Failed to regenerate the kernel metadata");
4404 goto end;
4405 }
4406 }
4407
4408 if (session->ust_session) {
4409 ret = ust_regenerate_metadata(session->ust_session);
4410 if (ret < 0) {
4411 ERR("Failed to regenerate the UST metadata");
4412 goto end;
4413 }
4414 }
4415 DBG("Cmd metadata regenerate for session %s", session->name);
4416 ret = LTTNG_OK;
4417
4418 end:
4419 return ret;
4420 }
4421
4422 /*
4423 * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library.
4424 *
4425 * Ask the tracer to regenerate a new statedump.
4426 *
4427 * Return LTTNG_OK on success or else a LTTNG_ERR code.
4428 */
4429 int cmd_regenerate_statedump(struct ltt_session *session)
4430 {
4431 int ret;
4432
4433 assert(session);
4434
4435 if (!session->active) {
4436 ret = LTTNG_ERR_SESSION_NOT_STARTED;
4437 goto end;
4438 }
4439
4440 if (session->kernel_session) {
4441 ret = kernctl_session_regenerate_statedump(
4442 session->kernel_session->fd);
4443 /*
4444 * Currently, the statedump in kernel can only fail if out
4445 * of memory.
4446 */
4447 if (ret < 0) {
4448 if (ret == -ENOMEM) {
4449 ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM;
4450 } else {
4451 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4452 }
4453 ERR("Failed to regenerate the kernel statedump");
4454 goto end;
4455 }
4456 }
4457
4458 if (session->ust_session) {
4459 ret = ust_app_regenerate_statedump_all(session->ust_session);
4460 /*
4461 * Currently, the statedump in UST always returns 0.
4462 */
4463 if (ret < 0) {
4464 ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL;
4465 ERR("Failed to regenerate the UST statedump");
4466 goto end;
4467 }
4468 }
4469 DBG("Cmd regenerate statedump for session %s", session->name);
4470 ret = LTTNG_OK;
4471
4472 end:
4473 return ret;
4474 }
4475
4476 static
4477 enum lttng_error_code synchronize_tracer_notifier_register(
4478 struct notification_thread_handle *notification_thread,
4479 struct lttng_trigger *trigger, const struct lttng_credentials *cmd_creds)
4480 {
4481 enum lttng_error_code ret_code;
4482 const struct lttng_condition *condition =
4483 lttng_trigger_get_const_condition(trigger);
4484 const char *trigger_name;
4485 uid_t trigger_owner;
4486 enum lttng_trigger_status trigger_status;
4487 const enum lttng_domain_type trigger_domain =
4488 lttng_trigger_get_underlying_domain_type_restriction(
4489 trigger);
4490
4491 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4492 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4493
4494 assert(condition);
4495 assert(lttng_condition_get_type(condition) ==
4496 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4497
4498 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4499 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4500 trigger_name : "(anonymous)";
4501
4502 session_lock_list();
4503 switch (trigger_domain) {
4504 case LTTNG_DOMAIN_KERNEL:
4505 {
4506 ret_code = kernel_register_event_notifier(trigger, cmd_creds);
4507 if (ret_code != LTTNG_OK) {
4508 enum lttng_error_code notif_thread_unregister_ret;
4509
4510 notif_thread_unregister_ret =
4511 notification_thread_command_unregister_trigger(
4512 notification_thread, trigger);
4513
4514 if (notif_thread_unregister_ret != LTTNG_OK) {
4515 /* Return the original error code. */
4516 ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d",
4517 trigger_name,
4518 (int) trigger_owner,
4519 ret_code);
4520 }
4521 }
4522 break;
4523 }
4524 case LTTNG_DOMAIN_UST:
4525 ust_app_global_update_all_event_notifier_rules();
4526 break;
4527 case LTTNG_DOMAIN_JUL:
4528 case LTTNG_DOMAIN_LOG4J:
4529 case LTTNG_DOMAIN_PYTHON:
4530 {
4531 /* Agent domains. */
4532 struct agent *agt = agent_find_by_event_notifier_domain(
4533 trigger_domain);
4534
4535 if (!agt) {
4536 agt = agent_create(trigger_domain);
4537 if (!agt) {
4538 ret_code = LTTNG_ERR_NOMEM;
4539 goto end_unlock_session_list;
4540 }
4541
4542 agent_add(agt, the_trigger_agents_ht_by_domain);
4543 }
4544
4545 ret_code = trigger_agent_enable(trigger, agt);
4546 if (ret_code != LTTNG_OK) {
4547 goto end_unlock_session_list;
4548 }
4549
4550 break;
4551 }
4552 case LTTNG_DOMAIN_NONE:
4553 default:
4554 abort();
4555 }
4556
4557 ret_code = LTTNG_OK;
4558 end_unlock_session_list:
4559 session_unlock_list();
4560 return ret_code;
4561 }
4562
4563 enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds,
4564 struct lttng_trigger *trigger,
4565 bool is_trigger_anonymous,
4566 struct notification_thread_handle *notification_thread,
4567 struct lttng_trigger **return_trigger)
4568 {
4569 enum lttng_error_code ret_code;
4570 const char *trigger_name;
4571 uid_t trigger_owner;
4572 enum lttng_trigger_status trigger_status;
4573
4574 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4575 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4576 trigger_name : "(anonymous)";
4577
4578 trigger_status = lttng_trigger_get_owner_uid(
4579 trigger, &trigger_owner);
4580 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4581
4582 DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4583 trigger_name, (int) trigger_owner,
4584 (int) lttng_credentials_get_uid(cmd_creds));
4585
4586 /*
4587 * Validate the trigger credentials against the command credentials.
4588 * Only the root user can register a trigger with non-matching
4589 * credentials.
4590 */
4591 if (!lttng_credentials_is_equal_uid(
4592 lttng_trigger_get_credentials(trigger),
4593 cmd_creds)) {
4594 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4595 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4596 trigger_name, (int) trigger_owner,
4597 (int) lttng_credentials_get_uid(cmd_creds));
4598 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4599 goto end;
4600 }
4601 }
4602
4603 /*
4604 * The bytecode generation also serves as a validation step for the
4605 * bytecode expressions.
4606 */
4607 ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds);
4608 if (ret_code != LTTNG_OK) {
4609 ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d",
4610 trigger_name, (int) trigger_owner, ret_code);
4611 goto end;
4612 }
4613
4614 /*
4615 * A reference to the trigger is acquired by the notification thread.
4616 * It is safe to return the same trigger to the caller since it the
4617 * other user holds a reference.
4618 *
4619 * The trigger is modified during the execution of the
4620 * "register trigger" command. However, by the time the command returns,
4621 * it is safe to use without any locking as its properties are
4622 * immutable.
4623 */
4624 ret_code = notification_thread_command_register_trigger(
4625 notification_thread, trigger, is_trigger_anonymous);
4626 if (ret_code != LTTNG_OK) {
4627 DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4628 trigger_name, (int) trigger_owner, ret_code);
4629 goto end;
4630 }
4631
4632 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4633 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4634 trigger_name : "(anonymous)";
4635
4636 /*
4637 * Synchronize tracers if the trigger adds an event notifier.
4638 */
4639 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4640 ret_code = synchronize_tracer_notifier_register(notification_thread,
4641 trigger, cmd_creds);
4642 if (ret_code != LTTNG_OK) {
4643 ERR("Error registering tracer notifier: %s",
4644 lttng_strerror(-ret_code));
4645 goto end;
4646 }
4647 }
4648
4649 /*
4650 * Return an updated trigger to the client.
4651 *
4652 * Since a modified version of the same trigger is returned, acquire a
4653 * reference to the trigger so the caller doesn't have to care if those
4654 * are distinct instances or not.
4655 */
4656 if (ret_code == LTTNG_OK) {
4657 lttng_trigger_get(trigger);
4658 *return_trigger = trigger;
4659 /* Ownership of trigger was transferred to caller. */
4660 trigger = NULL;
4661 }
4662 end:
4663 return ret_code;
4664 }
4665
4666 static
4667 enum lttng_error_code synchronize_tracer_notifier_unregister(
4668 const struct lttng_trigger *trigger)
4669 {
4670 enum lttng_error_code ret_code;
4671 const struct lttng_condition *condition =
4672 lttng_trigger_get_const_condition(trigger);
4673 const enum lttng_domain_type trigger_domain =
4674 lttng_trigger_get_underlying_domain_type_restriction(
4675 trigger);
4676
4677 assert(condition);
4678 assert(lttng_condition_get_type(condition) ==
4679 LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
4680
4681 session_lock_list();
4682 switch (trigger_domain) {
4683 case LTTNG_DOMAIN_KERNEL:
4684 ret_code = kernel_unregister_event_notifier(trigger);
4685 if (ret_code != LTTNG_OK) {
4686 goto end_unlock_session_list;
4687 }
4688
4689 break;
4690 case LTTNG_DOMAIN_UST:
4691 ust_app_global_update_all_event_notifier_rules();
4692 break;
4693 case LTTNG_DOMAIN_JUL:
4694 case LTTNG_DOMAIN_LOG4J:
4695 case LTTNG_DOMAIN_PYTHON:
4696 {
4697 /* Agent domains. */
4698 struct agent *agt = agent_find_by_event_notifier_domain(
4699 trigger_domain);
4700
4701 /*
4702 * This trigger was never registered in the first place. Calling
4703 * this function under those circumstances is an internal error.
4704 */
4705 assert(agt);
4706 ret_code = trigger_agent_disable(trigger, agt);
4707 if (ret_code != LTTNG_OK) {
4708 goto end_unlock_session_list;
4709 }
4710
4711 break;
4712 }
4713 case LTTNG_DOMAIN_NONE:
4714 default:
4715 abort();
4716 }
4717
4718 ret_code = LTTNG_OK;
4719
4720 end_unlock_session_list:
4721 session_unlock_list();
4722 return ret_code;
4723 }
4724
4725 enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds,
4726 const struct lttng_trigger *trigger,
4727 struct notification_thread_handle *notification_thread)
4728 {
4729 enum lttng_error_code ret_code;
4730 const char *trigger_name;
4731 uid_t trigger_owner;
4732 enum lttng_trigger_status trigger_status;
4733 struct lttng_trigger *sessiond_trigger = NULL;
4734
4735 trigger_status = lttng_trigger_get_name(trigger, &trigger_name);
4736 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(anonymous)";
4737 trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner);
4738 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4739
4740 DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4741 trigger_name, (int) trigger_owner,
4742 (int) lttng_credentials_get_uid(cmd_creds));
4743
4744 /*
4745 * Validate the trigger credentials against the command credentials.
4746 * Only the root user can unregister a trigger with non-matching
4747 * credentials.
4748 */
4749 if (!lttng_credentials_is_equal_uid(
4750 lttng_trigger_get_credentials(trigger),
4751 cmd_creds)) {
4752 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4753 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4754 trigger_name, (int) trigger_owner,
4755 (int) lttng_credentials_get_uid(cmd_creds));
4756 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4757 goto end;
4758 }
4759 }
4760
4761 /* Fetch the sessiond side trigger object. */
4762 ret_code = notification_thread_command_get_trigger(
4763 notification_thread, trigger, &sessiond_trigger);
4764 if (ret_code != LTTNG_OK) {
4765 DBG("Failed to get trigger from notification thread during unregister: trigger name = '%s', trigger owner uid = %d, error code = %d",
4766 trigger_name, (int) trigger_owner, ret_code);
4767 goto end;
4768 }
4769
4770 assert(sessiond_trigger);
4771
4772 /*
4773 * From this point on, no matter what, consider the trigger
4774 * unregistered.
4775 *
4776 * We set the unregistered state of the sessiond side trigger object in
4777 * the client thread since we want to minimize the possibility of the
4778 * notification thread being stalled due to a long execution of an
4779 * action that required the trigger lock.
4780 */
4781 lttng_trigger_set_as_unregistered(sessiond_trigger);
4782
4783 ret_code = notification_thread_command_unregister_trigger(notification_thread,
4784 trigger);
4785 if (ret_code != LTTNG_OK) {
4786 DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d",
4787 trigger_name, (int) trigger_owner, ret_code);
4788 goto end;
4789 }
4790
4791 /*
4792 * Synchronize tracers if the trigger removes an event notifier.
4793 * Do this even if the trigger unregistration failed to at least stop
4794 * the tracers from producing notifications associated with this
4795 * event notifier.
4796 */
4797 if (lttng_trigger_needs_tracer_notifier(trigger)) {
4798 ret_code = synchronize_tracer_notifier_unregister(trigger);
4799 if (ret_code != LTTNG_OK) {
4800 ERR("Error unregistering trigger to tracer.");
4801 goto end;
4802 }
4803
4804 }
4805
4806 end:
4807 lttng_trigger_put(sessiond_trigger);
4808 return ret_code;
4809 }
4810
4811 enum lttng_error_code cmd_list_triggers(struct command_ctx *cmd_ctx,
4812 struct notification_thread_handle *notification_thread,
4813 struct lttng_triggers **return_triggers)
4814 {
4815 int ret;
4816 enum lttng_error_code ret_code;
4817 struct lttng_triggers *triggers = NULL;
4818
4819 /* Get the set of triggers from the notification thread. */
4820 ret_code = notification_thread_command_list_triggers(
4821 notification_thread, cmd_ctx->creds.uid, &triggers);
4822 if (ret_code != LTTNG_OK) {
4823 goto end;
4824 }
4825
4826 ret = lttng_triggers_remove_hidden_triggers(triggers);
4827 if (ret) {
4828 ret_code = LTTNG_ERR_UNK;
4829 goto end;
4830 }
4831
4832 *return_triggers = triggers;
4833 triggers = NULL;
4834 ret_code = LTTNG_OK;
4835 end:
4836 lttng_triggers_destroy(triggers);
4837 return ret_code;
4838 }
4839
4840 enum lttng_error_code cmd_execute_error_query(const struct lttng_credentials *cmd_creds,
4841 const struct lttng_error_query *query,
4842 struct lttng_error_query_results **_results,
4843 struct notification_thread_handle *notification_thread)
4844 {
4845 enum lttng_error_code ret_code;
4846 const struct lttng_trigger *query_target_trigger;
4847 const struct lttng_action *query_target_action = NULL;
4848 struct lttng_trigger *matching_trigger = NULL;
4849 const char *trigger_name;
4850 uid_t trigger_owner;
4851 enum lttng_trigger_status trigger_status;
4852 struct lttng_error_query_results *results = NULL;
4853
4854 switch (lttng_error_query_get_target_type(query)) {
4855 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4856 query_target_trigger = lttng_error_query_trigger_borrow_target(query);
4857 break;
4858 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4859 query_target_trigger =
4860 lttng_error_query_condition_borrow_target(query);
4861 break;
4862 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4863 query_target_trigger = lttng_error_query_action_borrow_trigger_target(
4864 query);
4865 break;
4866 default:
4867 abort();
4868 }
4869
4870 assert(query_target_trigger);
4871
4872 ret_code = notification_thread_command_get_trigger(notification_thread,
4873 query_target_trigger, &matching_trigger);
4874 if (ret_code != LTTNG_OK) {
4875 goto end;
4876 }
4877
4878 /* No longer needed. */
4879 query_target_trigger = NULL;
4880
4881 if (lttng_error_query_get_target_type(query) ==
4882 LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION) {
4883 /* Get the sessiond-side version of the target action. */
4884 query_target_action =
4885 lttng_error_query_action_borrow_action_target(
4886 query, matching_trigger);
4887 }
4888
4889 trigger_status = lttng_trigger_get_name(matching_trigger, &trigger_name);
4890 trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ?
4891 trigger_name : "(anonymous)";
4892 trigger_status = lttng_trigger_get_owner_uid(matching_trigger,
4893 &trigger_owner);
4894 assert(trigger_status == LTTNG_TRIGGER_STATUS_OK);
4895
4896 results = lttng_error_query_results_create();
4897 if (!results) {
4898 ret_code = LTTNG_ERR_NOMEM;
4899 goto end;
4900 }
4901
4902 DBG("Running \"execute error query\" command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4903 trigger_name, (int) trigger_owner,
4904 (int) lttng_credentials_get_uid(cmd_creds));
4905
4906 /*
4907 * Validate the trigger credentials against the command credentials.
4908 * Only the root user can target a trigger with non-matching
4909 * credentials.
4910 */
4911 if (!lttng_credentials_is_equal_uid(
4912 lttng_trigger_get_credentials(matching_trigger),
4913 cmd_creds)) {
4914 if (lttng_credentials_get_uid(cmd_creds) != 0) {
4915 ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d",
4916 trigger_name, (int) trigger_owner,
4917 (int) lttng_credentials_get_uid(cmd_creds));
4918 ret_code = LTTNG_ERR_INVALID_TRIGGER;
4919 goto end;
4920 }
4921 }
4922
4923 switch (lttng_error_query_get_target_type(query)) {
4924 case LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER:
4925 trigger_status = lttng_trigger_add_error_results(
4926 matching_trigger, results);
4927
4928 switch (trigger_status) {
4929 case LTTNG_TRIGGER_STATUS_OK:
4930 break;
4931 default:
4932 ret_code = LTTNG_ERR_UNK;
4933 goto end;
4934 }
4935
4936 break;
4937 case LTTNG_ERROR_QUERY_TARGET_TYPE_CONDITION:
4938 {
4939 trigger_status = lttng_trigger_condition_add_error_results(
4940 matching_trigger, results);
4941
4942 switch (trigger_status) {
4943 case LTTNG_TRIGGER_STATUS_OK:
4944 break;
4945 default:
4946 ret_code = LTTNG_ERR_UNK;
4947 goto end;
4948 }
4949
4950 break;
4951 }
4952 case LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION:
4953 {
4954 const enum lttng_action_status action_status =
4955 lttng_action_add_error_query_results(
4956 query_target_action, results);
4957
4958 switch (action_status) {
4959 case LTTNG_ACTION_STATUS_OK:
4960 break;
4961 default:
4962 ret_code = LTTNG_ERR_UNK;
4963 goto end;
4964 }
4965
4966 break;
4967 }
4968 default:
4969 abort();
4970 break;
4971 }
4972
4973 *_results = results;
4974 results = NULL;
4975 ret_code = LTTNG_OK;
4976 end:
4977 lttng_trigger_put(matching_trigger);
4978 lttng_error_query_results_destroy(results);
4979 return ret_code;
4980 }
4981
4982 /*
4983 * Send relayd sockets from snapshot output to consumer. Ignore request if the
4984 * snapshot output is *not* set with a remote destination.
4985 *
4986 * Return LTTNG_OK on success or a LTTNG_ERR code.
4987 */
4988 static enum lttng_error_code set_relayd_for_snapshot(
4989 struct consumer_output *output,
4990 const struct ltt_session *session)
4991 {
4992 enum lttng_error_code status = LTTNG_OK;
4993 struct lttng_ht_iter iter;
4994 struct consumer_socket *socket;
4995 LTTNG_OPTIONAL(uint64_t) current_chunk_id = {};
4996 const char *base_path;
4997
4998 assert(output);
4999 assert(session);
5000
5001 DBG2("Set relayd object from snapshot output");
5002
5003 if (session->current_trace_chunk) {
5004 enum lttng_trace_chunk_status chunk_status =
5005 lttng_trace_chunk_get_id(
5006 session->current_trace_chunk,
5007 &current_chunk_id.value);
5008
5009 if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) {
5010 current_chunk_id.is_set = true;
5011 } else {
5012 ERR("Failed to get current trace chunk id");
5013 status = LTTNG_ERR_UNK;
5014 goto error;
5015 }
5016 }
5017
5018 /* Ignore if snapshot consumer output is not network. */
5019 if (output->type != CONSUMER_DST_NET) {
5020 goto error;
5021 }
5022
5023 /*
5024 * The snapshot record URI base path overrides the session
5025 * base path.
5026 */
5027 if (output->dst.net.control.subdir[0] != '\0') {
5028 base_path = output->dst.net.control.subdir;
5029 } else {
5030 base_path = session->base_path;
5031 }
5032
5033 /*
5034 * For each consumer socket, create and send the relayd object of the
5035 * snapshot output.
5036 */
5037 rcu_read_lock();
5038 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
5039 socket, node.node) {
5040 pthread_mutex_lock(socket->lock);
5041 status = send_consumer_relayd_sockets(0, session->id,
5042 output, socket,
5043 session->name, session->hostname,
5044 base_path,
5045 session->live_timer,
5046 current_chunk_id.is_set ? &current_chunk_id.value : NULL,
5047 session->creation_time,
5048 session->name_contains_creation_time);
5049 pthread_mutex_unlock(socket->lock);
5050 if (status != LTTNG_OK) {
5051 rcu_read_unlock();
5052 goto error;
5053 }
5054 }
5055 rcu_read_unlock();
5056
5057 error:
5058 return status;
5059 }
5060
5061 /*
5062 * Record a kernel snapshot.
5063 *
5064 * Return LTTNG_OK on success or a LTTNG_ERR code.
5065 */
5066 static enum lttng_error_code record_kernel_snapshot(
5067 struct ltt_kernel_session *ksess,
5068 const struct consumer_output *output,
5069 const struct ltt_session *session,
5070 int wait, uint64_t nb_packets_per_stream)
5071 {
5072 enum lttng_error_code status;
5073
5074 assert(ksess);
5075 assert(output);
5076 assert(session);
5077
5078 status = kernel_snapshot_record(
5079 ksess, output, wait, nb_packets_per_stream);
5080 return status;
5081 }
5082
5083 /*
5084 * Record a UST snapshot.
5085 *
5086 * Returns LTTNG_OK on success or a LTTNG_ERR error code.
5087 */
5088 static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess,
5089 const struct consumer_output *output,
5090 const struct ltt_session *session,
5091 int wait, uint64_t nb_packets_per_stream)
5092 {
5093 enum lttng_error_code status;
5094
5095 assert(usess);
5096 assert(output);
5097 assert(session);
5098
5099 status = ust_app_snapshot_record(
5100 usess, output, wait, nb_packets_per_stream);
5101 return status;
5102 }
5103
5104 static
5105 uint64_t get_session_size_one_more_packet_per_stream(
5106 const struct ltt_session *session, uint64_t cur_nr_packets)
5107 {
5108 uint64_t tot_size = 0;
5109
5110 if (session->kernel_session) {
5111 struct ltt_kernel_channel *chan;
5112 const struct ltt_kernel_session *ksess =
5113 session->kernel_session;
5114
5115 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
5116 if (cur_nr_packets >= chan->channel->attr.num_subbuf) {
5117 /*
5118 * Don't take channel into account if we
5119 * already grab all its packets.
5120 */
5121 continue;
5122 }
5123 tot_size += chan->channel->attr.subbuf_size
5124 * chan->stream_count;
5125 }
5126 }
5127
5128 if (session->ust_session) {
5129 const struct ltt_ust_session *usess = session->ust_session;
5130
5131 tot_size += ust_app_get_size_one_more_packet_per_stream(usess,
5132 cur_nr_packets);
5133 }
5134
5135 return tot_size;
5136 }
5137
5138 /*
5139 * Calculate the number of packets we can grab from each stream that
5140 * fits within the overall snapshot max size.
5141 *
5142 * Returns -1 on error, 0 means infinite number of packets, else > 0 is
5143 * the number of packets per stream.
5144 *
5145 * TODO: this approach is not perfect: we consider the worse case
5146 * (packet filling the sub-buffers) as an upper bound, but we could do
5147 * better if we do this calculation while we actually grab the packet
5148 * content: we would know how much padding we don't actually store into
5149 * the file.
5150 *
5151 * This algorithm is currently bounded by the number of packets per
5152 * stream.
5153 *
5154 * Since we call this algorithm before actually grabbing the data, it's
5155 * an approximation: for instance, applications could appear/disappear
5156 * in between this call and actually grabbing data.
5157 */
5158 static
5159 int64_t get_session_nb_packets_per_stream(const struct ltt_session *session,
5160 uint64_t max_size)
5161 {
5162 int64_t size_left;
5163 uint64_t cur_nb_packets = 0;
5164
5165 if (!max_size) {
5166 return 0; /* Infinite */
5167 }
5168
5169 size_left = max_size;
5170 for (;;) {
5171 uint64_t one_more_packet_tot_size;
5172
5173 one_more_packet_tot_size = get_session_size_one_more_packet_per_stream(
5174 session, cur_nb_packets);
5175 if (!one_more_packet_tot_size) {
5176 /* We are already grabbing all packets. */
5177 break;
5178 }
5179 size_left -= one_more_packet_tot_size;
5180 if (size_left < 0) {
5181 break;
5182 }
5183 cur_nb_packets++;
5184 }
5185 if (!cur_nb_packets && size_left != max_size) {
5186 /* Not enough room to grab one packet of each stream, error. */
5187 return -1;
5188 }
5189 return cur_nb_packets;
5190 }
5191
5192 static
5193 enum lttng_error_code snapshot_record(struct ltt_session *session,
5194 const struct snapshot_output *snapshot_output, int wait)
5195 {
5196 int64_t nb_packets_per_stream;
5197 char snapshot_chunk_name[LTTNG_NAME_MAX];
5198 int ret;
5199 enum lttng_error_code ret_code = LTTNG_OK;
5200 struct lttng_trace_chunk *snapshot_trace_chunk;
5201 struct consumer_output *original_ust_consumer_output = NULL;
5202 struct consumer_output *original_kernel_consumer_output = NULL;
5203 struct consumer_output *snapshot_ust_consumer_output = NULL;
5204 struct consumer_output *snapshot_kernel_consumer_output = NULL;
5205
5206 ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name),
5207 "%s-%s-%" PRIu64,
5208 snapshot_output->name,
5209 snapshot_output->datetime,
5210 snapshot_output->nb_snapshot);
5211 if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) {
5212 ERR("Failed to format snapshot name");
5213 ret_code = LTTNG_ERR_INVALID;
5214 goto error;
5215 }
5216 DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"",
5217 snapshot_output->name, session->name,
5218 snapshot_chunk_name);
5219 if (!session->kernel_session && !session->ust_session) {
5220 ERR("Failed to record snapshot as no channels exist");
5221 ret_code = LTTNG_ERR_NO_CHANNEL;
5222 goto error;
5223 }
5224
5225 if (session->kernel_session) {
5226 original_kernel_consumer_output =
5227 session->kernel_session->consumer;
5228 snapshot_kernel_consumer_output =
5229 consumer_copy_output(snapshot_output->consumer);
5230 strcpy(snapshot_kernel_consumer_output->chunk_path,
5231 snapshot_chunk_name);
5232
5233 /* Copy the original domain subdir. */
5234 strcpy(snapshot_kernel_consumer_output->domain_subdir,
5235 original_kernel_consumer_output->domain_subdir);
5236
5237 ret = consumer_copy_sockets(snapshot_kernel_consumer_output,
5238 original_kernel_consumer_output);
5239 if (ret < 0) {
5240 ERR("Failed to copy consumer sockets from snapshot output configuration");
5241 ret_code = LTTNG_ERR_NOMEM;
5242 goto error;
5243 }
5244 ret_code = set_relayd_for_snapshot(
5245 snapshot_kernel_consumer_output, session);
5246 if (ret_code != LTTNG_OK) {
5247 ERR("Failed to setup relay daemon for kernel tracer snapshot");
5248 goto error;
5249 }
5250 session->kernel_session->consumer =
5251 snapshot_kernel_consumer_output;
5252 }
5253 if (session->ust_session) {
5254 original_ust_consumer_output = session->ust_session->consumer;
5255 snapshot_ust_consumer_output =
5256 consumer_copy_output(snapshot_output->consumer);
5257 strcpy(snapshot_ust_consumer_output->chunk_path,
5258 snapshot_chunk_name);
5259
5260 /* Copy the original domain subdir. */
5261 strcpy(snapshot_ust_consumer_output->domain_subdir,
5262 original_ust_consumer_output->domain_subdir);
5263
5264 ret = consumer_copy_sockets(snapshot_ust_consumer_output,
5265 original_ust_consumer_output);
5266 if (ret < 0) {
5267 ERR("Failed to copy consumer sockets from snapshot output configuration");
5268 ret_code = LTTNG_ERR_NOMEM;
5269 goto error;
5270 }
5271 ret_code = set_relayd_for_snapshot(
5272 snapshot_ust_consumer_output, session);
5273 if (ret_code != LTTNG_OK) {
5274 ERR("Failed to setup relay daemon for userspace tracer snapshot");
5275 goto error;
5276 }
5277 session->ust_session->consumer =
5278 snapshot_ust_consumer_output;
5279 }
5280
5281 snapshot_trace_chunk = session_create_new_trace_chunk(session,
5282 snapshot_kernel_consumer_output ?:
5283 snapshot_ust_consumer_output,
5284 consumer_output_get_base_path(
5285 snapshot_output->consumer),
5286 snapshot_chunk_name);
5287 if (!snapshot_trace_chunk) {
5288 ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"",
5289 session->name);
5290 ret_code = LTTNG_ERR_CREATE_DIR_FAIL;
5291 goto error;
5292 }
5293 assert(!session->current_trace_chunk);
5294 ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL);
5295 lttng_trace_chunk_put(snapshot_trace_chunk);
5296 snapshot_trace_chunk = NULL;
5297 if (ret) {
5298 ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"",
5299 session->name);
5300 ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5301 goto error;
5302 }
5303
5304 nb_packets_per_stream = get_session_nb_packets_per_stream(session,
5305 snapshot_output->max_size);
5306 if (nb_packets_per_stream < 0) {
5307 ret_code = LTTNG_ERR_MAX_SIZE_INVALID;
5308 goto error_close_trace_chunk;
5309 }
5310
5311 if (session->kernel_session) {
5312 ret_code = record_kernel_snapshot(session->kernel_session,
5313 snapshot_kernel_consumer_output, session,
5314 wait, nb_packets_per_stream);
5315 if (ret_code != LTTNG_OK) {
5316 goto error_close_trace_chunk;
5317 }
5318 }
5319
5320 if (session->ust_session) {
5321 ret_code = record_ust_snapshot(session->ust_session,
5322 snapshot_ust_consumer_output, session,
5323 wait, nb_packets_per_stream);
5324 if (ret_code != LTTNG_OK) {
5325 goto error_close_trace_chunk;
5326 }
5327 }
5328
5329 error_close_trace_chunk:
5330 if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) {
5331 ERR("Failed to release the current trace chunk of session \"%s\"",
5332 session->name);
5333 ret_code = LTTNG_ERR_UNK;
5334 }
5335
5336 if (session_close_trace_chunk(session, snapshot_trace_chunk,
5337 LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) {
5338 /*
5339 * Don't goto end; make sure the chunk is closed for the session
5340 * to allow future snapshots.
5341 */
5342 ERR("Failed to close snapshot trace chunk of session \"%s\"",
5343 session->name);
5344 ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5345 }
5346
5347 lttng_trace_chunk_put(snapshot_trace_chunk);
5348 snapshot_trace_chunk = NULL;
5349 error:
5350 if (original_ust_consumer_output) {
5351 session->ust_session->consumer = original_ust_consumer_output;
5352 }
5353 if (original_kernel_consumer_output) {
5354 session->kernel_session->consumer =
5355 original_kernel_consumer_output;
5356 }
5357 consumer_output_put(snapshot_ust_consumer_output);
5358 consumer_output_put(snapshot_kernel_consumer_output);
5359 return ret_code;
5360 }
5361
5362 /*
5363 * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl.
5364 *
5365 * The wait parameter is ignored so this call always wait for the snapshot to
5366 * complete before returning.
5367 *
5368 * Return LTTNG_OK on success or else a LTTNG_ERR code.
5369 */
5370 int cmd_snapshot_record(struct ltt_session *session,
5371 const struct lttng_snapshot_output *output, int wait)
5372 {
5373 enum lttng_error_code cmd_ret = LTTNG_OK;
5374 int ret;
5375 unsigned int snapshot_success = 0;
5376 char datetime[16];
5377 struct snapshot_output *tmp_output = NULL;
5378
5379 assert(session);
5380 assert(output);
5381
5382 DBG("Cmd snapshot record for session %s", session->name);
5383
5384 /* Get the datetime for the snapshot output directory. */
5385 ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime,
5386 sizeof(datetime));
5387 if (!ret) {
5388 cmd_ret = LTTNG_ERR_INVALID;
5389 goto error;
5390 }
5391
5392 /*
5393 * Permission denied to create an output if the session is not
5394 * set in no output mode.
5395 */
5396 if (session->output_traces) {
5397 cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION;
5398 goto error;
5399 }
5400
5401 /* The session needs to be started at least once. */
5402 if (!session->has_been_started) {
5403 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5404 goto error;
5405 }
5406
5407 /* Use temporary output for the session. */
5408 if (*output->ctrl_url != '\0') {
5409 tmp_output = snapshot_output_alloc();
5410 if (!tmp_output) {
5411 cmd_ret = LTTNG_ERR_NOMEM;
5412 goto error;
5413 }
5414
5415 ret = snapshot_output_init(session, output->max_size,
5416 output->name,
5417 output->ctrl_url, output->data_url,
5418 session->consumer,
5419 tmp_output, NULL);
5420 if (ret < 0) {
5421 if (ret == -ENOMEM) {
5422 cmd_ret = LTTNG_ERR_NOMEM;
5423 } else {
5424 cmd_ret = LTTNG_ERR_INVALID;
5425 }
5426 goto error;
5427 }
5428 /* Use the global session count for the temporary snapshot. */
5429 tmp_output->nb_snapshot = session->snapshot.nb_snapshot;
5430
5431 /* Use the global datetime */
5432 memcpy(tmp_output->datetime, datetime, sizeof(datetime));
5433 cmd_ret = snapshot_record(session, tmp_output, wait);
5434 if (cmd_ret != LTTNG_OK) {
5435 goto error;
5436 }
5437 snapshot_success = 1;
5438 } else {
5439 struct snapshot_output *sout;
5440 struct lttng_ht_iter iter;
5441
5442 rcu_read_lock();
5443 cds_lfht_for_each_entry(session->snapshot.output_ht->ht,
5444 &iter.iter, sout, node.node) {
5445 struct snapshot_output output_copy;
5446
5447 /*
5448 * Make a local copy of the output and override output
5449 * parameters with those provided as part of the
5450 * command.
5451 */
5452 memcpy(&output_copy, sout, sizeof(output_copy));
5453
5454 if (output->max_size != (uint64_t) -1ULL) {
5455 output_copy.max_size = output->max_size;
5456 }
5457
5458 output_copy.nb_snapshot = session->snapshot.nb_snapshot;
5459 memcpy(output_copy.datetime, datetime,
5460 sizeof(datetime));
5461
5462 /* Use temporary name. */
5463 if (*output->name != '\0') {
5464 if (lttng_strncpy(output_copy.name,
5465 output->name,
5466 sizeof(output_copy.name))) {
5467 cmd_ret = LTTNG_ERR_INVALID;
5468 rcu_read_unlock();
5469 goto error;
5470 }
5471 }
5472
5473 cmd_ret = snapshot_record(session, &output_copy, wait);
5474 if (cmd_ret != LTTNG_OK) {
5475 rcu_read_unlock();
5476 goto error;
5477 }
5478 snapshot_success = 1;
5479 }
5480 rcu_read_unlock();
5481 }
5482
5483 if (snapshot_success) {
5484 session->snapshot.nb_snapshot++;
5485 } else {
5486 cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL;
5487 }
5488
5489 error:
5490 if (tmp_output) {
5491 snapshot_output_destroy(tmp_output);
5492 }
5493 return cmd_ret;
5494 }
5495
5496 /*
5497 * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread.
5498 */
5499 int cmd_set_session_shm_path(struct ltt_session *session,
5500 const char *shm_path)
5501 {
5502 /* Safety net */
5503 assert(session);
5504
5505 /*
5506 * Can only set shm path before session is started.
5507 */
5508 if (session->has_been_started) {
5509 return LTTNG_ERR_SESSION_STARTED;
5510 }
5511
5512 strncpy(session->shm_path, shm_path,
5513 sizeof(session->shm_path));
5514 session->shm_path[sizeof(session->shm_path) - 1] = '\0';
5515
5516 return LTTNG_OK;
5517 }
5518
5519 /*
5520 * Command LTTNG_ROTATE_SESSION from the lttng-ctl library.
5521 *
5522 * Ask the consumer to rotate the session output directory.
5523 * The session lock must be held.
5524 *
5525 * Returns LTTNG_OK on success or else a negative LTTng error code.
5526 */
5527 int cmd_rotate_session(struct ltt_session *session,
5528 struct lttng_rotate_session_return *rotate_return,
5529 bool quiet_rotation,
5530 enum lttng_trace_chunk_command_type command)
5531 {
5532 int ret;
5533 uint64_t ongoing_rotation_chunk_id;
5534 enum lttng_error_code cmd_ret = LTTNG_OK;
5535 struct lttng_trace_chunk *chunk_being_archived = NULL;
5536 struct lttng_trace_chunk *new_trace_chunk = NULL;
5537 enum lttng_trace_chunk_status chunk_status;
5538 bool failed_to_rotate = false;
5539 enum lttng_error_code rotation_fail_code = LTTNG_OK;
5540
5541 assert(session);
5542
5543 if (!session->has_been_started) {
5544 cmd_ret = LTTNG_ERR_START_SESSION_ONCE;
5545 goto end;
5546 }
5547
5548 /*
5549 * Explicit rotation is not supported for live sessions.
5550 * However, live sessions can perform a quiet rotation on
5551 * destroy.
5552 * Rotation is not supported for snapshot traces (no output).
5553 */
5554 if ((!quiet_rotation && session->live_timer) ||
5555 !session->output_traces) {
5556 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5557 goto end;
5558 }
5559
5560 /* Unsupported feature in lttng-relayd before 2.11. */
5561 if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET &&
5562 (session->consumer->relay_major_version == 2 &&
5563 session->consumer->relay_minor_version < 11)) {
5564 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY;
5565 goto end;
5566 }
5567
5568 /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */
5569 if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) {
5570 cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL;
5571 goto end;
5572 }
5573
5574 if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) {
5575 DBG("Refusing to launch a rotation; a rotation is already in progress for session %s",
5576 session->name);
5577 cmd_ret = LTTNG_ERR_ROTATION_PENDING;
5578 goto end;
5579 }
5580
5581 /*
5582 * After a stop, we only allow one rotation to occur, the other ones are
5583 * useless until a new start.
5584 */
5585 if (session->rotated_after_last_stop) {
5586 DBG("Session \"%s\" was already rotated after stop, refusing rotation",
5587 session->name);
5588 cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP;
5589 goto end;
5590 }
5591
5592 /*
5593 * After a stop followed by a clear, disallow following rotations a they would
5594 * generate empty chunks.
5595 */
5596 if (session->cleared_after_last_stop) {
5597 DBG("Session \"%s\" was already cleared after stop, refusing rotation",
5598 session->name);
5599 cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR;
5600 goto end;
5601 }
5602
5603 if (session->active) {
5604 new_trace_chunk = session_create_new_trace_chunk(session, NULL,
5605 NULL, NULL);
5606 if (!new_trace_chunk) {
5607 cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL;
5608 goto error;
5609 }
5610 }
5611
5612 /*
5613 * The current trace chunk becomes the chunk being archived.
5614 *
5615 * After this point, "chunk_being_archived" must absolutely
5616 * be closed on the consumer(s), otherwise it will never be
5617 * cleaned-up, which will result in a leak.
5618 */
5619 ret = session_set_trace_chunk(session, new_trace_chunk,
5620 &chunk_being_archived);
5621 if (ret) {
5622 cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER;
5623 goto error;
5624 }
5625
5626 if (session->kernel_session) {
5627 cmd_ret = kernel_rotate_session(session);
5628 if (cmd_ret != LTTNG_OK) {
5629 failed_to_rotate = true;
5630 rotation_fail_code = cmd_ret;
5631 }
5632 }
5633 if (session->ust_session) {
5634 cmd_ret = ust_app_rotate_session(session);
5635 if (cmd_ret != LTTNG_OK) {
5636 failed_to_rotate = true;
5637 rotation_fail_code = cmd_ret;
5638 }
5639 }
5640
5641 if (!session->active) {
5642 session->rotated_after_last_stop = true;
5643 }
5644
5645 if (!chunk_being_archived) {
5646 DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check",
5647 session->name);
5648 if (failed_to_rotate) {
5649 cmd_ret = rotation_fail_code;
5650 goto error;
5651 }
5652 cmd_ret = LTTNG_OK;
5653 goto end;
5654 }
5655
5656 session->rotation_state = LTTNG_ROTATION_STATE_ONGOING;
5657 chunk_status = lttng_trace_chunk_get_id(chunk_being_archived,
5658 &ongoing_rotation_chunk_id);
5659 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5660
5661 ret = session_close_trace_chunk(session, chunk_being_archived,
5662 command, session->last_chunk_path);
5663 if (ret) {
5664 cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER;
5665 goto error;
5666 }
5667
5668 if (failed_to_rotate) {
5669 cmd_ret = rotation_fail_code;
5670 goto error;
5671 }
5672
5673 session->quiet_rotation = quiet_rotation;
5674 ret = timer_session_rotation_pending_check_start(session,
5675 DEFAULT_ROTATE_PENDING_TIMER);
5676 if (ret) {
5677 cmd_ret = LTTNG_ERR_UNK;
5678 goto error;
5679 }
5680
5681 if (rotate_return) {
5682 rotate_return->rotation_id = ongoing_rotation_chunk_id;
5683 }
5684
5685 session->chunk_being_archived = chunk_being_archived;
5686 chunk_being_archived = NULL;
5687 if (!quiet_rotation) {
5688 ret = notification_thread_command_session_rotation_ongoing(
5689 the_notification_thread_handle, session->name,
5690 session->uid, session->gid,
5691 ongoing_rotation_chunk_id);
5692 if (ret != LTTNG_OK) {
5693 ERR("Failed to notify notification thread that a session rotation is ongoing for session %s",
5694 session->name);
5695 cmd_ret = ret;
5696 }
5697 }
5698
5699 DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent",
5700 session->name, ongoing_rotation_chunk_id);
5701 end:
5702 lttng_trace_chunk_put(new_trace_chunk);
5703 lttng_trace_chunk_put(chunk_being_archived);
5704 ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret);
5705 return ret;
5706 error:
5707 if (session_reset_rotation_state(session,
5708 LTTNG_ROTATION_STATE_ERROR)) {
5709 ERR("Failed to reset rotation state of session \"%s\"",
5710 session->name);
5711 }
5712 goto end;
5713 }
5714
5715 /*
5716 * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library.
5717 *
5718 * Check if the session has finished its rotation.
5719 *
5720 * Return LTTNG_OK on success or else an LTTNG_ERR code.
5721 */
5722 int cmd_rotate_get_info(struct ltt_session *session,
5723 struct lttng_rotation_get_info_return *info_return,
5724 uint64_t rotation_id)
5725 {
5726 enum lttng_error_code cmd_ret = LTTNG_OK;
5727 enum lttng_rotation_state rotation_state;
5728
5729 DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name,
5730 session->most_recent_chunk_id.value);
5731
5732 if (session->chunk_being_archived) {
5733 enum lttng_trace_chunk_status chunk_status;
5734 uint64_t chunk_id;
5735
5736 chunk_status = lttng_trace_chunk_get_id(
5737 session->chunk_being_archived,
5738 &chunk_id);
5739 assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK);
5740
5741 rotation_state = rotation_id == chunk_id ?
5742 LTTNG_ROTATION_STATE_ONGOING :
5743 LTTNG_ROTATION_STATE_EXPIRED;
5744 } else {
5745 if (session->last_archived_chunk_id.is_set &&
5746 rotation_id != session->last_archived_chunk_id.value) {
5747 rotation_state = LTTNG_ROTATION_STATE_EXPIRED;
5748 } else {
5749 rotation_state = session->rotation_state;
5750 }
5751 }
5752
5753 switch (rotation_state) {
5754 case LTTNG_ROTATION_STATE_NO_ROTATION:
5755 DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"",
5756 session->name);
5757 goto end;
5758 case LTTNG_ROTATION_STATE_EXPIRED:
5759 DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired",
5760 rotation_id, session->name);
5761 break;
5762 case LTTNG_ROTATION_STATE_ONGOING:
5763 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending",
5764 rotation_id, session->name);
5765 break;
5766 case LTTNG_ROTATION_STATE_COMPLETED:
5767 {
5768 int fmt_ret;
5769 char *chunk_path;
5770 char *current_tracing_path_reply;
5771 size_t current_tracing_path_reply_len;
5772
5773 DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed",
5774 rotation_id, session->name);
5775
5776 switch (session_get_consumer_destination_type(session)) {
5777 case CONSUMER_DST_LOCAL:
5778 current_tracing_path_reply =
5779 info_return->location.local.absolute_path;
5780 current_tracing_path_reply_len =
5781 sizeof(info_return->location.local.absolute_path);
5782 info_return->location_type =
5783 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL;
5784 fmt_ret = asprintf(&chunk_path,
5785 "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s",
5786 session_get_base_path(session),
5787 session->last_archived_chunk_name);
5788 if (fmt_ret == -1) {
5789 PERROR("Failed to format the path of the last archived trace chunk");
5790 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5791 cmd_ret = LTTNG_ERR_UNK;
5792 goto end;
5793 }
5794 break;
5795 case CONSUMER_DST_NET:
5796 {
5797 uint16_t ctrl_port, data_port;
5798
5799 current_tracing_path_reply =
5800 info_return->location.relay.relative_path;
5801 current_tracing_path_reply_len =
5802 sizeof(info_return->location.relay.relative_path);
5803 /* Currently the only supported relay protocol. */
5804 info_return->location.relay.protocol =
5805 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP;
5806
5807 fmt_ret = lttng_strncpy(info_return->location.relay.host,
5808 session_get_net_consumer_hostname(session),
5809 sizeof(info_return->location.relay.host));
5810 if (fmt_ret) {
5811 ERR("Failed to copy host name to rotate_get_info reply");
5812 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5813 cmd_ret = LTTNG_ERR_SET_URL;
5814 goto end;
5815 }
5816
5817 session_get_net_consumer_ports(session, &ctrl_port, &data_port);
5818 info_return->location.relay.ports.control = ctrl_port;
5819 info_return->location.relay.ports.data = data_port;
5820 info_return->location_type =
5821 (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY;
5822 chunk_path = strdup(session->last_chunk_path);
5823 if (!chunk_path) {
5824 ERR("Failed to allocate the path of the last archived trace chunk");
5825 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5826 cmd_ret = LTTNG_ERR_UNK;
5827 goto end;
5828 }
5829 break;
5830 }
5831 default:
5832 abort();
5833 }
5834
5835 fmt_ret = lttng_strncpy(current_tracing_path_reply,
5836 chunk_path, current_tracing_path_reply_len);
5837 free(chunk_path);
5838 if (fmt_ret) {
5839 ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply");
5840 info_return->status = LTTNG_ROTATION_STATUS_ERROR;
5841 cmd_ret = LTTNG_ERR_UNK;
5842 goto end;
5843 }
5844
5845 break;
5846 }
5847 case LTTNG_ROTATION_STATE_ERROR:
5848 DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"",
5849 rotation_id, session->name);
5850 break;
5851 default:
5852 abort();
5853 }
5854
5855 cmd_ret = LTTNG_OK;
5856 end:
5857 info_return->status = (int32_t) rotation_state;
5858 return cmd_ret;
5859 }
5860
5861 /*
5862 * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library.
5863 *
5864 * Configure the automatic rotation parameters.
5865 * 'activate' to true means activate the rotation schedule type with 'new_value'.
5866 * 'activate' to false means deactivate the rotation schedule and validate that
5867 * 'new_value' has the same value as the currently active value.
5868 *
5869 * Return LTTNG_OK on success or else a positive LTTNG_ERR code.
5870 */
5871 int cmd_rotation_set_schedule(struct ltt_session *session,
5872 bool activate, enum lttng_rotation_schedule_type schedule_type,
5873 uint64_t new_value,
5874 struct notification_thread_handle *notification_thread_handle)
5875 {
5876 int ret;
5877 uint64_t *parameter_value;
5878
5879 assert(session);
5880
5881 DBG("Cmd rotate set schedule session %s", session->name);
5882
5883 if (session->live_timer || !session->output_traces) {
5884 DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session");
5885 ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE;
5886 goto end;
5887 }
5888
5889 switch (schedule_type) {
5890 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5891 parameter_value = &session->rotate_size;
5892 break;
5893 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5894 parameter_value = &session->rotate_timer_period;
5895 if (new_value >= UINT_MAX) {
5896 DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)",
5897 new_value, UINT_MAX);
5898 ret = LTTNG_ERR_INVALID;
5899 goto end;
5900 }
5901 break;
5902 default:
5903 WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type");
5904 ret = LTTNG_ERR_INVALID;
5905 goto end;
5906 }
5907
5908 /* Improper use of the API. */
5909 if (new_value == -1ULL) {
5910 WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1");
5911 ret = LTTNG_ERR_INVALID;
5912 goto end;
5913 }
5914
5915 /*
5916 * As indicated in struct ltt_session's comments, a value of == 0 means
5917 * this schedule rotation type is not in use.
5918 *
5919 * Reject the command if we were asked to activate a schedule that was
5920 * already active.
5921 */
5922 if (activate && *parameter_value != 0) {
5923 DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active");
5924 ret = LTTNG_ERR_ROTATION_SCHEDULE_SET;
5925 goto end;
5926 }
5927
5928 /*
5929 * Reject the command if we were asked to deactivate a schedule that was
5930 * not active.
5931 */
5932 if (!activate && *parameter_value == 0) {
5933 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive");
5934 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5935 goto end;
5936 }
5937
5938 /*
5939 * Reject the command if we were asked to deactivate a schedule that
5940 * doesn't exist.
5941 */
5942 if (!activate && *parameter_value != new_value) {
5943 DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided");
5944 ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET;
5945 goto end;
5946 }
5947
5948 *parameter_value = activate ? new_value : 0;
5949
5950 switch (schedule_type) {
5951 case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC:
5952 if (activate && session->active) {
5953 /*
5954 * Only start the timer if the session is active,
5955 * otherwise it will be started when the session starts.
5956 */
5957 ret = timer_session_rotation_schedule_timer_start(
5958 session, new_value);
5959 if (ret) {
5960 ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command");
5961 ret = LTTNG_ERR_UNK;
5962 goto end;
5963 }
5964 } else {
5965 ret = timer_session_rotation_schedule_timer_stop(
5966 session);
5967 if (ret) {
5968 ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command");
5969 ret = LTTNG_ERR_UNK;
5970 goto end;
5971 }
5972 }
5973 break;
5974 case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD:
5975 if (activate) {
5976 ret = subscribe_session_consumed_size_rotation(session,
5977 new_value, notification_thread_handle);
5978 if (ret) {
5979 ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command");
5980 ret = LTTNG_ERR_UNK;
5981 goto end;
5982 }
5983 } else {
5984 ret = unsubscribe_session_consumed_size_rotation(session,
5985 notification_thread_handle);
5986 if (ret) {
5987 ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command");
5988 ret = LTTNG_ERR_UNK;
5989 goto end;
5990 }
5991
5992 }
5993 break;
5994 default:
5995 /* Would have been caught before. */
5996 abort();
5997 }
5998
5999 ret = LTTNG_OK;
6000
6001 goto end;
6002
6003 end:
6004 return ret;
6005 }
6006
6007 /* Wait for a given path to be removed before continuing. */
6008 static enum lttng_error_code wait_on_path(void *path_data)
6009 {
6010 const char *shm_path = path_data;
6011
6012 DBG("Waiting for the shm path at %s to be removed before completing session destruction",
6013 shm_path);
6014 while (true) {
6015 int ret;
6016 struct stat st;
6017
6018 ret = stat(shm_path, &st);
6019 if (ret) {
6020 if (errno != ENOENT) {
6021 PERROR("stat() returned an error while checking for the existence of the shm path");
6022 } else {
6023 DBG("shm path no longer exists, completing the destruction of session");
6024 }
6025 break;
6026 } else {
6027 if (!S_ISDIR(st.st_mode)) {
6028 ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal",
6029 shm_path);
6030 break;
6031 }
6032 }
6033 usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US);
6034 }
6035 return LTTNG_OK;
6036 }
6037
6038 /*
6039 * Returns a pointer to a handler to run on completion of a command.
6040 * Returns NULL if no handler has to be run for the last command executed.
6041 */
6042 const struct cmd_completion_handler *cmd_pop_completion_handler(void)
6043 {
6044 struct cmd_completion_handler *handler = current_completion_handler;
6045
6046 current_completion_handler = NULL;
6047 return handler;
6048 }
6049
6050 /*
6051 * Init command subsystem.
6052 */
6053 void cmd_init(void)
6054 {
6055 /*
6056 * Set network sequence index to 1 for streams to match a relayd
6057 * socket on the consumer side.
6058 */
6059 pthread_mutex_lock(&relayd_net_seq_idx_lock);
6060 relayd_net_seq_idx = 1;
6061 pthread_mutex_unlock(&relayd_net_seq_idx_lock);
6062
6063 DBG("Command subsystem initialized");
6064 }
This page took 0.252133 seconds and 3 git commands to generate.