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