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