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