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