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