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