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