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