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