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