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