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